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: Michael Olson (mwolson@gnu.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://sv.gnu.org/projects/erc/
40 ;; * http://www.emacswiki.org/cgi-bin/wiki.pl?EmacsIRCClient
42 ;; As of 2006-06-13, ERC development is now hosted on Savannah
43 ;; (http://sv.gnu.org/projects/erc). I invite everyone who wants to
44 ;; hack on it to contact me <mwolson@gnu.org> in order to get write
45 ;; access to the shared Arch archive.
49 ;; Put erc.el in your load-path, and put (require 'erc) in your .emacs.
53 ;; Use M-x customize-group RET erc RET to get an overview
54 ;; of all the variables you can tweak.
58 ;; To connect to an IRC server, do
62 ;; After you are connected to a server, you can use C-h m or have a look at
70 (defconst erc-version-string "Version 5.1.4"
71 "ERC version. This is used by function `erc-version'.")
73 (eval-when-compile (require 'cl))
80 (defvar erc-official-location
81 "http://emacswiki.org/cgi-bin/wiki/ERC (mailing list: erc-discuss@gnu.org)"
82 "Location of the ERC client on the Internet.")
85 "Emacs Internet Relay Chat client."
86 :link '(url-link "http://www.emacswiki.org/cgi-bin/wiki/ERC")
90 (defgroup erc-buffers nil
91 "Creating new ERC buffers"
94 (defgroup erc-display nil
95 "Settings for how various things are displayed"
98 (defgroup erc-mode-line-and-header nil
99 "Displaying information in the mode-line and header"
102 (defgroup erc-ignore nil
103 "Ignoring certain messages"
106 (defgroup erc-query nil
107 "Using separate buffers for private discussions"
110 (defgroup erc-quit-and-part nil
111 "Quitting and parting channels"
114 (defgroup erc-paranoia nil
115 "Know what is sent and received; control the display of sensitive data."
118 (defgroup erc-scripts nil
119 "Running scripts at startup and with /LOAD"
122 (require 'erc-backend)
124 ;; compatibility with older ERC releases
126 (if (fboundp 'defvaralias)
128 (defvaralias 'erc-announced-server-name 'erc-server-announced-name)
129 (erc-make-obsolete-variable 'erc-announced-server-name
130 'erc-server-announced-name
132 (defvaralias 'erc-process 'erc-server-process)
133 (erc-make-obsolete-variable 'erc-process 'erc-server-process "ERC 5.1")
134 (defvaralias 'erc-default-coding-system 'erc-server-coding-system)
135 (erc-make-obsolete-variable 'erc-default-coding-system
136 'erc-server-coding-system
138 (message (concat "ERC: The function `defvaralias' is not bound. See the "
139 "NEWS file for variable name changes since ERC 5.0.4.")))
141 (defalias 'erc-send-command 'erc-server-send)
142 (erc-make-obsolete 'erc-send-command 'erc-server-send "ERC 5.1")
144 ;; tunable connection and authentication parameters
146 (defcustom erc-server nil
147 "IRC server to use if one is not provided.
148 See function `erc-compute-server' for more details on connection
149 parameters and authentication."
151 :type '(choice (const :tag "None" nil)
152 (string :tag "Server")))
154 (defcustom erc-port nil
155 "IRC port to use if not specified.
157 This can be either a string or a number."
159 :type '(choice (const :tag "None" nil)
160 (integer :tag "Port number")
161 (string :tag "Port string")))
163 (defcustom erc-nick nil
164 "Nickname to use if one is not provided.
166 This can be either a string, or a list of strings.
167 In the latter case, if the first nick in the list is already in use,
168 other nicks are tried in the list order.
170 See function `erc-compute-nick' for more details on connection
171 parameters and authentication."
173 :type '(choice (const :tag "None" nil)
174 (string :tag "Nickname")
175 (repeat (string :tag "Nickname"))))
177 (defcustom erc-nick-uniquifier "`"
178 "The string to append to the nick if it is already in use."
182 (defcustom erc-try-new-nick-p t
183 "If the nickname you chose isn't available, and this option is non-nil,
184 ERC should automatically attempt to connect with another nickname.
186 You can manually set another nickname with the /NICK command."
190 (defcustom erc-user-full-name nil
193 This can be either a string or a function to call.
195 See function `erc-compute-full-name' for more details on connection
196 parameters and authentication."
198 :type '(choice (const :tag "No name" nil)
200 (function :tag "Get from function"))
201 :set (lambda (sym val)
203 (set sym (funcall val))
206 (defvar erc-password nil
207 "Password to use when authenticating to an IRC server.
208 It is not strictly necessary to provide this, since ERC will
211 (defcustom erc-user-mode nil
212 "Initial user modes to be set after a connection is established."
214 :type '(choice (const nil) string function))
217 (defcustom erc-prompt-for-password t
218 "Asks before using the default password, or whether to enter a new one."
222 (defcustom erc-warn-about-blank-lines t
223 "Warn the user if they attempt to send a blank line."
227 (defcustom erc-send-whitespace-lines nil
228 "If set to non-nil, send lines consisting of only whitespace."
232 (defcustom erc-hide-prompt nil
233 "If non-nil, do not display the prompt for commands.
235 \(A command is any input starting with a '/').
237 See also the variables `erc-prompt' and `erc-command-indicator'."
243 (defcustom erc-show-my-nick t
244 "If non-nil, display one's own nickname when sending a message.
246 If non-nil, \"<nickname>\" will be shown.
247 If nil, only \"> \" will be shown."
251 (define-widget 'erc-message-type 'set
252 "A set of standard IRC Message types."
253 :args '((const "JOIN")
259 (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))
261 (defcustom erc-hide-list nil
262 "*List of IRC type messages to hide.
263 A typical value would be '(\"JOIN\" \"PART\" \"QUIT\")."
265 :type 'erc-message-type)
267 (defvar erc-session-password nil
268 "The password used for the current session.")
269 (make-variable-buffer-local 'erc-session-password)
271 (defcustom erc-disconnected-hook nil
272 "Run this hook with arguments (NICK IP REASON) when disconnected.
273 This happens before automatic reconnection. Note, that
274 `erc-server-QUIT-functions' might not be run when we disconnect,
275 simply because we do not necessarily receive the QUIT event."
279 (defcustom erc-complete-functions nil
280 "These functions get called when the user hits TAB in ERC.
281 Each function in turn is called until one returns non-nil to
282 indicate it has handled the input."
286 (defcustom erc-join-hook nil
287 "Hook run when we join a channel. Hook functions are called
288 without arguments, with the current buffer set to the buffer of
291 See also `erc-server-JOIN-functions', `erc-part-hook'."
295 (defcustom erc-quit-hook nil
296 "Hook run when processing a quit command directed at our nick.
298 The hook receives one argument, the current PROCESS.
299 See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
303 (defcustom erc-part-hook nil
304 "Hook run when processing a PART message directed at our nick.
306 The hook receives one argument, the current BUFFER.
307 See also `erc-server-QUIT-functions', `erc-quit-hook' and
308 `erc-disconnected-hook'."
312 (defcustom erc-kick-hook nil
313 "Hook run when processing a KICK message directed at our nick.
315 The hook receives one argument, the current BUFFER.
316 See also `erc-server-PART-functions' and `erc-part-hook'."
320 (defcustom erc-nick-changed-functions nil
321 "List of functions run when your nick was successfully changed.
323 Each function should accept two arguments, NEW-NICK and OLD-NICK."
327 (defcustom erc-connect-pre-hook '(erc-initialize-log-marker)
328 "Hook called just before `erc' calls `erc-connect'.
329 Functions are run in the buffer-to-be."
334 (defvar erc-channel-users nil
335 "A hash table of members in the current channel, which
336 associates nicknames with cons cells of the form:
337 \(USER . MEMBER-DATA) where USER is a pointer to an
338 erc-server-user struct, and MEMBER-DATA is a pointer to an
339 erc-channel-user struct.")
340 (make-variable-buffer-local 'erc-channel-users)
342 (defvar erc-server-users nil
343 "A hash table of users on the current server, which associates
344 nicknames with erc-server-user struct instances.")
345 (make-variable-buffer-local 'erc-server-users)
347 (defun erc-downcase (string)
348 "Convert STRING to IRC standard conforming downcase."
349 (let ((s (downcase string))
355 (while (string-match "[]\\[~]" s)
356 (aset s (match-beginning 0)
357 (cdr (assq (aref s (match-beginning 0)) c)))))
360 (defstruct (erc-server-user (:type vector) :named)
362 nickname host login full-name info
365 ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
366 ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
370 (defstruct (erc-channel-user (:type vector) :named)
372 ;; Last message time (in the form of the return value of
375 ;; This is useful for ordered name completion.
376 (last-message-time nil))
378 (defsubst erc-get-channel-user (nick)
379 "Finds the (USER . CHANNEL-DATA) element corresponding to NICK
380 in the current buffer's `erc-channel-users' hash table."
381 (gethash (erc-downcase nick) erc-channel-users))
383 (defsubst erc-get-server-user (nick)
384 "Finds the USER corresponding to NICK in the current server's
385 `erc-server-users' hash table."
386 (with-current-buffer (process-buffer erc-server-process)
387 (gethash (erc-downcase nick) erc-server-users)))
389 (defsubst erc-add-server-user (nick user)
390 "This function is for internal use only.
392 Adds USER with nickname NICK to the `erc-server-users' hash table."
393 (with-current-buffer (process-buffer erc-server-process)
394 (puthash (erc-downcase nick) user erc-server-users)))
396 (defsubst erc-remove-server-user (nick)
397 "This function is for internal use only.
399 Removes the user with nickname NICK from the `erc-server-users'
400 hash table. This user is not removed from the
401 `erc-channel-users' lists of other buffers.
403 See also: `erc-remove-user'."
404 (with-current-buffer (process-buffer erc-server-process)
405 (remhash (erc-downcase nick) erc-server-users)))
407 (defun erc-change-user-nickname (user new-nick)
408 "This function is for internal use only.
410 Changes the nickname of USER to NEW-NICK in the
411 `erc-server-users' hash table. The `erc-channel-users' lists of
412 other buffers are also changed."
413 (let ((nick (erc-server-user-nickname user)))
414 (setf (erc-server-user-nickname user) new-nick)
415 (with-current-buffer (process-buffer erc-server-process)
416 (remhash (erc-downcase nick) erc-server-users)
417 (puthash (erc-downcase new-nick) user erc-server-users))
418 (dolist (buf (erc-server-user-buffers user))
419 (if (buffer-live-p buf)
420 (with-current-buffer buf
421 (let ((cdata (erc-get-channel-user nick)))
422 (remhash (erc-downcase nick) erc-channel-users)
423 (puthash (erc-downcase new-nick) cdata
424 erc-channel-users)))))))
426 (defun erc-remove-channel-user (nick)
427 "This function is for internal use only.
429 Removes the user with nickname NICK from the `erc-channel-users'
430 list for this channel. If this user is not in the
431 `erc-channel-users' list of any other buffers, the user is also
432 removed from the server's `erc-server-users' list.
434 See also: `erc-remove-server-user' and `erc-remove-user'."
435 (let ((channel-data (erc-get-channel-user nick)))
437 (let ((user (car channel-data)))
438 (setf (erc-server-user-buffers user)
439 (delq (current-buffer)
440 (erc-server-user-buffers user)))
441 (remhash (erc-downcase nick) erc-channel-users)
442 (if (null (erc-server-user-buffers user))
443 (erc-remove-server-user nick))))))
445 (defun erc-remove-user (nick)
446 "This function is for internal use only.
448 Removes the user with nickname NICK from the `erc-server-users'
449 list as well as from all `erc-channel-users' lists.
451 See also: `erc-remove-server-user' and
452 `erc-remove-channel-user'."
453 (let ((user (erc-get-server-user nick)))
455 (let ((buffers (erc-server-user-buffers user)))
456 (dolist (buf buffers)
457 (if (buffer-live-p buf)
458 (with-current-buffer buf
459 (remhash (erc-downcase nick) erc-channel-users)
460 (run-hooks 'erc-channel-members-changed-hook)))))
461 (erc-remove-server-user nick))))
463 (defun erc-remove-channel-users ()
464 "This function is for internal use only.
466 Removes all users in the current channel. This is called by
467 `erc-server-PART' and `erc-server-QUIT'."
468 (when (and erc-server-connected
469 (erc-server-process-alive)
470 (hash-table-p erc-channel-users))
471 (maphash (lambda (nick cdata)
472 (erc-remove-channel-user nick))
474 (clrhash erc-channel-users)))
476 (defsubst erc-channel-user-op-p (nick)
477 "Return `t' if NICK is an operator in the current channel."
479 (hash-table-p erc-channel-users)
480 (let ((cdata (erc-get-channel-user nick)))
481 (and cdata (cdr cdata)
482 (erc-channel-user-op (cdr cdata))))))
484 (defsubst erc-channel-user-voice-p (nick)
485 "Return `t' if NICK has voice in the current channel."
487 (hash-table-p erc-channel-users)
488 (let ((cdata (erc-get-channel-user nick)))
489 (and cdata (cdr cdata)
490 (erc-channel-user-voice (cdr cdata))))))
492 (defun erc-get-channel-user-list ()
493 "Returns a list of users in the current channel. Each element
494 of the list is of the form (USER . CHANNEL-DATA), where USER is
495 an erc-server-user struct, and CHANNEL-DATA is either `nil' or an
496 erc-channel-user struct.
498 See also: `erc-sort-channel-users-by-activity'"
500 (if (hash-table-p erc-channel-users)
501 (maphash (lambda (nick cdata)
502 (setq users (cons cdata users)))
506 (defun erc-get-server-nickname-list ()
507 "Returns a list of known nicknames on the current server."
508 (if (erc-server-process-alive)
509 (with-current-buffer (erc-server-buffer)
511 (when (hash-table-p erc-server-users)
512 (maphash (lambda (n user)
514 (cons (erc-server-user-nickname user)
519 (defun erc-get-channel-nickname-list ()
520 "Returns a list of known nicknames on the current channel."
522 (when (hash-table-p erc-channel-users)
523 (maphash (lambda (n cdata)
525 (cons (erc-server-user-nickname (car cdata))
530 (defun erc-get-server-nickname-alist ()
531 "Returns an alist of known nicknames on the current server."
532 (if (erc-server-process-alive)
533 (with-current-buffer (erc-server-buffer)
535 (when (hash-table-p erc-server-users)
536 (maphash (lambda (n user)
538 (cons (cons (erc-server-user-nickname user) nil)
543 (defun erc-get-channel-nickname-alist ()
544 "Returns an alist of known nicknames on the current channel."
546 (when (hash-table-p erc-channel-users)
547 (maphash (lambda (n cdata)
549 (cons (cons (erc-server-user-nickname (car cdata)) nil)
554 (defun erc-sort-channel-users-by-activity (list)
555 "Sorts LIST such that users which have spoken most recently are
556 listed first. LIST must be of the form (USER . CHANNEL-DATA).
558 See also: `erc-get-channel-user-list'."
563 (let ((tx (erc-channel-user-last-message-time (cdr x)))
564 (ty (erc-channel-user-last-message-time (cdr y))))
571 (defun erc-sort-channel-users-alphabetically (list)
572 "Sort LIST so that users' nicknames are in alphabetical order.
573 LIST must be of the form (USER . CHANNEL-DATA).
575 See also: `erc-get-channel-user-list'."
580 (let ((nickx (downcase (erc-server-user-nickname (car x))))
581 (nicky (downcase (erc-server-user-nickname (car y)))))
584 (string-lessp nickx nicky)
588 (defvar erc-channel-topic nil
589 "A topic string for the channel. Should only be used in channel-buffers.")
590 (make-variable-buffer-local 'erc-channel-topic)
592 (defvar erc-channel-modes nil
593 "List of strings representing channel modes.
594 E.g. '(\"i\" \"m\" \"s\" \"b Quake!*@*\")
595 \(not sure the ban list will be here, but why not)")
596 (make-variable-buffer-local 'erc-channel-modes)
598 (defvar erc-insert-marker nil
599 "The place where insertion of new text in erc buffers should happen.")
600 (make-variable-buffer-local 'erc-insert-marker)
602 (defvar erc-input-marker nil
603 "The marker where input should be inserted.")
604 (make-variable-buffer-local 'erc-input-marker)
606 (defun erc-string-no-properties (string)
607 "Return a copy of STRING will all text-properties removed."
608 (let ((newstring (copy-sequence string)))
609 (set-text-properties 0 (length newstring) nil newstring)
612 (defcustom erc-prompt "ERC>"
613 "Prompt used by ERC. Trailing whitespace is not required."
615 :type '(choice string function))
618 "Return the input prompt as a string.
620 See also the variable `erc-prompt'."
621 (let ((prompt (if (functionp erc-prompt)
624 (if (> (length prompt) 0)
628 (defcustom erc-command-indicator nil
629 "Indicator used by ERC for showing commands.
631 If non-nil, this will be used in the ERC buffer to indicate
632 commands (i.e., input starting with a '/').
634 If nil, the prompt will be constructed from the variable `erc-prompt'."
636 :type '(choice (const nil) string function))
638 (defun erc-command-indicator ()
639 "Return the command indicator prompt as a string.
641 This only has any meaning if the variable `erc-command-indicator' is non-nil."
642 (and erc-command-indicator
643 (let ((prompt (if (functionp erc-command-indicator)
644 (funcall erc-command-indicator)
645 erc-command-indicator)))
646 (if (> (length prompt) 0)
650 (defcustom erc-notice-prefix "*** "
651 "*Prefix for all notices."
655 (defcustom erc-notice-highlight-type 'all
656 "*Determines how to highlight notices.
657 See `erc-notice-prefix'.
659 The following values are allowed:
661 'prefix - highlight notice prefix only
662 'all - highlight the entire notice
664 Any other value disables notice's highlighting altogether."
666 :type '(choice (const :tag "highlight notice prefix only" prefix)
667 (const :tag "highlight the entire notice" all)
668 (const :tag "don't highlight notices at all" nil)))
670 (defcustom erc-echo-notice-hook nil
671 "*Specifies a list of functions to call to echo a private
672 notice. Each function is called with four arguments, the string
673 to display, the parsed server message, the target buffer (or
674 nil), and the sender. The functions are called in order, until a
675 function evaluates to non-nil. These hooks are called after
676 those specified in `erc-echo-notice-always-hook'.
678 See also: `erc-echo-notice-always-hook',
679 `erc-echo-notice-in-default-buffer',
680 `erc-echo-notice-in-target-buffer',
681 `erc-echo-notice-in-minibuffer',
682 `erc-echo-notice-in-server-buffer',
683 `erc-echo-notice-in-active-non-server-buffer',
684 `erc-echo-notice-in-active-buffer',
685 `erc-echo-notice-in-user-buffers',
686 `erc-echo-notice-in-user-and-target-buffers',
687 `erc-echo-notice-in-first-user-buffer'"
690 :options '(erc-echo-notice-in-default-buffer
691 erc-echo-notice-in-target-buffer
692 erc-echo-notice-in-minibuffer
693 erc-echo-notice-in-server-buffer
694 erc-echo-notice-in-active-non-server-buffer
695 erc-echo-notice-in-active-buffer
696 erc-echo-notice-in-user-buffers
697 erc-echo-notice-in-user-and-target-buffers
698 erc-echo-notice-in-first-user-buffer))
700 (defcustom erc-echo-notice-always-hook
701 '(erc-echo-notice-in-default-buffer)
702 "*Specifies a list of functions to call to echo a private
703 notice. Each function is called with four arguments, the string
704 to display, the parsed server message, the target buffer (or
705 nil), and the sender. The functions are called in order, and all
706 functions are called. These hooks are called before those
707 specified in `erc-echo-notice-hook'.
709 See also: `erc-echo-notice-hook',
710 `erc-echo-notice-in-default-buffer',
711 `erc-echo-notice-in-target-buffer',
712 `erc-echo-notice-in-minibuffer',
713 `erc-echo-notice-in-server-buffer',
714 `erc-echo-notice-in-active-non-server-buffer',
715 `erc-echo-notice-in-active-buffer',
716 `erc-echo-notice-in-user-buffers',
717 `erc-echo-notice-in-user-and-target-buffers',
718 `erc-echo-notice-in-first-user-buffer'"
721 :options '(erc-echo-notice-in-default-buffer
722 erc-echo-notice-in-target-buffer
723 erc-echo-notice-in-minibuffer
724 erc-echo-notice-in-server-buffer
725 erc-echo-notice-in-active-non-server-buffer
726 erc-echo-notice-in-active-buffer
727 erc-echo-notice-in-user-buffers
728 erc-echo-notice-in-user-and-target-buffers
729 erc-echo-notice-in-first-user-buffer))
731 ;; other tunable parameters
733 (defcustom erc-whowas-on-nosuchnick nil
734 "*If non-nil, do a whowas on a nick if no such nick."
738 (defcustom erc-verbose-server-ping nil
739 "*If non-nil, show every time you get a PING or PONG from the server."
743 (defcustom erc-public-away-p nil
744 "*Let others know you are back when you are no longer marked away.
745 This happens in this form:
746 * <nick> is back (gone for <time>)
748 Many consider it impolite to do so automatically."
752 (defcustom erc-away-nickname nil
753 "*The nickname to take when you are marked as being away."
755 :type '(choice (const nil)
758 (defcustom erc-paranoid nil
759 "If non-nil, then all incoming CTCP requests will be shown."
763 (defcustom erc-disable-ctcp-replies nil
764 "Disable replies to CTCP requests that require a reply.
765 If non-nil, then all incoming CTCP requests that normally require
766 an automatic reply (like VERSION or PING) will be ignored. Good to
767 set if some hacker is trying to flood you away."
771 (defcustom erc-anonymous-login t
772 "Be paranoid, don't give away your machine name."
776 (defcustom erc-prompt-for-channel-key nil
777 "Prompt for channel key when using `erc-join-channel' interactively"
781 (defcustom erc-email-userid "user"
782 "Use this as your email user ID."
786 (defcustom erc-ignore-list nil
787 "*List of regexps matching user identifiers to ignore.
789 A user identifier has the form \"nick!login@host\". If an
790 identifier matches, the message from the person will not be
793 :type '(repeat regexp))
794 (make-variable-buffer-local 'erc-ignore-list)
796 (defcustom erc-ignore-reply-list nil
797 "*List of regexps matching user identifiers to ignore completely.
799 This differs from `erc-ignore-list' in that it also ignores any
800 messages directed at the user.
802 A user identifier has the form \"nick!login@host\".
804 If an identifier matches, or a message is addressed to a nick
805 whose identifier matches, the message will not be processed.
807 CAVEAT: ERC doesn't know about the user and host of anyone who
808 was already in the channel when you joined, but never said
809 anything, so it won't be able to match the user and host of those
810 people. You can update the ERC internal info using /WHO *."
812 :type '(repeat regexp))
814 (defvar erc-flood-protect t
815 "*If non-nil, flood protection is enabled.
816 Flooding is sending too much information to the server in too
817 short of an interval, which may cause the server to terminate the
820 See `erc-server-flood-margin' for other flood-related parameters.")
824 (defcustom erc-startup-file-list
825 '("~/.emacs.d/.ercrc.el" "~/.emacs.d/.ercrc"
826 "~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
827 "List of files to try for a startup script.
828 The first existent and readable one will get executed.
830 If the filename ends with `.el' it is presumed to be an emacs-lisp
831 script and it gets (load)ed. Otherwise is is treated as a bunch of
832 regular IRC commands"
834 :type '(repeat file))
836 (defcustom erc-script-path nil
837 "List of directories to look for a script in /load command.
838 The script is first searched in the current directory, then in each
839 directory in the list."
841 :type '(repeat directory))
843 (defcustom erc-script-echo t
844 "*If not-NIL, echo the IRC script commands locally."
848 (defvar erc-last-saved-position nil
849 "A marker containing the position the current buffer was last saved at.")
850 (make-variable-buffer-local 'erc-last-saved-position)
852 (defcustom erc-kill-buffer-on-part nil
853 "Kill the channel buffer on PART.
854 This variable should probably stay nil, as ERC can reuse buffers if
855 you rejoin them later."
856 :group 'erc-quit-and-part
859 (defcustom erc-kill-queries-on-quit nil
860 "Kill all query (also channel) buffers of this server on QUIT.
861 See the variable `erc-kill-buffer-on-part' for details."
862 :group 'erc-quit-and-part
865 (defcustom erc-kill-server-buffer-on-quit nil
866 "Kill the server buffer of the process on QUIT."
867 :group 'erc-quit-and-part
870 (defcustom erc-quit-reason-various-alist nil
871 "Alist of possible arguments to the /quit command.
873 Each element has the form:
876 If REGEXP matches the argument to /quit, then its relevant RESULT
877 will be used. RESULT may be either a string, or a function. If
878 a function, it should return the quit message as a string.
880 If no elements match, then the empty string is used.
883 (setq erc-quit-reason-various-alist
884 '((\"zippy\" erc-quit-reason-zippy)
885 (\"xmms\" dme:now-playing)
886 (\"version\" erc-quit-reason-normal)
887 (\"home\" \"Gone home !\")
888 (\"^$\" \"Default Reason\")))
889 If the user types \"/quit zippy\", then a Zippy the Pinhead quotation
890 will be used as the quit message."
891 :group 'erc-quit-and-part
892 :type '(repeat (list regexp (choice (string) (function)))))
894 (defcustom erc-part-reason-various-alist nil
895 "Alist of possible arguments to the /part command.
897 Each element has the form:
900 If REGEXP matches the argument to /part, then its relevant RESULT
901 will be used. RESULT may be either a string, or a function. If
902 a function, it should return the part message as a string.
904 If no elements match, then the empty string is used.
907 (setq erc-part-reason-various-alist
908 '((\"zippy\" erc-part-reason-zippy)
909 (\"xmms\" dme:now-playing)
910 (\"version\" erc-part-reason-normal)
911 (\"home\" \"Gone home !\")
912 (\"^$\" \"Default Reason\")))
913 If the user types \"/part zippy\", then a Zippy the Pinhead quotation
914 will be used as the part message."
915 :group 'erc-quit-and-part
916 :type '(repeat (list regexp (choice (string) (function)))))
918 (defcustom erc-quit-reason 'erc-quit-reason-normal
919 "*A function which returns the reason for quitting.
921 The function is passed a single argument, the string typed by the
922 user after \"/quit\"."
923 :group 'erc-quit-and-part
924 :type '(choice (const erc-quit-reason-normal)
925 (const erc-quit-reason-zippy)
926 (const erc-quit-reason-various)
929 (defcustom erc-part-reason 'erc-part-reason-normal
930 "A function which returns the reason for parting a channel.
932 The function is passed a single argument, the string typed by the
933 user after \"/PART\"."
934 :group 'erc-quit-and-part
935 :type '(choice (const erc-part-reason-normal)
936 (const erc-part-reason-zippy)
937 (const erc-part-reason-various)
940 (defvar erc-grab-buffer-name "*erc-grab*"
941 "The name of the buffer created by `erc-grab-region'.")
943 ;; variables available for IRC scripts
945 (defvar erc-user-information "ERC User"
946 "USER_INFORMATION IRC variable.")
950 (defgroup erc-hooks nil
951 "Hook variables for fancy customizations of ERC."
954 (defcustom erc-mode-hook nil
955 "Hook run after `erc-mode' setup is finished."
958 :options '(erc-add-scroll-to-bottom))
960 (defcustom erc-timer-hook nil
961 "Put functions which should get called more or less periodically here.
962 The idea is that servers always play ping pong with the client, and so there
963 is no need for any idle-timer games with Emacs."
967 (defcustom erc-insert-pre-hook nil
968 "Hook called first when some text is inserted through `erc-display-line'.
969 It gets called with one argument, STRING.
970 To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
971 Filtering functions can set `erc-insert-this' to nil to avoid
972 display of that particular string at all."
976 (defcustom erc-send-pre-hook nil
977 "Hook called first when some text is sent through `erc-send-current-line'.
978 It gets called with one argument, STRING.
980 To change the text that will be sent, set the variable STR which is
981 used in `erc-send-current-line'.
983 To change the text inserted into the buffer without changing the text
984 that will be sent, use `erc-send-modify-hook' instead.
986 Filtering functions can set `erc-send-this' to nil to avoid sending of
987 that particular string at all and `erc-insert-this' to prevent
988 inserting that particular string into the buffer.
990 Note that it's useless to set `erc-send-this' to nil and
991 `erc-insert-this' to t. ERC is sane enough to not insert the text
996 (defvar erc-insert-this t
997 "Insert the text into the target buffer or not.
998 Functions on `erc-insert-pre-hook' can set this variable to nil
999 if they wish to avoid insertion of a particular string.")
1001 (defvar erc-send-this t
1002 "Send the text to the target or not.
1003 Functions on `erc-send-pre-hook' can set this variable to nil
1004 if they wish to avoid sending of a particular string.")
1006 (defcustom erc-insert-modify-hook ()
1007 "Insertion hook for functions that will change the text's appearance.
1008 This hook is called just after `erc-insert-pre-hook' when the value
1009 of `erc-insert-this' is t.
1010 While this hook is run, narrowing is in effect and `current-buffer' is
1011 the buffer where the text got inserted. One possible value to add here
1016 (defcustom erc-insert-post-hook nil
1017 "This hook is called just after `erc-insert-modify-hook'.
1018 At this point, all modifications from prior hook functions are done."
1021 :options '(erc-truncate-buffer
1023 erc-save-buffer-in-logs))
1025 (defcustom erc-send-modify-hook nil
1026 "Sending hook for functions that will change the text's appearance.
1027 This hook is called just after `erc-send-pre-hook' when the values
1028 of `erc-send-this' and `erc-insert-this' are both t.
1029 While this hook is run, narrowing is in effect and `current-buffer' is
1030 the buffer where the text got inserted.
1032 Note that no function in this hook can change the appearance of the
1033 text that is sent. Only changing the sent text's appearance on the
1034 sending user's screen is possible. One possible value to add here
1039 (defcustom erc-send-post-hook nil
1040 "This hook is called just after `erc-send-modify-hook'.
1041 At this point, all modifications from prior hook functions are done.
1042 NOTE: The functions on this hook are called _before_ sending a command
1045 This function is called with narrowing, ala `erc-send-modify-hook'"
1048 :options '(erc-make-read-only))
1050 (defcustom erc-send-completed-hook
1051 (when (featurep 'emacspeak)
1054 (emacspeak-auditory-icon 'select-object)))))
1055 "Hook called after a message has been parsed by ERC.
1057 The single argument to the functions is the unmodified string
1058 which the local user typed."
1061 ;; mode-specific tables
1063 (defvar erc-mode-syntax-table
1064 (let ((syntax-table (make-syntax-table)))
1065 (modify-syntax-entry ?\" ". " syntax-table)
1066 (modify-syntax-entry ?\\ ". " syntax-table)
1067 (modify-syntax-entry ?' "w " syntax-table)
1068 ;; Make dabbrev-expand useful for nick names
1069 (modify-syntax-entry ?< "." syntax-table)
1070 (modify-syntax-entry ?> "." syntax-table)
1072 "Syntax table used while in ERC mode.")
1074 (defvar erc-mode-abbrev-table nil
1075 "Abbrev table used while in ERC mode.")
1076 (define-abbrev-table 'erc-mode-abbrev-table ())
1078 (defvar erc-mode-map
1079 (let ((map (make-sparse-keymap)))
1080 (define-key map "\C-m" 'erc-send-current-line)
1081 (define-key map "\C-a" 'erc-bol)
1082 (define-key map [home] 'erc-bol)
1083 (define-key map "\C-c\C-a" 'erc-bol)
1084 (define-key map "\C-c\C-b" 'erc-iswitchb)
1085 (define-key map "\C-c\C-c" 'erc-toggle-interpret-controls)
1086 (define-key map "\C-c\C-d" 'erc-input-action)
1087 (define-key map "\C-c\C-e" 'erc-toggle-ctcp-autoresponse)
1088 (define-key map "\C-c\C-f" 'erc-toggle-flood-control)
1089 (define-key map "\C-c\C-i" 'erc-invite-only-mode)
1090 (define-key map "\C-c\C-j" 'erc-join-channel)
1091 (define-key map "\C-c\C-n" 'erc-channel-names)
1092 (define-key map "\C-c\C-o" 'erc-get-channel-mode-from-keypress)
1093 (define-key map "\C-c\C-p" 'erc-part-from-channel)
1094 (define-key map "\C-c\C-q" 'erc-quit-server)
1095 (define-key map "\C-c\C-r" 'erc-remove-text-properties-region)
1096 (define-key map "\C-c\C-t" 'erc-set-topic)
1097 (define-key map "\C-c\C-u" 'erc-kill-input)
1098 (define-key map "\M-\t" 'ispell-complete-word)
1099 (define-key map "\t" 'erc-complete-word)
1101 ;; Suppress `font-lock-fontify-block' key binding since it
1102 ;; destroys face properties.
1103 (if (fboundp 'command-remapping)
1104 (define-key map [remap font-lock-fontify-block] 'undefined)
1105 (substitute-key-definition
1106 'font-lock-fontify-block 'undefined map global-map))
1113 ; Honestly, I have a horrible sense of color and the "defaults" below
1114 ; are supposed to be really bad. But colors ARE required in IRC to
1115 ; convey different parts of conversation. If you think you know better
1116 ; defaults - send them to me.
1118 ;; Now colors are a bit nicer, at least to my eyes.
1119 ;; You may still want to change them to better fit your background.-- S.B.
1121 (defgroup erc-faces nil
1125 (defface erc-default-face '((t))
1129 (defface erc-direct-msg-face '((t (:foreground "IndianRed")))
1130 "ERC face used for messages you receive in the main erc buffer."
1133 (defface erc-header-line
1134 '((t (:foreground "grey20" :background "grey90")))
1135 "ERC face used for the header line.
1137 This will only be used if `erc-header-line-face-method' is non-nil."
1140 (defface erc-input-face '((t (:foreground "brown")))
1141 "ERC face used for your input."
1144 (defface erc-prompt-face
1145 '((t (:bold t :foreground "Black" :background "lightBlue2")))
1146 "ERC face for the prompt."
1149 (defface erc-command-indicator-face
1151 "ERC face for the command indicator.
1152 See the variable `erc-command-indicator'."
1155 (defface erc-notice-face '((t (:bold t :foreground "SlateBlue")))
1156 "ERC face for notices."
1159 (defface erc-action-face '((t (:bold t)))
1160 "ERC face for actions generated by /ME."
1163 (defface erc-error-face '((t (:foreground "red")))
1164 "ERC face for errors."
1167 (defface erc-nick-default-face '((t (:bold t)))
1168 "ERC nickname default face."
1171 (defface erc-nick-msg-face '((t (:bold t :foreground "IndianRed")))
1172 "ERC nickname face for private messages."
1175 ;; Debugging support
1177 (defvar erc-log-p nil
1178 "When set to t, generate debug messages in a separate debug buffer.")
1180 (defvar erc-debug-log-file (expand-file-name "ERC.debug")
1181 "Debug log file name.")
1183 (defvar erc-dbuf nil)
1184 (make-variable-buffer-local 'erc-dbuf)
1186 (defmacro define-erc-module (name alias doc enable-body disable-body
1188 "Define a new minor mode using ERC conventions.
1189 Symbol NAME is the name of the module.
1190 Symbol ALIAS is the alias to use, or nil.
1191 DOC is the documentation string to use for the minor mode.
1192 ENABLE-BODY is a list of expressions used to enable the mode.
1193 DISABLE-BODY is a list of expressions used to disable the mode.
1194 If LOCAL-P is non-nil, the mode will be created as a buffer-local
1195 mode. Rather than a global one.
1197 This will define a minor mode called erc-NAME-mode, possibly
1198 an alias erc-ALIAS-mode, as well as the helper functions
1199 erc-NAME-enable, and erc-NAME-disable.
1203 ;;;###autoload (autoload 'erc-replace-mode \"erc-replace\")
1204 (define-erc-module replace nil
1205 \"This mode replaces incoming text according to `erc-replace-alist'.\"
1206 ((add-hook 'erc-insert-modify-hook
1207 'erc-replace-insert))
1208 ((remove-hook 'erc-insert-modify-hook
1209 'erc-replace-insert)))"
1210 (let* ((sn (symbol-name name))
1211 (mode (intern (format "erc-%s-mode" (downcase sn))))
1212 (group (intern (format "erc-%s" (downcase sn))))
1213 (enable (intern (format "erc-%s-enable" (downcase sn))))
1214 (disable (intern (format "erc-%s-disable" (downcase sn)))))
1216 (erc-define-minor-mode
1218 ,(format "Toggle ERC %S mode.
1219 With arg, turn ERC %S mode on if and only if arg is positive.
1222 :global ,(not local-p) :group (quote ,group)
1227 ,(format "Enable ERC %S mode."
1230 (add-to-list 'erc-modules (quote ,name))
1234 ,(format "Disable ERC %S mode."
1237 (setq erc-modules (delq (quote ,name) erc-modules))
1240 ,(when (and alias (not (eq name alias)))
1244 (format "erc-%s-mode"
1245 (downcase (symbol-name alias)))))
1248 ;; For find-function and find-variable.
1249 (put ',mode 'definition-name ',name)
1250 (put ',enable 'definition-name ',name)
1251 (put ',disable 'definition-name ',name))))
1253 (put 'define-erc-module 'doc-string-elt 3)
1255 (defun erc-once-with-server-event (event &rest forms)
1256 "Execute FORMS the next time EVENT occurs in the `current-buffer'.
1258 You should make sure that `current-buffer' is a server buffer.
1260 This function temporarily adds a function to EVENT's hook to
1261 execute FORMS. After FORMS are run, the function is removed from
1262 EVENT's hook. The last expression of FORMS should be either nil
1263 or t. nil indicates that the other functions on EVENT's hook
1264 should be run too, and t indicates that other functions should
1267 Please be sure to use this function in server-buffers. In
1268 channel-buffers it may not work at all, as it uses the LOCAL
1269 argument of `add-hook' and `remove-hook' to ensure multiserver
1271 (unless (erc-server-buffer-p)
1273 "You should only run `erc-once-with-server-event' in a server buffer"))
1274 (let ((fun (erc-gensym))
1275 (hook (erc-get-hook event)))
1276 (put fun 'erc-original-buffer (current-buffer))
1277 (fset fun `(lambda (proc parsed)
1278 (with-current-buffer (get ',fun 'erc-original-buffer)
1279 (remove-hook ',hook ',fun t))
1282 (add-hook hook fun nil t)
1285 (defun erc-once-with-server-event-global (event &rest forms)
1286 "Execute FORMS the next time EVENT occurs in any server buffer.
1288 This function temporarily prepends a function to EVENT's hook to
1289 execute FORMS. After FORMS are run, the function is removed from
1290 EVENT's hook. The last expression of FORMS should be either nil
1291 or t. nil indicates that the other functions on EVENT's hook
1292 should be run too, and t indicates that other functions should
1295 When FORMS execute, the current buffer is the server buffer associated with the
1296 connection over which the data was received that triggered EVENT."
1297 (let ((fun (erc-gensym))
1298 (hook (erc-get-hook event)))
1299 (fset fun `(lambda (proc parsed)
1300 (remove-hook ',hook ',fun)
1303 (add-hook hook fun nil nil)
1306 (defmacro erc-log (string)
1307 "Logs STRING if logging is on (see `erc-log-p')."
1309 (erc-log-aux ,string)))
1311 (defun erc-server-buffer ()
1312 "Return the server buffer for the current buffer's process.
1313 The buffer-local variable `erc-server-process' is used to find
1314 the process buffer."
1315 (and (erc-server-buffer-live-p)
1316 (process-buffer erc-server-process)))
1318 (defun erc-server-buffer-live-p ()
1319 "Return t if the buffer associated with `erc-server-process'
1320 has not been killed."
1321 (and (processp erc-server-process)
1322 (buffer-live-p (process-buffer erc-server-process))))
1324 (defun erc-server-buffer-p (&optional buffer)
1325 "Return non-nil if argument BUFFER is an ERC server buffer.
1327 If BUFFER is nil, the current buffer is used."
1328 (with-current-buffer (or buffer (current-buffer))
1329 (and (eq major-mode 'erc-mode)
1330 (null (erc-default-target)))))
1332 (defun erc-query-buffer-p (&optional buffer)
1333 "Return non-nil if BUFFER is an ERC query buffer.
1334 If BUFFER is nil, the current buffer is used."
1335 (with-current-buffer (or buffer (current-buffer))
1336 (let ((target (erc-default-target)))
1337 (and (eq major-mode 'erc-mode)
1339 (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
1341 (defun erc-ison-p (nick)
1342 "Return non-nil if NICK is online."
1343 (interactive "sNick: ")
1344 (with-current-buffer (erc-server-buffer)
1345 (let ((erc-online-p 'unknown))
1346 (erc-once-with-server-event
1348 `(let ((ison (split-string (aref parsed 3))))
1349 (setq erc-online-p (car (erc-member-ignore-case ,nick ison)))
1351 (erc-server-send (format "ISON %s" nick))
1352 (while (eq erc-online-p 'unknown) (accept-process-output))
1354 (message "%s is %sonline"
1355 (or erc-online-p nick)
1356 (if erc-online-p "" "not "))
1359 (defun erc-log-aux (string)
1360 "Do the debug logging of STRING."
1361 (let ((cb (current-buffer))
1364 (session-buffer (erc-server-buffer)))
1367 (set-buffer session-buffer)
1368 (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
1370 (setq erc-dbuf (get-buffer-create
1371 (concat "*ERC-DEBUG: "
1372 erc-session-server "*")))))
1373 (set-buffer erc-dbuf)
1374 (setq point (point))
1375 (setq was-eob (eobp))
1376 (goto-char (point-max))
1377 (insert (concat "** " string "\n"))
1378 (if was-eob (goto-char (point-max))
1381 (message "ERC: ** %s" string))))
1383 ;; Last active buffer, to print server messages in the right place
1385 (defvar erc-active-buffer nil
1386 "The current active buffer, the one where the user typed the last command.
1387 Defaults to the server buffer, and should only be set in the
1389 (make-variable-buffer-local 'erc-active-buffer)
1391 (defun erc-active-buffer ()
1392 "Return the value of `erc-active-buffer' for the current server.
1393 Defaults to the server buffer."
1394 (with-current-buffer (erc-server-buffer)
1395 (if (buffer-live-p erc-active-buffer)
1397 (setq erc-active-buffer (current-buffer)))))
1399 (defun erc-set-active-buffer (buffer)
1400 "Set the value of `erc-active-buffer' to BUFFER."
1401 (cond ((erc-server-buffer)
1402 (with-current-buffer (erc-server-buffer)
1403 (setq erc-active-buffer buffer)))
1404 (t (setq erc-active-buffer buffer))))
1406 ;; Mode activation routines
1409 "Major mode for Emacs IRC.
1414 Turning on `erc-mode' runs the hook `erc-mode-hook'."
1415 (kill-all-local-variables)
1416 (use-local-map erc-mode-map)
1417 (setq mode-name "ERC"
1418 major-mode 'erc-mode
1419 local-abbrev-table erc-mode-abbrev-table)
1420 (set-syntax-table erc-mode-syntax-table)
1421 (when (boundp 'next-line-add-newlines)
1422 (set (make-local-variable 'next-line-add-newlines) nil))
1423 (setq line-move-ignore-invisible t)
1424 (set (make-local-variable 'paragraph-separate)
1425 (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
1426 (set (make-local-variable 'paragraph-start)
1427 (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
1428 ;; Run the mode hooks
1429 (run-hooks 'erc-mode-hook))
1433 (defconst erc-default-server "irc.freenode.net"
1434 "IRC server to use if it cannot be detected otherwise.")
1436 (defconst erc-default-port "ircd"
1437 "IRC port to use if it cannot be detected otherwise.")
1439 (defcustom erc-join-buffer 'buffer
1440 "Determines how to display the newly created IRC buffer.
1441 'window - in another window,
1442 'window-noselect - in another window, but don't select that one,
1443 'frame - in another frame,
1444 'bury - bury it in a new buffer,
1445 any other value - in place of the current buffer"
1447 :type '(choice (const window)
1448 (const window-noselect)
1453 (defcustom erc-frame-alist nil
1454 "*Alist of frame parameters for creating erc frames.
1455 A value of `nil means to use `default-frame-alist'."
1457 :type '(repeat (cons :format "%v"
1458 (symbol :tag "Parameter")
1459 (sexp :tag "Value"))))
1461 (defcustom erc-frame-dedicated-flag nil
1462 "*Non-nil means the erc frames are dedicated to that buffer.
1463 This only has effect when `erc-join-buffer' is set to `frame'."
1467 (defun erc-channel-p (channel)
1468 "Return non-nil if CHANNEL seems to be an IRC channel name."
1469 (cond ((stringp channel)
1470 (memq (aref channel 0) '(?# ?& ?+ ?!)))
1471 ((and (bufferp channel) (buffer-live-p channel))
1472 (with-current-buffer channel
1473 (erc-channel-p (erc-default-target))))
1476 (defcustom erc-reuse-buffers t
1477 "*If nil, create new buffers on joining a channel/query.
1478 If non-nil, a new buffer will only be created when you join
1479 channels with same names on different servers, or have query buffers
1480 open with nicks of the same name on different servers. Otherwise,
1481 the existing buffers will be reused."
1485 (defun erc-normalize-port (port)
1486 "Normalize the port specification PORT to integer form.
1487 PORT may be an integer, a string or a symbol. If it is a string or a
1488 symbol, it may have these values:
1492 * ircd-dalnet -> 7000"
1495 (erc-normalize-port (symbol-name port)))
1497 (let ((port-nr (string-to-number port)))
1501 ((string-equal port "irc")
1503 ((string-equal port "ircs")
1505 ((string-equal port "ircd")
1507 ((string-equal port "ircd-dalnet")
1516 (defun erc-port-equal (a b)
1517 "Check whether ports A and B are equal."
1518 (= (erc-normalize-port a) (erc-normalize-port b)))
1520 (defun erc-generate-new-buffer-name (server port target &optional proc)
1521 "Create a new buffer name based on the arguments."
1522 (when (numberp port) (setq port (number-to-string port)))
1523 (let* ((buf-name (or target
1524 (or (let ((name (concat server ":" port)))
1525 (when (> (length name) 1)
1527 ; This fallback should in fact never happen
1528 "*erc-server-buffer*"))))
1529 ;; Reuse existing buffers, but not if the buffer is a connected server
1530 ;; buffer and not if its associated with a different server than the
1531 ;; current ERC buffer.
1532 (if (and erc-reuse-buffers
1533 (get-buffer buf-name)
1535 (with-current-buffer (get-buffer buf-name)
1536 (and (erc-server-buffer-p)
1537 (not erc-server-connected))))
1538 (with-current-buffer (get-buffer buf-name)
1539 (and (string= erc-session-server server)
1540 (erc-port-equal erc-session-port port))))
1542 (generate-new-buffer-name buf-name))))
1544 (defun erc-get-buffer-create (server port target &optional proc)
1545 "Create a new buffer based on the arguments."
1546 (get-buffer-create (erc-generate-new-buffer-name server port target proc)))
1549 (defun erc-member-ignore-case (string list)
1550 "Return non-nil if STRING is a member of LIST.
1552 All strings are compared according to IRC protocol case rules, see
1554 (setq string (erc-downcase string))
1557 (if (string= string (erc-downcase (car list)))
1558 (throw 'result list) (setq list (cdr list))))))
1560 (defmacro erc-with-buffer (spec &rest body)
1561 "Execute BODY in the buffer associated with SPEC.
1563 SPEC should have the form
1567 If TARGET is a buffer, use it. Otherwise, use the buffer
1568 matching TARGET in the process specified by PROCESS.
1570 If PROCESS is nil, use the current `erc-server-process'
1571 See `erc-get-buffer' for details.
1573 See also `with-current-buffer'.
1575 \(fn (TARGET [PROCESS]) BODY...)"
1576 (let ((buf (erc-gensym))
1578 (target (erc-gensym))
1579 (process (erc-gensym)))
1580 `(let* ((,target ,(car spec))
1581 (,process ,(cadr spec))
1582 (,buf (if (bufferp ,target)
1584 (let ((,proc (or ,process
1585 (and (processp erc-server-process)
1586 erc-server-process))))
1587 (if (and ,target ,proc)
1588 (erc-get-buffer ,target ,proc))))))
1590 (with-current-buffer ,buf
1592 (put 'erc-with-buffer 'lisp-indent-function 1)
1593 (put 'erc-with-buffer 'edebug-form-spec '((form &optional form) body))
1595 (defun erc-get-buffer (target &optional proc)
1596 "Return the buffer matching TARGET in the process PROC.
1597 If PROC is not supplied, all processes are searched."
1598 (let ((downcased-target (erc-downcase target)))
1602 (let ((current (erc-default-target)))
1603 (and (stringp current)
1604 (string-equal downcased-target (erc-downcase current))
1605 (throw 'buffer (current-buffer)))))
1608 (defun erc-buffer-filter (predicate &optional proc)
1609 "Return a list of `erc-mode' buffers matching certain criteria.
1610 PREDICATE is a function executed with each buffer, if it returns t, that buffer
1611 is considered a valid match.
1613 PROC is either an `erc-server-process', identifying a certain
1614 server connection, or nil which means all open connections."
1618 (mapcar (lambda (buf)
1619 (when (buffer-live-p buf)
1620 (with-current-buffer buf
1621 (and (eq major-mode 'erc-mode)
1623 (eq proc erc-server-process))
1628 (defun erc-buffer-list (&optional predicate proc)
1629 "Return a list of ERC buffers.
1630 PREDICATE is a function which executes with every buffer satisfying
1631 the predicate. If PREDICATE is passed as nil, return a list of all ERC
1632 buffers. If PROC is given, the buffers local variable `erc-server-process'
1633 needs to match PROC."
1635 (setq predicate (lambda () t)))
1636 (erc-buffer-filter predicate proc))
1638 (defmacro erc-with-all-buffers-of-server (process pred &rest forms)
1639 "Execute FORMS in all buffers which have same process as this server.
1640 FORMS will be evaluated in all buffers having the process PROCESS and
1641 where PRED matches or in all buffers of the server process if PRED is
1643 ;; Make the evaluation have the correct order
1644 (let ((pre (erc-gensym))
1646 `(let ((,pro ,process)
1648 (mapcar (lambda (buffer)
1649 (with-current-buffer buffer
1651 (erc-buffer-list ,pre
1653 (put 'erc-with-all-buffers-of-server 'lisp-indent-function 1)
1654 (put 'erc-with-all-buffers-of-server 'edebug-form-spec '(form form body))
1656 (defun erc-iswitchb (&optional arg)
1657 "Use `iswitchb-read-buffer' to prompt for a ERC buffer to switch to.
1658 When invoked with prefix argument, use all erc buffers. Without prefix
1659 ARG, allow only buffers related to same session server.
1660 If `erc-track-mode' is in enabled, put the last element of
1661 `erc-modified-channels-alist' in front of the buffer list.
1663 Due to some yet unresolved reason, global function `iswitchb-mode'
1664 needs to be active for this function to work."
1667 (require 'iswitchb))
1668 (let ((iswitchb-make-buflist-hook
1670 (setq iswitchb-temp-buflist
1671 (mapcar 'buffer-name
1674 (when (and arg (boundp 'erc-server-process))
1675 erc-server-process)))))))
1677 (iswitchb-read-buffer
1679 (if (boundp 'erc-modified-channels-alist)
1680 (buffer-name (caar (last erc-modified-channels-alist)))
1684 (defun erc-channel-list (proc)
1685 "Return a list of channel buffers.
1686 PROC is the process for the server connection. If PROC is nil, return
1687 all channel buffers on all servers."
1690 (and (erc-default-target)
1691 (erc-channel-p (erc-default-target))))
1694 (defun erc-buffer-list-with-nick (nick proc)
1695 "Return buffers containing NICK in the `erc-channel-users' list."
1696 (with-current-buffer (process-buffer proc)
1697 (let ((user (gethash (erc-downcase nick) erc-server-users)))
1699 (erc-server-user-buffers user)
1702 ;; Some local variables
1704 (defvar erc-default-recipients nil
1705 "List of default recipients of the current buffer.")
1706 (make-variable-buffer-local 'erc-default-recipients)
1708 (defvar erc-session-user-full-name nil
1709 "Full name of the user on the current server.")
1710 (make-variable-buffer-local 'erc-session-user-full-name)
1712 (defvar erc-channel-user-limit nil
1713 "Limit of users per channel.")
1714 (make-variable-buffer-local 'erc-channel-user-limit)
1716 (defvar erc-channel-key nil
1717 "Key needed to join channel.")
1718 (make-variable-buffer-local 'erc-channel-key)
1720 (defvar erc-invitation nil
1721 "Last invitation channel.")
1722 (make-variable-buffer-local 'erc-invitation)
1724 (defvar erc-away nil
1725 "Non-nil indicates that we are away.")
1726 (make-variable-buffer-local 'erc-away)
1728 (defvar erc-channel-list nil
1729 "Server channel list.")
1730 (make-variable-buffer-local 'erc-channel-list)
1732 (defvar erc-bad-nick nil
1733 "Non-nil indicates that we got a `nick in use' error while connecting.")
1734 (make-variable-buffer-local 'erc-bad-nick)
1736 (defvar erc-logged-in nil
1737 "Non-nil indicates that we are logged in.")
1738 (make-variable-buffer-local 'erc-logged-in)
1740 (defvar erc-default-nicks nil
1741 "The local copy of `erc-nick' - the list of nicks to choose from.")
1742 (make-variable-buffer-local 'erc-default-nicks)
1744 (defvar erc-nick-change-attempt-count 0
1745 "Used to keep track of how many times an attempt at changing nick is made.")
1746 (make-variable-buffer-local 'erc-nick-change-attempt-count)
1748 (defun erc-migrate-modules (mods)
1749 "Migrate old names of ERC modules to new ones."
1750 ;; modify `transforms' to specify what needs to be changed
1751 ;; each item is in the format '(old . new)
1752 (let ((transforms '((pcomplete . completion))))
1754 (mapcar (lambda (m) (or (cdr (assoc m transforms)) m))
1757 (defcustom erc-modules '(netsplit fill button match track completion readonly
1758 ring autojoin noncommands irccontrols
1760 "A list of modules which erc should enable.
1761 If you set the value of this without using `customize' remember to call
1762 \(erc-update-modules) after you change it. When using `customize', modules
1763 removed from the list will be disabled."
1765 ;; replace outdated names with their newer equivalents
1766 (erc-migrate-modules (symbol-value sym)))
1767 :set (lambda (sym val)
1768 ;; disable modules which have just been removed
1769 (when (and (boundp 'erc-modules) erc-modules val)
1770 (dolist (module erc-modules)
1771 (unless (member module val)
1772 (let ((f (intern-soft (format "erc-%s-mode" module))))
1773 (when (and (fboundp f) (boundp f) (symbol-value f))
1774 (message "Disabling `erc-%s'" module)
1777 ;; this test is for the case where erc hasn't been loaded yet
1778 (when (fboundp 'erc-update-modules)
1779 (erc-update-modules)))
1783 (const :tag "Set away status automatically" autoaway)
1784 (const :tag "Join channels automatically" autojoin)
1785 (const :tag "Buttonize URLs, nicknames, and other text" button)
1786 (const :tag "Wrap long lines" fill)
1787 (const :tag "Launch an identd server on port 8113" identd)
1788 (const :tag "Highlight or remove IRC control characters"
1790 (const :tag "List channels in a separate buffer" list)
1791 (const :tag "Save buffers in logs" log)
1792 (const :tag "Highlight pals, fools, and other keywords" match)
1793 (const :tag "Detect netsplits" netsplit)
1794 (const :tag "Don't display non-IRC commands after evaluation"
1797 "Notify when the online status of certain users changes"
1799 (const :tag "Complete nicknames and commands (programmable)"
1801 (const :tag "Complete nicknames and commands (old)" hecomplete)
1802 (const :tag "Process CTCP PAGE requests from IRC" page)
1803 (const :tag "Make displayed lines read-only" readonly)
1804 (const :tag "Replace text in messages" replace)
1805 (const :tag "Enable an input history" ring)
1806 (const :tag "Scroll to the bottom of the buffer" scrolltobottom)
1807 (const :tag "Identify to Nickserv (IRC Services) automatically"
1809 (const :tag "Convert smileys to pretty icons" smiley)
1810 (const :tag "Play sounds when you receive CTCP SOUND requests"
1812 (const :tag "Add timestamps to messages" stamp)
1813 (const :tag "Check spelling" spelling)
1814 (const :tag "Track channel activity in the mode-line" track)
1815 (const :tag "Truncate buffers to a certain size" truncate)
1816 (const :tag "Translate morse code in messages" unmorse)
1817 (repeat :tag "Others" :inline t symbol))
1820 (defun erc-update-modules ()
1821 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1823 (dolist (mod erc-modules)
1824 (setq req (concat "erc-" (symbol-name mod)))
1826 ;; yuck. perhaps we should bring the filenames into sync?
1827 ((string= req "erc-completion")
1828 (setq req "erc-pcomplete"))
1829 ((string= req "erc-pcomplete")
1830 (setq mod 'completion))
1831 ((string= req "erc-autojoin")
1832 (setq req "erc-join")))
1834 (require (intern req))
1836 (funcall (or (intern-soft (concat "erc-" (symbol-name mod) "-mode"))
1837 (error "`%s' is not a known ERC module" mod))
1840 (defun erc-setup-buffer (buffer)
1841 "Consults `erc-join-buffer' to find out how to display `BUFFER'."
1842 (cond ((eq erc-join-buffer 'window)
1843 (if (active-minibuffer-window)
1844 (display-buffer buffer)
1845 (switch-to-buffer-other-window buffer)))
1846 ((eq erc-join-buffer 'window-noselect)
1847 (display-buffer buffer))
1848 ((eq erc-join-buffer 'bury)
1850 ((eq erc-join-buffer 'frame)
1851 (funcall '(lambda (frame)
1853 (select-frame frame))
1854 (make-frame (or erc-frame-alist
1855 default-frame-alist)))
1856 (switch-to-buffer buffer)
1857 (when erc-frame-dedicated-flag
1858 (set-window-dedicated-p (selected-window) t)))
1860 (if (active-minibuffer-window)
1861 (display-buffer buffer)
1862 (switch-to-buffer buffer)))))
1864 (defun erc (&optional server port nick full-name
1865 connect passwd tgt-list channel process)
1866 "ERC is a powerful, modular, and extensible IRC client.
1868 Connect to SERVER on PORT as NICK with FULL-NAME.
1870 If CONNECT is non-nil, connect to the server. Otherwise assume
1871 already connected and just create a separate buffer for the new
1874 Use PASSWD as user password on the server. If TGT-LIST is
1875 non-nil, use it to initialise `erc-default-recipients'.
1877 Returns the buffer for the given server or channel."
1878 (let ((server-announced-name (when (and (boundp 'erc-session-server)
1879 (string= server erc-session-server))
1880 erc-server-announced-name))
1881 (connected-p (unless connect erc-server-connected))
1882 (buffer (erc-get-buffer-create server port channel))
1883 (old-buffer (current-buffer))
1885 (erc-update-modules)
1888 (setq erc-server-announced-name server-announced-name)
1889 (setq erc-server-connected connected-p)
1890 ;; connection parameters
1891 (setq erc-server-process process)
1892 (setq erc-insert-marker (make-marker))
1893 (setq erc-input-marker (make-marker))
1894 ;; go to the end of the buffer and open a new line
1895 ;; (the buffer may have existed)
1896 (goto-char (point-max))
1898 (when (get-text-property (point) 'erc-prompt)
1899 (setq continued-session t)
1900 (set-marker erc-input-marker
1901 (or (next-single-property-change (point) 'erc-prompt)
1903 (unless continued-session
1904 (goto-char (point-max))
1906 (set-marker erc-insert-marker (point))
1907 ;; stack of default recipients
1908 (setq erc-default-recipients tgt-list)
1909 (setq erc-server-current-nick nil)
1910 ;; Initialize erc-server-users and erc-channel-users
1912 (progn ;; server buffer
1913 (setq erc-server-users
1914 (make-hash-table :test 'equal))
1915 (setq erc-channel-users nil))
1916 (progn ;; target buffer
1917 (setq erc-server-users nil)
1918 (setq erc-channel-users
1919 (make-hash-table :test 'equal))))
1920 ;; clear last incomplete line read
1921 (setq erc-server-filter-data nil)
1922 (setq erc-channel-topic "")
1923 ;; limit on the number of users on the channel (mode +l)
1924 (setq erc-channel-user-limit nil)
1925 (setq erc-channel-key nil)
1926 ;; last active buffer, defaults to this one
1927 (erc-set-active-buffer buffer)
1928 ;; last invitation channel
1929 (setq erc-invitation nil)
1931 ;; Should only be used in session-buffers
1932 (setq erc-away (let ((serverbuf (erc-server-buffer)))
1933 (and serverbuf (with-current-buffer serverbuf erc-away))))
1934 ;; Server channel list
1935 (setq erc-channel-list ())
1936 ;; login-time 'nick in use' error
1937 (setq erc-bad-nick nil)
1938 ;; whether we have logged in
1939 (setq erc-logged-in nil)
1940 ;; The local copy of `erc-nick' - the list of nicks to choose
1941 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
1943 (setq erc-session-password passwd)
1944 ;; debug output buffer
1947 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
1948 (erc-determine-parameters server port nick full-name)
1950 ;; Saving log file on exit
1951 (run-hooks 'erc-connect-pre-hook)
1954 (erc-server-connect erc-session-server erc-session-port))
1955 (erc-update-mode-line)
1956 (set-marker erc-insert-marker (point))
1957 (unless continued-session
1958 (goto-char (point-max))
1960 (set-marker (process-mark erc-server-process) (point))
1961 (if continued-session
1962 (goto-char (point-max))
1963 (set-marker erc-insert-marker (point))
1964 (erc-display-prompt)
1965 (goto-char (point-max)))
1967 ;; Now display the buffer in a window as per user wishes.
1968 (unless (eq buffer old-buffer)
1970 ;; we can't log to debug buffer, it may not exist yet
1971 (message "erc: old buffer %s, switching to %s"
1973 (erc-setup-buffer buffer))
1977 (defun erc-initialize-log-marker ()
1978 "Initialize the `erc-last-saved-position' marker to a sensible position."
1979 (setq erc-last-saved-position (make-marker))
1980 (move-marker erc-last-saved-position
1981 (1- (marker-position erc-insert-marker))))
1983 ;; interactive startup
1985 (defvar erc-server-history-list nil
1986 "IRC server interactive selection history list.")
1988 (defvar erc-nick-history-list nil
1989 "Nickname interactive selection history list.")
1991 (defun erc-already-logged-in (server port nick)
1992 "Return the buffers corresponding to a NICK on PORT of a session SERVER.
1993 This is determined by looking for the appropriate buffer and checking
1994 whether the connection is still alive.
1995 If no buffer matches, return nil."
1998 (and (erc-server-process-alive)
1999 (string= erc-session-server server)
2000 (erc-port-equal erc-session-port port)
2001 (erc-current-nick-p nick)))))
2003 (if (not (fboundp 'read-passwd))
2004 (defun read-passwd (prompt)
2005 "Substitute for `read-passwd' in early emacsen."
2006 (read-from-minibuffer prompt)))
2008 (defcustom erc-before-connect nil
2009 "Hook called before connecting to a server.
2010 This hook gets executed before `erc-select' actually invokes `erc-mode'
2011 with your input data. The functions in here get called with three
2012 parameters, SERVER, PORT and NICK."
2016 (defcustom erc-after-connect nil
2017 "Hook called after connecting to a server.
2018 This hook gets executed when an end of MOTD has been received. All
2019 functions in here get called with the parameters SERVER and NICK."
2024 (defun erc-select-read-args ()
2025 "Prompt the user for values of nick, server, port, and password."
2026 (let (user-input server port nick passwd)
2027 (setq user-input (read-from-minibuffer
2029 (erc-compute-server) nil nil 'erc-server-history-list))
2031 (if (string-match "\\(.*\\):\\(.*\\)\\'" user-input)
2032 (setq port (erc-string-to-port (match-string 2 user-input))
2033 user-input (match-string 1 user-input))
2035 (erc-string-to-port (read-from-minibuffer
2036 "IRC port: " (erc-port-to-string
2037 (erc-compute-port))))))
2039 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
2040 (setq nick (match-string 1 user-input)
2041 user-input (match-string 2 user-input))
2043 (if (erc-already-logged-in server port nick)
2044 (read-from-minibuffer
2045 (erc-format-message 'nick-in-use ?n nick)
2047 nil nil 'erc-nick-history-list)
2048 (read-from-minibuffer
2049 "Nickname: " (erc-compute-nick nick)
2050 nil nil 'erc-nick-history-list))))
2052 (setq server user-input)
2054 (setq passwd (if erc-prompt-for-password
2055 (if (and erc-password
2056 (y-or-n-p "Use the default password? "))
2058 (read-passwd "Password: "))
2060 (when (and passwd (string= "" passwd))
2063 (while (erc-already-logged-in server port nick)
2064 ;; hmm, this is a problem when using multiple connections to a bnc
2065 ;; with the same nick. Currently this code prevents using more than one
2066 ;; bnc with the same nick. actually it would be nice to have
2067 ;; bncs transparent, so that erc-compute-buffer-name displays
2068 ;; the server one is connected to.
2069 (setq nick (read-from-minibuffer
2070 (erc-format-message 'nick-in-use ?n nick)
2072 nil nil 'erc-nick-history-list)))
2073 (list :server server :port port :nick nick :password passwd)))
2076 (defun* erc-select (&key (server (erc-compute-server))
2077 (port (erc-compute-port))
2078 (nick (erc-compute-nick))
2080 (full-name (erc-compute-full-name)))
2081 "Select connection parameters and run ERC.
2082 Non-interactively, it takes keyword arguments
2083 (server (erc-compute-server))
2084 (port (erc-compute-port))
2085 (nick (erc-compute-nick))
2087 (full-name (erc-compute-full-name)))
2089 That is, if called with
2091 (erc-select :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
2093 server and full-name will be set to those values, whereas
2094 `erc-compute-port', `erc-compute-nick' and `erc-compute-full-name' will
2095 be invoked for the values of the other parameters."
2096 (interactive (erc-select-read-args))
2098 (run-hook-with-args 'erc-before-connect server port nick)
2099 (erc server port nick erc-user-full-name t password))
2102 (defun erc-select-ssl (&rest r)
2103 "Interactively select SSL connection parameters and run ERC.
2104 Arguments are as to erc-select."
2105 (interactive (erc-select-read-args))
2106 (let ((erc-server-connect-function 'erc-open-ssl-stream))
2107 (apply 'erc-select r)))
2109 (defun erc-open-ssl-stream (name buffer host port)
2110 "Open an SSL stream to an IRC server.
2111 The process will be given the name NAME, its target buffer will be
2112 BUFFER. HOST and PORT specify the connection target."
2113 (when (require 'tls)
2114 (let ((proc (open-tls-stream name buffer host port)))
2115 ;; Ugly hack, but it works for now. Problem is it is
2116 ;; very hard to detect when ssl is established, because s_client
2117 ;; doesn't give any CONNECTIONESTABLISHED kind of message, and
2118 ;; most IRC servers send nothing and wait for you to identify.
2119 ;; Disabled when switching to tls.el -- jas
2123 ;;; Debugging the protocol
2125 (defvar erc-debug-irc-protocol nil
2126 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2128 The buffer is created if it doesn't exist.
2130 NOTE: If this variable is non-nil, and you kill the the only
2131 visible \"*erc-protocol*\" buffer, it will be recreated shortly,
2132 but you won't see it.
2134 WARNING: Do not set this variable directly! Instead, use the
2135 function `erc-toggle-debug-irc-protocol' to toggle its value.")
2137 (defun erc-log-irc-protocol (string &optional outbound)
2138 "Append STRING to the buffer *erc-protocol*.
2140 This only has any effect if `erc-debug-irc-protocol' is non-nil.
2142 The buffer is created if it doesn't exist.
2144 If OUTBOUND is non-nil, STRING is being sent to the IRC server
2145 and appears in erc-input-face in the buffer."
2146 (when erc-debug-irc-protocol
2147 (let ((network-name (or (ignore-errors (erc-network-name))
2149 (with-current-buffer (get-buffer-create "*erc-protocol*")
2151 (goto-char (point-max))
2152 (let ((inhibit-read-only t))
2153 (insert (if (not outbound)
2154 ;; Cope with the fact that string might
2155 ;; contain multiple lines of text.
2156 (let ((lines (delete "" (split-string string
2159 (dolist (line lines)
2160 (setq result (concat result network-name
2164 (concat network-name " >> " string
2167 (1- (length string))))
2169 'face 'erc-input-face)))))
2170 (let ((orig-win (selected-window))
2171 (debug-buffer-window (get-buffer-window (current-buffer) t)))
2172 (when debug-buffer-window
2173 (select-window debug-buffer-window)
2174 (when (= 1 (count-lines (point) (point-max)))
2175 (goto-char (point-max))
2177 (select-window orig-win)))))))
2179 (defun erc-toggle-debug-irc-protocol (&optional arg)
2180 "Toggle the value of `erc-debug-irc-protocol'.
2182 If ARG is non-nil, show the *erc-protocol* buffer."
2184 (let* ((buf (get-buffer-create "*erc-protocol*")))
2185 (with-current-buffer buf
2186 (erc-view-mode-enter 1)
2187 (when (null (current-local-map))
2188 (let ((inhibit-read-only t))
2189 (insert (erc-make-notice "This buffer displays all IRC protocol traffic exchanged with each server.\n"))
2190 (insert (erc-make-notice "Kill this buffer to terminate protocol logging.\n\n")))
2191 (use-local-map (make-sparse-keymap))
2192 (local-set-key (kbd "RET") 'erc-toggle-debug-irc-protocol))
2193 (add-hook 'kill-buffer-hook
2194 #'(lambda () (setq erc-debug-irc-protocol nil))
2196 (goto-char (point-max))
2197 (let ((inhibit-read-only t))
2198 (insert (erc-make-notice
2199 (format "IRC protocol logging %s at %s -- Press ENTER to toggle logging.\n"
2200 (if erc-debug-irc-protocol "disabled" "enabled")
2201 (current-time-string))))))
2202 (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
2204 (not (get-buffer-window "*erc-protocol*" t)))
2205 (display-buffer buf t))
2206 (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
2207 (if erc-debug-irc-protocol "enabled" "disabled"))))
2213 (defun erc-send-action (tgt str &optional force)
2214 "Send CTCP ACTION information described by STR to TGT."
2215 (erc-send-ctcp-message tgt (format "ACTION %s" str) force)
2216 (erc-display-message
2217 nil 'input (current-buffer)
2218 'ACTION ?n (erc-current-nick) ?a str ?u "" ?h ""))
2220 ;; Display interface
2222 (defun erc-string-invisible-p (string)
2223 "Check whether STRING is invisible or not.
2224 I.e. any char in it has the `invisible' property set."
2225 (text-property-any 0 (length string) 'invisible t string))
2227 (defun erc-display-line-1 (string buffer)
2228 "Display STRING in `erc-mode' BUFFER.
2229 Auxiliary function used in `erc-display-line'. The line gets filtered to
2230 interpret the control characters. Then, `erc-insert-pre-hook' gets called.
2231 If `erc-insert-this' is still t, STRING gets inserted into the buffer.
2232 Afterwards, `erc-insert-modify' and `erc-insert-post-hook' get called.
2233 If STRING is nil, the function does nothing."
2236 (set-buffer (or buffer (process-buffer erc-server-process)))
2237 (let ((insert-position (or (marker-position erc-insert-marker)
2239 (let ((string string) ;; FIXME! Can this be removed?
2240 (buffer-undo-list t)
2241 (inhibit-read-only t))
2242 (unless (string-match "\n$" string)
2243 (setq string (concat string "\n"))
2244 (when (erc-string-invisible-p string)
2245 (erc-put-text-properties 0 (length string) string
2246 '(invisible intangible))))
2247 (erc-log (concat "erc-display-line: " string
2248 (format "(%S)" string) " in buffer "
2249 (format "%s" buffer)))
2250 (setq erc-insert-this t)
2251 (run-hook-with-args 'erc-insert-pre-hook string)
2252 (if (null erc-insert-this)
2253 ;; Leave erc-insert-this set to t as much as possible. Fran
2254 ;; Litterio <franl> has seen erc-insert-this set to nil while
2255 ;; erc-send-pre-hook is running, which should never happen. This
2257 (setq erc-insert-this t)
2258 (save-excursion ;; to restore point in the new buffer
2261 (goto-char insert-position)
2262 (insert-before-markers string)
2263 ;; run insertion hook, with point at restored location
2265 (narrow-to-region insert-position (point))
2266 (run-hooks 'erc-insert-modify-hook)
2267 (run-hooks 'erc-insert-post-hook))))))
2268 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2270 insert-position))))))
2272 (defun erc-update-undo-list (shift)
2273 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2274 (unless (or (zerop shift) (atom buffer-undo-list))
2275 (let ((list buffer-undo-list) elt)
2277 (setq elt (car list))
2278 (cond ((integerp elt) ; POSITION
2279 (incf (car list) shift))
2280 ((or (atom elt) ; nil, EXTENT
2281 ;; (eq t (car elt)) ; (t HIGH . LOW)
2282 (markerp (car elt))) ; (MARKER . DISTANCE)
2284 ((integerp (car elt)) ; (BEGIN . END)
2285 (incf (car elt) shift)
2286 (incf (cdr elt) shift))
2287 ((stringp (car elt)) ; (TEXT . POSITION)
2288 (incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
2289 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2290 (let ((cons (nthcdr 3 elt)))
2291 (incf (car cons) shift)
2292 (incf (cdr cons) shift)))
2293 ((and (featurep 'xemacs)
2294 (extentp (car elt))) ; (EXTENT START END)
2295 (incf (nth 1 elt) shift)
2296 (incf (nth 2 elt) shift)))
2297 (setq list (cdr list))))))
2299 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2300 "Regexp which matches all legal characters in a IRC nickname.")
2302 (defun erc-is-valid-nick-p (nick)
2303 "Check if NICK is a valid IRC nickname."
2304 (string-match (concat "^" erc-valid-nick-regexp "$") nick))
2306 (defun erc-display-line (string &optional buffer)
2307 "Display STRING in the ERC BUFFER.
2308 All screen output must be done through this function. If BUFFER is nil
2309 or omitted, the default ERC buffer for the `erc-session-server' is used.
2310 The BUFFER can be an actual buffer, a list of buffers, 'all or 'active.
2311 If BUFFER = 'all, the string is displayed in all the ERC buffers for the
2312 current session. 'active means the current active buffer
2313 \(`erc-active-buffer'). If the buffer can't be resolved, the current
2314 buffer is used. `erc-display-line-1' is used to display STRING.
2316 If STRING is nil, the function does nothing."
2317 (let ((inhibit-point-motion-hooks t)
2320 ((bufferp buffer) (list buffer))
2321 ((listp buffer) buffer)
2322 ((processp buffer) (list (process-buffer buffer)))
2324 (and (boundp 'erc-server-process)
2325 ;; Hmm, or all of the same session server?
2326 (erc-buffer-list nil erc-server-process)))
2327 ((and (eq 'active buffer) (erc-active-buffer))
2328 (list (erc-active-buffer)))
2329 ((erc-server-buffer-live-p)
2330 (list (process-buffer erc-server-process)))
2331 (t (list (current-buffer)))))
2332 (when (buffer-live-p buf)
2333 (erc-display-line-1 string buf)
2334 (add-to-list 'new-bufs buf)))
2335 (when (null new-bufs)
2336 (if (erc-server-buffer-live-p)
2337 (erc-display-line-1 string (process-buffer erc-server-process))
2338 (erc-display-line-1 string (current-buffer))))))
2340 (defun erc-display-message-highlight (type string)
2341 "Highlight STRING according to TYPE, where erc-TYPE-face is an erc face.
2343 See also `erc-make-notice'"
2344 (cond ((eq type 'notice)
2345 (erc-make-notice string))
2347 (erc-put-text-property
2349 'face (or (intern-soft
2350 (concat "erc-" (symbol-name type) "-face"))
2355 (defun erc-display-message (parsed type buffer msg &rest args)
2356 "Display MSG in BUFFER.
2358 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2360 See also `erc-format-message' and `erc-display-line'."
2361 (let ((string (if (symbolp msg)
2362 (apply 'erc-format-message msg args)
2369 (mapc (lambda (type)
2371 (erc-display-message-highlight type string)))
2375 (erc-display-message-highlight type string))))
2377 (if (not (erc-response-p parsed))
2378 (erc-display-line string buffer)
2379 (unless (member (erc-response.command parsed) erc-hide-list)
2380 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
2381 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2382 (erc-display-line string buffer)))))
2384 (defun erc-message-type-member (position list)
2385 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2387 This function relies on the erc-parsed text-property being
2389 (let ((prop-val (get-text-property position 'erc-parsed)))
2390 (and prop-val (member (erc-response.command prop-val) list))))
2392 (defvar erc-send-input-line-function 'erc-send-input-line)
2393 (make-variable-buffer-local 'erc-send-input-line-function)
2395 (defun erc-send-input-line (target line &optional force)
2396 "Send LINE to TARGET.
2398 See also `erc-server-send'."
2399 (setq line (format "PRIVMSG %s :%s"
2401 ;; If the line is empty, we still want to
2402 ;; send it - i.e. an empty pasted line.
2403 (if (string= line "\n")
2406 (erc-server-send line force target))
2408 (defun erc-get-arglist (fun)
2409 "Return the argument list of a function without the parens."
2410 (let ((arglist (format "%S" (erc-function-arglist fun))))
2411 (if (string-match "^(\\(.*\\))$" arglist)
2412 (match-string 1 arglist)
2415 (defun erc-command-name (cmd)
2416 "For CMD being the function name of a ERC command, something like
2417 erc-cmd-FOO, this returns a string /FOO."
2418 (let ((command-name (symbol-name cmd)))
2419 (if (string-match "^erc-cmd-\\(.*\\)$" command-name)
2420 (concat "/" (match-string 1 command-name))
2423 (defun erc-process-input-line (line &optional force no-command)
2424 "Translate LINE to an RFC1459 command and send it based.
2425 Returns non-nil if the command is actually sent to the server, and nil
2428 If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
2429 it is passed to `erc-cmd-default'. If LINE is not a command (ie. doesn't
2430 start with /<COMMAND>) then it is sent as a message.
2432 An optional FORCE argument forces sending the line when flood
2433 protection is in effect. The optional NO-COMMAND argument prohibits
2434 this function from interpreting the line as a command."
2435 (let ((command-list (erc-extract-command-from-line line)))
2436 (if (and command-list
2438 (let* ((cmd (nth 0 command-list))
2439 (args (nth 1 command-list)))
2444 (wrong-number-of-arguments
2445 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2446 ?c (erc-command-name cmd)
2447 ?u (or (erc-get-arglist cmd)
2450 (or (documentation cmd) "")))
2452 (let ((r (erc-default-target)))
2454 (funcall erc-send-input-line-function r line force)
2455 (erc-display-message nil 'error (current-buffer) 'no-target)
2458 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2459 ;; Input commands handlers
2460 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2462 (defun erc-cmd-AMSG (line)
2463 "Send LINE to all channels of the current server that you are on."
2464 (interactive "sSend to all channels you're on: ")
2465 (setq line (erc-trim-string line))
2466 (erc-with-all-buffers-of-server nil
2468 (erc-channel-p (erc-default-target)))
2469 (erc-send-message line)))
2470 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2472 (defun erc-cmd-SAY (line)
2473 "Send LINE to the current query or channel as a message, not a command.
2475 Use this when you want to send a message with a leading '/'. Note
2476 that since multi-line messages are never a command, you don't
2477 need this when pasting multiple lines of text."
2478 (if (string-match "^\\s-*$" line)
2480 (string-match "^ ?\\(.*\\)" line)
2481 (erc-process-input-line (match-string 1 line) nil t)))
2482 (put 'erc-cmd-SAY 'do-not-parse-args t)
2484 (defun erc-cmd-SET (line)
2485 "Set the variable named by the first word in LINE to some VALUE.
2486 VALUE is computed by evaluating the rest of LINE in Lisp."
2488 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2489 (let ((var (read (concat "erc-" (match-string 1 line))))
2490 (val (read (match-string 2 line))))
2493 (set var (eval val))
2494 (erc-display-message
2495 nil nil 'active (format "Set %S to %S" var val))
2497 (setq var (read (match-string 1 line)))
2500 (set var (eval val))
2501 (erc-display-message
2502 nil nil 'active (format "Set %S to %S" var val))
2504 (erc-display-message nil 'error 'active 'variable-not-bound)
2506 ((string-match "^\\s-*$" line)
2508 (concat "Available user variables:\n"
2513 (let ((val (symbol-value var)))
2514 (concat (format "%S:" var)
2516 (concat "\n" (pp-to-string val))
2517 (format " %S\n" val)))))
2518 (apropos-internal "^erc-" 'user-variable-p))))
2519 (current-buffer)) t)
2521 (defalias 'erc-cmd-VAR 'erc-cmd-SET)
2522 (defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2523 (put 'erc-cmd-SET 'do-not-parse-args t)
2525 (defun erc-cmd-default (line)
2528 Commands for which no erc-cmd-xxx exists, are tunnelled through
2529 this function. LINE is sent to the server verbatim, and
2530 therefore has to contain the command itself as well."
2531 (erc-log (format "cmd: DEFAULT: %s" line))
2532 (erc-server-send (substring line 1))
2535 (defun erc-cmd-IGNORE (&optional user)
2536 "Ignore USER. This should be a regexp matching nick!user@host.
2537 If no USER argument is specified, list the contents of `erc-ignore-list'."
2541 (erc-make-notice (format "Now ignoring %s" user))
2543 (with-current-buffer (erc-server-buffer)
2544 (add-to-list 'erc-ignore-list user)))
2545 (if (null (with-current-buffer (erc-server-buffer) erc-ignore-list))
2546 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2547 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2548 (mapc #'(lambda (item)
2549 (erc-display-line (erc-make-notice item)
2551 (with-current-buffer (erc-server-buffer) erc-ignore-list))))
2554 (defun erc-cmd-UNIGNORE (user)
2555 "Remove the user specified in USER from the ignore list."
2556 (let ((ignored-nick (car (with-current-buffer (erc-server-buffer)
2557 (erc-member-ignore-case user erc-ignore-list)))))
2558 (if (null ignored-nick)
2560 (erc-make-notice (format "%s is not currently ignored!" user))
2563 (erc-make-notice (format "No longer ignoring %s" user))
2565 (with-current-buffer (erc-server-buffer)
2566 (setq erc-ignore-list (delete ignored-nick erc-ignore-list))))
2569 (defun erc-cmd-CLEAR ()
2570 "Clear the window content."
2574 (defun erc-cmd-OPS ()
2575 "Show the ops in the current channel."
2578 (if erc-channel-users
2579 (maphash (lambda (nick user-data)
2580 (let ((cuser (cdr user-data)))
2582 (erc-channel-user-op cuser))
2583 (setq ops (cons (erc-server-user-nickname
2587 (setq ops (sort ops 'string-lessp))
2589 (erc-display-message
2590 nil 'notice (current-buffer) 'ops
2591 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2592 ?o (mapconcat 'identity ops " "))
2593 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2596 (defun erc-cmd-COUNTRY (tld)
2597 "Display the country associated with the top level domain TLD."
2598 (require 'mail-extr)
2599 (let ((co (ignore-errors (what-domain tld))))
2601 (erc-display-message
2602 nil 'notice 'active 'country ?c co ?d tld)
2603 (erc-display-message
2604 nil 'notice 'active 'country-unknown ?d tld))
2607 (defun erc-cmd-AWAY (line)
2608 "Mark the user as being away, the reason being indicated by LINE.
2609 If no reason is given, unset away status."
2610 (when (string-match "^\\s-*\\(.*\\)$" line)
2611 (let ((reason (match-string 1 line)))
2612 (erc-log (format "cmd: AWAY: %s" reason))
2614 (if (string= reason "")
2616 (concat "AWAY :" reason))))
2618 (put 'erc-cmd-AWAY 'do-not-parse-args t)
2620 (defun erc-cmd-GAWAY (line)
2621 "Mark the user as being away everywhere, the reason being indicated by LINE."
2622 ;; on all server buffers.
2623 (erc-with-all-buffers-of-server nil
2624 #'erc-server-buffer-p
2625 (erc-cmd-AWAY line)))
2626 (put 'erc-cmd-GAWAY 'do-not-parse-args t)
2628 (defun erc-cmd-CTCP (nick cmd &rest args)
2629 "Send a Client To Client Protocol message to NICK.
2631 CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2632 VERSION and so on. It is called with ARGS."
2633 (let ((str (concat cmd
2635 (concat " " (mapconcat #'identity args " "))))))
2636 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2637 (erc-send-ctcp-message nick str)
2640 (defun erc-cmd-HELP (&optional func)
2641 "Popup help information.
2643 If FUNC contains a valid function or variable, help about that
2644 will be displayed. If FUNC is empty, display an apropos about
2645 erc commands. Otherwise, do apropos in the erc namespace
2649 To find out about erc and bbdb, do
2652 For help about the WHOIS command, do:
2655 For a list of user commands (/join /part, ...):
2658 (let* ((sym (or (let ((sym (intern-soft
2659 (concat "erc-cmd-" (upcase func)))))
2660 (if (and sym (or (boundp sym) (fboundp sym)))
2663 (let ((sym (intern-soft func)))
2664 (if (and sym (or (boundp sym) (fboundp sym)))
2667 (let ((sym (intern-soft (concat "erc-" func))))
2668 (if (and sym (or (boundp sym) (fboundp sym)))
2673 ((boundp sym) (describe-variable sym))
2674 ((fboundp sym) (describe-function sym))
2676 (apropos-command (concat "erc-.*" func) nil
2679 (get x 'custom-type))))
2681 (apropos "erc-cmd-")
2682 (message "Type C-h m to get additional information about keybindings.")
2685 (defalias 'erc-cmd-H 'erc-cmd-HELP)
2687 (defun erc-cmd-JOIN (channel &optional key)
2688 "Join the channel given in CHANNEL, optionally with KEY.
2689 If CHANNEL is specified as \"-invite\", join the channel to which you
2690 were most recently invited. See also `invitation'."
2692 (if (string= (upcase channel) "-INVITE")
2694 (setq chnl erc-invitation)
2695 (erc-display-message nil 'error (current-buffer) 'no-invitation))
2696 (setq chnl (erc-ensure-channel-name channel)))
2698 ;; Prevent double joining of same channel on same server.
2699 (let ((joined-channels
2700 (mapcar #'(lambda (chanbuf)
2701 (with-current-buffer chanbuf (erc-default-target)))
2702 (erc-channel-list erc-server-process))))
2703 (if (erc-member-ignore-case chnl joined-channels)
2704 (switch-to-buffer (car (erc-member-ignore-case chnl
2706 (erc-log (format "cmd: JOIN: %s" chnl))
2708 (erc-server-send (format "JOIN %s %s" chnl key))
2709 (erc-server-send (format "JOIN %s" chnl)))))))
2712 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
2713 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
2715 (defvar erc-channel-new-member-names nil
2716 "If non-nil, a names list is currently being received.
2718 If non-nil, this variable is a hash-table that associates
2719 received nicks with t.")
2720 (make-variable-buffer-local 'erc-channel-new-member-names)
2722 (defun erc-cmd-NAMES (&optional channel)
2723 "Display the users in CHANNEL.
2724 If CHANNEL is not specified, display the users in the current channel.
2725 This function clears the channel name list first, then sends the
2727 (let ((tgt (or (and (erc-channel-p channel) channel)
2728 (erc-default-target))))
2729 (if (and tgt (erc-channel-p tgt))
2731 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
2734 (erc-channel-begin-receiving-names))
2735 (erc-server-send (concat "NAMES " tgt)))
2736 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
2738 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
2740 (defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
2741 "Kick the user indicated in LINE from the current channel.
2742 LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
2743 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
2744 (if (string= "" reasonstring)
2745 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
2746 (if (erc-channel-p target)
2747 (let ((nick reason-or-nick))
2748 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
2749 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
2753 (let ((ch (erc-default-target)))
2754 (setq reasonstring (concat
2755 (if reason-or-nick (concat reason-or-nick " "))
2760 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
2762 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
2763 (erc-display-message nil 'error (current-buffer)
2764 'no-default-channel))
2767 (defvar erc-script-args nil)
2769 (defun erc-cmd-LOAD (line)
2770 "Load the script provided in the LINE.
2771 If LINE continues beyond the file name,
2772 the rest of it is put in a (local) variable
2773 `erc-script-args', which can be used in elisp scripts.
2775 The optional FORCE argument is ignored here - you can't force loading
2776 a script after exceeding the flood threshold."
2778 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
2779 (let* ((file-to-find (match-string 1 line))
2780 (erc-script-args (match-string 2 line))
2781 (file (erc-find-file file-to-find erc-script-path)))
2782 (erc-log (format "cmd: LOAD: %s" file-to-find))
2785 (erc-display-message nil 'error (current-buffer)
2786 'cannot-find-file ?f file-to-find))
2787 ((not (file-readable-p file))
2788 (erc-display-message nil 'error (current-buffer)
2789 'cannot-read-file ?f file))
2791 (message "Loading \'%s\'..." file)
2792 (erc-load-script file)
2793 (message "Loading \'%s\'...done" file))))
2797 (defun erc-cmd-WHOIS (user &optional server)
2798 "Display whois information for USER.
2800 If SERVER is non-nil, use that, rather than the current server."
2801 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
2802 (let ((send (if server
2803 (format "WHOIS %s %s" user server)
2804 (format "WHOIS %s" user))))
2805 (erc-log (format "cmd: %s" send))
2806 (erc-server-send send)
2808 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
2810 (defun erc-cmd-WHOAMI ()
2811 "Display whois information about yourself."
2812 (erc-cmd-WHOIS (erc-current-nick))
2815 (defun erc-cmd-IDLE (nick)
2816 "Show the length of time NICK has been idle."
2817 (let ((serverbuf (erc-server-buffer))
2818 (origbuf (current-buffer))
2820 (with-current-buffer serverbuf
2821 (add-to-list 'symlist
2822 (cons (erc-once-with-server-event
2825 (erc-response.command-args parsed))))
2826 'erc-server-311-functions))
2827 (add-to-list 'symlist
2828 (cons (erc-once-with-server-event
2831 (erc-response.command-args parsed))))
2832 'erc-server-312-functions))
2833 (add-to-list 'symlist
2834 (cons (erc-once-with-server-event
2837 (erc-response.command-args parsed))))
2838 'erc-server-318-functions))
2839 (add-to-list 'symlist
2840 (cons (erc-once-with-server-event
2843 (erc-response.command-args parsed))))
2844 'erc-server-319-functions))
2845 (add-to-list 'symlist
2846 (cons (erc-once-with-server-event
2849 (erc-response.command-args parsed))))
2850 'erc-server-320-functions))
2851 (add-to-list 'symlist
2852 (cons (erc-once-with-server-event
2855 (erc-response.command-args parsed))))
2856 'erc-server-330-functions))
2857 (add-to-list 'symlist
2858 (cons (erc-once-with-server-event
2863 (erc-response.command-args parsed)))))
2866 (format "%s has been idle for %s."
2867 (erc-string-no-properties ,nick)
2868 (erc-seconds-to-string idleseconds)))
2871 'erc-server-317-functions))
2873 ;; Send the WHOIS command.
2874 (erc-cmd-WHOIS nick)
2876 ;; Remove the uninterned symbols from the server hooks that did not run.
2877 (run-at-time 20 nil `(lambda ()
2878 (with-current-buffer ,(current-buffer)
2879 (dolist (sym ',symlist)
2880 (let ((hooksym (cdr sym))
2881 (funcsym (car sym)))
2882 (remove-hook hooksym funcsym t))))))))
2885 (defun erc-cmd-DESCRIBE (line)
2886 "Pose some action to a certain user.
2887 LINE has the format \"USER ACTION\"."
2890 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
2891 (let ((dst (match-string 1 line))
2892 (s (match-string 2 line)))
2893 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
2894 (erc-send-action dst s))
2897 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
2899 (defun erc-cmd-ME (line)
2900 "Send LINE as an action."
2902 ((string-match "^\\s-\\(.*\\)$" line)
2903 (let ((s (match-string 1 line)))
2904 (erc-log (format "cmd: ME: %s" s))
2905 (erc-send-action (erc-default-target) s))
2908 (put 'erc-cmd-ME 'do-not-parse-args t)
2910 (defun erc-cmd-LASTLOG (line)
2911 "Show all lines in the current buffer matching the regexp LINE.
2913 If a match spreads across multiple lines, all those lines are shown.
2915 The lines are shown in a buffer named `*Occur*'.
2916 It serves as a menu to find any of the occurrences in this buffer.
2917 \\[describe-mode] in that buffer will explain how.
2919 If LINE contains upper case characters (excluding those preceded by `\'),
2920 the matching is case-sensitive."
2923 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
2925 (defun erc-send-message (line &optional force)
2926 "Send LINE to the current channel or user and display it.
2928 See also `erc-message' and `erc-display-line'."
2929 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
2931 (concat (erc-format-my-nick) line)
2933 ;; FIXME - treat multiline, run hooks, or remove me?
2936 (defun erc-cmd-MODE (line)
2937 "Change or display the mode value of a channel or user.
2938 The first word specifies the target. The rest is the mode string
2941 If only one word is given, display the mode of that target.
2943 A list of valid mode strings for Freenode may be found at
2944 `http://freenode.net/using_the_network.shtml'."
2946 ((string-match "^\\s-\\(.*\\)$" line)
2947 (let ((s (match-string 1 line)))
2948 (erc-log (format "cmd: MODE: %s" s))
2949 (erc-server-send (concat "MODE " line)))
2952 (put 'erc-cmd-MODE 'do-not-parse-args t)
2954 (defun erc-cmd-NOTICE (channel-or-user &rest message)
2955 "Send a notice to the channel or user given as the first word.
2956 The rest is the message to send."
2957 (erc-message "NOTICE" (concat channel-or-user " "
2958 (mapconcat #'identity message " "))))
2960 (defun erc-cmd-MSG (line)
2961 "Send a message to the channel or user given as the first word in LINE.
2963 The rest of LINE is the message to send."
2964 (erc-message "PRIVMSG" line))
2966 (defalias 'erc-cmd-M 'erc-cmd-MSG)
2967 (put 'erc-cmd-MSG 'do-not-parse-args t)
2969 (defun erc-cmd-SQUERY (line)
2970 "Send a Service Query to the service given as the first word in LINE.
2972 The rest of LINE is the message to send."
2973 (erc-message "SQUERY" line))
2975 (defun erc-cmd-NICK (nick)
2976 "Change current nickname to NICK."
2977 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
2978 (let ((nicklen (cdr (assoc "NICKLEN" (with-current-buffer (erc-server-buffer)
2979 erc-server-parameters)))))
2980 (and nicklen (> (length nick) (string-to-number nicklen))
2981 (erc-display-message
2982 nil 'notice 'active 'nick-too-long
2983 ?i (length nick) ?l nicklen)))
2984 (erc-server-send (format "NICK %s" nick))
2986 (erc-set-current-nick nick)
2987 (erc-update-mode-line)
2988 (setq erc-bad-nick nil)))
2991 (defun erc-cmd-PART (line)
2992 "When LINE is an empty string, leave the current channel.
2993 Otherwise leave the channel indicated by LINE."
2995 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
2996 (let* ((ch (match-string 1 line))
2997 (msg (match-string 2 line))
2998 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
2999 (erc-log (format "cmd: PART: %s: %s" ch reason))
3000 (erc-server-send (if (string= reason "")
3001 (format "PART %s" ch)
3002 (format "PART %s :%s" ch reason))
3005 ((string-match "^\\s-*\\(.*\\)$" line)
3006 (let* ((ch (erc-default-target))
3007 (msg (match-string 1 line))
3008 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3009 (if (and ch (erc-channel-p ch))
3011 (erc-log (format "cmd: PART: %s: %s" ch reason))
3012 (erc-server-send (if (string= reason "")
3013 (format "PART %s" ch)
3014 (format "PART %s :%s" ch reason))
3016 (erc-display-message nil 'error (current-buffer) 'no-target)))
3019 (put 'erc-cmd-PART 'do-not-parse-args t)
3021 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
3023 (defun erc-cmd-PING (recipient)
3025 (let ((time (format "%f" (erc-current-time))))
3026 (erc-log (format "cmd: PING: %s" time))
3027 (erc-cmd-CTCP recipient "PING" time)))
3029 (defun erc-cmd-QUOTE (line)
3030 "Send LINE directly to the server.
3031 All the text given as argument is sent to the sever as unmodified,
3032 just as you provided it. Use this command with care!"
3034 ((string-match "^ ?\\(.+\\)$" line)
3035 (erc-server-send (match-string 1 line)))
3037 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
3039 (defun erc-cmd-QUERY (&optional user)
3040 "Open a query with USER.
3041 The type of query window/frame/etc will depend on the value of
3042 `erc-join-buffer'. If USER is omitted, close the current query buffer if one
3043 exists - except this is broken now ;-)"
3045 (list (read-from-minibuffer "Start a query with: " nil)))
3046 (let ((session-buffer (erc-server-buffer)))
3048 (erc-query user session-buffer)
3049 ;; currently broken, evil hack to display help anyway
3050 ;(erc-delete-query))))
3051 (signal 'wrong-number-of-arguments ""))))
3052 (defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3054 (defun erc-quit-reason-normal (&optional s)
3055 "Normal quit message.
3057 If S is non-nil, it will be used as the quit reason."
3059 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3060 erc-version-string) ; erc-official-location)
3063 (defun erc-quit-reason-zippy (&optional s)
3064 "Zippy quit message.
3066 If S is non-nil, it will be used as the quit reason."
3068 (erc-replace-regexp-in-string "\n" "" (yow))))
3070 (defun erc-quit-reason-various (s)
3071 "Choose a quit reason based on S (a string)."
3072 (when (featurep 'xemacs) (require 'poe))
3073 (let ((res (car (assoc-default (or s "")
3074 erc-quit-reason-various-alist 'string-match))))
3076 ((functionp res) (funcall res))
3079 (t (erc-quit-reason-normal)))))
3081 (defun erc-part-reason-normal (&optional s)
3082 "Normal part message.
3084 If S is non-nil, it will be used as the quit reason."
3086 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3087 erc-version-string) ; erc-official-location)
3090 (defun erc-part-reason-zippy (&optional s)
3091 "Zippy part message.
3093 If S is non-nil, it will be used as the quit reason."
3095 (erc-replace-regexp-in-string "\n" "" (yow))))
3097 (defun erc-part-reason-various (s)
3098 "Choose a part reason based on S (a string)."
3099 (when (featurep 'xemacs) (require 'poe))
3100 (let ((res (car (assoc-default (or s "")
3101 erc-part-reason-various-alist 'string-match))))
3103 ((functionp res) (funcall res))
3106 (t (erc-part-reason-normal)))))
3108 (defun erc-cmd-QUIT (reason)
3109 "Disconnect from the current server.
3110 If REASON is omitted, display a default quit message, otherwise display
3111 the message given by REASON."
3115 ((string-match "^\\s-*\\(.*\\)$" reason)
3116 (let* ((s (match-string 1 reason))
3117 (buffer (erc-server-buffer))
3118 (reason (funcall erc-quit-reason (if (equal s "") nil s))))
3119 (with-current-buffer (if (and buffer
3123 (erc-log (format "cmd: QUIT: %s" reason))
3124 (setq erc-server-quitting t)
3125 (erc-set-active-buffer (erc-server-buffer))
3126 (erc-server-send (format "QUIT :%s" reason)))
3127 (run-hook-with-args 'erc-quit-hook erc-server-process)
3128 (when erc-kill-queries-on-quit
3129 (erc-kill-query-buffers erc-server-process)))
3133 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3134 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3135 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3136 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3138 (defun erc-cmd-GQUIT (reason)
3139 "Disconnect from all servers at once with the same quit REASON."
3140 (erc-with-all-buffers-of-server nil #'(lambda ()
3141 (and (erc-server-buffer-p)
3142 (erc-server-process-alive)))
3143 (erc-cmd-QUIT reason)))
3145 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3146 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3148 (defun erc-cmd-SERVER (server)
3149 "Connect to SERVER, leaving existing connection intact."
3150 (erc-log (format "cmd: SERVER: %s" server))
3152 (erc-select :server server :nick (erc-current-nick))
3154 (message "Cannot find host %s." server)
3159 (defvar motif-version-string)
3160 (defvar gtk-version-string))
3162 (defun erc-cmd-SV ()
3163 "Say the current ERC and Emacs version into channel."
3164 (erc-send-message (format "I'm using ERC %s with %s %s (%s%s) of %s."
3166 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3168 system-configuration
3170 (cond ((featurep 'motif)
3171 (concat ", " (substring
3172 motif-version-string 4)))
3174 (concat ", GTK+ Version "
3175 gtk-version-string))
3176 ((featurep 'mac-carbon) ", Mac Carbon")
3177 ((featurep 'x-toolkit) ", X toolkit")
3179 (if (and (boundp 'x-toolkit-scroll-bars)
3180 (memq x-toolkit-scroll-bars
3182 (format ", %s scroll bars"
3183 (capitalize (symbol-name
3184 x-toolkit-scroll-bars)))
3186 (if (featurep 'multi-tty) ", multi-tty" ""))
3187 erc-emacs-build-time))
3190 (defun erc-cmd-SM ()
3191 "Say the current ERC modes into channel."
3192 (erc-send-message (format "I'm using the following modules: %s!"
3196 (defun erc-cmd-DEOP (&rest people)
3197 "Remove the operator setting from user(s) given in PEOPLE."
3198 (when (> (length people) 0)
3199 (erc-server-send (concat "MODE " (erc-default-target)
3201 (make-string (length people) ?o)
3203 (mapconcat 'identity people " ")))
3206 (defun erc-cmd-OP (&rest people)
3207 "Add the operator setting to users(s) given in PEOPLE."
3208 (when (> (length people) 0)
3209 (erc-server-send (concat "MODE " (erc-default-target)
3211 (make-string (length people) ?o)
3213 (mapconcat 'identity people " ")))
3216 (defun erc-cmd-TIME (&optional line)
3217 "Request the current time and date from the current server."
3219 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3220 (let ((args (match-string 1 line)))
3221 (erc-log (format "cmd: TIME: %s" args))
3222 (erc-server-send (concat "TIME " args)))
3224 (t (erc-server-send "TIME"))))
3225 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3227 (defun erc-cmd-TOPIC (topic)
3228 "Set or request the topic for a channel.
3229 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3230 or the empty string.
3232 If no #CHANNEL is given, the default channel is used. If TOPIC is
3233 given, the channel topic is modified, otherwise the current topic will
3236 ;; /topic #channel TOPIC
3237 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3238 (let ((ch (match-string 1 topic))
3239 (topic (match-string 2 topic)))
3240 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3241 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3244 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3245 (let ((ch (match-string 1 topic)))
3246 (erc-server-send (format "TOPIC %s" ch) nil ch)
3249 ((string-match "^\\s-*$" topic)
3250 (let ((ch (erc-default-target)))
3251 (erc-server-send (format "TOPIC %s" ch) nil ch)
3254 ((string-match "^\\s-*\\(.*\\)$" topic)
3255 (let ((ch (erc-default-target))
3256 (topic (match-string 1 topic)))
3257 (if (and ch (erc-channel-p ch))
3259 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3260 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3261 (erc-display-message nil 'error (current-buffer) 'no-target)))
3264 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3265 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3267 (defun erc-cmd-APPENDTOPIC (topic)
3268 "Append TOPIC to the current channel topic, separated by a space."
3269 (let ((oldtopic erc-channel-topic))
3270 ;; display help when given no arguments
3271 (when (string-match "^\\s-*$" topic)
3272 (signal 'wrong-number-of-arguments nil))
3273 ;; strip trailing ^O
3274 (when (string-match "\\(.*\\)\C-o" oldtopic)
3275 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3276 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3277 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3279 (defun erc-cmd-CLEARTOPIC (&optional channel)
3280 "Clear the topic for a CHANNEL.
3281 If CHANNEL is not specified, clear the topic for the default channel."
3282 (interactive "sClear topic of channel (RET is current channel): ")
3283 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3285 (erc-server-send (format "TOPIC %s :" chnl))
3290 (defvar erc-channel-banlist nil
3291 "A list of bans seen for the current channel.
3293 Each ban is an alist of the form:
3296 The property `received-from-server' indicates whether
3297 or not the ban list has been requested from the server.")
3298 (make-variable-buffer-local 'erc-channel-banlist)
3299 (put 'erc-channel-banlist 'received-from-server nil)
3301 (defun erc-cmd-BANLIST ()
3302 "Pretty-print the contents of `erc-channel-banlist'.
3304 The ban list is fetched from the server if necessary."
3305 (let ((chnl (erc-default-target))
3306 (chnl-name (buffer-name)))
3309 ((not (erc-channel-p chnl))
3310 (erc-display-line (erc-make-notice "You're not on a channel\n")
3313 ((not (get 'erc-channel-banlist 'received-from-server))
3314 (let ((old-367-hook erc-server-367-functions))
3315 (setq erc-server-367-functions 'erc-banlist-store
3316 erc-channel-banlist nil)
3317 ;; fetch the ban list then callback
3318 (with-current-buffer (erc-server-buffer)
3319 (erc-once-with-server-event
3321 `(with-current-buffer ,chnl-name
3322 (put 'erc-channel-banlist 'received-from-server t)
3323 (setq erc-server-367-functions ',old-367-hook)
3326 (erc-server-send (format "MODE %s b" chnl)))))
3328 ((null erc-channel-banlist)
3329 (erc-display-line (erc-make-notice
3330 (format "No bans for channel: %s\n" chnl))
3332 (put 'erc-channel-banlist 'received-from-server nil))
3335 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3337 (and (boundp 'fill-column)
3339 (1- (window-width))))
3340 (separator (make-string erc-fill-column (string-to-char "=")))
3342 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3343 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3346 (erc-make-notice (format "Ban list for channel: %s\n"
3347 (erc-default-target)))
3350 (erc-display-line separator 'active)
3351 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3352 (erc-display-line separator 'active)
3358 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3360 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3363 erc-channel-banlist)
3365 (erc-display-line (erc-make-notice "End of Ban list")
3367 (put 'erc-channel-banlist 'received-from-server nil)))))
3370 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3372 (defun erc-cmd-MASSUNBAN ()
3375 Unban all currently banned users in the current channel."
3376 (let ((chnl (erc-default-target)))
3379 ((not (erc-channel-p chnl))
3381 (erc-make-notice "You're not on a channel\n")
3384 ((not (get 'erc-channel-banlist 'received-from-server))
3385 (let ((old-367-hook erc-server-367-functions))
3386 (setq erc-server-367-functions 'erc-banlist-store)
3387 ;; fetch the ban list then callback
3388 (with-current-buffer (erc-server-buffer)
3389 (erc-once-with-server-event
3391 `(with-current-buffer ,chnl
3392 (put 'erc-channel-banlist 'received-from-server t)
3393 (setq erc-server-367-functions ,old-367-hook)
3396 (erc-server-send (format "MODE %s b" chnl)))))
3398 (t (let ((bans (mapcar 'cdr erc-channel-banlist)))
3400 ;; Glob the bans into groups of three, and carry out the unban.
3401 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3405 (format "MODE %s -%s %s" (erc-default-target)
3406 (make-string (length x) (string-to-char "b"))
3407 (mapconcat 'identity x " "))))
3408 (erc-group-list bans 3))))
3411 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3413 ;;;; End of IRC commands
3415 (defun erc-ensure-channel-name (channel)
3416 "Return CHANNEL if it is a valid channel name.
3417 Eventually add a # in front of it, if that turns it into a valid channel name."
3418 (if (erc-channel-p channel)
3420 (concat "#" channel)))
3422 (defun erc-grab-region (start end)
3423 "Copy the region between START and END in a recreatable format.
3425 Converts all the IRC text properties in each line of the region
3426 into control codes and writes them to a separate buffer. The
3427 resulting text may be used directly as a script to generate this
3430 (erc-set-active-buffer (current-buffer))
3432 (let* ((cb (current-buffer))
3433 (buf (generate-new-buffer erc-grab-buffer-name))
3434 (region (buffer-substring start end))
3435 (lines (erc-split-multiline-safe region)))
3437 (dolist (line lines)
3438 (insert (concat line "\n")))
3440 (switch-to-buffer-other-window buf)))
3441 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3444 (defun erc-display-prompt (&optional buffer pos prompt face)
3445 "Display PROMPT in BUFFER at position POS.
3446 Display an ERC prompt in BUFFER.
3448 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3449 If BUFFER is nil, the `current-buffer' is used.
3450 If POS is nil, PROMPT will be displayed at `point'.
3451 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3452 `erc-prompt-face' will be used."
3453 (let* ((prompt (or prompt (erc-prompt)))
3455 (ob (current-buffer)))
3456 ;; We cannot use save-excursion because we move point, therefore
3457 ;; we resort to the ol' ob trick to restore this.
3458 (when (and buffer (bufferp buffer))
3459 (set-buffer buffer))
3461 ;; now save excursion again to store where point and mark are
3462 ;; in the current buffer
3464 (setq pos (or pos (point)))
3467 ;; Do not extend the text properties when typing at the end
3468 ;; of the prompt, but stuff typed in front of the prompt
3469 ;; shall remain part of the prompt.
3470 (setq prompt (erc-propertize prompt
3471 'start-open t ; XEmacs
3472 'rear-nonsticky t ; Emacs
3476 (erc-put-text-property 0 (1- (length prompt))
3477 'face (or face 'erc-prompt-face)
3480 ;; Set the input marker
3481 (set-marker erc-input-marker (point)))
3483 ;; Now we are back at the old position. If the prompt was
3484 ;; inserted here or before us, advance point by the length of
3486 (when (or (not pos) (<= (point) pos))
3488 ;; Clear the undo buffer now, so the user can undo his stuff,
3489 ;; but not the stuff we did. Sneaky!
3490 (setq buffer-undo-list nil)
3493 ;; interactive operations
3495 (defun erc-input-message ()
3496 "Read input from the minibuffer."
3498 (let ((minibuffer-allow-text-properties t)
3499 (read-map minibuffer-local-map))
3500 (insert (read-from-minibuffer "Message: "
3501 (string last-command-char) read-map))
3502 (erc-send-current-line)))
3504 (defvar erc-action-history-list ()
3505 "History list for interactive action input.")
3507 (defun erc-input-action ()
3508 "Interactively input a user action and send it to IRC."
3510 (erc-set-active-buffer (current-buffer))
3511 (let ((action (read-from-minibuffer
3512 "Action: " nil nil nil 'erc-action-history-list)))
3513 (if (not (string-match "^\\s-*$" action))
3514 (erc-send-action (erc-default-target) action))))
3516 (defun erc-join-channel (channel &optional key)
3519 If `point' is at the beginning of a channel name, use that as default."
3522 (let ((chnl (if (looking-at "\\([&#+!][^ \n]+\\)") (match-string 1) ""))
3523 (table (when (erc-server-buffer-live-p)
3524 (set-buffer (process-buffer erc-server-process))
3526 (completing-read "Join channel: " table nil nil nil nil chnl))
3527 (when erc-prompt-for-channel-key
3528 (read-from-minibuffer "Channel key (RET for none): " nil))))
3529 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3531 (defun erc-part-from-channel (reason)
3532 "Part from the current channel and prompt for a REASON."
3535 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3537 (read-from-minibuffer (concat "Leave " (erc-default-target)
3539 (cons "No reason" 0)))))
3540 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3542 (defun erc-set-topic (topic)
3543 "Prompt for a TOPIC for the current channel."
3546 (read-from-minibuffer
3547 (concat "Set topic of " (erc-default-target) ": ")
3548 (when erc-channel-topic
3549 (cons (apply 'concat (butlast (split-string erc-channel-topic "\C-o")))
3551 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3552 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3554 (defun erc-set-channel-limit (&optional limit)
3555 "Set a LIMIT for the current channel. Remove limit if nil.
3556 Prompt for one if called interactively."
3557 (interactive (list (read-from-minibuffer
3558 (format "Limit for %s (RET to remove limit): "
3559 (erc-default-target)))))
3560 (let ((tgt (erc-default-target)))
3561 (if (and limit (>= (length limit) 1))
3562 (erc-server-send (format "MODE %s +l %s" tgt limit))
3563 (erc-server-send (format "MODE %s -l" tgt)))))
3565 (defun erc-set-channel-key (&optional key)
3566 "Set a KEY for the current channel. Remove key if nil.
3567 Prompt for one if called interactively."
3568 (interactive (list (read-from-minibuffer
3569 (format "Key for %s (RET to remove key): "
3570 (erc-default-target)))))
3571 (let ((tgt (erc-default-target)))
3572 (if (and key (>= (length key) 1))
3573 (erc-server-send (format "MODE %s +k %s" tgt key))
3574 (erc-server-send (format "MODE %s -k" tgt)))))
3576 (defun erc-quit-server (reason)
3577 "Disconnect from current server after prompting for REASON.
3578 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
3579 (interactive (list (read-from-minibuffer
3580 (format "Reason for quitting %s: "
3581 (or erc-server-announced-name
3582 erc-session-server)))))
3583 (erc-cmd-QUIT reason))
3585 ;; Movement of point
3588 "Move `point' to the beginning of the current line.
3590 This places `point' just after the prompt, or at the beginning of the line."
3593 (when (get-text-property (point) 'erc-prompt)
3594 (goto-char erc-input-marker))
3597 (defun erc-kill-input ()
3598 "Kill current input line using `erc-bol' followed by `kill-line'."
3600 (when (and (erc-bol)
3601 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
3602 ;; there's nothing to kill
3603 (if (boundp 'erc-input-ring-index)
3604 (setq erc-input-ring-index nil))
3607 (defun erc-complete-word ()
3608 "Complete the word before point.
3610 This function uses `erc-complete-functions'."
3612 (unless (run-hook-with-args-until-success 'erc-complete-functions)
3615 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3617 ;; IRC SERVER INPUT HANDLING
3619 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3621 ;;;; New Input parsing
3623 ; Stolen from ZenIRC. I just wanna test this code, so here is
3626 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
3627 erc-default-server-handler)
3628 "*Default for server messages which aren't covered by `erc-server-hooks'."
3629 :group 'erc-server-hooks
3632 (defun erc-default-server-handler (proc parsed)
3633 "Default server handler.
3635 Displays PROC and PARSED appropriately using `erc-display-message'."
3636 (erc-display-message
3643 (setq res (append res (list x)))))
3648 (defvar erc-server-vectors
3649 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
3650 "List of received server messages which ERC does not specifically handle.
3651 See `erc-debug-missing-hooks'.")
3652 ;(make-variable-buffer-local 'erc-server-vectors)
3654 (defun erc-debug-missing-hooks (proc parsed)
3655 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
3656 These vectors can be helpful when adding new server message handlers to ERC.
3657 See `erc-default-server-hook'."
3658 (nconc erc-server-vectors (list parsed))
3661 (defun erc-query (target server)
3662 "Open a query buffer on TARGET, using SERVER.
3663 To change how this query window is displayed, use `let' to bind
3664 `erc-join-buffer' before calling this."
3666 (buffer-live-p server)
3667 (set-buffer server))
3668 (error "Couldn't switch to server buffer"))
3669 (let ((buf (erc erc-session-server
3672 erc-session-user-full-name
3677 erc-server-process)))
3679 (error "Couldn't open query window"))
3680 (erc-update-mode-line)
3683 (defcustom erc-auto-query nil
3684 "If non-nil, create a query buffer each time you receive a private message.
3686 If the buffer doesn't already exist it is created. This can be
3687 set to a symbol, to control how the new query window should
3688 appear. See the documentation for `erc-join-buffer' for
3691 :type '(choice (const nil)
3694 (const window-noselect)
3698 (defcustom erc-query-on-unjoined-chan-privmsg t
3699 "If non-nil create query buffer on receiving any PRIVMSG at all.
3700 This includes PRIVMSGs directed to channels. If you are using an IRC
3701 bouncer, such as dircproxy, to keep a log of channels when you are
3702 disconnected, you should set this option to t."
3706 (defcustom erc-format-query-as-channel-p t
3707 "If non-nil, format text from others in a query buffer like in a channel,
3708 otherwise format like a private message."
3712 (defcustom erc-minibuffer-notice nil
3713 "If non-nil, print ERC notices for the user in the minibuffer.
3714 Only happens when the session buffer isn't visible."
3718 (defcustom erc-minibuffer-ignored nil
3719 "If non-nil, print a message in the minibuffer if we ignored something."
3723 (defun erc-wash-quit-reason (reason nick login host)
3724 "Remove duplicate text from quit REASON.
3725 Specifically in relation to NICK (user@host) information. Returns REASON
3726 unmodified if nothing can be removed.
3727 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
3728 \"Read error: 110\". The same applies for \"Ping Timeout\"."
3729 (setq nick (regexp-quote nick)
3730 login (regexp-quote login)
3731 host (regexp-quote host))
3732 (or (when (string-match (concat "^\\(Read error\\) to "
3733 nick "\\[" host "\\]: "
3734 "\\(.+\\)$") reason)
3735 (concat (match-string 1 reason) ": " (match-string 2 reason)))
3736 (when (string-match (concat "^\\(Ping timeout\\) for "
3737 nick "\\[" host "\\]$") reason)
3738 (match-string 1 reason))
3741 (defun erc-nickname-in-use (nick reason)
3742 "If NICK is unavailable, tell the user the REASON.
3744 See also `erc-display-error-notice'."
3745 (if (or (not erc-try-new-nick-p)
3746 ;; how many default-nicks are left + one more try...
3747 (eq erc-nick-change-attempt-count
3748 (if (consp erc-nick)
3749 (+ (length erc-nick) 1)
3751 (erc-display-error-notice
3753 (format "Nickname %s is %s, try another." nick reason))
3754 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
3755 (let ((newnick (nth 1 erc-default-nicks))
3756 (nicklen (cdr (assoc "NICKLEN"
3757 (with-current-buffer (erc-server-buffer)
3758 erc-server-parameters)))))
3759 (setq erc-bad-nick t)
3760 ;; try to use a different nick
3761 (if erc-default-nicks
3762 (setq erc-default-nicks (cdr erc-default-nicks)))
3764 (setq newnick (concat (truncate-string-to-width
3766 (if (and erc-server-connected nicklen)
3767 (- (string-to-number nicklen)
3768 (length erc-nick-uniquifier))
3769 ;; rfc2812 max nick length = 9
3770 ;; we must assume this is the
3771 ;; server's setting if we haven't
3772 ;; established a connection yet
3773 (- 9 (length erc-nick-uniquifier))))
3774 erc-nick-uniquifier)))
3775 (erc-cmd-NICK newnick)
3776 (erc-display-error-notice
3778 (format "Nickname %s is %s, trying %s"
3779 nick reason newnick)))))
3783 (defgroup erc-server-hooks nil
3784 "Server event callbacks.
3785 Every server event - like numeric replies - has it's own hook.
3786 Those hooks are all called using `run-hook-with-args-until-success'.
3787 They receive as first argument the process object from where the event
3789 and as second argument the event parsed as a vector."
3792 (defun erc-display-server-message (proc parsed)
3793 "Display the message sent by the server as a notice."
3794 (erc-display-message
3795 parsed 'notice 'active (erc-response.contents parsed)))
3797 (defun erc-auto-query (proc parsed)
3798 ;; FIXME: This needs more documentation, unless it's not a user function --
3799 ;; Lawrence 2004-01-08
3800 "Put this on `erc-server-PRIVMSG-functions'."
3801 (when erc-auto-query
3802 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
3803 (target (car (erc-response.command-args parsed)))
3804 (msg (erc-response.contents parsed))
3805 (query (if (not erc-query-on-unjoined-chan-privmsg)
3807 (if (erc-current-nick-p target)
3810 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
3811 (or erc-query-on-unjoined-chan-privmsg
3812 (string= target (erc-current-nick)))
3813 (not (erc-get-buffer query proc))
3814 (not (erc-is-message-ctcp-and-not-action-p msg))
3815 (let ((erc-join-buffer erc-auto-query))
3816 (erc-cmd-QUERY query))
3819 (defun erc-is-message-ctcp-p (message)
3820 "Check if MESSAGE is a CTCP message or not."
3821 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
3823 (defun erc-is-message-ctcp-and-not-action-p (message)
3824 "Check if MESSAGE is a CTCP message or not."
3825 (and (erc-is-message-ctcp-p message)
3826 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
3828 (defun erc-format-privmessage (nick msg privp msgp)
3829 "Format a PRIVMSG in an insertible fashion."
3830 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
3831 (mark-e (if msgp (if privp "*" ">") "-"))
3832 (str (format "%s%s%s %s" mark-s nick mark-e msg))
3833 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
3834 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
3835 ;; add text properties to text before the nick, the nick and after the nick
3836 (erc-put-text-property 0 (length mark-s) 'face msg-face str)
3837 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
3838 'face nick-face str)
3839 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
3843 (defcustom erc-format-nick-function 'erc-format-nick
3844 "*Function to format a nickname for message display."
3848 (defun erc-format-nick (&optional user channel-data)
3849 "Standard nickname formatting function. Only returns the value
3852 (erc-server-user-nickname user)))
3854 (defun erc-format-@nick (&optional user channel-data)
3855 "Format a nickname such that @ or + are prefix for the NICK
3856 if OP or VOICE are t respectively."
3860 (setq op (erc-channel-user-op channel-data)
3861 voice (erc-channel-user-voice channel-data)))
3862 (concat (if voice "+" "")
3864 (erc-server-user-nickname user)))))
3866 (defun erc-format-my-nick ()
3867 "Return the beginning of this user's message, correctly propertized"
3868 (if erc-show-my-nick
3871 (nick (erc-current-nick)))
3873 (erc-propertize open 'face 'erc-default-face)
3874 (erc-propertize nick 'face 'erc-nick-default-face)
3875 (erc-propertize close 'face 'erc-default-face)))
3876 (let ((prefix "> "))
3877 (erc-propertize prefix 'face 'erc-default-face))))
3879 (defun erc-echo-notice-in-default-buffer (s parsed buffer sender)
3880 "Echos a private notice in the default buffer, namely the
3881 target buffer specified by BUFFER, or there is no target buffer,
3882 the server buffer. This function is designed to be added to
3883 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
3884 and always returns t."
3885 (erc-display-message parsed nil buffer s)
3888 (defun erc-echo-notice-in-target-buffer (s parsed buffer sender)
3889 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
3890 function is designed to be added to either `erc-echo-notice-hook'
3891 or `erc-echo-notice-always-hook', and returns non-nil iff BUFFER
3894 (progn (erc-display-message parsed nil buffer s) t)
3897 (defun erc-echo-notice-in-minibuffer (s parsed buffer sender)
3898 "Echos a private notice in the minibuffer. This function is
3899 designed to be added to either `erc-echo-notice-hook' or
3900 `erc-echo-notice-always-hook', and always returns t."
3901 (message "%s" (concat "NOTICE: " s))
3904 (defun erc-echo-notice-in-server-buffer (s parsed buffer sender)
3905 "Echos a private notice in the server buffer. This function is
3906 designed to be added to either `erc-echo-notice-hook' or
3907 `erc-echo-notice-always-hook', and always returns t."
3908 (erc-display-message parsed nil nil s)
3911 (defun erc-echo-notice-in-active-non-server-buffer (s parsed buffer sender)
3912 "Echos a private notice in the active buffer if the active
3913 buffer is not the server buffer. This function is designed to be
3914 added to either `erc-echo-notice-hook' or
3915 `erc-echo-notice-always-hook', and returns non-nil iff the active
3916 buffer is not the server buffer."
3917 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
3918 (progn (erc-display-message parsed nil 'active s) t)
3921 (defun erc-echo-notice-in-active-buffer (s parsed buffer sender)
3922 "Echos a private notice in the active buffer. This function is
3923 designed to be added to either `erc-echo-notice-hook' or
3924 `erc-echo-notice-always-hook', and always returns t."
3925 (erc-display-message parsed nil 'active s)
3928 (defun erc-echo-notice-in-user-buffers (s parsed buffer sender)
3929 "Echos a private notice in all of the buffers for which SENDER
3930 is a member. This function is designed to be added to either
3931 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
3932 returns non-nil iff there is at least one buffer for which the
3935 See also: `erc-echo-notice-in-first-user-buffer',
3936 `erc-buffer-list-with-nick'"
3937 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3939 (progn (erc-display-message parsed nil buffers s) t)
3942 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
3943 "Echos a private notice in BUFFER and in all of the buffers for
3944 which SENDER is a member. This function is designed to be added
3945 to either `erc-echo-notice-hook' or
3946 `erc-echo-notice-always-hook', and returns non-nil iff there is
3947 at least one buffer for which the sender is a member or the
3950 See also: `erc-echo-notice-in-user-buffers',
3951 `erc-buffer-list-with-nick'"
3952 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3953 (add-to-list 'buffers buffer)
3955 (progn (erc-display-message parsed nil buffers s) t)
3958 (defun erc-echo-notice-in-first-user-buffer (s parsed buffer sender)
3959 "Echos a private notice in one of the buffers for which SENDER
3960 is a member. This function is designed to be added to either
3961 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
3962 returns non-nil iff there is at least one buffer for which the
3965 See also: `erc-echo-notice-in-user-buffers',
3966 `erc-buffer-list-with-nick'"
3967 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3969 (progn (erc-display-message parsed nil (car buffers) s) t)
3972 ;;; Ban manipulation
3974 (defun erc-banlist-store (proc parsed)
3975 "Record ban entries for a channel."
3976 (multiple-value-bind (channel mask whoset)
3977 (cdr (erc-response.command-args parsed))
3978 ;; Determine to which buffer the message corresponds
3979 (let ((buffer (erc-get-buffer channel proc)))
3980 (with-current-buffer buffer
3981 (unless (member (cons whoset mask) erc-channel-banlist)
3982 (setq erc-channel-banlist (cons (cons whoset mask)
3983 erc-channel-banlist))))))
3986 (defun erc-banlist-finished (proc parsed)
3987 "Record that we have received the banlist."
3988 (let* ((channel (second (erc-response.command-args parsed)))
3989 (buffer (erc-get-buffer channel proc)))
3990 (with-current-buffer buffer
3991 (put 'erc-channel-banlist 'received-from-server t)))
3992 t) ; suppress the 'end of banlist' message
3994 (defun erc-banlist-update (proc parsed)
3995 "Check MODE commands for bans and update the banlist appropriately."
3996 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
3997 (let* ((tgt (first (erc-response.command-args parsed)))
3998 (mode (erc-response.contents parsed))
3999 (whoset (erc-response.sender parsed))
4000 (buffer (erc-get-buffer tgt proc)))
4002 (with-current-buffer buffer
4003 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
4004 ((string-match "^\\([+-]\\)b" mode)
4007 ((string-match "^-" mode)
4008 ;; Remove the unbanned masks from the ban list
4009 (setq erc-channel-banlist
4012 (member (upcase (cdr y))
4014 (cdr (split-string mode)))))
4015 erc-channel-banlist)))
4016 ((string-match "^+" mode)
4017 ;; Add the banned mask(s) to the ban list
4020 (unless (member (cons whoset mask) erc-channel-banlist)
4021 (setq erc-channel-banlist
4022 (cons (cons whoset mask) erc-channel-banlist))))
4023 (cdr (split-string mode))))))))))
4026 ;; used for the banlist cmds
4027 (defun erc-group-list (list n)
4028 "Group LIST into sublists of length N."
4029 (cond ((null list) nil)
4030 ((null (nthcdr n list)) (list list))
4031 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
4036 (defun erc-handle-login ()
4037 "Handle the logging in process of connection."
4038 (unless erc-logged-in
4039 (setq erc-logged-in t)
4040 (message "Logging in as \'%s\'... done" (erc-current-nick))
4041 ;; execute a startup script
4042 (let ((f (erc-select-startup-file)))
4044 (erc-load-script f)))))
4046 (defun erc-connection-established (proc parsed)
4047 "Run just after connection.
4049 Set user modes and run `erc-after-connect hook'."
4050 (unless erc-server-connected ; only once per session
4051 (let ((server (or erc-server-announced-name (erc-response.sender parsed)))
4052 (nick (car (erc-response.command-args parsed ))))
4053 (setq erc-server-connected t)
4054 (erc-update-mode-line)
4055 (erc-set-initial-user-mode nick)
4056 (erc-server-setup-periodical-server-ping)
4057 (run-hook-with-args 'erc-after-connect server nick))))
4059 (defun erc-set-initial-user-mode (nick)
4060 "If `erc-user-mode' is non-nil for NICK, set the user modes."
4062 (let ((mode (if (functionp erc-user-mode)
4063 (funcall erc-user-mode)
4065 (when (stringp mode)
4066 (erc-log (format "changing mode for %s to %s" nick mode))
4067 (erc-server-send (format "MODE %s %s" nick mode))))))
4069 (defun erc-display-error-notice (parsed string)
4070 "Display STRING as an error notice.
4072 See also `erc-display-message'."
4073 (erc-display-message
4074 parsed '(notice error) 'active string))
4076 (defun erc-process-ctcp-query (proc parsed nick login host)
4077 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4078 "Process a CTCP query."
4079 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4081 (if (> (length queries) 4)
4082 (erc-display-message
4083 parsed (list 'notice 'error) proc 'ctcp-too-many)
4084 (if (= 0 (length queries))
4085 (erc-display-message
4086 parsed (list 'notice 'error) proc
4087 'ctcp-empty ?n nick)
4089 (let* ((type (upcase (car (split-string (car queries)))))
4090 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4091 (if (and hook (boundp hook))
4092 (if (string-equal type "ACTION")
4093 (run-hook-with-args-until-success
4094 hook proc parsed nick login host
4095 (car (erc-response.command-args parsed))
4098 (if (erc-current-nick-p
4099 (car (erc-response.command-args parsed)))
4100 (erc-display-message
4101 parsed 'error 'active 'ctcp-request
4102 ?n nick ?u login ?h host ?r (car queries))
4103 (erc-display-message
4104 parsed 'error 'active 'ctcp-request-to
4105 ?n nick ?u login ?h host ?r (car queries)
4106 ?t (car (erc-response.command-args parsed)))))
4107 (run-hook-with-args-until-success
4108 hook proc nick login host
4109 (car (erc-response.command-args parsed))
4111 (erc-display-message
4112 parsed (list 'notice 'error) proc
4114 (setq queries (cdr queries)))))))
4116 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4118 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4119 "Respond to a CTCP ACTION query."
4120 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4121 (let ((s (match-string 1 msg))
4122 (buf (or (erc-get-buffer to proc)
4123 (erc-get-buffer nick proc)
4124 (process-buffer proc))))
4125 (erc-display-message
4127 'ACTION ?n nick ?u login ?h host ?a s))))
4129 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4131 (defun erc-ctcp-query-CLIENTINFO (proc nick login host to msg)
4132 "Respond to a CTCP CLIENTINFO query."
4133 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4134 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4135 (unless erc-disable-ctcp-replies
4136 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4139 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4140 (defun erc-ctcp-query-ECHO (proc nick login host to msg)
4141 "Respond to a CTCP ECHO query."
4142 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4143 (let ((s (match-string 1 msg)))
4144 (unless erc-disable-ctcp-replies
4145 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4148 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4149 (defun erc-ctcp-query-FINGER (proc nick login host to msg)
4150 "Respond to a CTCP FINGER query."
4151 (unless erc-disable-ctcp-replies
4152 (let ((s (if erc-anonymous-login
4153 (format "FINGER I'm %s." (erc-current-nick))
4154 (format "FINGER %s (%s@%s)."
4158 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4160 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4161 (erc-send-ctcp-notice nick s)))
4164 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4165 (defun erc-ctcp-query-PING (proc nick login host to msg)
4166 "Respond to a CTCP PING query."
4167 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4168 (unless erc-disable-ctcp-replies
4169 (let ((arg (match-string 1 msg)))
4170 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4173 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4174 (defun erc-ctcp-query-TIME (proc nick login host to msg)
4175 "Respond to a CTCP TIME query."
4176 (unless erc-disable-ctcp-replies
4177 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4180 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4181 (defun erc-ctcp-query-USERINFO (proc nick login host to msg)
4182 "Respond to a CTCP USERINFO query."
4183 (unless erc-disable-ctcp-replies
4184 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4187 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4188 (defun erc-ctcp-query-VERSION (proc nick login host to msg)
4189 "Respond to a CTCP VERSION query."
4190 (unless erc-disable-ctcp-replies
4191 (erc-send-ctcp-notice
4193 "VERSION \C-bERC\C-b %s - an IRC client for emacs (\C-b%s\C-b)"
4195 erc-official-location)))
4198 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4199 "Process MSG as a CTCP reply."
4200 (let* ((type (car (split-string msg)))
4201 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4203 (run-hook-with-args-until-success
4204 hook proc nick login host
4205 (car (erc-response.command-args parsed)) msg)
4206 (erc-display-message
4207 parsed 'notice 'active
4208 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4210 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4211 (defun erc-ctcp-reply-ECHO (proc nick login host to msg)
4212 "Handle a CTCP ECHO reply."
4213 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4214 (let ((message (match-string 1 msg)))
4215 (erc-display-message
4216 nil '(notice action) 'active
4217 'CTCP-ECHO ?n nick ?m message)))
4220 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4221 (defun erc-ctcp-reply-CLIENTINFO (proc nick login host to msg)
4222 "Handle a CTCP CLIENTINFO reply."
4223 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4224 (let ((message (match-string 1 msg)))
4225 (erc-display-message
4227 'CTCP-CLIENTINFO ?n nick ?m message)))
4230 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4231 (defun erc-ctcp-reply-FINGER (proc nick login host to msg)
4232 "Handle a CTCP FINGER reply."
4233 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4234 (let ((message (match-string 1 msg)))
4235 (erc-display-message
4237 'CTCP-FINGER ?n nick ?m message)))
4240 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4241 (defun erc-ctcp-reply-PING (proc nick login host to msg)
4242 "Handle a CTCP PING reply."
4243 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4245 (let ((time (match-string 1 msg)))
4247 (let ((delta (erc-time-diff (string-to-number time)
4248 (erc-current-time))))
4249 (erc-display-message
4252 ?t (erc-sec-to-time delta)))
4254 (erc-display-message
4256 'bad-ping-response ?n nick ?t time))))))
4258 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4259 (defun erc-ctcp-reply-TIME (proc nick login host to msg)
4260 "Handle a CTCP TIME reply."
4261 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4262 (let ((message (match-string 1 msg)))
4263 (erc-display-message
4265 'CTCP-TIME ?n nick ?m message)))
4268 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4269 (defun erc-ctcp-reply-VERSION (proc nick login host to msg)
4270 "Handle a CTCP VERSION reply."
4271 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4272 (let ((message (match-string 1 msg)))
4273 (erc-display-message
4275 'CTCP-VERSION ?n nick ?m message)))
4278 (defun erc-process-away (proc away-p)
4279 ;; FIXME: This docstring is AWFUL -- Lawrence 2004-01-08
4280 "Process the user being away, or returning from an away break."
4281 (let ((sessionbuf (process-buffer proc)))
4283 (with-current-buffer sessionbuf
4284 (when erc-away-nickname
4285 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4286 erc-away-nickname away-p))
4287 (erc-cmd-NICK (if away-p
4292 (erc-with-all-buffers-of-server proc nil
4293 (setq erc-away (current-time))))
4295 (let ((away-time erc-away))
4296 ;; away must be set to NIL BEFORE sending anything to prevent
4297 ;; an infinite recursion
4298 (erc-with-all-buffers-of-server proc nil
4299 (setq erc-away nil))
4301 (set-buffer (erc-active-buffer))
4302 (when erc-public-away-p
4304 (erc-default-target)
4306 (format "is back (gone for %s)"
4309 (erc-emacs-time-to-erc-time away-time)
4310 (erc-current-time))))
4312 (erc-update-mode-line)))
4314 ;;;; List of channel members handling
4316 (defun erc-channel-begin-receiving-names ()
4319 Used when a channel names list is about to be received. Should
4320 be called with the current buffer set to the channel buffer.
4322 See also `erc-channel-end-receiving-names'."
4323 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4325 (defun erc-channel-end-receiving-names ()
4328 Used to fix `erc-channel-users' after a channel names list has been
4329 received. Should be called with the current buffer set to the
4332 See also `erc-channel-begin-receiving-names'."
4333 (maphash (lambda (nick user)
4334 (if (null (gethash nick erc-channel-new-member-names))
4335 (erc-remove-channel-user nick)))
4337 (setq erc-channel-new-member-names nil))
4339 (defun erc-channel-receive-names (names-string)
4340 "This function is for internal use only.
4342 Update `erc-channel-users' according to NAMES-STRING.
4343 NAMES-STRING is a string listing some of the names on the
4345 (let (names name op voice)
4346 ;; We need to delete "" because in XEmacs, (split-string "a ")
4347 ;; returns ("a" "").
4348 (setq names (delete "" (split-string names-string)))
4349 (let ((erc-channel-members-changed-hook nil))
4350 (dolist (item names)
4351 (cond ((string-match "^@\\(.*\\)$" item)
4352 (setq name (match-string 1 item)
4355 ((string-match "^+\\(.*\\)$" item)
4356 (setq name (match-string 1 item)
4362 (puthash (erc-downcase name) t
4363 erc-channel-new-member-names)
4364 (erc-update-current-channel-member
4365 name name t op voice)))
4366 (run-hooks 'erc-channel-members-changed-hook)))
4368 (defcustom erc-channel-members-changed-hook nil
4369 "*This hook is called every time the variable `channel-members' changes.
4370 The buffer where the change happened is current while this hook is called."
4374 (defun erc-update-user-nick (nick &optional new-nick
4375 host login full-name info)
4376 "Updates the stored user information for the user with nickname
4379 See also: `erc-update-user'"
4380 (erc-update-user (erc-get-server-user nick) new-nick
4381 host login full-name info))
4383 (defun erc-update-user (user &optional new-nick
4384 host login full-name info)
4385 "Update user info for USER. USER must be an erc-server-user
4386 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4387 non-nil and not equal to the existing values for USER are used to
4388 replace the stored values in USER.
4390 If, any only if a change is made,
4391 `erc-channel-members-changed-hook' is run for each channel for
4392 which USER is a member, and `t' is returned."
4396 (not (equal (erc-server-user-nickname user)
4399 (erc-change-user-nickname user new-nick))
4401 (not (equal (erc-server-user-host user) host)))
4403 (setf (erc-server-user-host user) host))
4405 (not (equal (erc-server-user-login user) login)))
4407 (setf (erc-server-user-login user) login))
4408 (when (and full-name
4409 (not (equal (erc-server-user-full-name user)
4412 (setf (erc-server-user-full-name user) full-name))
4414 (not (equal (erc-server-user-info user) info)))
4416 (setf (erc-server-user-info user) info))
4418 (dolist (buf (erc-server-user-buffers user))
4419 (if (buffer-live-p buf)
4420 (with-current-buffer buf
4421 (run-hooks 'erc-channel-members-changed-hook))))))
4424 (defun erc-update-current-channel-member
4425 (nick new-nick &optional add op voice host login full-name info
4426 update-message-time)
4427 "Updates the stored user information for the user with nickname
4428 NICK. `erc-update-user' is called to handle changes to nickname,
4429 host, login, full-name, and info. If `op' or `voice' are
4430 non-nil, they must be equal to either `on' or `off', in which
4431 case the operator or voice status of USER in the current channel
4432 is changed accordingly. If `update-message-time' is non-nil, the
4433 last-message-time of the user in the current channel is set
4436 If ADD is non-nil, the user will be added with the specified
4437 information if it is not already present in the user or channel
4440 If, and only if, changes are made, or the user is added,
4441 `erc-channel-members-updated-hook' is run, and `t' is returned.
4443 See also: `erc-update-user' and `erc-update-channel-member'."
4444 (let* (changed user-changed
4445 (channel-data (erc-get-channel-user nick))
4446 (cuser (if channel-data (cdr channel-data)))
4447 (user (if channel-data (car channel-data)
4448 (erc-get-server-user nick))))
4451 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4453 (not (eq (erc-channel-user-op cuser) op)))
4455 (setf (erc-channel-user-op cuser)
4456 (cond ((eq op 'on) t)
4460 (not (eq (erc-channel-user-voice cuser) voice)))
4462 (setf (erc-channel-user-voice cuser)
4463 (cond ((eq voice 'on) t)
4464 ((eq voice 'off) nil)
4466 (when update-message-time
4467 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4469 (erc-update-user user new-nick
4470 host login full-name info)))
4474 (setq user (make-erc-server-user
4477 :full-name full-name
4480 :buffers (list (current-buffer))))
4481 (erc-add-server-user nick user))
4482 (setf (erc-server-user-buffers user)
4483 (cons (current-buffer)
4484 (erc-server-user-buffers user))))
4485 (setq cuser (make-erc-channel-user
4486 :op (cond ((eq op 'on) t)
4489 :voice (cond ((eq voice 'on) t)
4490 ((eq voice 'off) nil)
4493 (if update-message-time (current-time))))
4494 (puthash (erc-downcase nick) (cons user cuser)
4497 (when (and changed (null user-changed))
4498 (run-hooks 'erc-channel-members-changed-hook))
4499 (or changed user-changed add)))
4501 (defun erc-update-channel-member (channel nick new-nick
4502 &optional add op voice host login
4503 full-name info update-message-time)
4504 "Updates user and channel information for the user with
4505 nickname NICK in channel CHANNEL.
4507 See also: `erc-update-current-channel-member'"
4510 (erc-update-current-channel-member nick new-nick add op voice host
4511 login full-name info
4512 update-message-time)))
4514 (defun erc-remove-current-channel-member (nick)
4515 "Remove NICK from current channel membership list. Runs
4516 `erc-channel-members-changed-hook'."
4517 (let ((channel-data (erc-get-channel-user nick)))
4519 (erc-remove-channel-user nick)
4520 (run-hooks 'erc-channel-members-changed-hook))))
4522 (defun erc-remove-channel-member (channel nick)
4523 "Remove NICK from CHANNEL's membership list.
4525 See also `erc-remove-current-channel-member'."
4528 (erc-remove-current-channel-member nick)))
4530 (defun erc-update-channel-topic (channel topic &optional modify)
4531 "Find a buffer for CHANNEL and set the TOPIC for it.
4533 If optional MODIFY is 'append or 'prepend, then append or prepend the
4534 TOPIC string to the current topic."
4535 (erc-with-buffer (channel)
4536 (cond ((eq modify 'append)
4537 (setq erc-channel-topic (concat erc-channel-topic topic)))
4538 ((eq modify 'prepend)
4539 (setq erc-channel-topic (concat topic erc-channel-topic)))
4540 (t (setq erc-channel-topic topic)))
4541 (erc-update-mode-line-buffer (current-buffer))))
4543 (defun erc-set-modes (tgt mode-string)
4544 "Set the modes for the TGT provided as MODE-STRING."
4545 (let* ((modes (erc-parse-modes mode-string))
4546 (add-modes (nth 0 modes))
4547 (remove-modes (nth 1 modes))
4548 ;; list of triples: (mode-char 'on/'off argument)
4549 (arg-modes (nth 2 modes)))
4550 (cond ((erc-channel-p tgt); channel modes
4551 (let ((buf (and (boundp 'erc-server-process) erc-server-process
4552 (erc-get-buffer tgt erc-server-process))))
4554 (with-current-buffer buf
4555 (setq erc-channel-modes add-modes)
4556 (setq erc-channel-user-limit nil)
4557 (setq erc-channel-key nil)
4559 (let ((mode (nth 0 (car arg-modes)))
4560 (onoff (nth 1 (car arg-modes)))
4561 (arg (nth 2 (car arg-modes))))
4562 (cond ((string-match "^[Ll]" mode)
4563 (erc-update-channel-limit tgt onoff arg))
4564 ((string-match "^[Kk]" mode)
4565 (erc-update-channel-key tgt onoff arg))
4567 (setq arg-modes (cdr arg-modes)))
4568 (erc-update-mode-line-buffer buf)))))
4569 ;; we do not keep our nick's modes yet
4570 ;;(t (setq erc-user-modes add-modes))
4574 (defun erc-sort-strings (list-of-strings)
4575 "Sort LIST-OF-STRINGS in lexicographic order.
4578 (sort (copy-sequence list-of-strings) 'string<))
4580 (defun erc-parse-modes (mode-string)
4581 "Parse MODE-STRING into a list.
4583 Returns a list of three elements:
4585 (ADD-MODES REMOVE-MODES ARG-MODES).
4587 The add-modes and remove-modes are lists of single-character strings
4588 for modes without parameters to add and remove respectively. The
4589 arg-modes is a list of triples of the form:
4591 (MODE-CHAR ON/OFF ARGUMENT)."
4592 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
4593 (let ((chars (mapcar 'char-to-string (match-string 1 mode-string)))
4594 ;; arguments in channel modes
4595 (args-str (match-string 2 mode-string))
4599 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
4601 ;; make the argument list
4602 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
4603 (setq args (cons (match-string 1 args-str) args))
4604 (setq args-str (match-string 2 args-str)))
4605 (setq args (nreverse args))
4606 ;; collect what modes changed, and match them with arguments
4608 (cond ((string= (car chars) "+") (setq add-p t))
4609 ((string= (car chars) "-") (setq add-p nil))
4610 ((string-match "^[ovbOVB]" (car chars))
4611 (setq arg-modes (cons (list (car chars)
4613 (if args (car args) nil))
4615 (if args (setq args (cdr args))))
4616 ((string-match "^[LlKk]" (car chars))
4617 (setq arg-modes (cons (list (car chars)
4619 (if (and add-p args)
4622 (if (and add-p args) (setq args (cdr args))))
4623 (add-p (setq add-modes (cons (car chars) add-modes)))
4624 (t (setq remove-modes (cons (car chars) remove-modes))))
4625 (setq chars (cdr chars)))
4626 (setq add-modes (nreverse add-modes))
4627 (setq remove-modes (nreverse remove-modes))
4628 (setq arg-modes (nreverse arg-modes))
4629 (list add-modes remove-modes arg-modes))
4632 (defun erc-update-modes (tgt mode-string &optional nick host login)
4633 "Update the mode information for TGT, provided as MODE-STRING.
4634 Optional arguments: NICK, HOST and LOGIN - the attributes of the
4635 person who changed the modes."
4636 (let* ((modes (erc-parse-modes mode-string))
4637 (add-modes (nth 0 modes))
4638 (remove-modes (nth 1 modes))
4639 ;; list of triples: (mode-char 'on/'off argument)
4640 (arg-modes (nth 2 modes)))
4641 ;; now parse the modes changes and do the updates
4642 (cond ((erc-channel-p tgt); channel modes
4643 (let ((buf (and (boundp 'erc-server-process) erc-server-process
4644 (erc-get-buffer tgt erc-server-process))))
4646 ;; FIXME! This used to have an original buffer
4647 ;; variable, but it never switched back to the original
4648 ;; buffer. Is this wanted behavior?
4650 (if (not (boundp 'erc-channel-modes))
4651 (setq erc-channel-modes nil))
4653 (setq erc-channel-modes (delete (car remove-modes)
4655 remove-modes (cdr remove-modes)))
4657 (setq erc-channel-modes (cons (car add-modes)
4659 add-modes (cdr add-modes)))
4660 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
4662 (let ((mode (nth 0 (car arg-modes)))
4663 (onoff (nth 1 (car arg-modes)))
4664 (arg (nth 2 (car arg-modes))))
4665 (cond ((string-match "^[oO]" mode)
4666 (erc-update-channel-member tgt arg arg nil onoff))
4667 ((string-match "^[Vv]" mode)
4668 (erc-update-channel-member tgt arg arg nil nil
4670 ((string-match "^[Ll]" mode)
4671 (erc-update-channel-limit tgt onoff arg))
4672 ((string-match "^[Kk]" mode)
4673 (erc-update-channel-key tgt onoff arg))
4674 (t nil)); only ops are tracked now
4675 (setq arg-modes (cdr arg-modes))))
4676 (erc-update-mode-line buf))))
4677 ;; nick modes - ignored at this point
4680 (defun erc-update-channel-limit (channel onoff n)
4681 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
4682 "Update CHANNEL's user limit to N."
4683 (if (or (not (eq onoff 'on))
4684 (and (stringp n) (string-match "^[0-9]+$" n)))
4687 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
4688 (t (setq erc-channel-user-limit nil))))))
4690 (defun erc-update-channel-key (channel onoff key)
4691 "Update CHANNEL's key to KEY if ONOFF is 'on or to nil if it's 'off."
4694 (cond ((eq onoff 'on) (setq erc-channel-key key))
4695 (t (setq erc-channel-key nil)))))
4697 (defun erc-handle-user-status-change (type nlh &optional l)
4698 "Handle changes in any user's status.
4700 So far, only nick change is handled.
4702 Generally, the TYPE argument is a symbol describing the change type, NLH is
4703 a list containing the original nickname, login name and hostname for the user,
4704 and L is a list containing additional TYPE-specific arguments.
4706 So far the following TYPE/L pairs are supported:
4710 nickname change 'nick (NEW-NICK)"
4711 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
4719 (defun erc-highlight-notice (s)
4720 "Highlight notice message S and return it.
4721 See also variable `erc-notice-highlight-type'"
4723 ((eq erc-notice-highlight-type 'prefix)
4724 (erc-put-text-property 0 (length erc-notice-prefix)
4725 'face 'erc-notice-face s)
4727 ((eq erc-notice-highlight-type 'all)
4728 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
4732 (defun erc-make-notice (message)
4733 "Notify the user of MESSAGE."
4734 (when erc-minibuffer-notice
4735 (message "%s" message))
4736 (erc-highlight-notice (concat erc-notice-prefix message)))
4738 (defun erc-highlight-error (s)
4739 "Highlight error message S and return it."
4740 (erc-put-text-property 0 (length s) 'face 'erc-error-face s)
4743 (defun erc-put-text-property (start end property value &optional object)
4744 "Set text-property for an object (usually a string).
4745 START and END define the characters covered.
4746 PROPERTY is the text-property set, usually the symbol `face'.
4747 VALUE is the value for the text-property, usually a face symbol such as
4748 the face `bold' or `erc-pal-face'.
4749 OBJECT is a string which will be modified and returned.
4750 OBJECT is modified without being copied first.
4752 You can redefine or `defadvice' this function in order to add
4753 EmacsSpeak support."
4754 (put-text-property start end property value object))
4756 (defun erc-list (thing)
4757 "Return THING if THING is a list, or a list with THING as its element."
4762 (defun erc-parse-user (string)
4763 "Parse STRING as a user specification (nick!login@host).
4765 Return a list of the three separate tokens."
4767 ((string-match "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$" string)
4768 (list (match-string 1 string)
4769 (match-string 2 string)
4770 (match-string 3 string)))
4771 ;; Some bogus bouncers send Nick!(null), try to live with that.
4772 ((string-match "^\\([^!\n]*\\)!\\(.*\\)$" string)
4773 (list (match-string 1 string)
4775 (match-string 2 string)))
4777 (list string "" ""))))
4779 (defun erc-extract-nick (string)
4780 "Return the nick corresponding to a user specification STRING.
4782 See also `erc-parse-user'."
4783 (car (erc-parse-user string)))
4785 (defun erc-put-text-properties (start end properties
4786 &optional object value-list)
4787 "Set text-properties for OBJECT.
4789 START and END describe positions in OBJECT.
4790 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
4791 each property to the corresponding value in VALUE-LIST."
4793 (setq value-list (mapcar (lambda (x)
4796 (mapcar* (lambda (prop value)
4797 (erc-put-text-property start end prop value object))
4798 properties value-list))
4800 ;;; Input area handling:
4802 (defun erc-beg-of-input-line ()
4803 "Return the value of `point' at the beginning of the input line.
4805 Specifically, return the position of `erc-insert-marker'."
4806 (or (and (boundp 'erc-insert-marker)
4807 (markerp erc-insert-marker))
4808 (error "erc-insert-marker has no value, please report a bug"))
4809 (marker-position erc-insert-marker))
4811 (defun erc-end-of-input-line ()
4812 "Return the value of `point' at the end of the input line."
4815 (defun erc-send-current-line ()
4816 "Parse current line and send it to IRC."
4821 ((< (point) (erc-beg-of-input-line))
4822 (message "Point is not in the input area")
4824 ((not (erc-server-buffer-live-p))
4825 (message "ERC: No process running")
4828 (erc-set-active-buffer (current-buffer))
4829 (let ((inhibit-read-only t)
4830 (str (erc-user-input))
4831 (old-buf (current-buffer)))
4833 ;; Kill the input and the prompt
4834 (delete-region (erc-beg-of-input-line)
4835 (erc-end-of-input-line))
4838 (erc-send-input str)
4839 ;; Fix the buffer if the command didn't kill it
4840 (when (buffer-live-p old-buf)
4841 (with-current-buffer old-buf
4844 (goto-char (point-max))
4845 (set-marker (process-mark erc-server-process) (point))
4846 (set-marker erc-insert-marker (point))
4847 (let ((buffer-modified (buffer-modified-p)))
4848 (erc-display-prompt)
4849 (set-buffer-modified-p buffer-modified))))))
4851 ;; Only when last hook has been run...
4852 (run-hook-with-args 'erc-send-completed-hook str))))))
4854 (defun erc-user-input ()
4855 "Return the input of the user in the current buffer."
4858 (erc-end-of-input-line)))
4860 (defun erc-send-input (input)
4861 "Treat INPUT as typed in by the user. It is assumed that the input
4862 and the prompt is already deleted.
4863 This returns non-nil only iff we actually send anything."
4864 ;; Handle different kinds of inputs
4866 ;; Ignore empty input
4867 ((if erc-send-whitespace-lines
4869 (string-match "\\`[ \t\r\f\n]*\\'" input))
4870 (when erc-warn-about-blank-lines
4871 (message "Blank line - ignoring...")
4876 (erc-insert-this t))
4877 (setq erc-send-this t)
4878 (run-hook-with-args 'erc-send-pre-hook input)
4880 (if (or (string-match "\n" str)
4881 (not (char-equal (aref str 0) ?/)))
4886 ;; Insert what has to be inserted for this.
4887 (erc-display-msg line)
4888 (erc-process-input-line (concat line "\n")
4889 (null erc-flood-protect) t))
4890 (or (and erc-flood-protect (erc-split-line line))
4892 (split-string str "\n"))
4893 ;; Insert the prompt along with the command.
4894 (erc-display-command str)
4895 (erc-process-input-line (concat str "\n") t nil))
4898 (defun erc-display-command (line)
4899 (when erc-insert-this
4900 (let ((insert-position (point)))
4901 (unless erc-hide-prompt
4902 (erc-display-prompt nil nil (erc-command-indicator)
4903 (and (erc-command-indicator)
4904 'erc-command-indicator-face)))
4905 (let ((beg (point)))
4907 (erc-put-text-property beg (point)
4908 'face 'erc-command-indicator-face)
4910 (set-marker (process-mark erc-server-process) (point))
4911 (set-marker erc-insert-marker (point))
4914 (narrow-to-region insert-position (point))
4915 (run-hooks 'erc-send-modify-hook)
4916 (run-hooks 'erc-send-post-hook))))))
4918 (defun erc-display-msg (line)
4919 "Display LINE as a message of the user to the current target at the
4921 (when erc-insert-this
4922 (let ((insert-position (point)))
4923 (insert (erc-format-my-nick))
4924 (let ((beg (point)))
4926 (erc-put-text-property beg (point)
4927 'face 'erc-input-face))
4929 (set-marker (process-mark erc-server-process) (point))
4930 (set-marker erc-insert-marker (point))
4933 (narrow-to-region insert-position (point))
4934 (run-hooks 'erc-send-modify-hook)
4935 (run-hooks 'erc-send-post-hook))))))
4937 (defun erc-command-symbol (command)
4938 "Return the erc command symbol for COMMAND if it exists and is bound."
4939 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
4940 (when (fboundp cmd) cmd)))
4942 (defun erc-extract-command-from-line (line)
4943 "Extract command and args from the input LINE.
4944 If no command was given, return nil. If command matches, return a
4945 list of the form: (command args) where both elements are strings."
4946 (when (string-match "^/\\([A-Za-z]+\\)\\(\\s-+.*\\|\\s-*\\)$" line)
4947 (let* ((cmd (erc-command-symbol (match-string 1 line)))
4948 ;; note: return is nil, we apply this simply for side effects
4949 (canon-defun (while (and cmd (symbolp (symbol-function cmd)))
4950 (setq cmd (symbol-function cmd))))
4951 (cmd-fun (or cmd #'erc-cmd-default))
4953 (if (get cmd-fun 'do-not-parse-args)
4954 (format "%s" (match-string 2 line))
4955 (delete "" (split-string (erc-trim-string
4956 (match-string 2 line)) " ")))
4958 (list cmd-fun arg))))
4960 (defun erc-split-multiline-safe (string)
4961 "Split STRING, containing multiple lines and return them in a list.
4962 Do it only for STRING as the complete input, do not carry unfinished
4963 strings over to the next call."
4968 (let ((i (string-match "\r?\n" string i0))
4969 (s (substring string i0)))
4970 (cond (i (setq l (cons (substring string i0 i) l))
4971 (setq i0 (match-end 0)))
4973 (setq l (cons s l))(setq doit nil))
4974 (t (setq doit nil)))))
4979 (defun erc-set-current-nick (nick)
4980 "Set the current nickname to NICK."
4981 (with-current-buffer (or (erc-server-buffer)
4983 (setq erc-server-current-nick nick)))
4985 (defun erc-current-nick ()
4986 "Return the current nickname."
4987 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
4990 erc-server-current-nick))
4992 (defun erc-current-nick-p (nick)
4993 "Return non-nil if NICK is the current nickname."
4994 (erc-nick-equal-p nick (erc-current-nick)))
4996 (defun erc-nick-equal-p (nick1 nick2)
4997 "Return non-nil if NICK1 and NICK2 are the same.
4999 This matches strings according to the IRC protocol's case convention.
5001 See also `erc-downcase'."
5002 (string= (erc-downcase nick1)
5003 (erc-downcase nick2)))
5005 ;; default target handling
5007 (defun erc-default-target ()
5008 "Return the current default target (as a character string) or nil if none."
5009 (let ((tgt (car erc-default-recipients)))
5012 ((listp tgt) (cdr tgt))
5015 (defun erc-add-default-channel (channel)
5016 "Add CHANNEL to the default channel list."
5018 (let ((d1 (car erc-default-recipients))
5019 (d2 (cdr erc-default-recipients))
5020 (chl (downcase channel)))
5021 (setq erc-default-recipients
5022 (cons chl erc-default-recipients))))
5024 (defun erc-delete-default-channel (channel &optional buffer)
5025 "Delete CHANNEL from the default channel list."
5026 (let ((ob (current-buffer)))
5027 (with-current-buffer (if (and buffer
5031 (setq erc-default-recipients (delete (downcase channel)
5032 erc-default-recipients)))))
5034 (defun erc-add-query (nickname)
5035 "Add QUERY'd NICKNAME to the default channel list.
5037 The previous default target of QUERY type gets removed"
5038 (let ((d1 (car erc-default-recipients))
5039 (d2 (cdr erc-default-recipients))
5040 (qt (cons 'QUERY (downcase nickname))))
5042 (eq (car d1) 'QUERY))
5043 (setq erc-default-recipients (cons qt d2))
5044 (setq erc-default-recipients (cons qt erc-default-recipients)))))
5046 (defun erc-delete-query ()
5047 "Delete the topmost target if it is a QUERY."
5049 (let ((d1 (car erc-default-recipients))
5050 (d2 (cdr erc-default-recipients)))
5052 (eq (car d1) 'QUERY))
5053 (setq erc-default-recipients d2)
5054 (error "Current target is not a QUERY"))))
5056 (defun erc-ignored-user-p (spec)
5057 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5059 Takes a full SPEC of a user in the form \"nick!login@host\", and
5060 matches against all the regexp's in `erc-ignore-list'. If any
5061 match, returns that regexp."
5062 (dolist (ignored (with-current-buffer (erc-server-buffer) erc-ignore-list))
5063 (if (string-match ignored spec)
5064 ;; We have `require'd cl, so we can return from the block named nil
5067 (defun erc-ignored-reply-p (msg tgt proc)
5068 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5069 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5071 Takes a message MSG to a channel and returns non-nil if the addressed
5072 user matches any regexp in `erc-ignore-reply-list'."
5073 (let ((target-nick (erc-message-target msg)))
5074 (if (not target-nick)
5076 (erc-with-buffer (tgt proc)
5077 (let ((user (erc-get-server-user target-nick)))
5079 (erc-list-match erc-ignore-reply-list
5080 (erc-user-spec user))))))))
5082 (defun erc-message-target (msg)
5083 "Return the addressed target in MSG.
5085 The addressed target is the string before the first colon in MSG."
5086 (if (string-match "^\\([^: \n]*\\):" msg)
5087 (match-string 1 msg)
5090 (defun erc-user-spec (user)
5091 "Create a nick!user@host spec from a user struct."
5092 (let ((nick (erc-server-user-nickname user))
5093 (host (erc-server-user-host user))
5094 (login (erc-server-user-login user)))
5107 (defun erc-list-match (lst str)
5108 "Return non-nil if any regexp in LST matches STR."
5109 (memq nil (mapcar (lambda (regexp)
5110 (not (string-match regexp str)))
5115 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5116 "Toggle automatic CTCP replies (like VERSION and PING).
5118 If ARG is positive, turns CTCP replies on.
5120 If ARG is non-nil and not positive, turns CTCP replies off."
5122 (cond ((and (numberp arg) (> arg 0))
5123 (setq erc-disable-ctcp-replies t))
5124 (arg (setq erc-disable-ctcp-replies nil))
5125 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5126 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5128 (defun erc-toggle-flood-control (&optional arg)
5129 "Toggle use of flood control on sent messages.
5131 If ARG is positive, use flood control.
5132 If ARG is non-nil and not positive, do not use flood control.
5134 See `erc-server-flood-margin' for an explanation of the available
5135 flood control parameters."
5137 (cond ((and (numberp arg) (> arg 0))
5138 (setq erc-flood-protect t))
5139 (arg (setq erc-flood-protect nil))
5140 (t (setq erc-flood-protect (not erc-flood-protect))))
5141 (message "ERC flood control is %s"
5142 (cond (erc-flood-protect "ON")
5145 ;; Some useful channel and nick commands for fast key bindings
5147 (defun erc-invite-only-mode (&optional arg)
5148 "Turn on the invite only mode (+i) for the current channel.
5150 If ARG is non-nil, turn this mode off (-i).
5152 This command is sent even if excess flood is detected."
5154 (erc-set-active-buffer (current-buffer))
5155 (let ((tgt (erc-default-target))
5157 (cond ((or (not tgt) (not (erc-channel-p tgt)))
5158 (erc-display-message nil 'error (current-buffer) 'no-target))
5159 (arg (erc-load-irc-script-lines (list (concat "/mode " tgt " -i"))
5161 (t (erc-load-irc-script-lines (list (concat "/mode " tgt " +i"))
5164 (defun erc-get-channel-mode-from-keypress (key)
5165 "Read a key sequence and call the corresponding channel mode function.
5166 After doing C-c C-o, type in a channel mode letter.
5169 RET lets you type more than one mode at a time.
5170 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5171 If \"k\" is pressed, `erc-set-channel-key' gets called.
5172 Anything else will be sent to `erc-toggle-channel-mode'."
5173 (interactive "kChannel mode (RET to set more than one): ")
5174 (when (featurep 'xemacs)
5175 (setq key (char-to-string (event-to-character (aref key 0)))))
5176 (cond ((equal key "\C-g")
5179 (erc-insert-mode-command))
5181 (call-interactively 'erc-set-channel-limit))
5183 (call-interactively 'erc-set-channel-key))
5184 (t (erc-toggle-channel-mode key))))
5186 (defun erc-toggle-channel-mode (mode &optional channel)
5187 "Toggle channel MODE.
5189 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5190 `erc-default-target'."
5192 (erc-set-active-buffer (current-buffer))
5193 (let ((tgt (or channel (erc-default-target)))
5195 (cond ((or (null tgt) (null (erc-channel-p tgt)))
5196 (erc-display-message nil 'error 'active 'no-target))
5197 ((member mode erc-channel-modes)
5198 (erc-log (format "%s: Toggle mode %s OFF" tgt mode))
5199 (message "Toggle channel mode %s OFF" mode)
5200 (erc-server-send (format "MODE %s -%s" tgt mode)))
5201 (t (erc-log (format "%s: Toggle channel mode %s ON" tgt mode))
5202 (message "Toggle channel mode %s ON" mode)
5203 (erc-server-send (format "MODE %s +%s" tgt mode))))))
5205 (defun erc-insert-mode-command ()
5206 "Insert the line \"/mode <current target> \" at `point'."
5208 (let ((tgt (erc-default-target)))
5209 (if tgt (insert (concat "/mode " tgt " "))
5210 (erc-display-message nil 'error (current-buffer) 'no-target))))
5212 (defun erc-channel-names ()
5213 "Run \"/names #channel\" in the current channel."
5215 (erc-set-active-buffer (current-buffer))
5216 (let ((tgt (erc-default-target)))
5217 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5218 (erc-display-message nil 'error (current-buffer) 'no-target))))
5220 (defun erc-remove-text-properties-region (start end &optional object)
5221 "Clears the region (START,END) in OBJECT from all colors, etc."
5224 (let ((inhibit-read-only t))
5225 (set-text-properties start end nil object))))
5227 ;; script execution and startup
5229 (defun erc-find-file (file &optional path)
5230 "Search for a FILE in the filesystem.
5231 First the `default-directory' is searched for FILE, then any directories
5232 specified in the list PATH.
5234 If FILE is found, return the path to it."
5235 (let ((filepath file))
5236 (if (file-readable-p filepath) filepath
5239 (progn (setq filepath (expand-file-name file (car path)))
5240 (not (file-readable-p filepath))))
5241 (setq path (cdr path)))
5242 (if path filepath nil)))))
5244 (defun erc-select-startup-file ()
5245 "Select an ERC startup file.
5246 See also `erc-startup-file-list'."
5248 (dolist (f erc-startup-file-list)
5249 (setq f (convert-standard-filename f))
5250 (when (file-readable-p f)
5251 (throw 'found f)))))
5253 (defun erc-find-script-file (file)
5254 "Search for FILE in `default-directory', and any in `erc-script-path'."
5255 (erc-find-file file erc-script-path))
5257 (defun erc-load-script (file)
5258 "Load a script from FILE.
5260 FILE must be the full name, it is not searched in the
5261 `erc-script-path'. If the filename ends with `.el', then load it
5262 as a emacs-lisp program. Otherwise, treat it as a regular IRC
5264 (erc-log (concat "erc-load-script: " file))
5266 ((string-match "\\.el$" file)
5269 (erc-load-irc-script file))))
5271 (defun erc-process-script-line (line &optional args)
5272 "Process an IRC script LINE.
5274 Does script-specific substitutions (script arguments, current nick,
5275 server, etc.) in LINE and returns it.
5277 Substitutions are: %C and %c = current target (channel or nick),
5278 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5279 where x is any other character;
5280 $* = the entire argument string, $1 = the first argument, $2 = the second,
5282 (if (not args) (setq args ""))
5283 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5284 (percent-regexp "\\(%.\\)")
5285 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5286 (tgt (erc-default-target))
5287 (server (and (boundp 'erc-session-server) erc-session-server))
5288 (nick (erc-current-nick))
5293 (if (not tgt) (setq tgt ""))
5294 (if (not server) (setq server ""))
5295 (if (not nick) (setq nick ""))
5296 ;; First, compute the argument list
5298 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5299 (setq arg-list (cons (match-string 1 tmp) arg-list))
5300 (setq tmp (match-string 2 tmp)))
5301 (setq arg-list (nreverse arg-list))
5302 (setq arg-num (length arg-list))
5303 ;; now do the substitution
5304 (setq tmp (string-match esc-regexp line))
5306 ;;(message "beginning of while: tmp=%S" tmp)
5307 (let* ((hd (substring line 0 tmp))
5310 (tail (substring line tmp)))
5311 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5312 (setq esc (match-string 1 tail))
5313 (setq tail (substring tail (match-end 1))))
5314 ((string-match (concat "^" percent-regexp) tail)
5315 (setq esc (match-string 1 tail))
5316 (setq tail (substring tail (match-end 1)))))
5317 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5318 (setq res (concat res hd))
5320 (cond ((string= esc "") "")
5321 ((string-match "^\\$\\*$" esc) args)
5322 ((string-match "^\\$\\([0-9]+\\)$" esc)
5323 (let ((n (string-to-number (match-string 1 esc))))
5324 (message "n = %S, integerp(n)=%S" n (integerp n))
5325 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5326 ((string-match "^%[Cc]$" esc) tgt)
5327 ((string-match "^%[Ss]$" esc) server)
5328 ((string-match "^%[Nn]$" esc) nick)
5329 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5330 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5331 (message "BUG IN ERC: esc=%S" esc)
5334 (setq tmp (string-match esc-regexp line))
5335 (setq res (concat res subst))
5336 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5338 (setq res (concat res line))
5341 (defun erc-load-irc-script (file &optional force)
5342 "Load an IRC script from FILE."
5343 (erc-log (concat "erc-load-script: " file))
5344 (let ((str (with-temp-buffer
5345 (insert-file-contents file)
5347 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5349 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5350 "Load IRC script LINES (a list of strings).
5352 If optional NOEXPAND is non-nil, do not expand script-specific
5353 sequences, process the lines verbatim. Use this for multiline
5355 (let* ((cb (current-buffer))
5358 (sp (or (erc-command-indicator) (erc-prompt)))
5359 (args (and (boundp 'erc-script-args) erc-script-args)))
5360 (if (and args (string-match "^ " args))
5361 (setq args (substring args 1)))
5362 ;; prepare the prompt string for echo
5363 (erc-put-text-property 0 (length sp)
5364 'face 'erc-command-indicator-face sp)
5366 (setq s (car lines))
5367 (erc-log (concat "erc-load-script: CMD: " s))
5368 (unless (string-match "^\\s-*$" s)
5369 (let ((line (if noexpand s (erc-process-script-line s args))))
5370 (if (and (erc-process-input-line line force)
5373 (erc-put-text-property 0 (length line)
5374 'face 'erc-input-face line)
5375 (erc-display-line (concat sp line) cb)))))
5376 (setq lines (cdr lines)))))
5381 "Perform user authentication at the IRC server."
5382 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5387 erc-session-user-full-name))
5388 (if erc-session-password
5389 (erc-server-send (format "PASS %s" erc-session-password))
5390 (message "Logging in without password"))
5391 (erc-server-send (format "NICK %s" (erc-current-nick)))
5393 (format "USER %s %s %s :%s"
5395 (if erc-anonymous-login erc-email-userid (user-login-name))
5397 erc-session-user-full-name))
5398 (erc-update-mode-line))
5400 ;; connection properties' heuristics
5402 (defun erc-determine-parameters (&optional server port nick name)
5403 "Determine the connection and authentication parameters.
5404 Sets the buffer local variables:
5406 - erc-session-server
5408 - erc-session-full-name
5409 - erc-server-current-nick"
5410 (setq erc-session-server (erc-compute-server server)
5411 erc-session-port (or port erc-default-port)
5412 erc-session-user-full-name (erc-compute-full-name name))
5413 (erc-set-current-nick (erc-compute-nick nick)))
5415 (defun erc-compute-server (&optional server)
5416 "Return an IRC server name.
5418 This tries a number of increasingly more default methods until a
5419 non-nil value is found.
5421 - SERVER (the argument passwd to this function)
5422 - The `erc-server' option
5423 - The value of the IRCSERVER environment variable
5424 - The `erc-default-server' variable"
5427 (getenv "IRCSERVER")
5428 erc-default-server))
5430 (defun erc-compute-nick (&optional nick)
5431 "Return user's IRC nick.
5433 This tries a number of increasingly more default methods until a
5434 non-nil value is found.
5436 - NICK (the argument passed to this function)
5437 - The `erc-nick' option
5438 - The value of the IRCNICK environment variable
5439 - The result from the `user-login-name' function"
5441 (if (consp erc-nick) (car erc-nick) erc-nick)
5446 (defun erc-compute-full-name (&optional full-name)
5447 "Return user's full name.
5449 This tries a number of increasingly more default methods until a
5450 non-nil value is found.
5452 - FULL-NAME (the argument passed to this function)
5453 - The `erc-user-full-name' option
5454 - The value of the IRCNAME environment variable
5455 - The result from the `user-full-name' function"
5459 (if erc-anonymous-login "unknown" nil)
5462 (defun erc-compute-port (&optional port)
5463 "Return a port for an IRC server.
5465 This tries a number of increasingly more default methods until a
5466 non-nil value is found.
5468 - PORT (the argument passed to this function)
5469 - The `erc-port' option
5470 - The `erc-default-port' variable"
5471 (or port erc-port erc-default-port))
5475 (defun erc-string-to-emacs-time (string)
5476 "Convert the long number represented by STRING into an Emacs format.
5477 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
5478 (let* ((n (string-to-number (concat string ".0"))))
5479 (list (truncate (/ n 65536))
5480 (truncate (mod n 65536)))))
5482 (defun erc-emacs-time-to-erc-time (time)
5483 "Convert Emacs TIME to a number of seconds since the epoch."
5485 (+ (* (nth 0 time) 65536.0) (nth 1 time))))
5486 ; (round (+ (* (nth 0 tm) 65536.0) (nth 1 tm))))
5488 (defun erc-current-time ()
5489 "Return the `current-time' as a number of seconds since the epoch.
5491 See also `erc-emacs-time-to-erc-time'."
5492 (erc-emacs-time-to-erc-time (current-time)))
5494 (defun erc-time-diff (t1 t2)
5495 "Return the time difference in seconds between T1 and T2."
5498 (defun erc-time-gt (t1 t2)
5499 "Check whether T1 > T2."
5502 (defun erc-sec-to-time (ns)
5503 "Convert NS to a time string HH:MM.SS."
5504 (setq ns (truncate ns))
5505 (format "%02d:%02d.%02d"
5510 (defun erc-seconds-to-string (seconds)
5511 "Convert a number of SECONDS into an English phrase."
5512 (let (days hours minutes format-args output)
5513 (setq days (/ seconds 86400)
5514 seconds (% seconds 86400)
5515 hours (/ seconds 3600)
5516 seconds (% seconds 3600)
5517 minutes (/ seconds 60)
5518 seconds (% seconds 60)
5519 format-args (if (> days 0)
5520 `("%d days, %d hours, %d minutes, %d seconds"
5521 ,days ,hours ,minutes ,seconds)
5523 `("%d hours, %d minutes, %d seconds"
5524 ,hours ,minutes ,seconds)
5526 `("%d minutes, %d seconds" ,minutes ,seconds)
5527 `("%d seconds" ,seconds))))
5528 output (apply 'format format-args))
5529 ;; Change all "1 units" to "1 unit".
5530 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
5531 (setq output (erc-replace-match-subexpression-in-string
5532 "" output (match-string 2 output) 2 (match-beginning 2))))
5538 (defconst erc-clientinfo-alist
5539 '(("ACTION" . "is used to inform about one's current activity")
5540 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
5541 ("ECHO" . "echoes its arguments back")
5542 ("FINGER" . "shows user's name, location, and idle time")
5543 ("PING" . "measures delay between peers")
5544 ("TIME" . "shows client-side time")
5545 ("USERINFO" . "shows information provided by a user")
5546 ("VERSION" . "shows client type and version"))
5547 "Alist of CTCP CLIENTINFO for ERC commands.")
5549 (defun erc-client-info (s)
5550 "Return CTCP CLIENTINFO on command S.
5551 If S is NIL or an empty string then return general CLIENTINFO"
5552 (if (or (not s) (string= s ""))
5556 (concat (car e) " "))
5557 erc-clientinfo-alist))
5558 ": use CLIENTINFO <COMMAND> to get more specific information")
5559 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
5561 (concat s " " (cdr h))
5562 (concat s ": unknown command")))))
5566 (defun erc-directory-writable-p (dir)
5567 "Determine whether DIR is a writable directory.
5568 If it doesn't exist, create it."
5569 (unless (file-attributes dir) (make-directory dir))
5570 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
5572 (defun erc-kill-query-buffers (process)
5573 "Kill all buffers of PROCESS."
5574 ;; here, we only want to match the channel buffers, to avoid
5575 ;; "selecting killed buffers" b0rkage.
5576 (erc-with-all-buffers-of-server process
5578 (not (erc-server-buffer-p)))
5579 (kill-buffer (current-buffer))))
5581 (defun erc-nick-at-point ()
5582 "Give information about the nickname at `point'.
5584 If called interactively, give a human readable message in the
5585 minibuffer. If called programatically, return the corresponding
5586 entry of `channel-members'."
5588 (require 'thingatpt)
5589 (let* ((word (word-at-point))
5590 (channel-data (erc-get-channel-user word))
5591 (cuser (cdr channel-data))
5592 (user (if channel-data
5594 (erc-get-server-user word)))
5595 host login full-name info nick op voice)
5597 (setq nick (erc-server-user-nickname user)
5598 host (erc-server-user-host user)
5599 login (erc-server-user-login user)
5600 full-name (erc-server-user-full-name user)
5601 info (erc-server-user-info user))
5603 (setq op (erc-channel-user-op cuser)
5604 voice (erc-channel-user-voice cuser)))
5606 (message "%s is %s@%s%s%s"
5608 (if full-name (format " (%s)" full-name) "")
5610 (format " and is +%s%s on %s"
5613 (erc-default-target))
5617 (defun erc-away-p ()
5618 "Return t if the current ERC process is set away."
5620 (and (erc-server-buffer-live-p)
5621 (set-buffer (process-buffer erc-server-process))
5624 ;; Mode line handling
5626 (defcustom erc-mode-line-format "%s %a"
5627 "A string to be formatted and shown in the mode-line in `erc-mode'.
5629 The string is formatted using `format-spec' and the result is set as the value
5630 of `mode-line-buffer-identification'.
5632 The following characters are replaced:
5633 %a: String indicating away status or \"\" if you are not away
5634 %m: The modes of the channel
5635 %n: The current nick name
5636 %o: The topic of the channel
5637 %p: The session port
5638 %t: The name of the target (channel, nickname, or servername:port)
5639 %s: In the server-buffer, this gets filled with the value of
5640 `erc-server-announced-name', in a channel, the value of
5641 (erc-default-target) also get concatenated."
5642 :group 'erc-mode-line-and-header
5645 (defcustom erc-header-line-format "[IRC] %n on %t %m %o"
5646 "A string to be formatted and shown in the header-line in `erc-mode'.
5647 Only used in Emacs 21.
5649 See `erc-mode-line-format' for which characters are can be used."
5650 :group 'erc-mode-line-and-header
5653 (defcustom erc-header-line-uses-help-echo-p t
5654 "Show the contents of the header line in the echo area or as a tooltip
5655 when you move point into the header line."
5656 :group 'erc-mode-line-and-header
5659 (defcustom erc-header-line-face-method nil
5660 "Determine what method to use when colorizing the header line text.
5662 If nil, don't colorize the header text.
5663 If given a function, call it and use the resulting face name.
5664 Otherwise, use the `erc-header-line' face."
5665 :group 'erc-mode-line-and-header
5666 :type '(choice (const :tag "Don't colorize" nil)
5667 (const :tag "Use the erc-header-line face" t)
5668 (function :tag "Call a function")))
5670 (defcustom erc-show-channel-key-p t
5671 "Show the the channel key in the header line."
5672 :group 'erc-paranoia
5675 (defcustom erc-common-server-suffixes
5676 '(("openprojects.net$" . "OPN")
5677 ("freenode.net$" . "OPN"))
5678 "Alist of common server name suffixes.
5679 This variable is used in mode-line display to save screen
5680 real estate. Set it to nil if you want to avoid changing
5681 displayed hostnames."
5682 :group 'erc-mode-line-and-header
5685 (defcustom erc-mode-line-away-status-format
5686 "(AWAY since %a %b %d %H:%M) "
5687 "When you're away on a server, this is shown in the mode line.
5688 This should be a string with substitution variables recognized by
5689 format-time-message."
5690 :group 'erc-mode-line-and-header
5693 (defun erc-shorten-server-name (server-name)
5694 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
5695 (if (stringp server-name)
5697 (insert server-name)
5698 (let ((alist erc-common-server-suffixes))
5700 (goto-char (point-min))
5701 (if (re-search-forward (caar alist) nil t)
5702 (replace-match (cdar alist)))
5703 (setq alist (cdr alist))))
5706 (defun erc-format-target ()
5707 "Return the name of the target (channel or nickname or servername:port)."
5708 (let ((target (erc-default-target)))
5710 (concat (erc-shorten-server-name
5711 (or erc-server-announced-name
5712 erc-session-server))
5713 ":" (erc-port-to-string erc-session-port)))))
5715 (defun erc-format-target-and/or-server ()
5716 "Return the server name or the current target and server name combined."
5717 (let ((server-name (erc-shorten-server-name
5718 (or erc-server-announced-name
5719 erc-session-server))))
5720 (cond ((erc-default-target)
5721 (concat (erc-string-no-properties (erc-default-target))
5723 (server-name server-name)
5724 (t (buffer-name (current-buffer))))))
5726 (defun erc-format-away-status ()
5727 "Return a formatted `erc-mode-line-away-status-format'
5728 if `erc-away' is non-nil."
5729 (let ((a (when (erc-server-buffer-live-p)
5730 (with-current-buffer (process-buffer erc-server-process)
5733 (format-time-string erc-mode-line-away-status-format a)
5736 (defun erc-format-channel-modes ()
5737 "Return the current channel's modes and the estimated lag."
5738 (let ((lag (when (erc-server-buffer-live-p)
5739 (with-current-buffer (process-buffer erc-server-process)
5741 (concat (apply 'concat
5742 "(+" erc-channel-modes)
5743 (cond ((and erc-channel-user-limit erc-channel-key)
5744 (if erc-show-channel-key-p
5745 (format "lk %.0f %s" erc-channel-user-limit
5747 (format "kl %.0f" erc-channel-user-limit)))
5748 (erc-channel-user-limit
5749 ;; Emacs has no bignums
5750 (format "l %.0f" erc-channel-user-limit))
5752 (if erc-show-channel-key-p
5753 (format "k %s" erc-channel-key)
5756 (if lag (format ",lag:%.0f" lag) "")
5759 (defun erc-update-mode-line-buffer (buffer)
5760 "Update the mode line in a single ERC buffer BUFFER."
5761 (with-current-buffer buffer
5762 (let ((spec (format-spec-make
5763 ?a (erc-format-away-status)
5764 ?m (erc-format-channel-modes)
5765 ?n (or (erc-current-nick) "")
5766 ?o (erc-controls-strip erc-channel-topic)
5767 ?p (erc-port-to-string erc-session-port)
5768 ?s (erc-format-target-and/or-server)
5769 ?t (erc-format-target)))
5770 (process-status (cond ((and (erc-server-process-alive)
5771 (not erc-server-connected))
5773 ((erc-server-process-alive)
5777 (face (cond ((eq erc-header-line-face-method nil)
5779 ((functionp erc-header-line-face-method)
5780 (funcall erc-header-line-face-method))
5783 (cond ((featurep 'xemacs)
5784 (setq modeline-buffer-identification
5785 (list (format-spec erc-mode-line-format spec)))
5786 (setq modeline-process (list process-status)))
5788 (setq mode-line-buffer-identification
5789 (list (format-spec erc-mode-line-format spec)))
5790 (setq mode-line-process (list process-status))))
5791 (when (boundp 'header-line-format)
5792 (let ((header (if erc-header-line-format
5793 (format-spec erc-header-line-format spec)
5795 (cond ((null header)
5796 (setq header-line-format nil))
5797 (erc-header-line-uses-help-echo-p
5798 (let ((help-echo (with-temp-buffer
5800 (fill-region (point-min) (point-max))
5802 (setq header-line-format
5803 (erc-replace-regexp-in-string
5807 (erc-propertize header 'help-echo help-echo
5809 (erc-propertize header 'help-echo help-echo))))))
5810 (t (setq header-line-format header))))))
5811 (if (featurep 'xemacs)
5813 (force-mode-line-update))))
5815 (defun erc-update-mode-line (&optional buffer)
5816 "Update the mode line in BUFFER.
5818 If BUFFER is nil, update the mode line in all ERC buffers."
5819 (if (and buffer (bufferp buffer))
5820 (erc-update-mode-line-buffer buffer)
5821 (dolist (buf (erc-buffer-list))
5822 (when (buffer-live-p buf)
5823 (erc-update-mode-line-buffer buf)))))
5827 (defun erc-port-to-string (p)
5828 "Convert port P to a string.
5829 P may be an integer or a service name."
5834 (defun erc-string-to-port (s)
5835 "Convert string S to either an integer port number or a service name."
5836 (let ((n (string-to-number s)))
5841 (defun erc-version (&optional here)
5842 "Show the version number of ERC in the minibuffer.
5843 If optional argument HERE is non-nil, insert version number at point."
5845 (let ((version-string
5846 (format "ERC %s (GNU Emacs %s)" erc-version-string emacs-version)))
5848 (insert version-string)
5850 (message "%s" version-string)
5853 (defun erc-modes (&optional here)
5854 "Show the active ERC modes in the minibuffer.
5855 If optional argument HERE is non-nil, insert version number at point."
5858 (mapconcat 'identity
5859 (let (modes (case-fold-search nil))
5860 (dolist (var (apropos-internal "^erc-.*mode$"))
5861 (when (and (boundp var)
5863 (setq modes (cons (symbol-name var)
5870 (message "%s" string)
5873 (defun erc-trim-string (s)
5874 "Trim leading and trailing spaces off S."
5876 ((not (stringp s)) nil)
5877 ((string-match "^\\s-*$" s)
5879 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
5884 (defun erc-arrange-session-in-multiple-windows ()
5885 "Open a window for every non-server buffer related to `erc-session-server'.
5887 All windows are opened in the current frame."
5889 (unless (boundp 'erc-server-process)
5890 (error "No erc-process found in current buffer"))
5891 (let ((bufs (erc-buffer-list nil erc-server-process)))
5893 (delete-other-windows)
5894 (switch-to-buffer (car bufs))
5895 (setq bufs (cdr bufs))
5899 (switch-to-buffer (car bufs))
5900 (setq bufs (cdr bufs))
5901 (balance-windows)))))
5903 (defun erc-popup-input-buffer ()
5904 "Provide a input buffer."
5906 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
5911 (list (symbol-name e)))
5912 (apropos-internal "-mode$" 'commandp))
5914 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
5916 (narrow-to-region (point) (point))
5917 (shrink-window-if-larger-than-buffer)))
5921 (defun erc-make-message-variable-name (catalog entry)
5922 "Create a variable name corresponding to CATALOG's ENTRY."
5923 (intern (concat "erc-message-"
5924 (symbol-name catalog) "-" (symbol-name entry))))
5926 (defun erc-define-catalog-entry (catalog entry format-spec)
5927 "Set CATALOG's ENTRY to FORMAT-SPEC."
5928 (set (erc-make-message-variable-name catalog entry)
5931 (defun erc-define-catalog (catalog entries)
5932 "Define a CATALOG according to ENTRIES."
5933 (dolist (entry entries)
5934 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
5938 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
5939 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
5940 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
5941 (cannot-find-file . "Cannot find file %f")
5942 (cannot-read-file . "Cannot read file %f")
5943 (connect . "Connecting to %S:%p... ")
5945 (country-unknown . "%d: No such domain")
5946 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
5947 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
5948 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
5949 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
5950 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
5952 . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
5953 (disconnected . "\n\nConnection failed! Re-establishing connection...\n")
5954 (disconnected-noreconnect
5955 . "\n\nConnection failed! Not re-establishing connection.\n")
5956 (finished . "\n\n*** ERC finished ***\n")
5957 (terminated . "\n\n*** ERC terminated: %e\n")
5958 (login . "Logging in as \'%n\'...")
5959 (nick-in-use . "%n is in use. Choose new nickname: ")
5961 . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
5962 (no-default-channel . "No default channel")
5963 (no-invitation . "You've got no invitation")
5964 (no-target . "No target")
5965 (ops . "%i operator%s: %o")
5966 (ops-none . "No operators in this channel.")
5967 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
5968 (variable-not-bound . "Variable not bound!")
5969 (ACTION . "* %n %a")
5970 (CTCP-CLIENTINFO . "Client info for %n: %m")
5971 (CTCP-ECHO . "Echo %n: %m")
5972 (CTCP-FINGER . "Finger info for %n: %m")
5973 (CTCP-PING . "Ping time to %n is %t")
5974 (CTCP-TIME . "Time by %n is %m")
5975 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
5976 (CTCP-VERSION . "Version for %n is %m")
5977 (ERROR . "==> ERROR from %s: %c\n")
5978 (INVITE . "%n (%u@%h) invites you to channel %c")
5979 (JOIN . "%n (%u@%h) has joined channel %c")
5980 (JOIN-you . "You have joined channel %c")
5981 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
5982 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
5983 (KICK-by-you . "You have kicked %k off channel %c: %r")
5984 (MODE . "%n (%u@%h) has changed mode for %t to %m")
5985 (MODE-nick . "%n has changed mode for %t to %m")
5986 (NICK . "%n (%u@%h) is now known as %N")
5987 (NICK-you . "Your new nickname is %N")
5988 (PART . erc-message-english-PART)
5989 (PING . "PING from server (last: %s sec. ago)")
5990 (PONG . "PONG from %h (%i second%s)")
5991 (QUIT . "%n (%u@%h) has quit: %r")
5992 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
5993 (WALLOPS . "Wallops from %n: %m")
5994 (s004 . "%s %v %U %C")
5995 (s221 . "User modes for %n: %m")
5996 (s252 . "%i operator(s) online")
5997 (s253 . "%i unknown connection(s)")
5998 (s254 . "%i channels formed")
5999 (s301 . "%n is AWAY: %r")
6000 (s303 . "Is online: %n")
6003 (s311 . "%n is %f (%u@%h)")
6004 (s312 . "%n is/was on server %s (%c)")
6005 (s313 . "%n is an IRC operator")
6006 (s314 . "%n was %f (%u@%h)")
6007 (s317 . "%n has been idle for %i")
6008 (s317-on-since . "%n has been idle for %i, on since %t")
6009 (s319 . "%n is on channel(s): %c")
6010 (s320 . "%n is an identified user")
6011 (s321 . "Channel Users Topic")
6012 (s322 . "%c [%u] %t")
6013 (s324 . "%c modes: %m")
6014 (s329 . "%c was created on %t")
6016 (s331 . "No topic is set for %c")
6017 (s332 . "Topic for %c: %T")
6018 (s333 . "%c: topic set by %n, %t")
6019 (s341 . "Inviting %n to channel %c")
6020 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
6021 (s353 . "Users on %c: %u")
6022 (s367 . "Ban on %b on %c set by %s on %t (Use /banlist!)")
6023 (s368 . "Banlist of %c ends.")
6024 (s379 . "%c: Forwarded to %f")
6025 (s391 . "The time at %s is %t")
6026 (s401 . "%n: No such nick/channel")
6027 (s403 . "%c: No such channel")
6028 (s404 . "%c: Cannot send to channel")
6029 (s405 . "%c: You have joined too many channels")
6030 (s406 . "%n: There was no such nickname")
6031 (s412 . "No text to send")
6032 (s421 . "%c: Unknown command")
6033 (s431 . "No nickname given")
6034 (s432 . "%n is an erroneous nickname")
6035 (s442 . "%c: You're not on that channel")
6036 (s445 . "SUMMON has been disabled")
6037 (s446 . "USERS has been disabled")
6038 (s451 . "You have not registered")
6039 (s461 . "%c: not enough parameters")
6040 (s462 . "Unauthorized command (already registered)")
6041 (s463 . "Your host isn't among the privileged")
6042 (s464 . "Password incorrect")
6043 (s465 . "You are banned from this server")
6044 (s474 . "You can't join %c because you're banned (+b)")
6045 (s475 . "You must specify the correct channel key (+k) to join %c")
6046 (s481 . "Permission Denied - You're not an IRC operator")
6047 (s482 . "You need to be a channel operator of %c to do that")
6048 (s483 . "You can't kill a server!")
6049 (s484 . "Your connection is restricted!")
6050 (s485 . "You're not the original channel operator")
6051 (s491 . "No O-lines for your host")
6052 (s501 . "Unknown MODE flag")
6053 (s502 . "You can't change modes for other users")))
6055 (defun erc-message-english-PART (&rest args)
6056 "Format a proper PART message.
6058 This function is an example on what could be done with formatting
6060 (let ((nick (cadr (memq ?n args)))
6061 (user (cadr (memq ?u args)))
6062 (host (cadr (memq ?h args)))
6063 (channel (cadr (memq ?c args)))
6064 (reason (cadr (memq ?r args))))
6065 (if (string= nick (erc-current-nick))
6066 (format "You have left channel %s" channel)
6067 (format "%s (%s@%s) has left channel %s%s"
6068 nick user host channel
6069 (if (not (string= reason ""))
6070 (format ": %s" reason)
6074 (defvar erc-current-message-catalog 'english)
6075 (make-variable-buffer-local 'erc-current-message-catalog)
6077 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6078 "Retrieve ENTRY from CATALOG.
6080 If CATALOG is nil, `erc-current-message-catalog' is used.
6082 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6084 (unless catalog (setq catalog erc-current-message-catalog))
6085 (let ((var (erc-make-message-variable-name catalog entry)))
6088 (when (boundp (erc-make-message-variable-name 'english entry))
6089 (symbol-value (erc-make-message-variable-name 'english entry))))))
6091 (defun erc-format-message (msg &rest args)
6092 "Format MSG according to ARGS.
6094 See also `format-spec'."
6095 (when (eq (logand (length args) 1) 1) ; oddp
6096 (error "Obscure usage of this function appeared"))
6097 (let ((entry (erc-retrieve-catalog-entry msg)))
6099 (error "No format spec for message %s" msg))
6100 (when (functionp entry)
6101 (setq entry (apply entry args)))
6102 (format-spec entry (apply 'format-spec-make args))))
6104 ;;; Various hook functions
6106 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6108 (defcustom erc-kill-server-hook '(erc-kill-server)
6109 "*Invoked whenever a server-buffer is killed via `kill-buffer'."
6113 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6114 "*Invoked whenever a channel-buffer is killed via `kill-buffer'."
6118 (defcustom erc-kill-buffer-hook nil
6119 "*Hook run whenever a non-server or channel buffer is killed.
6121 See also `kill-buffer'."
6125 (defun erc-kill-buffer-function ()
6126 "Function to call when an ERC buffer is killed.
6127 This function should be on `kill-buffer-hook'.
6128 When the current buffer is in `erc-mode', this function will run
6129 one of the following hooks:
6130 `erc-kill-server-hook' if the server buffer was killed,
6131 `erc-kill-channel-hook' if a channel buffer was killed,
6132 or `erc-kill-buffer-hook' if any other buffer."
6133 (when (eq major-mode 'erc-mode)
6134 (erc-remove-channel-users)
6136 ((eq (erc-server-buffer) (current-buffer))
6137 (run-hooks 'erc-kill-server-hook))
6138 ((erc-channel-p (erc-default-target))
6139 (run-hooks 'erc-kill-channel-hook))
6141 (run-hooks 'erc-kill-buffer-hook)))))
6143 (defun erc-kill-server ()
6144 "Sends a QUIT command to the server when the server buffer is killed.
6145 This function should be on `erc-kill-server-hook'."
6146 (when (erc-server-process-alive)
6147 (setq erc-server-quitting t)
6148 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6150 (defun erc-kill-channel ()
6151 "Sends a PART command to the server when the channel buffer is killed.
6152 This function should be on `erc-kill-channel-hook'."
6153 (when (erc-server-process-alive)
6154 (let ((tgt (erc-default-target)))
6155 (erc-server-send (format "PART %s :%s" tgt
6156 (funcall erc-part-reason nil))
6159 ;;; Dealing with `erc-parsed'
6161 (defun erc-get-parsed-vector (point)
6162 "Return the whole parsed vector on POINT."
6163 (get-text-property point 'erc-parsed))
6165 (defun erc-get-parsed-vector-nick (vect)
6166 "Return nickname in the parsed vector VECT."
6167 (let* ((untreated-nick (and vect (erc-response.sender vect)))
6168 (maybe-nick (when untreated-nick
6169 (car (split-string untreated-nick "!")))))
6170 (when (and (not (null maybe-nick))
6171 (erc-is-valid-nick-p maybe-nick))
6174 (defun erc-get-parsed-vector-type (vect)
6175 "Return message type in the parsed vector VECT."
6177 (erc-response.command vect)))
6179 ;; Teach url.el how to open irc:// URLs with ERC.
6180 ;; To activate, customize `url-irc-function' to `url-irc-erc'.
6183 (defun erc-handle-irc-url (host port channel user password)
6184 "Use ERC to IRC on HOST:PORT in CHANNEL as USER with PASSWORD.
6185 If ERC is already connected to HOST:PORT, simply /join CHANNEL.
6186 Otherwise, connect to HOST:PORT as USER and /join CHANNEL."
6187 (let ((server-buffer
6188 (car (erc-buffer-filter
6190 (and (string-equal erc-session-server host)
6191 (= erc-session-port port)
6192 erc-server-connected
6193 (eq (erc-server-buffer) (current-buffer))))))))
6194 (with-current-buffer (or server-buffer (current-buffer))
6195 (if (and server-buffer channel)
6196 (erc-cmd-JOIN channel)
6197 (erc host port (or user (erc-compute-nick)) (erc-compute-full-name)
6198 (not server-buffer) password nil channel
6200 (get-buffer-process server-buffer)))))))
6204 ;;; Deprecated. We might eventually stop requiring the goodies automatically.
6205 ;;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6206 ;;; avoid a recursive require error when byte-compiling the entire package.
6207 (require 'erc-goodies)
6209 ;;; erc.el ends here
6212 ;; outline-regexp: ";;+"
6213 ;; indent-tabs-mode: t
6217 ;; arch-tag: d19587f6-627e-48c1-8d86-58595fa3eca3