Merge from emacs-24; up to 2012-05-04T19:17:01Z!monnier@iro.umontreal.ca
[emacs.git] / lisp / erc / erc-services.el
blob5986d81efed234511975a7f4e01b4d0ab6401d59
1 ;;; erc-services.el --- Identify to NickServ
3 ;; Copyright (C) 2002-2004, 2006-2012 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
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 of the License, or
10 ;; (at your option) any later version.
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.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20 ;;; Commentary:
22 ;; There are two ways to go about identifying yourself automatically to
23 ;; NickServ with this module. The more secure way is to listen for identify
24 ;; requests from the user NickServ. Another way is to identify yourself to
25 ;; NickServ directly after a successful connection and every time you change
26 ;; your nickname. This method is rather insecure, though, because no checks
27 ;; are made to test if NickServ is the real NickServ for a given network or
28 ;; server.
30 ;; As a default, ERC has the data for the official nickname services on
31 ;; the networks Austnet, BrasNET, Dalnet, freenode, GalaxyNet, GRnet,
32 ;; and Slashnet. You can add more by using M-x customize-variable RET
33 ;; erc-nickserv-alist.
35 ;; Usage:
37 ;; Put into your .emacs:
39 ;; (require 'erc-services)
40 ;; (erc-services-mode 1)
42 ;; Add your nickname and NickServ password to `erc-nickserv-passwords'.
43 ;; Using the freenode network as an example:
45 ;; (setq erc-nickserv-passwords '((freenode (("nickname" "password")))))
47 ;; The default automatic identification mode is autodetection of NickServ
48 ;; identify requests. Set the variable `erc-nickserv-identify-mode' if
49 ;; you'd like to change this behavior. You can also change the way
50 ;; automatic identification is handled by using:
52 ;; M-x erc-nickserv-identify-mode
54 ;; If you'd rather not identify yourself automatically but would like access
55 ;; to the functions contained in this file, just load this file without
56 ;; enabling `erc-services-mode'.
59 ;;; Code:
61 (require 'erc)
62 (require 'erc-networks)
63 (eval-when-compile (require 'cl))
65 ;; Customization:
67 (defgroup erc-services nil
68 "Configuration for IRC services.
70 On some networks, there exists a special type of automated irc bot,
71 called Services. Those usually allow you to register your nickname,
72 post/read memos to other registered users who are currently offline,
73 and do various other things.
75 This group allows you to set variables to somewhat automate
76 communication with those Services."
77 :group 'erc)
79 (defcustom erc-nickserv-identify-mode 'both
80 "The mode which is used when identifying to Nickserv.
82 Possible settings are:.
84 'autodetect - Identify when the real Nickserv sends an identify request.
85 'nick-change - Identify when you log in or change your nickname.
86 'both - Do the former if the network supports it, otherwise do the
87 latter.
88 nil - Disables automatic Nickserv identification.
90 You can also use M-x erc-nickserv-identify-mode to change modes."
91 :group 'erc-services
92 :type '(choice (const autodetect)
93 (const nick-change)
94 (const both)
95 (const nil))
96 :set (lambda (sym val)
97 (set sym val)
98 ;; avoid recursive load at startup
99 (when (featurep 'erc-services)
100 (erc-nickserv-identify-mode val))))
102 ;;;###autoload (autoload 'erc-services-mode "erc-services" nil t)
103 (define-erc-module services nickserv
104 "This mode automates communication with services."
105 ((erc-nickserv-identify-mode erc-nickserv-identify-mode))
106 ((remove-hook 'erc-server-NOTICE-functions
107 'erc-nickserv-identify-autodetect)
108 (remove-hook 'erc-after-connect
109 'erc-nickserv-identify-on-connect)
110 (remove-hook 'erc-nick-changed-functions
111 'erc-nickserv-identify-on-nick-change)
112 (remove-hook 'erc-server-NOTICE-functions
113 'erc-nickserv-identification-autodetect)))
115 ;;;###autoload
116 (defun erc-nickserv-identify-mode (mode)
117 "Set up hooks according to which MODE the user has chosen."
118 (interactive
119 (list (intern (completing-read
120 "Choose Nickserv identify mode (RET to disable): "
121 '(("autodetect") ("nick-change") ("both")) nil t))))
122 (add-hook 'erc-server-NOTICE-functions
123 'erc-nickserv-identification-autodetect)
124 (unless erc-networks-mode
125 ;; Force-enable networks module, because we need it to set
126 ;; erc-network for us.
127 (erc-networks-enable))
128 (cond ((eq mode 'autodetect)
129 (setq erc-nickserv-identify-mode 'autodetect)
130 (add-hook 'erc-server-NOTICE-functions
131 'erc-nickserv-identify-autodetect)
132 (remove-hook 'erc-nick-changed-functions
133 'erc-nickserv-identify-on-nick-change)
134 (remove-hook 'erc-after-connect
135 'erc-nickserv-identify-on-connect))
136 ((eq mode 'nick-change)
137 (setq erc-nickserv-identify-mode 'nick-change)
138 (add-hook 'erc-after-connect
139 'erc-nickserv-identify-on-connect)
140 (add-hook 'erc-nick-changed-functions
141 'erc-nickserv-identify-on-nick-change)
142 (remove-hook 'erc-server-NOTICE-functions
143 'erc-nickserv-identify-autodetect))
144 ((eq mode 'both)
145 (setq erc-nickserv-identify-mode 'both)
146 (add-hook 'erc-server-NOTICE-functions
147 'erc-nickserv-identify-autodetect)
148 (add-hook 'erc-after-connect
149 'erc-nickserv-identify-on-connect)
150 (add-hook 'erc-nick-changed-functions
151 'erc-nickserv-identify-on-nick-change))
153 (setq erc-nickserv-identify-mode nil)
154 (remove-hook 'erc-server-NOTICE-functions
155 'erc-nickserv-identify-autodetect)
156 (remove-hook 'erc-after-connect
157 'erc-nickserv-identify-on-connect)
158 (remove-hook 'erc-nick-changed-functions
159 'erc-nickserv-identify-on-nick-change)
160 (remove-hook 'erc-server-NOTICE-functions
161 'erc-nickserv-identification-autodetect))))
163 (defcustom erc-prompt-for-nickserv-password t
164 "Ask for the password when identifying to NickServ."
165 :group 'erc-services
166 :type 'boolean)
168 (defcustom erc-nickserv-passwords nil
169 "Passwords used when identifying to NickServ automatically.
171 Example of use:
172 (setq erc-nickserv-passwords
173 '((freenode ((\"nick-one\" . \"password\")
174 (\"nick-two\" . \"password\")))
175 (DALnet ((\"nick\" . \"password\")))))"
176 :group 'erc-services
177 :type '(repeat
178 (list :tag "Network"
179 (choice :tag "Network name"
180 (const Ars)
181 (const Austnet)
182 (const Azzurra)
183 (const BitlBee)
184 (const BRASnet)
185 (const DALnet)
186 (const freenode)
187 (const GalaxyNet)
188 (const GRnet)
189 (const iip)
190 (const OFTC)
191 (const QuakeNet)
192 (const Rizon)
193 (const SlashNET)
194 (symbol :tag "Network name"))
195 (repeat :tag "Nickname and password"
196 (cons :tag "Identity"
197 (string :tag "Nick")
198 (string :tag "Password"
199 :secret ?*))))))
201 ;; Variables:
203 (defcustom erc-nickserv-alist
204 '((Ars
205 nil nil
206 "Census"
207 "IDENTIFY" nil nil nil)
208 (Austnet
209 "NickOP!service@austnet.org"
210 "/msg\\s-NickOP@austnet.org\\s-identify\\s-<password>"
211 "nickop@austnet.org"
212 "identify" nil nil nil)
213 (Azzurra
214 "NickServ!service@azzurra.org"
215 "\x02/ns\\s-IDENTIFY\\s-password\x02"
216 "NickServ"
217 "IDENTIFY" nil nil nil)
218 (BitlBee
219 nil nil
220 "&bitlbee"
221 "identify" nil nil nil)
222 (BRASnet
223 "NickServ!services@brasnet.org"
224 "\x02/NickServ\\s-IDENTIFY\\s-\x1fsenha\x1f\x02"
225 "NickServ"
226 "IDENTIFY" nil "" nil)
227 (DALnet
228 "NickServ!service@dal.net"
229 "/msg\\s-NickServ@services.dal.net\\s-IDENTIFY\\s-<password>"
230 "NickServ@services.dal.net"
231 "IDENTIFY" nil nil nil)
232 (freenode
233 "NickServ!NickServ@services."
234 ;; freenode also accepts a password at login, see the `erc'
235 ;; :password argument.
236 "This\\s-nickname\\s-is\\s-registered.\\s-Please\\s-choose"
237 "NickServ"
238 "IDENTIFY" nil nil
239 ;; See also the 901 response code message.
240 "You\\s-are\\s-now\\s-identified\\s-for\\s-")
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.
261 "NickServ"
262 "IDENTIFY" nil nil
263 "You\\s-are\\s-successfully\\s-identified\\s-as\\s-\x02")
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-\x1fpassword"
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)
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 (choice :tag "Detect Success"
309 (regexp :tag "Pattern to match")
310 (const :tag "Do not try to detect success" nil)))))
313 (defsubst erc-nickserv-alist-sender (network &optional entry)
314 (nth 1 (or entry (assoc network erc-nickserv-alist))))
316 (defsubst erc-nickserv-alist-regexp (network &optional entry)
317 (nth 2 (or entry (assoc network erc-nickserv-alist))))
319 (defsubst erc-nickserv-alist-nickserv (network &optional entry)
320 (nth 3 (or entry (assoc network erc-nickserv-alist))))
322 (defsubst erc-nickserv-alist-ident-keyword (network &optional entry)
323 (nth 4 (or entry (assoc network erc-nickserv-alist))))
325 (defsubst erc-nickserv-alist-use-nick-p (network &optional entry)
326 (nth 5 (or entry (assoc network erc-nickserv-alist))))
328 (defsubst erc-nickserv-alist-ident-command (network &optional entry)
329 (nth 6 (or entry (assoc network erc-nickserv-alist))))
331 (defsubst erc-nickserv-alist-identified-regexp (network &optional entry)
332 (nth 7 (or entry (assoc network erc-nickserv-alist))))
334 ;; Functions:
336 (defcustom erc-nickserv-identified-hook nil
337 "Run this hook when NickServ acknowledged successful identification.
338 Hooks are called with arguments (NETWORK NICK)."
339 :group 'erc-services
340 :type 'hook)
342 (defun erc-nickserv-identification-autodetect (proc parsed)
343 "Check for NickServ's successful identification notice.
344 Make sure it is the real NickServ for this network and that it has
345 specifically confirmed a successful identification attempt.
346 If this is the case, run `erc-nickserv-identified-hook'."
347 (let* ((network (erc-network))
348 (sender (erc-nickserv-alist-sender network))
349 (success-regex (erc-nickserv-alist-identified-regexp network))
350 (sspec (erc-response.sender parsed))
351 (nick (car (erc-response.command-args parsed)))
352 (msg (erc-response.contents parsed)))
353 ;; continue only if we're sure it's the real nickserv for this network
354 ;; and it's told us we've successfully identified
355 (when (and sender (equal sspec sender)
356 success-regex
357 (string-match success-regex msg))
358 (erc-log "NickServ IDENTIFY success notification detected")
359 (run-hook-with-args 'erc-nickserv-identified-hook network nick)
360 nil)))
362 (defun erc-nickserv-identify-autodetect (proc parsed)
363 "Identify to NickServ when an identify request is received.
364 Make sure it is the real NickServ for this network.
365 If `erc-prompt-for-nickserv-password' is non-nil, prompt the user for the
366 password for this nickname, otherwise try to send it automatically."
367 (unless (and (null erc-nickserv-passwords)
368 (null erc-prompt-for-nickserv-password))
369 (let* ((network (erc-network))
370 (sender (erc-nickserv-alist-sender network))
371 (identify-regex (erc-nickserv-alist-regexp network))
372 (sspec (erc-response.sender parsed))
373 (nick (car (erc-response.command-args parsed)))
374 (msg (erc-response.contents parsed)))
375 ;; continue only if we're sure it's the real nickserv for this network
376 ;; and it's asked us to identify
377 (when (and sender (equal sspec sender)
378 identify-regex
379 (string-match identify-regex msg))
380 (erc-log "NickServ IDENTIFY request detected")
381 (erc-nickserv-call-identify-function nick)
382 nil))))
384 (defun erc-nickserv-identify-on-connect (server nick)
385 "Identify to Nickserv after the connection to the server is established."
386 (unless (or (and (null erc-nickserv-passwords)
387 (null erc-prompt-for-nickserv-password))
388 (and (eq erc-nickserv-identify-mode 'both)
389 (erc-nickserv-alist-regexp (erc-network))))
390 (erc-nickserv-call-identify-function nick)))
392 (defun erc-nickserv-identify-on-nick-change (nick old-nick)
393 "Identify to Nickserv whenever your nick changes."
394 (unless (or (and (null erc-nickserv-passwords)
395 (null erc-prompt-for-nickserv-password))
396 (and (eq erc-nickserv-identify-mode 'both)
397 (erc-nickserv-alist-regexp (erc-network))))
398 (erc-nickserv-call-identify-function nick)))
400 (defun erc-nickserv-call-identify-function (nickname)
401 "Call `erc-nickserv-identify' interactively or run it with NICKNAME's
402 password.
403 The action is determined by the value of `erc-prompt-for-nickserv-password'."
404 (if erc-prompt-for-nickserv-password
405 (call-interactively 'erc-nickserv-identify)
406 (when erc-nickserv-passwords
407 (erc-nickserv-identify
408 (cdr (assoc nickname
409 (nth 1 (assoc (erc-network)
410 erc-nickserv-passwords))))))))
412 ;;;###autoload
413 (defun erc-nickserv-identify (password)
414 "Send an \"identify <PASSWORD>\" message to NickServ.
415 When called interactively, read the password using `read-passwd'."
416 (interactive
417 (list (read-passwd
418 (format "NickServ password for %s on %s (RET to cancel): "
419 (erc-current-nick)
420 (or (and (erc-network)
421 (symbol-name (erc-network)))
422 "Unknown network")))))
423 (when (and password (not (string= "" password)))
424 (let* ((erc-auto-discard-away nil)
425 (network (erc-network))
426 (nickserv-info (assoc network erc-nickserv-alist))
427 (nickserv (or (erc-nickserv-alist-nickserv nil nickserv-info)
428 "NickServ"))
429 (identify-word (or (erc-nickserv-alist-ident-keyword
430 nil nickserv-info)
431 "IDENTIFY"))
432 (nick (if (erc-nickserv-alist-use-nick-p nil nickserv-info)
433 (concat (erc-current-nick) " ")
434 ""))
435 (msgtype (or (erc-nickserv-alist-ident-command nil nickserv-info)
436 "PRIVMSG")))
437 (erc-message msgtype
438 (concat nickserv " " identify-word " " nick password)))))
440 (provide 'erc-services)
442 ;;; erc-services.el ends here
444 ;; Local Variables:
445 ;; indent-tabs-mode: t
446 ;; tab-width: 8
447 ;; End: