1 ;; erc.el --- An Emacs Internet Relay Chat client -*- lexical-binding:t -*-
3 ;; Copyright (C) 1997-2014 Free Software Foundation, Inc.
5 ;; Author: Alexander L. Belikoff (alexander@belikoff.net)
6 ;; Contributors: Sergey Berezin (sergey.berezin@cs.cmu.edu),
7 ;; Mario Lang (mlang@delysid.org),
8 ;; Alex Schroeder (alex@gnu.org)
9 ;; Andreas Fuchs (afs@void.at)
10 ;; Gergely Nagy (algernon@midgard.debian.net)
11 ;; David Edmondson (dme@dme.org)
12 ;; Kelvin White (kwhite@gnu.org)
13 ;; Maintainer: emacs-devel@gnu.org
14 ;; 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 3 of the License, or
22 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
34 ;; ERC is a powerful, modular, and extensible IRC client for Emacs.
36 ;; For more information, see the following URLs:
37 ;; * http://sv.gnu.org/projects/erc/
38 ;; * http://www.emacswiki.org/cgi-bin/wiki/ERC
40 ;; As of 2006-06-13, ERC development is now hosted on Savannah
41 ;; (http://sv.gnu.org/projects/erc). I invite everyone who wants to
42 ;; hack on it to contact me <mwolson@gnu.org> in order to get write
43 ;; access to the shared Arch archive.
47 ;; Put erc.el in your load-path, and put (require 'erc) in your .emacs.
51 ;; Use M-x customize-group RET erc RET to get an overview
52 ;; of all the variables you can tweak.
56 ;; To connect to an IRC server, do
60 ;; After you are connected to a server, you can use C-h m or have a look at
66 (defconst erc-version-string (format "\C-bERC\C-b (IRC client for Emacs %s)" emacs-version)
67 "ERC version. This is used by function `erc-version'.")
71 (eval-when-compile (require 'cl-lib))
75 (require 'auth-source)
78 (defvar erc-official-location
79 "http://emacswiki.org/cgi-bin/wiki/ERC (mailing list: erc-discuss@gnu.org)"
80 "Location of the ERC client on the Internet.")
83 "Emacs Internet Relay Chat client."
84 :link '(url-link "http://www.emacswiki.org/cgi-bin/wiki/ERC")
85 :link '(custom-manual "(erc) Top")
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-lurker nil
106 "Hide specified message types sent by lurkers"
110 (defgroup erc-query nil
111 "Using separate buffers for private discussions"
114 (defgroup erc-quit-and-part nil
115 "Quitting and parting channels"
118 (defgroup erc-paranoia nil
119 "Know what is sent and received; control the display of sensitive data."
122 (defgroup erc-scripts nil
123 "Running scripts at startup and with /LOAD"
126 (require 'erc-backend)
128 ;; compatibility with older ERC releases
130 (define-obsolete-variable-alias 'erc-announced-server-name
131 'erc-server-announced-name "ERC 5.1")
132 (define-obsolete-variable-alias 'erc-process 'erc-server-process "ERC 5.1")
133 (define-obsolete-variable-alias 'erc-default-coding-system
134 'erc-server-coding-system "ERC 5.1")
136 (define-obsolete-function-alias 'erc-send-command
137 'erc-server-send "ERC 5.1")
139 ;; tunable connection and authentication parameters
141 (defcustom erc-server nil
142 "IRC server to use if one is not provided.
143 See function `erc-compute-server' for more details on connection
144 parameters and authentication."
146 :type '(choice (const :tag "None" nil)
147 (string :tag "Server")))
149 (defcustom erc-port nil
150 "IRC port to use if not specified.
152 This can be either a string or a number."
154 :type '(choice (const :tag "None" nil)
155 (integer :tag "Port number")
156 (string :tag "Port string")))
158 (defcustom erc-nick nil
159 "Nickname to use if one is not provided.
161 This can be either a string, or a list of strings.
162 In the latter case, if the first nick in the list is already in use,
163 other nicks are tried in the list order.
165 See function `erc-compute-nick' for more details on connection
166 parameters and authentication."
168 :type '(choice (const :tag "None" nil)
169 (string :tag "Nickname")
170 (repeat (string :tag "Nickname"))))
172 (defcustom erc-nick-uniquifier "`"
173 "The string to append to the nick if it is already in use."
177 (defcustom erc-try-new-nick-p t
178 "If the nickname you chose isn't available, and this option is non-nil,
179 ERC should automatically attempt to connect with another nickname.
181 You can manually set another nickname with the /NICK command."
185 (defcustom erc-user-full-name nil
188 This can be either a string or a function to call.
190 See function `erc-compute-full-name' for more details on connection
191 parameters and authentication."
193 :type '(choice (const :tag "No name" nil)
195 (function :tag "Get from function"))
196 :set (lambda (sym val)
197 (set sym (if (functionp val) (funcall val) val))))
199 (defcustom erc-rename-buffers nil
200 "When this is set to t, buffers will be renamed to network name if available"
204 (defvar erc-password nil
205 "Password to use when authenticating to an IRC server.
206 It is not strictly necessary to provide this, since ERC will
209 (defcustom erc-user-mode nil
210 "Initial user modes to be set after a connection is established."
212 :type '(choice (const nil) string function))
215 (defcustom erc-prompt-for-password t
216 "Asks before using the default password, or whether to enter a new one."
220 (defcustom erc-warn-about-blank-lines t
221 "Warn the user if they attempt to send a blank line."
225 (defcustom erc-send-whitespace-lines nil
226 "If set to non-nil, send lines consisting of only whitespace."
230 (defcustom erc-hide-prompt nil
231 "If non-nil, do not display the prompt for commands.
233 \(A command is any input starting with a '/').
235 See also the variables `erc-prompt' and `erc-command-indicator'."
241 (defcustom erc-show-my-nick t
242 "If non-nil, display one's own nickname when sending a message.
244 If non-nil, \"<nickname>\" will be shown.
245 If nil, only \"> \" will be shown."
249 (define-widget 'erc-message-type 'set
250 "A set of standard IRC Message types."
251 :args '((const "JOIN")
257 (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))
259 (defcustom erc-hide-list nil
260 "List of IRC type messages to hide.
261 A typical value would be '(\"JOIN\" \"PART\" \"QUIT\")."
263 :type 'erc-message-type)
265 (defvar erc-session-password nil
266 "The password used for the current session.")
267 (make-variable-buffer-local 'erc-session-password)
269 (defcustom erc-disconnected-hook nil
270 "Run this hook with arguments (NICK IP REASON) when disconnected.
271 This happens before automatic reconnection. Note, that
272 `erc-server-QUIT-functions' might not be run when we disconnect,
273 simply because we do not necessarily receive the QUIT event."
277 (defcustom erc-complete-functions nil
278 "These functions get called when the user hits TAB in ERC.
279 Each function in turn is called until one returns non-nil to
280 indicate it has handled the input."
284 (defcustom erc-join-hook nil
285 "Hook run when we join a channel. Hook functions are called
286 without arguments, with the current buffer set to the buffer of
289 See also `erc-server-JOIN-functions', `erc-part-hook'."
293 (defcustom erc-quit-hook nil
294 "Hook run when processing a quit command directed at our nick.
296 The hook receives one argument, the current PROCESS.
297 See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
301 (defcustom erc-part-hook nil
302 "Hook run when processing a PART message directed at our nick.
304 The hook receives one argument, the current BUFFER.
305 See also `erc-server-QUIT-functions', `erc-quit-hook' and
306 `erc-disconnected-hook'."
310 (defcustom erc-kick-hook nil
311 "Hook run when processing a KICK message directed at our nick.
313 The hook receives one argument, the current BUFFER.
314 See also `erc-server-PART-functions' and `erc-part-hook'."
318 (defcustom erc-nick-changed-functions nil
319 "List of functions run when your nick was successfully changed.
321 Each function should accept two arguments, NEW-NICK and OLD-NICK."
325 (defcustom erc-connect-pre-hook '(erc-initialize-log-marker)
326 "Hook called just before `erc' calls `erc-connect'.
327 Functions are passed a buffer as the first argument."
332 (defvar erc-channel-users nil
333 "A hash table of members in the current channel, which
334 associates nicknames with cons cells of the form:
335 \(USER . MEMBER-DATA) where USER is a pointer to an
336 erc-server-user struct, and MEMBER-DATA is a pointer to an
337 erc-channel-user struct.")
338 (make-variable-buffer-local 'erc-channel-users)
340 (defvar erc-server-users nil
341 "A hash table of users on the current server, which associates
342 nicknames with erc-server-user struct instances.")
343 (make-variable-buffer-local 'erc-server-users)
345 (defun erc-downcase (string)
346 "Convert STRING to IRC standard conforming downcase."
347 (let ((s (downcase string))
353 (while (string-match "[]\\[~]" s)
354 (aset s (match-beginning 0)
355 (cdr (assq (aref s (match-beginning 0)) c)))))
358 (defmacro erc-with-server-buffer (&rest body)
359 "Execute BODY in the current ERC server buffer.
360 If no server buffer exists, return nil."
361 (declare (indent 0) (debug (body)))
362 (let ((buffer (make-symbol "buffer")))
363 `(let ((,buffer (erc-server-buffer)))
364 (when (buffer-live-p ,buffer)
365 (with-current-buffer ,buffer
368 (cl-defstruct (erc-server-user (:type vector) :named)
370 nickname host login full-name info
373 ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
374 ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
378 (cl-defstruct (erc-channel-user (:type vector) :named)
379 voice halfop op admin owner
380 ;; Last message time (in the form of the return value of
383 ;; This is useful for ordered name completion.
384 (last-message-time nil))
386 (defsubst erc-get-channel-user (nick)
387 "Find the (USER . CHANNEL-DATA) element corresponding to NICK
388 in the current buffer's `erc-channel-users' hash table."
389 (gethash (erc-downcase nick) erc-channel-users))
391 (defsubst erc-get-server-user (nick)
392 "Find the USER corresponding to NICK in the current server's
393 `erc-server-users' hash table."
394 (erc-with-server-buffer
395 (gethash (erc-downcase nick) erc-server-users)))
397 (defsubst erc-add-server-user (nick user)
398 "This function is for internal use only.
400 Adds USER with nickname NICK to the `erc-server-users' hash table."
401 (erc-with-server-buffer
402 (puthash (erc-downcase nick) user erc-server-users)))
404 (defsubst erc-remove-server-user (nick)
405 "This function is for internal use only.
407 Removes the user with nickname NICK from the `erc-server-users'
408 hash table. This user is not removed from the
409 `erc-channel-users' lists of other buffers.
411 See also: `erc-remove-user'."
412 (erc-with-server-buffer
413 (remhash (erc-downcase nick) erc-server-users)))
415 (defun erc-change-user-nickname (user new-nick)
416 "This function is for internal use only.
418 Changes the nickname of USER to NEW-NICK in the
419 `erc-server-users' hash table. The `erc-channel-users' lists of
420 other buffers are also changed."
421 (let ((nick (erc-server-user-nickname user)))
422 (setf (erc-server-user-nickname user) new-nick)
423 (erc-with-server-buffer
424 (remhash (erc-downcase nick) erc-server-users)
425 (puthash (erc-downcase new-nick) user erc-server-users))
426 (dolist (buf (erc-server-user-buffers user))
427 (if (buffer-live-p buf)
428 (with-current-buffer buf
429 (let ((cdata (erc-get-channel-user nick)))
430 (remhash (erc-downcase nick) erc-channel-users)
431 (puthash (erc-downcase new-nick) cdata
432 erc-channel-users)))))))
434 (defun erc-remove-channel-user (nick)
435 "This function is for internal use only.
437 Removes the user with nickname NICK from the `erc-channel-users'
438 list for this channel. If this user is not in the
439 `erc-channel-users' list of any other buffers, the user is also
440 removed from the server's `erc-server-users' list.
442 See also: `erc-remove-server-user' and `erc-remove-user'."
443 (let ((channel-data (erc-get-channel-user nick)))
445 (let ((user (car channel-data)))
446 (setf (erc-server-user-buffers user)
447 (delq (current-buffer)
448 (erc-server-user-buffers user)))
449 (remhash (erc-downcase nick) erc-channel-users)
450 (if (null (erc-server-user-buffers user))
451 (erc-remove-server-user nick))))))
453 (defun erc-remove-user (nick)
454 "This function is for internal use only.
456 Removes the user with nickname NICK from the `erc-server-users'
457 list as well as from all `erc-channel-users' lists.
459 See also: `erc-remove-server-user' and
460 `erc-remove-channel-user'."
461 (let ((user (erc-get-server-user nick)))
463 (let ((buffers (erc-server-user-buffers user)))
464 (dolist (buf buffers)
465 (if (buffer-live-p buf)
466 (with-current-buffer buf
467 (remhash (erc-downcase nick) erc-channel-users)
468 (run-hooks 'erc-channel-members-changed-hook)))))
469 (erc-remove-server-user nick))))
471 (defun erc-remove-channel-users ()
472 "This function is for internal use only.
474 Removes all users in the current channel. This is called by
475 `erc-server-PART' and `erc-server-QUIT'."
476 (when (and erc-server-connected
477 (erc-server-process-alive)
478 (hash-table-p erc-channel-users))
479 (maphash (lambda (nick _cdata)
480 (erc-remove-channel-user nick))
482 (clrhash erc-channel-users)))
484 (defsubst erc-channel-user-owner-p (nick)
485 "Return non-nil if NICK is an owner of the current channel."
487 (hash-table-p erc-channel-users)
488 (let ((cdata (erc-get-channel-user nick)))
489 (and cdata (cdr cdata)
490 (erc-channel-user-owner (cdr cdata))))))
492 (defsubst erc-channel-user-admin-p (nick)
493 "Return non-nil if NICK is an admin in the current channel."
495 (hash-table-p erc-channel-users)
496 (let ((cdata (erc-get-channel-user nick)))
497 (and cdata (cdr cdata)
498 (erc-channel-user-admin (cdr cdata))))))
500 (defsubst erc-channel-user-op-p (nick)
501 "Return non-nil if NICK is an operator in the current channel."
503 (hash-table-p erc-channel-users)
504 (let ((cdata (erc-get-channel-user nick)))
505 (and cdata (cdr cdata)
506 (erc-channel-user-op (cdr cdata))))))
508 (defsubst erc-channel-user-halfop-p (nick)
509 "Return non-nil if NICK is a half-operator in the current channel."
511 (hash-table-p erc-channel-users)
512 (let ((cdata (erc-get-channel-user nick)))
513 (and cdata (cdr cdata)
514 (erc-channel-user-halfop (cdr cdata))))))
516 (defsubst erc-channel-user-voice-p (nick)
517 "Return non-nil if NICK has voice in the current channel."
519 (hash-table-p erc-channel-users)
520 (let ((cdata (erc-get-channel-user nick)))
521 (and cdata (cdr cdata)
522 (erc-channel-user-voice (cdr cdata))))))
524 (defun erc-get-channel-user-list ()
525 "Return a list of users in the current channel. Each element
526 of the list is of the form (USER . CHANNEL-DATA), where USER is
527 an erc-server-user struct, and CHANNEL-DATA is either nil or an
528 erc-channel-user struct.
530 See also: `erc-sort-channel-users-by-activity'"
532 (if (hash-table-p erc-channel-users)
533 (maphash (lambda (_nick cdata)
534 (setq users (cons cdata users)))
538 (defun erc-get-server-nickname-list ()
539 "Return a list of known nicknames on the current server."
540 (erc-with-server-buffer
542 (when (hash-table-p erc-server-users)
543 (maphash (lambda (_n user)
545 (cons (erc-server-user-nickname user)
550 (defun erc-get-channel-nickname-list ()
551 "Return a list of known nicknames on the current channel."
553 (when (hash-table-p erc-channel-users)
554 (maphash (lambda (_n cdata)
556 (cons (erc-server-user-nickname (car cdata))
561 (defun erc-get-server-nickname-alist ()
562 "Return an alist of known nicknames on the current server."
563 (erc-with-server-buffer
565 (when (hash-table-p erc-server-users)
566 (maphash (lambda (_n user)
568 (cons (cons (erc-server-user-nickname user) nil)
573 (defun erc-get-channel-nickname-alist ()
574 "Return an alist of known nicknames on the current channel."
576 (when (hash-table-p erc-channel-users)
577 (maphash (lambda (_n cdata)
579 (cons (cons (erc-server-user-nickname (car cdata)) nil)
584 (defun erc-sort-channel-users-by-activity (list)
585 "Sort LIST such that users which have spoken most recently are listed first.
586 LIST must be of the form (USER . CHANNEL-DATA).
588 See also: `erc-get-channel-user-list'."
591 (when (and (cdr x) (cdr y))
592 (let ((tx (erc-channel-user-last-message-time (cdr x)))
593 (ty (erc-channel-user-last-message-time (cdr y))))
596 (time-less-p ty tx))))))))
598 (defun erc-sort-channel-users-alphabetically (list)
599 "Sort LIST so that users' nicknames are in alphabetical order.
600 LIST must be of the form (USER . CHANNEL-DATA).
602 See also: `erc-get-channel-user-list'."
605 (when (and (cdr x) (cdr y))
606 (let ((nickx (downcase (erc-server-user-nickname (car x))))
607 (nicky (downcase (erc-server-user-nickname (car y)))))
610 (string-lessp nickx nicky))))))))
612 (defvar erc-channel-topic nil
613 "A topic string for the channel. Should only be used in channel-buffers.")
614 (make-variable-buffer-local 'erc-channel-topic)
616 (defvar erc-channel-modes nil
617 "List of strings representing channel modes.
618 E.g. '(\"i\" \"m\" \"s\" \"b Quake!*@*\")
619 \(not sure the ban list will be here, but why not)")
620 (make-variable-buffer-local 'erc-channel-modes)
622 (defvar erc-insert-marker nil
623 "The place where insertion of new text in erc buffers should happen.")
624 (make-variable-buffer-local 'erc-insert-marker)
626 (defvar erc-input-marker nil
627 "The marker where input should be inserted.")
628 (make-variable-buffer-local 'erc-input-marker)
630 (defun erc-string-no-properties (string)
631 "Return a copy of STRING will all text-properties removed."
632 (let ((newstring (copy-sequence string)))
633 (set-text-properties 0 (length newstring) nil newstring)
636 (defcustom erc-prompt "ERC>"
637 "Prompt used by ERC. Trailing whitespace is not required."
639 :type '(choice string function))
642 "Return the input prompt as a string.
644 See also the variable `erc-prompt'."
645 (let ((prompt (if (functionp erc-prompt)
648 (if (> (length prompt) 0)
652 (defcustom erc-command-indicator nil
653 "Indicator used by ERC for showing commands.
655 If non-nil, this will be used in the ERC buffer to indicate
656 commands (i.e., input starting with a '/').
658 If nil, the prompt will be constructed from the variable `erc-prompt'."
660 :type '(choice (const nil) string function))
662 (defun erc-command-indicator ()
663 "Return the command indicator prompt as a string.
665 This only has any meaning if the variable `erc-command-indicator' is non-nil."
666 (and erc-command-indicator
667 (let ((prompt (if (functionp erc-command-indicator)
668 (funcall erc-command-indicator)
669 erc-command-indicator)))
670 (if (> (length prompt) 0)
674 (defcustom erc-notice-prefix "*** "
675 "Prefix for all notices."
679 (defcustom erc-notice-highlight-type 'all
680 "Determines how to highlight notices.
681 See `erc-notice-prefix'.
683 The following values are allowed:
685 'prefix - highlight notice prefix only
686 'all - highlight the entire notice
688 Any other value disables notice's highlighting altogether."
690 :type '(choice (const :tag "highlight notice prefix only" prefix)
691 (const :tag "highlight the entire notice" all)
692 (const :tag "don't highlight notices at all" nil)))
694 (defcustom erc-echo-notice-hook nil
695 "List of functions to call to echo a private notice.
696 Each function is called with four arguments, the string
697 to display, the parsed server message, the target buffer (or
698 nil), and the sender. The functions are called in order, until a
699 function evaluates to non-nil. These hooks are called after
700 those specified in `erc-echo-notice-always-hook'.
702 See also: `erc-echo-notice-always-hook',
703 `erc-echo-notice-in-default-buffer',
704 `erc-echo-notice-in-target-buffer',
705 `erc-echo-notice-in-minibuffer',
706 `erc-echo-notice-in-server-buffer',
707 `erc-echo-notice-in-active-non-server-buffer',
708 `erc-echo-notice-in-active-buffer',
709 `erc-echo-notice-in-user-buffers',
710 `erc-echo-notice-in-user-and-target-buffers',
711 `erc-echo-notice-in-first-user-buffer'"
714 :options '(erc-echo-notice-in-default-buffer
715 erc-echo-notice-in-target-buffer
716 erc-echo-notice-in-minibuffer
717 erc-echo-notice-in-server-buffer
718 erc-echo-notice-in-active-non-server-buffer
719 erc-echo-notice-in-active-buffer
720 erc-echo-notice-in-user-buffers
721 erc-echo-notice-in-user-and-target-buffers
722 erc-echo-notice-in-first-user-buffer))
724 (defcustom erc-echo-notice-always-hook
725 '(erc-echo-notice-in-default-buffer)
726 "List of functions to call to echo a private notice.
727 Each function is called with four arguments, the string
728 to display, the parsed server message, the target buffer (or
729 nil), and the sender. The functions are called in order, and all
730 functions are called. These hooks are called before those
731 specified in `erc-echo-notice-hook'.
733 See also: `erc-echo-notice-hook',
734 `erc-echo-notice-in-default-buffer',
735 `erc-echo-notice-in-target-buffer',
736 `erc-echo-notice-in-minibuffer',
737 `erc-echo-notice-in-server-buffer',
738 `erc-echo-notice-in-active-non-server-buffer',
739 `erc-echo-notice-in-active-buffer',
740 `erc-echo-notice-in-user-buffers',
741 `erc-echo-notice-in-user-and-target-buffers',
742 `erc-echo-notice-in-first-user-buffer'"
745 :options '(erc-echo-notice-in-default-buffer
746 erc-echo-notice-in-target-buffer
747 erc-echo-notice-in-minibuffer
748 erc-echo-notice-in-server-buffer
749 erc-echo-notice-in-active-non-server-buffer
750 erc-echo-notice-in-active-buffer
751 erc-echo-notice-in-user-buffers
752 erc-echo-notice-in-user-and-target-buffers
753 erc-echo-notice-in-first-user-buffer))
755 ;; other tunable parameters
757 (defcustom erc-whowas-on-nosuchnick nil
758 "If non-nil, do a whowas on a nick if no such nick."
762 (defcustom erc-verbose-server-ping nil
763 "If non-nil, show every time you get a PING or PONG from the server."
767 (defcustom erc-public-away-p nil
768 "Let others know you are back when you are no longer marked away.
769 This happens in this form:
770 * <nick> is back (gone for <time>)
772 Many consider it impolite to do so automatically."
776 (defcustom erc-away-nickname nil
777 "The nickname to take when you are marked as being away."
779 :type '(choice (const nil)
782 (defcustom erc-paranoid nil
783 "If non-nil, then all incoming CTCP requests will be shown."
787 (defcustom erc-disable-ctcp-replies nil
788 "Disable replies to CTCP requests that require a reply.
789 If non-nil, then all incoming CTCP requests that normally require
790 an automatic reply (like VERSION or PING) will be ignored. Good to
791 set if some hacker is trying to flood you away."
795 (defcustom erc-anonymous-login t
796 "Be paranoid, don't give away your machine name."
800 (defcustom erc-prompt-for-channel-key nil
801 "Prompt for channel key when using `erc-join-channel' interactively."
805 (defcustom erc-email-userid "user"
806 "Use this as your email user ID."
810 (defcustom erc-system-name nil
811 "Use this as the name of your system.
812 If nil, ERC will call `system-name' to get this information."
814 :type '(choice (const :tag "Default system name" nil)
817 (defcustom erc-ignore-list nil
818 "List of regexps matching user identifiers to ignore.
820 A user identifier has the form \"nick!login@host\". If an
821 identifier matches, the message from the person will not be
824 :type '(repeat regexp))
825 (make-variable-buffer-local 'erc-ignore-list)
827 (defcustom erc-ignore-reply-list nil
828 "List of regexps matching user identifiers to ignore completely.
830 This differs from `erc-ignore-list' in that it also ignores any
831 messages directed at the user.
833 A user identifier has the form \"nick!login@host\".
835 If an identifier matches, or a message is addressed to a nick
836 whose identifier matches, the message will not be processed.
838 CAVEAT: ERC doesn't know about the user and host of anyone who
839 was already in the channel when you joined, but never said
840 anything, so it won't be able to match the user and host of those
841 people. You can update the ERC internal info using /WHO *."
843 :type '(repeat regexp))
845 (defvar erc-flood-protect t
846 "If non-nil, flood protection is enabled.
847 Flooding is sending too much information to the server in too
848 short of an interval, which may cause the server to terminate the
851 See `erc-server-flood-margin' for other flood-related parameters.")
855 (defcustom erc-startup-file-list
856 (list (concat erc-user-emacs-directory ".ercrc.el")
857 (concat erc-user-emacs-directory ".ercrc")
858 "~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
859 "List of files to try for a startup script.
860 The first existent and readable one will get executed.
862 If the filename ends with `.el' it is presumed to be an Emacs Lisp
863 script and it gets (load)ed. Otherwise it is treated as a bunch of
864 regular IRC commands."
866 :type '(repeat file))
868 (defcustom erc-script-path nil
869 "List of directories to look for a script in /load command.
870 The script is first searched in the current directory, then in each
871 directory in the list."
873 :type '(repeat directory))
875 (defcustom erc-script-echo t
876 "If non-nil, echo the IRC script commands locally."
880 (defvar erc-last-saved-position nil
881 "A marker containing the position the current buffer was last saved at.")
882 (make-variable-buffer-local 'erc-last-saved-position)
884 (defcustom erc-kill-buffer-on-part nil
885 "Kill the channel buffer on PART.
886 This variable should probably stay nil, as ERC can reuse buffers if
887 you rejoin them later."
888 :group 'erc-quit-and-part
891 (defcustom erc-kill-queries-on-quit nil
892 "Kill all query (also channel) buffers of this server on QUIT.
893 See the variable `erc-kill-buffer-on-part' for details."
894 :group 'erc-quit-and-part
897 (defcustom erc-kill-server-buffer-on-quit nil
898 "Kill the server buffer of the process on QUIT."
899 :group 'erc-quit-and-part
902 (defcustom erc-quit-reason-various-alist nil
903 "Alist of possible arguments to the /quit command.
905 Each element has the form:
908 If REGEXP matches the argument to /quit, then its relevant RESULT
909 will be used. RESULT may be either a string, or a function. If
910 a function, it should return the quit message as a string.
912 If no elements match, then the empty string is used.
915 (setq erc-quit-reason-various-alist
916 '((\"xmms\" dme:now-playing)
917 (\"version\" erc-quit-reason-normal)
918 (\"home\" \"Gone home !\")
919 (\"^$\" \"Default Reason\")))
920 If the user types \"/quit home\", then \"Gone home !\" will be used
921 as the quit message."
922 :group 'erc-quit-and-part
923 :type '(repeat (list regexp (choice (string) (function)))))
925 (defcustom erc-part-reason-various-alist nil
926 "Alist of possible arguments to the /part command.
928 Each element has the form:
931 If REGEXP matches the argument to /part, then its relevant RESULT
932 will be used. RESULT may be either a string, or a function. If
933 a function, it should return the part message as a string.
935 If no elements match, then the empty string is used.
938 (setq erc-part-reason-various-alist
939 '((\"xmms\" dme:now-playing)
940 (\"version\" erc-part-reason-normal)
941 (\"home\" \"Gone home !\")
942 (\"^$\" \"Default Reason\")))
943 If the user types \"/part home\", then \"Gone home !\" will be used
944 as the part message."
945 :group 'erc-quit-and-part
946 :type '(repeat (list regexp (choice (string) (function)))))
948 (defcustom erc-quit-reason 'erc-quit-reason-normal
949 "A function which returns the reason for quitting.
951 The function is passed a single argument, the string typed by the
952 user after \"/quit\"."
953 :group 'erc-quit-and-part
954 :type '(choice (const erc-quit-reason-normal)
955 (const erc-quit-reason-various)
958 (defcustom erc-part-reason 'erc-part-reason-normal
959 "A function which returns the reason for parting a channel.
961 The function is passed a single argument, the string typed by the
962 user after \"/PART\"."
963 :group 'erc-quit-and-part
964 :type '(choice (const erc-part-reason-normal)
965 (const erc-part-reason-various)
968 (defvar erc-grab-buffer-name "*erc-grab*"
969 "The name of the buffer created by `erc-grab-region'.")
971 ;; variables available for IRC scripts
973 (defvar erc-user-information "ERC User"
974 "USER_INFORMATION IRC variable.")
978 (defgroup erc-hooks nil
979 "Hook variables for fancy customizations of ERC."
982 (defcustom erc-mode-hook nil
983 "Hook run after `erc-mode' setup is finished."
986 :options '(erc-add-scroll-to-bottom))
988 (defcustom erc-timer-hook nil
989 "Put functions which should get called more or less periodically here.
990 The idea is that servers always play ping pong with the client, and so there
991 is no need for any idle-timer games with Emacs."
995 (defcustom erc-insert-pre-hook nil
996 "Hook called first when some text is inserted through `erc-display-line'.
997 It gets called with one argument, STRING.
998 To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
999 Filtering functions can set `erc-insert-this' to nil to avoid
1000 display of that particular string at all."
1004 (defcustom erc-send-pre-hook nil
1005 "Hook called first when some text is sent through `erc-send-current-line'.
1006 It gets called with one argument, STRING.
1008 To change the text that will be sent, set the variable STR which is
1009 used in `erc-send-current-line'.
1011 To change the text inserted into the buffer without changing the text
1012 that will be sent, use `erc-send-modify-hook' instead.
1014 Filtering functions can set `erc-send-this' to nil to avoid sending of
1015 that particular string at all and `erc-insert-this' to prevent
1016 inserting that particular string into the buffer.
1018 Note that it's useless to set `erc-send-this' to nil and
1019 `erc-insert-this' to t. ERC is sane enough to not insert the text
1024 (defvar erc-insert-this t
1025 "Insert the text into the target buffer or not.
1026 Functions on `erc-insert-pre-hook' can set this variable to nil
1027 if they wish to avoid insertion of a particular string.")
1029 (defvar erc-send-this t
1030 "Send the text to the target or not.
1031 Functions on `erc-send-pre-hook' can set this variable to nil
1032 if they wish to avoid sending of a particular string.")
1034 (defcustom erc-insert-modify-hook ()
1035 "Insertion hook for functions that will change the text's appearance.
1036 This hook is called just after `erc-insert-pre-hook' when the value
1037 of `erc-insert-this' is t.
1038 While this hook is run, narrowing is in effect and `current-buffer' is
1039 the buffer where the text got inserted. One possible value to add here
1044 (defcustom erc-insert-post-hook nil
1045 "This hook is called just after `erc-insert-modify-hook'.
1046 At this point, all modifications from prior hook functions are done."
1049 :options '(erc-truncate-buffer
1051 erc-save-buffer-in-logs))
1053 (defcustom erc-send-modify-hook nil
1054 "Sending hook for functions that will change the text's appearance.
1055 This hook is called just after `erc-send-pre-hook' when the values
1056 of `erc-send-this' and `erc-insert-this' are both t.
1057 While this hook is run, narrowing is in effect and `current-buffer' is
1058 the buffer where the text got inserted.
1060 Note that no function in this hook can change the appearance of the
1061 text that is sent. Only changing the sent text's appearance on the
1062 sending user's screen is possible. One possible value to add here
1067 (defcustom erc-send-post-hook nil
1068 "This hook is called just after `erc-send-modify-hook'.
1069 At this point, all modifications from prior hook functions are done.
1070 NOTE: The functions on this hook are called _before_ sending a command
1073 This function is called with narrowing, ala `erc-send-modify-hook'."
1076 :options '(erc-make-read-only))
1078 (defcustom erc-send-completed-hook
1079 (when (fboundp 'emacspeak-auditory-icon)
1082 (emacspeak-auditory-icon 'select-object)))))
1083 "Hook called after a message has been parsed by ERC.
1085 The single argument to the functions is the unmodified string
1086 which the local user typed."
1089 ;; mode-specific tables
1091 (defvar erc-mode-syntax-table
1092 (let ((syntax-table (make-syntax-table)))
1093 (modify-syntax-entry ?\" ". " syntax-table)
1094 (modify-syntax-entry ?\\ ". " syntax-table)
1095 (modify-syntax-entry ?' "w " syntax-table)
1096 ;; Make dabbrev-expand useful for nick names
1097 (modify-syntax-entry ?< "." syntax-table)
1098 (modify-syntax-entry ?> "." syntax-table)
1100 "Syntax table used while in ERC mode.")
1102 (defvar erc-mode-abbrev-table nil
1103 "Abbrev table used while in ERC mode.")
1104 (define-abbrev-table 'erc-mode-abbrev-table ())
1106 (defvar erc-mode-map
1107 (let ((map (make-sparse-keymap)))
1108 (define-key map "\C-m" 'erc-send-current-line)
1109 (define-key map "\C-a" 'erc-bol)
1110 (define-key map [home] 'erc-bol)
1111 (define-key map "\C-c\C-a" 'erc-bol)
1112 (define-key map "\C-c\C-b" 'erc-iswitchb)
1113 (define-key map "\C-c\C-c" 'erc-toggle-interpret-controls)
1114 (define-key map "\C-c\C-d" 'erc-input-action)
1115 (define-key map "\C-c\C-e" 'erc-toggle-ctcp-autoresponse)
1116 (define-key map "\C-c\C-f" 'erc-toggle-flood-control)
1117 (define-key map "\C-c\C-i" 'erc-invite-only-mode)
1118 (define-key map "\C-c\C-j" 'erc-join-channel)
1119 (define-key map "\C-c\C-n" 'erc-channel-names)
1120 (define-key map "\C-c\C-o" 'erc-get-channel-mode-from-keypress)
1121 (define-key map "\C-c\C-p" 'erc-part-from-channel)
1122 (define-key map "\C-c\C-q" 'erc-quit-server)
1123 (define-key map "\C-c\C-r" 'erc-remove-text-properties-region)
1124 (define-key map "\C-c\C-t" 'erc-set-topic)
1125 (define-key map "\C-c\C-u" 'erc-kill-input)
1126 (define-key map "\C-c\C-x" 'erc-quit-server)
1127 (define-key map "\M-\t" 'ispell-complete-word)
1128 (define-key map "\t" 'completion-at-point)
1130 ;; Suppress `font-lock-fontify-block' key binding since it
1131 ;; destroys face properties.
1132 (define-key map [remap font-lock-fontify-block] 'undefined)
1139 ; Honestly, I have a horrible sense of color and the "defaults" below
1140 ; are supposed to be really bad. But colors ARE required in IRC to
1141 ; convey different parts of conversation. If you think you know better
1142 ; defaults - send them to me.
1144 ;; Now colors are a bit nicer, at least to my eyes.
1145 ;; You may still want to change them to better fit your background.-- S.B.
1147 (defgroup erc-faces nil
1151 (defface erc-default-face '((t))
1155 (defface erc-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
1156 "ERC face used for user mode prefix."
1159 (defface erc-my-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
1160 "ERC face used for my user mode prefix."
1163 (defface erc-direct-msg-face '((t :foreground "IndianRed"))
1164 "ERC face used for messages you receive in the main erc buffer."
1167 (defface erc-header-line
1168 '((t :foreground "grey20" :background "grey90"))
1169 "ERC face used for the header line.
1171 This will only be used if `erc-header-line-face-method' is non-nil."
1174 (defface erc-input-face '((t :foreground "brown"))
1175 "ERC face used for your input."
1178 (defface erc-prompt-face
1179 '((t :weight bold :foreground "Black" :background "lightBlue2"))
1180 "ERC face for the prompt."
1183 (defface erc-command-indicator-face
1185 "ERC face for the command indicator.
1186 See the variable `erc-command-indicator'."
1189 (defface erc-notice-face
1190 '((default :weight bold)
1191 (((class color) (min-colors 88)) :foreground "SlateBlue")
1192 (t :foreground "blue"))
1193 "ERC face for notices."
1196 (defface erc-action-face '((t :weight bold))
1197 "ERC face for actions generated by /ME."
1200 (defface erc-error-face '((t :foreground "red"))
1201 "ERC face for errors."
1204 ;; same default color as `erc-input-face'
1205 (defface erc-my-nick-face '((t :weight bold :foreground "brown"))
1206 "ERC face for your current nickname in messages sent by you.
1207 See also `erc-show-my-nick'."
1210 (defface erc-nick-default-face '((t :weight bold))
1211 "ERC nickname default face."
1214 (defface erc-nick-msg-face '((t :weight bold :foreground "IndianRed"))
1215 "ERC nickname face for private messages."
1218 ;; Debugging support
1220 (defvar erc-log-p nil
1221 "When set to t, generate debug messages in a separate debug buffer.")
1223 (defvar erc-debug-log-file (expand-file-name "ERC.debug")
1224 "Debug log file name.")
1226 (defvar erc-dbuf nil)
1227 (make-variable-buffer-local 'erc-dbuf)
1229 (defmacro define-erc-module (name alias doc enable-body disable-body
1231 "Define a new minor mode using ERC conventions.
1232 Symbol NAME is the name of the module.
1233 Symbol ALIAS is the alias to use, or nil.
1234 DOC is the documentation string to use for the minor mode.
1235 ENABLE-BODY is a list of expressions used to enable the mode.
1236 DISABLE-BODY is a list of expressions used to disable the mode.
1237 If LOCAL-P is non-nil, the mode will be created as a buffer-local
1238 mode, rather than a global one.
1240 This will define a minor mode called erc-NAME-mode, possibly
1241 an alias erc-ALIAS-mode, as well as the helper functions
1242 erc-NAME-enable, and erc-NAME-disable.
1246 ;;;###autoload (autoload 'erc-replace-mode \"erc-replace\")
1247 (define-erc-module replace nil
1248 \"This mode replaces incoming text according to `erc-replace-alist'.\"
1249 ((add-hook 'erc-insert-modify-hook
1250 'erc-replace-insert))
1251 ((remove-hook 'erc-insert-modify-hook
1252 'erc-replace-insert)))"
1253 (declare (doc-string 3))
1254 (let* ((sn (symbol-name name))
1255 (mode (intern (format "erc-%s-mode" (downcase sn))))
1256 (group (intern (format "erc-%s" (downcase sn))))
1257 (enable (intern (format "erc-%s-enable" (downcase sn))))
1258 (disable (intern (format "erc-%s-disable" (downcase sn)))))
1260 (erc-define-minor-mode
1262 ,(format "Toggle ERC %S mode.
1263 With a prefix argument ARG, enable %s if ARG is positive,
1264 and disable it otherwise. If called from Lisp, enable the mode
1265 if ARG is omitted or nil.
1268 :global ,(not local-p) :group (quote ,group)
1273 ,(format "Enable ERC %S mode."
1276 (add-to-list 'erc-modules (quote ,name))
1280 ,(format "Disable ERC %S mode."
1283 (setq erc-modules (delq (quote ,name) erc-modules))
1286 ,(when (and alias (not (eq name alias)))
1290 (format "erc-%s-mode"
1291 (downcase (symbol-name alias)))))
1294 ;; For find-function and find-variable.
1295 (put ',mode 'definition-name ',name)
1296 (put ',enable 'definition-name ',name)
1297 (put ',disable 'definition-name ',name))))
1299 (defun erc-once-with-server-event (event f)
1300 "Run function F the next time EVENT occurs in the `current-buffer'.
1302 You should make sure that `current-buffer' is a server buffer.
1304 This function temporarily adds a function to EVENT's hook to call F with
1305 two arguments (`proc' and `parsed'). After F is called, the function is
1306 removed from EVENT's hook. F should return either nil
1307 or t, where nil indicates that the other functions on EVENT's hook
1308 should be run too, and t indicates that other functions should
1311 Please be sure to use this function in server-buffers. In
1312 channel-buffers it may not work at all, as it uses the LOCAL
1313 argument of `add-hook' and `remove-hook' to ensure multiserver
1315 (unless (erc-server-buffer-p)
1317 "You should only run `erc-once-with-server-event' in a server buffer"))
1318 (let ((fun (make-symbol "fun"))
1319 (hook (erc-get-hook event)))
1320 (put fun 'erc-original-buffer (current-buffer))
1321 (fset fun (lambda (proc parsed)
1322 (with-current-buffer (get fun 'erc-original-buffer)
1323 (remove-hook hook fun t))
1325 (funcall f proc parsed)))
1326 (add-hook hook fun nil t)
1329 (defsubst erc-log (string)
1330 "Logs STRING if logging is on (see `erc-log-p')."
1332 (erc-log-aux string)))
1334 (defun erc-server-buffer ()
1335 "Return the server buffer for the current buffer's process.
1336 The buffer-local variable `erc-server-process' is used to find
1337 the process buffer."
1338 (and (erc-server-buffer-live-p)
1339 (process-buffer erc-server-process)))
1341 (defun erc-server-buffer-live-p ()
1342 "Return t if the server buffer has not been killed."
1343 (and (processp erc-server-process)
1344 (buffer-live-p (process-buffer erc-server-process))))
1346 (defun erc-server-buffer-p (&optional buffer)
1347 "Return non-nil if argument BUFFER is an ERC server buffer.
1349 If BUFFER is nil, the current buffer is used."
1350 (with-current-buffer (or buffer (current-buffer))
1351 (and (eq major-mode 'erc-mode)
1352 (null (erc-default-target)))))
1354 (defun erc-open-server-buffer-p (&optional buffer)
1355 "Return non-nil if argument BUFFER is an ERC server buffer that
1356 has an open IRC process.
1358 If BUFFER is nil, the current buffer is used."
1359 (and (erc-server-buffer-p buffer)
1360 (erc-server-process-alive buffer)))
1362 (defun erc-query-buffer-p (&optional buffer)
1363 "Return non-nil if BUFFER is an ERC query buffer.
1364 If BUFFER is nil, the current buffer is used."
1365 (with-current-buffer (or buffer (current-buffer))
1366 (let ((target (erc-default-target)))
1367 (and (eq major-mode 'erc-mode)
1369 (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
1371 (defun erc-ison-p (nick)
1372 "Return non-nil if NICK is online."
1373 (interactive "sNick: ")
1374 (erc-with-server-buffer
1375 (let ((erc-online-p 'unknown))
1376 (erc-once-with-server-event
1378 (lambda (_proc parsed)
1379 (let ((ison (split-string (aref parsed 3))))
1380 (setq erc-online-p (car (erc-member-ignore-case nick ison)))
1382 (erc-server-send (format "ISON %s" nick))
1383 (while (eq erc-online-p 'unknown) (accept-process-output))
1384 (if (called-interactively-p 'interactive)
1385 (message "%s is %sonline"
1386 (or erc-online-p nick)
1387 (if erc-online-p "" "not "))
1390 (defun erc-log-aux (string)
1391 "Do the debug logging of STRING."
1392 (let ((cb (current-buffer))
1395 (session-buffer (erc-server-buffer)))
1398 (set-buffer session-buffer)
1399 (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
1401 (setq erc-dbuf (get-buffer-create
1402 (concat "*ERC-DEBUG: "
1403 erc-session-server "*")))))
1404 (set-buffer erc-dbuf)
1405 (setq point (point))
1406 (setq was-eob (eobp))
1407 (goto-char (point-max))
1408 (insert (concat "** " string "\n"))
1409 (if was-eob (goto-char (point-max))
1412 (message "ERC: ** %s" string))))
1414 ;; Last active buffer, to print server messages in the right place
1416 (defvar erc-active-buffer nil
1417 "The current active buffer, the one where the user typed the last command.
1418 Defaults to the server buffer, and should only be set in the
1420 (make-variable-buffer-local 'erc-active-buffer)
1422 (defun erc-active-buffer ()
1423 "Return the value of `erc-active-buffer' for the current server.
1424 Defaults to the server buffer."
1425 (erc-with-server-buffer
1426 (if (buffer-live-p erc-active-buffer)
1428 (setq erc-active-buffer (current-buffer)))))
1430 (defun erc-set-active-buffer (buffer)
1431 "Set the value of `erc-active-buffer' to BUFFER."
1432 (cond ((erc-server-buffer)
1433 (with-current-buffer (erc-server-buffer)
1434 (setq erc-active-buffer buffer)))
1435 (t (setq erc-active-buffer buffer))))
1437 ;; Mode activation routines
1439 (define-derived-mode erc-mode fundamental-mode "ERC"
1440 "Major mode for Emacs IRC."
1441 (setq local-abbrev-table erc-mode-abbrev-table)
1442 (when (boundp 'next-line-add-newlines)
1443 (set (make-local-variable 'next-line-add-newlines) nil))
1444 (setq line-move-ignore-invisible t)
1445 (set (make-local-variable 'paragraph-separate)
1446 (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
1447 (set (make-local-variable 'paragraph-start)
1448 (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
1449 (add-hook 'completion-at-point-functions 'erc-complete-word-at-point nil t))
1453 (defconst erc-default-server "irc.freenode.net"
1454 "IRC server to use if it cannot be detected otherwise.")
1456 (defconst erc-default-port 6667
1457 "IRC port to use if it cannot be detected otherwise.")
1459 (defcustom erc-join-buffer 'buffer
1460 "Determines how to display a newly created IRC buffer.
1462 The available choices are:
1464 'window - in another window,
1465 'window-noselect - in another window, but don't select that one,
1466 'frame - in another frame,
1467 'bury - bury it in a new buffer,
1468 'buffer - in place of the current buffer,
1469 any other value - in place of the current buffer."
1471 :type '(choice (const :tag "Split window and select" window)
1472 (const :tag "Split window, don't select" window-noselect)
1473 (const :tag "New frame" frame)
1474 (const :tag "Bury in new buffer" bury)
1475 (const :tag "Use current buffer" buffer)
1476 (const :tag "Use current buffer" t)))
1478 (defcustom erc-frame-alist nil
1479 "Alist of frame parameters for creating erc frames.
1480 A value of nil means to use `default-frame-alist'."
1482 :type '(repeat (cons :format "%v"
1483 (symbol :tag "Parameter")
1484 (sexp :tag "Value"))))
1486 (defcustom erc-frame-dedicated-flag nil
1487 "Non-nil means the erc frames are dedicated to that buffer.
1488 This only has effect when `erc-join-buffer' is set to `frame'."
1492 (defcustom erc-reuse-frames t
1493 "Determines whether new frames are always created.
1494 Non-nil means that a new frame is not created to display an ERC
1495 buffer if there is already a window displaying it. This only has
1496 effect when `erc-join-buffer' is set to `frame'."
1500 (defun erc-channel-p (channel)
1501 "Return non-nil if CHANNEL seems to be an IRC channel name."
1502 (cond ((stringp channel)
1503 (memq (aref channel 0) '(?# ?& ?+ ?!)))
1504 ((and (bufferp channel) (buffer-live-p channel))
1505 (with-current-buffer channel
1506 (erc-channel-p (erc-default-target))))
1509 (defcustom erc-reuse-buffers t
1510 "If nil, create new buffers on joining a channel/query.
1511 If non-nil, a new buffer will only be created when you join
1512 channels with same names on different servers, or have query buffers
1513 open with nicks of the same name on different servers. Otherwise,
1514 the existing buffers will be reused."
1518 (defun erc-normalize-port (port)
1519 "Normalize the port specification PORT to integer form.
1520 PORT may be an integer, a string or a symbol. If it is a string or a
1521 symbol, it may have these values:
1525 * ircd-dalnet -> 7000"
1528 (erc-normalize-port (symbol-name port)))
1530 (let ((port-nr (string-to-number port)))
1534 ((string-equal port "irc")
1536 ((string-equal port "ircs")
1538 ((string-equal port "ircd")
1540 ((string-equal port "ircd-dalnet")
1549 (defun erc-port-equal (a b)
1550 "Check whether ports A and B are equal."
1551 (= (erc-normalize-port a) (erc-normalize-port b)))
1553 (defun erc-generate-new-buffer-name (server port target)
1554 "Create a new buffer name based on the arguments."
1555 (when (numberp port) (setq port (number-to-string port)))
1556 (let ((buf-name (or target
1557 (or (let ((name (concat server ":" port)))
1558 (when (> (length name) 1)
1560 ;; This fallback should in fact never happen
1561 "*erc-server-buffer*")))
1563 ;; Reuse existing buffers, but not if the buffer is a connected server
1564 ;; buffer and not if its associated with a different server than the
1565 ;; current ERC buffer.
1566 ;; if buf-name is taken by a different connection (or by something !erc)
1567 ;; then see if "buf-name/server" meets the same criteria
1568 (dolist (candidate (list buf-name (concat buf-name "/" server)))
1569 (if (and (not buffer-name)
1571 (get-buffer candidate)
1573 (with-current-buffer (get-buffer candidate)
1574 (and (erc-server-buffer-p)
1575 (not (erc-server-process-alive)))))
1576 (with-current-buffer (get-buffer candidate)
1577 (and (string= erc-session-server server)
1578 (erc-port-equal erc-session-port port))))
1579 (setq buffer-name candidate)))
1580 ;; if buffer-name is unset, neither candidate worked out for us,
1581 ;; fallback to the old <N> uniquification method:
1582 (or buffer-name (generate-new-buffer-name buf-name)) ))
1584 (defun erc-get-buffer-create (server port target)
1585 "Create a new buffer based on the arguments."
1586 (get-buffer-create (erc-generate-new-buffer-name server port target)))
1589 (defun erc-member-ignore-case (string list)
1590 "Return non-nil if STRING is a member of LIST.
1592 All strings are compared according to IRC protocol case rules, see
1594 (setq string (erc-downcase string))
1597 (if (string= string (erc-downcase (car list)))
1598 (throw 'result list)
1599 (setq list (cdr list))))))
1601 (defmacro erc-with-buffer (spec &rest body)
1602 "Execute BODY in the buffer associated with SPEC.
1604 SPEC should have the form
1608 If TARGET is a buffer, use it. Otherwise, use the buffer
1609 matching TARGET in the process specified by PROCESS.
1611 If PROCESS is nil, use the current `erc-server-process'.
1612 See `erc-get-buffer' for details.
1614 See also `with-current-buffer'.
1616 \(fn (TARGET [PROCESS]) BODY...)"
1617 (declare (indent 1) (debug ((form &optional form) body)))
1618 (let ((buf (make-symbol "buf"))
1619 (proc (make-symbol "proc"))
1620 (target (make-symbol "target"))
1621 (process (make-symbol "process")))
1622 `(let* ((,target ,(car spec))
1623 (,process ,(cadr spec))
1624 (,buf (if (bufferp ,target)
1626 (let ((,proc (or ,process
1627 (and (processp erc-server-process)
1628 erc-server-process))))
1629 (if (and ,target ,proc)
1630 (erc-get-buffer ,target ,proc))))))
1631 (when (buffer-live-p ,buf)
1632 (with-current-buffer ,buf
1635 (defun erc-get-buffer (target &optional proc)
1636 "Return the buffer matching TARGET in the process PROC.
1637 If PROC is not supplied, all processes are searched."
1638 (let ((downcased-target (erc-downcase target)))
1642 (let ((current (erc-default-target)))
1643 (and (stringp current)
1644 (string-equal downcased-target (erc-downcase current))
1645 (throw 'buffer (current-buffer)))))
1648 (defun erc-buffer-filter (predicate &optional proc)
1649 "Return a list of `erc-mode' buffers matching certain criteria.
1650 PREDICATE is a function executed with each buffer, if it returns t, that buffer
1651 is considered a valid match.
1653 PROC is either an `erc-server-process', identifying a certain
1654 server connection, or nil which means all open connections."
1658 (mapcar (lambda (buf)
1659 (when (buffer-live-p buf)
1660 (with-current-buffer buf
1661 (and (eq major-mode 'erc-mode)
1663 (eq proc erc-server-process))
1668 (defun erc-buffer-list (&optional predicate proc)
1669 "Return a list of ERC buffers.
1670 PREDICATE is a function which executes with every buffer satisfying
1671 the predicate. If PREDICATE is passed as nil, return a list of all ERC
1672 buffers. If PROC is given, the buffers local variable `erc-server-process'
1673 needs to match PROC."
1675 (setq predicate (lambda () t)))
1676 (erc-buffer-filter predicate proc))
1678 (defmacro erc-with-all-buffers-of-server (process pred &rest forms)
1679 "Execute FORMS in all buffers which have same process as this server.
1680 FORMS will be evaluated in all buffers having the process PROCESS and
1681 where PRED matches or in all buffers of the server process if PRED is
1683 (declare (indent 1) (debug (form form body)))
1684 ;; Make the evaluation have the correct order
1685 (let ((pre (make-symbol "pre"))
1686 (pro (make-symbol "pro")))
1687 `(let* ((,pro ,process)
1689 (res (mapcar (lambda (buffer)
1690 (with-current-buffer buffer
1692 (erc-buffer-list ,pre
1694 ;; Silence the byte-compiler by binding the result of mapcar to
1698 ;; (iswitchb-mode) will autoload iswitchb.el
1699 (defvar iswitchb-temp-buflist)
1700 (declare-function iswitchb-read-buffer "iswitchb"
1701 (prompt &optional default require-match start matches-set))
1702 (defvar iswitchb-make-buflist-hook)
1704 (defun erc-iswitchb (&optional arg)
1705 "Use `iswitchb-read-buffer' to prompt for a ERC buffer to switch to.
1706 When invoked with prefix argument, use all erc buffers. Without prefix
1707 ARG, allow only buffers related to same session server.
1708 If `erc-track-mode' is in enabled, put the last element of
1709 `erc-modified-channels-alist' in front of the buffer list.
1711 Due to some yet unresolved reason, global function `iswitchb-mode'
1712 needs to be active for this function to work."
1714 (let ((enabled (bound-and-true-p iswitchb-mode)))
1715 (or enabled (iswitchb-mode 1))
1717 (let ((iswitchb-make-buflist-hook
1719 (setq iswitchb-temp-buflist
1720 (mapcar 'buffer-name
1723 (when arg erc-server-process)))))))
1725 (iswitchb-read-buffer
1727 (if (boundp 'erc-modified-channels-alist)
1728 (buffer-name (caar (last erc-modified-channels-alist)))
1731 (or enabled (iswitchb-mode -1)))))
1733 (defun erc-channel-list (proc)
1734 "Return a list of channel buffers.
1735 PROC is the process for the server connection. If PROC is nil, return
1736 all channel buffers on all servers."
1739 (and (erc-default-target)
1740 (erc-channel-p (erc-default-target))))
1743 (defun erc-buffer-list-with-nick (nick proc)
1744 "Return buffers containing NICK in the `erc-channel-users' list."
1745 (with-current-buffer (process-buffer proc)
1746 (let ((user (gethash (erc-downcase nick) erc-server-users)))
1748 (erc-server-user-buffers user)
1751 ;; Some local variables
1753 (defvar erc-default-recipients nil
1754 "List of default recipients of the current buffer.")
1755 (make-variable-buffer-local 'erc-default-recipients)
1757 (defvar erc-session-user-full-name nil
1758 "Full name of the user on the current server.")
1759 (make-variable-buffer-local 'erc-session-user-full-name)
1761 (defvar erc-channel-user-limit nil
1762 "Limit of users per channel.")
1763 (make-variable-buffer-local 'erc-channel-user-limit)
1765 (defvar erc-channel-key nil
1766 "Key needed to join channel.")
1767 (make-variable-buffer-local 'erc-channel-key)
1769 (defvar erc-invitation nil
1770 "Last invitation channel.")
1771 (make-variable-buffer-local 'erc-invitation)
1773 (defvar erc-away nil
1774 "Non-nil indicates that we are away.
1776 Use `erc-away-time' to access this if you might be in a channel
1777 buffer rather than a server buffer.")
1778 (make-variable-buffer-local 'erc-away)
1780 (defvar erc-channel-list nil
1781 "Server channel list.")
1782 (make-variable-buffer-local 'erc-channel-list)
1784 (defvar erc-bad-nick nil
1785 "Non-nil indicates that we got a `nick in use' error while connecting.")
1786 (make-variable-buffer-local 'erc-bad-nick)
1788 (defvar erc-logged-in nil
1789 "Non-nil indicates that we are logged in.")
1790 (make-variable-buffer-local 'erc-logged-in)
1792 (defvar erc-default-nicks nil
1793 "The local copy of `erc-nick' - the list of nicks to choose from.")
1794 (make-variable-buffer-local 'erc-default-nicks)
1796 (defvar erc-nick-change-attempt-count 0
1797 "Used to keep track of how many times an attempt at changing nick is made.")
1798 (make-variable-buffer-local 'erc-nick-change-attempt-count)
1800 (defun erc-migrate-modules (mods)
1801 "Migrate old names of ERC modules to new ones."
1802 ;; modify `transforms' to specify what needs to be changed
1803 ;; each item is in the format '(old . new)
1804 (let ((transforms '((pcomplete . completion))))
1806 (mapcar (lambda (m) (or (cdr (assoc m transforms)) m))
1809 (defcustom erc-modules '(netsplit fill button match track completion readonly
1810 networks ring autojoin noncommands irccontrols
1811 move-to-prompt stamp menu list)
1812 "A list of modules which ERC should enable.
1813 If you set the value of this without using `customize' remember to call
1814 \(erc-update-modules) after you change it. When using `customize', modules
1815 removed from the list will be disabled."
1817 ;; replace outdated names with their newer equivalents
1818 (erc-migrate-modules (symbol-value sym)))
1819 :set (lambda (sym val)
1820 ;; disable modules which have just been removed
1821 (when (and (boundp 'erc-modules) erc-modules val)
1822 (dolist (module erc-modules)
1823 (unless (member module val)
1824 (let ((f (intern-soft (format "erc-%s-mode" module))))
1825 (when (and (fboundp f) (boundp f) (symbol-value f))
1826 (message "Disabling `erc-%s'" module)
1829 ;; this test is for the case where erc hasn't been loaded yet
1830 (when (fboundp 'erc-update-modules)
1831 (erc-update-modules)))
1835 (const :tag "autoaway: Set away status automatically" autoaway)
1836 (const :tag "autojoin: Join channels automatically" autojoin)
1837 (const :tag "button: Buttonize URLs, nicknames, and other text" button)
1838 (const :tag "capab: Mark unidentified users on servers supporting CAPAB"
1840 (const :tag "completion: Complete nicknames and commands (programmable)"
1842 (const :tag "hecomplete: Complete nicknames and commands (obsolete, use \"completion\")" hecomplete)
1843 (const :tag "dcc: Provide Direct Client-to-Client support" dcc)
1844 (const :tag "fill: Wrap long lines" fill)
1845 (const :tag "identd: Launch an identd server on port 8113" identd)
1846 (const :tag "irccontrols: Highlight or remove IRC control characters"
1848 (const :tag "keep-place: Leave point above un-viewed text" keep-place)
1849 (const :tag "list: List channels in a separate buffer" list)
1850 (const :tag "log: Save buffers in logs" log)
1851 (const :tag "match: Highlight pals, fools, and other keywords" match)
1852 (const :tag "menu: Display a menu in ERC buffers" menu)
1853 (const :tag "move-to-prompt: Move to the prompt when typing text"
1855 (const :tag "netsplit: Detect netsplits" netsplit)
1856 (const :tag "networks: Provide data about IRC networks" networks)
1857 (const :tag "noncommands: Don't display non-IRC commands after evaluation"
1860 "notify: Notify when the online status of certain users changes"
1862 (const :tag "notifications: Send notifications on PRIVMSG or nickname mentions"
1864 (const :tag "page: Process CTCP PAGE requests from IRC" page)
1865 (const :tag "readonly: Make displayed lines read-only" readonly)
1866 (const :tag "replace: Replace text in messages" replace)
1867 (const :tag "ring: Enable an input history" ring)
1868 (const :tag "scrolltobottom: Scroll to the bottom of the buffer"
1870 (const :tag "services: Identify to Nickserv (IRC Services) automatically"
1872 (const :tag "smiley: Convert smileys to pretty icons" smiley)
1873 (const :tag "sound: Play sounds when you receive CTCP SOUND requests"
1875 (const :tag "stamp: Add timestamps to messages" stamp)
1876 (const :tag "spelling: Check spelling" spelling)
1877 (const :tag "track: Track channel activity in the mode-line" track)
1878 (const :tag "truncate: Truncate buffers to a certain size" truncate)
1879 (const :tag "unmorse: Translate morse code in messages" unmorse)
1880 (const :tag "xdcc: Act as an XDCC file-server" xdcc)
1881 (repeat :tag "Others" :inline t symbol))
1884 (defun erc-update-modules ()
1885 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1887 (dolist (mod erc-modules)
1888 (setq req (concat "erc-" (symbol-name mod)))
1890 ;; yuck. perhaps we should bring the filenames into sync?
1891 ((string= req "erc-capab-identify")
1892 (setq req "erc-capab"))
1893 ((string= req "erc-completion")
1894 (setq req "erc-pcomplete"))
1895 ((string= req "erc-pcomplete")
1896 (setq mod 'completion))
1897 ((string= req "erc-autojoin")
1898 (setq req "erc-join")))
1900 (require (intern req))
1902 (let ((sym (intern-soft (concat "erc-" (symbol-name mod) "-mode"))))
1905 (error "`%s' is not a known ERC module" mod))))))
1907 (defun erc-setup-buffer (buffer)
1908 "Consults `erc-join-buffer' to find out how to display `BUFFER'."
1909 (pcase erc-join-buffer
1911 (if (active-minibuffer-window)
1912 (display-buffer buffer)
1913 (switch-to-buffer-other-window buffer)))
1915 (display-buffer buffer))
1919 (when (or (not erc-reuse-frames)
1920 (not (get-buffer-window buffer t)))
1921 (let ((frame (make-frame (or erc-frame-alist
1922 default-frame-alist))))
1924 (select-frame frame))
1925 (switch-to-buffer buffer)
1926 (when erc-frame-dedicated-flag
1927 (set-window-dedicated-p (selected-window) t))))
1929 (if (active-minibuffer-window)
1930 (display-buffer buffer)
1931 (switch-to-buffer buffer)))))
1933 (defun erc-open (&optional server port nick full-name
1934 connect passwd tgt-list channel process)
1935 "Connect to SERVER on PORT as NICK with FULL-NAME.
1937 If CONNECT is non-nil, connect to the server. Otherwise assume
1938 already connected and just create a separate buffer for the new
1941 Use PASSWD as user password on the server. If TGT-LIST is
1942 non-nil, use it to initialize `erc-default-recipients'.
1944 Returns the buffer for the given server or channel."
1945 (let ((server-announced-name (when (and (boundp 'erc-session-server)
1946 (string= server erc-session-server))
1947 erc-server-announced-name))
1948 (connected-p (unless connect erc-server-connected))
1949 (buffer (erc-get-buffer-create server port channel))
1950 (old-buffer (current-buffer))
1953 (when connect (run-hook-with-args 'erc-before-connect server port nick))
1954 (erc-update-modules)
1956 (setq old-point (point))
1958 (setq erc-server-announced-name server-announced-name)
1959 (setq erc-server-connected connected-p)
1960 ;; connection parameters
1961 (setq erc-server-process process)
1962 (setq erc-insert-marker (make-marker))
1963 (setq erc-input-marker (make-marker))
1964 ;; go to the end of the buffer and open a new line
1965 ;; (the buffer may have existed)
1966 (goto-char (point-max))
1968 (when (get-text-property (point) 'erc-prompt)
1969 (setq continued-session t)
1970 (set-marker erc-input-marker
1971 (or (next-single-property-change (point) 'erc-prompt)
1973 (unless continued-session
1974 (goto-char (point-max))
1976 (set-marker erc-insert-marker (point))
1977 ;; stack of default recipients
1978 (setq erc-default-recipients tgt-list)
1979 (setq erc-server-current-nick nil)
1980 ;; Initialize erc-server-users and erc-channel-users
1982 (progn ;; server buffer
1983 (setq erc-server-users
1984 (make-hash-table :test 'equal))
1985 (setq erc-channel-users nil))
1986 (progn ;; target buffer
1987 (setq erc-server-users nil)
1988 (setq erc-channel-users
1989 (make-hash-table :test 'equal))))
1990 ;; clear last incomplete line read
1991 (setq erc-server-filter-data nil)
1992 (setq erc-channel-topic "")
1993 ;; limit on the number of users on the channel (mode +l)
1994 (setq erc-channel-user-limit nil)
1995 (setq erc-channel-key nil)
1996 ;; last active buffer, defaults to this one
1997 (erc-set-active-buffer buffer)
1998 ;; last invitation channel
1999 (setq erc-invitation nil)
2000 ;; Server channel list
2001 (setq erc-channel-list ())
2002 ;; login-time 'nick in use' error
2003 (setq erc-bad-nick nil)
2004 ;; whether we have logged in
2005 (setq erc-logged-in nil)
2006 ;; The local copy of `erc-nick' - the list of nicks to choose
2007 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
2009 (setq erc-session-password
2014 (auth-source-search :host server
2018 :require '(:secret)))
2020 (if (functionp secret)
2023 ;; debug output buffer
2026 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
2028 (unless continued-session
2029 (goto-char (point-max))
2031 (if continued-session
2032 (goto-char old-point)
2033 (set-marker erc-insert-marker (point))
2034 (erc-display-prompt)
2035 (goto-char (point-max)))
2037 (erc-determine-parameters server port nick full-name)
2039 ;; Saving log file on exit
2040 (run-hook-with-args 'erc-connect-pre-hook buffer)
2043 (erc-server-connect erc-session-server erc-session-port buffer))
2044 (erc-update-mode-line)
2046 ;; Now display the buffer in a window as per user wishes.
2047 (unless (eq buffer old-buffer)
2049 ;; we can't log to debug buffer, it may not exist yet
2050 (message "erc: old buffer %s, switching to %s"
2052 (erc-setup-buffer buffer))
2056 (defun erc-initialize-log-marker (buffer)
2057 "Initialize the `erc-last-saved-position' marker to a sensible position.
2058 BUFFER is the current buffer."
2059 (with-current-buffer buffer
2060 (setq erc-last-saved-position (make-marker))
2061 (move-marker erc-last-saved-position
2062 (1- (marker-position erc-insert-marker)))))
2064 ;; interactive startup
2066 (defvar erc-server-history-list nil
2067 "IRC server interactive selection history list.")
2069 (defvar erc-nick-history-list nil
2070 "Nickname interactive selection history list.")
2072 (defun erc-already-logged-in (server port nick)
2073 "Return the buffers corresponding to a NICK on PORT of a session SERVER.
2074 This is determined by looking for the appropriate buffer and checking
2075 whether the connection is still alive.
2076 If no buffer matches, return nil."
2079 (and (erc-server-process-alive)
2080 (string= erc-session-server server)
2081 (erc-port-equal erc-session-port port)
2082 (erc-current-nick-p nick)))))
2084 (defcustom erc-before-connect nil
2085 "Hook called before connecting to a server.
2086 This hook gets executed before `erc' actually invokes `erc-mode'
2087 with your input data. The functions in here get called with three
2088 parameters, SERVER, PORT and NICK."
2092 (defcustom erc-after-connect nil
2093 "Hook called after connecting to a server.
2094 This hook gets executed when an end of MOTD has been received. All
2095 functions in here get called with the parameters SERVER and NICK."
2100 (defun erc-select-read-args ()
2101 "Prompt the user for values of nick, server, port, and password."
2102 (let (user-input server port nick passwd)
2103 (setq user-input (read-from-minibuffer
2105 (erc-compute-server) nil nil 'erc-server-history-list))
2107 (if (string-match "\\(.*\\):\\(.*\\)\\'" user-input)
2108 (setq port (erc-string-to-port (match-string 2 user-input))
2109 user-input (match-string 1 user-input))
2111 (erc-string-to-port (read-from-minibuffer
2112 "IRC port: " (erc-port-to-string
2113 (erc-compute-port))))))
2115 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
2116 (setq nick (match-string 1 user-input)
2117 user-input (match-string 2 user-input))
2119 (if (erc-already-logged-in server port nick)
2120 (read-from-minibuffer
2121 (erc-format-message 'nick-in-use ?n nick)
2123 nil nil 'erc-nick-history-list)
2124 (read-from-minibuffer
2125 "Nickname: " (erc-compute-nick nick)
2126 nil nil 'erc-nick-history-list))))
2128 (setq server user-input)
2130 (setq passwd (if erc-prompt-for-password
2131 (if (and erc-password
2132 (y-or-n-p "Use the default password? "))
2134 (read-passwd "Password: "))
2136 (when (and passwd (string= "" passwd))
2139 (while (erc-already-logged-in server port nick)
2140 ;; hmm, this is a problem when using multiple connections to a bnc
2141 ;; with the same nick. Currently this code prevents using more than one
2142 ;; bnc with the same nick. actually it would be nice to have
2143 ;; bncs transparent, so that erc-compute-buffer-name displays
2144 ;; the server one is connected to.
2145 (setq nick (read-from-minibuffer
2146 (erc-format-message 'nick-in-use ?n nick)
2148 nil nil 'erc-nick-history-list)))
2149 (list :server server :port port :nick nick :password passwd)))
2152 (cl-defun erc (&key (server (erc-compute-server))
2153 (port (erc-compute-port))
2154 (nick (erc-compute-nick))
2156 (full-name (erc-compute-full-name)))
2157 "ERC is a powerful, modular, and extensible IRC client.
2158 This function is the main entry point for ERC.
2160 It permits you to select connection parameters, and then starts ERC.
2162 Non-interactively, it takes the keyword arguments
2163 (server (erc-compute-server))
2164 (port (erc-compute-port))
2165 (nick (erc-compute-nick))
2167 (full-name (erc-compute-full-name)))
2169 That is, if called with
2171 (erc :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
2173 then the server and full-name will be set to those values, whereas
2174 `erc-compute-port', `erc-compute-nick' and `erc-compute-full-name' will
2175 be invoked for the values of the other parameters."
2176 (interactive (erc-select-read-args))
2177 (erc-open server port nick full-name t password))
2180 (defalias 'erc-select 'erc)
2181 (defalias 'erc-ssl 'erc-tls)
2184 (defun erc-tls (&rest r)
2185 "Interactively select TLS connection parameters and run ERC.
2186 Arguments are the same as for `erc'."
2187 (interactive (erc-select-read-args))
2188 (let ((erc-server-connect-function 'erc-open-tls-stream))
2191 (defun erc-open-tls-stream (name buffer host port)
2192 "Open an TLS stream to an IRC server.
2193 The process will be given the name NAME, its target buffer will be
2194 BUFFER. HOST and PORT specify the connection target."
2195 (open-network-stream name buffer host port
2198 ;;; Displaying error messages
2200 (defun erc-error (&rest args)
2201 "Pass ARGS to `format', and display the result as an error message.
2202 If `debug-on-error' is set to non-nil, then throw a real error with this
2203 message instead, to make debugging easier."
2205 (apply #'error args)
2206 (apply #'message args)
2209 ;;; Debugging the protocol
2211 (defvar erc-debug-irc-protocol nil
2212 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2214 The buffer is created if it doesn't exist.
2216 NOTE: If this variable is non-nil, and you kill the only
2217 visible \"*erc-protocol*\" buffer, it will be recreated shortly,
2218 but you won't see it.
2220 WARNING: Do not set this variable directly! Instead, use the
2221 function `erc-toggle-debug-irc-protocol' to toggle its value.")
2223 (declare-function erc-network-name "erc-networks" ())
2225 (defun erc-log-irc-protocol (string &optional outbound)
2226 "Append STRING to the buffer *erc-protocol*.
2228 This only has any effect if `erc-debug-irc-protocol' is non-nil.
2230 The buffer is created if it doesn't exist.
2232 If OUTBOUND is non-nil, STRING is being sent to the IRC server
2233 and appears in face `erc-input-face' in the buffer."
2234 (when erc-debug-irc-protocol
2235 (let ((network-name (or (ignore-errors (erc-network-name))
2237 (with-current-buffer (get-buffer-create "*erc-protocol*")
2239 (goto-char (point-max))
2240 (let ((inhibit-read-only t))
2241 (insert (if (not outbound)
2242 ;; Cope with the fact that string might
2243 ;; contain multiple lines of text.
2244 (let ((lines (delete "" (split-string string
2247 (dolist (line lines)
2248 (setq result (concat result network-name
2252 (concat network-name " >> " string
2255 (1- (length string))))
2257 'face 'erc-input-face)))))
2258 (let ((orig-win (selected-window))
2259 (debug-buffer-window (get-buffer-window (current-buffer) t)))
2260 (when debug-buffer-window
2261 (select-window debug-buffer-window)
2262 (when (= 1 (count-lines (point) (point-max)))
2263 (goto-char (point-max))
2265 (select-window orig-win)))))))
2267 (defun erc-toggle-debug-irc-protocol (&optional arg)
2268 "Toggle the value of `erc-debug-irc-protocol'.
2270 If ARG is non-nil, show the *erc-protocol* buffer."
2272 (let* ((buf (get-buffer-create "*erc-protocol*")))
2273 (with-current-buffer buf
2274 (erc-view-mode-enter)
2275 (when (null (current-local-map))
2276 (let ((inhibit-read-only t))
2277 (insert (erc-make-notice "This buffer displays all IRC protocol traffic exchanged with each server.\n"))
2278 (insert (erc-make-notice "Kill this buffer to terminate protocol logging.\n\n")))
2279 (use-local-map (make-sparse-keymap))
2280 (local-set-key (kbd "t") 'erc-toggle-debug-irc-protocol))
2281 (add-hook 'kill-buffer-hook
2282 #'(lambda () (setq erc-debug-irc-protocol nil))
2284 (goto-char (point-max))
2285 (let ((inhibit-read-only t))
2286 (insert (erc-make-notice
2287 (format "IRC protocol logging %s at %s -- Press `t' to toggle logging.\n"
2288 (if erc-debug-irc-protocol "disabled" "enabled")
2289 (current-time-string))))))
2290 (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
2292 (not (get-buffer-window "*erc-protocol*" t)))
2293 (display-buffer buf t))
2294 (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
2295 (if erc-debug-irc-protocol "enabled" "disabled"))))
2301 (defun erc-send-action (tgt str &optional force)
2302 "Send CTCP ACTION information described by STR to TGT."
2303 (erc-send-ctcp-message tgt (format "ACTION %s" str) force)
2304 (erc-display-message
2305 nil 'input (current-buffer)
2306 'ACTION ?n (erc-current-nick) ?a str ?u "" ?h ""))
2308 ;; Display interface
2310 (defun erc-string-invisible-p (string)
2311 "Check whether STRING is invisible or not.
2312 I.e. any char in it has the `invisible' property set."
2313 (text-property-any 0 (length string) 'invisible t string))
2315 (defcustom erc-remove-parsed-property t
2316 "Whether to remove the erc-parsed text property after displaying a message.
2318 The default is to remove it, since it causes ERC to take up extra
2319 memory. If you have code that relies on this property, then set
2320 this option to nil."
2324 (defun erc-display-line-1 (string buffer)
2325 "Display STRING in `erc-mode' BUFFER.
2326 Auxiliary function used in `erc-display-line'. The line gets filtered to
2327 interpret the control characters. Then, `erc-insert-pre-hook' gets called.
2328 If `erc-insert-this' is still t, STRING gets inserted into the buffer.
2329 Afterwards, `erc-insert-modify' and `erc-insert-post-hook' get called.
2330 If STRING is nil, the function does nothing."
2332 (with-current-buffer (or buffer (process-buffer erc-server-process))
2333 (let ((insert-position (or (marker-position erc-insert-marker)
2335 (let ((string string) ;; FIXME! Can this be removed?
2336 (buffer-undo-list t)
2337 (inhibit-read-only t))
2338 (unless (string-match "\n$" string)
2339 (setq string (concat string "\n"))
2340 (when (erc-string-invisible-p string)
2341 (erc-put-text-properties 0 (length string)
2342 '(invisible intangible) string)))
2343 (erc-log (concat "erc-display-line: " string
2344 (format "(%S)" string) " in buffer "
2345 (format "%s" buffer)))
2346 (setq erc-insert-this t)
2347 (run-hook-with-args 'erc-insert-pre-hook string)
2348 (if (null erc-insert-this)
2349 ;; Leave erc-insert-this set to t as much as possible. Fran
2350 ;; Litterio <franl> has seen erc-insert-this set to nil while
2351 ;; erc-send-pre-hook is running, which should never happen. This
2353 (setq erc-insert-this t)
2354 (save-excursion ;; to restore point in the new buffer
2357 (goto-char insert-position)
2358 (insert-before-markers string)
2359 ;; run insertion hook, with point at restored location
2361 (narrow-to-region insert-position (point))
2362 (run-hooks 'erc-insert-modify-hook)
2363 (run-hooks 'erc-insert-post-hook)
2364 (when erc-remove-parsed-property
2365 (remove-text-properties (point-min) (point-max)
2366 '(erc-parsed nil))))))))
2367 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2369 insert-position))))))
2371 (defun erc-update-undo-list (shift)
2372 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2373 (unless (or (zerop shift) (atom buffer-undo-list))
2374 (let ((list buffer-undo-list) elt)
2376 (setq elt (car list))
2377 (cond ((integerp elt) ; POSITION
2378 (cl-incf (car list) shift))
2379 ((or (atom elt) ; nil, EXTENT
2380 ;; (eq t (car elt)) ; (t . TIME)
2381 (markerp (car elt))) ; (MARKER . DISTANCE)
2383 ((integerp (car elt)) ; (BEGIN . END)
2384 (cl-incf (car elt) shift)
2385 (cl-incf (cdr elt) shift))
2386 ((stringp (car elt)) ; (TEXT . POSITION)
2387 (cl-incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
2388 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2389 (let ((cons (nthcdr 3 elt)))
2390 (cl-incf (car cons) shift)
2391 (cl-incf (cdr cons) shift)))
2392 ((and (featurep 'xemacs)
2393 (extentp (car elt))) ; (EXTENT START END)
2394 (cl-incf (nth 1 elt) shift)
2395 (cl-incf (nth 2 elt) shift)))
2396 (setq list (cdr list))))))
2398 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2399 "Regexp which matches all valid characters in a IRC nickname.")
2401 (defun erc-is-valid-nick-p (nick)
2402 "Check if NICK is a valid IRC nickname."
2403 (string-match (concat "^" erc-valid-nick-regexp "$") nick))
2405 (defun erc-display-line (string &optional buffer)
2406 "Display STRING in the ERC BUFFER.
2407 All screen output must be done through this function. If BUFFER is nil
2408 or omitted, the default ERC buffer for the `erc-session-server' is used.
2409 The BUFFER can be an actual buffer, a list of buffers, 'all or 'active.
2410 If BUFFER = 'all, the string is displayed in all the ERC buffers for the
2411 current session. 'active means the current active buffer
2412 \(`erc-active-buffer'). If the buffer can't be resolved, the current
2413 buffer is used. `erc-display-line-1' is used to display STRING.
2415 If STRING is nil, the function does nothing."
2416 (let ((inhibit-point-motion-hooks t)
2419 ((bufferp buffer) (list buffer))
2420 ((listp buffer) buffer)
2421 ((processp buffer) (list (process-buffer buffer)))
2423 ;; Hmm, or all of the same session server?
2424 (erc-buffer-list nil erc-server-process))
2425 ((and (eq 'active buffer) (erc-active-buffer))
2426 (list (erc-active-buffer)))
2427 ((erc-server-buffer-live-p)
2428 (list (process-buffer erc-server-process)))
2429 (t (list (current-buffer)))))
2430 (when (buffer-live-p buf)
2431 (erc-display-line-1 string buf)
2432 (push buf new-bufs)))
2433 (when (null new-bufs)
2434 (erc-display-line-1 string (if (erc-server-buffer-live-p)
2435 (process-buffer erc-server-process)
2436 (current-buffer))))))
2438 (defun erc-display-message-highlight (type string)
2439 "Highlight STRING according to TYPE, where erc-TYPE-face is an ERC face.
2441 See also `erc-make-notice'."
2442 (cond ((eq type 'notice)
2443 (erc-make-notice string))
2445 (erc-put-text-property
2447 'face (or (intern-soft
2448 (concat "erc-" (symbol-name type) "-face"))
2453 (defvar erc-lurker-state nil
2454 "Track the time of the last PRIVMSG for each (server,nick) pair.
2456 This is implemented as a hash of hashes, where the outer key is
2457 the canonicalized server name (as returned by
2458 `erc-canonicalize-server-name') and the outer value is a hash
2459 table mapping nicks (as returned by `erc-lurker-maybe-trim') to
2460 the times of their most recently received PRIVMSG on any channel
2461 on the given server.")
2463 (defcustom erc-lurker-trim-nicks t
2464 "If t, trim trailing `erc-lurker-ignore-chars' from nicks.
2466 This causes e.g. nick and nick` to be considered as the same
2467 individual for activity tracking and lurkiness detection
2472 (defcustom erc-lurker-ignore-chars "`_"
2473 "Characters at the end of a nick to strip for activity tracking purposes.
2475 See also `erc-lurker-trim-nicks'."
2479 (defun erc-lurker-maybe-trim (nick)
2480 "Maybe trim trailing `erc-lurker-ignore-chars' from NICK.
2482 Returns NICK unmodified unless `erc-lurker-trim-nicks' is
2484 (if erc-lurker-trim-nicks
2485 (replace-regexp-in-string
2487 (mapconcat (lambda (char)
2488 (regexp-quote (char-to-string char)))
2489 erc-lurker-ignore-chars ""))
2493 (defcustom erc-lurker-hide-list nil
2494 "List of IRC type messages to hide when sent by lurkers.
2496 A typical value would be '(\"JOIN\" \"PART\" \"QUIT\").
2497 See also `erc-lurker-p' and `erc-hide-list'."
2499 :type 'erc-message-type)
2501 (defcustom erc-lurker-threshold-time (* 60 60 24) ; 24h by default
2502 "Nicks from which no PRIVMSGs have been received within this
2503 interval (in units of seconds) are considered lurkers by
2504 `erc-lurker-p' and as a result their messages of types in
2505 `erc-lurker-hide-list' will be hidden."
2509 (defun erc-lurker-initialize ()
2510 "Initialize ERC lurker tracking functionality.
2512 This function adds `erc-lurker-update-status' to
2513 `erc-insert-pre-hook' in order to record the time of each nick's
2514 most recent PRIVMSG as well as initializing the state variable
2515 storing this information."
2516 (setq erc-lurker-state (make-hash-table :test 'equal))
2517 (add-hook 'erc-insert-pre-hook 'erc-lurker-update-status))
2519 (defun erc-lurker-cleanup ()
2520 "Remove all last PRIVMSG state older than `erc-lurker-threshold-time'.
2522 This should be called regularly to avoid excessive resource
2523 consumption for long-lived IRC or Emacs sessions."
2525 (lambda (server hash)
2527 (lambda (nick last-PRIVMSG-time)
2529 (> (float-time (time-subtract
2532 erc-lurker-threshold-time)
2533 (remhash nick hash)))
2535 (if (zerop (hash-table-count hash))
2536 (remhash server erc-lurker-state)))
2539 (defvar erc-lurker-cleanup-count 0
2540 "Internal counter variable for use with `erc-lurker-cleanup-interval'.")
2542 (defvar erc-lurker-cleanup-interval 100
2543 "Frequency of cleaning up stale erc-lurker state.
2545 `erc-lurker-update-status' calls `erc-lurker-cleanup' once for
2546 every `erc-lurker-cleanup-interval' updates to
2547 `erc-lurker-state'. This is designed to limit the memory
2548 consumption of lurker state during long Emacs sessions and/or ERC
2549 sessions with large numbers of incoming PRIVMSGs.")
2551 (defun erc-lurker-update-status (_message)
2552 "Update `erc-lurker-state' if necessary.
2554 This function is called from `erc-insert-pre-hook'. If the
2555 current message is a PRIVMSG, update `erc-lurker-state' to
2556 reflect the fact that its sender has issued a PRIVMSG at the
2557 current time. Otherwise, take no action.
2559 This function depends on the fact that `erc-display-message'
2560 dynamically binds `parsed', which is used to check if the current
2561 message is a PRIVMSG and to determine its sender. See also
2562 `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'.
2564 In order to limit memory consumption, this function also calls
2565 `erc-lurker-cleanup' once every `erc-lurker-cleanup-interval'
2566 updates of `erc-lurker-state'."
2567 (when (and (boundp 'parsed) (erc-response-p parsed))
2568 (let* ((command (erc-response.command parsed))
2570 (erc-lurker-maybe-trim
2571 (car (erc-parse-user (erc-response.sender parsed)))))
2573 (erc-canonicalize-server-name erc-server-announced-name)))
2574 (when (equal command "PRIVMSG")
2575 (when (>= (cl-incf erc-lurker-cleanup-count)
2576 erc-lurker-cleanup-interval)
2577 (setq erc-lurker-cleanup-count 0)
2578 (erc-lurker-cleanup))
2579 (unless (gethash server erc-lurker-state)
2580 (puthash server (make-hash-table :test 'equal) erc-lurker-state))
2581 (puthash sender (current-time)
2582 (gethash server erc-lurker-state))))))
2584 (defun erc-lurker-p (nick)
2585 "Predicate indicating NICK's lurking status on the current server.
2587 Lurking is the condition where NICK has issued no PRIVMSG on this
2588 server within `erc-lurker-threshold-time'. See also
2589 `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'."
2590 (unless erc-lurker-state (erc-lurker-initialize))
2592 (erc-canonicalize-server-name erc-server-announced-name))
2594 (gethash (erc-lurker-maybe-trim nick)
2595 (gethash server erc-lurker-state (make-hash-table)))))
2596 (or (null last-PRIVMSG-time)
2598 (time-subtract (current-time) last-PRIVMSG-time))
2599 erc-lurker-threshold-time))))
2601 (defcustom erc-common-server-suffixes
2602 '(("openprojects.net$" . "OPN")
2603 ("freenode.net$" . "freenode")
2604 ("oftc.net$" . "OFTC"))
2605 "Alist of common server name suffixes.
2606 This variable is used in mode-line display to save screen
2607 real estate. Set it to nil if you want to avoid changing
2608 displayed hostnames."
2609 :group 'erc-mode-line-and-header
2612 (defun erc-canonicalize-server-name (server)
2613 "Return the canonical network name for SERVER if any,
2614 otherwise `erc-server-announced-name'. SERVER is matched against
2615 `erc-common-server-suffixes'."
2617 (or (cdar (erc-remove-if-not
2618 (lambda (net) (string-match (car net) server))
2619 erc-common-server-suffixes))
2620 erc-server-announced-name)))
2622 (defun erc-hide-current-message-p (parsed)
2623 "Predicate indicating whether the parsed ERC response PARSED should be hidden.
2625 Messages are always hidden if the message type of PARSED appears in
2626 `erc-hide-list'. In addition, messages whose type is a member of
2627 `erc-lurker-hide-list' are hidden if `erc-lurker-p' returns true."
2628 (let* ((command (erc-response.command parsed))
2629 (sender (car (erc-parse-user (erc-response.sender parsed)))))
2630 (or (member command erc-hide-list)
2631 (and (member command erc-lurker-hide-list) (erc-lurker-p sender)))))
2633 (defun erc-display-message (parsed type buffer msg &rest args)
2634 "Display MSG in BUFFER.
2636 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2638 See also `erc-format-message' and `erc-display-line'."
2639 (let ((string (if (symbolp msg)
2640 (apply 'erc-format-message msg args)
2647 (mapc (lambda (type)
2649 (erc-display-message-highlight type string)))
2653 (erc-display-message-highlight type string))))
2655 (if (not (erc-response-p parsed))
2656 (erc-display-line string buffer)
2657 (unless (erc-hide-current-message-p parsed)
2658 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
2659 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2660 (erc-display-line string buffer)))))
2662 (defun erc-message-type-member (position list)
2663 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2665 This function relies on the erc-parsed text-property being
2667 (let ((prop-val (erc-get-parsed-vector position)))
2668 (and prop-val (member (erc-response.command prop-val) list))))
2670 (defvar erc-send-input-line-function 'erc-send-input-line)
2671 (make-variable-buffer-local 'erc-send-input-line-function)
2673 (defun erc-send-input-line (target line &optional force)
2674 "Send LINE to TARGET.
2676 See also `erc-server-send'."
2677 (setq line (format "PRIVMSG %s :%s"
2679 ;; If the line is empty, we still want to
2680 ;; send it - i.e. an empty pasted line.
2681 (if (string= line "\n")
2684 (erc-server-send line force target))
2686 (defun erc-get-arglist (fun)
2687 "Return the argument list of a function without the parens."
2688 (let ((arglist (format "%S" (erc-function-arglist fun))))
2689 (if (string-match "^(\\(.*\\))$" arglist)
2690 (match-string 1 arglist)
2693 (defun erc-command-no-process-p (str)
2694 "Return non-nil if STR is an ERC command that can be run when the process
2695 is not alive, nil otherwise."
2696 (let ((fun (erc-extract-command-from-line str)))
2699 (get (car fun) 'process-not-needed))))
2701 (defun erc-command-name (cmd)
2702 "For CMD being the function name of a ERC command, something like
2703 erc-cmd-FOO, this returns a string /FOO."
2704 (let ((command-name (symbol-name cmd)))
2705 (if (string-match "^erc-cmd-\\(.*\\)$" command-name)
2706 (concat "/" (match-string 1 command-name))
2709 (defun erc-process-input-line (line &optional force no-command)
2710 "Translate LINE to an RFC1459 command and send it based.
2711 Returns non-nil if the command is actually sent to the server, and nil
2714 If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
2715 it is passed to `erc-cmd-default'. If LINE is not a command (i.e. doesn't
2716 start with /<COMMAND>) then it is sent as a message.
2718 An optional FORCE argument forces sending the line when flood
2719 protection is in effect. The optional NO-COMMAND argument prohibits
2720 this function from interpreting the line as a command."
2721 (let ((command-list (erc-extract-command-from-line line)))
2722 (if (and command-list
2724 (let* ((cmd (nth 0 command-list))
2725 (args (nth 1 command-list)))
2730 (wrong-number-of-arguments
2731 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2732 ?c (erc-command-name cmd)
2733 ?u (or (erc-get-arglist cmd)
2736 (or (documentation cmd) "")))
2738 (let ((r (erc-default-target)))
2740 (funcall erc-send-input-line-function r line force)
2741 (erc-display-message nil 'error (current-buffer) 'no-target)
2744 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2745 ;; Input commands handlers
2746 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2748 (defun erc-cmd-AMSG (line)
2749 "Send LINE to all channels of the current server that you are on."
2750 (interactive "sSend to all channels you're on: ")
2751 (setq line (erc-trim-string line))
2752 (erc-with-all-buffers-of-server nil
2754 (erc-channel-p (erc-default-target)))
2755 (erc-send-message line)))
2756 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2758 (defun erc-cmd-SAY (line)
2759 "Send LINE to the current query or channel as a message, not a command.
2761 Use this when you want to send a message with a leading '/'. Note
2762 that since multi-line messages are never a command, you don't
2763 need this when pasting multiple lines of text."
2764 (if (string-match "^\\s-*$" line)
2766 (string-match "^ ?\\(.*\\)" line)
2767 (erc-process-input-line (match-string 1 line) nil t)))
2768 (put 'erc-cmd-SAY 'do-not-parse-args t)
2770 (defun erc-cmd-SET (line)
2771 "Set the variable named by the first word in LINE to some VALUE.
2772 VALUE is computed by evaluating the rest of LINE in Lisp."
2774 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2775 (let ((var (read (concat "erc-" (match-string 1 line))))
2776 (val (read (match-string 2 line))))
2779 (set var (eval val))
2780 (erc-display-message
2781 nil nil 'active (format "Set %S to %S" var val))
2783 (setq var (read (match-string 1 line)))
2786 (set var (eval val))
2787 (erc-display-message
2788 nil nil 'active (format "Set %S to %S" var val))
2790 (erc-display-message nil 'error 'active 'variable-not-bound)
2792 ((string-match "^\\s-*$" line)
2794 (concat "Available user variables:\n"
2799 (let ((val (symbol-value var)))
2800 (concat (format "%S:" var)
2802 (concat "\n" (pp-to-string val))
2803 (format " %S\n" val)))))
2804 (apropos-internal "^erc-" 'custom-variable-p))))
2808 (defalias 'erc-cmd-VAR 'erc-cmd-SET)
2809 (defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2810 (put 'erc-cmd-SET 'do-not-parse-args t)
2811 (put 'erc-cmd-SET 'process-not-needed t)
2813 (defun erc-cmd-default (line)
2816 Commands for which no erc-cmd-xxx exists, are tunneled through
2817 this function. LINE is sent to the server verbatim, and
2818 therefore has to contain the command itself as well."
2819 (erc-log (format "cmd: DEFAULT: %s" line))
2820 (erc-server-send (substring line 1))
2823 (defun erc-cmd-IGNORE (&optional user)
2824 "Ignore USER. This should be a regexp matching nick!user@host.
2825 If no USER argument is specified, list the contents of `erc-ignore-list'."
2827 (let ((quoted (regexp-quote user)))
2828 (when (and (not (string= user quoted))
2829 (y-or-n-p (format "Use regexp-quoted form (%s) instead? "
2833 (erc-make-notice (format "Now ignoring %s" user))
2835 (erc-with-server-buffer (add-to-list 'erc-ignore-list user)))
2836 (if (null (erc-with-server-buffer erc-ignore-list))
2837 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2838 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2839 (mapc #'(lambda (item)
2840 (erc-display-line (erc-make-notice item)
2842 (erc-with-server-buffer erc-ignore-list))))
2845 (defun erc-cmd-UNIGNORE (user)
2846 "Remove the user specified in USER from the ignore list."
2847 (let ((ignored-nick (car (erc-with-server-buffer
2848 (erc-member-ignore-case (regexp-quote user)
2849 erc-ignore-list)))))
2850 (unless ignored-nick
2851 (if (setq ignored-nick (erc-ignored-user-p user))
2852 (unless (y-or-n-p (format "Remove this regexp (%s)? "
2854 (setq ignored-nick nil))
2856 (erc-make-notice (format "%s is not currently ignored!" user))
2860 (erc-make-notice (format "No longer ignoring %s" user))
2862 (erc-with-server-buffer
2863 (setq erc-ignore-list (delete ignored-nick erc-ignore-list)))))
2866 (defun erc-cmd-CLEAR ()
2867 "Clear the window content."
2870 (put 'erc-cmd-CLEAR 'process-not-needed t)
2872 (defun erc-cmd-OPS ()
2873 "Show the ops in the current channel."
2876 (if erc-channel-users
2877 (maphash (lambda (_nick user-data)
2878 (let ((cuser (cdr user-data)))
2880 (erc-channel-user-op cuser))
2881 (setq ops (cons (erc-server-user-nickname
2885 (setq ops (sort ops 'string-lessp))
2887 (erc-display-message
2888 nil 'notice (current-buffer) 'ops
2889 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2890 ?o (mapconcat 'identity ops " "))
2891 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2894 (defun erc-cmd-COUNTRY (tld)
2895 "Display the country associated with the top level domain TLD."
2896 (require 'mail-extr)
2897 (let ((co (ignore-errors (what-domain tld))))
2899 (erc-display-message
2900 nil 'notice 'active 'country ?c co ?d tld)
2901 (erc-display-message
2902 nil 'notice 'active 'country-unknown ?d tld))
2904 (put 'erc-cmd-COUNTRY 'process-not-needed t)
2906 (defun erc-cmd-AWAY (line)
2907 "Mark the user as being away, the reason being indicated by LINE.
2908 If no reason is given, unset away status."
2909 (when (string-match "^\\s-*\\(.*\\)$" line)
2910 (let ((reason (match-string 1 line)))
2911 (erc-log (format "cmd: AWAY: %s" reason))
2913 (if (string= reason "")
2915 (concat "AWAY :" reason))))
2917 (put 'erc-cmd-AWAY 'do-not-parse-args t)
2919 (defun erc-cmd-GAWAY (line)
2920 "Mark the user as being away everywhere, the reason being indicated by LINE."
2921 ;; on all server buffers.
2922 (erc-with-all-buffers-of-server nil
2923 #'erc-open-server-buffer-p
2924 (erc-cmd-AWAY line)))
2925 (put 'erc-cmd-GAWAY 'do-not-parse-args t)
2927 (defun erc-cmd-CTCP (nick cmd &rest args)
2928 "Send a Client To Client Protocol message to NICK.
2930 CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2931 VERSION and so on. It is called with ARGS."
2932 (let ((str (concat cmd
2934 (concat " " (mapconcat #'identity args " "))))))
2935 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2936 (erc-send-ctcp-message nick str)
2939 (defun erc-cmd-HELP (&optional func)
2940 "Popup help information.
2942 If FUNC contains a valid function or variable, help about that
2943 will be displayed. If FUNC is empty, display an apropos about
2944 ERC commands. Otherwise, do `apropos' in the ERC namespace
2948 To find out about erc and bbdb, do
2951 For help about the WHOIS command, do:
2954 For a list of user commands (/join /part, ...):
2957 (let* ((sym (or (let ((sym (intern-soft
2958 (concat "erc-cmd-" (upcase func)))))
2959 (if (and sym (or (boundp sym) (fboundp sym)))
2962 (let ((sym (intern-soft func)))
2963 (if (and sym (or (boundp sym) (fboundp sym)))
2966 (let ((sym (intern-soft (concat "erc-" func))))
2967 (if (and sym (or (boundp sym) (fboundp sym)))
2972 ((boundp sym) (describe-variable sym))
2973 ((fboundp sym) (describe-function sym))
2975 (apropos-command (concat "erc-.*" func) nil
2978 (get x 'custom-type))))
2980 (apropos "erc-cmd-")
2981 (message "Type C-h m to get additional information about keybindings.")
2984 (defalias 'erc-cmd-H 'erc-cmd-HELP)
2985 (put 'erc-cmd-HELP 'process-not-needed t)
2987 (defun erc-cmd-JOIN (channel &optional key)
2988 "Join the channel given in CHANNEL, optionally with KEY.
2989 If CHANNEL is specified as \"-invite\", join the channel to which you
2990 were most recently invited. See also `invitation'."
2992 (if (string= (upcase channel) "-INVITE")
2994 (setq chnl erc-invitation)
2995 (erc-display-message nil 'error (current-buffer) 'no-invitation))
2996 (setq chnl (erc-ensure-channel-name channel)))
2998 ;; Prevent double joining of same channel on same server.
2999 (let ((joined-channels
3000 (mapcar #'(lambda (chanbuf)
3001 (with-current-buffer chanbuf (erc-default-target)))
3002 (erc-channel-list erc-server-process))))
3003 (if (erc-member-ignore-case chnl joined-channels)
3004 (switch-to-buffer (car (erc-member-ignore-case chnl
3006 (erc-log (format "cmd: JOIN: %s" chnl))
3007 (erc-server-send (if (and chnl key)
3008 (format "JOIN %s %s" chnl key)
3009 (format "JOIN %s" chnl)))))))
3012 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
3013 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
3015 (defvar erc-channel-new-member-names nil
3016 "If non-nil, a names list is currently being received.
3018 If non-nil, this variable is a hash-table that associates
3019 received nicks with t.")
3020 (make-variable-buffer-local 'erc-channel-new-member-names)
3022 (defun erc-cmd-NAMES (&optional channel)
3023 "Display the users in CHANNEL.
3024 If CHANNEL is not specified, display the users in the current channel.
3025 This function clears the channel name list first, then sends the
3027 (let ((tgt (or (and (erc-channel-p channel) channel)
3028 (erc-default-target))))
3029 (if (and tgt (erc-channel-p tgt))
3031 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
3034 (erc-channel-begin-receiving-names))
3035 (erc-server-send (concat "NAMES " tgt)))
3036 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
3038 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
3040 (defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
3041 "Kick the user indicated in LINE from the current channel.
3042 LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
3043 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
3044 (if (string= "" reasonstring)
3045 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
3046 (if (erc-channel-p target)
3047 (let ((nick reason-or-nick))
3048 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
3049 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
3053 (let ((ch (erc-default-target)))
3054 (setq reasonstring (concat
3055 (if reason-or-nick (concat reason-or-nick " "))
3060 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
3062 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
3063 (erc-display-message nil 'error (current-buffer)
3064 'no-default-channel))
3067 (defvar erc-script-args nil)
3069 (defun erc-cmd-LOAD (line)
3070 "Load the script provided in the LINE.
3071 If LINE continues beyond the file name, the rest of
3072 it is put in a (local) variable `erc-script-args',
3073 which can be used in Emacs Lisp scripts.
3075 The optional FORCE argument is ignored here - you can't force loading
3076 a script after exceeding the flood threshold."
3078 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
3079 (let* ((file-to-find (match-string 1 line))
3080 (erc-script-args (match-string 2 line))
3081 (file (erc-find-file file-to-find erc-script-path)))
3082 (erc-log (format "cmd: LOAD: %s" file-to-find))
3085 (erc-display-message nil 'error (current-buffer)
3086 'cannot-find-file ?f file-to-find))
3087 ((not (file-readable-p file))
3088 (erc-display-message nil 'error (current-buffer)
3089 'cannot-read-file ?f file))
3091 (message "Loading \'%s\'..." file)
3092 (erc-load-script file)
3093 (message "Loading \'%s\'...done" file))))
3097 (defun erc-cmd-WHOIS (user &optional server)
3098 "Display whois information for USER.
3100 If SERVER is non-nil, use that, rather than the current server."
3101 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
3102 (let ((send (if server
3103 (format "WHOIS %s %s" user server)
3104 (format "WHOIS %s" user))))
3105 (erc-log (format "cmd: %s" send))
3106 (erc-server-send send)
3108 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
3110 (defun erc-cmd-WHOAMI ()
3111 "Display whois information about yourself."
3112 (erc-cmd-WHOIS (erc-current-nick))
3115 (defun erc-cmd-IDLE (nick)
3116 "Show the length of time NICK has been idle."
3117 (let ((origbuf (current-buffer))
3119 (erc-with-server-buffer
3120 (push (cons (erc-once-with-server-event
3121 311 (lambda (_proc parsed)
3123 (nth 1 (erc-response.command-args
3125 'erc-server-311-functions)
3127 (push (cons (erc-once-with-server-event
3128 312 (lambda (_proc parsed)
3130 (nth 1 (erc-response.command-args
3132 'erc-server-312-functions)
3134 (push (cons (erc-once-with-server-event
3135 318 (lambda (_proc parsed)
3137 (nth 1 (erc-response.command-args
3139 'erc-server-318-functions)
3141 (push (cons (erc-once-with-server-event
3142 319 (lambda (_proc parsed)
3144 (nth 1 (erc-response.command-args
3146 'erc-server-319-functions)
3148 (push (cons (erc-once-with-server-event
3149 320 (lambda (_proc parsed)
3151 (nth 1 (erc-response.command-args
3153 'erc-server-320-functions)
3155 (push (cons (erc-once-with-server-event
3156 330 (lambda (_proc parsed)
3158 (nth 1 (erc-response.command-args
3160 'erc-server-330-functions)
3162 (push (cons (erc-once-with-server-event
3164 (lambda (_proc parsed)
3168 (erc-response.command-args parsed)))))
3171 (format "%s has been idle for %s."
3172 (erc-string-no-properties nick)
3173 (erc-seconds-to-string idleseconds)))
3176 'erc-server-317-functions)
3179 ;; Send the WHOIS command.
3180 (erc-cmd-WHOIS nick)
3182 ;; Remove the uninterned symbols from the server hooks that did not run.
3183 (run-at-time 20 nil (lambda (buf symlist)
3184 (with-current-buffer buf
3185 (dolist (sym symlist)
3186 (let ((hooksym (cdr sym))
3187 (funcsym (car sym)))
3188 (remove-hook hooksym funcsym t)))))
3189 (current-buffer) symlist)))
3192 (defun erc-cmd-DESCRIBE (line)
3193 "Pose some action to a certain user.
3194 LINE has the format \"USER ACTION\"."
3197 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
3198 (let ((dst (match-string 1 line))
3199 (s (match-string 2 line)))
3200 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
3201 (erc-send-action dst s))
3204 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
3206 (defun erc-cmd-ME (line)
3207 "Send LINE as an action."
3209 ((string-match "^\\s-\\(.*\\)$" line)
3210 (let ((s (match-string 1 line)))
3211 (erc-log (format "cmd: ME: %s" s))
3212 (erc-send-action (erc-default-target) s))
3215 (put 'erc-cmd-ME 'do-not-parse-args t)
3217 (defun erc-cmd-ME\'S (line)
3218 "Do a /ME command, but add the string \" 's\" to the beginning."
3219 (erc-cmd-ME (concat " 's" line)))
3220 (put 'erc-cmd-ME\'S 'do-not-parse-args t)
3222 (defun erc-cmd-LASTLOG (line)
3223 "Show all lines in the current buffer matching the regexp LINE.
3225 If a match spreads across multiple lines, all those lines are shown.
3227 The lines are shown in a buffer named `*Occur*'.
3228 It serves as a menu to find any of the occurrences in this buffer.
3229 \\[describe-mode] in that buffer will explain how.
3231 If LINE contains upper case characters (excluding those preceded by `\'),
3232 the matching is case-sensitive."
3235 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
3236 (put 'erc-cmd-LASTLOG 'process-not-needed t)
3238 (defun erc-send-message (line &optional force)
3239 "Send LINE to the current channel or user and display it.
3241 See also `erc-message' and `erc-display-line'."
3242 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
3244 (concat (erc-format-my-nick) line)
3246 ;; FIXME - treat multiline, run hooks, or remove me?
3249 (defun erc-cmd-MODE (line)
3250 "Change or display the mode value of a channel or user.
3251 The first word specifies the target. The rest is the mode string
3254 If only one word is given, display the mode of that target.
3256 A list of valid mode strings for Freenode may be found at
3257 URL `http://freenode.net/using_the_network.shtml'."
3259 ((string-match "^\\s-\\(.*\\)$" line)
3260 (let ((s (match-string 1 line)))
3261 (erc-log (format "cmd: MODE: %s" s))
3262 (erc-server-send (concat "MODE " line)))
3265 (put 'erc-cmd-MODE 'do-not-parse-args t)
3267 (defun erc-cmd-NOTICE (channel-or-user &rest message)
3268 "Send a notice to the channel or user given as the first word.
3269 The rest is the message to send."
3270 (erc-message "NOTICE" (concat channel-or-user " "
3271 (mapconcat #'identity message " "))))
3273 (defun erc-cmd-MSG (line)
3274 "Send a message to the channel or user given as the first word in LINE.
3276 The rest of LINE is the message to send."
3277 (erc-message "PRIVMSG" line))
3279 (defalias 'erc-cmd-M 'erc-cmd-MSG)
3280 (put 'erc-cmd-MSG 'do-not-parse-args t)
3282 (defun erc-cmd-SQUERY (line)
3283 "Send a Service Query to the service given as the first word in LINE.
3285 The rest of LINE is the message to send."
3286 (erc-message "SQUERY" line))
3288 (defun erc-cmd-NICK (nick)
3289 "Change current nickname to NICK."
3290 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
3291 (let ((nicklen (cdr (assoc "NICKLEN" (erc-with-server-buffer
3292 erc-server-parameters)))))
3293 (and nicklen (> (length nick) (string-to-number nicklen))
3294 (erc-display-message
3295 nil 'notice 'active 'nick-too-long
3296 ?i (length nick) ?l nicklen)))
3297 (erc-server-send (format "NICK %s" nick))
3299 (erc-set-current-nick nick)
3300 (erc-update-mode-line)
3301 (setq erc-bad-nick nil)))
3304 (defun erc-cmd-PART (line)
3305 "When LINE is an empty string, leave the current channel.
3306 Otherwise leave the channel indicated by LINE."
3308 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
3309 (let* ((ch (match-string 1 line))
3310 (msg (match-string 2 line))
3311 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3312 (erc-log (format "cmd: PART: %s: %s" ch reason))
3313 (erc-server-send (if (string= reason "")
3314 (format "PART %s" ch)
3315 (format "PART %s :%s" ch reason))
3318 ((string-match "^\\s-*\\(.*\\)$" line)
3319 (let* ((ch (erc-default-target))
3320 (msg (match-string 1 line))
3321 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3322 (if (and ch (erc-channel-p ch))
3324 (erc-log (format "cmd: PART: %s: %s" ch reason))
3325 (erc-server-send (if (string= reason "")
3326 (format "PART %s" ch)
3327 (format "PART %s :%s" ch reason))
3329 (erc-display-message nil 'error (current-buffer) 'no-target)))
3332 (put 'erc-cmd-PART 'do-not-parse-args t)
3334 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
3336 (defun erc-cmd-PING (recipient)
3338 (let ((time (format "%f" (erc-current-time))))
3339 (erc-log (format "cmd: PING: %s" time))
3340 (erc-cmd-CTCP recipient "PING" time)))
3342 (defun erc-cmd-QUOTE (line)
3343 "Send LINE directly to the server.
3344 All the text given as argument is sent to the sever as unmodified,
3345 just as you provided it. Use this command with care!"
3347 ((string-match "^ ?\\(.+\\)$" line)
3348 (erc-server-send (match-string 1 line)))
3350 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
3352 (defcustom erc-query-display 'window
3353 "Indicates how to display query buffers when using the /QUERY
3354 command to talk to someone.
3356 The default behavior is to display the message in a new window
3357 and bring it to the front. See the documentation for
3358 `erc-join-buffer' for a description of the available choices.
3360 See also `erc-auto-query' to decide how private messages from
3361 other people should be displayed."
3363 :type '(choice (const :tag "Split window and select" window)
3364 (const :tag "Split window, don't select" window-noselect)
3365 (const :tag "New frame" frame)
3366 (const :tag "Bury in new buffer" bury)
3367 (const :tag "Use current buffer" buffer)
3368 (const :tag "Use current buffer" t)))
3370 (defun erc-cmd-QUERY (&optional user)
3371 "Open a query with USER.
3372 The type of query window/frame/etc will depend on the value of
3373 `erc-query-display'.
3375 If USER is omitted, close the current query buffer if one exists
3376 - except this is broken now ;-)"
3378 (list (read-from-minibuffer "Start a query with: " nil)))
3379 (let ((session-buffer (erc-server-buffer))
3380 (erc-join-buffer erc-query-display))
3382 (erc-query user session-buffer)
3383 ;; currently broken, evil hack to display help anyway
3384 ;(erc-delete-query))))
3385 (signal 'wrong-number-of-arguments ""))))
3386 (defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3388 (defun erc-quit/part-reason-default ()
3389 "Default quit/part message."
3390 (format "\C-bERC\C-b (IRC client for Emacs %s)" emacs-version))
3393 (defun erc-quit-reason-normal (&optional s)
3394 "Normal quit message.
3396 If S is non-nil, it will be used as the quit reason."
3397 (or s (erc-quit/part-reason-default)))
3399 (defun erc-quit-reason-zippy (&optional s)
3400 "Zippy quit message.
3402 If S is non-nil, it will be used as the quit reason."
3405 (erc-replace-regexp-in-string "\n" "" (yow))
3406 (erc-quit/part-reason-default))))
3408 (make-obsolete 'erc-quit-reason-zippy "it will be removed." "24.4")
3410 (defun erc-quit-reason-various (s)
3411 "Choose a quit reason based on S (a string)."
3412 (when (featurep 'xemacs) (require 'poe))
3413 (let ((res (car (assoc-default (or s "")
3414 erc-quit-reason-various-alist 'string-match))))
3416 ((functionp res) (funcall res))
3419 (t (erc-quit/part-reason-default)))))
3421 (defun erc-part-reason-normal (&optional s)
3422 "Normal part message.
3424 If S is non-nil, it will be used as the part reason."
3425 (or s (erc-quit/part-reason-default)))
3427 (defun erc-part-reason-zippy (&optional s)
3428 "Zippy part message.
3430 If S is non-nil, it will be used as the quit reason."
3433 (erc-replace-regexp-in-string "\n" "" (yow))
3434 (erc-quit/part-reason-default))))
3436 (make-obsolete 'erc-part-reason-zippy "it will be removed." "24.4")
3438 (defun erc-part-reason-various (s)
3439 "Choose a part reason based on S (a string)."
3440 (when (featurep 'xemacs) (require 'poe))
3441 (let ((res (car (assoc-default (or s "")
3442 erc-part-reason-various-alist 'string-match))))
3444 ((functionp res) (funcall res))
3447 (t (erc-quit/part-reason-default)))))
3449 (defun erc-cmd-QUIT (reason)
3450 "Disconnect from the current server.
3451 If REASON is omitted, display a default quit message, otherwise display
3452 the message given by REASON."
3456 ((string-match "^\\s-*\\(.*\\)$" reason)
3457 (let* ((s (match-string 1 reason))
3458 (buffer (erc-server-buffer))
3459 (reason (funcall erc-quit-reason (if (equal s "") nil s)))
3461 (with-current-buffer (if (and buffer
3465 (erc-log (format "cmd: QUIT: %s" reason))
3466 (setq erc-server-quitting t)
3467 (erc-set-active-buffer (erc-server-buffer))
3468 (setq server-proc erc-server-process)
3469 (erc-server-send (format "QUIT :%s" reason)))
3470 (run-hook-with-args 'erc-quit-hook server-proc)
3471 (when erc-kill-queries-on-quit
3472 (erc-kill-query-buffers server-proc))
3473 ;; if the process has not been killed within 4 seconds, kill it
3476 (when (and (processp proc)
3477 (memq (process-status proc) '(run open)))
3478 (delete-process proc)))
3483 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3484 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3485 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3486 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3487 (put 'erc-cmd-QUIT 'process-not-needed t)
3489 (defun erc-cmd-GQUIT (reason)
3490 "Disconnect from all servers at once with the same quit REASON."
3491 (erc-with-all-buffers-of-server nil #'erc-open-server-buffer-p
3492 (erc-cmd-QUIT reason))
3493 (when erc-kill-queries-on-quit
3494 ;; if the query buffers have not been killed within 4 seconds,
3499 (dolist (buffer (erc-buffer-list (lambda (buf)
3500 (not (erc-server-buffer-p buf)))))
3501 (kill-buffer buffer)))))
3504 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3505 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3506 (put 'erc-cmd-GQUIT 'process-not-needed t)
3508 (defun erc-cmd-RECONNECT ()
3509 "Try to reconnect to the current IRC server."
3510 (let ((buffer (erc-server-buffer))
3512 (unless (buffer-live-p buffer)
3513 (setq buffer (current-buffer)))
3514 (with-current-buffer buffer
3515 (setq erc-server-quitting nil)
3516 (setq erc-server-reconnecting t)
3517 (setq erc-server-reconnect-count 0)
3518 (setq process (get-buffer-process (erc-server-buffer)))
3520 (delete-process process)
3521 (erc-server-reconnect))
3522 (setq erc-server-reconnecting nil)))
3524 (put 'erc-cmd-RECONNECT 'process-not-needed t)
3526 (defun erc-cmd-SERVER (server)
3527 "Connect to SERVER, leaving existing connection intact."
3528 (erc-log (format "cmd: SERVER: %s" server))
3530 (erc :server server :nick (erc-current-nick))
3532 (erc-error "Cannot find host %s." server)))
3534 (put 'erc-cmd-SERVER 'process-not-needed t)
3536 (defvar motif-version-string)
3537 (defvar gtk-version-string)
3539 (defun erc-cmd-SV ()
3540 "Say the current ERC and Emacs version into channel."
3541 (erc-send-message (format "I'm using ERC with %s %s (%s%s) of %s."
3542 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3544 system-configuration
3546 (cond ((featurep 'motif)
3547 (concat ", " (substring
3548 motif-version-string 4)))
3550 (concat ", GTK+ Version "
3551 gtk-version-string))
3552 ((featurep 'x-toolkit) ", X toolkit")
3554 (if (and (boundp 'x-toolkit-scroll-bars)
3555 (memq x-toolkit-scroll-bars
3557 (format ", %s scroll bars"
3558 (capitalize (symbol-name
3559 x-toolkit-scroll-bars)))
3561 (if (featurep 'multi-tty) ", multi-tty" ""))
3562 erc-emacs-build-time))
3565 (defun erc-cmd-SM ()
3566 "Say the current ERC modes into channel."
3567 (erc-send-message (format "I'm using the following modules: %s!"
3571 (defun erc-cmd-DEOP (&rest people)
3572 "Remove the operator setting from user(s) given in PEOPLE."
3573 (when (> (length people) 0)
3574 (erc-server-send (concat "MODE " (erc-default-target)
3576 (make-string (length people) ?o)
3578 (mapconcat 'identity people " ")))
3581 (defun erc-cmd-OP (&rest people)
3582 "Add the operator setting to users(s) given in PEOPLE."
3583 (when (> (length people) 0)
3584 (erc-server-send (concat "MODE " (erc-default-target)
3586 (make-string (length people) ?o)
3588 (mapconcat 'identity people " ")))
3591 (defun erc-cmd-TIME (&optional line)
3592 "Request the current time and date from the current server."
3594 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3595 (let ((args (match-string 1 line)))
3596 (erc-log (format "cmd: TIME: %s" args))
3597 (erc-server-send (concat "TIME " args)))
3599 (t (erc-server-send "TIME"))))
3600 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3602 (defun erc-cmd-TOPIC (topic)
3603 "Set or request the topic for a channel.
3604 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3605 or the empty string.
3607 If no #CHANNEL is given, the default channel is used. If TOPIC is
3608 given, the channel topic is modified, otherwise the current topic will
3611 ;; /topic #channel TOPIC
3612 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3613 (let ((ch (match-string 1 topic))
3614 (topic (match-string 2 topic)))
3615 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3616 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3619 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3620 (let ((ch (match-string 1 topic)))
3621 (erc-server-send (format "TOPIC %s" ch) nil ch)
3624 ((string-match "^\\s-*$" topic)
3625 (let ((ch (erc-default-target)))
3626 (erc-server-send (format "TOPIC %s" ch) nil ch)
3629 ((string-match "^\\s-*\\(.*\\)$" topic)
3630 (let ((ch (erc-default-target))
3631 (topic (match-string 1 topic)))
3632 (if (and ch (erc-channel-p ch))
3634 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3635 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3636 (erc-display-message nil 'error (current-buffer) 'no-target)))
3639 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3640 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3642 (defun erc-cmd-APPENDTOPIC (topic)
3643 "Append TOPIC to the current channel topic, separated by a space."
3644 (let ((oldtopic erc-channel-topic))
3645 ;; display help when given no arguments
3646 (when (string-match "^\\s-*$" topic)
3647 (signal 'wrong-number-of-arguments nil))
3648 ;; strip trailing ^O
3649 (when (string-match "\\(.*\\)\C-o" oldtopic)
3650 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3651 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3652 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3654 (defun erc-cmd-CLEARTOPIC (&optional channel)
3655 "Clear the topic for a CHANNEL.
3656 If CHANNEL is not specified, clear the topic for the default channel."
3657 (interactive "sClear topic of channel (RET is current channel): ")
3658 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3660 (erc-server-send (format "TOPIC %s :" chnl))
3665 (defvar erc-channel-banlist nil
3666 "A list of bans seen for the current channel.
3668 Each ban is an alist of the form:
3671 The property `received-from-server' indicates whether
3672 or not the ban list has been requested from the server.")
3673 (make-variable-buffer-local 'erc-channel-banlist)
3674 (put 'erc-channel-banlist 'received-from-server nil)
3676 (defun erc-cmd-BANLIST ()
3677 "Pretty-print the contents of `erc-channel-banlist'.
3679 The ban list is fetched from the server if necessary."
3680 (let ((chnl (erc-default-target))
3681 (chnl-name (buffer-name)))
3684 ((not (erc-channel-p chnl))
3685 (erc-display-line (erc-make-notice "You're not on a channel\n")
3688 ((not (get 'erc-channel-banlist 'received-from-server))
3689 (let ((old-367-hook erc-server-367-functions))
3690 (setq erc-server-367-functions 'erc-banlist-store
3691 erc-channel-banlist nil)
3692 ;; fetch the ban list then callback
3693 (erc-with-server-buffer
3694 (erc-once-with-server-event
3696 (lambda (_proc _parsed)
3697 (with-current-buffer chnl-name
3698 (put 'erc-channel-banlist 'received-from-server t)
3699 (setq erc-server-367-functions old-367-hook)
3702 (erc-server-send (format "MODE %s b" chnl)))))
3704 ((null erc-channel-banlist)
3705 (erc-display-line (erc-make-notice
3706 (format "No bans for channel: %s\n" chnl))
3708 (put 'erc-channel-banlist 'received-from-server nil))
3711 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3713 (and (boundp 'fill-column)
3715 (1- (window-width))))
3716 (separator (make-string erc-fill-column ?=))
3718 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3719 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3722 (erc-make-notice (format "Ban list for channel: %s\n"
3723 (erc-default-target)))
3726 (erc-display-line separator 'active)
3727 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3728 (erc-display-line separator 'active)
3734 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3736 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3739 erc-channel-banlist)
3741 (erc-display-line (erc-make-notice "End of Ban list")
3743 (put 'erc-channel-banlist 'received-from-server nil)))))
3746 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3748 (defun erc-cmd-MASSUNBAN ()
3751 Unban all currently banned users in the current channel."
3752 (let ((chnl (erc-default-target)))
3755 ((not (erc-channel-p chnl))
3757 (erc-make-notice "You're not on a channel\n")
3760 ((not (get 'erc-channel-banlist 'received-from-server))
3761 (let ((old-367-hook erc-server-367-functions))
3762 (setq erc-server-367-functions 'erc-banlist-store)
3763 ;; fetch the ban list then callback
3764 (erc-with-server-buffer
3765 (erc-once-with-server-event
3767 (lambda (_proc _parsed)
3768 (with-current-buffer chnl
3769 (put 'erc-channel-banlist 'received-from-server t)
3770 (setq erc-server-367-functions old-367-hook)
3773 (erc-server-send (format "MODE %s b" chnl)))))
3775 (t (let ((bans (mapcar 'cdr erc-channel-banlist)))
3777 ;; Glob the bans into groups of three, and carry out the unban.
3778 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3782 (format "MODE %s -%s %s" (erc-default-target)
3783 (make-string (length x) ?b)
3784 (mapconcat 'identity x " "))))
3785 (erc-group-list bans 3))))
3788 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3790 ;;;; End of IRC commands
3792 (defun erc-ensure-channel-name (channel)
3793 "Return CHANNEL if it is a valid channel name.
3794 Eventually add a # in front of it, if that turns it into a valid channel name."
3795 (if (erc-channel-p channel)
3797 (concat "#" channel)))
3799 (defun erc-grab-region (start end)
3800 "Copy the region between START and END in a recreatable format.
3802 Converts all the IRC text properties in each line of the region
3803 into control codes and writes them to a separate buffer. The
3804 resulting text may be used directly as a script to generate this
3807 (erc-set-active-buffer (current-buffer))
3809 (let* ((cb (current-buffer))
3810 (buf (generate-new-buffer erc-grab-buffer-name))
3811 (region (buffer-substring start end))
3812 (lines (erc-split-multiline-safe region)))
3814 (dolist (line lines)
3815 (insert (concat line "\n")))
3817 (switch-to-buffer-other-window buf)))
3818 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3821 (defun erc-display-prompt (&optional buffer pos prompt face)
3822 "Display PROMPT in BUFFER at position POS.
3823 Display an ERC prompt in BUFFER.
3825 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3826 If BUFFER is nil, the `current-buffer' is used.
3827 If POS is nil, PROMPT will be displayed at `point'.
3828 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3829 `erc-prompt-face' will be used."
3830 (let* ((prompt (or prompt (erc-prompt)))
3832 (ob (current-buffer)))
3833 ;; We cannot use save-excursion because we move point, therefore
3834 ;; we resort to the ol' ob trick to restore this.
3835 (when (and buffer (bufferp buffer))
3836 (set-buffer buffer))
3838 ;; now save excursion again to store where point and mark are
3839 ;; in the current buffer
3841 (setq pos (or pos (point)))
3844 ;; Do not extend the text properties when typing at the end
3845 ;; of the prompt, but stuff typed in front of the prompt
3846 ;; shall remain part of the prompt.
3847 (setq prompt (erc-propertize prompt
3848 'start-open t ; XEmacs
3849 'rear-nonsticky t ; Emacs
3854 (erc-put-text-property 0 (1- (length prompt))
3855 'face (or face 'erc-prompt-face)
3858 ;; Set the input marker
3859 (set-marker erc-input-marker (point)))
3861 ;; Now we are back at the old position. If the prompt was
3862 ;; inserted here or before us, advance point by the length of
3864 (when (or (not pos) (<= (point) pos))
3866 ;; Clear the undo buffer now, so the user can undo his stuff,
3867 ;; but not the stuff we did. Sneaky!
3868 (setq buffer-undo-list nil)
3871 ;; interactive operations
3873 (defun erc-input-message ()
3874 "Read input from the minibuffer."
3876 (let ((minibuffer-allow-text-properties t)
3877 (read-map minibuffer-local-map))
3878 (insert (read-from-minibuffer "Message: "
3879 (string (if (featurep 'xemacs)
3881 last-command-event))
3883 (erc-send-current-line)))
3885 (defvar erc-action-history-list ()
3886 "History list for interactive action input.")
3888 (defun erc-input-action ()
3889 "Interactively input a user action and send it to IRC."
3891 (erc-set-active-buffer (current-buffer))
3892 (let ((action (read-from-minibuffer
3893 "Action: " nil nil nil 'erc-action-history-list)))
3894 (if (not (string-match "^\\s-*$" action))
3895 (erc-send-action (erc-default-target) action))))
3897 (defun erc-join-channel (channel &optional key)
3900 If `point' is at the beginning of a channel name, use that as default."
3903 (let ((chnl (if (looking-at "\\([&#+!][^ \n]+\\)") (match-string 1) ""))
3904 (table (when (erc-server-buffer-live-p)
3905 (set-buffer (process-buffer erc-server-process))
3907 (completing-read "Join channel: " table nil nil nil nil chnl))
3908 (when (or current-prefix-arg erc-prompt-for-channel-key)
3909 (read-from-minibuffer "Channel key (RET for none): " nil))))
3910 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3912 (defun erc-part-from-channel (reason)
3913 "Part from the current channel and prompt for a REASON."
3916 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3918 (read-from-minibuffer (concat "Reason for leaving " (erc-default-target)
3920 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3922 (defun erc-set-topic (topic)
3923 "Prompt for a TOPIC for the current channel."
3926 (read-from-minibuffer
3927 (concat "Set topic of " (erc-default-target) ": ")
3928 (when erc-channel-topic
3929 (let ((ss (split-string erc-channel-topic "\C-o")))
3930 (cons (apply 'concat (if (cdr ss) (butlast ss) ss))
3932 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3933 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3935 (defun erc-set-channel-limit (&optional limit)
3936 "Set a LIMIT for the current channel. Remove limit if nil.
3937 Prompt for one if called interactively."
3938 (interactive (list (read-from-minibuffer
3939 (format "Limit for %s (RET to remove limit): "
3940 (erc-default-target)))))
3941 (let ((tgt (erc-default-target)))
3942 (erc-server-send (if (and limit (>= (length limit) 1))
3943 (format "MODE %s +l %s" tgt limit)
3944 (format "MODE %s -l" tgt)))))
3946 (defun erc-set-channel-key (&optional key)
3947 "Set a KEY for the current channel. Remove key if nil.
3948 Prompt for one if called interactively."
3949 (interactive (list (read-from-minibuffer
3950 (format "Key for %s (RET to remove key): "
3951 (erc-default-target)))))
3952 (let ((tgt (erc-default-target)))
3953 (erc-server-send (if (and key (>= (length key) 1))
3954 (format "MODE %s +k %s" tgt key)
3955 (format "MODE %s -k" tgt)))))
3957 (defun erc-quit-server (reason)
3958 "Disconnect from current server after prompting for REASON.
3959 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
3960 (interactive (list (read-from-minibuffer
3961 (format "Reason for quitting %s: "
3962 (or erc-server-announced-name
3963 erc-session-server)))))
3964 (erc-cmd-QUIT reason))
3966 ;; Movement of point
3969 "Move `point' to the beginning of the current line.
3971 This places `point' just after the prompt, or at the beginning of the line."
3974 (when (get-text-property (point) 'erc-prompt)
3975 (goto-char erc-input-marker))
3978 (defun erc-kill-input ()
3979 "Kill current input line using `erc-bol' followed by `kill-line'."
3981 (when (and (erc-bol)
3982 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
3983 ;; there's nothing to kill
3984 (if (boundp 'erc-input-ring-index)
3985 (setq erc-input-ring-index nil))
3988 (defun erc-complete-word-at-point ()
3989 (run-hook-with-args-until-success 'erc-complete-functions))
3991 (define-obsolete-function-alias 'erc-complete-word 'completion-at-point "24.1")
3993 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3995 ;; IRC SERVER INPUT HANDLING
3997 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3999 ;;;; New Input parsing
4001 ; Stolen from ZenIRC. I just wanna test this code, so here is
4004 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
4005 erc-default-server-handler)
4006 "Default for server messages which aren't covered by `erc-server-hooks'."
4007 :group 'erc-server-hooks
4010 (defun erc-default-server-handler (proc parsed)
4011 "Default server handler.
4013 Displays PROC and PARSED appropriately using `erc-display-message'."
4014 (erc-display-message
4021 (setq res (append res (list x)))))
4026 (defvar erc-server-vectors
4027 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
4028 "List of received server messages which ERC does not specifically handle.
4029 See `erc-debug-missing-hooks'.")
4030 ;(make-variable-buffer-local 'erc-server-vectors)
4032 (defun erc-debug-missing-hooks (_proc parsed)
4033 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
4034 These vectors can be helpful when adding new server message handlers to ERC.
4035 See `erc-default-server-hook'."
4036 (nconc erc-server-vectors (list parsed))
4039 (defun erc-query (target server)
4040 "Open a query buffer on TARGET, using SERVER.
4041 To change how this query window is displayed, use `let' to bind
4042 `erc-join-buffer' before calling this."
4044 (buffer-live-p server)
4045 (set-buffer server))
4046 (error "Couldn't switch to server buffer"))
4047 (let ((buf (erc-open erc-session-server
4050 erc-session-user-full-name
4055 erc-server-process)))
4057 (error "Couldn't open query window"))
4058 (erc-update-mode-line)
4061 (defcustom erc-auto-query 'window-noselect
4062 "If non-nil, create a query buffer each time you receive a private message.
4063 If the buffer doesn't already exist, it is created.
4065 This can be set to a symbol, to control how the new query window
4066 should appear. The default behavior is to display the buffer in
4067 a new window, but not to select it. See the documentation for
4068 `erc-join-buffer' for a description of the available choices."
4070 :type '(choice (const :tag "Don't create query window" nil)
4071 (const :tag "Split window and select" window)
4072 (const :tag "Split window, don't select" window-noselect)
4073 (const :tag "New frame" frame)
4074 (const :tag "Bury in new buffer" bury)
4075 (const :tag "Use current buffer" buffer)
4076 (const :tag "Use current buffer" t)))
4078 (defcustom erc-query-on-unjoined-chan-privmsg t
4079 "If non-nil create query buffer on receiving any PRIVMSG at all.
4080 This includes PRIVMSGs directed to channels. If you are using an IRC
4081 bouncer, such as dircproxy, to keep a log of channels when you are
4082 disconnected, you should set this option to t."
4086 (defcustom erc-format-query-as-channel-p t
4087 "If non-nil, format text from others in a query buffer like in a channel,
4088 otherwise format like a private message."
4092 (defcustom erc-minibuffer-notice nil
4093 "If non-nil, print ERC notices for the user in the minibuffer.
4094 Only happens when the session buffer isn't visible."
4098 (defcustom erc-minibuffer-ignored nil
4099 "If non-nil, print a message in the minibuffer if we ignored something."
4103 (defun erc-wash-quit-reason (reason nick login host)
4104 "Remove duplicate text from quit REASON.
4105 Specifically in relation to NICK (user@host) information. Returns REASON
4106 unmodified if nothing can be removed.
4107 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
4108 \"Read error: 110\". The same applies for \"Ping Timeout\"."
4109 (setq nick (regexp-quote nick)
4110 login (regexp-quote login)
4111 host (regexp-quote host))
4112 (or (when (string-match (concat "^\\(Read error\\) to "
4113 nick "\\[" host "\\]: "
4116 (concat (match-string 1 reason) ": " (match-string 2 reason)))
4117 (when (string-match (concat "^\\(Ping timeout\\) for "
4118 nick "\\[" host "\\]$")
4120 (match-string 1 reason))
4123 (defun erc-nickname-in-use (nick reason)
4124 "If NICK is unavailable, tell the user the REASON.
4126 See also `erc-display-error-notice'."
4127 (if (or (not erc-try-new-nick-p)
4128 ;; how many default-nicks are left + one more try...
4129 (eq erc-nick-change-attempt-count
4130 (if (consp erc-nick)
4131 (+ (length erc-nick) 1)
4133 (erc-display-error-notice
4135 (format "Nickname %s is %s, try another." nick reason))
4136 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
4137 (let ((newnick (nth 1 erc-default-nicks))
4138 (nicklen (cdr (assoc "NICKLEN"
4139 (erc-with-server-buffer
4140 erc-server-parameters)))))
4141 (setq erc-bad-nick t)
4142 ;; try to use a different nick
4143 (if erc-default-nicks
4144 (setq erc-default-nicks (cdr erc-default-nicks)))
4146 (setq newnick (concat (truncate-string-to-width
4148 (if (and erc-server-connected nicklen)
4149 (- (string-to-number nicklen)
4150 (length erc-nick-uniquifier))
4151 ;; rfc2812 max nick length = 9
4152 ;; we must assume this is the
4153 ;; server's setting if we haven't
4154 ;; established a connection yet
4155 (- 9 (length erc-nick-uniquifier))))
4156 erc-nick-uniquifier)))
4157 (erc-cmd-NICK newnick)
4158 (erc-display-error-notice
4160 (format "Nickname %s is %s, trying %s"
4161 nick reason newnick)))))
4165 (defgroup erc-server-hooks nil
4166 "Server event callbacks.
4167 Every server event - like numeric replies - has its own hook.
4168 Those hooks are all called using `run-hook-with-args-until-success'.
4169 They receive as first argument the process object from where the event
4171 and as second argument the event parsed as a vector."
4174 (defun erc-display-server-message (_proc parsed)
4175 "Display the message sent by the server as a notice."
4176 (erc-display-message
4177 parsed 'notice 'active (erc-response.contents parsed)))
4179 (defun erc-auto-query (proc parsed)
4180 ;; FIXME: This needs more documentation, unless it's not a user function --
4181 ;; Lawrence 2004-01-08
4182 "Put this on `erc-server-PRIVMSG-functions'."
4183 (when erc-auto-query
4184 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
4185 (target (car (erc-response.command-args parsed)))
4186 (msg (erc-response.contents parsed))
4187 (query (if (not erc-query-on-unjoined-chan-privmsg)
4189 (if (erc-current-nick-p target)
4192 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
4193 (or erc-query-on-unjoined-chan-privmsg
4194 (string= target (erc-current-nick)))
4195 (not (erc-get-buffer query proc))
4196 (not (erc-is-message-ctcp-and-not-action-p msg))
4197 (let ((erc-query-display erc-auto-query))
4198 (erc-cmd-QUERY query))
4201 (defun erc-is-message-ctcp-p (message)
4202 "Check if MESSAGE is a CTCP message or not."
4203 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
4205 (defun erc-is-message-ctcp-and-not-action-p (message)
4206 "Check if MESSAGE is a CTCP message or not."
4207 (and (erc-is-message-ctcp-p message)
4208 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
4210 (defun erc-format-privmessage (nick msg privp msgp)
4211 "Format a PRIVMSG in an insertable fashion."
4212 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
4213 (mark-e (if msgp (if privp "*" ">") "-"))
4214 (str (format "%s%s%s %s" mark-s nick mark-e msg))
4215 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
4216 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
4217 ;; add text properties to text before the nick, the nick and after the nick
4218 (erc-put-text-property 0 (length mark-s) 'face msg-face str)
4219 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
4220 'face nick-face str)
4221 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
4225 (defcustom erc-format-nick-function 'erc-format-nick
4226 "Function to format a nickname for message display."
4230 (defun erc-format-nick (&optional user _channel-data)
4231 "Return the nickname of USER.
4232 See also `erc-format-nick-function'."
4233 (when user (erc-server-user-nickname user)))
4235 (defun erc-get-user-mode-prefix (user)
4237 (cond ((erc-channel-user-owner-p user)
4238 (erc-propertize "~" 'help-echo "owner"))
4239 ((erc-channel-user-admin-p user)
4240 (erc-propertize "&" 'help-echo "admin"))
4241 ((erc-channel-user-op-p user)
4242 (erc-propertize "@" 'help-echo "operator"))
4243 ((erc-channel-user-halfop-p user)
4244 (erc-propertize "%" 'help-echo "half-op"))
4245 ((erc-channel-user-voice-p user)
4246 (erc-propertize "+" 'help-echo "voice"))
4249 (defun erc-format-@nick (&optional user channel-data)
4250 "Format the nickname of USER showing if USER has a voice, is an
4251 operator, half-op, admin or owner. Owners have \"~\", admins have
4252 \"&\", operators have \"@\" and users with voice have \"+\" as a
4253 prefix. Use CHANNEL-DATA to determine op and voice status. See
4254 also `erc-format-nick-function'."
4256 (let ((nick (erc-server-user-nickname user)))
4257 (concat (erc-propertize
4258 (erc-get-user-mode-prefix nick)
4259 'face 'erc-nick-prefix-face)
4262 (defun erc-format-my-nick ()
4263 "Return the beginning of this user's message, correctly propertized."
4264 (if erc-show-my-nick
4267 (nick (erc-current-nick))
4268 (mode (erc-get-user-mode-prefix nick)))
4270 (erc-propertize open 'face 'erc-default-face)
4271 (erc-propertize mode 'face 'erc-my-nick-prefix-face)
4272 (erc-propertize nick 'face 'erc-my-nick-face)
4273 (erc-propertize close 'face 'erc-default-face)))
4274 (let ((prefix "> "))
4275 (erc-propertize prefix 'face 'erc-default-face))))
4277 (defun erc-echo-notice-in-default-buffer (s parsed buffer _sender)
4278 "Echos a private notice in the default buffer, namely the
4279 target buffer specified by BUFFER, or there is no target buffer,
4280 the server buffer. This function is designed to be added to
4281 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
4282 and always returns t."
4283 (erc-display-message parsed nil buffer s)
4286 (defun erc-echo-notice-in-target-buffer (s parsed buffer _sender)
4287 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
4288 function is designed to be added to either `erc-echo-notice-hook'
4289 or `erc-echo-notice-always-hook', and returns non-nil if BUFFER
4292 (progn (erc-display-message parsed nil buffer s) t)
4295 (defun erc-echo-notice-in-minibuffer (s _parsed _buffer _sender)
4296 "Echos a private notice in the minibuffer. This function is
4297 designed to be added to either `erc-echo-notice-hook' or
4298 `erc-echo-notice-always-hook', and always returns t."
4299 (message "%s" (concat "NOTICE: " s))
4302 (defun erc-echo-notice-in-server-buffer (s parsed _buffer _sender)
4303 "Echos a private notice in the server buffer. This function is
4304 designed to be added to either `erc-echo-notice-hook' or
4305 `erc-echo-notice-always-hook', and always returns t."
4306 (erc-display-message parsed nil nil s)
4309 (defun erc-echo-notice-in-active-non-server-buffer (s parsed _buffer _sender)
4310 "Echos a private notice in the active buffer if the active
4311 buffer is not the server buffer. This function is designed to be
4312 added to either `erc-echo-notice-hook' or
4313 `erc-echo-notice-always-hook', and returns non-nil if the active
4314 buffer is not the server buffer."
4315 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
4316 (progn (erc-display-message parsed nil 'active s) t)
4319 (defun erc-echo-notice-in-active-buffer (s parsed _buffer _sender)
4320 "Echos a private notice in the active buffer. This function is
4321 designed to be added to either `erc-echo-notice-hook' or
4322 `erc-echo-notice-always-hook', and always returns t."
4323 (erc-display-message parsed nil 'active s)
4326 (defun erc-echo-notice-in-user-buffers (s parsed _buffer sender)
4327 "Echos a private notice in all of the buffers for which SENDER
4328 is a member. This function is designed to be added to either
4329 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4330 returns non-nil if there is at least one buffer for which the
4333 See also: `erc-echo-notice-in-first-user-buffer',
4334 `erc-buffer-list-with-nick'."
4335 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4337 (progn (erc-display-message parsed nil buffers s) t)
4340 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
4341 "Echos a private notice in BUFFER and in all of the buffers for
4342 which SENDER is a member. This function is designed to be added
4343 to either `erc-echo-notice-hook' or
4344 `erc-echo-notice-always-hook', and returns non-nil if there is
4345 at least one buffer for which the sender is a member or the
4348 See also: `erc-echo-notice-in-user-buffers',
4349 `erc-buffer-list-with-nick'."
4350 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4351 (unless (memq buffer buffers) (push buffer buffers))
4352 (if buffers ;FIXME: How could it be nil?
4353 (progn (erc-display-message parsed nil buffers s) t)
4356 (defun erc-echo-notice-in-first-user-buffer (s parsed _buffer sender)
4357 "Echos a private notice in one of the buffers for which SENDER
4358 is a member. This function is designed to be added to either
4359 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4360 returns non-nil if there is at least one buffer for which the
4363 See also: `erc-echo-notice-in-user-buffers',
4364 `erc-buffer-list-with-nick'."
4365 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4367 (progn (erc-display-message parsed nil (car buffers) s) t)
4370 ;;; Ban manipulation
4372 (defun erc-banlist-store (proc parsed)
4373 "Record ban entries for a channel."
4374 (pcase-let ((`(,channel ,mask ,whoset)
4375 (cdr (erc-response.command-args parsed))))
4376 ;; Determine to which buffer the message corresponds
4377 (let ((buffer (erc-get-buffer channel proc)))
4378 (with-current-buffer buffer
4379 (unless (member (cons whoset mask) erc-channel-banlist)
4380 (setq erc-channel-banlist (cons (cons whoset mask)
4381 erc-channel-banlist))))))
4384 (defun erc-banlist-finished (proc parsed)
4385 "Record that we have received the banlist."
4386 (let* ((channel (nth 1 (erc-response.command-args parsed)))
4387 (buffer (erc-get-buffer channel proc)))
4388 (with-current-buffer buffer
4389 (put 'erc-channel-banlist 'received-from-server t)))
4390 t) ; suppress the 'end of banlist' message
4392 (defun erc-banlist-update (proc parsed)
4393 "Check MODE commands for bans and update the banlist appropriately."
4394 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
4395 (let* ((tgt (car (erc-response.command-args parsed)))
4396 (mode (erc-response.contents parsed))
4397 (whoset (erc-response.sender parsed))
4398 (buffer (erc-get-buffer tgt proc)))
4400 (with-current-buffer buffer
4401 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
4402 ((string-match "^\\([+-]\\)b" mode)
4405 ((string-match "^-" mode)
4406 ;; Remove the unbanned masks from the ban list
4407 (setq erc-channel-banlist
4410 (member (upcase (cdr y))
4412 (cdr (split-string mode)))))
4413 erc-channel-banlist)))
4414 ((string-match "^+" mode)
4415 ;; Add the banned mask(s) to the ban list
4418 (unless (member (cons whoset mask) erc-channel-banlist)
4419 (setq erc-channel-banlist
4420 (cons (cons whoset mask) erc-channel-banlist))))
4421 (cdr (split-string mode))))))))))
4424 ;; used for the banlist cmds
4425 (defun erc-group-list (list n)
4426 "Group LIST into sublists of length N."
4427 (cond ((null list) nil)
4428 ((null (nthcdr n list)) (list list))
4429 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
4434 (defun erc-handle-login ()
4435 "Handle the logging in process of connection."
4436 (unless erc-logged-in
4437 (setq erc-logged-in t)
4438 (message "Logging in as \'%s\'... done" (erc-current-nick))
4439 ;; execute a startup script
4440 (let ((f (erc-select-startup-file)))
4442 (erc-load-script f)))))
4444 (defun erc-connection-established (proc parsed)
4445 "Run just after connection.
4447 Set user modes and run `erc-after-connect' hook."
4448 (with-current-buffer (process-buffer proc)
4449 (unless erc-server-connected ; only once per session
4450 (let ((server (or erc-server-announced-name
4451 (erc-response.sender parsed)))
4452 (nick (car (erc-response.command-args parsed)))
4453 (buffer (process-buffer proc)))
4454 (setq erc-server-connected t)
4455 (erc-update-mode-line)
4456 (erc-set-initial-user-mode nick buffer)
4457 (erc-server-setup-periodical-ping buffer)
4458 (run-hook-with-args 'erc-after-connect server nick)))))
4460 (defun erc-set-initial-user-mode (nick buffer)
4461 "If `erc-user-mode' is non-nil for NICK, set the user modes.
4462 The server buffer is given by BUFFER."
4463 (with-current-buffer buffer
4465 (let ((mode (if (functionp erc-user-mode)
4466 (funcall erc-user-mode)
4468 (when (stringp mode)
4469 (erc-log (format "changing mode for %s to %s" nick mode))
4470 (erc-server-send (format "MODE %s %s" nick mode)))))))
4472 (defun erc-display-error-notice (parsed string)
4473 "Display STRING as an error notice.
4475 See also `erc-display-message'."
4476 (erc-display-message
4477 parsed '(notice error) 'active string))
4479 (defun erc-process-ctcp-query (proc parsed nick login host)
4480 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4481 "Process a CTCP query."
4482 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4484 (if (> (length queries) 4)
4485 (erc-display-message
4486 parsed (list 'notice 'error) proc 'ctcp-too-many)
4487 (if (= 0 (length queries))
4488 (erc-display-message
4489 parsed (list 'notice 'error) proc
4490 'ctcp-empty ?n nick)
4492 (let* ((type (upcase (car (split-string (car queries)))))
4493 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4494 (if (and hook (boundp hook))
4495 (if (string-equal type "ACTION")
4496 (run-hook-with-args-until-success
4497 hook proc parsed nick login host
4498 (car (erc-response.command-args parsed))
4501 (if (erc-current-nick-p
4502 (car (erc-response.command-args parsed)))
4503 (erc-display-message
4504 parsed 'error 'active 'ctcp-request
4505 ?n nick ?u login ?h host ?r (car queries))
4506 (erc-display-message
4507 parsed 'error 'active 'ctcp-request-to
4508 ?n nick ?u login ?h host ?r (car queries)
4509 ?t (car (erc-response.command-args parsed)))))
4510 (run-hook-with-args-until-success
4511 hook proc nick login host
4512 (car (erc-response.command-args parsed))
4514 (erc-display-message
4515 parsed (list 'notice 'error) proc
4517 (setq queries (cdr queries)))))))
4519 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4521 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4522 "Respond to a CTCP ACTION query."
4523 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4524 (let ((s (match-string 1 msg))
4525 (buf (or (erc-get-buffer to proc)
4526 (erc-get-buffer nick proc)
4527 (process-buffer proc))))
4528 (erc-display-message
4530 'ACTION ?n nick ?u login ?h host ?a s))))
4532 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4534 (defun erc-ctcp-query-CLIENTINFO (_proc nick _login _host _to msg)
4535 "Respond to a CTCP CLIENTINFO query."
4536 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4537 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4538 (unless erc-disable-ctcp-replies
4539 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4542 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4543 (defun erc-ctcp-query-ECHO (_proc nick _login _host _to msg)
4544 "Respond to a CTCP ECHO query."
4545 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4546 (let ((s (match-string 1 msg)))
4547 (unless erc-disable-ctcp-replies
4548 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4551 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4552 (defun erc-ctcp-query-FINGER (_proc nick _login _host _to _msg)
4553 "Respond to a CTCP FINGER query."
4554 (unless erc-disable-ctcp-replies
4555 (let ((s (if erc-anonymous-login
4556 (format "FINGER I'm %s." (erc-current-nick))
4557 (format "FINGER %s (%s@%s)."
4561 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4563 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4564 (erc-send-ctcp-notice nick s)))
4567 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4568 (defun erc-ctcp-query-PING (_proc nick _login _host _to msg)
4569 "Respond to a CTCP PING query."
4570 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4571 (unless erc-disable-ctcp-replies
4572 (let ((arg (match-string 1 msg)))
4573 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4576 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4577 (defun erc-ctcp-query-TIME (_proc nick _login _host _to _msg)
4578 "Respond to a CTCP TIME query."
4579 (unless erc-disable-ctcp-replies
4580 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4583 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4584 (defun erc-ctcp-query-USERINFO (_proc nick _login _host _to _msg)
4585 "Respond to a CTCP USERINFO query."
4586 (unless erc-disable-ctcp-replies
4587 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4590 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4591 (defun erc-ctcp-query-VERSION (_proc nick _login _host _to _msg)
4592 "Respond to a CTCP VERSION query."
4593 (unless erc-disable-ctcp-replies
4594 (erc-send-ctcp-notice
4596 "VERSION \C-bERC\C-b - an IRC client for Emacs %s (\C-b%s\C-b)"
4598 erc-official-location)))
4601 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4602 "Process MSG as a CTCP reply."
4603 (let* ((type (car (split-string msg)))
4604 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4606 (run-hook-with-args-until-success
4607 hook proc nick login host
4608 (car (erc-response.command-args parsed)) msg)
4609 (erc-display-message
4610 parsed 'notice 'active
4611 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4613 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4614 (defun erc-ctcp-reply-ECHO (_proc nick _login _host _to msg)
4615 "Handle a CTCP ECHO reply."
4616 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4617 (let ((message (match-string 1 msg)))
4618 (erc-display-message
4619 nil '(notice action) 'active
4620 'CTCP-ECHO ?n nick ?m message)))
4623 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4624 (defun erc-ctcp-reply-CLIENTINFO (_proc nick _login _host _to msg)
4625 "Handle a CTCP CLIENTINFO reply."
4626 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4627 (let ((message (match-string 1 msg)))
4628 (erc-display-message
4630 'CTCP-CLIENTINFO ?n nick ?m message)))
4633 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4634 (defun erc-ctcp-reply-FINGER (_proc nick _login _host _to msg)
4635 "Handle a CTCP FINGER reply."
4636 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4637 (let ((message (match-string 1 msg)))
4638 (erc-display-message
4640 'CTCP-FINGER ?n nick ?m message)))
4643 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4644 (defun erc-ctcp-reply-PING (_proc nick _login _host _to msg)
4645 "Handle a CTCP PING reply."
4646 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4648 (let ((time (match-string 1 msg)))
4650 (let ((delta (erc-time-diff (string-to-number time)
4651 (erc-current-time))))
4652 (erc-display-message
4655 ?t (erc-sec-to-time delta)))
4657 (erc-display-message
4659 'bad-ping-response ?n nick ?t time))))))
4661 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4662 (defun erc-ctcp-reply-TIME (_proc nick _login _host _to msg)
4663 "Handle a CTCP TIME reply."
4664 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4665 (let ((message (match-string 1 msg)))
4666 (erc-display-message
4668 'CTCP-TIME ?n nick ?m message)))
4671 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4672 (defun erc-ctcp-reply-VERSION (_proc nick _login _host _to msg)
4673 "Handle a CTCP VERSION reply."
4674 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4675 (let ((message (match-string 1 msg)))
4676 (erc-display-message
4678 'CTCP-VERSION ?n nick ?m message)))
4681 (defun erc-process-away (proc away-p)
4682 "Toggle the away status of the user depending on the value of AWAY-P.
4684 If nil, set the user as away.
4685 If non-nil, return from being away."
4686 (let ((sessionbuf (process-buffer proc)))
4688 (with-current-buffer sessionbuf
4689 (when erc-away-nickname
4690 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4691 erc-away-nickname away-p))
4692 (erc-cmd-NICK (if away-p
4697 (setq erc-away (current-time)))
4699 (let ((away-time erc-away))
4700 ;; away must be set to NIL BEFORE sending anything to prevent
4701 ;; an infinite recursion
4703 (with-current-buffer (erc-active-buffer)
4704 (when erc-public-away-p
4706 (erc-default-target)
4708 (format "is back (gone for %s)"
4711 (erc-emacs-time-to-erc-time away-time)
4712 (erc-current-time))))
4714 (erc-update-mode-line)))
4716 ;;;; List of channel members handling
4718 (defun erc-channel-begin-receiving-names ()
4721 Used when a channel names list is about to be received. Should
4722 be called with the current buffer set to the channel buffer.
4724 See also `erc-channel-end-receiving-names'."
4725 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4727 (defun erc-channel-end-receiving-names ()
4730 Used to fix `erc-channel-users' after a channel names list has been
4731 received. Should be called with the current buffer set to the
4734 See also `erc-channel-begin-receiving-names'."
4735 (maphash (lambda (nick _user)
4736 (if (null (gethash nick erc-channel-new-member-names))
4737 (erc-remove-channel-user nick)))
4739 (setq erc-channel-new-member-names nil))
4741 (defun erc-parse-prefix ()
4742 "Return an alist of valid prefix character types and their representations.
4743 Example: (operator) o => @, (voiced) v => +."
4744 (let ((str (or (cdr (assoc "PREFIX" (erc-with-server-buffer
4745 erc-server-parameters)))
4746 ;; provide a sane default
4749 (when (string-match "^(\\([^)]+\\))\\(.+\\)$" str)
4750 (setq types (match-string 1 str)
4751 chars (match-string 2 str))
4752 (let ((len (min (length types) (length chars)))
4756 (setq alist (cons (cons (elt types i) (elt chars i))
4761 (defun erc-channel-receive-names (names-string)
4762 "This function is for internal use only.
4764 Update `erc-channel-users' according to NAMES-STRING.
4765 NAMES-STRING is a string listing some of the names on the
4767 (let* ((prefix (erc-parse-prefix))
4768 (voice-ch (cdr (assq ?v prefix)))
4769 (op-ch (cdr (assq ?o prefix)))
4770 (hop-ch (cdr (assq ?h prefix)))
4771 (adm-ch (cdr (assq ?a prefix)))
4772 (own-ch (cdr (assq ?q prefix)))
4773 names name op voice halfop admin owner)
4774 (setq names (delete "" (split-string names-string)))
4775 (let ((erc-channel-members-changed-hook nil))
4776 (dolist (item names)
4778 (setq name item op 'off voice 'off halfop 'off admin 'off owner 'off)
4779 (if (rassq (elt item 0) prefix)
4780 (if (= (length item) 1)
4782 (setq name (substring item 1))
4783 (setf (pcase (aref item 0)
4784 ((pred (eq voice-ch)) voice)
4785 ((pred (eq hop-ch)) halfop)
4786 ((pred (eq op-ch)) op)
4787 ((pred (eq adm-ch)) admin)
4788 ((pred (eq own-ch)) owner))
4791 (puthash (erc-downcase name) t
4792 erc-channel-new-member-names)
4793 (erc-update-current-channel-member
4794 name name t voice halfop op admin owner)))))
4795 (run-hooks 'erc-channel-members-changed-hook)))
4798 (defcustom erc-channel-members-changed-hook nil
4799 "This hook is called every time the variable `channel-members' changes.
4800 The buffer where the change happened is current while this hook is called."
4804 (defun erc-update-user-nick (nick &optional new-nick
4805 host login full-name info)
4806 "Update the stored user information for the user with nickname NICK.
4808 See also: `erc-update-user'."
4809 (erc-update-user (erc-get-server-user nick) new-nick
4810 host login full-name info))
4812 (defun erc-update-user (user &optional new-nick
4813 host login full-name info)
4814 "Update user info for USER. USER must be an erc-server-user
4815 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4816 non-nil and not equal to the existing values for USER are used to
4817 replace the stored values in USER.
4819 If, and only if, a change is made,
4820 `erc-channel-members-changed-hook' is run for each channel for
4821 which USER is a member, and t is returned."
4825 (not (equal (erc-server-user-nickname user)
4828 (erc-change-user-nickname user new-nick))
4830 (not (equal (erc-server-user-host user) host)))
4832 (setf (erc-server-user-host user) host))
4834 (not (equal (erc-server-user-login user) login)))
4836 (setf (erc-server-user-login user) login))
4837 (when (and full-name
4838 (not (equal (erc-server-user-full-name user)
4841 (setf (erc-server-user-full-name user) full-name))
4843 (not (equal (erc-server-user-info user) info)))
4845 (setf (erc-server-user-info user) info))
4847 (dolist (buf (erc-server-user-buffers user))
4848 (if (buffer-live-p buf)
4849 (with-current-buffer buf
4850 (run-hooks 'erc-channel-members-changed-hook))))))
4853 (defun erc-update-current-channel-member
4854 (nick new-nick &optional add voice halfop op admin owner host login full-name info
4855 update-message-time)
4856 "Update the stored user information for the user with nickname NICK.
4857 `erc-update-user' is called to handle changes to nickname,
4858 HOST, LOGIN, FULL-NAME, and INFO. If VOICE HALFOP OP ADMIN or OWNER
4859 are non-nil, they must be equal to either `on' or `off', in which
4860 case the status of the user in the current channel is changed accordingly.
4861 If UPDATE-MESSAGE-TIME is non-nil, the last-message-time of the user
4862 in the current channel is set to (current-time).
4864 If ADD is non-nil, the user will be added with the specified
4865 information if it is not already present in the user or channel
4868 If, and only if, changes are made, or the user is added,
4869 `erc-channel-members-updated-hook' is run, and t is returned.
4871 See also: `erc-update-user' and `erc-update-channel-member'."
4872 (let* (changed user-changed
4873 (channel-data (erc-get-channel-user nick))
4874 (cuser (cdr channel-data))
4875 (user (if channel-data (car channel-data)
4876 (erc-get-server-user nick))))
4879 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4881 (not (eq (erc-channel-user-voice cuser) voice)))
4883 (setf (erc-channel-user-voice cuser)
4884 (cond ((eq voice 'on) t)
4885 ((eq voice 'off) nil)
4888 (not (eq (erc-channel-user-halfop cuser) halfop)))
4890 (setf (erc-channel-user-halfop cuser)
4891 (cond ((eq halfop 'on) t)
4892 ((eq halfop 'off) nil)
4895 (not (eq (erc-channel-user-op cuser) op)))
4897 (setf (erc-channel-user-op cuser)
4898 (cond ((eq op 'on) t)
4902 (not (eq (erc-channel-user-admin cuser) admin)))
4904 (setf (erc-channel-user-admin cuser)
4905 (cond ((eq admin 'on) t)
4906 ((eq admin 'off) nil)
4909 (not (eq (erc-channel-user-owner cuser) owner)))
4911 (setf (erc-channel-user-owner cuser)
4912 (cond ((eq owner 'on) t)
4913 ((eq owner 'off) nil)
4915 (when update-message-time
4916 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4918 (erc-update-user user new-nick
4919 host login full-name info)))
4923 (setq user (make-erc-server-user
4926 :full-name full-name
4929 :buffers (list (current-buffer))))
4930 (erc-add-server-user nick user))
4931 (setf (erc-server-user-buffers user)
4932 (cons (current-buffer)
4933 (erc-server-user-buffers user))))
4934 (setq cuser (make-erc-channel-user
4935 :voice (cond ((eq voice 'on) t)
4936 ((eq voice 'off) nil)
4938 :halfop (cond ((eq halfop 'on) t)
4939 ((eq halfop 'off) nil)
4941 :op (cond ((eq op 'on) t)
4944 :admin (cond ((eq admin 'on) t)
4945 ((eq admin 'off) nil)
4947 :owner (cond ((eq owner 'on) t)
4948 ((eq owner 'off) nil)
4951 (if update-message-time (current-time))))
4952 (puthash (erc-downcase nick) (cons user cuser)
4955 (when (and changed (null user-changed))
4956 (run-hooks 'erc-channel-members-changed-hook))
4957 (or changed user-changed add)))
4959 (defun erc-update-channel-member (channel nick new-nick
4960 &optional add voice halfop op admin owner host login
4961 full-name info update-message-time)
4962 "Update user and channel information for the user with
4963 nickname NICK in channel CHANNEL.
4965 See also: `erc-update-current-channel-member'."
4968 (erc-update-current-channel-member nick new-nick add voice halfop op admin owner host
4969 login full-name info
4970 update-message-time)))
4972 (defun erc-remove-current-channel-member (nick)
4973 "Remove NICK from current channel membership list.
4974 Runs `erc-channel-members-changed-hook'."
4975 (let ((channel-data (erc-get-channel-user nick)))
4977 (erc-remove-channel-user nick)
4978 (run-hooks 'erc-channel-members-changed-hook))))
4980 (defun erc-remove-channel-member (channel nick)
4981 "Remove NICK from CHANNEL's membership list.
4983 See also `erc-remove-current-channel-member'."
4986 (erc-remove-current-channel-member nick)))
4988 (defun erc-update-channel-topic (channel topic &optional modify)
4989 "Find a buffer for CHANNEL and set the TOPIC for it.
4991 If optional MODIFY is 'append or 'prepend, then append or prepend the
4992 TOPIC string to the current topic."
4993 (erc-with-buffer (channel)
4994 (cond ((eq modify 'append)
4995 (setq erc-channel-topic (concat erc-channel-topic topic)))
4996 ((eq modify 'prepend)
4997 (setq erc-channel-topic (concat topic erc-channel-topic)))
4998 (t (setq erc-channel-topic topic)))
4999 (erc-update-mode-line-buffer (current-buffer))))
5001 (defun erc-set-modes (tgt mode-string)
5002 "Set the modes for the TGT provided as MODE-STRING."
5003 (let* ((modes (erc-parse-modes mode-string))
5004 (add-modes (nth 0 modes))
5005 ;; list of triples: (mode-char 'on/'off argument)
5006 (arg-modes (nth 2 modes)))
5007 (cond ((erc-channel-p tgt); channel modes
5008 (let ((buf (and erc-server-process
5009 (erc-get-buffer tgt erc-server-process))))
5011 (with-current-buffer buf
5012 (setq erc-channel-modes add-modes)
5013 (setq erc-channel-user-limit nil)
5014 (setq erc-channel-key nil)
5016 (let ((mode (nth 0 (car arg-modes)))
5017 (onoff (nth 1 (car arg-modes)))
5018 (arg (nth 2 (car arg-modes))))
5019 (cond ((string-match "^[Ll]" mode)
5020 (erc-update-channel-limit tgt onoff arg))
5021 ((string-match "^[Kk]" mode)
5022 (erc-update-channel-key tgt onoff arg))
5024 (setq arg-modes (cdr arg-modes)))
5025 (erc-update-mode-line-buffer buf)))))
5026 ;; we do not keep our nick's modes yet
5027 ;;(t (setq erc-user-modes add-modes))
5031 (defun erc-sort-strings (list-of-strings)
5032 "Sort LIST-OF-STRINGS in lexicographic order.
5035 (sort (copy-sequence list-of-strings) 'string<))
5037 (defun erc-parse-modes (mode-string)
5038 "Parse MODE-STRING into a list.
5040 Returns a list of three elements:
5042 (ADD-MODES REMOVE-MODES ARG-MODES).
5044 The add-modes and remove-modes are lists of single-character strings
5045 for modes without parameters to add and remove respectively. The
5046 arg-modes is a list of triples of the form:
5048 (MODE-CHAR ON/OFF ARGUMENT)."
5049 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
5050 (let ((chars (mapcar 'char-to-string (match-string 1 mode-string)))
5051 ;; arguments in channel modes
5052 (args-str (match-string 2 mode-string))
5056 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
5058 ;; make the argument list
5059 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
5060 (setq args (cons (match-string 1 args-str) args))
5061 (setq args-str (match-string 2 args-str)))
5062 (setq args (nreverse args))
5063 ;; collect what modes changed, and match them with arguments
5065 (cond ((string= (car chars) "+") (setq add-p t))
5066 ((string= (car chars) "-") (setq add-p nil))
5067 ((string-match "^[qaovhbQAOVHB]" (car chars))
5068 (setq arg-modes (cons (list (car chars)
5070 (if args (car args) nil))
5072 (if args (setq args (cdr args))))
5073 ((string-match "^[LlKk]" (car chars))
5074 (setq arg-modes (cons (list (car chars)
5076 (if (and add-p args)
5079 (if (and add-p args) (setq args (cdr args))))
5080 (add-p (setq add-modes (cons (car chars) add-modes)))
5081 (t (setq remove-modes (cons (car chars) remove-modes))))
5082 (setq chars (cdr chars)))
5083 (setq add-modes (nreverse add-modes))
5084 (setq remove-modes (nreverse remove-modes))
5085 (setq arg-modes (nreverse arg-modes))
5086 (list add-modes remove-modes arg-modes))
5089 (defun erc-update-modes (tgt mode-string &optional nick host login)
5090 "Update the mode information for TGT, provided as MODE-STRING.
5091 Optional arguments: NICK, HOST and LOGIN - the attributes of the
5092 person who changed the modes."
5093 ;; FIXME: neither of nick, host, and login are used!
5094 (let* ((modes (erc-parse-modes mode-string))
5095 (add-modes (nth 0 modes))
5096 (remove-modes (nth 1 modes))
5097 ;; list of triples: (mode-char 'on/'off argument)
5098 (arg-modes (nth 2 modes)))
5099 ;; now parse the modes changes and do the updates
5100 (cond ((erc-channel-p tgt); channel modes
5101 (let ((buf (and erc-server-process
5102 (erc-get-buffer tgt erc-server-process))))
5104 ;; FIXME! This used to have an original buffer
5105 ;; variable, but it never switched back to the original
5106 ;; buffer. Is this wanted behavior?
5108 (if (not (boundp 'erc-channel-modes))
5109 (setq erc-channel-modes nil))
5111 (setq erc-channel-modes (delete (car remove-modes)
5113 remove-modes (cdr remove-modes)))
5115 (setq erc-channel-modes (cons (car add-modes)
5117 add-modes (cdr add-modes)))
5118 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
5120 (let ((mode (nth 0 (car arg-modes)))
5121 (onoff (nth 1 (car arg-modes)))
5122 (arg (nth 2 (car arg-modes))))
5123 (cond ((string-match "^[Vv]" mode)
5124 (erc-update-channel-member tgt arg arg nil onoff))
5125 ((string-match "^[hH]" mode)
5126 (erc-update-channel-member tgt arg arg nil nil onoff))
5127 ((string-match "^[oO]" mode)
5128 (erc-update-channel-member tgt arg arg nil nil nil onoff))
5129 ((string-match "^[aA]" mode)
5130 (erc-update-channel-member tgt arg arg nil nil nil nil onoff))
5131 ((string-match "^[qQ]" mode)
5132 (erc-update-channel-member tgt arg arg nil nil nil nil nil onoff))
5133 ((string-match "^[Ll]" mode)
5134 (erc-update-channel-limit tgt onoff arg))
5135 ((string-match "^[Kk]" mode)
5136 (erc-update-channel-key tgt onoff arg))
5137 (t nil)); only ops are tracked now
5138 (setq arg-modes (cdr arg-modes))))
5139 (erc-update-mode-line buf))))
5140 ;; nick modes - ignored at this point
5143 (defun erc-update-channel-limit (channel onoff n)
5144 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
5145 "Update CHANNEL's user limit to N."
5146 (if (or (not (eq onoff 'on))
5147 (and (stringp n) (string-match "^[0-9]+$" n)))
5150 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
5151 (t (setq erc-channel-user-limit nil))))))
5153 (defun erc-update-channel-key (channel onoff key)
5154 "Update CHANNEL's key to KEY if ONOFF is 'on or to nil if it's 'off."
5157 (cond ((eq onoff 'on) (setq erc-channel-key key))
5158 (t (setq erc-channel-key nil)))))
5160 (defun erc-handle-user-status-change (type nlh &optional l)
5161 "Handle changes in any user's status.
5163 So far, only nick change is handled.
5165 Generally, the TYPE argument is a symbol describing the change type, NLH is
5166 a list containing the original nickname, login name and hostname for the user,
5167 and L is a list containing additional TYPE-specific arguments.
5169 So far the following TYPE/L pairs are supported:
5173 nickname change 'nick (NEW-NICK)"
5174 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
5182 (defun erc-highlight-notice (s)
5183 "Highlight notice message S and return it.
5184 See also variable `erc-notice-highlight-type'."
5186 ((eq erc-notice-highlight-type 'prefix)
5187 (erc-put-text-property 0 (length erc-notice-prefix)
5188 'face 'erc-notice-face s)
5190 ((eq erc-notice-highlight-type 'all)
5191 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
5195 (defun erc-make-notice (message)
5196 "Notify the user of MESSAGE."
5197 (when erc-minibuffer-notice
5198 (message "%s" message))
5199 (erc-highlight-notice (concat erc-notice-prefix message)))
5201 (defun erc-highlight-error (s)
5202 "Highlight error message S and return it."
5203 (erc-put-text-property 0 (length s) 'face 'erc-error-face s)
5206 (defun erc-put-text-property (start end property value &optional object)
5207 "Set text-property for an object (usually a string).
5208 START and END define the characters covered.
5209 PROPERTY is the text-property set, usually the symbol `face'.
5210 VALUE is the value for the text-property, usually a face symbol such as
5211 the face `bold' or `erc-pal-face'.
5212 OBJECT is a string which will be modified and returned.
5213 OBJECT is modified without being copied first.
5215 You can redefine or `defadvice' this function in order to add
5216 EmacsSpeak support."
5217 (put-text-property start end property value object))
5219 (defun erc-list (thing)
5220 "Return THING if THING is a list, or a list with THING as its element."
5225 (defun erc-parse-user (string)
5226 "Parse STRING as a user specification (nick!login@host).
5228 Return a list of the three separate tokens."
5230 ((string-match "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$" string)
5231 (list (match-string 1 string)
5232 (match-string 2 string)
5233 (match-string 3 string)))
5234 ;; Some bogus bouncers send Nick!(null), try to live with that.
5235 ((string-match "^\\([^!\n]*\\)!\\(.*\\)$" string)
5236 (list (match-string 1 string)
5238 (match-string 2 string)))
5240 (list string "" ""))))
5242 (defun erc-extract-nick (string)
5243 "Return the nick corresponding to a user specification STRING.
5245 See also `erc-parse-user'."
5246 (car (erc-parse-user string)))
5248 (defun erc-put-text-properties (start end properties
5249 &optional object value-list)
5250 "Set text-properties for OBJECT.
5252 START and END describe positions in OBJECT.
5253 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
5254 each property to the corresponding value in VALUE-LIST."
5256 (setq value-list (mapcar (lambda (_x) t)
5258 (while (and properties value-list)
5259 (erc-put-text-property
5260 start end (pop properties) (pop value-list) object)))
5262 ;;; Input area handling:
5264 (defun erc-beg-of-input-line ()
5265 "Return the value of `point' at the beginning of the input line.
5267 Specifically, return the position of `erc-insert-marker'."
5268 (or (and (boundp 'erc-insert-marker)
5269 (markerp erc-insert-marker))
5270 (error "erc-insert-marker has no value, please report a bug"))
5271 (marker-position erc-insert-marker))
5273 (defun erc-end-of-input-line ()
5274 "Return the value of `point' at the end of the input line."
5277 (defvar erc-last-input-time 0
5278 "Time of last call to `erc-send-current-line'.
5279 If that function has never been called, the value is 0.")
5281 (defcustom erc-accidental-paste-threshold-seconds nil
5282 "Minimum time, in seconds, before sending new lines via IRC.
5283 If the value is a number, `erc-send-current-line' signals an error
5284 if its previous invocation was fewer than this many seconds ago.
5285 This is useful so that if you accidentally enter large amounts of text
5286 into the ERC buffer, that text is not sent to the IRC server.
5288 If the value is nil, `erc-send-current-line' always considers any
5289 submitted line to be intentional."
5292 :type '(choice number (other :tag "disabled" nil)))
5294 (defun erc-send-current-line ()
5295 "Parse current line and send it to IRC."
5297 (let ((now (float-time)))
5298 (if (or (not erc-accidental-paste-threshold-seconds)
5299 (< erc-accidental-paste-threshold-seconds
5300 (- now erc-last-input-time)))
5303 (if (< (point) (erc-beg-of-input-line))
5304 (erc-error "Point is not in the input area")
5305 (let ((inhibit-read-only t)
5306 (str (erc-user-input))
5307 (old-buf (current-buffer)))
5308 (if (and (not (erc-server-buffer-live-p))
5309 (not (erc-command-no-process-p str)))
5310 (erc-error "ERC: No process running")
5311 (erc-set-active-buffer (current-buffer))
5312 ;; Kill the input and the prompt
5313 (delete-region (erc-beg-of-input-line)
5314 (erc-end-of-input-line))
5316 (erc-send-input str)
5317 ;; Fix the buffer if the command didn't kill it
5318 (when (buffer-live-p old-buf)
5319 (with-current-buffer old-buf
5322 (goto-char (point-max))
5323 (when (processp erc-server-process)
5324 (set-marker (process-mark erc-server-process) (point)))
5325 (set-marker erc-insert-marker (point))
5326 (let ((buffer-modified (buffer-modified-p)))
5327 (erc-display-prompt)
5328 (set-buffer-modified-p buffer-modified))))))
5330 ;; Only when last hook has been run...
5331 (run-hook-with-args 'erc-send-completed-hook str))))
5332 (setq erc-last-input-time now))
5333 (switch-to-buffer "*ERC Accidental Paste Overflow*")
5334 (lwarn 'erc :warning
5335 "You seem to have accidentally pasted some text!"))))
5337 (defun erc-user-input ()
5338 "Return the input of the user in the current buffer."
5339 (buffer-substring-no-properties
5341 (erc-end-of-input-line)))
5343 (defvar erc-command-regexp "^/\\([A-Za-z']+\\)\\(\\s-+.*\\|\\s-*\\)$"
5344 "Regular expression used for matching commands in ERC.")
5346 (defun erc-send-input (input)
5347 "Treat INPUT as typed in by the user. It is assumed that the input
5348 and the prompt is already deleted.
5349 This returns non-nil only if we actually send anything."
5350 ;; Handle different kinds of inputs
5352 ;; Ignore empty input
5353 ((if erc-send-whitespace-lines
5355 (string-match "\\`[ \t\r\f\n]*\\'" input))
5356 (when erc-warn-about-blank-lines
5357 (message "Blank line - ignoring...")
5362 (erc-insert-this t))
5363 (setq erc-send-this t)
5364 (run-hook-with-args 'erc-send-pre-hook input)
5366 (if (or (string-match "\n" str)
5367 (not (string-match erc-command-regexp str)))
5372 ;; Insert what has to be inserted for this.
5373 (erc-display-msg line)
5374 (erc-process-input-line (concat line "\n")
5375 (null erc-flood-protect) t))
5376 (or (and erc-flood-protect (erc-split-line line))
5378 (split-string str "\n"))
5379 (erc-process-input-line (concat str "\n") t nil))
5382 (defun erc-display-command (line)
5383 (when erc-insert-this
5384 (let ((insert-position (point)))
5385 (unless erc-hide-prompt
5386 (erc-display-prompt nil nil (erc-command-indicator)
5387 (and (erc-command-indicator)
5388 'erc-command-indicator-face)))
5389 (let ((beg (point)))
5391 (erc-put-text-property beg (point)
5392 'face 'erc-command-indicator-face)
5394 (when (processp erc-server-process)
5395 (set-marker (process-mark erc-server-process) (point)))
5396 (set-marker erc-insert-marker (point))
5399 (narrow-to-region insert-position (point))
5400 (run-hooks 'erc-send-modify-hook)
5401 (run-hooks 'erc-send-post-hook))))))
5403 (defun erc-display-msg (line)
5404 "Display LINE as a message of the user to the current target at the
5406 (when erc-insert-this
5407 (let ((insert-position (point)))
5408 (insert (erc-format-my-nick))
5409 (let ((beg (point)))
5411 (erc-put-text-property beg (point)
5412 'face 'erc-input-face))
5414 (when (processp erc-server-process)
5415 (set-marker (process-mark erc-server-process) (point)))
5416 (set-marker erc-insert-marker (point))
5419 (narrow-to-region insert-position (point))
5420 (run-hooks 'erc-send-modify-hook)
5421 (run-hooks 'erc-send-post-hook))))))
5423 (defun erc-command-symbol (command)
5424 "Return the ERC command symbol for COMMAND if it exists and is bound."
5425 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
5426 (when (fboundp cmd) cmd)))
5428 (defun erc-extract-command-from-line (line)
5429 "Extract command and args from the input LINE.
5430 If no command was given, return nil. If command matches, return a
5431 list of the form: (command args) where both elements are strings."
5432 (when (string-match erc-command-regexp line)
5433 (let* ((cmd (erc-command-symbol (match-string 1 line)))
5434 ;; note: return is nil, we apply this simply for side effects
5435 (_canon-defun (while (and cmd (symbolp (symbol-function cmd)))
5436 (setq cmd (symbol-function cmd))))
5437 (cmd-fun (or cmd #'erc-cmd-default))
5439 (if (get cmd-fun 'do-not-parse-args)
5440 (format "%s" (match-string 2 line))
5441 (delete "" (split-string (erc-trim-string
5442 (match-string 2 line)) " ")))
5444 (list cmd-fun arg))))
5446 (defun erc-split-multiline-safe (string)
5447 "Split STRING, containing multiple lines and return them in a list.
5448 Do it only for STRING as the complete input, do not carry unfinished
5449 strings over to the next call."
5454 (let ((i (string-match "\r?\n" string i0))
5455 (s (substring string i0)))
5456 (cond (i (setq l (cons (substring string i0 i) l))
5457 (setq i0 (match-end 0)))
5459 (setq l (cons s l))(setq doit nil))
5460 (t (setq doit nil)))))
5465 (defun erc-set-current-nick (nick)
5466 "Set the current nickname to NICK."
5467 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5470 (setq erc-server-current-nick nick)))
5472 (defun erc-current-nick ()
5473 "Return the current nickname."
5474 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5477 erc-server-current-nick))
5479 (defun erc-current-nick-p (nick)
5480 "Return non-nil if NICK is the current nickname."
5481 (erc-nick-equal-p nick (erc-current-nick)))
5483 (defun erc-nick-equal-p (nick1 nick2)
5484 "Return non-nil if NICK1 and NICK2 are the same.
5486 This matches strings according to the IRC protocol's case convention.
5488 See also `erc-downcase'."
5489 (string= (erc-downcase nick1)
5490 (erc-downcase nick2)))
5492 ;; default target handling
5494 (defun erc-default-target ()
5495 "Return the current default target (as a character string) or nil if none."
5496 (let ((tgt (car erc-default-recipients)))
5499 ((listp tgt) (cdr tgt))
5502 (defun erc-add-default-channel (channel)
5503 "Add CHANNEL to the default channel list."
5504 (let ((chl (downcase channel)))
5505 (setq erc-default-recipients
5506 (cons chl erc-default-recipients))))
5508 (defun erc-delete-default-channel (channel &optional buffer)
5509 "Delete CHANNEL from the default channel list."
5510 (with-current-buffer (if (and buffer
5514 (setq erc-default-recipients (delete (downcase channel)
5515 erc-default-recipients))))
5517 (defun erc-add-query (nickname)
5518 "Add QUERY'd NICKNAME to the default channel list.
5520 The previous default target of QUERY type gets removed."
5521 (let ((d1 (car erc-default-recipients))
5522 (d2 (cdr erc-default-recipients))
5523 (qt (cons 'QUERY (downcase nickname))))
5524 (setq erc-default-recipients (cons qt (if (and (listp d1)
5525 (eq (car d1) 'QUERY))
5527 erc-default-recipients)))))
5529 (defun erc-delete-query ()
5530 "Delete the topmost target if it is a QUERY."
5532 (let ((d1 (car erc-default-recipients))
5533 (d2 (cdr erc-default-recipients)))
5535 (eq (car d1) 'QUERY))
5536 (setq erc-default-recipients d2)
5537 (error "Current target is not a QUERY"))))
5539 (defun erc-ignored-user-p (spec)
5540 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5542 Takes a full SPEC of a user in the form \"nick!login@host\", and
5543 matches against all the regexp's in `erc-ignore-list'. If any
5544 match, returns that regexp."
5546 (dolist (ignored (erc-with-server-buffer erc-ignore-list))
5547 (if (string-match ignored spec)
5548 (throw 'found ignored)))))
5550 (defun erc-ignored-reply-p (msg tgt proc)
5551 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5552 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5554 Takes a message MSG to a channel and returns non-nil if the addressed
5555 user matches any regexp in `erc-ignore-reply-list'."
5556 (let ((target-nick (erc-message-target msg)))
5557 (if (not target-nick)
5559 (erc-with-buffer (tgt proc)
5560 (let ((user (erc-get-server-user target-nick)))
5562 (erc-list-match erc-ignore-reply-list
5563 (erc-user-spec user))))))))
5565 (defun erc-message-target (msg)
5566 "Return the addressed target in MSG.
5568 The addressed target is the string before the first colon in MSG."
5569 (if (string-match "^\\([^: \n]*\\):" msg)
5570 (match-string 1 msg)
5573 (defun erc-user-spec (user)
5574 "Create a nick!user@host spec from a user struct."
5575 (let ((nick (erc-server-user-nickname user))
5576 (host (erc-server-user-host user))
5577 (login (erc-server-user-login user)))
5578 (concat (or nick "")
5584 (defun erc-list-match (lst str)
5585 "Return non-nil if any regexp in LST matches STR."
5586 (memq nil (mapcar (lambda (regexp)
5587 (not (string-match regexp str)))
5592 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5593 "Toggle automatic CTCP replies (like VERSION and PING).
5595 If ARG is positive, turns CTCP replies on.
5597 If ARG is non-nil and not positive, turns CTCP replies off."
5599 (cond ((and (numberp arg) (> arg 0))
5600 (setq erc-disable-ctcp-replies t))
5601 (arg (setq erc-disable-ctcp-replies nil))
5602 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5603 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5605 (defun erc-toggle-flood-control (&optional arg)
5606 "Toggle use of flood control on sent messages.
5608 If ARG is positive, use flood control.
5609 If ARG is non-nil and not positive, do not use flood control.
5611 See `erc-server-flood-margin' for an explanation of the available
5612 flood control parameters."
5614 (cond ((and (numberp arg) (> arg 0))
5615 (setq erc-flood-protect t))
5616 (arg (setq erc-flood-protect nil))
5617 (t (setq erc-flood-protect (not erc-flood-protect))))
5618 (message "ERC flood control is %s"
5619 (cond (erc-flood-protect "ON")
5622 ;; Some useful channel and nick commands for fast key bindings
5624 (defun erc-invite-only-mode (&optional arg)
5625 "Turn on the invite only mode (+i) for the current channel.
5627 If ARG is non-nil, turn this mode off (-i).
5629 This command is sent even if excess flood is detected."
5631 (erc-set-active-buffer (current-buffer))
5632 (let ((tgt (erc-default-target)))
5633 (if (or (not tgt) (not (erc-channel-p tgt)))
5634 (erc-display-message nil 'error (current-buffer) 'no-target)
5635 (erc-load-irc-script-lines
5636 (list (concat "/mode " tgt (if arg " -i" " +i")))
5639 (defun erc-get-channel-mode-from-keypress (key)
5640 "Read a key sequence and call the corresponding channel mode function.
5641 After doing C-c C-o, type in a channel mode letter.
5644 RET lets you type more than one mode at a time.
5645 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5646 If \"k\" is pressed, `erc-set-channel-key' gets called.
5647 Anything else will be sent to `erc-toggle-channel-mode'."
5648 (interactive "kChannel mode (RET to set more than one): ")
5649 (when (featurep 'xemacs)
5650 (setq key (char-to-string (event-to-character (aref key 0)))))
5651 (cond ((equal key "\C-g")
5654 (erc-insert-mode-command))
5656 (call-interactively 'erc-set-channel-limit))
5658 (call-interactively 'erc-set-channel-key))
5659 (t (erc-toggle-channel-mode key))))
5661 (defun erc-toggle-channel-mode (mode &optional channel)
5662 "Toggle channel MODE.
5664 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5665 `erc-default-target'."
5667 (erc-set-active-buffer (current-buffer))
5668 (let ((tgt (or channel (erc-default-target))))
5669 (if (or (null tgt) (null (erc-channel-p tgt)))
5670 (erc-display-message nil 'error 'active 'no-target)
5671 (let* ((active (member mode erc-channel-modes))
5672 (newstate (if active "OFF" "ON")))
5673 (erc-log (format "%s: Toggle mode %s %s" tgt mode newstate))
5674 (message "Toggle channel mode %s %s" mode newstate)
5675 (erc-server-send (format "MODE %s %s%s"
5676 tgt (if active "-" "+") mode))))))
5678 (defun erc-insert-mode-command ()
5679 "Insert the line \"/mode <current target> \" at `point'."
5681 (let ((tgt (erc-default-target)))
5682 (if tgt (insert (concat "/mode " tgt " "))
5683 (erc-display-message nil 'error (current-buffer) 'no-target))))
5685 (defun erc-channel-names ()
5686 "Run \"/names #channel\" in the current channel."
5688 (erc-set-active-buffer (current-buffer))
5689 (let ((tgt (erc-default-target)))
5690 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5691 (erc-display-message nil 'error (current-buffer) 'no-target))))
5693 (defun erc-remove-text-properties-region (start end &optional object)
5694 "Clears the region (START,END) in OBJECT from all colors, etc."
5697 (let ((inhibit-read-only t))
5698 (set-text-properties start end nil object))))
5699 (put 'erc-remove-text-properties-region 'disabled t)
5701 ;; script execution and startup
5703 (defun erc-find-file (file &optional path)
5704 "Search for a FILE in the filesystem.
5705 First the `default-directory' is searched for FILE, then any directories
5706 specified in the list PATH.
5708 If FILE is found, return the path to it."
5709 (let ((filepath file))
5710 (if (file-readable-p filepath) filepath
5712 (progn (setq filepath (expand-file-name file (car path)))
5713 (not (file-readable-p filepath))))
5714 (setq path (cdr path)))
5715 (if path filepath nil))))
5717 (defun erc-select-startup-file ()
5718 "Select an ERC startup file.
5719 See also `erc-startup-file-list'."
5721 (dolist (f erc-startup-file-list)
5722 (setq f (convert-standard-filename f))
5723 (when (file-readable-p f)
5724 (throw 'found f)))))
5726 (defun erc-find-script-file (file)
5727 "Search for FILE in `default-directory', and any in `erc-script-path'."
5728 (erc-find-file file erc-script-path))
5730 (defun erc-load-script (file)
5731 "Load a script from FILE.
5733 FILE must be the full name, it is not searched in the
5734 `erc-script-path'. If the filename ends with `.el', then load it
5735 as an Emacs Lisp program. Otherwise, treat it as a regular IRC
5737 (erc-log (concat "erc-load-script: " file))
5739 ((string-match "\\.el\\'" file)
5742 (erc-load-irc-script file))))
5744 (defun erc-process-script-line (line &optional args)
5745 "Process an IRC script LINE.
5747 Does script-specific substitutions (script arguments, current nick,
5748 server, etc.) in LINE and returns it.
5750 Substitutions are: %C and %c = current target (channel or nick),
5751 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5752 where x is any other character;
5753 $* = the entire argument string, $1 = the first argument, $2 = the second,
5755 (if (not args) (setq args ""))
5756 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5757 (percent-regexp "\\(%.\\)")
5758 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5759 (tgt (erc-default-target))
5760 (server (and (boundp 'erc-session-server) erc-session-server))
5761 (nick (erc-current-nick))
5766 (if (not tgt) (setq tgt ""))
5767 (if (not server) (setq server ""))
5768 (if (not nick) (setq nick ""))
5769 ;; First, compute the argument list
5771 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5772 (setq arg-list (cons (match-string 1 tmp) arg-list))
5773 (setq tmp (match-string 2 tmp)))
5774 (setq arg-list (nreverse arg-list))
5775 (setq arg-num (length arg-list))
5776 ;; now do the substitution
5777 (setq tmp (string-match esc-regexp line))
5779 ;;(message "beginning of while: tmp=%S" tmp)
5780 (let* ((hd (substring line 0 tmp))
5783 (tail (substring line tmp)))
5784 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5785 (setq esc (match-string 1 tail))
5786 (setq tail (substring tail (match-end 1))))
5787 ((string-match (concat "^" percent-regexp) tail)
5788 (setq esc (match-string 1 tail))
5789 (setq tail (substring tail (match-end 1)))))
5790 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5791 (setq res (concat res hd))
5793 (cond ((string= esc "") "")
5794 ((string-match "^\\$\\*$" esc) args)
5795 ((string-match "^\\$\\([0-9]+\\)$" esc)
5796 (let ((n (string-to-number (match-string 1 esc))))
5797 (message "n = %S, integerp(n)=%S" n (integerp n))
5798 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5799 ((string-match "^%[Cc]$" esc) tgt)
5800 ((string-match "^%[Ss]$" esc) server)
5801 ((string-match "^%[Nn]$" esc) nick)
5802 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5803 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5804 (message "BUG IN ERC: esc=%S" esc)
5807 (setq tmp (string-match esc-regexp line))
5808 (setq res (concat res subst))
5809 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5811 (setq res (concat res line))
5814 (defun erc-load-irc-script (file &optional force)
5815 "Load an IRC script from FILE."
5816 (erc-log (concat "erc-load-script: " file))
5817 (let ((str (with-temp-buffer
5818 (insert-file-contents file)
5820 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5822 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5823 "Load IRC script LINES (a list of strings).
5825 If optional NOEXPAND is non-nil, do not expand script-specific
5826 sequences, process the lines verbatim. Use this for multiline
5828 (let* ((cb (current-buffer))
5830 (sp (or (erc-command-indicator) (erc-prompt)))
5831 (args (and (boundp 'erc-script-args) erc-script-args)))
5832 (if (and args (string-match "^ " args))
5833 (setq args (substring args 1)))
5834 ;; prepare the prompt string for echo
5835 (erc-put-text-property 0 (length sp)
5836 'face 'erc-command-indicator-face sp)
5838 (setq s (car lines))
5839 (erc-log (concat "erc-load-script: CMD: " s))
5840 (unless (string-match "^\\s-*$" s)
5841 (let ((line (if noexpand s (erc-process-script-line s args))))
5842 (if (and (erc-process-input-line line force)
5845 (erc-put-text-property 0 (length line)
5846 'face 'erc-input-face line)
5847 (erc-display-line (concat sp line) cb)))))
5848 (setq lines (cdr lines)))))
5853 "Perform user authentication at the IRC server."
5854 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5857 (or erc-system-name (system-name))
5859 erc-session-user-full-name))
5860 (if erc-session-password
5861 (erc-server-send (format "PASS %s" erc-session-password))
5862 (message "Logging in without password"))
5863 (erc-server-send (format "NICK %s" (erc-current-nick)))
5865 (format "USER %s %s %s :%s"
5867 (if erc-anonymous-login erc-email-userid (user-login-name))
5869 erc-session-user-full-name))
5870 (erc-update-mode-line))
5872 ;; connection properties' heuristics
5874 (defun erc-determine-parameters (&optional server port nick name)
5875 "Determine the connection and authentication parameters.
5876 Sets the buffer local variables:
5878 - `erc-session-connector'
5879 - `erc-session-server'
5880 - `erc-session-port'
5881 - `erc-session-full-name'
5882 - `erc-server-current-nick'"
5883 (setq erc-session-connector erc-server-connect-function
5884 erc-session-server (erc-compute-server server)
5885 erc-session-port (or port erc-default-port)
5886 erc-session-user-full-name (erc-compute-full-name name))
5887 (erc-set-current-nick (erc-compute-nick nick)))
5889 (defun erc-compute-server (&optional server)
5890 "Return an IRC server name.
5892 This tries a number of increasingly more default methods until a
5893 non-nil value is found.
5895 - SERVER (the argument passed to this function)
5896 - The `erc-server' option
5897 - The value of the IRCSERVER environment variable
5898 - The `erc-default-server' variable"
5901 (getenv "IRCSERVER")
5902 erc-default-server))
5904 (defun erc-compute-nick (&optional nick)
5905 "Return user's IRC nick.
5907 This tries a number of increasingly more default methods until a
5908 non-nil value is found.
5910 - NICK (the argument passed to this function)
5911 - The `erc-nick' option
5912 - The value of the IRCNICK environment variable
5913 - The result from the `user-login-name' function"
5915 (if (consp erc-nick) (car erc-nick) erc-nick)
5920 (defun erc-compute-full-name (&optional full-name)
5921 "Return user's full name.
5923 This tries a number of increasingly more default methods until a
5924 non-nil value is found.
5926 - FULL-NAME (the argument passed to this function)
5927 - The `erc-user-full-name' option
5928 - The value of the IRCNAME environment variable
5929 - The result from the `user-full-name' function"
5933 (if erc-anonymous-login "unknown" nil)
5936 (defun erc-compute-port (&optional port)
5937 "Return a port for an IRC server.
5939 This tries a number of increasingly more default methods until a
5940 non-nil value is found.
5942 - PORT (the argument passed to this function)
5943 - The `erc-port' option
5944 - The `erc-default-port' variable"
5945 (or port erc-port erc-default-port))
5949 (defun erc-string-to-emacs-time (string)
5950 "Convert the long number represented by STRING into an Emacs format.
5951 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
5952 (let* ((n (string-to-number (concat string ".0"))))
5953 (list (truncate (/ n 65536))
5954 (truncate (mod n 65536)))))
5956 (defun erc-emacs-time-to-erc-time (time)
5957 "Convert Emacs TIME to a number of seconds since the epoch."
5959 (+ (* (nth 0 time) 65536.0) (nth 1 time))))
5960 ; (round (+ (* (nth 0 tm) 65536.0) (nth 1 tm))))
5962 (defun erc-current-time ()
5963 "Return the `current-time' as a number of seconds since the epoch.
5965 See also `erc-emacs-time-to-erc-time'."
5966 (erc-emacs-time-to-erc-time (current-time)))
5968 (defun erc-time-diff (t1 t2)
5969 "Return the time difference in seconds between T1 and T2."
5972 (defun erc-time-gt (t1 t2)
5973 "Check whether T1 > T2."
5976 (defun erc-sec-to-time (ns)
5977 "Convert NS to a time string HH:MM.SS."
5978 (setq ns (truncate ns))
5979 (format "%02d:%02d.%02d"
5984 (defun erc-seconds-to-string (seconds)
5985 "Convert a number of SECONDS into an English phrase."
5986 (let (days hours minutes format-args output)
5987 (setq days (/ seconds 86400)
5988 seconds (% seconds 86400)
5989 hours (/ seconds 3600)
5990 seconds (% seconds 3600)
5991 minutes (/ seconds 60)
5992 seconds (% seconds 60)
5993 format-args (if (> days 0)
5994 `("%d days, %d hours, %d minutes, %d seconds"
5995 ,days ,hours ,minutes ,seconds)
5997 `("%d hours, %d minutes, %d seconds"
5998 ,hours ,minutes ,seconds)
6000 `("%d minutes, %d seconds" ,minutes ,seconds)
6001 `("%d seconds" ,seconds))))
6002 output (apply 'format format-args))
6003 ;; Change all "1 units" to "1 unit".
6004 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
6005 (setq output (erc-replace-match-subexpression-in-string
6006 "" output (match-string 2 output) 2 (match-beginning 2))))
6012 (defconst erc-clientinfo-alist
6013 '(("ACTION" . "is used to inform about one's current activity")
6014 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
6015 ("ECHO" . "echoes its arguments back")
6016 ("FINGER" . "shows user's name, location, and idle time")
6017 ("PING" . "measures delay between peers")
6018 ("TIME" . "shows client-side time")
6019 ("USERINFO" . "shows information provided by a user")
6020 ("VERSION" . "shows client type and version"))
6021 "Alist of CTCP CLIENTINFO for ERC commands.")
6023 (defun erc-client-info (s)
6024 "Return CTCP CLIENTINFO on command S.
6025 If S is nil or an empty string then return general CLIENTINFO."
6026 (if (or (not s) (string= s ""))
6030 (concat (car e) " "))
6031 erc-clientinfo-alist))
6032 ": use CLIENTINFO <COMMAND> to get more specific information")
6033 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
6035 (concat s " " (cdr h))
6036 (concat s ": unknown command")))))
6040 (defun erc-directory-writable-p (dir)
6041 "Determine whether DIR is a writable directory.
6042 If it doesn't exist, create it."
6043 (unless (file-attributes dir) (make-directory dir))
6044 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
6046 (defun erc-kill-query-buffers (process)
6047 "Kill all buffers of PROCESS."
6048 ;; here, we only want to match the channel buffers, to avoid
6049 ;; "selecting killed buffers" b0rkage.
6050 (erc-with-all-buffers-of-server process
6052 (not (erc-server-buffer-p)))
6053 (kill-buffer (current-buffer))))
6055 (defun erc-nick-at-point ()
6056 "Give information about the nickname at `point'.
6058 If called interactively, give a human readable message in the
6059 minibuffer. If called programmatically, return the corresponding
6060 entry of `channel-members'."
6062 (require 'thingatpt)
6063 (let* ((word (word-at-point))
6064 (channel-data (erc-get-channel-user word))
6065 (cuser (cdr channel-data))
6066 (user (if channel-data
6068 (erc-get-server-user word)))
6069 host login full-name nick voice halfop op admin owner)
6071 (setq nick (erc-server-user-nickname user)
6072 host (erc-server-user-host user)
6073 login (erc-server-user-login user)
6074 full-name (erc-server-user-full-name user))
6076 (setq voice (erc-channel-user-voice cuser)
6077 halfop (erc-channel-user-halfop cuser)
6078 op (erc-channel-user-op cuser)
6079 admin (erc-channel-user-admin cuser)
6080 owner (erc-channel-user-owner cuser))))
6081 (if (called-interactively-p 'interactive)
6082 (message "%s is %s@%s%s%s"
6084 (if full-name (format " (%s)" full-name) "")
6085 (if (or voice halfop op admin owner)
6086 (format " and is +%s%s%s%s%s on %s"
6092 (erc-default-target))
6096 (defun erc-away-time ()
6097 "Return non-nil if the current ERC process is set away.
6099 In particular, the time that we were set away is returned.
6100 See `current-time' for details on the time format."
6101 (erc-with-server-buffer erc-away))
6103 ;; Mode line handling
6105 (defcustom erc-mode-line-format "%S %a"
6106 "A string to be formatted and shown in the mode-line in `erc-mode'.
6108 The string is formatted using `format-spec' and the result is set as the value
6109 of `mode-line-buffer-identification'.
6111 The following characters are replaced:
6112 %a: String indicating away status or \"\" if you are not away
6113 %l: The estimated lag time to the server
6114 %m: The modes of the channel
6115 %n: The current nick name
6116 %N: The name of the network
6117 %o: The topic of the channel
6118 %p: The session port
6119 %t: The name of the target (channel, nickname, or servername:port)
6120 %s: In the server-buffer, this gets filled with the value of
6121 `erc-server-announced-name', in a channel, the value of
6122 (erc-default-target) also get concatenated.
6123 %S: In the server-buffer, this gets filled with the value of
6124 `erc-network', in a channel, the value of (erc-default-target)
6125 also get concatenated."
6126 :group 'erc-mode-line-and-header
6129 (defcustom erc-header-line-format "%n on %t (%m,%l) %o"
6130 "A string to be formatted and shown in the header-line in `erc-mode'.
6131 Only used starting in Emacs 21.
6133 Set this to nil if you do not want the header line to be
6136 See `erc-mode-line-format' for which characters are can be used."
6137 :group 'erc-mode-line-and-header
6138 :set (lambda (sym val)
6140 (when (fboundp 'erc-update-mode-line)
6141 (erc-update-mode-line nil)))
6142 :type '(choice (const :tag "Disabled" nil)
6145 (defcustom erc-header-line-uses-tabbar-p nil
6146 "Use tabbar mode instead of the header line to display the header."
6147 :group 'erc-mode-line-and-header
6150 (defcustom erc-header-line-uses-help-echo-p t
6151 "Show the contents of the header line in the echo area or as a tooltip
6152 when you move point into the header line."
6153 :group 'erc-mode-line-and-header
6156 (defcustom erc-header-line-face-method nil
6157 "Determine what method to use when colorizing the header line text.
6159 If nil, don't colorize the header text.
6160 If given a function, call it and use the resulting face name.
6161 Otherwise, use the `erc-header-line' face."
6162 :group 'erc-mode-line-and-header
6163 :type '(choice (const :tag "Don't colorize" nil)
6164 (const :tag "Use the erc-header-line face" t)
6165 (function :tag "Call a function")))
6167 (defcustom erc-show-channel-key-p t
6168 "Show the channel key in the header line."
6169 :group 'erc-paranoia
6172 (defcustom erc-mode-line-away-status-format
6173 "(AWAY since %a %b %d %H:%M) "
6174 "When you're away on a server, this is shown in the mode line.
6175 This should be a string with substitution variables recognized by
6176 `format-time-string'."
6177 :group 'erc-mode-line-and-header
6180 (defun erc-shorten-server-name (server-name)
6181 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
6182 (if (stringp server-name)
6184 (insert server-name)
6185 (let ((alist erc-common-server-suffixes))
6187 (goto-char (point-min))
6188 (if (re-search-forward (caar alist) nil t)
6189 (replace-match (cdar alist)))
6190 (setq alist (cdr alist))))
6193 (defun erc-format-target ()
6194 "Return the name of the target (channel or nickname or servername:port)."
6195 (let ((target (erc-default-target)))
6197 (concat (erc-shorten-server-name
6198 (or erc-server-announced-name
6199 erc-session-server))
6200 ":" (erc-port-to-string erc-session-port)))))
6202 (defun erc-format-target-and/or-server ()
6203 "Return the server name or the current target and server name combined."
6204 (let ((server-name (erc-shorten-server-name
6205 (or erc-server-announced-name
6206 erc-session-server))))
6207 (cond ((erc-default-target)
6208 (concat (erc-string-no-properties (erc-default-target))
6210 (server-name server-name)
6211 (t (buffer-name (current-buffer))))))
6213 (defun erc-format-network ()
6214 "Return the name of the network we are currently on."
6215 (let ((network (and (fboundp 'erc-network-name) (erc-network-name))))
6216 (if (and network (symbolp network))
6217 (symbol-name network)
6220 (defun erc-format-target-and/or-network ()
6221 "Return the network or the current target and network combined.
6222 If the name of the network is not available, then use the
6223 shortened server name instead."
6224 (let ((network-name (or (and (fboundp 'erc-network-name) (erc-network-name))
6225 (erc-shorten-server-name
6226 (or erc-server-announced-name
6227 erc-session-server)))))
6228 (when (and network-name (symbolp network-name))
6229 (setq network-name (symbol-name network-name)))
6230 (cond ((erc-default-target)
6231 (concat (erc-string-no-properties (erc-default-target))
6234 (not (get-buffer network-name)))
6235 (when erc-rename-buffers
6236 (rename-buffer network-name))
6238 (t (buffer-name (current-buffer))))))
6240 (defun erc-format-away-status ()
6241 "Return a formatted `erc-mode-line-away-status-format'
6242 if `erc-away' is non-nil."
6243 (let ((a (erc-away-time)))
6245 (format-time-string erc-mode-line-away-status-format a)
6248 (defun erc-format-channel-modes ()
6249 "Return the current channel's modes."
6250 (concat (apply 'concat
6251 "+" erc-channel-modes)
6252 (cond ((and erc-channel-user-limit erc-channel-key)
6253 (if erc-show-channel-key-p
6254 (format "lk %.0f %s" erc-channel-user-limit
6256 (format "kl %.0f" erc-channel-user-limit)))
6257 (erc-channel-user-limit
6258 ;; Emacs has no bignums
6259 (format "l %.0f" erc-channel-user-limit))
6261 (if erc-show-channel-key-p
6262 (format "k %s" erc-channel-key)
6266 (defun erc-format-lag-time ()
6267 "Return the estimated lag time to server, `erc-server-lag'."
6268 (let ((lag (erc-with-server-buffer erc-server-lag)))
6269 (cond (lag (format "lag:%.0f" lag))
6272 ;; erc-goodies is required at end of this file.
6273 (declare-function erc-controls-strip "erc-goodies" (str))
6275 (defvar tabbar--local-hlf)
6277 (defun erc-update-mode-line-buffer (buffer)
6278 "Update the mode line in a single ERC buffer BUFFER."
6279 (with-current-buffer buffer
6280 (let ((spec (format-spec-make
6281 ?a (erc-format-away-status)
6282 ?l (erc-format-lag-time)
6283 ?m (erc-format-channel-modes)
6284 ?n (or (erc-current-nick) "")
6285 ?N (erc-format-network)
6286 ?o (or (erc-controls-strip erc-channel-topic) "")
6287 ?p (erc-port-to-string erc-session-port)
6288 ?s (erc-format-target-and/or-server)
6289 ?S (erc-format-target-and/or-network)
6290 ?t (erc-format-target)))
6291 (process-status (cond ((and (erc-server-process-alive)
6292 (not erc-server-connected))
6294 ((erc-server-process-alive)
6298 (face (cond ((eq erc-header-line-face-method nil)
6300 ((functionp erc-header-line-face-method)
6301 (funcall erc-header-line-face-method))
6303 'erc-header-line))))
6304 (cond ((featurep 'xemacs)
6305 (setq modeline-buffer-identification
6306 (list (format-spec erc-mode-line-format spec)))
6307 (setq modeline-process (list process-status)))
6309 (setq mode-line-buffer-identification
6310 (list (format-spec erc-mode-line-format spec)))
6311 (setq mode-line-process (list process-status))))
6312 (when (boundp 'header-line-format)
6313 (let ((header (if erc-header-line-format
6314 (format-spec erc-header-line-format spec)
6316 (cond (erc-header-line-uses-tabbar-p
6317 (set (make-local-variable 'tabbar--local-hlf)
6319 (kill-local-variable 'header-line-format))
6321 (setq header-line-format nil))
6322 (erc-header-line-uses-help-echo-p
6323 (let ((help-echo (with-temp-buffer
6325 (fill-region (point-min) (point-max))
6327 (setq header-line-format
6328 (erc-replace-regexp-in-string
6332 (erc-propertize header 'help-echo help-echo
6334 (erc-propertize header 'help-echo help-echo))))))
6335 (t (setq header-line-format
6337 (erc-propertize header 'face face)
6339 (if (featurep 'xemacs)
6341 (force-mode-line-update))))
6343 (defun erc-update-mode-line (&optional buffer)
6344 "Update the mode line in BUFFER.
6346 If BUFFER is nil, update the mode line in all ERC buffers."
6347 (if (and buffer (bufferp buffer))
6348 (erc-update-mode-line-buffer buffer)
6349 (dolist (buf (erc-buffer-list))
6350 (when (buffer-live-p buf)
6351 (erc-update-mode-line-buffer buf)))))
6355 (defun erc-port-to-string (p)
6356 "Convert port P to a string.
6357 P may be an integer or a service name."
6362 (defun erc-string-to-port (s)
6363 "Convert string S to either an integer port number or a service name."
6366 (let ((n (string-to-number s)))
6371 (defun erc-version (&optional here)
6372 "Show the version number of ERC in the minibuffer.
6373 If optional argument HERE is non-nil, insert version number at point."
6375 (let ((version-string
6376 (format "ERC (IRC client for Emacs %s)" emacs-version)))
6378 (insert version-string)
6379 (if (called-interactively-p 'interactive)
6380 (message "%s" version-string)
6383 (defun erc-modes (&optional here)
6384 "Show the active ERC modes in the minibuffer.
6385 If optional argument HERE is non-nil, insert version number at point."
6388 (mapconcat 'identity
6389 (let (modes (case-fold-search nil))
6390 (dolist (var (apropos-internal "^erc-.*mode$"))
6391 (when (and (boundp var)
6393 (setq modes (cons (symbol-name var)
6399 (if (called-interactively-p 'interactive)
6400 (message "%s" string)
6403 (defun erc-trim-string (s)
6404 "Trim leading and trailing spaces off S."
6406 ((not (stringp s)) nil)
6407 ((string-match "^\\s-*$" s)
6409 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
6414 (defun erc-arrange-session-in-multiple-windows ()
6415 "Open a window for every non-server buffer related to `erc-session-server'.
6417 All windows are opened in the current frame."
6419 (unless erc-server-process
6420 (error "No erc-server-process found in current buffer"))
6421 (let ((bufs (erc-buffer-list nil erc-server-process)))
6423 (delete-other-windows)
6424 (switch-to-buffer (car bufs))
6425 (setq bufs (cdr bufs))
6429 (switch-to-buffer (car bufs))
6430 (setq bufs (cdr bufs))
6431 (balance-windows)))))
6433 (defun erc-popup-input-buffer ()
6434 "Provide an input buffer."
6436 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
6441 (list (symbol-name e)))
6442 (apropos-internal "-mode$" 'commandp))
6444 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
6446 (narrow-to-region (point) (point))
6447 (shrink-window-if-larger-than-buffer)))
6451 (defun erc-make-message-variable-name (catalog entry)
6452 "Create a variable name corresponding to CATALOG's ENTRY."
6453 (intern (concat "erc-message-"
6454 (symbol-name catalog) "-" (symbol-name entry))))
6456 (defun erc-define-catalog-entry (catalog entry format-spec)
6457 "Set CATALOG's ENTRY to FORMAT-SPEC."
6458 (set (erc-make-message-variable-name catalog entry)
6461 (defun erc-define-catalog (catalog entries)
6462 "Define a CATALOG according to ENTRIES."
6463 (dolist (entry entries)
6464 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
6468 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
6469 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
6470 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
6471 (cannot-find-file . "Cannot find file %f")
6472 (cannot-read-file . "Cannot read file %f")
6473 (connect . "Connecting to %S:%p... ")
6475 (country-unknown . "%d: No such domain")
6476 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
6477 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
6478 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
6479 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
6480 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
6482 . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
6483 (disconnected . "\n\nConnection failed! Re-establishing connection...\n")
6484 (disconnected-noreconnect
6485 . "\n\nConnection failed! Not re-establishing connection.\n")
6486 (finished . "\n\n*** ERC finished ***\n")
6487 (terminated . "\n\n*** ERC terminated: %e\n")
6488 (login . "Logging in as \'%n\'...")
6489 (nick-in-use . "%n is in use. Choose new nickname: ")
6491 . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
6492 (no-default-channel . "No default channel")
6493 (no-invitation . "You've got no invitation")
6494 (no-target . "No target")
6495 (ops . "%i operator%s: %o")
6496 (ops-none . "No operators in this channel.")
6497 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
6498 (variable-not-bound . "Variable not bound!")
6499 (ACTION . "* %n %a")
6500 (CTCP-CLIENTINFO . "Client info for %n: %m")
6501 (CTCP-ECHO . "Echo %n: %m")
6502 (CTCP-FINGER . "Finger info for %n: %m")
6503 (CTCP-PING . "Ping time to %n is %t")
6504 (CTCP-TIME . "Time by %n is %m")
6505 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
6506 (CTCP-VERSION . "Version for %n is %m")
6507 (ERROR . "==> ERROR from %s: %c\n")
6508 (INVITE . "%n (%u@%h) invites you to channel %c")
6509 (JOIN . "%n (%u@%h) has joined channel %c")
6510 (JOIN-you . "You have joined channel %c")
6511 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
6512 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
6513 (KICK-by-you . "You have kicked %k off channel %c: %r")
6514 (MODE . "%n (%u@%h) has changed mode for %t to %m")
6515 (MODE-nick . "%n has changed mode for %t to %m")
6516 (NICK . "%n (%u@%h) is now known as %N")
6517 (NICK-you . "Your new nickname is %N")
6518 (PART . erc-message-english-PART)
6519 (PING . "PING from server (last: %s sec. ago)")
6520 (PONG . "PONG from %h (%i second%s)")
6521 (QUIT . "%n (%u@%h) has quit: %r")
6522 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
6523 (WALLOPS . "Wallops from %n: %m")
6524 (s004 . "%s %v %U %C")
6525 (s221 . "User modes for %n: %m")
6526 (s252 . "%i operator(s) online")
6527 (s253 . "%i unknown connection(s)")
6528 (s254 . "%i channels formed")
6530 (s301 . "%n is AWAY: %r")
6531 (s303 . "Is online: %n")
6535 (s311 . "%n is %f (%u@%h)")
6536 (s312 . "%n is/was on server %s (%c)")
6537 (s313 . "%n is an IRC operator")
6538 (s314 . "%n was %f (%u@%h)")
6539 (s317 . "%n has been idle for %i")
6540 (s317-on-since . "%n has been idle for %i, on since %t")
6541 (s319 . "%n is on channel(s): %c")
6542 (s320 . "%n is an identified user")
6543 (s321 . "Channel Users Topic")
6544 (s322 . "%c [%u] %t")
6545 (s324 . "%c modes: %m")
6546 (s328 . "%c URL: %u")
6547 (s329 . "%c was created on %t")
6549 (s331 . "No topic is set for %c")
6550 (s332 . "Topic for %c: %T")
6551 (s333 . "%c: topic set by %n, %t")
6552 (s341 . "Inviting %n to channel %c")
6553 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
6554 (s353 . "Users on %c: %u")
6555 (s367 . "Ban for %b on %c")
6556 (s367-set-by . "Ban for %b on %c set by %s on %t")
6557 (s368 . "Banlist of %c ends.")
6558 (s379 . "%c: Forwarded to %f")
6559 (s391 . "The time at %s is %t")
6560 (s401 . "%n: No such nick/channel")
6561 (s403 . "%c: No such channel")
6562 (s404 . "%c: Cannot send to channel")
6563 (s405 . "%c: You have joined too many channels")
6564 (s406 . "%n: There was no such nickname")
6565 (s412 . "No text to send")
6566 (s421 . "%c: Unknown command")
6567 (s431 . "No nickname given")
6568 (s432 . "%n is an erroneous nickname")
6569 (s442 . "%c: You're not on that channel")
6570 (s445 . "SUMMON has been disabled")
6571 (s446 . "USERS has been disabled")
6572 (s451 . "You have not registered")
6573 (s461 . "%c: not enough parameters")
6574 (s462 . "Unauthorized command (already registered)")
6575 (s463 . "Your host isn't among the privileged")
6576 (s464 . "Password incorrect")
6577 (s465 . "You are banned from this server")
6578 (s474 . "You can't join %c because you're banned (+b)")
6579 (s475 . "You must specify the correct channel key (+k) to join %c")
6580 (s481 . "Permission Denied - You're not an IRC operator")
6581 (s482 . "You need to be a channel operator of %c to do that")
6582 (s483 . "You can't kill a server!")
6583 (s484 . "Your connection is restricted!")
6584 (s485 . "You're not the original channel operator")
6585 (s491 . "No O-lines for your host")
6586 (s501 . "Unknown MODE flag")
6587 (s502 . "You can't change modes for other users")
6590 (defun erc-message-english-PART (&rest args)
6591 "Format a proper PART message.
6593 This function is an example on what could be done with formatting
6595 (let ((nick (cadr (memq ?n args)))
6596 (user (cadr (memq ?u args)))
6597 (host (cadr (memq ?h args)))
6598 (channel (cadr (memq ?c args)))
6599 (reason (cadr (memq ?r args))))
6600 (if (string= nick (erc-current-nick))
6601 (format "You have left channel %s" channel)
6602 (format "%s (%s@%s) has left channel %s%s"
6603 nick user host channel
6604 (if (not (string= reason ""))
6606 (erc-replace-regexp-in-string "%" "%%" reason))
6610 (defvar erc-current-message-catalog 'english)
6611 (make-variable-buffer-local 'erc-current-message-catalog)
6613 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6614 "Retrieve ENTRY from CATALOG.
6616 If CATALOG is nil, `erc-current-message-catalog' is used.
6618 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6620 (unless catalog (setq catalog erc-current-message-catalog))
6621 (let ((var (erc-make-message-variable-name catalog entry)))
6624 (when (boundp (erc-make-message-variable-name 'english entry))
6625 (symbol-value (erc-make-message-variable-name 'english entry))))))
6627 (defun erc-format-message (msg &rest args)
6628 "Format MSG according to ARGS.
6630 See also `format-spec'."
6631 (when (eq (logand (length args) 1) 1) ; oddp
6632 (error "Obscure usage of this function appeared"))
6633 (let ((entry (erc-retrieve-catalog-entry msg)))
6635 (error "No format spec for message %s" msg))
6636 (when (functionp entry)
6637 (setq entry (apply entry args)))
6638 (format-spec entry (apply 'format-spec-make args))))
6640 ;;; Various hook functions
6642 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6644 (defcustom erc-kill-server-hook '(erc-kill-server)
6645 "Invoked whenever a server buffer is killed via `kill-buffer'."
6649 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6650 "Invoked whenever a channel-buffer is killed via `kill-buffer'."
6654 (defcustom erc-kill-buffer-hook nil
6655 "Hook run whenever a non-server or channel buffer is killed.
6657 See also `kill-buffer'."
6661 (defun erc-kill-buffer-function ()
6662 "Function to call when an ERC buffer is killed.
6663 This function should be on `kill-buffer-hook'.
6664 When the current buffer is in `erc-mode', this function will run
6665 one of the following hooks:
6666 `erc-kill-server-hook' if the server buffer was killed,
6667 `erc-kill-channel-hook' if a channel buffer was killed,
6668 or `erc-kill-buffer-hook' if any other buffer."
6669 (when (eq major-mode 'erc-mode)
6670 (erc-remove-channel-users)
6672 ((eq (erc-server-buffer) (current-buffer))
6673 (run-hooks 'erc-kill-server-hook))
6674 ((erc-channel-p (erc-default-target))
6675 (run-hooks 'erc-kill-channel-hook))
6677 (run-hooks 'erc-kill-buffer-hook)))))
6679 (defun erc-kill-server ()
6680 "Sends a QUIT command to the server when the server buffer is killed.
6681 This function should be on `erc-kill-server-hook'."
6682 (when (erc-server-process-alive)
6683 (setq erc-server-quitting t)
6684 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6686 (defun erc-kill-channel ()
6687 "Sends a PART command to the server when the channel buffer is killed.
6688 This function should be on `erc-kill-channel-hook'."
6689 (when (erc-server-process-alive)
6690 (let ((tgt (erc-default-target)))
6691 (erc-server-send (format "PART %s :%s" tgt
6692 (funcall erc-part-reason nil))
6695 ;;; Dealing with `erc-parsed'
6697 (defun erc-find-parsed-property ()
6698 "Find the next occurrence of the `erc-parsed' text property."
6699 (text-property-not-all (point-min) (point-max) 'erc-parsed nil))
6701 (defun erc-restore-text-properties ()
6702 "Restore the property 'erc-parsed for the region."
6703 (let ((parsed-posn (erc-find-parsed-property)))
6705 (point-min) (point-max)
6706 'erc-parsed (when parsed-posn (erc-get-parsed-vector parsed-posn)))))
6708 (defun erc-get-parsed-vector (point)
6709 "Return the whole parsed vector on POINT."
6710 (get-text-property point 'erc-parsed))
6712 (defun erc-get-parsed-vector-nick (vect)
6713 "Return nickname in the parsed vector VECT."
6714 (let* ((untreated-nick (and vect (erc-response.sender vect)))
6715 (maybe-nick (when untreated-nick
6716 (car (split-string untreated-nick "!")))))
6717 (when (and (not (null maybe-nick))
6718 (erc-is-valid-nick-p maybe-nick))
6721 (defun erc-get-parsed-vector-type (vect)
6722 "Return message type in the parsed vector VECT."
6724 (erc-response.command vect)))
6726 ;; Teach url.el how to open irc:// URLs with ERC.
6727 ;; To activate, customize `url-irc-function' to `url-irc-erc'.
6730 (defun erc-handle-irc-url (host port channel user password)
6731 "Use ERC to IRC on HOST:PORT in CHANNEL as USER with PASSWORD.
6732 If ERC is already connected to HOST:PORT, simply /join CHANNEL.
6733 Otherwise, connect to HOST:PORT as USER and /join CHANNEL."
6734 (let ((server-buffer
6735 (car (erc-buffer-filter
6737 (and (string-equal erc-session-server host)
6738 (= erc-session-port port)
6739 (erc-open-server-buffer-p)))))))
6740 (with-current-buffer (or server-buffer (current-buffer))
6741 (if (and server-buffer channel)
6742 (erc-cmd-JOIN channel)
6743 (erc-open host port (or user (erc-compute-nick)) (erc-compute-full-name)
6744 (not server-buffer) password nil channel
6746 (get-buffer-process server-buffer)))))))
6750 ;; Deprecated. We might eventually stop requiring the goodies automatically.
6751 ;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6752 ;; avoid a recursive require error when byte-compiling the entire package.
6753 (require 'erc-goodies)
6755 ;;; erc.el ends here
6758 ;; outline-regexp: ";;+"
6759 ;; indent-tabs-mode: t