1 ;;; erc-backend.el --- Backend network communication for ERC
3 ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; Filename: erc-backend.el
6 ;; Author: Lawrence Mitchell <wence@gmx.li>
8 ;; Keywords: IRC chat client internet
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This file defines backend network communication handlers for ERC.
33 ;; You define a new handler with `define-erc-response-handler'. This
34 ;; defines a function, a corresponding hook variable, and populates a
35 ;; global hash table `erc-server-responses' with a map from response
36 ;; to hook variable. See the function documentation for more
39 ;; Upon receiving a line from the server, `erc-parse-server-response'
42 ;; A line generally looks like:
44 ;; LINE := ':' SENDER ' ' COMMAND ' ' (COMMAND-ARGS ' ')* ':' CONTENTS
45 ;; SENDER := Not ':' | ' '
46 ;; COMMAND := Not ':' | ' '
47 ;; COMMAND-ARGS := Not ':' | ' '
49 ;; This gets parsed and stuffed into an `erc-response' struct. You
50 ;; can access the fields of the struct with:
52 ;; COMMAND --- `erc-response.command'
53 ;; COMMAND-ARGS --- `erc-response.command-args'
54 ;; CONTENTS --- `erc-response.contents'
55 ;; SENDER --- `erc-response.sender'
56 ;; LINE --- `erc-response.unparsed'
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
67 ;; (while (erc-response.command-args parsed)
68 ;; (let ((a (pop (erc-response.command-args parsed))))
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'.
84 ;; o Generalise 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
94 ;; 2004/05/10 -- Handler bodies taken out of erc.el and ported to new
97 ;; 2005-08-13 -- Moved sending commands from erc.el.
101 (require 'erc-compat
)
102 (eval-when-compile (require 'cl
))
103 (autoload 'erc-with-buffer
"erc" nil nil
'macro
)
104 (autoload 'erc-log
"erc" nil nil
'macro
)
106 ;;;; Variables and options
108 (defvar erc-server-responses
(make-hash-table :test
#'equal
)
109 "Hashtable mapping server responses to their handler hooks.")
111 (defstruct (erc-response (:conc-name erc-response.
))
112 (unparsed "" :type string
)
113 (sender "" :type string
)
114 (command "" :type string
)
115 (command-args '() :type list
)
116 (contents "" :type string
))
120 (defvar erc-server-current-nick nil
121 "Nickname on the current server.
122 Use `erc-current-nick' to access this.")
123 (make-variable-buffer-local 'erc-server-current-nick
)
125 ;;; Server attributes
127 (defvar erc-server-process nil
128 "The process object of the corresponding server connection.")
129 (make-variable-buffer-local 'erc-server-process
)
131 (defvar erc-session-server nil
132 "The server name used to connect to for this session.")
133 (make-variable-buffer-local 'erc-session-server
)
135 (defvar erc-session-port nil
136 "The port used to connect to.")
137 (make-variable-buffer-local 'erc-session-port
)
139 (defvar erc-server-announced-name nil
140 "The name the server announced to use.")
141 (make-variable-buffer-local 'erc-server-announced-name
)
143 (defvar erc-server-version nil
144 "The name and version of the server's ircd.")
145 (make-variable-buffer-local 'erc-server-version
)
147 (defvar erc-server-parameters nil
148 "Alist listing the supported server parameters.
150 This is only set if the server sends 005 messages saying what is
151 supported on the server.
153 Entries are of the form:
156 (PARAMETER) if no value is provided.
158 Some examples of possible parameters sent by servers:
159 CHANMODES=b,k,l,imnpst - list of supported channel modes
160 CHANNELLEN=50 - maximum length of channel names
161 CHANTYPES=#&!+ - supported channel prefixes
162 CHARMAPPING=rfc1459 - character mapping used for nickname and channels
163 KICKLEN=160 - maximum allowed kick message length
164 MAXBANS=30 - maximum number of bans per channel
165 MAXCHANNELS=10 - maximum number of channels allowed to join
166 NETWORK=EFnet - the network identifier
167 NICKLEN=9 - maximum allowed length of nicknames
168 PREFIX=(ov)@+ - list of channel modes and the user prefixes if user has mode
169 RFC2812 - server supports RFC 2812 features
170 SILENCE=10 - supports the SILENCE command, maximum allowed number of entries
171 TOPICLEN=160 - maximum allowed topic length
172 WALLCHOPS - supports sending messages to all operators in a channel")
173 (make-variable-buffer-local 'erc-server-parameters
)
175 ;;; Server and connection state
177 (defvar erc-server-connected nil
178 "Non-nil if the `current-buffer' is associated with an open IRC connection.
179 This variable is buffer-local.")
180 (make-variable-buffer-local 'erc-server-connected
)
182 (defvar erc-server-reconnect-count
0
183 "Number of times we have failed to reconnect to the current server.")
184 (make-variable-buffer-local 'erc-server-reconnect-count
)
186 (defvar erc-server-quitting nil
187 "Non-nil if the user requests a quit.")
188 (make-variable-buffer-local 'erc-server-quitting
)
190 (defvar erc-server-banned nil
191 "Non-nil if the user is denied access because of a server ban.")
192 (make-variable-buffer-local 'erc-server-banned
)
194 (defvar erc-server-lines-sent nil
196 (make-variable-buffer-local 'erc-server-lines-sent
)
198 (defvar erc-server-last-peers
'(nil . nil
)
199 "Last peers used, both sender and receiver.
200 Those are used for /MSG destination shortcuts.")
201 (make-variable-buffer-local 'erc-server-last-peers
)
203 (defvar erc-server-last-sent-time nil
204 "Time the message was sent.
205 This is useful for flood protection.")
206 (make-variable-buffer-local 'erc-server-last-sent-time
)
208 (defvar erc-server-last-ping-time nil
209 "Time the last ping was sent.
210 This is useful for flood protection.")
211 (make-variable-buffer-local 'erc-server-last-ping-time
)
213 (defvar erc-server-lag nil
214 "Calculated server lag time in seconds.
215 This variable is only set in a server buffer.")
216 (make-variable-buffer-local 'erc-server-lag
)
218 (defvar erc-server-filter-data nil
219 "The data that arrived from the server
220 but has not been processed yet.")
221 (make-variable-buffer-local 'erc-server-filter-data
)
223 (defvar erc-server-duplicates
(make-hash-table :test
'equal
)
224 "Internal variable used to track duplicate messages.")
225 (make-variable-buffer-local 'erc-server-duplicates
)
228 (defvar erc-server-processing-p nil
229 "Non-nil when we're currently processing a message.
231 When ERC receives a private message, it sets up a new buffer for
232 this query. These in turn, though, do start flyspell. This
233 involves starting an external process, in which case Emacs will
234 wait - and when it waits, it does accept other stuff from, say,
235 network exceptions. So, if someone sends you two messages
236 quickly after each other, ispell is started for the first, but
237 might take long enough for the second message to be processed
239 (make-variable-buffer-local 'erc-server-processing-p
)
241 (defvar erc-server-flood-last-message
0
242 "When we sent the last message.
243 See `erc-server-flood-margin' for an explanation of the flood
244 protection algorithm.")
245 (make-variable-buffer-local 'erc-server-flood-last-message
)
247 (defvar erc-server-flood-queue nil
248 "The queue of messages waiting to be sent to the server.
249 See `erc-server-flood-margin' for an explanation of the flood
250 protection algorithm.")
251 (make-variable-buffer-local 'erc-server-flood-queue
)
253 (defvar erc-server-flood-timer nil
254 "The timer to resume sending.")
255 (make-variable-buffer-local 'erc-server-flood-timer
)
257 ;;; IRC protocol and misc options
259 (defgroup erc-server nil
260 "Parameters for dealing with IRC servers."
263 (defcustom erc-server-auto-reconnect t
264 "Non-nil means that ERC will attempt to reestablish broken connections.
266 Reconnection will happen automatically for any unexpected disconnection."
270 (defcustom erc-server-reconnect-attempts
2
271 "The number of times that ERC will attempt to reestablish a
272 broken connection, or t to always attempt to reconnect.
274 This only has an effect if `erc-server-auto-reconnect' is non-nil."
276 :type
'(choice (const :tag
"Always reconnect" t
)
279 (defcustom erc-server-reconnect-timeout
1
280 "The amount of time, in seconds, that ERC will wait between
281 successive reconnect attempts.
283 If a key is pressed while ERC is waiting, it will stop waiting."
287 (defcustom erc-split-line-length
440
288 "*The maximum length of a single message.
289 If a message exceeds this size, it is broken into multiple ones.
291 IRC allows for lines up to 512 bytes. Two of them are CR LF.
292 And a typical message looks like this:
294 :nicky!uhuser@host212223.dialin.fnordisp.net PRIVMSG #lazybastards :Hello!
296 You can limit here the maximum length of the \"Hello!\" part.
301 (defcustom erc-server-coding-system
(if (and (fboundp 'coding-system-p
)
302 (coding-system-p 'undecided
)
303 (coding-system-p 'utf-8
))
306 "The default coding system for incoming and outgoing text.
307 This is either a coding system, a cons, a function, or nil.
309 If a cons, the encoding system for outgoing text is in the car
310 and the decoding system for incoming text is in the cdr. The most
311 interesting use for this is to put `undecided' in the cdr. If a
312 function, it is called with no arguments and should return a
313 coding system or a cons as described above. Note that you can use
314 the dynamically bound variable `target' to get the current
315 target. See `erc-coding-system-for-target'.
317 If you need to send non-ASCII text to people not using a client that
318 does decoding on its own, you must tell ERC what encoding to use.
319 Emacs cannot guess it, since it does not know what the people on the
320 other end of the line are using."
322 :type
'(choice (const :tag
"None" nil
)
324 (cons (coding-system :tag
"encoding" :value utf-8
)
325 (coding-system :tag
"decoding" :value undecided
))
328 (defcustom erc-encoding-coding-alist nil
329 "Alist of target regexp and coding-system pairs to use.
330 This overrides `erc-server-coding-system' depending on the
331 current target as returned by `erc-default-target'.
333 Example: If you know that the channel #linux-ru uses the coding-system
334 `cyrillic-koi8', then add '(\"#linux-ru\" . cyrillic-koi8) to the
337 :type
'(repeat (cons (string :tag
"Target")
340 (defcustom erc-server-connect-function
'open-network-stream
341 "Function used to initiate a connection.
342 It should take same arguments as `open-network-stream' does."
346 (defcustom erc-server-prevent-duplicates
'("301")
347 "*Either nil or a list of strings.
348 Each string is a IRC message type, like PRIVMSG or NOTICE.
349 All Message types in that list of subjected to duplicate prevention."
350 :type
'(choice (const nil
) (list string
))
353 (defcustom erc-server-duplicate-timeout
60
354 "*The time allowed in seconds between duplicate messages.
356 If two identical messages arrive within this value of one another, the second
363 ;; Most of this is courtesy of Jorgen Schaefer and Circe
364 ;; (http://www.nongnu.org/circe)
366 (defcustom erc-server-flood-margin
10
367 "*A margin on how much excess data we send.
368 The flood protection algorithm of ERC works like the one
369 detailed in RFC 2813, section 5.8 \"Flood control of clients\".
371 * If `erc-server-flood-last-message' is less than the current
373 * While `erc-server-flood-last-message' is less than
374 `erc-server-flood-margin' seconds ahead of the current
375 time, send a message, and increase
376 `erc-server-flood-last-message' by
377 `erc-server-flood-penalty' for each message."
381 (defcustom erc-server-flood-penalty
3
382 "How much we penalize a message.
383 See `erc-server-flood-margin' for an explanation of the flood
384 protection algorithm."
390 (defcustom erc-server-send-ping-interval
90
391 "*Interval of sending pings to the server, in seconds.
392 If this is set to nil, pinging the server is disabled."
394 :type
'(choice (const nil
) (integer :tag
"Seconds")))
396 (defvar erc-server-ping-handler nil
397 "This variable holds the periodic ping timer.")
398 (make-variable-buffer-local 'erc-server-ping-handler
)
400 ;;;; Helper functions
403 (defun erc-split-line (longline)
404 "Return a list of lines which are not too long for IRC.
405 The length is specified in `erc-split-line-length'.
407 Currently this is called by `erc-send-input'."
408 (if (< (length longline
)
409 erc-split-line-length
)
413 (let ((fill-column erc-split-line-length
))
414 (fill-region (point-min) (point-max)
416 (split-string (buffer-string) "\n"))))
418 ;; Used by CTCP functions
419 (defun erc-upcase-first-word (str)
420 "Upcase the first word in STR."
423 (goto-char (point-min))
427 (defun erc-server-setup-periodical-server-ping (&rest ignore
)
428 "Set up a timer to periodically ping the current server."
429 (and erc-server-ping-handler
(erc-cancel-timer erc-server-ping-handler
))
430 (when erc-server-send-ping-interval
431 (setq erc-server-ping-handler
433 4 erc-server-send-ping-interval
435 (when (buffer-live-p buf
)
436 (with-current-buffer buf
439 (erc-current-time))))))
442 (defun erc-server-process-alive ()
443 "Return non-nil when `erc-server-process' is open or running."
444 (and erc-server-process
445 (processp erc-server-process
)
446 (memq (process-status erc-server-process
) '(run open
))))
448 ;;;; Connecting to a server
450 (defun erc-server-connect (server port
)
451 "Perform the connection and login.
452 We will store server variables in the current buffer."
453 (let ((msg (erc-format-message 'connect ?S server ?p port
)))
455 (setq erc-server-process
456 (funcall erc-server-connect-function
457 (format "erc-%s-%s" server port
)
458 (current-buffer) server port
))
459 (message "%s...done" msg
))
460 ;; Misc server variables
461 (setq erc-server-quitting nil
)
462 (setq erc-server-banned nil
)
463 (setq erc-server-last-sent-time
(erc-current-time))
464 (setq erc-server-last-ping-time
(erc-current-time))
465 (setq erc-server-lines-sent
0)
466 ;; last peers (sender and receiver)
467 (setq erc-server-last-peers
'(nil . nil
))
469 (set-process-sentinel erc-server-process
'erc-process-sentinel
)
470 (set-process-filter erc-server-process
'erc-server-filter-function
)
471 ;; we do our own encoding and decoding
472 (when (fboundp 'set-process-coding-system
)
473 (set-process-coding-system erc-server-process
'raw-text
))
474 (set-marker (process-mark erc-server-process
) (point))
475 (erc-log "\n\n\n********************************************\n")
476 (message (erc-format-message 'login ?n
(erc-current-nick)))
477 ;; wait with script loading until we receive a confirmation (first
479 (if (eq erc-server-connect-function
'open-network-stream-nowait
)
480 ;; it's a bit unclear otherwise that it's attempting to establish a
482 (erc-display-message nil nil
(current-buffer)
483 "Opening connection..\n")
486 (defun erc-server-reconnect ()
487 "Reestablish the current IRC connection.
488 Make sure you are in an ERC buffer when running this."
489 (let ((server (erc-server-buffer)))
491 (buffer-live-p server
))
492 (error "Couldn't switch to server buffer"))
493 (with-current-buffer server
494 (erc-update-mode-line)
495 (erc-set-active-buffer (current-buffer))
496 (setq erc-server-last-sent-time
0)
497 (setq erc-server-lines-sent
0)
498 (erc-open erc-session-server erc-session-port erc-server-current-nick
499 erc-session-user-full-name t erc-session-password
))))
501 (defun erc-server-filter-function (process string
)
502 "The process filter for the ERC server."
503 (with-current-buffer (process-buffer process
)
504 ;; If you think this is written in a weird way - please refer to the
505 ;; docstring of `erc-server-processing-p'
506 (if erc-server-processing-p
507 (setq erc-server-filter-data
508 (if erc-server-filter-data
509 (concat erc-server-filter-data string
)
511 ;; This will be true even if another process is spawned!
512 (let ((erc-server-processing-p t
))
513 (setq erc-server-filter-data
(if erc-server-filter-data
514 (concat erc-server-filter-data
517 (while (and erc-server-filter-data
518 (string-match "[\n\r]+" erc-server-filter-data
))
519 (let ((line (substring erc-server-filter-data
520 0 (match-beginning 0))))
521 (setq erc-server-filter-data
523 (length erc-server-filter-data
))
525 (substring erc-server-filter-data
527 (erc-parse-server-response process line
)))))))
529 (defsubst erc-server-reconnect-p
(event)
530 "Return non-nil if ERC should attempt to reconnect automatically.
531 EVENT is the message received from the closed connection process."
532 (and erc-server-auto-reconnect
533 (not erc-server-banned
)
534 ;; make sure we don't infinitely try to reconnect, unless the
536 (or (eq erc-server-reconnect-attempts t
)
537 (and (integerp erc-server-reconnect-attempts
)
538 (< erc-server-reconnect-count erc-server-reconnect-attempts
)))
539 (not (string-match "^deleted" event
))
540 ;; open-network-stream-nowait error for connection refused
541 (not (string-match "^failed with code 111" event
))))
543 (defun erc-process-sentinel-1 (event)
544 "Called when `erc-process-sentinel' has decided that we're disconnecting.
545 Determine whether user has quit or whether erc has been terminated.
546 Conditionally try to reconnect and take appropriate action."
547 (if erc-server-quitting
550 (erc-display-message nil
'error
(current-buffer) 'finished
)
551 (when erc-kill-server-buffer-on-quit
552 (set-buffer-modified-p nil
)
553 (kill-buffer (current-buffer))))
554 ;; unexpected disconnect
558 (erc-display-message nil
'error
(current-buffer)
559 (if (erc-server-reconnect-p event
)
561 'disconnected-noreconnect
))
562 (if (erc-server-reconnect-p event
)
565 (erc-server-reconnect)
566 (setq erc-server-reconnect-count
0))
567 (error (when (integerp erc-server-reconnect-attempts
)
568 (setq erc-server-reconnect-count
569 (1+ erc-server-reconnect-count
))
570 (sit-for erc-server-reconnect-timeout
)
572 ;; terminate, do not reconnect
573 (erc-display-message nil
'error
(current-buffer)
574 'terminated ?e event
))))))
576 (defun erc-process-sentinel (cproc event
)
577 "Sentinel function for ERC process."
578 (with-current-buffer (process-buffer cproc
)
580 "SENTINEL: proc: %S status: %S event: %S (quitting: %S)"
581 cproc
(process-status cproc
) event erc-server-quitting
))
582 (if (string-match "^open" event
)
583 ;; newly opened connection (no wait)
585 ;; assume event is 'failed
586 (let ((buf (process-buffer cproc
)))
587 (erc-with-all-buffers-of-server cproc nil
588 (setq erc-server-connected nil
))
589 (when erc-server-ping-handler
590 (progn (erc-cancel-timer erc-server-ping-handler
)
591 (setq erc-server-ping-handler nil
)))
592 (run-hook-with-args 'erc-disconnected-hook
593 (erc-current-nick) (system-name) "")
595 (goto-char (or (marker-position erc-input-marker
) (point-max)))
597 (erc-remove-text-properties-region (point) (point-max))
598 (delete-region (point) (point-max))
599 ;; Decide what to do with the buffer
600 ;; Restart if disconnected
601 (erc-process-sentinel-1 event
)
602 ;; Make sure we don't write to the buffer if it has been
604 (when (buffer-live-p buf
)
605 (erc-update-mode-line)
606 (set-buffer-modified-p nil
))))))
608 ;;;; Sending messages
610 (defun erc-coding-system-for-target (target)
611 "Return the coding system or cons cell appropriate for TARGET.
612 This is determined via `erc-encoding-coding-alist' or
613 `erc-server-coding-system'."
615 (let ((case-fold-search t
))
617 (dolist (pat erc-encoding-coding-alist
)
618 (when (string-match (car pat
) target
)
619 (throw 'match
(cdr pat
)))))))
620 (and (functionp erc-server-coding-system
)
621 (funcall erc-server-coding-system
))
622 erc-server-coding-system
))
624 (defun erc-decode-string-from-target (str target
)
625 "Decode STR as appropriate for TARGET.
626 This is indicated by `erc-encoding-coding-alist', defaulting to the value of
627 `erc-server-coding-system'."
628 (unless (stringp str
)
630 (let ((coding (erc-coding-system-for-target target
)))
632 (setq coding
(cdr coding
)))
633 (erc-decode-coding-string str coding
)))
635 ;; proposed name, not used by anything yet
636 (defun erc-send-line (text display-fn
)
637 "Send TEXT to the current server. Wrapping and flood control apply.
638 Use DISPLAY-FN to show the results."
640 (erc-server-send line
)
641 (funcall display-fn
))
642 (erc-split-line text
)))
644 ;; From Circe, with modifications
645 (defun erc-server-send (string &optional forcep target
)
646 "Send STRING to the current server.
647 If FORCEP is non-nil, no flood protection is done - the string is
648 sent directly. This might cause the messages to arrive in a wrong
651 If TARGET is specified, look up encoding information for that
652 channel in `erc-encoding-coding-alist' or
653 `erc-server-coding-system'.
655 See `erc-server-flood-margin' for an explanation of the flood
656 protection algorithm."
657 (erc-log (concat "erc-server-send: " string
"(" (buffer-name) ")"))
658 (setq erc-server-last-sent-time
(erc-current-time))
659 (let ((buf (erc-server-buffer))
660 (encoding (erc-coding-system-for-target
661 (or target
(erc-default-target)))))
662 (when (consp encoding
)
663 (setq encoding
(car encoding
)))
665 (erc-server-process-alive))
666 (with-current-buffer buf
667 (let ((str (concat string
"\r\n")))
670 (setq erc-server-flood-last-message
671 (+ erc-server-flood-penalty
672 erc-server-flood-last-message
))
673 (erc-log-irc-protocol str
'outbound
)
676 ;; Set encoding just before sending the string
677 (when (fboundp 'set-process-coding-system
)
678 (set-process-coding-system erc-server-process
680 (process-send-string erc-server-process str
))
681 ;; See `erc-server-send-queue' for full
682 ;; explanation of why we need this condition-case
684 (setq erc-server-flood-queue
685 (append erc-server-flood-queue
686 (list (cons str encoding
))))
687 (erc-server-send-queue (current-buffer))))
689 (message "ERC: No process running")
693 (defun erc-server-send-queue (buffer)
694 "Send messages in `erc-server-flood-queue'.
695 See `erc-server-flood-margin' for an explanation of the flood
696 protection algorithm."
697 (with-current-buffer buffer
698 (let ((now (erc-current-time)))
699 (when erc-server-flood-timer
700 (erc-cancel-timer erc-server-flood-timer
)
701 (setq erc-server-flood-timer nil
))
702 (when (< erc-server-flood-last-message
704 (setq erc-server-flood-last-message now
))
705 (while (and erc-server-flood-queue
706 (< erc-server-flood-last-message
707 (+ now erc-server-flood-margin
)))
708 (let ((msg (caar erc-server-flood-queue
))
709 (encoding (cdar erc-server-flood-queue
)))
710 (setq erc-server-flood-queue
(cdr erc-server-flood-queue
)
711 erc-server-flood-last-message
712 (+ erc-server-flood-last-message
713 erc-server-flood-penalty
))
714 (erc-log-irc-protocol msg
'outbound
)
715 (erc-log (concat "erc-server-send-queue: "
716 msg
"(" (buffer-name buffer
) ")"))
717 (when (erc-server-process-alive)
719 ;; Set encoding just before sending the string
721 (when (fboundp 'set-process-coding-system
)
722 (set-process-coding-system erc-server-process
724 (process-send-string erc-server-process msg
))
725 ;; Sometimes the send can occur while the process is
726 ;; being killed, which results in a weird SIGPIPE error.
727 ;; Catch this and ignore it.
729 (when erc-server-flood-queue
730 (setq erc-server-flood-timer
731 (run-at-time (+ 0.2 erc-server-flood-penalty
)
732 nil
#'erc-server-send-queue buffer
))))))
734 (defun erc-message (message-command line
&optional force
)
735 "Send LINE to the server as a privmsg or a notice.
736 MESSAGE-COMMAND should be either \"PRIVMSG\" or \"NOTICE\".
737 If the target is \",\", the last person you've got a message from will
738 be used. If the target is \".\", the last person you've sent a message
741 ((string-match "^\\s-*\\(\\S-+\\) ?\\(.*\\)" line
)
742 (let ((tgt (match-string 1 line
))
743 (s (match-string 2 line
)))
744 (erc-log (format "cmd: MSG(%s): [%s] %s" message-command tgt s
))
747 (if (car erc-server-last-peers
)
748 (setq tgt
(car erc-server-last-peers
))
751 (if (cdr erc-server-last-peers
)
752 (setq tgt
(cdr erc-server-last-peers
))
756 (setcdr erc-server-last-peers tgt
)
757 (erc-server-send (format "%s %s :%s" message-command tgt s
)
760 (erc-display-message nil
'error
(current-buffer) 'no-target
))))
766 (defun erc-send-ctcp-message (tgt l
&optional force
)
767 "Send CTCP message L to TGT.
769 If TGT is nil the message is not sent.
770 The command must contain neither a prefix nor a trailing `\\n'.
772 See also `erc-server-send'."
773 (let ((l (erc-upcase-first-word l
)))
776 (erc-log (format "erc-send-CTCP-message: [%s] %s" tgt l
))
777 (erc-server-send (format "PRIVMSG %s :\C-a%s\C-a" tgt l
)
780 (defun erc-send-ctcp-notice (tgt l
&optional force
)
781 "Send CTCP notice L to TGT.
783 If TGT is nil the message is not sent.
784 The command must contain neither a prefix nor a trailing `\\n'.
786 See also `erc-server-send'."
787 (let ((l (erc-upcase-first-word l
)))
790 (erc-log (format "erc-send-CTCP-notice: [%s] %s" tgt l
))
791 (erc-server-send (format "NOTICE %s :\C-a%s\C-a" tgt l
)
794 ;;;; Handling responses
796 (defun erc-parse-server-response (proc string
)
797 "Parse and act upon a complete line from an IRC server.
798 PROC is the process (connection) from which STRING was received.
799 PROCs `process-buffer' is `current-buffer' when this function is called."
800 (unless (string= string
"") ;; Ignore empty strings
802 (let ((posn (if (eq (aref string
0) ?
:)
803 (string-match " " string
)
805 (msg (make-erc-response :unparsed string
)))
807 (setf (erc-response.sender msg
)
810 (substring string
1 posn
)))
812 (setf (erc-response.command msg
)
813 (let* ((bposn (string-match "[^ \n]" string posn
))
814 (eposn (string-match " " string bposn
)))
815 (setq posn
(and eposn
816 (string-match "[^ \n]" string eposn
)))
817 (substring string bposn eposn
)))
820 (not (eq (aref string posn
) ?
:)))
821 (push (let* ((bposn posn
)
822 (eposn (string-match " " string bposn
)))
823 (setq posn
(and eposn
824 (string-match "[^ \n]" string eposn
)))
825 (substring string bposn eposn
))
826 (erc-response.command-args msg
)))
828 (let ((str (substring string
(1+ posn
))))
829 (push str
(erc-response.command-args msg
))))
831 (setf (erc-response.contents msg
)
832 (first (erc-response.command-args msg
)))
834 (setf (erc-response.command-args msg
)
835 (nreverse (erc-response.command-args msg
)))
837 (erc-decode-parsed-server-response msg
)
839 (erc-handle-parsed-server-response proc msg
)))))
841 (defun erc-decode-parsed-server-response (parsed-response)
842 "Decode a pre-parsed PARSED-RESPONSE before it can be handled.
844 If there is a channel name in `erc-response.command-args', decode
845 `erc-response' according to this channel name and
846 `erc-encoding-coding-alist', or use `erc-server-coding-system'
848 (let ((args (erc-response.command-args parsed-response
))
851 (dolist (arg args nil
)
852 (when (string-match "^[#&].*" arg
)
853 (setq decode-target arg
)))
854 (when (stringp decode-target
)
855 (setq decode-target
(erc-decode-string-from-target decode-target nil
)))
856 (setf (erc-response.unparsed parsed-response
)
857 (erc-decode-string-from-target
858 (erc-response.unparsed parsed-response
)
860 (setf (erc-response.sender parsed-response
)
861 (erc-decode-string-from-target
862 (erc-response.sender parsed-response
)
864 (setf (erc-response.command parsed-response
)
865 (erc-decode-string-from-target
866 (erc-response.command parsed-response
)
868 (dolist (arg (nreverse args
) nil
)
869 (push (erc-decode-string-from-target arg decode-target
)
871 (setf (erc-response.command-args parsed-response
) decoded-args
)
872 (setf (erc-response.contents parsed-response
)
873 (erc-decode-string-from-target
874 (erc-response.contents parsed-response
)
877 (defun erc-handle-parsed-server-response (process parsed-response
)
878 "Handle a pre-parsed PARSED-RESPONSE from PROCESS.
880 Hands off to helper functions via `erc-call-hooks'."
881 (if (member (erc-response.command parsed-response
)
882 erc-server-prevent-duplicates
)
883 (let ((m (erc-response.unparsed parsed-response
)))
884 ;; duplicate supression
885 (if (< (or (gethash m erc-server-duplicates
) 0)
886 (- (erc-current-time) erc-server-duplicate-timeout
))
887 (erc-call-hooks process parsed-response
))
888 (puthash m
(erc-current-time) erc-server-duplicates
))
889 ;; Hand off to the relevant handler.
890 (erc-call-hooks process parsed-response
)))
892 (defun erc-get-hook (command)
893 "Return the hook variable associated with COMMAND.
895 See also `erc-server-responses'."
896 (gethash (format (if (numberp command
) "%03i" "%s") command
)
897 erc-server-responses
))
899 (defun erc-call-hooks (process message
)
900 "Call hooks associated with MESSAGE in PROCESS.
902 Finds hooks by looking in the `erc-server-responses' hashtable."
903 (let ((hook (or (erc-get-hook (erc-response.command message
))
904 'erc-default-server-functions
)))
905 (run-hook-with-args-until-success hook process message
)
906 (let ((server-buffer (erc-server-buffer)))
907 (when (buffer-live-p server-buffer
)
908 (with-current-buffer server-buffer
909 (run-hook-with-args 'erc-timer-hook
(erc-current-time)))))))
911 (add-hook 'erc-default-server-functions
'erc-handle-unknown-server-response
)
913 (defun erc-handle-unknown-server-response (proc parsed
)
914 "Display unknown server response's message."
915 (let ((line (concat (erc-response.sender parsed
)
917 (erc-response.command parsed
)
919 (mapconcat 'identity
(erc-response.command-args parsed
)
921 (erc-display-message parsed
'notice proc line
)))
924 (put 'define-erc-response-handler
'edebug-form-spec
925 '(&define
:name erc-response-handler
927 &optional sexp sexp def-body
))
929 (defmacro* define-erc-response-handler
((name &rest aliases
)
930 &optional extra-fn-doc extra-var-doc
932 "Define an ERC handler hook/function pair.
933 NAME is the response name as sent by the server (see the IRC RFC for
937 - a hook variable `erc-server-NAME-functions' initialised to `erc-server-NAME'.
938 - a function `erc-server-NAME' with body FN-BODY.
940 If ALIASES is non-nil, each alias in ALIASES is `defalias'ed to
942 Alias hook variables are created as `erc-server-ALIAS-functions' and
943 initialised to the same default value as `erc-server-NAME-functions'.
945 FN-BODY is the body of `erc-server-NAME' it may refer to the two
946 function arguments PROC and PARSED.
948 If EXTRA-FN-DOC is non-nil, it is inserted at the beginning of the
949 defined function's docstring.
951 If EXTRA-VAR-DOC is non-nil, it is inserted at the beginning of the
952 defined variable's docstring.
956 (define-erc-response-handler (311 WHOIS WI)
957 \"Some non-generic function documentation.\"
958 \"Some non-generic variable documentation.\"
959 (do-stuff-with-whois proc parsed))
964 (defvar erc-server-311-functions 'erc-server-311
965 \"Some non-generic variable documentation.
967 Hook called upon receiving a 311 server response.
968 Each function is called with two arguments, the process associated
969 with the response and the parsed response.
970 See also `erc-server-311'.\")
972 (defun erc-server-311 (proc parsed)
973 \"Some non-generic function documentation.
975 Handler for a 311 server response.
976 PROC is the server process which returned the response.
977 PARSED is the actual response as an `erc-response' struct.
978 If you want to add responses don't modify this function, but rather
979 add things to `erc-server-311-functions' instead.\"
980 (do-stuff-with-whois proc parsed))
982 (puthash \"311\" 'erc-server-311-functions erc-server-responses)
983 (puthash \"WHOIS\" 'erc-server-WHOIS-functions erc-server-responses)
984 (puthash \"WI\" 'erc-server-WI-functions erc-server-responses)
986 (defalias 'erc-server-WHOIS 'erc-server-311)
987 (defvar erc-server-WHOIS-functions 'erc-server-311
988 \"Some non-generic variable documentation.
990 Hook called upon receiving a WHOIS server response.
991 Each function is called with two arguments, the process associated
992 with the response and the parsed response.
993 See also `erc-server-311'.\")
995 (defalias 'erc-server-WI 'erc-server-311)
996 (defvar erc-server-WI-functions 'erc-server-311
997 \"Some non-generic variable documentation.
999 Hook called upon receiving a WI server response.
1000 Each function is called with two arguments, the process associated
1001 with the response and the parsed response.
1002 See also `erc-server-311'.\"))
1004 \(fn (NAME &rest ALIASES) &optional EXTRA-FN-DOC EXTRA-VAR-DOC &rest FN-BODY)"
1005 (if (numberp name
) (setq name
(intern (format "%03i" name
))))
1006 (setq aliases
(mapcar (lambda (a)
1011 (let* ((hook-name (intern (format "erc-server-%s-functions" name
)))
1012 (fn-name (intern (format "erc-server-%s" name
)))
1013 (hook-doc (format "%sHook called upon receiving a %%s server response.
1014 Each function is called with two arguments, the process associated
1015 with the response and the parsed response.
1018 (concat extra-var-doc
"\n\n")
1021 (fn-doc (format "%sHandler for a %s server response.
1022 PROC is the server process which returned the response.
1023 PARSED is the actual response as an `erc-response' struct.
1024 If you want to add responses don't modify this function, but rather
1025 add things to `%s' instead."
1027 (concat extra-fn-doc
"\n\n")
1031 (loop for alias in aliases
1032 collect
(intern (format "erc-server-%s" alias
))))
1034 (loop for alias in aliases
1035 collect
(intern (format "erc-server-%s-functions" alias
)))))
1037 ;; Normal hook variable.
1038 (defvar ,hook-name
',fn-name
,(format hook-doc name
))
1040 (defun ,fn-name
(proc parsed
)
1044 ;; Make find-function and find-variable find them
1045 (put ',fn-name
'definition-name
',name
)
1046 (put ',hook-name
'definition-name
',name
)
1048 ;; Hashtable map of responses to hook variables
1049 ,@(loop for response in
(cons name aliases
)
1050 for var in
(cons hook-name var-alternates
)
1051 collect
`(puthash ,(format "%s" response
) ',var
1052 erc-server-responses
))
1054 ;; Functions are defaliased, hook variables are defvared so we
1055 ;; can add hooks to one alias, but not another.
1056 ,@(loop for fn in fn-alternates
1057 for var in var-alternates
1059 nconc
(list `(defalias ',fn
',fn-name
)
1060 `(defvar ,var
',fn-name
,(format hook-doc a
))
1061 `(put ',var
'definition-name
',hook-name
))))))
1063 (define-erc-response-handler (ERROR)
1064 "Handle an ERROR command from the server." nil
1065 (erc-display-message
1066 parsed
'error nil
'ERROR
1067 ?s
(erc-response.sender parsed
) ?c
(erc-response.contents parsed
)))
1069 (define-erc-response-handler (INVITE)
1070 "Handle invitation messages."
1072 (let ((target (first (erc-response.command-args parsed
)))
1073 (chnl (erc-response.contents parsed
)))
1074 (multiple-value-bind (nick login host
)
1075 (erc-parse-user (erc-response.sender parsed
))
1076 (setq erc-invitation chnl
)
1077 (when (string= target
(erc-current-nick))
1078 (erc-display-message
1079 parsed
'notice
'active
1080 'INVITE ?n nick ?u login ?h host ?c chnl
)))))
1083 (define-erc-response-handler (JOIN)
1084 "Handle join messages."
1086 (let ((chnl (erc-response.contents parsed
))
1088 (multiple-value-bind (nick login host
)
1089 (erc-parse-user (erc-response.sender parsed
))
1090 ;; strip the stupid combined JOIN facility (IRC 2.9)
1091 (if (string-match "^\\(.*\\)?\^g.*$" chnl
)
1092 (setq chnl
(match-string 1 chnl
)))
1095 ;; If I have joined a channel
1096 ((erc-current-nick-p nick
)
1097 (setq buffer
(erc-open erc-session-server erc-session-port
1098 nick erc-session-user-full-name
1100 erc-default-recipients chnl
1101 erc-server-process
))
1104 (erc-add-default-channel chnl
)
1105 (erc-server-send (format "MODE %s" chnl
)))
1106 (erc-with-buffer (chnl proc
)
1107 (erc-channel-begin-receiving-names))
1108 (erc-update-mode-line)
1109 (run-hooks 'erc-join-hook
)
1111 (erc-format-message 'JOIN-you ?c chnl
)))
1113 (setq buffer
(erc-get-buffer chnl proc
))
1116 'JOIN ?n nick ?u login ?h host ?c chnl
))))))
1117 (when buffer
(set-buffer buffer
))
1118 (erc-update-channel-member chnl nick nick t nil nil host login
)
1119 ;; on join, we want to stay in the new channel buffer
1121 (erc-display-message parsed nil buffer str
))))))
1123 (define-erc-response-handler (KICK)
1124 "Handle kick messages received from the server." nil
1125 (let* ((ch (first (erc-response.command-args parsed
)))
1126 (tgt (second (erc-response.command-args parsed
)))
1127 (reason (erc-trim-string (erc-response.contents parsed
)))
1128 (buffer (erc-get-buffer ch proc
)))
1129 (multiple-value-bind (nick login host
)
1130 (erc-parse-user (erc-response.sender parsed
))
1131 (erc-remove-channel-member buffer tgt
)
1133 ((string= tgt
(erc-current-nick))
1134 (erc-display-message
1135 parsed
'notice buffer
1136 'KICK-you ?n nick ?u login ?h host ?c ch ?r reason
)
1137 (run-hook-with-args 'erc-kick-hook buffer
)
1140 (erc-remove-channel-users))
1141 (erc-delete-default-channel ch buffer
)
1142 (erc-update-mode-line buffer
))
1143 ((string= nick
(erc-current-nick))
1144 (erc-display-message
1145 parsed
'notice buffer
1146 'KICK-by-you ?k tgt ?c ch ?r reason
))
1147 (t (erc-display-message
1148 parsed
'notice buffer
1149 'KICK ?k tgt ?n nick ?u login ?h host ?c ch ?r reason
))))))
1151 (define-erc-response-handler (MODE)
1152 "Handle server mode changes." nil
1153 (let ((tgt (first (erc-response.command-args parsed
)))
1154 (mode (mapconcat 'identity
(cdr (erc-response.command-args parsed
))
1156 (multiple-value-bind (nick login host
)
1157 (erc-parse-user (erc-response.sender parsed
))
1158 (erc-log (format "MODE: %s -> %s: %s" nick tgt mode
))
1160 (let ((buf (cond ((erc-channel-p tgt
)
1161 (erc-get-buffer tgt proc
))
1162 ((string= tgt
(erc-current-nick)) nil
)
1163 ((erc-active-buffer) (erc-active-buffer))
1164 (t (erc-get-buffer tgt
)))))
1165 (with-current-buffer (or buf
1167 (erc-update-modes tgt mode nick host login
))
1168 (if (or (string= login
"") (string= host
""))
1169 (erc-display-message parsed
'notice buf
1172 (erc-display-message parsed
'notice buf
1173 'MODE ?n nick ?u login
1174 ?h host ?t tgt ?m mode
)))
1175 (erc-banlist-update proc parsed
))))
1177 (define-erc-response-handler (NICK)
1178 "Handle nick change messages." nil
1179 (let ((nn (erc-response.contents parsed
))
1181 (multiple-value-bind (nick login host
)
1182 (erc-parse-user (erc-response.sender parsed
))
1183 (setq bufs
(erc-buffer-list-with-nick nick proc
))
1184 (erc-log (format "NICK: %s -> %s" nick nn
))
1185 ;; if we had a query with this user, make sure future messages will be
1186 ;; sent to the correct nick. also add to bufs, since the user will want
1187 ;; to see the nick change in the query, and if it's a newly begun query,
1188 ;; erc-channel-users won't contain it
1191 (when (equal (erc-default-target) nick
)
1192 (setq erc-default-recipients
1193 (cons nn
(cdr erc-default-recipients
)))
1195 (erc-update-mode-line)
1196 (add-to-list 'bufs
(current-buffer)))))
1197 (erc-update-user-nick nick nn host nil nil login
)
1199 ((string= nick
(erc-current-nick))
1200 (add-to-list 'bufs
(erc-server-buffer))
1201 (erc-set-current-nick nn
)
1202 (erc-update-mode-line)
1203 (setq erc-nick-change-attempt-count
0)
1204 (setq erc-default-nicks
(if (consp erc-nick
) erc-nick
(list erc-nick
)))
1205 (erc-display-message
1207 'NICK-you ?n nick ?N nn
)
1208 (run-hook-with-args 'erc-nick-changed-functions nn nick
))
1210 (erc-handle-user-status-change 'nick
(list nick login host
) (list nn
))
1211 (erc-display-message parsed
'notice bufs
'NICK ?n nick
1212 ?u login ?h host ?N nn
))))))
1214 (define-erc-response-handler (PART)
1215 "Handle part messages." nil
1216 (let* ((chnl (first (erc-response.command-args parsed
)))
1217 (reason (erc-trim-string (erc-response.contents parsed
)))
1218 (buffer (erc-get-buffer chnl proc
)))
1219 (multiple-value-bind (nick login host
)
1220 (erc-parse-user (erc-response.sender parsed
))
1221 (erc-remove-channel-member buffer nick
)
1222 (erc-display-message parsed
'notice buffer
1223 'PART ?n nick ?u login
1224 ?h host ?c chnl ?r
(or reason
""))
1225 (when (string= nick
(erc-current-nick))
1226 (run-hook-with-args 'erc-part-hook buffer
)
1229 (erc-remove-channel-users))
1230 (erc-delete-default-channel chnl buffer
)
1231 (erc-update-mode-line buffer
)
1232 (when erc-kill-buffer-on-part
1233 (kill-buffer buffer
))))))
1235 (define-erc-response-handler (PING)
1236 "Handle ping messages." nil
1237 (let ((pinger (first (erc-response.command-args parsed
))))
1238 (erc-log (format "PING: %s" pinger
))
1239 ;; ping response to the server MUST be forced, or you can lose big
1240 (erc-server-send (format "PONG :%s" pinger
) t
)
1241 (when erc-verbose-server-ping
1242 (erc-display-message
1244 'PING ?s
(erc-time-diff erc-server-last-ping-time
(erc-current-time))))
1245 (setq erc-server-last-ping-time
(erc-current-time))))
1247 (define-erc-response-handler (PONG)
1248 "Handle pong messages." nil
1249 (let ((time (string-to-number (erc-response.contents parsed
))))
1251 (setq erc-server-lag
(erc-time-diff time
(erc-current-time)))
1252 (when erc-verbose-server-ping
1253 (erc-display-message
1254 parsed
'notice proc
'PONG
1255 ?h
(first (erc-response.command-args parsed
)) ?i erc-server-lag
1256 ?s
(if (/= erc-server-lag
1) "s" "")))
1257 (erc-update-mode-line))))
1259 (define-erc-response-handler (PRIVMSG NOTICE
)
1261 (let ((sender-spec (erc-response.sender parsed
))
1262 (cmd (erc-response.command parsed
))
1263 (tgt (car (erc-response.command-args parsed
)))
1264 (msg (erc-response.contents parsed
)))
1265 (if (or (erc-ignored-user-p sender-spec
)
1266 (erc-ignored-reply-p msg tgt proc
))
1267 (when erc-minibuffer-ignored
1268 (message "Ignored %s from %s to %s" cmd sender-spec tgt
))
1269 (let* ((sndr (erc-parse-user sender-spec
))
1271 (login (nth 1 sndr
))
1273 (msgp (string= cmd
"PRIVMSG"))
1274 (noticep (string= cmd
"NOTICE"))
1275 ;; S.B. downcase *both* tgt and current nick
1276 (privp (erc-current-nick-p tgt
))
1279 (setf (erc-response.contents parsed
) msg
)
1280 (setq buffer
(erc-get-buffer (if privp nick tgt
) proc
))
1282 (with-current-buffer buffer
1283 ;; update the chat partner info. Add to the list if private
1284 ;; message. We will accumulate private identities indefinitely
1286 (erc-update-channel-member (if privp nick tgt
) nick nick
1287 privp nil nil host login nil nil t
)
1288 (let ((cdata (erc-get-channel-user nick
)))
1289 (setq fnick
(funcall erc-format-nick-function
1290 (car cdata
) (cdr cdata
))))))
1292 ((erc-is-message-ctcp-p msg
)
1294 (erc-process-ctcp-query proc parsed nick login host
)
1295 (erc-process-ctcp-reply proc parsed nick login host
1296 (match-string 1 msg
)))))
1298 (setcar erc-server-last-peers nick
)
1299 (setq s
(erc-format-privmessage
1301 ;; If buffer is a query buffer,
1302 ;; format the nick as for a channel.
1303 (and (not (and buffer
1304 (erc-query-buffer-p buffer
)
1305 erc-format-query-as-channel-p
))
1309 (if (and noticep privp
)
1311 (run-hook-with-args 'erc-echo-notice-always-hook
1312 s parsed buffer nick
)
1313 (run-hook-with-args-until-success
1314 'erc-echo-notice-hook s parsed buffer nick
))
1315 (erc-display-message parsed nil buffer s
)))
1316 (when (string= cmd
"PRIVMSG")
1317 (erc-auto-query proc parsed
))))))
1319 ;; FIXME: need clean way of specifiying extra hooks in
1320 ;; define-erc-response-handler.
1321 (add-hook 'erc-server-PRIVMSG-functions
'erc-auto-query
)
1323 (define-erc-response-handler (QUIT)
1325 (let ((reason (erc-response.contents parsed
))
1327 (multiple-value-bind (nick login host
)
1328 (erc-parse-user (erc-response.sender parsed
))
1329 (setq bufs
(erc-buffer-list-with-nick nick proc
))
1330 (erc-remove-user nick
)
1331 (setq reason
(erc-wash-quit-reason reason nick login host
))
1332 (erc-display-message parsed
'notice bufs
1333 'QUIT ?n nick ?u login
1334 ?h host ?r reason
))))
1336 (define-erc-response-handler (TOPIC)
1338 (let* ((ch (first (erc-response.command-args parsed
)))
1339 (topic (erc-trim-string (erc-response.contents parsed
)))
1340 (time (format-time-string "%T %m/%d/%y" (current-time))))
1341 (multiple-value-bind (nick login host
)
1342 (erc-parse-user (erc-response.sender parsed
))
1343 (erc-update-channel-member ch nick nick nil nil nil host login
)
1344 (erc-update-channel-topic ch
(format "%s\C-o (%s, %s)" topic nick time
))
1345 (erc-display-message parsed
'notice
(erc-get-buffer ch proc
)
1346 'TOPIC ?n nick ?u login ?h host
1349 (define-erc-response-handler (WALLOPS)
1351 (let ((message (erc-response.contents parsed
)))
1352 (multiple-value-bind (nick login host
)
1353 (erc-parse-user (erc-response.sender parsed
))
1354 (erc-display-message
1356 'WALLOPS ?n nick ?m message
))))
1358 (define-erc-response-handler (001)
1359 "Set `erc-server-current-nick' to reflect server settings and display the welcome message."
1361 (erc-set-current-nick (first (erc-response.command-args parsed
)))
1362 (erc-update-mode-line) ; needed here?
1363 (setq erc-nick-change-attempt-count
0)
1364 (setq erc-default-nicks
(if (consp erc-nick
) erc-nick
(list erc-nick
)))
1365 (erc-display-message
1366 parsed
'notice
'active
(erc-response.contents parsed
)))
1368 (define-erc-response-handler (MOTD 002 003 371 372 374 375)
1369 "Display the server's message of the day." nil
1371 (erc-display-message
1372 parsed
'notice
(if erc-server-connected
'active proc
)
1373 (erc-response.contents parsed
)))
1375 (define-erc-response-handler (376 422)
1377 (erc-server-MOTD proc parsed
)
1378 (erc-connection-established proc parsed
))
1380 (define-erc-response-handler (004)
1382 (multiple-value-bind (server-name server-version
)
1383 (cdr (erc-response.command-args parsed
))
1384 (setq erc-server-version server-version
)
1385 (setq erc-server-announced-name server-name
)
1386 (erc-update-mode-line-buffer (process-buffer proc
))
1387 (erc-display-message
1389 's004 ?s server-name ?v server-version
1390 ?U
(fourth (erc-response.command-args parsed
))
1391 ?C
(fifth (erc-response.command-args parsed
)))))
1393 (define-erc-response-handler (005)
1394 "Set the variable `erc-server-parameters' and display the received message.
1396 According to RFC 2812, suggests alternate servers on the network.
1397 Many servers, however, use this code to show which parameters they have set,
1398 for example, the network identifier, maximum allowed topic length, whether
1399 certain commands are accepted and more. See documentation for
1400 `erc-server-parameters' for more information on the parameters sent.
1402 A server may send more than one 005 message."
1404 (let ((line (mapconcat 'identity
1405 (setf (erc-response.command-args parsed
)
1406 (cdr (erc-response.command-args parsed
)))
1408 (while (erc-response.command-args parsed
)
1409 (let ((section (pop (erc-response.command-args parsed
))))
1410 ;; fill erc-server-parameters
1411 (when (string-match "^\\([A-Z]+\\)\=\\(.*\\)$\\|^\\([A-Z]+\\)$"
1413 (add-to-list 'erc-server-parameters
1414 `(,(or (match-string 1 section
)
1415 (match-string 3 section
))
1417 ,(match-string 2 section
))))))
1418 (erc-display-message parsed
'notice proc line
)))
1420 (define-erc-response-handler (221)
1422 (let* ((nick (first (erc-response.command-args parsed
)))
1423 (modes (mapconcat 'identity
1424 (cdr (erc-response.command-args parsed
)) " ")))
1425 (erc-set-modes nick modes
)
1426 (erc-display-message parsed
'notice
'active
's221 ?n nick ?m modes
)))
1428 (define-erc-response-handler (252)
1429 "Display the number of IRC operators online." nil
1430 (erc-display-message parsed
'notice
'active
's252
1431 ?i
(second (erc-response.command-args parsed
))))
1433 (define-erc-response-handler (253)
1434 "Display the number of unknown connections." nil
1435 (erc-display-message parsed
'notice
'active
's253
1436 ?i
(second (erc-response.command-args parsed
))))
1438 (define-erc-response-handler (254)
1439 "Display the number of channels formed." nil
1440 (erc-display-message parsed
'notice
'active
's254
1441 ?i
(second (erc-response.command-args parsed
))))
1443 (define-erc-response-handler (250 251 255 256 257 258 259 265 266 377 378)
1444 "Generic display of server messages as notices.
1446 See `erc-display-server-message'." nil
1447 (erc-display-server-message proc parsed
))
1449 (define-erc-response-handler (301)
1451 (erc-display-message parsed
'notice
'active
's301
1452 ?n
(second (erc-response.command-args parsed
))
1453 ?r
(erc-response.contents parsed
)))
1455 (define-erc-response-handler (303)
1457 (erc-display-message parsed
'notice
'active
's303
1458 ?n
(second (erc-response.command-args parsed
))))
1460 (define-erc-response-handler (305)
1461 "Return from AWAYness." nil
1462 (erc-process-away proc nil
)
1463 (erc-display-message parsed
'notice
'active
1464 's305 ?m
(erc-response.contents parsed
)))
1466 (define-erc-response-handler (306)
1468 (erc-process-away proc t
)
1469 (erc-display-message parsed
'notice
'active
1470 's306 ?m
(erc-response.contents parsed
)))
1472 (define-erc-response-handler (311 314)
1473 "WHOIS/WHOWAS notices." nil
1474 (let ((fname (erc-response.contents parsed
))
1475 (catalog-entry (intern (format "s%s" (erc-response.command parsed
)))))
1476 (multiple-value-bind (nick user host
)
1477 (cdr (erc-response.command-args parsed
))
1478 (erc-update-user-nick nick nick host nil fname user
)
1479 (erc-display-message
1480 parsed
'notice
'active catalog-entry
1481 ?n nick ?f fname ?u user ?h host
))))
1483 (define-erc-response-handler (312)
1485 (multiple-value-bind (nick server-host
)
1486 (cdr (erc-response.command-args parsed
))
1487 (erc-display-message
1488 parsed
'notice
'active
's312
1489 ?n nick ?s server-host ?c
(erc-response.contents parsed
))))
1491 (define-erc-response-handler (313)
1492 "IRC Operator response in WHOIS." nil
1493 (erc-display-message
1494 parsed
'notice
'active
's313
1495 ?n
(second (erc-response.command-args parsed
))))
1497 (define-erc-response-handler (315 318 323 369)
1499 ;; 318 - End of WHOIS list
1500 ;; 323 - End of channel LIST
1501 ;; 369 - End of WHOWAS
1503 (ignore proc parsed
))
1505 (define-erc-response-handler (317)
1507 (multiple-value-bind (nick seconds-idle on-since time
)
1508 (cdr (erc-response.command-args parsed
))
1509 (setq time
(when on-since
1510 (format-time-string "%T %Y/%m/%d"
1511 (erc-string-to-emacs-time on-since
))))
1512 (erc-update-user-nick nick nick nil nil nil
1513 (and time
(format "on since %s" time
)))
1515 (erc-display-message
1516 parsed
'notice
'active
's317-on-since
1517 ?n nick ?i
(erc-sec-to-time (string-to-number seconds-idle
)) ?t time
)
1518 (erc-display-message
1519 parsed
'notice
'active
's317
1520 ?n nick ?i
(erc-sec-to-time (string-to-number seconds-idle
))))))
1522 (define-erc-response-handler (319)
1524 (erc-display-message
1525 parsed
'notice
'active
's319
1526 ?n
(second (erc-response.command-args parsed
))
1527 ?c
(erc-response.contents parsed
)))
1529 (define-erc-response-handler (320)
1530 "Identified user in WHOIS." nil
1531 (erc-display-message
1532 parsed
'notice
'active
's320
1533 ?n
(second (erc-response.command-args parsed
))))
1535 (define-erc-response-handler (321)
1537 (setq erc-channel-list nil
)
1538 (erc-display-message parsed
'notice
'active
's321
))
1540 (define-erc-response-handler (322)
1542 (let ((topic (erc-response.contents parsed
)))
1543 (multiple-value-bind (channel num-users
)
1544 (cdr (erc-response.command-args parsed
))
1545 (add-to-list 'erc-channel-list
(list channel
))
1546 (erc-update-channel-topic channel topic
)
1547 (erc-display-message
1548 parsed
'notice
'active
's322
1549 ?c channel ?u num-users ?t
(or topic
"")))))
1551 (define-erc-response-handler (324)
1552 "Channel or nick modes." nil
1553 (let ((channel (second (erc-response.command-args parsed
)))
1554 (modes (mapconcat 'identity
(cddr (erc-response.command-args parsed
))
1556 (erc-set-modes channel modes
)
1557 (erc-display-message
1558 parsed
'notice
(erc-get-buffer channel proc
)
1559 's324 ?c channel ?m modes
)))
1561 (define-erc-response-handler (329)
1562 "Channel creation date." nil
1563 (let ((channel (second (erc-response.command-args parsed
)))
1564 (time (erc-string-to-emacs-time
1565 (third (erc-response.command-args parsed
)))))
1566 (erc-display-message
1567 parsed
'notice
(erc-get-buffer channel proc
)
1568 's329 ?c channel ?t
(format-time-string "%A %Y/%m/%d %X" time
))))
1570 (define-erc-response-handler (330)
1572 ;; FIXME: I don't know what the magic numbers mean. Mummy, make
1573 ;; the magic numbers go away.
1574 ;; No seriously, I have no clue about the format of this command,
1575 ;; and don't sit on Quakenet, so can't test. Originally we had:
1576 ;; nick == (aref parsed 3)
1577 ;; authaccount == (aref parsed 4)
1578 ;; authmsg == (aref parsed 5)
1579 ;; The guesses below are, well, just that. -- Lawrence 2004/05/10
1580 (let ((nick (second (erc-response.command-args parsed
)))
1581 (authaccount (third (erc-response.command-args parsed
)))
1582 (authmsg (erc-response.contents parsed
)))
1583 (erc-display-message parsed
'notice
'active
's330
1584 ?n nick ?a authmsg ?i authaccount
)))
1586 (define-erc-response-handler (331)
1587 "Channel topic." nil
1588 (let ((channel (second (erc-response.command-args parsed
)))
1589 (topic (erc-response.contents parsed
)))
1590 ;; FIXME: why don't we do anything with the topic? -- Lawrence 2004/05/10
1591 (erc-display-message parsed
'notice
(erc-get-buffer channel proc
)
1594 (define-erc-response-handler (332)
1596 (let ((channel (second (erc-response.command-args parsed
)))
1597 (topic (erc-response.contents parsed
)))
1598 (erc-update-channel-topic channel topic
)
1599 (erc-display-message parsed
'notice
(erc-get-buffer channel proc
)
1600 's332 ?c channel ?T topic
)))
1602 (define-erc-response-handler (333)
1603 ;; Who set the topic, and when
1605 (multiple-value-bind (channel nick time
)
1606 (cdr (erc-response.command-args parsed
))
1607 (setq time
(format-time-string "%T %Y/%m/%d"
1608 (erc-string-to-emacs-time time
)))
1609 (erc-update-channel-topic channel
1610 (format "\C-o (%s, %s)" nick time
)
1612 (erc-display-message parsed
'notice
(erc-get-buffer channel proc
)
1613 's333 ?c channel ?n nick ?t time
)))
1615 (define-erc-response-handler (341)
1616 "Let user know when an INVITE attempt has been sent successfully."
1618 (multiple-value-bind (nick channel
)
1619 (cdr (erc-response.command-args parsed
))
1620 (erc-display-message parsed
'notice
(erc-get-buffer channel proc
)
1621 's341 ?n nick ?c channel
)))
1623 (define-erc-response-handler (352)
1625 (multiple-value-bind (channel user host server nick away-flag
)
1626 (cdr (erc-response.command-args parsed
))
1627 (let ((full-name (erc-response.contents parsed
))
1629 (when (string-match "\\(^[0-9]+ \\)\\(.*\\)$" full-name
)
1630 (setq hopcount
(match-string 1 full-name
))
1631 (setq full-name
(match-string 2 full-name
)))
1632 (erc-update-channel-member channel nick nick nil nil nil host
1634 (erc-display-message parsed
'notice
'active
's352
1635 ?c channel ?n nick ?a away-flag
1636 ?u user ?h host ?f full-name
))))
1638 (define-erc-response-handler (353)
1640 (let ((channel (third (erc-response.command-args parsed
)))
1641 (users (erc-response.contents parsed
)))
1642 (erc-display-message parsed
'notice
(or (erc-get-buffer channel proc
)
1644 's353 ?c channel ?u users
)
1645 (erc-with-buffer (channel proc
)
1646 (erc-channel-receive-names users
))))
1648 (define-erc-response-handler (366)
1650 (erc-with-buffer ((second (erc-response.command-args parsed
)) proc
)
1651 (erc-channel-end-receiving-names)))
1653 (define-erc-response-handler (367)
1654 "Channel ban list entries" nil
1655 (multiple-value-bind (channel banmask setter time
)
1656 (cdr (erc-response.command-args parsed
))
1657 ;; setter and time are not standard
1659 (erc-display-message parsed
'notice
'active
's367-set-by
1664 (erc-display-message parsed
'notice
'active
's367
1668 (define-erc-response-handler (368)
1669 "End of channel ban list" nil
1670 (let ((channel (second (erc-response.command-args parsed
))))
1671 (erc-display-message parsed
'notice
'active
's368
1674 (define-erc-response-handler (379)
1675 "Forwarding to another channel." nil
1676 ;; FIXME: Yet more magic numbers in original code, I'm guessing this
1677 ;; command takes two arguments, and doesn't have any "contents". --
1678 ;; Lawrence 2004/05/10
1679 (multiple-value-bind (from to
)
1680 (cdr (erc-response.command-args parsed
))
1681 (erc-display-message parsed
'notice
'active
1682 's379 ?c from ?f to
)))
1684 (define-erc-response-handler (391)
1685 "Server's time string" nil
1686 (erc-display-message
1687 parsed
'notice
'active
1688 's391 ?s
(second (erc-response.command-args parsed
))
1689 ?t
(third (erc-response.command-args parsed
))))
1691 (define-erc-response-handler (401)
1692 "No such nick/channel." nil
1693 (let ((nick/channel
(second (erc-response.command-args parsed
))))
1694 (when erc-whowas-on-nosuchnick
1695 (erc-log (format "cmd: WHOWAS: %s" nick
/channel
))
1696 (erc-server-send (format "WHOWAS %s 1" nick
/channel
)))
1697 (erc-display-message parsed
'(notice error
) 'active
1698 's401 ?n nick
/channel
)))
1700 (define-erc-response-handler (403)
1701 "No such channel." nil
1702 (erc-display-message parsed
'(notice error
) 'active
1703 's403 ?c
(second (erc-response.command-args parsed
))))
1705 (define-erc-response-handler (404)
1706 "Cannot send to channel." nil
1707 (erc-display-message parsed
'(notice error
) 'active
1708 's404 ?c
(second (erc-response.command-args parsed
))))
1711 (define-erc-response-handler (405)
1712 ;; Can't join that many channels.
1714 (erc-display-message parsed
'(notice error
) 'active
1715 's405 ?c
(second (erc-response.command-args parsed
))))
1717 (define-erc-response-handler (406)
1720 (erc-display-message parsed
'(notice error
) 'active
1721 's406 ?n
(second (erc-response.command-args parsed
))))
1723 (define-erc-response-handler (412)
1726 (erc-display-message parsed
'(notice error
) 'active
's412
))
1728 (define-erc-response-handler (421)
1731 (erc-display-message parsed
'(notice error
) 'active
's421
1732 ?c
(second (erc-response.command-args parsed
))))
1734 (define-erc-response-handler (432)
1737 (erc-display-message parsed
'(notice error
) 'active
's432
1738 ?n
(second (erc-response.command-args parsed
))))
1740 (define-erc-response-handler (433)
1741 ;; Login-time "nick in use"
1743 (erc-nickname-in-use (second (erc-response.command-args parsed
))
1746 (define-erc-response-handler (437)
1747 ;; Nick temporarily unavailable (IRCnet)
1749 (let ((nick/channel
(second (erc-response.command-args parsed
))))
1750 (unless (erc-channel-p nick
/channel
)
1751 (erc-nickname-in-use nick
/channel
"temporarily unavailable"))))
1753 (define-erc-response-handler (442)
1756 (erc-display-message parsed
'(notice error
) 'active
's442
1757 ?c
(second (erc-response.command-args parsed
))))
1759 (define-erc-response-handler (461)
1760 ;; Not enough params for command.
1762 (erc-display-message parsed
'(notice error
) 'active
's461
1763 ?c
(second (erc-response.command-args parsed
))
1764 ?m
(erc-response.contents parsed
)))
1766 (define-erc-response-handler (465)
1767 "You are banned from this server." nil
1768 (setq erc-server-banned t
)
1769 ;; show the server's message, as a reason might be provided
1770 (erc-display-error-notice
1772 (erc-response.contents parsed
)))
1774 (define-erc-response-handler (474)
1775 "Banned from channel errors" nil
1776 (erc-display-message parsed
'(notice error
) nil
1777 (intern (format "s%s"
1778 (erc-response.command parsed
)))
1779 ?c
(second (erc-response.command-args parsed
))))
1781 (define-erc-response-handler (475)
1782 "Channel key needed." nil
1783 (erc-display-message parsed
'(notice error
) nil
's475
1784 ?c
(second (erc-response.command-args parsed
)))
1785 (when erc-prompt-for-channel-key
1786 (let ((channel (second (erc-response.command-args parsed
)))
1787 (key (read-from-minibuffer
1788 (format "Channel %s is mode +k. Enter key (RET to cancel): "
1789 (second (erc-response.command-args parsed
))))))
1790 (when (and key
(> (length key
) 0))
1791 (erc-cmd-JOIN channel key
)))))
1793 (define-erc-response-handler (477)
1795 (let ((channel (second (erc-response.command-args parsed
)))
1796 (message (erc-response.contents parsed
)))
1797 (erc-display-message parsed
'notice
(erc-get-buffer channel proc
)
1798 (format "%s: %s" channel message
))))
1800 (define-erc-response-handler (482)
1802 (let ((channel (second (erc-response.command-args parsed
)))
1803 (message (erc-response.contents parsed
)))
1804 (erc-display-message parsed
'(error notice
) 'active
's482
1805 ?c channel ?m message
)))
1807 (define-erc-response-handler (431 445 446 451 462 463 464 481 483 484 485
1809 ;; 431 - No nickname given
1810 ;; 445 - SUMMON has been disabled
1811 ;; 446 - USERS has been disabled
1812 ;; 451 - You have not registered
1813 ;; 462 - Unauthorized command (already registered)
1814 ;; 463 - Your host isn't among the privileged
1815 ;; 464 - Password incorrect
1816 ;; 481 - Need IRCop privileges
1817 ;; 483 - You can't kill a server!
1818 ;; 484 - Your connection is restricted!
1819 ;; 485 - You're not the original channel operator
1820 ;; 491 - No O-lines for your host
1821 ;; 501 - Unknown MODE flag
1822 ;; 502 - Cannot change mode for other users
1824 (erc-display-error-notice
1826 (intern (format "s%s" (erc-response.command parsed
)))))
1828 ;; FIXME: These are yet to be implemented, they're just stubs for now
1829 ;; -- Lawrence 2004/05/12
1831 ;; response numbers left here for reference
1833 ;; (define-erc-response-handler (323 364 365 381 382 392 393 394 395
1834 ;; 200 201 202 203 204 205 206 208 209 211 212 213
1835 ;; 214 215 216 217 218 219 241 242 243 244 249 261
1836 ;; 262 302 342 351 402 407 409 411 413 414 415
1837 ;; 423 424 436 441 443 444 467 471 472 473 KILL)
1839 ;; (ignore proc parsed))
1841 (provide 'erc-backend
)
1843 ;;; erc-backend.el ends here
1845 ;; indent-tabs-mode: nil
1848 ;; arch-tag: a64e6bb7-a780-4efd-8f98-083b18c7c84a