Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[bpt/emacs.git] / lisp / erc / erc-services.el
CommitLineData
c6b99621 1;;; erc-services.el --- Identify to NickServ
597993cf 2
49f70d46 3;; Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
597993cf
MB
4
5;; This file is part of GNU Emacs.
6
4ee57b2a 7;; GNU Emacs is free software: you can redistribute it and/or modify
597993cf 8;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
597993cf
MB
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
4ee57b2a 18;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
597993cf
MB
19
20;;; Commentary:
21
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.
29
1c36df97
MO
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.
597993cf
MB
34
35;; Usage:
36;;
37;; Put into your .emacs:
38;;
c6b99621 39;; (require 'erc-services)
597993cf
MB
40;; (erc-services-mode 1)
41;;
42;; Add your nickname and NickServ password to `erc-nickserv-passwords'.
43;; Using the freenode network as an example:
44;;
45;; (setq erc-nickserv-passwords '((freenode (("nickname" "password")))))
46;;
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:
51;;
52;; M-x erc-nickserv-identify-mode
53;;
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'.
57;;
58
59;;; Code:
60
61(require 'erc)
c6b99621 62(require 'erc-networks)
597993cf
MB
63(eval-when-compile (require 'cl))
64
65;; Customization:
66
67(defgroup erc-services nil
68 "Configuration for IRC services.
69
70On some networks, there exists a special type of automated irc bot,
71called Services. Those usually allow you to register your nickname,
72post/read memos to other registered users who are currently offline,
73and do various other things.
74
75This group allows you to set variables to somewhat automate
76communication with those Services."
77 :group 'erc)
78
6904f7fe
MB
79(defcustom erc-nickserv-identify-mode 'both
80 "The mode which is used when identifying to Nickserv.
81
82Possible settings are:.
83
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.
88nil - Disables automatic Nickserv identification.
89
90You 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))))
101
c6b99621 102;;;###autoload (autoload 'erc-services-mode "erc-services" nil t)
597993cf
MB
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
1c36df97
MO
111 'erc-nickserv-identify-on-nick-change)
112 (remove-hook 'erc-server-NOTICE-functions
113 'erc-nickserv-identification-autodetect)))
597993cf
MB
114
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): "
10dc9f9e 121 '(("autodetect") ("nick-change") ("both")) nil t))))
1c36df97
MO
122 (add-hook 'erc-server-NOTICE-functions
123 'erc-nickserv-identification-autodetect)
5e56b3fb
MO
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))
597993cf
MB
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))
10dc9f9e
MB
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))
597993cf
MB
152 (t
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
1c36df97
MO
159 'erc-nickserv-identify-on-nick-change)
160 (remove-hook 'erc-server-NOTICE-functions
161 'erc-nickserv-identification-autodetect))))
597993cf 162
597993cf
MB
163(defcustom erc-prompt-for-nickserv-password t
164 "Ask for the password when identifying to NickServ."
165 :group 'erc-services
166 :type 'boolean)
167
168(defcustom erc-nickserv-passwords nil
169 "Passwords used when identifying to NickServ automatically.
170
171Example 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"
ff59d266
MB
180 (const Ars)
181 (const Austnet)
182 (const Azzurra)
183 (const BitlBee)
184 (const BRASnet)
597993cf 185 (const DALnet)
ff59d266 186 (const freenode)
597993cf 187 (const GalaxyNet)
1c36df97 188 (const GRnet)
597993cf 189 (const iip)
ff59d266
MB
190 (const OFTC)
191 (const QuakeNet)
5e56b3fb 192 (const Rizon)
ff59d266 193 (const SlashNET)
597993cf
MB
194 (symbol :tag "Network name"))
195 (repeat :tag "Nickname and password"
196 (cons :tag "Identity"
197 (string :tag "Nick")
198 (string :tag "Password"))))))
199
200;; Variables:
201
202(defcustom erc-nickserv-alist
ff59d266
MB
203 '((Ars
204 nil nil
205 "Census"
1c36df97 206 "IDENTIFY" nil nil nil)
ff59d266
MB
207 (Austnet
208 "NickOP!service@austnet.org"
209 "/msg\\s-NickOP@austnet.org\\s-identify\\s-<password>"
210 "nickop@austnet.org"
1c36df97 211 "identify" nil nil nil)
ff59d266
MB
212 (Azzurra
213 "NickServ!service@azzurra.org"
214 "\ 2/ns\\s-IDENTIFY\\s-password\ 2"
215 "NickServ"
1c36df97 216 "IDENTIFY" nil nil nil)
ff59d266
MB
217 (BitlBee
218 nil nil
6904f7fe 219 "&bitlbee"
1c36df97 220 "identify" nil nil nil)
ff59d266
MB
221 (BRASnet
222 "NickServ!services@brasnet.org"
223 "\ 2/NickServ\\s-IDENTIFY\\s-\1fsenha\1f\ 2"
224 "NickServ"
1c36df97 225 "IDENTIFY" nil "" nil)
6904f7fe 226 (DALnet
597993cf
MB
227 "NickServ!service@dal.net"
228 "/msg\\s-NickServ@services.dal.net\\s-IDENTIFY\\s-<password>"
229 "NickServ@services.dal.net"
1c36df97 230 "IDENTIFY" nil nil nil)
597993cf
MB
231 (freenode
232 "NickServ!NickServ@services."
712e2b05
MO
233 ;; freenode also accepts a password at login, see the `erc'
234 ;; :password argument.
40ef8242 235 "This\\s-nickname\\s-is\\s-registered.\\s-Please\\s-choose"
597993cf 236 "NickServ"
1c36df97 237 "IDENTIFY" nil nil
40ef8242
MO
238 ;; See also the 901 response code message.
239 "You\\s-are\\s-now\\s-identified\\s-for\\s-")
597993cf
MB
240 (GalaxyNet
241 "NS!nickserv@galaxynet.org"
242 "Please\\s-change\\s-nicks\\s-or\\s-authenticate."
243 "NS@services.galaxynet.org"
1c36df97
MO
244 "AUTH" t nil nil)
245 (GRnet
246 "NickServ!service@irc.gr"
247 "This\\s-nickname\\s-is\\s-registered\\s-and\\s-protected."
248 "NickServ"
249 "IDENTIFY" nil nil
250 "Password\\s-accepted\\s--\\s-you\\s-are\\s-now\\s-recognized.")
597993cf
MB
251 (iip
252 "Trent@anon.iip"
253 "type\\s-/squery\\s-Trent\\s-identify\\s-<password>"
254 "Trent@anon.iip"
1c36df97 255 "IDENTIFY" nil "SQUERY" nil)
10dc9f9e
MB
256 (OFTC
257 "NickServ!services@services.oftc.net"
712e2b05
MO
258 ;; OFTC's NickServ doesn't ask you to identify anymore.
259 nil
10dc9f9e 260 "NickServ"
712e2b05
MO
261 "IDENTIFY" nil nil
262 "You\\s-are\\s-successfully\\s-identified\\s-as\\s-\ 2")
5e56b3fb
MO
263 (Rizon
264 "NickServ!service@rizon.net"
265 "This\\s-nickname\\s-is\\s-registered\\s-and\\s-protected."
266 "NickServ"
267 "IDENTIFY" nil nil
268 "Password\\s-accepted\\s--\\s-you\\s-are\\s-now\\s-recognized.")
ff59d266
MB
269 (QuakeNet
270 nil nil
271 "Q@CServe.quakenet.org"
1c36df97 272 "auth" t nil nil)
ff59d266
MB
273 (SlashNET
274 "NickServ!services@services.slashnet.org"
275 "/msg\\s-NickServ\\s-IDENTIFY\\s-\1fpassword"
276 "NickServ@services.slashnet.org"
1c36df97 277 "IDENTIFY" nil nil nil))
597993cf
MB
278 "Alist of NickServer details, sorted by network.
279Every element in the list has the form
1c36df97 280 \(SYMBOL NICKSERV REGEXP NICK KEYWORD USE-CURRENT ANSWER SUCCESS-REGEXP)
597993cf
MB
281
282SYMBOL is a network identifier, a symbol, as used in `erc-networks-alist'.
283NICKSERV is the description of the nickserv in the form nick!user@host.
284REGEXP is a regular expression matching the message from nickserv.
285NICK is nickserv's nickname. Use nick@server where necessary/possible.
286KEYWORD is the keyword to use in the reply message to identify yourself.
287USE-CURRENT indicates whether the current nickname must be used when
288 identifying.
289ANSWER is the command to use for the answer. The default is 'privmsg.
1c36df97
MO
290SUCCESS-REGEXP is a regular expression matching the message nickserv
291 sends when you've successfully identified.
292The last two elements are optional."
597993cf
MB
293 :group 'erc-services
294 :type '(repeat
295 (list :tag "Nickserv data"
296 (symbol :tag "Network name")
ff59d266
MB
297 (choice (string :tag "Nickserv's nick!user@host")
298 (const :tag "No message sent by Nickserv" nil))
299 (choice (regexp :tag "Identify request sent by Nickserv")
300 (const :tag "No message sent by Nickserv" nil))
597993cf
MB
301 (string :tag "Identify to")
302 (string :tag "Identify keyword")
303 (boolean :tag "Use current nick in identify message?")
304 (choice :tag "Command to use (optional)"
305 (string :tag "Command")
5cb9f674
CY
306 (const :tag "No special command necessary" nil))
307 (choice :tag "Detect Success"
308 (regexp :tag "Pattern to match")
309 (const :tag "Do not try to detect success" nil)))))
310
597993cf 311
6904f7fe
MB
312(defsubst erc-nickserv-alist-sender (network &optional entry)
313 (nth 1 (or entry (assoc network erc-nickserv-alist))))
314
315(defsubst erc-nickserv-alist-regexp (network &optional entry)
316 (nth 2 (or entry (assoc network erc-nickserv-alist))))
317
318(defsubst erc-nickserv-alist-nickserv (network &optional entry)
319 (nth 3 (or entry (assoc network erc-nickserv-alist))))
320
321(defsubst erc-nickserv-alist-ident-keyword (network &optional entry)
322 (nth 4 (or entry (assoc network erc-nickserv-alist))))
323
324(defsubst erc-nickserv-alist-use-nick-p (network &optional entry)
325 (nth 5 (or entry (assoc network erc-nickserv-alist))))
326
327(defsubst erc-nickserv-alist-ident-command (network &optional entry)
328 (nth 6 (or entry (assoc network erc-nickserv-alist))))
329
1c36df97
MO
330(defsubst erc-nickserv-alist-identified-regexp (network &optional entry)
331 (nth 7 (or entry (assoc network erc-nickserv-alist))))
332
597993cf
MB
333;; Functions:
334
1c36df97
MO
335(defcustom erc-nickserv-identified-hook nil
336 "Run this hook when NickServ acknowledged successful identification.
337Hooks are called with arguments (NETWORK NICK)."
338 :group 'erc-services
339 :type 'hook)
340
341(defun erc-nickserv-identification-autodetect (proc parsed)
342 "Check for NickServ's successful identification notice.
343Make sure it is the real NickServ for this network and that it has
344specifically confirmed a successful identification attempt.
345If this is the case, run `erc-nickserv-identified-hook'."
346 (let* ((network (erc-network))
347 (sender (erc-nickserv-alist-sender network))
348 (success-regex (erc-nickserv-alist-identified-regexp network))
349 (sspec (erc-response.sender parsed))
350 (nick (car (erc-response.command-args parsed)))
351 (msg (erc-response.contents parsed)))
352 ;; continue only if we're sure it's the real nickserv for this network
353 ;; and it's told us we've successfully identified
354 (when (and sender (equal sspec sender)
712e2b05 355 success-regex
1c36df97
MO
356 (string-match success-regex msg))
357 (erc-log "NickServ IDENTIFY success notification detected")
358 (run-hook-with-args 'erc-nickserv-identified-hook network nick)
359 nil)))
360
597993cf 361(defun erc-nickserv-identify-autodetect (proc parsed)
712e2b05
MO
362 "Identify to NickServ when an identify request is received.
363Make sure it is the real NickServ for this network.
597993cf
MB
364If `erc-prompt-for-nickserv-password' is non-nil, prompt the user for the
365password for this nickname, otherwise try to send it automatically."
366 (unless (and (null erc-nickserv-passwords)
367 (null erc-prompt-for-nickserv-password))
368 (let* ((network (erc-network))
6904f7fe
MB
369 (sender (erc-nickserv-alist-sender network))
370 (identify-regex (erc-nickserv-alist-regexp network))
597993cf
MB
371 (sspec (erc-response.sender parsed))
372 (nick (car (erc-response.command-args parsed)))
373 (msg (erc-response.contents parsed)))
374 ;; continue only if we're sure it's the real nickserv for this network
375 ;; and it's asked us to identify
6904f7fe 376 (when (and sender (equal sspec sender)
712e2b05 377 identify-regex
597993cf
MB
378 (string-match identify-regex msg))
379 (erc-log "NickServ IDENTIFY request detected")
380 (erc-nickserv-call-identify-function nick)
381 nil))))
382
383(defun erc-nickserv-identify-on-connect (server nick)
384 "Identify to Nickserv after the connection to the server is established."
10dc9f9e
MB
385 (unless (or (and (null erc-nickserv-passwords)
386 (null erc-prompt-for-nickserv-password))
387 (and (eq erc-nickserv-identify-mode 'both)
6904f7fe 388 (erc-nickserv-alist-regexp (erc-network))))
597993cf
MB
389 (erc-nickserv-call-identify-function nick)))
390
391(defun erc-nickserv-identify-on-nick-change (nick old-nick)
392 "Identify to Nickserv whenever your nick changes."
10dc9f9e
MB
393 (unless (or (and (null erc-nickserv-passwords)
394 (null erc-prompt-for-nickserv-password))
395 (and (eq erc-nickserv-identify-mode 'both)
6904f7fe 396 (erc-nickserv-alist-regexp (erc-network))))
597993cf
MB
397 (erc-nickserv-call-identify-function nick)))
398
399(defun erc-nickserv-call-identify-function (nickname)
400 "Call `erc-nickserv-identify' interactively or run it with NICKNAME's
401password.
402The action is determined by the value of `erc-prompt-for-nickserv-password'."
403 (if erc-prompt-for-nickserv-password
404 (call-interactively 'erc-nickserv-identify)
405 (when erc-nickserv-passwords
406 (erc-nickserv-identify
407 (cdr (assoc nickname
408 (nth 1 (assoc (erc-network)
409 erc-nickserv-passwords))))))))
410
411;;;###autoload
412(defun erc-nickserv-identify (password)
413 "Send an \"identify <PASSWORD>\" message to NickServ.
414When called interactively, read the password using `read-passwd'."
415 (interactive
416 (list (read-passwd
417 (format "NickServ password for %s on %s (RET to cancel): "
418 (erc-current-nick)
419 (or (and (erc-network)
420 (symbol-name (erc-network)))
421 "Unknown network")))))
422 (when (and password (not (string= "" password)))
423 (let* ((erc-auto-discard-away nil)
424 (network (erc-network))
425 (nickserv-info (assoc network erc-nickserv-alist))
6904f7fe
MB
426 (nickserv (or (erc-nickserv-alist-nickserv nil nickserv-info)
427 "NickServ"))
428 (identify-word (or (erc-nickserv-alist-ident-keyword
429 nil nickserv-info)
430 "IDENTIFY"))
431 (nick (if (erc-nickserv-alist-use-nick-p nil nickserv-info)
597993cf
MB
432 (concat (erc-current-nick) " ")
433 ""))
6904f7fe
MB
434 (msgtype (or (erc-nickserv-alist-ident-command nil nickserv-info)
435 "PRIVMSG")))
597993cf
MB
436 (erc-message msgtype
437 (concat nickserv " " identify-word " " nick password)))))
438
c6b99621 439(provide 'erc-services)
597993cf 440
c6b99621 441;;; erc-services.el ends here
597993cf
MB
442;;
443;; Local Variables:
444;; indent-tabs-mode: t
445;; tab-width: 8
446;; End:
447
448;; arch-tag: d401c8aa-d938-4255-96a9-3efb64c47e58