1 ;;; org-irc.el --- Store links to IRC sessions
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
5 ;; Author: Philip Jackson <emacs@shellarchive.co.uk>
6 ;; Keywords: erc, irc, link, org
9 ;; This file is part of GNU Emacs.
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)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Link to an IRC session. Only ERC has been implemented at the
31 ;; Please configure the variable `org-default-extensions' to ensure that
32 ;; this extension will be loaded.
34 ;; Please note that at the moment only ERC is supported. Other clients
35 ;; shouldn't be diffficult to add though.
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.
41 ;; Links within an org buffer might look like this:
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/]]
47 ;; If, when the resulting link is visited, there is no connection to a
48 ;; requested server then one will be created.
54 (defvar org-irc-client
'erc
55 "The IRC client to act on")
56 (defvar org-irc-link-to-logs nil
57 "non-nil will store a link to the logs, nil will store an irc: style link")
59 ;; Generic functions/config (extend these for other clients)
61 (add-to-list 'org-store-link-functions
64 (org-add-link-type "irc" 'org-irc-visit nil
)
66 (defun org-irc-visit (link)
67 "Dispatch to the correct visit function based on the client"
68 (let ((link (org-irc-parse-link link
)))
70 ((eq org-irc-client
'erc
)
71 (org-irc-visit-erc link
))
73 (error "erc only known client")))))
75 (defun org-irc-parse-link (link)
76 "Get a of irc link attributes where `link' looks like
77 server:port/chan/user (port, chan and user being optional)."
78 (let* ((parts (split-string link
"/" t
))
80 (when (or (< len
1) (> len
3))
81 (error "Failed to parse link needed 1-3 parts, got %d." len
))
82 (setcar parts
(split-string (car parts
) ":" t
))
86 (defun org-irc-store-link ()
87 "Dispatch to the appropreate function to store a link to
88 something IRC related"
90 ((eq major-mode
'erc-mode
)
91 (org-irc-erc-store-link))))
93 ;; ERC specific functions
95 (defun org-irc-erc-get-line-from-log (erc-line)
96 "Find the most suitable line to link to from the erc logs. If
97 the user is on the erc-prompt then search backward for the first
98 non-blank line, otherwise return the current line."
99 (erc-save-buffer-in-logs)
100 (with-current-buffer (find-file-noselect (erc-current-logfile))
101 (goto-char (point-max))
103 "file:" (abbreviate-file-name
105 ;; can we get a '::' part?
106 (if (equal erc-line
(erc-prompt))
108 (goto-char (point-at-bol))
109 (when (search-backward-regexp "^[^ ]" nil t
)
110 (concat "::" (buffer-substring-no-properties
113 (when (search-backward erc-line nil t
)
114 (concat "::" (buffer-substring-no-properties
116 (point-at-eol))))))))
118 (defun org-irc-erc-store-link ()
119 "Return an org compatible file:/ link which links to a log file
120 of the current ERC session"
121 (if org-irc-link-to-logs
122 (let ((erc-line (buffer-substring-no-properties
123 (point-at-bol) (point-at-eol))))
124 (if (erc-logging-enabled nil
)
126 (setq cpltxt
(org-irc-erc-get-line-from-log erc-line
))
128 (error "This ERC session is not being logged")))
129 (let ((link (org-irc-get-erc-link)))
132 (setq cpltxt
(concat "irc:/" link
))
133 (org-make-link cpltxt
)
134 (setq link
(org-irc-parse-link link
))
135 (org-store-link-props :type
"irc"
136 :server
(car (car link
))
137 :port
(or (cadr (pop link
))
141 (error "Failed to create (non-log) ERC link")))))
143 (defun org-irc-get-erc-link ()
144 "Return an org compatible irc:/ link from an ERC buffer"
145 (let ((link (concat erc-server-announced-name
":"
148 (if (and (erc-default-target)
149 (erc-channel-p (erc-default-target))
150 (car (get-text-property (point) 'erc-data
)))
152 (let ((nick (car (get-text-property (point) 'erc-data
))))
153 (concat (erc-default-target) "/" nick
))
154 (erc-default-target)))))
156 (defun org-irc-visit-erc (link)
157 "Visit an ERC buffer based on criteria from the followed link"
158 (let* ((server (car (car link
)))
159 (port (or (cadr (pop link
)) erc-default-port
))
164 (let ((tmp-server-buf (erc-server-buffer)))
166 (with-current-buffer tmp-server-buf
168 (string= erc-session-port port
)
169 (string= erc-server-announced-name server
)
170 (setq server-buffer tmp-server-buf
)))))))))
172 (let ((chan-name (pop link
)))
173 ;; if we got a channel name then switch to it or join it
175 (let ((chan-buf (find-if
177 (string= (buffer-name x
) chan-name
))
181 (switch-to-buffer chan-buf
)
182 ;; if we got a nick, and they're in the chan,
183 ;; then start a chat with them
184 (let ((nick (pop link
)))
186 (if (find nick
(erc-get-server-nickname-list)
189 (goto-char (point-max))
190 (insert (concat nick
": ")))
191 (error "%s not found in %s" nick chan
)))))
193 (switch-to-buffer server-buffer
)
194 (erc-cmd-JOIN chan-name
))))
195 (switch-to-buffer server-buffer
)))
196 ;; no server match, make new connection
197 (erc-select :server server
:port port
))))
201 ;;; org-irc.el ends here