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 ;; This file is loaded by default whenever org.el is loaded. Please
32 ;; customize the variable `org-default-extensions' to select extensions
33 ;; you would like to use, and to deselect those which you don't want.
35 ;; Please note that at the moment only ERC is supported. Other clients
36 ;; shouldn't be diffficult to add though.
38 ;; Then set `org-irc-link-to-logs' to non-nil if you would like a
39 ;; file:/ type link to be created to the current line in the logs or
40 ;; to t if you would like to create an irc:/ style link.
42 ;; Links within an org buffer might look like this:
44 ;; [[irc:/irc.freenode.net/#emacs/bob][chat with bob in #emacs on freenode]]
45 ;; [[irc:/irc.freenode.net/#emacs][#emacs on freenode]]
46 ;; [[irc:/irc.freenode.net/]]
48 ;; If, when the resulting link is visited, there is no connection to a
49 ;; requested server then one will be created.
55 (defvar org-irc-client
'erc
56 "The IRC client to act on")
57 (defvar org-irc-link-to-logs nil
58 "non-nil will store a link to the logs, nil will store an irc: style link")
60 ;; Generic functions/config (extend these for other clients)
62 (add-to-list 'org-store-link-functions
65 (org-add-link-type "irc" 'org-irc-visit nil
)
67 (defun org-irc-visit (link)
68 "Dispatch to the correct visit function based on the client"
69 (let ((link (org-irc-parse-link link
)))
71 ((eq org-irc-client
'erc
)
72 (org-irc-visit-erc link
))
74 (error "erc only known client")))))
76 (defun org-irc-parse-link (link)
77 "Get a of irc link attributes where `link' looks like
78 server:port/chan/user (port, chan and user being optional)."
79 (let* ((parts (split-string link
"/" t
))
81 (when (or (< len
1) (> len
3))
82 (error "Failed to parse link needed 1-3 parts, got %d." len
))
83 (setcar parts
(split-string (car parts
) ":" t
))
87 (defun org-irc-store-link ()
88 "Dispatch to the appropreate function to store a link to
89 something IRC related"
91 ((eq major-mode
'erc-mode
)
92 (org-irc-erc-store-link))))
94 (defun org-irc-elipsify-description (string &optional after
)
95 "Strip starting and ending whitespace and replace any chars
96 that appear after the value in `after' with '...'"
97 (let* ((after (number-to-string (or after
30)))
98 (replace-map (list (cons "^[ \t]*" "")
100 (cons (concat "^\\(.\\{" after
101 "\\}\\).*") "\\1..."))))
103 (when (string-match (car x
) string
)
104 (setq string
(replace-match (cdr x
) nil nil string
))))
108 ;; ERC specific functions
110 (defun org-irc-erc-get-line-from-log (erc-line)
111 "Find the most suitable line to link to from the erc logs. If
112 the user is on the erc-prompt then search backward for the first
113 non-blank line, otherwise return the current line. The result is
114 a cons of the filename and search string."
115 (erc-save-buffer-in-logs)
116 (with-current-buffer (find-file-noselect (erc-current-logfile))
117 (goto-char (point-max))
119 (abbreviate-file-name buffer-file-name
)
120 ;; can we get a '::' part?
121 (if (string= erc-line
(erc-prompt))
123 (goto-char (point-at-bol))
124 (when (search-backward-regexp "^[^ ]" nil t
)
125 (buffer-substring-no-properties (point-at-bol)
127 (when (search-backward erc-line nil t
)
128 (buffer-substring-no-properties (point-at-bol)
131 (defun org-irc-erc-store-link ()
132 "Depending on the variable `org-irc-link-to-logs' store either
133 a link to the log file for the current session or an irc: link to
135 (if org-irc-link-to-logs
136 (let* ((erc-line (buffer-substring-no-properties
137 (point-at-bol) (point-at-eol)))
138 (parsed-line (org-irc-erc-get-line-from-log erc-line
)))
139 (if (erc-logging-enabled nil
)
141 (org-store-link-props
143 :description
(concat "'" (org-irc-elipsify-description
144 (cadr parsed-line
) 20)
145 "' from an IRC conversation")
146 :link
(concat "file:" (car parsed-line
) "::"
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
)))
154 (org-store-link-props
156 :link
(org-make-link "irc:/" link-text
)
157 :description
(concat "irc session '" link-text
"'")
158 :server
(car (car link
))
159 :port
(or (cadr (pop link
)) erc-default-port
)
162 (error "Failed to create ('irc:/' style) ERC link")))))
164 (defun org-irc-get-erc-link ()
165 "Return an org compatible irc:/ link from an ERC buffer"
166 (let ((link (concat erc-server-announced-name
":"
169 (if (and (erc-default-target)
170 (erc-channel-p (erc-default-target))
171 (car (get-text-property (point) 'erc-data
)))
173 (let ((nick (car (get-text-property (point) 'erc-data
))))
174 (concat (erc-default-target) "/" nick
))
175 (erc-default-target)))))
177 (defun org-irc-visit-erc (link)
178 "Visit an ERC buffer based on criteria from the followed link"
179 (let* ((server (car (car link
)))
180 (port (or (cadr (pop link
)) erc-default-port
))
185 (let ((tmp-server-buf (erc-server-buffer)))
187 (with-current-buffer tmp-server-buf
189 (string= erc-session-port port
)
190 (string= erc-server-announced-name server
)
191 (setq server-buffer tmp-server-buf
)))))))))
193 (let ((chan-name (pop link
)))
194 ;; if we got a channel name then switch to it or join it
196 (let ((chan-buf (find-if
198 (string= (buffer-name x
) chan-name
))
202 (switch-to-buffer chan-buf
)
203 ;; if we got a nick, and they're in the chan,
204 ;; then start a chat with them
205 (let ((nick (pop link
)))
207 (if (find nick
(erc-get-server-nickname-list)
210 (goto-char (point-max))
211 (insert (concat nick
": ")))
212 (error "%s not found in %s" nick chan
)))))
214 (switch-to-buffer server-buffer
)
215 (erc-cmd-JOIN chan-name
))))
216 (switch-to-buffer server-buffer
)))
217 ;; no server match, make new connection
218 (erc-select :server server
:port port
))))
222 ;;; org-irc.el ends here