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