org-irc.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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: 6.13pre02
  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 of the License, or
  14. ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
  21. ;;; Commentary:
  22. ;; This file implements links to an IRC session from within Org-mode.
  23. ;; Org-mode loads this module by default - if this is not what you want,
  24. ;; configure the variable `org-modules'.
  25. ;;
  26. ;; Please customize the variable `org-modules' to select
  27. ;; extensions you would like to use, and to deselect those which you don't
  28. ;; want.
  29. ;;
  30. ;; Please note that at the moment only ERC is supported. Other clients
  31. ;; shouldn't be difficult to add though.
  32. ;;
  33. ;; Then set `org-irc-link-to-logs' to non-nil if you would like a
  34. ;; file:/ type link to be created to the current line in the logs or
  35. ;; to t if you would like to create an irc:/ style link.
  36. ;;
  37. ;; Links within an org buffer might look like this:
  38. ;;
  39. ;; [[irc:/irc.freenode.net/#emacs/bob][chat with bob in #emacs on freenode]]
  40. ;; [[irc:/irc.freenode.net/#emacs][#emacs on freenode]]
  41. ;; [[irc:/irc.freenode.net/]]
  42. ;;
  43. ;; If, when the resulting link is visited, there is no connection to a
  44. ;; requested server then one will be created.
  45. ;;; Code:
  46. (require 'org)
  47. ;; Declare the function fomr ERC that we use.
  48. (declare-function erc-current-logfile "erc-log" (&optional buffer))
  49. (declare-function erc-prompt "erc" ())
  50. (declare-function erc-default-target "erc" ())
  51. (declare-function erc-channel-p "erc" (channel))
  52. (declare-function erc-buffer-filter "erc" (predicate &optional proc))
  53. (declare-function erc-server-buffer "erc" ())
  54. (declare-function erc-get-server-nickname-list "erc" ())
  55. (declare-function erc-cmd-JOIN "erc" (channel &optional key))
  56. (defvar org-irc-client 'erc
  57. "The IRC client to act on.")
  58. (defvar org-irc-link-to-logs nil
  59. "Non-nil will store a link to the logs, nil will store an irc: style link.")
  60. (defvar erc-default-port) ; dynamically scoped from erc.el
  61. (defvar erc-session-port) ; dynamically scoped form erc-backend.el
  62. (defvar erc-session-server) ; dynamically scoped form erc-backend.el
  63. ;; Generic functions/config (extend these for other clients)
  64. (add-to-list 'org-store-link-functions 'org-irc-store-link)
  65. (org-add-link-type "irc" 'org-irc-visit nil)
  66. (defun org-irc-visit (link)
  67. "Parse LINK and dispatch to the correct function based on the client found."
  68. (let ((link (org-irc-parse-link link)))
  69. (cond
  70. ((eq org-irc-client 'erc)
  71. (org-irc-visit-erc link))
  72. (t
  73. (error "erc only known client")))))
  74. (defun org-irc-parse-link (link)
  75. "Parse an IRC LINK and return the attributes found.
  76. Parse a LINK that looks like server:port/chan/user (port, chan
  77. and user being optional) and return any of the port, channel or user
  78. attributes that are found."
  79. (let* ((parts (split-string link "/" t))
  80. (len (length parts)))
  81. (when (or (< len 1) (> len 3))
  82. (error "Failed to parse link needed 1-3 parts, got %d" len))
  83. (setcar parts (split-string (car parts) ":" t))
  84. parts))
  85. ;;;###autoload
  86. (defun org-irc-store-link ()
  87. "Dispatch to the appropriate function to store a link to an IRC session."
  88. (cond
  89. ((eq major-mode 'erc-mode)
  90. (org-irc-erc-store-link))))
  91. (defun org-irc-elipsify-description (string &optional after)
  92. "Remove unnecessary white space from STRING and add ellipses if necessary.
  93. Strip starting and ending white space from STRING and replace any
  94. chars that the value AFTER with '...'"
  95. (let* ((after (number-to-string (or after 30)))
  96. (replace-map (list (cons "^[ \t]*" "")
  97. (cons "[ \t]*$" "")
  98. (cons (concat "^\\(.\\{" after
  99. "\\}\\).*") "\\1..."))))
  100. (mapc (lambda (x)
  101. (when (string-match (car x) string)
  102. (setq string (replace-match (cdr x) nil nil string))))
  103. replace-map)
  104. string))
  105. ;; ERC specific functions
  106. (defun org-irc-erc-get-line-from-log (erc-line)
  107. "Find the best line to link to from the ERC logs given ERC-LINE as a start.
  108. If the user is on the ERC-prompt then search backward for the
  109. first non-blank line, otherwise return the current line. The
  110. result is a cons of the filename and search string."
  111. (erc-save-buffer-in-logs)
  112. (require 'erc-log)
  113. (with-current-buffer (find-file-noselect (erc-current-logfile))
  114. (goto-char (point-max))
  115. (list
  116. (abbreviate-file-name buffer-file-name)
  117. ;; can we get a '::' part?
  118. (if (string= erc-line (erc-prompt))
  119. (progn
  120. (goto-char (point-at-bol))
  121. (when (search-backward-regexp "^[^ ]" nil t)
  122. (buffer-substring-no-properties (point-at-bol)
  123. (point-at-eol))))
  124. (when (search-backward erc-line nil t)
  125. (buffer-substring-no-properties (point-at-bol)
  126. (point-at-eol)))))))
  127. (defun org-irc-erc-store-link ()
  128. "Store a link to the IRC log file or the session itself.
  129. Depending on the variable `org-irc-link-to-logs' store either a
  130. link to the log file for the current session or an irc: link to
  131. the session itself."
  132. (require 'erc-log)
  133. (if org-irc-link-to-logs
  134. (let* ((erc-line (buffer-substring-no-properties
  135. (point-at-bol) (point-at-eol)))
  136. (parsed-line (org-irc-erc-get-line-from-log erc-line)))
  137. (if (erc-logging-enabled nil)
  138. (progn
  139. (org-store-link-props
  140. :type "file"
  141. :description (concat "'" (org-irc-elipsify-description
  142. (cadr parsed-line) 20)
  143. "' from an IRC conversation")
  144. :link (concat "file:" (car parsed-line) "::"
  145. (cadr parsed-line)))
  146. t)
  147. (error "This ERC session is not being logged")))
  148. (let* ((link-text (org-irc-get-erc-link))
  149. (link (org-irc-parse-link link-text)))
  150. (if link-text
  151. (progn
  152. (org-store-link-props
  153. :type "irc"
  154. :link (org-make-link "irc:/" link-text)
  155. :description (concat "irc session '" link-text "'")
  156. :server (car (car link))
  157. :port (or (string-to-number (cadr (pop link))) erc-default-port)
  158. :nick (pop link))
  159. t)
  160. (error "Failed to create ('irc:/' style) ERC link")))))
  161. (defun org-irc-get-erc-link ()
  162. "Return an org compatible irc:/ link from an ERC buffer."
  163. (let* ((session-port (if (numberp erc-session-port)
  164. (number-to-string erc-session-port)
  165. erc-session-port))
  166. (link (concat erc-session-server ":" session-port)))
  167. (concat link "/"
  168. (if (and (erc-default-target)
  169. (erc-channel-p (erc-default-target))
  170. (car (get-text-property (point) 'erc-data)))
  171. ;; we can get a nick
  172. (let ((nick (car (get-text-property (point) 'erc-data))))
  173. (concat (erc-default-target) "/" nick))
  174. (erc-default-target)))))
  175. (defun org-irc-get-current-erc-port ()
  176. "Return the current port as a number.
  177. Return the current port number or, if none is set, return the ERC
  178. default."
  179. (cond
  180. ((stringp erc-session-port)
  181. (string-to-number erc-session-port))
  182. ((numberp erc-session-port)
  183. erc-session-port)
  184. (t
  185. erc-default-port)))
  186. (defun org-irc-visit-erc (link)
  187. "Visit an ERC buffer based on criteria found in LINK."
  188. (require 'erc)
  189. (require 'erc-log)
  190. (let* ((server (car (car link)))
  191. (port (or (string-to-number (cadr (pop link))) erc-default-port))
  192. (server-buffer)
  193. (buffer-list
  194. (erc-buffer-filter
  195. (lambda nil
  196. (let ((tmp-server-buf (erc-server-buffer)))
  197. (and tmp-server-buf
  198. (with-current-buffer tmp-server-buf
  199. (and
  200. (eq (org-irc-get-current-erc-port) port)
  201. (string= erc-session-server server)
  202. (setq server-buffer tmp-server-buf)))))))))
  203. (if buffer-list
  204. (let ((chan-name (pop link)))
  205. ;; if we got a channel name then switch to it or join it
  206. (if chan-name
  207. (let ((chan-buf (catch 'found
  208. (dolist (x buffer-list)
  209. (if (string= (buffer-name x) chan-name)
  210. (throw 'found x))))))
  211. (if chan-buf
  212. (progn
  213. (switch-to-buffer chan-buf)
  214. ;; if we got a nick, and they're in the chan,
  215. ;; then start a chat with them
  216. (let ((nick (pop link)))
  217. (when nick
  218. (if (member nick (erc-get-server-nickname-list))
  219. (progn
  220. (goto-char (point-max))
  221. (insert (concat nick ": ")))
  222. (error "%s not found in %s" nick chan-name)))))
  223. (progn
  224. (switch-to-buffer server-buffer)
  225. (erc-cmd-JOIN chan-name))))
  226. (switch-to-buffer server-buffer)))
  227. ;; no server match, make new connection
  228. (erc-select :server server :port port))))
  229. (provide 'org-irc)
  230. ;; arch-tag: 018d7dda-53b8-4a35-ba92-6670939e525a
  231. ;;; org-irc.el ends here