Merge from emacs--rel--22
[bpt/emacs.git] / lisp / org / org-irc.el
1 ;;; org-irc.el --- Store links to IRC sessions
2
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
4
5 ;; Author: Philip Jackson <emacs@shellarchive.co.uk>
6 ;; Keywords: erc, irc, link, org
7 ;; Version: 1.3
8
9 ;; This file is part of GNU Emacs.
10
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)
14 ;; any later version.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; Link to an IRC session. Only ERC has been implemented at the
29 ;; moment.
30 ;;
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.
34 ;;
35 ;; Please note that at the moment only ERC is supported. Other clients
36 ;; shouldn't be diffficult to add though.
37 ;;
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.
41 ;;
42 ;; Links within an org buffer might look like this:
43 ;;
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/]]
47 ;;
48 ;; If, when the resulting link is visited, there is no connection to a
49 ;; requested server then one will be created.
50
51 ;;; Code:
52
53 (require 'org)
54 (require 'erc)
55 (require 'erc-log)
56
57 (defvar org-irc-client 'erc
58 "The IRC client to act on")
59 (defvar org-irc-link-to-logs nil
60 "non-nil will store a link to the logs, nil will store an irc: style link")
61
62 (defvar erc-default-port) ; dynamically scoped from erc.el
63 (defvar erc-session-port) ; dynamically scoped form erc-backend.el
64 (defvar erc-server-announced-name) ; dynamically scoped form erc-backend.el
65
66 ;; Generic functions/config (extend these for other clients)
67
68 (add-to-list 'org-store-link-functions
69 'org-irc-store-link)
70
71 (org-add-link-type "irc" 'org-irc-visit nil)
72
73 (defun org-irc-visit (link)
74 "Dispatch to the correct visit function based on the client"
75 (let ((link (org-irc-parse-link link)))
76 (cond
77 ((eq org-irc-client 'erc)
78 (org-irc-visit-erc link))
79 (t
80 (error "erc only known client")))))
81
82 (defun org-irc-parse-link (link)
83 "Get a of irc link attributes where `link' looks like
84 server:port/chan/user (port, chan and user being optional)."
85 (let* ((parts (split-string link "/" t))
86 (len (length parts)))
87 (when (or (< len 1) (> len 3))
88 (error "Failed to parse link needed 1-3 parts, got %d." len))
89 (setcar parts (split-string (car parts) ":" t))
90 parts))
91
92 ;;;###autoload
93 (defun org-irc-store-link ()
94 "Dispatch to the appropreate function to store a link to
95 something IRC related"
96 (cond
97 ((eq major-mode 'erc-mode)
98 (org-irc-erc-store-link))))
99
100 (defun org-irc-elipsify-description (string &optional after)
101 "Strip starting and ending whitespace and replace any chars
102 that appear after the value in `after' with '...'"
103 (let* ((after (number-to-string (or after 30)))
104 (replace-map (list (cons "^[ \t]*" "")
105 (cons "[ \t]*$" "")
106 (cons (concat "^\\(.\\{" after
107 "\\}\\).*") "\\1..."))))
108 (mapc (lambda (x)
109 (when (string-match (car x) string)
110 (setq string (replace-match (cdr x) nil nil string))))
111 replace-map)
112 string))
113
114 ;; ERC specific functions
115
116 (defun org-irc-erc-get-line-from-log (erc-line)
117 "Find the most suitable line to link to from the erc logs. If
118 the user is on the erc-prompt then search backward for the first
119 non-blank line, otherwise return the current line. The result is
120 a cons of the filename and search string."
121 (erc-save-buffer-in-logs)
122 (with-current-buffer (find-file-noselect (erc-current-logfile))
123 (goto-char (point-max))
124 (list
125 (abbreviate-file-name buffer-file-name)
126 ;; can we get a '::' part?
127 (if (string= erc-line (erc-prompt))
128 (progn
129 (goto-char (point-at-bol))
130 (when (search-backward-regexp "^[^ ]" nil t)
131 (buffer-substring-no-properties (point-at-bol)
132 (point-at-eol))))
133 (when (search-backward erc-line nil t)
134 (buffer-substring-no-properties (point-at-bol)
135 (point-at-eol)))))))
136
137 (defun org-irc-erc-store-link ()
138 "Depending on the variable `org-irc-link-to-logs' store either
139 a link to the log file for the current session or an irc: link to
140 the session itself."
141 (if org-irc-link-to-logs
142 (let* ((erc-line (buffer-substring-no-properties
143 (point-at-bol) (point-at-eol)))
144 (parsed-line (org-irc-erc-get-line-from-log erc-line)))
145 (if (erc-logging-enabled nil)
146 (progn
147 (org-store-link-props
148 :type "file"
149 :description (concat "'" (org-irc-elipsify-description
150 (cadr parsed-line) 20)
151 "' from an IRC conversation")
152 :link (concat "file:" (car parsed-line) "::"
153 (cadr parsed-line)))
154 t)
155 (error "This ERC session is not being logged")))
156 (let* ((link-text (org-irc-get-erc-link))
157 (link (org-irc-parse-link link-text)))
158 (if link-text
159 (progn
160 (org-store-link-props
161 :type "irc"
162 :link (org-make-link "irc:/" link-text)
163 :description (concat "irc session '" link-text "'")
164 :server (car (car link))
165 :port (or (cadr (pop link)) erc-default-port)
166 :nick (pop link))
167 t)
168 (error "Failed to create ('irc:/' style) ERC link")))))
169
170 (defun org-irc-get-erc-link ()
171 "Return an org compatible irc:/ link from an ERC buffer"
172 (let ((link (concat erc-server-announced-name ":"
173 (number-to-string erc-session-port))))
174 (concat link "/"
175 (if (and (erc-default-target)
176 (erc-channel-p (erc-default-target))
177 (car (get-text-property (point) 'erc-data)))
178 ;; we can get a nick
179 (let ((nick (car (get-text-property (point) 'erc-data))))
180 (concat (erc-default-target) "/" nick))
181 (erc-default-target)))))
182
183 (defun org-irc-visit-erc (link)
184 "Visit an ERC buffer based on criteria from the followed link"
185 (let* ((server (car (car link)))
186 (port (or (cadr (pop link)) erc-default-port))
187 (server-buffer)
188 (buffer-list
189 (erc-buffer-filter
190 (lambda nil
191 (let ((tmp-server-buf (erc-server-buffer)))
192 (and tmp-server-buf
193 (with-current-buffer tmp-server-buf
194 (and
195 (string= erc-session-port port)
196 (string= erc-server-announced-name server)
197 (setq server-buffer tmp-server-buf)))))))))
198 (if buffer-list
199 (let ((chan-name (pop link)))
200 ;; if we got a channel name then switch to it or join it
201 (if chan-name
202 (let ((chan-buf (catch 'found
203 (dolist (x buffer-list)
204 (if (string= (buffer-name x) chan-name)
205 (throw 'found x))))))
206 (if chan-buf
207 (progn
208 (switch-to-buffer chan-buf)
209 ;; if we got a nick, and they're in the chan,
210 ;; then start a chat with them
211 (let ((nick (pop link)))
212 (when nick
213 (if (member nick (erc-get-server-nickname-list))
214 (progn
215 (goto-char (point-max))
216 (insert (concat nick ": ")))
217 (error "%s not found in %s" nick chan-name)))))
218 (progn
219 (switch-to-buffer server-buffer)
220 (erc-cmd-JOIN chan-name))))
221 (switch-to-buffer server-buffer)))
222 ;; no server match, make new connection
223 (erc-select :server server :port port))))
224
225 (provide 'org-irc)
226
227 ;; arch-tag: 018d7dda-53b8-4a35-ba92-6670939e525a
228 ;;; org-irc.el ends here