org-irc.el 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. ;;; org-irc.el --- Store links to IRC sessions
  2. ;;
  3. ;; Copyright (C) 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Philip Jackson <emacs@shellarchive.co.uk>
  6. ;; Keywords: erc, irc, link, org
  7. ;; Version: 1.4
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 3, or (at your option)
  14. ;; any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  21. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. ;; Boston, MA 02110-1301, USA.
  23. ;;; Commentary:
  24. ;; Link to an IRC session. Only ERC has been implemented at the
  25. ;; moment.
  26. ;;
  27. ;; This file is loaded by default whenever org.el is loaded. Please
  28. ;; customize the variable `org-default-extensions' to select extensions
  29. ;; you would like to use, and to deselect those which you don't want.
  30. ;;
  31. ;; Please note that at the moment only ERC is supported. Other clients
  32. ;; shouldn't be diffficult to add though.
  33. ;;
  34. ;; Then set `org-irc-link-to-logs' to non-nil if you would like a
  35. ;; file:/ type link to be created to the current line in the logs or
  36. ;; to t if you would like to create an irc:/ style link.
  37. ;;
  38. ;; Links within an org buffer might look like this:
  39. ;;
  40. ;; [[irc:/irc.freenode.net/#emacs/bob][chat with bob in #emacs on freenode]]
  41. ;; [[irc:/irc.freenode.net/#emacs][#emacs on freenode]]
  42. ;; [[irc:/irc.freenode.net/]]
  43. ;;
  44. ;; If, when the resulting link is visited, there is no connection to a
  45. ;; requested server then one will be created.
  46. ;;; Code:
  47. (require 'org)
  48. (require 'erc)
  49. (require 'erc-log)
  50. (defvar org-irc-client 'erc
  51. "The IRC client to act on")
  52. (defvar org-irc-link-to-logs nil
  53. "non-nil will store a link to the logs, nil will store an irc: style link")
  54. (defvar erc-default-port) ; dynamically scoped from erc.el
  55. (defvar erc-session-port) ; dynamically scoped form erc-backend.el
  56. (defvar erc-session-server) ; dynamically scoped form erc-backend.el
  57. ;; Generic functions/config (extend these for other clients)
  58. (add-to-list 'org-store-link-functions
  59. 'org-irc-store-link)
  60. (org-add-link-type "irc" 'org-irc-visit nil)
  61. (defun org-irc-visit (link)
  62. "Dispatch to the correct visit function based on the client"
  63. (let ((link (org-irc-parse-link link)))
  64. (cond
  65. ((eq org-irc-client 'erc)
  66. (org-irc-visit-erc link))
  67. (t
  68. (error "erc only known client")))))
  69. (defun org-irc-parse-link (link)
  70. "Get a of irc link attributes where `link' looks like
  71. server:port/chan/user (port, chan and user being optional)."
  72. (let* ((parts (split-string link "/" t))
  73. (len (length parts)))
  74. (when (or (< len 1) (> len 3))
  75. (error "Failed to parse link needed 1-3 parts, got %d." len))
  76. (setcar parts (split-string (car parts) ":" t))
  77. parts))
  78. ;;;###autoload
  79. (defun org-irc-store-link ()
  80. "Dispatch to the appropreate function to store a link to
  81. something IRC related"
  82. (cond
  83. ((eq major-mode 'erc-mode)
  84. (org-irc-erc-store-link))))
  85. (defun org-irc-elipsify-description (string &optional after)
  86. "Strip starting and ending whitespace and replace any chars
  87. that appear after the value in `after' with '...'"
  88. (let* ((after (number-to-string (or after 30)))
  89. (replace-map (list (cons "^[ \t]*" "")
  90. (cons "[ \t]*$" "")
  91. (cons (concat "^\\(.\\{" after
  92. "\\}\\).*") "\\1..."))))
  93. (mapc (lambda (x)
  94. (when (string-match (car x) string)
  95. (setq string (replace-match (cdr x) nil nil string))))
  96. replace-map)
  97. string))
  98. ;; ERC specific functions
  99. (defun org-irc-erc-get-line-from-log (erc-line)
  100. "Find the most suitable line to link to from the erc logs. If
  101. the user is on the erc-prompt then search backward for the first
  102. non-blank line, otherwise return the current line. The result is
  103. a cons of the filename and search string."
  104. (erc-save-buffer-in-logs)
  105. (with-current-buffer (find-file-noselect (erc-current-logfile))
  106. (goto-char (point-max))
  107. (list
  108. (abbreviate-file-name buffer-file-name)
  109. ;; can we get a '::' part?
  110. (if (string= erc-line (erc-prompt))
  111. (progn
  112. (goto-char (point-at-bol))
  113. (when (search-backward-regexp "^[^ ]" nil t)
  114. (buffer-substring-no-properties (point-at-bol)
  115. (point-at-eol))))
  116. (when (search-backward erc-line nil t)
  117. (buffer-substring-no-properties (point-at-bol)
  118. (point-at-eol)))))))
  119. (defun org-irc-erc-store-link ()
  120. "Depending on the variable `org-irc-link-to-logs' store either
  121. a link to the log file for the current session or an irc: link to
  122. the session itself."
  123. (if org-irc-link-to-logs
  124. (let* ((erc-line (buffer-substring-no-properties
  125. (point-at-bol) (point-at-eol)))
  126. (parsed-line (org-irc-erc-get-line-from-log erc-line)))
  127. (if (erc-logging-enabled nil)
  128. (progn
  129. (org-store-link-props
  130. :type "file"
  131. :description (concat "'" (org-irc-elipsify-description
  132. (cadr parsed-line) 20)
  133. "' from an IRC conversation")
  134. :link (concat "file:" (car parsed-line) "::"
  135. (cadr parsed-line)))
  136. t)
  137. (error "This ERC session is not being logged")))
  138. (let* ((link-text (org-irc-get-erc-link))
  139. (link (org-irc-parse-link link-text)))
  140. (if link-text
  141. (progn
  142. (org-store-link-props
  143. :type "irc"
  144. :link (org-make-link "irc:/" link-text)
  145. :description (concat "irc session '" link-text "'")
  146. :server (car (car link))
  147. :port (or (string-to-number (cadr (pop link))) erc-default-port)
  148. :nick (pop link))
  149. t)
  150. (error "Failed to create ('irc:/' style) ERC link")))))
  151. (defun org-irc-get-erc-link ()
  152. "Return an org compatible irc:/ link from an ERC buffer"
  153. (let* ((session-port (if (numberp erc-session-port)
  154. (number-to-string erc-session-port)
  155. erc-session-port))
  156. (link (concat erc-session-server ":" session-port)))
  157. (concat link "/"
  158. (if (and (erc-default-target)
  159. (erc-channel-p (erc-default-target))
  160. (car (get-text-property (point) 'erc-data)))
  161. ;; we can get a nick
  162. (let ((nick (car (get-text-property (point) 'erc-data))))
  163. (concat (erc-default-target) "/" nick))
  164. (erc-default-target)))))
  165. (defun org-irc-get-current-erc-port ()
  166. "Return the current port as a number. If there is not an
  167. explicit port set then return the erc default."
  168. (cond
  169. ((stringp erc-session-port)
  170. (string-to-number erc-session-port))
  171. ((numberp erc-session-port)
  172. erc-session-port)
  173. (t
  174. erc-default-port)))
  175. (defun org-irc-visit-erc (link)
  176. "Visit an ERC buffer based on criteria from the followed link"
  177. (let* ((server (car (car link)))
  178. (port (or (string-to-number (cadr (pop link))) erc-default-port))
  179. (server-buffer)
  180. (buffer-list
  181. (erc-buffer-filter
  182. (lambda nil
  183. (let ((tmp-server-buf (erc-server-buffer)))
  184. (and tmp-server-buf
  185. (with-current-buffer tmp-server-buf
  186. (and
  187. (eq (org-irc-get-current-erc-port) port)
  188. (string= erc-session-server server)
  189. (setq server-buffer tmp-server-buf)))))))))
  190. (if buffer-list
  191. (let ((chan-name (pop link)))
  192. ;; if we got a channel name then switch to it or join it
  193. (if chan-name
  194. (let ((chan-buf (catch 'found
  195. (dolist (x buffer-list)
  196. (if (string= (buffer-name x) chan-name)
  197. (throw 'found x))))))
  198. (if chan-buf
  199. (progn
  200. (switch-to-buffer chan-buf)
  201. ;; if we got a nick, and they're in the chan,
  202. ;; then start a chat with them
  203. (let ((nick (pop link)))
  204. (when nick
  205. (if (member nick (erc-get-server-nickname-list))
  206. (progn
  207. (goto-char (point-max))
  208. (insert (concat nick ": ")))
  209. (error "%s not found in %s" nick chan-name)))))
  210. (progn
  211. (switch-to-buffer server-buffer)
  212. (erc-cmd-JOIN chan-name))))
  213. (switch-to-buffer server-buffer)))
  214. ;; no server match, make new connection
  215. (erc-select :server server :port port))))
  216. (provide 'org-irc)
  217. ;; arch-tag: 018d7dda-53b8-4a35-ba92-6670939e525a
  218. ;;; org-irc.el ends here