Merge from erc--emacs--22
[bpt/emacs.git] / lisp / erc / erc-backend.el
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.
122 Use `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
150 This is only set if the server sends 005 messages saying what is
151 supported on the server.
152
153 Entries are of the form:
154 (PARAMETER . VALUE)
155 or
156 (PARAMETER) if no value is provided.
157
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)
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.
179 This 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.
192 Those 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.
197 This 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.
202 This 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.
207 This 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
212 but 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
223 When ERC receives a private message, it sets up a new buffer for
224 this query. These in turn, though, do start flyspell. This
225 involves starting an external process, in which case Emacs will
226 wait - and when it waits, it does accept other stuff from, say,
227 network exceptions. So, if someone sends you two messages
228 quickly after each other, ispell is started for the first, but
229 might take long enough for the second message to be processed
230 first.")
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.
235 See `erc-server-flood-margin' for an explanation of the flood
236 protection 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.
241 See `erc-server-flood-margin' for an explanation of the flood
242 protection 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
258 Reconnection 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.
264 If a message exceeds this size, it is broken into multiple ones.
265
266 IRC allows for lines up to 512 bytes. Two of them are CR LF.
267 And a typical message looks like this:
268
269 :nicky!uhuser@host212223.dialin.fnordisp.net PRIVMSG #lazybastards :Hello!
270
271 You can limit here the maximum length of the \"Hello!\" part.
272 Good 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.
282 This is either a coding system, a cons, a function, or nil.
283
284 If a cons, the encoding system for outgoing text is in the car
285 and the decoding system for incoming text is in the cdr. The most
286 interesting use for this is to put `undecided' in the cdr. If a
287 function, it is called with no arguments and should return a
288 coding system or a cons as described above. Note that you can use
289 the dynamically bound variable `target' to get the current
290 target. See `erc-coding-system-for-target'.
291
292 If you need to send non-ASCII text to people not using a client that
293 does decoding on its own, you must tell ERC what encoding to use.
294 Emacs cannot guess it, since it does not know what the people on the
295 other 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.
305 This overrides `erc-server-coding-system' depending on the
306 current target as returned by `erc-default-target'.
307
308 Example: If you know that the channel #linux-ru uses the coding-system
309 `cyrillic-koi8', then add '(\"#linux-ru\" . cyrillic-koi8) to the
310 alist."
311 :group 'erc-server
312 :type '(repeat (cons (string :tag "Target")
313 coding-system)))
314
315 (defcustom erc-server-connect-function 'open-network-stream
316 "Function used to initiate a connection.
317 It 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.
323 Each string is a IRC message type, like PRIVMSG or NOTICE.
324 All 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
331 If two identical messages arrive within this value of one another, the second
332 isn'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.
343 The flood protection algorithm of ERC works like the one
344 detailed 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.
358 See `erc-server-flood-margin' for an explanation of the flood
359 protection 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.
367 If 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.
380 The length is specified in `erc-split-line-length'.
381
382 Currently 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 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.
427 We 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
490 are going to quit. Determine whether user has quit or whether erc has
491 been terminated. Conditionally try to reconnect and take appropriate
492 action."
493 (if erc-server-quitting
494 ;; normal quit
495 (progn
496 (erc-display-message nil 'error (current-buffer) 'finished)
497 (when erc-kill-server-buffer-on-quit
498 (set-buffer-modified-p nil)
499 (kill-buffer (current-buffer))))
500 ;; unexpected disconnect
501 (erc-display-message nil 'error (current-buffer)
502 (if erc-server-auto-reconnect
503 'disconnected
504 'disconnected-noreconnect))
505 (erc-update-mode-line)
506 (erc-set-active-buffer (current-buffer))
507 (setq erc-server-last-sent-time 0)
508 (setq erc-server-lines-sent 0)
509 (if (and erc-server-auto-reconnect
510 (not (string-match "^deleted" event))
511 ;; open-network-stream-nowait error for connection refused
512 (not (string-match "^failed with code 111" event)))
513 ;; Yuck, this should perhaps funcall
514 ;; erc-server-reconnect-function with no args
515 (erc-open erc-session-server erc-session-port erc-server-current-nick
516 erc-session-user-full-name t erc-session-password)
517 ;; terminate, do not reconnect
518 (erc-display-message nil 'error (current-buffer)
519 'terminated ?e event))))
520
521 (defun erc-process-sentinel (cproc event)
522 "Sentinel function for ERC process."
523 (with-current-buffer (process-buffer cproc)
524 (erc-log (format
525 "SENTINEL: proc: %S status: %S event: %S (quitting: %S)"
526 cproc (process-status cproc) event erc-server-quitting))
527 (if (string-match "^open" event)
528 ;; newly opened connection (no wait)
529 (erc-login)
530 ;; assume event is 'failed
531 (let ((buf (process-buffer cproc)))
532 (erc-with-all-buffers-of-server cproc nil
533 (setq erc-server-connected nil))
534 (when erc-server-ping-handler
535 (progn (erc-cancel-timer erc-server-ping-handler)
536 (setq erc-server-ping-handler nil)))
537 (run-hook-with-args 'erc-disconnected-hook
538 (erc-current-nick) (system-name) "")
539 ;; Remove the prompt
540 (goto-char (or (marker-position erc-input-marker) (point-max)))
541 (forward-line 0)
542 (erc-remove-text-properties-region (point) (point-max))
543 (delete-region (point) (point-max))
544 ;; Decide what to do with the buffer
545 ;; Restart if disconnected
546 (erc-process-sentinel-1 event)
547 ;; Make sure we don't write to the buffer if it has been
548 ;; killed
549 (when (buffer-live-p buf)
550 (erc-update-mode-line)
551 (set-buffer-modified-p nil))))))
552
553 ;;;; Sending messages
554
555 (defun erc-coding-system-for-target (target)
556 "Return the coding system or cons cell appropriate for TARGET.
557 This is determined via `erc-encoding-coding-alist' or
558 `erc-server-coding-system'."
559 (or (when target
560 (let ((case-fold-search t))
561 (catch 'match
562 (dolist (pat erc-encoding-coding-alist)
563 (when (string-match (car pat) target)
564 (throw 'match (cdr pat)))))))
565 (and (functionp erc-server-coding-system)
566 (funcall erc-server-coding-system))
567 erc-server-coding-system))
568
569 (defun erc-decode-string-from-target (str target)
570 "Decode STR as appropriate for TARGET.
571 This is indicated by `erc-encoding-coding-alist', defaulting to the value of
572 `erc-server-coding-system'."
573 (unless (stringp str)
574 (setq str ""))
575 (let ((coding (erc-coding-system-for-target target)))
576 (when (consp coding)
577 (setq coding (cdr coding)))
578 (erc-decode-coding-string str coding)))
579
580 ;; proposed name, not used by anything yet
581 (defun erc-send-line (text display-fn)
582 "Send TEXT to the current server. Wrapping and flood control apply.
583 Use DISPLAY-FN to show the results."
584 (mapc (lambda (line)
585 (erc-server-send line)
586 (funcall display-fn))
587 (erc-split-line text)))
588
589 ;; From Circe, with modifications
590 (defun erc-server-send (string &optional forcep target)
591 "Send STRING to the current server.
592 If FORCEP is non-nil, no flood protection is done - the string is
593 sent directly. This might cause the messages to arrive in a wrong
594 order.
595
596 If TARGET is specified, look up encoding information for that
597 channel in `erc-encoding-coding-alist' or
598 `erc-server-coding-system'.
599
600 See `erc-server-flood-margin' for an explanation of the flood
601 protection algorithm."
602 (erc-log (concat "erc-server-send: " string "(" (buffer-name) ")"))
603 (setq erc-server-last-sent-time (erc-current-time))
604 (let ((buf (erc-server-buffer))
605 (encoding (erc-coding-system-for-target
606 (or target (erc-default-target)))))
607 (when (consp encoding)
608 (setq encoding (car encoding)))
609 (if (and buf
610 (erc-server-process-alive))
611 (with-current-buffer buf
612 (let ((str (concat string "\r\n")))
613 (if forcep
614 (progn
615 (setq erc-server-flood-last-message
616 (+ erc-server-flood-penalty
617 erc-server-flood-last-message))
618 (erc-log-irc-protocol str 'outbound)
619 (condition-case err
620 (progn
621 ;; Set encoding just before sending the string
622 (when (fboundp 'set-process-coding-system)
623 (set-process-coding-system erc-server-process
624 'raw-text encoding))
625 (process-send-string erc-server-process str))
626 ;; See `erc-server-send-queue' for full
627 ;; explanation of why we need this condition-case
628 (error nil)))
629 (setq erc-server-flood-queue
630 (append erc-server-flood-queue
631 (list (cons str encoding))))
632 (erc-server-send-queue (current-buffer))))
633 t)
634 (message "ERC: No process running")
635 nil)))
636
637 ;; From Circe
638 (defun erc-server-send-queue (buffer)
639 "Send messages in `erc-server-flood-queue'.
640 See `erc-server-flood-margin' for an explanation of the flood
641 protection algorithm."
642 (with-current-buffer buffer
643 (let ((now (erc-current-time)))
644 (when erc-server-flood-timer
645 (erc-cancel-timer erc-server-flood-timer)
646 (setq erc-server-flood-timer nil))
647 (when (< erc-server-flood-last-message
648 now)
649 (setq erc-server-flood-last-message now))
650 (while (and erc-server-flood-queue
651 (< erc-server-flood-last-message
652 (+ now erc-server-flood-margin)))
653 (let ((msg (caar erc-server-flood-queue))
654 (encoding (cdar erc-server-flood-queue)))
655 (setq erc-server-flood-queue (cdr erc-server-flood-queue)
656 erc-server-flood-last-message
657 (+ erc-server-flood-last-message
658 erc-server-flood-penalty))
659 (erc-log-irc-protocol msg 'outbound)
660 (erc-log (concat "erc-server-send-queue: "
661 msg "(" (buffer-name buffer) ")"))
662 (when (erc-server-process-alive)
663 (condition-case err
664 ;; Set encoding just before sending the string
665 (progn
666 (when (fboundp 'set-process-coding-system)
667 (set-process-coding-system erc-server-process
668 'raw-text encoding))
669 (process-send-string erc-server-process msg))
670 ;; Sometimes the send can occur while the process is
671 ;; being killed, which results in a weird SIGPIPE error.
672 ;; Catch this and ignore it.
673 (error nil)))))
674 (when erc-server-flood-queue
675 (setq erc-server-flood-timer
676 (run-at-time (+ 0.2 erc-server-flood-penalty)
677 nil #'erc-server-send-queue buffer))))))
678
679 (defun erc-message (message-command line &optional force)
680 "Send LINE to the server as a privmsg or a notice.
681 MESSAGE-COMMAND should be either \"PRIVMSG\" or \"NOTICE\".
682 If the target is \",\", the last person you've got a message from will
683 be used. If the target is \".\", the last person you've sent a message
684 to will be used."
685 (cond
686 ((string-match "^\\s-*\\(\\S-+\\) ?\\(.*\\)" line)
687 (let ((tgt (match-string 1 line))
688 (s (match-string 2 line)))
689 (erc-log (format "cmd: MSG(%s): [%s] %s" message-command tgt s))
690 (cond
691 ((string= tgt ",")
692 (if (car erc-server-last-peers)
693 (setq tgt (car erc-server-last-peers))
694 (setq tgt nil)))
695 ((string= tgt ".")
696 (if (cdr erc-server-last-peers)
697 (setq tgt (cdr erc-server-last-peers))
698 (setq tgt nil))))
699 (cond
700 (tgt
701 (setcdr erc-server-last-peers tgt)
702 (erc-server-send (format "%s %s :%s" message-command tgt s)
703 force))
704 (t
705 (erc-display-message nil 'error (current-buffer) 'no-target))))
706 t)
707 (t nil)))
708
709 ;;; CTCP
710
711 (defun erc-send-ctcp-message (tgt l &optional force)
712 "Send CTCP message L to TGT.
713
714 If TGT is nil the message is not sent.
715 The command must contain neither a prefix nor a trailing `\\n'.
716
717 See also `erc-server-send'."
718 (let ((l (erc-upcase-first-word l)))
719 (cond
720 (tgt
721 (erc-log (format "erc-send-CTCP-message: [%s] %s" tgt l))
722 (erc-server-send (format "PRIVMSG %s :\C-a%s\C-a" tgt l)
723 force)))))
724
725 (defun erc-send-ctcp-notice (tgt l &optional force)
726 "Send CTCP notice L to TGT.
727
728 If TGT is nil the message is not sent.
729 The command must contain neither a prefix nor a trailing `\\n'.
730
731 See also `erc-server-send'."
732 (let ((l (erc-upcase-first-word l)))
733 (cond
734 (tgt
735 (erc-log (format "erc-send-CTCP-notice: [%s] %s" tgt l))
736 (erc-server-send (format "NOTICE %s :\C-a%s\C-a" tgt l)
737 force)))))
738
739 ;;;; Handling responses
740
741 (defun erc-parse-server-response (proc string)
742 "Parse and act upon a complete line from an IRC server.
743 PROC is the process (connection) from which STRING was received.
744 PROCs `process-buffer' is `current-buffer' when this function is called."
745 (unless (string= string "") ;; Ignore empty strings
746 (save-match-data
747 (let ((posn (if (eq (aref string 0) ?:)
748 (string-match " " string)
749 0))
750 (msg (make-erc-response :unparsed string)))
751
752 (setf (erc-response.sender msg)
753 (if (eq posn 0)
754 erc-session-server
755 (substring string 1 posn)))
756
757 (setf (erc-response.command msg)
758 (let* ((bposn (string-match "[^ \n]" string posn))
759 (eposn (string-match " " string bposn)))
760 (setq posn (and eposn
761 (string-match "[^ \n]" string eposn)))
762 (substring string bposn eposn)))
763
764 (while (and posn
765 (not (eq (aref string posn) ?:)))
766 (push (let* ((bposn posn)
767 (eposn (string-match " " string bposn)))
768 (setq posn (and eposn
769 (string-match "[^ \n]" string eposn)))
770 (substring string bposn eposn))
771 (erc-response.command-args msg)))
772 (when posn
773 (let ((str (substring string (1+ posn))))
774 (push str (erc-response.command-args msg))))
775
776 (setf (erc-response.contents msg)
777 (first (erc-response.command-args msg)))
778
779 (setf (erc-response.command-args msg)
780 (nreverse (erc-response.command-args msg)))
781
782 (erc-decode-parsed-server-response msg)
783
784 (erc-handle-parsed-server-response proc msg)))))
785
786 (defun erc-decode-parsed-server-response (parsed-response)
787 "Decode a pre-parsed PARSED-RESPONSE before it can be handled.
788
789 If there is a channel name in `erc-response.command-args', decode
790 `erc-response' according to this channel name and
791 `erc-encoding-coding-alist', or use `erc-server-coding-system'
792 for decoding."
793 (let ((args (erc-response.command-args parsed-response))
794 (decode-target nil)
795 (decoded-args ()))
796 (dolist (arg args nil)
797 (when (string-match "^[#&].*" arg)
798 (setq decode-target arg)))
799 (when (stringp decode-target)
800 (setq decode-target (erc-decode-string-from-target decode-target nil)))
801 (setf (erc-response.unparsed parsed-response)
802 (erc-decode-string-from-target
803 (erc-response.unparsed parsed-response)
804 decode-target))
805 (setf (erc-response.sender parsed-response)
806 (erc-decode-string-from-target
807 (erc-response.sender parsed-response)
808 decode-target))
809 (setf (erc-response.command parsed-response)
810 (erc-decode-string-from-target
811 (erc-response.command parsed-response)
812 decode-target))
813 (dolist (arg (nreverse args) nil)
814 (push (erc-decode-string-from-target arg decode-target)
815 decoded-args))
816 (setf (erc-response.command-args parsed-response) decoded-args)
817 (setf (erc-response.contents parsed-response)
818 (erc-decode-string-from-target
819 (erc-response.contents parsed-response)
820 decode-target))))
821
822 (defun erc-handle-parsed-server-response (process parsed-response)
823 "Handle a pre-parsed PARSED-RESPONSE from PROCESS.
824
825 Hands off to helper functions via `erc-call-hooks'."
826 (if (member (erc-response.command parsed-response)
827 erc-server-prevent-duplicates)
828 (let ((m (erc-response.unparsed parsed-response)))
829 ;; duplicate supression
830 (if (< (or (gethash m erc-server-duplicates) 0)
831 (- (erc-current-time) erc-server-duplicate-timeout))
832 (erc-call-hooks process parsed-response))
833 (puthash m (erc-current-time) erc-server-duplicates))
834 ;; Hand off to the relevant handler.
835 (erc-call-hooks process parsed-response)))
836
837 (defun erc-get-hook (command)
838 "Return the hook variable associated with COMMAND.
839
840 See also `erc-server-responses'."
841 (gethash (format (if (numberp command) "%03i" "%s") command)
842 erc-server-responses))
843
844 (defun erc-call-hooks (process message)
845 "Call hooks associated with MESSAGE in PROCESS.
846
847 Finds hooks by looking in the `erc-server-responses' hashtable."
848 (let ((hook (or (erc-get-hook (erc-response.command message))
849 'erc-default-server-functions)))
850 (run-hook-with-args-until-success hook process message)
851 (let ((server-buffer (erc-server-buffer)))
852 (when (buffer-live-p server-buffer)
853 (with-current-buffer server-buffer
854 (run-hook-with-args 'erc-timer-hook (erc-current-time)))))))
855
856 (add-hook 'erc-default-server-functions 'erc-handle-unknown-server-response)
857
858 (defun erc-handle-unknown-server-response (proc parsed)
859 "Display unknown server response's message."
860 (let ((line (concat (erc-response.sender parsed)
861 " "
862 (erc-response.command parsed)
863 " "
864 (mapconcat 'identity (erc-response.command-args parsed)
865 " "))))
866 (erc-display-message parsed 'notice proc line)))
867
868
869 (put 'define-erc-response-handler 'edebug-form-spec
870 '(&define :name erc-response-handler
871 (name &rest name)
872 &optional sexp sexp def-body))
873
874 (defmacro* define-erc-response-handler ((name &rest aliases)
875 &optional extra-fn-doc extra-var-doc
876 &rest fn-body)
877 "Define an ERC handler hook/function pair.
878 NAME is the response name as sent by the server (see the IRC RFC for
879 meanings).
880
881 This creates:
882 - a hook variable `erc-server-NAME-functions' initialised to `erc-server-NAME'.
883 - a function `erc-server-NAME' with body FN-BODY.
884
885 If ALIASES is non-nil, each alias in ALIASES is `defalias'ed to
886 `erc-server-NAME'.
887 Alias hook variables are created as `erc-server-ALIAS-functions' and
888 initialised to the same default value as `erc-server-NAME-functions'.
889
890 FN-BODY is the body of `erc-server-NAME' it may refer to the two
891 function arguments PROC and PARSED.
892
893 If EXTRA-FN-DOC is non-nil, it is inserted at the beginning of the
894 defined function's docstring.
895
896 If EXTRA-VAR-DOC is non-nil, it is inserted at the beginning of the
897 defined variable's docstring.
898
899 As an example:
900
901 (define-erc-response-handler (311 WHOIS WI)
902 \"Some non-generic function documentation.\"
903 \"Some non-generic variable documentation.\"
904 (do-stuff-with-whois proc parsed))
905
906 Would expand to:
907
908 (prog2
909 (defvar erc-server-311-functions 'erc-server-311
910 \"Some non-generic variable documentation.
911
912 Hook called upon receiving a 311 server response.
913 Each function is called with two arguments, the process associated
914 with the response and the parsed response.
915 See also `erc-server-311'.\")
916
917 (defun erc-server-311 (proc parsed)
918 \"Some non-generic function documentation.
919
920 Handler for a 311 server response.
921 PROC is the server process which returned the response.
922 PARSED is the actual response as an `erc-response' struct.
923 If you want to add responses don't modify this function, but rather
924 add things to `erc-server-311-functions' instead.\"
925 (do-stuff-with-whois proc parsed))
926
927 (puthash \"311\" 'erc-server-311-functions erc-server-responses)
928 (puthash \"WHOIS\" 'erc-server-WHOIS-functions erc-server-responses)
929 (puthash \"WI\" 'erc-server-WI-functions erc-server-responses)
930
931 (defalias 'erc-server-WHOIS 'erc-server-311)
932 (defvar erc-server-WHOIS-functions 'erc-server-311
933 \"Some non-generic variable documentation.
934
935 Hook called upon receiving a WHOIS server response.
936 Each function is called with two arguments, the process associated
937 with the response and the parsed response.
938 See also `erc-server-311'.\")
939
940 (defalias 'erc-server-WI 'erc-server-311)
941 (defvar erc-server-WI-functions 'erc-server-311
942 \"Some non-generic variable documentation.
943
944 Hook called upon receiving a WI server response.
945 Each function is called with two arguments, the process associated
946 with the response and the parsed response.
947 See also `erc-server-311'.\"))
948
949 \(fn (NAME &rest ALIASES) &optional EXTRA-FN-DOC EXTRA-VAR-DOC &rest FN-BODY)"
950 (if (numberp name) (setq name (intern (format "%03i" name))))
951 (setq aliases (mapcar (lambda (a)
952 (if (numberp a)
953 (format "%03i" a)
954 a))
955 aliases))
956 (let* ((hook-name (intern (format "erc-server-%s-functions" name)))
957 (fn-name (intern (format "erc-server-%s" name)))
958 (hook-doc (format "%sHook called upon receiving a %%s server response.
959 Each function is called with two arguments, the process associated
960 with the response and the parsed response.
961 See also `%s'."
962 (if extra-var-doc
963 (concat extra-var-doc "\n\n")
964 "")
965 fn-name))
966 (fn-doc (format "%sHandler for a %s server response.
967 PROC is the server process which returned the response.
968 PARSED is the actual response as an `erc-response' struct.
969 If you want to add responses don't modify this function, but rather
970 add things to `%s' instead."
971 (if extra-fn-doc
972 (concat extra-fn-doc "\n\n")
973 "")
974 name hook-name))
975 (fn-alternates
976 (loop for alias in aliases
977 collect (intern (format "erc-server-%s" alias))))
978 (var-alternates
979 (loop for alias in aliases
980 collect (intern (format "erc-server-%s-functions" alias)))))
981 `(prog2
982 ;; Normal hook variable.
983 (defvar ,hook-name ',fn-name ,(format hook-doc name))
984 ;; Handler function
985 (defun ,fn-name (proc parsed)
986 ,fn-doc
987 ,@fn-body)
988
989 ;; Make find-function and find-variable find them
990 (put ',fn-name 'definition-name ',name)
991 (put ',hook-name 'definition-name ',name)
992
993 ;; Hashtable map of responses to hook variables
994 ,@(loop for response in (cons name aliases)
995 for var in (cons hook-name var-alternates)
996 collect `(puthash ,(format "%s" response) ',var
997 erc-server-responses))
998 ;; Alternates.
999 ;; Functions are defaliased, hook variables are defvared so we
1000 ;; can add hooks to one alias, but not another.
1001 ,@(loop for fn in fn-alternates
1002 for var in var-alternates
1003 for a in aliases
1004 nconc (list `(defalias ',fn ',fn-name)
1005 `(defvar ,var ',fn-name ,(format hook-doc a))
1006 `(put ',var 'definition-name ',hook-name))))))
1007
1008 (define-erc-response-handler (ERROR)
1009 "Handle an ERROR command from the server." nil
1010 (erc-display-message
1011 parsed 'error nil 'ERROR
1012 ?s (erc-response.sender parsed) ?c (erc-response.contents parsed)))
1013
1014 (define-erc-response-handler (INVITE)
1015 "Handle invitation messages."
1016 nil
1017 (let ((target (first (erc-response.command-args parsed)))
1018 (chnl (erc-response.contents parsed)))
1019 (multiple-value-bind (nick login host)
1020 (erc-parse-user (erc-response.sender parsed))
1021 (setq erc-invitation chnl)
1022 (when (string= target (erc-current-nick))
1023 (erc-display-message
1024 parsed 'notice 'active
1025 'INVITE ?n nick ?u login ?h host ?c chnl)))))
1026
1027
1028 (define-erc-response-handler (JOIN)
1029 "Handle join messages."
1030 nil
1031 (let ((chnl (erc-response.contents parsed))
1032 (buffer nil))
1033 (multiple-value-bind (nick login host)
1034 (erc-parse-user (erc-response.sender parsed))
1035 ;; strip the stupid combined JOIN facility (IRC 2.9)
1036 (if (string-match "^\\(.*\\)?\^g.*$" chnl)
1037 (setq chnl (match-string 1 chnl)))
1038 (save-excursion
1039 (let* ((str (cond
1040 ;; If I have joined a channel
1041 ((erc-current-nick-p nick)
1042 (setq buffer (erc-open erc-session-server erc-session-port
1043 nick erc-session-user-full-name
1044 nil nil
1045 erc-default-recipients chnl
1046 erc-server-process))
1047 (when buffer
1048 (set-buffer buffer)
1049 (erc-add-default-channel chnl)
1050 (erc-server-send (format "MODE %s" chnl)))
1051 (erc-with-buffer (chnl proc)
1052 (erc-channel-begin-receiving-names))
1053 (erc-update-mode-line)
1054 (run-hooks 'erc-join-hook)
1055 (erc-make-notice
1056 (erc-format-message 'JOIN-you ?c chnl)))
1057 (t
1058 (setq buffer (erc-get-buffer chnl proc))
1059 (erc-make-notice
1060 (erc-format-message
1061 'JOIN ?n nick ?u login ?h host ?c chnl))))))
1062 (when buffer (set-buffer buffer))
1063 (erc-update-channel-member chnl nick nick t nil nil host login)
1064 ;; on join, we want to stay in the new channel buffer
1065 ;;(set-buffer ob)
1066 (erc-display-message parsed nil buffer str))))))
1067
1068 (define-erc-response-handler (KICK)
1069 "Handle kick messages received from the server." nil
1070 (let* ((ch (first (erc-response.command-args parsed)))
1071 (tgt (second (erc-response.command-args parsed)))
1072 (reason (erc-trim-string (erc-response.contents parsed)))
1073 (buffer (erc-get-buffer ch proc)))
1074 (multiple-value-bind (nick login host)
1075 (erc-parse-user (erc-response.sender parsed))
1076 (erc-remove-channel-member buffer tgt)
1077 (cond
1078 ((string= tgt (erc-current-nick))
1079 (erc-display-message
1080 parsed 'notice buffer
1081 'KICK-you ?n nick ?u login ?h host ?c ch ?r reason)
1082 (run-hook-with-args 'erc-kick-hook buffer)
1083 (erc-with-buffer
1084 (buffer)
1085 (erc-remove-channel-users))
1086 (erc-delete-default-channel ch buffer)
1087 (erc-update-mode-line buffer))
1088 ((string= nick (erc-current-nick))
1089 (erc-display-message
1090 parsed 'notice buffer
1091 'KICK-by-you ?k tgt ?c ch ?r reason))
1092 (t (erc-display-message
1093 parsed 'notice buffer
1094 'KICK ?k tgt ?n nick ?u login ?h host ?c ch ?r reason))))))
1095
1096 (define-erc-response-handler (MODE)
1097 "Handle server mode changes." nil
1098 (let ((tgt (first (erc-response.command-args parsed)))
1099 (mode (mapconcat 'identity (cdr (erc-response.command-args parsed))
1100 " ")))
1101 (multiple-value-bind (nick login host)
1102 (erc-parse-user (erc-response.sender parsed))
1103 (erc-log (format "MODE: %s -> %s: %s" nick tgt mode))
1104 ;; dirty hack
1105 (let ((buf (cond ((erc-channel-p tgt)
1106 (erc-get-buffer tgt proc))
1107 ((string= tgt (erc-current-nick)) nil)
1108 ((erc-active-buffer) (erc-active-buffer))
1109 (t (erc-get-buffer tgt)))))
1110 (with-current-buffer (or buf
1111 (current-buffer))
1112 (erc-update-modes tgt mode nick host login))
1113 (if (or (string= login "") (string= host ""))
1114 (erc-display-message parsed 'notice buf
1115 'MODE-nick ?n nick
1116 ?t tgt ?m mode)
1117 (erc-display-message parsed 'notice buf
1118 'MODE ?n nick ?u login
1119 ?h host ?t tgt ?m mode)))
1120 (erc-banlist-update proc parsed))))
1121
1122 (define-erc-response-handler (NICK)
1123 "Handle nick change messages." nil
1124 (let ((nn (erc-response.contents parsed))
1125 bufs)
1126 (multiple-value-bind (nick login host)
1127 (erc-parse-user (erc-response.sender parsed))
1128 (setq bufs (erc-buffer-list-with-nick nick proc))
1129 (erc-log (format "NICK: %s -> %s" nick nn))
1130 ;; if we had a query with this user, make sure future messages will be
1131 ;; sent to the correct nick. also add to bufs, since the user will want
1132 ;; to see the nick change in the query, and if it's a newly begun query,
1133 ;; erc-channel-users won't contain it
1134 (erc-buffer-filter
1135 (lambda ()
1136 (when (equal (erc-default-target) nick)
1137 (setq erc-default-recipients
1138 (cons nn (cdr erc-default-recipients)))
1139 (rename-buffer nn)
1140 (erc-update-mode-line)
1141 (add-to-list 'bufs (current-buffer)))))
1142 (erc-update-user-nick nick nn host nil nil login)
1143 (cond
1144 ((string= nick (erc-current-nick))
1145 (add-to-list 'bufs (erc-server-buffer))
1146 (erc-set-current-nick nn)
1147 (erc-update-mode-line)
1148 (setq erc-nick-change-attempt-count 0)
1149 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
1150 (erc-display-message
1151 parsed 'notice bufs
1152 'NICK-you ?n nick ?N nn)
1153 (run-hook-with-args 'erc-nick-changed-functions nn nick))
1154 (t
1155 (erc-handle-user-status-change 'nick (list nick login host) (list nn))
1156 (erc-display-message parsed 'notice bufs 'NICK ?n nick
1157 ?u login ?h host ?N nn))))))
1158
1159 (define-erc-response-handler (PART)
1160 "Handle part messages." nil
1161 (let* ((chnl (first (erc-response.command-args parsed)))
1162 (reason (erc-trim-string (erc-response.contents parsed)))
1163 (buffer (erc-get-buffer chnl proc)))
1164 (multiple-value-bind (nick login host)
1165 (erc-parse-user (erc-response.sender parsed))
1166 (erc-remove-channel-member buffer nick)
1167 (erc-display-message parsed 'notice buffer
1168 'PART ?n nick ?u login
1169 ?h host ?c chnl ?r (or reason ""))
1170 (when (string= nick (erc-current-nick))
1171 (run-hook-with-args 'erc-part-hook buffer)
1172 (erc-with-buffer
1173 (buffer)
1174 (erc-remove-channel-users))
1175 (erc-delete-default-channel chnl buffer)
1176 (erc-update-mode-line buffer)
1177 (when erc-kill-buffer-on-part
1178 (kill-buffer buffer))))))
1179
1180 (define-erc-response-handler (PING)
1181 "Handle ping messages." nil
1182 (let ((pinger (first (erc-response.command-args parsed))))
1183 (erc-log (format "PING: %s" pinger))
1184 ;; ping response to the server MUST be forced, or you can lose big
1185 (erc-server-send (format "PONG :%s" pinger) t)
1186 (when erc-verbose-server-ping
1187 (erc-display-message
1188 parsed 'error proc
1189 'PING ?s (erc-time-diff erc-server-last-ping-time (erc-current-time))))
1190 (setq erc-server-last-ping-time (erc-current-time))))
1191
1192 (define-erc-response-handler (PONG)
1193 "Handle pong messages." nil
1194 (let ((time (string-to-number (erc-response.contents parsed))))
1195 (when (> time 0)
1196 (setq erc-server-lag (erc-time-diff time (erc-current-time)))
1197 (when erc-verbose-server-ping
1198 (erc-display-message
1199 parsed 'notice proc 'PONG
1200 ?h (first (erc-response.command-args parsed)) ?i erc-server-lag
1201 ?s (if (/= erc-server-lag 1) "s" "")))
1202 (erc-update-mode-line))))
1203
1204 (define-erc-response-handler (PRIVMSG NOTICE)
1205 nil nil
1206 (let ((sender-spec (erc-response.sender parsed))
1207 (cmd (erc-response.command parsed))
1208 (tgt (car (erc-response.command-args parsed)))
1209 (msg (erc-response.contents parsed)))
1210 (if (or (erc-ignored-user-p sender-spec)
1211 (erc-ignored-reply-p msg tgt proc))
1212 (when erc-minibuffer-ignored
1213 (message "Ignored %s from %s to %s" cmd sender-spec tgt))
1214 (let* ((sndr (erc-parse-user sender-spec))
1215 (nick (nth 0 sndr))
1216 (login (nth 1 sndr))
1217 (host (nth 2 sndr))
1218 (msgp (string= cmd "PRIVMSG"))
1219 (noticep (string= cmd "NOTICE"))
1220 ;; S.B. downcase *both* tgt and current nick
1221 (privp (erc-current-nick-p tgt))
1222 s buffer
1223 fnick)
1224 (setf (erc-response.contents parsed) msg)
1225 (setq buffer (erc-get-buffer (if privp nick tgt) proc))
1226 (when buffer
1227 (with-current-buffer buffer
1228 ;; update the chat partner info. Add to the list if private
1229 ;; message. We will accumulate private identities indefinitely
1230 ;; at this point.
1231 (erc-update-channel-member (if privp nick tgt) nick nick
1232 privp nil nil host login nil nil t)
1233 (let ((cdata (erc-get-channel-user nick)))
1234 (setq fnick (funcall erc-format-nick-function
1235 (car cdata) (cdr cdata))))))
1236 (cond
1237 ((erc-is-message-ctcp-p msg)
1238 (setq s (if msgp
1239 (erc-process-ctcp-query proc parsed nick login host)
1240 (erc-process-ctcp-reply proc parsed nick login host
1241 (match-string 1 msg)))))
1242 (t
1243 (setcar erc-server-last-peers nick)
1244 (setq s (erc-format-privmessage
1245 (or fnick nick) msg
1246 ;; If buffer is a query buffer,
1247 ;; format the nick as for a channel.
1248 (and (not (and buffer
1249 (erc-query-buffer-p buffer)
1250 erc-format-query-as-channel-p))
1251 privp)
1252 msgp))))
1253 (when s
1254 (if (and noticep privp)
1255 (progn
1256 (run-hook-with-args 'erc-echo-notice-always-hook
1257 s parsed buffer nick)
1258 (run-hook-with-args-until-success
1259 'erc-echo-notice-hook s parsed buffer nick))
1260 (erc-display-message parsed nil buffer s)))
1261 (when (string= cmd "PRIVMSG")
1262 (erc-auto-query proc parsed))))))
1263
1264 ;; FIXME: need clean way of specifiying extra hooks in
1265 ;; define-erc-response-handler.
1266 (add-hook 'erc-server-PRIVMSG-functions 'erc-auto-query)
1267
1268 (define-erc-response-handler (QUIT)
1269 nil nil
1270 (let ((reason (erc-response.contents parsed))
1271 bufs)
1272 (multiple-value-bind (nick login host)
1273 (erc-parse-user (erc-response.sender parsed))
1274 (setq bufs (erc-buffer-list-with-nick nick proc))
1275 (erc-remove-user nick)
1276 (setq reason (erc-wash-quit-reason reason nick login host))
1277 (erc-display-message parsed 'notice bufs
1278 'QUIT ?n nick ?u login
1279 ?h host ?r reason))))
1280
1281 (define-erc-response-handler (TOPIC)
1282 nil nil
1283 (let* ((ch (first (erc-response.command-args parsed)))
1284 (topic (erc-trim-string (erc-response.contents parsed)))
1285 (time (format-time-string "%T %m/%d/%y" (current-time))))
1286 (multiple-value-bind (nick login host)
1287 (erc-parse-user (erc-response.sender parsed))
1288 (erc-update-channel-member ch nick nick nil nil nil host login)
1289 (erc-update-channel-topic ch (format "%s\C-o (%s, %s)" topic nick time))
1290 (erc-display-message parsed 'notice (erc-get-buffer ch proc)
1291 'TOPIC ?n nick ?u login ?h host
1292 ?c ch ?T topic))))
1293
1294 (define-erc-response-handler (WALLOPS)
1295 nil nil
1296 (let ((message (erc-response.contents parsed)))
1297 (multiple-value-bind (nick login host)
1298 (erc-parse-user (erc-response.sender parsed))
1299 (erc-display-message
1300 parsed 'notice nil
1301 'WALLOPS ?n nick ?m message))))
1302
1303 (define-erc-response-handler (001)
1304 "Set `erc-server-current-nick' to reflect server settings and display the welcome message."
1305 nil
1306 (erc-set-current-nick (first (erc-response.command-args parsed)))
1307 (erc-update-mode-line) ; needed here?
1308 (setq erc-nick-change-attempt-count 0)
1309 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
1310 (erc-display-message
1311 parsed 'notice 'active (erc-response.contents parsed)))
1312
1313 (define-erc-response-handler (MOTD 002 003 371 372 374 375)
1314 "Display the server's message of the day." nil
1315 (erc-handle-login)
1316 (erc-display-message
1317 parsed 'notice (if erc-server-connected 'active proc)
1318 (erc-response.contents parsed)))
1319
1320 (define-erc-response-handler (376 422)
1321 nil nil
1322 (erc-server-MOTD proc parsed)
1323 (erc-connection-established proc parsed))
1324
1325 (define-erc-response-handler (004)
1326 nil nil
1327 (multiple-value-bind (server-name server-version)
1328 (cdr (erc-response.command-args parsed))
1329 (setq erc-server-version server-version)
1330 (setq erc-server-announced-name server-name)
1331 (erc-update-mode-line-buffer (process-buffer proc))
1332 (erc-display-message
1333 parsed 'notice proc
1334 's004 ?s server-name ?v server-version
1335 ?U (fourth (erc-response.command-args parsed))
1336 ?C (fifth (erc-response.command-args parsed)))))
1337
1338 (define-erc-response-handler (005)
1339 "Set the variable `erc-server-parameters' and display the received message.
1340
1341 According to RFC 2812, suggests alternate servers on the network.
1342 Many servers, however, use this code to show which parameters they have set,
1343 for example, the network identifier, maximum allowed topic length, whether
1344 certain commands are accepted and more. See documentation for
1345 `erc-server-parameters' for more information on the parameters sent.
1346
1347 A server may send more than one 005 message."
1348 nil
1349 (let ((line (mapconcat 'identity
1350 (setf (erc-response.command-args parsed)
1351 (cdr (erc-response.command-args parsed)))
1352 " ")))
1353 (while (erc-response.command-args parsed)
1354 (let ((section (pop (erc-response.command-args parsed))))
1355 ;; fill erc-server-parameters
1356 (when (string-match "^\\([A-Z]+\\)\=\\(.*\\)$\\|^\\([A-Z]+\\)$"
1357 section)
1358 (add-to-list 'erc-server-parameters
1359 `(,(or (match-string 1 section)
1360 (match-string 3 section))
1361 .
1362 ,(match-string 2 section))))))
1363 (erc-display-message parsed 'notice proc line)))
1364
1365 (define-erc-response-handler (221)
1366 nil nil
1367 (let* ((nick (first (erc-response.command-args parsed)))
1368 (modes (mapconcat 'identity
1369 (cdr (erc-response.command-args parsed)) " ")))
1370 (erc-set-modes nick modes)
1371 (erc-display-message parsed 'notice 'active 's221 ?n nick ?m modes)))
1372
1373 (define-erc-response-handler (252)
1374 "Display the number of IRC operators online." nil
1375 (erc-display-message parsed 'notice 'active 's252
1376 ?i (second (erc-response.command-args parsed))))
1377
1378 (define-erc-response-handler (253)
1379 "Display the number of unknown connections." nil
1380 (erc-display-message parsed 'notice 'active 's253
1381 ?i (second (erc-response.command-args parsed))))
1382
1383 (define-erc-response-handler (254)
1384 "Display the number of channels formed." nil
1385 (erc-display-message parsed 'notice 'active 's254
1386 ?i (second (erc-response.command-args parsed))))
1387
1388 (define-erc-response-handler (250 251 255 256 257 258 259 265 266 377 378)
1389 "Generic display of server messages as notices.
1390
1391 See `erc-display-server-message'." nil
1392 (erc-display-server-message proc parsed))
1393
1394 (define-erc-response-handler (301)
1395 "AWAY notice." nil
1396 (erc-display-message parsed 'notice 'active 's301
1397 ?n (second (erc-response.command-args parsed))
1398 ?r (erc-response.contents parsed)))
1399
1400 (define-erc-response-handler (303)
1401 "ISON reply" nil
1402 (erc-display-message parsed 'notice 'active 's303
1403 ?n (second (erc-response.command-args parsed))))
1404
1405 (define-erc-response-handler (305)
1406 "Return from AWAYness." nil
1407 (erc-process-away proc nil)
1408 (erc-display-message parsed 'notice 'active
1409 's305 ?m (erc-response.contents parsed)))
1410
1411 (define-erc-response-handler (306)
1412 "Set AWAYness." nil
1413 (erc-process-away proc t)
1414 (erc-display-message parsed 'notice 'active
1415 's306 ?m (erc-response.contents parsed)))
1416
1417 (define-erc-response-handler (311 314)
1418 "WHOIS/WHOWAS notices." nil
1419 (let ((fname (erc-response.contents parsed))
1420 (catalog-entry (intern (format "s%s" (erc-response.command parsed)))))
1421 (multiple-value-bind (nick user host)
1422 (cdr (erc-response.command-args parsed))
1423 (erc-update-user-nick nick nick host nil fname user)
1424 (erc-display-message
1425 parsed 'notice 'active catalog-entry
1426 ?n nick ?f fname ?u user ?h host))))
1427
1428 (define-erc-response-handler (312)
1429 nil nil
1430 (multiple-value-bind (nick server-host)
1431 (cdr (erc-response.command-args parsed))
1432 (erc-display-message
1433 parsed 'notice 'active 's312
1434 ?n nick ?s server-host ?c (erc-response.contents parsed))))
1435
1436 (define-erc-response-handler (313)
1437 "IRC Operator response in WHOIS." nil
1438 (erc-display-message
1439 parsed 'notice 'active 's313
1440 ?n (second (erc-response.command-args parsed))))
1441
1442 (define-erc-response-handler (315 318 323 369)
1443 ;; 315 - End of WHO
1444 ;; 318 - End of WHOIS list
1445 ;; 323 - End of channel LIST
1446 ;; 369 - End of WHOWAS
1447 nil nil
1448 (ignore proc parsed))
1449
1450 (define-erc-response-handler (317)
1451 "IDLE notice." nil
1452 (multiple-value-bind (nick seconds-idle on-since time)
1453 (cdr (erc-response.command-args parsed))
1454 (setq time (when on-since
1455 (format-time-string "%T %Y/%m/%d"
1456 (erc-string-to-emacs-time on-since))))
1457 (erc-update-user-nick nick nick nil nil nil
1458 (and time (format "on since %s" time)))
1459 (if time
1460 (erc-display-message
1461 parsed 'notice 'active 's317-on-since
1462 ?n nick ?i (erc-sec-to-time (string-to-number seconds-idle)) ?t time)
1463 (erc-display-message
1464 parsed 'notice 'active 's317
1465 ?n nick ?i (erc-sec-to-time (string-to-number seconds-idle))))))
1466
1467 (define-erc-response-handler (319)
1468 nil nil
1469 (erc-display-message
1470 parsed 'notice 'active 's319
1471 ?n (second (erc-response.command-args parsed))
1472 ?c (erc-response.contents parsed)))
1473
1474 (define-erc-response-handler (320)
1475 "Identified user in WHOIS." nil
1476 (erc-display-message
1477 parsed 'notice 'active 's320
1478 ?n (second (erc-response.command-args parsed))))
1479
1480 (define-erc-response-handler (321)
1481 "LIST header." nil
1482 (setq erc-channel-list nil)
1483 (erc-display-message parsed 'notice 'active 's321))
1484
1485 (define-erc-response-handler (322)
1486 "LIST notice." nil
1487 (let ((topic (erc-response.contents parsed)))
1488 (multiple-value-bind (channel num-users)
1489 (cdr (erc-response.command-args parsed))
1490 (add-to-list 'erc-channel-list (list channel))
1491 (erc-update-channel-topic channel topic)
1492 (erc-display-message
1493 parsed 'notice 'active 's322
1494 ?c channel ?u num-users ?t (or topic "")))))
1495
1496 (define-erc-response-handler (324)
1497 "Channel or nick modes." nil
1498 (let ((channel (second (erc-response.command-args parsed)))
1499 (modes (mapconcat 'identity (cddr (erc-response.command-args parsed))
1500 " ")))
1501 (erc-set-modes channel modes)
1502 (erc-display-message
1503 parsed 'notice (erc-get-buffer channel proc)
1504 's324 ?c channel ?m modes)))
1505
1506 (define-erc-response-handler (329)
1507 "Channel creation date." nil
1508 (let ((channel (second (erc-response.command-args parsed)))
1509 (time (erc-string-to-emacs-time
1510 (third (erc-response.command-args parsed)))))
1511 (erc-display-message
1512 parsed 'notice (erc-get-buffer channel proc)
1513 's329 ?c channel ?t (format-time-string "%A %Y/%m/%d %X" time))))
1514
1515 (define-erc-response-handler (330)
1516 nil nil
1517 ;; FIXME: I don't know what the magic numbers mean. Mummy, make
1518 ;; the magic numbers go away.
1519 ;; No seriously, I have no clue about the format of this command,
1520 ;; and don't sit on Quakenet, so can't test. Originally we had:
1521 ;; nick == (aref parsed 3)
1522 ;; authaccount == (aref parsed 4)
1523 ;; authmsg == (aref parsed 5)
1524 ;; The guesses below are, well, just that. -- Lawrence 2004/05/10
1525 (let ((nick (second (erc-response.command-args parsed)))
1526 (authaccount (third (erc-response.command-args parsed)))
1527 (authmsg (erc-response.contents parsed)))
1528 (erc-display-message parsed 'notice 'active 's330
1529 ?n nick ?a authmsg ?i authaccount)))
1530
1531 (define-erc-response-handler (331)
1532 "Channel topic." nil
1533 (let ((channel (second (erc-response.command-args parsed)))
1534 (topic (erc-response.contents parsed)))
1535 ;; FIXME: why don't we do anything with the topic? -- Lawrence 2004/05/10
1536 (erc-display-message parsed 'notice (erc-get-buffer channel proc)
1537 's331 ?c channel)))
1538
1539 (define-erc-response-handler (332)
1540 "TOPIC notice." nil
1541 (let ((channel (second (erc-response.command-args parsed)))
1542 (topic (erc-response.contents parsed)))
1543 (erc-update-channel-topic channel topic)
1544 (erc-display-message parsed 'notice (erc-get-buffer channel proc)
1545 's332 ?c channel ?T topic)))
1546
1547 (define-erc-response-handler (333)
1548 ;; Who set the topic, and when
1549 nil nil
1550 (multiple-value-bind (channel nick time)
1551 (cdr (erc-response.command-args parsed))
1552 (setq time (format-time-string "%T %Y/%m/%d"
1553 (erc-string-to-emacs-time time)))
1554 (erc-update-channel-topic channel
1555 (format "\C-o (%s, %s)" nick time)
1556 'append)
1557 (erc-display-message parsed 'notice (erc-get-buffer channel proc)
1558 's333 ?c channel ?n nick ?t time)))
1559
1560 (define-erc-response-handler (341)
1561 "Let user know when an INVITE attempt has been sent successfully."
1562 nil
1563 (multiple-value-bind (nick channel)
1564 (cdr (erc-response.command-args parsed))
1565 (erc-display-message parsed 'notice (erc-get-buffer channel proc)
1566 's341 ?n nick ?c channel)))
1567
1568 (define-erc-response-handler (352)
1569 "WHO notice." nil
1570 (multiple-value-bind (channel user host server nick away-flag)
1571 (cdr (erc-response.command-args parsed))
1572 (let ((full-name (erc-response.contents parsed))
1573 hopcount)
1574 (when (string-match "\\(^[0-9]+ \\)\\(.*\\)$" full-name)
1575 (setq hopcount (match-string 1 full-name))
1576 (setq full-name (match-string 2 full-name)))
1577 (erc-update-channel-member channel nick nick nil nil nil host
1578 user full-name)
1579 (erc-display-message parsed 'notice 'active 's352
1580 ?c channel ?n nick ?a away-flag
1581 ?u user ?h host ?f full-name))))
1582
1583 (define-erc-response-handler (353)
1584 "NAMES notice." nil
1585 (let ((channel (third (erc-response.command-args parsed)))
1586 (users (erc-response.contents parsed)))
1587 (erc-display-message parsed 'notice (or (erc-get-buffer channel proc)
1588 'active)
1589 's353 ?c channel ?u users)
1590 (erc-with-buffer (channel proc)
1591 (erc-channel-receive-names users))))
1592
1593 (define-erc-response-handler (366)
1594 "End of NAMES." nil
1595 (erc-with-buffer ((second (erc-response.command-args parsed)) proc)
1596 (erc-channel-end-receiving-names)))
1597
1598 (define-erc-response-handler (367)
1599 "Channel ban list entries" nil
1600 (multiple-value-bind (channel banmask setter time)
1601 (cdr (erc-response.command-args parsed))
1602 ;; setter and time are not standard
1603 (if setter
1604 (erc-display-message parsed 'notice 'active 's367-set-by
1605 ?c channel
1606 ?b banmask
1607 ?s setter
1608 ?t (or time ""))
1609 (erc-display-message parsed 'notice 'active 's367
1610 ?c channel
1611 ?b banmask))))
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