org-irc.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.0
  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. ;; Please configure the variable `org-default-extensions' to ensure that
  30. ;; this extension will be loaded.
  31. ;;
  32. ;; Please note that at the moment only ERC is supported. Other clients
  33. ;; shouldn't be diffficult 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. ;;
  48. ;;; Code:
  49. (require 'org-irc)
  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. ;; Generic functions/config (extend these for other clients)
  55. (add-to-list 'org-store-link-functions
  56. 'org-irc-store-link)
  57. (org-add-link-type "irc" 'org-irc-visit nil)
  58. (defun org-irc-visit (link)
  59. "Dispatch to the correct visit function based on the client"
  60. (let ((link (org-irc-parse-link link)))
  61. (cond
  62. ((eq org-irc-client 'erc)
  63. (org-irc-visit-erc link))
  64. (t
  65. (error "erc only known client")))))
  66. (defun org-irc-parse-link (link)
  67. "Get a of irc link attributes where `link' looks like
  68. server:port/chan/user (port, chan and user being optional)."
  69. (let* ((parts (split-string link "/" t))
  70. (len (length parts)))
  71. (when (or (< len 1) (> len 3))
  72. (error "Failed to parse link needed 1-3 parts, got %d." len))
  73. (setcar parts (split-string (car parts) ":" t))
  74. parts))
  75. ;;;###autoload
  76. (defun org-irc-store-link ()
  77. "Dispatch to the appropreate function to store a link to
  78. something IRC related"
  79. (cond
  80. ((eq major-mode 'erc-mode)
  81. (org-irc-erc-store-link))))
  82. ;; ERC specific functions
  83. (defun org-irc-erc-get-line-from-log (erc-line)
  84. "Find the most suitable line to link to from the erc logs. If
  85. the user is on the erc-prompt then search backward for the first
  86. non-blank line, otherwise return the current line."
  87. (erc-save-buffer-in-logs)
  88. (with-current-buffer (find-file-noselect (erc-current-logfile))
  89. (goto-char (point-max))
  90. (concat
  91. "file:" (abbreviate-file-name
  92. buffer-file-name)
  93. ;; can we get a '::' part?
  94. (if (equal erc-line (erc-prompt))
  95. (progn
  96. (goto-char (point-at-bol))
  97. (when (search-backward-regexp "^[^ ]" nil t)
  98. (concat "::" (buffer-substring-no-properties
  99. (point-at-bol)
  100. (point-at-eol)))))
  101. (when (search-backward erc-line nil t)
  102. (concat "::" (buffer-substring-no-properties
  103. (point-at-bol)
  104. (point-at-eol))))))))
  105. (defun org-irc-erc-store-link ()
  106. "Return an org compatible file:/ link which links to a log file
  107. of the current ERC session"
  108. (if org-irc-link-to-logs
  109. (let ((erc-line (buffer-substring-no-properties
  110. (point-at-bol) (point-at-eol))))
  111. (if (erc-logging-enabled nil)
  112. (progn
  113. (setq cpltxt (org-irc-erc-get-line-from-log erc-line))
  114. t)
  115. (error "This ERC session is not being logged")))
  116. (let ((link (org-irc-get-erc-link)))
  117. (if link
  118. (progn
  119. (setq cpltxt (concat "irc:/" link))
  120. (org-make-link cpltxt)
  121. (setq link (org-irc-parse-link link))
  122. (org-store-link-props :type "irc"
  123. :server (car (car link))
  124. :port (or (cadr (pop link))
  125. erc-default-port)
  126. :nick (pop link))
  127. t)
  128. (error "Failed to create (non-log) ERC link")))))
  129. (defun org-irc-get-erc-link ()
  130. "Return an org compatible irc:/ link from an ERC buffer"
  131. (let ((link (concat erc-server-announced-name ":"
  132. erc-session-port)))
  133. (concat link "/"
  134. (if (and (erc-default-target)
  135. (erc-channel-p (erc-default-target))
  136. (car (get-text-property (point) 'erc-data)))
  137. ;; we can get a nick
  138. (let ((nick (car (get-text-property (point) 'erc-data))))
  139. (concat (erc-default-target) "/" nick))
  140. (erc-default-target)))))
  141. (defun org-irc-visit-erc (link)
  142. "Visit an ERC buffer based on criteria from the followed link"
  143. (let* ((server (car (car link)))
  144. (port (or (cadr (pop link)) erc-default-port))
  145. (server-buffer)
  146. (buffer-list
  147. (erc-buffer-filter
  148. (lambda nil
  149. (let ((tmp-server-buf (erc-server-buffer)))
  150. (and tmp-server-buf
  151. (with-current-buffer tmp-server-buf
  152. (and
  153. (string= erc-session-port port)
  154. (string= erc-server-announced-name server)
  155. (setq server-buffer tmp-server-buf)))))))))
  156. (if buffer-list
  157. (let ((chan-name (pop link)))
  158. ;; if we got a channel name then switch to it or join it
  159. (if chan-name
  160. (let ((chan-buf (find-if
  161. (lambda (x)
  162. (string= (buffer-name x) chan-name))
  163. buffer-list)))
  164. (if chan-buf
  165. (progn
  166. (switch-to-buffer chan-buf)
  167. ;; if we got a nick, and they're in the chan,
  168. ;; then start a chat with them
  169. (let ((nick (pop link)))
  170. (when nick
  171. (if (find nick (erc-get-server-nickname-list)
  172. :test 'string=)
  173. (progn
  174. (goto-char (point-max))
  175. (insert (concat nick ": ")))
  176. (error "%s not found in %s" nick chan)))))
  177. (progn
  178. (switch-to-buffer server-buffer)
  179. (erc-cmd-JOIN chan-name))))
  180. (switch-to-buffer server-buffer)))
  181. ;; no server match, make new connection
  182. (erc-select :server server :port port))))
  183. (provide 'org-irc)
  184. ;;; org-irc.el ends here