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