b25a10dc5cab3ce87219c8052605b62369d61db8
[bpt/emacs.git] / lisp / erc / erc-services.el
1 ;;; erc-services.el --- Identify to NickServ
2
3 ;; Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 3, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
21
22 ;;; Commentary:
23
24 ;; There are two ways to go about identifying yourself automatically to
25 ;; NickServ with this module. The more secure way is to listen for identify
26 ;; requests from the user NickServ. Another way is to identify yourself to
27 ;; NickServ directly after a successful connection and every time you change
28 ;; your nickname. This method is rather insecure, though, because no checks
29 ;; are made to test if NickServ is the real NickServ for a given network or
30 ;; server.
31
32 ;; As a default, ERC has the data for the official nickname services on
33 ;; the networks Austnet, BrasNET, Dalnet, freenode, GalaxyNet, GRnet,
34 ;; and Slashnet. You can add more by using M-x customize-variable RET
35 ;; erc-nickserv-alist.
36
37 ;; Usage:
38 ;;
39 ;; Put into your .emacs:
40 ;;
41 ;; (require 'erc-services)
42 ;; (erc-services-mode 1)
43 ;;
44 ;; Add your nickname and NickServ password to `erc-nickserv-passwords'.
45 ;; Using the freenode network as an example:
46 ;;
47 ;; (setq erc-nickserv-passwords '((freenode (("nickname" "password")))))
48 ;;
49 ;; The default automatic identification mode is autodetection of NickServ
50 ;; identify requests. Set the variable `erc-nickserv-identify-mode' if
51 ;; you'd like to change this behavior. You can also change the way
52 ;; automatic identification is handled by using:
53 ;;
54 ;; M-x erc-nickserv-identify-mode
55 ;;
56 ;; If you'd rather not identify yourself automatically but would like access
57 ;; to the functions contained in this file, just load this file without
58 ;; enabling `erc-services-mode'.
59 ;;
60
61 ;;; Code:
62
63 (require 'erc)
64 (require 'erc-networks)
65 (eval-when-compile (require 'cl))
66
67 ;; Customization:
68
69 (defgroup erc-services nil
70 "Configuration for IRC services.
71
72 On some networks, there exists a special type of automated irc bot,
73 called Services. Those usually allow you to register your nickname,
74 post/read memos to other registered users who are currently offline,
75 and do various other things.
76
77 This group allows you to set variables to somewhat automate
78 communication with those Services."
79 :group 'erc)
80
81 (defcustom erc-nickserv-identify-mode 'both
82 "The mode which is used when identifying to Nickserv.
83
84 Possible settings are:.
85
86 'autodetect - Identify when the real Nickserv sends an identify request.
87 'nick-change - Identify when you log in or change your nickname.
88 'both - Do the former if the network supports it, otherwise do the
89 latter.
90 nil - Disables automatic Nickserv identification.
91
92 You can also use M-x erc-nickserv-identify-mode to change modes."
93 :group 'erc-services
94 :type '(choice (const autodetect)
95 (const nick-change)
96 (const both)
97 (const nil))
98 :set (lambda (sym val)
99 (set sym val)
100 ;; avoid recursive load at startup
101 (when (featurep 'erc-services)
102 (erc-nickserv-identify-mode val))))
103
104 ;;;###autoload (autoload 'erc-services-mode "erc-services" nil t)
105 (define-erc-module services nickserv
106 "This mode automates communication with services."
107 ((erc-nickserv-identify-mode erc-nickserv-identify-mode))
108 ((remove-hook 'erc-server-NOTICE-functions
109 'erc-nickserv-identify-autodetect)
110 (remove-hook 'erc-after-connect
111 'erc-nickserv-identify-on-connect)
112 (remove-hook 'erc-nick-changed-functions
113 'erc-nickserv-identify-on-nick-change)
114 (remove-hook 'erc-server-NOTICE-functions
115 'erc-nickserv-identification-autodetect)))
116
117 ;;;###autoload
118 (defun erc-nickserv-identify-mode (mode)
119 "Set up hooks according to which MODE the user has chosen."
120 (interactive
121 (list (intern (completing-read
122 "Choose Nickserv identify mode (RET to disable): "
123 '(("autodetect") ("nick-change") ("both")) nil t))))
124 (add-hook 'erc-server-NOTICE-functions
125 'erc-nickserv-identification-autodetect)
126 (unless erc-networks-mode
127 ;; Force-enable networks module, because we need it to set
128 ;; erc-network for us.
129 (erc-networks-enable))
130 (cond ((eq mode 'autodetect)
131 (setq erc-nickserv-identify-mode 'autodetect)
132 (add-hook 'erc-server-NOTICE-functions
133 'erc-nickserv-identify-autodetect)
134 (remove-hook 'erc-nick-changed-functions
135 'erc-nickserv-identify-on-nick-change)
136 (remove-hook 'erc-after-connect
137 'erc-nickserv-identify-on-connect))
138 ((eq mode 'nick-change)
139 (setq erc-nickserv-identify-mode 'nick-change)
140 (add-hook 'erc-after-connect
141 'erc-nickserv-identify-on-connect)
142 (add-hook 'erc-nick-changed-functions
143 'erc-nickserv-identify-on-nick-change)
144 (remove-hook 'erc-server-NOTICE-functions
145 'erc-nickserv-identify-autodetect))
146 ((eq mode 'both)
147 (setq erc-nickserv-identify-mode 'both)
148 (add-hook 'erc-server-NOTICE-functions
149 'erc-nickserv-identify-autodetect)
150 (add-hook 'erc-after-connect
151 'erc-nickserv-identify-on-connect)
152 (add-hook 'erc-nick-changed-functions
153 'erc-nickserv-identify-on-nick-change))
154 (t
155 (setq erc-nickserv-identify-mode nil)
156 (remove-hook 'erc-server-NOTICE-functions
157 'erc-nickserv-identify-autodetect)
158 (remove-hook 'erc-after-connect
159 'erc-nickserv-identify-on-connect)
160 (remove-hook 'erc-nick-changed-functions
161 'erc-nickserv-identify-on-nick-change)
162 (remove-hook 'erc-server-NOTICE-functions
163 'erc-nickserv-identification-autodetect))))
164
165 (defcustom erc-prompt-for-nickserv-password t
166 "Ask for the password when identifying to NickServ."
167 :group 'erc-services
168 :type 'boolean)
169
170 (defcustom erc-nickserv-passwords nil
171 "Passwords used when identifying to NickServ automatically.
172
173 Example of use:
174 (setq erc-nickserv-passwords
175 '((freenode ((\"nick-one\" . \"password\")
176 (\"nick-two\" . \"password\")))
177 (DALnet ((\"nick\" . \"password\")))))"
178 :group 'erc-services
179 :type '(repeat
180 (list :tag "Network"
181 (choice :tag "Network name"
182 (const Ars)
183 (const Austnet)
184 (const Azzurra)
185 (const BitlBee)
186 (const BRASnet)
187 (const DALnet)
188 (const freenode)
189 (const GalaxyNet)
190 (const GRnet)
191 (const iip)
192 (const OFTC)
193 (const QuakeNet)
194 (const Rizon)
195 (const SlashNET)
196 (symbol :tag "Network name"))
197 (repeat :tag "Nickname and password"
198 (cons :tag "Identity"
199 (string :tag "Nick")
200 (string :tag "Password"))))))
201
202 ;; Variables:
203
204 (defcustom erc-nickserv-alist
205 '((Ars
206 nil nil
207 "Census"
208 "IDENTIFY" nil nil nil)
209 (Austnet
210 "NickOP!service@austnet.org"
211 "/msg\\s-NickOP@austnet.org\\s-identify\\s-<password>"
212 "nickop@austnet.org"
213 "identify" nil nil nil)
214 (Azzurra
215 "NickServ!service@azzurra.org"
216 "\ 2/ns\\s-IDENTIFY\\s-password\ 2"
217 "NickServ"
218 "IDENTIFY" nil nil nil)
219 (BitlBee
220 nil nil
221 "&bitlbee"
222 "identify" nil nil nil)
223 (BRASnet
224 "NickServ!services@brasnet.org"
225 "\ 2/NickServ\\s-IDENTIFY\\s-\1fsenha\1f\ 2"
226 "NickServ"
227 "IDENTIFY" nil "" nil)
228 (DALnet
229 "NickServ!service@dal.net"
230 "/msg\\s-NickServ@services.dal.net\\s-IDENTIFY\\s-<password>"
231 "NickServ@services.dal.net"
232 "IDENTIFY" nil nil nil)
233 (freenode
234 "NickServ!NickServ@services."
235 ;; freenode also accepts a password at login, see the `erc'
236 ;; :password argument.
237 "/msg\\s-NickServ\\s-\ 2IDENTIFY\ 2\\s-<password>"
238 "NickServ"
239 "IDENTIFY" nil nil
240 "Password\\s-accepted\\s--\\s-you\\s-are\\s-now\\s-recognized")
241 (GalaxyNet
242 "NS!nickserv@galaxynet.org"
243 "Please\\s-change\\s-nicks\\s-or\\s-authenticate."
244 "NS@services.galaxynet.org"
245 "AUTH" t nil nil)
246 (GRnet
247 "NickServ!service@irc.gr"
248 "This\\s-nickname\\s-is\\s-registered\\s-and\\s-protected."
249 "NickServ"
250 "IDENTIFY" nil nil
251 "Password\\s-accepted\\s--\\s-you\\s-are\\s-now\\s-recognized.")
252 (iip
253 "Trent@anon.iip"
254 "type\\s-/squery\\s-Trent\\s-identify\\s-<password>"
255 "Trent@anon.iip"
256 "IDENTIFY" nil "SQUERY" nil)
257 (OFTC
258 "NickServ!services@services.oftc.net"
259 ;; OFTC's NickServ doesn't ask you to identify anymore.
260 nil
261 "NickServ"
262 "IDENTIFY" nil nil
263 "You\\s-are\\s-successfully\\s-identified\\s-as\\s-\ 2")
264 (Rizon
265 "NickServ!service@rizon.net"
266 "This\\s-nickname\\s-is\\s-registered\\s-and\\s-protected."
267 "NickServ"
268 "IDENTIFY" nil nil
269 "Password\\s-accepted\\s--\\s-you\\s-are\\s-now\\s-recognized.")
270 (QuakeNet
271 nil nil
272 "Q@CServe.quakenet.org"
273 "auth" t nil nil)
274 (SlashNET
275 "NickServ!services@services.slashnet.org"
276 "/msg\\s-NickServ\\s-IDENTIFY\\s-\1fpassword"
277 "NickServ@services.slashnet.org"
278 "IDENTIFY" nil nil nil))
279 "Alist of NickServer details, sorted by network.
280 Every element in the list has the form
281 \(SYMBOL NICKSERV REGEXP NICK KEYWORD USE-CURRENT ANSWER SUCCESS-REGEXP)
282
283 SYMBOL is a network identifier, a symbol, as used in `erc-networks-alist'.
284 NICKSERV is the description of the nickserv in the form nick!user@host.
285 REGEXP is a regular expression matching the message from nickserv.
286 NICK is nickserv's nickname. Use nick@server where necessary/possible.
287 KEYWORD is the keyword to use in the reply message to identify yourself.
288 USE-CURRENT indicates whether the current nickname must be used when
289 identifying.
290 ANSWER is the command to use for the answer. The default is 'privmsg.
291 SUCCESS-REGEXP is a regular expression matching the message nickserv
292 sends when you've successfully identified.
293 The last two elements are optional."
294 :group 'erc-services
295 :type '(repeat
296 (list :tag "Nickserv data"
297 (symbol :tag "Network name")
298 (choice (string :tag "Nickserv's nick!user@host")
299 (const :tag "No message sent by Nickserv" nil))
300 (choice (regexp :tag "Identify request sent by Nickserv")
301 (const :tag "No message sent by Nickserv" nil))
302 (string :tag "Identify to")
303 (string :tag "Identify keyword")
304 (boolean :tag "Use current nick in identify message?")
305 (choice :tag "Command to use (optional)"
306 (string :tag "Command")
307 (const :tag "No special command necessary" nil)))))
308
309 (defsubst erc-nickserv-alist-sender (network &optional entry)
310 (nth 1 (or entry (assoc network erc-nickserv-alist))))
311
312 (defsubst erc-nickserv-alist-regexp (network &optional entry)
313 (nth 2 (or entry (assoc network erc-nickserv-alist))))
314
315 (defsubst erc-nickserv-alist-nickserv (network &optional entry)
316 (nth 3 (or entry (assoc network erc-nickserv-alist))))
317
318 (defsubst erc-nickserv-alist-ident-keyword (network &optional entry)
319 (nth 4 (or entry (assoc network erc-nickserv-alist))))
320
321 (defsubst erc-nickserv-alist-use-nick-p (network &optional entry)
322 (nth 5 (or entry (assoc network erc-nickserv-alist))))
323
324 (defsubst erc-nickserv-alist-ident-command (network &optional entry)
325 (nth 6 (or entry (assoc network erc-nickserv-alist))))
326
327 (defsubst erc-nickserv-alist-identified-regexp (network &optional entry)
328 (nth 7 (or entry (assoc network erc-nickserv-alist))))
329
330 ;; Functions:
331
332 (defcustom erc-nickserv-identified-hook nil
333 "Run this hook when NickServ acknowledged successful identification.
334 Hooks are called with arguments (NETWORK NICK)."
335 :group 'erc-services
336 :type 'hook)
337
338 (defun erc-nickserv-identification-autodetect (proc parsed)
339 "Check for NickServ's successful identification notice.
340 Make sure it is the real NickServ for this network and that it has
341 specifically confirmed a successful identification attempt.
342 If this is the case, run `erc-nickserv-identified-hook'."
343 (let* ((network (erc-network))
344 (sender (erc-nickserv-alist-sender network))
345 (success-regex (erc-nickserv-alist-identified-regexp network))
346 (sspec (erc-response.sender parsed))
347 (nick (car (erc-response.command-args parsed)))
348 (msg (erc-response.contents parsed)))
349 ;; continue only if we're sure it's the real nickserv for this network
350 ;; and it's told us we've successfully identified
351 (when (and sender (equal sspec sender)
352 success-regex
353 (string-match success-regex msg))
354 (erc-log "NickServ IDENTIFY success notification detected")
355 (run-hook-with-args 'erc-nickserv-identified-hook network nick)
356 nil)))
357
358 (defun erc-nickserv-identify-autodetect (proc parsed)
359 "Identify to NickServ when an identify request is received.
360 Make sure it is the real NickServ for this network.
361 If `erc-prompt-for-nickserv-password' is non-nil, prompt the user for the
362 password for this nickname, otherwise try to send it automatically."
363 (unless (and (null erc-nickserv-passwords)
364 (null erc-prompt-for-nickserv-password))
365 (let* ((network (erc-network))
366 (sender (erc-nickserv-alist-sender network))
367 (identify-regex (erc-nickserv-alist-regexp network))
368 (sspec (erc-response.sender parsed))
369 (nick (car (erc-response.command-args parsed)))
370 (msg (erc-response.contents parsed)))
371 ;; continue only if we're sure it's the real nickserv for this network
372 ;; and it's asked us to identify
373 (when (and sender (equal sspec sender)
374 identify-regex
375 (string-match identify-regex msg))
376 (erc-log "NickServ IDENTIFY request detected")
377 (erc-nickserv-call-identify-function nick)
378 nil))))
379
380 (defun erc-nickserv-identify-on-connect (server nick)
381 "Identify to Nickserv after the connection to the server is established."
382 (unless (or (and (null erc-nickserv-passwords)
383 (null erc-prompt-for-nickserv-password))
384 (and (eq erc-nickserv-identify-mode 'both)
385 (erc-nickserv-alist-regexp (erc-network))))
386 (erc-nickserv-call-identify-function nick)))
387
388 (defun erc-nickserv-identify-on-nick-change (nick old-nick)
389 "Identify to Nickserv whenever your nick changes."
390 (unless (or (and (null erc-nickserv-passwords)
391 (null erc-prompt-for-nickserv-password))
392 (and (eq erc-nickserv-identify-mode 'both)
393 (erc-nickserv-alist-regexp (erc-network))))
394 (erc-nickserv-call-identify-function nick)))
395
396 (defun erc-nickserv-call-identify-function (nickname)
397 "Call `erc-nickserv-identify' interactively or run it with NICKNAME's
398 password.
399 The action is determined by the value of `erc-prompt-for-nickserv-password'."
400 (if erc-prompt-for-nickserv-password
401 (call-interactively 'erc-nickserv-identify)
402 (when erc-nickserv-passwords
403 (erc-nickserv-identify
404 (cdr (assoc nickname
405 (nth 1 (assoc (erc-network)
406 erc-nickserv-passwords))))))))
407
408 ;;;###autoload
409 (defun erc-nickserv-identify (password)
410 "Send an \"identify <PASSWORD>\" message to NickServ.
411 When called interactively, read the password using `read-passwd'."
412 (interactive
413 (list (read-passwd
414 (format "NickServ password for %s on %s (RET to cancel): "
415 (erc-current-nick)
416 (or (and (erc-network)
417 (symbol-name (erc-network)))
418 "Unknown network")))))
419 (when (and password (not (string= "" password)))
420 (let* ((erc-auto-discard-away nil)
421 (network (erc-network))
422 (nickserv-info (assoc network erc-nickserv-alist))
423 (nickserv (or (erc-nickserv-alist-nickserv nil nickserv-info)
424 "NickServ"))
425 (identify-word (or (erc-nickserv-alist-ident-keyword
426 nil nickserv-info)
427 "IDENTIFY"))
428 (nick (if (erc-nickserv-alist-use-nick-p nil nickserv-info)
429 (concat (erc-current-nick) " ")
430 ""))
431 (msgtype (or (erc-nickserv-alist-ident-command nil nickserv-info)
432 "PRIVMSG")))
433 (erc-message msgtype
434 (concat nickserv " " identify-word " " nick password)))))
435
436 (provide 'erc-services)
437
438 ;;; erc-services.el ends here
439 ;;
440 ;; Local Variables:
441 ;; indent-tabs-mode: t
442 ;; tab-width: 8
443 ;; End:
444
445 ;; arch-tag: d401c8aa-d938-4255-96a9-3efb64c47e58