(match): Use yellow background on light-bg terminals.
[emacs.git] / lisp / erc / erc-services.el
blob5d03a7b6365e91697ade7ea30ee20b331ea985ec
1 ;;; erc-services.el --- Identify to NickServ
3 ;; Copyright (C) 2002, 2003, 2004, 2006, 2007 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 2, or (at your option)
10 ;; 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; 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.
22 ;;; Commentary:
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.
32 ;; As a default, ERC has the data for the official nickname services on the
33 ;; networks Austnet, BrasNET, Dalnet, freenode, GalaxyNet, and Slashnet.
34 ;; You can add more by using M-x customize-variable RET erc-nickserv-alist.
36 ;; Usage:
38 ;; Put into your .emacs:
40 ;; (require 'erc-services)
41 ;; (erc-services-mode 1)
43 ;; Add your nickname and NickServ password to `erc-nickserv-passwords'.
44 ;; Using the freenode network as an example:
46 ;; (setq erc-nickserv-passwords '((freenode (("nickname" "password")))))
48 ;; The default automatic identification mode is autodetection of NickServ
49 ;; identify requests. Set the variable `erc-nickserv-identify-mode' if
50 ;; you'd like to change this behavior. You can also change the way
51 ;; automatic identification is handled by using:
53 ;; M-x erc-nickserv-identify-mode
55 ;; If you'd rather not identify yourself automatically but would like access
56 ;; to the functions contained in this file, just load this file without
57 ;; enabling `erc-services-mode'.
60 ;;; Code:
62 (require 'erc)
63 (require 'erc-networks)
64 (eval-when-compile (require 'cl))
66 ;; Customization:
68 (defgroup erc-services nil
69 "Configuration for IRC services.
71 On some networks, there exists a special type of automated irc bot,
72 called Services. Those usually allow you to register your nickname,
73 post/read memos to other registered users who are currently offline,
74 and do various other things.
76 This group allows you to set variables to somewhat automate
77 communication with those Services."
78 :group 'erc)
80 (defcustom erc-nickserv-identify-mode 'both
81 "The mode which is used when identifying to Nickserv.
83 Possible settings are:.
85 'autodetect - Identify when the real Nickserv sends an identify request.
86 'nick-change - Identify when you log in or change your nickname.
87 'both - Do the former if the network supports it, otherwise do the
88 latter.
89 nil - Disables automatic Nickserv identification.
91 You can also use M-x erc-nickserv-identify-mode to change modes."
92 :group 'erc-services
93 :type '(choice (const autodetect)
94 (const nick-change)
95 (const both)
96 (const nil))
97 :set (lambda (sym val)
98 (set sym val)
99 ;; avoid recursive load at startup
100 (when (featurep 'erc-services)
101 (erc-nickserv-identify-mode val))))
103 ;;;###autoload (autoload 'erc-services-mode "erc-services" nil t)
104 (define-erc-module services nickserv
105 "This mode automates communication with services."
106 ((erc-nickserv-identify-mode erc-nickserv-identify-mode))
107 ((remove-hook 'erc-server-NOTICE-functions
108 'erc-nickserv-identify-autodetect)
109 (remove-hook 'erc-after-connect
110 'erc-nickserv-identify-on-connect)
111 (remove-hook 'erc-nick-changed-functions
112 'erc-nickserv-identify-on-nick-change)))
114 ;;;###autoload
115 (defun erc-nickserv-identify-mode (mode)
116 "Set up hooks according to which MODE the user has chosen."
117 (interactive
118 (list (intern (completing-read
119 "Choose Nickserv identify mode (RET to disable): "
120 '(("autodetect") ("nick-change") ("both")) nil t))))
121 (cond ((eq mode 'autodetect)
122 (setq erc-nickserv-identify-mode 'autodetect)
123 (add-hook 'erc-server-NOTICE-functions
124 'erc-nickserv-identify-autodetect)
125 (remove-hook 'erc-nick-changed-functions
126 'erc-nickserv-identify-on-nick-change)
127 (remove-hook 'erc-after-connect
128 'erc-nickserv-identify-on-connect))
129 ((eq mode 'nick-change)
130 (setq erc-nickserv-identify-mode 'nick-change)
131 (add-hook 'erc-after-connect
132 'erc-nickserv-identify-on-connect)
133 (add-hook 'erc-nick-changed-functions
134 'erc-nickserv-identify-on-nick-change)
135 (remove-hook 'erc-server-NOTICE-functions
136 'erc-nickserv-identify-autodetect))
137 ((eq mode 'both)
138 (setq erc-nickserv-identify-mode 'both)
139 (add-hook 'erc-server-NOTICE-functions
140 'erc-nickserv-identify-autodetect)
141 (add-hook 'erc-after-connect
142 'erc-nickserv-identify-on-connect)
143 (add-hook 'erc-nick-changed-functions
144 'erc-nickserv-identify-on-nick-change))
146 (setq erc-nickserv-identify-mode nil)
147 (remove-hook 'erc-server-NOTICE-functions
148 'erc-nickserv-identify-autodetect)
149 (remove-hook 'erc-after-connect
150 'erc-nickserv-identify-on-connect)
151 (remove-hook 'erc-nick-changed-functions
152 'erc-nickserv-identify-on-nick-change))))
154 (defcustom erc-prompt-for-nickserv-password t
155 "Ask for the password when identifying to NickServ."
156 :group 'erc-services
157 :type 'boolean)
159 (defcustom erc-nickserv-passwords nil
160 "Passwords used when identifying to NickServ automatically.
162 Example of use:
163 (setq erc-nickserv-passwords
164 '((freenode ((\"nick-one\" . \"password\")
165 (\"nick-two\" . \"password\")))
166 (DALnet ((\"nick\" . \"password\")))))"
167 :group 'erc-services
168 :type '(repeat
169 (list :tag "Network"
170 (choice :tag "Network name"
171 (const freenode)
172 (const OFTC)
173 (const DALnet)
174 (const GalaxyNet)
175 (const SlashNET)
176 (const BRASnet)
177 (const iip)
178 (const Austnet)
179 (const Azzurra)
180 (symbol :tag "Network name"))
181 (repeat :tag "Nickname and password"
182 (cons :tag "Identity"
183 (string :tag "Nick")
184 (string :tag "Password"))))))
186 ;; Variables:
188 (defcustom erc-nickserv-alist
189 '((BitlBee
192 "&bitlbee"
193 "identify"
194 nil)
195 (DALnet
196 "NickServ!service@dal.net"
197 "/msg\\s-NickServ@services.dal.net\\s-IDENTIFY\\s-<password>"
198 "NickServ@services.dal.net"
199 "IDENTIFY"
200 nil)
201 (freenode
202 "NickServ!NickServ@services."
203 "/msg\\s-NickServ\\s-\x02IDENTIFY\x02\\s-<password>"
204 "NickServ"
205 "IDENTIFY"
206 nil)
207 (GalaxyNet
208 "NS!nickserv@galaxynet.org"
209 "Please\\s-change\\s-nicks\\s-or\\s-authenticate."
210 "NS@services.galaxynet.org"
211 "AUTH"
213 (SlashNET
214 "NickServ!services@services.slashnet.org"
215 "/msg\\s-NickServ\\s-IDENTIFY\\s-\x1fpassword"
216 "NickServ@services.slashnet.org"
217 "IDENTIFY"
218 nil)
219 (iip
220 "Trent@anon.iip"
221 "type\\s-/squery\\s-Trent\\s-identify\\s-<password>"
222 "Trent@anon.iip"
223 "IDENTIFY"
225 "SQUERY")
226 (BRASnet
227 "NickServ!services@brasnet.org"
228 "\x02/NickServ\\s-IDENTIFY\\s-\x1fsenha\x1f\x02"
229 "NickServ"
230 "IDENTIFY"
233 (Austnet
234 "NickOP!service@austnet.org"
235 "/msg\\s-NickOP@austnet.org\\s-identify\\s-<password>"
236 "nickop@austnet.org"
237 "identify"
238 nil)
239 (Azzurra
240 "NickServ!service@azzurra.org"
241 "\x02/ns\\s-IDENTIFY\\s-password\x02"
242 "NickServ"
243 "IDENTIFY"
244 nil)
245 (OFTC
246 "NickServ!services@services.oftc.net"
247 "/msg\\s-NickServ\\s-IDENTIFY\\s-\^_password"
248 "NickServ"
249 "IDENTIFY"
250 nil))
251 "Alist of NickServer details, sorted by network.
252 Every element in the list has the form
253 \(SYMBOL NICKSERV REGEXP NICK KEYWORD USE-CURRENT ANSWER)
255 SYMBOL is a network identifier, a symbol, as used in `erc-networks-alist'.
256 NICKSERV is the description of the nickserv in the form nick!user@host.
257 REGEXP is a regular expression matching the message from nickserv.
258 NICK is nickserv's nickname. Use nick@server where necessary/possible.
259 KEYWORD is the keyword to use in the reply message to identify yourself.
260 USE-CURRENT indicates whether the current nickname must be used when
261 identifying.
262 ANSWER is the command to use for the answer. The default is 'privmsg.
263 This last element is optional."
264 :group 'erc-services
265 :type '(repeat
266 (list :tag "Nickserv data"
267 (symbol :tag "Network name")
268 (string :tag "Nickserv's nick!user@host")
269 (regexp :tag "Identify request sent by Nickserv")
270 (string :tag "Identify to")
271 (string :tag "Identify keyword")
272 (boolean :tag "Use current nick in identify message?")
273 (choice :tag "Command to use (optional)"
274 (string :tag "Command")
275 (const :tag "No special command necessary" nil)))))
277 (defsubst erc-nickserv-alist-sender (network &optional entry)
278 (nth 1 (or entry (assoc network erc-nickserv-alist))))
280 (defsubst erc-nickserv-alist-regexp (network &optional entry)
281 (nth 2 (or entry (assoc network erc-nickserv-alist))))
283 (defsubst erc-nickserv-alist-nickserv (network &optional entry)
284 (nth 3 (or entry (assoc network erc-nickserv-alist))))
286 (defsubst erc-nickserv-alist-ident-keyword (network &optional entry)
287 (nth 4 (or entry (assoc network erc-nickserv-alist))))
289 (defsubst erc-nickserv-alist-use-nick-p (network &optional entry)
290 (nth 5 (or entry (assoc network erc-nickserv-alist))))
292 (defsubst erc-nickserv-alist-ident-command (network &optional entry)
293 (nth 6 (or entry (assoc network erc-nickserv-alist))))
295 ;; Functions:
297 (defun erc-nickserv-identify-autodetect (proc parsed)
298 "Check for a NickServ identify request everytime a notice is received.
299 Make sure it is the real NickServ for this network and that it has
300 specifically asked the user to IDENTIFY.
301 If `erc-prompt-for-nickserv-password' is non-nil, prompt the user for the
302 password for this nickname, otherwise try to send it automatically."
303 (unless (and (null erc-nickserv-passwords)
304 (null erc-prompt-for-nickserv-password))
305 (let* ((network (erc-network))
306 (sender (erc-nickserv-alist-sender network))
307 (identify-regex (erc-nickserv-alist-regexp network))
308 (sspec (erc-response.sender parsed))
309 (nick (car (erc-response.command-args parsed)))
310 (msg (erc-response.contents parsed)))
311 ;; continue only if we're sure it's the real nickserv for this network
312 ;; and it's asked us to identify
313 (when (and sender (equal sspec sender)
314 (string-match identify-regex msg))
315 (erc-log "NickServ IDENTIFY request detected")
316 (erc-nickserv-call-identify-function nick)
317 nil))))
319 (defun erc-nickserv-identify-on-connect (server nick)
320 "Identify to Nickserv after the connection to the server is established."
321 (unless (or (and (null erc-nickserv-passwords)
322 (null erc-prompt-for-nickserv-password))
323 (and (eq erc-nickserv-identify-mode 'both)
324 (erc-nickserv-alist-regexp (erc-network))))
325 (erc-nickserv-call-identify-function nick)))
327 (defun erc-nickserv-identify-on-nick-change (nick old-nick)
328 "Identify to Nickserv whenever your nick changes."
329 (unless (or (and (null erc-nickserv-passwords)
330 (null erc-prompt-for-nickserv-password))
331 (and (eq erc-nickserv-identify-mode 'both)
332 (erc-nickserv-alist-regexp (erc-network))))
333 (erc-nickserv-call-identify-function nick)))
335 (defun erc-nickserv-call-identify-function (nickname)
336 "Call `erc-nickserv-identify' interactively or run it with NICKNAME's
337 password.
338 The action is determined by the value of `erc-prompt-for-nickserv-password'."
339 (if erc-prompt-for-nickserv-password
340 (call-interactively 'erc-nickserv-identify)
341 (when erc-nickserv-passwords
342 (erc-nickserv-identify
343 (cdr (assoc nickname
344 (nth 1 (assoc (erc-network)
345 erc-nickserv-passwords))))))))
347 ;;;###autoload
348 (defun erc-nickserv-identify (password)
349 "Send an \"identify <PASSWORD>\" message to NickServ.
350 When called interactively, read the password using `read-passwd'."
351 (interactive
352 (list (read-passwd
353 (format "NickServ password for %s on %s (RET to cancel): "
354 (erc-current-nick)
355 (or (and (erc-network)
356 (symbol-name (erc-network)))
357 "Unknown network")))))
358 (when (and password (not (string= "" password)))
359 (let* ((erc-auto-discard-away nil)
360 (network (erc-network))
361 (nickserv-info (assoc network erc-nickserv-alist))
362 (nickserv (or (erc-nickserv-alist-nickserv nil nickserv-info)
363 "NickServ"))
364 (identify-word (or (erc-nickserv-alist-ident-keyword
365 nil nickserv-info)
366 "IDENTIFY"))
367 (nick (if (erc-nickserv-alist-use-nick-p nil nickserv-info)
368 (concat (erc-current-nick) " ")
369 ""))
370 (msgtype (or (erc-nickserv-alist-ident-command nil nickserv-info)
371 "PRIVMSG")))
372 (erc-message msgtype
373 (concat nickserv " " identify-word " " nick password)))))
375 (provide 'erc-services)
377 ;;; erc-services.el ends here
379 ;; Local Variables:
380 ;; indent-tabs-mode: t
381 ;; tab-width: 8
382 ;; End:
384 ;; arch-tag: d401c8aa-d938-4255-96a9-3efb64c47e58