1 ;; erc.el --- An Emacs Internet Relay Chat client
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
5 ;; Copyright (C) 2004 Brian Palmer
7 ;; Author: Alexander L. Belikoff (alexander@belikoff.net)
8 ;; Contributors: Sergey Berezin (sergey.berezin@cs.cmu.edu),
9 ;; Mario Lang (mlang@delysid.org),
10 ;; Alex Schroeder (alex@gnu.org)
11 ;; Andreas Fuchs (afs@void.at)
12 ;; Gergely Nagy (algernon@midgard.debian.net)
13 ;; David Edmondson (dme@dme.org)
14 ;; Maintainer: Mario Lang (mlang@delysid.org)
15 ;; Keywords: IRC, chat, client, Internet
17 ;; This file is part of GNU Emacs.
19 ;; GNU Emacs is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation; either version 2, or (at your option)
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs; see the file COPYING. If not, write to the
31 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 ;; Boston, MA 02110-1301, USA.
36 ;; ERC is an IRC client for Emacs.
38 ;; For more information, see the following URLs:
39 ;; * http://sf.net/projects/erc/
40 ;; * http://www.emacswiki.org/cgi-bin/wiki.pl?EmacsIRCClient
42 ;; Jul-26-2001. erc.el is now in CVS on SourceForge. I invite everyone
43 ;; who wants to hack it to contact me <mlang@delysid.org> in order to
44 ;; get write access on the CVS.
48 ;; Put erc.el in your load-path, and put (require 'erc) in your .emacs.
52 ;; Use M-x customize-group RET erc RET to get an overview
53 ;; of all the variables you can tweak.
57 ;; To connect to an IRC server, do
61 ;; After you are connected to a server, you can use C-h m or have a look at
69 (defconst erc-version-string "Version 5.1 (Emacs 22)"
70 "ERC version. This is used by function `erc-version'.")
72 (eval-when-compile (require 'cl))
79 (defvar erc-official-location
80 "http://erc.sf.net (comments mailto://mlang@delysid.org)"
81 "Location of the ERC client on the Internet.")
84 "Emacs Internet Relay Chat client."
85 :link '(url-link "http://www.emacswiki.org/cgi-bin/wiki.pl?EmacsIRCClient")
89 (defgroup erc-buffers nil
90 "Creating new ERC buffers"
93 (defgroup erc-display nil
94 "Settings for how various things are displayed"
97 (defgroup erc-mode-line-and-header nil
98 "Displaying information in the mode-line and header"
101 (defgroup erc-ignore nil
102 "Ignoring certain messages"
105 (defgroup erc-query nil
106 "Using separate buffers for private discussions"
109 (defgroup erc-quit-and-part nil
110 "Quitting and parting channels"
113 (defgroup erc-paranoia nil
114 "Know what is sent and received; control the display of sensitive data."
117 (defgroup erc-scripts nil
118 "Running scripts at startup and with /LOAD"
121 (require 'erc-backend)
123 ;; compatibility with older ERC releases
125 (if (fboundp 'defvaralias)
127 (defvaralias 'erc-announced-server-name 'erc-server-announced-name)
128 (erc-make-obsolete-variable 'erc-announced-server-name
129 'erc-server-announced-name
131 (defvaralias 'erc-process 'erc-server-process)
132 (erc-make-obsolete-variable 'erc-process 'erc-server-process "ERC 5.1")
133 (defvaralias 'erc-default-coding-system 'erc-server-coding-system)
134 (erc-make-obsolete-variable 'erc-default-coding-system
135 'erc-server-coding-system
137 (message (concat "ERC: The function `defvaralias' is not bound. See the "
138 "NEWS file for variable name changes since ERC 5.0.4.")))
140 (defalias 'erc-send-command 'erc-server-send)
141 (erc-make-obsolete 'erc-send-command 'erc-server-send "ERC 5.1")
143 ;; tunable connection and authentication parameters
145 (defcustom erc-server nil
147 See function `erc-compute-server' for more details on connection
148 parameters and authentication."
150 :type '(choice (const nil) string))
152 (defcustom erc-port nil
155 :type '(choice (const nil) number string))
157 (defcustom erc-nick nil
160 Can be either a string, or a list of strings.
161 In the latter case, if the first nick in the list is already in use,
162 other nicks are tried in the list order.
164 See function `erc-compute-nick' for more details on connection
165 parameters and authentication."
167 :type '(choice (const nil)
168 (string :tag "Nickname")
171 (defcustom erc-nick-uniquifier "`"
172 "The character to append to the nick if it is already in use."
176 (defcustom erc-manual-set-nick-on-bad-nick-p nil
177 "If the nickname you chose isn't available, ERC should not automatically
178 attempt to set another nickname. You can manually set another nickname with
183 (defcustom erc-user-full-name nil
186 See function `erc-compute-full-name' for more details on connection
187 parameters and authentication."
189 :type '(choice (const nil) string function)
190 :set (lambda (sym val)
192 (set sym (funcall val))
195 (defvar erc-password nil
196 "ERC password to use in authentication (not necessary).")
198 (defcustom erc-user-mode nil
199 "Initial user modes to be set after a connection is established."
201 :type '(choice (const nil) string function))
204 (defcustom erc-prompt-for-password t
205 "Asks before using the default password, or whether to enter a new one."
209 (defcustom erc-warn-about-blank-lines t
210 "Warn the user if they attempt to send a blank line."
214 (defcustom erc-send-whitespace-lines nil
215 "If set to non-nil, send lines consisting of only whitespace."
219 (defcustom erc-hide-prompt nil
220 "If non-nil, do not display the prompt for commands.
222 \(A command is any input starting with a '/').
224 See also the variables `erc-prompt' and `erc-command-indicator'."
230 (defcustom erc-show-my-nick t
231 "If non-nil, display one's own nickname when sending a message.
233 If non-nil, \"<nickname>\" will be shown.
234 If nil, only \"> \" will be shown."
238 (define-widget 'erc-message-type 'set
239 "A set of standard IRC Message types."
240 :args '((const "JOIN")
246 (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))
248 (defcustom erc-hide-list nil
249 "*List of IRC type messages to hide.
250 A typical value would be '(\"JOIN\" \"PART\" \"QUIT\")."
252 :type 'erc-message-type)
254 (defvar erc-session-password nil
255 "The password used for the current session.")
256 (make-variable-buffer-local 'erc-session-password)
258 (defcustom erc-disconnected-hook nil
259 "Run this hook with arguments (NICK IP REASON) when disconnected.
260 This happens before automatic reconnection. Note, that
261 `erc-server-QUIT-functions' might not be run when we disconnect,
262 simply because we do not necessarily receive the QUIT event."
266 (defcustom erc-complete-functions nil
267 "These functions get called when the user hits TAB in ERC.
268 Each function in turn is called until one returns non-nil to
269 indicate it has handled the input."
273 (defcustom erc-join-hook nil
274 "Hook run when we join a channel. Hook functions are called
275 without arguments, with the current buffer set to the buffer of
278 See also `erc-server-JOIN-functions', `erc-part-hook'."
282 (defcustom erc-quit-hook nil
283 "Hook run when processing a quit command directed at our nick.
285 The hook receives one argument, the current PROCESS.
286 See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
290 (defcustom erc-part-hook nil
291 "Hook run when processing a PART message directed at our nick.
293 The hook receives one argument, the current BUFFER.
294 See also `erc-server-QUIT-functions', `erc-quit-hook' and
295 `erc-disconnected-hook'."
299 (defcustom erc-kick-hook nil
300 "Hook run when processing a KICK message directed at our nick.
302 The hook receives one argument, the current BUFFER.
303 See also `erc-server-PART-functions' and `erc-part-hook'."
307 (defcustom erc-nick-changed-functions nil
308 "List of functions run when your nick was successfully changed.
310 Each function should accept two arguments, NEW-NICK and OLD-NICK."
314 (defcustom erc-connect-pre-hook '(erc-initialize-log-marker)
315 "Hook called just before `erc' calls `erc-connect'.
316 Functions are run in the buffer-to-be."
321 (defvar erc-channel-users nil
322 "A hash table of members in the current channel, which
323 associates nicknames with cons cells of the form:
324 \(USER . MEMBER-DATA) where USER is a pointer to an
325 erc-server-user struct, and MEMBER-DATA is a pointer to an
326 erc-channel-user struct.")
327 (make-variable-buffer-local 'erc-channel-users)
329 (defvar erc-server-users nil
330 "A hash table of users on the current server, which associates
331 nicknames with erc-server-user struct instances.")
332 (make-variable-buffer-local 'erc-server-users)
334 (defun erc-downcase (string)
335 "Convert STRING to IRC standard conforming downcase."
336 (let ((s (downcase string))
342 (while (string-match "[]\\[~]" s)
343 (aset s (match-beginning 0)
344 (cdr (assq (aref s (match-beginning 0)) c)))))
347 (defstruct (erc-server-user (:type vector) :named)
349 nickname host login full-name info
352 ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
353 ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
357 (defstruct (erc-channel-user (:type vector) :named)
359 ;; Last message time (in the form of the return value of
362 ;; This is useful for ordered name completion.
363 (last-message-time nil))
365 (defsubst erc-get-channel-user (nick)
366 "Finds the (USER . CHANNEL-DATA) element corresponding to NICK
367 in the current buffer's `erc-channel-users' hash table."
368 (gethash (erc-downcase nick) erc-channel-users))
370 (defsubst erc-get-server-user (nick)
371 "Finds the USER corresponding to NICK in the current server's
372 `erc-server-users' hash table."
373 (with-current-buffer (process-buffer erc-server-process)
374 (gethash (erc-downcase nick) erc-server-users)))
376 (defsubst erc-add-server-user (nick user)
377 "This function is for internal use only.
379 Adds USER with nickname NICK to the `erc-server-users' hash table."
380 (with-current-buffer (process-buffer erc-server-process)
381 (puthash (erc-downcase nick) user erc-server-users)))
383 (defsubst erc-remove-server-user (nick)
384 "This function is for internal use only.
386 Removes the user with nickname NICK from the `erc-server-users'
387 hash table. This user is not removed from the
388 `erc-channel-users' lists of other buffers.
390 See also: `erc-remove-user'."
391 (with-current-buffer (process-buffer erc-server-process)
392 (remhash (erc-downcase nick) erc-server-users)))
394 (defun erc-change-user-nickname (user new-nick)
395 "This function is for internal use only.
397 Changes the nickname of USER to NEW-NICK in the
398 `erc-server-users' hash table. The `erc-channel-users' lists of
399 other buffers are also changed."
400 (let ((nick (erc-server-user-nickname user)))
401 (setf (erc-server-user-nickname user) new-nick)
402 (with-current-buffer (process-buffer erc-server-process)
403 (remhash (erc-downcase nick) erc-server-users)
404 (puthash (erc-downcase new-nick) user erc-server-users))
405 (dolist (buf (erc-server-user-buffers user))
406 (if (buffer-live-p buf)
407 (with-current-buffer buf
408 (let ((cdata (erc-get-channel-user nick)))
409 (remhash (erc-downcase nick) erc-channel-users)
410 (puthash (erc-downcase new-nick) cdata
411 erc-channel-users)))))))
413 (defun erc-remove-channel-user (nick)
414 "This function is for internal use only.
416 Removes the user with nickname NICK from the `erc-channel-users'
417 list for this channel. If this user is not in the
418 `erc-channel-users' list of any other buffers, the user is also
419 removed from the server's `erc-server-users' list.
421 See also: `erc-remove-server-user' and `erc-remove-user'."
422 (let ((channel-data (erc-get-channel-user nick)))
424 (let ((user (car channel-data)))
425 (setf (erc-server-user-buffers user)
426 (delq (current-buffer)
427 (erc-server-user-buffers user)))
428 (remhash (erc-downcase nick) erc-channel-users)
429 (if (null (erc-server-user-buffers user))
430 (erc-remove-server-user nick))))))
432 (defun erc-remove-user (nick)
433 "This function is for internal use only.
435 Removes the user with nickname NICK from the `erc-server-users'
436 list as well as from all `erc-channel-users' lists.
438 See also: `erc-remove-server-user' and
439 `erc-remove-channel-user'."
440 (let ((user (erc-get-server-user nick)))
442 (let ((buffers (erc-server-user-buffers user)))
443 (dolist (buf buffers)
444 (if (buffer-live-p buf)
445 (with-current-buffer buf
446 (remhash (erc-downcase nick) erc-channel-users)
447 (run-hooks 'erc-channel-members-changed-hook)))))
448 (erc-remove-server-user nick))))
450 (defun erc-remove-channel-users ()
451 "This function is for internal use only.
453 Removes all users in the current channel. This is called by
454 `erc-server-PART' and `erc-server-QUIT'."
455 (when (and erc-server-connected
456 (erc-server-process-alive)
457 (hash-table-p erc-channel-users))
458 (maphash (lambda (nick cdata)
459 (erc-remove-channel-user nick))
461 (clrhash erc-channel-users)))
463 (defsubst erc-channel-user-op-p (nick)
464 "Return `t' if NICK is an operator in the current channel."
466 (hash-table-p erc-channel-users)
467 (let ((cdata (erc-get-channel-user nick)))
468 (and cdata (cdr cdata)
469 (erc-channel-user-op (cdr cdata))))))
471 (defsubst erc-channel-user-voice-p (nick)
472 "Return `t' if NICK has voice in the current channel."
474 (hash-table-p erc-channel-users)
475 (let ((cdata (erc-get-channel-user nick)))
476 (and cdata (cdr cdata)
477 (erc-channel-user-voice (cdr cdata))))))
479 (defun erc-get-channel-user-list ()
480 "Returns a list of users in the current channel. Each element
481 of the list is of the form (USER . CHANNEL-DATA), where USER is
482 an erc-server-user struct, and CHANNEL-DATA is either `nil' or an
483 erc-channel-user struct.
485 See also: `erc-sort-channel-users-by-activity'"
487 (if (hash-table-p erc-channel-users)
488 (maphash (lambda (nick cdata)
489 (setq users (cons cdata users)))
493 (defun erc-get-server-nickname-list ()
494 "Returns a list of known nicknames on the current server."
495 (if (erc-server-process-alive)
496 (with-current-buffer (erc-server-buffer)
498 (when (hash-table-p erc-server-users)
499 (maphash (lambda (n user)
501 (cons (erc-server-user-nickname user)
506 (defun erc-get-channel-nickname-list ()
507 "Returns a list of known nicknames on the current channel."
509 (when (hash-table-p erc-channel-users)
510 (maphash (lambda (n cdata)
512 (cons (erc-server-user-nickname (car cdata))
517 (defun erc-get-server-nickname-alist ()
518 "Returns an alist of known nicknames on the current server."
519 (if (erc-server-process-alive)
520 (with-current-buffer (erc-server-buffer)
522 (when (hash-table-p erc-server-users)
523 (maphash (lambda (n user)
525 (cons (cons (erc-server-user-nickname user) nil)
530 (defun erc-get-channel-nickname-alist ()
531 "Returns an alist of known nicknames on the current channel."
533 (when (hash-table-p erc-channel-users)
534 (maphash (lambda (n cdata)
536 (cons (cons (erc-server-user-nickname (car cdata)) nil)
541 (defun erc-sort-channel-users-by-activity (list)
542 "Sorts LIST such that users which have spoken most recently are
543 listed first. LIST must be of the form (USER . CHANNEL-DATA).
545 See also: `erc-get-channel-user-list'."
550 (let ((tx (erc-channel-user-last-message-time (cdr x)))
551 (ty (erc-channel-user-last-message-time (cdr y))))
558 (defun erc-sort-channel-users-alphabetically (list)
559 "Sort LIST so that users' nicknames are in alphabetical order.
560 LIST must be of the form (USER . CHANNEL-DATA).
562 See also: `erc-get-channel-user-list'."
567 (let ((nickx (downcase (erc-server-user-nickname (car x))))
568 (nicky (downcase (erc-server-user-nickname (car y)))))
571 (string-lessp nickx nicky)
575 (defvar erc-channel-topic nil
576 "A topic string for the channel. Should only be used in channel-buffers.")
577 (make-variable-buffer-local 'erc-channel-topic)
579 (defvar erc-channel-modes nil
580 "List of strings representing channel modes.
581 E.g. '(\"i\" \"m\" \"s\" \"b Quake!*@*\")
582 \(not sure the ban list will be here, but why not)")
583 (make-variable-buffer-local 'erc-channel-modes)
585 (defvar erc-insert-marker nil
586 "The place where insertion of new text in erc buffers should happen.")
587 (make-variable-buffer-local 'erc-insert-marker)
589 (defvar erc-input-marker nil
590 "The marker where input should be inserted.")
591 (make-variable-buffer-local 'erc-input-marker)
593 (defun erc-string-no-properties (string)
594 "Return a copy of STRING will all text-properties removed."
595 (let ((newstring (copy-sequence string)))
596 (set-text-properties 0 (length newstring) nil newstring)
599 (defcustom erc-prompt "ERC>"
600 "Prompt used by ERC. Trailing whitespace is not required."
602 :type '(choice string function))
605 "Return the input prompt as a string.
607 See also the variable `erc-prompt'."
608 (let ((prompt (if (functionp erc-prompt)
611 (if (> (length prompt) 0)
615 (defcustom erc-command-indicator nil
616 "Indicator used by ERC for showing commands.
618 If non-nil, this will be used in the ERC buffer to indicate
619 commands (i.e., input starting with a '/').
621 If nil, the prompt will be constructed from the variable `erc-prompt'."
623 :type '(choice (const nil) string function))
625 (defun erc-command-indicator ()
626 "Return the command indicator prompt as a string.
628 This only has any meaning if the variable `erc-command-indicator' is non-nil."
629 (and erc-command-indicator
630 (let ((prompt (if (functionp erc-command-indicator)
631 (funcall erc-command-indicator)
632 erc-command-indicator)))
633 (if (> (length prompt) 0)
637 (defcustom erc-notice-prefix "*** "
638 "*Prefix for all notices."
642 (defcustom erc-notice-highlight-type 'all
643 "*Determines how to highlight notices.
644 See `erc-notice-prefix'.
646 The following values are allowed:
648 'prefix - highlight notice prefix only
649 'all - highlight the entire notice
651 Any other value disables notice's highlighting altogether."
653 :type '(choice (const :tag "highlight notice prefix only" prefix)
654 (const :tag "highlight the entire notice" all)
655 (const :tag "don't highlight notices at all" nil)))
657 (defcustom erc-echo-notice-hook nil
658 "*Specifies a list of functions to call to echo a private
659 notice. Each function is called with four arguments, the string
660 to display, the parsed server message, the target buffer (or
661 nil), and the sender. The functions are called in order, until a
662 function evaluates to non-nil. These hooks are called after
663 those specified in `erc-echo-notice-always-hook'.
665 See also: `erc-echo-notice-always-hook',
666 `erc-echo-notice-in-default-buffer',
667 `erc-echo-notice-in-target-buffer',
668 `erc-echo-notice-in-minibuffer',
669 `erc-echo-notice-in-server-buffer',
670 `erc-echo-notice-in-active-non-server-buffer',
671 `erc-echo-notice-in-active-buffer',
672 `erc-echo-notice-in-user-buffers',
673 `erc-echo-notice-in-user-and-target-buffers',
674 `erc-echo-notice-in-first-user-buffer'"
677 :options '(erc-echo-notice-in-default-buffer
678 erc-echo-notice-in-target-buffer
679 erc-echo-notice-in-minibuffer
680 erc-echo-notice-in-server-buffer
681 erc-echo-notice-in-active-non-server-buffer
682 erc-echo-notice-in-active-buffer
683 erc-echo-notice-in-user-buffers
684 erc-echo-notice-in-user-and-target-buffers
685 erc-echo-notice-in-first-user-buffer))
687 (defcustom erc-echo-notice-always-hook
688 '(erc-echo-notice-in-default-buffer)
689 "*Specifies a list of functions to call to echo a private
690 notice. Each function is called with four arguments, the string
691 to display, the parsed server message, the target buffer (or
692 nil), and the sender. The functions are called in order, and all
693 functions are called. These hooks are called before those
694 specified in `erc-echo-notice-hook'.
696 See also: `erc-echo-notice-hook',
697 `erc-echo-notice-in-default-buffer',
698 `erc-echo-notice-in-target-buffer',
699 `erc-echo-notice-in-minibuffer',
700 `erc-echo-notice-in-server-buffer',
701 `erc-echo-notice-in-active-non-server-buffer',
702 `erc-echo-notice-in-active-buffer',
703 `erc-echo-notice-in-user-buffers',
704 `erc-echo-notice-in-user-and-target-buffers',
705 `erc-echo-notice-in-first-user-buffer'"
708 :options '(erc-echo-notice-in-default-buffer
709 erc-echo-notice-in-target-buffer
710 erc-echo-notice-in-minibuffer
711 erc-echo-notice-in-server-buffer
712 erc-echo-notice-in-active-non-server-buffer
713 erc-echo-notice-in-active-buffer
714 erc-echo-notice-in-user-buffers
715 erc-echo-notice-in-user-and-target-buffers
716 erc-echo-notice-in-first-user-buffer))
718 ;; other tunable parameters
720 (defcustom erc-whowas-on-nosuchnick nil
721 "*If non-nil, do a whowas on a nick if no such nick."
725 (defcustom erc-verbose-server-ping nil
726 "*If non-nil, show every time you get a PING or PONG from the server."
730 (defcustom erc-public-away-p nil
731 "*Let others know you are back when you are no longer marked away.
732 This happens in this form:
733 * <nick> is back (gone for <time>)
735 Many consider it impolite to do so automatically."
739 (defcustom erc-away-nickname nil
740 "*The nickname to take when you are marked as being away."
742 :type '(choice (const nil)
745 (defcustom erc-paranoid nil
746 "If non-nil, then all incoming CTCP requests will be shown."
750 (defcustom erc-disable-ctcp-replies nil
751 "Disable replies to CTCP requests that require a reply.
752 If non-nil, then all incoming CTCP requests that normally require
753 an automatic reply (like VERSION or PING) will be ignored. Good to
754 set if some hacker is trying to flood you away."
758 (defcustom erc-anonymous-login t
759 "Be paranoid, don't give away your machine name."
763 (defcustom erc-prompt-for-channel-key nil
764 "Prompt for channel key when using `erc-join-channel' interactively"
768 (defcustom erc-email-userid "user"
769 "Use this as your email user ID."
773 (defcustom erc-ignore-list nil
774 "*List of regexps matching user identifiers to ignore.
776 A user identifier has the form \"nick!login@host\". If an
777 identifier matches, the message from the person will not be
780 :type '(repeat regexp))
781 (make-variable-buffer-local 'erc-ignore-list)
783 (defcustom erc-ignore-reply-list nil
784 "*List of regexps matching user identifiers to ignore completely.
786 This differs from `erc-ignore-list' in that it also ignores any
787 messages directed at the user.
789 A user identifier has the form \"nick!login@host\".
791 If an identifier matches, or a message is addressed to a nick
792 whose identifier matches, the message will not be processed.
794 CAVEAT: ERC doesn't know about the user and host of anyone who
795 was already in the channel when you joined, but never said
796 anything, so it won't be able to match the user and host of those
797 people. You can update the ERC internal info using /WHO *."
799 :type '(repeat regexp))
801 (defvar erc-flood-protect t
802 "*If non-nil, flood protection is enabled.
803 Flooding is sending too much information to the server in too
804 short of an interval, which may cause the server to terminate the
807 See `erc-server-flood-margin' for other flood-related parameters.")
811 (defcustom erc-startup-file-list
812 '("~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
813 "List of files to try for a startup script.
814 The first existent and readable one will get executed.
816 If the filename ends with `.el' it is presumed to be an emacs-lisp
817 script and it gets (load)ed. Otherwise is is treated as a bunch of
818 regular IRC commands"
820 :type '(repeat file))
822 (defcustom erc-script-path nil
823 "List of directories to look for a script in /load command.
824 The script is first searched in the current directory, then in each
825 directory in the list."
827 :type '(repeat directory))
829 (defcustom erc-script-echo t
830 "*If not-NIL, echo the IRC script commands locally."
834 (defvar erc-last-saved-position nil
835 "A marker containing the position the current buffer was last saved at.")
836 (make-variable-buffer-local 'erc-last-saved-position)
838 (defcustom erc-kill-buffer-on-part nil
839 "Kill the channel buffer on PART.
840 This variable should probably stay nil, as ERC can reuse buffers if
841 you rejoin them later."
842 :group 'erc-quit-and-part
845 (defcustom erc-kill-queries-on-quit nil
846 "Kill all query (also channel) buffers of this server on QUIT.
847 See the variable `erc-kill-buffer-on-part' for details."
848 :group 'erc-quit-and-part
851 (defcustom erc-kill-server-buffer-on-quit nil
852 "Kill the server buffer of the process on QUIT."
853 :group 'erc-quit-and-part
856 (defcustom erc-quit-reason-various-alist nil
857 "Alist of possible arguments to the /quit command.
859 Each element has the form:
862 If REGEXP matches the argument to /quit, then its relevant RESULT
863 will be used. RESULT may be either a string, or a function. If
864 a function, it should return the quit message as a string.
866 If no elements match, then the empty string is used.
869 (setq erc-quit-reason-various-alist
870 '((\"zippy\" erc-quit-reason-zippy)
871 (\"xmms\" dme:now-playing)
872 (\"version\" erc-quit-reason-normal)
873 (\"home\" \"Gone home !\")
874 (\"\" \"Default Reason\")))
875 If the user types \"/quit zippy\", then a Zippy the Pinhead quotation
876 will be used as the quit message."
877 :group 'erc-quit-and-part
878 :type '(repeat (list regexp (choice (string) (function)))))
880 (defcustom erc-part-reason-various-alist nil
881 "Alist of possible arguments to the /part command.
883 Each element has the form:
886 If REGEXP matches the argument to /part, then its relevant RESULT
887 will be used. RESULT may be either a string, or a function. If
888 a function, it should return the part message as a string.
890 If no elements match, then the empty string is used.
893 (setq erc-part-reason-various-alist
894 '((\"zippy\" erc-part-reason-zippy)
895 (\"xmms\" dme:now-playing)
896 (\"version\" erc-part-reason-normal)
897 (\"home\" \"Gone home !\")
898 (\"\" \"Default Reason\")))
899 If the user types \"/part zippy\", then a Zippy the Pinhead quotation
900 will be used as the part message."
901 :group 'erc-quit-and-part
902 :type '(repeat (list regexp (choice (string) (function)))))
904 (defcustom erc-quit-reason 'erc-quit-reason-normal
905 "*A function which returns the reason for quitting.
907 The function is passed a single argument, the string typed by the
908 user after \"/quit\"."
909 :group 'erc-quit-and-part
910 :type '(choice (const erc-quit-reason-normal)
911 (const erc-quit-reason-zippy)
912 (const erc-quit-reason-various)
915 (defcustom erc-part-reason 'erc-part-reason-normal
916 "A function which returns the reason for parting a channel.
918 The function is passed a single argument, the string typed by the
919 user after \"/PART\"."
920 :group 'erc-quit-and-part
921 :type '(choice (const erc-part-reason-normal)
922 (const erc-part-reason-zippy)
923 (const erc-part-reason-various)
926 (defvar erc-grab-buffer-name "*erc-grab*"
927 "The name of the buffer created by `erc-grab-region'.")
929 ;; variables available for IRC scripts
931 (defvar erc-user-information "ERC User"
932 "USER_INFORMATION IRC variable.")
936 (defgroup erc-hooks nil
937 "Hook variables for fancy customizations of ERC."
940 (defcustom erc-mode-hook nil
941 "Hook run after `erc-mode' setup is finished."
944 :options '(erc-add-scroll-to-bottom))
946 (defcustom erc-timer-hook nil
947 "Put functions which should get called more or less periodically here.
948 The idea is that servers always play ping pong with the client, and so there
949 is no need for any idle-timer games with Emacs."
953 (defcustom erc-insert-pre-hook nil
954 "Hook called first when some text is inserted through `erc-display-line'.
955 It gets called with one argument, STRING.
956 To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
957 Filtering functions can set `erc-insert-this' to nil to avoid
958 display of that particular string at all."
962 (defcustom erc-send-pre-hook nil
963 "Hook called first when some text is sent through `erc-send-current-line'.
964 It gets called with one argument, STRING.
966 To change the text that will be sent, set the variable STR which is
967 used in `erc-send-current-line'.
969 To change the text inserted into the buffer without changing the text
970 that will be sent, use `erc-send-modify-hook' instead.
972 Filtering functions can set `erc-send-this' to nil to avoid sending of
973 that particular string at all and `erc-insert-this' to prevent
974 inserting that particular string into the buffer.
976 Note that it's useless to set `erc-send-this' to nil and
977 `erc-insert-this' to t. ERC is sane enough to not insert the text
982 (defvar erc-insert-this t
983 "Insert the text into the target buffer or not.
984 Functions on `erc-insert-pre-hook' can set this variable to nil
985 if they wish to avoid insertion of a particular string.")
987 (defvar erc-send-this t
988 "Send the text to the target or not.
989 Functions on `erc-send-pre-hook' can set this variable to nil
990 if they wish to avoid sending of a particular string.")
992 (defcustom erc-insert-modify-hook ()
993 "Insertion hook for functions that will change the text's appearance.
994 This hook is called just after `erc-insert-pre-hook' when the value
995 of `erc-insert-this' is t.
996 While this hook is run, narrowing is in effect and `current-buffer' is
997 the buffer where the text got inserted. One possible value to add here
1002 (defcustom erc-insert-post-hook nil
1003 "This hook is called just after `erc-insert-modify-hook'.
1004 At this point, all modifications from prior hook functions are done."
1007 :options '(erc-truncate-buffer
1009 erc-save-buffer-in-logs))
1011 (defcustom erc-send-modify-hook nil
1012 "Sending hook for functions that will change the text's appearance.
1013 This hook is called just after `erc-send-pre-hook' when the values
1014 of `erc-send-this' and `erc-insert-this' are both t.
1015 While this hook is run, narrowing is in effect and `current-buffer' is
1016 the buffer where the text got inserted.
1018 Note that no function in this hook can change the appearance of the
1019 text that is sent. Only changing the sent text's appearance on the
1020 sending user's screen is possible. One possible value to add here
1025 (defcustom erc-send-post-hook nil
1026 "This hook is called just after `erc-send-modify-hook'.
1027 At this point, all modifications from prior hook functions are done.
1028 NOTE: The functions on this hook are called _before_ sending a command
1031 This function is called with narrowing, ala `erc-send-modify-hook'"
1034 :options '(erc-make-read-only))
1036 (defcustom erc-send-completed-hook
1037 (when (featurep 'emacspeak)
1040 (emacspeak-auditory-icon 'select-object)))))
1041 "Hook called after a message has been parsed by ERC.
1043 The single argument to the functions is the unmodified string
1044 which the local user typed."
1047 ;; mode-specific tables
1049 (defvar erc-mode-syntax-table
1050 (let ((syntax-table (make-syntax-table)))
1051 (modify-syntax-entry ?\" ". " syntax-table)
1052 (modify-syntax-entry ?\\ ". " syntax-table)
1053 (modify-syntax-entry ?' "w " syntax-table)
1054 ;; Make dabbrev-expand useful for nick names
1055 (modify-syntax-entry ?< "." syntax-table)
1056 (modify-syntax-entry ?> "." syntax-table)
1058 "Syntax table used while in ERC mode.")
1060 (defvar erc-mode-abbrev-table nil
1061 "Abbrev table used while in ERC mode.")
1062 (define-abbrev-table 'erc-mode-abbrev-table ())
1064 (defvar erc-mode-map
1065 (let ((map (make-sparse-keymap)))
1066 (define-key map "\C-m" 'erc-send-current-line)
1067 (define-key map "\C-a" 'erc-bol)
1068 (define-key map [home] 'erc-bol)
1069 (define-key map "\C-c\C-a" 'erc-bol)
1070 (define-key map "\C-c\C-b" 'erc-iswitchb)
1071 (define-key map "\C-c\C-c" 'erc-toggle-interpret-controls)
1072 (define-key map "\C-c\C-d" 'erc-input-action)
1073 (define-key map "\C-c\C-e" 'erc-toggle-ctcp-autoresponse)
1074 (define-key map "\C-c\C-f" 'erc-toggle-flood-control)
1075 (define-key map "\C-c\C-i" 'erc-invite-only-mode)
1076 (define-key map "\C-c\C-j" 'erc-join-channel)
1077 (define-key map "\C-c\C-n" 'erc-channel-names)
1078 (define-key map "\C-c\C-o" 'erc-get-channel-mode-from-keypress)
1079 (define-key map "\C-c\C-p" 'erc-part-from-channel)
1080 (define-key map "\C-c\C-q" 'erc-quit-server)
1081 (define-key map "\C-c\C-r" 'erc-remove-text-properties-region)
1082 (define-key map "\C-c\C-t" 'erc-set-topic)
1083 (define-key map "\C-c\C-u" 'erc-kill-input)
1084 (define-key map "\M-\t" 'ispell-complete-word)
1085 (define-key map "\t" 'erc-complete-word)
1087 ;; Suppress `font-lock-fontify-block' key binding since it
1088 ;; destroys face properties.
1089 (if (fboundp 'command-remapping)
1090 (define-key map [remap font-lock-fontify-block] 'undefined)
1091 (substitute-key-definition
1092 'font-lock-fontify-block 'undefined map global-map))
1099 ; Honestly, I have a horrible sense of color and the "defaults" below
1100 ; are supposed to be really bad. But colors ARE required in IRC to
1101 ; convey different parts of conversation. If you think you know better
1102 ; defaults - send them to me.
1104 ;; Now colors are a bit nicer, at least to my eyes.
1105 ;; You may still want to change them to better fit your background.-- S.B.
1107 (defgroup erc-faces nil
1111 (defface erc-default-face '((t))
1115 (defface erc-direct-msg-face '((t (:foreground "IndianRed")))
1116 "ERC face used for messages you receive in the main erc buffer."
1119 (defface erc-input-face '((t (:foreground "brown")))
1120 "ERC face used for your input."
1123 (defface erc-prompt-face
1124 '((t (:bold t :foreground "Black" :background"lightBlue2")))
1125 "ERC face for the prompt."
1128 (defface erc-command-indicator-face
1130 "ERC face for the command indicator.
1131 See the variable `erc-command-indicator'."
1134 (defface erc-notice-face '((t (:bold t :foreground "SlateBlue")))
1135 "ERC face for notices."
1138 (defface erc-action-face '((t (:bold t)))
1139 "ERC face for actions generated by /ME."
1142 (defface erc-error-face '((t (:foreground "red")))
1143 "ERC face for errors."
1146 (defface erc-nick-default-face '((t (:bold t)))
1147 "ERC nickname default face."
1150 (defface erc-nick-msg-face '((t (:bold t :foreground "IndianRed")))
1151 "ERC nickname face for private messages."
1154 ;; Debugging support
1156 (defvar erc-log-p nil
1157 "When set to t, generate debug messages in a separate debug buffer.")
1159 (defvar erc-debug-log-file (expand-file-name "ERC.debug")
1160 "Debug log file name.")
1162 (defvar erc-dbuf nil)
1163 (make-variable-buffer-local 'erc-dbuf)
1165 (defmacro define-erc-module (name alias doc enable-body disable-body
1167 "Define a new minor mode using ERC conventions.
1168 Symbol NAME is the name of the module.
1169 Symbol ALIAS is the alias to use, or nil.
1170 DOC is the documentation string to use for the minor mode.
1171 ENABLE-BODY is a list of expressions used to enable the mode.
1172 DISABLE-BODY is a list of expressions used to disable the mode.
1173 If LOCAL-P is non-nil, the mode will be created as a buffer-local
1174 mode. Rather than a global one.
1176 This will define a minor mode called erc-NAME-mode, possibly
1177 an alias erc-ALIAS-mode, as well as the helper functions
1178 erc-NAME-enable, and erc-NAME-disable.
1182 ;;;###autoload (autoload 'erc-replace-mode \"erc-replace\")
1183 (define-erc-module replace nil
1184 \"This mode replaces incoming text according to `erc-replace-alist'.\"
1185 ((add-hook 'erc-insert-modify-hook
1186 'erc-replace-insert))
1187 ((remove-hook 'erc-insert-modify-hook
1188 'erc-replace-insert)))"
1189 (let* ((sn (symbol-name name))
1190 (mode (intern (format "erc-%s-mode" (downcase sn))))
1191 (group (intern (format "erc-%s" (downcase sn))))
1192 (enable (intern (format "erc-%s-enable" (downcase sn))))
1193 (disable (intern (format "erc-%s-disable" (downcase sn)))))
1195 (erc-define-minor-mode
1197 ,(format "Toggle ERC %S mode.
1198 With arg, turn ERC %S mode on if and only if arg is positive.
1201 :global ,(not local-p) :group (quote ,group)
1206 ,(format "Enable ERC %S mode."
1209 (add-to-list 'erc-modules (quote ,name))
1213 ,(format "Disable ERC %S mode."
1216 (setq erc-modules (delq (quote ,name) erc-modules))
1219 ,(when (and alias (not (eq name alias)))
1223 (format "erc-%s-mode"
1224 (downcase (symbol-name alias)))))
1228 (put 'define-erc-module 'doc-string-elt 3)
1230 (defun erc-once-with-server-event (event &rest forms)
1231 "Execute FORMS the next time EVENT occurs in the `current-buffer'.
1233 You should make sure that `current-buffer' is a server buffer.
1235 This function temporarily adds a function to EVENT's hook to
1236 execute FORMS. After FORMS are run, the function is removed from
1237 EVENT's hook. The last expression of FORMS should be either nil
1238 or t. nil indicates that the other functions on EVENT's hook
1239 should be run too, and t indicates that other functions should
1242 Please be sure to use this function in server-buffers. In
1243 channel-buffers it may not work at all, as it uses the LOCAL
1244 argument of `add-hook' and `remove-hook' to ensure multiserver
1246 (unless (erc-server-buffer-p)
1248 "You should only run `erc-once-with-server-event' in a server buffer"))
1249 (let ((fun (erc-gensym))
1250 (hook (erc-get-hook event)))
1251 (put fun 'erc-original-buffer (current-buffer))
1252 (fset fun `(lambda (proc parsed)
1253 (with-current-buffer (get ',fun 'erc-original-buffer)
1254 (remove-hook ',hook ',fun t))
1257 (add-hook hook fun nil t)
1260 (defun erc-once-with-server-event-global (event &rest forms)
1261 "Execute FORMS the next time EVENT occurs in any server buffer.
1263 This function temporarily prepends a function to EVENT's hook to
1264 execute FORMS. After FORMS are run, the function is removed from
1265 EVENT's hook. The last expression of FORMS should be either nil
1266 or t. nil indicates that the other functions on EVENT's hook
1267 should be run too, and t indicates that other functions should
1270 When FORMS execute, the current buffer is the server buffer associated with the
1271 connection over which the data was received that triggered EVENT."
1272 (let ((fun (erc-gensym))
1273 (hook (erc-get-hook event)))
1274 (fset fun `(lambda (proc parsed)
1275 (remove-hook ',hook ',fun)
1278 (add-hook hook fun nil nil)
1281 (defmacro erc-log (string)
1282 "Logs STRING if logging is on (see `erc-log-p')."
1284 (erc-log-aux ,string)))
1286 (defun erc-server-buffer ()
1287 "Return the server buffer for the current buffer's process.
1288 The buffer-local variable `erc-server-process' is used to find
1289 the process buffer."
1290 (and (erc-server-buffer-live-p)
1291 (process-buffer erc-server-process)))
1293 (defun erc-server-buffer-live-p ()
1294 "Return t if the buffer associated with `erc-server-process'
1295 has not been killed."
1296 (and (processp erc-server-process)
1297 (buffer-live-p (process-buffer erc-server-process))))
1299 (defun erc-server-buffer-p (&optional buffer)
1300 "Return non-nil if argument BUFFER is an ERC server buffer.
1302 If BUFFER is nil, the current buffer is used."
1303 (with-current-buffer (or buffer (current-buffer))
1304 (and (eq major-mode 'erc-mode)
1305 (null (erc-default-target)))))
1307 (defun erc-query-buffer-p (&optional buffer)
1308 "Return non-nil if BUFFER is an ERC query buffer.
1309 If BUFFER is nil, the current buffer is used."
1310 (with-current-buffer (or buffer (current-buffer))
1311 (let ((target (erc-default-target)))
1312 (and (eq major-mode 'erc-mode)
1314 (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
1316 (defun erc-ison-p (nick)
1317 "Return non-nil if NICK is online."
1318 (interactive "sNick: ")
1319 (with-current-buffer (erc-server-buffer)
1320 (let ((erc-online-p 'unknown))
1321 (erc-once-with-server-event
1323 `(let ((ison (split-string (aref parsed 3))))
1324 (setq erc-online-p (car (erc-member-ignore-case ,nick ison)))
1326 (erc-server-send (format "ISON %s" nick))
1327 (while (eq erc-online-p 'unknown) (accept-process-output))
1329 (message "%s is %sonline"
1330 (or erc-online-p nick)
1331 (if erc-online-p "" "not "))
1334 (defun erc-log-aux (string)
1335 "Do the debug logging of STRING."
1336 (let ((cb (current-buffer))
1339 (session-buffer (erc-server-buffer)))
1342 (set-buffer session-buffer)
1343 (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
1345 (setq erc-dbuf (get-buffer-create
1346 (concat "*ERC-DEBUG: "
1347 erc-session-server "*")))))
1348 (set-buffer erc-dbuf)
1349 (setq point (point))
1350 (setq was-eob (eobp))
1351 (goto-char (point-max))
1352 (insert (concat "** " string "\n"))
1353 (if was-eob (goto-char (point-max))
1356 (message "ERC: ** %s" string))))
1358 ;; Last active buffer, to print server messages in the right place
1360 (defvar erc-active-buffer nil
1361 "The current active buffer, the one where the user typed the last command.
1362 Defaults to the server buffer, and should only be set in the
1364 (make-variable-buffer-local 'erc-active-buffer)
1366 (defun erc-active-buffer ()
1367 "Return the value of `erc-active-buffer' for the current server.
1368 Defaults to the server buffer."
1369 (with-current-buffer (erc-server-buffer) erc-active-buffer))
1371 (defun erc-set-active-buffer (buffer)
1372 "Set the value of `erc-active-buffer' to BUFFER."
1373 (cond ((erc-server-buffer)
1374 (with-current-buffer (erc-server-buffer)
1375 (setq erc-active-buffer buffer)))
1376 (t (setq erc-active-buffer buffer))))
1378 ;; Mode activation routines
1381 "Major mode for Emacs IRC.
1386 Turning on `erc-mode' runs the hook `erc-mode-hook'."
1387 (kill-all-local-variables)
1388 (use-local-map erc-mode-map)
1389 (setq mode-name "ERC"
1390 major-mode 'erc-mode
1391 local-abbrev-table erc-mode-abbrev-table)
1392 (set-syntax-table erc-mode-syntax-table)
1393 (when (boundp 'next-line-add-newlines)
1394 (set (make-local-variable 'next-line-add-newlines) nil))
1395 (setq line-move-ignore-invisible t)
1396 (set (make-local-variable 'paragraph-separate)
1397 (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
1398 (set (make-local-variable 'paragraph-start)
1399 (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
1400 ;; Run the mode hooks
1401 (run-hooks 'erc-mode-hook))
1405 (defconst erc-default-server "irc.freenode.net"
1406 "IRC server to use if it cannot be detected otherwise.")
1408 (defconst erc-default-port "ircd"
1409 "IRC port to use if it cannot be detected otherwise.")
1411 (defcustom erc-join-buffer 'buffer
1412 "Determines how to display the newly created IRC buffer.
1413 'window - in another window,
1414 'window-noselect - in another window, but don't select that one,
1415 'frame - in another frame,
1416 'bury - bury it in a new buffer,
1417 any other value - in place of the current buffer"
1419 :type '(choice (const window)
1420 (const window-noselect)
1425 (defcustom erc-frame-alist nil
1426 "*Alist of frame parameters for creating erc frames.
1427 A value of `nil means to use `default-frame-alist'."
1429 :type '(repeat (cons :format "%v"
1430 (symbol :tag "Parameter")
1431 (sexp :tag "Value"))))
1433 (defcustom erc-frame-dedicated-flag nil
1434 "*Non-nil means the erc frames are dedicated to that buffer.
1435 This only has effect when `erc-join-buffer' is set to `frame'."
1439 (defun erc-channel-p (channel)
1440 "Return non-nil if CHANNEL seems to be an IRC channel name."
1441 (cond ((stringp channel)
1442 (memq (aref channel 0) '(?# ?& ?+ ?!)))
1443 ((and (bufferp channel) (buffer-live-p channel))
1444 (with-current-buffer channel
1445 (erc-channel-p (erc-default-target))))
1448 (defcustom erc-reuse-buffers t
1449 "*If nil, create new buffers on joining a channel/query.
1450 If non-nil, a new buffer will only be created when you join
1451 channels with same names on different servers, or have query buffers
1452 open with nicks of the same name on different servers. Otherwise,
1453 the existing buffers will be reused."
1457 (defun erc-normalize-port (port)
1458 "Normalize the port specification PORT to integer form.
1459 PORT may be an integer, a string or a symbol. If it is a string or a
1460 symbol, it may have these values:
1464 * ircd-dalnet -> 7000"
1467 (erc-normalize-port (symbol-name port)))
1469 (let ((port-nr (string-to-number port)))
1473 ((string-equal port "irc")
1475 ((string-equal port "ircs")
1477 ((string-equal port "ircd")
1479 ((string-equal port "ircd-dalnet")
1488 (defun erc-port-equal (a b)
1489 "Check whether ports A and B are equal."
1490 (= (erc-normalize-port a) (erc-normalize-port b)))
1492 (defun erc-generate-new-buffer-name (server port target &optional proc)
1493 "Create a new buffer name based on the arguments."
1494 (when (numberp port) (setq port (number-to-string port)))
1495 (let* ((buf-name (or target
1496 (or (let ((name (concat server ":" port)))
1497 (when (> (length name) 1)
1499 ; This fallback should in fact never happen
1500 "*erc-server-buffer*"))))
1501 ;; Reuse existing buffers, but not if the buffer is a connected server
1502 ;; buffer and not if its associated with a different server than the
1503 ;; current ERC buffer.
1504 (if (and erc-reuse-buffers
1505 (get-buffer buf-name)
1507 (with-current-buffer (get-buffer buf-name)
1508 (and (erc-server-buffer-p)
1509 (not erc-server-connected))))
1510 (with-current-buffer (get-buffer buf-name)
1511 (and (string= erc-session-server server)
1512 (erc-port-equal erc-session-port port))))
1514 (generate-new-buffer-name buf-name))))
1516 (defun erc-get-buffer-create (server port target &optional proc)
1517 "Create a new buffer based on the arguments."
1518 (get-buffer-create (erc-generate-new-buffer-name server port target proc)))
1521 (defun erc-member-ignore-case (string list)
1522 "Return non-nil if STRING is a member of LIST.
1524 All strings are compared according to IRC protocol case rules, see
1526 (setq string (erc-downcase string))
1529 (if (string= string (erc-downcase (car list)))
1530 (throw 'result list) (setq list (cdr list))))))
1532 (defmacro erc-with-buffer (spec &rest body)
1533 "Execute BODY in the buffer associated with SPEC.
1535 SPEC should have the form
1539 If TARGET is a buffer, use it. Otherwise, use the buffer
1540 matching TARGET in the process specified by PROCESS.
1542 If PROCESS is nil, use the current `erc-server-process'
1543 See `erc-get-buffer' for details.
1545 See also `with-current-buffer'.
1547 \(fn (TARGET [PROCESS]) BODY...)"
1548 (let ((buf (erc-gensym))
1550 (target (erc-gensym))
1551 (process (erc-gensym)))
1552 `(let* ((,target ,(car spec))
1553 (,process ,(cadr spec))
1554 (,buf (if (bufferp ,target)
1556 (let ((,proc (or ,process
1557 (and (processp erc-server-process)
1558 erc-server-process))))
1559 (if (and ,target ,proc)
1560 (erc-get-buffer ,target ,proc))))))
1562 (with-current-buffer ,buf
1564 (put 'erc-with-buffer 'lisp-indent-function 1)
1565 (put 'erc-with-buffer 'edebug-form-spec '((form &optional form) body))
1567 (defun erc-get-buffer (target &optional proc)
1568 "Return the buffer matching TARGET in the process PROC.
1569 If PROC is not supplied, all processes are searched."
1570 (let ((downcased-target (erc-downcase target)))
1574 (let ((current (erc-default-target)))
1575 (and (stringp current)
1576 (string-equal downcased-target (erc-downcase current))
1577 (throw 'buffer (current-buffer)))))
1580 (defun erc-buffer-filter (predicate &optional proc)
1581 "Return a list of `erc-mode' buffers matching certain criteria.
1582 PREDICATE is a function executed with each buffer, if it returns t, that buffer
1583 is considered a valid match.
1585 PROC is either an `erc-server-process', identifying a certain
1586 server connection, or nil which means all open connections."
1590 (mapcar (lambda (buf)
1591 (with-current-buffer buf
1592 (and (eq major-mode 'erc-mode)
1594 (eq proc erc-server-process))
1599 (defun erc-buffer-list (&optional predicate proc)
1600 "Return a list of ERC buffers.
1601 PREDICATE is a function which executes with every buffer satisfying
1602 the predicate. If PREDICATE is passed as nil, return a list of all ERC
1603 buffers. If PROC is given, the buffers local variable `erc-server-process'
1604 needs to match PROC."
1606 (setq predicate (lambda () t)))
1607 (erc-buffer-filter predicate proc))
1609 (defmacro erc-with-all-buffers-of-server (process pred &rest forms)
1610 "Execute FORMS in all buffers which have same process as this server.
1611 FORMS will be evaluated in all buffers having the process PROCESS and
1612 where PRED matches or in all buffers of the server process if PRED is
1614 ;; Make the evaluation have the correct order
1615 (let ((pre (erc-gensym))
1617 `(let ((,pro ,process)
1619 (mapcar (lambda (buffer)
1620 (with-current-buffer buffer
1622 (erc-buffer-list ,pre
1624 (put 'erc-with-all-buffers-of-server 'lisp-indent-function 1)
1625 (put 'erc-with-all-buffers-of-server 'edebug-form-spec '(form form body))
1627 (defun erc-iswitchb (&optional arg)
1628 "Use `iswitchb-read-buffer' to prompt for a ERC buffer to switch to.
1629 When invoked with prefix argument, use all erc buffers. Without prefix
1630 ARG, allow only buffers related to same session server.
1631 If `erc-track-mode' is in enabled, put the last element of
1632 `erc-modified-channels-alist' in front of the buffer list.
1634 Due to some yet unresolved reason, global function `iswitchb-mode'
1635 needs to be active for this function to work."
1638 (require 'iswitchb))
1639 (let ((iswitchb-make-buflist-hook
1641 (setq iswitchb-temp-buflist
1642 (mapcar 'buffer-name
1645 (when (and arg (boundp 'erc-server-process))
1646 erc-server-process)))))))
1648 (iswitchb-read-buffer
1650 (if (boundp 'erc-modified-channels-alist)
1651 (buffer-name (caar (last erc-modified-channels-alist)))
1655 (defun erc-channel-list (proc)
1656 "Return a list of channel buffers.
1657 PROC is the process for the server connection. If PROC is nil, return
1658 all channel buffers on all servers."
1661 (and (erc-default-target)
1662 (erc-channel-p (erc-default-target))))
1665 (defun erc-buffer-list-with-nick (nick proc)
1666 "Return buffers containing NICK in the `erc-channel-users' list."
1667 (with-current-buffer (process-buffer proc)
1668 (let ((user (gethash (erc-downcase nick) erc-server-users)))
1670 (erc-server-user-buffers user)
1673 ;; Some local variables
1675 (defvar erc-default-recipients nil
1676 "List of default recipients of the current buffer.")
1677 (make-variable-buffer-local 'erc-default-recipients)
1679 (defvar erc-session-user-full-name nil
1680 "Full name of the user on the current server.")
1681 (make-variable-buffer-local 'erc-session-user-full-name)
1683 (defvar erc-channel-user-limit nil
1684 "Limit of users per channel.")
1685 (make-variable-buffer-local 'erc-channel-user-limit)
1687 (defvar erc-channel-key nil
1688 "Key needed to join channel.")
1689 (make-variable-buffer-local 'erc-channel-key)
1691 (defvar erc-invitation nil
1692 "Last invitation channel.")
1693 (make-variable-buffer-local 'erc-invitation)
1695 (defvar erc-away nil
1696 "Non-nil indicates that we are away.")
1697 (make-variable-buffer-local 'erc-away)
1699 (defvar erc-channel-list nil
1700 "Server channel list.")
1701 (make-variable-buffer-local 'erc-channel-list)
1703 (defvar erc-bad-nick nil
1704 "Non-nil indicates that we got a `nick in use' error while connecting.")
1705 (make-variable-buffer-local 'erc-bad-nick)
1707 (defvar erc-logged-in nil
1708 "Non-nil indicates that we are logged in.")
1709 (make-variable-buffer-local 'erc-logged-in)
1711 (defvar erc-default-nicks nil
1712 "The local copy of `erc-nick' - the list of nicks to choose from.")
1713 (make-variable-buffer-local 'erc-default-nicks)
1715 (defvar erc-nick-change-attempt-count 0
1716 "Used to keep track of how many times an attempt at changing nick is made.")
1717 (make-variable-buffer-local 'erc-nick-change-attempt-count)
1719 (defcustom erc-modules '(netsplit fill button match track pcomplete readonly
1720 ring autojoin noncommands irccontrols
1722 "A list of modules which erc should enable.
1723 If you set the value of this without using `customize' remember to call
1724 \(erc-update-modules) after you change it. When using `customize', modules
1725 removed from the list will be disabled."
1726 :set (lambda (sym val)
1727 ;; disable modules which have just been removed
1728 (when (and (boundp 'erc-modules) erc-modules val)
1729 (dolist (module erc-modules)
1730 (unless (member module val)
1731 (let ((f (intern-soft (format "erc-%s-mode" module))))
1732 (when (and (fboundp f) (boundp f) (symbol-value f))
1733 (message "Disabling `erc-%s'" module)
1735 (set-default sym val)
1736 ;; this test is for the case where erc hasn't been loaded yet
1737 (when (fboundp 'erc-update-modules)
1738 (erc-update-modules)))
1739 :type '(set :greedy t
1740 (const :tag "Set away status automatically" autoaway)
1741 (const :tag "Join channels automatically" autojoin)
1742 (const :tag "Integrate with Big Brother Database" bbdb)
1743 (const :tag "Buttonize URLs, nicknames, and other text" button)
1744 (const :tag "Wrap long lines" fill)
1745 (const :tag "Highlight or remove IRC control characters"
1747 (const :tag "Save buffers in logs" log)
1748 (const :tag "Highlight pals, fools, and other keywords" match)
1749 (const :tag "Detect netsplits" netsplit)
1750 (const :tag "Don't display non-IRC commands after evaluation"
1753 "Notify when the online status of certain users changes"
1755 (const :tag "Complete nicknames and commands (programmable)"
1757 (const :tag "Complete nicknames and commands (old)" completion)
1758 (const :tag "Make displayed lines read-only" readonly)
1759 (const :tag "Replace text in messages" replace)
1760 (const :tag "Enable an input history" ring)
1761 (const :tag "Scroll to the bottom of the buffer" scrolltobottom)
1762 (const :tag "Identify to Nickserv (IRC Services) automatically"
1764 (const :tag "Convert smileys to pretty icons" smiley)
1765 (const :tag "Play sounds when you receive CTCP SOUND requests"
1767 (const :tag "Add timestamps to messages" stamp)
1768 (const :tag "Check spelling" spelling)
1769 (const :tag "Track channel activity in the mode-line" track)
1770 (const :tag "Truncate buffers to a certain size" truncate)
1771 (const :tag "Translate morse code in messages" unmorse)
1772 (repeat :tag "Others" :inline t symbol))
1775 (defun erc-update-modules ()
1776 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1778 (dolist (mod erc-modules)
1779 (setq req (concat "erc-" (symbol-name mod)))
1781 ;; yuck. perhaps we should bring the filenames into sync?
1782 ((string= req "erc-completion")
1783 (setq req "erc-pcomplete")
1784 (setq mod 'pcomplete))
1785 ((string= req "erc-services")
1786 (setq req "erc-nickserv")
1787 (setq mod 'services)))
1789 (require (intern req))
1791 (funcall (or (intern-soft (concat "erc-" (symbol-name mod) "-mode"))
1792 (error "`%s' is not a known ERC module" mod))
1795 (defun erc-setup-buffer (buffer)
1796 "Consults `erc-join-buffer' to find out how to display `BUFFER'."
1797 (cond ((eq erc-join-buffer 'window)
1798 (if (active-minibuffer-window)
1799 (display-buffer buffer)
1800 (switch-to-buffer-other-window buffer)))
1801 ((eq erc-join-buffer 'window-noselect)
1802 (display-buffer buffer))
1803 ((eq erc-join-buffer 'bury)
1805 ((eq erc-join-buffer 'frame)
1806 (funcall '(lambda (frame)
1808 (select-frame frame))
1809 (make-frame (or erc-frame-alist
1810 default-frame-alist)))
1811 (switch-to-buffer buffer)
1812 (when erc-frame-dedicated-flag
1813 (set-window-dedicated-p (selected-window) t)))
1815 (if (active-minibuffer-window)
1816 (display-buffer buffer)
1817 (switch-to-buffer buffer)))))
1819 (defun erc (&optional server port nick full-name
1820 connect passwd tgt-list channel process)
1821 "ERC is a powerful, modular, and extensible IRC client.
1823 Connect to SERVER on PORT as NICK with FULL-NAME.
1825 If CONNECT is non-nil, connect to the server. Otherwise assume
1826 already connected and just create a separate buffer for the new
1829 Use PASSWD as user password on the server. If TGT-LIST is
1830 non-nil, use it to initialise `erc-default-recipients'.
1832 Returns the buffer for the given server or channel."
1833 (let ((server-announced-name (when (and (boundp 'erc-session-server)
1834 (string= server erc-session-server))
1835 erc-server-announced-name))
1836 (connected-p (unless connect erc-server-connected))
1837 (buffer (erc-get-buffer-create server port channel))
1838 (old-buffer (current-buffer))
1840 (erc-update-modules)
1843 (setq erc-server-announced-name server-announced-name)
1844 (setq erc-server-connected connected-p)
1845 ;; connection parameters
1846 (setq erc-server-process process)
1847 (setq erc-insert-marker (make-marker))
1848 (setq erc-input-marker (make-marker))
1849 ;; go to the end of the buffer and open a new line
1850 ;; (the buffer may have existed)
1851 (goto-char (point-max))
1853 (when (get-text-property (point) 'erc-prompt)
1854 (setq continued-session t)
1855 (set-marker erc-input-marker
1856 (or (next-single-property-change (point) 'erc-prompt)
1858 (unless continued-session
1859 (goto-char (point-max))
1861 (set-marker erc-insert-marker (point))
1862 ;; stack of default recipients
1863 (setq erc-default-recipients tgt-list)
1864 (setq erc-server-current-nick nil)
1865 ;; Initialize erc-server-users and erc-channel-users
1867 (progn ;; server buffer
1868 (setq erc-server-users
1869 (make-hash-table :test 'equal))
1870 (setq erc-channel-users nil))
1871 (progn ;; target buffer
1872 (setq erc-server-users nil)
1873 (setq erc-channel-users
1874 (make-hash-table :test 'equal))))
1875 ;; clear last incomplete line read
1876 (setq erc-server-filter-data nil)
1877 (setq erc-channel-topic "")
1878 ;; limit on the number of users on the channel (mode +l)
1879 (setq erc-channel-user-limit nil)
1880 (setq erc-channel-key nil)
1881 ;; last active buffer, defaults to this one
1882 (erc-set-active-buffer buffer)
1883 ;; last invitation channel
1884 (setq erc-invitation nil)
1886 ;; Should only be used in session-buffers
1887 (setq erc-away (let ((serverbuf (erc-server-buffer)))
1888 (and serverbuf (with-current-buffer serverbuf erc-away))))
1889 ;; Server channel list
1890 (setq erc-channel-list ())
1891 ;; login-time 'nick in use' error
1892 (setq erc-bad-nick nil)
1893 ;; whether we have logged in
1894 (setq erc-logged-in nil)
1895 ;; The local copy of `erc-nick' - the list of nicks to choose
1896 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
1898 (setq erc-session-password passwd)
1899 ;; debug output buffer
1902 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
1903 (erc-determine-parameters server port nick full-name)
1905 ;; Saving log file on exit
1906 (run-hooks 'erc-connect-pre-hook)
1909 (erc-server-connect erc-session-server erc-session-port))
1910 (erc-update-mode-line)
1911 (set-marker erc-insert-marker (point))
1912 (unless continued-session
1913 (goto-char (point-max))
1915 (set-marker (process-mark erc-server-process) (point))
1916 (unless continued-session
1917 (set-marker erc-insert-marker (point))
1918 (erc-display-prompt)
1919 (goto-char (point-max)))
1921 ;; Now display the buffer in a window as per user wishes.
1922 (unless (eq buffer old-buffer)
1924 ;; we can't log to debug buffer, it may not exist yet
1925 (message "erc: old buffer %s, switching to %s"
1927 (erc-setup-buffer buffer))
1931 (defun erc-initialize-log-marker ()
1932 "Initialize the `erc-last-saved-position' marker to a sensible position."
1933 (setq erc-last-saved-position (make-marker))
1934 (move-marker erc-last-saved-position
1935 (1- (marker-position erc-insert-marker))))
1937 ;; interactive startup
1939 (defvar erc-server-history-list nil
1940 "IRC server interactive selection history list.")
1942 (defvar erc-nick-history-list nil
1943 "Nickname interactive selection history list.")
1945 (defun erc-already-logged-in (server port nick)
1946 "Return the buffers corresponding to a NICK on PORT of a session SERVER.
1947 This is determined by looking for the appropriate buffer and checking
1948 whether the connection is still alive.
1949 If no buffer matches, return nil."
1952 (and (erc-server-process-alive)
1953 (string= erc-session-server server)
1954 (erc-port-equal erc-session-port port)
1955 (erc-current-nick-p nick)))))
1957 (if (not (fboundp 'read-passwd))
1958 (defun read-passwd (prompt)
1959 "Substitute for read-passwd in early emacsen"
1960 (read-from-minibuffer prompt)))
1962 (defcustom erc-before-connect nil
1963 "Hook called before connecting to a server.
1964 This hook gets executed before `erc-select' actually invokes `erc-mode'
1965 with your input data. The functions in here get called with three
1966 parameters, SERVER, PORT and NICK."
1970 (defcustom erc-after-connect nil
1971 "Hook called after connecting to a server.
1972 This hook gets executed when an end of MOTD has been received. All
1973 functions in here get called with the parameters SERVER and NICK."
1978 (defun erc-select-read-args ()
1979 "Prompt the user for values of nick, server, port, and password."
1980 (let (user-input server port nick passwd)
1981 (setq user-input (read-from-minibuffer
1983 (erc-compute-server) nil nil 'erc-server-history-list))
1985 (if (string-match "\\(.*\\):\\(.*\\)\\'" user-input)
1986 (setq port (erc-string-to-port (match-string 2 user-input))
1987 user-input (match-string 1 user-input))
1989 (erc-string-to-port (read-from-minibuffer
1990 "IRC port: " (erc-port-to-string
1991 (erc-compute-port))))))
1993 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
1994 (setq nick (match-string 1 user-input)
1995 user-input (match-string 2 user-input))
1997 (if (erc-already-logged-in server port nick)
1998 (read-from-minibuffer
1999 (erc-format-message 'nick-in-use ?n nick)
2001 nil nil 'erc-nick-history-list)
2002 (read-from-minibuffer
2003 "Nickname: " (erc-compute-nick nick)
2004 nil nil 'erc-nick-history-list))))
2006 (setq server user-input)
2008 (setq passwd (if erc-prompt-for-password
2009 (if (and erc-password
2010 (y-or-n-p "Use the default password? "))
2012 (read-passwd "Password: "))
2014 (when (and passwd (string= "" passwd))
2017 (while (erc-already-logged-in server port nick)
2018 ;; hmm, this is a problem when using multiple connections to a bnc
2019 ;; with the same nick. Currently this code prevents using more than one
2020 ;; bnc with the same nick. actually it would be nice to have
2021 ;; bncs transparent, so that erc-compute-buffer-name displays
2022 ;; the server one is connected to.
2023 (setq nick (read-from-minibuffer
2024 (erc-format-message 'nick-in-use ?n nick)
2026 nil nil 'erc-nick-history-list)))
2027 (list :server server :port port :nick nick :password passwd)))
2030 (defun* erc-select (&key (server (erc-compute-server))
2031 (port (erc-compute-port))
2032 (nick (erc-compute-nick))
2034 (full-name (erc-compute-full-name)))
2035 "Select connection parameters and run ERC.
2036 Non-interactively, it takes keyword arguments
2037 (server (erc-compute-server))
2038 (port (erc-compute-port))
2039 (nick (erc-compute-nick))
2041 (full-name (erc-compute-full-name)))
2043 That is, if called with
2044 (erc-select :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
2045 server and full-name will be set to those values, whereas
2046 erc-compute-port, erc-compute-nick and erc-compute-full-name will
2047 be invoked for those parameters' values"
2048 (interactive (erc-select-read-args))
2050 (run-hook-with-args 'erc-before-connect server port nick)
2051 (erc server port nick erc-user-full-name t password))
2054 (defun erc-select-ssl (&rest r)
2055 "Interactively select SSL connection parameters and run ERC.
2056 Arguments are as to erc-select."
2057 (interactive (erc-select-read-args))
2058 (let ((erc-server-connect-function 'erc-open-ssl-stream))
2059 (apply 'erc-select r)))
2061 (defun erc-open-ssl-stream (name buffer host port)
2062 "Open an SSL stream to an IRC server.
2063 The process will be given the name NAME, its target buffer will be
2064 BUFFER. HOST and PORT specify the connection target."
2065 (when (require 'ssl)
2066 (let ((proc (open-ssl-stream name buffer host port)))
2067 ;; Ugly hack, but it works for now. Problem is it is
2068 ;; very hard to detect when ssl is established, because s_client
2069 ;; doesn't give any CONNECTIONESTABLISHED kind of message, and
2070 ;; most IRC servers send nothing and wait for you to identify.
2074 ;;; Debugging the protocol
2076 (defvar erc-debug-irc-protocol nil
2077 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2079 The buffer is created if it doesn't exist.
2081 NOTE: If this variable is non-nil, and you kill the the only
2082 visible \"*erc-protocol*\" buffer, it will be recreated shortly,
2083 but you won't see it.
2085 WARNING: Do not set this variable directly! Instead, use the
2086 function `erc-toggle-debug-irc-protocol' to toggle its value.")
2088 (defun erc-log-irc-protocol (string &optional outbound)
2089 "Append STRING to the buffer *erc-protocol*.
2091 This only has any effect if `erc-debug-irc-protocol' is non-nil.
2093 The buffer is created if it doesn't exist.
2095 If OUTBOUND is non-nil, STRING is being sent to the IRC server
2096 and appears in erc-input-face in the buffer."
2097 (when erc-debug-irc-protocol
2098 (let ((network-name (or (ignore-errors (erc-network-name))
2100 (with-current-buffer (get-buffer-create "*erc-protocol*")
2102 (goto-char (point-max))
2103 (let ((inhibit-read-only t))
2104 (insert (if (not outbound)
2105 ;; Cope with the fact that string might
2106 ;; contain multiple lines of text.
2107 (let ((lines (delete "" (split-string string
2110 (dolist (line lines)
2111 (setq result (concat result network-name
2115 (concat network-name " >> " string
2118 (1- (length string))))
2120 'face 'erc-input-face)))))
2121 (let ((orig-win (selected-window))
2122 (debug-buffer-window (get-buffer-window (current-buffer) t)))
2123 (when debug-buffer-window
2124 (select-window debug-buffer-window)
2125 (when (= 1 (count-lines (point) (point-max)))
2126 (goto-char (point-max))
2128 (select-window orig-win)))))))
2130 (defun erc-toggle-debug-irc-protocol (&optional arg)
2131 "Toggle the value of `erc-debug-irc-protocol'.
2133 If ARG is non-nil, show the *erc-protocol* buffer."
2135 (let* ((buf (get-buffer-create "*erc-protocol*")))
2136 (with-current-buffer buf
2137 (erc-view-mode-enter 1)
2138 (when (null (current-local-map))
2139 (let ((inhibit-read-only t))
2140 (insert (erc-make-notice "This buffer displays all IRC protocol traffic exchanged with each server.\n"))
2141 (insert (erc-make-notice "Kill this buffer to terminate protocol logging.\n\n")))
2142 (use-local-map (make-sparse-keymap))
2143 (local-set-key (kbd "RET") 'erc-toggle-debug-irc-protocol))
2144 (add-hook 'kill-buffer-hook
2145 #'(lambda () (setq erc-debug-irc-protocol nil))
2147 (goto-char (point-max))
2148 (let ((inhibit-read-only t))
2149 (insert (erc-make-notice
2150 (format "IRC protocol logging %s at %s -- Press ENTER to toggle logging.\n"
2151 (if erc-debug-irc-protocol "disabled" "enabled")
2152 (current-time-string))))))
2153 (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
2155 (not (get-buffer-window "*erc-protocol*" t)))
2156 (display-buffer buf t))
2157 (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
2158 (if erc-debug-irc-protocol "enabled" "disabled"))))
2164 (defun erc-send-action (tgt str &optional force)
2165 "Send CTCP ACTION information described by STR to TGT."
2166 (erc-send-ctcp-message tgt (format "ACTION %s" str) force)
2167 (erc-display-message
2168 nil 'input (current-buffer)
2169 'ACTION ?n (erc-current-nick) ?a str ?u "" ?h ""))
2171 ;; Display interface
2173 (defun erc-string-invisible-p (string)
2174 "Check whether STRING is invisible or not.
2175 I.e. any char in it has the `invisible' property set."
2176 (text-property-any 0 (length string) 'invisible t string))
2178 (defun erc-display-line-1 (string buffer)
2179 "Display STRING in `erc-mode' BUFFER.
2180 Auxiliary function used in `erc-display-line'. The line gets filtered to
2181 interpret the control characters. Then, `erc-insert-pre-hook' gets called.
2182 If `erc-insert-this' is still t, STRING gets inserted into the buffer.
2183 Afterwards, `erc-insert-modify' and `erc-insert-post-hook' get called.
2184 If STRING is nil, the function does nothing."
2187 (set-buffer (or buffer (process-buffer erc-server-process)))
2188 (let ((insert-position (or (marker-position erc-insert-marker)
2190 (let ((string string) ;; FIXME! Can this be removed?
2191 (buffer-undo-list t)
2192 (inhibit-read-only t))
2193 (unless (string-match "\n$" string)
2194 (setq string (concat string "\n"))
2195 (when (erc-string-invisible-p string)
2196 (erc-put-text-properties 0 (length string) string
2197 '(invisible intangible))))
2198 (erc-log (concat "erc-display-line: " string
2199 (format "(%S)" string) " in buffer "
2200 (format "%s" buffer)))
2201 (setq erc-insert-this t)
2202 (run-hook-with-args 'erc-insert-pre-hook string)
2203 (if (null erc-insert-this)
2204 ;; Leave erc-insert-this set to t as much as possible. Fran
2205 ;; Litterio <franl> has seen erc-insert-this set to nil while
2206 ;; erc-send-pre-hook is running, which should never happen. This
2208 (setq erc-insert-this t)
2209 (save-excursion ;; to restore point in the new buffer
2212 (goto-char insert-position)
2213 (insert-before-markers string)
2214 ;; run insertion hook, with point at restored location
2216 (narrow-to-region insert-position (point))
2217 (run-hooks 'erc-insert-modify-hook)
2218 (run-hooks 'erc-insert-post-hook))))))
2219 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2221 insert-position))))))
2223 (defun erc-update-undo-list (shift)
2224 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2225 (unless (or (zerop shift) (atom buffer-undo-list))
2226 (let ((list buffer-undo-list) elt)
2228 (setq elt (car list))
2229 (cond ((integerp elt) ; POSITION
2230 (incf (car list) shift))
2231 ((or (atom elt) ; nil, EXTENT
2232 ;; (eq t (car elt)) ; (t HIGH . LOW)
2233 (markerp (car elt))) ; (MARKER . DISTANCE)
2235 ((integerp (car elt)) ; (BEGIN . END)
2236 (incf (car elt) shift)
2237 (incf (cdr elt) shift))
2238 ((stringp (car elt)) ; (TEXT . POSITION)
2239 (incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
2240 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2241 (let ((cons (nthcdr 3 elt)))
2242 (incf (car cons) shift)
2243 (incf (cdr cons) shift)))
2244 ((and (featurep 'xemacs)
2245 (extentp (car elt))) ; (EXTENT START END)
2246 (incf (nth 1 elt) shift)
2247 (incf (nth 2 elt) shift)))
2248 (setq list (cdr list))))))
2250 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2251 "Regexp which matches all legal characters in a IRC nickname.")
2253 (defun erc-is-valid-nick-p (nick)
2254 "Check if NICK is a valid IRC nickname."
2255 (string-match (concat "^" erc-valid-nick-regexp "$") nick))
2257 (defun erc-display-line (string &optional buffer)
2258 "Display STRING in the ERC BUFFER.
2259 All screen output must be done through this function. If BUFFER is nil
2260 or omitted, the default ERC buffer for the `erc-session-server' is used.
2261 The BUFFER can be an actual buffer, a list of buffers, 'all or 'active.
2262 If BUFFER = 'all, the string is displayed in all the ERC buffers for the
2263 current session. 'active means the current active buffer
2264 \(`erc-active-buffer'). If the buffer can't be resolved, the current
2265 buffer is used. `erc-display-line-1' is used to display STRING.
2267 If STRING is nil, the function does nothing."
2268 (let ((inhibit-point-motion-hooks t)
2271 ((bufferp buffer) (list buffer))
2272 ((listp buffer) buffer)
2273 ((processp buffer) (list (process-buffer buffer)))
2275 (and (boundp 'erc-server-process)
2276 ;; Hmm, or all of the same session server?
2277 (erc-buffer-list nil erc-server-process)))
2278 ((and (eq 'active buffer) (erc-active-buffer))
2279 (list (erc-active-buffer)))
2280 ((erc-server-buffer-live-p)
2281 (list (process-buffer erc-server-process)))
2282 (t (list (current-buffer)))))
2283 (when (buffer-live-p buf)
2284 (erc-display-line-1 string buf)
2285 (add-to-list 'new-bufs buf)))
2286 (when (null new-bufs)
2287 (if (erc-server-buffer-live-p)
2288 (erc-display-line-1 string (process-buffer erc-server-process))
2289 (erc-display-line-1 string (current-buffer))))))
2291 (defun erc-display-message-highlight (type string)
2292 "Highlight STRING according to TYPE, where erc-TYPE-face is an erc face.
2294 See also `erc-make-notice'"
2295 (cond ((eq type 'notice)
2296 (erc-make-notice string))
2298 (erc-put-text-property
2300 'face (or (intern-soft
2301 (concat "erc-" (symbol-name type) "-face"))
2306 (defun erc-display-message (parsed type buffer msg &rest args)
2307 "Display MSG in BUFFER.
2309 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2311 See also `erc-format-message' and `erc-display-line'."
2312 (let ((string (if (symbolp msg)
2313 (apply 'erc-format-message msg args)
2318 (mapc (lambda (type)
2320 (erc-display-message-highlight type string)))
2324 (erc-display-message-highlight type string))))
2326 (if (not (erc-response-p parsed))
2327 (erc-display-line string buffer)
2328 (unless (member (erc-response.command parsed) erc-hide-list)
2329 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
2330 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2331 (erc-display-line string buffer)))))
2333 (defun erc-message-type-member (position list)
2334 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2336 This function relies on the erc-parsed text-property being
2338 (let ((prop-val (get-text-property position 'erc-parsed)))
2339 (and prop-val (member (erc-response.command prop-val) list))))
2341 (defvar erc-send-input-line-function 'erc-send-input-line)
2342 (make-variable-buffer-local 'erc-send-input-line-function)
2344 (defun erc-send-input-line (target line &optional force)
2345 "Send LINE to TARGET.
2347 See also `erc-server-send'."
2348 (setq line (format "PRIVMSG %s :%s"
2350 ;; If the line is empty, we still want to
2351 ;; send it - i.e. an empty pasted line.
2352 (if (string= line "\n")
2355 (erc-server-send line force target))
2357 (defun erc-get-arglist (fun)
2358 "Return the argument list of a function without the parens."
2359 (let ((arglist (format "%S" (erc-function-arglist fun))))
2360 (if (string-match "^(\\(.*\\))$" arglist)
2361 (match-string 1 arglist)
2364 (defun erc-command-name (cmd)
2365 "For CMD being the function name of a ERC command, something like
2366 erc-cmd-FOO, this returns a string /FOO."
2367 (let ((command-name (symbol-name cmd)))
2368 (if (string-match "^erc-cmd-\\(.*\\)$" command-name)
2369 (concat "/" (match-string 1 command-name))
2372 (defun erc-process-input-line (line &optional force no-command)
2373 "Translate LINE to an RFC1459 command and send it based.
2374 Returns non-nil if the command is actually sent to the server, and nil
2377 If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
2378 it is passed to `erc-cmd-default'. If LINE is not a command (ie. doesn't
2379 start with /<COMMAND>) then it is sent as a message.
2381 An optional FORCE argument forces sending the line when flood
2382 protection is in effect. The optional NO-COMMAND argument prohibits
2383 this function from interpreting the line as a command."
2384 (let ((command-list (erc-extract-command-from-line line)))
2385 (if (and command-list
2387 (let* ((cmd (nth 0 command-list))
2388 (args (nth 1 command-list)))
2393 (wrong-number-of-arguments
2394 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2395 ?c (erc-command-name cmd)
2396 ?u (or (erc-get-arglist cmd)
2399 (or (documentation cmd) "")))
2401 (let ((r (erc-default-target)))
2403 (funcall erc-send-input-line-function r line force)
2404 (erc-display-message nil 'error (current-buffer) 'no-target)
2407 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2408 ;; Input commands handlers
2409 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2411 (defun erc-cmd-AMSG (line)
2412 "Send LINE to all channels of the current server that you are on."
2413 (interactive "sSend to all channels you're on: ")
2414 (setq line (erc-trim-string line))
2415 (erc-with-all-buffers-of-server nil
2417 (erc-channel-p (erc-default-target)))
2418 (erc-send-message line)))
2419 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2421 (defun erc-cmd-SAY (line)
2422 "Send LINE to the current query or channel as a message, not a command.
2424 Use this when you want to send a message with a leading '/'. Note
2425 that since multi-line messages are never a command, you don't
2426 need this when pasting multiple lines of text."
2427 (if (string-match "^\\s-*$" line)
2429 (string-match "^ ?\\(.*\\)" line)
2430 (erc-process-input-line (match-string 1 line) nil t)))
2431 (put 'erc-cmd-SAY 'do-not-parse-args t)
2433 (defun erc-cmd-SET (line)
2434 "Set the variable named by the first word in LINE to some VALUE.
2435 VALUE is computed by evaluating the rest of LINE in Lisp."
2437 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2438 (let ((var (read (concat "erc-" (match-string 1 line))))
2439 (val (read (match-string 2 line))))
2442 (set var (eval val))
2443 (erc-display-message
2444 nil nil 'active (format "Set %S to %S" var val))
2446 (setq var (read (match-string 1 line)))
2449 (set var (eval val))
2450 (erc-display-message
2451 nil nil 'active (format "Set %S to %S" var val))
2453 (erc-display-message nil 'error 'active 'variable-not-bound)
2455 ((string-match "^\\s-*$" line)
2457 (concat "Available user variables:\n"
2462 (let ((val (symbol-value var)))
2463 (concat (format "%S:" var)
2465 (concat "\n" (pp-to-string val))
2466 (format " %S\n" val)))))
2467 (apropos-internal "^erc-" 'user-variable-p))))
2468 (current-buffer)) t)
2470 (defalias 'erc-cmd-VAR 'erc-cmd-SET)
2471 (defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2472 (put 'erc-cmd-SET 'do-not-parse-args t)
2474 (defun erc-cmd-default (line)
2477 Commands for which no erc-cmd-xxx exists, are tunnelled through
2478 this function. LINE is sent to the server verbatim, and
2479 therefore has to contain the command itself as well."
2480 (erc-log (format "cmd: DEFAULT: %s" line))
2481 (erc-server-send (substring line 1))
2484 (defun erc-cmd-IGNORE (&optional user)
2485 "Ignore USER. This should be a regexp matching nick!user@host.
2486 If no USER argument is specified, list the contents of `erc-ignore-list'."
2490 (erc-make-notice (format "Now ignoring %s" user))
2492 (with-current-buffer (erc-server-buffer)
2493 (add-to-list 'erc-ignore-list user)))
2494 (if (null (with-current-buffer (erc-server-buffer) erc-ignore-list))
2495 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2496 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2497 (mapc #'(lambda (item)
2498 (erc-display-line (erc-make-notice item)
2500 (with-current-buffer (erc-server-buffer) erc-ignore-list))))
2503 (defun erc-cmd-UNIGNORE (user)
2504 "Remove the user specified in USER from the ignore list."
2505 (let ((ignored-nick (car (with-current-buffer (erc-server-buffer)
2506 (erc-member-ignore-case user erc-ignore-list)))))
2507 (if (null ignored-nick)
2509 (erc-make-notice (format "%s is not currently ignored!" user))
2512 (erc-make-notice (format "No longer ignoring %s" user))
2514 (with-current-buffer (erc-server-buffer)
2515 (setq erc-ignore-list (delete ignored-nick erc-ignore-list))))
2518 (defun erc-cmd-CLEAR ()
2519 "Clear the window content."
2523 (defun erc-cmd-OPS ()
2524 "Show the ops in the current channel."
2527 (if erc-channel-users
2528 (maphash (lambda (nick user-data)
2529 (let ((cuser (cdr user-data)))
2531 (erc-channel-user-op cuser))
2532 (setq ops (cons (erc-server-user-nickname
2536 (setq ops (sort ops 'string-lessp))
2538 (erc-display-message
2539 nil 'notice (current-buffer) 'ops
2540 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2541 ?o (mapconcat 'identity ops " "))
2542 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2545 (defun erc-cmd-COUNTRY (tld)
2546 "Display the country associated with the top level domain TLD."
2547 (require 'mail-extr)
2548 (let ((co (ignore-errors (what-domain tld))))
2550 (erc-display-message
2551 nil 'notice 'active 'country ?c co ?d tld)
2552 (erc-display-message
2553 nil 'notice 'active 'country-unknown ?d tld))
2556 (defun erc-cmd-AWAY (line)
2557 "Mark the user as being away, the reason being indicated by LINE.
2558 If no reason is given, unset away status."
2559 (when (string-match "^\\s-*\\(.*\\)$" line)
2560 (let ((reason (match-string 1 line)))
2561 (erc-log (format "cmd: AWAY: %s" reason))
2563 (if (string= reason "")
2565 (concat "AWAY :" reason))))
2567 (put 'erc-cmd-AWAY 'do-not-parse-args t)
2569 (defun erc-cmd-GAWAY (line)
2570 "Mark the user as being away everywhere, the reason being indicated by LINE."
2571 ;; on all server buffers.
2572 (erc-with-all-buffers-of-server nil
2573 #'erc-server-buffer-p
2574 (erc-cmd-AWAY line)))
2575 (put 'erc-cmd-GAWAY 'do-not-parse-args t)
2577 (defun erc-cmd-CTCP (nick cmd &rest args)
2578 "Send a Client To Client Protocol message to NICK.
2580 CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2581 VERSION and so on. It is called with ARGS."
2582 (let ((str (concat cmd
2584 (concat " " (mapconcat #'identity args " "))))))
2585 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2586 (erc-send-ctcp-message nick str)
2589 (defun erc-cmd-HELP (&optional func)
2590 "Popup help information.
2592 If FUNC contains a valid function or variable, help about that
2593 will be displayed. If FUNC is empty, display an apropos about
2594 erc commands. Otherwise, do apropos in the erc namespace
2598 To find out about erc and bbdb, do
2601 For help about the WHOIS command, do:
2604 For a list of user commands (/join /part, ...):
2607 (let* ((sym (or (let ((sym (intern-soft
2608 (concat "erc-cmd-" (upcase func)))))
2609 (if (and sym (or (boundp sym) (fboundp sym)))
2612 (let ((sym (intern-soft func)))
2613 (if (and sym (or (boundp sym) (fboundp sym)))
2616 (let ((sym (intern-soft (concat "erc-" func))))
2617 (if (and sym (or (boundp sym) (fboundp sym)))
2622 ((boundp sym) (describe-variable sym))
2623 ((fboundp sym) (describe-function sym))
2625 (apropos-command (concat "erc-.*" func) nil
2628 (get x 'custom-type))))
2630 (apropos "erc-cmd-")
2631 (message "Type C-h m to get additional information about keybindings.")
2634 (defalias 'erc-cmd-H 'erc-cmd-HELP)
2636 (defun erc-cmd-JOIN (channel &optional key)
2637 "Join the channel given in CHANNEL, optionally with KEY.
2638 If CHANNEL is specified as \"-invite\", join the channel to which you
2639 were most recently invited. See also `invitation'."
2641 (if (string= (upcase channel) "-INVITE")
2643 (setq chnl erc-invitation)
2644 (erc-display-message nil 'error (current-buffer) 'no-invitation))
2645 (setq chnl (erc-ensure-channel-name channel)))
2647 ;; Prevent double joining of same channel on same server.
2648 (let ((joined-channels
2649 (mapcar #'(lambda (chanbuf)
2650 (with-current-buffer chanbuf (erc-default-target)))
2651 (erc-channel-list erc-server-process))))
2652 (if (erc-member-ignore-case chnl joined-channels)
2653 (switch-to-buffer (car (erc-member-ignore-case chnl
2655 (erc-log (format "cmd: JOIN: %s" chnl))
2657 (erc-server-send (format "JOIN %s %s" chnl key))
2658 (erc-server-send (format "JOIN %s" chnl)))))))
2661 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
2662 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
2664 (defvar erc-channel-new-member-names nil
2665 "If non-nil, a names list is currently being received.
2667 If non-nil, this variable is a hash-table that associates
2668 received nicks with t.")
2669 (make-variable-buffer-local 'erc-channel-new-member-names)
2671 (defun erc-cmd-NAMES (&optional channel)
2672 "Display the users in CHANNEL.
2673 If CHANNEL is not specified, display the users in the current channel.
2674 This function clears the channel name list first, then sends the
2676 (let ((tgt (or (and (erc-channel-p channel) channel)
2677 (erc-default-target))))
2678 (if (and tgt (erc-channel-p tgt))
2680 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
2683 (erc-channel-begin-receiving-names))
2684 (erc-server-send (concat "NAMES " tgt)))
2685 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
2687 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
2689 (defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
2690 "Kick the user indicated in LINE from the current channel.
2691 LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
2692 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
2693 (if (string= "" reasonstring)
2694 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
2695 (if (erc-channel-p target)
2696 (let ((nick reason-or-nick))
2697 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
2698 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
2702 (let ((ch (erc-default-target)))
2703 (setq reasonstring (concat
2704 (if reason-or-nick (concat reason-or-nick " "))
2709 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
2711 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
2712 (erc-display-message nil 'error (current-buffer)
2713 'no-default-channel))
2716 (defvar erc-script-args nil)
2718 (defun erc-cmd-LOAD (line)
2719 "Load the script provided in the LINE.
2720 If LINE continues beyond the file name,
2721 the rest of it is put in a (local) variable
2722 `erc-script-args', which can be used in elisp scripts.
2724 The optional FORCE argument is ignored here - you can't force loading
2725 a script after exceeding the flood threshold."
2727 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
2728 (let* ((file-to-find (match-string 1 line))
2729 (erc-script-args (match-string 2 line))
2730 (file (erc-find-file file-to-find erc-script-path)))
2731 (erc-log (format "cmd: LOAD: %s" file-to-find))
2734 (erc-display-message nil 'error (current-buffer)
2735 'cannot-find-file ?f file-to-find))
2736 ((not (file-readable-p file))
2737 (erc-display-message nil 'error (current-buffer)
2738 'cannot-read-file ?f file))
2740 (message "Loading \'%s\'..." file)
2741 (erc-load-script file)
2742 (message "Loading \'%s\'...done" file))))
2746 (defun erc-cmd-WHOIS (user &optional server)
2747 "Display whois information for USER.
2749 If SERVER is non-nil, use that, rather than the current server."
2750 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
2751 (let ((send (if server
2752 (format "WHOIS %s %s" user server)
2753 (format "WHOIS %s" user))))
2754 (erc-log (format "cmd: %s" send))
2755 (erc-server-send send)
2757 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
2759 (defun erc-cmd-WHOAMI ()
2760 "Display whois information about yourself."
2761 (erc-cmd-WHOIS (erc-current-nick))
2764 (defun erc-cmd-IDLE (nick)
2765 "Show the length of time NICK has been idle."
2766 (let ((serverbuf (erc-server-buffer))
2767 (origbuf (current-buffer))
2769 (with-current-buffer serverbuf
2770 (add-to-list 'symlist
2771 (cons (erc-once-with-server-event
2774 (erc-response.command-args parsed))))
2775 'erc-server-311-functions))
2776 (add-to-list 'symlist
2777 (cons (erc-once-with-server-event
2780 (erc-response.command-args parsed))))
2781 'erc-server-312-functions))
2782 (add-to-list 'symlist
2783 (cons (erc-once-with-server-event
2786 (erc-response.command-args parsed))))
2787 'erc-server-318-functions))
2788 (add-to-list 'symlist
2789 (cons (erc-once-with-server-event
2792 (erc-response.command-args parsed))))
2793 'erc-server-319-functions))
2794 (add-to-list 'symlist
2795 (cons (erc-once-with-server-event
2798 (erc-response.command-args parsed))))
2799 'erc-server-320-functions))
2800 (add-to-list 'symlist
2801 (cons (erc-once-with-server-event
2804 (erc-response.command-args parsed))))
2805 'erc-server-330-functions))
2806 (add-to-list 'symlist
2807 (cons (erc-once-with-server-event
2812 (erc-response.command-args parsed)))))
2815 (format "%s has been idle for %s."
2816 (erc-string-no-properties ,nick)
2817 (erc-seconds-to-string idleseconds)))
2820 'erc-server-317-functions))
2822 ;; Send the WHOIS command.
2823 (erc-cmd-WHOIS nick)
2825 ;; Remove the uninterned symbols from the server hooks that did not run.
2826 (run-at-time 20 nil `(lambda ()
2827 (with-current-buffer ,(current-buffer)
2828 (dolist (sym ',symlist)
2829 (let ((hooksym (cdr sym))
2830 (funcsym (car sym)))
2831 (remove-hook hooksym funcsym t))))))))
2834 (defun erc-cmd-DESCRIBE (line)
2835 "Pose some action to a certain user.
2836 LINE has the format \"USER ACTION\"."
2839 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
2840 (let ((dst (match-string 1 line))
2841 (s (match-string 2 line)))
2842 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
2843 (erc-send-action dst s))
2846 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
2848 (defun erc-cmd-ME (line)
2849 "Send LINE as an action."
2851 ((string-match "^\\s-\\(.*\\)$" line)
2852 (let ((s (match-string 1 line)))
2853 (erc-log (format "cmd: ME: %s" s))
2854 (erc-send-action (erc-default-target) s))
2857 (put 'erc-cmd-ME 'do-not-parse-args t)
2859 (defun erc-cmd-LASTLOG (line)
2860 "Show all lines in the current buffer matching the regexp LINE.
2862 If a match spreads across multiple lines, all those lines are shown.
2864 The lines are shown in a buffer named `*Occur*'.
2865 It serves as a menu to find any of the occurrences in this buffer.
2866 \\[describe-mode] in that buffer will explain how.
2868 If LINE contains upper case characters (excluding those preceded by `\'),
2869 the matching is case-sensitive."
2872 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
2874 (defun erc-send-message (line &optional force)
2875 "Send LINE to the current channel or user and display it.
2877 See also `erc-message' and `erc-display-line'."
2878 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
2880 (concat (erc-format-my-nick) line)
2882 ;; FIXME - treat multiline, run hooks, or remove me?
2885 (defun erc-cmd-MODE (line)
2886 "Change or display the mode value of a channel or user.
2887 The first word specifies the target. The rest is the mode string
2890 If only one word is given, display the mode of that target.
2892 A list of valid mode strings for Freenode may be found at
2893 `http://freenode.net/using_the_network.shtml'."
2895 ((string-match "^\\s-\\(.*\\)$" line)
2896 (let ((s (match-string 1 line)))
2897 (erc-log (format "cmd: MODE: %s" s))
2898 (erc-server-send (concat "MODE " line)))
2901 (put 'erc-cmd-MODE 'do-not-parse-args t)
2903 (defun erc-cmd-NOTICE (channel-or-user &rest message)
2904 "Send a notice to the channel or user given as the first word.
2905 The rest is the message to send."
2906 (erc-message "NOTICE" (concat channel-or-user " "
2907 (mapconcat #'identity message " "))))
2909 (defun erc-cmd-MSG (line)
2910 "Send a message to the channel or user given as the first word in LINE.
2912 The rest of LINE is the message to send."
2913 (erc-message "PRIVMSG" line))
2915 (defalias 'erc-cmd-M 'erc-cmd-MSG)
2916 (put 'erc-cmd-MSG 'do-not-parse-args t)
2918 (defun erc-cmd-SQUERY (line)
2919 "Send a Service Query to the service given as the first word in LINE.
2921 The rest of LINE is the message to send."
2922 (erc-message "SQUERY" line))
2924 (defun erc-cmd-NICK (nick)
2925 "Change current nickname to NICK."
2926 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
2927 (let ((nicklen (cdr (assoc "NICKLEN" (with-current-buffer (erc-server-buffer)
2928 erc-server-parameters)))))
2929 (and nicklen (> (length nick) (string-to-number nicklen))
2930 (erc-display-message
2931 nil 'notice 'active 'nick-too-long
2932 ?i (length nick) ?l nicklen)))
2933 (erc-server-send (format "NICK %s" nick))
2935 (erc-set-current-nick nick)
2936 (erc-update-mode-line)
2937 (setq erc-bad-nick nil)))
2940 (defun erc-cmd-PART (line)
2941 "When LINE is an empty string, leave the current channel.
2942 Otherwise leave the channel indicated by LINE."
2944 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
2945 (let* ((ch (match-string 1 line))
2946 (msg (match-string 2 line))
2947 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
2948 (erc-log (format "cmd: PART: %s: %s" ch reason))
2949 (erc-server-send (if (string= reason "")
2950 (format "PART %s" ch)
2951 (format "PART %s :%s" ch reason))
2954 ((string-match "^\\s-*\\(.*\\)$" line)
2955 (let* ((ch (erc-default-target))
2956 (msg (match-string 1 line))
2957 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
2958 (if (and ch (erc-channel-p ch))
2960 (erc-log (format "cmd: PART: %s: %s" ch reason))
2961 (erc-server-send (if (string= reason "")
2962 (format "PART %s" ch)
2963 (format "PART %s :%s" ch reason))
2965 (erc-display-message nil 'error (current-buffer) 'no-target)))
2968 (put 'erc-cmd-PART 'do-not-parse-args t)
2970 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
2972 (defun erc-cmd-PING (recipient)
2974 (let ((time (format "%f" (erc-current-time))))
2975 (erc-log (format "cmd: PING: %s" time))
2976 (erc-cmd-CTCP recipient "PING" time)))
2978 (defun erc-cmd-QUOTE (line)
2979 "Send LINE directly to the server.
2980 All the text given as argument is sent to the sever as unmodified,
2981 just as you provided it. Use this command with care!"
2983 ((string-match "^\\s-\\(.+\\)$" line)
2984 (erc-server-send (match-string 1 line)))
2986 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
2988 (defun erc-cmd-QUERY (&optional user)
2989 "Open a query with USER.
2990 The type of query window/frame/etc will depend on the value of
2991 `erc-join-buffer'. If USER is omitted, close the current query buffer if one
2992 exists - except this is broken now ;-)"
2994 (list (read-from-minibuffer "Start a query with: " nil)))
2995 (let ((session-buffer (erc-server-buffer)))
2997 (erc-query user session-buffer)
2998 ;; currently broken, evil hack to display help anyway
2999 ;(erc-delete-query))))
3000 (signal 'wrong-number-of-arguments ""))))
3001 (defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3003 (defun erc-quit-reason-normal (&optional s)
3004 "Normal quit message.
3006 If S is non-nil, it will be used as the quit reason."
3008 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3009 erc-version-string) ; erc-official-location)
3012 (defun erc-quit-reason-zippy (&optional s)
3013 "Zippy quit message.
3015 If S is non-nil, it will be used as the quit reason."
3017 (erc-replace-regexp-in-string "\n" "" (yow))))
3019 (defun erc-quit-reason-various (s)
3020 "Choose a quit reason based on S (a string)."
3021 (when (featurep 'xemacs) (require 'poe))
3022 (let ((res (car (assoc-default (or s "")
3023 erc-quit-reason-various-alist 'string-match))))
3025 ((functionp res) (funcall res))
3027 ;; hopefully never reached
3030 (defun erc-part-reason-normal (&optional s)
3031 "Normal part message.
3033 If S is non-nil, it will be used as the quit reason."
3035 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3036 erc-version-string) ; erc-official-location)
3039 (defun erc-part-reason-zippy (&optional s)
3040 "Zippy part message.
3042 If S is non-nil, it will be used as the quit reason."
3044 (erc-replace-regexp-in-string "\n" "" (yow))))
3046 (defun erc-part-reason-various (s)
3047 "Choose a part reason based on S (a string)."
3048 (when (featurep 'xemacs) (require 'poe))
3049 (let ((res (car (assoc-default (or s "")
3050 erc-part-reason-various-alist 'string-match))))
3052 ((functionp res) (funcall res))
3056 (defun erc-cmd-QUIT (reason)
3057 "Disconnect from the current server.
3058 If REASON is omitted, display a default quit message, otherwise display
3059 the message given by REASON."
3063 ((string-match "^\\s-*\\(.*\\)$" reason)
3064 (let* ((s (match-string 1 reason))
3065 (buffer (erc-server-buffer))
3066 (reason (funcall erc-quit-reason (if (equal s "") nil s))))
3067 (with-current-buffer (if (and buffer
3071 (erc-log (format "cmd: QUIT: %s" reason))
3072 (setq erc-server-quitting t)
3073 (erc-set-active-buffer (erc-server-buffer))
3074 (erc-server-send (format "QUIT :%s" reason)))
3075 (run-hook-with-args 'erc-quit-hook erc-server-process)
3076 (when erc-kill-queries-on-quit
3077 (erc-kill-query-buffers erc-server-process)))
3081 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3082 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3083 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3084 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3086 (defun erc-cmd-GQUIT (reason)
3087 "Disconnect from all servers at once with the same quit REASON."
3088 (erc-with-all-buffers-of-server nil #'(lambda ()
3089 (and (erc-server-buffer-p)
3090 (erc-server-process-alive)))
3091 (erc-cmd-QUIT reason)))
3093 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3094 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3096 (defun erc-cmd-SERVER (server)
3097 "Connect to SERVER, leaving existing connection intact."
3098 (erc-log (format "cmd: SERVER: %s" server))
3100 (erc-select :server server :nick (erc-current-nick))
3102 (message "Cannot find host %s." server)
3107 (defvar motif-version-string)
3108 (defvar gtk-version-string))
3110 (defun erc-cmd-SV ()
3111 "Say the current ERC and Emacs version into channel."
3112 (erc-send-message (format "I'm using ERC %s with %s %s (%s%s%s)!"
3114 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3116 system-configuration
3118 (cond ((featurep 'motif)
3119 (concat ", " (substring
3120 motif-version-string 4)))
3122 (concat ", GTK+ Version "
3123 gtk-version-string))
3124 ((featurep 'mac-carbon) ", Mac Carbon")
3125 ((featurep 'x-toolkit) ", X toolkit")
3127 (if (and (boundp 'x-toolkit-scroll-bars)
3128 (memq x-toolkit-scroll-bars
3130 (format ", %s scroll bars"
3131 (capitalize (symbol-name
3132 x-toolkit-scroll-bars)))
3134 (if (featurep 'multi-tty) ", multi-tty" ""))
3135 (concat ", built " erc-emacs-build-time)))
3138 (defun erc-cmd-SM ()
3139 "Say the current ERC modes into channel."
3140 (erc-send-message (format "I'm using the following modules: %s!"
3144 (defun erc-cmd-SMV ()
3145 "Say the current ERC module versions into channel."
3146 (erc-send-message (format "I'm using the following module revisions: %s!"
3147 (erc-version-modules)))
3150 (defun erc-cmd-DEOP (&rest people)
3151 "Remove the operator setting from user(s) given in PEOPLE."
3152 (when (> (length people) 0)
3153 (erc-server-send (concat "MODE " (erc-default-target)
3155 (make-string (length people) ?o)
3157 (mapconcat 'identity people " ")))
3160 (defun erc-cmd-OP (&rest people)
3161 "Add the operator setting to users(s) given in PEOPLE."
3162 (when (> (length people) 0)
3163 (erc-server-send (concat "MODE " (erc-default-target)
3165 (make-string (length people) ?o)
3167 (mapconcat 'identity people " ")))
3170 (defun erc-cmd-TIME (&optional line)
3171 "Request the current time and date from the current server."
3173 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3174 (let ((args (match-string 1 line)))
3175 (erc-log (format "cmd: TIME: %s" args))
3176 (erc-server-send (concat "TIME " args)))
3178 (t (erc-server-send "TIME"))))
3179 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3181 (defun erc-cmd-TOPIC (topic)
3182 "Set or request the topic for a channel.
3183 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3184 or the empty string.
3186 If no #CHANNEL is given, the default channel is used. If TOPIC is
3187 given, the channel topic is modified, otherwise the current topic will
3190 ;; /topic #channel TOPIC
3191 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3192 (let ((ch (match-string 1 topic))
3193 (topic (match-string 2 topic)))
3194 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3195 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3198 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3199 (let ((ch (match-string 1 topic)))
3200 (erc-server-send (format "TOPIC %s" ch) nil ch)
3203 ((string-match "^\\s-*$" topic)
3204 (let ((ch (erc-default-target)))
3205 (erc-server-send (format "TOPIC %s" ch) nil ch)
3208 ((string-match "^\\s-*\\(.*\\)$" topic)
3209 (let ((ch (erc-default-target))
3210 (topic (match-string 1 topic)))
3211 (if (and ch (erc-channel-p ch))
3213 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3214 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3215 (erc-display-message nil 'error (current-buffer) 'no-target)))
3218 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3219 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3221 (defun erc-cmd-APPENDTOPIC (topic)
3222 "Append TOPIC to the current channel topic, separated by a space."
3223 (let ((oldtopic erc-channel-topic))
3224 ;; display help when given no arguments
3225 (when (string-match "^\\s-*$" topic)
3226 (signal 'wrong-number-of-arguments nil))
3227 ;; strip trailing ^O
3228 (when (string-match "\\(.*\\)\C-o" oldtopic)
3229 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3230 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3231 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3233 (defun erc-cmd-CLEARTOPIC (&optional channel)
3234 "Clear the topic for a CHANNEL.
3235 If CHANNEL is not specified, clear the topic for the default channel."
3236 (interactive "sClear topic of channel (RET is current channel): ")
3237 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3239 (erc-server-send (format "TOPIC %s :" chnl))
3244 (defvar erc-channel-banlist nil
3245 "A list of bans seen for the current channel.
3247 Each ban is an alist of the form:
3250 The property `received-from-server' indicates whether
3251 or not the ban list has been requested from the server.")
3252 (make-variable-buffer-local 'erc-channel-banlist)
3253 (put 'erc-channel-banlist 'received-from-server nil)
3255 (defun erc-cmd-BANLIST ()
3256 "Pretty-print the contents of `erc-channel-banlist'.
3258 The ban list is fetched from the server if necessary."
3259 (let ((chnl (erc-default-target))
3260 (chnl-name (buffer-name)))
3263 ((not (erc-channel-p chnl))
3264 (erc-display-line (erc-make-notice "You're not on a channel\n")
3267 ((not (get 'erc-channel-banlist 'received-from-server))
3268 (let ((old-367-hook erc-server-367-functions))
3269 (setq erc-server-367-functions 'erc-banlist-store
3270 erc-channel-banlist nil)
3271 ;; fetch the ban list then callback
3272 (with-current-buffer (erc-server-buffer)
3273 (erc-once-with-server-event
3275 `(with-current-buffer ,chnl-name
3276 (put 'erc-channel-banlist 'received-from-server t)
3277 (setq erc-server-367-functions ',old-367-hook)
3280 (erc-server-send (format "MODE %s b" chnl)))))
3282 ((null erc-channel-banlist)
3283 (erc-display-line (erc-make-notice
3284 (format "No bans for channel: %s\n" chnl))
3286 (put 'erc-channel-banlist 'received-from-server nil))
3289 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3291 (and (boundp 'fill-column)
3293 (1- (window-width))))
3294 (separator (make-string erc-fill-column (string-to-char "=")))
3296 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3297 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3300 (erc-make-notice (format "Ban list for channel: %s\n"
3301 (erc-default-target)))
3304 (erc-display-line separator 'active)
3305 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3306 (erc-display-line separator 'active)
3312 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3314 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3317 erc-channel-banlist)
3319 (erc-display-line (erc-make-notice "End of Ban list")
3321 (put 'erc-channel-banlist 'received-from-server nil)))))
3324 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3326 (defun erc-cmd-MASSUNBAN ()
3329 Unban all currently banned users in the current channel."
3330 (let ((chnl (erc-default-target)))
3333 ((not (erc-channel-p chnl))
3335 (erc-make-notice "You're not on a channel\n")
3338 ((not (get 'erc-channel-banlist 'received-from-server))
3339 (let ((old-367-hook erc-server-367-functions))
3340 (setq erc-server-367-functions 'erc-banlist-store)
3341 ;; fetch the ban list then callback
3342 (with-current-buffer (erc-server-buffer)
3343 (erc-once-with-server-event
3345 `(with-current-buffer ,chnl
3346 (put 'erc-channel-banlist 'received-from-server t)
3347 (setq erc-server-367-functions ,old-367-hook)
3350 (erc-server-send (format "MODE %s b" chnl)))))
3352 (t (let ((bans (mapcar 'cdr erc-channel-banlist)))
3354 ;; Glob the bans into groups of three, and carry out the unban.
3355 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3359 (format "MODE %s -%s %s" (erc-default-target)
3360 (make-string (length x) (string-to-char "b"))
3361 (mapconcat 'identity x " "))))
3362 (erc-group-list bans 3))))
3365 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3367 ;;;; End of IRC commands
3369 (defun erc-ensure-channel-name (channel)
3370 "Return CHANNEL if it is a valid channel name.
3371 Eventually add a # in front of it, if that turns it into a valid channel name."
3372 (if (erc-channel-p channel)
3374 (concat "#" channel)))
3376 (defun erc-grab-region (start end)
3377 "Copy the region between START and END in a recreatable format.
3379 Converts all the IRC text properties in each line of the region
3380 into control codes and writes them to a separate buffer. The
3381 resulting text may be used directly as a script to generate this
3384 (erc-set-active-buffer (current-buffer))
3386 (let* ((cb (current-buffer))
3387 (buf (generate-new-buffer erc-grab-buffer-name))
3388 (region (buffer-substring start end))
3389 (lines (erc-split-multiline-safe region)))
3391 (dolist (line lines)
3392 (insert (concat line "\n")))
3394 (switch-to-buffer-other-window buf)))
3395 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3398 (defun erc-display-prompt (&optional buffer pos prompt face)
3399 "Display PROMPT in BUFFER at position POS.
3400 Display an ERC prompt in BUFFER.
3402 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3403 If BUFFER is nil, the `current-buffer' is used.
3404 If POS is nil, PROMPT will be displayed at `point'.
3405 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3406 `erc-prompt-face' will be used."
3407 (let* ((prompt (or prompt (erc-prompt)))
3409 (ob (current-buffer)))
3410 ;; We cannot use save-excursion because we move point, therefore
3411 ;; we resort to the ol' ob trick to restore this.
3412 (when (and buffer (bufferp buffer))
3413 (set-buffer buffer))
3415 ;; now save excursion again to store where point and mark are
3416 ;; in the current buffer
3418 (setq pos (or pos (point)))
3421 ;; Do not extend the text properties when typing at the end
3422 ;; of the prompt, but stuff typed in front of the prompt
3423 ;; shall remain part of the prompt.
3424 (setq prompt (erc-propertize prompt
3425 'start-open t ; XEmacs
3426 'rear-nonsticky t ; Emacs
3430 (erc-put-text-property 0 (1- (length prompt))
3431 'face (or face 'erc-prompt-face)
3434 ;; Set the input marker
3435 (set-marker erc-input-marker (point)))
3437 ;; Now we are back at the old position. If the prompt was
3438 ;; inserted here or before us, advance point by the length of
3440 (when (or (not pos) (<= (point) pos))
3442 ;; Clear the undo buffer now, so the user can undo his stuff,
3443 ;; but not the stuff we did. Sneaky!
3444 (setq buffer-undo-list nil)
3447 ;; interactive operations
3449 (defun erc-input-message ()
3450 "Read input from the minibuffer."
3452 (let ((minibuffer-allow-text-properties t)
3453 (read-map minibuffer-local-map))
3454 (insert (read-from-minibuffer "Message: "
3455 (string last-command-char) read-map))
3456 (erc-send-current-line)))
3458 (defvar erc-action-history-list ()
3459 "History list for interactive action input.")
3461 (defun erc-input-action ()
3462 "Interactively input a user action and send it to IRC."
3464 (erc-set-active-buffer (current-buffer))
3465 (let ((action (read-from-minibuffer
3466 "Action: " nil nil nil 'erc-action-history-list)))
3467 (if (not (string-match "^\\s-*$" action))
3468 (erc-send-action (erc-default-target) action))))
3470 (defun erc-join-channel (channel &optional key)
3473 If `point' is at the beginning of a channel name, use that as default."
3476 (let ((chnl (if (looking-at "\\([&#+!][^ ]+\\)") (match-string 1) ""))
3477 (table (when (erc-server-buffer-live-p)
3478 (set-buffer (process-buffer erc-server-process))
3480 (completing-read "Join channel: " table nil nil nil nil chnl))
3481 (when erc-prompt-for-channel-key
3482 (read-from-minibuffer "Channel key (RET for none): " nil))))
3483 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3485 (defun erc-part-from-channel (reason)
3486 "Part from the current channel and prompt for a REASON."
3489 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3491 (read-from-minibuffer (concat "Leave " (erc-default-target)
3493 (cons "No reason" 0)))))
3494 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3496 (defun erc-set-topic (topic)
3497 "Prompt for a TOPIC for the current channel."
3500 (read-from-minibuffer
3501 (concat "Set topic of " (erc-default-target) ": ")
3502 (when erc-channel-topic
3503 (cons (apply 'concat (butlast (split-string erc-channel-topic "\C-o")))
3505 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3506 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3508 (defun erc-set-channel-limit (&optional limit)
3509 "Set a LIMIT for the current channel. Remove limit if nil.
3510 Prompt for one if called interactively."
3511 (interactive (list (read-from-minibuffer
3512 (format "Limit for %s (RET to remove limit): "
3513 (erc-default-target)))))
3514 (let ((tgt (erc-default-target)))
3515 (if (and limit (>= (length limit) 1))
3516 (erc-server-send (format "MODE %s +l %s" tgt limit))
3517 (erc-server-send (format "MODE %s -l" tgt)))))
3519 (defun erc-set-channel-key (&optional key)
3520 "Set a KEY for the current channel. Remove key if nil.
3521 Prompt for one if called interactively."
3522 (interactive (list (read-from-minibuffer
3523 (format "Key for %s (RET to remove key): "
3524 (erc-default-target)))))
3525 (let ((tgt (erc-default-target)))
3526 (if (and key (>= (length key) 1))
3527 (erc-server-send (format "MODE %s +k %s" tgt key))
3528 (erc-server-send (format "MODE %s -k" tgt)))))
3530 (defun erc-quit-server (reason)
3531 "Disconnect from current server after prompting for REASON.
3532 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
3533 (interactive (list (read-from-minibuffer
3534 (format "Reason for quitting %s: "
3535 (or erc-server-announced-name
3536 erc-session-server)))))
3537 (erc-cmd-QUIT reason))
3539 ;; Movement of point
3542 "Move `point' to the beginning of the current line.
3544 This places `point' just after the prompt, or at the beginning of the line."
3547 (when (get-text-property (point) 'erc-prompt)
3548 (goto-char erc-input-marker))
3551 (defun erc-kill-input ()
3552 "Kill current input line using `erc-bol' followed by `kill-line'."
3554 (when (and (erc-bol)
3555 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
3556 ;; there's nothing to kill
3557 (if (boundp 'erc-input-ring-index)
3558 (setq erc-input-ring-index nil))
3561 (defun erc-complete-word ()
3562 "Complete the word before point.
3564 This function uses `erc-complete-functions'."
3566 (unless (run-hook-with-args-until-success 'erc-complete-functions)
3569 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3571 ;; IRC SERVER INPUT HANDLING
3573 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3575 ;;;; New Input parsing
3577 ; Stolen from ZenIRC. I just wanna test this code, so here is
3580 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
3581 erc-default-server-handler)
3582 "*Default for server messages which aren't covered by `erc-server-hooks'."
3583 :group 'erc-server-hooks
3586 (defun erc-default-server-handler (proc parsed)
3587 "Default server handler.
3589 Displays PROC and PARSED appropriately using `erc-display-message'."
3590 (erc-display-message
3597 (setq res (append res (list x)))))
3602 (defvar erc-server-vectors
3603 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
3604 "List of received server messages which ERC does not specifically handle.
3605 See `erc-debug-missing-hooks'.")
3606 ;(make-variable-buffer-local 'erc-server-vectors)
3608 (defun erc-debug-missing-hooks (proc parsed)
3609 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
3610 These vectors can be helpful when adding new server message handlers to ERC.
3611 See `erc-default-server-hook'."
3612 (nconc erc-server-vectors (list parsed))
3615 (defun erc-query (target server)
3616 "Open a query buffer on TARGET, using SERVER.
3617 To change how this query window is displayed, use `let' to bind
3618 `erc-join-buffer' before calling this."
3620 (buffer-live-p server)
3621 (set-buffer server))
3622 (error "Couldn't switch to server buffer"))
3623 (let ((buf (erc erc-session-server
3626 erc-session-user-full-name
3631 erc-server-process)))
3633 (error "Couldn't open query window"))
3634 (erc-update-mode-line)
3637 (defcustom erc-auto-query nil
3638 "If non-nil, create a query buffer each time you receive a private message.
3640 If the buffer doesn't already exist it is created. This can be
3641 set to a symbol, to control how the new query window should
3642 appear. See the documentation for `erc-join-buffer' for
3645 :type '(choice (const nil)
3648 (const window-noselect)
3652 (defcustom erc-query-on-unjoined-chan-privmsg t
3653 "If non-nil create query buffer on receiving any PRIVMSG at all.
3654 This includes PRIVMSGs directed to channels. If you are using an IRC
3655 bouncer, such as dircproxy, to keep a log of channels when you are
3656 disconnected, you should set this option to t."
3660 (defcustom erc-format-query-as-channel-p t
3661 "If non-nil, format text from others in a query buffer like in a channel,
3662 otherwise format like a private message."
3666 (defcustom erc-minibuffer-notice nil
3667 "If non-nil, print ERC notices for the user in the minibuffer.
3668 Only happens when the session buffer isn't visible."
3672 (defcustom erc-minibuffer-ignored nil
3673 "If non-nil, print a message in the minibuffer if we ignored something."
3677 (defun erc-wash-quit-reason (reason nick login host)
3678 "Remove duplicate text from quit REASON.
3679 Specifically in relation to NICK (user@host) information. Returns REASON
3680 unmodified if nothing can be removed.
3681 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
3682 \"Read error: 110\". The same applies for \"Ping Timeout\"."
3683 (setq nick (regexp-quote nick)
3684 login (regexp-quote login)
3685 host (regexp-quote host))
3686 (or (when (string-match (concat "^\\(Read error\\) to "
3687 nick "\\[" host "\\]: "
3688 "\\(.+\\)$") reason)
3689 (concat (match-string 1 reason) ": " (match-string 2 reason)))
3690 (when (string-match (concat "^\\(Ping timeout\\) for "
3691 nick "\\[" host "\\]$") reason)
3692 (match-string 1 reason))
3695 (defun erc-nickname-in-use (nick reason)
3696 "If NICK is unavailable, tell the user the REASON.
3698 See also `erc-display-error-notice'."
3699 (if (or erc-manual-set-nick-on-bad-nick-p
3700 ;; how many default-nicks are left + one more try...
3701 (eq erc-nick-change-attempt-count
3702 (if (consp erc-nick)
3703 (+ (length erc-nick) 1)
3705 (erc-display-error-notice
3707 (format "Nickname %s is %s, try another." nick reason))
3708 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
3709 (let ((newnick (nth 1 erc-default-nicks))
3710 (nicklen (cdr (assoc "NICKLEN"
3711 (with-current-buffer (erc-server-buffer)
3712 erc-server-parameters)))))
3713 (setq erc-bad-nick t)
3714 ;; try to use a different nick
3715 (if erc-default-nicks
3716 (setq erc-default-nicks (cdr erc-default-nicks)))
3718 (setq newnick (concat (truncate-string-to-width
3720 (if (and erc-server-connected nicklen)
3721 (- (string-to-number nicklen) 1)
3722 ;; rfc2812 max nick length = 9
3723 ;; we must assume this is the
3724 ;; server's setting if we haven't
3725 ;; established a connection yet
3727 erc-nick-uniquifier)))
3728 (erc-cmd-NICK newnick)
3729 (erc-display-error-notice
3731 (format "Nickname %s is %s, trying %s"
3732 nick reason newnick)))))
3736 (defgroup erc-server-hooks nil
3737 "Server event callbacks.
3738 Every server event - like numeric replies - has it's own hook.
3739 Those hooks are all called using `run-hook-with-args-until-success'.
3740 They receive as first argument the process object from where the event
3742 and as second argument the event parsed as a vector."
3745 (defun erc-display-server-message (proc parsed)
3746 "Display the message sent by the server as a notice."
3747 (erc-display-message
3748 parsed 'notice 'active (erc-response.contents parsed)))
3750 (defun erc-auto-query (proc parsed)
3751 ;; FIXME: This needs more documentation, unless it's not a user function --
3752 ;; Lawrence 2004-01-08
3753 "Put this on `erc-server-PRIVMSG-functions'."
3754 (when erc-auto-query
3755 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
3756 (target (car (erc-response.command-args parsed)))
3757 (msg (erc-response.contents parsed))
3758 (query (if (not erc-query-on-unjoined-chan-privmsg)
3760 (if (erc-current-nick-p target)
3763 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
3764 (or erc-query-on-unjoined-chan-privmsg
3765 (string= target (erc-current-nick)))
3766 (not (erc-get-buffer query proc))
3767 (not (erc-is-message-ctcp-and-not-action-p msg))
3768 (let ((erc-join-buffer erc-auto-query))
3769 (erc-cmd-QUERY query))
3772 (defun erc-is-message-ctcp-p (message)
3773 "Check if MESSAGE is a CTCP message or not."
3774 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
3776 (defun erc-is-message-ctcp-and-not-action-p (message)
3777 "Check if MESSAGE is a CTCP message or not."
3778 (and (erc-is-message-ctcp-p message)
3779 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
3781 (defun erc-format-privmessage (nick msg privp msgp)
3782 "Format a PRIVMSG in an insertible fashion."
3783 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
3784 (mark-e (if msgp (if privp "*" ">") "-"))
3785 (str (format "%s%s%s %s" mark-s nick mark-e msg))
3786 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
3787 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
3788 ;; add text properties to text before the nick, the nick and after the nick
3789 (erc-put-text-property 0 (length mark-s) 'face msg-face str)
3790 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
3791 'face nick-face str)
3792 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
3796 (defcustom erc-format-nick-function 'erc-format-nick
3797 "*Function to format a nickname for message display."
3801 (defun erc-format-nick (&optional user channel-data)
3802 "Standard nickname formatting function. Only returns the value
3805 (erc-server-user-nickname user)))
3807 (defun erc-format-@nick (&optional user channel-data)
3808 "Format a nickname such that @ or + are prefix for the NICK
3809 if OP or VOICE are t respectively."
3813 (setq op (erc-channel-user-op channel-data)
3814 voice (erc-channel-user-voice channel-data)))
3815 (concat (if voice "+" "")
3817 (erc-server-user-nickname user)))))
3819 (defun erc-format-my-nick ()
3820 "Return the beginning of this user's message, correctly propertized"
3821 (if erc-show-my-nick
3824 (nick (erc-current-nick)))
3826 (erc-propertize open 'face 'erc-default-face)
3827 (erc-propertize nick 'face 'erc-nick-default-face)
3828 (erc-propertize close 'face 'erc-default-face)))
3829 (let ((prefix "> "))
3830 (erc-propertize prefix 'face 'erc-default-face))))
3832 (defun erc-echo-notice-in-default-buffer (s parsed buffer sender)
3833 "Echos a private notice in the default buffer, namely the
3834 target buffer specified by BUFFER, or there is no target buffer,
3835 the server buffer. This function is designed to be added to
3836 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
3837 and always returns t."
3838 (erc-display-message parsed nil buffer s)
3841 (defun erc-echo-notice-in-target-buffer (s parsed buffer sender)
3842 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
3843 function is designed to be added to either `erc-echo-notice-hook'
3844 or `erc-echo-notice-always-hook', and returns non-nil iff BUFFER
3847 (progn (erc-display-message parsed nil buffer s) t)
3850 (defun erc-echo-notice-in-minibuffer (s parsed buffer sender)
3851 "Echos a private notice in the minibuffer. This function is
3852 designed to be added to either `erc-echo-notice-hook' or
3853 `erc-echo-notice-always-hook', and always returns t."
3854 (message "%s" (concat "NOTICE: " s))
3857 (defun erc-echo-notice-in-server-buffer (s parsed buffer sender)
3858 "Echos a private notice in the server buffer. This function is
3859 designed to be added to either `erc-echo-notice-hook' or
3860 `erc-echo-notice-always-hook', and always returns t."
3861 (erc-display-message parsed nil nil s)
3864 (defun erc-echo-notice-in-active-non-server-buffer (s parsed buffer sender)
3865 "Echos a private notice in the active buffer if the active
3866 buffer is not the server buffer. This function is designed to be
3867 added to either `erc-echo-notice-hook' or
3868 `erc-echo-notice-always-hook', and returns non-nil iff the active
3869 buffer is not the server buffer."
3870 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
3871 (progn (erc-display-message parsed nil 'active s) t)
3874 (defun erc-echo-notice-in-active-buffer (s parsed buffer sender)
3875 "Echos a private notice in the active buffer. This function is
3876 designed to be added to either `erc-echo-notice-hook' or
3877 `erc-echo-notice-always-hook', and always returns t."
3878 (erc-display-message parsed nil 'active s)
3881 (defun erc-echo-notice-in-user-buffers (s parsed buffer sender)
3882 "Echos a private notice in all of the buffers for which SENDER
3883 is a member. This function is designed to be added to either
3884 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
3885 returns non-nil iff there is at least one buffer for which the
3888 See also: `erc-echo-notice-in-first-user-buffer',
3889 `erc-buffer-list-with-nick'"
3890 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3892 (progn (erc-display-message parsed nil buffers s) t)
3895 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
3896 "Echos a private notice in BUFFER and in all of the buffers for
3897 which SENDER is a member. This function is designed to be added
3898 to either `erc-echo-notice-hook' or
3899 `erc-echo-notice-always-hook', and returns non-nil iff there is
3900 at least one buffer for which the sender is a member or the
3903 See also: `erc-echo-notice-in-user-buffers',
3904 `erc-buffer-list-with-nick'"
3905 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3906 (add-to-list 'buffers buffer)
3908 (progn (erc-display-message parsed nil buffers s) t)
3911 (defun erc-echo-notice-in-first-user-buffer (s parsed buffer sender)
3912 "Echos a private notice in one of the buffers for which SENDER
3913 is a member. This function is designed to be added to either
3914 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
3915 returns non-nil iff there is at least one buffer for which the
3918 See also: `erc-echo-notice-in-user-buffers',
3919 `erc-buffer-list-with-nick'"
3920 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3922 (progn (erc-display-message parsed nil (car buffers) s) t)
3925 ;;; Ban manipulation
3927 (defun erc-banlist-store (proc parsed)
3928 "Record ban entries for a channel."
3929 (multiple-value-bind (channel mask whoset)
3930 (cdr (erc-response.command-args parsed))
3931 ;; Determine to which buffer the message corresponds
3932 (let ((buffer (erc-get-buffer channel proc)))
3933 (with-current-buffer buffer
3934 (unless (member (cons whoset mask) erc-channel-banlist)
3935 (setq erc-channel-banlist (cons (cons whoset mask)
3936 erc-channel-banlist))))))
3939 (defun erc-banlist-finished (proc parsed)
3940 "Record that we have received the banlist."
3941 (let* ((channel (second (erc-response.command-args parsed)))
3942 (buffer (erc-get-buffer channel proc)))
3943 (with-current-buffer buffer
3944 (put 'erc-channel-banlist 'received-from-server t)))
3945 t) ; suppress the 'end of banlist' message
3947 (defun erc-banlist-update (proc parsed)
3948 "Check MODE commands for bans and update the banlist appropriately."
3949 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
3950 (let* ((tgt (first (erc-response.command-args parsed)))
3951 (mode (erc-response.contents parsed))
3952 (whoset (erc-response.sender parsed))
3953 (buffer (erc-get-buffer tgt proc)))
3955 (with-current-buffer buffer
3956 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
3957 ((string-match "^\\([+-]\\)b" mode)
3960 ((string-match "^-" mode)
3961 ;; Remove the unbanned masks from the ban list
3962 (setq erc-channel-banlist
3965 (member (upcase (cdr y))
3967 (cdr (split-string mode)))))
3968 erc-channel-banlist)))
3969 ((string-match "^+" mode)
3970 ;; Add the banned mask(s) to the ban list
3973 (unless (member (cons whoset mask) erc-channel-banlist)
3974 (setq erc-channel-banlist
3975 (cons (cons whoset mask) erc-channel-banlist))))
3976 (cdr (split-string mode))))))))))
3979 ;; used for the banlist cmds
3980 (defun erc-group-list (list n)
3981 "Group LIST into sublists of length N."
3982 (cond ((null list) nil)
3983 ((null (nthcdr n list)) (list list))
3984 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
3989 (defun erc-handle-login ()
3990 "Handle the logging in process of connection."
3991 (unless erc-logged-in
3992 (setq erc-logged-in t)
3993 (message "Logging in as \'%s\'... done" (erc-current-nick))
3994 ;; execute a startup script
3995 (let ((f (erc-select-startup-file)))
3997 (erc-load-script f)))))
3999 (defun erc-connection-established (proc parsed)
4000 "Run just after connection.
4002 Set user modes and run `erc-after-connect hook'."
4003 (unless erc-server-connected ; only once per session
4004 (let ((server (or erc-server-announced-name (erc-response.sender parsed)))
4005 (nick (car (erc-response.command-args parsed ))))
4006 (setq erc-server-connected t)
4007 (erc-update-mode-line)
4008 (erc-set-initial-user-mode nick)
4009 (erc-server-setup-periodical-server-ping)
4010 (run-hook-with-args 'erc-after-connect server nick))))
4012 (defun erc-set-initial-user-mode (nick)
4013 "If `erc-user-mode' is non-nil for NICK, set the user modes."
4015 (let ((mode (if (functionp erc-user-mode)
4016 (funcall erc-user-mode)
4018 (when (stringp mode)
4019 (erc-log (format "changing mode for %s to %s" nick mode))
4020 (erc-server-send (format "MODE %s %s" nick mode))))))
4022 (defun erc-display-error-notice (parsed string)
4023 "Display STRING as an error notice.
4025 See also `erc-display-message'."
4026 (erc-display-message
4027 parsed '(notice error) 'active string))
4029 (defun erc-process-ctcp-query (proc parsed nick login host)
4030 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4031 "Process a CTCP query."
4032 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4034 (if (> (length queries) 4)
4035 (erc-display-message
4036 parsed (list 'notice 'error) proc 'ctcp-too-many)
4037 (if (= 0 (length queries))
4038 (erc-display-message
4039 parsed (list 'notice 'error) proc
4040 'ctcp-empty ?n nick)
4042 (let* ((type (upcase (car (split-string (car queries)))))
4043 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4044 (if (and hook (boundp hook))
4045 (if (string-equal type "ACTION")
4046 (run-hook-with-args-until-success
4047 hook proc parsed nick login host
4048 (car (erc-response.command-args parsed))
4051 (if (erc-current-nick-p
4052 (car (erc-response.command-args parsed)))
4053 (erc-display-message
4054 parsed 'error 'active 'ctcp-request
4055 ?n nick ?u login ?h host ?r (car queries))
4056 (erc-display-message
4057 parsed 'error 'active 'ctcp-request-to
4058 ?n nick ?u login ?h host ?r (car queries)
4059 ?t (car (erc-response.command-args parsed)))))
4060 (run-hook-with-args-until-success
4061 hook proc nick login host
4062 (car (erc-response.command-args parsed))
4064 (erc-display-message
4065 parsed (list 'notice 'error) proc
4067 (setq queries (cdr queries)))))))
4069 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4071 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4072 "Respond to a CTCP ACTION query."
4073 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4074 (let ((s (match-string 1 msg))
4075 (buf (or (erc-get-buffer to proc)
4076 (erc-get-buffer nick proc)
4077 (process-buffer proc))))
4078 (erc-display-message
4080 'ACTION ?n nick ?u login ?h host ?a s))))
4082 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4084 (defun erc-ctcp-query-CLIENTINFO (proc nick login host to msg)
4085 "Respond to a CTCP CLIENTINFO query."
4086 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4087 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4088 (unless erc-disable-ctcp-replies
4089 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4092 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4093 (defun erc-ctcp-query-ECHO (proc nick login host to msg)
4094 "Respond to a CTCP ECHO query."
4095 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4096 (let ((s (match-string 1 msg)))
4097 (unless erc-disable-ctcp-replies
4098 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4101 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4102 (defun erc-ctcp-query-FINGER (proc nick login host to msg)
4103 "Respond to a CTCP FINGER query."
4104 (unless erc-disable-ctcp-replies
4105 (let ((s (if erc-anonymous-login
4106 (format "FINGER I'm %s." (erc-current-nick))
4107 (format "FINGER %s (%s@%s)."
4111 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4113 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4114 (erc-send-ctcp-notice nick s)))
4117 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4118 (defun erc-ctcp-query-PING (proc nick login host to msg)
4119 "Respond to a CTCP PING query."
4120 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4121 (unless erc-disable-ctcp-replies
4122 (let ((arg (match-string 1 msg)))
4123 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4126 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4127 (defun erc-ctcp-query-TIME (proc nick login host to msg)
4128 "Respond to a CTCP TIME query."
4129 (unless erc-disable-ctcp-replies
4130 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4133 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4134 (defun erc-ctcp-query-USERINFO (proc nick login host to msg)
4135 "Respond to a CTCP USERINFO query."
4136 (unless erc-disable-ctcp-replies
4137 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4140 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4141 (defun erc-ctcp-query-VERSION (proc nick login host to msg)
4142 "Respond to a CTCP VERSION query."
4143 (unless erc-disable-ctcp-replies
4144 (erc-send-ctcp-notice
4146 "VERSION \C-bERC\C-b %s - an IRC client for emacs (\C-b%s\C-b)"
4148 erc-official-location)))
4151 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4152 "Process MSG as a CTCP reply."
4153 (let* ((type (car (split-string msg)))
4154 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4156 (run-hook-with-args-until-success
4157 hook proc nick login host
4158 (car (erc-response.command-args parsed)) msg)
4159 (erc-display-message
4160 parsed 'notice 'active
4161 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4163 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4164 (defun erc-ctcp-reply-ECHO (proc nick login host to msg)
4165 "Handle a CTCP ECHO reply."
4166 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4167 (let ((message (match-string 1 msg)))
4168 (erc-display-message
4169 nil '(notice action) 'active
4170 'CTCP-ECHO ?n nick ?m message)))
4173 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4174 (defun erc-ctcp-reply-CLIENTINFO (proc nick login host to msg)
4175 "Handle a CTCP CLIENTINFO reply."
4176 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4177 (let ((message (match-string 1 msg)))
4178 (erc-display-message
4180 'CTCP-CLIENTINFO ?n nick ?m message)))
4183 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4184 (defun erc-ctcp-reply-FINGER (proc nick login host to msg)
4185 "Handle a CTCP FINGER reply."
4186 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4187 (let ((message (match-string 1 msg)))
4188 (erc-display-message
4190 'CTCP-FINGER ?n nick ?m message)))
4193 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4194 (defun erc-ctcp-reply-PING (proc nick login host to msg)
4195 "Handle a CTCP PING reply."
4196 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4198 (let ((time (match-string 1 msg)))
4200 (let ((delta (erc-time-diff (string-to-number time)
4201 (erc-current-time))))
4202 (erc-display-message
4205 ?t (erc-sec-to-time delta)))
4207 (erc-display-message
4209 'bad-ping-response ?n nick ?t time))))))
4211 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4212 (defun erc-ctcp-reply-TIME (proc nick login host to msg)
4213 "Handle a CTCP TIME reply."
4214 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4215 (let ((message (match-string 1 msg)))
4216 (erc-display-message
4218 'CTCP-TIME ?n nick ?m message)))
4221 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4222 (defun erc-ctcp-reply-VERSION (proc nick login host to msg)
4223 "Handle a CTCP VERSION reply."
4224 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4225 (let ((message (match-string 1 msg)))
4226 (erc-display-message
4228 'CTCP-VERSION ?n nick ?m message)))
4231 (defun erc-process-away (proc away-p)
4232 ;; FIXME: This docstring is AWFUL -- Lawrence 2004-01-08
4233 "Process the user being away, or returning from an away break."
4234 (let ((sessionbuf (process-buffer proc)))
4236 (with-current-buffer sessionbuf
4237 (when erc-away-nickname
4238 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4239 erc-away-nickname away-p))
4240 (erc-cmd-NICK (if away-p
4245 (erc-with-all-buffers-of-server proc nil
4246 (setq erc-away (current-time))))
4248 (let ((away-time erc-away))
4249 ;; away must be set to NIL BEFORE sending anything to prevent
4250 ;; an infinite recursion
4251 (erc-with-all-buffers-of-server proc nil
4252 (setq erc-away nil))
4254 (set-buffer (erc-active-buffer))
4255 (when erc-public-away-p
4257 (erc-default-target)
4259 (format "is back (gone for %s)"
4262 (erc-emacs-time-to-erc-time away-time)
4263 (erc-current-time))))
4265 (erc-update-mode-line)))
4267 ;;;; List of channel members handling
4269 (defun erc-channel-begin-receiving-names ()
4272 Used when a channel names list is about to be received. Should
4273 be called with the current buffer set to the channel buffer.
4275 See also `erc-channel-end-receiving-names'."
4276 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4278 (defun erc-channel-end-receiving-names ()
4281 Used to fix `erc-channel-users' after a channel names list has been
4282 received. Should be called with the current buffer set to the
4285 See also `erc-channel-begin-receiving-names'."
4286 (maphash (lambda (nick user)
4287 (if (null (gethash nick erc-channel-new-member-names))
4288 (erc-remove-channel-user nick)))
4290 (setq erc-channel-new-member-names nil))
4292 (defun erc-channel-receive-names (names-string)
4293 "This function is for internal use only.
4295 Update `erc-channel-users' according to NAMES-STRING.
4296 NAMES-STRING is a string listing some of the names on the
4298 (let (names name op voice)
4299 ;; We need to delete "" because in XEmacs, (split-string "a ")
4300 ;; returns ("a" "").
4301 (setq names (delete "" (split-string names-string)))
4302 (let ((erc-channel-members-changed-hook nil))
4303 (dolist (item names)
4304 (cond ((string-match "^@\\(.*\\)$" item)
4305 (setq name (match-string 1 item)
4308 ((string-match "^+\\(.*\\)$" item)
4309 (setq name (match-string 1 item)
4315 (puthash (erc-downcase name) t
4316 erc-channel-new-member-names)
4317 (erc-update-current-channel-member
4318 name name t op voice)))
4319 (run-hooks 'erc-channel-members-changed-hook)))
4321 (defcustom erc-channel-members-changed-hook nil
4322 "*This hook is called every time the variable `channel-members' changes.
4323 The buffer where the change happened is current while this hook is called."
4327 (defun erc-update-user-nick (nick &optional new-nick
4328 host login full-name info)
4329 "Updates the stored user information for the user with nickname
4332 See also: `erc-update-user'"
4333 (erc-update-user (erc-get-server-user nick) new-nick
4334 host login full-name info))
4336 (defun erc-update-user (user &optional new-nick
4337 host login full-name info)
4338 "Update user info for USER. USER must be an erc-server-user
4339 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4340 non-nil and not equal to the existing values for USER are used to
4341 replace the stored values in USER.
4343 If, any only if a change is made,
4344 `erc-channel-members-changed-hook' is run for each channel for
4345 which USER is a member, and `t' is returned."
4349 (not (equal (erc-server-user-nickname user)
4352 (erc-change-user-nickname user new-nick))
4354 (not (equal (erc-server-user-host user) host)))
4356 (setf (erc-server-user-host user) host))
4358 (not (equal (erc-server-user-login user) login)))
4360 (setf (erc-server-user-login user) login))
4361 (when (and full-name
4362 (not (equal (erc-server-user-full-name user)
4365 (setf (erc-server-user-full-name user) full-name))
4367 (not (equal (erc-server-user-info user) info)))
4369 (setf (erc-server-user-info user) info))
4371 (dolist (buf (erc-server-user-buffers user))
4372 (if (buffer-live-p buf)
4373 (with-current-buffer buf
4374 (run-hooks 'erc-channel-members-changed-hook))))))
4377 (defun erc-update-current-channel-member
4378 (nick new-nick &optional add op voice host login full-name info
4379 update-message-time)
4380 "Updates the stored user information for the user with nickname
4381 NICK. `erc-update-user' is called to handle changes to nickname,
4382 host, login, full-name, and info. If `op' or `voice' are
4383 non-nil, they must be equal to either `on' or `off', in which
4384 case the operator or voice status of USER in the current channel
4385 is changed accordingly. If `update-message-time' is non-nil, the
4386 last-message-time of the user in the current channel is set
4389 If ADD is non-nil, the user will be added with the specified
4390 information if it is not already present in the user or channel
4393 If, and only if, changes are made, or the user is added,
4394 `erc-channel-members-updated-hook' is run, and `t' is returned.
4396 See also: `erc-update-user' and `erc-update-channel-member'."
4397 (let* (changed user-changed
4398 (channel-data (erc-get-channel-user nick))
4399 (cuser (if channel-data (cdr channel-data)))
4400 (user (if channel-data (car channel-data)
4401 (erc-get-server-user nick))))
4404 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4406 (not (eq (erc-channel-user-op cuser) op)))
4408 (setf (erc-channel-user-op cuser)
4409 (cond ((eq op 'on) t)
4413 (not (eq (erc-channel-user-voice cuser) voice)))
4415 (setf (erc-channel-user-voice cuser)
4416 (cond ((eq voice 'on) t)
4417 ((eq voice 'off) nil)
4419 (when update-message-time
4420 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4422 (erc-update-user user new-nick
4423 host login full-name info)))
4427 (setq user (make-erc-server-user
4430 :full-name full-name
4433 :buffers (list (current-buffer))))
4434 (erc-add-server-user nick user))
4435 (setf (erc-server-user-buffers user)
4436 (cons (current-buffer)
4437 (erc-server-user-buffers user))))
4438 (setq cuser (make-erc-channel-user
4439 :op (cond ((eq op 'on) t)
4442 :voice (cond ((eq voice 'on) t)
4443 ((eq voice 'off) nil)
4446 (if update-message-time (current-time))))
4447 (puthash (erc-downcase nick) (cons user cuser)
4450 (when (and changed (null user-changed))
4451 (run-hooks 'erc-channel-members-changed-hook))
4452 (or changed user-changed add)))
4454 (defun erc-update-channel-member (channel nick new-nick
4455 &optional add op voice host login
4456 full-name info update-message-time)
4457 "Updates user and channel information for the user with
4458 nickname NICK in channel CHANNEL.
4460 See also: `erc-update-current-channel-member'"
4463 (erc-update-current-channel-member nick new-nick add op voice host
4464 login full-name info
4465 update-message-time)))
4467 (defun erc-remove-current-channel-member (nick)
4468 "Remove NICK from current channel membership list. Runs
4469 `erc-channel-members-changed-hook'."
4470 (let ((channel-data (erc-get-channel-user nick)))
4472 (erc-remove-channel-user nick)
4473 (run-hooks 'erc-channel-members-changed-hook))))
4475 (defun erc-remove-channel-member (channel nick)
4476 "Remove NICK from CHANNEL's membership list.
4478 See also `erc-remove-current-channel-member'."
4481 (erc-remove-current-channel-member nick)))
4483 (defun erc-update-channel-topic (channel topic &optional modify)
4484 "Find a buffer for CHANNEL and set the TOPIC for it.
4486 If optional MODIFY is 'append or 'prepend, then append or prepend the
4487 TOPIC string to the current topic."
4488 (erc-with-buffer (channel)
4489 (cond ((eq modify 'append)
4490 (setq erc-channel-topic (concat erc-channel-topic topic)))
4491 ((eq modify 'prepend)
4492 (setq erc-channel-topic (concat topic erc-channel-topic)))
4493 (t (setq erc-channel-topic topic)))
4494 (erc-update-mode-line-buffer (current-buffer))))
4496 (defun erc-set-modes (tgt mode-string)
4497 "Set the modes for the TGT provided as MODE-STRING."
4498 (let* ((modes (erc-parse-modes mode-string))
4499 (add-modes (nth 0 modes))
4500 (remove-modes (nth 1 modes))
4501 ;; list of triples: (mode-char 'on/'off argument)
4502 (arg-modes (nth 2 modes)))
4503 (cond ((erc-channel-p tgt); channel modes
4504 (let ((buf (and (boundp 'erc-server-process) erc-server-process
4505 (erc-get-buffer tgt erc-server-process))))
4507 (with-current-buffer buf
4508 (setq erc-channel-modes add-modes)
4509 (setq erc-channel-user-limit nil)
4510 (setq erc-channel-key nil)
4512 (let ((mode (nth 0 (car arg-modes)))
4513 (onoff (nth 1 (car arg-modes)))
4514 (arg (nth 2 (car arg-modes))))
4515 (cond ((string-match "^[Ll]" mode)
4516 (erc-update-channel-limit tgt onoff arg))
4517 ((string-match "^[Kk]" mode)
4518 (erc-update-channel-key tgt onoff arg))
4520 (setq arg-modes (cdr arg-modes)))
4521 (erc-update-mode-line-buffer buf)))))
4522 ;; we do not keep our nick's modes yet
4523 ;;(t (setq erc-user-modes add-modes))
4527 (defun erc-sort-strings (list-of-strings)
4528 "Sort LIST-OF-STRINGS in lexicographic order.
4531 (sort (copy-sequence list-of-strings) 'string<))
4533 (defun erc-parse-modes (mode-string)
4534 "Parse MODE-STRING into a list.
4536 Returns a list of three elements:
4538 (ADD-MODES REMOVE-MODES ARG-MODES).
4540 The add-modes and remove-modes are lists of single-character strings
4541 for modes without parameters to add and remove respectively. The
4542 arg-modes is a list of triples of the form:
4544 (MODE-CHAR ON/OFF ARGUMENT)."
4545 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
4546 (let ((chars (mapcar 'char-to-string (match-string 1 mode-string)))
4547 ;; arguments in channel modes
4548 (args-str (match-string 2 mode-string))
4552 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
4554 ;; make the argument list
4555 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
4556 (setq args (cons (match-string 1 args-str) args))
4557 (setq args-str (match-string 2 args-str)))
4558 (setq args (nreverse args))
4559 ;; collect what modes changed, and match them with arguments
4561 (cond ((string= (car chars) "+") (setq add-p t))
4562 ((string= (car chars) "-") (setq add-p nil))
4563 ((string-match "^[ovbOVB]" (car chars))
4564 (setq arg-modes (cons (list (car chars)
4566 (if args (car args) nil))
4568 (if args (setq args (cdr args))))
4569 ((string-match "^[LlKk]" (car chars))
4570 (setq arg-modes (cons (list (car chars)
4572 (if (and add-p args)
4575 (if (and add-p args) (setq args (cdr args))))
4576 (add-p (setq add-modes (cons (car chars) add-modes)))
4577 (t (setq remove-modes (cons (car chars) remove-modes))))
4578 (setq chars (cdr chars)))
4579 (setq add-modes (nreverse add-modes))
4580 (setq remove-modes (nreverse remove-modes))
4581 (setq arg-modes (nreverse arg-modes))
4582 (list add-modes remove-modes arg-modes))
4585 (defun erc-update-modes (tgt mode-string &optional nick host login)
4586 "Update the mode information for TGT, provided as MODE-STRING.
4587 Optional arguments: NICK, HOST and LOGIN - the attributes of the
4588 person who changed the modes."
4589 (let* ((modes (erc-parse-modes mode-string))
4590 (add-modes (nth 0 modes))
4591 (remove-modes (nth 1 modes))
4592 ;; list of triples: (mode-char 'on/'off argument)
4593 (arg-modes (nth 2 modes)))
4594 ;; now parse the modes changes and do the updates
4595 (cond ((erc-channel-p tgt); channel modes
4596 (let ((buf (and (boundp 'erc-server-process) erc-server-process
4597 (erc-get-buffer tgt erc-server-process))))
4599 ;; FIXME! This used to have an original buffer
4600 ;; variable, but it never switched back to the original
4601 ;; buffer. Is this wanted behavior?
4603 (if (not (boundp 'erc-channel-modes))
4604 (setq erc-channel-modes nil))
4606 (setq erc-channel-modes (delete (car remove-modes)
4608 remove-modes (cdr remove-modes)))
4610 (setq erc-channel-modes (cons (car add-modes)
4612 add-modes (cdr add-modes)))
4613 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
4615 (let ((mode (nth 0 (car arg-modes)))
4616 (onoff (nth 1 (car arg-modes)))
4617 (arg (nth 2 (car arg-modes))))
4618 (cond ((string-match "^[oO]" mode)
4619 (erc-update-channel-member tgt arg arg nil onoff))
4620 ((string-match "^[Vv]" mode)
4621 (erc-update-channel-member tgt arg arg nil nil
4623 ((string-match "^[Ll]" mode)
4624 (erc-update-channel-limit tgt onoff arg))
4625 ((string-match "^[Kk]" mode)
4626 (erc-update-channel-key tgt onoff arg))
4627 (t nil)); only ops are tracked now
4628 (setq arg-modes (cdr arg-modes))))
4629 (erc-update-mode-line buf))))
4630 ;; nick modes - ignored at this point
4633 (defun erc-update-channel-limit (channel onoff n)
4634 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
4635 "Update CHANNEL's user limit to N."
4636 (if (or (not (eq onoff 'on))
4637 (and (stringp n) (string-match "^[0-9]+$" n)))
4640 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
4641 (t (setq erc-channel-user-limit nil))))))
4643 (defun erc-update-channel-key (channel onoff key)
4644 "Update CHANNEL's key to KEY if ONOFF is 'on or to nil if it's 'off."
4647 (cond ((eq onoff 'on) (setq erc-channel-key key))
4648 (t (setq erc-channel-key nil)))))
4650 (defun erc-handle-user-status-change (type nlh &optional l)
4651 "Handle changes in any user's status.
4653 So far, only nick change is handled.
4655 Generally, the TYPE argument is a symbol describing the change type, NLH is
4656 a list containing the original nickname, login name and hostname for the user,
4657 and L is a list containing additional TYPE-specific arguments.
4659 So far the following TYPE/L pairs are supported:
4663 nickname change 'nick (NEW-NICK)"
4664 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
4672 (defun erc-highlight-notice (s)
4673 "Highlight notice message S and return it.
4674 See also variable `erc-notice-highlight-type'"
4676 ((eq erc-notice-highlight-type 'prefix)
4677 (erc-put-text-property 0 (length erc-notice-prefix)
4678 'face 'erc-notice-face s)
4680 ((eq erc-notice-highlight-type 'all)
4681 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
4685 (defun erc-make-notice (message)
4686 "Notify the user of MESSAGE."
4687 (when erc-minibuffer-notice
4688 (message "%s" message))
4689 (erc-highlight-notice (concat erc-notice-prefix message)))
4691 (defun erc-highlight-error (s)
4692 "Highlight error message S and return it."
4693 (erc-put-text-property 0 (length s) 'face 'erc-error-face s)
4696 (defun erc-put-text-property (start end property value &optional object)
4697 "Set text-property for an object (usually a string).
4698 START and END define the characters covered.
4699 PROPERTY is the text-property set, usually the symbol `face'.
4700 VALUE is the value for the text-property, usually a face symbol such as
4701 the face `bold' or `erc-pal-face'.
4702 OBJECT is a string which will be modified and returned.
4703 OBJECT is modified without being copied first.
4705 You can redefine or `defadvice' this function in order to add
4706 EmacsSpeak support."
4707 (put-text-property start end property value object))
4709 (defun erc-list (thing)
4710 "Return THING if THING is a list, or a list with THING as its element."
4715 (defun erc-parse-user (string)
4716 "Parse STRING as a user specification (nick!login@host).
4718 Return a list of the three separate tokens."
4720 ((string-match "^\\([^!]*\\)!\\([^@]*\\)@\\(.*\\)$" string)
4721 (list (match-string 1 string)
4722 (match-string 2 string)
4723 (match-string 3 string)))
4724 ;; Some bogus bouncers send Nick!(null), try to live with that.
4725 ((string-match "^\\([^!]*\\)!\\(.*\\)$" string)
4726 (list (match-string 1 string)
4728 (match-string 2 string)))
4730 (list string "" ""))))
4732 (defun erc-extract-nick (string)
4733 "Return the nick corresponding to a user specification STRING.
4735 See also `erc-parse-user'."
4736 (car (erc-parse-user string)))
4738 (defun erc-put-text-properties (start end properties
4739 &optional object value-list)
4740 "Set text-properties for OBJECT.
4742 START and END describe positions in OBJECT.
4743 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
4744 each property to the corresponding value in VALUE-LIST."
4746 (setq value-list (mapcar (lambda (x)
4749 (mapcar* (lambda (prop value)
4750 (erc-put-text-property start end prop value object))
4751 properties value-list))
4753 ;;; Input area handling:
4755 (defun erc-beg-of-input-line ()
4756 "Return the value of `point' at the beginning of the input line.
4758 Specifically, return the position of `erc-insert-marker'."
4759 (or (and (boundp 'erc-insert-marker)
4760 (markerp erc-insert-marker))
4761 (error "erc-insert-marker has no value, please report a bug"))
4762 (marker-position erc-insert-marker))
4764 (defun erc-end-of-input-line ()
4765 "Return the value of `point' at the end of the input line."
4768 (defun erc-send-current-line ()
4769 "Parse current line and send it to IRC."
4774 ((< (point) (erc-beg-of-input-line))
4775 (message "Point is not in the input area")
4777 ((not (erc-server-buffer-live-p))
4778 (message "ERC: No process running")
4781 (erc-set-active-buffer (current-buffer))
4782 (let ((inhibit-read-only t)
4783 (str (erc-user-input))
4784 (old-buf (current-buffer)))
4786 ;; Kill the input and the prompt
4787 (delete-region (erc-beg-of-input-line)
4788 (erc-end-of-input-line))
4791 (erc-send-input str)
4792 ;; Fix the buffer if the command didn't kill it
4793 (when (buffer-live-p old-buf)
4794 (with-current-buffer old-buf
4797 (goto-char (point-max))
4798 (set-marker (process-mark erc-server-process) (point))
4799 (set-marker erc-insert-marker (point))
4800 (let ((buffer-modified (buffer-modified-p)))
4801 (erc-display-prompt)
4802 (set-buffer-modified-p buffer-modified))))))
4804 ;; Only when last hook has been run...
4805 (run-hook-with-args 'erc-send-completed-hook str))))))
4807 (defun erc-user-input ()
4808 "Return the input of the user in the current buffer."
4811 (erc-end-of-input-line)))
4813 (defun erc-send-input (input)
4814 "Treat INPUT as typed in by the user. It is assumed that the input
4815 and the prompt is already deleted.
4816 This returns non-nil only iff we actually send anything."
4817 ;; Handle different kinds of inputs
4819 ;; Ignore empty input
4820 ((if erc-send-whitespace-lines
4822 (string-match "\\`[ \t\r\f\n]*\\'" input))
4823 (when erc-warn-about-blank-lines
4824 (message "Blank line - ignoring...")
4829 (erc-insert-this t))
4830 (setq erc-send-this t)
4831 (run-hook-with-args 'erc-send-pre-hook input)
4833 (if (or (string-match "\n" str)
4834 (not (char-equal (aref str 0) ?/)))
4839 ;; Insert what has to be inserted for this.
4840 (erc-display-msg line)
4841 (erc-process-input-line (concat line "\n")
4842 (null erc-flood-protect) t))
4843 (erc-split-line line)))
4844 (split-string str "\n"))
4845 ;; Insert the prompt along with the command.
4846 (erc-display-command str)
4847 (erc-process-input-line (concat str "\n") t nil))
4850 (defun erc-display-command (line)
4851 (when erc-insert-this
4852 (let ((insert-position (point)))
4853 (unless erc-hide-prompt
4854 (erc-display-prompt nil nil (erc-command-indicator)
4855 (and (erc-command-indicator)
4856 'erc-command-indicator-face)))
4857 (let ((beg (point)))
4859 (erc-put-text-property beg (point)
4860 'face 'erc-command-indicator-face)
4862 (set-marker (process-mark erc-server-process) (point))
4863 (set-marker erc-insert-marker (point))
4866 (narrow-to-region insert-position (point))
4867 (run-hooks 'erc-send-modify-hook)
4868 (run-hooks 'erc-send-post-hook))))))
4870 (defun erc-display-msg (line)
4871 "Display LINE as a message of the user to the current target at the
4873 (when erc-insert-this
4874 (let ((insert-position (point)))
4875 (insert (erc-format-my-nick))
4876 (let ((beg (point)))
4878 (erc-put-text-property beg (point)
4879 'face 'erc-input-face))
4881 (set-marker (process-mark erc-server-process) (point))
4882 (set-marker erc-insert-marker (point))
4885 (narrow-to-region insert-position (point))
4886 (run-hooks 'erc-send-modify-hook)
4887 (run-hooks 'erc-send-post-hook))))))
4889 (defun erc-command-symbol (command)
4890 "Return the erc command symbol for COMMAND if it exists and is bound."
4891 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
4892 (when (fboundp cmd) cmd)))
4894 (defun erc-extract-command-from-line (line)
4895 "Extract command and args from the input LINE.
4896 If no command was given, return nil. If command matches, return a
4897 list of the form: (command args) where both elements are strings."
4898 (when (string-match "^/\\([A-Za-z]+\\)\\(\\s-+.*\\|\\s-*\\)$" line)
4899 (let* ((cmd (erc-command-symbol (match-string 1 line)))
4900 ;; note: return is nil, we apply this simply for side effects
4901 (canon-defun (while (and cmd (symbolp (symbol-function cmd)))
4902 (setq cmd (symbol-function cmd))))
4903 (cmd-fun (or cmd #'erc-cmd-default))
4905 (if (get cmd-fun 'do-not-parse-args)
4906 (format "%s" (match-string 2 line))
4907 (delete "" (split-string (erc-trim-string
4908 (match-string 2 line)) " ")))
4910 (list cmd-fun arg))))
4912 (defun erc-split-multiline-safe (string)
4913 "Split STRING, containing multiple lines and return them in a list.
4914 Do it only for STRING as the complete input, do not carry unfinished
4915 strings over to the next call."
4920 (let ((i (string-match "\r?\n" string i0))
4921 (s (substring string i0)))
4922 (cond (i (setq l (cons (substring string i0 i) l))
4923 (setq i0 (match-end 0)))
4925 (setq l (cons s l))(setq doit nil))
4926 (t (setq doit nil)))))
4931 (defun erc-set-current-nick (nick)
4932 "Set the current nickname to NICK."
4933 (with-current-buffer (or (erc-server-buffer)
4935 (setq erc-server-current-nick nick)))
4937 (defun erc-current-nick ()
4938 "Return the current nickname."
4939 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
4942 erc-server-current-nick))
4944 (defun erc-current-nick-p (nick)
4945 "Return non-nil if NICK is the current nickname."
4946 (erc-nick-equal-p nick (erc-current-nick)))
4948 (defun erc-nick-equal-p (nick1 nick2)
4949 "Return non-nil if NICK1 and NICK2 are the same.
4951 This matches strings according to the IRC protocol's case convention.
4953 See also `erc-downcase'."
4954 (string= (erc-downcase nick1)
4955 (erc-downcase nick2)))
4957 ;; default target handling
4959 (defun erc-default-target ()
4960 "Return the current default target (as a character string) or nil if none."
4961 (let ((tgt (car erc-default-recipients)))
4964 ((listp tgt) (cdr tgt))
4967 (defun erc-add-default-channel (channel)
4968 "Add CHANNEL to the default channel list."
4970 (let ((d1 (car erc-default-recipients))
4971 (d2 (cdr erc-default-recipients))
4972 (chl (downcase channel)))
4973 (setq erc-default-recipients
4974 (cons chl erc-default-recipients))))
4976 (defun erc-delete-default-channel (channel &optional buffer)
4977 "Delete CHANNEL from the default channel list."
4978 (let ((ob (current-buffer)))
4979 (with-current-buffer (if (and buffer
4983 (setq erc-default-recipients (delete (downcase channel)
4984 erc-default-recipients)))))
4986 (defun erc-add-query (nickname)
4987 "Add QUERY'd NICKNAME to the default channel list.
4989 The previous default target of QUERY type gets removed"
4990 (let ((d1 (car erc-default-recipients))
4991 (d2 (cdr erc-default-recipients))
4992 (qt (cons 'QUERY (downcase nickname))))
4994 (eq (car d1) 'QUERY))
4995 (setq erc-default-recipients (cons qt d2))
4996 (setq erc-default-recipients (cons qt erc-default-recipients)))))
4998 (defun erc-delete-query ()
4999 "Delete the topmost target if it is a QUERY."
5001 (let ((d1 (car erc-default-recipients))
5002 (d2 (cdr erc-default-recipients)))
5004 (eq (car d1) 'QUERY))
5005 (setq erc-default-recipients d2)
5006 (error "Current target is not a QUERY"))))
5008 (defun erc-ignored-user-p (spec)
5009 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5011 Takes a full SPEC of a user in the form \"nick!login@host\", and
5012 matches against all the regexp's in `erc-ignore-list'. If any
5013 match, returns that regexp."
5014 (dolist (ignored (with-current-buffer (erc-server-buffer) erc-ignore-list))
5015 (if (string-match ignored spec)
5016 ;; We have `require'd cl, so we can return from the block named nil
5019 (defun erc-ignored-reply-p (msg tgt proc)
5020 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5021 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5023 Takes a message MSG to a channel and returns non-nil if the addressed
5024 user matches any regexp in `erc-ignore-reply-list'."
5025 (let ((target-nick (erc-message-target msg)))
5026 (if (not target-nick)
5028 (erc-with-buffer (tgt proc)
5029 (let ((user (erc-get-server-user target-nick)))
5031 (erc-list-match erc-ignore-reply-list
5032 (erc-user-spec user))))))))
5034 (defun erc-message-target (msg)
5035 "Return the addressed target in MSG.
5037 The addressed target is the string before the first colon in MSG."
5038 (if (string-match "^\\([^: ]*\\):" msg)
5039 (match-string 1 msg)
5042 (defun erc-user-spec (user)
5043 "Create a nick!user@host spec from a user struct."
5044 (let ((nick (erc-server-user-nickname user))
5045 (host (erc-server-user-host user))
5046 (login (erc-server-user-login user)))
5059 (defun erc-list-match (lst str)
5060 "Return non-nil if any regexp in LST matches STR."
5061 (memq nil (mapcar (lambda (regexp)
5062 (not (string-match regexp str)))
5067 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5068 "Toggle automatic CTCP replies (like VERSION and PING).
5070 If ARG is positive, turns CTCP replies on.
5072 If ARG is non-nil and not positive, turns CTCP replies off."
5074 (cond ((and (numberp arg) (> arg 0))
5075 (setq erc-disable-ctcp-replies t))
5076 (arg (setq erc-disable-ctcp-replies nil))
5077 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5078 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5080 (defun erc-toggle-flood-control (&optional arg)
5081 "Toggle use of flood control on sent messages.
5083 If ARG is non-nil, use flood control.
5084 If ARG is nil, do not use flood control.
5086 See `erc-server-flood-margin' for an explanation of the available
5087 flood control parameters."
5089 (setq erc-flood-protect arg)
5090 (message "ERC flood control is %s"
5091 (cond (erc-flood-protect "ON")
5094 ;; Some useful channel and nick commands for fast key bindings
5096 (defun erc-invite-only-mode (&optional arg)
5097 "Turn on the invite only mode (+i) for the current channel.
5099 If ARG is non-nil, turn this mode off (-i).
5101 This command is sent even if excess flood is detected."
5103 (erc-set-active-buffer (current-buffer))
5104 (let ((tgt (erc-default-target))
5106 (cond ((or (not tgt) (not (erc-channel-p tgt)))
5107 (erc-display-message nil 'error (current-buffer) 'no-target))
5108 (arg (erc-load-irc-script-lines (list (concat "/mode " tgt " -i"))
5110 (t (erc-load-irc-script-lines (list (concat "/mode " tgt " +i"))
5113 (defun erc-get-channel-mode-from-keypress (key)
5114 "Read a key sequence and call the corresponding channel mode function.
5115 After doing C-c C-o type in a channel mode letter.
5118 RET let's you type more than one mode at a time.
5119 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5120 If \"k\" is pressed, `erc-set-channel-key' gets called.
5121 Anything else will be sent to `erc-toggle-channel-mode'."
5122 (interactive "kChannel mode (RET to set more than one): ")
5123 (when (featurep 'xemacs)
5124 (setq key (char-to-string (event-to-character (aref key 0)))))
5125 (cond ((equal key "\C-g")
5128 (erc-insert-mode-command))
5130 (call-interactively 'erc-set-channel-limit))
5132 (call-interactively 'erc-set-channel-key))
5133 (t (erc-toggle-channel-mode key))))
5135 (defun erc-toggle-channel-mode (mode &optional channel)
5136 "Toggle channel MODE.
5138 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5139 `erc-default-target'."
5141 (erc-set-active-buffer (current-buffer))
5142 (let ((tgt (or channel (erc-default-target)))
5144 (cond ((or (null tgt) (null (erc-channel-p tgt)))
5145 (erc-display-message nil 'error 'active 'no-target))
5146 ((member mode erc-channel-modes)
5147 (erc-log (format "%s: Toggle mode %s OFF" tgt mode))
5148 (message "Toggle channel mode %s OFF" mode)
5149 (erc-server-send (format "MODE %s -%s" tgt mode)))
5150 (t (erc-log (format "%s: Toggle channel mode %s ON" tgt mode))
5151 (message "Toggle channel mode %s ON" mode)
5152 (erc-server-send (format "MODE %s +%s" tgt mode))))))
5154 (defun erc-insert-mode-command ()
5155 "Insert the line \"/mode <current target> \" at `point'."
5157 (let ((tgt (erc-default-target)))
5158 (if tgt (insert (concat "/mode " tgt " "))
5159 (erc-display-message nil 'error (current-buffer) 'no-target))))
5161 (defun erc-channel-names ()
5162 "Run \"/names #channel\" in the current channel."
5164 (erc-set-active-buffer (current-buffer))
5165 (let ((tgt (erc-default-target)))
5166 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5167 (erc-display-message nil 'error (current-buffer) 'no-target))))
5169 (defun erc-remove-text-properties-region (start end &optional object)
5170 "Clears the region (START,END) in OBJECT from all colors, etc."
5173 (let ((inhibit-read-only t))
5174 (set-text-properties start end nil object))))
5176 ;; script execution and startup
5178 (defun erc-find-file (file &optional path)
5179 "Search for a FILE in the filesystem.
5180 First the `default-directory' is searched for FILE, then any directories
5181 specified in the list PATH.
5183 If FILE is found, return the path to it."
5184 (let ((filepath file))
5185 (if (file-readable-p filepath) filepath
5188 (progn (setq filepath (expand-file-name file (car path)))
5189 (not (file-readable-p filepath))))
5190 (setq path (cdr path)))
5191 (if path filepath nil)))))
5193 (defun erc-select-startup-file ()
5194 "Select an ERC startup file.
5195 See also `erc-startup-file-list'."
5196 (let ((l erc-startup-file-list)
5198 (while (and (not f) l)
5199 (if (file-readable-p (car l))
5204 (defun erc-find-script-file (file)
5205 "Search for FILE in `default-directory', and any in `erc-script-path'."
5206 (erc-find-file file erc-script-path))
5208 (defun erc-load-script (file)
5209 "Load a script from FILE.
5211 FILE must be the full name, it is not searched in the
5212 `erc-script-path'. If the filename ends with `.el', then load it
5213 as a emacs-lisp program. Otherwise, treat it as a regular IRC
5215 (erc-log (concat "erc-load-script: " file))
5217 ((string-match "\\.el$" file)
5220 (erc-load-irc-script file))))
5222 (defun erc-process-script-line (line &optional args)
5223 "Process an IRC script LINE.
5225 Does script-specific substitutions (script arguments, current nick,
5226 server, etc.) in LINE and returns it.
5228 Substitutions are: %C and %c = current target (channel or nick),
5229 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5230 where x is any other character;
5231 $* = the entire argument string, $1 = the first argument, $2 = the second,
5233 (if (not args) (setq args ""))
5234 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5235 (percent-regexp "\\(%.\\)")
5236 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5237 (tgt (erc-default-target))
5238 (server (and (boundp 'erc-session-server) erc-session-server))
5239 (nick (erc-current-nick))
5244 (if (not tgt) (setq tgt ""))
5245 (if (not server) (setq server ""))
5246 (if (not nick) (setq nick ""))
5247 ;; First, compute the argument list
5249 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5250 (setq arg-list (cons (match-string 1 tmp) arg-list))
5251 (setq tmp (match-string 2 tmp)))
5252 (setq arg-list (nreverse arg-list))
5253 (setq arg-num (length arg-list))
5254 ;; now do the substitution
5255 (setq tmp (string-match esc-regexp line))
5257 ;;(message "beginning of while: tmp=%S" tmp)
5258 (let* ((hd (substring line 0 tmp))
5261 (tail (substring line tmp)))
5262 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5263 (setq esc (match-string 1 tail))
5264 (setq tail (substring tail (match-end 1))))
5265 ((string-match (concat "^" percent-regexp) tail)
5266 (setq esc (match-string 1 tail))
5267 (setq tail (substring tail (match-end 1)))))
5268 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5269 (setq res (concat res hd))
5271 (cond ((string= esc "") "")
5272 ((string-match "^\\$\\*$" esc) args)
5273 ((string-match "^\\$\\([0-9]+\\)$" esc)
5274 (let ((n (string-to-number (match-string 1 esc))))
5275 (message "n = %S, integerp(n)=%S" n (integerp n))
5276 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5277 ((string-match "^%[Cc]$" esc) tgt)
5278 ((string-match "^%[Ss]$" esc) server)
5279 ((string-match "^%[Nn]$" esc) nick)
5280 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5281 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5282 (message "BUG IN ERC: esc=%S" esc)
5285 (setq tmp (string-match esc-regexp line))
5286 (setq res (concat res subst))
5287 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5289 (setq res (concat res line))
5292 (defun erc-load-irc-script (file &optional force)
5293 "Load an IRC script from FILE."
5294 (erc-log (concat "erc-load-script: " file))
5295 (let ((str (with-temp-buffer
5296 (insert-file-contents file)
5298 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5300 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5301 "Load IRC script LINES (a list of strings).
5303 If optional NOEXPAND is non-nil, do not expand script-specific
5304 sequences, process the lines verbatim. Use this for multiline
5306 (let* ((cb (current-buffer))
5309 (sp (or (erc-command-indicator) (erc-prompt)))
5310 (args (and (boundp 'erc-script-args) erc-script-args)))
5311 (if (and args (string-match "^ " args))
5312 (setq args (substring args 1)))
5313 ;; prepare the prompt string for echo
5314 (erc-put-text-property 0 (length sp)
5315 'face 'erc-command-indicator-face sp)
5317 (setq s (car lines))
5318 (erc-log (concat "erc-load-script: CMD: " s))
5319 (unless (string-match "^\\s-*$" s)
5320 (let ((line (if noexpand s (erc-process-script-line s args))))
5321 (if (and (erc-process-input-line line force)
5324 (erc-put-text-property 0 (length line)
5325 'face 'erc-input-face line)
5326 (erc-display-line (concat sp line) cb)))))
5327 (setq lines (cdr lines)))))
5332 "Perform user authentication at the IRC server."
5333 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5338 erc-session-user-full-name))
5339 (if erc-session-password
5340 (erc-server-send (format "PASS %s" erc-session-password))
5341 (message "Logging in without password"))
5342 (erc-server-send (format "NICK %s" (erc-current-nick)))
5344 (format "USER %s %s %s :%s"
5346 (if erc-anonymous-login erc-email-userid (user-login-name))
5348 erc-session-user-full-name))
5349 (erc-update-mode-line))
5351 ;; connection properties' heuristics
5353 (defun erc-determine-parameters (&optional server port nick name)
5354 "Determine the connection and authentication parameters.
5355 Sets the buffer local variables:
5357 - erc-session-server
5359 - erc-session-full-name
5360 - erc-server-current-nick"
5361 (setq erc-session-server (erc-compute-server server)
5362 erc-session-port (or port erc-default-port)
5363 erc-session-user-full-name (erc-compute-full-name name))
5364 (erc-set-current-nick (erc-compute-nick nick)))
5366 (defun erc-compute-server (&optional server)
5367 "Return an IRC server name.
5369 Tries a number of increasingly more default methods until a non-nil value is
5374 - The value of the IRCSERVER environment variable
5375 - `erc-default-server'."
5378 (getenv "IRCSERVER")
5379 erc-default-server))
5381 (defun erc-compute-nick (&optional nick)
5382 "Return user's NICK.
5384 Tries a number of increasingly more default methods until a non-nil value is
5389 - The value of the IRCNICK environment variable
5390 - via the function `user-login-name'."
5392 (if (consp erc-nick) (car erc-nick) erc-nick)
5397 (defun erc-compute-full-name (&optional full-name)
5398 "Return user's FULL-NAME.
5400 Tries a number of increasingly more default methods until a non-nil value is
5404 - `erc-user-full-name'
5405 - The value of the IRCNAME environment variable
5406 - via the function `user-full-name'."
5410 (if erc-anonymous-login "unknown" nil)
5413 (defun erc-compute-port (&optional port)
5414 "Return a port for an IRC server.
5416 Tries a number of increasingly more default methods until a non-nil
5421 (or port erc-port "ircd"))
5425 (defun erc-string-to-emacs-time (string)
5426 "Convert the long number represented by STRING into an Emacs format.
5427 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
5428 (let* ((n (string-to-number (concat string ".0"))))
5429 (list (truncate (/ n 65536))
5430 (truncate (mod n 65536)))))
5432 (defun erc-emacs-time-to-erc-time (time)
5433 "Convert Emacs TIME to a number of seconds since the epoch."
5435 (+ (* (nth 0 time) 65536.0) (nth 1 time))))
5436 ; (round (+ (* (nth 0 tm) 65536.0) (nth 1 tm))))
5438 (defun erc-current-time ()
5439 "Return the `current-time' as a number of seconds since the epoch.
5441 See also `erc-emacs-time-to-erc-time'."
5442 (erc-emacs-time-to-erc-time (current-time)))
5444 (defun erc-time-diff (t1 t2)
5445 "Return the time difference in seconds between T1 and T2."
5448 (defun erc-time-gt (t1 t2)
5449 "Check whether T1 > T2."
5452 (defun erc-sec-to-time (ns)
5453 "Convert NS to a time string HH:MM.SS."
5454 (setq ns (truncate ns))
5455 (format "%02d:%02d.%02d"
5460 (defun erc-seconds-to-string (seconds)
5461 "Convert a number of SECONDS into an English phrase."
5462 (let (days hours minutes format-args output)
5463 (setq days (/ seconds 86400)
5464 seconds (% seconds 86400)
5465 hours (/ seconds 3600)
5466 seconds (% seconds 3600)
5467 minutes (/ seconds 60)
5468 seconds (% seconds 60)
5469 format-args (if (> days 0)
5470 `("%d days, %d hours, %d minutes, %d seconds"
5471 ,days ,hours ,minutes ,seconds)
5473 `("%d hours, %d minutes, %d seconds"
5474 ,hours ,minutes ,seconds)
5476 `("%d minutes, %d seconds" ,minutes ,seconds)
5477 `("%d seconds" ,seconds))))
5478 output (apply 'format format-args))
5479 ;; Change all "1 units" to "1 unit".
5480 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
5481 (setq output (erc-replace-match-subexpression-in-string
5482 "" output (match-string 2 output) 2 (match-beginning 2))))
5488 (defconst erc-clientinfo-alist
5489 '(("ACTION" . "is used to inform about one's current activity")
5490 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
5491 ("ECHO" . "echoes its arguments back")
5492 ("FINGER" . "shows user's name, location, and idle time")
5493 ("PING" . "measures delay between peers")
5494 ("TIME" . "shows client-side time")
5495 ("USERINFO" . "shows information provided by a user")
5496 ("VERSION" . "shows client type and version"))
5497 "Alist of CTCP CLIENTINFO for ERC commands.")
5499 (defun erc-client-info (s)
5500 "Return CTCP CLIENTINFO on command S.
5501 If S is NIL or an empty string then return general CLIENTINFO"
5502 (if (or (not s) (string= s ""))
5506 (concat (car e) " "))
5507 erc-clientinfo-alist))
5508 ": use CLIENTINFO <COMMAND> to get more specific information")
5509 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
5511 (concat s " " (cdr h))
5512 (concat s ": unknown command")))))
5516 (defun erc-directory-writable-p (dir)
5517 "Determine whether DIR is a writable directory.
5518 If it doesn't exist, create it."
5519 (unless (file-attributes dir) (make-directory dir))
5520 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
5522 (defun erc-kill-query-buffers (process)
5523 "Kill all buffers of PROCESS."
5524 ;; here, we only want to match the channel buffers, to avoid
5525 ;; "selecting killed buffers" b0rkage.
5526 (erc-with-all-buffers-of-server process
5528 (not (erc-server-buffer-p)))
5529 (kill-buffer (current-buffer))))
5531 (defun erc-nick-at-point ()
5532 "Give information about the nickname at `point'.
5534 If called interactively, give a human readable message in the
5535 minibuffer. If called programatically, return the corresponding
5536 entry of `channel-members'."
5538 (require 'thingatpt)
5539 (let* ((word (word-at-point))
5540 (channel-data (erc-get-channel-user word))
5541 (cuser (cdr channel-data))
5542 (user (if channel-data
5544 (erc-get-server-user word)))
5545 host login full-name info nick op voice)
5547 (setq nick (erc-server-user-nickname user)
5548 host (erc-server-user-host user)
5549 login (erc-server-user-login user)
5550 full-name (erc-server-user-full-name user)
5551 info (erc-server-user-info user))
5553 (setq op (erc-channel-user-op cuser)
5554 voice (erc-channel-user-voice cuser)))
5556 (message "%s is %s@%s%s%s"
5558 (if full-name (format " (%s)" full-name) "")
5560 (format " and is +%s%s on %s"
5563 (erc-default-target))
5567 (defun erc-away-p ()
5568 "Return t if the current ERC process is set away."
5570 (and (erc-server-buffer-live-p)
5571 (set-buffer (process-buffer erc-server-process))
5574 ;; Mode line handling
5576 (defcustom erc-mode-line-format "%s %a"
5577 "A string to be formatted and shown in the mode-line in `erc-mode'.
5579 The string is formatted using `format-spec' and the result is set as the value
5580 of `mode-line-buffer-identification'.
5582 The following characters are replaced:
5583 %a: String indicating away status or \"\" if you are not away
5584 %m: The modes of the channel
5585 %n: The current nick name
5586 %o: The topic of the channel
5587 %p: The session port
5588 %t: The name of the target (channel, nickname, or servername:port)
5589 %s: In the server-buffer, this gets filled with the value of
5590 `erc-server-announced-name', in a channel, the value of
5591 (erc-default-target) also get concatenated."
5592 :group 'erc-mode-line-and-header
5595 (defcustom erc-header-line-format "[IRC] %n on %t %m %o"
5596 "A string to be formatted and shown in the header-line in `erc-mode'.
5597 Only used in Emacs 21.
5599 See `erc-mode-line-format' for which characters are can be used."
5600 :group 'erc-mode-line-and-header
5603 (defcustom erc-header-line-uses-help-echo-p t
5604 "Show the contents of the header line in the echo area or as a tooltip
5605 when you move point into the header line."
5606 :group 'erc-mode-line-and-header
5609 (defcustom erc-show-channel-key-p t
5610 "Show the the channel key in the header line."
5611 :group 'erc-paranoia
5614 (defcustom erc-common-server-suffixes
5615 '(("openprojects.net$" . "OPN")
5616 ("freenode.net$" . "OPN"))
5617 "Alist of common server name suffixes.
5618 This variable is used in mode-line display to save screen
5619 real estate. Set it to nil if you want to avoid changing
5620 displayed hostnames."
5621 :group 'erc-mode-line-and-header
5624 (defcustom erc-mode-line-away-status-format
5625 "(AWAY since %a %b %d %H:%M) "
5626 "When you're away on a server, this is shown in the mode line.
5627 This should be a string with substitution variables recognized by
5628 format-time-message."
5629 :group 'erc-mode-line-and-header
5632 (defun erc-shorten-server-name (server-name)
5633 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
5634 (if (stringp server-name)
5636 (insert server-name)
5637 (let ((alist erc-common-server-suffixes))
5639 (goto-char (point-min))
5640 (if (re-search-forward (caar alist) nil t)
5641 (replace-match (cdar alist)))
5642 (setq alist (cdr alist))))
5645 (defun erc-format-target ()
5646 "Return the name of the target (channel or nickname or servername:port)."
5647 (let ((target (erc-default-target)))
5649 (concat (erc-shorten-server-name
5650 (or erc-server-announced-name
5651 erc-session-server))
5652 ":" (erc-port-to-string erc-session-port)))))
5654 (defun erc-format-target-and/or-server ()
5655 "Return the server name or the current target and server name combined."
5656 (let ((server-name (erc-shorten-server-name
5657 (or erc-server-announced-name
5658 erc-session-server))))
5659 (cond ((erc-default-target)
5660 (concat (erc-string-no-properties (erc-default-target))
5662 (server-name server-name)
5663 (t (buffer-name (current-buffer))))))
5665 (defun erc-format-away-status ()
5666 "Return a formatted `erc-mode-line-away-status-format'
5667 if `erc-away' is non-nil."
5668 (let ((a (when (erc-server-buffer-live-p)
5669 (with-current-buffer (process-buffer erc-server-process)
5672 (format-time-string erc-mode-line-away-status-format a)
5675 (defun erc-format-channel-modes ()
5676 "Return the current channel's modes and the estimated lag."
5677 (let ((lag (when (erc-server-buffer-live-p)
5678 (with-current-buffer (process-buffer erc-server-process)
5680 (concat (apply 'concat
5681 "(+" erc-channel-modes)
5682 (cond ((and erc-channel-user-limit erc-channel-key)
5683 (if erc-show-channel-key-p
5684 (format "lk %.0f %s" erc-channel-user-limit
5686 (format "kl %.0f" erc-channel-user-limit)))
5687 (erc-channel-user-limit
5688 ;; Emacs has no bignums
5689 (format "l %.0f" erc-channel-user-limit))
5691 (if erc-show-channel-key-p
5692 (format "k %s" erc-channel-key)
5695 (if lag (format ",lag:%.0f" lag) "")
5698 (defun erc-update-mode-line-buffer (buffer)
5699 "Update the mode line in a single ERC buffer BUFFER."
5700 (with-current-buffer buffer
5701 (let ((spec (format-spec-make
5702 ?a (erc-format-away-status)
5703 ?m (erc-format-channel-modes)
5704 ?n (or (erc-current-nick) "")
5705 ?o (erc-controls-strip erc-channel-topic)
5706 ?p (erc-port-to-string erc-session-port)
5707 ?s (erc-format-target-and/or-server)
5708 ?t (erc-format-target)))
5709 (process-status (cond ((and (erc-server-process-alive)
5710 (not erc-server-connected))
5712 ((erc-server-process-alive)
5716 (cond ((featurep 'xemacs)
5717 (setq modeline-buffer-identification
5718 (list (format-spec erc-mode-line-format spec)))
5719 (setq modeline-process (list process-status)))
5721 (setq mode-line-buffer-identification
5722 (list (format-spec erc-mode-line-format spec)))
5723 (setq mode-line-process (list process-status))))
5724 (when (boundp 'header-line-format)
5725 (let ((header (if erc-header-line-format
5726 (format-spec erc-header-line-format spec)
5728 (cond ((null header)
5729 (setq header-line-format nil))
5730 (erc-header-line-uses-help-echo-p
5731 (let ((help-echo (with-temp-buffer
5733 (fill-region (point-min) (point-max))
5735 (setq header-line-format
5736 (erc-replace-regexp-in-string
5739 (erc-propertize header 'help-echo help-echo)))))
5740 (t (setq header-line-format header))))))
5741 (if (featurep 'xemacs)
5743 (force-mode-line-update))))
5745 (defun erc-update-mode-line (&optional buffer)
5746 "Update the mode line in BUFFER.
5748 If BUFFER is nil, update the mode line in all ERC buffers."
5749 (if (and buffer (bufferp buffer))
5750 (erc-update-mode-line-buffer buffer)
5751 (dolist (buf (erc-buffer-list))
5752 (when (buffer-live-p buf)
5753 (erc-update-mode-line-buffer buf)))))
5757 (defun erc-port-to-string (p)
5758 "Convert port P to a string.
5759 P may be an integer or a service name."
5764 (defun erc-string-to-port (s)
5765 "Convert string S to either an integer port number or a service name."
5766 (let ((n (string-to-number s)))
5771 (defun erc-version (&optional here)
5772 "Show the version number of ERC in the minibuffer.
5773 If optional argument HERE is non-nil, insert version number at point."
5775 (let ((version-string
5776 (format "ERC %s" erc-version-string)))
5778 (insert version-string)
5780 (message "%s" version-string)
5783 (defun erc-version-modules (&optional here)
5784 "Show the version numbers of all loaded ERC modules in the minibuffer.
5785 If optional argument HERE is non-nil, insert version number at point."
5787 (let ((version-string
5788 (mapconcat 'identity
5789 (let (versions (case-fold-search nil))
5790 (dolist (var (apropos-internal "^erc-.*version$"))
5791 (when (and (boundp var)
5792 (stringp (symbol-value var)))
5793 (setq versions (cons (format "%S: %s"
5794 var (symbol-value var))
5798 (insert version-string)
5800 (message "%s" version-string)
5803 (defun erc-modes (&optional here)
5804 "Show the active ERC modes in the minibuffer.
5805 If optional argument HERE is non-nil, insert version number at point."
5808 (mapconcat 'identity
5809 (let (modes (case-fold-search nil))
5810 (dolist (var (apropos-internal "^erc-.*mode$"))
5811 (when (and (boundp var)
5813 (setq modes (cons (symbol-name var)
5820 (message "%s" string)
5823 (defun erc-latest-version ()
5824 "Retrieve the latest erc.el version from CVS."
5826 (if (ignore-errors (require 'url))
5828 (switch-to-buffer (get-buffer-create "*erc.el latest version*"))
5829 (delete-region (point-min) (point-max))
5830 (kill-all-local-variables)
5831 (url-insert-file-contents (concat
5832 "http://cvs.sourceforge.net/viewcvs.py/"
5833 "*checkout*/erc/erc/erc.el?content-type"
5834 "=text%2Fplain&rev=HEAD"))
5837 (error "URL needs to be installed")))
5839 (defun erc-ediff-latest-version ()
5840 "Ediff your installed erc.el with the latest CVS version.
5841 See also `erc-latest-version'."
5843 (let ((current (locate-library "erc.el")))
5845 (ediff-buffers (find-file current)
5846 (erc-latest-version))
5847 (error "You do not appear to have the uncompiled erc.el file"))))
5849 (defun erc-trim-string (s)
5850 "Trim leading and trailing spaces off S."
5852 ((not (stringp s)) nil)
5853 ((string-match "^\\s-*$" s)
5855 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
5860 (defun erc-arrange-session-in-multiple-windows ()
5861 "Open a window for every non-server buffer related to `erc-session-server'.
5863 All windows are opened in the current frame."
5865 (unless (boundp 'erc-server-process)
5866 (error "No erc-process found in current buffer"))
5867 (let ((bufs (erc-buffer-list nil erc-server-process)))
5869 (delete-other-windows)
5870 (switch-to-buffer (car bufs))
5871 (setq bufs (cdr bufs))
5874 (switch-to-buffer-other-window (car bufs))
5875 (setq bufs (cdr bufs))
5876 (balance-windows)))))
5878 (defun erc-popup-input-buffer ()
5879 "Provide a input buffer."
5881 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
5886 (list (symbol-name e)))
5887 (apropos-internal "-mode$" 'commandp))
5889 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
5891 (narrow-to-region (point) (point))
5892 (shrink-window-if-larger-than-buffer)))
5896 (defun erc-make-message-variable-name (catalog entry)
5897 "Create a variable name corresponding to CATALOG's ENTRY."
5898 (intern (concat "erc-message-"
5899 (symbol-name catalog) "-" (symbol-name entry))))
5901 (defun erc-define-catalog-entry (catalog entry format-spec)
5902 "Set CATALOG's ENTRY to FORMAT-SPEC."
5903 (set (erc-make-message-variable-name catalog entry)
5906 (defun erc-define-catalog (catalog entries)
5907 "Define a CATALOG according to ENTRIES."
5908 (dolist (entry entries)
5909 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
5913 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
5914 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
5915 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
5916 (cannot-find-file . "Cannot find file %f")
5917 (cannot-read-file . "Cannot read file %f")
5918 (connect . "Connecting to %S:%p... ")
5920 (country-unknown . "%d: No such domain")
5921 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
5922 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
5923 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
5924 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
5925 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
5926 (flood-strict-mode . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
5927 (disconnected . "Connection failed! Re-establishing connection...")
5928 (disconnected-noreconnect . "Connection failed! Not re-establishing connection.")
5929 (login . "Logging in as \'%n\'...")
5930 (nick-in-use . "%n is in use. Choose new nickname: ")
5931 (nick-too-long . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
5932 (no-default-channel . "No default channel")
5933 (no-invitation . "You've got no invitation")
5934 (no-target . "No target")
5935 (ops . "%i operator%s: %o")
5936 (ops-none . "No operators in this channel.")
5937 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
5938 (variable-not-bound . "Variable not bound!")
5939 (ACTION . "* %n %a")
5940 (CTCP-CLIENTINFO . "Client info for %n: %m")
5941 (CTCP-ECHO . "Echo %n: %m")
5942 (CTCP-FINGER . "Finger info for %n: %m")
5943 (CTCP-PING . "Ping time to %n is %t")
5944 (CTCP-TIME . "Time by %n is %m")
5945 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
5946 (CTCP-VERSION . "Version for %n is %m")
5947 (ERROR . "==> ERROR from %s: %c\n")
5948 (INVITE . "%n (%u@%h) invites you to channel %c")
5949 (JOIN . "%n (%u@%h) has joined channel %c")
5950 (JOIN-you . "You have joined channel %c")
5951 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
5952 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
5953 (KICK-by-you . "You have kicked %k off channel %c: %r")
5954 (MODE . "%n (%u@%h) has changed mode for %t to %m")
5955 (MODE-nick . "%n has changed mode for %t to %m")
5956 (NICK . "%n (%u@%h) is now known as %N")
5957 (NICK-you . "Your new nickname is %N")
5958 (PART . erc-message-english-PART)
5959 (PING . "PING from server (last: %s sec. ago)")
5960 (PONG . "PONG from %h (%i second%s)")
5961 (QUIT . "%n (%u@%h) has quit: %r")
5962 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
5963 (WALLOPS . "Wallops from %n: %m")
5964 (s004 . "%s %v %U %C")
5965 (s221 . "User modes for %n: %m")
5966 (s252 . "%i operator(s) online")
5967 (s253 . "%i unknown connection(s)")
5968 (s254 . "%i channels formed")
5969 (s301 . "%n is AWAY: %r")
5970 (s303 . "Is online: %n")
5973 (s311 . "%n is %f (%u@%h)")
5974 (s312 . "%n is/was on server %s (%c)")
5975 (s313 . "%n is an IRC operator")
5976 (s314 . "%n was %f (%u@%h)")
5977 (s317 . "%n has been idle for %i")
5978 (s317-on-since . "%n has been idle for %i, on since %t")
5979 (s319 . "%n is on channel(s): %c")
5980 (s320 . "%n is an identified user")
5981 (s321 . "Channel Users Topic")
5982 (s322 . "%c [%u] %t")
5983 (s324 . "%c modes: %m")
5984 (s329 . "%c was created on %t")
5986 (s331 . "No topic is set for %c")
5987 (s332 . "Topic for %c: %T")
5988 (s333 . "%c: topic set by %n, %t")
5989 (s341 . "Inviting %n to channel %c")
5990 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
5991 (s353 . "Users on %c: %u")
5992 (s367 . "Ban on %b on %c set by %s on %t (Use /banlist!)")
5993 (s368 . "Banlist of %c ends.")
5994 (s379 . "%c: Forwarded to %f")
5995 (s391 . "The time at %s is %t")
5996 (s401 . "%n: No such nick/channel")
5997 (s403 . "%c: No such channel")
5998 (s404 . "%c: Cannot send to channel")
5999 (s405 . "%c: You have joined too many channels")
6000 (s406 . "%n: There was no such nickname")
6001 (s412 . "No text to send")
6002 (s421 . "%c: Unknown command")
6003 (s431 . "No nickname given")
6004 (s432 . "%n is an erroneous nickname")
6005 (s442 . "%c: You're not on that channel")
6006 (s445 . "SUMMON has been disabled")
6007 (s446 . "USERS has been disabled")
6008 (s451 . "You have not registered")
6009 (s461 . "%c: not enough parameters")
6010 (s462 . "Unauthorized command (already registered)")
6011 (s463 . "Your host isn't among the privileged")
6012 (s464 . "Password incorrect")
6013 (s465 . "You are banned from this server")
6014 (s474 . "You can't join %c because you're banned (+b)")
6015 (s475 . "You must specify the correct channel key (+k) to join %c")
6016 (s481 . "Permission Denied - You're not an IRC operator")
6017 (s482 . "You need to be a channel operator of %c to do that")
6018 (s483 . "You can't kill a server!")
6019 (s484 . "Your connection is restricted!")
6020 (s485 . "You're not the original channel operator")
6021 (s491 . "No O-lines for your host")
6022 (s501 . "Unknown MODE flag")
6023 (s502 . "You can't change modes for other users")))
6025 (defun erc-message-english-PART (&rest args)
6026 "Format a proper PART message.
6028 This function is an example on what could be done with formatting
6030 (let ((nick (cadr (memq ?n args)))
6031 (user (cadr (memq ?u args)))
6032 (host (cadr (memq ?h args)))
6033 (channel (cadr (memq ?c args)))
6034 (reason (cadr (memq ?r args))))
6035 (if (string= nick (erc-current-nick))
6036 (format "You have left channel %s" channel)
6037 (format "%s (%s@%s) has left channel %s%s"
6038 nick user host channel
6039 (if (not (string= reason ""))
6040 (format ": %s" reason)
6044 (defvar erc-current-message-catalog 'english)
6045 (make-variable-buffer-local 'erc-current-message-catalog)
6047 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6048 "Retrieve ENTRY from CATALOG.
6050 If CATALOG is nil, `erc-current-message-catalog' is used.
6052 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6054 (unless catalog (setq catalog erc-current-message-catalog))
6055 (let ((var (erc-make-message-variable-name catalog entry)))
6058 (when (boundp (erc-make-message-variable-name 'english entry))
6059 (symbol-value (erc-make-message-variable-name 'english entry))))))
6061 (defun erc-format-message (msg &rest args)
6062 "Format MSG according to ARGS.
6064 See also `format-spec'."
6065 (when (eq (logand (length args) 1) 1) ; oddp
6066 (error "Obscure usage of this function appeared"))
6067 (let ((entry (erc-retrieve-catalog-entry msg)))
6069 (error "No format spec for message %s" msg))
6070 (when (functionp entry)
6071 (setq entry (apply entry args)))
6072 (format-spec entry (apply 'format-spec-make args))))
6074 ;;; Various hook functions
6076 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6078 (defcustom erc-kill-server-hook '(erc-kill-server)
6079 "*Invoked whenever a server-buffer is killed via `kill-buffer'."
6083 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6084 "*Invoked whenever a channel-buffer is killed via `kill-buffer'."
6088 (defcustom erc-kill-buffer-hook nil
6089 "*Hook run whenever a non-server or channel buffer is killed.
6091 See also `kill-buffer'."
6095 (defun erc-kill-buffer-function ()
6096 "Function to call when an ERC buffer is killed.
6097 This function should be on `kill-buffer-hook'.
6098 When the current buffer is in `erc-mode', this function will run
6099 one of the following hooks:
6100 `erc-kill-server-hook' if the server buffer was killed,
6101 `erc-kill-channel-hook' if a channel buffer was killed,
6102 or `erc-kill-buffer-hook' if any other buffer."
6103 (when (eq major-mode 'erc-mode)
6104 (erc-remove-channel-users)
6106 ((eq (erc-server-buffer) (current-buffer))
6107 (run-hooks 'erc-kill-server-hook))
6108 ((erc-channel-p (erc-default-target))
6109 (run-hooks 'erc-kill-channel-hook))
6111 (run-hooks 'erc-kill-buffer-hook)))))
6113 (defun erc-kill-server ()
6114 "Sends a QUIT command to the server when the server buffer is killed.
6115 This function should be on `erc-kill-server-hook'."
6116 (when (erc-server-process-alive)
6117 (setq erc-server-quitting t)
6118 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6120 (defun erc-kill-channel ()
6121 "Sends a PART command to the server when the channel buffer is killed.
6122 This function should be on `erc-kill-channel-hook'."
6123 (when (erc-server-process-alive)
6124 (let ((tgt (erc-default-target)))
6125 (erc-server-send (format "PART %s :%s" tgt
6126 (funcall erc-part-reason nil))
6131 ;;; Deprecated. We might eventually stop requiring the goodies automatically.
6132 ;;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6133 ;;; avoid a recursive require error when byte-compiling the entire package.
6134 (require 'erc-goodies)
6136 ;;; erc.el ends here
6139 ;; outline-regexp: ";;+"
6140 ;; indent-tabs-mode: t
6144 ;; arch-tag: d19587f6-627e-48c1-8d86-58595fa3eca3