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