org-irc.el 9.6 KB

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