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