org-irc.el 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.3
  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. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Link to an IRC session. Only ERC has been implemented at the
  27. ;; moment.
  28. ;;
  29. ;; This file is loaded by default whenever org.el is loaded. Please
  30. ;; customize the variable `org-default-extensions' to select extensions
  31. ;; you would like to use, and to deselect those which you don't want.
  32. ;;
  33. ;; Please note that at the moment only ERC is supported. Other clients
  34. ;; shouldn't be diffficult to add though.
  35. ;;
  36. ;; Then set `org-irc-link-to-logs' to non-nil if you would like a
  37. ;; file:/ type link to be created to the current line in the logs or
  38. ;; to t if you would like to create an irc:/ style link.
  39. ;;
  40. ;; Links within an org buffer might look like this:
  41. ;;
  42. ;; [[irc:/irc.freenode.net/#emacs/bob][chat with bob in #emacs on freenode]]
  43. ;; [[irc:/irc.freenode.net/#emacs][#emacs on freenode]]
  44. ;; [[irc:/irc.freenode.net/]]
  45. ;;
  46. ;; If, when the resulting link is visited, there is no connection to a
  47. ;; requested server then one will be created.
  48. ;;
  49. ;;; Code:
  50. (eval-when-compile
  51. (require 'cl))
  52. (require 'org)
  53. (require 'erc)
  54. (require 'erc-log)
  55. (defvar org-irc-client 'erc
  56. "The IRC client to act on")
  57. (defvar org-irc-link-to-logs nil
  58. "non-nil will store a link to the logs, nil will store an irc: style link")
  59. (defvar erc-default-port) ; dynamically scoped from erc.el
  60. (defvar erc-session-port) ; dynamically scoped form erc-backend.el
  61. (defvar erc-session-server) ; dynamically scoped form erc-backend.el
  62. ;; Generic functions/config (extend these for other clients)
  63. (add-to-list 'org-store-link-functions
  64. 'org-irc-store-link)
  65. (org-add-link-type "irc" 'org-irc-visit nil)
  66. (defun org-irc-visit (link)
  67. "Dispatch to the correct visit function based on the client"
  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. "Get a of irc link attributes where `link' looks like
  76. server:port/chan/user (port, chan and user being optional)."
  77. (let* ((parts (split-string link "/" t))
  78. (len (length parts)))
  79. (when (or (< len 1) (> len 3))
  80. (error "Failed to parse link needed 1-3 parts, got %d." len))
  81. (setcar parts (split-string (car parts) ":" t))
  82. parts))
  83. ;;;###autoload
  84. (defun org-irc-store-link ()
  85. "Dispatch to the appropreate function to store a link to
  86. something IRC related"
  87. (cond
  88. ((eq major-mode 'erc-mode)
  89. (org-irc-erc-store-link))))
  90. (defun org-irc-elipsify-description (string &optional after)
  91. "Strip starting and ending whitespace and replace any chars
  92. that appear after the value in `after' with '...'"
  93. (let* ((after (number-to-string (or after 30)))
  94. (replace-map (list (cons "^[ \t]*" "")
  95. (cons "[ \t]*$" "")
  96. (cons (concat "^\\(.\\{" after
  97. "\\}\\).*") "\\1..."))))
  98. (mapc (lambda (x)
  99. (when (string-match (car x) string)
  100. (setq string (replace-match (cdr x) nil nil string))))
  101. replace-map)
  102. string))
  103. ;; ERC specific functions
  104. (defun org-irc-erc-get-line-from-log (erc-line)
  105. "Find the most suitable line to link to from the erc logs. If
  106. the user is on the erc-prompt then search backward for the first
  107. non-blank line, otherwise return the current line. The result is
  108. a cons of the filename and search string."
  109. (erc-save-buffer-in-logs)
  110. (with-current-buffer (find-file-noselect (erc-current-logfile))
  111. (goto-char (point-max))
  112. (list
  113. (abbreviate-file-name buffer-file-name)
  114. ;; can we get a '::' part?
  115. (if (string= erc-line (erc-prompt))
  116. (progn
  117. (goto-char (point-at-bol))
  118. (when (search-backward-regexp "^[^ ]" nil t)
  119. (buffer-substring-no-properties (point-at-bol)
  120. (point-at-eol))))
  121. (when (search-backward erc-line nil t)
  122. (buffer-substring-no-properties (point-at-bol)
  123. (point-at-eol)))))))
  124. (defun org-irc-erc-store-link ()
  125. "Depending on the variable `org-irc-link-to-logs' store either
  126. a link to the log file for the current session or an irc: link to
  127. the session itself."
  128. (if org-irc-link-to-logs
  129. (let* ((erc-line (buffer-substring-no-properties
  130. (point-at-bol) (point-at-eol)))
  131. (parsed-line (org-irc-erc-get-line-from-log erc-line)))
  132. (if (erc-logging-enabled nil)
  133. (progn
  134. (org-store-link-props
  135. :type "file"
  136. :description (concat "'" (org-irc-elipsify-description
  137. (cadr parsed-line) 20)
  138. "' from an IRC conversation")
  139. :link (concat "file:" (car parsed-line) "::"
  140. (cadr parsed-line)))
  141. t)
  142. (error "This ERC session is not being logged")))
  143. (let* ((link-text (org-irc-get-erc-link))
  144. (link (org-irc-parse-link link-text)))
  145. (if link-text
  146. (progn
  147. (org-store-link-props
  148. :type "irc"
  149. :link (org-make-link "irc:/" link-text)
  150. :description (concat "irc session '" link-text "'")
  151. :server (car (car link))
  152. :port (or (string-to-number (cadr (pop link))) erc-default-port)
  153. :nick (pop link))
  154. t)
  155. (error "Failed to create ('irc:/' style) ERC link")))))
  156. (defun org-irc-get-erc-link ()
  157. "Return an org compatible irc:/ link from an ERC buffer"
  158. (let* ((session-port (if (numberp erc-session-port)
  159. (number-to-string erc-session-port)
  160. erc-session-port))
  161. (link (concat erc-session-server ":" session-port)))
  162. (concat link "/"
  163. (if (and (erc-default-target)
  164. (erc-channel-p (erc-default-target))
  165. (car (get-text-property (point) 'erc-data)))
  166. ;; we can get a nick
  167. (let ((nick (car (get-text-property (point) 'erc-data))))
  168. (concat (erc-default-target) "/" nick))
  169. (erc-default-target)))))
  170. (defun org-irc-get-current-erc-port ()
  171. "Return the current port as a number. If there is not an
  172. explicit port set then return the erc 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 from the followed 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 (find-if
  200. (lambda (x)
  201. (string= (buffer-name x) chan-name))
  202. buffer-list)))
  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 (find nick (erc-get-server-nickname-list)
  211. :test 'string=)
  212. (progn
  213. (goto-char (point-max))
  214. (insert (concat nick ": ")))
  215. (error "%s not found in %s" nick chan-name)))))
  216. (progn
  217. (switch-to-buffer server-buffer)
  218. (erc-cmd-JOIN chan-name))))
  219. (switch-to-buffer server-buffer)))
  220. ;; no server match, make new connection
  221. (erc-select :server server :port port))))
  222. (provide 'org-irc)
  223. ;;; org-irc.el ends here