]> code.delx.au - gnu-emacs/blob - lisp/erc/erc-services.el
Merged from emacs@sv.gnu.org
[gnu-emacs] / lisp / erc / erc-services.el
1 ;;; erc-services.el --- Identify to NickServ
2
3 ;; Copyright (C) 2002, 2003, 2004, 2006 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 2, 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 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.
35
36 ;; Usage:
37 ;;
38 ;; Put into your .emacs:
39 ;;
40 ;; (require 'erc-services)
41 ;; (erc-services-mode 1)
42 ;;
43 ;; Add your nickname and NickServ password to `erc-nickserv-passwords'.
44 ;; Using the freenode network as an example:
45 ;;
46 ;; (setq erc-nickserv-passwords '((freenode (("nickname" "password")))))
47 ;;
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:
52 ;;
53 ;; M-x erc-nickserv-identify-mode
54 ;;
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'.
58 ;;
59
60 ;;; Code:
61
62 (require 'erc)
63 (require 'erc-networks)
64 (eval-when-compile (require 'cl))
65
66 ;; Customization:
67
68 (defgroup erc-services nil
69 "Configuration for IRC services.
70
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.
75
76 This group allows you to set variables to somewhat automate
77 communication with those Services."
78 :group 'erc)
79
80 ;;;###autoload (autoload 'erc-services-mode "erc-services" nil t)
81 (define-erc-module services nickserv
82 "This mode automates communication with services."
83 ((erc-nickserv-identify-mode erc-nickserv-identify-mode))
84 ((remove-hook 'erc-server-NOTICE-functions
85 'erc-nickserv-identify-autodetect)
86 (remove-hook 'erc-after-connect
87 'erc-nickserv-identify-on-connect)
88 (remove-hook 'erc-nick-changed-functions
89 'erc-nickserv-identify-on-nick-change)))
90
91 ;;;###autoload
92 (defun erc-nickserv-identify-mode (mode)
93 "Set up hooks according to which MODE the user has chosen."
94 (interactive
95 (list (intern (completing-read
96 "Choose Nickserv identify mode (RET to disable): "
97 '(("autodetect") ("nick-change") ("both")) nil t))))
98 (cond ((eq mode 'autodetect)
99 (setq erc-nickserv-identify-mode 'autodetect)
100 (add-hook 'erc-server-NOTICE-functions
101 'erc-nickserv-identify-autodetect)
102 (remove-hook 'erc-nick-changed-functions
103 'erc-nickserv-identify-on-nick-change)
104 (remove-hook 'erc-after-connect
105 'erc-nickserv-identify-on-connect))
106 ((eq mode 'nick-change)
107 (setq erc-nickserv-identify-mode 'nick-change)
108 (add-hook 'erc-after-connect
109 'erc-nickserv-identify-on-connect)
110 (add-hook 'erc-nick-changed-functions
111 'erc-nickserv-identify-on-nick-change)
112 (remove-hook 'erc-server-NOTICE-functions
113 'erc-nickserv-identify-autodetect))
114 ((eq mode 'both)
115 (setq erc-nickserv-identify-mode 'both)
116 (add-hook 'erc-server-NOTICE-functions
117 'erc-nickserv-identify-autodetect)
118 (add-hook 'erc-after-connect
119 'erc-nickserv-identify-on-connect)
120 (add-hook 'erc-nick-changed-functions
121 'erc-nickserv-identify-on-nick-change))
122 (t
123 (setq erc-nickserv-identify-mode nil)
124 (remove-hook 'erc-server-NOTICE-functions
125 'erc-nickserv-identify-autodetect)
126 (remove-hook 'erc-after-connect
127 'erc-nickserv-identify-on-connect)
128 (remove-hook 'erc-nick-changed-functions
129 'erc-nickserv-identify-on-nick-change))))
130
131 (defcustom erc-nickserv-identify-mode 'both
132 "The mode which is used when identifying to Nickserv.
133
134 Possible settings are:.
135
136 'autodetect - Identify when the real Nickserv sends an identify request.
137 'nick-change - Identify when you change your nickname.
138 'both - Do the former if the network supports it, otherwise do the
139 latter.
140 nil - Disables automatic Nickserv identification.
141
142 You can also use M-x erc-nickserv-identify-mode to change modes."
143 :group 'erc-services
144 :type '(choice (const autodetect)
145 (const nick-change)
146 (const both)
147 (const nil))
148 :set (lambda (sym val)
149 (set sym val)
150 (erc-nickserv-identify-mode val)))
151
152 (defcustom erc-prompt-for-nickserv-password t
153 "Ask for the password when identifying to NickServ."
154 :group 'erc-services
155 :type 'boolean)
156
157 (defcustom erc-nickserv-passwords nil
158 "Passwords used when identifying to NickServ automatically.
159
160 Example of use:
161 (setq erc-nickserv-passwords
162 '((freenode ((\"nick-one\" . \"password\")
163 (\"nick-two\" . \"password\")))
164 (DALnet ((\"nick\" . \"password\")))))"
165 :group 'erc-services
166 :type '(repeat
167 (list :tag "Network"
168 (choice :tag "Network name"
169 (const freenode)
170 (const OFTC)
171 (const DALnet)
172 (const GalaxyNet)
173 (const SlashNET)
174 (const BRASnet)
175 (const iip)
176 (const Austnet)
177 (const Azzurra)
178 (symbol :tag "Network name"))
179 (repeat :tag "Nickname and password"
180 (cons :tag "Identity"
181 (string :tag "Nick")
182 (string :tag "Password"))))))
183
184 ;; Variables:
185
186 (defcustom erc-nickserv-alist
187 '((DALnet
188 "NickServ!service@dal.net"
189 "/msg\\s-NickServ@services.dal.net\\s-IDENTIFY\\s-<password>"
190 "NickServ@services.dal.net"
191 "IDENTIFY"
192 nil)
193 (freenode
194 "NickServ!NickServ@services."
195 "/msg\\s-NickServ\\s-\ 2IDENTIFY\ 2\\s-<password>"
196 "NickServ"
197 "IDENTIFY"
198 nil)
199 (GalaxyNet
200 "NS!nickserv@galaxynet.org"
201 "Please\\s-change\\s-nicks\\s-or\\s-authenticate."
202 "NS@services.galaxynet.org"
203 "AUTH"
204 t)
205 (SlashNET
206 "NickServ!services@services.slashnet.org"
207 "/msg\\s-NickServ\\s-IDENTIFY\\s-\1fpassword"
208 "NickServ@services.slashnet.org"
209 "IDENTIFY"
210 nil)
211 (iip
212 "Trent@anon.iip"
213 "type\\s-/squery\\s-Trent\\s-identify\\s-<password>"
214 "Trent@anon.iip"
215 "IDENTIFY"
216 nil
217 "SQUERY")
218 (BRASnet
219 "NickServ!services@brasnet.org"
220 "\ 2/NickServ\\s-IDENTIFY\\s-\1fsenha\1f\ 2"
221 "NickServ"
222 "IDENTIFY"
223 nil
224 "")
225 (Austnet
226 "NickOP!service@austnet.org"
227 "/msg\\s-NickOP@austnet.org\\s-identify\\s-<password>"
228 "nickop@austnet.org"
229 "identify"
230 nil)
231 (Azzurra
232 "NickServ!service@azzurra.org"
233 "\ 2/ns\\s-IDENTIFY\\s-password\ 2"
234 "NickServ"
235 "IDENTIFY"
236 nil)
237 (OFTC
238 "NickServ!services@services.oftc.net"
239 "/msg\\s-NickServ\\s-IDENTIFY\\s-\^_password"
240 "NickServ"
241 "IDENTIFY"
242 nil))
243 "Alist of NickServer details, sorted by network.
244 Every element in the list has the form
245 \(SYMBOL NICKSERV REGEXP NICK KEYWORD USE-CURRENT ANSWER)
246
247 SYMBOL is a network identifier, a symbol, as used in `erc-networks-alist'.
248 NICKSERV is the description of the nickserv in the form nick!user@host.
249 REGEXP is a regular expression matching the message from nickserv.
250 NICK is nickserv's nickname. Use nick@server where necessary/possible.
251 KEYWORD is the keyword to use in the reply message to identify yourself.
252 USE-CURRENT indicates whether the current nickname must be used when
253 identifying.
254 ANSWER is the command to use for the answer. The default is 'privmsg.
255 This last element is optional."
256 :group 'erc-services
257 :type '(repeat
258 (list :tag "Nickserv data"
259 (symbol :tag "Network name")
260 (string :tag "Nickserv's nick!user@host")
261 (regexp :tag "Identify request sent by Nickserv")
262 (string :tag "Identify to")
263 (string :tag "Identify keyword")
264 (boolean :tag "Use current nick in identify message?")
265 (choice :tag "Command to use (optional)"
266 (string :tag "Command")
267 (const :tag "No special command necessary" nil)))))
268
269 ;; Functions:
270
271 (defun erc-nickserv-identify-autodetect (proc parsed)
272 "Check for a NickServ identify request everytime a notice is received.
273 Make sure it is the real NickServ for this network and that it has
274 specifically asked the user to IDENTIFY.
275 If `erc-prompt-for-nickserv-password' is non-nil, prompt the user for the
276 password for this nickname, otherwise try to send it automatically."
277 (unless (and (null erc-nickserv-passwords)
278 (null erc-prompt-for-nickserv-password))
279 (let* ((network (erc-network))
280 (nickserv (nth 1 (assoc network erc-nickserv-alist)))
281 (identify-regex (nth 2 (assoc network erc-nickserv-alist)))
282 (sspec (erc-response.sender parsed))
283 (nick (car (erc-response.command-args parsed)))
284 (msg (erc-response.contents parsed)))
285 ;; continue only if we're sure it's the real nickserv for this network
286 ;; and it's asked us to identify
287 (when (and nickserv (equal sspec nickserv)
288 (string-match identify-regex msg))
289 (erc-log "NickServ IDENTIFY request detected")
290 (erc-nickserv-call-identify-function nick)
291 nil))))
292
293 (defun erc-nickserv-identify-on-connect (server nick)
294 "Identify to Nickserv after the connection to the server is established."
295 (unless (or (and (null erc-nickserv-passwords)
296 (null erc-prompt-for-nickserv-password))
297 (and (eq erc-nickserv-identify-mode 'both)
298 (nth 2 (assoc (erc-network) erc-nickserv-alist))))
299 (erc-nickserv-call-identify-function nick)))
300
301 (defun erc-nickserv-identify-on-nick-change (nick old-nick)
302 "Identify to Nickserv whenever your nick changes."
303 (unless (or (and (null erc-nickserv-passwords)
304 (null erc-prompt-for-nickserv-password))
305 (and (eq erc-nickserv-identify-mode 'both)
306 (nth 2 (assoc (erc-network) erc-nickserv-alist))))
307 (erc-nickserv-call-identify-function nick)))
308
309 (defun erc-nickserv-call-identify-function (nickname)
310 "Call `erc-nickserv-identify' interactively or run it with NICKNAME's
311 password.
312 The action is determined by the value of `erc-prompt-for-nickserv-password'."
313 (if erc-prompt-for-nickserv-password
314 (call-interactively 'erc-nickserv-identify)
315 (when erc-nickserv-passwords
316 (erc-nickserv-identify
317 (cdr (assoc nickname
318 (nth 1 (assoc (erc-network)
319 erc-nickserv-passwords))))))))
320
321 ;;;###autoload
322 (defun erc-nickserv-identify (password)
323 "Send an \"identify <PASSWORD>\" message to NickServ.
324 When called interactively, read the password using `read-passwd'."
325 (interactive
326 (list (read-passwd
327 (format "NickServ password for %s on %s (RET to cancel): "
328 (erc-current-nick)
329 (or (and (erc-network)
330 (symbol-name (erc-network)))
331 "Unknown network")))))
332 (when (and password (not (string= "" password)))
333 (let* ((erc-auto-discard-away nil)
334 (network (erc-network))
335 (nickserv-info (assoc network erc-nickserv-alist))
336 (nickserv (or (nth 3 nickserv-info) "NickServ"))
337 (identify-word (or (nth 4 nickserv-info) "IDENTIFY"))
338 (nick (if (nth 5 nickserv-info)
339 (concat (erc-current-nick) " ")
340 ""))
341 (msgtype (or (nth 6 nickserv-info) "PRIVMSG")))
342 (erc-message msgtype
343 (concat nickserv " " identify-word " " nick password)))))
344
345 (provide 'erc-services)
346
347 ;;; erc-services.el ends here
348 ;;
349 ;; Local Variables:
350 ;; indent-tabs-mode: t
351 ;; tab-width: 8
352 ;; End:
353
354 ;; arch-tag: d401c8aa-d938-4255-96a9-3efb64c47e58