org-irc.el 7.8 KB

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