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