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