1 ;; erc.el --- An Emacs Internet Relay Chat client
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
5 ;; Copyright (C) 2004 Brian Palmer
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
17 ;; This file is part of GNU Emacs.
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)
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.
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.
36 ;; ERC is an IRC client for Emacs.
38 ;; For more information, see the following URLs:
39 ;; * http://sf.net/projects/erc/
40 ;; * http://www.emacswiki.org/cgi-bin/wiki.pl?EmacsIRCClient
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.
48 ;; Put erc.el in your load-path, and put (require 'erc) in your .emacs.
52 ;; Use M-x customize-group RET erc RET to get an overview
53 ;; of all the variables you can tweak.
57 ;; To connect to an IRC server, do
61 ;; After you are connected to a server, you can use C-h m or have a look at
69 (defconst erc-version-string "Version 5.1 (Emacs 22)"
70 "ERC version. This is used by function `erc-version'.")
72 (eval-when-compile (require 'cl))
79 (defvar erc-official-location
80 "http://erc.sf.net (comments mailto://mlang@delysid.org)"
81 "Location of the ERC client on the Internet.")
84 "Emacs Internet Relay Chat client."
85 :link '(url-link "http://www.emacswiki.org/cgi-bin/wiki.pl?EmacsIRCClient")
89 (defgroup erc-buffers nil
90 "Creating new ERC buffers"
93 (defgroup erc-display nil
94 "Settings for how various things are displayed"
97 (defgroup erc-mode-line-and-header nil
98 "Displaying information in the mode-line and header"
101 (defgroup erc-ignore nil
102 "Ignoring certain messages"
105 (defgroup erc-query nil
106 "Using separate buffers for private discussions"
109 (defgroup erc-quit-and-part nil
110 "Quitting and parting channels"
113 (defgroup erc-paranoia nil
114 "Know what is sent and received; control the display of sensitive data."
117 (defgroup erc-scripts nil
118 "Running scripts at startup and with /LOAD"
121 (require 'erc-backend)
123 ;; compatibility with older ERC releases
125 (if (fboundp 'defvaralias)
127 (defvaralias 'erc-announced-server-name 'erc-server-announced-name)
128 (erc-make-obsolete-variable 'erc-announced-server-name
129 'erc-server-announced-name
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
137 (message (concat "ERC: The function `defvaralias' is not bound. See the "
138 "NEWS file for variable name changes since ERC 5.0.4.")))
140 (defalias 'erc-send-command 'erc-server-send)
141 (erc-make-obsolete 'erc-send-command 'erc-server-send "ERC 5.1")
143 ;; tunable connection and authentication parameters
145 (defcustom erc-server nil
147 See function `erc-compute-server' for more details on connection
148 parameters and authentication."
150 :type '(choice (const nil) string))
152 (defcustom erc-port nil
155 :type '(choice (const nil) number string))
157 (defcustom erc-nick nil
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.
164 See function `erc-compute-nick' for more details on connection
165 parameters and authentication."
167 :type '(choice (const nil)
168 (string :tag "Nickname")
171 (defcustom erc-nick-uniquifier "`"
172 "The character to append to the nick if it is already in use."
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
183 (defcustom erc-user-full-name nil
186 See function `erc-compute-full-name' for more details on connection
187 parameters and authentication."
189 :type '(choice (const nil) string function)
190 :set (lambda (sym val)
192 (set sym (funcall val))
195 (defvar erc-password nil
196 "ERC password to use in authentication (not necessary).")
198 (defcustom erc-user-mode nil
199 "Initial user modes to be set after a connection is established."
201 :type '(choice (const nil) string function))
204 (defcustom erc-prompt-for-password t
205 "Asks before using the default password, or whether to enter a new one."
209 (defcustom erc-warn-about-blank-lines t
210 "Warn the user if they attempt to send a blank line."
214 (defcustom erc-send-whitespace-lines nil
215 "If set to non-nil, send lines consisting of only whitespace."
219 (defcustom erc-hide-prompt nil
220 "If non-nil, do not display the prompt for commands.
222 \(A command is any input starting with a '/').
224 See also the variables `erc-prompt' and `erc-command-indicator'."
230 (defcustom erc-show-my-nick t
231 "If non-nil, display one's own nickname when sending a message.
233 If non-nil, \"<nickname>\" will be shown.
234 If nil, only \"> \" will be shown."
238 (define-widget 'erc-message-type 'set
239 "A set of standard IRC Message types."
240 :args '((const "JOIN")
246 (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))
248 (defcustom erc-hide-list nil
249 "*List of IRC type messages to hide.
250 A typical value would be '(\"JOIN\" \"PART\" \"QUIT\")."
252 :type 'erc-message-type)
254 (defvar erc-session-password nil
255 "The password used for the current session.")
256 (make-variable-buffer-local 'erc-session-password)
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."
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."
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
278 See also `erc-server-JOIN-functions', `erc-part-hook'."
282 (defcustom erc-quit-hook nil
283 "Hook run when processing a quit command directed at our nick.
285 The hook receives one argument, the current PROCESS.
286 See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
290 (defcustom erc-part-hook nil
291 "Hook run when processing a PART message directed at our nick.
293 The hook receives one argument, the current BUFFER.
294 See also `erc-server-QUIT-functions', `erc-quit-hook' and
295 `erc-disconnected-hook'."
299 (defcustom erc-kick-hook nil
300 "Hook run when processing a KICK message directed at our nick.
302 The hook receives one argument, the current BUFFER.
303 See also `erc-server-PART-functions' and `erc-part-hook'."
307 (defcustom erc-nick-changed-functions nil
308 "List of functions run when your nick was successfully changed.
310 Each function should accept two arguments, NEW-NICK and OLD-NICK."
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."
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)
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)
334 (defun erc-downcase (string)
335 "Convert STRING to IRC standard conforming downcase."
336 (let ((s (downcase string))
342 (while (string-match "[]\\[~]" s)
343 (aset s (match-beginning 0)
344 (cdr (assq (aref s (match-beginning 0)) c)))))
347 (defstruct (erc-server-user (:type vector) :named)
349 nickname host login full-name info
352 ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
353 ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
357 (defstruct (erc-channel-user (:type vector) :named)
359 ;; Last message time (in the form of the return value of
362 ;; This is useful for ordered name completion.
363 (last-message-time nil))
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))
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)))
376 (defsubst erc-add-server-user (nick user)
377 "This function is for internal use only.
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)))
383 (defsubst erc-remove-server-user (nick)
384 "This function is for internal use only.
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.
390 See also: `erc-remove-user'."
391 (with-current-buffer (process-buffer erc-server-process)
392 (remhash (erc-downcase nick) erc-server-users)))
394 (defun erc-change-user-nickname (user new-nick)
395 "This function is for internal use only.
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)))))))
413 (defun erc-remove-channel-user (nick)
414 "This function is for internal use only.
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.
421 See also: `erc-remove-server-user' and `erc-remove-user'."
422 (let ((channel-data (erc-get-channel-user nick)))
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))))))
432 (defun erc-remove-user (nick)
433 "This function is for internal use only.
435 Removes the user with nickname NICK from the `erc-server-users'
436 list as well as from all `erc-channel-users' lists.
438 See also: `erc-remove-server-user' and
439 `erc-remove-channel-user'."
440 (let ((user (erc-get-server-user nick)))
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))))
450 (defun erc-remove-channel-users ()
451 "This function is for internal use only.
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))
461 (clrhash erc-channel-users)))
463 (defsubst erc-channel-user-op-p (nick)
464 "Return `t' if NICK is an operator in the current channel."
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))))))
471 (defsubst erc-channel-user-voice-p (nick)
472 "Return `t' if NICK has voice in the current channel."
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))))))
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.
485 See also: `erc-sort-channel-users-by-activity'"
487 (if (hash-table-p erc-channel-users)
488 (maphash (lambda (nick cdata)
489 (setq users (cons cdata users)))
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)
498 (when (hash-table-p erc-server-users)
499 (maphash (lambda (n user)
501 (cons (erc-server-user-nickname user)
506 (defun erc-get-channel-nickname-list ()
507 "Returns a list of known nicknames on the current channel."
509 (when (hash-table-p erc-channel-users)
510 (maphash (lambda (n cdata)
512 (cons (erc-server-user-nickname (car cdata))
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)
522 (when (hash-table-p erc-server-users)
523 (maphash (lambda (n user)
525 (cons (cons (erc-server-user-nickname user) nil)
530 (defun erc-get-channel-nickname-alist ()
531 "Returns an alist of known nicknames on the current channel."
533 (when (hash-table-p erc-channel-users)
534 (maphash (lambda (n cdata)
536 (cons (cons (erc-server-user-nickname (car cdata)) nil)
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).
545 See also: `erc-get-channel-user-list'."
550 (let ((tx (erc-channel-user-last-message-time (cdr x)))
551 (ty (erc-channel-user-last-message-time (cdr y))))
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).
562 See also: `erc-get-channel-user-list'."
567 (let ((nickx (downcase (erc-server-user-nickname (car x))))
568 (nicky (downcase (erc-server-user-nickname (car y)))))
571 (string-lessp nickx nicky)
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)
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)
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)
589 (defvar erc-input-marker nil
590 "The marker where input should be inserted.")
591 (make-variable-buffer-local 'erc-input-marker)
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)
599 (defcustom erc-prompt "ERC>"
600 "Prompt used by ERC. Trailing whitespace is not required."
602 :type '(choice string function))
605 "Return the input prompt as a string.
607 See also the variable `erc-prompt'."
608 (let ((prompt (if (functionp erc-prompt)
611 (if (> (length prompt) 0)
615 (defcustom erc-command-indicator nil
616 "Indicator used by ERC for showing commands.
618 If non-nil, this will be used in the ERC buffer to indicate
619 commands (i.e., input starting with a '/').
621 If nil, the prompt will be constructed from the variable `erc-prompt'."
623 :type '(choice (const nil) string function))
625 (defun erc-command-indicator ()
626 "Return the command indicator prompt as a string.
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)
637 (defcustom erc-notice-prefix "*** "
638 "*Prefix for all notices."
642 (defcustom erc-notice-highlight-type 'all
643 "*Determines how to highlight notices.
644 See `erc-notice-prefix'.
646 The following values are allowed:
648 'prefix - highlight notice prefix only
649 'all - highlight the entire notice
651 Any other value disables notice's highlighting altogether."
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)))
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'.
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'"
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))
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'.
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'"
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))
718 ;; other tunable parameters
720 (defcustom erc-whowas-on-nosuchnick nil
721 "*If non-nil, do a whowas on a nick if no such nick."
725 (defcustom erc-verbose-server-ping nil
726 "*If non-nil, show every time you get a PING or PONG from the server."
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>)
735 Many consider it impolite to do so automatically."
739 (defcustom erc-away-nickname nil
740 "*The nickname to take when you are marked as being away."
742 :type '(choice (const nil)
745 (defcustom erc-paranoid nil
746 "If non-nil, then all incoming CTCP requests will be shown."
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."
758 (defcustom erc-anonymous-login t
759 "Be paranoid, don't give away your machine name."
763 (defcustom erc-prompt-for-channel-key nil
764 "Prompt for channel key when using `erc-join-channel' interactively"
768 (defcustom erc-email-userid "user"
769 "Use this as your email user ID."
773 (defcustom erc-ignore-list nil
774 "*List of regexps matching user identifiers to ignore.
776 A user identifier has the form \"nick!login@host\". If an
777 identifier matches, the message from the person will not be
780 :type '(repeat regexp))
781 (make-variable-buffer-local 'erc-ignore-list)
783 (defcustom erc-ignore-reply-list nil
784 "*List of regexps matching user identifiers to ignore completely.
786 This differs from `erc-ignore-list' in that it also ignores any
787 messages directed at the user.
789 A user identifier has the form \"nick!login@host\".
791 If an identifier matches, or a message is addressed to a nick
792 whose identifier matches, the message will not be processed.
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 *."
799 :type '(repeat regexp))
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
807 See `erc-server-flood-margin' for other flood-related parameters.")
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.
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"
820 :type '(repeat file))
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."
827 :type '(repeat directory))
829 (defcustom erc-script-echo t
830 "*If not-NIL, echo the IRC script commands locally."
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)
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
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
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
856 (defcustom erc-quit-reason-various-alist nil
857 "Alist of possible arguments to the /quit command.
859 Each element has the form:
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.
866 If no elements match, then the empty string is used.
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)))))
880 (defcustom erc-part-reason-various-alist nil
881 "Alist of possible arguments to the /part command.
883 Each element has the form:
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.
890 If no elements match, then the empty string is used.
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)))))
904 (defcustom erc-quit-reason 'erc-quit-reason-normal
905 "*A function which returns the reason for quitting.
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)
915 (defcustom erc-part-reason 'erc-part-reason-normal
916 "A function which returns the reason for parting a channel.
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)
926 (defvar erc-grab-buffer-name "*erc-grab*"
927 "The name of the buffer created by `erc-grab-region'.")
929 ;; variables available for IRC scripts
931 (defvar erc-user-information "ERC User"
932 "USER_INFORMATION IRC variable.")
936 (defgroup erc-hooks nil
937 "Hook variables for fancy customizations of ERC."
940 (defcustom erc-mode-hook nil
941 "Hook run after `erc-mode' setup is finished."
944 :options '(erc-add-scroll-to-bottom))
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."
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."
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.
966 To change the text that will be sent, set the variable STR which is
967 used in `erc-send-current-line'.
969 To change the text inserted into the buffer without changing the text
970 that will be sent, use `erc-send-modify-hook' instead.
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.
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
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.")
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.")
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
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."
1007 :options '(erc-truncate-buffer
1009 erc-save-buffer-in-logs))
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.
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
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
1031 This function is called with narrowing, ala `erc-send-modify-hook'"
1034 :options '(erc-make-read-only))
1036 (defcustom erc-send-completed-hook
1037 (when (featurep 'emacspeak)
1040 (emacspeak-auditory-icon 'select-object)))))
1041 "Hook called after a message has been parsed by ERC.
1043 The single argument to the functions is the unmodified string
1044 which the local user typed."
1047 ;; mode-specific tables
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)
1058 "Syntax table used while in ERC mode.")
1060 (defvar erc-mode-abbrev-table nil
1061 "Abbrev table used while in ERC mode.")
1062 (define-abbrev-table 'erc-mode-abbrev-table ())
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)
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))
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.
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.
1107 (defgroup erc-faces nil
1111 (defface erc-default-face '((t))
1115 (defface erc-direct-msg-face '((t (:foreground "IndianRed")))
1116 "ERC face used for messages you receive in the main erc buffer."
1119 (defface erc-input-face '((t (:foreground "brown")))
1120 "ERC face used for your input."
1123 (defface erc-prompt-face
1124 '((t (:bold t :foreground "Black" :background"lightBlue2")))
1125 "ERC face for the prompt."
1128 (defface erc-command-indicator-face
1130 "ERC face for the command indicator.
1131 See the variable `erc-command-indicator'."
1134 (defface erc-notice-face '((t (:bold t :foreground "SlateBlue")))
1135 "ERC face for notices."
1138 (defface erc-action-face '((t (:bold t)))
1139 "ERC face for actions generated by /ME."
1142 (defface erc-error-face '((t (:foreground "red")))
1143 "ERC face for errors."
1146 (defface erc-nick-default-face '((t (:bold t)))
1147 "ERC nickname default face."
1150 (defface erc-nick-msg-face '((t (:bold t :foreground "IndianRed")))
1151 "ERC nickname face for private messages."
1154 ;; Debugging support
1156 (defvar erc-log-p nil
1157 "When set to t, generate debug messages in a separate debug buffer.")
1159 (defvar erc-debug-log-file (expand-file-name "ERC.debug")
1160 "Debug log file name.")
1162 (defvar erc-dbuf nil)
1163 (make-variable-buffer-local 'erc-dbuf)
1165 (defmacro define-erc-module (name alias doc enable-body disable-body
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.
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.
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)))))
1195 (erc-define-minor-mode
1197 ,(format "Toggle ERC %S mode.
1198 With arg, turn ERC %S mode on if and only if arg is positive.
1201 :global ,(not local-p) :group (quote ,group)
1206 ,(format "Enable ERC %S mode."
1209 (add-to-list 'erc-modules (quote ,name))
1213 ,(format "Disable ERC %S mode."
1216 (setq erc-modules (delq (quote ,name) erc-modules))
1219 ,(when (and alias (not (eq name alias)))
1223 (format "erc-%s-mode"
1224 (downcase (symbol-name alias)))))
1228 (put 'define-erc-module 'doc-string-elt 3)
1230 (defun erc-once-with-server-event (event &rest forms)
1231 "Execute FORMS the next time EVENT occurs in the `current-buffer'.
1233 You should make sure that `current-buffer' is a server buffer.
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
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
1246 (unless (erc-server-buffer-p)
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))
1257 (add-hook hook fun nil t)
1260 (defun erc-once-with-server-event-global (event &rest forms)
1261 "Execute FORMS the next time EVENT occurs in any server buffer.
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
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)
1278 (add-hook hook fun nil nil)
1281 (defmacro erc-log (string)
1282 "Logs STRING if logging is on (see `erc-log-p')."
1284 (erc-log-aux ,string)))
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)))
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))))
1299 (defun erc-server-buffer-p (&optional buffer)
1300 "Return non-nil if argument BUFFER is an ERC server buffer.
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)))))
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)
1314 (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
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
1323 `(let ((ison (split-string (aref parsed 3))))
1324 (setq erc-online-p (car (erc-member-ignore-case ,nick ison)))
1326 (erc-server-send (format "ISON %s" nick))
1327 (while (eq erc-online-p 'unknown) (accept-process-output))
1329 (message "%s is %sonline"
1330 (or erc-online-p nick)
1331 (if erc-online-p "" "not "))
1334 (defun erc-log-aux (string)
1335 "Do the debug logging of STRING."
1336 (let ((cb (current-buffer))
1339 (session-buffer (erc-server-buffer)))
1342 (set-buffer session-buffer)
1343 (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
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))
1356 (message "ERC: ** %s" string))))
1358 ;; Last active buffer, to print server messages in the right place
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
1364 (make-variable-buffer-local 'erc-active-buffer)
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))
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))))
1378 ;; Mode activation routines
1381 "Major mode for Emacs IRC.
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))
1405 (defconst erc-default-server "irc.freenode.net"
1406 "IRC server to use if it cannot be detected otherwise.")
1408 (defconst erc-default-port "ircd"
1409 "IRC port to use if it cannot be detected otherwise.")
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"
1419 :type '(choice (const window)
1420 (const window-noselect)
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'."
1429 :type '(repeat (cons :format "%v"
1430 (symbol :tag "Parameter")
1431 (sexp :tag "Value"))))
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'."
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))))
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."
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:
1464 * ircd-dalnet -> 7000"
1467 (erc-normalize-port (symbol-name port)))
1469 (let ((port-nr (string-to-number port)))
1473 ((string-equal port "irc")
1475 ((string-equal port "ircs")
1477 ((string-equal port "ircd")
1479 ((string-equal port "ircd-dalnet")
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)))
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)
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)
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))))
1514 (generate-new-buffer-name buf-name))))
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)))
1521 (defun erc-member-ignore-case (string list)
1522 "Return non-nil if STRING is a member of LIST.
1524 All strings are compared according to IRC protocol case rules, see
1526 (setq string (erc-downcase string))
1529 (if (string= string (erc-downcase (car list)))
1530 (throw 'result list) (setq list (cdr list))))))
1532 (defmacro erc-with-buffer (spec &rest body)
1533 "Execute BODY in the buffer associated with SPEC.
1535 SPEC should have the form
1539 If TARGET is a buffer, use it. Otherwise, use the buffer
1540 matching TARGET in the process specified by PROCESS.
1542 If PROCESS is nil, use the current `erc-server-process'
1543 See `erc-get-buffer' for details.
1545 See also `with-current-buffer'.
1547 \(fn (TARGET [PROCESS]) BODY...)"
1548 (let ((buf (erc-gensym))
1550 (target (erc-gensym))
1551 (process (erc-gensym)))
1552 `(let* ((,target ,(car spec))
1553 (,process ,(cadr spec))
1554 (,buf (if (bufferp ,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))))))
1562 (with-current-buffer ,buf
1564 (put 'erc-with-buffer 'lisp-indent-function 1)
1565 (put 'erc-with-buffer 'edebug-form-spec '((form &optional form) body))
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)))
1574 (let ((current (erc-default-target)))
1575 (and (stringp current)
1576 (string-equal downcased-target (erc-downcase current))
1577 (throw 'buffer (current-buffer)))))
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.
1585 PROC is either an `erc-server-process', identifying a certain
1586 server connection, or nil which means all open connections."
1590 (mapcar (lambda (buf)
1591 (with-current-buffer buf
1592 (and (eq major-mode 'erc-mode)
1594 (eq proc erc-server-process))
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."
1606 (setq predicate (lambda () t)))
1607 (erc-buffer-filter predicate proc))
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
1614 ;; Make the evaluation have the correct order
1615 (let ((pre (erc-gensym))
1617 `(let ((,pro ,process)
1619 (mapcar (lambda (buffer)
1620 (with-current-buffer buffer
1622 (erc-buffer-list ,pre
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))
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.
1634 Due to some yet unresolved reason, global function `iswitchb-mode'
1635 needs to be active for this function to work."
1638 (require 'iswitchb))
1639 (let ((iswitchb-make-buflist-hook
1641 (setq iswitchb-temp-buflist
1642 (mapcar 'buffer-name
1645 (when (and arg (boundp 'erc-server-process))
1646 erc-server-process)))))))
1648 (iswitchb-read-buffer
1650 (if (boundp 'erc-modified-channels-alist)
1651 (buffer-name (caar (last erc-modified-channels-alist)))
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."
1661 (and (erc-default-target)
1662 (erc-channel-p (erc-default-target))))
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)))
1670 (erc-server-user-buffers user)
1673 ;; Some local variables
1675 (defvar erc-default-recipients nil
1676 "List of default recipients of the current buffer.")
1677 (make-variable-buffer-local 'erc-default-recipients)
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)
1683 (defvar erc-channel-user-limit nil
1684 "Limit of users per channel.")
1685 (make-variable-buffer-local 'erc-channel-user-limit)
1687 (defvar erc-channel-key nil
1688 "Key needed to join channel.")
1689 (make-variable-buffer-local 'erc-channel-key)
1691 (defvar erc-invitation nil
1692 "Last invitation channel.")
1693 (make-variable-buffer-local 'erc-invitation)
1695 (defvar erc-away nil
1696 "Non-nil indicates that we are away.")
1697 (make-variable-buffer-local 'erc-away)
1699 (defvar erc-channel-list nil
1700 "Server channel list.")
1701 (make-variable-buffer-local 'erc-channel-list)
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)
1707 (defvar erc-logged-in nil
1708 "Non-nil indicates that we are logged in.")
1709 (make-variable-buffer-local 'erc-logged-in)
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)
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)
1719 (defcustom erc-modules '(netsplit fill button match track pcomplete readonly
1720 ring autojoin noncommands irccontrols
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)
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"
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"
1753 "Notify when the online status of certain users changes"
1755 (const :tag "Complete nicknames and commands (programmable)"
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"
1764 (const :tag "Convert smileys to pretty icons" smiley)
1765 (const :tag "Play sounds when you receive CTCP SOUND requests"
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))
1775 (defun erc-update-modules ()
1776 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1778 (dolist (mod erc-modules)
1779 (setq req (concat "erc-" (symbol-name mod)))
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)))
1789 (require (intern req))
1791 (funcall (or (intern-soft (concat "erc-" (symbol-name mod) "-mode"))
1792 (error "`%s' is not a known ERC module" mod))
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)
1805 ((eq erc-join-buffer 'frame)
1806 (funcall '(lambda (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)))
1815 (if (active-minibuffer-window)
1816 (display-buffer buffer)
1817 (switch-to-buffer buffer)))))
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.
1823 Connect to SERVER on PORT as NICK with FULL-NAME.
1825 If CONNECT is non-nil, connect to the server. Otherwise assume
1826 already connected and just create a separate buffer for the new
1829 Use PASSWD as user password on the server. If TGT-LIST is
1830 non-nil, use it to initialise `erc-default-recipients'.
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))
1840 (erc-update-modules)
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))
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)
1858 (unless continued-session
1859 (goto-char (point-max))
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
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)
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)))
1898 (setq erc-session-password passwd)
1899 ;; debug output buffer
1902 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
1903 (erc-determine-parameters server port nick full-name)
1905 ;; Saving log file on exit
1906 (run-hooks 'erc-connect-pre-hook)
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))
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)))
1921 ;; Now display the buffer in a window as per user wishes.
1922 (unless (eq buffer old-buffer)
1924 ;; we can't log to debug buffer, it may not exist yet
1925 (message "erc: old buffer %s, switching to %s"
1927 (erc-setup-buffer buffer))
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))))
1937 ;; interactive startup
1939 (defvar erc-server-history-list nil
1940 "IRC server interactive selection history list.")
1942 (defvar erc-nick-history-list nil
1943 "Nickname interactive selection history list.")
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."
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)))))
1957 (if (not (fboundp 'read-passwd))
1958 (defun read-passwd (prompt)
1959 "Substitute for read-passwd in early emacsen"
1960 (read-from-minibuffer prompt)))
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."
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."
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
1983 (erc-compute-server) nil nil 'erc-server-history-list))
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))
1989 (erc-string-to-port (read-from-minibuffer
1990 "IRC port: " (erc-port-to-string
1991 (erc-compute-port))))))
1993 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
1994 (setq nick (match-string 1 user-input)
1995 user-input (match-string 2 user-input))
1997 (if (erc-already-logged-in server port nick)
1998 (read-from-minibuffer
1999 (erc-format-message 'nick-in-use ?n 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))))
2006 (setq server user-input)
2008 (setq passwd (if erc-prompt-for-password
2009 (if (and erc-password
2010 (y-or-n-p "Use the default password? "))
2012 (read-passwd "Password: "))
2014 (when (and passwd (string= "" passwd))
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)
2026 nil nil 'erc-nick-history-list)))
2027 (list :server server :port port :nick nick :password passwd)))
2030 (defun* erc-select (&key (server (erc-compute-server))
2031 (port (erc-compute-port))
2032 (nick (erc-compute-nick))
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))
2041 (full-name (erc-compute-full-name)))
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))
2050 (run-hook-with-args 'erc-before-connect server port nick)
2051 (erc server port nick erc-user-full-name t password))
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)))
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
2075 ;;; Debugging the protocol
2077 (defvar erc-debug-irc-protocol nil
2078 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2080 The buffer is created if it doesn't exist.
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.
2086 WARNING: Do not set this variable directly! Instead, use the
2087 function `erc-toggle-debug-irc-protocol' to toggle its value.")
2089 (defun erc-log-irc-protocol (string &optional outbound)
2090 "Append STRING to the buffer *erc-protocol*.
2092 This only has any effect if `erc-debug-irc-protocol' is non-nil.
2094 The buffer is created if it doesn't exist.
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))
2101 (with-current-buffer (get-buffer-create "*erc-protocol*")
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
2111 (dolist (line lines)
2112 (setq result (concat result network-name
2116 (concat network-name " >> " string
2119 (1- (length string))))
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))
2129 (select-window orig-win)))))))
2131 (defun erc-toggle-debug-irc-protocol (&optional arg)
2132 "Toggle the value of `erc-debug-irc-protocol'.
2134 If ARG is non-nil, show the *erc-protocol* buffer."
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))
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))
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"))))
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 ""))
2172 ;; Display interface
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))
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."
2188 (set-buffer (or buffer (process-buffer erc-server-process)))
2189 (let ((insert-position (or (marker-position erc-insert-marker)
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
2209 (setq erc-insert-this t)
2210 (save-excursion ;; to restore point in the new buffer
2213 (goto-char insert-position)
2214 (insert-before-markers string)
2215 ;; run insertion hook, with point at restored location
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)
2222 insert-position))))))
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)
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)
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))))))
2251 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2252 "Regexp which matches all legal characters in a IRC nickname.")
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))
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.
2268 If STRING is nil, the function does nothing."
2269 (let ((inhibit-point-motion-hooks t)
2272 ((bufferp buffer) (list buffer))
2273 ((listp buffer) buffer)
2274 ((processp buffer) (list (process-buffer 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))))))
2292 (defun erc-display-message-highlight (type string)
2293 "Highlight STRING according to TYPE, where erc-TYPE-face is an erc face.
2295 See also `erc-make-notice'"
2296 (cond ((eq type 'notice)
2297 (erc-make-notice string))
2299 (erc-put-text-property
2301 'face (or (intern-soft
2302 (concat "erc-" (symbol-name type) "-face"))
2307 (defun erc-display-message (parsed type buffer msg &rest args)
2308 "Display MSG in BUFFER.
2310 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2312 See also `erc-format-message' and `erc-display-line'."
2313 (let ((string (if (symbolp msg)
2314 (apply 'erc-format-message msg args)
2319 (mapc (lambda (type)
2321 (erc-display-message-highlight type string)))
2325 (erc-display-message-highlight type string))))
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)))))
2334 (defun erc-message-type-member (position list)
2335 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2337 This function relies on the erc-parsed text-property being
2339 (let ((prop-val (get-text-property position 'erc-parsed)))
2340 (and prop-val (member (erc-response.command prop-val) list))))
2342 (defvar erc-send-input-line-function 'erc-send-input-line)
2343 (make-variable-buffer-local 'erc-send-input-line-function)
2345 (defun erc-send-input-line (target line &optional force)
2346 "Send LINE to TARGET.
2348 See also `erc-server-send'."
2349 (setq line (format "PRIVMSG %s :%s"
2351 ;; If the line is empty, we still want to
2352 ;; send it - i.e. an empty pasted line.
2353 (if (string= line "\n")
2356 (erc-server-send line force target))
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)
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))
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
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.
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
2388 (let* ((cmd (nth 0 command-list))
2389 (args (nth 1 command-list)))
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)
2400 (or (documentation cmd) "")))
2402 (let ((r (erc-default-target)))
2404 (funcall erc-send-input-line-function r line force)
2405 (erc-display-message nil 'error (current-buffer) 'no-target)
2408 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2409 ;; Input commands handlers
2410 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
2418 (erc-channel-p (erc-default-target)))
2419 (erc-send-message line)))
2420 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2422 (defun erc-cmd-SAY (line)
2423 "Send LINE to the current query or channel as a message, not a command.
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)
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)
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."
2438 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2439 (let ((var (read (concat "erc-" (match-string 1 line))))
2440 (val (read (match-string 2 line))))
2443 (set var (eval val))
2444 (erc-display-message
2445 nil nil 'active (format "Set %S to %S" var val))
2447 (setq var (read (match-string 1 line)))
2450 (set var (eval val))
2451 (erc-display-message
2452 nil nil 'active (format "Set %S to %S" var val))
2454 (erc-display-message nil 'error 'active 'variable-not-bound)
2456 ((string-match "^\\s-*$" line)
2458 (concat "Available user variables:\n"
2463 (let ((val (symbol-value var)))
2464 (concat (format "%S:" var)
2466 (concat "\n" (pp-to-string val))
2467 (format " %S\n" val)))))
2468 (apropos-internal "^erc-" 'user-variable-p))))
2469 (current-buffer)) t)
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)
2475 (defun erc-cmd-default (line)
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))
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'."
2491 (erc-make-notice (format "Now ignoring %s" user))
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)
2501 (with-current-buffer (erc-server-buffer) erc-ignore-list))))
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)
2510 (erc-make-notice (format "%s is not currently ignored!" user))
2513 (erc-make-notice (format "No longer ignoring %s" user))
2515 (with-current-buffer (erc-server-buffer)
2516 (setq erc-ignore-list (delete ignored-nick erc-ignore-list))))
2519 (defun erc-cmd-CLEAR ()
2520 "Clear the window content."
2524 (defun erc-cmd-OPS ()
2525 "Show the ops in the current channel."
2528 (if erc-channel-users
2529 (maphash (lambda (nick user-data)
2530 (let ((cuser (cdr user-data)))
2532 (erc-channel-user-op cuser))
2533 (setq ops (cons (erc-server-user-nickname
2537 (setq ops (sort ops 'string-lessp))
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)))
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))))
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))
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))
2564 (if (string= reason "")
2566 (concat "AWAY :" reason))))
2568 (put 'erc-cmd-AWAY 'do-not-parse-args t)
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)
2578 (defun erc-cmd-CTCP (nick cmd &rest args)
2579 "Send a Client To Client Protocol message to NICK.
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
2585 (concat " " (mapconcat #'identity args " "))))))
2586 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2587 (erc-send-ctcp-message nick str)
2590 (defun erc-cmd-HELP (&optional func)
2591 "Popup help information.
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
2599 To find out about erc and bbdb, do
2602 For help about the WHOIS command, do:
2605 For a list of user commands (/join /part, ...):
2608 (let* ((sym (or (let ((sym (intern-soft
2609 (concat "erc-cmd-" (upcase func)))))
2610 (if (and sym (or (boundp sym) (fboundp sym)))
2613 (let ((sym (intern-soft func)))
2614 (if (and sym (or (boundp sym) (fboundp sym)))
2617 (let ((sym (intern-soft (concat "erc-" func))))
2618 (if (and sym (or (boundp sym) (fboundp sym)))
2623 ((boundp sym) (describe-variable sym))
2624 ((fboundp sym) (describe-function sym))
2626 (apropos-command (concat "erc-.*" func) nil
2629 (get x 'custom-type))))
2631 (apropos "erc-cmd-")
2632 (message "Type C-h m to get additional information about keybindings.")
2635 (defalias 'erc-cmd-H 'erc-cmd-HELP)
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'."
2642 (if (string= (upcase channel) "-INVITE")
2644 (setq chnl erc-invitation)
2645 (erc-display-message nil 'error (current-buffer) 'no-invitation))
2646 (setq chnl (erc-ensure-channel-name channel)))
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
2656 (erc-log (format "cmd: JOIN: %s" chnl))
2658 (erc-server-send (format "JOIN %s %s" chnl key))
2659 (erc-server-send (format "JOIN %s" chnl)))))))
2662 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
2663 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
2665 (defvar erc-channel-new-member-names nil
2666 "If non-nil, a names list is currently being received.
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)
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
2677 (let ((tgt (or (and (erc-channel-p channel) channel)
2678 (erc-default-target))))
2679 (if (and tgt (erc-channel-p tgt))
2681 (erc-log (format "cmd: DEFAULT: NAMES %s" 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)))
2688 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
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)
2703 (let ((ch (erc-default-target)))
2704 (setq reasonstring (concat
2705 (if reason-or-nick (concat reason-or-nick " "))
2710 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
2712 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
2713 (erc-display-message nil 'error (current-buffer)
2714 'no-default-channel))
2717 (defvar erc-script-args nil)
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.
2725 The optional FORCE argument is ignored here - you can't force loading
2726 a script after exceeding the flood threshold."
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))
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))
2741 (message "Loading \'%s\'..." file)
2742 (erc-load-script file)
2743 (message "Loading \'%s\'...done" file))))
2747 (defun erc-cmd-WHOIS (user &optional server)
2748 "Display whois information for USER.
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)
2758 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
2760 (defun erc-cmd-WHOAMI ()
2761 "Display whois information about yourself."
2762 (erc-cmd-WHOIS (erc-current-nick))
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))
2770 (with-current-buffer serverbuf
2771 (add-to-list 'symlist
2772 (cons (erc-once-with-server-event
2775 (erc-response.command-args parsed))))
2776 'erc-server-311-functions))
2777 (add-to-list 'symlist
2778 (cons (erc-once-with-server-event
2781 (erc-response.command-args parsed))))
2782 'erc-server-312-functions))
2783 (add-to-list 'symlist
2784 (cons (erc-once-with-server-event
2787 (erc-response.command-args parsed))))
2788 'erc-server-318-functions))
2789 (add-to-list 'symlist
2790 (cons (erc-once-with-server-event
2793 (erc-response.command-args parsed))))
2794 'erc-server-319-functions))
2795 (add-to-list 'symlist
2796 (cons (erc-once-with-server-event
2799 (erc-response.command-args parsed))))
2800 'erc-server-320-functions))
2801 (add-to-list 'symlist
2802 (cons (erc-once-with-server-event
2805 (erc-response.command-args parsed))))
2806 'erc-server-330-functions))
2807 (add-to-list 'symlist
2808 (cons (erc-once-with-server-event
2813 (erc-response.command-args parsed)))))
2816 (format "%s has been idle for %s."
2817 (erc-string-no-properties ,nick)
2818 (erc-seconds-to-string idleseconds)))
2821 'erc-server-317-functions))
2823 ;; Send the WHOIS command.
2824 (erc-cmd-WHOIS nick)
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))))))))
2835 (defun erc-cmd-DESCRIBE (line)
2836 "Pose some action to a certain user.
2837 LINE has the format \"USER ACTION\"."
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))
2847 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
2849 (defun erc-cmd-ME (line)
2850 "Send LINE as an action."
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))
2858 (put 'erc-cmd-ME 'do-not-parse-args t)
2860 (defun erc-cmd-LASTLOG (line)
2861 "Show all lines in the current buffer matching the regexp LINE.
2863 If a match spreads across multiple lines, all those lines are shown.
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.
2869 If LINE contains upper case characters (excluding those preceded by `\'),
2870 the matching is case-sensitive."
2873 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
2875 (defun erc-send-message (line &optional force)
2876 "Send LINE to the current channel or user and display it.
2878 See also `erc-message' and `erc-display-line'."
2879 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
2881 (concat (erc-format-my-nick) line)
2883 ;; FIXME - treat multiline, run hooks, or remove me?
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
2891 If only one word is given, display the mode of that target.
2893 A list of valid mode strings for Freenode may be found at
2894 `http://freenode.net/using_the_network.shtml'."
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)))
2902 (put 'erc-cmd-MODE 'do-not-parse-args t)
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 " "))))
2910 (defun erc-cmd-MSG (line)
2911 "Send a message to the channel or user given as the first word in LINE.
2913 The rest of LINE is the message to send."
2914 (erc-message "PRIVMSG" line))
2916 (defalias 'erc-cmd-M 'erc-cmd-MSG)
2917 (put 'erc-cmd-MSG 'do-not-parse-args t)
2919 (defun erc-cmd-SQUERY (line)
2920 "Send a Service Query to the service given as the first word in LINE.
2922 The rest of LINE is the message to send."
2923 (erc-message "SQUERY" line))
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))
2936 (erc-set-current-nick nick)
2937 (erc-update-mode-line)
2938 (setq erc-bad-nick nil)))
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."
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))
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))
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))
2966 (erc-display-message nil 'error (current-buffer) 'no-target)))
2969 (put 'erc-cmd-PART 'do-not-parse-args t)
2971 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
2973 (defun erc-cmd-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)))
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!"
2984 ((string-match "^\\s-\\(.+\\)$" line)
2985 (erc-server-send (match-string 1 line)))
2987 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
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 ;-)"
2995 (list (read-from-minibuffer "Start a query with: " nil)))
2996 (let ((session-buffer (erc-server-buffer)))
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)
3004 (defun erc-quit-reason-normal (&optional s)
3005 "Normal quit message.
3007 If S is non-nil, it will be used as the quit reason."
3009 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3010 erc-version-string) ; erc-official-location)
3013 (defun erc-quit-reason-zippy (&optional s)
3014 "Zippy quit message.
3016 If S is non-nil, it will be used as the quit reason."
3018 (erc-replace-regexp-in-string "\n" "" (yow))))
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))))
3026 ((functionp res) (funcall res))
3028 ;; hopefully never reached
3031 (defun erc-part-reason-normal (&optional s)
3032 "Normal part message.
3034 If S is non-nil, it will be used as the quit reason."
3036 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3037 erc-version-string) ; erc-official-location)
3040 (defun erc-part-reason-zippy (&optional s)
3041 "Zippy part message.
3043 If S is non-nil, it will be used as the quit reason."
3045 (erc-replace-regexp-in-string "\n" "" (yow))))
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))))
3053 ((functionp res) (funcall res))
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."
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
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)))
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)
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)))
3094 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3095 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3097 (defun erc-cmd-SERVER (server)
3098 "Connect to SERVER, leaving existing connection intact."
3099 (erc-log (format "cmd: SERVER: %s" server))
3101 (erc-select :server server :nick (erc-current-nick))
3103 (message "Cannot find host %s." server)
3108 (defvar motif-version-string)
3109 (defvar gtk-version-string))
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)!"
3115 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3117 system-configuration
3119 (cond ((featurep 'motif)
3120 (concat ", " (substring
3121 motif-version-string 4)))
3123 (concat ", GTK+ Version "
3124 gtk-version-string))
3125 ((featurep 'mac-carbon) ", Mac Carbon")
3126 ((featurep 'x-toolkit) ", X toolkit")
3128 (if (and (boundp 'x-toolkit-scroll-bars)
3129 (memq x-toolkit-scroll-bars
3131 (format ", %s scroll bars"
3132 (capitalize (symbol-name
3133 x-toolkit-scroll-bars)))
3135 (if (featurep 'multi-tty) ", multi-tty" ""))
3136 (concat ", built " erc-emacs-build-time)))
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!"
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)))
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)
3156 (make-string (length people) ?o)
3158 (mapconcat 'identity people " ")))
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)
3166 (make-string (length people) ?o)
3168 (mapconcat 'identity people " ")))
3171 (defun erc-cmd-TIME (&optional line)
3172 "Request the current time and date from the current server."
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)))
3179 (t (erc-server-send "TIME"))))
3180 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
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.
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
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))
3199 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3200 (let ((ch (match-string 1 topic)))
3201 (erc-server-send (format "TOPIC %s" ch) nil ch)
3204 ((string-match "^\\s-*$" topic)
3205 (let ((ch (erc-default-target)))
3206 (erc-server-send (format "TOPIC %s" ch) nil ch)
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))
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)))
3219 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3220 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
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)
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))))
3240 (erc-server-send (format "TOPIC %s :" chnl))
3245 (defvar erc-channel-banlist nil
3246 "A list of bans seen for the current channel.
3248 Each ban is an alist of the form:
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)
3256 (defun erc-cmd-BANLIST ()
3257 "Pretty-print the contents of `erc-channel-banlist'.
3259 The ban list is fetched from the server if necessary."
3260 (let ((chnl (erc-default-target))
3261 (chnl-name (buffer-name)))
3264 ((not (erc-channel-p chnl))
3265 (erc-display-line (erc-make-notice "You're not on a channel\n")
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
3276 `(with-current-buffer ,chnl-name
3277 (put 'erc-channel-banlist 'received-from-server t)
3278 (setq erc-server-367-functions ',old-367-hook)
3281 (erc-server-send (format "MODE %s b" chnl)))))
3283 ((null erc-channel-banlist)
3284 (erc-display-line (erc-make-notice
3285 (format "No bans for channel: %s\n" chnl))
3287 (put 'erc-channel-banlist 'received-from-server nil))
3290 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3292 (and (boundp 'fill-column)
3294 (1- (window-width))))
3295 (separator (make-string erc-fill-column (string-to-char "=")))
3297 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3298 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3301 (erc-make-notice (format "Ban list for channel: %s\n"
3302 (erc-default-target)))
3305 (erc-display-line separator 'active)
3306 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3307 (erc-display-line separator 'active)
3313 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3315 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3318 erc-channel-banlist)
3320 (erc-display-line (erc-make-notice "End of Ban list")
3322 (put 'erc-channel-banlist 'received-from-server nil)))))
3325 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3327 (defun erc-cmd-MASSUNBAN ()
3330 Unban all currently banned users in the current channel."
3331 (let ((chnl (erc-default-target)))
3334 ((not (erc-channel-p chnl))
3336 (erc-make-notice "You're not on a channel\n")
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
3346 `(with-current-buffer ,chnl
3347 (put 'erc-channel-banlist 'received-from-server t)
3348 (setq erc-server-367-functions ,old-367-hook)
3351 (erc-server-send (format "MODE %s b" chnl)))))
3353 (t (let ((bans (mapcar 'cdr erc-channel-banlist)))
3355 ;; Glob the bans into groups of three, and carry out the unban.
3356 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
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))))
3366 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3368 ;;;; End of IRC commands
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)
3375 (concat "#" channel)))
3377 (defun erc-grab-region (start end)
3378 "Copy the region between START and END in a recreatable format.
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
3385 (erc-set-active-buffer (current-buffer))
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)))
3392 (dolist (line lines)
3393 (insert (concat line "\n")))
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.")
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.
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)))
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))
3416 ;; now save excursion again to store where point and mark are
3417 ;; in the current buffer
3419 (setq pos (or pos (point)))
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
3431 (erc-put-text-property 0 (1- (length prompt))
3432 'face (or face 'erc-prompt-face)
3435 ;; Set the input marker
3436 (set-marker erc-input-marker (point)))
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
3441 (when (or (not pos) (<= (point) pos))
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)
3448 ;; interactive operations
3450 (defun erc-input-message ()
3451 "Read input from the minibuffer."
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)))
3459 (defvar erc-action-history-list ()
3460 "History list for interactive action input.")
3462 (defun erc-input-action ()
3463 "Interactively input a user action and send it to IRC."
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))))
3471 (defun erc-join-channel (channel &optional key)
3474 If `point' is at the beginning of a channel name, use that as default."
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))
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)))
3486 (defun erc-part-from-channel (reason)
3487 "Part from the current channel and prompt for a REASON."
3490 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3492 (read-from-minibuffer (concat "Leave " (erc-default-target)
3494 (cons "No reason" 0)))))
3495 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3497 (defun erc-set-topic (topic)
3498 "Prompt for a TOPIC for the current channel."
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")))
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)))))
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)))))
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)))))
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))
3540 ;; Movement of point
3543 "Move `point' to the beginning of the current line.
3545 This places `point' just after the prompt, or at the beginning of the line."
3548 (when (get-text-property (point) 'erc-prompt)
3549 (goto-char erc-input-marker))
3552 (defun erc-kill-input ()
3553 "Kill current input line using `erc-bol' followed by `kill-line'."
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))
3562 (defun erc-complete-word ()
3563 "Complete the word before point.
3565 This function uses `erc-complete-functions'."
3567 (unless (run-hook-with-args-until-success 'erc-complete-functions)
3570 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3572 ;; IRC SERVER INPUT HANDLING
3574 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3576 ;;;; New Input parsing
3578 ; Stolen from ZenIRC. I just wanna test this code, so here is
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
3587 (defun erc-default-server-handler (proc parsed)
3588 "Default server handler.
3590 Displays PROC and PARSED appropriately using `erc-display-message'."
3591 (erc-display-message
3598 (setq res (append res (list x)))))
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)
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))
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."
3621 (buffer-live-p server)
3622 (set-buffer server))
3623 (error "Couldn't switch to server buffer"))
3624 (let ((buf (erc erc-session-server
3627 erc-session-user-full-name
3632 erc-server-process)))
3634 (error "Couldn't open query window"))
3635 (erc-update-mode-line)
3638 (defcustom erc-auto-query nil
3639 "If non-nil, create a query buffer each time you receive a private message.
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
3646 :type '(choice (const nil)
3649 (const window-noselect)
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."
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."
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."
3673 (defcustom erc-minibuffer-ignored nil
3674 "If non-nil, print a message in the minibuffer if we ignored something."
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))
3696 (defun erc-nickname-in-use (nick reason)
3697 "If NICK is unavailable, tell the user the REASON.
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)
3706 (erc-display-error-notice
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)))
3719 (setq newnick (concat (truncate-string-to-width
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
3728 erc-nick-uniquifier)))
3729 (erc-cmd-NICK newnick)
3730 (erc-display-error-notice
3732 (format "Nickname %s is %s, trying %s"
3733 nick reason newnick)))))
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
3743 and as second argument the event parsed as a vector."
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)))
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)
3761 (if (erc-current-nick-p 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))
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))
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))))
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)
3797 (defcustom erc-format-nick-function 'erc-format-nick
3798 "*Function to format a nickname for message display."
3802 (defun erc-format-nick (&optional user channel-data)
3803 "Standard nickname formatting function. Only returns the value
3806 (erc-server-user-nickname user)))
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."
3814 (setq op (erc-channel-user-op channel-data)
3815 voice (erc-channel-user-voice channel-data)))
3816 (concat (if voice "+" "")
3818 (erc-server-user-nickname user)))))
3820 (defun erc-format-my-nick ()
3821 "Return the beginning of this user's message, correctly propertized"
3822 (if erc-show-my-nick
3825 (nick (erc-current-nick)))
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))))
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)
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
3848 (progn (erc-display-message parsed nil buffer s) t)
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))
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)
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)
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)
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
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)))
3893 (progn (erc-display-message parsed nil buffers s) t)
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
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)
3909 (progn (erc-display-message parsed nil buffers s) t)
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
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)))
3923 (progn (erc-display-message parsed nil (car buffers) s) t)
3926 ;;; Ban manipulation
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))))))
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
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)))
3956 (with-current-buffer buffer
3957 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
3958 ((string-match "^\\([+-]\\)b" mode)
3961 ((string-match "^-" mode)
3962 ;; Remove the unbanned masks from the ban list
3963 (setq erc-channel-banlist
3966 (member (upcase (cdr y))
3968 (cdr (split-string mode)))))
3969 erc-channel-banlist)))
3970 ((string-match "^+" mode)
3971 ;; Add the banned mask(s) to the ban list
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))))))))))
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)))))
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)))
3998 (erc-load-script f)))))
4000 (defun erc-connection-established (proc parsed)
4001 "Run just after connection.
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))))
4013 (defun erc-set-initial-user-mode (nick)
4014 "If `erc-user-mode' is non-nil for NICK, set the user modes."
4016 (let ((mode (if (functionp erc-user-mode)
4017 (funcall 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))))))
4023 (defun erc-display-error-notice (parsed string)
4024 "Display STRING as an error notice.
4026 See also `erc-display-message'."
4027 (erc-display-message
4028 parsed '(notice error) 'active string))
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)
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)
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))
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))
4065 (erc-display-message
4066 parsed (list 'notice 'error) proc
4068 (setq queries (cdr queries)))))))
4070 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
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
4081 'ACTION ?n nick ?u login ?h host ?a s))))
4083 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
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)))))
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)))))
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)."
4112 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4114 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4115 (erc-send-ctcp-notice nick s)))
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)))))
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))))
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)))
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
4147 "VERSION \C-bERC\C-b %s - an IRC client for emacs (\C-b%s\C-b)"
4149 erc-official-location)))
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"))))
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))))
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)))
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
4181 'CTCP-CLIENTINFO ?n nick ?m message)))
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
4191 'CTCP-FINGER ?n nick ?m message)))
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))
4199 (let ((time (match-string 1 msg)))
4201 (let ((delta (erc-time-diff (string-to-number time)
4202 (erc-current-time))))
4203 (erc-display-message
4206 ?t (erc-sec-to-time delta)))
4208 (erc-display-message
4210 'bad-ping-response ?n nick ?t time))))))
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
4219 'CTCP-TIME ?n nick ?m message)))
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
4229 'CTCP-VERSION ?n nick ?m message)))
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)))
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
4246 (erc-with-all-buffers-of-server proc nil
4247 (setq erc-away (current-time))))
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))
4255 (set-buffer (erc-active-buffer))
4256 (when erc-public-away-p
4258 (erc-default-target)
4260 (format "is back (gone for %s)"
4263 (erc-emacs-time-to-erc-time away-time)
4264 (erc-current-time))))
4266 (erc-update-mode-line)))
4268 ;;;; List of channel members handling
4270 (defun erc-channel-begin-receiving-names ()
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.
4276 See also `erc-channel-end-receiving-names'."
4277 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4279 (defun erc-channel-end-receiving-names ()
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
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)))
4291 (setq erc-channel-new-member-names nil))
4293 (defun erc-channel-receive-names (names-string)
4294 "This function is for internal use only.
4296 Update `erc-channel-users' according to NAMES-STRING.
4297 NAMES-STRING is a string listing some of the names on the
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)
4309 ((string-match "^+\\(.*\\)$" item)
4310 (setq name (match-string 1 item)
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)))
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."
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
4333 See also: `erc-update-user'"
4334 (erc-update-user (erc-get-server-user nick) new-nick
4335 host login full-name info))
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.
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."
4350 (not (equal (erc-server-user-nickname user)
4353 (erc-change-user-nickname user new-nick))
4355 (not (equal (erc-server-user-host user) host)))
4357 (setf (erc-server-user-host user) host))
4359 (not (equal (erc-server-user-login user) login)))
4361 (setf (erc-server-user-login user) login))
4362 (when (and full-name
4363 (not (equal (erc-server-user-full-name user)
4366 (setf (erc-server-user-full-name user) full-name))
4368 (not (equal (erc-server-user-info user) info)))
4370 (setf (erc-server-user-info user) info))
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))))))
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
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
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.
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))))
4405 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4407 (not (eq (erc-channel-user-op cuser) op)))
4409 (setf (erc-channel-user-op cuser)
4410 (cond ((eq op 'on) t)
4414 (not (eq (erc-channel-user-voice cuser) voice)))
4416 (setf (erc-channel-user-voice cuser)
4417 (cond ((eq voice 'on) t)
4418 ((eq voice 'off) nil)
4420 (when update-message-time
4421 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4423 (erc-update-user user new-nick
4424 host login full-name info)))
4428 (setq user (make-erc-server-user
4431 :full-name full-name
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)
4443 :voice (cond ((eq voice 'on) t)
4444 ((eq voice 'off) nil)
4447 (if update-message-time (current-time))))
4448 (puthash (erc-downcase nick) (cons user cuser)
4451 (when (and changed (null user-changed))
4452 (run-hooks 'erc-channel-members-changed-hook))
4453 (or changed user-changed add)))
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.
4461 See also: `erc-update-current-channel-member'"
4464 (erc-update-current-channel-member nick new-nick add op voice host
4465 login full-name info
4466 update-message-time)))
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)))
4473 (erc-remove-channel-user nick)
4474 (run-hooks 'erc-channel-members-changed-hook))))
4476 (defun erc-remove-channel-member (channel nick)
4477 "Remove NICK from CHANNEL's membership list.
4479 See also `erc-remove-current-channel-member'."
4482 (erc-remove-current-channel-member nick)))
4484 (defun erc-update-channel-topic (channel topic &optional modify)
4485 "Find a buffer for CHANNEL and set the TOPIC for it.
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))))
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))))
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)
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))
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))
4528 (defun erc-sort-strings (list-of-strings)
4529 "Sort LIST-OF-STRINGS in lexicographic order.
4532 (sort (copy-sequence list-of-strings) 'string<))
4534 (defun erc-parse-modes (mode-string)
4535 "Parse MODE-STRING into a list.
4537 Returns a list of three elements:
4539 (ADD-MODES REMOVE-MODES ARG-MODES).
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:
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))
4553 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
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
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)
4567 (if args (car args) nil))
4569 (if args (setq args (cdr args))))
4570 ((string-match "^[LlKk]" (car chars))
4571 (setq arg-modes (cons (list (car chars)
4573 (if (and add-p args)
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))
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))))
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?
4604 (if (not (boundp 'erc-channel-modes))
4605 (setq erc-channel-modes nil))
4607 (setq erc-channel-modes (delete (car remove-modes)
4609 remove-modes (cdr remove-modes)))
4611 (setq erc-channel-modes (cons (car add-modes)
4613 add-modes (cdr add-modes)))
4614 (setq erc-channel-modes (erc-sort-strings erc-channel-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
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
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)))
4641 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
4642 (t (setq erc-channel-user-limit nil))))))
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."
4648 (cond ((eq onoff 'on) (setq erc-channel-key key))
4649 (t (setq erc-channel-key nil)))))
4651 (defun erc-handle-user-status-change (type nlh &optional l)
4652 "Handle changes in any user's status.
4654 So far, only nick change is handled.
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.
4660 So far the following TYPE/L pairs are supported:
4664 nickname change 'nick (NEW-NICK)"
4665 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
4673 (defun erc-highlight-notice (s)
4674 "Highlight notice message S and return it.
4675 See also variable `erc-notice-highlight-type'"
4677 ((eq erc-notice-highlight-type 'prefix)
4678 (erc-put-text-property 0 (length erc-notice-prefix)
4679 'face 'erc-notice-face s)
4681 ((eq erc-notice-highlight-type 'all)
4682 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
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)))
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)
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.
4706 You can redefine or `defadvice' this function in order to add
4707 EmacsSpeak support."
4708 (put-text-property start end property value object))
4710 (defun erc-list (thing)
4711 "Return THING if THING is a list, or a list with THING as its element."
4716 (defun erc-parse-user (string)
4717 "Parse STRING as a user specification (nick!login@host).
4719 Return a list of the three separate tokens."
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)
4729 (match-string 2 string)))
4731 (list string "" ""))))
4733 (defun erc-extract-nick (string)
4734 "Return the nick corresponding to a user specification STRING.
4736 See also `erc-parse-user'."
4737 (car (erc-parse-user string)))
4739 (defun erc-put-text-properties (start end properties
4740 &optional object value-list)
4741 "Set text-properties for OBJECT.
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."
4747 (setq value-list (mapcar (lambda (x)
4750 (mapcar* (lambda (prop value)
4751 (erc-put-text-property start end prop value object))
4752 properties value-list))
4754 ;;; Input area handling:
4756 (defun erc-beg-of-input-line ()
4757 "Return the value of `point' at the beginning of the input line.
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))
4765 (defun erc-end-of-input-line ()
4766 "Return the value of `point' at the end of the input line."
4769 (defun erc-send-current-line ()
4770 "Parse current line and send it to IRC."
4775 ((< (point) (erc-beg-of-input-line))
4776 (message "Point is not in the input area")
4778 ((not (erc-server-buffer-live-p))
4779 (message "ERC: No process running")
4782 (erc-set-active-buffer (current-buffer))
4783 (let ((inhibit-read-only t)
4784 (str (erc-user-input))
4785 (old-buf (current-buffer)))
4787 ;; Kill the input and the prompt
4788 (delete-region (erc-beg-of-input-line)
4789 (erc-end-of-input-line))
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
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))))))
4805 ;; Only when last hook has been run...
4806 (run-hook-with-args 'erc-send-completed-hook str))))))
4808 (defun erc-user-input ()
4809 "Return the input of the user in the current buffer."
4812 (erc-end-of-input-line)))
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
4820 ;; Ignore empty input
4821 ((if erc-send-whitespace-lines
4823 (string-match "\\`[ \t\r\f\n]*\\'" input))
4824 (when erc-warn-about-blank-lines
4825 (message "Blank line - ignoring...")
4830 (erc-insert-this t))
4831 (setq erc-send-this t)
4832 (run-hook-with-args 'erc-send-pre-hook input)
4834 (if (or (string-match "\n" str)
4835 (not (char-equal (aref str 0) ?/)))
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))
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)))
4860 (erc-put-text-property beg (point)
4861 'face 'erc-command-indicator-face)
4863 (set-marker (process-mark erc-server-process) (point))
4864 (set-marker erc-insert-marker (point))
4867 (narrow-to-region insert-position (point))
4868 (run-hooks 'erc-send-modify-hook)
4869 (run-hooks 'erc-send-post-hook))))))
4871 (defun erc-display-msg (line)
4872 "Display LINE as a message of the user to the current target at the
4874 (when erc-insert-this
4875 (let ((insert-position (point)))
4876 (insert (erc-format-my-nick))
4877 (let ((beg (point)))
4879 (erc-put-text-property beg (point)
4880 'face 'erc-input-face))
4882 (set-marker (process-mark erc-server-process) (point))
4883 (set-marker erc-insert-marker (point))
4886 (narrow-to-region insert-position (point))
4887 (run-hooks 'erc-send-modify-hook)
4888 (run-hooks 'erc-send-post-hook))))))
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)))
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))
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)) " ")))
4911 (list cmd-fun arg))))
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."
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)))
4926 (setq l (cons s l))(setq doit nil))
4927 (t (setq doit nil)))))
4932 (defun erc-set-current-nick (nick)
4933 "Set the current nickname to NICK."
4934 (with-current-buffer (or (erc-server-buffer)
4936 (setq erc-server-current-nick nick)))
4938 (defun erc-current-nick ()
4939 "Return the current nickname."
4940 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
4943 erc-server-current-nick))
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)))
4949 (defun erc-nick-equal-p (nick1 nick2)
4950 "Return non-nil if NICK1 and NICK2 are the same.
4952 This matches strings according to the IRC protocol's case convention.
4954 See also `erc-downcase'."
4955 (string= (erc-downcase nick1)
4956 (erc-downcase nick2)))
4958 ;; default target handling
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)))
4965 ((listp tgt) (cdr tgt))
4968 (defun erc-add-default-channel (channel)
4969 "Add CHANNEL to the default channel list."
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))))
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
4984 (setq erc-default-recipients (delete (downcase channel)
4985 erc-default-recipients)))))
4987 (defun erc-add-query (nickname)
4988 "Add QUERY'd NICKNAME to the default channel list.
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))))
4995 (eq (car d1) 'QUERY))
4996 (setq erc-default-recipients (cons qt d2))
4997 (setq erc-default-recipients (cons qt erc-default-recipients)))))
4999 (defun erc-delete-query ()
5000 "Delete the topmost target if it is a QUERY."
5002 (let ((d1 (car erc-default-recipients))
5003 (d2 (cdr erc-default-recipients)))
5005 (eq (car d1) 'QUERY))
5006 (setq erc-default-recipients d2)
5007 (error "Current target is not a QUERY"))))
5009 (defun erc-ignored-user-p (spec)
5010 "Return non-nil if SPEC matches something in `erc-ignore-list'.
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
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'.
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)
5029 (erc-with-buffer (tgt proc)
5030 (let ((user (erc-get-server-user target-nick)))
5032 (erc-list-match erc-ignore-reply-list
5033 (erc-user-spec user))))))))
5035 (defun erc-message-target (msg)
5036 "Return the addressed target in MSG.
5038 The addressed target is the string before the first colon in MSG."
5039 (if (string-match "^\\([^: ]*\\):" msg)
5040 (match-string 1 msg)
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)))
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)))
5068 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5069 "Toggle automatic CTCP replies (like VERSION and PING).
5071 If ARG is positive, turns CTCP replies on.
5073 If ARG is non-nil and not positive, turns CTCP replies off."
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")))
5081 (defun erc-toggle-flood-control (&optional arg)
5082 "Toggle use of flood control on sent messages.
5084 If ARG is non-nil, use flood control.
5085 If ARG is nil, do not use flood control.
5087 See `erc-server-flood-margin' for an explanation of the available
5088 flood control parameters."
5090 (setq erc-flood-protect arg)
5091 (message "ERC flood control is %s"
5092 (cond (erc-flood-protect "ON")
5095 ;; Some useful channel and nick commands for fast key bindings
5097 (defun erc-invite-only-mode (&optional arg)
5098 "Turn on the invite only mode (+i) for the current channel.
5100 If ARG is non-nil, turn this mode off (-i).
5102 This command is sent even if excess flood is detected."
5104 (erc-set-active-buffer (current-buffer))
5105 (let ((tgt (erc-default-target))
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"))
5111 (t (erc-load-irc-script-lines (list (concat "/mode " tgt " +i"))
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.
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")
5129 (erc-insert-mode-command))
5131 (call-interactively 'erc-set-channel-limit))
5133 (call-interactively 'erc-set-channel-key))
5134 (t (erc-toggle-channel-mode key))))
5136 (defun erc-toggle-channel-mode (mode &optional channel)
5137 "Toggle channel MODE.
5139 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5140 `erc-default-target'."
5142 (erc-set-active-buffer (current-buffer))
5143 (let ((tgt (or channel (erc-default-target)))
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))))))
5155 (defun erc-insert-mode-command ()
5156 "Insert the line \"/mode <current target> \" at `point'."
5158 (let ((tgt (erc-default-target)))
5159 (if tgt (insert (concat "/mode " tgt " "))
5160 (erc-display-message nil 'error (current-buffer) 'no-target))))
5162 (defun erc-channel-names ()
5163 "Run \"/names #channel\" in the current channel."
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))))
5170 (defun erc-remove-text-properties-region (start end &optional object)
5171 "Clears the region (START,END) in OBJECT from all colors, etc."
5174 (let ((inhibit-read-only t))
5175 (set-text-properties start end nil object))))
5177 ;; script execution and startup
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.
5184 If FILE is found, return the path to it."
5185 (let ((filepath file))
5186 (if (file-readable-p filepath) filepath
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)))))
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)
5199 (while (and (not f) l)
5200 (if (file-readable-p (car l))
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))
5209 (defun erc-load-script (file)
5210 "Load a script from FILE.
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
5216 (erc-log (concat "erc-load-script: " file))
5218 ((string-match "\\.el$" file)
5221 (erc-load-irc-script file))))
5223 (defun erc-process-script-line (line &optional args)
5224 "Process an IRC script LINE.
5226 Does script-specific substitutions (script arguments, current nick,
5227 server, etc.) in LINE and returns it.
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,
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))
5245 (if (not tgt) (setq tgt ""))
5246 (if (not server) (setq server ""))
5247 (if (not nick) (setq nick ""))
5248 ;; First, compute the argument list
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))
5258 ;;(message "beginning of while: tmp=%S" tmp)
5259 (let* ((hd (substring line 0 tmp))
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))
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)
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)
5290 (setq res (concat res line))
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)
5299 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5301 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5302 "Load IRC script LINES (a list of strings).
5304 If optional NOEXPAND is non-nil, do not expand script-specific
5305 sequences, process the lines verbatim. Use this for multiline
5307 (let* ((cb (current-buffer))
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)
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)
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)))))
5333 "Perform user authentication at the IRC server."
5334 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
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)))
5345 (format "USER %s %s %s :%s"
5347 (if erc-anonymous-login erc-email-userid (user-login-name))
5349 erc-session-user-full-name))
5350 (erc-update-mode-line))
5352 ;; connection properties' heuristics
5354 (defun erc-determine-parameters (&optional server port nick name)
5355 "Determine the connection and authentication parameters.
5356 Sets the buffer local variables:
5358 - erc-session-server
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)))
5367 (defun erc-compute-server (&optional server)
5368 "Return an IRC server name.
5370 Tries a number of increasingly more default methods until a non-nil value is
5375 - The value of the IRCSERVER environment variable
5376 - `erc-default-server'."
5379 (getenv "IRCSERVER")
5380 erc-default-server))
5382 (defun erc-compute-nick (&optional nick)
5383 "Return user's NICK.
5385 Tries a number of increasingly more default methods until a non-nil value is
5390 - The value of the IRCNICK environment variable
5391 - via the function `user-login-name'."
5393 (if (consp erc-nick) (car erc-nick) erc-nick)
5398 (defun erc-compute-full-name (&optional full-name)
5399 "Return user's FULL-NAME.
5401 Tries a number of increasingly more default methods until a non-nil value is
5405 - `erc-user-full-name'
5406 - The value of the IRCNAME environment variable
5407 - via the function `user-full-name'."
5411 (if erc-anonymous-login "unknown" nil)
5414 (defun erc-compute-port (&optional port)
5415 "Return a port for an IRC server.
5417 Tries a number of increasingly more default methods until a non-nil
5422 (or port erc-port "ircd"))
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)))))
5433 (defun erc-emacs-time-to-erc-time (time)
5434 "Convert Emacs TIME to a number of seconds since the epoch."
5436 (+ (* (nth 0 time) 65536.0) (nth 1 time))))
5437 ; (round (+ (* (nth 0 tm) 65536.0) (nth 1 tm))))
5439 (defun erc-current-time ()
5440 "Return the `current-time' as a number of seconds since the epoch.
5442 See also `erc-emacs-time-to-erc-time'."
5443 (erc-emacs-time-to-erc-time (current-time)))
5445 (defun erc-time-diff (t1 t2)
5446 "Return the time difference in seconds between T1 and T2."
5449 (defun erc-time-gt (t1 t2)
5450 "Check whether T1 > T2."
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"
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)
5474 `("%d hours, %d minutes, %d seconds"
5475 ,hours ,minutes ,seconds)
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))))
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.")
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 ""))
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)))
5512 (concat s " " (cdr h))
5513 (concat s ": unknown command")))))
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)))
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
5529 (not (erc-server-buffer-p)))
5530 (kill-buffer (current-buffer))))
5532 (defun erc-nick-at-point ()
5533 "Give information about the nickname at `point'.
5535 If called interactively, give a human readable message in the
5536 minibuffer. If called programatically, return the corresponding
5537 entry of `channel-members'."
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
5545 (erc-get-server-user word)))
5546 host login full-name info nick op voice)
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))
5554 (setq op (erc-channel-user-op cuser)
5555 voice (erc-channel-user-voice cuser)))
5557 (message "%s is %s@%s%s%s"
5559 (if full-name (format " (%s)" full-name) "")
5561 (format " and is +%s%s on %s"
5564 (erc-default-target))
5568 (defun erc-away-p ()
5569 "Return t if the current ERC process is set away."
5571 (and (erc-server-buffer-live-p)
5572 (set-buffer (process-buffer erc-server-process))
5575 ;; Mode line handling
5577 (defcustom erc-mode-line-format "%s %a"
5578 "A string to be formatted and shown in the mode-line in `erc-mode'.
5580 The string is formatted using `format-spec' and the result is set as the value
5581 of `mode-line-buffer-identification'.
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
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.
5600 See `erc-mode-line-format' for which characters are can be used."
5601 :group 'erc-mode-line-and-header
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
5610 (defcustom erc-show-channel-key-p t
5611 "Show the the channel key in the header line."
5612 :group 'erc-paranoia
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
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
5633 (defun erc-shorten-server-name (server-name)
5634 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
5635 (if (stringp server-name)
5637 (insert server-name)
5638 (let ((alist erc-common-server-suffixes))
5640 (goto-char (point-min))
5641 (if (re-search-forward (caar alist) nil t)
5642 (replace-match (cdar alist)))
5643 (setq alist (cdr alist))))
5646 (defun erc-format-target ()
5647 "Return the name of the target (channel or nickname or servername:port)."
5648 (let ((target (erc-default-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)))))
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))
5663 (server-name server-name)
5664 (t (buffer-name (current-buffer))))))
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)
5673 (format-time-string erc-mode-line-away-status-format a)
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)
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
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))
5692 (if erc-show-channel-key-p
5693 (format "k %s" erc-channel-key)
5696 (if lag (format ",lag:%.0f" lag) "")
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))
5713 ((erc-server-process-alive)
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)))
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)
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
5734 (fill-region (point-min) (point-max))
5736 (setq header-line-format
5737 (erc-replace-regexp-in-string
5740 (erc-propertize header 'help-echo help-echo)))))
5741 (t (setq header-line-format header))))))
5742 (if (featurep 'xemacs)
5744 (force-mode-line-update))))
5746 (defun erc-update-mode-line (&optional buffer)
5747 "Update the mode line in BUFFER.
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)))))
5758 (defun erc-port-to-string (p)
5759 "Convert port P to a string.
5760 P may be an integer or a service name."
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)))
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."
5776 (let ((version-string
5777 (format "ERC %s" erc-version-string)))
5779 (insert version-string)
5781 (message "%s" version-string)
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."
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))
5799 (insert version-string)
5801 (message "%s" version-string)
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."
5809 (mapconcat 'identity
5810 (let (modes (case-fold-search nil))
5811 (dolist (var (apropos-internal "^erc-.*mode$"))
5812 (when (and (boundp var)
5814 (setq modes (cons (symbol-name var)
5821 (message "%s" string)
5824 (defun erc-latest-version ()
5825 "Retrieve the latest erc.el version from CVS."
5827 (if (ignore-errors (require 'url))
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"))
5838 (error "URL needs to be installed")))
5840 (defun erc-ediff-latest-version ()
5841 "Ediff your installed erc.el with the latest CVS version.
5842 See also `erc-latest-version'."
5844 (let ((current (locate-library "erc.el")))
5846 (ediff-buffers (find-file current)
5847 (erc-latest-version))
5848 (error "You do not appear to have the uncompiled erc.el file"))))
5850 (defun erc-trim-string (s)
5851 "Trim leading and trailing spaces off S."
5853 ((not (stringp s)) nil)
5854 ((string-match "^\\s-*$" s)
5856 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
5861 (defun erc-arrange-session-in-multiple-windows ()
5862 "Open a window for every non-server buffer related to `erc-session-server'.
5864 All windows are opened in the current frame."
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)))
5870 (delete-other-windows)
5871 (switch-to-buffer (car bufs))
5872 (setq bufs (cdr bufs))
5875 (switch-to-buffer-other-window (car bufs))
5876 (setq bufs (cdr bufs))
5877 (balance-windows)))))
5879 (defun erc-popup-input-buffer ()
5880 "Provide a input buffer."
5882 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
5887 (list (symbol-name e)))
5888 (apropos-internal "-mode$" 'commandp))
5890 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
5892 (narrow-to-region (point) (point))
5893 (shrink-window-if-larger-than-buffer)))
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))))
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)
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))))
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... ")
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")
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")
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")))
6026 (defun erc-message-english-PART (&rest args)
6027 "Format a proper PART message.
6029 This function is an example on what could be done with formatting
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)
6045 (defvar erc-current-message-catalog 'english)
6046 (make-variable-buffer-local 'erc-current-message-catalog)
6048 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6049 "Retrieve ENTRY from CATALOG.
6051 If CATALOG is nil, `erc-current-message-catalog' is used.
6053 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6055 (unless catalog (setq catalog erc-current-message-catalog))
6056 (let ((var (erc-make-message-variable-name catalog entry)))
6059 (when (boundp (erc-make-message-variable-name 'english entry))
6060 (symbol-value (erc-make-message-variable-name 'english entry))))))
6062 (defun erc-format-message (msg &rest args)
6063 "Format MSG according to ARGS.
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)))
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))))
6075 ;;; Various hook functions
6077 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6079 (defcustom erc-kill-server-hook '(erc-kill-server)
6080 "*Invoked whenever a server-buffer is killed via `kill-buffer'."
6084 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6085 "*Invoked whenever a channel-buffer is killed via `kill-buffer'."
6089 (defcustom erc-kill-buffer-hook nil
6090 "*Hook run whenever a non-server or channel buffer is killed.
6092 See also `kill-buffer'."
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)
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))
6112 (run-hooks 'erc-kill-buffer-hook)))))
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)))))
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))
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)
6137 ;;; erc.el ends here
6140 ;; outline-regexp: ";;+"
6141 ;; indent-tabs-mode: t
6145 ;; arch-tag: d19587f6-627e-48c1-8d86-58595fa3eca3