ol-irc.el 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. ;;; ol-irc.el --- Links to IRC Sessions -*- lexical-binding: t; -*-
  2. ;;
  3. ;; Copyright (C) 2008-2022 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 <https://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.libera.chat/#emacs/bob][chat with bob in #emacs on Libera.Chat]]
  39. ;; [[irc:/irc.libera.chat/#emacs][#emacs on Libera.Chat]]
  40. ;; [[irc:/irc.libera.chat/]]
  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-macs)
  46. (org-assert-version)
  47. (require 'ol)
  48. (declare-function erc-buffer-filter "erc" (predicate &optional proc))
  49. (declare-function erc-channel-p "erc" (channel))
  50. (declare-function erc-cmd-JOIN "erc" (channel &optional key))
  51. (declare-function erc-current-logfile "erc-log" (&optional buffer))
  52. (declare-function erc-default-target "erc" ())
  53. (declare-function erc-get-server-nickname-list "erc" ())
  54. (declare-function erc-logging-enabled "erc-log" (&optional buffer))
  55. (declare-function erc-prompt "erc" ())
  56. (declare-function erc-save-buffer-in-logs "erc-log" (&optional buffer))
  57. (declare-function erc-server-buffer "erc" ())
  58. (defvar org-irc-client 'erc
  59. "The IRC client to act on.")
  60. (defvar org-irc-link-to-logs nil
  61. "Non-nil will store a link to the logs, nil will store an irc: style link.")
  62. (defvar erc-default-port) ; dynamically scoped from erc.el
  63. (defvar erc-session-port) ; dynamically scoped form erc-backend.el
  64. (defvar erc-session-server) ; dynamically scoped form erc-backend.el
  65. ;; Generic functions/config (extend these for other clients)
  66. (org-link-set-parameters "irc"
  67. :follow #'org-irc-visit
  68. :store #'org-irc-store-link
  69. :export #'org-irc-export)
  70. (defun org-irc-visit (link _)
  71. "Parse LINK and dispatch to the correct function based on the client found."
  72. (let ((link (org-irc-parse-link link)))
  73. (cond
  74. ((eq org-irc-client 'erc)
  75. (org-irc-visit-erc link))
  76. (t
  77. (error "ERC only known client")))))
  78. (defun org-irc-parse-link (link)
  79. "Parse an IRC LINK and return the attributes found.
  80. Parse a LINK that looks like server:port/chan/user (port, chan
  81. and user being optional) and return any of the port, channel or user
  82. attributes that are found."
  83. (let* ((parts (split-string link "/" t))
  84. (len (length parts)))
  85. (when (or (< len 1) (> len 3))
  86. (error "Failed to parse link needed 1-3 parts, got %d" len))
  87. (setcar parts (split-string (car parts) ":" t))
  88. parts))
  89. ;;;###autoload
  90. (defun org-irc-store-link ()
  91. "Dispatch to the appropriate function to store a link to an IRC session."
  92. (cond
  93. ((eq major-mode 'erc-mode)
  94. (org-irc-erc-store-link))))
  95. (defun org-irc-ellipsify-description (string &optional after)
  96. "Remove unnecessary white space from STRING and add ellipses if necessary.
  97. Strip starting and ending white space from STRING and replace any
  98. chars that the value AFTER with `...'"
  99. (let* ((after (number-to-string (or after 30)))
  100. (replace-map (list (cons "^[ \t]*" "")
  101. (cons "[ \t]*$" "")
  102. (cons (concat "^\\(.\\{" after
  103. "\\}\\).*") "\\1..."))))
  104. (dolist (x replace-map string)
  105. (when (string-match (car x) string)
  106. (setq string (replace-match (cdr x) nil nil string))))))
  107. ;; ERC specific functions
  108. (defun org-irc-erc-get-line-from-log (erc-line)
  109. "Find the best line to link to from the ERC logs given ERC-LINE as a start.
  110. If the user is on the ERC-prompt then search backward for the
  111. first non-blank line, otherwise return the current line. The
  112. result is a cons of the filename and search string."
  113. (erc-save-buffer-in-logs)
  114. (require 'erc-log)
  115. (with-current-buffer (find-file-noselect (erc-current-logfile))
  116. (goto-char (point-max))
  117. (list
  118. (abbreviate-file-name buffer-file-name)
  119. ;; can we get a '::' part?
  120. (if (string= erc-line (erc-prompt))
  121. (progn
  122. (goto-char (line-beginning-position))
  123. (when (search-backward-regexp "^[^ ]" nil t)
  124. (buffer-substring-no-properties (line-beginning-position)
  125. (line-end-position))))
  126. (when (search-backward erc-line nil t)
  127. (buffer-substring-no-properties (line-beginning-position)
  128. (line-end-position)))))))
  129. (defun org-irc-erc-store-link ()
  130. "Store a link to the IRC log file or the session itself.
  131. Depending on the variable `org-irc-link-to-logs' store either a
  132. link to the log file for the current session or an irc: link to
  133. the session itself."
  134. (require 'erc-log)
  135. (if org-irc-link-to-logs
  136. (let* ((erc-line (buffer-substring-no-properties
  137. (line-beginning-position) (line-end-position)))
  138. (parsed-line (org-irc-erc-get-line-from-log erc-line)))
  139. (if (erc-logging-enabled nil)
  140. (progn
  141. (org-link-store-props
  142. :type "file"
  143. :description (concat "'" (org-irc-ellipsify-description
  144. (cadr parsed-line) 20)
  145. "' from an IRC conversation")
  146. :link (concat "file:" (car parsed-line) "::"
  147. (cadr parsed-line)))
  148. t)
  149. (error "This ERC session is not being logged")))
  150. (let* ((link-text (org-irc-get-erc-link))
  151. (link (org-irc-parse-link link-text)))
  152. (if link-text
  153. (progn
  154. (org-link-store-props
  155. :type "irc"
  156. :link (concat "irc:/" link-text)
  157. :description (concat "irc session `" link-text "'")
  158. :server (car (car link))
  159. :port (or (string-to-number (cadr (pop link))) erc-default-port)
  160. :nick (pop link))
  161. t)
  162. (error "Failed to create ('irc:/' style) ERC link")))))
  163. (defun org-irc-get-erc-link ()
  164. "Return an org compatible irc:/ link from an ERC buffer."
  165. (let* ((session-port (if (numberp erc-session-port)
  166. (number-to-string erc-session-port)
  167. erc-session-port))
  168. (link (concat erc-session-server ":" session-port)))
  169. (concat link "/"
  170. (if (and (erc-default-target)
  171. (erc-channel-p (erc-default-target))
  172. (car (get-text-property (point) 'erc-data)))
  173. ;; we can get a nick
  174. (let ((nick (car (get-text-property (point) 'erc-data))))
  175. (concat (erc-default-target) "/" nick))
  176. (erc-default-target)))))
  177. (defun org-irc-get-current-erc-port ()
  178. "Return the current port as a number.
  179. Return the current port number or, if none is set, return the ERC
  180. default."
  181. (cond
  182. ((stringp erc-session-port)
  183. (string-to-number erc-session-port))
  184. ((numberp erc-session-port)
  185. erc-session-port)
  186. (t
  187. erc-default-port)))
  188. (defun org-irc-visit-erc (link)
  189. "Visit an ERC buffer based on criteria found in LINK."
  190. (require 'erc)
  191. (require 'erc-log)
  192. (let* ((server (car (car link)))
  193. (port (let ((p (cadr (pop link))))
  194. (if p (string-to-number p) erc-default-port)))
  195. (server-buffer)
  196. (buffer-list
  197. (erc-buffer-filter
  198. (lambda nil
  199. (let ((tmp-server-buf (erc-server-buffer)))
  200. (and tmp-server-buf
  201. (with-current-buffer tmp-server-buf
  202. (and
  203. (eq (org-irc-get-current-erc-port) port)
  204. (string= erc-session-server server)
  205. (setq server-buffer tmp-server-buf)))))))))
  206. (if buffer-list
  207. (let ((chan-name (pop link)))
  208. ;; if we got a channel name then switch to it or join it
  209. (if chan-name
  210. (let ((chan-buf (catch 'found
  211. (dolist (x buffer-list)
  212. (if (string= (buffer-name x) chan-name)
  213. (throw 'found x))))))
  214. (if chan-buf
  215. (progn
  216. (pop-to-buffer-same-window chan-buf)
  217. ;; if we got a nick, and they're in the chan,
  218. ;; then start a chat with them
  219. (let ((nick (pop link)))
  220. (when nick
  221. (if (member nick (erc-get-server-nickname-list))
  222. (progn
  223. (goto-char (point-max))
  224. (insert (concat nick ": ")))
  225. (error "%s not found in %s" nick chan-name)))))
  226. (progn
  227. (pop-to-buffer-same-window server-buffer)
  228. (erc-cmd-JOIN chan-name))))
  229. (pop-to-buffer-same-window server-buffer)))
  230. ;; no server match, make new connection
  231. (erc-select :server server :port port))))
  232. (defun org-irc-export (link description format)
  233. "Export an IRC link.
  234. See `org-link-parameters' for details about LINK, DESCRIPTION and
  235. FORMAT."
  236. (let ((desc (or description link)))
  237. (pcase format
  238. (`html (format "<a href=\"irc:%s\">%s</a>" link desc))
  239. (`md (format "[%s](irc:%s)" desc link))
  240. (_ nil))))
  241. (provide 'ol-irc)
  242. ;; Local variables:
  243. ;; generated-autoload-file: "org-loaddefs.el"
  244. ;; End:
  245. ;;; ol-irc.el ends here