1 ;;; org-irc.el --- Store Links to IRC Sessions -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2008-2016 Free Software Foundation, Inc.
5 ;; Author: Philip Jackson <emacs@shellarchive.co.uk>
6 ;; Keywords: erc, irc, link, org
8 ;; This file is part of GNU Emacs.
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 <http://www.gnu.org/licenses/>.
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
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.
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" :follow
#'org-irc-visit
:store
#'org-irc-store-link
)
76 (defun org-irc-visit (link)
77 "Parse LINK and dispatch to the correct function based on the client found."
78 (let ((link (org-irc-parse-link link
)))
80 ((eq org-irc-client
'erc
)
81 (org-irc-visit-erc link
))
83 (error "ERC only known client")))))
85 (defun org-irc-parse-link (link)
86 "Parse an IRC LINK and return the attributes found.
87 Parse a LINK that looks like server:port/chan/user (port, chan
88 and user being optional) and return any of the port, channel or user
89 attributes that are found."
90 (let* ((parts (split-string link
"/" t
))
92 (when (or (< len
1) (> len
3))
93 (error "Failed to parse link needed 1-3 parts, got %d" len
))
94 (setcar parts
(split-string (car parts
) ":" t
))
98 (defun org-irc-store-link ()
99 "Dispatch to the appropriate function to store a link to an IRC session."
101 ((eq major-mode
'erc-mode
)
102 (org-irc-erc-store-link))))
104 (defun org-irc-ellipsify-description (string &optional after
)
105 "Remove unnecessary white space from STRING and add ellipses if necessary.
106 Strip starting and ending white space from STRING and replace any
107 chars that the value AFTER with `...'"
108 (let* ((after (number-to-string (or after
30)))
109 (replace-map (list (cons "^[ \t]*" "")
111 (cons (concat "^\\(.\\{" after
112 "\\}\\).*") "\\1..."))))
113 (dolist (x replace-map string
)
114 (when (string-match (car x
) string
)
115 (setq string
(replace-match (cdr x
) nil nil string
))))))
117 ;; ERC specific functions
119 (defun org-irc-erc-get-line-from-log (erc-line)
120 "Find the best line to link to from the ERC logs given ERC-LINE as a start.
121 If the user is on the ERC-prompt then search backward for the
122 first non-blank line, otherwise return the current line. The
123 result is a cons of the filename and search string."
124 (erc-save-buffer-in-logs)
126 (with-current-buffer (find-file-noselect (erc-current-logfile))
127 (goto-char (point-max))
129 (abbreviate-file-name buffer-file-name
)
130 ;; can we get a '::' part?
131 (if (string= erc-line
(erc-prompt))
133 (goto-char (point-at-bol))
134 (when (search-backward-regexp "^[^ ]" nil t
)
135 (buffer-substring-no-properties (point-at-bol)
137 (when (search-backward erc-line nil t
)
138 (buffer-substring-no-properties (point-at-bol)
141 (defun org-irc-erc-store-link ()
142 "Store a link to the IRC log file or the session itself.
143 Depending on the variable `org-irc-link-to-logs' store either a
144 link to the log file for the current session or an irc: link to
147 (if org-irc-link-to-logs
148 (let* ((erc-line (buffer-substring-no-properties
149 (point-at-bol) (point-at-eol)))
150 (parsed-line (org-irc-erc-get-line-from-log erc-line
)))
151 (if (erc-logging-enabled nil
)
153 (org-store-link-props
155 :description
(concat "'" (org-irc-ellipsify-description
156 (cadr parsed-line
) 20)
157 "' from an IRC conversation")
158 :link
(concat "file:" (car parsed-line
) "::"
161 (error "This ERC session is not being logged")))
162 (let* ((link-text (org-irc-get-erc-link))
163 (link (org-irc-parse-link link-text
)))
166 (org-store-link-props
168 :link
(concat "irc:/" link-text
)
169 :description
(concat "irc session `" link-text
"'")
170 :server
(car (car link
))
171 :port
(or (string-to-number (cadr (pop link
))) erc-default-port
)
174 (error "Failed to create ('irc:/' style) ERC link")))))
176 (defun org-irc-get-erc-link ()
177 "Return an org compatible irc:/ link from an ERC buffer."
178 (let* ((session-port (if (numberp erc-session-port
)
179 (number-to-string erc-session-port
)
181 (link (concat erc-session-server
":" session-port
)))
183 (if (and (erc-default-target)
184 (erc-channel-p (erc-default-target))
185 (car (get-text-property (point) 'erc-data
)))
187 (let ((nick (car (get-text-property (point) 'erc-data
))))
188 (concat (erc-default-target) "/" nick
))
189 (erc-default-target)))))
191 (defun org-irc-get-current-erc-port ()
192 "Return the current port as a number.
193 Return the current port number or, if none is set, return the ERC
196 ((stringp erc-session-port
)
197 (string-to-number erc-session-port
))
198 ((numberp erc-session-port
)
203 (defun org-irc-visit-erc (link)
204 "Visit an ERC buffer based on criteria found in LINK."
207 (let* ((server (car (car link
)))
208 (port (or (string-to-number (cadr (pop link
))) erc-default-port
))
213 (let ((tmp-server-buf (erc-server-buffer)))
215 (with-current-buffer tmp-server-buf
217 (eq (org-irc-get-current-erc-port) port
)
218 (string= erc-session-server server
)
219 (setq server-buffer tmp-server-buf
)))))))))
221 (let ((chan-name (pop link
)))
222 ;; if we got a channel name then switch to it or join it
224 (let ((chan-buf (catch 'found
225 (dolist (x buffer-list
)
226 (if (string= (buffer-name x
) chan-name
)
227 (throw 'found x
))))))
230 (pop-to-buffer-same-window chan-buf
)
231 ;; if we got a nick, and they're in the chan,
232 ;; then start a chat with them
233 (let ((nick (pop link
)))
235 (if (member nick
(erc-get-server-nickname-list))
237 (goto-char (point-max))
238 (insert (concat nick
": ")))
239 (error "%s not found in %s" nick chan-name
)))))
241 (pop-to-buffer-same-window server-buffer
)
242 (erc-cmd-JOIN chan-name
))))
243 (pop-to-buffer-same-window server-buffer
)))
244 ;; no server match, make new connection
245 (erc-select :server server
:port port
))))
250 ;; generated-autoload-file: "org-loaddefs.el"
253 ;;; org-irc.el ends here