]> code.delx.au - gnu-emacs/blob - lisp/erc/erc.el
Merged from
[gnu-emacs] / lisp / erc / erc.el
1 ;; erc.el --- An Emacs Internet Relay Chat client
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
5 ;; Copyright (C) 2004 Brian Palmer
6
7 ;; Author: Alexander L. Belikoff (alexander@belikoff.net)
8 ;; Contributors: Sergey Berezin (sergey.berezin@cs.cmu.edu),
9 ;; Mario Lang (mlang@delysid.org),
10 ;; Alex Schroeder (alex@gnu.org)
11 ;; Andreas Fuchs (afs@void.at)
12 ;; Gergely Nagy (algernon@midgard.debian.net)
13 ;; David Edmondson (dme@dme.org)
14 ;; Maintainer: Mario Lang (mlang@delysid.org)
15 ;; Keywords: IRC, chat, client, Internet
16
17 ;; This file is part of GNU Emacs.
18
19 ;; GNU Emacs is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation; either version 2, or (at your option)
22 ;; any later version.
23
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
28
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs; see the file COPYING. If not, write to the
31 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 ;; Boston, MA 02110-1301, USA.
33
34 ;;; Commentary:
35
36 ;; ERC is an IRC client for Emacs.
37
38 ;; For more information, see the following URLs:
39 ;; * http://sf.net/projects/erc/
40 ;; * http://www.emacswiki.org/cgi-bin/wiki.pl?EmacsIRCClient
41
42 ;; Jul-26-2001. erc.el is now in CVS on SourceForge. I invite everyone
43 ;; who wants to hack it to contact me <mlang@delysid.org> in order to
44 ;; get write access on the CVS.
45
46 ;; Installation:
47
48 ;; Put erc.el in your load-path, and put (require 'erc) in your .emacs.
49
50 ;; Configuration:
51
52 ;; Use M-x customize-group RET erc RET to get an overview
53 ;; of all the variables you can tweak.
54
55 ;; Usage:
56
57 ;; To connect to an IRC server, do
58 ;;
59 ;; M-x erc-select RET
60 ;;
61 ;; After you are connected to a server, you can use C-h m or have a look at
62 ;; the IRC menu.
63
64 ;;; History:
65 ;;
66
67 ;;; Code:
68
69 (defconst erc-version-string "Version 5.1 (Emacs 22)"
70 "ERC version. This is used by function `erc-version'.")
71
72 (eval-when-compile (require 'cl))
73 (require 'font-lock)
74 (require 'pp)
75 (require 'thingatpt)
76 (require 'erc-compat)
77 (require 'erc-menu)
78
79 (defvar erc-official-location
80 "http://erc.sf.net (comments mailto://mlang@delysid.org)"
81 "Location of the ERC client on the Internet.")
82
83 (defgroup erc nil
84 "Emacs Internet Relay Chat client."
85 :link '(url-link "http://www.emacswiki.org/cgi-bin/wiki.pl?EmacsIRCClient")
86 :prefix "erc-"
87 :group 'applications)
88
89 (defgroup erc-buffers nil
90 "Creating new ERC buffers"
91 :group 'erc)
92
93 (defgroup erc-display nil
94 "Settings for how various things are displayed"
95 :group 'erc)
96
97 (defgroup erc-mode-line-and-header nil
98 "Displaying information in the mode-line and header"
99 :group 'erc-display)
100
101 (defgroup erc-ignore nil
102 "Ignoring certain messages"
103 :group 'erc)
104
105 (defgroup erc-query nil
106 "Using separate buffers for private discussions"
107 :group 'erc)
108
109 (defgroup erc-quit-and-part nil
110 "Quitting and parting channels"
111 :group 'erc)
112
113 (defgroup erc-paranoia nil
114 "Know what is sent and received; control the display of sensitive data."
115 :group 'erc)
116
117 (defgroup erc-scripts nil
118 "Running scripts at startup and with /LOAD"
119 :group 'erc)
120
121 (require 'erc-backend)
122
123 ;; compatibility with older ERC releases
124
125 (if (fboundp 'defvaralias)
126 (progn
127 (defvaralias 'erc-announced-server-name 'erc-server-announced-name)
128 (erc-make-obsolete-variable 'erc-announced-server-name
129 'erc-server-announced-name
130 "ERC 5.1")
131 (defvaralias 'erc-process 'erc-server-process)
132 (erc-make-obsolete-variable 'erc-process 'erc-server-process "ERC 5.1")
133 (defvaralias 'erc-default-coding-system 'erc-server-coding-system)
134 (erc-make-obsolete-variable 'erc-default-coding-system
135 'erc-server-coding-system
136 "ERC 5.1"))
137 (message (concat "ERC: The function `defvaralias' is not bound. See the "
138 "NEWS file for variable name changes since ERC 5.0.4.")))
139
140 (defalias 'erc-send-command 'erc-server-send)
141 (erc-make-obsolete 'erc-send-command 'erc-server-send "ERC 5.1")
142
143 ;; tunable connection and authentication parameters
144
145 (defcustom erc-server nil
146 "IRC server to use.
147 See function `erc-compute-server' for more details on connection
148 parameters and authentication."
149 :group 'erc
150 :type '(choice (const nil) string))
151
152 (defcustom erc-port nil
153 "IRC port to use."
154 :group 'erc
155 :type '(choice (const nil) number string))
156
157 (defcustom erc-nick nil
158 "Nickname to use.
159
160 Can be either a string, or a list of strings.
161 In the latter case, if the first nick in the list is already in use,
162 other nicks are tried in the list order.
163
164 See function `erc-compute-nick' for more details on connection
165 parameters and authentication."
166 :group 'erc
167 :type '(choice (const nil)
168 (string :tag "Nickname")
169 (repeat string)))
170
171 (defcustom erc-nick-uniquifier "`"
172 "The character to append to the nick if it is already in use."
173 :group 'erc
174 :type 'string)
175
176 (defcustom erc-manual-set-nick-on-bad-nick-p nil
177 "If the nickname you chose isn't available, ERC should not automatically
178 attempt to set another nickname. You can manually set another nickname with
179 the /NICK command."
180 :group 'erc
181 :type 'boolean)
182
183 (defcustom erc-user-full-name nil
184 "User full name.
185
186 See function `erc-compute-full-name' for more details on connection
187 parameters and authentication."
188 :group 'erc
189 :type '(choice (const nil) string function)
190 :set (lambda (sym val)
191 (if (functionp val)
192 (set sym (funcall val))
193 (set sym val))))
194
195 (defvar erc-password nil
196 "ERC password to use in authentication (not necessary).")
197
198 (defcustom erc-user-mode nil
199 "Initial user modes to be set after a connection is established."
200 :group 'erc
201 :type '(choice (const nil) string function))
202
203
204 (defcustom erc-prompt-for-password t
205 "Asks before using the default password, or whether to enter a new one."
206 :group 'erc
207 :type 'boolean)
208
209 (defcustom erc-warn-about-blank-lines t
210 "Warn the user if they attempt to send a blank line."
211 :group 'erc
212 :type 'boolean)
213
214 (defcustom erc-send-whitespace-lines nil
215 "If set to non-nil, send lines consisting of only whitespace."
216 :group 'erc
217 :type 'boolean)
218
219 (defcustom erc-hide-prompt nil
220 "If non-nil, do not display the prompt for commands.
221
222 \(A command is any input starting with a '/').
223
224 See also the variables `erc-prompt' and `erc-command-indicator'."
225 :group 'erc-display
226 :type 'boolean)
227
228 ;; tunable GUI stuff
229
230 (defcustom erc-show-my-nick t
231 "If non-nil, display one's own nickname when sending a message.
232
233 If non-nil, \"<nickname>\" will be shown.
234 If nil, only \"> \" will be shown."
235 :group 'erc-display
236 :type 'boolean)
237
238 (define-widget 'erc-message-type 'set
239 "A set of standard IRC Message types."
240 :args '((const "JOIN")
241 (const "KICK")
242 (const "NICK")
243 (const "PART")
244 (const "QUIT")
245 (const "MODE")
246 (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))
247
248 (defcustom erc-hide-list nil
249 "*List of IRC type messages to hide.
250 A typical value would be '(\"JOIN\" \"PART\" \"QUIT\")."
251 :group 'erc-ignore
252 :type 'erc-message-type)
253
254 (defvar erc-session-password nil
255 "The password used for the current session.")
256 (make-variable-buffer-local 'erc-session-password)
257
258 (defcustom erc-disconnected-hook nil
259 "Run this hook with arguments (NICK IP REASON) when disconnected.
260 This happens before automatic reconnection. Note, that
261 `erc-server-QUIT-functions' might not be run when we disconnect,
262 simply because we do not necessarily receive the QUIT event."
263 :group 'erc-hooks
264 :type 'hook)
265
266 (defcustom erc-complete-functions nil
267 "These functions get called when the user hits TAB in ERC.
268 Each function in turn is called until one returns non-nil to
269 indicate it has handled the input."
270 :group 'erc-hooks
271 :type 'hook)
272
273 (defcustom erc-join-hook nil
274 "Hook run when we join a channel. Hook functions are called
275 without arguments, with the current buffer set to the buffer of
276 the new channel.
277
278 See also `erc-server-JOIN-functions', `erc-part-hook'."
279 :group 'erc-hooks
280 :type 'hook)
281
282 (defcustom erc-quit-hook nil
283 "Hook run when processing a quit command directed at our nick.
284
285 The hook receives one argument, the current PROCESS.
286 See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
287 :group 'erc-hooks
288 :type 'hook)
289
290 (defcustom erc-part-hook nil
291 "Hook run when processing a PART message directed at our nick.
292
293 The hook receives one argument, the current BUFFER.
294 See also `erc-server-QUIT-functions', `erc-quit-hook' and
295 `erc-disconnected-hook'."
296 :group 'erc-hooks
297 :type 'hook)
298
299 (defcustom erc-kick-hook nil
300 "Hook run when processing a KICK message directed at our nick.
301
302 The hook receives one argument, the current BUFFER.
303 See also `erc-server-PART-functions' and `erc-part-hook'."
304 :group 'erc-hooks
305 :type 'hook)
306
307 (defcustom erc-nick-changed-functions nil
308 "List of functions run when your nick was successfully changed.
309
310 Each function should accept two arguments, NEW-NICK and OLD-NICK."
311 :group 'erc-hooks
312 :type 'hook)
313
314 (defcustom erc-connect-pre-hook '(erc-initialize-log-marker)
315 "Hook called just before `erc' calls `erc-connect'.
316 Functions are run in the buffer-to-be."
317 :group 'erc-hooks
318 :type 'hook)
319
320
321 (defvar erc-channel-users nil
322 "A hash table of members in the current channel, which
323 associates nicknames with cons cells of the form:
324 \(USER . MEMBER-DATA) where USER is a pointer to an
325 erc-server-user struct, and MEMBER-DATA is a pointer to an
326 erc-channel-user struct.")
327 (make-variable-buffer-local 'erc-channel-users)
328
329 (defvar erc-server-users nil
330 "A hash table of users on the current server, which associates
331 nicknames with erc-server-user struct instances.")
332 (make-variable-buffer-local 'erc-server-users)
333
334 (defun erc-downcase (string)
335 "Convert STRING to IRC standard conforming downcase."
336 (let ((s (downcase string))
337 (c '((?\[ . ?\{)
338 (?\] . ?\})
339 (?\\ . ?\|)
340 (?~ . ?^))))
341 (save-match-data
342 (while (string-match "[]\\[~]" s)
343 (aset s (match-beginning 0)
344 (cdr (assq (aref s (match-beginning 0)) c)))))
345 s))
346
347 (defstruct (erc-server-user (:type vector) :named)
348 ;; User data
349 nickname host login full-name info
350 ;; Buffers
351 ;;
352 ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
353 ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
354 (buffers nil)
355 )
356
357 (defstruct (erc-channel-user (:type vector) :named)
358 op voice
359 ;; Last message time (in the form of the return value of
360 ;; (current-time)
361 ;;
362 ;; This is useful for ordered name completion.
363 (last-message-time nil))
364
365 (defsubst erc-get-channel-user (nick)
366 "Finds the (USER . CHANNEL-DATA) element corresponding to NICK
367 in the current buffer's `erc-channel-users' hash table."
368 (gethash (erc-downcase nick) erc-channel-users))
369
370 (defsubst erc-get-server-user (nick)
371 "Finds the USER corresponding to NICK in the current server's
372 `erc-server-users' hash table."
373 (with-current-buffer (process-buffer erc-server-process)
374 (gethash (erc-downcase nick) erc-server-users)))
375
376 (defsubst erc-add-server-user (nick user)
377 "This function is for internal use only.
378
379 Adds USER with nickname NICK to the `erc-server-users' hash table."
380 (with-current-buffer (process-buffer erc-server-process)
381 (puthash (erc-downcase nick) user erc-server-users)))
382
383 (defsubst erc-remove-server-user (nick)
384 "This function is for internal use only.
385
386 Removes the user with nickname NICK from the `erc-server-users'
387 hash table. This user is not removed from the
388 `erc-channel-users' lists of other buffers.
389
390 See also: `erc-remove-user'."
391 (with-current-buffer (process-buffer erc-server-process)
392 (remhash (erc-downcase nick) erc-server-users)))
393
394 (defun erc-change-user-nickname (user new-nick)
395 "This function is for internal use only.
396
397 Changes the nickname of USER to NEW-NICK in the
398 `erc-server-users' hash table. The `erc-channel-users' lists of
399 other buffers are also changed."
400 (let ((nick (erc-server-user-nickname user)))
401 (setf (erc-server-user-nickname user) new-nick)
402 (with-current-buffer (process-buffer erc-server-process)
403 (remhash (erc-downcase nick) erc-server-users)
404 (puthash (erc-downcase new-nick) user erc-server-users))
405 (dolist (buf (erc-server-user-buffers user))
406 (if (buffer-live-p buf)
407 (with-current-buffer buf
408 (let ((cdata (erc-get-channel-user nick)))
409 (remhash (erc-downcase nick) erc-channel-users)
410 (puthash (erc-downcase new-nick) cdata
411 erc-channel-users)))))))
412
413 (defun erc-remove-channel-user (nick)
414 "This function is for internal use only.
415
416 Removes the user with nickname NICK from the `erc-channel-users'
417 list for this channel. If this user is not in the
418 `erc-channel-users' list of any other buffers, the user is also
419 removed from the server's `erc-server-users' list.
420
421 See also: `erc-remove-server-user' and `erc-remove-user'."
422 (let ((channel-data (erc-get-channel-user nick)))
423 (when channel-data
424 (let ((user (car channel-data)))
425 (setf (erc-server-user-buffers user)
426 (delq (current-buffer)
427 (erc-server-user-buffers user)))
428 (remhash (erc-downcase nick) erc-channel-users)
429 (if (null (erc-server-user-buffers user))
430 (erc-remove-server-user nick))))))
431
432 (defun erc-remove-user (nick)
433 "This function is for internal use only.
434
435 Removes the user with nickname NICK from the `erc-server-users'
436 list as well as from all `erc-channel-users' lists.
437
438 See also: `erc-remove-server-user' and
439 `erc-remove-channel-user'."
440 (let ((user (erc-get-server-user nick)))
441 (when user
442 (let ((buffers (erc-server-user-buffers user)))
443 (dolist (buf buffers)
444 (if (buffer-live-p buf)
445 (with-current-buffer buf
446 (remhash (erc-downcase nick) erc-channel-users)
447 (run-hooks 'erc-channel-members-changed-hook)))))
448 (erc-remove-server-user nick))))
449
450 (defun erc-remove-channel-users ()
451 "This function is for internal use only.
452
453 Removes all users in the current channel. This is called by
454 `erc-server-PART' and `erc-server-QUIT'."
455 (when (and erc-server-connected
456 (erc-server-process-alive)
457 (hash-table-p erc-channel-users))
458 (maphash (lambda (nick cdata)
459 (erc-remove-channel-user nick))
460 erc-channel-users)
461 (clrhash erc-channel-users)))
462
463 (defsubst erc-channel-user-op-p (nick)
464 "Return `t' if NICK is an operator in the current channel."
465 (and nick
466 (hash-table-p erc-channel-users)
467 (let ((cdata (erc-get-channel-user nick)))
468 (and cdata (cdr cdata)
469 (erc-channel-user-op (cdr cdata))))))
470
471 (defsubst erc-channel-user-voice-p (nick)
472 "Return `t' if NICK has voice in the current channel."
473 (and nick
474 (hash-table-p erc-channel-users)
475 (let ((cdata (erc-get-channel-user nick)))
476 (and cdata (cdr cdata)
477 (erc-channel-user-voice (cdr cdata))))))
478
479 (defun erc-get-channel-user-list ()
480 "Returns a list of users in the current channel. Each element
481 of the list is of the form (USER . CHANNEL-DATA), where USER is
482 an erc-server-user struct, and CHANNEL-DATA is either `nil' or an
483 erc-channel-user struct.
484
485 See also: `erc-sort-channel-users-by-activity'"
486 (let (users)
487 (if (hash-table-p erc-channel-users)
488 (maphash (lambda (nick cdata)
489 (setq users (cons cdata users)))
490 erc-channel-users))
491 users))
492
493 (defun erc-get-server-nickname-list ()
494 "Returns a list of known nicknames on the current server."
495 (if (erc-server-process-alive)
496 (with-current-buffer (erc-server-buffer)
497 (let (nicks)
498 (when (hash-table-p erc-server-users)
499 (maphash (lambda (n user)
500 (setq nicks
501 (cons (erc-server-user-nickname user)
502 nicks)))
503 erc-server-users)
504 nicks)))))
505
506 (defun erc-get-channel-nickname-list ()
507 "Returns a list of known nicknames on the current channel."
508 (let (nicks)
509 (when (hash-table-p erc-channel-users)
510 (maphash (lambda (n cdata)
511 (setq nicks
512 (cons (erc-server-user-nickname (car cdata))
513 nicks)))
514 erc-channel-users)
515 nicks)))
516
517 (defun erc-get-server-nickname-alist ()
518 "Returns an alist of known nicknames on the current server."
519 (if (erc-server-process-alive)
520 (with-current-buffer (erc-server-buffer)
521 (let (nicks)
522 (when (hash-table-p erc-server-users)
523 (maphash (lambda (n user)
524 (setq nicks
525 (cons (cons (erc-server-user-nickname user) nil)
526 nicks)))
527 erc-server-users)
528 nicks)))))
529
530 (defun erc-get-channel-nickname-alist ()
531 "Returns an alist of known nicknames on the current channel."
532 (let (nicks)
533 (when (hash-table-p erc-channel-users)
534 (maphash (lambda (n cdata)
535 (setq nicks
536 (cons (cons (erc-server-user-nickname (car cdata)) nil)
537 nicks)))
538 erc-channel-users)
539 nicks)))
540
541 (defun erc-sort-channel-users-by-activity (list)
542 "Sorts LIST such that users which have spoken most recently are
543 listed first. LIST must be of the form (USER . CHANNEL-DATA).
544
545 See also: `erc-get-channel-user-list'."
546 (sort list
547 (lambda (x y)
548 (when (and
549 (cdr x) (cdr y))
550 (let ((tx (erc-channel-user-last-message-time (cdr x)))
551 (ty (erc-channel-user-last-message-time (cdr y))))
552 (if tx
553 (if ty
554 (time-less-p ty tx)
555 t)
556 nil))))))
557
558 (defun erc-sort-channel-users-alphabetically (list)
559 "Sort LIST so that users' nicknames are in alphabetical order.
560 LIST must be of the form (USER . CHANNEL-DATA).
561
562 See also: `erc-get-channel-user-list'."
563 (sort list
564 (lambda (x y)
565 (when (and
566 (cdr x) (cdr y))
567 (let ((nickx (downcase (erc-server-user-nickname (car x))))
568 (nicky (downcase (erc-server-user-nickname (car y)))))
569 (if nickx
570 (if nicky
571 (string-lessp nickx nicky)
572 t)
573 nil))))))
574
575 (defvar erc-channel-topic nil
576 "A topic string for the channel. Should only be used in channel-buffers.")
577 (make-variable-buffer-local 'erc-channel-topic)
578
579 (defvar erc-channel-modes nil
580 "List of strings representing channel modes.
581 E.g. '(\"i\" \"m\" \"s\" \"b Quake!*@*\")
582 \(not sure the ban list will be here, but why not)")
583 (make-variable-buffer-local 'erc-channel-modes)
584
585 (defvar erc-insert-marker nil
586 "The place where insertion of new text in erc buffers should happen.")
587 (make-variable-buffer-local 'erc-insert-marker)
588
589 (defvar erc-input-marker nil
590 "The marker where input should be inserted.")
591 (make-variable-buffer-local 'erc-input-marker)
592
593 (defun erc-string-no-properties (string)
594 "Return a copy of STRING will all text-properties removed."
595 (let ((newstring (copy-sequence string)))
596 (set-text-properties 0 (length newstring) nil newstring)
597 newstring))
598
599 (defcustom erc-prompt "ERC>"
600 "Prompt used by ERC. Trailing whitespace is not required."
601 :group 'erc-display
602 :type '(choice string function))
603
604 (defun erc-prompt ()
605 "Return the input prompt as a string.
606
607 See also the variable `erc-prompt'."
608 (let ((prompt (if (functionp erc-prompt)
609 (funcall erc-prompt)
610 erc-prompt)))
611 (if (> (length prompt) 0)
612 (concat prompt " ")
613 prompt)))
614
615 (defcustom erc-command-indicator nil
616 "Indicator used by ERC for showing commands.
617
618 If non-nil, this will be used in the ERC buffer to indicate
619 commands (i.e., input starting with a '/').
620
621 If nil, the prompt will be constructed from the variable `erc-prompt'."
622 :group 'erc-display
623 :type '(choice (const nil) string function))
624
625 (defun erc-command-indicator ()
626 "Return the command indicator prompt as a string.
627
628 This only has any meaning if the variable `erc-command-indicator' is non-nil."
629 (and erc-command-indicator
630 (let ((prompt (if (functionp erc-command-indicator)
631 (funcall erc-command-indicator)
632 erc-command-indicator)))
633 (if (> (length prompt) 0)
634 (concat prompt " ")
635 prompt))))
636
637 (defcustom erc-notice-prefix "*** "
638 "*Prefix for all notices."
639 :group 'erc-display
640 :type 'string)
641
642 (defcustom erc-notice-highlight-type 'all
643 "*Determines how to highlight notices.
644 See `erc-notice-prefix'.
645
646 The following values are allowed:
647
648 'prefix - highlight notice prefix only
649 'all - highlight the entire notice
650
651 Any other value disables notice's highlighting altogether."
652 :group 'erc-display
653 :type '(choice (const :tag "highlight notice prefix only" prefix)
654 (const :tag "highlight the entire notice" all)
655 (const :tag "don't highlight notices at all" nil)))
656
657 (defcustom erc-echo-notice-hook nil
658 "*Specifies a list of functions to call to echo a private
659 notice. Each function is called with four arguments, the string
660 to display, the parsed server message, the target buffer (or
661 nil), and the sender. The functions are called in order, until a
662 function evaluates to non-nil. These hooks are called after
663 those specified in `erc-echo-notice-always-hook'.
664
665 See also: `erc-echo-notice-always-hook',
666 `erc-echo-notice-in-default-buffer',
667 `erc-echo-notice-in-target-buffer',
668 `erc-echo-notice-in-minibuffer',
669 `erc-echo-notice-in-server-buffer',
670 `erc-echo-notice-in-active-non-server-buffer',
671 `erc-echo-notice-in-active-buffer',
672 `erc-echo-notice-in-user-buffers',
673 `erc-echo-notice-in-user-and-target-buffers',
674 `erc-echo-notice-in-first-user-buffer'"
675 :group 'erc-hooks
676 :type 'hook
677 :options '(erc-echo-notice-in-default-buffer
678 erc-echo-notice-in-target-buffer
679 erc-echo-notice-in-minibuffer
680 erc-echo-notice-in-server-buffer
681 erc-echo-notice-in-active-non-server-buffer
682 erc-echo-notice-in-active-buffer
683 erc-echo-notice-in-user-buffers
684 erc-echo-notice-in-user-and-target-buffers
685 erc-echo-notice-in-first-user-buffer))
686
687 (defcustom erc-echo-notice-always-hook
688 '(erc-echo-notice-in-default-buffer)
689 "*Specifies a list of functions to call to echo a private
690 notice. Each function is called with four arguments, the string
691 to display, the parsed server message, the target buffer (or
692 nil), and the sender. The functions are called in order, and all
693 functions are called. These hooks are called before those
694 specified in `erc-echo-notice-hook'.
695
696 See also: `erc-echo-notice-hook',
697 `erc-echo-notice-in-default-buffer',
698 `erc-echo-notice-in-target-buffer',
699 `erc-echo-notice-in-minibuffer',
700 `erc-echo-notice-in-server-buffer',
701 `erc-echo-notice-in-active-non-server-buffer',
702 `erc-echo-notice-in-active-buffer',
703 `erc-echo-notice-in-user-buffers',
704 `erc-echo-notice-in-user-and-target-buffers',
705 `erc-echo-notice-in-first-user-buffer'"
706 :group 'erc-hooks
707 :type 'hook
708 :options '(erc-echo-notice-in-default-buffer
709 erc-echo-notice-in-target-buffer
710 erc-echo-notice-in-minibuffer
711 erc-echo-notice-in-server-buffer
712 erc-echo-notice-in-active-non-server-buffer
713 erc-echo-notice-in-active-buffer
714 erc-echo-notice-in-user-buffers
715 erc-echo-notice-in-user-and-target-buffers
716 erc-echo-notice-in-first-user-buffer))
717
718 ;; other tunable parameters
719
720 (defcustom erc-whowas-on-nosuchnick nil
721 "*If non-nil, do a whowas on a nick if no such nick."
722 :group 'erc
723 :type 'boolean)
724
725 (defcustom erc-verbose-server-ping nil
726 "*If non-nil, show every time you get a PING or PONG from the server."
727 :group 'erc-paranoia
728 :type 'boolean)
729
730 (defcustom erc-public-away-p nil
731 "*Let others know you are back when you are no longer marked away.
732 This happens in this form:
733 * <nick> is back (gone for <time>)
734
735 Many consider it impolite to do so automatically."
736 :group 'erc
737 :type 'boolean)
738
739 (defcustom erc-away-nickname nil
740 "*The nickname to take when you are marked as being away."
741 :group 'erc
742 :type '(choice (const nil)
743 string))
744
745 (defcustom erc-paranoid nil
746 "If non-nil, then all incoming CTCP requests will be shown."
747 :group 'erc-paranoia
748 :type 'boolean)
749
750 (defcustom erc-disable-ctcp-replies nil
751 "Disable replies to CTCP requests that require a reply.
752 If non-nil, then all incoming CTCP requests that normally require
753 an automatic reply (like VERSION or PING) will be ignored. Good to
754 set if some hacker is trying to flood you away."
755 :group 'erc-paranoia
756 :type 'boolean)
757
758 (defcustom erc-anonymous-login t
759 "Be paranoid, don't give away your machine name."
760 :group 'erc-paranoia
761 :type 'boolean)
762
763 (defcustom erc-prompt-for-channel-key nil
764 "Prompt for channel key when using `erc-join-channel' interactively"
765 :group 'erc
766 :type 'boolean)
767
768 (defcustom erc-email-userid "user"
769 "Use this as your email user ID."
770 :group 'erc
771 :type 'string)
772
773 (defcustom erc-ignore-list nil
774 "*List of regexps matching user identifiers to ignore.
775
776 A user identifier has the form \"nick!login@host\". If an
777 identifier matches, the message from the person will not be
778 processed."
779 :group 'erc-ignore
780 :type '(repeat regexp))
781 (make-variable-buffer-local 'erc-ignore-list)
782
783 (defcustom erc-ignore-reply-list nil
784 "*List of regexps matching user identifiers to ignore completely.
785
786 This differs from `erc-ignore-list' in that it also ignores any
787 messages directed at the user.
788
789 A user identifier has the form \"nick!login@host\".
790
791 If an identifier matches, or a message is addressed to a nick
792 whose identifier matches, the message will not be processed.
793
794 CAVEAT: ERC doesn't know about the user and host of anyone who
795 was already in the channel when you joined, but never said
796 anything, so it won't be able to match the user and host of those
797 people. You can update the ERC internal info using /WHO *."
798 :group 'erc-ignore
799 :type '(repeat regexp))
800
801 (defvar erc-flood-protect t
802 "*If non-nil, flood protection is enabled.
803 Flooding is sending too much information to the server in too
804 short of an interval, which may cause the server to terminate the
805 connection.
806
807 See `erc-server-flood-margin' for other flood-related parameters.")
808
809 ;; Script parameters
810
811 (defcustom erc-startup-file-list
812 '("~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
813 "List of files to try for a startup script.
814 The first existent and readable one will get executed.
815
816 If the filename ends with `.el' it is presumed to be an emacs-lisp
817 script and it gets (load)ed. Otherwise is is treated as a bunch of
818 regular IRC commands"
819 :group 'erc-scripts
820 :type '(repeat file))
821
822 (defcustom erc-script-path nil
823 "List of directories to look for a script in /load command.
824 The script is first searched in the current directory, then in each
825 directory in the list."
826 :group 'erc-scripts
827 :type '(repeat directory))
828
829 (defcustom erc-script-echo t
830 "*If not-NIL, echo the IRC script commands locally."
831 :group 'erc-scripts
832 :type 'boolean)
833
834 (defvar erc-last-saved-position nil
835 "A marker containing the position the current buffer was last saved at.")
836 (make-variable-buffer-local 'erc-last-saved-position)
837
838 (defcustom erc-kill-buffer-on-part nil
839 "Kill the channel buffer on PART.
840 This variable should probably stay nil, as ERC can reuse buffers if
841 you rejoin them later."
842 :group 'erc-quit-and-part
843 :type 'boolean)
844
845 (defcustom erc-kill-queries-on-quit nil
846 "Kill all query (also channel) buffers of this server on QUIT.
847 See the variable `erc-kill-buffer-on-part' for details."
848 :group 'erc-quit-and-part
849 :type 'boolean)
850
851 (defcustom erc-kill-server-buffer-on-quit nil
852 "Kill the server buffer of the process on QUIT."
853 :group 'erc-quit-and-part
854 :type 'boolean)
855
856 (defcustom erc-quit-reason-various-alist nil
857 "Alist of possible arguments to the /quit command.
858
859 Each element has the form:
860 (REGEXP RESULT)
861
862 If REGEXP matches the argument to /quit, then its relevant RESULT
863 will be used. RESULT may be either a string, or a function. If
864 a function, it should return the quit message as a string.
865
866 If no elements match, then the empty string is used.
867
868 As an example:
869 (setq erc-quit-reason-various-alist
870 '((\"zippy\" erc-quit-reason-zippy)
871 (\"xmms\" dme:now-playing)
872 (\"version\" erc-quit-reason-normal)
873 (\"home\" \"Gone home !\")
874 (\"\" \"Default Reason\")))
875 If the user types \"/quit zippy\", then a Zippy the Pinhead quotation
876 will be used as the quit message."
877 :group 'erc-quit-and-part
878 :type '(repeat (list regexp (choice (string) (function)))))
879
880 (defcustom erc-part-reason-various-alist nil
881 "Alist of possible arguments to the /part command.
882
883 Each element has the form:
884 (REGEXP RESULT)
885
886 If REGEXP matches the argument to /part, then its relevant RESULT
887 will be used. RESULT may be either a string, or a function. If
888 a function, it should return the part message as a string.
889
890 If no elements match, then the empty string is used.
891
892 As an example:
893 (setq erc-part-reason-various-alist
894 '((\"zippy\" erc-part-reason-zippy)
895 (\"xmms\" dme:now-playing)
896 (\"version\" erc-part-reason-normal)
897 (\"home\" \"Gone home !\")
898 (\"\" \"Default Reason\")))
899 If the user types \"/part zippy\", then a Zippy the Pinhead quotation
900 will be used as the part message."
901 :group 'erc-quit-and-part
902 :type '(repeat (list regexp (choice (string) (function)))))
903
904 (defcustom erc-quit-reason 'erc-quit-reason-normal
905 "*A function which returns the reason for quitting.
906
907 The function is passed a single argument, the string typed by the
908 user after \"/quit\"."
909 :group 'erc-quit-and-part
910 :type '(choice (const erc-quit-reason-normal)
911 (const erc-quit-reason-zippy)
912 (const erc-quit-reason-various)
913 (symbol)))
914
915 (defcustom erc-part-reason 'erc-part-reason-normal
916 "A function which returns the reason for parting a channel.
917
918 The function is passed a single argument, the string typed by the
919 user after \"/PART\"."
920 :group 'erc-quit-and-part
921 :type '(choice (const erc-part-reason-normal)
922 (const erc-part-reason-zippy)
923 (const erc-part-reason-various)
924 (symbol)))
925
926 (defvar erc-grab-buffer-name "*erc-grab*"
927 "The name of the buffer created by `erc-grab-region'.")
928
929 ;; variables available for IRC scripts
930
931 (defvar erc-user-information "ERC User"
932 "USER_INFORMATION IRC variable.")
933
934 ;; Hooks
935
936 (defgroup erc-hooks nil
937 "Hook variables for fancy customizations of ERC."
938 :group 'erc)
939
940 (defcustom erc-mode-hook nil
941 "Hook run after `erc-mode' setup is finished."
942 :group 'erc-hooks
943 :type 'hook
944 :options '(erc-add-scroll-to-bottom))
945
946 (defcustom erc-timer-hook nil
947 "Put functions which should get called more or less periodically here.
948 The idea is that servers always play ping pong with the client, and so there
949 is no need for any idle-timer games with Emacs."
950 :group 'erc-hooks
951 :type 'hook)
952
953 (defcustom erc-insert-pre-hook nil
954 "Hook called first when some text is inserted through `erc-display-line'.
955 It gets called with one argument, STRING.
956 To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
957 Filtering functions can set `erc-insert-this' to nil to avoid
958 display of that particular string at all."
959 :group 'erc-hooks
960 :type 'hook)
961
962 (defcustom erc-send-pre-hook nil
963 "Hook called first when some text is sent through `erc-send-current-line'.
964 It gets called with one argument, STRING.
965
966 To change the text that will be sent, set the variable STR which is
967 used in `erc-send-current-line'.
968
969 To change the text inserted into the buffer without changing the text
970 that will be sent, use `erc-send-modify-hook' instead.
971
972 Filtering functions can set `erc-send-this' to nil to avoid sending of
973 that particular string at all and `erc-insert-this' to prevent
974 inserting that particular string into the buffer.
975
976 Note that it's useless to set `erc-send-this' to nil and
977 `erc-insert-this' to t. ERC is sane enough to not insert the text
978 anyway."
979 :group 'erc-hooks
980 :type 'hook)
981
982 (defvar erc-insert-this t
983 "Insert the text into the target buffer or not.
984 Functions on `erc-insert-pre-hook' can set this variable to nil
985 if they wish to avoid insertion of a particular string.")
986
987 (defvar erc-send-this t
988 "Send the text to the target or not.
989 Functions on `erc-send-pre-hook' can set this variable to nil
990 if they wish to avoid sending of a particular string.")
991
992 (defcustom erc-insert-modify-hook ()
993 "Insertion hook for functions that will change the text's appearance.
994 This hook is called just after `erc-insert-pre-hook' when the value
995 of `erc-insert-this' is t.
996 While this hook is run, narrowing is in effect and `current-buffer' is
997 the buffer where the text got inserted. One possible value to add here
998 is `erc-fill'."
999 :group 'erc-hooks
1000 :type 'hook)
1001
1002 (defcustom erc-insert-post-hook nil
1003 "This hook is called just after `erc-insert-modify-hook'.
1004 At this point, all modifications from prior hook functions are done."
1005 :group 'erc-hooks
1006 :type 'hook
1007 :options '(erc-truncate-buffer
1008 erc-make-read-only
1009 erc-save-buffer-in-logs))
1010
1011 (defcustom erc-send-modify-hook nil
1012 "Sending hook for functions that will change the text's appearance.
1013 This hook is called just after `erc-send-pre-hook' when the values
1014 of `erc-send-this' and `erc-insert-this' are both t.
1015 While this hook is run, narrowing is in effect and `current-buffer' is
1016 the buffer where the text got inserted.
1017
1018 Note that no function in this hook can change the appearance of the
1019 text that is sent. Only changing the sent text's appearance on the
1020 sending user's screen is possible. One possible value to add here
1021 is `erc-fill'."
1022 :group 'erc-hooks
1023 :type 'hook)
1024
1025 (defcustom erc-send-post-hook nil
1026 "This hook is called just after `erc-send-modify-hook'.
1027 At this point, all modifications from prior hook functions are done.
1028 NOTE: The functions on this hook are called _before_ sending a command
1029 to the server.
1030
1031 This function is called with narrowing, ala `erc-send-modify-hook'"
1032 :group 'erc-hooks
1033 :type 'hook
1034 :options '(erc-make-read-only))
1035
1036 (defcustom erc-send-completed-hook
1037 (when (featurep 'emacspeak)
1038 (list (byte-compile
1039 (lambda (str)
1040 (emacspeak-auditory-icon 'select-object)))))
1041 "Hook called after a message has been parsed by ERC.
1042
1043 The single argument to the functions is the unmodified string
1044 which the local user typed."
1045 :group 'erc-hooks
1046 :type 'hook)
1047 ;; mode-specific tables
1048
1049 (defvar erc-mode-syntax-table
1050 (let ((syntax-table (make-syntax-table)))
1051 (modify-syntax-entry ?\" ". " syntax-table)
1052 (modify-syntax-entry ?\\ ". " syntax-table)
1053 (modify-syntax-entry ?' "w " syntax-table)
1054 ;; Make dabbrev-expand useful for nick names
1055 (modify-syntax-entry ?< "." syntax-table)
1056 (modify-syntax-entry ?> "." syntax-table)
1057 syntax-table)
1058 "Syntax table used while in ERC mode.")
1059
1060 (defvar erc-mode-abbrev-table nil
1061 "Abbrev table used while in ERC mode.")
1062 (define-abbrev-table 'erc-mode-abbrev-table ())
1063
1064 (defvar erc-mode-map
1065 (let ((map (make-sparse-keymap)))
1066 (define-key map "\C-m" 'erc-send-current-line)
1067 (define-key map "\C-a" 'erc-bol)
1068 (define-key map [home] 'erc-bol)
1069 (define-key map "\C-c\C-a" 'erc-bol)
1070 (define-key map "\C-c\C-b" 'erc-iswitchb)
1071 (define-key map "\C-c\C-c" 'erc-toggle-interpret-controls)
1072 (define-key map "\C-c\C-d" 'erc-input-action)
1073 (define-key map "\C-c\C-e" 'erc-toggle-ctcp-autoresponse)
1074 (define-key map "\C-c\C-f" 'erc-toggle-flood-control)
1075 (define-key map "\C-c\C-i" 'erc-invite-only-mode)
1076 (define-key map "\C-c\C-j" 'erc-join-channel)
1077 (define-key map "\C-c\C-n" 'erc-channel-names)
1078 (define-key map "\C-c\C-o" 'erc-get-channel-mode-from-keypress)
1079 (define-key map "\C-c\C-p" 'erc-part-from-channel)
1080 (define-key map "\C-c\C-q" 'erc-quit-server)
1081 (define-key map "\C-c\C-r" 'erc-remove-text-properties-region)
1082 (define-key map "\C-c\C-t" 'erc-set-topic)
1083 (define-key map "\C-c\C-u" 'erc-kill-input)
1084 (define-key map "\M-\t" 'ispell-complete-word)
1085 (define-key map "\t" 'erc-complete-word)
1086
1087 ;; Suppress `font-lock-fontify-block' key binding since it
1088 ;; destroys face properties.
1089 (if (fboundp 'command-remapping)
1090 (define-key map [remap font-lock-fontify-block] 'undefined)
1091 (substitute-key-definition
1092 'font-lock-fontify-block 'undefined map global-map))
1093
1094 map)
1095 "ERC keymap.")
1096
1097 ;; Faces
1098
1099 ; Honestly, I have a horrible sense of color and the "defaults" below
1100 ; are supposed to be really bad. But colors ARE required in IRC to
1101 ; convey different parts of conversation. If you think you know better
1102 ; defaults - send them to me.
1103
1104 ;; Now colors are a bit nicer, at least to my eyes.
1105 ;; You may still want to change them to better fit your background.-- S.B.
1106
1107 (defgroup erc-faces nil
1108 "Faces for ERC."
1109 :group 'erc)
1110
1111 (defface erc-default-face '((t))
1112 "ERC default face."
1113 :group 'erc-faces)
1114
1115 (defface erc-direct-msg-face '((t (:foreground "IndianRed")))
1116 "ERC face used for messages you receive in the main erc buffer."
1117 :group 'erc-faces)
1118
1119 (defface erc-input-face '((t (:foreground "brown")))
1120 "ERC face used for your input."
1121 :group 'erc-faces)
1122
1123 (defface erc-prompt-face
1124 '((t (:bold t :foreground "Black" :background"lightBlue2")))
1125 "ERC face for the prompt."
1126 :group 'erc-faces)
1127
1128 (defface erc-command-indicator-face
1129 '((t (:bold t)))
1130 "ERC face for the command indicator.
1131 See the variable `erc-command-indicator'."
1132 :group 'erc-faces)
1133
1134 (defface erc-notice-face '((t (:bold t :foreground "SlateBlue")))
1135 "ERC face for notices."
1136 :group 'erc-faces)
1137
1138 (defface erc-action-face '((t (:bold t)))
1139 "ERC face for actions generated by /ME."
1140 :group 'erc-faces)
1141
1142 (defface erc-error-face '((t (:foreground "red")))
1143 "ERC face for errors."
1144 :group 'erc-faces)
1145
1146 (defface erc-nick-default-face '((t (:bold t)))
1147 "ERC nickname default face."
1148 :group 'erc-faces)
1149
1150 (defface erc-nick-msg-face '((t (:bold t :foreground "IndianRed")))
1151 "ERC nickname face for private messages."
1152 :group 'erc-faces)
1153
1154 ;; Debugging support
1155
1156 (defvar erc-log-p nil
1157 "When set to t, generate debug messages in a separate debug buffer.")
1158
1159 (defvar erc-debug-log-file (expand-file-name "ERC.debug")
1160 "Debug log file name.")
1161
1162 (defvar erc-dbuf nil)
1163 (make-variable-buffer-local 'erc-dbuf)
1164
1165 (defmacro define-erc-module (name alias doc enable-body disable-body
1166 &optional local-p)
1167 "Define a new minor mode using ERC conventions.
1168 Symbol NAME is the name of the module.
1169 Symbol ALIAS is the alias to use, or nil.
1170 DOC is the documentation string to use for the minor mode.
1171 ENABLE-BODY is a list of expressions used to enable the mode.
1172 DISABLE-BODY is a list of expressions used to disable the mode.
1173 If LOCAL-P is non-nil, the mode will be created as a buffer-local
1174 mode. Rather than a global one.
1175
1176 This will define a minor mode called erc-NAME-mode, possibly
1177 an alias erc-ALIAS-mode, as well as the helper functions
1178 erc-NAME-enable, and erc-NAME-disable.
1179
1180 Example:
1181
1182 ;;;###autoload (autoload 'erc-replace-mode \"erc-replace\")
1183 (define-erc-module replace nil
1184 \"This mode replaces incoming text according to `erc-replace-alist'.\"
1185 ((add-hook 'erc-insert-modify-hook
1186 'erc-replace-insert))
1187 ((remove-hook 'erc-insert-modify-hook
1188 'erc-replace-insert)))"
1189 (let* ((sn (symbol-name name))
1190 (mode (intern (format "erc-%s-mode" (downcase sn))))
1191 (group (intern (format "erc-%s" (downcase sn))))
1192 (enable (intern (format "erc-%s-enable" (downcase sn))))
1193 (disable (intern (format "erc-%s-disable" (downcase sn)))))
1194 `(progn
1195 (erc-define-minor-mode
1196 ,mode
1197 ,(format "Toggle ERC %S mode.
1198 With arg, turn ERC %S mode on if and only if arg is positive.
1199 %s" name name doc)
1200 nil nil nil
1201 :global ,(not local-p) :group (quote ,group)
1202 (if ,mode
1203 (,enable)
1204 (,disable)))
1205 (defun ,enable ()
1206 ,(format "Enable ERC %S mode."
1207 name)
1208 (interactive)
1209 (add-to-list 'erc-modules (quote ,name))
1210 (setq ,mode t)
1211 ,@enable-body)
1212 (defun ,disable ()
1213 ,(format "Disable ERC %S mode."
1214 name)
1215 (interactive)
1216 (setq erc-modules (delq (quote ,name) erc-modules))
1217 (setq ,mode nil)
1218 ,@disable-body)
1219 ,(when (and alias (not (eq name alias)))
1220 `(defalias
1221 (quote
1222 ,(intern
1223 (format "erc-%s-mode"
1224 (downcase (symbol-name alias)))))
1225 (quote
1226 ,mode))))))
1227
1228 (put 'define-erc-module 'doc-string-elt 3)
1229
1230 (defun erc-once-with-server-event (event &rest forms)
1231 "Execute FORMS the next time EVENT occurs in the `current-buffer'.
1232
1233 You should make sure that `current-buffer' is a server buffer.
1234
1235 This function temporarily adds a function to EVENT's hook to
1236 execute FORMS. After FORMS are run, the function is removed from
1237 EVENT's hook. The last expression of FORMS should be either nil
1238 or t. nil indicates that the other functions on EVENT's hook
1239 should be run too, and t indicates that other functions should
1240 not be run.
1241
1242 Please be sure to use this function in server-buffers. In
1243 channel-buffers it may not work at all, as it uses the LOCAL
1244 argument of `add-hook' and `remove-hook' to ensure multiserver
1245 capabilities."
1246 (unless (erc-server-buffer-p)
1247 (error
1248 "You should only run `erc-once-with-server-event' in a server buffer"))
1249 (let ((fun (erc-gensym))
1250 (hook (erc-get-hook event)))
1251 (put fun 'erc-original-buffer (current-buffer))
1252 (fset fun `(lambda (proc parsed)
1253 (with-current-buffer (get ',fun 'erc-original-buffer)
1254 (remove-hook ',hook ',fun t))
1255 (fmakunbound ',fun)
1256 ,@forms))
1257 (add-hook hook fun nil t)
1258 fun))
1259
1260 (defun erc-once-with-server-event-global (event &rest forms)
1261 "Execute FORMS the next time EVENT occurs in any server buffer.
1262
1263 This function temporarily prepends a function to EVENT's hook to
1264 execute FORMS. After FORMS are run, the function is removed from
1265 EVENT's hook. The last expression of FORMS should be either nil
1266 or t. nil indicates that the other functions on EVENT's hook
1267 should be run too, and t indicates that other functions should
1268 not be run.
1269
1270 When FORMS execute, the current buffer is the server buffer associated with the
1271 connection over which the data was received that triggered EVENT."
1272 (let ((fun (erc-gensym))
1273 (hook (erc-get-hook event)))
1274 (fset fun `(lambda (proc parsed)
1275 (remove-hook ',hook ',fun)
1276 (fmakunbound ',fun)
1277 ,@forms))
1278 (add-hook hook fun nil nil)
1279 fun))
1280
1281 (defmacro erc-log (string)
1282 "Logs STRING if logging is on (see `erc-log-p')."
1283 `(when erc-log-p
1284 (erc-log-aux ,string)))
1285
1286 (defun erc-server-buffer ()
1287 "Return the server buffer for the current buffer's process.
1288 The buffer-local variable `erc-server-process' is used to find
1289 the process buffer."
1290 (and (erc-server-buffer-live-p)
1291 (process-buffer erc-server-process)))
1292
1293 (defun erc-server-buffer-live-p ()
1294 "Return t if the buffer associated with `erc-server-process'
1295 has not been killed."
1296 (and (processp erc-server-process)
1297 (buffer-live-p (process-buffer erc-server-process))))
1298
1299 (defun erc-server-buffer-p (&optional buffer)
1300 "Return non-nil if argument BUFFER is an ERC server buffer.
1301
1302 If BUFFER is nil, the current buffer is used."
1303 (with-current-buffer (or buffer (current-buffer))
1304 (and (eq major-mode 'erc-mode)
1305 (null (erc-default-target)))))
1306
1307 (defun erc-query-buffer-p (&optional buffer)
1308 "Return non-nil if BUFFER is an ERC query buffer.
1309 If BUFFER is nil, the current buffer is used."
1310 (with-current-buffer (or buffer (current-buffer))
1311 (let ((target (erc-default-target)))
1312 (and (eq major-mode 'erc-mode)
1313 target
1314 (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
1315
1316 (defun erc-ison-p (nick)
1317 "Return non-nil if NICK is online."
1318 (interactive "sNick: ")
1319 (with-current-buffer (erc-server-buffer)
1320 (let ((erc-online-p 'unknown))
1321 (erc-once-with-server-event
1322 303
1323 `(let ((ison (split-string (aref parsed 3))))
1324 (setq erc-online-p (car (erc-member-ignore-case ,nick ison)))
1325 t))
1326 (erc-server-send (format "ISON %s" nick))
1327 (while (eq erc-online-p 'unknown) (accept-process-output))
1328 (if (interactive-p)
1329 (message "%s is %sonline"
1330 (or erc-online-p nick)
1331 (if erc-online-p "" "not "))
1332 erc-online-p))))
1333
1334 (defun erc-log-aux (string)
1335 "Do the debug logging of STRING."
1336 (let ((cb (current-buffer))
1337 (point 1)
1338 (was-eob nil)
1339 (session-buffer (erc-server-buffer)))
1340 (if session-buffer
1341 (progn
1342 (set-buffer session-buffer)
1343 (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
1344 (progn
1345 (setq erc-dbuf (get-buffer-create
1346 (concat "*ERC-DEBUG: "
1347 erc-session-server "*")))))
1348 (set-buffer erc-dbuf)
1349 (setq point (point))
1350 (setq was-eob (eobp))
1351 (goto-char (point-max))
1352 (insert (concat "** " string "\n"))
1353 (if was-eob (goto-char (point-max))
1354 (goto-char point))
1355 (set-buffer cb))
1356 (message "ERC: ** %s" string))))
1357
1358 ;; Last active buffer, to print server messages in the right place
1359
1360 (defvar erc-active-buffer nil
1361 "The current active buffer, the one where the user typed the last command.
1362 Defaults to the server buffer, and should only be set in the
1363 server buffer")
1364 (make-variable-buffer-local 'erc-active-buffer)
1365
1366 (defun erc-active-buffer ()
1367 "Return the value of `erc-active-buffer' for the current server.
1368 Defaults to the server buffer."
1369 (with-current-buffer (erc-server-buffer) erc-active-buffer))
1370
1371 (defun erc-set-active-buffer (buffer)
1372 "Set the value of `erc-active-buffer' to BUFFER."
1373 (cond ((erc-server-buffer)
1374 (with-current-buffer (erc-server-buffer)
1375 (setq erc-active-buffer buffer)))
1376 (t (setq erc-active-buffer buffer))))
1377
1378 ;; Mode activation routines
1379
1380 (defun erc-mode ()
1381 "Major mode for Emacs IRC.
1382 Special commands:
1383
1384 \\{erc-mode-map}
1385
1386 Turning on `erc-mode' runs the hook `erc-mode-hook'."
1387 (kill-all-local-variables)
1388 (use-local-map erc-mode-map)
1389 (setq mode-name "ERC"
1390 major-mode 'erc-mode
1391 local-abbrev-table erc-mode-abbrev-table)
1392 (set-syntax-table erc-mode-syntax-table)
1393 (when (boundp 'next-line-add-newlines)
1394 (set (make-local-variable 'next-line-add-newlines) nil))
1395 (setq line-move-ignore-invisible t)
1396 (set (make-local-variable 'paragraph-separate)
1397 (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
1398 (set (make-local-variable 'paragraph-start)
1399 (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
1400 ;; Run the mode hooks
1401 (run-hooks 'erc-mode-hook))
1402
1403 ;; activation
1404
1405 (defconst erc-default-server "irc.freenode.net"
1406 "IRC server to use if it cannot be detected otherwise.")
1407
1408 (defconst erc-default-port "ircd"
1409 "IRC port to use if it cannot be detected otherwise.")
1410
1411 (defcustom erc-join-buffer 'buffer
1412 "Determines how to display the newly created IRC buffer.
1413 'window - in another window,
1414 'window-noselect - in another window, but don't select that one,
1415 'frame - in another frame,
1416 'bury - bury it in a new buffer,
1417 any other value - in place of the current buffer"
1418 :group 'erc-buffers
1419 :type '(choice (const window)
1420 (const window-noselect)
1421 (const frame)
1422 (const bury)
1423 (const buffer)))
1424
1425 (defcustom erc-frame-alist nil
1426 "*Alist of frame parameters for creating erc frames.
1427 A value of `nil means to use `default-frame-alist'."
1428 :group 'erc-buffers
1429 :type '(repeat (cons :format "%v"
1430 (symbol :tag "Parameter")
1431 (sexp :tag "Value"))))
1432
1433 (defcustom erc-frame-dedicated-flag nil
1434 "*Non-nil means the erc frames are dedicated to that buffer.
1435 This only has effect when `erc-join-buffer' is set to `frame'."
1436 :group 'erc-buffers
1437 :type 'boolean)
1438
1439 (defun erc-channel-p (channel)
1440 "Return non-nil if CHANNEL seems to be an IRC channel name."
1441 (cond ((stringp channel)
1442 (memq (aref channel 0) '(?# ?& ?+ ?!)))
1443 ((and (bufferp channel) (buffer-live-p channel))
1444 (with-current-buffer channel
1445 (erc-channel-p (erc-default-target))))
1446 (t nil)))
1447
1448 (defcustom erc-reuse-buffers t
1449 "*If nil, create new buffers on joining a channel/query.
1450 If non-nil, a new buffer will only be created when you join
1451 channels with same names on different servers, or have query buffers
1452 open with nicks of the same name on different servers. Otherwise,
1453 the existing buffers will be reused."
1454 :group 'erc-buffers
1455 :type 'boolean)
1456
1457 (defun erc-normalize-port (port)
1458 "Normalize the port specification PORT to integer form.
1459 PORT may be an integer, a string or a symbol. If it is a string or a
1460 symbol, it may have these values:
1461 * irc -> 194
1462 * ircs -> 994
1463 * ircd -> 6667
1464 * ircd-dalnet -> 7000"
1465 (cond
1466 ((symbolp port)
1467 (erc-normalize-port (symbol-name port)))
1468 ((stringp port)
1469 (let ((port-nr (string-to-number port)))
1470 (cond
1471 ((> port-nr 0)
1472 port-nr)
1473 ((string-equal port "irc")
1474 194)
1475 ((string-equal port "ircs")
1476 994)
1477 ((string-equal port "ircd")
1478 6667)
1479 ((string-equal port "ircd-dalnet")
1480 7000)
1481 (t
1482 nil))))
1483 ((numberp port)
1484 port)
1485 (t
1486 nil)))
1487
1488 (defun erc-port-equal (a b)
1489 "Check whether ports A and B are equal."
1490 (= (erc-normalize-port a) (erc-normalize-port b)))
1491
1492 (defun erc-generate-new-buffer-name (server port target &optional proc)
1493 "Create a new buffer name based on the arguments."
1494 (when (numberp port) (setq port (number-to-string port)))
1495 (let* ((buf-name (or target
1496 (or (let ((name (concat server ":" port)))
1497 (when (> (length name) 1)
1498 name))
1499 ; This fallback should in fact never happen
1500 "*erc-server-buffer*"))))
1501 ;; Reuse existing buffers, but not if the buffer is a connected server
1502 ;; buffer and not if its associated with a different server than the
1503 ;; current ERC buffer.
1504 (if (and erc-reuse-buffers
1505 (get-buffer buf-name)
1506 (or target
1507 (with-current-buffer (get-buffer buf-name)
1508 (and (erc-server-buffer-p)
1509 (not erc-server-connected))))
1510 (with-current-buffer (get-buffer buf-name)
1511 (and (string= erc-session-server server)
1512 (erc-port-equal erc-session-port port))))
1513 buf-name
1514 (generate-new-buffer-name buf-name))))
1515
1516 (defun erc-get-buffer-create (server port target &optional proc)
1517 "Create a new buffer based on the arguments."
1518 (get-buffer-create (erc-generate-new-buffer-name server port target proc)))
1519
1520
1521 (defun erc-member-ignore-case (string list)
1522 "Return non-nil if STRING is a member of LIST.
1523
1524 All strings are compared according to IRC protocol case rules, see
1525 `erc-downcase'."
1526 (setq string (erc-downcase string))
1527 (catch 'result
1528 (while list
1529 (if (string= string (erc-downcase (car list)))
1530 (throw 'result list) (setq list (cdr list))))))
1531
1532 (defmacro erc-with-buffer (spec &rest body)
1533 "Execute BODY in the buffer associated with SPEC.
1534
1535 SPEC should have the form
1536
1537 (TARGET [PROCESS])
1538
1539 If TARGET is a buffer, use it. Otherwise, use the buffer
1540 matching TARGET in the process specified by PROCESS.
1541
1542 If PROCESS is nil, use the current `erc-server-process'
1543 See `erc-get-buffer' for details.
1544
1545 See also `with-current-buffer'.
1546
1547 \(fn (TARGET [PROCESS]) BODY...)"
1548 (let ((buf (erc-gensym))
1549 (proc (erc-gensym))
1550 (target (erc-gensym))
1551 (process (erc-gensym)))
1552 `(let* ((,target ,(car spec))
1553 (,process ,(cadr spec))
1554 (,buf (if (bufferp ,target)
1555 ,target
1556 (let ((,proc (or ,process
1557 (and (processp erc-server-process)
1558 erc-server-process))))
1559 (if (and ,target ,proc)
1560 (erc-get-buffer ,target ,proc))))))
1561 (when ,buf
1562 (with-current-buffer ,buf
1563 ,@body)))))
1564 (put 'erc-with-buffer 'lisp-indent-function 1)
1565 (put 'erc-with-buffer 'edebug-form-spec '((form &optional form) body))
1566
1567 (defun erc-get-buffer (target &optional proc)
1568 "Return the buffer matching TARGET in the process PROC.
1569 If PROC is not supplied, all processes are searched."
1570 (let ((downcased-target (erc-downcase target)))
1571 (catch 'buffer
1572 (erc-buffer-filter
1573 (lambda ()
1574 (let ((current (erc-default-target)))
1575 (and (stringp current)
1576 (string-equal downcased-target (erc-downcase current))
1577 (throw 'buffer (current-buffer)))))
1578 proc))))
1579
1580 (defun erc-buffer-filter (predicate &optional proc)
1581 "Return a list of `erc-mode' buffers matching certain criteria.
1582 PREDICATE is a function executed with each buffer, if it returns t, that buffer
1583 is considered a valid match.
1584
1585 PROC is either an `erc-server-process', identifying a certain
1586 server connection, or nil which means all open connections."
1587 (save-excursion
1588 (delq
1589 nil
1590 (mapcar (lambda (buf)
1591 (with-current-buffer buf
1592 (and (eq major-mode 'erc-mode)
1593 (or (not proc)
1594 (eq proc erc-server-process))
1595 (funcall predicate)
1596 buf)))
1597 (buffer-list)))))
1598
1599 (defun erc-buffer-list (&optional predicate proc)
1600 "Return a list of ERC buffers.
1601 PREDICATE is a function which executes with every buffer satisfying
1602 the predicate. If PREDICATE is passed as nil, return a list of all ERC
1603 buffers. If PROC is given, the buffers local variable `erc-server-process'
1604 needs to match PROC."
1605 (unless predicate
1606 (setq predicate (lambda () t)))
1607 (erc-buffer-filter predicate proc))
1608
1609 (defmacro erc-with-all-buffers-of-server (process pred &rest forms)
1610 "Execute FORMS in all buffers which have same process as this server.
1611 FORMS will be evaluated in all buffers having the process PROCESS and
1612 where PRED matches or in all buffers of the server process if PRED is
1613 nil."
1614 ;; Make the evaluation have the correct order
1615 (let ((pre (erc-gensym))
1616 (pro (erc-gensym)))
1617 `(let ((,pro ,process)
1618 (,pre ,pred))
1619 (mapcar (lambda (buffer)
1620 (with-current-buffer buffer
1621 ,@forms))
1622 (erc-buffer-list ,pre
1623 ,pro)))))
1624 (put 'erc-with-all-buffers-of-server 'lisp-indent-function 1)
1625 (put 'erc-with-all-buffers-of-server 'edebug-form-spec '(form form body))
1626
1627 (defun erc-iswitchb (&optional arg)
1628 "Use `iswitchb-read-buffer' to prompt for a ERC buffer to switch to.
1629 When invoked with prefix argument, use all erc buffers. Without prefix
1630 ARG, allow only buffers related to same session server.
1631 If `erc-track-mode' is in enabled, put the last element of
1632 `erc-modified-channels-alist' in front of the buffer list.
1633
1634 Due to some yet unresolved reason, global function `iswitchb-mode'
1635 needs to be active for this function to work."
1636 (interactive "P")
1637 (eval-when-compile
1638 (require 'iswitchb))
1639 (let ((iswitchb-make-buflist-hook
1640 (lambda ()
1641 (setq iswitchb-temp-buflist
1642 (mapcar 'buffer-name
1643 (erc-buffer-list
1644 nil
1645 (when (and arg (boundp 'erc-server-process))
1646 erc-server-process)))))))
1647 (switch-to-buffer
1648 (iswitchb-read-buffer
1649 "Switch-to: "
1650 (if (boundp 'erc-modified-channels-alist)
1651 (buffer-name (caar (last erc-modified-channels-alist)))
1652 nil)
1653 t))))
1654
1655 (defun erc-channel-list (proc)
1656 "Return a list of channel buffers.
1657 PROC is the process for the server connection. If PROC is nil, return
1658 all channel buffers on all servers."
1659 (erc-buffer-filter
1660 (lambda ()
1661 (and (erc-default-target)
1662 (erc-channel-p (erc-default-target))))
1663 proc))
1664
1665 (defun erc-buffer-list-with-nick (nick proc)
1666 "Return buffers containing NICK in the `erc-channel-users' list."
1667 (with-current-buffer (process-buffer proc)
1668 (let ((user (gethash (erc-downcase nick) erc-server-users)))
1669 (if user
1670 (erc-server-user-buffers user)
1671 nil))))
1672
1673 ;; Some local variables
1674
1675 (defvar erc-default-recipients nil
1676 "List of default recipients of the current buffer.")
1677 (make-variable-buffer-local 'erc-default-recipients)
1678
1679 (defvar erc-session-user-full-name nil
1680 "Full name of the user on the current server.")
1681 (make-variable-buffer-local 'erc-session-user-full-name)
1682
1683 (defvar erc-channel-user-limit nil
1684 "Limit of users per channel.")
1685 (make-variable-buffer-local 'erc-channel-user-limit)
1686
1687 (defvar erc-channel-key nil
1688 "Key needed to join channel.")
1689 (make-variable-buffer-local 'erc-channel-key)
1690
1691 (defvar erc-invitation nil
1692 "Last invitation channel.")
1693 (make-variable-buffer-local 'erc-invitation)
1694
1695 (defvar erc-away nil
1696 "Non-nil indicates that we are away.")
1697 (make-variable-buffer-local 'erc-away)
1698
1699 (defvar erc-channel-list nil
1700 "Server channel list.")
1701 (make-variable-buffer-local 'erc-channel-list)
1702
1703 (defvar erc-bad-nick nil
1704 "Non-nil indicates that we got a `nick in use' error while connecting.")
1705 (make-variable-buffer-local 'erc-bad-nick)
1706
1707 (defvar erc-logged-in nil
1708 "Non-nil indicates that we are logged in.")
1709 (make-variable-buffer-local 'erc-logged-in)
1710
1711 (defvar erc-default-nicks nil
1712 "The local copy of `erc-nick' - the list of nicks to choose from.")
1713 (make-variable-buffer-local 'erc-default-nicks)
1714
1715 (defvar erc-nick-change-attempt-count 0
1716 "Used to keep track of how many times an attempt at changing nick is made.")
1717 (make-variable-buffer-local 'erc-nick-change-attempt-count)
1718
1719 (defcustom erc-modules '(netsplit fill button match track pcomplete readonly
1720 ring autojoin noncommands irccontrols
1721 stamp)
1722 "A list of modules which erc should enable.
1723 If you set the value of this without using `customize' remember to call
1724 \(erc-update-modules) after you change it. When using `customize', modules
1725 removed from the list will be disabled."
1726 :set (lambda (sym val)
1727 ;; disable modules which have just been removed
1728 (when (and (boundp 'erc-modules) erc-modules val)
1729 (dolist (module erc-modules)
1730 (unless (member module val)
1731 (let ((f (intern-soft (format "erc-%s-mode" module))))
1732 (when (and (fboundp f) (boundp f) (symbol-value f))
1733 (message "Disabling `erc-%s'" module)
1734 (funcall f 0))))))
1735 (set-default sym val)
1736 ;; this test is for the case where erc hasn't been loaded yet
1737 (when (fboundp 'erc-update-modules)
1738 (erc-update-modules)))
1739 :type '(set :greedy t
1740 (const :tag "Set away status automatically" autoaway)
1741 (const :tag "Join channels automatically" autojoin)
1742 (const :tag "Integrate with Big Brother Database" bbdb)
1743 (const :tag "Buttonize URLs, nicknames, and other text" button)
1744 (const :tag "Wrap long lines" fill)
1745 (const :tag "Highlight or remove IRC control characters"
1746 irccontrols)
1747 (const :tag "Save buffers in logs" log)
1748 (const :tag "Highlight pals, fools, and other keywords" match)
1749 (const :tag "Detect netsplits" netsplit)
1750 (const :tag "Don't display non-IRC commands after evaluation"
1751 noncommands)
1752 (const :tag
1753 "Notify when the online status of certain users changes"
1754 notify)
1755 (const :tag "Complete nicknames and commands (programmable)"
1756 pcomplete)
1757 (const :tag "Complete nicknames and commands (old)" completion)
1758 (const :tag "Make displayed lines read-only" readonly)
1759 (const :tag "Replace text in messages" replace)
1760 (const :tag "Enable an input history" ring)
1761 (const :tag "Scroll to the bottom of the buffer" scrolltobottom)
1762 (const :tag "Identify to Nickserv (IRC Services) automatically"
1763 services)
1764 (const :tag "Convert smileys to pretty icons" smiley)
1765 (const :tag "Play sounds when you receive CTCP SOUND requests"
1766 sound)
1767 (const :tag "Add timestamps to messages" stamp)
1768 (const :tag "Check spelling" spelling)
1769 (const :tag "Track channel activity in the mode-line" track)
1770 (const :tag "Truncate buffers to a certain size" truncate)
1771 (const :tag "Translate morse code in messages" unmorse)
1772 (repeat :tag "Others" :inline t symbol))
1773 :group 'erc)
1774
1775 (defun erc-update-modules ()
1776 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1777 (let (req)
1778 (dolist (mod erc-modules)
1779 (setq req (concat "erc-" (symbol-name mod)))
1780 (cond
1781 ;; yuck. perhaps we should bring the filenames into sync?
1782 ((string= req "erc-completion")
1783 (setq req "erc-pcomplete")
1784 (setq mod 'pcomplete))
1785 ((string= req "erc-services")
1786 (setq req "erc-nickserv")
1787 (setq mod 'services)))
1788 (condition-case nil
1789 (require (intern req))
1790 (error nil))
1791 (funcall (or (intern-soft (concat "erc-" (symbol-name mod) "-mode"))
1792 (error "`%s' is not a known ERC module" mod))
1793 1))))
1794
1795 (defun erc-setup-buffer (buffer)
1796 "Consults `erc-join-buffer' to find out how to display `BUFFER'."
1797 (cond ((eq erc-join-buffer 'window)
1798 (if (active-minibuffer-window)
1799 (display-buffer buffer)
1800 (switch-to-buffer-other-window buffer)))
1801 ((eq erc-join-buffer 'window-noselect)
1802 (display-buffer buffer))
1803 ((eq erc-join-buffer 'bury)
1804 nil)
1805 ((eq erc-join-buffer 'frame)
1806 (funcall '(lambda (frame)
1807 (raise-frame frame)
1808 (select-frame frame))
1809 (make-frame (or erc-frame-alist
1810 default-frame-alist)))
1811 (switch-to-buffer buffer)
1812 (when erc-frame-dedicated-flag
1813 (set-window-dedicated-p (selected-window) t)))
1814 (t
1815 (if (active-minibuffer-window)
1816 (display-buffer buffer)
1817 (switch-to-buffer buffer)))))
1818
1819 (defun erc (&optional server port nick full-name
1820 connect passwd tgt-list channel process)
1821 "ERC is a powerful, modular, and extensible IRC client.
1822
1823 Connect to SERVER on PORT as NICK with FULL-NAME.
1824
1825 If CONNECT is non-nil, connect to the server. Otherwise assume
1826 already connected and just create a separate buffer for the new
1827 target CHANNEL.
1828
1829 Use PASSWD as user password on the server. If TGT-LIST is
1830 non-nil, use it to initialise `erc-default-recipients'.
1831
1832 Returns the buffer for the given server or channel."
1833 (let ((server-announced-name (when (and (boundp 'erc-session-server)
1834 (string= server erc-session-server))
1835 erc-server-announced-name))
1836 (connected-p (unless connect erc-server-connected))
1837 (buffer (erc-get-buffer-create server port channel))
1838 (old-buffer (current-buffer))
1839 continued-session)
1840 (erc-update-modules)
1841 (set-buffer buffer)
1842 (erc-mode)
1843 (setq erc-server-announced-name server-announced-name)
1844 (setq erc-server-connected connected-p)
1845 ;; connection parameters
1846 (setq erc-server-process process)
1847 (setq erc-insert-marker (make-marker))
1848 (setq erc-input-marker (make-marker))
1849 ;; go to the end of the buffer and open a new line
1850 ;; (the buffer may have existed)
1851 (goto-char (point-max))
1852 (forward-line 0)
1853 (when (get-text-property (point) 'erc-prompt)
1854 (setq continued-session t)
1855 (set-marker erc-input-marker
1856 (or (next-single-property-change (point) 'erc-prompt)
1857 (point-max))))
1858 (unless continued-session
1859 (goto-char (point-max))
1860 (insert "\n"))
1861 (set-marker erc-insert-marker (point))
1862 ;; stack of default recipients
1863 (setq erc-default-recipients tgt-list)
1864 (setq erc-server-current-nick nil)
1865 ;; Initialize erc-server-users and erc-channel-users
1866 (if connect
1867 (progn ;; server buffer
1868 (setq erc-server-users
1869 (make-hash-table :test 'equal))
1870 (setq erc-channel-users nil))
1871 (progn ;; target buffer
1872 (setq erc-server-users nil)
1873 (setq erc-channel-users
1874 (make-hash-table :test 'equal))))
1875 ;; clear last incomplete line read
1876 (setq erc-server-filter-data nil)
1877 (setq erc-channel-topic "")
1878 ;; limit on the number of users on the channel (mode +l)
1879 (setq erc-channel-user-limit nil)
1880 (setq erc-channel-key nil)
1881 ;; last active buffer, defaults to this one
1882 (erc-set-active-buffer buffer)
1883 ;; last invitation channel
1884 (setq erc-invitation nil)
1885 ;; away flag
1886 ;; Should only be used in session-buffers
1887 (setq erc-away (let ((serverbuf (erc-server-buffer)))
1888 (and serverbuf (with-current-buffer serverbuf erc-away))))
1889 ;; Server channel list
1890 (setq erc-channel-list ())
1891 ;; login-time 'nick in use' error
1892 (setq erc-bad-nick nil)
1893 ;; whether we have logged in
1894 (setq erc-logged-in nil)
1895 ;; The local copy of `erc-nick' - the list of nicks to choose
1896 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
1897 ;; password stuff
1898 (setq erc-session-password passwd)
1899 ;; debug output buffer
1900 (setq erc-dbuf
1901 (when erc-log-p
1902 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
1903 (erc-determine-parameters server port nick full-name)
1904
1905 ;; Saving log file on exit
1906 (run-hooks 'erc-connect-pre-hook)
1907
1908 (when connect
1909 (erc-server-connect erc-session-server erc-session-port))
1910 (erc-update-mode-line)
1911 (set-marker erc-insert-marker (point))
1912 (unless continued-session
1913 (goto-char (point-max))
1914 (insert "\n"))
1915 (set-marker (process-mark erc-server-process) (point))
1916 (unless continued-session
1917 (set-marker erc-insert-marker (point))
1918 (erc-display-prompt)
1919 (goto-char (point-max)))
1920
1921 ;; Now display the buffer in a window as per user wishes.
1922 (unless (eq buffer old-buffer)
1923 (when erc-log-p
1924 ;; we can't log to debug buffer, it may not exist yet
1925 (message "erc: old buffer %s, switching to %s"
1926 old-buffer buffer))
1927 (erc-setup-buffer buffer))
1928
1929 buffer))
1930
1931 (defun erc-initialize-log-marker ()
1932 "Initialize the `erc-last-saved-position' marker to a sensible position."
1933 (setq erc-last-saved-position (make-marker))
1934 (move-marker erc-last-saved-position
1935 (1- (marker-position erc-insert-marker))))
1936
1937 ;; interactive startup
1938
1939 (defvar erc-server-history-list nil
1940 "IRC server interactive selection history list.")
1941
1942 (defvar erc-nick-history-list nil
1943 "Nickname interactive selection history list.")
1944
1945 (defun erc-already-logged-in (server port nick)
1946 "Return the buffers corresponding to a NICK on PORT of a session SERVER.
1947 This is determined by looking for the appropriate buffer and checking
1948 whether the connection is still alive.
1949 If no buffer matches, return nil."
1950 (erc-buffer-list
1951 (lambda ()
1952 (and (erc-server-process-alive)
1953 (string= erc-session-server server)
1954 (erc-port-equal erc-session-port port)
1955 (erc-current-nick-p nick)))))
1956
1957 (if (not (fboundp 'read-passwd))
1958 (defun read-passwd (prompt)
1959 "Substitute for read-passwd in early emacsen"
1960 (read-from-minibuffer prompt)))
1961
1962 (defcustom erc-before-connect nil
1963 "Hook called before connecting to a server.
1964 This hook gets executed before `erc-select' actually invokes `erc-mode'
1965 with your input data. The functions in here get called with three
1966 parameters, SERVER, PORT and NICK."
1967 :group 'erc-hooks
1968 :type 'hook)
1969
1970 (defcustom erc-after-connect nil
1971 "Hook called after connecting to a server.
1972 This hook gets executed when an end of MOTD has been received. All
1973 functions in here get called with the parameters SERVER and NICK."
1974 :group 'erc-hooks
1975 :type 'hook)
1976
1977 ;;;###autoload
1978 (defun erc-select-read-args ()
1979 "Prompt the user for values of nick, server, port, and password."
1980 (let (user-input server port nick passwd)
1981 (setq user-input (read-from-minibuffer
1982 "IRC server: "
1983 (erc-compute-server) nil nil 'erc-server-history-list))
1984
1985 (if (string-match "\\(.*\\):\\(.*\\)\\'" user-input)
1986 (setq port (erc-string-to-port (match-string 2 user-input))
1987 user-input (match-string 1 user-input))
1988 (setq port
1989 (erc-string-to-port (read-from-minibuffer
1990 "IRC port: " (erc-port-to-string
1991 (erc-compute-port))))))
1992
1993 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
1994 (setq nick (match-string 1 user-input)
1995 user-input (match-string 2 user-input))
1996 (setq nick
1997 (if (erc-already-logged-in server port nick)
1998 (read-from-minibuffer
1999 (erc-format-message 'nick-in-use ?n nick)
2000 nick
2001 nil nil 'erc-nick-history-list)
2002 (read-from-minibuffer
2003 "Nickname: " (erc-compute-nick nick)
2004 nil nil 'erc-nick-history-list))))
2005
2006 (setq server user-input)
2007
2008 (setq passwd (if erc-prompt-for-password
2009 (if (and erc-password
2010 (y-or-n-p "Use the default password? "))
2011 erc-password
2012 (read-passwd "Password: "))
2013 erc-password))
2014 (when (and passwd (string= "" passwd))
2015 (setq passwd nil))
2016
2017 (while (erc-already-logged-in server port nick)
2018 ;; hmm, this is a problem when using multiple connections to a bnc
2019 ;; with the same nick. Currently this code prevents using more than one
2020 ;; bnc with the same nick. actually it would be nice to have
2021 ;; bncs transparent, so that erc-compute-buffer-name displays
2022 ;; the server one is connected to.
2023 (setq nick (read-from-minibuffer
2024 (erc-format-message 'nick-in-use ?n nick)
2025 nick
2026 nil nil 'erc-nick-history-list)))
2027 (list :server server :port port :nick nick :password passwd)))
2028
2029 ;;;###autoload
2030 (defun* erc-select (&key (server (erc-compute-server))
2031 (port (erc-compute-port))
2032 (nick (erc-compute-nick))
2033 password
2034 (full-name (erc-compute-full-name)))
2035 "Select connection parameters and run ERC.
2036 Non-interactively, it takes keyword arguments
2037 (server (erc-compute-server))
2038 (port (erc-compute-port))
2039 (nick (erc-compute-nick))
2040 password
2041 (full-name (erc-compute-full-name)))
2042
2043 That is, if called with
2044 (erc-select :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
2045 server and full-name will be set to those values, whereas
2046 erc-compute-port, erc-compute-nick and erc-compute-full-name will
2047 be invoked for those parameters' values"
2048 (interactive (erc-select-read-args))
2049
2050 (run-hook-with-args 'erc-before-connect server port nick)
2051 (erc server port nick erc-user-full-name t password))
2052
2053
2054 (defun erc-select-ssl (&rest r)
2055 "Interactively select SSL connection parameters and run ERC.
2056 Arguments are as to erc-select."
2057 (interactive (erc-select-read-args))
2058 (let ((erc-server-connect-function 'erc-open-ssl-stream))
2059 (apply 'erc-select r)))
2060
2061 (defun erc-open-ssl-stream (name buffer host port)
2062 "Open an SSL stream to an IRC server.
2063 The process will be given the name NAME, its target buffer will be
2064 BUFFER. HOST and PORT specify the connection target."
2065 (when (require 'tls)
2066 (let ((proc (open-tls-stream name buffer host port)))
2067 ;; Ugly hack, but it works for now. Problem is it is
2068 ;; very hard to detect when ssl is established, because s_client
2069 ;; doesn't give any CONNECTIONESTABLISHED kind of message, and
2070 ;; most IRC servers send nothing and wait for you to identify.
2071 ;; Disabled when switching to tls.el -- jas
2072 ;(sit-for 5)
2073 proc)))
2074
2075 ;;; Debugging the protocol
2076
2077 (defvar erc-debug-irc-protocol nil
2078 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2079
2080 The buffer is created if it doesn't exist.
2081
2082 NOTE: If this variable is non-nil, and you kill the the only
2083 visible \"*erc-protocol*\" buffer, it will be recreated shortly,
2084 but you won't see it.
2085
2086 WARNING: Do not set this variable directly! Instead, use the
2087 function `erc-toggle-debug-irc-protocol' to toggle its value.")
2088
2089 (defun erc-log-irc-protocol (string &optional outbound)
2090 "Append STRING to the buffer *erc-protocol*.
2091
2092 This only has any effect if `erc-debug-irc-protocol' is non-nil.
2093
2094 The buffer is created if it doesn't exist.
2095
2096 If OUTBOUND is non-nil, STRING is being sent to the IRC server
2097 and appears in erc-input-face in the buffer."
2098 (when erc-debug-irc-protocol
2099 (let ((network-name (or (ignore-errors (erc-network-name))
2100 "???")))
2101 (with-current-buffer (get-buffer-create "*erc-protocol*")
2102 (save-excursion
2103 (goto-char (point-max))
2104 (let ((inhibit-read-only t))
2105 (insert (if (not outbound)
2106 ;; Cope with the fact that string might
2107 ;; contain multiple lines of text.
2108 (let ((lines (delete "" (split-string string
2109 "\n\\|\r\n")))
2110 (result ""))
2111 (dolist (line lines)
2112 (setq result (concat result network-name
2113 " << " line "\n")))
2114 result)
2115 (erc-propertize
2116 (concat network-name " >> " string
2117 (if (/= ?\n
2118 (aref string
2119 (1- (length string))))
2120 "\n"))
2121 'face 'erc-input-face)))))
2122 (let ((orig-win (selected-window))
2123 (debug-buffer-window (get-buffer-window (current-buffer) t)))
2124 (when debug-buffer-window
2125 (select-window debug-buffer-window)
2126 (when (= 1 (count-lines (point) (point-max)))
2127 (goto-char (point-max))
2128 (recenter -1))
2129 (select-window orig-win)))))))
2130
2131 (defun erc-toggle-debug-irc-protocol (&optional arg)
2132 "Toggle the value of `erc-debug-irc-protocol'.
2133
2134 If ARG is non-nil, show the *erc-protocol* buffer."
2135 (interactive "P")
2136 (let* ((buf (get-buffer-create "*erc-protocol*")))
2137 (with-current-buffer buf
2138 (erc-view-mode-enter 1)
2139 (when (null (current-local-map))
2140 (let ((inhibit-read-only t))
2141 (insert (erc-make-notice "This buffer displays all IRC protocol traffic exchanged with each server.\n"))
2142 (insert (erc-make-notice "Kill this buffer to terminate protocol logging.\n\n")))
2143 (use-local-map (make-sparse-keymap))
2144 (local-set-key (kbd "RET") 'erc-toggle-debug-irc-protocol))
2145 (add-hook 'kill-buffer-hook
2146 #'(lambda () (setq erc-debug-irc-protocol nil))
2147 nil 'local)
2148 (goto-char (point-max))
2149 (let ((inhibit-read-only t))
2150 (insert (erc-make-notice
2151 (format "IRC protocol logging %s at %s -- Press ENTER to toggle logging.\n"
2152 (if erc-debug-irc-protocol "disabled" "enabled")
2153 (current-time-string))))))
2154 (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
2155 (if (and arg
2156 (not (get-buffer-window "*erc-protocol*" t)))
2157 (display-buffer buf t))
2158 (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
2159 (if erc-debug-irc-protocol "enabled" "disabled"))))
2160
2161 ;;; I/O interface
2162
2163 ;; send interface
2164
2165 (defun erc-send-action (tgt str &optional force)
2166 "Send CTCP ACTION information described by STR to TGT."
2167 (erc-send-ctcp-message tgt (format "ACTION %s" str) force)
2168 (erc-display-message
2169 nil 'input (current-buffer)
2170 'ACTION ?n (erc-current-nick) ?a str ?u "" ?h ""))
2171
2172 ;; Display interface
2173
2174 (defun erc-string-invisible-p (string)
2175 "Check whether STRING is invisible or not.
2176 I.e. any char in it has the `invisible' property set."
2177 (text-property-any 0 (length string) 'invisible t string))
2178
2179 (defun erc-display-line-1 (string buffer)
2180 "Display STRING in `erc-mode' BUFFER.
2181 Auxiliary function used in `erc-display-line'. The line gets filtered to
2182 interpret the control characters. Then, `erc-insert-pre-hook' gets called.
2183 If `erc-insert-this' is still t, STRING gets inserted into the buffer.
2184 Afterwards, `erc-insert-modify' and `erc-insert-post-hook' get called.
2185 If STRING is nil, the function does nothing."
2186 (when string
2187 (save-excursion
2188 (set-buffer (or buffer (process-buffer erc-server-process)))
2189 (let ((insert-position (or (marker-position erc-insert-marker)
2190 (point-max))))
2191 (let ((string string) ;; FIXME! Can this be removed?
2192 (buffer-undo-list t)
2193 (inhibit-read-only t))
2194 (unless (string-match "\n$" string)
2195 (setq string (concat string "\n"))
2196 (when (erc-string-invisible-p string)
2197 (erc-put-text-properties 0 (length string) string
2198 '(invisible intangible))))
2199 (erc-log (concat "erc-display-line: " string
2200 (format "(%S)" string) " in buffer "
2201 (format "%s" buffer)))
2202 (setq erc-insert-this t)
2203 (run-hook-with-args 'erc-insert-pre-hook string)
2204 (if (null erc-insert-this)
2205 ;; Leave erc-insert-this set to t as much as possible. Fran
2206 ;; Litterio <franl> has seen erc-insert-this set to nil while
2207 ;; erc-send-pre-hook is running, which should never happen. This
2208 ;; may cure it.
2209 (setq erc-insert-this t)
2210 (save-excursion ;; to restore point in the new buffer
2211 (save-restriction
2212 (widen)
2213 (goto-char insert-position)
2214 (insert-before-markers string)
2215 ;; run insertion hook, with point at restored location
2216 (save-restriction
2217 (narrow-to-region insert-position (point))
2218 (run-hooks 'erc-insert-modify-hook)
2219 (run-hooks 'erc-insert-post-hook))))))
2220 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2221 (point-max))
2222 insert-position))))))
2223
2224 (defun erc-update-undo-list (shift)
2225 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2226 (unless (or (zerop shift) (atom buffer-undo-list))
2227 (let ((list buffer-undo-list) elt)
2228 (while list
2229 (setq elt (car list))
2230 (cond ((integerp elt) ; POSITION
2231 (incf (car list) shift))
2232 ((or (atom elt) ; nil, EXTENT
2233 ;; (eq t (car elt)) ; (t HIGH . LOW)
2234 (markerp (car elt))) ; (MARKER . DISTANCE)
2235 nil)
2236 ((integerp (car elt)) ; (BEGIN . END)
2237 (incf (car elt) shift)
2238 (incf (cdr elt) shift))
2239 ((stringp (car elt)) ; (TEXT . POSITION)
2240 (incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
2241 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2242 (let ((cons (nthcdr 3 elt)))
2243 (incf (car cons) shift)
2244 (incf (cdr cons) shift)))
2245 ((and (featurep 'xemacs)
2246 (extentp (car elt))) ; (EXTENT START END)
2247 (incf (nth 1 elt) shift)
2248 (incf (nth 2 elt) shift)))
2249 (setq list (cdr list))))))
2250
2251 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2252 "Regexp which matches all legal characters in a IRC nickname.")
2253
2254 (defun erc-is-valid-nick-p (nick)
2255 "Check if NICK is a valid IRC nickname."
2256 (string-match (concat "^" erc-valid-nick-regexp "$") nick))
2257
2258 (defun erc-display-line (string &optional buffer)
2259 "Display STRING in the ERC BUFFER.
2260 All screen output must be done through this function. If BUFFER is nil
2261 or omitted, the default ERC buffer for the `erc-session-server' is used.
2262 The BUFFER can be an actual buffer, a list of buffers, 'all or 'active.
2263 If BUFFER = 'all, the string is displayed in all the ERC buffers for the
2264 current session. 'active means the current active buffer
2265 \(`erc-active-buffer'). If the buffer can't be resolved, the current
2266 buffer is used. `erc-display-line-1' is used to display STRING.
2267
2268 If STRING is nil, the function does nothing."
2269 (let ((inhibit-point-motion-hooks t)
2270 new-bufs)
2271 (dolist (buf (cond
2272 ((bufferp buffer) (list buffer))
2273 ((listp buffer) buffer)
2274 ((processp buffer) (list (process-buffer buffer)))
2275 ((eq 'all buffer)
2276 (and (boundp 'erc-server-process)
2277 ;; Hmm, or all of the same session server?
2278 (erc-buffer-list nil erc-server-process)))
2279 ((and (eq 'active buffer) (erc-active-buffer))
2280 (list (erc-active-buffer)))
2281 ((erc-server-buffer-live-p)
2282 (list (process-buffer erc-server-process)))
2283 (t (list (current-buffer)))))
2284 (when (buffer-live-p buf)
2285 (erc-display-line-1 string buf)
2286 (add-to-list 'new-bufs buf)))
2287 (when (null new-bufs)
2288 (if (erc-server-buffer-live-p)
2289 (erc-display-line-1 string (process-buffer erc-server-process))
2290 (erc-display-line-1 string (current-buffer))))))
2291
2292 (defun erc-display-message-highlight (type string)
2293 "Highlight STRING according to TYPE, where erc-TYPE-face is an erc face.
2294
2295 See also `erc-make-notice'"
2296 (cond ((eq type 'notice)
2297 (erc-make-notice string))
2298 (t
2299 (erc-put-text-property
2300 0 (length string)
2301 'face (or (intern-soft
2302 (concat "erc-" (symbol-name type) "-face"))
2303 "erc-default-face")
2304 string)
2305 string)))
2306
2307 (defun erc-display-message (parsed type buffer msg &rest args)
2308 "Display MSG in BUFFER.
2309
2310 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2311
2312 See also `erc-format-message' and `erc-display-line'."
2313 (let ((string (if (symbolp msg)
2314 (apply 'erc-format-message msg args)
2315 msg)))
2316 (setq string
2317 (cond
2318 ((listp type)
2319 (mapc (lambda (type)
2320 (setq string
2321 (erc-display-message-highlight type string)))
2322 type)
2323 string)
2324 ((symbolp type)
2325 (erc-display-message-highlight type string))))
2326
2327 (if (not (erc-response-p parsed))
2328 (erc-display-line string buffer)
2329 (unless (member (erc-response.command parsed) erc-hide-list)
2330 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
2331 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2332 (erc-display-line string buffer)))))
2333
2334 (defun erc-message-type-member (position list)
2335 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2336
2337 This function relies on the erc-parsed text-property being
2338 present."
2339 (let ((prop-val (get-text-property position 'erc-parsed)))
2340 (and prop-val (member (erc-response.command prop-val) list))))
2341
2342 (defvar erc-send-input-line-function 'erc-send-input-line)
2343 (make-variable-buffer-local 'erc-send-input-line-function)
2344
2345 (defun erc-send-input-line (target line &optional force)
2346 "Send LINE to TARGET.
2347
2348 See also `erc-server-send'."
2349 (setq line (format "PRIVMSG %s :%s"
2350 target
2351 ;; If the line is empty, we still want to
2352 ;; send it - i.e. an empty pasted line.
2353 (if (string= line "\n")
2354 " \n"
2355 line)))
2356 (erc-server-send line force target))
2357
2358 (defun erc-get-arglist (fun)
2359 "Return the argument list of a function without the parens."
2360 (let ((arglist (format "%S" (erc-function-arglist fun))))
2361 (if (string-match "^(\\(.*\\))$" arglist)
2362 (match-string 1 arglist)
2363 arglist)))
2364
2365 (defun erc-command-name (cmd)
2366 "For CMD being the function name of a ERC command, something like
2367 erc-cmd-FOO, this returns a string /FOO."
2368 (let ((command-name (symbol-name cmd)))
2369 (if (string-match "^erc-cmd-\\(.*\\)$" command-name)
2370 (concat "/" (match-string 1 command-name))
2371 command-name)))
2372
2373 (defun erc-process-input-line (line &optional force no-command)
2374 "Translate LINE to an RFC1459 command and send it based.
2375 Returns non-nil if the command is actually sent to the server, and nil
2376 otherwise.
2377
2378 If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
2379 it is passed to `erc-cmd-default'. If LINE is not a command (ie. doesn't
2380 start with /<COMMAND>) then it is sent as a message.
2381
2382 An optional FORCE argument forces sending the line when flood
2383 protection is in effect. The optional NO-COMMAND argument prohibits
2384 this function from interpreting the line as a command."
2385 (let ((command-list (erc-extract-command-from-line line)))
2386 (if (and command-list
2387 (not no-command))
2388 (let* ((cmd (nth 0 command-list))
2389 (args (nth 1 command-list)))
2390 (condition-case nil
2391 (if (listp args)
2392 (apply cmd args)
2393 (funcall cmd args))
2394 (wrong-number-of-arguments
2395 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2396 ?c (erc-command-name cmd)
2397 ?u (or (erc-get-arglist cmd)
2398 "")
2399 ?d (format "%s\n"
2400 (or (documentation cmd) "")))
2401 nil)))
2402 (let ((r (erc-default-target)))
2403 (if r
2404 (funcall erc-send-input-line-function r line force)
2405 (erc-display-message nil 'error (current-buffer) 'no-target)
2406 nil)))))
2407
2408 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2409 ;; Input commands handlers
2410 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2411
2412 (defun erc-cmd-AMSG (line)
2413 "Send LINE to all channels of the current server that you are on."
2414 (interactive "sSend to all channels you're on: ")
2415 (setq line (erc-trim-string line))
2416 (erc-with-all-buffers-of-server nil
2417 (lambda ()
2418 (erc-channel-p (erc-default-target)))
2419 (erc-send-message line)))
2420 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2421
2422 (defun erc-cmd-SAY (line)
2423 "Send LINE to the current query or channel as a message, not a command.
2424
2425 Use this when you want to send a message with a leading '/'. Note
2426 that since multi-line messages are never a command, you don't
2427 need this when pasting multiple lines of text."
2428 (if (string-match "^\\s-*$" line)
2429 nil
2430 (string-match "^ ?\\(.*\\)" line)
2431 (erc-process-input-line (match-string 1 line) nil t)))
2432 (put 'erc-cmd-SAY 'do-not-parse-args t)
2433
2434 (defun erc-cmd-SET (line)
2435 "Set the variable named by the first word in LINE to some VALUE.
2436 VALUE is computed by evaluating the rest of LINE in Lisp."
2437 (cond
2438 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2439 (let ((var (read (concat "erc-" (match-string 1 line))))
2440 (val (read (match-string 2 line))))
2441 (if (boundp var)
2442 (progn
2443 (set var (eval val))
2444 (erc-display-message
2445 nil nil 'active (format "Set %S to %S" var val))
2446 t)
2447 (setq var (read (match-string 1 line)))
2448 (if (boundp var)
2449 (progn
2450 (set var (eval val))
2451 (erc-display-message
2452 nil nil 'active (format "Set %S to %S" var val))
2453 t)
2454 (erc-display-message nil 'error 'active 'variable-not-bound)
2455 nil))))
2456 ((string-match "^\\s-*$" line)
2457 (erc-display-line
2458 (concat "Available user variables:\n"
2459 (apply
2460 'concat
2461 (mapcar
2462 (lambda (var)
2463 (let ((val (symbol-value var)))
2464 (concat (format "%S:" var)
2465 (if (consp val)
2466 (concat "\n" (pp-to-string val))
2467 (format " %S\n" val)))))
2468 (apropos-internal "^erc-" 'user-variable-p))))
2469 (current-buffer)) t)
2470 (t nil)))
2471 (defalias 'erc-cmd-VAR 'erc-cmd-SET)
2472 (defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2473 (put 'erc-cmd-SET 'do-not-parse-args t)
2474
2475 (defun erc-cmd-default (line)
2476 "Fallback command.
2477
2478 Commands for which no erc-cmd-xxx exists, are tunnelled through
2479 this function. LINE is sent to the server verbatim, and
2480 therefore has to contain the command itself as well."
2481 (erc-log (format "cmd: DEFAULT: %s" line))
2482 (erc-server-send (substring line 1))
2483 t)
2484
2485 (defun erc-cmd-IGNORE (&optional user)
2486 "Ignore USER. This should be a regexp matching nick!user@host.
2487 If no USER argument is specified, list the contents of `erc-ignore-list'."
2488 (if user
2489 (progn
2490 (erc-display-line
2491 (erc-make-notice (format "Now ignoring %s" user))
2492 'active)
2493 (with-current-buffer (erc-server-buffer)
2494 (add-to-list 'erc-ignore-list user)))
2495 (if (null (with-current-buffer (erc-server-buffer) erc-ignore-list))
2496 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2497 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2498 (mapc #'(lambda (item)
2499 (erc-display-line (erc-make-notice item)
2500 'active))
2501 (with-current-buffer (erc-server-buffer) erc-ignore-list))))
2502 t)
2503
2504 (defun erc-cmd-UNIGNORE (user)
2505 "Remove the user specified in USER from the ignore list."
2506 (let ((ignored-nick (car (with-current-buffer (erc-server-buffer)
2507 (erc-member-ignore-case user erc-ignore-list)))))
2508 (if (null ignored-nick)
2509 (erc-display-line
2510 (erc-make-notice (format "%s is not currently ignored!" user))
2511 'active)
2512 (erc-display-line
2513 (erc-make-notice (format "No longer ignoring %s" user))
2514 'active))
2515 (with-current-buffer (erc-server-buffer)
2516 (setq erc-ignore-list (delete ignored-nick erc-ignore-list))))
2517 t)
2518
2519 (defun erc-cmd-CLEAR ()
2520 "Clear the window content."
2521 (recenter 0)
2522 t)
2523
2524 (defun erc-cmd-OPS ()
2525 "Show the ops in the current channel."
2526 (interactive)
2527 (let ((ops nil))
2528 (if erc-channel-users
2529 (maphash (lambda (nick user-data)
2530 (let ((cuser (cdr user-data)))
2531 (if (and cuser
2532 (erc-channel-user-op cuser))
2533 (setq ops (cons (erc-server-user-nickname
2534 (car user-data))
2535 ops)))))
2536 erc-channel-users))
2537 (setq ops (sort ops 'string-lessp))
2538 (if ops
2539 (erc-display-message
2540 nil 'notice (current-buffer) 'ops
2541 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2542 ?o (mapconcat 'identity ops " "))
2543 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2544 t)
2545
2546 (defun erc-cmd-COUNTRY (tld)
2547 "Display the country associated with the top level domain TLD."
2548 (require 'mail-extr)
2549 (let ((co (ignore-errors (what-domain tld))))
2550 (if co
2551 (erc-display-message
2552 nil 'notice 'active 'country ?c co ?d tld)
2553 (erc-display-message
2554 nil 'notice 'active 'country-unknown ?d tld))
2555 t))
2556
2557 (defun erc-cmd-AWAY (line)
2558 "Mark the user as being away, the reason being indicated by LINE.
2559 If no reason is given, unset away status."
2560 (when (string-match "^\\s-*\\(.*\\)$" line)
2561 (let ((reason (match-string 1 line)))
2562 (erc-log (format "cmd: AWAY: %s" reason))
2563 (erc-server-send
2564 (if (string= reason "")
2565 "AWAY"
2566 (concat "AWAY :" reason))))
2567 t))
2568 (put 'erc-cmd-AWAY 'do-not-parse-args t)
2569
2570 (defun erc-cmd-GAWAY (line)
2571 "Mark the user as being away everywhere, the reason being indicated by LINE."
2572 ;; on all server buffers.
2573 (erc-with-all-buffers-of-server nil
2574 #'erc-server-buffer-p
2575 (erc-cmd-AWAY line)))
2576 (put 'erc-cmd-GAWAY 'do-not-parse-args t)
2577
2578 (defun erc-cmd-CTCP (nick cmd &rest args)
2579 "Send a Client To Client Protocol message to NICK.
2580
2581 CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2582 VERSION and so on. It is called with ARGS."
2583 (let ((str (concat cmd
2584 (when args
2585 (concat " " (mapconcat #'identity args " "))))))
2586 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2587 (erc-send-ctcp-message nick str)
2588 t))
2589
2590 (defun erc-cmd-HELP (&optional func)
2591 "Popup help information.
2592
2593 If FUNC contains a valid function or variable, help about that
2594 will be displayed. If FUNC is empty, display an apropos about
2595 erc commands. Otherwise, do apropos in the erc namespace
2596 \(\"erc-.*LINE\"\).
2597
2598 Examples:
2599 To find out about erc and bbdb, do
2600 /help bbdb.*
2601
2602 For help about the WHOIS command, do:
2603 /help whois
2604
2605 For a list of user commands (/join /part, ...):
2606 /help."
2607 (if func
2608 (let* ((sym (or (let ((sym (intern-soft
2609 (concat "erc-cmd-" (upcase func)))))
2610 (if (and sym (or (boundp sym) (fboundp sym)))
2611 sym
2612 nil))
2613 (let ((sym (intern-soft func)))
2614 (if (and sym (or (boundp sym) (fboundp sym)))
2615 sym
2616 nil))
2617 (let ((sym (intern-soft (concat "erc-" func))))
2618 (if (and sym (or (boundp sym) (fboundp sym)))
2619 sym
2620 nil)))))
2621 (if sym
2622 (cond
2623 ((boundp sym) (describe-variable sym))
2624 ((fboundp sym) (describe-function sym))
2625 (t nil))
2626 (apropos-command (concat "erc-.*" func) nil
2627 (lambda (x)
2628 (or (commandp x)
2629 (get x 'custom-type))))
2630 t))
2631 (apropos "erc-cmd-")
2632 (message "Type C-h m to get additional information about keybindings.")
2633 t))
2634
2635 (defalias 'erc-cmd-H 'erc-cmd-HELP)
2636
2637 (defun erc-cmd-JOIN (channel &optional key)
2638 "Join the channel given in CHANNEL, optionally with KEY.
2639 If CHANNEL is specified as \"-invite\", join the channel to which you
2640 were most recently invited. See also `invitation'."
2641 (let (chnl)
2642 (if (string= (upcase channel) "-INVITE")
2643 (if erc-invitation
2644 (setq chnl erc-invitation)
2645 (erc-display-message nil 'error (current-buffer) 'no-invitation))
2646 (setq chnl (erc-ensure-channel-name channel)))
2647 (when chnl
2648 ;; Prevent double joining of same channel on same server.
2649 (let ((joined-channels
2650 (mapcar #'(lambda (chanbuf)
2651 (with-current-buffer chanbuf (erc-default-target)))
2652 (erc-channel-list erc-server-process))))
2653 (if (erc-member-ignore-case chnl joined-channels)
2654 (switch-to-buffer (car (erc-member-ignore-case chnl
2655 joined-channels)))
2656 (erc-log (format "cmd: JOIN: %s" chnl))
2657 (if (and chnl key)
2658 (erc-server-send (format "JOIN %s %s" chnl key))
2659 (erc-server-send (format "JOIN %s" chnl)))))))
2660 t)
2661
2662 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
2663 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
2664
2665 (defvar erc-channel-new-member-names nil
2666 "If non-nil, a names list is currently being received.
2667
2668 If non-nil, this variable is a hash-table that associates
2669 received nicks with t.")
2670 (make-variable-buffer-local 'erc-channel-new-member-names)
2671
2672 (defun erc-cmd-NAMES (&optional channel)
2673 "Display the users in CHANNEL.
2674 If CHANNEL is not specified, display the users in the current channel.
2675 This function clears the channel name list first, then sends the
2676 command."
2677 (let ((tgt (or (and (erc-channel-p channel) channel)
2678 (erc-default-target))))
2679 (if (and tgt (erc-channel-p tgt))
2680 (progn
2681 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
2682 (erc-with-buffer
2683 (tgt)
2684 (erc-channel-begin-receiving-names))
2685 (erc-server-send (concat "NAMES " tgt)))
2686 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
2687 t)
2688 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
2689
2690 (defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
2691 "Kick the user indicated in LINE from the current channel.
2692 LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
2693 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
2694 (if (string= "" reasonstring)
2695 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
2696 (if (erc-channel-p target)
2697 (let ((nick reason-or-nick))
2698 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
2699 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
2700 nil target)
2701 t)
2702 (when target
2703 (let ((ch (erc-default-target)))
2704 (setq reasonstring (concat
2705 (if reason-or-nick (concat reason-or-nick " "))
2706 reasonstring))
2707 (if ch
2708 (progn
2709 (erc-log
2710 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
2711 (erc-server-send
2712 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
2713 (erc-display-message nil 'error (current-buffer)
2714 'no-default-channel))
2715 t)))))
2716
2717 (defvar erc-script-args nil)
2718
2719 (defun erc-cmd-LOAD (line)
2720 "Load the script provided in the LINE.
2721 If LINE continues beyond the file name,
2722 the rest of it is put in a (local) variable
2723 `erc-script-args', which can be used in elisp scripts.
2724
2725 The optional FORCE argument is ignored here - you can't force loading
2726 a script after exceeding the flood threshold."
2727 (cond
2728 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
2729 (let* ((file-to-find (match-string 1 line))
2730 (erc-script-args (match-string 2 line))
2731 (file (erc-find-file file-to-find erc-script-path)))
2732 (erc-log (format "cmd: LOAD: %s" file-to-find))
2733 (cond
2734 ((not file)
2735 (erc-display-message nil 'error (current-buffer)
2736 'cannot-find-file ?f file-to-find))
2737 ((not (file-readable-p file))
2738 (erc-display-message nil 'error (current-buffer)
2739 'cannot-read-file ?f file))
2740 (t
2741 (message "Loading \'%s\'..." file)
2742 (erc-load-script file)
2743 (message "Loading \'%s\'...done" file))))
2744 t)
2745 (t nil)))
2746
2747 (defun erc-cmd-WHOIS (user &optional server)
2748 "Display whois information for USER.
2749
2750 If SERVER is non-nil, use that, rather than the current server."
2751 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
2752 (let ((send (if server
2753 (format "WHOIS %s %s" user server)
2754 (format "WHOIS %s" user))))
2755 (erc-log (format "cmd: %s" send))
2756 (erc-server-send send)
2757 t))
2758 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
2759
2760 (defun erc-cmd-WHOAMI ()
2761 "Display whois information about yourself."
2762 (erc-cmd-WHOIS (erc-current-nick))
2763 t)
2764
2765 (defun erc-cmd-IDLE (nick)
2766 "Show the length of time NICK has been idle."
2767 (let ((serverbuf (erc-server-buffer))
2768 (origbuf (current-buffer))
2769 symlist)
2770 (with-current-buffer serverbuf
2771 (add-to-list 'symlist
2772 (cons (erc-once-with-server-event
2773 311 `(string= ,nick
2774 (second
2775 (erc-response.command-args parsed))))
2776 'erc-server-311-functions))
2777 (add-to-list 'symlist
2778 (cons (erc-once-with-server-event
2779 312 `(string= ,nick
2780 (second
2781 (erc-response.command-args parsed))))
2782 'erc-server-312-functions))
2783 (add-to-list 'symlist
2784 (cons (erc-once-with-server-event
2785 318 `(string= ,nick
2786 (second
2787 (erc-response.command-args parsed))))
2788 'erc-server-318-functions))
2789 (add-to-list 'symlist
2790 (cons (erc-once-with-server-event
2791 319 `(string= ,nick
2792 (second
2793 (erc-response.command-args parsed))))
2794 'erc-server-319-functions))
2795 (add-to-list 'symlist
2796 (cons (erc-once-with-server-event
2797 320 `(string= ,nick
2798 (second
2799 (erc-response.command-args parsed))))
2800 'erc-server-320-functions))
2801 (add-to-list 'symlist
2802 (cons (erc-once-with-server-event
2803 330 `(string= ,nick
2804 (second
2805 (erc-response.command-args parsed))))
2806 'erc-server-330-functions))
2807 (add-to-list 'symlist
2808 (cons (erc-once-with-server-event
2809 317
2810 `(let ((idleseconds
2811 (string-to-number
2812 (third
2813 (erc-response.command-args parsed)))))
2814 (erc-display-line
2815 (erc-make-notice
2816 (format "%s has been idle for %s."
2817 (erc-string-no-properties ,nick)
2818 (erc-seconds-to-string idleseconds)))
2819 ,origbuf))
2820 t)
2821 'erc-server-317-functions))
2822
2823 ;; Send the WHOIS command.
2824 (erc-cmd-WHOIS nick)
2825
2826 ;; Remove the uninterned symbols from the server hooks that did not run.
2827 (run-at-time 20 nil `(lambda ()
2828 (with-current-buffer ,(current-buffer)
2829 (dolist (sym ',symlist)
2830 (let ((hooksym (cdr sym))
2831 (funcsym (car sym)))
2832 (remove-hook hooksym funcsym t))))))))
2833 t)
2834
2835 (defun erc-cmd-DESCRIBE (line)
2836 "Pose some action to a certain user.
2837 LINE has the format \"USER ACTION\"."
2838 (cond
2839 ((string-match
2840 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
2841 (let ((dst (match-string 1 line))
2842 (s (match-string 2 line)))
2843 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
2844 (erc-send-action dst s))
2845 t)
2846 (t nil)))
2847 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
2848
2849 (defun erc-cmd-ME (line)
2850 "Send LINE as an action."
2851 (cond
2852 ((string-match "^\\s-\\(.*\\)$" line)
2853 (let ((s (match-string 1 line)))
2854 (erc-log (format "cmd: ME: %s" s))
2855 (erc-send-action (erc-default-target) s))
2856 t)
2857 (t nil)))
2858 (put 'erc-cmd-ME 'do-not-parse-args t)
2859
2860 (defun erc-cmd-LASTLOG (line)
2861 "Show all lines in the current buffer matching the regexp LINE.
2862
2863 If a match spreads across multiple lines, all those lines are shown.
2864
2865 The lines are shown in a buffer named `*Occur*'.
2866 It serves as a menu to find any of the occurrences in this buffer.
2867 \\[describe-mode] in that buffer will explain how.
2868
2869 If LINE contains upper case characters (excluding those preceded by `\'),
2870 the matching is case-sensitive."
2871 (occur line)
2872 t)
2873 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
2874
2875 (defun erc-send-message (line &optional force)
2876 "Send LINE to the current channel or user and display it.
2877
2878 See also `erc-message' and `erc-display-line'."
2879 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
2880 (erc-display-line
2881 (concat (erc-format-my-nick) line)
2882 (current-buffer))
2883 ;; FIXME - treat multiline, run hooks, or remove me?
2884 t)
2885
2886 (defun erc-cmd-MODE (line)
2887 "Change or display the mode value of a channel or user.
2888 The first word specifies the target. The rest is the mode string
2889 to send.
2890
2891 If only one word is given, display the mode of that target.
2892
2893 A list of valid mode strings for Freenode may be found at
2894 `http://freenode.net/using_the_network.shtml'."
2895 (cond
2896 ((string-match "^\\s-\\(.*\\)$" line)
2897 (let ((s (match-string 1 line)))
2898 (erc-log (format "cmd: MODE: %s" s))
2899 (erc-server-send (concat "MODE " line)))
2900 t)
2901 (t nil)))
2902 (put 'erc-cmd-MODE 'do-not-parse-args t)
2903
2904 (defun erc-cmd-NOTICE (channel-or-user &rest message)
2905 "Send a notice to the channel or user given as the first word.
2906 The rest is the message to send."
2907 (erc-message "NOTICE" (concat channel-or-user " "
2908 (mapconcat #'identity message " "))))
2909
2910 (defun erc-cmd-MSG (line)
2911 "Send a message to the channel or user given as the first word in LINE.
2912
2913 The rest of LINE is the message to send."
2914 (erc-message "PRIVMSG" line))
2915
2916 (defalias 'erc-cmd-M 'erc-cmd-MSG)
2917 (put 'erc-cmd-MSG 'do-not-parse-args t)
2918
2919 (defun erc-cmd-SQUERY (line)
2920 "Send a Service Query to the service given as the first word in LINE.
2921
2922 The rest of LINE is the message to send."
2923 (erc-message "SQUERY" line))
2924
2925 (defun erc-cmd-NICK (nick)
2926 "Change current nickname to NICK."
2927 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
2928 (let ((nicklen (cdr (assoc "NICKLEN" (with-current-buffer (erc-server-buffer)
2929 erc-server-parameters)))))
2930 (and nicklen (> (length nick) (string-to-number nicklen))
2931 (erc-display-message
2932 nil 'notice 'active 'nick-too-long
2933 ?i (length nick) ?l nicklen)))
2934 (erc-server-send (format "NICK %s" nick))
2935 (cond (erc-bad-nick
2936 (erc-set-current-nick nick)
2937 (erc-update-mode-line)
2938 (setq erc-bad-nick nil)))
2939 t)
2940
2941 (defun erc-cmd-PART (line)
2942 "When LINE is an empty string, leave the current channel.
2943 Otherwise leave the channel indicated by LINE."
2944 (cond
2945 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
2946 (let* ((ch (match-string 1 line))
2947 (msg (match-string 2 line))
2948 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
2949 (erc-log (format "cmd: PART: %s: %s" ch reason))
2950 (erc-server-send (if (string= reason "")
2951 (format "PART %s" ch)
2952 (format "PART %s :%s" ch reason))
2953 nil ch))
2954 t)
2955 ((string-match "^\\s-*\\(.*\\)$" line)
2956 (let* ((ch (erc-default-target))
2957 (msg (match-string 1 line))
2958 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
2959 (if (and ch (erc-channel-p ch))
2960 (progn
2961 (erc-log (format "cmd: PART: %s: %s" ch reason))
2962 (erc-server-send (if (string= reason "")
2963 (format "PART %s" ch)
2964 (format "PART %s :%s" ch reason))
2965 nil ch))
2966 (erc-display-message nil 'error (current-buffer) 'no-target)))
2967 t)
2968 (t nil)))
2969 (put 'erc-cmd-PART 'do-not-parse-args t)
2970
2971 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
2972
2973 (defun erc-cmd-PING (recipient)
2974 "Ping RECIPIENT."
2975 (let ((time (format "%f" (erc-current-time))))
2976 (erc-log (format "cmd: PING: %s" time))
2977 (erc-cmd-CTCP recipient "PING" time)))
2978
2979 (defun erc-cmd-QUOTE (line)
2980 "Send LINE directly to the server.
2981 All the text given as argument is sent to the sever as unmodified,
2982 just as you provided it. Use this command with care!"
2983 (cond
2984 ((string-match "^\\s-\\(.+\\)$" line)
2985 (erc-server-send (match-string 1 line)))
2986 (t nil)))
2987 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
2988
2989 (defun erc-cmd-QUERY (&optional user)
2990 "Open a query with USER.
2991 The type of query window/frame/etc will depend on the value of
2992 `erc-join-buffer'. If USER is omitted, close the current query buffer if one
2993 exists - except this is broken now ;-)"
2994 (interactive
2995 (list (read-from-minibuffer "Start a query with: " nil)))
2996 (let ((session-buffer (erc-server-buffer)))
2997 (if user
2998 (erc-query user session-buffer)
2999 ;; currently broken, evil hack to display help anyway
3000 ;(erc-delete-query))))
3001 (signal 'wrong-number-of-arguments ""))))
3002 (defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3003
3004 (defun erc-quit-reason-normal (&optional s)
3005 "Normal quit message.
3006
3007 If S is non-nil, it will be used as the quit reason."
3008 (or s
3009 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3010 erc-version-string) ; erc-official-location)
3011 ))
3012
3013 (defun erc-quit-reason-zippy (&optional s)
3014 "Zippy quit message.
3015
3016 If S is non-nil, it will be used as the quit reason."
3017 (or s
3018 (erc-replace-regexp-in-string "\n" "" (yow))))
3019
3020 (defun erc-quit-reason-various (s)
3021 "Choose a quit reason based on S (a string)."
3022 (when (featurep 'xemacs) (require 'poe))
3023 (let ((res (car (assoc-default (or s "")
3024 erc-quit-reason-various-alist 'string-match))))
3025 (cond
3026 ((functionp res) (funcall res))
3027 ((stringp res) res)
3028 ;; hopefully never reached
3029 (s))))
3030
3031 (defun erc-part-reason-normal (&optional s)
3032 "Normal part message.
3033
3034 If S is non-nil, it will be used as the quit reason."
3035 (or s
3036 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3037 erc-version-string) ; erc-official-location)
3038 ))
3039
3040 (defun erc-part-reason-zippy (&optional s)
3041 "Zippy part message.
3042
3043 If S is non-nil, it will be used as the quit reason."
3044 (or s
3045 (erc-replace-regexp-in-string "\n" "" (yow))))
3046
3047 (defun erc-part-reason-various (s)
3048 "Choose a part reason based on S (a string)."
3049 (when (featurep 'xemacs) (require 'poe))
3050 (let ((res (car (assoc-default (or s "")
3051 erc-part-reason-various-alist 'string-match))))
3052 (cond
3053 ((functionp res) (funcall res))
3054 ((stringp res) res)
3055 (s))))
3056
3057 (defun erc-cmd-QUIT (reason)
3058 "Disconnect from the current server.
3059 If REASON is omitted, display a default quit message, otherwise display
3060 the message given by REASON."
3061 (unless reason
3062 (setq reason ""))
3063 (cond
3064 ((string-match "^\\s-*\\(.*\\)$" reason)
3065 (let* ((s (match-string 1 reason))
3066 (buffer (erc-server-buffer))
3067 (reason (funcall erc-quit-reason (if (equal s "") nil s))))
3068 (with-current-buffer (if (and buffer
3069 (bufferp buffer))
3070 buffer
3071 (current-buffer))
3072 (erc-log (format "cmd: QUIT: %s" reason))
3073 (setq erc-server-quitting t)
3074 (erc-set-active-buffer (erc-server-buffer))
3075 (erc-server-send (format "QUIT :%s" reason)))
3076 (run-hook-with-args 'erc-quit-hook erc-server-process)
3077 (when erc-kill-queries-on-quit
3078 (erc-kill-query-buffers erc-server-process)))
3079 t)
3080 (t nil)))
3081
3082 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3083 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3084 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3085 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3086
3087 (defun erc-cmd-GQUIT (reason)
3088 "Disconnect from all servers at once with the same quit REASON."
3089 (erc-with-all-buffers-of-server nil #'(lambda ()
3090 (and (erc-server-buffer-p)
3091 (erc-server-process-alive)))
3092 (erc-cmd-QUIT reason)))
3093
3094 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3095 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3096
3097 (defun erc-cmd-SERVER (server)
3098 "Connect to SERVER, leaving existing connection intact."
3099 (erc-log (format "cmd: SERVER: %s" server))
3100 (condition-case nil
3101 (erc-select :server server :nick (erc-current-nick))
3102 (error
3103 (message "Cannot find host %s." server)
3104 (beep)))
3105 t)
3106
3107 (eval-when-compile
3108 (defvar motif-version-string)
3109 (defvar gtk-version-string))
3110
3111 (defun erc-cmd-SV ()
3112 "Say the current ERC and Emacs version into channel."
3113 (erc-send-message (format "I'm using ERC %s with %s %s (%s%s%s)!"
3114 erc-version-string
3115 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3116 emacs-version
3117 system-configuration
3118 (concat
3119 (cond ((featurep 'motif)
3120 (concat ", " (substring
3121 motif-version-string 4)))
3122 ((featurep 'gtk)
3123 (concat ", GTK+ Version "
3124 gtk-version-string))
3125 ((featurep 'mac-carbon) ", Mac Carbon")
3126 ((featurep 'x-toolkit) ", X toolkit")
3127 (t ""))
3128 (if (and (boundp 'x-toolkit-scroll-bars)
3129 (memq x-toolkit-scroll-bars
3130 '(xaw xaw3d)))
3131 (format ", %s scroll bars"
3132 (capitalize (symbol-name
3133 x-toolkit-scroll-bars)))
3134 "")
3135 (if (featurep 'multi-tty) ", multi-tty" ""))
3136 (concat ", built " erc-emacs-build-time)))
3137 t)
3138
3139 (defun erc-cmd-SM ()
3140 "Say the current ERC modes into channel."
3141 (erc-send-message (format "I'm using the following modules: %s!"
3142 (erc-modes)))
3143 t)
3144
3145 (defun erc-cmd-SMV ()
3146 "Say the current ERC module versions into channel."
3147 (erc-send-message (format "I'm using the following module revisions: %s!"
3148 (erc-version-modules)))
3149 t)
3150
3151 (defun erc-cmd-DEOP (&rest people)
3152 "Remove the operator setting from user(s) given in PEOPLE."
3153 (when (> (length people) 0)
3154 (erc-server-send (concat "MODE " (erc-default-target)
3155 " -"
3156 (make-string (length people) ?o)
3157 " "
3158 (mapconcat 'identity people " ")))
3159 t))
3160
3161 (defun erc-cmd-OP (&rest people)
3162 "Add the operator setting to users(s) given in PEOPLE."
3163 (when (> (length people) 0)
3164 (erc-server-send (concat "MODE " (erc-default-target)
3165 " +"
3166 (make-string (length people) ?o)
3167 " "
3168 (mapconcat 'identity people " ")))
3169 t))
3170
3171 (defun erc-cmd-TIME (&optional line)
3172 "Request the current time and date from the current server."
3173 (cond
3174 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3175 (let ((args (match-string 1 line)))
3176 (erc-log (format "cmd: TIME: %s" args))
3177 (erc-server-send (concat "TIME " args)))
3178 t)
3179 (t (erc-server-send "TIME"))))
3180 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3181
3182 (defun erc-cmd-TOPIC (topic)
3183 "Set or request the topic for a channel.
3184 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3185 or the empty string.
3186
3187 If no #CHANNEL is given, the default channel is used. If TOPIC is
3188 given, the channel topic is modified, otherwise the current topic will
3189 be displayed."
3190 (cond
3191 ;; /topic #channel TOPIC
3192 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3193 (let ((ch (match-string 1 topic))
3194 (topic (match-string 2 topic)))
3195 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3196 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3197 t)
3198 ;; /topic #channel
3199 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3200 (let ((ch (match-string 1 topic)))
3201 (erc-server-send (format "TOPIC %s" ch) nil ch)
3202 t))
3203 ;; /topic
3204 ((string-match "^\\s-*$" topic)
3205 (let ((ch (erc-default-target)))
3206 (erc-server-send (format "TOPIC %s" ch) nil ch)
3207 t))
3208 ;; /topic TOPIC
3209 ((string-match "^\\s-*\\(.*\\)$" topic)
3210 (let ((ch (erc-default-target))
3211 (topic (match-string 1 topic)))
3212 (if (and ch (erc-channel-p ch))
3213 (progn
3214 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3215 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3216 (erc-display-message nil 'error (current-buffer) 'no-target)))
3217 t)
3218 (t nil)))
3219 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3220 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3221
3222 (defun erc-cmd-APPENDTOPIC (topic)
3223 "Append TOPIC to the current channel topic, separated by a space."
3224 (let ((oldtopic erc-channel-topic))
3225 ;; display help when given no arguments
3226 (when (string-match "^\\s-*$" topic)
3227 (signal 'wrong-number-of-arguments nil))
3228 ;; strip trailing ^O
3229 (when (string-match "\\(.*\\)\C-o" oldtopic)
3230 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3231 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3232 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3233
3234 (defun erc-cmd-CLEARTOPIC (&optional channel)
3235 "Clear the topic for a CHANNEL.
3236 If CHANNEL is not specified, clear the topic for the default channel."
3237 (interactive "sClear topic of channel (RET is current channel): ")
3238 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3239 (when chnl
3240 (erc-server-send (format "TOPIC %s :" chnl))
3241 t)))
3242
3243 ;;; Banlists
3244
3245 (defvar erc-channel-banlist nil
3246 "A list of bans seen for the current channel.
3247
3248 Each ban is an alist of the form:
3249 (WHOSET . MASK)
3250
3251 The property `received-from-server' indicates whether
3252 or not the ban list has been requested from the server.")
3253 (make-variable-buffer-local 'erc-channel-banlist)
3254 (put 'erc-channel-banlist 'received-from-server nil)
3255
3256 (defun erc-cmd-BANLIST ()
3257 "Pretty-print the contents of `erc-channel-banlist'.
3258
3259 The ban list is fetched from the server if necessary."
3260 (let ((chnl (erc-default-target))
3261 (chnl-name (buffer-name)))
3262
3263 (cond
3264 ((not (erc-channel-p chnl))
3265 (erc-display-line (erc-make-notice "You're not on a channel\n")
3266 'active))
3267
3268 ((not (get 'erc-channel-banlist 'received-from-server))
3269 (let ((old-367-hook erc-server-367-functions))
3270 (setq erc-server-367-functions 'erc-banlist-store
3271 erc-channel-banlist nil)
3272 ;; fetch the ban list then callback
3273 (with-current-buffer (erc-server-buffer)
3274 (erc-once-with-server-event
3275 368
3276 `(with-current-buffer ,chnl-name
3277 (put 'erc-channel-banlist 'received-from-server t)
3278 (setq erc-server-367-functions ',old-367-hook)
3279 (erc-cmd-BANLIST)
3280 t))
3281 (erc-server-send (format "MODE %s b" chnl)))))
3282
3283 ((null erc-channel-banlist)
3284 (erc-display-line (erc-make-notice
3285 (format "No bans for channel: %s\n" chnl))
3286 'active)
3287 (put 'erc-channel-banlist 'received-from-server nil))
3288
3289 (t
3290 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3291 erc-fill-column)
3292 (and (boundp 'fill-column)
3293 fill-column)
3294 (1- (window-width))))
3295 (separator (make-string erc-fill-column (string-to-char "=")))
3296 (fmt (concat
3297 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3298 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3299
3300 (erc-display-line
3301 (erc-make-notice (format "Ban list for channel: %s\n"
3302 (erc-default-target)))
3303 'active)
3304
3305 (erc-display-line separator 'active)
3306 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3307 (erc-display-line separator 'active)
3308
3309 (mapc
3310 (lambda (x)
3311 (erc-display-line
3312 (format fmt
3313 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3314 (if (car x)
3315 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3316 ""))
3317 'active))
3318 erc-channel-banlist)
3319
3320 (erc-display-line (erc-make-notice "End of Ban list")
3321 'active)
3322 (put 'erc-channel-banlist 'received-from-server nil)))))
3323 t)
3324
3325 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3326
3327 (defun erc-cmd-MASSUNBAN ()
3328 "Mass Unban.
3329
3330 Unban all currently banned users in the current channel."
3331 (let ((chnl (erc-default-target)))
3332 (cond
3333
3334 ((not (erc-channel-p chnl))
3335 (erc-display-line
3336 (erc-make-notice "You're not on a channel\n")
3337 'active))
3338
3339 ((not (get 'erc-channel-banlist 'received-from-server))
3340 (let ((old-367-hook erc-server-367-functions))
3341 (setq erc-server-367-functions 'erc-banlist-store)
3342 ;; fetch the ban list then callback
3343 (with-current-buffer (erc-server-buffer)
3344 (erc-once-with-server-event
3345 368
3346 `(with-current-buffer ,chnl
3347 (put 'erc-channel-banlist 'received-from-server t)
3348 (setq erc-server-367-functions ,old-367-hook)
3349 (erc-cmd-MASSUNBAN)
3350 t))
3351 (erc-server-send (format "MODE %s b" chnl)))))
3352
3353 (t (let ((bans (mapcar 'cdr erc-channel-banlist)))
3354 (when bans
3355 ;; Glob the bans into groups of three, and carry out the unban.
3356 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3357 (mapc
3358 (lambda (x)
3359 (erc-server-send
3360 (format "MODE %s -%s %s" (erc-default-target)
3361 (make-string (length x) (string-to-char "b"))
3362 (mapconcat 'identity x " "))))
3363 (erc-group-list bans 3))))
3364 t))))
3365
3366 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3367
3368 ;;;; End of IRC commands
3369
3370 (defun erc-ensure-channel-name (channel)
3371 "Return CHANNEL if it is a valid channel name.
3372 Eventually add a # in front of it, if that turns it into a valid channel name."
3373 (if (erc-channel-p channel)
3374 channel
3375 (concat "#" channel)))
3376
3377 (defun erc-grab-region (start end)
3378 "Copy the region between START and END in a recreatable format.
3379
3380 Converts all the IRC text properties in each line of the region
3381 into control codes and writes them to a separate buffer. The
3382 resulting text may be used directly as a script to generate this
3383 text again."
3384 (interactive "r")
3385 (erc-set-active-buffer (current-buffer))
3386 (save-excursion
3387 (let* ((cb (current-buffer))
3388 (buf (generate-new-buffer erc-grab-buffer-name))
3389 (region (buffer-substring start end))
3390 (lines (erc-split-multiline-safe region)))
3391 (set-buffer buf)
3392 (dolist (line lines)
3393 (insert (concat line "\n")))
3394 (set-buffer cb)
3395 (switch-to-buffer-other-window buf)))
3396 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3397 (ding))
3398
3399 (defun erc-display-prompt (&optional buffer pos prompt face)
3400 "Display PROMPT in BUFFER at position POS.
3401 Display an ERC prompt in BUFFER.
3402
3403 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3404 If BUFFER is nil, the `current-buffer' is used.
3405 If POS is nil, PROMPT will be displayed at `point'.
3406 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3407 `erc-prompt-face' will be used."
3408 (let* ((prompt (or prompt (erc-prompt)))
3409 (l (length prompt))
3410 (ob (current-buffer)))
3411 ;; We cannot use save-excursion because we move point, therefore
3412 ;; we resort to the ol' ob trick to restore this.
3413 (when (and buffer (bufferp buffer))
3414 (set-buffer buffer))
3415
3416 ;; now save excursion again to store where point and mark are
3417 ;; in the current buffer
3418 (save-excursion
3419 (setq pos (or pos (point)))
3420 (goto-char pos)
3421 (when (> l 0)
3422 ;; Do not extend the text properties when typing at the end
3423 ;; of the prompt, but stuff typed in front of the prompt
3424 ;; shall remain part of the prompt.
3425 (setq prompt (erc-propertize prompt
3426 'start-open t ; XEmacs
3427 'rear-nonsticky t ; Emacs
3428 'erc-prompt t
3429 'front-sticky t
3430 'read-only t))
3431 (erc-put-text-property 0 (1- (length prompt))
3432 'face (or face 'erc-prompt-face)
3433 prompt)
3434 (insert prompt))
3435 ;; Set the input marker
3436 (set-marker erc-input-marker (point)))
3437
3438 ;; Now we are back at the old position. If the prompt was
3439 ;; inserted here or before us, advance point by the length of
3440 ;; the prompt.
3441 (when (or (not pos) (<= (point) pos))
3442 (forward-char l))
3443 ;; Clear the undo buffer now, so the user can undo his stuff,
3444 ;; but not the stuff we did. Sneaky!
3445 (setq buffer-undo-list nil)
3446 (set-buffer ob)))
3447
3448 ;; interactive operations
3449
3450 (defun erc-input-message ()
3451 "Read input from the minibuffer."
3452 (interactive)
3453 (let ((minibuffer-allow-text-properties t)
3454 (read-map minibuffer-local-map))
3455 (insert (read-from-minibuffer "Message: "
3456 (string last-command-char) read-map))
3457 (erc-send-current-line)))
3458
3459 (defvar erc-action-history-list ()
3460 "History list for interactive action input.")
3461
3462 (defun erc-input-action ()
3463 "Interactively input a user action and send it to IRC."
3464 (interactive "")
3465 (erc-set-active-buffer (current-buffer))
3466 (let ((action (read-from-minibuffer
3467 "Action: " nil nil nil 'erc-action-history-list)))
3468 (if (not (string-match "^\\s-*$" action))
3469 (erc-send-action (erc-default-target) action))))
3470
3471 (defun erc-join-channel (channel &optional key)
3472 "Join CHANNEL.
3473
3474 If `point' is at the beginning of a channel name, use that as default."
3475 (interactive
3476 (list
3477 (let ((chnl (if (looking-at "\\([&#+!][^ ]+\\)") (match-string 1) ""))
3478 (table (when (erc-server-buffer-live-p)
3479 (set-buffer (process-buffer erc-server-process))
3480 erc-channel-list)))
3481 (completing-read "Join channel: " table nil nil nil nil chnl))
3482 (when erc-prompt-for-channel-key
3483 (read-from-minibuffer "Channel key (RET for none): " nil))))
3484 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3485
3486 (defun erc-part-from-channel (reason)
3487 "Part from the current channel and prompt for a REASON."
3488 (interactive
3489 (list
3490 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3491 reason
3492 (read-from-minibuffer (concat "Leave " (erc-default-target)
3493 ", Reason? ")
3494 (cons "No reason" 0)))))
3495 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3496
3497 (defun erc-set-topic (topic)
3498 "Prompt for a TOPIC for the current channel."
3499 (interactive
3500 (list
3501 (read-from-minibuffer
3502 (concat "Set topic of " (erc-default-target) ": ")
3503 (when erc-channel-topic
3504 (cons (apply 'concat (butlast (split-string erc-channel-topic "\C-o")))
3505 0)))))
3506 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3507 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3508
3509 (defun erc-set-channel-limit (&optional limit)
3510 "Set a LIMIT for the current channel. Remove limit if nil.
3511 Prompt for one if called interactively."
3512 (interactive (list (read-from-minibuffer
3513 (format "Limit for %s (RET to remove limit): "
3514 (erc-default-target)))))
3515 (let ((tgt (erc-default-target)))
3516 (if (and limit (>= (length limit) 1))
3517 (erc-server-send (format "MODE %s +l %s" tgt limit))
3518 (erc-server-send (format "MODE %s -l" tgt)))))
3519
3520 (defun erc-set-channel-key (&optional key)
3521 "Set a KEY for the current channel. Remove key if nil.
3522 Prompt for one if called interactively."
3523 (interactive (list (read-from-minibuffer
3524 (format "Key for %s (RET to remove key): "
3525 (erc-default-target)))))
3526 (let ((tgt (erc-default-target)))
3527 (if (and key (>= (length key) 1))
3528 (erc-server-send (format "MODE %s +k %s" tgt key))
3529 (erc-server-send (format "MODE %s -k" tgt)))))
3530
3531 (defun erc-quit-server (reason)
3532 "Disconnect from current server after prompting for REASON.
3533 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
3534 (interactive (list (read-from-minibuffer
3535 (format "Reason for quitting %s: "
3536 (or erc-server-announced-name
3537 erc-session-server)))))
3538 (erc-cmd-QUIT reason))
3539
3540 ;; Movement of point
3541
3542 (defun erc-bol ()
3543 "Move `point' to the beginning of the current line.
3544
3545 This places `point' just after the prompt, or at the beginning of the line."
3546 (interactive)
3547 (forward-line 0)
3548 (when (get-text-property (point) 'erc-prompt)
3549 (goto-char erc-input-marker))
3550 (point))
3551
3552 (defun erc-kill-input ()
3553 "Kill current input line using `erc-bol' followed by `kill-line'."
3554 (interactive)
3555 (when (and (erc-bol)
3556 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
3557 ;; there's nothing to kill
3558 (if (boundp 'erc-input-ring-index)
3559 (setq erc-input-ring-index nil))
3560 (kill-line)))
3561
3562 (defun erc-complete-word ()
3563 "Complete the word before point.
3564
3565 This function uses `erc-complete-functions'."
3566 (interactive)
3567 (unless (run-hook-with-args-until-success 'erc-complete-functions)
3568 (beep)))
3569
3570 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3571 ;;
3572 ;; IRC SERVER INPUT HANDLING
3573 ;;
3574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3575
3576 ;;;; New Input parsing
3577
3578 ; Stolen from ZenIRC. I just wanna test this code, so here is
3579 ; experiment area.
3580
3581 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
3582 erc-default-server-handler)
3583 "*Default for server messages which aren't covered by `erc-server-hooks'."
3584 :group 'erc-server-hooks
3585 :type 'hook)
3586
3587 (defun erc-default-server-handler (proc parsed)
3588 "Default server handler.
3589
3590 Displays PROC and PARSED appropriately using `erc-display-message'."
3591 (erc-display-message
3592 parsed 'notice proc
3593 (mapconcat
3594 'identity
3595 (let (res)
3596 (mapc #'(lambda (x)
3597 (if (stringp x)
3598 (setq res (append res (list x)))))
3599 parsed)
3600 res)
3601 " ")))
3602
3603 (defvar erc-server-vectors
3604 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
3605 "List of received server messages which ERC does not specifically handle.
3606 See `erc-debug-missing-hooks'.")
3607 ;(make-variable-buffer-local 'erc-server-vectors)
3608
3609 (defun erc-debug-missing-hooks (proc parsed)
3610 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
3611 These vectors can be helpful when adding new server message handlers to ERC.
3612 See `erc-default-server-hook'."
3613 (nconc erc-server-vectors (list parsed))
3614 nil)
3615
3616 (defun erc-query (target server)
3617 "Open a query buffer on TARGET, using SERVER.
3618 To change how this query window is displayed, use `let' to bind
3619 `erc-join-buffer' before calling this."
3620 (unless (and server
3621 (buffer-live-p server)
3622 (set-buffer server))
3623 (error "Couldn't switch to server buffer"))
3624 (let ((buf (erc erc-session-server
3625 erc-session-port
3626 (erc-current-nick)
3627 erc-session-user-full-name
3628 nil
3629 nil
3630 (list target)
3631 target
3632 erc-server-process)))
3633 (unless buf
3634 (error "Couldn't open query window"))
3635 (erc-update-mode-line)
3636 buf))
3637
3638 (defcustom erc-auto-query nil
3639 "If non-nil, create a query buffer each time you receive a private message.
3640
3641 If the buffer doesn't already exist it is created. This can be
3642 set to a symbol, to control how the new query window should
3643 appear. See the documentation for `erc-join-buffer' for
3644 available choices."
3645 :group 'erc-query
3646 :type '(choice (const nil)
3647 (const buffer)
3648 (const window)
3649 (const window-noselect)
3650 (const bury)
3651 (const frame)))
3652
3653 (defcustom erc-query-on-unjoined-chan-privmsg t
3654 "If non-nil create query buffer on receiving any PRIVMSG at all.
3655 This includes PRIVMSGs directed to channels. If you are using an IRC
3656 bouncer, such as dircproxy, to keep a log of channels when you are
3657 disconnected, you should set this option to t."
3658 :group 'erc-query
3659 :type 'boolean)
3660
3661 (defcustom erc-format-query-as-channel-p t
3662 "If non-nil, format text from others in a query buffer like in a channel,
3663 otherwise format like a private message."
3664 :group 'erc-query
3665 :type 'boolean)
3666
3667 (defcustom erc-minibuffer-notice nil
3668 "If non-nil, print ERC notices for the user in the minibuffer.
3669 Only happens when the session buffer isn't visible."
3670 :group 'erc-display
3671 :type 'boolean)
3672
3673 (defcustom erc-minibuffer-ignored nil
3674 "If non-nil, print a message in the minibuffer if we ignored something."
3675 :group 'erc-ignore
3676 :type 'boolean)
3677
3678 (defun erc-wash-quit-reason (reason nick login host)
3679 "Remove duplicate text from quit REASON.
3680 Specifically in relation to NICK (user@host) information. Returns REASON
3681 unmodified if nothing can be removed.
3682 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
3683 \"Read error: 110\". The same applies for \"Ping Timeout\"."
3684 (setq nick (regexp-quote nick)
3685 login (regexp-quote login)
3686 host (regexp-quote host))
3687 (or (when (string-match (concat "^\\(Read error\\) to "
3688 nick "\\[" host "\\]: "
3689 "\\(.+\\)$") reason)
3690 (concat (match-string 1 reason) ": " (match-string 2 reason)))
3691 (when (string-match (concat "^\\(Ping timeout\\) for "
3692 nick "\\[" host "\\]$") reason)
3693 (match-string 1 reason))
3694 reason))
3695
3696 (defun erc-nickname-in-use (nick reason)
3697 "If NICK is unavailable, tell the user the REASON.
3698
3699 See also `erc-display-error-notice'."
3700 (if (or erc-manual-set-nick-on-bad-nick-p
3701 ;; how many default-nicks are left + one more try...
3702 (eq erc-nick-change-attempt-count
3703 (if (consp erc-nick)
3704 (+ (length erc-nick) 1)
3705 1)))
3706 (erc-display-error-notice
3707 nil
3708 (format "Nickname %s is %s, try another." nick reason))
3709 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
3710 (let ((newnick (nth 1 erc-default-nicks))
3711 (nicklen (cdr (assoc "NICKLEN"
3712 (with-current-buffer (erc-server-buffer)
3713 erc-server-parameters)))))
3714 (setq erc-bad-nick t)
3715 ;; try to use a different nick
3716 (if erc-default-nicks
3717 (setq erc-default-nicks (cdr erc-default-nicks)))
3718 (if (not newnick)
3719 (setq newnick (concat (truncate-string-to-width
3720 nick
3721 (if (and erc-server-connected nicklen)
3722 (- (string-to-number nicklen) 1)
3723 ;; rfc2812 max nick length = 9
3724 ;; we must assume this is the
3725 ;; server's setting if we haven't
3726 ;; established a connection yet
3727 8))
3728 erc-nick-uniquifier)))
3729 (erc-cmd-NICK newnick)
3730 (erc-display-error-notice
3731 nil
3732 (format "Nickname %s is %s, trying %s"
3733 nick reason newnick)))))
3734
3735 ;;; Server messages
3736
3737 (defgroup erc-server-hooks nil
3738 "Server event callbacks.
3739 Every server event - like numeric replies - has it's own hook.
3740 Those hooks are all called using `run-hook-with-args-until-success'.
3741 They receive as first argument the process object from where the event
3742 originated from,
3743 and as second argument the event parsed as a vector."
3744 :group 'erc-hooks)
3745
3746 (defun erc-display-server-message (proc parsed)
3747 "Display the message sent by the server as a notice."
3748 (erc-display-message
3749 parsed 'notice 'active (erc-response.contents parsed)))
3750
3751 (defun erc-auto-query (proc parsed)
3752 ;; FIXME: This needs more documentation, unless it's not a user function --
3753 ;; Lawrence 2004-01-08
3754 "Put this on `erc-server-PRIVMSG-functions'."
3755 (when erc-auto-query
3756 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
3757 (target (car (erc-response.command-args parsed)))
3758 (msg (erc-response.contents parsed))
3759 (query (if (not erc-query-on-unjoined-chan-privmsg)
3760 nick
3761 (if (erc-current-nick-p target)
3762 nick
3763 target))))
3764 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
3765 (or erc-query-on-unjoined-chan-privmsg
3766 (string= target (erc-current-nick)))
3767 (not (erc-get-buffer query proc))
3768 (not (erc-is-message-ctcp-and-not-action-p msg))
3769 (let ((erc-join-buffer erc-auto-query))
3770 (erc-cmd-QUERY query))
3771 nil))))
3772
3773 (defun erc-is-message-ctcp-p (message)
3774 "Check if MESSAGE is a CTCP message or not."
3775 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
3776
3777 (defun erc-is-message-ctcp-and-not-action-p (message)
3778 "Check if MESSAGE is a CTCP message or not."
3779 (and (erc-is-message-ctcp-p message)
3780 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
3781
3782 (defun erc-format-privmessage (nick msg privp msgp)
3783 "Format a PRIVMSG in an insertible fashion."
3784 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
3785 (mark-e (if msgp (if privp "*" ">") "-"))
3786 (str (format "%s%s%s %s" mark-s nick mark-e msg))
3787 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
3788 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
3789 ;; add text properties to text before the nick, the nick and after the nick
3790 (erc-put-text-property 0 (length mark-s) 'face msg-face str)
3791 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
3792 'face nick-face str)
3793 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
3794 'face msg-face str)
3795 str))
3796
3797 (defcustom erc-format-nick-function 'erc-format-nick
3798 "*Function to format a nickname for message display."
3799 :group 'erc-display
3800 :type 'function)
3801
3802 (defun erc-format-nick (&optional user channel-data)
3803 "Standard nickname formatting function. Only returns the value
3804 of NICK."
3805 (if user
3806 (erc-server-user-nickname user)))
3807
3808 (defun erc-format-@nick (&optional user channel-data)
3809 "Format a nickname such that @ or + are prefix for the NICK
3810 if OP or VOICE are t respectively."
3811 (if user
3812 (let (op voice)
3813 (if channel-data
3814 (setq op (erc-channel-user-op channel-data)
3815 voice (erc-channel-user-voice channel-data)))
3816 (concat (if voice "+" "")
3817 (if op "@" "")
3818 (erc-server-user-nickname user)))))
3819
3820 (defun erc-format-my-nick ()
3821 "Return the beginning of this user's message, correctly propertized"
3822 (if erc-show-my-nick
3823 (let ((open "<")
3824 (close "> ")
3825 (nick (erc-current-nick)))
3826 (concat
3827 (erc-propertize open 'face 'erc-default-face)
3828 (erc-propertize nick 'face 'erc-nick-default-face)
3829 (erc-propertize close 'face 'erc-default-face)))
3830 (let ((prefix "> "))
3831 (erc-propertize prefix 'face 'erc-default-face))))
3832
3833 (defun erc-echo-notice-in-default-buffer (s parsed buffer sender)
3834 "Echos a private notice in the default buffer, namely the
3835 target buffer specified by BUFFER, or there is no target buffer,
3836 the server buffer. This function is designed to be added to
3837 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
3838 and always returns t."
3839 (erc-display-message parsed nil buffer s)
3840 t)
3841
3842 (defun erc-echo-notice-in-target-buffer (s parsed buffer sender)
3843 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
3844 function is designed to be added to either `erc-echo-notice-hook'
3845 or `erc-echo-notice-always-hook', and returns non-nil iff BUFFER
3846 is non-nil."
3847 (if buffer
3848 (progn (erc-display-message parsed nil buffer s) t)
3849 nil))
3850
3851 (defun erc-echo-notice-in-minibuffer (s parsed buffer sender)
3852 "Echos a private notice in the minibuffer. This function is
3853 designed to be added to either `erc-echo-notice-hook' or
3854 `erc-echo-notice-always-hook', and always returns t."
3855 (message "%s" (concat "NOTICE: " s))
3856 t)
3857
3858 (defun erc-echo-notice-in-server-buffer (s parsed buffer sender)
3859 "Echos a private notice in the server buffer. This function is
3860 designed to be added to either `erc-echo-notice-hook' or
3861 `erc-echo-notice-always-hook', and always returns t."
3862 (erc-display-message parsed nil nil s)
3863 t)
3864
3865 (defun erc-echo-notice-in-active-non-server-buffer (s parsed buffer sender)
3866 "Echos a private notice in the active buffer if the active
3867 buffer is not the server buffer. This function is designed to be
3868 added to either `erc-echo-notice-hook' or
3869 `erc-echo-notice-always-hook', and returns non-nil iff the active
3870 buffer is not the server buffer."
3871 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
3872 (progn (erc-display-message parsed nil 'active s) t)
3873 nil))
3874
3875 (defun erc-echo-notice-in-active-buffer (s parsed buffer sender)
3876 "Echos a private notice in the active buffer. This function is
3877 designed to be added to either `erc-echo-notice-hook' or
3878 `erc-echo-notice-always-hook', and always returns t."
3879 (erc-display-message parsed nil 'active s)
3880 t)
3881
3882 (defun erc-echo-notice-in-user-buffers (s parsed buffer sender)
3883 "Echos a private notice in all of the buffers for which SENDER
3884 is a member. This function is designed to be added to either
3885 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
3886 returns non-nil iff there is at least one buffer for which the
3887 sender is a member.
3888
3889 See also: `erc-echo-notice-in-first-user-buffer',
3890 `erc-buffer-list-with-nick'"
3891 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3892 (if buffers
3893 (progn (erc-display-message parsed nil buffers s) t)
3894 nil)))
3895
3896 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
3897 "Echos a private notice in BUFFER and in all of the buffers for
3898 which SENDER is a member. This function is designed to be added
3899 to either `erc-echo-notice-hook' or
3900 `erc-echo-notice-always-hook', and returns non-nil iff there is
3901 at least one buffer for which the sender is a member or the
3902 default target.
3903
3904 See also: `erc-echo-notice-in-user-buffers',
3905 `erc-buffer-list-with-nick'"
3906 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3907 (add-to-list 'buffers buffer)
3908 (if buffers
3909 (progn (erc-display-message parsed nil buffers s) t)
3910 nil)))
3911
3912 (defun erc-echo-notice-in-first-user-buffer (s parsed buffer sender)
3913 "Echos a private notice in one of the buffers for which SENDER
3914 is a member. This function is designed to be added to either
3915 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
3916 returns non-nil iff there is at least one buffer for which the
3917 sender is a member.
3918
3919 See also: `erc-echo-notice-in-user-buffers',
3920 `erc-buffer-list-with-nick'"
3921 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3922 (if buffers
3923 (progn (erc-display-message parsed nil (car buffers) s) t)
3924 nil)))
3925
3926 ;;; Ban manipulation
3927
3928 (defun erc-banlist-store (proc parsed)
3929 "Record ban entries for a channel."
3930 (multiple-value-bind (channel mask whoset)
3931 (cdr (erc-response.command-args parsed))
3932 ;; Determine to which buffer the message corresponds
3933 (let ((buffer (erc-get-buffer channel proc)))
3934 (with-current-buffer buffer
3935 (unless (member (cons whoset mask) erc-channel-banlist)
3936 (setq erc-channel-banlist (cons (cons whoset mask)
3937 erc-channel-banlist))))))
3938 nil)
3939
3940 (defun erc-banlist-finished (proc parsed)
3941 "Record that we have received the banlist."
3942 (let* ((channel (second (erc-response.command-args parsed)))
3943 (buffer (erc-get-buffer channel proc)))
3944 (with-current-buffer buffer
3945 (put 'erc-channel-banlist 'received-from-server t)))
3946 t) ; suppress the 'end of banlist' message
3947
3948 (defun erc-banlist-update (proc parsed)
3949 "Check MODE commands for bans and update the banlist appropriately."
3950 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
3951 (let* ((tgt (first (erc-response.command-args parsed)))
3952 (mode (erc-response.contents parsed))
3953 (whoset (erc-response.sender parsed))
3954 (buffer (erc-get-buffer tgt proc)))
3955 (when buffer
3956 (with-current-buffer buffer
3957 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
3958 ((string-match "^\\([+-]\\)b" mode)
3959 ;; This is a ban
3960 (cond
3961 ((string-match "^-" mode)
3962 ;; Remove the unbanned masks from the ban list
3963 (setq erc-channel-banlist
3964 (erc-delete-if
3965 #'(lambda (y)
3966 (member (upcase (cdr y))
3967 (mapcar #'upcase
3968 (cdr (split-string mode)))))
3969 erc-channel-banlist)))
3970 ((string-match "^+" mode)
3971 ;; Add the banned mask(s) to the ban list
3972 (mapc
3973 (lambda (mask)
3974 (unless (member (cons whoset mask) erc-channel-banlist)
3975 (setq erc-channel-banlist
3976 (cons (cons whoset mask) erc-channel-banlist))))
3977 (cdr (split-string mode))))))))))
3978 nil)
3979
3980 ;; used for the banlist cmds
3981 (defun erc-group-list (list n)
3982 "Group LIST into sublists of length N."
3983 (cond ((null list) nil)
3984 ((null (nthcdr n list)) (list list))
3985 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
3986
3987
3988 ;;; MOTD numreplies
3989
3990 (defun erc-handle-login ()
3991 "Handle the logging in process of connection."
3992 (unless erc-logged-in
3993 (setq erc-logged-in t)
3994 (message "Logging in as \'%s\'... done" (erc-current-nick))
3995 ;; execute a startup script
3996 (let ((f (erc-select-startup-file)))
3997 (when f
3998 (erc-load-script f)))))
3999
4000 (defun erc-connection-established (proc parsed)
4001 "Run just after connection.
4002
4003 Set user modes and run `erc-after-connect hook'."
4004 (unless erc-server-connected ; only once per session
4005 (let ((server (or erc-server-announced-name (erc-response.sender parsed)))
4006 (nick (car (erc-response.command-args parsed ))))
4007 (setq erc-server-connected t)
4008 (erc-update-mode-line)
4009 (erc-set-initial-user-mode nick)
4010 (erc-server-setup-periodical-server-ping)
4011 (run-hook-with-args 'erc-after-connect server nick))))
4012
4013 (defun erc-set-initial-user-mode (nick)
4014 "If `erc-user-mode' is non-nil for NICK, set the user modes."
4015 (when erc-user-mode
4016 (let ((mode (if (functionp erc-user-mode)
4017 (funcall erc-user-mode)
4018 erc-user-mode)))
4019 (when (stringp mode)
4020 (erc-log (format "changing mode for %s to %s" nick mode))
4021 (erc-server-send (format "MODE %s %s" nick mode))))))
4022
4023 (defun erc-display-error-notice (parsed string)
4024 "Display STRING as an error notice.
4025
4026 See also `erc-display-message'."
4027 (erc-display-message
4028 parsed '(notice error) 'active string))
4029
4030 (defun erc-process-ctcp-query (proc parsed nick login host)
4031 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4032 "Process a CTCP query."
4033 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4034 "\C-a"))))
4035 (if (> (length queries) 4)
4036 (erc-display-message
4037 parsed (list 'notice 'error) proc 'ctcp-too-many)
4038 (if (= 0 (length queries))
4039 (erc-display-message
4040 parsed (list 'notice 'error) proc
4041 'ctcp-empty ?n nick)
4042 (while queries
4043 (let* ((type (upcase (car (split-string (car queries)))))
4044 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4045 (if (and hook (boundp hook))
4046 (if (string-equal type "ACTION")
4047 (run-hook-with-args-until-success
4048 hook proc parsed nick login host
4049 (car (erc-response.command-args parsed))
4050 (car queries))
4051 (when erc-paranoid
4052 (if (erc-current-nick-p
4053 (car (erc-response.command-args parsed)))
4054 (erc-display-message
4055 parsed 'error 'active 'ctcp-request
4056 ?n nick ?u login ?h host ?r (car queries))
4057 (erc-display-message
4058 parsed 'error 'active 'ctcp-request-to
4059 ?n nick ?u login ?h host ?r (car queries)
4060 ?t (car (erc-response.command-args parsed)))))
4061 (run-hook-with-args-until-success
4062 hook proc nick login host
4063 (car (erc-response.command-args parsed))
4064 (car queries)))
4065 (erc-display-message
4066 parsed (list 'notice 'error) proc
4067 'undefined-ctcp)))
4068 (setq queries (cdr queries)))))))
4069
4070 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4071
4072 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4073 "Respond to a CTCP ACTION query."
4074 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4075 (let ((s (match-string 1 msg))
4076 (buf (or (erc-get-buffer to proc)
4077 (erc-get-buffer nick proc)
4078 (process-buffer proc))))
4079 (erc-display-message
4080 parsed 'action buf
4081 'ACTION ?n nick ?u login ?h host ?a s))))
4082
4083 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4084
4085 (defun erc-ctcp-query-CLIENTINFO (proc nick login host to msg)
4086 "Respond to a CTCP CLIENTINFO query."
4087 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4088 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4089 (unless erc-disable-ctcp-replies
4090 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4091 nil)
4092
4093 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4094 (defun erc-ctcp-query-ECHO (proc nick login host to msg)
4095 "Respond to a CTCP ECHO query."
4096 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4097 (let ((s (match-string 1 msg)))
4098 (unless erc-disable-ctcp-replies
4099 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4100 nil)
4101
4102 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4103 (defun erc-ctcp-query-FINGER (proc nick login host to msg)
4104 "Respond to a CTCP FINGER query."
4105 (unless erc-disable-ctcp-replies
4106 (let ((s (if erc-anonymous-login
4107 (format "FINGER I'm %s." (erc-current-nick))
4108 (format "FINGER %s (%s@%s)."
4109 (user-full-name)
4110 (user-login-name)
4111 (system-name))))
4112 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4113 (when (> ns 0)
4114 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4115 (erc-send-ctcp-notice nick s)))
4116 nil)
4117
4118 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4119 (defun erc-ctcp-query-PING (proc nick login host to msg)
4120 "Respond to a CTCP PING query."
4121 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4122 (unless erc-disable-ctcp-replies
4123 (let ((arg (match-string 1 msg)))
4124 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4125 nil)
4126
4127 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4128 (defun erc-ctcp-query-TIME (proc nick login host to msg)
4129 "Respond to a CTCP TIME query."
4130 (unless erc-disable-ctcp-replies
4131 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4132 nil)
4133
4134 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4135 (defun erc-ctcp-query-USERINFO (proc nick login host to msg)
4136 "Respond to a CTCP USERINFO query."
4137 (unless erc-disable-ctcp-replies
4138 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4139 nil)
4140
4141 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4142 (defun erc-ctcp-query-VERSION (proc nick login host to msg)
4143 "Respond to a CTCP VERSION query."
4144 (unless erc-disable-ctcp-replies
4145 (erc-send-ctcp-notice
4146 nick (format
4147 "VERSION \C-bERC\C-b %s - an IRC client for emacs (\C-b%s\C-b)"
4148 erc-version-string
4149 erc-official-location)))
4150 nil)
4151
4152 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4153 "Process MSG as a CTCP reply."
4154 (let* ((type (car (split-string msg)))
4155 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4156 (if (boundp hook)
4157 (run-hook-with-args-until-success
4158 hook proc nick login host
4159 (car (erc-response.command-args parsed)) msg)
4160 (erc-display-message
4161 parsed 'notice 'active
4162 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4163
4164 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4165 (defun erc-ctcp-reply-ECHO (proc nick login host to msg)
4166 "Handle a CTCP ECHO reply."
4167 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4168 (let ((message (match-string 1 msg)))
4169 (erc-display-message
4170 nil '(notice action) 'active
4171 'CTCP-ECHO ?n nick ?m message)))
4172 nil)
4173
4174 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4175 (defun erc-ctcp-reply-CLIENTINFO (proc nick login host to msg)
4176 "Handle a CTCP CLIENTINFO reply."
4177 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4178 (let ((message (match-string 1 msg)))
4179 (erc-display-message
4180 nil 'notice 'active
4181 'CTCP-CLIENTINFO ?n nick ?m message)))
4182 nil)
4183
4184 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4185 (defun erc-ctcp-reply-FINGER (proc nick login host to msg)
4186 "Handle a CTCP FINGER reply."
4187 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4188 (let ((message (match-string 1 msg)))
4189 (erc-display-message
4190 nil 'notice 'active
4191 'CTCP-FINGER ?n nick ?m message)))
4192 nil)
4193
4194 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4195 (defun erc-ctcp-reply-PING (proc nick login host to msg)
4196 "Handle a CTCP PING reply."
4197 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4198 nil
4199 (let ((time (match-string 1 msg)))
4200 (condition-case nil
4201 (let ((delta (erc-time-diff (string-to-number time)
4202 (erc-current-time))))
4203 (erc-display-message
4204 nil 'notice 'active
4205 'CTCP-PING ?n nick
4206 ?t (erc-sec-to-time delta)))
4207 (range-error
4208 (erc-display-message
4209 nil 'error 'active
4210 'bad-ping-response ?n nick ?t time))))))
4211
4212 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4213 (defun erc-ctcp-reply-TIME (proc nick login host to msg)
4214 "Handle a CTCP TIME reply."
4215 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4216 (let ((message (match-string 1 msg)))
4217 (erc-display-message
4218 nil 'notice 'active
4219 'CTCP-TIME ?n nick ?m message)))
4220 nil)
4221
4222 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4223 (defun erc-ctcp-reply-VERSION (proc nick login host to msg)
4224 "Handle a CTCP VERSION reply."
4225 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4226 (let ((message (match-string 1 msg)))
4227 (erc-display-message
4228 nil 'notice 'active
4229 'CTCP-VERSION ?n nick ?m message)))
4230 nil)
4231
4232 (defun erc-process-away (proc away-p)
4233 ;; FIXME: This docstring is AWFUL -- Lawrence 2004-01-08
4234 "Process the user being away, or returning from an away break."
4235 (let ((sessionbuf (process-buffer proc)))
4236 (when sessionbuf
4237 (with-current-buffer sessionbuf
4238 (when erc-away-nickname
4239 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4240 erc-away-nickname away-p))
4241 (erc-cmd-NICK (if away-p
4242 erc-away-nickname
4243 erc-nick)))
4244 (cond
4245 (away-p
4246 (erc-with-all-buffers-of-server proc nil
4247 (setq erc-away (current-time))))
4248 (t
4249 (let ((away-time erc-away))
4250 ;; away must be set to NIL BEFORE sending anything to prevent
4251 ;; an infinite recursion
4252 (erc-with-all-buffers-of-server proc nil
4253 (setq erc-away nil))
4254 (save-excursion
4255 (set-buffer (erc-active-buffer))
4256 (when erc-public-away-p
4257 (erc-send-action
4258 (erc-default-target)
4259 (if away-time
4260 (format "is back (gone for %s)"
4261 (erc-sec-to-time
4262 (erc-time-diff
4263 (erc-emacs-time-to-erc-time away-time)
4264 (erc-current-time))))
4265 "is back")))))))))
4266 (erc-update-mode-line)))
4267
4268 ;;;; List of channel members handling
4269
4270 (defun erc-channel-begin-receiving-names ()
4271 "Internal function.
4272
4273 Used when a channel names list is about to be received. Should
4274 be called with the current buffer set to the channel buffer.
4275
4276 See also `erc-channel-end-receiving-names'."
4277 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4278
4279 (defun erc-channel-end-receiving-names ()
4280 "Internal function.
4281
4282 Used to fix `erc-channel-users' after a channel names list has been
4283 received. Should be called with the current buffer set to the
4284 channel buffer.
4285
4286 See also `erc-channel-begin-receiving-names'."
4287 (maphash (lambda (nick user)
4288 (if (null (gethash nick erc-channel-new-member-names))
4289 (erc-remove-channel-user nick)))
4290 erc-channel-users)
4291 (setq erc-channel-new-member-names nil))
4292
4293 (defun erc-channel-receive-names (names-string)
4294 "This function is for internal use only.
4295
4296 Update `erc-channel-users' according to NAMES-STRING.
4297 NAMES-STRING is a string listing some of the names on the
4298 channel."
4299 (let (names name op voice)
4300 ;; We need to delete "" because in XEmacs, (split-string "a ")
4301 ;; returns ("a" "").
4302 (setq names (delete "" (split-string names-string)))
4303 (let ((erc-channel-members-changed-hook nil))
4304 (dolist (item names)
4305 (cond ((string-match "^@\\(.*\\)$" item)
4306 (setq name (match-string 1 item)
4307 op 'on
4308 voice 'off))
4309 ((string-match "^+\\(.*\\)$" item)
4310 (setq name (match-string 1 item)
4311 op 'off
4312 voice 'on))
4313 (t (setq name item
4314 op 'off
4315 voice 'off)))
4316 (puthash (erc-downcase name) t
4317 erc-channel-new-member-names)
4318 (erc-update-current-channel-member
4319 name name t op voice)))
4320 (run-hooks 'erc-channel-members-changed-hook)))
4321
4322 (defcustom erc-channel-members-changed-hook nil
4323 "*This hook is called every time the variable `channel-members' changes.
4324 The buffer where the change happened is current while this hook is called."
4325 :group 'erc-hooks
4326 :type 'hook)
4327
4328 (defun erc-update-user-nick (nick &optional new-nick
4329 host login full-name info)
4330 "Updates the stored user information for the user with nickname
4331 NICK.
4332
4333 See also: `erc-update-user'"
4334 (erc-update-user (erc-get-server-user nick) new-nick
4335 host login full-name info))
4336
4337 (defun erc-update-user (user &optional new-nick
4338 host login full-name info)
4339 "Update user info for USER. USER must be an erc-server-user
4340 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4341 non-nil and not equal to the existing values for USER are used to
4342 replace the stored values in USER.
4343
4344 If, any only if a change is made,
4345 `erc-channel-members-changed-hook' is run for each channel for
4346 which USER is a member, and `t' is returned."
4347 (let (changed)
4348 (when user
4349 (when (and new-nick
4350 (not (equal (erc-server-user-nickname user)
4351 new-nick)))
4352 (setq changed t)
4353 (erc-change-user-nickname user new-nick))
4354 (when (and host
4355 (not (equal (erc-server-user-host user) host)))
4356 (setq changed t)
4357 (setf (erc-server-user-host user) host))
4358 (when (and login
4359 (not (equal (erc-server-user-login user) login)))
4360 (setq changed t)
4361 (setf (erc-server-user-login user) login))
4362 (when (and full-name
4363 (not (equal (erc-server-user-full-name user)
4364 full-name)))
4365 (setq changed t)
4366 (setf (erc-server-user-full-name user) full-name))
4367 (when (and info
4368 (not (equal (erc-server-user-info user) info)))
4369 (setq changed t)
4370 (setf (erc-server-user-info user) info))
4371 (if changed
4372 (dolist (buf (erc-server-user-buffers user))
4373 (if (buffer-live-p buf)
4374 (with-current-buffer buf
4375 (run-hooks 'erc-channel-members-changed-hook))))))
4376 changed))
4377
4378 (defun erc-update-current-channel-member
4379 (nick new-nick &optional add op voice host login full-name info
4380 update-message-time)
4381 "Updates the stored user information for the user with nickname
4382 NICK. `erc-update-user' is called to handle changes to nickname,
4383 host, login, full-name, and info. If `op' or `voice' are
4384 non-nil, they must be equal to either `on' or `off', in which
4385 case the operator or voice status of USER in the current channel
4386 is changed accordingly. If `update-message-time' is non-nil, the
4387 last-message-time of the user in the current channel is set
4388 to (current-time).
4389
4390 If ADD is non-nil, the user will be added with the specified
4391 information if it is not already present in the user or channel
4392 lists.
4393
4394 If, and only if, changes are made, or the user is added,
4395 `erc-channel-members-updated-hook' is run, and `t' is returned.
4396
4397 See also: `erc-update-user' and `erc-update-channel-member'."
4398 (let* (changed user-changed
4399 (channel-data (erc-get-channel-user nick))
4400 (cuser (if channel-data (cdr channel-data)))
4401 (user (if channel-data (car channel-data)
4402 (erc-get-server-user nick))))
4403 (if cuser
4404 (progn
4405 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4406 (when (and op
4407 (not (eq (erc-channel-user-op cuser) op)))
4408 (setq changed t)
4409 (setf (erc-channel-user-op cuser)
4410 (cond ((eq op 'on) t)
4411 ((eq op 'off) nil)
4412 (t op))))
4413 (when (and voice
4414 (not (eq (erc-channel-user-voice cuser) voice)))
4415 (setq changed t)
4416 (setf (erc-channel-user-voice cuser)
4417 (cond ((eq voice 'on) t)
4418 ((eq voice 'off) nil)
4419 (t voice))))
4420 (when update-message-time
4421 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4422 (setq user-changed
4423 (erc-update-user user new-nick
4424 host login full-name info)))
4425 (when add
4426 (if (null user)
4427 (progn
4428 (setq user (make-erc-server-user
4429 :nickname nick
4430 :host host
4431 :full-name full-name
4432 :login login
4433 :info info
4434 :buffers (list (current-buffer))))
4435 (erc-add-server-user nick user))
4436 (setf (erc-server-user-buffers user)
4437 (cons (current-buffer)
4438 (erc-server-user-buffers user))))
4439 (setq cuser (make-erc-channel-user
4440 :op (cond ((eq op 'on) t)
4441 ((eq op 'off) nil)
4442 (t op))
4443 :voice (cond ((eq voice 'on) t)
4444 ((eq voice 'off) nil)
4445 (t voice))
4446 :last-message-time
4447 (if update-message-time (current-time))))
4448 (puthash (erc-downcase nick) (cons user cuser)
4449 erc-channel-users)
4450 (setq changed t)))
4451 (when (and changed (null user-changed))
4452 (run-hooks 'erc-channel-members-changed-hook))
4453 (or changed user-changed add)))
4454
4455 (defun erc-update-channel-member (channel nick new-nick
4456 &optional add op voice host login
4457 full-name info update-message-time)
4458 "Updates user and channel information for the user with
4459 nickname NICK in channel CHANNEL.
4460
4461 See also: `erc-update-current-channel-member'"
4462 (erc-with-buffer
4463 (channel)
4464 (erc-update-current-channel-member nick new-nick add op voice host
4465 login full-name info
4466 update-message-time)))
4467
4468 (defun erc-remove-current-channel-member (nick)
4469 "Remove NICK from current channel membership list. Runs
4470 `erc-channel-members-changed-hook'."
4471 (let ((channel-data (erc-get-channel-user nick)))
4472 (when channel-data
4473 (erc-remove-channel-user nick)
4474 (run-hooks 'erc-channel-members-changed-hook))))
4475
4476 (defun erc-remove-channel-member (channel nick)
4477 "Remove NICK from CHANNEL's membership list.
4478
4479 See also `erc-remove-current-channel-member'."
4480 (erc-with-buffer
4481 (channel)
4482 (erc-remove-current-channel-member nick)))
4483
4484 (defun erc-update-channel-topic (channel topic &optional modify)
4485 "Find a buffer for CHANNEL and set the TOPIC for it.
4486
4487 If optional MODIFY is 'append or 'prepend, then append or prepend the
4488 TOPIC string to the current topic."
4489 (erc-with-buffer (channel)
4490 (cond ((eq modify 'append)
4491 (setq erc-channel-topic (concat erc-channel-topic topic)))
4492 ((eq modify 'prepend)
4493 (setq erc-channel-topic (concat topic erc-channel-topic)))
4494 (t (setq erc-channel-topic topic)))
4495 (erc-update-mode-line-buffer (current-buffer))))
4496
4497 (defun erc-set-modes (tgt mode-string)
4498 "Set the modes for the TGT provided as MODE-STRING."
4499 (let* ((modes (erc-parse-modes mode-string))
4500 (add-modes (nth 0 modes))
4501 (remove-modes (nth 1 modes))
4502 ;; list of triples: (mode-char 'on/'off argument)
4503 (arg-modes (nth 2 modes)))
4504 (cond ((erc-channel-p tgt); channel modes
4505 (let ((buf (and (boundp 'erc-server-process) erc-server-process
4506 (erc-get-buffer tgt erc-server-process))))
4507 (when buf
4508 (with-current-buffer buf
4509 (setq erc-channel-modes add-modes)
4510 (setq erc-channel-user-limit nil)
4511 (setq erc-channel-key nil)
4512 (while arg-modes
4513 (let ((mode (nth 0 (car arg-modes)))
4514 (onoff (nth 1 (car arg-modes)))
4515 (arg (nth 2 (car arg-modes))))
4516 (cond ((string-match "^[Ll]" mode)
4517 (erc-update-channel-limit tgt onoff arg))
4518 ((string-match "^[Kk]" mode)
4519 (erc-update-channel-key tgt onoff arg))
4520 (t nil)))
4521 (setq arg-modes (cdr arg-modes)))
4522 (erc-update-mode-line-buffer buf)))))
4523 ;; we do not keep our nick's modes yet
4524 ;;(t (setq erc-user-modes add-modes))
4525 )
4526 ))
4527
4528 (defun erc-sort-strings (list-of-strings)
4529 "Sort LIST-OF-STRINGS in lexicographic order.
4530
4531 Side-effect free."
4532 (sort (copy-sequence list-of-strings) 'string<))
4533
4534 (defun erc-parse-modes (mode-string)
4535 "Parse MODE-STRING into a list.
4536
4537 Returns a list of three elements:
4538
4539 (ADD-MODES REMOVE-MODES ARG-MODES).
4540
4541 The add-modes and remove-modes are lists of single-character strings
4542 for modes without parameters to add and remove respectively. The
4543 arg-modes is a list of triples of the form:
4544
4545 (MODE-CHAR ON/OFF ARGUMENT)."
4546 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
4547 (let ((chars (mapcar 'char-to-string (match-string 1 mode-string)))
4548 ;; arguments in channel modes
4549 (args-str (match-string 2 mode-string))
4550 (args nil)
4551 (add-modes nil)
4552 (remove-modes nil)
4553 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
4554 (add-p t))
4555 ;; make the argument list
4556 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
4557 (setq args (cons (match-string 1 args-str) args))
4558 (setq args-str (match-string 2 args-str)))
4559 (setq args (nreverse args))
4560 ;; collect what modes changed, and match them with arguments
4561 (while chars
4562 (cond ((string= (car chars) "+") (setq add-p t))
4563 ((string= (car chars) "-") (setq add-p nil))
4564 ((string-match "^[ovbOVB]" (car chars))
4565 (setq arg-modes (cons (list (car chars)
4566 (if add-p 'on 'off)
4567 (if args (car args) nil))
4568 arg-modes))
4569 (if args (setq args (cdr args))))
4570 ((string-match "^[LlKk]" (car chars))
4571 (setq arg-modes (cons (list (car chars)
4572 (if add-p 'on 'off)
4573 (if (and add-p args)
4574 (car args) nil))
4575 arg-modes))
4576 (if (and add-p args) (setq args (cdr args))))
4577 (add-p (setq add-modes (cons (car chars) add-modes)))
4578 (t (setq remove-modes (cons (car chars) remove-modes))))
4579 (setq chars (cdr chars)))
4580 (setq add-modes (nreverse add-modes))
4581 (setq remove-modes (nreverse remove-modes))
4582 (setq arg-modes (nreverse arg-modes))
4583 (list add-modes remove-modes arg-modes))
4584 nil))
4585
4586 (defun erc-update-modes (tgt mode-string &optional nick host login)
4587 "Update the mode information for TGT, provided as MODE-STRING.
4588 Optional arguments: NICK, HOST and LOGIN - the attributes of the
4589 person who changed the modes."
4590 (let* ((modes (erc-parse-modes mode-string))
4591 (add-modes (nth 0 modes))
4592 (remove-modes (nth 1 modes))
4593 ;; list of triples: (mode-char 'on/'off argument)
4594 (arg-modes (nth 2 modes)))
4595 ;; now parse the modes changes and do the updates
4596 (cond ((erc-channel-p tgt); channel modes
4597 (let ((buf (and (boundp 'erc-server-process) erc-server-process
4598 (erc-get-buffer tgt erc-server-process))))
4599 (when buf
4600 ;; FIXME! This used to have an original buffer
4601 ;; variable, but it never switched back to the original
4602 ;; buffer. Is this wanted behavior?
4603 (set-buffer buf)
4604 (if (not (boundp 'erc-channel-modes))
4605 (setq erc-channel-modes nil))
4606 (while remove-modes
4607 (setq erc-channel-modes (delete (car remove-modes)
4608 erc-channel-modes)
4609 remove-modes (cdr remove-modes)))
4610 (while add-modes
4611 (setq erc-channel-modes (cons (car add-modes)
4612 erc-channel-modes)
4613 add-modes (cdr add-modes)))
4614 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
4615 (while arg-modes
4616 (let ((mode (nth 0 (car arg-modes)))
4617 (onoff (nth 1 (car arg-modes)))
4618 (arg (nth 2 (car arg-modes))))
4619 (cond ((string-match "^[oO]" mode)
4620 (erc-update-channel-member tgt arg arg nil onoff))
4621 ((string-match "^[Vv]" mode)
4622 (erc-update-channel-member tgt arg arg nil nil
4623 onoff))
4624 ((string-match "^[Ll]" mode)
4625 (erc-update-channel-limit tgt onoff arg))
4626 ((string-match "^[Kk]" mode)
4627 (erc-update-channel-key tgt onoff arg))
4628 (t nil)); only ops are tracked now
4629 (setq arg-modes (cdr arg-modes))))
4630 (erc-update-mode-line buf))))
4631 ;; nick modes - ignored at this point
4632 (t nil))))
4633
4634 (defun erc-update-channel-limit (channel onoff n)
4635 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
4636 "Update CHANNEL's user limit to N."
4637 (if (or (not (eq onoff 'on))
4638 (and (stringp n) (string-match "^[0-9]+$" n)))
4639 (erc-with-buffer
4640 (channel)
4641 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
4642 (t (setq erc-channel-user-limit nil))))))
4643
4644 (defun erc-update-channel-key (channel onoff key)
4645 "Update CHANNEL's key to KEY if ONOFF is 'on or to nil if it's 'off."
4646 (erc-with-buffer
4647 (channel)
4648 (cond ((eq onoff 'on) (setq erc-channel-key key))
4649 (t (setq erc-channel-key nil)))))
4650
4651 (defun erc-handle-user-status-change (type nlh &optional l)
4652 "Handle changes in any user's status.
4653
4654 So far, only nick change is handled.
4655
4656 Generally, the TYPE argument is a symbol describing the change type, NLH is
4657 a list containing the original nickname, login name and hostname for the user,
4658 and L is a list containing additional TYPE-specific arguments.
4659
4660 So far the following TYPE/L pairs are supported:
4661
4662 Event TYPE L
4663
4664 nickname change 'nick (NEW-NICK)"
4665 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
4666 (cond
4667 ;; nickname change
4668 ((equal type 'nick)
4669 t)
4670 (t
4671 nil)))
4672
4673 (defun erc-highlight-notice (s)
4674 "Highlight notice message S and return it.
4675 See also variable `erc-notice-highlight-type'"
4676 (cond
4677 ((eq erc-notice-highlight-type 'prefix)
4678 (erc-put-text-property 0 (length erc-notice-prefix)
4679 'face 'erc-notice-face s)
4680 s)
4681 ((eq erc-notice-highlight-type 'all)
4682 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
4683 s)
4684 (t s)))
4685
4686 (defun erc-make-notice (message)
4687 "Notify the user of MESSAGE."
4688 (when erc-minibuffer-notice
4689 (message "%s" message))
4690 (erc-highlight-notice (concat erc-notice-prefix message)))
4691
4692 (defun erc-highlight-error (s)
4693 "Highlight error message S and return it."
4694 (erc-put-text-property 0 (length s) 'face 'erc-error-face s)
4695 s)
4696
4697 (defun erc-put-text-property (start end property value &optional object)
4698 "Set text-property for an object (usually a string).
4699 START and END define the characters covered.
4700 PROPERTY is the text-property set, usually the symbol `face'.
4701 VALUE is the value for the text-property, usually a face symbol such as
4702 the face `bold' or `erc-pal-face'.
4703 OBJECT is a string which will be modified and returned.
4704 OBJECT is modified without being copied first.
4705
4706 You can redefine or `defadvice' this function in order to add
4707 EmacsSpeak support."
4708 (put-text-property start end property value object))
4709
4710 (defun erc-list (thing)
4711 "Return THING if THING is a list, or a list with THING as its element."
4712 (if (listp thing)
4713 thing
4714 (list thing)))
4715
4716 (defun erc-parse-user (string)
4717 "Parse STRING as a user specification (nick!login@host).
4718
4719 Return a list of the three separate tokens."
4720 (cond
4721 ((string-match "^\\([^!]*\\)!\\([^@]*\\)@\\(.*\\)$" string)
4722 (list (match-string 1 string)
4723 (match-string 2 string)
4724 (match-string 3 string)))
4725 ;; Some bogus bouncers send Nick!(null), try to live with that.
4726 ((string-match "^\\([^!]*\\)!\\(.*\\)$" string)
4727 (list (match-string 1 string)
4728 ""
4729 (match-string 2 string)))
4730 (t
4731 (list string "" ""))))
4732
4733 (defun erc-extract-nick (string)
4734 "Return the nick corresponding to a user specification STRING.
4735
4736 See also `erc-parse-user'."
4737 (car (erc-parse-user string)))
4738
4739 (defun erc-put-text-properties (start end properties
4740 &optional object value-list)
4741 "Set text-properties for OBJECT.
4742
4743 START and END describe positions in OBJECT.
4744 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
4745 each property to the corresponding value in VALUE-LIST."
4746 (unless value-list
4747 (setq value-list (mapcar (lambda (x)
4748 t)
4749 properties)))
4750 (mapcar* (lambda (prop value)
4751 (erc-put-text-property start end prop value object))
4752 properties value-list))
4753
4754 ;;; Input area handling:
4755
4756 (defun erc-beg-of-input-line ()
4757 "Return the value of `point' at the beginning of the input line.
4758
4759 Specifically, return the position of `erc-insert-marker'."
4760 (or (and (boundp 'erc-insert-marker)
4761 (markerp erc-insert-marker))
4762 (error "erc-insert-marker has no value, please report a bug"))
4763 (marker-position erc-insert-marker))
4764
4765 (defun erc-end-of-input-line ()
4766 "Return the value of `point' at the end of the input line."
4767 (point-max))
4768
4769 (defun erc-send-current-line ()
4770 "Parse current line and send it to IRC."
4771 (interactive)
4772 (save-restriction
4773 (widen)
4774 (cond
4775 ((< (point) (erc-beg-of-input-line))
4776 (message "Point is not in the input area")
4777 (beep))
4778 ((not (erc-server-buffer-live-p))
4779 (message "ERC: No process running")
4780 (beep))
4781 (t
4782 (erc-set-active-buffer (current-buffer))
4783 (let ((inhibit-read-only t)
4784 (str (erc-user-input))
4785 (old-buf (current-buffer)))
4786
4787 ;; Kill the input and the prompt
4788 (delete-region (erc-beg-of-input-line)
4789 (erc-end-of-input-line))
4790
4791 (unwind-protect
4792 (erc-send-input str)
4793 ;; Fix the buffer if the command didn't kill it
4794 (when (buffer-live-p old-buf)
4795 (with-current-buffer old-buf
4796 (save-restriction
4797 (widen)
4798 (goto-char (point-max))
4799 (set-marker (process-mark erc-server-process) (point))
4800 (set-marker erc-insert-marker (point))
4801 (let ((buffer-modified (buffer-modified-p)))
4802 (erc-display-prompt)
4803 (set-buffer-modified-p buffer-modified))))))
4804
4805 ;; Only when last hook has been run...
4806 (run-hook-with-args 'erc-send-completed-hook str))))))
4807
4808 (defun erc-user-input ()
4809 "Return the input of the user in the current buffer."
4810 (buffer-substring
4811 erc-input-marker
4812 (erc-end-of-input-line)))
4813
4814 (defun erc-send-input (input)
4815 "Treat INPUT as typed in by the user. It is assumed that the input
4816 and the prompt is already deleted.
4817 This returns non-nil only iff we actually send anything."
4818 ;; Handle different kinds of inputs
4819 (cond
4820 ;; Ignore empty input
4821 ((if erc-send-whitespace-lines
4822 (string= input "")
4823 (string-match "\\`[ \t\r\f\n]*\\'" input))
4824 (when erc-warn-about-blank-lines
4825 (message "Blank line - ignoring...")
4826 (beep))
4827 nil)
4828 (t
4829 (let ((str input)
4830 (erc-insert-this t))
4831 (setq erc-send-this t)
4832 (run-hook-with-args 'erc-send-pre-hook input)
4833 (when erc-send-this
4834 (if (or (string-match "\n" str)
4835 (not (char-equal (aref str 0) ?/)))
4836 (mapc
4837 (lambda (line)
4838 (mapc
4839 (lambda (line)
4840 ;; Insert what has to be inserted for this.
4841 (erc-display-msg line)
4842 (erc-process-input-line (concat line "\n")
4843 (null erc-flood-protect) t))
4844 (erc-split-line line)))
4845 (split-string str "\n"))
4846 ;; Insert the prompt along with the command.
4847 (erc-display-command str)
4848 (erc-process-input-line (concat str "\n") t nil))
4849 t)))))
4850
4851 (defun erc-display-command (line)
4852 (when erc-insert-this
4853 (let ((insert-position (point)))
4854 (unless erc-hide-prompt
4855 (erc-display-prompt nil nil (erc-command-indicator)
4856 (and (erc-command-indicator)
4857 'erc-command-indicator-face)))
4858 (let ((beg (point)))
4859 (insert line)
4860 (erc-put-text-property beg (point)
4861 'face 'erc-command-indicator-face)
4862 (insert "\n"))
4863 (set-marker (process-mark erc-server-process) (point))
4864 (set-marker erc-insert-marker (point))
4865 (save-excursion
4866 (save-restriction
4867 (narrow-to-region insert-position (point))
4868 (run-hooks 'erc-send-modify-hook)
4869 (run-hooks 'erc-send-post-hook))))))
4870
4871 (defun erc-display-msg (line)
4872 "Display LINE as a message of the user to the current target at the
4873 current position."
4874 (when erc-insert-this
4875 (let ((insert-position (point)))
4876 (insert (erc-format-my-nick))
4877 (let ((beg (point)))
4878 (insert line)
4879 (erc-put-text-property beg (point)
4880 'face 'erc-input-face))
4881 (insert "\n")
4882 (set-marker (process-mark erc-server-process) (point))
4883 (set-marker erc-insert-marker (point))
4884 (save-excursion
4885 (save-restriction
4886 (narrow-to-region insert-position (point))
4887 (run-hooks 'erc-send-modify-hook)
4888 (run-hooks 'erc-send-post-hook))))))
4889
4890 (defun erc-command-symbol (command)
4891 "Return the erc command symbol for COMMAND if it exists and is bound."
4892 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
4893 (when (fboundp cmd) cmd)))
4894
4895 (defun erc-extract-command-from-line (line)
4896 "Extract command and args from the input LINE.
4897 If no command was given, return nil. If command matches, return a
4898 list of the form: (command args) where both elements are strings."
4899 (when (string-match "^/\\([A-Za-z]+\\)\\(\\s-+.*\\|\\s-*\\)$" line)
4900 (let* ((cmd (erc-command-symbol (match-string 1 line)))
4901 ;; note: return is nil, we apply this simply for side effects
4902 (canon-defun (while (and cmd (symbolp (symbol-function cmd)))
4903 (setq cmd (symbol-function cmd))))
4904 (cmd-fun (or cmd #'erc-cmd-default))
4905 (arg (if cmd
4906 (if (get cmd-fun 'do-not-parse-args)
4907 (format "%s" (match-string 2 line))
4908 (delete "" (split-string (erc-trim-string
4909 (match-string 2 line)) " ")))
4910 line)))
4911 (list cmd-fun arg))))
4912
4913 (defun erc-split-multiline-safe (string)
4914 "Split STRING, containing multiple lines and return them in a list.
4915 Do it only for STRING as the complete input, do not carry unfinished
4916 strings over to the next call."
4917 (let ((l ())
4918 (i0 0)
4919 (doit t))
4920 (while doit
4921 (let ((i (string-match "\r?\n" string i0))
4922 (s (substring string i0)))
4923 (cond (i (setq l (cons (substring string i0 i) l))
4924 (setq i0 (match-end 0)))
4925 ((> (length s) 0)
4926 (setq l (cons s l))(setq doit nil))
4927 (t (setq doit nil)))))
4928 (nreverse l)))
4929
4930 ;; nick handling
4931
4932 (defun erc-set-current-nick (nick)
4933 "Set the current nickname to NICK."
4934 (with-current-buffer (or (erc-server-buffer)
4935 (current-buffer))
4936 (setq erc-server-current-nick nick)))
4937
4938 (defun erc-current-nick ()
4939 "Return the current nickname."
4940 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
4941 (erc-server-buffer)
4942 (current-buffer))
4943 erc-server-current-nick))
4944
4945 (defun erc-current-nick-p (nick)
4946 "Return non-nil if NICK is the current nickname."
4947 (erc-nick-equal-p nick (erc-current-nick)))
4948
4949 (defun erc-nick-equal-p (nick1 nick2)
4950 "Return non-nil if NICK1 and NICK2 are the same.
4951
4952 This matches strings according to the IRC protocol's case convention.
4953
4954 See also `erc-downcase'."
4955 (string= (erc-downcase nick1)
4956 (erc-downcase nick2)))
4957
4958 ;; default target handling
4959
4960 (defun erc-default-target ()
4961 "Return the current default target (as a character string) or nil if none."
4962 (let ((tgt (car erc-default-recipients)))
4963 (cond
4964 ((not tgt) nil)
4965 ((listp tgt) (cdr tgt))
4966 (t tgt))))
4967
4968 (defun erc-add-default-channel (channel)
4969 "Add CHANNEL to the default channel list."
4970
4971 (let ((d1 (car erc-default-recipients))
4972 (d2 (cdr erc-default-recipients))
4973 (chl (downcase channel)))
4974 (setq erc-default-recipients
4975 (cons chl erc-default-recipients))))
4976
4977 (defun erc-delete-default-channel (channel &optional buffer)
4978 "Delete CHANNEL from the default channel list."
4979 (let ((ob (current-buffer)))
4980 (with-current-buffer (if (and buffer
4981 (bufferp buffer))
4982 buffer
4983 (current-buffer))
4984 (setq erc-default-recipients (delete (downcase channel)
4985 erc-default-recipients)))))
4986
4987 (defun erc-add-query (nickname)
4988 "Add QUERY'd NICKNAME to the default channel list.
4989
4990 The previous default target of QUERY type gets removed"
4991 (let ((d1 (car erc-default-recipients))
4992 (d2 (cdr erc-default-recipients))
4993 (qt (cons 'QUERY (downcase nickname))))
4994 (if (and (listp d1)
4995 (eq (car d1) 'QUERY))
4996 (setq erc-default-recipients (cons qt d2))
4997 (setq erc-default-recipients (cons qt erc-default-recipients)))))
4998
4999 (defun erc-delete-query ()
5000 "Delete the topmost target if it is a QUERY."
5001
5002 (let ((d1 (car erc-default-recipients))
5003 (d2 (cdr erc-default-recipients)))
5004 (if (and (listp d1)
5005 (eq (car d1) 'QUERY))
5006 (setq erc-default-recipients d2)
5007 (error "Current target is not a QUERY"))))
5008
5009 (defun erc-ignored-user-p (spec)
5010 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5011
5012 Takes a full SPEC of a user in the form \"nick!login@host\", and
5013 matches against all the regexp's in `erc-ignore-list'. If any
5014 match, returns that regexp."
5015 (dolist (ignored (with-current-buffer (erc-server-buffer) erc-ignore-list))
5016 (if (string-match ignored spec)
5017 ;; We have `require'd cl, so we can return from the block named nil
5018 (return ignored))))
5019
5020 (defun erc-ignored-reply-p (msg tgt proc)
5021 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5022 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5023
5024 Takes a message MSG to a channel and returns non-nil if the addressed
5025 user matches any regexp in `erc-ignore-reply-list'."
5026 (let ((target-nick (erc-message-target msg)))
5027 (if (not target-nick)
5028 nil
5029 (erc-with-buffer (tgt proc)
5030 (let ((user (erc-get-server-user target-nick)))
5031 (when user
5032 (erc-list-match erc-ignore-reply-list
5033 (erc-user-spec user))))))))
5034
5035 (defun erc-message-target (msg)
5036 "Return the addressed target in MSG.
5037
5038 The addressed target is the string before the first colon in MSG."
5039 (if (string-match "^\\([^: ]*\\):" msg)
5040 (match-string 1 msg)
5041 nil))
5042
5043 (defun erc-user-spec (user)
5044 "Create a nick!user@host spec from a user struct."
5045 (let ((nick (erc-server-user-nickname user))
5046 (host (erc-server-user-host user))
5047 (login (erc-server-user-login user)))
5048 (concat (if nick
5049 nick
5050 "")
5051 "!"
5052 (if login
5053 login
5054 "")
5055 "@"
5056 (if host
5057 host
5058 ""))))
5059
5060 (defun erc-list-match (lst str)
5061 "Return non-nil if any regexp in LST matches STR."
5062 (memq nil (mapcar (lambda (regexp)
5063 (not (string-match regexp str)))
5064 lst)))
5065
5066 ;; other "toggles"
5067
5068 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5069 "Toggle automatic CTCP replies (like VERSION and PING).
5070
5071 If ARG is positive, turns CTCP replies on.
5072
5073 If ARG is non-nil and not positive, turns CTCP replies off."
5074 (interactive "P")
5075 (cond ((and (numberp arg) (> arg 0))
5076 (setq erc-disable-ctcp-replies t))
5077 (arg (setq erc-disable-ctcp-replies nil))
5078 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5079 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5080
5081 (defun erc-toggle-flood-control (&optional arg)
5082 "Toggle use of flood control on sent messages.
5083
5084 If ARG is non-nil, use flood control.
5085 If ARG is nil, do not use flood control.
5086
5087 See `erc-server-flood-margin' for an explanation of the available
5088 flood control parameters."
5089 (interactive "P")
5090 (setq erc-flood-protect arg)
5091 (message "ERC flood control is %s"
5092 (cond (erc-flood-protect "ON")
5093 (t "OFF"))))
5094
5095 ;; Some useful channel and nick commands for fast key bindings
5096
5097 (defun erc-invite-only-mode (&optional arg)
5098 "Turn on the invite only mode (+i) for the current channel.
5099
5100 If ARG is non-nil, turn this mode off (-i).
5101
5102 This command is sent even if excess flood is detected."
5103 (interactive "P")
5104 (erc-set-active-buffer (current-buffer))
5105 (let ((tgt (erc-default-target))
5106 (erc-force-send t))
5107 (cond ((or (not tgt) (not (erc-channel-p tgt)))
5108 (erc-display-message nil 'error (current-buffer) 'no-target))
5109 (arg (erc-load-irc-script-lines (list (concat "/mode " tgt " -i"))
5110 t))
5111 (t (erc-load-irc-script-lines (list (concat "/mode " tgt " +i"))
5112 t)))))
5113
5114 (defun erc-get-channel-mode-from-keypress (key)
5115 "Read a key sequence and call the corresponding channel mode function.
5116 After doing C-c C-o type in a channel mode letter.
5117
5118 C-g means quit.
5119 RET let's you type more than one mode at a time.
5120 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5121 If \"k\" is pressed, `erc-set-channel-key' gets called.
5122 Anything else will be sent to `erc-toggle-channel-mode'."
5123 (interactive "kChannel mode (RET to set more than one): ")
5124 (when (featurep 'xemacs)
5125 (setq key (char-to-string (event-to-character (aref key 0)))))
5126 (cond ((equal key "\C-g")
5127 (keyboard-quit))
5128 ((equal key "\C-m")
5129 (erc-insert-mode-command))
5130 ((equal key "l")
5131 (call-interactively 'erc-set-channel-limit))
5132 ((equal key "k")
5133 (call-interactively 'erc-set-channel-key))
5134 (t (erc-toggle-channel-mode key))))
5135
5136 (defun erc-toggle-channel-mode (mode &optional channel)
5137 "Toggle channel MODE.
5138
5139 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5140 `erc-default-target'."
5141 (interactive "P")
5142 (erc-set-active-buffer (current-buffer))
5143 (let ((tgt (or channel (erc-default-target)))
5144 (erc-force-send t))
5145 (cond ((or (null tgt) (null (erc-channel-p tgt)))
5146 (erc-display-message nil 'error 'active 'no-target))
5147 ((member mode erc-channel-modes)
5148 (erc-log (format "%s: Toggle mode %s OFF" tgt mode))
5149 (message "Toggle channel mode %s OFF" mode)
5150 (erc-server-send (format "MODE %s -%s" tgt mode)))
5151 (t (erc-log (format "%s: Toggle channel mode %s ON" tgt mode))
5152 (message "Toggle channel mode %s ON" mode)
5153 (erc-server-send (format "MODE %s +%s" tgt mode))))))
5154
5155 (defun erc-insert-mode-command ()
5156 "Insert the line \"/mode <current target> \" at `point'."
5157 (interactive)
5158 (let ((tgt (erc-default-target)))
5159 (if tgt (insert (concat "/mode " tgt " "))
5160 (erc-display-message nil 'error (current-buffer) 'no-target))))
5161
5162 (defun erc-channel-names ()
5163 "Run \"/names #channel\" in the current channel."
5164 (interactive)
5165 (erc-set-active-buffer (current-buffer))
5166 (let ((tgt (erc-default-target)))
5167 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5168 (erc-display-message nil 'error (current-buffer) 'no-target))))
5169
5170 (defun erc-remove-text-properties-region (start end &optional object)
5171 "Clears the region (START,END) in OBJECT from all colors, etc."
5172 (interactive "r")
5173 (save-excursion
5174 (let ((inhibit-read-only t))
5175 (set-text-properties start end nil object))))
5176
5177 ;; script execution and startup
5178
5179 (defun erc-find-file (file &optional path)
5180 "Search for a FILE in the filesystem.
5181 First the `default-directory' is searched for FILE, then any directories
5182 specified in the list PATH.
5183
5184 If FILE is found, return the path to it."
5185 (let ((filepath file))
5186 (if (file-readable-p filepath) filepath
5187 (progn
5188 (while (and path
5189 (progn (setq filepath (expand-file-name file (car path)))
5190 (not (file-readable-p filepath))))
5191 (setq path (cdr path)))
5192 (if path filepath nil)))))
5193
5194 (defun erc-select-startup-file ()
5195 "Select an ERC startup file.
5196 See also `erc-startup-file-list'."
5197 (let ((l erc-startup-file-list)
5198 (f nil))
5199 (while (and (not f) l)
5200 (if (file-readable-p (car l))
5201 (setq f (car l)))
5202 (setq l (cdr l)))
5203 f))
5204
5205 (defun erc-find-script-file (file)
5206 "Search for FILE in `default-directory', and any in `erc-script-path'."
5207 (erc-find-file file erc-script-path))
5208
5209 (defun erc-load-script (file)
5210 "Load a script from FILE.
5211
5212 FILE must be the full name, it is not searched in the
5213 `erc-script-path'. If the filename ends with `.el', then load it
5214 as a emacs-lisp program. Otherwise, treat it as a regular IRC
5215 script"
5216 (erc-log (concat "erc-load-script: " file))
5217 (cond
5218 ((string-match "\\.el$" file)
5219 (load file))
5220 (t
5221 (erc-load-irc-script file))))
5222
5223 (defun erc-process-script-line (line &optional args)
5224 "Process an IRC script LINE.
5225
5226 Does script-specific substitutions (script arguments, current nick,
5227 server, etc.) in LINE and returns it.
5228
5229 Substitutions are: %C and %c = current target (channel or nick),
5230 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5231 where x is any other character;
5232 $* = the entire argument string, $1 = the first argument, $2 = the second,
5233 end so on."
5234 (if (not args) (setq args ""))
5235 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5236 (percent-regexp "\\(%.\\)")
5237 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5238 (tgt (erc-default-target))
5239 (server (and (boundp 'erc-session-server) erc-session-server))
5240 (nick (erc-current-nick))
5241 (res "")
5242 (tmp nil)
5243 (arg-list nil)
5244 (arg-num 0))
5245 (if (not tgt) (setq tgt ""))
5246 (if (not server) (setq server ""))
5247 (if (not nick) (setq nick ""))
5248 ;; First, compute the argument list
5249 (setq tmp args)
5250 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5251 (setq arg-list (cons (match-string 1 tmp) arg-list))
5252 (setq tmp (match-string 2 tmp)))
5253 (setq arg-list (nreverse arg-list))
5254 (setq arg-num (length arg-list))
5255 ;; now do the substitution
5256 (setq tmp (string-match esc-regexp line))
5257 (while tmp
5258 ;;(message "beginning of while: tmp=%S" tmp)
5259 (let* ((hd (substring line 0 tmp))
5260 (esc "")
5261 (subst "")
5262 (tail (substring line tmp)))
5263 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5264 (setq esc (match-string 1 tail))
5265 (setq tail (substring tail (match-end 1))))
5266 ((string-match (concat "^" percent-regexp) tail)
5267 (setq esc (match-string 1 tail))
5268 (setq tail (substring tail (match-end 1)))))
5269 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5270 (setq res (concat res hd))
5271 (setq subst
5272 (cond ((string= esc "") "")
5273 ((string-match "^\\$\\*$" esc) args)
5274 ((string-match "^\\$\\([0-9]+\\)$" esc)
5275 (let ((n (string-to-number (match-string 1 esc))))
5276 (message "n = %S, integerp(n)=%S" n (integerp n))
5277 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5278 ((string-match "^%[Cc]$" esc) tgt)
5279 ((string-match "^%[Ss]$" esc) server)
5280 ((string-match "^%[Nn]$" esc) nick)
5281 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5282 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5283 (message "BUG IN ERC: esc=%S" esc)
5284 "")))
5285 (setq line tail)
5286 (setq tmp (string-match esc-regexp line))
5287 (setq res (concat res subst))
5288 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5289 ))
5290 (setq res (concat res line))
5291 res))
5292
5293 (defun erc-load-irc-script (file &optional force)
5294 "Load an IRC script from FILE."
5295 (erc-log (concat "erc-load-script: " file))
5296 (let ((str (with-temp-buffer
5297 (insert-file-contents file)
5298 (buffer-string))))
5299 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5300
5301 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5302 "Load IRC script LINES (a list of strings).
5303
5304 If optional NOEXPAND is non-nil, do not expand script-specific
5305 sequences, process the lines verbatim. Use this for multiline
5306 user input."
5307 (let* ((cb (current-buffer))
5308 (pnt (point))
5309 (s "")
5310 (sp (or (erc-command-indicator) (erc-prompt)))
5311 (args (and (boundp 'erc-script-args) erc-script-args)))
5312 (if (and args (string-match "^ " args))
5313 (setq args (substring args 1)))
5314 ;; prepare the prompt string for echo
5315 (erc-put-text-property 0 (length sp)
5316 'face 'erc-command-indicator-face sp)
5317 (while lines
5318 (setq s (car lines))
5319 (erc-log (concat "erc-load-script: CMD: " s))
5320 (unless (string-match "^\\s-*$" s)
5321 (let ((line (if noexpand s (erc-process-script-line s args))))
5322 (if (and (erc-process-input-line line force)
5323 erc-script-echo)
5324 (progn
5325 (erc-put-text-property 0 (length line)
5326 'face 'erc-input-face line)
5327 (erc-display-line (concat sp line) cb)))))
5328 (setq lines (cdr lines)))))
5329
5330 ;; authentication
5331
5332 (defun erc-login ()
5333 "Perform user authentication at the IRC server."
5334 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5335 (erc-current-nick)
5336 (user-login-name)
5337 (system-name)
5338 erc-session-server
5339 erc-session-user-full-name))
5340 (if erc-session-password
5341 (erc-server-send (format "PASS %s" erc-session-password))
5342 (message "Logging in without password"))
5343 (erc-server-send (format "NICK %s" (erc-current-nick)))
5344 (erc-server-send
5345 (format "USER %s %s %s :%s"
5346 ;; hacked - S.B.
5347 (if erc-anonymous-login erc-email-userid (user-login-name))
5348 "0" "*"
5349 erc-session-user-full-name))
5350 (erc-update-mode-line))
5351
5352 ;; connection properties' heuristics
5353
5354 (defun erc-determine-parameters (&optional server port nick name)
5355 "Determine the connection and authentication parameters.
5356 Sets the buffer local variables:
5357
5358 - erc-session-server
5359 - erc-session-port
5360 - erc-session-full-name
5361 - erc-server-current-nick"
5362 (setq erc-session-server (erc-compute-server server)
5363 erc-session-port (or port erc-default-port)
5364 erc-session-user-full-name (erc-compute-full-name name))
5365 (erc-set-current-nick (erc-compute-nick nick)))
5366
5367 (defun erc-compute-server (&optional server)
5368 "Return an IRC server name.
5369
5370 Tries a number of increasingly more default methods until a non-nil value is
5371 found:
5372
5373 - SERVER
5374 - `erc-server'
5375 - The value of the IRCSERVER environment variable
5376 - `erc-default-server'."
5377 (or server
5378 erc-server
5379 (getenv "IRCSERVER")
5380 erc-default-server))
5381
5382 (defun erc-compute-nick (&optional nick)
5383 "Return user's NICK.
5384
5385 Tries a number of increasingly more default methods until a non-nil value is
5386 found:
5387
5388 - NICK
5389 - `erc-nick'
5390 - The value of the IRCNICK environment variable
5391 - via the function `user-login-name'."
5392 (or nick
5393 (if (consp erc-nick) (car erc-nick) erc-nick)
5394 (getenv "IRCNICK")
5395 (user-login-name)))
5396
5397
5398 (defun erc-compute-full-name (&optional full-name)
5399 "Return user's FULL-NAME.
5400
5401 Tries a number of increasingly more default methods until a non-nil value is
5402 found:
5403
5404 - FULL-NAME
5405 - `erc-user-full-name'
5406 - The value of the IRCNAME environment variable
5407 - via the function `user-full-name'."
5408 (or full-name
5409 erc-user-full-name
5410 (getenv "IRCNAME")
5411 (if erc-anonymous-login "unknown" nil)
5412 (user-full-name)))
5413
5414 (defun erc-compute-port (&optional port)
5415 "Return a port for an IRC server.
5416
5417 Tries a number of increasingly more default methods until a non-nil
5418 value is found:
5419
5420 - PORT
5421 - \"ircd\"."
5422 (or port erc-port "ircd"))
5423
5424 ;; time routines
5425
5426 (defun erc-string-to-emacs-time (string)
5427 "Convert the long number represented by STRING into an Emacs format.
5428 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
5429 (let* ((n (string-to-number (concat string ".0"))))
5430 (list (truncate (/ n 65536))
5431 (truncate (mod n 65536)))))
5432
5433 (defun erc-emacs-time-to-erc-time (time)
5434 "Convert Emacs TIME to a number of seconds since the epoch."
5435 (when time
5436 (+ (* (nth 0 time) 65536.0) (nth 1 time))))
5437 ; (round (+ (* (nth 0 tm) 65536.0) (nth 1 tm))))
5438
5439 (defun erc-current-time ()
5440 "Return the `current-time' as a number of seconds since the epoch.
5441
5442 See also `erc-emacs-time-to-erc-time'."
5443 (erc-emacs-time-to-erc-time (current-time)))
5444
5445 (defun erc-time-diff (t1 t2)
5446 "Return the time difference in seconds between T1 and T2."
5447 (abs (- t2 t1)))
5448
5449 (defun erc-time-gt (t1 t2)
5450 "Check whether T1 > T2."
5451 (> t1 t2))
5452
5453 (defun erc-sec-to-time (ns)
5454 "Convert NS to a time string HH:MM.SS."
5455 (setq ns (truncate ns))
5456 (format "%02d:%02d.%02d"
5457 (/ ns 3600)
5458 (/ (% ns 3600) 60)
5459 (% ns 60)))
5460
5461 (defun erc-seconds-to-string (seconds)
5462 "Convert a number of SECONDS into an English phrase."
5463 (let (days hours minutes format-args output)
5464 (setq days (/ seconds 86400)
5465 seconds (% seconds 86400)
5466 hours (/ seconds 3600)
5467 seconds (% seconds 3600)
5468 minutes (/ seconds 60)
5469 seconds (% seconds 60)
5470 format-args (if (> days 0)
5471 `("%d days, %d hours, %d minutes, %d seconds"
5472 ,days ,hours ,minutes ,seconds)
5473 (if (> hours 0)
5474 `("%d hours, %d minutes, %d seconds"
5475 ,hours ,minutes ,seconds)
5476 (if (> minutes 0)
5477 `("%d minutes, %d seconds" ,minutes ,seconds)
5478 `("%d seconds" ,seconds))))
5479 output (apply 'format format-args))
5480 ;; Change all "1 units" to "1 unit".
5481 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
5482 (setq output (erc-replace-match-subexpression-in-string
5483 "" output (match-string 2 output) 2 (match-beginning 2))))
5484 output))
5485
5486
5487 ;; info
5488
5489 (defconst erc-clientinfo-alist
5490 '(("ACTION" . "is used to inform about one's current activity")
5491 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
5492 ("ECHO" . "echoes its arguments back")
5493 ("FINGER" . "shows user's name, location, and idle time")
5494 ("PING" . "measures delay between peers")
5495 ("TIME" . "shows client-side time")
5496 ("USERINFO" . "shows information provided by a user")
5497 ("VERSION" . "shows client type and version"))
5498 "Alist of CTCP CLIENTINFO for ERC commands.")
5499
5500 (defun erc-client-info (s)
5501 "Return CTCP CLIENTINFO on command S.
5502 If S is NIL or an empty string then return general CLIENTINFO"
5503 (if (or (not s) (string= s ""))
5504 (concat
5505 (apply #'concat
5506 (mapcar (lambda (e)
5507 (concat (car e) " "))
5508 erc-clientinfo-alist))
5509 ": use CLIENTINFO <COMMAND> to get more specific information")
5510 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
5511 (if h
5512 (concat s " " (cdr h))
5513 (concat s ": unknown command")))))
5514
5515 ;; Hook functions
5516
5517 (defun erc-directory-writable-p (dir)
5518 "Determine whether DIR is a writable directory.
5519 If it doesn't exist, create it."
5520 (unless (file-attributes dir) (make-directory dir))
5521 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
5522
5523 (defun erc-kill-query-buffers (process)
5524 "Kill all buffers of PROCESS."
5525 ;; here, we only want to match the channel buffers, to avoid
5526 ;; "selecting killed buffers" b0rkage.
5527 (erc-with-all-buffers-of-server process
5528 (lambda ()
5529 (not (erc-server-buffer-p)))
5530 (kill-buffer (current-buffer))))
5531
5532 (defun erc-nick-at-point ()
5533 "Give information about the nickname at `point'.
5534
5535 If called interactively, give a human readable message in the
5536 minibuffer. If called programatically, return the corresponding
5537 entry of `channel-members'."
5538 (interactive)
5539 (require 'thingatpt)
5540 (let* ((word (word-at-point))
5541 (channel-data (erc-get-channel-user word))
5542 (cuser (cdr channel-data))
5543 (user (if channel-data
5544 (car channel-data)
5545 (erc-get-server-user word)))
5546 host login full-name info nick op voice)
5547 (when user
5548 (setq nick (erc-server-user-nickname user)
5549 host (erc-server-user-host user)
5550 login (erc-server-user-login user)
5551 full-name (erc-server-user-full-name user)
5552 info (erc-server-user-info user))
5553 (if cuser
5554 (setq op (erc-channel-user-op cuser)
5555 voice (erc-channel-user-voice cuser)))
5556 (if (interactive-p)
5557 (message "%s is %s@%s%s%s"
5558 nick login host
5559 (if full-name (format " (%s)" full-name) "")
5560 (if (or op voice)
5561 (format " and is +%s%s on %s"
5562 (if op "o" "")
5563 (if voice "v" "")
5564 (erc-default-target))
5565 ""))
5566 user))))
5567
5568 (defun erc-away-p ()
5569 "Return t if the current ERC process is set away."
5570 (save-excursion
5571 (and (erc-server-buffer-live-p)
5572 (set-buffer (process-buffer erc-server-process))
5573 erc-away)))
5574
5575 ;; Mode line handling
5576
5577 (defcustom erc-mode-line-format "%s %a"
5578 "A string to be formatted and shown in the mode-line in `erc-mode'.
5579
5580 The string is formatted using `format-spec' and the result is set as the value
5581 of `mode-line-buffer-identification'.
5582
5583 The following characters are replaced:
5584 %a: String indicating away status or \"\" if you are not away
5585 %m: The modes of the channel
5586 %n: The current nick name
5587 %o: The topic of the channel
5588 %p: The session port
5589 %t: The name of the target (channel, nickname, or servername:port)
5590 %s: In the server-buffer, this gets filled with the value of
5591 `erc-server-announced-name', in a channel, the value of
5592 (erc-default-target) also get concatenated."
5593 :group 'erc-mode-line-and-header
5594 :type 'string)
5595
5596 (defcustom erc-header-line-format "[IRC] %n on %t %m %o"
5597 "A string to be formatted and shown in the header-line in `erc-mode'.
5598 Only used in Emacs 21.
5599
5600 See `erc-mode-line-format' for which characters are can be used."
5601 :group 'erc-mode-line-and-header
5602 :type 'string)
5603
5604 (defcustom erc-header-line-uses-help-echo-p t
5605 "Show the contents of the header line in the echo area or as a tooltip
5606 when you move point into the header line."
5607 :group 'erc-mode-line-and-header
5608 :type 'boolean)
5609
5610 (defcustom erc-show-channel-key-p t
5611 "Show the the channel key in the header line."
5612 :group 'erc-paranoia
5613 :type 'boolean)
5614
5615 (defcustom erc-common-server-suffixes
5616 '(("openprojects.net$" . "OPN")
5617 ("freenode.net$" . "OPN"))
5618 "Alist of common server name suffixes.
5619 This variable is used in mode-line display to save screen
5620 real estate. Set it to nil if you want to avoid changing
5621 displayed hostnames."
5622 :group 'erc-mode-line-and-header
5623 :type 'alist)
5624
5625 (defcustom erc-mode-line-away-status-format
5626 "(AWAY since %a %b %d %H:%M) "
5627 "When you're away on a server, this is shown in the mode line.
5628 This should be a string with substitution variables recognized by
5629 format-time-message."
5630 :group 'erc-mode-line-and-header
5631 :type 'string)
5632
5633 (defun erc-shorten-server-name (server-name)
5634 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
5635 (if (stringp server-name)
5636 (with-temp-buffer
5637 (insert server-name)
5638 (let ((alist erc-common-server-suffixes))
5639 (while alist
5640 (goto-char (point-min))
5641 (if (re-search-forward (caar alist) nil t)
5642 (replace-match (cdar alist)))
5643 (setq alist (cdr alist))))
5644 (buffer-string))))
5645
5646 (defun erc-format-target ()
5647 "Return the name of the target (channel or nickname or servername:port)."
5648 (let ((target (erc-default-target)))
5649 (or target
5650 (concat (erc-shorten-server-name
5651 (or erc-server-announced-name
5652 erc-session-server))
5653 ":" (erc-port-to-string erc-session-port)))))
5654
5655 (defun erc-format-target-and/or-server ()
5656 "Return the server name or the current target and server name combined."
5657 (let ((server-name (erc-shorten-server-name
5658 (or erc-server-announced-name
5659 erc-session-server))))
5660 (cond ((erc-default-target)
5661 (concat (erc-string-no-properties (erc-default-target))
5662 "@" server-name))
5663 (server-name server-name)
5664 (t (buffer-name (current-buffer))))))
5665
5666 (defun erc-format-away-status ()
5667 "Return a formatted `erc-mode-line-away-status-format'
5668 if `erc-away' is non-nil."
5669 (let ((a (when (erc-server-buffer-live-p)
5670 (with-current-buffer (process-buffer erc-server-process)
5671 erc-away))))
5672 (if a
5673 (format-time-string erc-mode-line-away-status-format a)
5674 "")))
5675
5676 (defun erc-format-channel-modes ()
5677 "Return the current channel's modes and the estimated lag."
5678 (let ((lag (when (erc-server-buffer-live-p)
5679 (with-current-buffer (process-buffer erc-server-process)
5680 erc-server-lag))))
5681 (concat (apply 'concat
5682 "(+" erc-channel-modes)
5683 (cond ((and erc-channel-user-limit erc-channel-key)
5684 (if erc-show-channel-key-p
5685 (format "lk %.0f %s" erc-channel-user-limit
5686 erc-channel-key)
5687 (format "kl %.0f" erc-channel-user-limit)))
5688 (erc-channel-user-limit
5689 ;; Emacs has no bignums
5690 (format "l %.0f" erc-channel-user-limit))
5691 (erc-channel-key
5692 (if erc-show-channel-key-p
5693 (format "k %s" erc-channel-key)
5694 "k"))
5695 (t ""))
5696 (if lag (format ",lag:%.0f" lag) "")
5697 ")")))
5698
5699 (defun erc-update-mode-line-buffer (buffer)
5700 "Update the mode line in a single ERC buffer BUFFER."
5701 (with-current-buffer buffer
5702 (let ((spec (format-spec-make
5703 ?a (erc-format-away-status)
5704 ?m (erc-format-channel-modes)
5705 ?n (or (erc-current-nick) "")
5706 ?o (erc-controls-strip erc-channel-topic)
5707 ?p (erc-port-to-string erc-session-port)
5708 ?s (erc-format-target-and/or-server)
5709 ?t (erc-format-target)))
5710 (process-status (cond ((and (erc-server-process-alive)
5711 (not erc-server-connected))
5712 ":connecting")
5713 ((erc-server-process-alive)
5714 "")
5715 (t
5716 ": CLOSED"))))
5717 (cond ((featurep 'xemacs)
5718 (setq modeline-buffer-identification
5719 (list (format-spec erc-mode-line-format spec)))
5720 (setq modeline-process (list process-status)))
5721 (t
5722 (setq mode-line-buffer-identification
5723 (list (format-spec erc-mode-line-format spec)))
5724 (setq mode-line-process (list process-status))))
5725 (when (boundp 'header-line-format)
5726 (let ((header (if erc-header-line-format
5727 (format-spec erc-header-line-format spec)
5728 nil)))
5729 (cond ((null header)
5730 (setq header-line-format nil))
5731 (erc-header-line-uses-help-echo-p
5732 (let ((help-echo (with-temp-buffer
5733 (insert header)
5734 (fill-region (point-min) (point-max))
5735 (buffer-string))))
5736 (setq header-line-format
5737 (erc-replace-regexp-in-string
5738 "%"
5739 "%%"
5740 (erc-propertize header 'help-echo help-echo)))))
5741 (t (setq header-line-format header))))))
5742 (if (featurep 'xemacs)
5743 (redraw-modeline)
5744 (force-mode-line-update))))
5745
5746 (defun erc-update-mode-line (&optional buffer)
5747 "Update the mode line in BUFFER.
5748
5749 If BUFFER is nil, update the mode line in all ERC buffers."
5750 (if (and buffer (bufferp buffer))
5751 (erc-update-mode-line-buffer buffer)
5752 (dolist (buf (erc-buffer-list))
5753 (when (buffer-live-p buf)
5754 (erc-update-mode-line-buffer buf)))))
5755
5756 ;; Miscellaneous
5757
5758 (defun erc-port-to-string (p)
5759 "Convert port P to a string.
5760 P may be an integer or a service name."
5761 (if (integerp p)
5762 (int-to-string p)
5763 p))
5764
5765 (defun erc-string-to-port (s)
5766 "Convert string S to either an integer port number or a service name."
5767 (let ((n (string-to-number s)))
5768 (if (= n 0)
5769 s
5770 n)))
5771
5772 (defun erc-version (&optional here)
5773 "Show the version number of ERC in the minibuffer.
5774 If optional argument HERE is non-nil, insert version number at point."
5775 (interactive "P")
5776 (let ((version-string
5777 (format "ERC %s" erc-version-string)))
5778 (if here
5779 (insert version-string)
5780 (if (interactive-p)
5781 (message "%s" version-string)
5782 version-string))))
5783
5784 (defun erc-version-modules (&optional here)
5785 "Show the version numbers of all loaded ERC modules in the minibuffer.
5786 If optional argument HERE is non-nil, insert version number at point."
5787 (interactive "P")
5788 (let ((version-string
5789 (mapconcat 'identity
5790 (let (versions (case-fold-search nil))
5791 (dolist (var (apropos-internal "^erc-.*version$"))
5792 (when (and (boundp var)
5793 (stringp (symbol-value var)))
5794 (setq versions (cons (format "%S: %s"
5795 var (symbol-value var))
5796 versions))))
5797 versions) ", ")))
5798 (if here
5799 (insert version-string)
5800 (if (interactive-p)
5801 (message "%s" version-string)
5802 version-string))))
5803
5804 (defun erc-modes (&optional here)
5805 "Show the active ERC modes in the minibuffer.
5806 If optional argument HERE is non-nil, insert version number at point."
5807 (interactive "P")
5808 (let ((string
5809 (mapconcat 'identity
5810 (let (modes (case-fold-search nil))
5811 (dolist (var (apropos-internal "^erc-.*mode$"))
5812 (when (and (boundp var)
5813 (symbol-value var))
5814 (setq modes (cons (symbol-name var)
5815 modes))))
5816 modes)
5817 ", ")))
5818 (if here
5819 (insert string)
5820 (if (interactive-p)
5821 (message "%s" string)
5822 string))))
5823
5824 (defun erc-latest-version ()
5825 "Retrieve the latest erc.el version from CVS."
5826 (interactive)
5827 (if (ignore-errors (require 'url))
5828 (progn
5829 (switch-to-buffer (get-buffer-create "*erc.el latest version*"))
5830 (delete-region (point-min) (point-max))
5831 (kill-all-local-variables)
5832 (url-insert-file-contents (concat
5833 "http://cvs.sourceforge.net/viewcvs.py/"
5834 "*checkout*/erc/erc/erc.el?content-type"
5835 "=text%2Fplain&rev=HEAD"))
5836 (emacs-lisp-mode)
5837 (current-buffer))
5838 (error "URL needs to be installed")))
5839
5840 (defun erc-ediff-latest-version ()
5841 "Ediff your installed erc.el with the latest CVS version.
5842 See also `erc-latest-version'."
5843 (interactive)
5844 (let ((current (locate-library "erc.el")))
5845 (if current
5846 (ediff-buffers (find-file current)
5847 (erc-latest-version))
5848 (error "You do not appear to have the uncompiled erc.el file"))))
5849
5850 (defun erc-trim-string (s)
5851 "Trim leading and trailing spaces off S."
5852 (cond
5853 ((not (stringp s)) nil)
5854 ((string-match "^\\s-*$" s)
5855 "")
5856 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
5857 (match-string 1 s))
5858 (t
5859 s)))
5860
5861 (defun erc-arrange-session-in-multiple-windows ()
5862 "Open a window for every non-server buffer related to `erc-session-server'.
5863
5864 All windows are opened in the current frame."
5865 (interactive)
5866 (unless (boundp 'erc-server-process)
5867 (error "No erc-process found in current buffer"))
5868 (let ((bufs (erc-buffer-list nil erc-server-process)))
5869 (when bufs
5870 (delete-other-windows)
5871 (switch-to-buffer (car bufs))
5872 (setq bufs (cdr bufs))
5873 (while bufs
5874 (split-window)
5875 (switch-to-buffer-other-window (car bufs))
5876 (setq bufs (cdr bufs))
5877 (balance-windows)))))
5878
5879 (defun erc-popup-input-buffer ()
5880 "Provide a input buffer."
5881 (interactive)
5882 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
5883 (mode (intern
5884 (completing-read
5885 "Mode: "
5886 (mapcar (lambda (e)
5887 (list (symbol-name e)))
5888 (apropos-internal "-mode$" 'commandp))
5889 nil t))))
5890 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
5891 (funcall mode)
5892 (narrow-to-region (point) (point))
5893 (shrink-window-if-larger-than-buffer)))
5894
5895 ;;; Message catalog
5896
5897 (defun erc-make-message-variable-name (catalog entry)
5898 "Create a variable name corresponding to CATALOG's ENTRY."
5899 (intern (concat "erc-message-"
5900 (symbol-name catalog) "-" (symbol-name entry))))
5901
5902 (defun erc-define-catalog-entry (catalog entry format-spec)
5903 "Set CATALOG's ENTRY to FORMAT-SPEC."
5904 (set (erc-make-message-variable-name catalog entry)
5905 format-spec))
5906
5907 (defun erc-define-catalog (catalog entries)
5908 "Define a CATALOG according to ENTRIES."
5909 (dolist (entry entries)
5910 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
5911
5912 (erc-define-catalog
5913 'english
5914 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
5915 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
5916 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
5917 (cannot-find-file . "Cannot find file %f")
5918 (cannot-read-file . "Cannot read file %f")
5919 (connect . "Connecting to %S:%p... ")
5920 (country . "%c")
5921 (country-unknown . "%d: No such domain")
5922 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
5923 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
5924 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
5925 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
5926 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
5927 (flood-strict-mode . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
5928 (disconnected . "Connection failed! Re-establishing connection...")
5929 (disconnected-noreconnect . "Connection failed! Not re-establishing connection.")
5930 (login . "Logging in as \'%n\'...")
5931 (nick-in-use . "%n is in use. Choose new nickname: ")
5932 (nick-too-long . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
5933 (no-default-channel . "No default channel")
5934 (no-invitation . "You've got no invitation")
5935 (no-target . "No target")
5936 (ops . "%i operator%s: %o")
5937 (ops-none . "No operators in this channel.")
5938 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
5939 (variable-not-bound . "Variable not bound!")
5940 (ACTION . "* %n %a")
5941 (CTCP-CLIENTINFO . "Client info for %n: %m")
5942 (CTCP-ECHO . "Echo %n: %m")
5943 (CTCP-FINGER . "Finger info for %n: %m")
5944 (CTCP-PING . "Ping time to %n is %t")
5945 (CTCP-TIME . "Time by %n is %m")
5946 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
5947 (CTCP-VERSION . "Version for %n is %m")
5948 (ERROR . "==> ERROR from %s: %c\n")
5949 (INVITE . "%n (%u@%h) invites you to channel %c")
5950 (JOIN . "%n (%u@%h) has joined channel %c")
5951 (JOIN-you . "You have joined channel %c")
5952 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
5953 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
5954 (KICK-by-you . "You have kicked %k off channel %c: %r")
5955 (MODE . "%n (%u@%h) has changed mode for %t to %m")
5956 (MODE-nick . "%n has changed mode for %t to %m")
5957 (NICK . "%n (%u@%h) is now known as %N")
5958 (NICK-you . "Your new nickname is %N")
5959 (PART . erc-message-english-PART)
5960 (PING . "PING from server (last: %s sec. ago)")
5961 (PONG . "PONG from %h (%i second%s)")
5962 (QUIT . "%n (%u@%h) has quit: %r")
5963 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
5964 (WALLOPS . "Wallops from %n: %m")
5965 (s004 . "%s %v %U %C")
5966 (s221 . "User modes for %n: %m")
5967 (s252 . "%i operator(s) online")
5968 (s253 . "%i unknown connection(s)")
5969 (s254 . "%i channels formed")
5970 (s301 . "%n is AWAY: %r")
5971 (s303 . "Is online: %n")
5972 (s305 . "%m")
5973 (s306 . "%m")
5974 (s311 . "%n is %f (%u@%h)")
5975 (s312 . "%n is/was on server %s (%c)")
5976 (s313 . "%n is an IRC operator")
5977 (s314 . "%n was %f (%u@%h)")
5978 (s317 . "%n has been idle for %i")
5979 (s317-on-since . "%n has been idle for %i, on since %t")
5980 (s319 . "%n is on channel(s): %c")
5981 (s320 . "%n is an identified user")
5982 (s321 . "Channel Users Topic")
5983 (s322 . "%c [%u] %t")
5984 (s324 . "%c modes: %m")
5985 (s329 . "%c was created on %t")
5986 (s330 . "%n %a %i")
5987 (s331 . "No topic is set for %c")
5988 (s332 . "Topic for %c: %T")
5989 (s333 . "%c: topic set by %n, %t")
5990 (s341 . "Inviting %n to channel %c")
5991 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
5992 (s353 . "Users on %c: %u")
5993 (s367 . "Ban on %b on %c set by %s on %t (Use /banlist!)")
5994 (s368 . "Banlist of %c ends.")
5995 (s379 . "%c: Forwarded to %f")
5996 (s391 . "The time at %s is %t")
5997 (s401 . "%n: No such nick/channel")
5998 (s403 . "%c: No such channel")
5999 (s404 . "%c: Cannot send to channel")
6000 (s405 . "%c: You have joined too many channels")
6001 (s406 . "%n: There was no such nickname")
6002 (s412 . "No text to send")
6003 (s421 . "%c: Unknown command")
6004 (s431 . "No nickname given")
6005 (s432 . "%n is an erroneous nickname")
6006 (s442 . "%c: You're not on that channel")
6007 (s445 . "SUMMON has been disabled")
6008 (s446 . "USERS has been disabled")
6009 (s451 . "You have not registered")
6010 (s461 . "%c: not enough parameters")
6011 (s462 . "Unauthorized command (already registered)")
6012 (s463 . "Your host isn't among the privileged")
6013 (s464 . "Password incorrect")
6014 (s465 . "You are banned from this server")
6015 (s474 . "You can't join %c because you're banned (+b)")
6016 (s475 . "You must specify the correct channel key (+k) to join %c")
6017 (s481 . "Permission Denied - You're not an IRC operator")
6018 (s482 . "You need to be a channel operator of %c to do that")
6019 (s483 . "You can't kill a server!")
6020 (s484 . "Your connection is restricted!")
6021 (s485 . "You're not the original channel operator")
6022 (s491 . "No O-lines for your host")
6023 (s501 . "Unknown MODE flag")
6024 (s502 . "You can't change modes for other users")))
6025
6026 (defun erc-message-english-PART (&rest args)
6027 "Format a proper PART message.
6028
6029 This function is an example on what could be done with formatting
6030 functions."
6031 (let ((nick (cadr (memq ?n args)))
6032 (user (cadr (memq ?u args)))
6033 (host (cadr (memq ?h args)))
6034 (channel (cadr (memq ?c args)))
6035 (reason (cadr (memq ?r args))))
6036 (if (string= nick (erc-current-nick))
6037 (format "You have left channel %s" channel)
6038 (format "%s (%s@%s) has left channel %s%s"
6039 nick user host channel
6040 (if (not (string= reason ""))
6041 (format ": %s" reason)
6042 "")))))
6043
6044
6045 (defvar erc-current-message-catalog 'english)
6046 (make-variable-buffer-local 'erc-current-message-catalog)
6047
6048 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6049 "Retrieve ENTRY from CATALOG.
6050
6051 If CATALOG is nil, `erc-current-message-catalog' is used.
6052
6053 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6054 english, catalog."
6055 (unless catalog (setq catalog erc-current-message-catalog))
6056 (let ((var (erc-make-message-variable-name catalog entry)))
6057 (if (boundp var)
6058 (symbol-value var)
6059 (when (boundp (erc-make-message-variable-name 'english entry))
6060 (symbol-value (erc-make-message-variable-name 'english entry))))))
6061
6062 (defun erc-format-message (msg &rest args)
6063 "Format MSG according to ARGS.
6064
6065 See also `format-spec'."
6066 (when (eq (logand (length args) 1) 1) ; oddp
6067 (error "Obscure usage of this function appeared"))
6068 (let ((entry (erc-retrieve-catalog-entry msg)))
6069 (when (not entry)
6070 (error "No format spec for message %s" msg))
6071 (when (functionp entry)
6072 (setq entry (apply entry args)))
6073 (format-spec entry (apply 'format-spec-make args))))
6074
6075 ;;; Various hook functions
6076
6077 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6078
6079 (defcustom erc-kill-server-hook '(erc-kill-server)
6080 "*Invoked whenever a server-buffer is killed via `kill-buffer'."
6081 :group 'erc-hooks
6082 :type 'hook)
6083
6084 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6085 "*Invoked whenever a channel-buffer is killed via `kill-buffer'."
6086 :group 'erc-hooks
6087 :type 'hook)
6088
6089 (defcustom erc-kill-buffer-hook nil
6090 "*Hook run whenever a non-server or channel buffer is killed.
6091
6092 See also `kill-buffer'."
6093 :group 'erc-hooks
6094 :type 'hook)
6095
6096 (defun erc-kill-buffer-function ()
6097 "Function to call when an ERC buffer is killed.
6098 This function should be on `kill-buffer-hook'.
6099 When the current buffer is in `erc-mode', this function will run
6100 one of the following hooks:
6101 `erc-kill-server-hook' if the server buffer was killed,
6102 `erc-kill-channel-hook' if a channel buffer was killed,
6103 or `erc-kill-buffer-hook' if any other buffer."
6104 (when (eq major-mode 'erc-mode)
6105 (erc-remove-channel-users)
6106 (cond
6107 ((eq (erc-server-buffer) (current-buffer))
6108 (run-hooks 'erc-kill-server-hook))
6109 ((erc-channel-p (erc-default-target))
6110 (run-hooks 'erc-kill-channel-hook))
6111 (t
6112 (run-hooks 'erc-kill-buffer-hook)))))
6113
6114 (defun erc-kill-server ()
6115 "Sends a QUIT command to the server when the server buffer is killed.
6116 This function should be on `erc-kill-server-hook'."
6117 (when (erc-server-process-alive)
6118 (setq erc-server-quitting t)
6119 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6120
6121 (defun erc-kill-channel ()
6122 "Sends a PART command to the server when the channel buffer is killed.
6123 This function should be on `erc-kill-channel-hook'."
6124 (when (erc-server-process-alive)
6125 (let ((tgt (erc-default-target)))
6126 (erc-server-send (format "PART %s :%s" tgt
6127 (funcall erc-part-reason nil))
6128 nil tgt))))
6129
6130 (provide 'erc)
6131
6132 ;;; Deprecated. We might eventually stop requiring the goodies automatically.
6133 ;;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6134 ;;; avoid a recursive require error when byte-compiling the entire package.
6135 (require 'erc-goodies)
6136
6137 ;;; erc.el ends here
6138 ;;
6139 ;; Local Variables:
6140 ;; outline-regexp: ";;+"
6141 ;; indent-tabs-mode: t
6142 ;; tab-width: 8
6143 ;; End:
6144
6145 ;; arch-tag: d19587f6-627e-48c1-8d86-58595fa3eca3