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