Merge from emacs--rel--22
[bpt/emacs.git] / lisp / erc / erc-backend.el
1 ;;; erc-backend.el --- Backend network communication for ERC
2
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 ;; Filename: erc-backend.el
6 ;; Author: Lawrence Mitchell <wence@gmx.li>
7 ;; Created: 2004-05-7
8 ;; Keywords: IRC chat client internet
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This file defines backend network communication handlers for ERC.
30 ;;
31 ;; How things work:
32 ;;
33 ;; You define a new handler with `define-erc-response-handler'. This
34 ;; defines a function, a corresponding hook variable, and populates a
35 ;; global hash table `erc-server-responses' with a map from response
36 ;; to hook variable. See the function documentation for more
37 ;; information.
38 ;;
39 ;; Upon receiving a line from the server, `erc-parse-server-response'
40 ;; is called on it.
41 ;;
42 ;; A line generally looks like:
43 ;;
44 ;; LINE := ':' SENDER ' ' COMMAND ' ' (COMMAND-ARGS ' ')* ':' CONTENTS
45 ;; SENDER := Not ':' | ' '
46 ;; COMMAND := Not ':' | ' '
47 ;; COMMAND-ARGS := Not ':' | ' '
48 ;;
49 ;; This gets parsed and stuffed into an `erc-response' struct. You
50 ;; can access the fields of the struct with:
51 ;;
52 ;; COMMAND --- `erc-response.command'
53 ;; COMMAND-ARGS --- `erc-response.command-args'
54 ;; CONTENTS --- `erc-response.contents'
55 ;; SENDER --- `erc-response.sender'
56 ;; LINE --- `erc-response.unparsed'
57 ;;
58 ;; WARNING, WARNING!!
59 ;; It's probably not a good idea to destructively modify the list
60 ;; of command-args in your handlers, since other functions down the
61 ;; line may well need to access the arguments too.
62 ;;
63 ;; That is, unless you're /absolutely/ sure that your handler doesn't
64 ;; invoke some other function that needs to use COMMAND-ARGS, don't do
65 ;; something like
66 ;;
67 ;; (while (erc-response.command-args parsed)
68 ;; (let ((a (pop (erc-response.command-args parsed))))
69 ;; ...))
70 ;;
71 ;; The parsed response is handed over to
72 ;; `erc-handle-parsed-server-response', which checks whether it should
73 ;; carry out duplicate suppression, and then runs `erc-call-hooks'.
74 ;; `erc-call-hooks' retrieves the relevant hook variable from
75 ;; `erc-server-responses' and runs it.
76 ;;
77 ;; Most handlers then destructure the parsed response in some way
78 ;; (depending on what the handler is, the arguments have different
79 ;; meanings), and generally display something, usually using
80 ;; `erc-display-message'.
81
82 ;;; TODO:
83
84 ;; o Generalise the display-line code so that we can use it to
85 ;; display the stuff we send, as well as the stuff we receive.
86 ;; Then, move all display-related code into another backend-like
87 ;; file, erc-display.el, say.
88 ;;
89 ;; o Clean up the handlers using new display code (has to be written
90 ;; first).
91
92 ;;; History:
93
94 ;; 2004/05/10 -- Handler bodies taken out of erc.el and ported to new
95 ;; interface.
96
97 ;; 2005-08-13 -- Moved sending commands from erc.el.
98
99 ;;; Code:
100
101 (require 'erc-compat)
102 (eval-when-compile (require 'cl))
103 (autoload 'erc-with-buffer "erc" nil nil 'macro)
104 (autoload 'erc-log "erc" nil nil 'macro)
105
106 ;;;; Variables and options
107
108 (defvar erc-server-responses (make-hash-table :test #'equal)
109 "Hashtable mapping server responses to their handler hooks.")
110
111 (defstruct (erc-response (:conc-name erc-response.))
112 (unparsed "" :type string)
113 (sender "" :type string)
114 (command "" :type string)
115 (command-args '() :type list)
116 (contents "" :type string))
117
118 ;;; User data
119
120 (defvar erc-server-current-nick nil
121 "Nickname on the current server.
122 Use `erc-current-nick' to access this.")
123 (make-variable-buffer-local 'erc-server-current-nick)
124
125 ;;; Server attributes
126
127 (defvar erc-server-process nil
128 "The process object of the corresponding server connection.")
129 (make-variable-buffer-local 'erc-server-process)
130
131 (defvar erc-session-server nil
132 "The server name used to connect to for this session.")
133 (make-variable-buffer-local 'erc-session-server)
134
135 (defvar erc-session-port nil
136 "The port used to connect to.")
137 (make-variable-buffer-local 'erc-session-port)
138
139 (defvar erc-server-announced-name nil
140 "The name the server announced to use.")
141 (make-variable-buffer-local 'erc-server-announced-name)
142
143 (defvar erc-server-version nil
144 "The name and version of the server's ircd.")
145 (make-variable-buffer-local 'erc-server-version)
146
147 (defvar erc-server-parameters nil
148 "Alist listing the supported server parameters.
149
150 This is only set if the server sends 005 messages saying what is
151 supported on the server.
152
153 Entries are of the form:
154 (PARAMETER . VALUE)
155 or
156 (PARAMETER) if no value is provided.
157
158 Some examples of possible parameters sent by servers:
159 CHANMODES=b,k,l,imnpst - list of supported channel modes
160 CHANNELLEN=50 - maximum length of channel names
161 CHANTYPES=#&!+ - supported channel prefixes
162 CHARMAPPING=rfc1459 - character mapping used for nickname and channels
163 KICKLEN=160 - maximum allowed kick message length
164 MAXBANS=30 - maximum number of bans per channel
165 MAXCHANNELS=10 - maximum number of channels allowed to join
166 NETWORK=EFnet - the network identifier
167 NICKLEN=9 - maximum allowed length of nicknames
168 PREFIX=(ov)@+ - list of channel modes and the user prefixes if user has mode
169 RFC2812 - server supports RFC 2812 features
170 SILENCE=10 - supports the SILENCE command, maximum allowed number of entries
171 TOPICLEN=160 - maximum allowed topic length
172 WALLCHOPS - supports sending messages to all operators in a channel")
173 (make-variable-buffer-local 'erc-server-parameters)
174
175 ;;; Server and connection state
176
177 (defvar erc-server-ping-timer-alist nil
178 "Mapping of server buffers to their specific ping timer.")
179
180 (defvar erc-server-connected nil
181 "Non-nil if the current buffer has been used by ERC to establish
182 an IRC connection.
183
184 If you wish to determine whether an IRC connection is currently
185 active, use the `erc-server-process-alive' function instead.")
186 (make-variable-buffer-local 'erc-server-connected)
187
188 (defvar erc-server-reconnect-count 0
189 "Number of times we have failed to reconnect to the current server.")
190 (make-variable-buffer-local 'erc-server-reconnect-count)
191
192 (defvar erc-server-quitting nil
193 "Non-nil if the user requests a quit.")
194 (make-variable-buffer-local 'erc-server-quitting)
195
196 (defvar erc-server-reconnecting nil
197 "Non-nil if the user requests an explicit reconnect, and the
198 current IRC process is still alive.")
199 (make-variable-buffer-local 'erc-server-reconnecting)
200
201 (defvar erc-server-timed-out nil
202 "Non-nil if the IRC server failed to respond to a ping.")
203 (make-variable-buffer-local 'erc-server-timed-out)
204
205 (defvar erc-server-banned nil
206 "Non-nil if the user is denied access because of a server ban.")
207 (make-variable-buffer-local 'erc-server-banned)
208
209 (defvar erc-server-error-occurred nil
210 "Non-nil if the user triggers some server error.")
211 (make-variable-buffer-local 'erc-server-error-occurred)
212
213 (defvar erc-server-lines-sent nil
214 "Line counter.")
215 (make-variable-buffer-local 'erc-server-lines-sent)
216
217 (defvar erc-server-last-peers '(nil . nil)
218 "Last peers used, both sender and receiver.
219 Those are used for /MSG destination shortcuts.")
220 (make-variable-buffer-local 'erc-server-last-peers)
221
222 (defvar erc-server-last-sent-time nil
223 "Time the message was sent.
224 This is useful for flood protection.")
225 (make-variable-buffer-local 'erc-server-last-sent-time)
226
227 (defvar erc-server-last-ping-time nil
228 "Time the last ping was sent.
229 This is useful for flood protection.")
230 (make-variable-buffer-local 'erc-server-last-ping-time)
231
232 (defvar erc-server-last-received-time nil
233 "Time the last message was received from the server.
234 This is useful for detecting hung connections.")
235 (make-variable-buffer-local 'erc-server-last-received-time)
236
237 (defvar erc-server-lag nil
238 "Calculated server lag time in seconds.
239 This variable is only set in a server buffer.")
240 (make-variable-buffer-local 'erc-server-lag)
241
242 (defvar erc-server-filter-data nil
243 "The data that arrived from the server
244 but has not been processed yet.")
245 (make-variable-buffer-local 'erc-server-filter-data)
246
247 (defvar erc-server-duplicates (make-hash-table :test 'equal)
248 "Internal variable used to track duplicate messages.")
249 (make-variable-buffer-local 'erc-server-duplicates)
250
251 ;; From Circe
252 (defvar erc-server-processing-p nil
253 "Non-nil when we're currently processing a message.
254
255 When ERC receives a private message, it sets up a new buffer for
256 this query. These in turn, though, do start flyspell. This
257 involves starting an external process, in which case Emacs will
258 wait - and when it waits, it does accept other stuff from, say,
259 network exceptions. So, if someone sends you two messages
260 quickly after each other, ispell is started for the first, but
261 might take long enough for the second message to be processed
262 first.")
263 (make-variable-buffer-local 'erc-server-processing-p)
264
265 (defvar erc-server-flood-last-message 0
266 "When we sent the last message.
267 See `erc-server-flood-margin' for an explanation of the flood
268 protection algorithm.")
269 (make-variable-buffer-local 'erc-server-flood-last-message)
270
271 (defvar erc-server-flood-queue nil
272 "The queue of messages waiting to be sent to the server.
273 See `erc-server-flood-margin' for an explanation of the flood
274 protection algorithm.")
275 (make-variable-buffer-local 'erc-server-flood-queue)
276
277 (defvar erc-server-flood-timer nil
278 "The timer to resume sending.")
279 (make-variable-buffer-local 'erc-server-flood-timer)
280
281 ;;; IRC protocol and misc options
282
283 (defgroup erc-server nil
284 "Parameters for dealing with IRC servers."
285 :group 'erc)
286
287 (defcustom erc-server-auto-reconnect t
288 "Non-nil means that ERC will attempt to reestablish broken connections.
289
290 Reconnection will happen automatically for any unexpected disconnection."
291 :group 'erc-server
292 :type 'boolean)
293
294 (defcustom erc-server-reconnect-attempts 2
295 "The number of times that ERC will attempt to reestablish a
296 broken connection, or t to always attempt to reconnect.
297
298 This only has an effect if `erc-server-auto-reconnect' is non-nil."
299 :group 'erc-server
300 :type '(choice (const :tag "Always reconnect" t)
301 integer))
302
303 (defcustom erc-server-reconnect-timeout 1
304 "The amount of time, in seconds, that ERC will wait between
305 successive reconnect attempts.
306
307 If a key is pressed while ERC is waiting, it will stop waiting."
308 :group 'erc-server
309 :type 'number)
310
311 (defcustom erc-split-line-length 440
312 "*The maximum length of a single message.
313 If a message exceeds this size, it is broken into multiple ones.
314
315 IRC allows for lines up to 512 bytes. Two of them are CR LF.
316 And a typical message looks like this:
317
318 :nicky!uhuser@host212223.dialin.fnordisp.net PRIVMSG #lazybastards :Hello!
319
320 You can limit here the maximum length of the \"Hello!\" part.
321 Good luck."
322 :type 'integer
323 :group 'erc-server)
324
325 (defcustom erc-server-coding-system (if (and (fboundp 'coding-system-p)
326 (coding-system-p 'undecided)
327 (coding-system-p 'utf-8))
328 '(utf-8 . undecided)
329 nil)
330 "The default coding system for incoming and outgoing text.
331 This is either a coding system, a cons, a function, or nil.
332
333 If a cons, the encoding system for outgoing text is in the car
334 and the decoding system for incoming text is in the cdr. The most
335 interesting use for this is to put `undecided' in the cdr. If a
336 function, it is called with no arguments and should return a
337 coding system or a cons as described above. Note that you can use
338 the dynamically bound variable `target' to get the current
339 target. See `erc-coding-system-for-target'.
340
341 If you need to send non-ASCII text to people not using a client that
342 does decoding on its own, you must tell ERC what encoding to use.
343 Emacs cannot guess it, since it does not know what the people on the
344 other end of the line are using."
345 :group 'erc-server
346 :type '(choice (const :tag "None" nil)
347 coding-system
348 (cons (coding-system :tag "encoding" :value utf-8)
349 (coding-system :tag "decoding" :value undecided))
350 function))
351
352 (defcustom erc-encoding-coding-alist nil
353 "Alist of target regexp and coding-system pairs to use.
354 This overrides `erc-server-coding-system' depending on the
355 current target as returned by `erc-default-target'.
356
357 Example: If you know that the channel #linux-ru uses the coding-system
358 `cyrillic-koi8', then add '(\"#linux-ru\" . cyrillic-koi8) to the
359 alist."
360 :group 'erc-server
361 :type '(repeat (cons (string :tag "Target")
362 coding-system)))
363
364 (defcustom erc-server-connect-function 'open-network-stream
365 "Function used to initiate a connection.
366 It should take same arguments as `open-network-stream' does."
367 :group 'erc-server
368 :type 'function)
369
370 (defcustom erc-server-prevent-duplicates '("301")
371 "*Either nil or a list of strings.
372 Each string is a IRC message type, like PRIVMSG or NOTICE.
373 All Message types in that list of subjected to duplicate prevention."
374 :type '(choice (const nil) (list string))
375 :group 'erc-server)
376
377 (defcustom erc-server-duplicate-timeout 60
378 "*The time allowed in seconds between duplicate messages.
379
380 If two identical messages arrive within this value of one another, the second
381 isn't displayed."
382 :type 'integer
383 :group 'erc-server)
384
385 ;;; Flood-related
386
387 ;; Most of this is courtesy of Jorgen Schaefer and Circe
388 ;; (http://www.nongnu.org/circe)
389
390 (defcustom erc-server-flood-margin 10
391 "*A margin on how much excess data we send.
392 The flood protection algorithm of ERC works like the one
393 detailed in RFC 2813, section 5.8 \"Flood control of clients\".
394
395 * If `erc-server-flood-last-message' is less than the current
396 time, set it equal.
397 * While `erc-server-flood-last-message' is less than
398 `erc-server-flood-margin' seconds ahead of the current
399 time, send a message, and increase
400 `erc-server-flood-last-message' by
401 `erc-server-flood-penalty' for each message."
402 :type 'integer
403 :group 'erc-server)
404
405 (defcustom erc-server-flood-penalty 3
406 "How much we penalize a message.
407 See `erc-server-flood-margin' for an explanation of the flood
408 protection algorithm."
409 :type 'integer
410 :group 'erc-server)
411
412 ;; Ping handling
413
414 (defcustom erc-server-send-ping-interval 30
415 "*Interval of sending pings to the server, in seconds.
416 If this is set to nil, pinging the server is disabled."
417 :group 'erc-server
418 :type '(choice (const :tag "Disabled" nil)
419 (integer :tag "Seconds")))
420
421 (defcustom erc-server-send-ping-timeout 120
422 "*If the time between ping and response is greater than this, reconnect.
423 The time is in seconds.
424
425 This must be greater than or equal to the value for
426 `erc-server-send-ping-interval'.
427
428 If this is set to nil, never try to reconnect."
429 :group 'erc-server
430 :type '(choice (const :tag "Disabled" nil)
431 (integer :tag "Seconds")))
432
433 (defvar erc-server-ping-handler nil
434 "This variable holds the periodic ping timer.")
435 (make-variable-buffer-local 'erc-server-ping-handler)
436
437 ;;;; Helper functions
438
439 ;; From Circe
440 (defun erc-split-line (longline)
441 "Return a list of lines which are not too long for IRC.
442 The length is specified in `erc-split-line-length'.
443
444 Currently this is called by `erc-send-input'."
445 (if (< (length longline)
446 erc-split-line-length)
447 (list longline)
448 (with-temp-buffer
449 (insert longline)
450 (let ((fill-column erc-split-line-length))
451 (fill-region (point-min) (point-max)
452 nil t))
453 (split-string (buffer-string) "\n"))))
454
455 ;; Used by CTCP functions
456 (defun erc-upcase-first-word (str)
457 "Upcase the first word in STR."
458 (with-temp-buffer
459 (insert str)
460 (goto-char (point-min))
461 (upcase-word 1)
462 (buffer-string)))
463
464 (defun erc-server-setup-periodical-ping (buffer)
465 "Set up a timer to periodically ping the current server.
466 The current buffer is given by BUFFER."
467 (with-current-buffer buffer
468 (and erc-server-ping-handler (erc-cancel-timer erc-server-ping-handler))
469 (when erc-server-send-ping-interval
470 (setq erc-server-ping-handler (run-with-timer
471 4 erc-server-send-ping-interval
472 #'erc-server-send-ping
473 buffer))
474 (setq erc-server-ping-timer-alist (cons (cons buffer
475 erc-server-ping-handler)
476 erc-server-ping-timer-alist)))))
477
478 (defun erc-server-process-alive ()
479 "Return non-nil when `erc-server-process' is open or running."
480 (and erc-server-process
481 (processp erc-server-process)
482 (memq (process-status erc-server-process) '(run open))))
483
484 ;;;; Connecting to a server
485
486 (defun erc-server-connect (server port buffer)
487 "Perform the connection and login using the specified SERVER and PORT.
488 We will store server variables in the buffer given by BUFFER."
489 (let ((msg (erc-format-message 'connect ?S server ?p port)))
490 (message "%s" msg)
491 (let ((process (funcall erc-server-connect-function
492 (format "erc-%s-%s" server port)
493 nil server port)))
494 (message "%s...done" msg)
495 ;; Misc server variables
496 (with-current-buffer buffer
497 (setq erc-server-process process)
498 (setq erc-server-quitting nil)
499 (setq erc-server-reconnecting nil)
500 (setq erc-server-timed-out nil)
501 (setq erc-server-banned nil)
502 (setq erc-server-error-occurred nil)
503 (let ((time (erc-current-time)))
504 (setq erc-server-last-sent-time time)
505 (setq erc-server-last-ping-time time)
506 (setq erc-server-last-received-time time))
507 (setq erc-server-lines-sent 0)
508 ;; last peers (sender and receiver)
509 (setq erc-server-last-peers '(nil . nil)))
510 ;; we do our own encoding and decoding
511 (when (fboundp 'set-process-coding-system)
512 (set-process-coding-system process 'raw-text))
513 ;; process handlers
514 (set-process-sentinel process 'erc-process-sentinel)
515 (set-process-filter process 'erc-server-filter-function)
516 (set-process-buffer process buffer)))
517 (erc-log "\n\n\n********************************************\n")
518 (message "%s" (erc-format-message
519 'login ?n
520 (with-current-buffer buffer (erc-current-nick))))
521 ;; wait with script loading until we receive a confirmation (first
522 ;; MOTD line)
523 (if (eq erc-server-connect-function 'open-network-stream-nowait)
524 ;; it's a bit unclear otherwise that it's attempting to establish a
525 ;; connection
526 (erc-display-message nil nil buffer "Opening connection..\n")
527 (erc-login)))
528
529 (defun erc-server-reconnect ()
530 "Reestablish the current IRC connection.
531 Make sure you are in an ERC buffer when running this."
532 (let ((buffer (erc-server-buffer)))
533 (unless (buffer-live-p buffer)
534 (if (eq major-mode 'erc-mode)
535 (setq buffer (current-buffer))
536 (error "Reconnect must be run from an ERC buffer")))
537 (with-current-buffer buffer
538 (erc-update-mode-line)
539 (erc-set-active-buffer (current-buffer))
540 (setq erc-server-last-sent-time 0)
541 (setq erc-server-lines-sent 0)
542 (erc-open erc-session-server erc-session-port erc-server-current-nick
543 erc-session-user-full-name t erc-session-password))))
544
545 (defun erc-server-filter-function (process string)
546 "The process filter for the ERC server."
547 (with-current-buffer (process-buffer process)
548 (setq erc-server-last-received-time (erc-current-time))
549 ;; If you think this is written in a weird way - please refer to the
550 ;; docstring of `erc-server-processing-p'
551 (if erc-server-processing-p
552 (setq erc-server-filter-data
553 (if erc-server-filter-data
554 (concat erc-server-filter-data string)
555 string))
556 ;; This will be true even if another process is spawned!
557 (let ((erc-server-processing-p t))
558 (setq erc-server-filter-data (if erc-server-filter-data
559 (concat erc-server-filter-data
560 string)
561 string))
562 (while (and erc-server-filter-data
563 (string-match "[\n\r]+" erc-server-filter-data))
564 (let ((line (substring erc-server-filter-data
565 0 (match-beginning 0))))
566 (setq erc-server-filter-data
567 (if (= (match-end 0)
568 (length erc-server-filter-data))
569 nil
570 (substring erc-server-filter-data
571 (match-end 0))))
572 (erc-parse-server-response process line)))))))
573
574 (defsubst erc-server-reconnect-p (event)
575 "Return non-nil if ERC should attempt to reconnect automatically.
576 EVENT is the message received from the closed connection process."
577 (or erc-server-reconnecting
578 (and erc-server-auto-reconnect
579 (not erc-server-banned)
580 (not erc-server-error-occurred)
581 ;; make sure we don't infinitely try to reconnect, unless the
582 ;; user wants that
583 (or (eq erc-server-reconnect-attempts t)
584 (and (integerp erc-server-reconnect-attempts)
585 (< erc-server-reconnect-count
586 erc-server-reconnect-attempts)))
587 (or erc-server-timed-out
588 (not (string-match "^deleted" event)))
589 ;; open-network-stream-nowait error for connection refused
590 (not (string-match "^failed with code 111" event)))))
591
592 (defun erc-process-sentinel-2 (event buffer)
593 "Called when `erc-process-sentinel-1' has detected an unexpected disconnect."
594 (if (not (buffer-live-p buffer))
595 (erc-update-mode-line)
596 (with-current-buffer buffer
597 (let ((reconnect-p (erc-server-reconnect-p event)))
598 (erc-display-message nil 'error (current-buffer)
599 (if reconnect-p 'disconnected
600 'disconnected-noreconnect))
601 (if (not reconnect-p)
602 ;; terminate, do not reconnect
603 (progn
604 (erc-display-message nil 'error (current-buffer)
605 'terminated ?e event)
606 ;; Update mode line indicators
607 (erc-update-mode-line)
608 (set-buffer-modified-p nil))
609 ;; reconnect
610 (condition-case err
611 (progn
612 (setq erc-server-reconnecting nil)
613 (erc-server-reconnect)
614 (setq erc-server-reconnect-count 0))
615 (error (when (buffer-live-p buffer)
616 (set-buffer buffer)
617 (if (integerp erc-server-reconnect-attempts)
618 (setq erc-server-reconnect-count
619 (1+ erc-server-reconnect-count))
620 (message "%s ... %s"
621 "Reconnecting until we succeed"
622 "kill the ERC server buffer to stop"))
623 (if (numberp erc-server-reconnect-timeout)
624 (run-at-time erc-server-reconnect-timeout nil
625 #'erc-process-sentinel-2
626 event buffer)
627 (error (concat "`erc-server-reconnect-timeout`"
628 " must be a number")))))))))))
629
630 (defun erc-process-sentinel-1 (event buffer)
631 "Called when `erc-process-sentinel' has decided that we're disconnecting.
632 Determine whether user has quit or whether erc has been terminated.
633 Conditionally try to reconnect and take appropriate action."
634 (with-current-buffer buffer
635 (if erc-server-quitting
636 ;; normal quit
637 (progn
638 (erc-display-message nil 'error (current-buffer) 'finished)
639 ;; Update mode line indicators
640 (erc-update-mode-line)
641 ;; Kill server buffer if user wants it
642 (set-buffer-modified-p nil)
643 (when erc-kill-server-buffer-on-quit
644 (kill-buffer (current-buffer))))
645 ;; unexpected disconnect
646 (erc-process-sentinel-2 event buffer))))
647
648 (defun erc-process-sentinel (cproc event)
649 "Sentinel function for ERC process."
650 (with-current-buffer (process-buffer cproc)
651 (erc-log (format
652 "SENTINEL: proc: %S status: %S event: %S (quitting: %S)"
653 cproc (process-status cproc) event erc-server-quitting))
654 (if (string-match "^open" event)
655 ;; newly opened connection (no wait)
656 (erc-login)
657 ;; assume event is 'failed
658 (let ((buf (process-buffer cproc)))
659 (erc-with-all-buffers-of-server cproc nil
660 (setq erc-server-connected nil))
661 (when erc-server-ping-handler
662 (progn (erc-cancel-timer erc-server-ping-handler)
663 (setq erc-server-ping-handler nil)))
664 (run-hook-with-args 'erc-disconnected-hook
665 (erc-current-nick) (system-name) "")
666 ;; Remove the prompt
667 (goto-char (or (marker-position erc-input-marker) (point-max)))
668 (forward-line 0)
669 (erc-remove-text-properties-region (point) (point-max))
670 (delete-region (point) (point-max))
671 ;; Decide what to do with the buffer
672 ;; Restart if disconnected
673 (erc-process-sentinel-1 event buf)))))
674
675 ;;;; Sending messages
676
677 (defun erc-coding-system-for-target (target)
678 "Return the coding system or cons cell appropriate for TARGET.
679 This is determined via `erc-encoding-coding-alist' or
680 `erc-server-coding-system'."
681 (unless target (setq target (erc-default-target)))
682 (or (when target
683 (let ((case-fold-search t))
684 (catch 'match
685 (dolist (pat erc-encoding-coding-alist)
686 (when (string-match (car pat) target)
687 (throw 'match (cdr pat)))))))
688 (and (functionp erc-server-coding-system)
689 (funcall erc-server-coding-system))
690 erc-server-coding-system))
691
692 (defun erc-decode-string-from-target (str target)
693 "Decode STR as appropriate for TARGET.
694 This is indicated by `erc-encoding-coding-alist', defaulting to the value of
695 `erc-server-coding-system'."
696 (unless (stringp str)
697 (setq str ""))
698 (let ((coding (erc-coding-system-for-target target)))
699 (when (consp coding)
700 (setq coding (cdr coding)))
701 (erc-decode-coding-string str coding)))
702
703 ;; proposed name, not used by anything yet
704 (defun erc-send-line (text display-fn)
705 "Send TEXT to the current server. Wrapping and flood control apply.
706 Use DISPLAY-FN to show the results."
707 (mapc (lambda (line)
708 (erc-server-send line)
709 (funcall display-fn))
710 (erc-split-line text)))
711
712 ;; From Circe, with modifications
713 (defun erc-server-send (string &optional forcep target)
714 "Send STRING to the current server.
715 If FORCEP is non-nil, no flood protection is done - the string is
716 sent directly. This might cause the messages to arrive in a wrong
717 order.
718
719 If TARGET is specified, look up encoding information for that
720 channel in `erc-encoding-coding-alist' or
721 `erc-server-coding-system'.
722
723 See `erc-server-flood-margin' for an explanation of the flood
724 protection algorithm."
725 (erc-log (concat "erc-server-send: " string "(" (buffer-name) ")"))
726 (setq erc-server-last-sent-time (erc-current-time))
727 (let ((encoding (erc-coding-system-for-target target)))
728 (when (consp encoding)
729 (setq encoding (car encoding)))
730 (if (erc-server-process-alive)
731 (erc-with-server-buffer
732 (let ((str (concat string "\r\n")))
733 (if forcep
734 (progn
735 (setq erc-server-flood-last-message
736 (+ erc-server-flood-penalty
737 erc-server-flood-last-message))
738 (erc-log-irc-protocol str 'outbound)
739 (condition-case err
740 (progn
741 ;; Set encoding just before sending the string
742 (when (fboundp 'set-process-coding-system)
743 (set-process-coding-system erc-server-process
744 'raw-text encoding))
745 (process-send-string erc-server-process str))
746 ;; See `erc-server-send-queue' for full
747 ;; explanation of why we need this condition-case
748 (error nil)))
749 (setq erc-server-flood-queue
750 (append erc-server-flood-queue
751 (list (cons str encoding))))
752 (erc-server-send-queue (current-buffer))))
753 t)
754 (message "ERC: No process running")
755 nil)))
756
757 (defun erc-server-send-ping (buf)
758 "Send a ping to the IRC server buffer in BUF.
759 Additionally, detect whether the IRC process has hung."
760 (if (buffer-live-p buf)
761 (with-current-buffer buf
762 (if (and erc-server-send-ping-timeout
763 (>
764 (erc-time-diff (erc-current-time)
765 erc-server-last-received-time)
766 erc-server-send-ping-timeout))
767 (progn
768 ;; if the process is hung, kill it
769 (setq erc-server-timed-out t)
770 (delete-process erc-server-process))
771 (erc-server-send (format "PING %.0f" (erc-current-time)))))
772 ;; remove timer if the server buffer has been killed
773 (let ((timer (assq buf erc-server-ping-timer-alist)))
774 (when timer
775 (erc-cancel-timer (cdr timer))
776 (setcdr timer nil)))))
777
778 ;; From Circe
779 (defun erc-server-send-queue (buffer)
780 "Send messages in `erc-server-flood-queue'.
781 See `erc-server-flood-margin' for an explanation of the flood
782 protection algorithm."
783 (with-current-buffer buffer
784 (let ((now (erc-current-time)))
785 (when erc-server-flood-timer
786 (erc-cancel-timer erc-server-flood-timer)
787 (setq erc-server-flood-timer nil))
788 (when (< erc-server-flood-last-message
789 now)
790 (setq erc-server-flood-last-message now))
791 (while (and erc-server-flood-queue
792 (< erc-server-flood-last-message
793 (+ now erc-server-flood-margin)))
794 (let ((msg (caar erc-server-flood-queue))
795 (encoding (cdar erc-server-flood-queue)))
796 (setq erc-server-flood-queue (cdr erc-server-flood-queue)
797 erc-server-flood-last-message
798 (+ erc-server-flood-last-message
799 erc-server-flood-penalty))
800 (erc-log-irc-protocol msg 'outbound)
801 (erc-log (concat "erc-server-send-queue: "
802 msg "(" (buffer-name buffer) ")"))
803 (when (erc-server-process-alive)
804 (condition-case err
805 ;; Set encoding just before sending the string
806 (progn
807 (when (fboundp 'set-process-coding-system)
808 (set-process-coding-system erc-server-process
809 'raw-text encoding))
810 (process-send-string erc-server-process msg))
811 ;; Sometimes the send can occur while the process is
812 ;; being killed, which results in a weird SIGPIPE error.
813 ;; Catch this and ignore it.
814 (error nil)))))
815 (when erc-server-flood-queue
816 (setq erc-server-flood-timer
817 (run-at-time (+ 0.2 erc-server-flood-penalty)
818 nil #'erc-server-send-queue buffer))))))
819
820 (defun erc-message (message-command line &optional force)
821 "Send LINE to the server as a privmsg or a notice.
822 MESSAGE-COMMAND should be either \"PRIVMSG\" or \"NOTICE\".
823 If the target is \",\", the last person you've got a message from will
824 be used. If the target is \".\", the last person you've sent a message
825 to will be used."
826 (cond
827 ((string-match "^\\s-*\\(\\S-+\\) ?\\(.*\\)" line)
828 (let ((tgt (match-string 1 line))
829 (s (match-string 2 line)))
830 (erc-log (format "cmd: MSG(%s): [%s] %s" message-command tgt s))
831 (cond
832 ((string= tgt ",")
833 (if (car erc-server-last-peers)
834 (setq tgt (car erc-server-last-peers))
835 (setq tgt nil)))
836 ((string= tgt ".")
837 (if (cdr erc-server-last-peers)
838 (setq tgt (cdr erc-server-last-peers))
839 (setq tgt nil))))
840 (cond
841 (tgt
842 (setcdr erc-server-last-peers tgt)
843 (erc-server-send (format "%s %s :%s" message-command tgt s)
844 force))
845 (t
846 (erc-display-message nil 'error (current-buffer) 'no-target))))
847 t)
848 (t nil)))
849
850 ;;; CTCP
851
852 (defun erc-send-ctcp-message (tgt l &optional force)
853 "Send CTCP message L to TGT.
854
855 If TGT is nil the message is not sent.
856 The command must contain neither a prefix nor a trailing `\\n'.
857
858 See also `erc-server-send'."
859 (let ((l (erc-upcase-first-word l)))
860 (cond
861 (tgt
862 (erc-log (format "erc-send-CTCP-message: [%s] %s" tgt l))
863 (erc-server-send (format "PRIVMSG %s :\C-a%s\C-a" tgt l)
864 force)))))
865
866 (defun erc-send-ctcp-notice (tgt l &optional force)
867 "Send CTCP notice L to TGT.
868
869 If TGT is nil the message is not sent.
870 The command must contain neither a prefix nor a trailing `\\n'.
871
872 See also `erc-server-send'."
873 (let ((l (erc-upcase-first-word l)))
874 (cond
875 (tgt
876 (erc-log (format "erc-send-CTCP-notice: [%s] %s" tgt l))
877 (erc-server-send (format "NOTICE %s :\C-a%s\C-a" tgt l)
878 force)))))
879
880 ;;;; Handling responses
881
882 (defun erc-parse-server-response (proc string)
883 "Parse and act upon a complete line from an IRC server.
884 PROC is the process (connection) from which STRING was received.
885 PROCs `process-buffer' is `current-buffer' when this function is called."
886 (unless (string= string "") ;; Ignore empty strings
887 (save-match-data
888 (let ((posn (if (eq (aref string 0) ?:)
889 (string-match " " string)
890 0))
891 (msg (make-erc-response :unparsed string)))
892
893 (setf (erc-response.sender msg)
894 (if (eq posn 0)
895 erc-session-server
896 (substring string 1 posn)))
897
898 (setf (erc-response.command msg)
899 (let* ((bposn (string-match "[^ \n]" string posn))
900 (eposn (string-match " " string bposn)))
901 (setq posn (and eposn
902 (string-match "[^ \n]" string eposn)))
903 (substring string bposn eposn)))
904
905 (while (and posn
906 (not (eq (aref string posn) ?:)))
907 (push (let* ((bposn posn)
908 (eposn (string-match " " string bposn)))
909 (setq posn (and eposn
910 (string-match "[^ \n]" string eposn)))
911 (substring string bposn eposn))
912 (erc-response.command-args msg)))
913 (when posn
914 (let ((str (substring string (1+ posn))))
915 (push str (erc-response.command-args msg))))
916
917 (setf (erc-response.contents msg)
918 (first (erc-response.command-args msg)))
919
920 (setf (erc-response.command-args msg)
921 (nreverse (erc-response.command-args msg)))
922
923 (erc-decode-parsed-server-response msg)
924
925 (erc-handle-parsed-server-response proc msg)))))
926
927 (defun erc-decode-parsed-server-response (parsed-response)
928 "Decode a pre-parsed PARSED-RESPONSE before it can be handled.
929
930 If there is a channel name in `erc-response.command-args', decode
931 `erc-response' according to this channel name and
932 `erc-encoding-coding-alist', or use `erc-server-coding-system'
933 for decoding."
934 (let ((args (erc-response.command-args parsed-response))
935 (decode-target nil)
936 (decoded-args ()))
937 (dolist (arg args nil)
938 (when (string-match "^[#&].*" arg)
939 (setq decode-target arg)))
940 (when (stringp decode-target)
941 (setq decode-target (erc-decode-string-from-target decode-target nil)))
942 (setf (erc-response.unparsed parsed-response)
943 (erc-decode-string-from-target
944 (erc-response.unparsed parsed-response)
945 decode-target))
946 (setf (erc-response.sender parsed-response)
947 (erc-decode-string-from-target
948 (erc-response.sender parsed-response)
949 decode-target))
950 (setf (erc-response.command parsed-response)
951 (erc-decode-string-from-target
952 (erc-response.command parsed-response)
953 decode-target))
954 (dolist (arg (nreverse args) nil)
955 (push (erc-decode-string-from-target arg decode-target)
956 decoded-args))
957 (setf (erc-response.command-args parsed-response) decoded-args)
958 (setf (erc-response.contents parsed-response)
959 (erc-decode-string-from-target
960 (erc-response.contents parsed-response)
961 decode-target))))
962
963 (defun erc-handle-parsed-server-response (process parsed-response)
964 "Handle a pre-parsed PARSED-RESPONSE from PROCESS.
965
966 Hands off to helper functions via `erc-call-hooks'."
967 (if (member (erc-response.command parsed-response)
968 erc-server-prevent-duplicates)
969 (let ((m (erc-response.unparsed parsed-response)))
970 ;; duplicate supression
971 (if (< (or (gethash m erc-server-duplicates) 0)
972 (- (erc-current-time) erc-server-duplicate-timeout))
973 (erc-call-hooks process parsed-response))
974 (puthash m (erc-current-time) erc-server-duplicates))
975 ;; Hand off to the relevant handler.
976 (erc-call-hooks process parsed-response)))
977
978 (defun erc-get-hook (command)
979 "Return the hook variable associated with COMMAND.
980
981 See also `erc-server-responses'."
982 (gethash (format (if (numberp command) "%03i" "%s") command)
983 erc-server-responses))
984
985 (defun erc-call-hooks (process message)
986 "Call hooks associated with MESSAGE in PROCESS.
987
988 Finds hooks by looking in the `erc-server-responses' hashtable."
989 (let ((hook (or (erc-get-hook (erc-response.command message))
990 'erc-default-server-functions)))
991 (run-hook-with-args-until-success hook process message)
992 (erc-with-server-buffer
993 (run-hook-with-args 'erc-timer-hook (erc-current-time)))))
994
995 (add-hook 'erc-default-server-functions 'erc-handle-unknown-server-response)
996
997 (defun erc-handle-unknown-server-response (proc parsed)
998 "Display unknown server response's message."
999 (let ((line (concat (erc-response.sender parsed)
1000 " "
1001 (erc-response.command parsed)
1002 " "
1003 (mapconcat 'identity (erc-response.command-args parsed)
1004 " "))))
1005 (erc-display-message parsed 'notice proc line)))
1006
1007
1008 (put 'define-erc-response-handler 'edebug-form-spec
1009 '(&define :name erc-response-handler
1010 (name &rest name)
1011 &optional sexp sexp def-body))
1012
1013 (defmacro* define-erc-response-handler ((name &rest aliases)
1014 &optional extra-fn-doc extra-var-doc
1015 &rest fn-body)
1016 "Define an ERC handler hook/function pair.
1017 NAME is the response name as sent by the server (see the IRC RFC for
1018 meanings).
1019
1020 This creates:
1021 - a hook variable `erc-server-NAME-functions' initialized to `erc-server-NAME'.
1022 - a function `erc-server-NAME' with body FN-BODY.
1023
1024 If ALIASES is non-nil, each alias in ALIASES is `defalias'ed to
1025 `erc-server-NAME'.
1026 Alias hook variables are created as `erc-server-ALIAS-functions' and
1027 initialized to the same default value as `erc-server-NAME-functions'.
1028
1029 FN-BODY is the body of `erc-server-NAME' it may refer to the two
1030 function arguments PROC and PARSED.
1031
1032 If EXTRA-FN-DOC is non-nil, it is inserted at the beginning of the
1033 defined function's docstring.
1034
1035 If EXTRA-VAR-DOC is non-nil, it is inserted at the beginning of the
1036 defined variable's docstring.
1037
1038 As an example:
1039
1040 (define-erc-response-handler (311 WHOIS WI)
1041 \"Some non-generic function documentation.\"
1042 \"Some non-generic variable documentation.\"
1043 (do-stuff-with-whois proc parsed))
1044
1045 Would expand to:
1046
1047 (prog2
1048 (defvar erc-server-311-functions 'erc-server-311
1049 \"Some non-generic variable documentation.
1050
1051 Hook called upon receiving a 311 server response.
1052 Each function is called with two arguments, the process associated
1053 with the response and the parsed response.
1054 See also `erc-server-311'.\")
1055
1056 (defun erc-server-311 (proc parsed)
1057 \"Some non-generic function documentation.
1058
1059 Handler for a 311 server response.
1060 PROC is the server process which returned the response.
1061 PARSED is the actual response as an `erc-response' struct.
1062 If you want to add responses don't modify this function, but rather
1063 add things to `erc-server-311-functions' instead.\"
1064 (do-stuff-with-whois proc parsed))
1065
1066 (puthash \"311\" 'erc-server-311-functions erc-server-responses)
1067 (puthash \"WHOIS\" 'erc-server-WHOIS-functions erc-server-responses)
1068 (puthash \"WI\" 'erc-server-WI-functions erc-server-responses)
1069
1070 (defalias 'erc-server-WHOIS 'erc-server-311)
1071 (defvar erc-server-WHOIS-functions 'erc-server-311
1072 \"Some non-generic variable documentation.
1073
1074 Hook called upon receiving a WHOIS server response.
1075
1076 Each function is called with two arguments, the process associated
1077 with the response and the parsed response. If the function returns
1078 non-nil, stop processing the hook. Otherwise, continue.
1079
1080 See also `erc-server-311'.\")
1081
1082 (defalias 'erc-server-WI 'erc-server-311)
1083 (defvar erc-server-WI-functions 'erc-server-311
1084 \"Some non-generic variable documentation.
1085
1086 Hook called upon receiving a WI server response.
1087 Each function is called with two arguments, the process associated
1088 with the response and the parsed response. If the function returns
1089 non-nil, stop processing the hook. Otherwise, continue.
1090
1091 See also `erc-server-311'.\"))
1092
1093 \(fn (NAME &rest ALIASES) &optional EXTRA-FN-DOC EXTRA-VAR-DOC &rest FN-BODY)"
1094 (if (numberp name) (setq name (intern (format "%03i" name))))
1095 (setq aliases (mapcar (lambda (a)
1096 (if (numberp a)
1097 (format "%03i" a)
1098 a))
1099 aliases))
1100 (let* ((hook-name (intern (format "erc-server-%s-functions" name)))
1101 (fn-name (intern (format "erc-server-%s" name)))
1102 (hook-doc (format "%sHook called upon receiving a %%s server response.
1103 Each function is called with two arguments, the process associated
1104 with the response and the parsed response. If the function returns
1105 non-nil, stop processing the hook. Otherwise, continue.
1106
1107 See also `%s'."
1108 (if extra-var-doc
1109 (concat extra-var-doc "\n\n")
1110 "")
1111 fn-name))
1112 (fn-doc (format "%sHandler for a %s server response.
1113 PROC is the server process which returned the response.
1114 PARSED is the actual response as an `erc-response' struct.
1115 If you want to add responses don't modify this function, but rather
1116 add things to `%s' instead."
1117 (if extra-fn-doc
1118 (concat extra-fn-doc "\n\n")
1119 "")
1120 name hook-name))
1121 (fn-alternates
1122 (loop for alias in aliases
1123 collect (intern (format "erc-server-%s" alias))))
1124 (var-alternates
1125 (loop for alias in aliases
1126 collect (intern (format "erc-server-%s-functions" alias)))))
1127 `(prog2
1128 ;; Normal hook variable.
1129 (defvar ,hook-name ',fn-name ,(format hook-doc name))
1130 ;; Handler function
1131 (defun ,fn-name (proc parsed)
1132 ,fn-doc
1133 ,@fn-body)
1134
1135 ;; Make find-function and find-variable find them
1136 (put ',fn-name 'definition-name ',name)
1137 (put ',hook-name 'definition-name ',name)
1138
1139 ;; Hashtable map of responses to hook variables
1140 ,@(loop for response in (cons name aliases)
1141 for var in (cons hook-name var-alternates)
1142 collect `(puthash ,(format "%s" response) ',var
1143 erc-server-responses))
1144 ;; Alternates.
1145 ;; Functions are defaliased, hook variables are defvared so we
1146 ;; can add hooks to one alias, but not another.
1147 ,@(loop for fn in fn-alternates
1148 for var in var-alternates
1149 for a in aliases
1150 nconc (list `(defalias ',fn ',fn-name)
1151 `(defvar ,var ',fn-name ,(format hook-doc a))
1152 `(put ',var 'definition-name ',hook-name))))))
1153
1154 (define-erc-response-handler (ERROR)
1155 "Handle an ERROR command from the server." nil
1156 (setq erc-server-error-occurred t)
1157 (erc-display-message
1158 parsed 'error nil 'ERROR
1159 ?s (erc-response.sender parsed) ?c (erc-response.contents parsed)))
1160
1161 (define-erc-response-handler (INVITE)
1162 "Handle invitation messages."
1163 nil
1164 (let ((target (first (erc-response.command-args parsed)))
1165 (chnl (erc-response.contents parsed)))
1166 (multiple-value-bind (nick login host)
1167 (erc-parse-user (erc-response.sender parsed))
1168 (setq erc-invitation chnl)
1169 (when (string= target (erc-current-nick))
1170 (erc-display-message
1171 parsed 'notice 'active
1172 'INVITE ?n nick ?u login ?h host ?c chnl)))))
1173
1174
1175 (define-erc-response-handler (JOIN)
1176 "Handle join messages."
1177 nil
1178 (let ((chnl (erc-response.contents parsed))
1179 (buffer nil))
1180 (multiple-value-bind (nick login host)
1181 (erc-parse-user (erc-response.sender parsed))
1182 ;; strip the stupid combined JOIN facility (IRC 2.9)
1183 (if (string-match "^\\(.*\\)?\^g.*$" chnl)
1184 (setq chnl (match-string 1 chnl)))
1185 (save-excursion
1186 (let* ((str (cond
1187 ;; If I have joined a channel
1188 ((erc-current-nick-p nick)
1189 (setq buffer (erc-open erc-session-server erc-session-port
1190 nick erc-session-user-full-name
1191 nil nil
1192 erc-default-recipients chnl
1193 erc-server-process))
1194 (when buffer
1195 (set-buffer buffer)
1196 (erc-add-default-channel chnl)
1197 (erc-server-send (format "MODE %s" chnl)))
1198 (erc-with-buffer (chnl proc)
1199 (erc-channel-begin-receiving-names))
1200 (erc-update-mode-line)
1201 (run-hooks 'erc-join-hook)
1202 (erc-make-notice
1203 (erc-format-message 'JOIN-you ?c chnl)))
1204 (t
1205 (setq buffer (erc-get-buffer chnl proc))
1206 (erc-make-notice
1207 (erc-format-message
1208 'JOIN ?n nick ?u login ?h host ?c chnl))))))
1209 (when buffer (set-buffer buffer))
1210 (erc-update-channel-member chnl nick nick t nil nil host login)
1211 ;; on join, we want to stay in the new channel buffer
1212 ;;(set-buffer ob)
1213 (erc-display-message parsed nil buffer str))))))
1214
1215 (define-erc-response-handler (KICK)
1216 "Handle kick messages received from the server." nil
1217 (let* ((ch (first (erc-response.command-args parsed)))
1218 (tgt (second (erc-response.command-args parsed)))
1219 (reason (erc-trim-string (erc-response.contents parsed)))
1220 (buffer (erc-get-buffer ch proc)))
1221 (multiple-value-bind (nick login host)
1222 (erc-parse-user (erc-response.sender parsed))
1223 (erc-remove-channel-member buffer tgt)
1224 (cond
1225 ((string= tgt (erc-current-nick))
1226 (erc-display-message
1227 parsed 'notice buffer
1228 'KICK-you ?n nick ?u login ?h host ?c ch ?r reason)
1229 (run-hook-with-args 'erc-kick-hook buffer)
1230 (erc-with-buffer
1231 (buffer)
1232 (erc-remove-channel-users))
1233 (erc-delete-default-channel ch buffer)
1234 (erc-update-mode-line buffer))
1235 ((string= nick (erc-current-nick))
1236 (erc-display-message
1237 parsed 'notice buffer
1238 'KICK-by-you ?k tgt ?c ch ?r reason))
1239 (t (erc-display-message
1240 parsed 'notice buffer
1241 'KICK ?k tgt ?n nick ?u login ?h host ?c ch ?r reason))))))
1242
1243 (define-erc-response-handler (MODE)
1244 "Handle server mode changes." nil
1245 (let ((tgt (first (erc-response.command-args parsed)))
1246 (mode (mapconcat 'identity (cdr (erc-response.command-args parsed))
1247 " ")))
1248 (multiple-value-bind (nick login host)
1249 (erc-parse-user (erc-response.sender parsed))
1250 (erc-log (format "MODE: %s -> %s: %s" nick tgt mode))
1251 ;; dirty hack
1252 (let ((buf (cond ((erc-channel-p tgt)
1253 (erc-get-buffer tgt proc))
1254 ((string= tgt (erc-current-nick)) nil)
1255 ((erc-active-buffer) (erc-active-buffer))
1256 (t (erc-get-buffer tgt)))))
1257 (with-current-buffer (or buf
1258 (current-buffer))
1259 (erc-update-modes tgt mode nick host login))
1260 (if (or (string= login "") (string= host ""))
1261 (erc-display-message parsed 'notice buf
1262 'MODE-nick ?n nick
1263 ?t tgt ?m mode)
1264 (erc-display-message parsed 'notice buf
1265 'MODE ?n nick ?u login
1266 ?h host ?t tgt ?m mode)))
1267 (erc-banlist-update proc parsed))))
1268
1269 (define-erc-response-handler (NICK)
1270 "Handle nick change messages." nil
1271 (let ((nn (erc-response.contents parsed))
1272 bufs)
1273 (multiple-value-bind (nick login host)
1274 (erc-parse-user (erc-response.sender parsed))
1275 (setq bufs (erc-buffer-list-with-nick nick proc))
1276 (erc-log (format "NICK: %s -> %s" nick nn))
1277 ;; if we had a query with this user, make sure future messages will be
1278 ;; sent to the correct nick. also add to bufs, since the user will want
1279 ;; to see the nick change in the query, and if it's a newly begun query,
1280 ;; erc-channel-users won't contain it
1281 (erc-buffer-filter
1282 (lambda ()
1283 (when (equal (erc-default-target) nick)
1284 (setq erc-default-recipients
1285 (cons nn (cdr erc-default-recipients)))
1286 (rename-buffer nn)
1287 (erc-update-mode-line)
1288 (add-to-list 'bufs (current-buffer)))))
1289 (erc-update-user-nick nick nn host nil nil login)
1290 (cond
1291 ((string= nick (erc-current-nick))
1292 (add-to-list 'bufs (erc-server-buffer))
1293 (erc-set-current-nick nn)
1294 (erc-update-mode-line)
1295 (setq erc-nick-change-attempt-count 0)
1296 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
1297 (erc-display-message
1298 parsed 'notice bufs
1299 'NICK-you ?n nick ?N nn)
1300 (run-hook-with-args 'erc-nick-changed-functions nn nick))
1301 (t
1302 (erc-handle-user-status-change 'nick (list nick login host) (list nn))
1303 (erc-display-message parsed 'notice bufs 'NICK ?n nick
1304 ?u login ?h host ?N nn))))))
1305
1306 (define-erc-response-handler (PART)
1307 "Handle part messages." nil
1308 (let* ((chnl (first (erc-response.command-args parsed)))
1309 (reason (erc-trim-string (erc-response.contents parsed)))
1310 (buffer (erc-get-buffer chnl proc)))
1311 (multiple-value-bind (nick login host)
1312 (erc-parse-user (erc-response.sender parsed))
1313 (erc-remove-channel-member buffer nick)
1314 (erc-display-message parsed 'notice buffer
1315 'PART ?n nick ?u login
1316 ?h host ?c chnl ?r (or reason ""))
1317 (when (string= nick (erc-current-nick))
1318 (run-hook-with-args 'erc-part-hook buffer)
1319 (erc-with-buffer
1320 (buffer)
1321 (erc-remove-channel-users))
1322 (erc-delete-default-channel chnl buffer)
1323 (erc-update-mode-line buffer)
1324 (when erc-kill-buffer-on-part
1325 (kill-buffer buffer))))))
1326
1327 (define-erc-response-handler (PING)
1328 "Handle ping messages." nil
1329 (let ((pinger (first (erc-response.command-args parsed))))
1330 (erc-log (format "PING: %s" pinger))
1331 ;; ping response to the server MUST be forced, or you can lose big
1332 (erc-server-send (format "PONG :%s" pinger) t)
1333 (when erc-verbose-server-ping
1334 (erc-display-message
1335 parsed 'error proc
1336 'PING ?s (erc-time-diff erc-server-last-ping-time (erc-current-time))))
1337 (setq erc-server-last-ping-time (erc-current-time))))
1338
1339 (define-erc-response-handler (PONG)
1340 "Handle pong messages." nil
1341 (let ((time (string-to-number (erc-response.contents parsed))))
1342 (when (> time 0)
1343 (setq erc-server-lag (erc-time-diff time (erc-current-time)))
1344 (when erc-verbose-server-ping
1345 (erc-display-message
1346 parsed 'notice proc 'PONG
1347 ?h (first (erc-response.command-args parsed)) ?i erc-server-lag
1348 ?s (if (/= erc-server-lag 1) "s" "")))
1349 (erc-update-mode-line))))
1350
1351 (define-erc-response-handler (PRIVMSG NOTICE)
1352 nil nil
1353 (let ((sender-spec (erc-response.sender parsed))
1354 (cmd (erc-response.command parsed))
1355 (tgt (car (erc-response.command-args parsed)))
1356 (msg (erc-response.contents parsed)))
1357 (if (or (erc-ignored-user-p sender-spec)
1358 (erc-ignored-reply-p msg tgt proc))
1359 (when erc-minibuffer-ignored
1360 (message "Ignored %s from %s to %s" cmd sender-spec tgt))
1361 (let* ((sndr (erc-parse-user sender-spec))
1362 (nick (nth 0 sndr))
1363 (login (nth 1 sndr))
1364 (host (nth 2 sndr))
1365 (msgp (string= cmd "PRIVMSG"))
1366 (noticep (string= cmd "NOTICE"))
1367 ;; S.B. downcase *both* tgt and current nick
1368 (privp (erc-current-nick-p tgt))
1369 s buffer
1370 fnick)
1371 (setf (erc-response.contents parsed) msg)
1372 (setq buffer (erc-get-buffer (if privp nick tgt) proc))
1373 (when buffer
1374 (with-current-buffer buffer
1375 ;; update the chat partner info. Add to the list if private
1376 ;; message. We will accumulate private identities indefinitely
1377 ;; at this point.
1378 (erc-update-channel-member (if privp nick tgt) nick nick
1379 privp nil nil host login nil nil t)
1380 (let ((cdata (erc-get-channel-user nick)))
1381 (setq fnick (funcall erc-format-nick-function
1382 (car cdata) (cdr cdata))))))
1383 (cond
1384 ((erc-is-message-ctcp-p msg)
1385 (setq s (if msgp
1386 (erc-process-ctcp-query proc parsed nick login host)
1387 (erc-process-ctcp-reply proc parsed nick login host
1388 (match-string 1 msg)))))
1389 (t
1390 (setcar erc-server-last-peers nick)
1391 (setq s (erc-format-privmessage
1392 (or fnick nick) msg
1393 ;; If buffer is a query buffer,
1394 ;; format the nick as for a channel.
1395 (and (not (and buffer
1396 (erc-query-buffer-p buffer)
1397 erc-format-query-as-channel-p))
1398 privp)
1399 msgp))))
1400 (when s
1401 (if (and noticep privp)
1402 (progn
1403 (run-hook-with-args 'erc-echo-notice-always-hook
1404 s parsed buffer nick)
1405 (run-hook-with-args-until-success
1406 'erc-echo-notice-hook s parsed buffer nick))
1407 (erc-display-message parsed nil buffer s)))
1408 (when (string= cmd "PRIVMSG")
1409 (erc-auto-query proc parsed))))))
1410
1411 ;; FIXME: need clean way of specifiying extra hooks in
1412 ;; define-erc-response-handler.
1413 (add-hook 'erc-server-PRIVMSG-functions 'erc-auto-query)
1414
1415 (define-erc-response-handler (QUIT)
1416 nil nil
1417 (let ((reason (erc-response.contents parsed))
1418 bufs)
1419 (multiple-value-bind (nick login host)
1420 (erc-parse-user (erc-response.sender parsed))
1421 (setq bufs (erc-buffer-list-with-nick nick proc))
1422 (erc-remove-user nick)
1423 (setq reason (erc-wash-quit-reason reason nick login host))
1424 (erc-display-message parsed 'notice bufs
1425 'QUIT ?n nick ?u login
1426 ?h host ?r reason))))
1427
1428 (define-erc-response-handler (TOPIC)
1429 nil nil
1430 (let* ((ch (first (erc-response.command-args parsed)))
1431 (topic (erc-trim-string (erc-response.contents parsed)))
1432 (time (format-time-string "%T %m/%d/%y" (current-time))))
1433 (multiple-value-bind (nick login host)
1434 (erc-parse-user (erc-response.sender parsed))
1435 (erc-update-channel-member ch nick nick nil nil nil host login)
1436 (erc-update-channel-topic ch (format "%s\C-o (%s, %s)" topic nick time))
1437 (erc-display-message parsed 'notice (erc-get-buffer ch proc)
1438 'TOPIC ?n nick ?u login ?h host
1439 ?c ch ?T topic))))
1440
1441 (define-erc-response-handler (WALLOPS)
1442 nil nil
1443 (let ((message (erc-response.contents parsed)))
1444 (multiple-value-bind (nick login host)
1445 (erc-parse-user (erc-response.sender parsed))
1446 (erc-display-message
1447 parsed 'notice nil
1448 'WALLOPS ?n nick ?m message))))
1449
1450 (define-erc-response-handler (001)
1451 "Set `erc-server-current-nick' to reflect server settings and display the welcome message."
1452 nil
1453 (erc-set-current-nick (first (erc-response.command-args parsed)))
1454 (erc-update-mode-line) ; needed here?
1455 (setq erc-nick-change-attempt-count 0)
1456 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
1457 (erc-display-message
1458 parsed 'notice 'active (erc-response.contents parsed)))
1459
1460 (define-erc-response-handler (MOTD 002 003 371 372 374 375)
1461 "Display the server's message of the day." nil
1462 (erc-handle-login)
1463 (erc-display-message
1464 parsed 'notice (if erc-server-connected 'active proc)
1465 (erc-response.contents parsed)))
1466
1467 (define-erc-response-handler (376 422)
1468 nil nil
1469 (erc-server-MOTD proc parsed)
1470 (erc-connection-established proc parsed))
1471
1472 (define-erc-response-handler (004)
1473 nil nil
1474 (multiple-value-bind (server-name server-version)
1475 (cdr (erc-response.command-args parsed))
1476 (setq erc-server-version server-version)
1477 (setq erc-server-announced-name server-name)
1478 (erc-update-mode-line-buffer (process-buffer proc))
1479 (erc-display-message
1480 parsed 'notice proc
1481 's004 ?s server-name ?v server-version
1482 ?U (fourth (erc-response.command-args parsed))
1483 ?C (fifth (erc-response.command-args parsed)))))
1484
1485 (define-erc-response-handler (005)
1486 "Set the variable `erc-server-parameters' and display the received message.
1487
1488 According to RFC 2812, suggests alternate servers on the network.
1489 Many servers, however, use this code to show which parameters they have set,
1490 for example, the network identifier, maximum allowed topic length, whether
1491 certain commands are accepted and more. See documentation for
1492 `erc-server-parameters' for more information on the parameters sent.
1493
1494 A server may send more than one 005 message."
1495 nil
1496 (let ((line (mapconcat 'identity
1497 (setf (erc-response.command-args parsed)
1498 (cdr (erc-response.command-args parsed)))
1499 " ")))
1500 (while (erc-response.command-args parsed)
1501 (let ((section (pop (erc-response.command-args parsed))))
1502 ;; fill erc-server-parameters
1503 (when (string-match "^\\([A-Z]+\\)\=\\(.*\\)$\\|^\\([A-Z]+\\)$"
1504 section)
1505 (add-to-list 'erc-server-parameters
1506 `(,(or (match-string 1 section)
1507 (match-string 3 section))
1508 .
1509 ,(match-string 2 section))))))
1510 (erc-display-message parsed 'notice proc line)))
1511
1512 (define-erc-response-handler (221)
1513 nil nil
1514 (let* ((nick (first (erc-response.command-args parsed)))
1515 (modes (mapconcat 'identity
1516 (cdr (erc-response.command-args parsed)) " ")))
1517 (erc-set-modes nick modes)
1518 (erc-display-message parsed 'notice 'active 's221 ?n nick ?m modes)))
1519
1520 (define-erc-response-handler (252)
1521 "Display the number of IRC operators online." nil
1522 (erc-display-message parsed 'notice 'active 's252
1523 ?i (second (erc-response.command-args parsed))))
1524
1525 (define-erc-response-handler (253)
1526 "Display the number of unknown connections." nil
1527 (erc-display-message parsed 'notice 'active 's253
1528 ?i (second (erc-response.command-args parsed))))
1529
1530 (define-erc-response-handler (254)
1531 "Display the number of channels formed." nil
1532 (erc-display-message parsed 'notice 'active 's254
1533 ?i (second (erc-response.command-args parsed))))
1534
1535 (define-erc-response-handler (250 251 255 256 257 258 259 265 266 377 378)
1536 "Generic display of server messages as notices.
1537
1538 See `erc-display-server-message'." nil
1539 (erc-display-server-message proc parsed))
1540
1541 (define-erc-response-handler (275)
1542 "Display secure connection message." nil
1543 (multiple-value-bind (nick user message)
1544 (cdr (erc-response.command-args parsed))
1545 (erc-display-message
1546 parsed 'notice 'active 's275
1547 ?n nick
1548 ?m (mapconcat 'identity (cddr (erc-response.command-args parsed))
1549 " "))))
1550
1551 (define-erc-response-handler (290)
1552 "Handle dancer-ircd CAPAB messages." nil nil)
1553
1554 (define-erc-response-handler (301)
1555 "AWAY notice." nil
1556 (erc-display-message parsed 'notice 'active 's301
1557 ?n (second (erc-response.command-args parsed))
1558 ?r (erc-response.contents parsed)))
1559
1560 (define-erc-response-handler (303)
1561 "ISON reply" nil
1562 (erc-display-message parsed 'notice 'active 's303
1563 ?n (second (erc-response.command-args parsed))))
1564
1565 (define-erc-response-handler (305)
1566 "Return from AWAYness." nil
1567 (erc-process-away proc nil)
1568 (erc-display-message parsed 'notice 'active
1569 's305 ?m (erc-response.contents parsed)))
1570
1571 (define-erc-response-handler (306)
1572 "Set AWAYness." nil
1573 (erc-process-away proc t)
1574 (erc-display-message parsed 'notice 'active
1575 's306 ?m (erc-response.contents parsed)))
1576
1577 (define-erc-response-handler (307)
1578 "Display nick-identified message." nil
1579 (multiple-value-bind (nick user message)
1580 (cdr (erc-response.command-args parsed))
1581 (erc-display-message
1582 parsed 'notice 'active 's307
1583 ?n nick
1584 ?m (mapconcat 'identity (cddr (erc-response.command-args parsed))
1585 " "))))
1586
1587 (define-erc-response-handler (311 314)
1588 "WHOIS/WHOWAS notices." nil
1589 (let ((fname (erc-response.contents parsed))
1590 (catalog-entry (intern (format "s%s" (erc-response.command parsed)))))
1591 (multiple-value-bind (nick user host)
1592 (cdr (erc-response.command-args parsed))
1593 (erc-update-user-nick nick nick host nil fname user)
1594 (erc-display-message
1595 parsed 'notice 'active catalog-entry
1596 ?n nick ?f fname ?u user ?h host))))
1597
1598 (define-erc-response-handler (312)
1599 nil nil
1600 (multiple-value-bind (nick server-host)
1601 (cdr (erc-response.command-args parsed))
1602 (erc-display-message
1603 parsed 'notice 'active 's312
1604 ?n nick ?s server-host ?c (erc-response.contents parsed))))
1605
1606 (define-erc-response-handler (313)
1607 "IRC Operator response in WHOIS." nil
1608 (erc-display-message
1609 parsed 'notice 'active 's313
1610 ?n (second (erc-response.command-args parsed))))
1611
1612 (define-erc-response-handler (315 318 323 369)
1613 ;; 315 - End of WHO
1614 ;; 318 - End of WHOIS list
1615 ;; 323 - End of channel LIST
1616 ;; 369 - End of WHOWAS
1617 nil nil
1618 (ignore proc parsed))
1619
1620 (define-erc-response-handler (317)
1621 "IDLE notice." nil
1622 (multiple-value-bind (nick seconds-idle on-since time)
1623 (cdr (erc-response.command-args parsed))
1624 (setq time (when on-since
1625 (format-time-string "%T %Y/%m/%d"
1626 (erc-string-to-emacs-time on-since))))
1627 (erc-update-user-nick nick nick nil nil nil
1628 (and time (format "on since %s" time)))
1629 (if time
1630 (erc-display-message
1631 parsed 'notice 'active 's317-on-since
1632 ?n nick ?i (erc-sec-to-time (string-to-number seconds-idle)) ?t time)
1633 (erc-display-message
1634 parsed 'notice 'active 's317
1635 ?n nick ?i (erc-sec-to-time (string-to-number seconds-idle))))))
1636
1637 (define-erc-response-handler (319)
1638 nil nil
1639 (erc-display-message
1640 parsed 'notice 'active 's319
1641 ?n (second (erc-response.command-args parsed))
1642 ?c (erc-response.contents parsed)))
1643
1644 (define-erc-response-handler (320)
1645 "Identified user in WHOIS." nil
1646 (erc-display-message
1647 parsed 'notice 'active 's320
1648 ?n (second (erc-response.command-args parsed))))
1649
1650 (define-erc-response-handler (321)
1651 "LIST header." nil
1652 (setq erc-channel-list nil)
1653 (erc-display-message parsed 'notice proc 's321))
1654
1655 (define-erc-response-handler (322)
1656 "LIST notice." nil
1657 (let ((topic (erc-response.contents parsed)))
1658 (multiple-value-bind (channel num-users)
1659 (cdr (erc-response.command-args parsed))
1660 (add-to-list 'erc-channel-list (list channel))
1661 (erc-update-channel-topic channel topic)
1662 (erc-display-message
1663 parsed 'notice proc 's322
1664 ?c channel ?u num-users ?t (or topic "")))))
1665
1666 (define-erc-response-handler (324)
1667 "Channel or nick modes." nil
1668 (let ((channel (second (erc-response.command-args parsed)))
1669 (modes (mapconcat 'identity (cddr (erc-response.command-args parsed))
1670 " ")))
1671 (erc-set-modes channel modes)
1672 (erc-display-message
1673 parsed 'notice (erc-get-buffer channel proc)
1674 's324 ?c channel ?m modes)))
1675
1676 (define-erc-response-handler (329)
1677 "Channel creation date." nil
1678 (let ((channel (second (erc-response.command-args parsed)))
1679 (time (erc-string-to-emacs-time
1680 (third (erc-response.command-args parsed)))))
1681 (erc-display-message
1682 parsed 'notice (erc-get-buffer channel proc)
1683 's329 ?c channel ?t (format-time-string "%A %Y/%m/%d %X" time))))
1684
1685 (define-erc-response-handler (330)
1686 nil nil
1687 ;; FIXME: I don't know what the magic numbers mean. Mummy, make
1688 ;; the magic numbers go away.
1689 ;; No seriously, I have no clue about the format of this command,
1690 ;; and don't sit on Quakenet, so can't test. Originally we had:
1691 ;; nick == (aref parsed 3)
1692 ;; authaccount == (aref parsed 4)
1693 ;; authmsg == (aref parsed 5)
1694 ;; The guesses below are, well, just that. -- Lawrence 2004/05/10
1695 (let ((nick (second (erc-response.command-args parsed)))
1696 (authaccount (third (erc-response.command-args parsed)))
1697 (authmsg (erc-response.contents parsed)))
1698 (erc-display-message parsed 'notice 'active 's330
1699 ?n nick ?a authmsg ?i authaccount)))
1700
1701 (define-erc-response-handler (331)
1702 "Channel topic." nil
1703 (let ((channel (second (erc-response.command-args parsed)))
1704 (topic (erc-response.contents parsed)))
1705 ;; FIXME: why don't we do anything with the topic? -- Lawrence 2004/05/10
1706 (erc-display-message parsed 'notice (erc-get-buffer channel proc)
1707 's331 ?c channel)))
1708
1709 (define-erc-response-handler (332)
1710 "TOPIC notice." nil
1711 (let ((channel (second (erc-response.command-args parsed)))
1712 (topic (erc-response.contents parsed)))
1713 (erc-update-channel-topic channel topic)
1714 (erc-display-message parsed 'notice (erc-get-buffer channel proc)
1715 's332 ?c channel ?T topic)))
1716
1717 (define-erc-response-handler (333)
1718 ;; Who set the topic, and when
1719 nil nil
1720 (multiple-value-bind (channel nick time)
1721 (cdr (erc-response.command-args parsed))
1722 (setq time (format-time-string "%T %Y/%m/%d"
1723 (erc-string-to-emacs-time time)))
1724 (erc-update-channel-topic channel
1725 (format "\C-o (%s, %s)" nick time)
1726 'append)
1727 (erc-display-message parsed 'notice (erc-get-buffer channel proc)
1728 's333 ?c channel ?n nick ?t time)))
1729
1730 (define-erc-response-handler (341)
1731 "Let user know when an INVITE attempt has been sent successfully."
1732 nil
1733 (multiple-value-bind (nick channel)
1734 (cdr (erc-response.command-args parsed))
1735 (erc-display-message parsed 'notice (erc-get-buffer channel proc)
1736 's341 ?n nick ?c channel)))
1737
1738 (define-erc-response-handler (352)
1739 "WHO notice." nil
1740 (multiple-value-bind (channel user host server nick away-flag)
1741 (cdr (erc-response.command-args parsed))
1742 (let ((full-name (erc-response.contents parsed))
1743 hopcount)
1744 (when (string-match "\\(^[0-9]+ \\)\\(.*\\)$" full-name)
1745 (setq hopcount (match-string 1 full-name))
1746 (setq full-name (match-string 2 full-name)))
1747 (erc-update-channel-member channel nick nick nil nil nil host
1748 user full-name)
1749 (erc-display-message parsed 'notice 'active 's352
1750 ?c channel ?n nick ?a away-flag
1751 ?u user ?h host ?f full-name))))
1752
1753 (define-erc-response-handler (353)
1754 "NAMES notice." nil
1755 (let ((channel (third (erc-response.command-args parsed)))
1756 (users (erc-response.contents parsed)))
1757 (erc-display-message parsed 'notice (or (erc-get-buffer channel proc)
1758 'active)
1759 's353 ?c channel ?u users)
1760 (erc-with-buffer (channel proc)
1761 (erc-channel-receive-names users))))
1762
1763 (define-erc-response-handler (366)
1764 "End of NAMES." nil
1765 (erc-with-buffer ((second (erc-response.command-args parsed)) proc)
1766 (erc-channel-end-receiving-names)))
1767
1768 (define-erc-response-handler (367)
1769 "Channel ban list entries" nil
1770 (multiple-value-bind (channel banmask setter time)
1771 (cdr (erc-response.command-args parsed))
1772 ;; setter and time are not standard
1773 (if setter
1774 (erc-display-message parsed 'notice 'active 's367-set-by
1775 ?c channel
1776 ?b banmask
1777 ?s setter
1778 ?t (or time ""))
1779 (erc-display-message parsed 'notice 'active 's367
1780 ?c channel
1781 ?b banmask))))
1782
1783 (define-erc-response-handler (368)
1784 "End of channel ban list" nil
1785 (let ((channel (second (erc-response.command-args parsed))))
1786 (erc-display-message parsed 'notice 'active 's368
1787 ?c channel)))
1788
1789 (define-erc-response-handler (379)
1790 "Forwarding to another channel." nil
1791 ;; FIXME: Yet more magic numbers in original code, I'm guessing this
1792 ;; command takes two arguments, and doesn't have any "contents". --
1793 ;; Lawrence 2004/05/10
1794 (multiple-value-bind (from to)
1795 (cdr (erc-response.command-args parsed))
1796 (erc-display-message parsed 'notice 'active
1797 's379 ?c from ?f to)))
1798
1799 (define-erc-response-handler (391)
1800 "Server's time string" nil
1801 (erc-display-message
1802 parsed 'notice 'active
1803 's391 ?s (second (erc-response.command-args parsed))
1804 ?t (third (erc-response.command-args parsed))))
1805
1806 (define-erc-response-handler (401)
1807 "No such nick/channel." nil
1808 (let ((nick/channel (second (erc-response.command-args parsed))))
1809 (when erc-whowas-on-nosuchnick
1810 (erc-log (format "cmd: WHOWAS: %s" nick/channel))
1811 (erc-server-send (format "WHOWAS %s 1" nick/channel)))
1812 (erc-display-message parsed '(notice error) 'active
1813 's401 ?n nick/channel)))
1814
1815 (define-erc-response-handler (403)
1816 "No such channel." nil
1817 (erc-display-message parsed '(notice error) 'active
1818 's403 ?c (second (erc-response.command-args parsed))))
1819
1820 (define-erc-response-handler (404)
1821 "Cannot send to channel." nil
1822 (erc-display-message parsed '(notice error) 'active
1823 's404 ?c (second (erc-response.command-args parsed))))
1824
1825
1826 (define-erc-response-handler (405)
1827 ;; Can't join that many channels.
1828 nil nil
1829 (erc-display-message parsed '(notice error) 'active
1830 's405 ?c (second (erc-response.command-args parsed))))
1831
1832 (define-erc-response-handler (406)
1833 ;; No such nick
1834 nil nil
1835 (erc-display-message parsed '(notice error) 'active
1836 's406 ?n (second (erc-response.command-args parsed))))
1837
1838 (define-erc-response-handler (412)
1839 ;; No text to send
1840 nil nil
1841 (erc-display-message parsed '(notice error) 'active 's412))
1842
1843 (define-erc-response-handler (421)
1844 ;; Unknown command
1845 nil nil
1846 (erc-display-message parsed '(notice error) 'active 's421
1847 ?c (second (erc-response.command-args parsed))))
1848
1849 (define-erc-response-handler (432)
1850 ;; Bad nick.
1851 nil nil
1852 (erc-display-message parsed '(notice error) 'active 's432
1853 ?n (second (erc-response.command-args parsed))))
1854
1855 (define-erc-response-handler (433)
1856 ;; Login-time "nick in use"
1857 nil nil
1858 (erc-nickname-in-use (second (erc-response.command-args parsed))
1859 "already in use"))
1860
1861 (define-erc-response-handler (437)
1862 ;; Nick temporarily unavailable (IRCnet)
1863 nil nil
1864 (let ((nick/channel (second (erc-response.command-args parsed))))
1865 (unless (erc-channel-p nick/channel)
1866 (erc-nickname-in-use nick/channel "temporarily unavailable"))))
1867
1868 (define-erc-response-handler (442)
1869 ;; Not on channel
1870 nil nil
1871 (erc-display-message parsed '(notice error) 'active 's442
1872 ?c (second (erc-response.command-args parsed))))
1873
1874 (define-erc-response-handler (461)
1875 ;; Not enough params for command.
1876 nil nil
1877 (erc-display-message parsed '(notice error) 'active 's461
1878 ?c (second (erc-response.command-args parsed))
1879 ?m (erc-response.contents parsed)))
1880
1881 (define-erc-response-handler (465)
1882 "You are banned from this server." nil
1883 (setq erc-server-banned t)
1884 ;; show the server's message, as a reason might be provided
1885 (erc-display-error-notice
1886 parsed
1887 (erc-response.contents parsed)))
1888
1889 (define-erc-response-handler (474)
1890 "Banned from channel errors" nil
1891 (erc-display-message parsed '(notice error) nil
1892 (intern (format "s%s"
1893 (erc-response.command parsed)))
1894 ?c (second (erc-response.command-args parsed))))
1895
1896 (define-erc-response-handler (475)
1897 "Channel key needed." nil
1898 (erc-display-message parsed '(notice error) nil 's475
1899 ?c (second (erc-response.command-args parsed)))
1900 (when erc-prompt-for-channel-key
1901 (let ((channel (second (erc-response.command-args parsed)))
1902 (key (read-from-minibuffer
1903 (format "Channel %s is mode +k. Enter key (RET to cancel): "
1904 (second (erc-response.command-args parsed))))))
1905 (when (and key (> (length key) 0))
1906 (erc-cmd-JOIN channel key)))))
1907
1908 (define-erc-response-handler (477)
1909 nil nil
1910 (let ((channel (second (erc-response.command-args parsed)))
1911 (message (erc-response.contents parsed)))
1912 (erc-display-message parsed 'notice (erc-get-buffer channel proc)
1913 (format "%s: %s" channel message))))
1914
1915 (define-erc-response-handler (482)
1916 nil nil
1917 (let ((channel (second (erc-response.command-args parsed)))
1918 (message (erc-response.contents parsed)))
1919 (erc-display-message parsed '(error notice) 'active 's482
1920 ?c channel ?m message)))
1921
1922 (define-erc-response-handler (431 445 446 451 462 463 464 481 483 484 485
1923 491 501 502)
1924 ;; 431 - No nickname given
1925 ;; 445 - SUMMON has been disabled
1926 ;; 446 - USERS has been disabled
1927 ;; 451 - You have not registered
1928 ;; 462 - Unauthorized command (already registered)
1929 ;; 463 - Your host isn't among the privileged
1930 ;; 464 - Password incorrect
1931 ;; 481 - Need IRCop privileges
1932 ;; 483 - You can't kill a server!
1933 ;; 484 - Your connection is restricted!
1934 ;; 485 - You're not the original channel operator
1935 ;; 491 - No O-lines for your host
1936 ;; 501 - Unknown MODE flag
1937 ;; 502 - Cannot change mode for other users
1938 nil nil
1939 (erc-display-error-notice
1940 parsed
1941 (intern (format "s%s" (erc-response.command parsed)))))
1942
1943 ;; FIXME: These are yet to be implemented, they're just stubs for now
1944 ;; -- Lawrence 2004/05/12
1945
1946 ;; response numbers left here for reference
1947
1948 ;; (define-erc-response-handler (323 364 365 381 382 392 393 394 395
1949 ;; 200 201 202 203 204 205 206 208 209 211 212 213
1950 ;; 214 215 216 217 218 219 241 242 243 244 249 261
1951 ;; 262 302 342 351 402 407 409 411 413 414 415
1952 ;; 423 424 436 441 443 444 467 471 472 473 KILL)
1953 ;; nil nil
1954 ;; (ignore proc parsed))
1955
1956 (provide 'erc-backend)
1957
1958 ;;; erc-backend.el ends here
1959 ;; Local Variables:
1960 ;; indent-tabs-mode: nil
1961 ;; End:
1962
1963 ;; arch-tag: a64e6bb7-a780-4efd-8f98-083b18c7c84a