org-irc.el 9.0 KB

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