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