]> code.delx.au - gnu-emacs/blob - lisp/erc/erc-join.el
Update copyright notices for 2013.
[gnu-emacs] / lisp / erc / erc-join.el
1 ;;; erc-join.el --- autojoin channels on connect and reconnects
2
3 ;; Copyright (C) 2002-2004, 2006-2013 Free Software Foundation, Inc.
4
5 ;; Author: Alex Schroeder <alex@gnu.org>
6 ;; Maintainer: FSF
7 ;; Keywords: irc
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcAutoJoin
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This allows us to customize an `erc-autojoin-channels-alist'. As
28 ;; we /JOIN and /PART channels, this alist is updated to reflect our
29 ;; current setup, so that when we reconnect, we rejoin the same
30 ;; channels. The alist can be customized, so that the customized
31 ;; value will be used when we reconnect in our next Emacs session.
32
33 ;;; Code:
34
35 (require 'erc)
36 (require 'auth-source)
37 (eval-when-compile (require 'cl))
38
39 (defgroup erc-autojoin nil
40 "Enable autojoining."
41 :group 'erc)
42
43 ;;;###autoload (autoload 'erc-autojoin-mode "erc-join" nil t)
44 (define-erc-module autojoin nil
45 "Makes ERC autojoin on connects and reconnects."
46 ((add-hook 'erc-after-connect 'erc-autojoin-channels)
47 (add-hook 'erc-nickserv-identified-hook 'erc-autojoin-after-ident)
48 (add-hook 'erc-server-JOIN-functions 'erc-autojoin-add)
49 (add-hook 'erc-server-PART-functions 'erc-autojoin-remove))
50 ((remove-hook 'erc-after-connect 'erc-autojoin-channels)
51 (remove-hook 'erc-nickserv-identified-hook 'erc-autojoin-after-ident)
52 (remove-hook 'erc-server-JOIN-functions 'erc-autojoin-add)
53 (remove-hook 'erc-server-PART-functions 'erc-autojoin-remove)))
54
55 (defcustom erc-autojoin-channels-alist nil
56 "Alist of channels to autojoin on IRC networks.
57 Every element in the alist has the form (SERVER . CHANNELS).
58 SERVER is a regexp matching the server, and channels is the
59 list of channels to join.
60
61 If the channel(s) require channel keys for joining, the passwords
62 are found via auth-source. For instance, if you use ~/.authinfo
63 as your auth-source backend, then put something like the
64 following in that file:
65
66 machine irc.example.net login \"#fsf\" password sEcReT
67
68 Customize this variable to set the value for your first connect.
69 Once you are connected and join and part channels, this alist
70 keeps track of what channels you are on, and will join them
71 again when you get disconnected. When you restart Emacs, however,
72 those changes are lost, and the customization you saved the last
73 time is used again."
74 :group 'erc-autojoin
75 :type '(repeat (cons :tag "Server"
76 (regexp :tag "Name")
77 (repeat :tag "Channels"
78 (string :tag "Name")))))
79
80 (defcustom erc-autojoin-timing 'connect
81 "When ERC should attempt to autojoin a channel.
82 If the value is `connect', autojoin immediately on connecting.
83 If the value is `ident', autojoin after successful NickServ
84 identification, or after `erc-autojoin-delay' seconds.
85 Any other value means the same as `connect'."
86 :group 'erc-autojoin
87 :version "24.1"
88 :type '(choice (const :tag "On Connection" connect)
89 (const :tag "When Identified" ident)))
90
91 (defcustom erc-autojoin-delay 30
92 "Number of seconds to wait before attempting to autojoin channels.
93 This only takes effect if `erc-autojoin-timing' is `ident'.
94 If NickServ identification occurs before this delay expires, ERC
95 autojoins immediately at that time."
96 :group 'erc-autojoin
97 :version "24.1"
98 :type 'integer)
99
100 (defcustom erc-autojoin-domain-only t
101 "Truncate host name to the domain name when joining a server.
102 If non-nil, and a channel on the server a.b.c is joined, then
103 only b.c is used as the server for `erc-autojoin-channels-alist'.
104 This is important for networks that redirect you to other
105 servers, presumably in the same domain."
106 :group 'erc-autojoin
107 :type 'boolean)
108
109 (defvar erc--autojoin-timer nil)
110 (make-variable-buffer-local 'erc--autojoin-timer)
111
112 (defun erc-autojoin-channels-delayed (server nick buffer)
113 "Attempt to autojoin channels.
114 This is called from a timer set up by `erc-autojoin-channels'."
115 (if erc--autojoin-timer
116 (setq erc--autojoin-timer
117 (erc-cancel-timer erc--autojoin-timer)))
118 (with-current-buffer buffer
119 ;; Don't kick of another delayed autojoin or try to wait for
120 ;; another ident response:
121 (let ((erc-autojoin-delay -1)
122 (erc-autojoin-timing 'connect))
123 (erc-log "Delayed autojoin started (no ident success detected yet)")
124 (erc-autojoin-channels server nick))))
125
126 (defun erc-autojoin-after-ident (network nick)
127 "Autojoin channels in `erc-autojoin-channels-alist'.
128 This function is run from `erc-nickserv-identified-hook'."
129 (if erc--autojoin-timer
130 (setq erc--autojoin-timer
131 (erc-cancel-timer erc--autojoin-timer)))
132 (when (eq erc-autojoin-timing 'ident)
133 (let ((server (or erc-server-announced-name erc-session-server))
134 (joined (mapcar (lambda (buf)
135 (with-current-buffer buf (erc-default-target)))
136 (erc-channel-list erc-server-process))))
137 ;; We may already be in these channels, e.g. because the
138 ;; autojoin timer went off.
139 (dolist (l erc-autojoin-channels-alist)
140 (when (string-match (car l) server)
141 (dolist (chan (cdr l))
142 (unless (erc-member-ignore-case chan joined)
143 (erc-server-join-channel server chan)))))))
144 nil)
145
146 (defun erc-autojoin-channels (server nick)
147 "Autojoin channels in `erc-autojoin-channels-alist'."
148 (if (eq erc-autojoin-timing 'ident)
149 ;; Prepare the delayed autojoin timer, in case ident doesn't
150 ;; happen within the allotted time limit:
151 (when (> erc-autojoin-delay 0)
152 (setq erc--autojoin-timer
153 (run-with-timer erc-autojoin-delay nil
154 'erc-autojoin-channels-delayed
155 server nick (current-buffer))))
156 ;; `erc-autojoin-timing' is `connect':
157 (dolist (l erc-autojoin-channels-alist)
158 (when (string-match (car l) server)
159 (dolist (chan (cdr l))
160 (erc-server-join-channel server chan)))))
161 ;; Return nil to avoid stomping on any other hook funcs.
162 nil)
163
164 (defun erc-server-join-channel (server channel)
165 (let* ((secret (plist-get (nth 0 (auth-source-search
166 :max 1
167 :host server
168 :port "irc"
169 :user channel))
170 :secret))
171 (password (if (functionp secret)
172 (funcall secret)
173 secret)))
174 (erc-server-send (concat "join " channel
175 (if password
176 (concat " " password)
177 "")))))
178
179 (defun erc-autojoin-add (proc parsed)
180 "Add the channel being joined to `erc-autojoin-channels-alist'."
181 (let* ((chnl (erc-response.contents parsed))
182 (nick (car (erc-parse-user (erc-response.sender parsed))))
183 (server (with-current-buffer (process-buffer proc)
184 (or erc-server-announced-name erc-session-server))))
185 (when (erc-current-nick-p nick)
186 (when (and erc-autojoin-domain-only
187 (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server))
188 (setq server (match-string 1 server)))
189 (let ((elem (assoc server erc-autojoin-channels-alist)))
190 (if elem
191 (unless (member chnl (cdr elem))
192 (setcdr elem (cons chnl (cdr elem))))
193 (setq erc-autojoin-channels-alist
194 (cons (list server chnl)
195 erc-autojoin-channels-alist))))))
196 ;; We must return nil to tell ERC to continue running the other
197 ;; functions.
198 nil)
199
200 ;; (erc-parse-user "kensanata!~user@dclient217-162-233-228.hispeed.ch")
201
202 (defun erc-autojoin-remove (proc parsed)
203 "Remove the channel being left from `erc-autojoin-channels-alist'."
204 (let* ((chnl (car (erc-response.command-args parsed)))
205 (nick (car (erc-parse-user (erc-response.sender parsed))))
206 (server (with-current-buffer (process-buffer proc)
207 (or erc-server-announced-name erc-session-server))))
208 (when (erc-current-nick-p nick)
209 (when (and erc-autojoin-domain-only
210 (string-match "[^.\n]+\\.\\([^.\n]+\\.[^.\n]+\\)$" server))
211 (setq server (match-string 1 server)))
212 (let ((elem (assoc server erc-autojoin-channels-alist)))
213 (when elem
214 (setcdr elem (delete chnl (cdr elem)))
215 (unless (cdr elem)
216 (setq erc-autojoin-channels-alist
217 (delete elem erc-autojoin-channels-alist)))))))
218 ;; We must return nil to tell ERC to continue running the other
219 ;; functions.
220 nil)
221
222 (provide 'erc-join)
223
224 ;;; erc-join.el ends here
225 ;;
226 ;; Local Variables:
227 ;; indent-tabs-mode: t
228 ;; tab-width: 8
229 ;; End:
230