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