1 ;; erc.el --- An Emacs Internet Relay Chat client -*- lexical-binding:t -*-
3 ;; Copyright (C) 1997-2017 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
18 ;; This file is part of GNU Emacs.
20 ;; GNU Emacs is free software: you can redistribute it and/or modify
21 ;; it under the terms of the GNU General Public License as published by
22 ;; the Free Software Foundation, either version 3 of the License, or
23 ;; (at your option) any later version.
25 ;; GNU Emacs is distributed in the hope that it will be useful,
26 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ;; GNU General Public License for more details.
30 ;; You should have received a copy of the GNU General Public License
31 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
35 ;; ERC is a powerful, modular, and extensible IRC client for Emacs.
37 ;; For more information, see the following URLs:
38 ;; * https://sv.gnu.org/projects/erc/
39 ;; * http://www.emacswiki.org/cgi-bin/wiki/ERC
43 ;; As of 2006-06-13, ERC development is now hosted on Savannah
44 ;; (https://sv.gnu.org/projects/erc). I invite everyone who wants to
45 ;; hack on it to contact me <mwolson@gnu.org> in order to get write
46 ;; access to the shared Arch archive.
50 ;; Use M-x customize-group RET erc RET to get an overview
51 ;; of all the variables you can tweak.
55 ;; To connect to an IRC server, do
59 ;; After you are connected to a server, you can use C-h m or have a look at
65 (defconst erc-version-string
(format "\C-bERC\C-b (IRC client for Emacs %s)" emacs-version
)
66 "ERC version. This is used by function `erc-version'.")
70 (load "erc-loaddefs" nil t
)
72 (eval-when-compile (require 'cl-lib
))
76 (require 'auth-source
)
79 (defvar erc-official-location
80 "https://emacswiki.org/cgi-bin/wiki/ERC (mailing list: erc-discuss@gnu.org)"
81 "Location of the ERC client on the Internet.")
84 "Emacs Internet Relay Chat client."
85 :link
'(url-link "http://www.emacswiki.org/cgi-bin/wiki/ERC")
86 :link
'(custom-manual "(erc) Top")
90 (defgroup erc-buffers nil
91 "Creating new ERC buffers"
94 (defgroup erc-display nil
95 "Settings for how various things are displayed"
98 (defgroup erc-mode-line-and-header nil
99 "Displaying information in the mode-line and header"
102 (defgroup erc-ignore nil
103 "Ignoring certain messages"
106 (defgroup erc-lurker nil
107 "Hide specified message types sent by lurkers"
111 (defgroup erc-query nil
112 "Using separate buffers for private discussions"
115 (defgroup erc-quit-and-part nil
116 "Quitting and parting channels"
119 (defgroup erc-paranoia nil
120 "Know what is sent and received; control the display of sensitive data."
123 (defgroup erc-scripts nil
124 "Running scripts at startup and with /LOAD"
127 (require 'erc-backend
)
129 ;; compatibility with older ERC releases
131 (define-obsolete-variable-alias 'erc-announced-server-name
132 'erc-server-announced-name
"ERC 5.1")
133 (define-obsolete-variable-alias 'erc-process
'erc-server-process
"ERC 5.1")
134 (define-obsolete-variable-alias 'erc-default-coding-system
135 'erc-server-coding-system
"ERC 5.1")
137 (define-obsolete-function-alias 'erc-send-command
138 'erc-server-send
"ERC 5.1")
140 ;; tunable connection and authentication parameters
142 (defcustom erc-server nil
143 "IRC server to use if one is not provided.
144 See function `erc-compute-server' for more details on connection
145 parameters and authentication."
147 :type
'(choice (const :tag
"None" nil
)
148 (string :tag
"Server")))
150 (defcustom erc-port nil
151 "IRC port to use if not specified.
153 This can be either a string or a number."
155 :type
'(choice (const :tag
"None" nil
)
156 (integer :tag
"Port number")
157 (string :tag
"Port string")))
159 (defcustom erc-nick nil
160 "Nickname to use if one is not provided.
162 This can be either a string, or a list of strings.
163 In the latter case, if the first nick in the list is already in use,
164 other nicks are tried in the list order.
166 See function `erc-compute-nick' for more details on connection
167 parameters and authentication."
169 :type
'(choice (const :tag
"None" nil
)
170 (string :tag
"Nickname")
171 (repeat (string :tag
"Nickname"))))
173 (defcustom erc-nick-uniquifier
"`"
174 "The string to append to the nick if it is already in use."
178 (defcustom erc-try-new-nick-p t
179 "If the nickname you chose isn't available, and this option is non-nil,
180 ERC should automatically attempt to connect with another nickname.
182 You can manually set another nickname with the /NICK command."
186 (defcustom erc-user-full-name nil
189 This can be either a string or a function to call.
191 See function `erc-compute-full-name' for more details on connection
192 parameters and authentication."
194 :type
'(choice (const :tag
"No name" nil
)
196 (function :tag
"Get from function"))
197 :set
(lambda (sym val
)
198 (set sym
(if (functionp val
) (funcall val
) val
))))
200 (defcustom erc-rename-buffers nil
201 "Non-nil means rename buffers with network name, if available."
206 (defvar erc-password nil
207 "Password to use when authenticating to an IRC server.
208 It is not strictly necessary to provide this, since ERC will
211 (defcustom erc-user-mode nil
212 "Initial user modes to be set after a connection is established."
214 :type
'(choice (const nil
) string function
))
217 (defcustom erc-prompt-for-password t
218 "Asks before using the default password, or whether to enter a new one."
222 (defcustom erc-warn-about-blank-lines t
223 "Warn the user if they attempt to send a blank line."
227 (defcustom erc-send-whitespace-lines nil
228 "If set to non-nil, send lines consisting of only whitespace."
232 (defcustom erc-hide-prompt nil
233 "If non-nil, do not display the prompt for commands.
235 \(A command is any input starting with a `/').
237 See also the variables `erc-prompt' and `erc-command-indicator'."
243 (defcustom erc-show-my-nick t
244 "If non-nil, display one's own nickname when sending a message.
246 If non-nil, \"<nickname>\" will be shown.
247 If nil, only \"> \" will be shown."
251 (define-widget 'erc-message-type
'set
252 "A set of standard IRC Message types."
253 :args
'((const "JOIN")
259 (repeat :inline t
:tag
"Others" (string :tag
"IRC Message Type"))))
261 (defcustom erc-hide-list nil
262 "A global list of IRC message types to hide.
263 A typical value would be \(\"JOIN\" \"PART\" \"QUIT\")."
265 :type
'erc-message-type
)
267 (defcustom erc-network-hide-list nil
268 "A list of IRC networks to hide message types from.
269 A typical value would be \((\"freenode\" \"MODE\")
270 \(\"OFTC\" \"JOIN\" \"QUIT\"))."
273 :type
'erc-message-type
)
275 (defcustom erc-channel-hide-list nil
276 "A list of IRC channels to hide message types from.
277 A typical value would be \((\"#emacs\" \"QUIT\" \"JOIN\")
278 \(\"#erc\" \"NICK\")."
281 :type
'erc-message-type
)
283 (defvar erc-session-password nil
284 "The password used for the current session.")
285 (make-variable-buffer-local 'erc-session-password
)
287 (defcustom erc-disconnected-hook nil
288 "Run this hook with arguments (NICK IP REASON) when disconnected.
289 This happens before automatic reconnection. Note, that
290 `erc-server-QUIT-functions' might not be run when we disconnect,
291 simply because we do not necessarily receive the QUIT event."
295 (defcustom erc-complete-functions nil
296 "These functions get called when the user hits TAB in ERC.
297 Each function in turn is called until one returns non-nil to
298 indicate it has handled the input."
302 (defcustom erc-join-hook nil
303 "Hook run when we join a channel. Hook functions are called
304 without arguments, with the current buffer set to the buffer of
307 See also `erc-server-JOIN-functions', `erc-part-hook'."
311 (defcustom erc-quit-hook nil
312 "Hook run when processing a quit command directed at our nick.
314 The hook receives one argument, the current PROCESS.
315 See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
319 (defcustom erc-part-hook nil
320 "Hook run when processing a PART message directed at our nick.
322 The hook receives one argument, the current BUFFER.
323 See also `erc-server-QUIT-functions', `erc-quit-hook' and
324 `erc-disconnected-hook'."
328 (defcustom erc-kick-hook nil
329 "Hook run when processing a KICK message directed at our nick.
331 The hook receives one argument, the current BUFFER.
332 See also `erc-server-PART-functions' and `erc-part-hook'."
336 (defcustom erc-nick-changed-functions nil
337 "List of functions run when your nick was successfully changed.
339 Each function should accept two arguments, NEW-NICK and OLD-NICK."
343 (defcustom erc-connect-pre-hook
'(erc-initialize-log-marker)
344 "Hook called just before `erc' calls `erc-connect'.
345 Functions are passed a buffer as the first argument."
350 (defvar erc-channel-users nil
351 "A hash table of members in the current channel, which
352 associates nicknames with cons cells of the form:
353 \(USER . MEMBER-DATA) where USER is a pointer to an
354 erc-server-user struct, and MEMBER-DATA is a pointer to an
355 erc-channel-user struct.")
356 (make-variable-buffer-local 'erc-channel-users
)
358 (defvar erc-server-users nil
359 "A hash table of users on the current server, which associates
360 nicknames with erc-server-user struct instances.")
361 (make-variable-buffer-local 'erc-server-users
)
363 (defun erc-downcase (string)
364 "Convert STRING to IRC standard conforming downcase."
365 (let ((s (downcase string
))
371 (while (string-match "[]\\[~]" s
)
372 (aset s
(match-beginning 0)
373 (cdr (assq (aref s
(match-beginning 0)) c
)))))
376 (defmacro erc-with-server-buffer
(&rest body
)
377 "Execute BODY in the current ERC server buffer.
378 If no server buffer exists, return nil."
379 (declare (indent 0) (debug (body)))
380 (let ((buffer (make-symbol "buffer")))
381 `(let ((,buffer
(erc-server-buffer)))
382 (when (buffer-live-p ,buffer
)
383 (with-current-buffer ,buffer
386 (cl-defstruct (erc-server-user (:type vector
) :named
)
388 nickname host login full-name info
391 ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
392 ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
396 (cl-defstruct (erc-channel-user (:type vector
) :named
)
397 voice halfop op admin owner
398 ;; Last message time (in the form of the return value of
401 ;; This is useful for ordered name completion.
402 (last-message-time nil
))
404 (define-inline erc-get-channel-user
(nick)
405 "Find the (USER . CHANNEL-DATA) element corresponding to NICK
406 in the current buffer's `erc-channel-users' hash table."
407 (inline-quote (gethash (erc-downcase ,nick
) erc-channel-users
)))
409 (define-inline erc-get-server-user
(nick)
410 "Find the USER corresponding to NICK in the current server's
411 `erc-server-users' hash table."
412 (inline-letevals (nick)
413 (inline-quote (erc-with-server-buffer
414 (gethash (erc-downcase ,nick
) erc-server-users
)))))
416 (define-inline erc-add-server-user
(nick user
)
417 "This function is for internal use only.
419 Adds USER with nickname NICK to the `erc-server-users' hash table."
420 (inline-letevals (nick user
)
422 (erc-with-server-buffer
423 (puthash (erc-downcase ,nick
) ,user erc-server-users
)))))
425 (define-inline erc-remove-server-user
(nick)
426 "This function is for internal use only.
428 Removes the user with nickname NICK from the `erc-server-users'
429 hash table. This user is not removed from the
430 `erc-channel-users' lists of other buffers.
432 See also: `erc-remove-user'."
433 (inline-letevals (nick)
435 (erc-with-server-buffer
436 (remhash (erc-downcase ,nick
) erc-server-users
)))))
438 (defun erc-change-user-nickname (user new-nick
)
439 "This function is for internal use only.
441 Changes the nickname of USER to NEW-NICK in the
442 `erc-server-users' hash table. The `erc-channel-users' lists of
443 other buffers are also changed."
444 (let ((nick (erc-server-user-nickname user
)))
445 (setf (erc-server-user-nickname user
) new-nick
)
446 (erc-with-server-buffer
447 (remhash (erc-downcase nick
) erc-server-users
)
448 (puthash (erc-downcase new-nick
) user erc-server-users
))
449 (dolist (buf (erc-server-user-buffers user
))
450 (if (buffer-live-p buf
)
451 (with-current-buffer buf
452 (let ((cdata (erc-get-channel-user nick
)))
453 (remhash (erc-downcase nick
) erc-channel-users
)
454 (puthash (erc-downcase new-nick
) cdata
455 erc-channel-users
)))))))
457 (defun erc-remove-channel-user (nick)
458 "This function is for internal use only.
460 Removes the user with nickname NICK from the `erc-channel-users'
461 list for this channel. If this user is not in the
462 `erc-channel-users' list of any other buffers, the user is also
463 removed from the server's `erc-server-users' list.
465 See also: `erc-remove-server-user' and `erc-remove-user'."
466 (let ((channel-data (erc-get-channel-user nick
)))
468 (let ((user (car channel-data
)))
469 (setf (erc-server-user-buffers user
)
470 (delq (current-buffer)
471 (erc-server-user-buffers user
)))
472 (remhash (erc-downcase nick
) erc-channel-users
)
473 (if (null (erc-server-user-buffers user
))
474 (erc-remove-server-user nick
))))))
476 (defun erc-remove-user (nick)
477 "This function is for internal use only.
479 Removes the user with nickname NICK from the `erc-server-users'
480 list as well as from all `erc-channel-users' lists.
482 See also: `erc-remove-server-user' and
483 `erc-remove-channel-user'."
484 (let ((user (erc-get-server-user nick
)))
486 (let ((buffers (erc-server-user-buffers user
)))
487 (dolist (buf buffers
)
488 (if (buffer-live-p buf
)
489 (with-current-buffer buf
490 (remhash (erc-downcase nick
) erc-channel-users
)
491 (run-hooks 'erc-channel-members-changed-hook
)))))
492 (erc-remove-server-user nick
))))
494 (defun erc-remove-channel-users ()
495 "This function is for internal use only.
497 Removes all users in the current channel. This is called by
498 `erc-server-PART' and `erc-server-QUIT'."
499 (when (and erc-server-connected
500 (erc-server-process-alive)
501 (hash-table-p erc-channel-users
))
502 (maphash (lambda (nick _cdata
)
503 (erc-remove-channel-user nick
))
505 (clrhash erc-channel-users
)))
507 (define-inline erc-channel-user-owner-p
(nick)
508 "Return non-nil if NICK is an owner of the current channel."
509 (inline-letevals (nick)
512 (hash-table-p erc-channel-users
)
513 (let ((cdata (erc-get-channel-user ,nick
)))
514 (and cdata
(cdr cdata
)
515 (erc-channel-user-owner (cdr cdata
))))))))
517 (define-inline erc-channel-user-admin-p
(nick)
518 "Return non-nil if NICK is an admin in the current channel."
519 (inline-letevals (nick)
522 (hash-table-p erc-channel-users
)
523 (let ((cdata (erc-get-channel-user ,nick
)))
524 (and cdata
(cdr cdata
)
525 (erc-channel-user-admin (cdr cdata
))))))))
527 (define-inline erc-channel-user-op-p
(nick)
528 "Return non-nil if NICK is an operator in the current channel."
529 (inline-letevals (nick)
532 (hash-table-p erc-channel-users
)
533 (let ((cdata (erc-get-channel-user ,nick
)))
534 (and cdata
(cdr cdata
)
535 (erc-channel-user-op (cdr cdata
))))))))
537 (define-inline erc-channel-user-halfop-p
(nick)
538 "Return non-nil if NICK is a half-operator in the current channel."
539 (inline-letevals (nick)
542 (hash-table-p erc-channel-users
)
543 (let ((cdata (erc-get-channel-user ,nick
)))
544 (and cdata
(cdr cdata
)
545 (erc-channel-user-halfop (cdr cdata
))))))))
547 (define-inline erc-channel-user-voice-p
(nick)
548 "Return non-nil if NICK has voice in the current channel."
549 (inline-letevals (nick)
552 (hash-table-p erc-channel-users
)
553 (let ((cdata (erc-get-channel-user ,nick
)))
554 (and cdata
(cdr cdata
)
555 (erc-channel-user-voice (cdr cdata
))))))))
557 (defun erc-get-channel-user-list ()
558 "Return a list of users in the current channel. Each element
559 of the list is of the form (USER . CHANNEL-DATA), where USER is
560 an erc-server-user struct, and CHANNEL-DATA is either nil or an
561 erc-channel-user struct.
563 See also: `erc-sort-channel-users-by-activity'"
565 (if (hash-table-p erc-channel-users
)
566 (maphash (lambda (_nick cdata
)
567 (setq users
(cons cdata users
)))
571 (defun erc-get-server-nickname-list ()
572 "Return a list of known nicknames on the current server."
573 (erc-with-server-buffer
575 (when (hash-table-p erc-server-users
)
576 (maphash (lambda (_n user
)
578 (cons (erc-server-user-nickname user
)
583 (defun erc-get-channel-nickname-list ()
584 "Return a list of known nicknames on the current channel."
586 (when (hash-table-p erc-channel-users
)
587 (maphash (lambda (_n cdata
)
589 (cons (erc-server-user-nickname (car cdata
))
594 (defun erc-get-server-nickname-alist ()
595 "Return an alist of known nicknames on the current server."
596 (erc-with-server-buffer
598 (when (hash-table-p erc-server-users
)
599 (maphash (lambda (_n user
)
601 (cons (cons (erc-server-user-nickname user
) nil
)
606 (defun erc-get-channel-nickname-alist ()
607 "Return an alist of known nicknames on the current channel."
609 (when (hash-table-p erc-channel-users
)
610 (maphash (lambda (_n cdata
)
612 (cons (cons (erc-server-user-nickname (car cdata
)) nil
)
617 (defun erc-sort-channel-users-by-activity (list)
618 "Sort LIST such that users which have spoken most recently are listed first.
619 LIST must be of the form (USER . CHANNEL-DATA).
621 See also: `erc-get-channel-user-list'."
624 (when (and (cdr x
) (cdr y
))
625 (let ((tx (erc-channel-user-last-message-time (cdr x
)))
626 (ty (erc-channel-user-last-message-time (cdr y
))))
629 (time-less-p ty tx
))))))))
631 (defun erc-sort-channel-users-alphabetically (list)
632 "Sort LIST so that users' nicknames are in alphabetical order.
633 LIST must be of the form (USER . CHANNEL-DATA).
635 See also: `erc-get-channel-user-list'."
638 (when (and (cdr x
) (cdr y
))
639 (let ((nickx (downcase (erc-server-user-nickname (car x
))))
640 (nicky (downcase (erc-server-user-nickname (car y
)))))
643 (string-lessp nickx nicky
))))))))
645 (defvar erc-channel-topic nil
646 "A topic string for the channel. Should only be used in channel-buffers.")
647 (make-variable-buffer-local 'erc-channel-topic
)
649 (defvar erc-channel-modes nil
650 "List of strings representing channel modes.
651 E.g. (\"i\" \"m\" \"s\" \"b Quake!*@*\")
652 \(not sure the ban list will be here, but why not)")
653 (make-variable-buffer-local 'erc-channel-modes
)
655 (defvar erc-insert-marker nil
656 "The place where insertion of new text in erc buffers should happen.")
657 (make-variable-buffer-local 'erc-insert-marker
)
659 (defvar erc-input-marker nil
660 "The marker where input should be inserted.")
661 (make-variable-buffer-local 'erc-input-marker
)
663 (defun erc-string-no-properties (string)
664 "Return a copy of STRING will all text-properties removed."
665 (let ((newstring (copy-sequence string
)))
666 (set-text-properties 0 (length newstring
) nil newstring
)
669 (defcustom erc-prompt
"ERC>"
670 "Prompt used by ERC. Trailing whitespace is not required."
672 :type
'(choice string function
))
675 "Return the input prompt as a string.
677 See also the variable `erc-prompt'."
678 (let ((prompt (if (functionp erc-prompt
)
681 (if (> (length prompt
) 0)
685 (defcustom erc-command-indicator nil
686 "Indicator used by ERC for showing commands.
688 If non-nil, this will be used in the ERC buffer to indicate
689 commands (i.e., input starting with a `/').
691 If nil, the prompt will be constructed from the variable `erc-prompt'."
693 :type
'(choice (const nil
) string function
))
695 (defun erc-command-indicator ()
696 "Return the command indicator prompt as a string.
698 This only has any meaning if the variable `erc-command-indicator' is non-nil."
699 (and erc-command-indicator
700 (let ((prompt (if (functionp erc-command-indicator
)
701 (funcall erc-command-indicator
)
702 erc-command-indicator
)))
703 (if (> (length prompt
) 0)
707 (defcustom erc-notice-prefix
"*** "
708 "Prefix for all notices."
712 (defcustom erc-notice-highlight-type
'all
713 "Determines how to highlight notices.
714 See `erc-notice-prefix'.
716 The following values are allowed:
718 `prefix' - highlight notice prefix only
719 `all' - highlight the entire notice
721 Any other value disables notice's highlighting altogether."
723 :type
'(choice (const :tag
"highlight notice prefix only" prefix
)
724 (const :tag
"highlight the entire notice" all
)
725 (const :tag
"don't highlight notices at all" nil
)))
727 (defcustom erc-echo-notice-hook nil
728 "List of functions to call to echo a private notice.
729 Each function is called with four arguments, the string
730 to display, the parsed server message, the target buffer (or
731 nil), and the sender. The functions are called in order, until a
732 function evaluates to non-nil. These hooks are called after
733 those specified in `erc-echo-notice-always-hook'.
735 See also: `erc-echo-notice-always-hook',
736 `erc-echo-notice-in-default-buffer',
737 `erc-echo-notice-in-target-buffer',
738 `erc-echo-notice-in-minibuffer',
739 `erc-echo-notice-in-server-buffer',
740 `erc-echo-notice-in-active-non-server-buffer',
741 `erc-echo-notice-in-active-buffer',
742 `erc-echo-notice-in-user-buffers',
743 `erc-echo-notice-in-user-and-target-buffers',
744 `erc-echo-notice-in-first-user-buffer'"
747 :options
'(erc-echo-notice-in-default-buffer
748 erc-echo-notice-in-target-buffer
749 erc-echo-notice-in-minibuffer
750 erc-echo-notice-in-server-buffer
751 erc-echo-notice-in-active-non-server-buffer
752 erc-echo-notice-in-active-buffer
753 erc-echo-notice-in-user-buffers
754 erc-echo-notice-in-user-and-target-buffers
755 erc-echo-notice-in-first-user-buffer
))
757 (defcustom erc-echo-notice-always-hook
758 '(erc-echo-notice-in-default-buffer)
759 "List of functions to call to echo a private notice.
760 Each function is called with four arguments, the string
761 to display, the parsed server message, the target buffer (or
762 nil), and the sender. The functions are called in order, and all
763 functions are called. These hooks are called before those
764 specified in `erc-echo-notice-hook'.
766 See also: `erc-echo-notice-hook',
767 `erc-echo-notice-in-default-buffer',
768 `erc-echo-notice-in-target-buffer',
769 `erc-echo-notice-in-minibuffer',
770 `erc-echo-notice-in-server-buffer',
771 `erc-echo-notice-in-active-non-server-buffer',
772 `erc-echo-notice-in-active-buffer',
773 `erc-echo-notice-in-user-buffers',
774 `erc-echo-notice-in-user-and-target-buffers',
775 `erc-echo-notice-in-first-user-buffer'"
778 :options
'(erc-echo-notice-in-default-buffer
779 erc-echo-notice-in-target-buffer
780 erc-echo-notice-in-minibuffer
781 erc-echo-notice-in-server-buffer
782 erc-echo-notice-in-active-non-server-buffer
783 erc-echo-notice-in-active-buffer
784 erc-echo-notice-in-user-buffers
785 erc-echo-notice-in-user-and-target-buffers
786 erc-echo-notice-in-first-user-buffer
))
788 ;; other tunable parameters
790 (defcustom erc-whowas-on-nosuchnick nil
791 "If non-nil, do a whowas on a nick if no such nick."
795 (defcustom erc-verbose-server-ping nil
796 "If non-nil, show every time you get a PING or PONG from the server."
800 (defcustom erc-public-away-p nil
801 "Let others know you are back when you are no longer marked away.
802 This happens in this form:
803 * <nick> is back (gone for <time>)
805 Many consider it impolite to do so automatically."
809 (defcustom erc-away-nickname nil
810 "The nickname to take when you are marked as being away."
812 :type
'(choice (const nil
)
815 (defcustom erc-paranoid nil
816 "If non-nil, then all incoming CTCP requests will be shown."
820 (defcustom erc-disable-ctcp-replies nil
821 "Disable replies to CTCP requests that require a reply.
822 If non-nil, then all incoming CTCP requests that normally require
823 an automatic reply (like VERSION or PING) will be ignored. Good to
824 set if some hacker is trying to flood you away."
828 (defcustom erc-anonymous-login t
829 "Be paranoid, don't give away your machine name."
833 (defcustom erc-prompt-for-channel-key nil
834 "Prompt for channel key when using `erc-join-channel' interactively."
838 (defcustom erc-email-userid
"user"
839 "Use this as your email user ID."
843 (defcustom erc-system-name nil
844 "Use this as the name of your system.
845 If nil, ERC will call `system-name' to get this information."
847 :type
'(choice (const :tag
"Default system name" nil
)
850 (defcustom erc-ignore-list nil
851 "List of regexps matching user identifiers to ignore.
853 A user identifier has the form \"nick!login@host\". If an
854 identifier matches, the message from the person will not be
857 :type
'(repeat regexp
))
858 (make-variable-buffer-local 'erc-ignore-list
)
860 (defcustom erc-ignore-reply-list nil
861 "List of regexps matching user identifiers to ignore completely.
863 This differs from `erc-ignore-list' in that it also ignores any
864 messages directed at the user.
866 A user identifier has the form \"nick!login@host\".
868 If an identifier matches, or a message is addressed to a nick
869 whose identifier matches, the message will not be processed.
871 CAVEAT: ERC doesn't know about the user and host of anyone who
872 was already in the channel when you joined, but never said
873 anything, so it won't be able to match the user and host of those
874 people. You can update the ERC internal info using /WHO *."
876 :type
'(repeat regexp
))
878 (defvar erc-flood-protect t
879 "If non-nil, flood protection is enabled.
880 Flooding is sending too much information to the server in too
881 short of an interval, which may cause the server to terminate the
884 See `erc-server-flood-margin' for other flood-related parameters.")
888 (defcustom erc-startup-file-list
889 (list (concat erc-user-emacs-directory
".ercrc.el")
890 (concat erc-user-emacs-directory
".ercrc")
891 "~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
892 "List of files to try for a startup script.
893 The first existent and readable one will get executed.
895 If the filename ends with `.el' it is presumed to be an Emacs Lisp
896 script and it gets (load)ed. Otherwise it is treated as a bunch of
897 regular IRC commands."
899 :type
'(repeat file
))
901 (defcustom erc-script-path nil
902 "List of directories to look for a script in /load command.
903 The script is first searched in the current directory, then in each
904 directory in the list."
906 :type
'(repeat directory
))
908 (defcustom erc-script-echo t
909 "If non-nil, echo the IRC script commands locally."
913 (defvar erc-last-saved-position nil
914 "A marker containing the position the current buffer was last saved at.")
915 (make-variable-buffer-local 'erc-last-saved-position
)
917 (defcustom erc-kill-buffer-on-part nil
918 "Kill the channel buffer on PART.
919 This variable should probably stay nil, as ERC can reuse buffers if
920 you rejoin them later."
921 :group
'erc-quit-and-part
924 (defcustom erc-kill-queries-on-quit nil
925 "Kill all query (also channel) buffers of this server on QUIT.
926 See the variable `erc-kill-buffer-on-part' for details."
927 :group
'erc-quit-and-part
930 (defcustom erc-kill-server-buffer-on-quit nil
931 "Kill the server buffer of the process on QUIT."
932 :group
'erc-quit-and-part
935 (defcustom erc-quit-reason-various-alist nil
936 "Alist of possible arguments to the /quit command.
938 Each element has the form:
941 If REGEXP matches the argument to /quit, then its relevant RESULT
942 will be used. RESULT may be either a string, or a function. If
943 a function, it should return the quit message as a string.
945 If no elements match, then the empty string is used.
948 (setq erc-quit-reason-various-alist
949 \\='((\"xmms\" dme:now-playing)
950 (\"version\" erc-quit-reason-normal)
951 (\"home\" \"Gone home !\")
952 (\"^$\" \"Default Reason\")))
953 If the user types \"/quit home\", then \"Gone home !\" will be used
954 as the quit message."
955 :group
'erc-quit-and-part
956 :type
'(repeat (list regexp
(choice (string) (function)))))
958 (defcustom erc-part-reason-various-alist nil
959 "Alist of possible arguments to the /part command.
961 Each element has the form:
964 If REGEXP matches the argument to /part, then its relevant RESULT
965 will be used. RESULT may be either a string, or a function. If
966 a function, it should return the part message as a string.
968 If no elements match, then the empty string is used.
971 (setq erc-part-reason-various-alist
972 \\='((\"xmms\" dme:now-playing)
973 (\"version\" erc-part-reason-normal)
974 (\"home\" \"Gone home !\")
975 (\"^$\" \"Default Reason\")))
976 If the user types \"/part home\", then \"Gone home !\" will be used
977 as the part message."
978 :group
'erc-quit-and-part
979 :type
'(repeat (list regexp
(choice (string) (function)))))
981 (defcustom erc-quit-reason
'erc-quit-reason-normal
982 "A function which returns the reason for quitting.
984 The function is passed a single argument, the string typed by the
985 user after \"/quit\"."
986 :group
'erc-quit-and-part
987 :type
'(choice (const erc-quit-reason-normal
)
988 (const erc-quit-reason-various
)
991 (defcustom erc-part-reason
'erc-part-reason-normal
992 "A function which returns the reason for parting a channel.
994 The function is passed a single argument, the string typed by the
995 user after \"/PART\"."
996 :group
'erc-quit-and-part
997 :type
'(choice (const erc-part-reason-normal
)
998 (const erc-part-reason-various
)
1001 (defvar erc-grab-buffer-name
"*erc-grab*"
1002 "The name of the buffer created by `erc-grab-region'.")
1004 ;; variables available for IRC scripts
1006 (defvar erc-user-information
"ERC User"
1007 "USER_INFORMATION IRC variable.")
1011 (defgroup erc-hooks nil
1012 "Hook variables for fancy customizations of ERC."
1015 (defcustom erc-mode-hook nil
1016 "Hook run after `erc-mode' setup is finished."
1019 :options
'(erc-add-scroll-to-bottom))
1021 (defcustom erc-timer-hook nil
1022 "Put functions which should get called more or less periodically here.
1023 The idea is that servers always play ping pong with the client, and so there
1024 is no need for any idle-timer games with Emacs."
1028 (defcustom erc-insert-pre-hook nil
1029 "Hook called first when some text is inserted through `erc-display-line'.
1030 It gets called with one argument, STRING.
1031 To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
1032 Filtering functions can set `erc-insert-this' to nil to avoid
1033 display of that particular string at all."
1037 (defcustom erc-send-pre-hook nil
1038 "Hook called first when some text is sent through `erc-send-current-line'.
1039 It gets called with one argument, STRING.
1041 To change the text that will be sent, set the variable `str' which is
1042 used in `erc-send-current-line'.
1044 To change the text inserted into the buffer without changing the text
1045 that will be sent, use `erc-send-modify-hook' instead.
1047 Filtering functions can set `erc-send-this' to nil to avoid sending of
1048 that particular string at all and `erc-insert-this' to prevent
1049 inserting that particular string into the buffer.
1051 Note that it's useless to set `erc-send-this' to nil and
1052 `erc-insert-this' to t. ERC is sane enough to not insert the text
1057 (defvar erc-insert-this t
1058 "Insert the text into the target buffer or not.
1059 Functions on `erc-insert-pre-hook' can set this variable to nil
1060 if they wish to avoid insertion of a particular string.")
1062 (defvar erc-send-this t
1063 "Send the text to the target or not.
1064 Functions on `erc-send-pre-hook' can set this variable to nil
1065 if they wish to avoid sending of a particular string.")
1067 (defcustom erc-insert-modify-hook
()
1068 "Insertion hook for functions that will change the text's appearance.
1069 This hook is called just after `erc-insert-pre-hook' when the value
1070 of `erc-insert-this' is t.
1071 While this hook is run, narrowing is in effect and `current-buffer' is
1072 the buffer where the text got inserted. One possible value to add here
1077 (defcustom erc-insert-post-hook nil
1078 "This hook is called just after `erc-insert-modify-hook'.
1079 At this point, all modifications from prior hook functions are done."
1082 :options
'(erc-truncate-buffer
1084 erc-save-buffer-in-logs
))
1086 (defcustom erc-send-modify-hook nil
1087 "Sending hook for functions that will change the text's appearance.
1088 This hook is called just after `erc-send-pre-hook' when the values
1089 of `erc-send-this' and `erc-insert-this' are both t.
1090 While this hook is run, narrowing is in effect and `current-buffer' is
1091 the buffer where the text got inserted.
1093 Note that no function in this hook can change the appearance of the
1094 text that is sent. Only changing the sent text's appearance on the
1095 sending user's screen is possible. One possible value to add here
1100 (defcustom erc-send-post-hook nil
1101 "This hook is called just after `erc-send-modify-hook'.
1102 At this point, all modifications from prior hook functions are done.
1103 NOTE: The functions on this hook are called _before_ sending a command
1106 This function is called with narrowing, ala `erc-send-modify-hook'."
1109 :options
'(erc-make-read-only))
1111 (defcustom erc-send-completed-hook
1112 (when (fboundp 'emacspeak-auditory-icon
)
1115 (emacspeak-auditory-icon 'select-object
)))))
1116 "Hook called after a message has been parsed by ERC.
1118 The single argument to the functions is the unmodified string
1119 which the local user typed."
1122 ;; mode-specific tables
1124 (defvar erc-mode-syntax-table
1125 (let ((syntax-table (make-syntax-table)))
1126 (modify-syntax-entry ?
\" ". " syntax-table
)
1127 (modify-syntax-entry ?
\\ ". " syntax-table
)
1128 (modify-syntax-entry ?
' "w " syntax-table
)
1129 ;; Make dabbrev-expand useful for nick names
1130 (modify-syntax-entry ?
< "." syntax-table
)
1131 (modify-syntax-entry ?
> "." syntax-table
)
1133 "Syntax table used while in ERC mode.")
1135 (defvar erc-mode-abbrev-table nil
1136 "Abbrev table used while in ERC mode.")
1137 (define-abbrev-table 'erc-mode-abbrev-table
())
1139 (defvar erc-mode-map
1140 (let ((map (make-sparse-keymap)))
1141 (define-key map
"\C-m" 'erc-send-current-line
)
1142 (define-key map
"\C-a" 'erc-bol
)
1143 (define-key map
[home] 'erc-bol)
1144 (define-key map "\C-c\C-a" 'erc-bol)
1145 (define-key map "\C-c\C-b" 'erc-switch-to-buffer)
1146 (define-key map "\C-c\C-c" 'erc-toggle-interpret-controls)
1147 (define-key map "\C-c\C-d" 'erc-input-action)
1148 (define-key map "\C-c\C-e" 'erc-toggle-ctcp-autoresponse)
1149 (define-key map "\C-c\C-f" 'erc-toggle-flood-control)
1150 (define-key map "\C-c\C-i" 'erc-invite-only-mode)
1151 (define-key map "\C-c\C-j" 'erc-join-channel)
1152 (define-key map "\C-c\C-n" 'erc-channel-names)
1153 (define-key map "\C-c\C-o" 'erc-get-channel-mode-from-keypress)
1154 (define-key map "\C-c\C-p" 'erc-part-from-channel)
1155 (define-key map "\C-c\C-q" 'erc-quit-server)
1156 (define-key map "\C-c\C-r" 'erc-remove-text-properties-region)
1157 (define-key map "\C-c\C-t" 'erc-set-topic)
1158 (define-key map "\C-c\C-u" 'erc-kill-input)
1159 (define-key map "\C-c\C-x" 'erc-quit-server)
1160 (define-key map "\M-\t" 'ispell-complete-word)
1161 (define-key map "\t" 'completion-at-point)
1163 ;; Suppress `font-lock-fontify-block' key binding since it
1164 ;; destroys face properties.
1165 (define-key map [remap font-lock-fontify-block] 'undefined)
1172 ; Honestly, I have a horrible sense of color and the "defaults" below
1173 ; are supposed to be really bad. But colors ARE required in IRC to
1174 ; convey different parts of conversation. If you think you know better
1175 ; defaults - send them to me.
1177 ;; Now colors are a bit nicer, at least to my eyes.
1178 ;; You may still want to change them to better fit your background.-- S.B.
1180 (defgroup erc-faces nil
1184 ;; FIXME faces should not end in "-face".
1185 (defface erc-default-face '((t))
1189 (defface erc-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
1190 "ERC face used for user mode prefix."
1193 (defface erc-my-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
1194 "ERC face used for my user mode prefix."
1197 (defface erc-direct-msg-face '((t :foreground "IndianRed"))
1198 "ERC face used for messages you receive in the main erc buffer."
1201 (defface erc-header-line
1202 '((t :foreground "grey20" :background "grey90"))
1203 "ERC face used for the header line.
1205 This will only be used if `erc-header-line-face-method' is non-nil."
1208 (defface erc-input-face '((t :foreground "brown"))
1209 "ERC face used for your input."
1212 (defface erc-prompt-face
1213 '((t :weight bold :foreground "Black" :background "lightBlue2"))
1214 "ERC face for the prompt."
1217 (defface erc-command-indicator-face
1219 "ERC face for the command indicator.
1220 See the variable `erc-command-indicator'."
1223 (defface erc-notice-face
1224 '((default :weight bold)
1225 (((class color) (min-colors 88)) :foreground "SlateBlue")
1226 (t :foreground "blue"))
1227 "ERC face for notices."
1230 (defface erc-action-face '((t :weight bold))
1231 "ERC face for actions generated by /ME."
1234 (defface erc-error-face '((t :foreground "red"))
1235 "ERC face for errors."
1238 ;; same default color as `erc-input-face'
1239 (defface erc-my-nick-face '((t :weight bold :foreground "brown"))
1240 "ERC face for your current nickname in messages sent by you.
1241 See also `erc-show-my-nick'."
1244 (defface erc-nick-default-face '((t :weight bold))
1245 "ERC nickname default face."
1248 (defface erc-nick-msg-face '((t :weight bold :foreground "IndianRed"))
1249 "ERC nickname face for private messages."
1252 ;; Debugging support
1254 (defvar erc-log-p nil
1255 "When set to t, generate debug messages in a separate debug buffer.")
1257 (defvar erc-debug-log-file (expand-file-name "ERC.debug")
1258 "Debug log file name.")
1260 (defvar erc-dbuf nil)
1261 (make-variable-buffer-local 'erc-dbuf)
1263 (defmacro define-erc-module (name alias doc enable-body disable-body
1265 "Define a new minor mode using ERC conventions.
1266 Symbol NAME is the name of the module.
1267 Symbol ALIAS is the alias to use, or nil.
1268 DOC is the documentation string to use for the minor mode.
1269 ENABLE-BODY is a list of expressions used to enable the mode.
1270 DISABLE-BODY is a list of expressions used to disable the mode.
1271 If LOCAL-P is non-nil, the mode will be created as a buffer-local
1272 mode, rather than a global one.
1274 This will define a minor mode called erc-NAME-mode, possibly
1275 an alias erc-ALIAS-mode, as well as the helper functions
1276 erc-NAME-enable, and erc-NAME-disable.
1280 ;;;###autoload(autoload \\='erc-replace-mode \"erc-replace\")
1281 (define-erc-module replace nil
1282 \"This mode replaces incoming text according to `erc-replace-alist'.\"
1283 ((add-hook \\='erc-insert-modify-hook
1284 \\='erc-replace-insert))
1285 ((remove-hook \\='erc-insert-modify-hook
1286 \\='erc-replace-insert)))"
1287 (declare (doc-string 3))
1288 (let* ((sn (symbol-name name))
1289 (mode (intern (format "erc-%s-mode" (downcase sn))))
1290 (group (intern (format "erc-%s" (downcase sn))))
1291 (enable (intern (format "erc-%s-enable" (downcase sn))))
1292 (disable (intern (format "erc-%s-disable" (downcase sn)))))
1294 (erc-define-minor-mode
1296 ,(format "Toggle ERC %S mode.
1297 With a prefix argument ARG, enable %s if ARG is positive,
1298 and disable it otherwise. If called from Lisp, enable the mode
1299 if ARG is omitted or nil.
1302 :global ,(not local-p) :group (quote ,group)
1307 ,(format "Enable ERC %S mode."
1310 (add-to-list 'erc-modules (quote ,name))
1314 ,(format "Disable ERC %S mode."
1317 (setq erc-modules (delq (quote ,name) erc-modules))
1320 ,(when (and alias (not (eq name alias)))
1324 (format "erc-%s-mode"
1325 (downcase (symbol-name alias)))))
1328 ;; For find-function and find-variable.
1329 (put ',mode 'definition-name ',name)
1330 (put ',enable 'definition-name ',name)
1331 (put ',disable 'definition-name ',name))))
1333 (defun erc-once-with-server-event (event f)
1334 "Run function F the next time EVENT occurs in the `current-buffer'.
1336 You should make sure that `current-buffer' is a server buffer.
1338 This function temporarily adds a function to EVENT's hook to call F with
1339 two arguments (`proc' and `parsed'). After F is called, the function is
1340 removed from EVENT's hook. F should return either nil
1341 or t, where nil indicates that the other functions on EVENT's hook
1342 should be run too, and t indicates that other functions should
1345 Please be sure to use this function in server-buffers. In
1346 channel-buffers it may not work at all, as it uses the LOCAL
1347 argument of `add-hook' and `remove-hook' to ensure multiserver
1349 (unless (erc-server-buffer-p)
1351 "You should only run `erc-once-with-server-event' in a server buffer"))
1352 (let ((fun (make-symbol "fun"))
1353 (hook (erc-get-hook event)))
1354 (put fun 'erc-original-buffer (current-buffer))
1355 (fset fun (lambda (proc parsed)
1356 (with-current-buffer (get fun 'erc-original-buffer)
1357 (remove-hook hook fun t))
1359 (funcall f proc parsed)))
1360 (add-hook hook fun nil t)
1363 (define-inline erc-log (string)
1364 "Logs STRING if logging is on (see `erc-log-p')."
1367 (erc-log-aux ,string))))
1369 (defun erc-server-buffer ()
1370 "Return the server buffer for the current buffer's process.
1371 The buffer-local variable `erc-server-process' is used to find
1372 the process buffer."
1373 (and (erc-server-buffer-live-p)
1374 (process-buffer erc-server-process)))
1376 (defun erc-server-buffer-live-p ()
1377 "Return t if the server buffer has not been killed."
1378 (and (processp erc-server-process)
1379 (buffer-live-p (process-buffer erc-server-process))))
1381 (defun erc-server-buffer-p (&optional buffer)
1382 "Return non-nil if argument BUFFER is an ERC server buffer.
1384 If BUFFER is nil, the current buffer is used."
1385 (with-current-buffer (or buffer (current-buffer))
1386 (and (eq major-mode 'erc-mode)
1387 (null (erc-default-target)))))
1389 (defun erc-open-server-buffer-p (&optional buffer)
1390 "Return non-nil if argument BUFFER is an ERC server buffer that
1391 has an open IRC process.
1393 If BUFFER is nil, the current buffer is used."
1394 (and (erc-server-buffer-p buffer)
1395 (erc-server-process-alive buffer)))
1397 (defun erc-query-buffer-p (&optional buffer)
1398 "Return non-nil if BUFFER is an ERC query buffer.
1399 If BUFFER is nil, the current buffer is used."
1400 (with-current-buffer (or buffer (current-buffer))
1401 (let ((target (erc-default-target)))
1402 (and (eq major-mode 'erc-mode)
1404 (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
1406 (defun erc-ison-p (nick)
1407 "Return non-nil if NICK is online."
1408 (interactive "sNick: ")
1409 (erc-with-server-buffer
1410 (let ((erc-online-p 'unknown))
1411 (erc-once-with-server-event
1413 (lambda (_proc parsed)
1414 (let ((ison (split-string (aref parsed 3))))
1415 (setq erc-online-p (car (erc-member-ignore-case nick ison)))
1417 (erc-server-send (format "ISON %s" nick))
1418 (while (eq erc-online-p 'unknown) (accept-process-output))
1419 (if (called-interactively-p 'interactive)
1420 (message "%s is %sonline"
1421 (or erc-online-p nick)
1422 (if erc-online-p "" "not "))
1425 (defun erc-log-aux (string)
1426 "Do the debug logging of STRING."
1427 (let ((cb (current-buffer))
1430 (session-buffer (erc-server-buffer)))
1433 (set-buffer session-buffer)
1434 (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
1436 (setq erc-dbuf (get-buffer-create
1437 (concat "*ERC-DEBUG: "
1438 erc-session-server "*")))))
1439 (set-buffer erc-dbuf)
1440 (setq point (point))
1441 (setq was-eob (eobp))
1442 (goto-char (point-max))
1443 (insert (concat "** " string "\n"))
1444 (if was-eob (goto-char (point-max))
1447 (message "ERC: ** %s" string))))
1449 ;; Last active buffer, to print server messages in the right place
1451 (defvar erc-active-buffer nil
1452 "The current active buffer, the one where the user typed the last command.
1453 Defaults to the server buffer, and should only be set in the
1455 (make-variable-buffer-local 'erc-active-buffer)
1457 (defun erc-active-buffer ()
1458 "Return the value of `erc-active-buffer' for the current server.
1459 Defaults to the server buffer."
1460 (erc-with-server-buffer
1461 (if (buffer-live-p erc-active-buffer)
1463 (setq erc-active-buffer (current-buffer)))))
1465 (defun erc-set-active-buffer (buffer)
1466 "Set the value of `erc-active-buffer' to BUFFER."
1467 (cond ((erc-server-buffer)
1468 (with-current-buffer (erc-server-buffer)
1469 (setq erc-active-buffer buffer)))
1470 (t (setq erc-active-buffer buffer))))
1472 ;; Mode activation routines
1474 (define-derived-mode erc-mode fundamental-mode "ERC"
1475 "Major mode for Emacs IRC."
1476 (setq local-abbrev-table erc-mode-abbrev-table)
1477 (when (boundp 'next-line-add-newlines)
1478 (set (make-local-variable 'next-line-add-newlines) nil))
1479 (setq line-move-ignore-invisible t)
1480 (set (make-local-variable 'paragraph-separate)
1481 (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
1482 (set (make-local-variable 'paragraph-start)
1483 (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
1484 (setq-local completion-ignore-case t)
1485 (add-hook 'completion-at-point-functions 'erc-complete-word-at-point nil t))
1489 (defconst erc-default-server "irc.freenode.net"
1490 "IRC server to use if it cannot be detected otherwise.")
1492 (defconst erc-default-port 6667
1493 "IRC port to use if it cannot be detected otherwise.")
1495 (defconst erc-default-port-tls 6697
1496 "IRC port to use for encrypted connections if it cannot be
1497 detected otherwise.")
1499 (defcustom erc-join-buffer 'buffer
1500 "Determines how to display a newly created IRC buffer.
1502 The available choices are:
1504 `window' - in another window,
1505 `window-noselect' - in another window, but don't select that one,
1506 `frame' - in another frame,
1507 `bury' - bury it in a new buffer,
1508 `buffer' - in place of the current buffer,
1509 any other value - in place of the current buffer."
1511 :type '(choice (const :tag "Split window and select" window)
1512 (const :tag "Split window, don't select" window-noselect)
1513 (const :tag "New frame" frame)
1514 (const :tag "Bury in new buffer" bury)
1515 (const :tag "Use current buffer" buffer)
1516 (const :tag "Use current buffer" t)))
1518 (defcustom erc-frame-alist nil
1519 "Alist of frame parameters for creating erc frames.
1520 A value of nil means to use `default-frame-alist'."
1522 :type '(repeat (cons :format "%v"
1523 (symbol :tag "Parameter")
1524 (sexp :tag "Value"))))
1526 (defcustom erc-frame-dedicated-flag nil
1527 "Non-nil means the erc frames are dedicated to that buffer.
1528 This only has effect when `erc-join-buffer' is set to `frame'."
1532 (defcustom erc-reuse-frames t
1533 "Determines whether new frames are always created.
1534 Non-nil means that a new frame is not created to display an ERC
1535 buffer if there is already a window displaying it. This only has
1536 effect when `erc-join-buffer' is set to `frame'."
1540 (defun erc-channel-p (channel)
1541 "Return non-nil if CHANNEL seems to be an IRC channel name."
1542 (cond ((stringp channel)
1543 (memq (aref channel 0) '(?# ?& ?+ ?!)))
1544 ((and (bufferp channel) (buffer-live-p channel))
1545 (with-current-buffer channel
1546 (erc-channel-p (erc-default-target))))
1549 (defcustom erc-reuse-buffers t
1550 "If nil, create new buffers on joining a channel/query.
1551 If non-nil, a new buffer will only be created when you join
1552 channels with same names on different servers, or have query buffers
1553 open with nicks of the same name on different servers. Otherwise,
1554 the existing buffers will be reused."
1558 (defun erc-normalize-port (port)
1559 "Normalize the port specification PORT to integer form.
1560 PORT may be an integer, a string or a symbol. If it is a string or a
1561 symbol, it may have these values:
1565 * ircd-dalnet -> 7000"
1568 (erc-normalize-port (symbol-name port)))
1570 (let ((port-nr (string-to-number port)))
1574 ((string-equal port "irc")
1576 ((string-equal port "ircs")
1578 ((string-equal port "ircd")
1580 ((string-equal port "ircd-dalnet")
1589 (defun erc-port-equal (a b)
1590 "Check whether ports A and B are equal."
1591 (= (erc-normalize-port a) (erc-normalize-port b)))
1593 (defun erc-generate-new-buffer-name (server port target)
1594 "Create a new buffer name based on the arguments."
1595 (when (numberp port) (setq port (number-to-string port)))
1596 (let ((buf-name (or target
1597 (or (let ((name (concat server ":" port)))
1598 (when (> (length name) 1)
1600 ;; This fallback should in fact never happen
1601 "*erc-server-buffer*")))
1603 ;; Reuse existing buffers, but not if the buffer is a connected server
1604 ;; buffer and not if its associated with a different server than the
1605 ;; current ERC buffer.
1606 ;; if buf-name is taken by a different connection (or by something !erc)
1607 ;; then see if "buf-name/server" meets the same criteria
1608 (dolist (candidate (list buf-name (concat buf-name "/" server)))
1609 (if (and (not buffer-name)
1611 (get-buffer candidate)
1613 (with-current-buffer (get-buffer candidate)
1614 (and (erc-server-buffer-p)
1615 (not (erc-server-process-alive)))))
1616 (with-current-buffer (get-buffer candidate)
1617 (and (string= erc-session-server server)
1618 (erc-port-equal erc-session-port port))))
1619 (setq buffer-name candidate)))
1620 ;; if buffer-name is unset, neither candidate worked out for us,
1621 ;; fallback to the old <N> uniquification method:
1622 (or buffer-name (generate-new-buffer-name buf-name)) ))
1624 (defun erc-get-buffer-create (server port target)
1625 "Create a new buffer based on the arguments."
1626 (get-buffer-create (erc-generate-new-buffer-name server port target)))
1629 (defun erc-member-ignore-case (string list)
1630 "Return non-nil if STRING is a member of LIST.
1632 All strings are compared according to IRC protocol case rules, see
1634 (setq string (erc-downcase string))
1637 (if (string= string (erc-downcase (car list)))
1638 (throw 'result list)
1639 (setq list (cdr list))))))
1641 (defmacro erc-with-buffer (spec &rest body)
1642 "Execute BODY in the buffer associated with SPEC.
1644 SPEC should have the form
1648 If TARGET is a buffer, use it. Otherwise, use the buffer
1649 matching TARGET in the process specified by PROCESS.
1651 If PROCESS is nil, use the current `erc-server-process'.
1652 See `erc-get-buffer' for details.
1654 See also `with-current-buffer'.
1656 \(fn (TARGET [PROCESS]) BODY...)"
1657 (declare (indent 1) (debug ((form &optional form) body)))
1658 (let ((buf (make-symbol "buf"))
1659 (proc (make-symbol "proc"))
1660 (target (make-symbol "target"))
1661 (process (make-symbol "process")))
1662 `(let* ((,target ,(car spec))
1663 (,process ,(cadr spec))
1664 (,buf (if (bufferp ,target)
1666 (let ((,proc (or ,process
1667 (and (processp erc-server-process)
1668 erc-server-process))))
1669 (if (and ,target ,proc)
1670 (erc-get-buffer ,target ,proc))))))
1671 (when (buffer-live-p ,buf)
1672 (with-current-buffer ,buf
1675 (defun erc-get-buffer (target &optional proc)
1676 "Return the buffer matching TARGET in the process PROC.
1677 If PROC is not supplied, all processes are searched."
1678 (let ((downcased-target (erc-downcase target)))
1682 (let ((current (erc-default-target)))
1683 (and (stringp current)
1684 (string-equal downcased-target (erc-downcase current))
1685 (throw 'buffer (current-buffer)))))
1688 (defun erc--buffer-p (buf predicate proc)
1689 (with-current-buffer buf
1690 (and (derived-mode-p 'erc-mode)
1692 (eq proc erc-server-process))
1696 (defun erc-buffer-filter (predicate &optional proc)
1697 "Return a list of `erc-mode' buffers matching certain criteria.
1698 PREDICATE is a function executed with each buffer, if it returns t, that buffer
1699 is considered a valid match.
1701 PROC is either an `erc-server-process', identifying a certain
1702 server connection, or nil which means all open connections."
1706 (mapcar (lambda (buf)
1707 (when (buffer-live-p buf)
1708 (erc--buffer-p buf predicate proc)))
1711 (defun erc-buffer-list (&optional predicate proc)
1712 "Return a list of ERC buffers.
1713 PREDICATE is a function which executes with every buffer satisfying
1714 the predicate. If PREDICATE is passed as nil, return a list of all ERC
1715 buffers. If PROC is given, the buffers local variable `erc-server-process'
1716 needs to match PROC."
1718 (setq predicate (lambda () t)))
1719 (erc-buffer-filter predicate proc))
1721 (defmacro erc-with-all-buffers-of-server (process pred &rest forms)
1722 "Execute FORMS in all buffers which have same process as this server.
1723 FORMS will be evaluated in all buffers having the process PROCESS and
1724 where PRED matches or in all buffers of the server process if PRED is
1726 (declare (indent 1) (debug (form form body)))
1727 ;; Make the evaluation have the correct order
1728 (let ((pre (make-symbol "pre"))
1729 (pro (make-symbol "pro")))
1730 `(let* ((,pro ,process)
1732 (res (mapcar (lambda (buffer)
1733 (with-current-buffer buffer
1735 (erc-buffer-list ,pre
1737 ;; Silence the byte-compiler by binding the result of mapcar to
1742 (define-obsolete-function-alias 'erc-iswitchb 'erc-switch-to-buffer "25.1")
1743 (defun erc-switch-to-buffer (&optional arg)
1744 "Prompt for a ERC buffer to switch to.
1745 When invoked with prefix argument, use all erc buffers. Without prefix
1746 ARG, allow only buffers related to same session server.
1747 If `erc-track-mode' is in enabled, put the last element of
1748 `erc-modified-channels-alist' in front of the buffer list."
1751 (read-buffer "Switch to ERC buffer: "
1752 (when (boundp 'erc-modified-channels-alist)
1753 (buffer-name (caar (last erc-modified-channels-alist))))
1755 ;; Only allow ERC buffers in the same session.
1756 (let ((proc (unless arg erc-server-process)))
1758 (let ((buf (if (consp bufname)
1759 (cdr bufname) (get-buffer bufname))))
1761 (erc--buffer-p buf (lambda () t) proc)
1762 (with-current-buffer buf
1763 (and (derived-mode-p 'erc-mode)
1765 (eq proc erc-server-process)))))))))))
1767 (defun erc-channel-list (proc)
1768 "Return a list of channel buffers.
1769 PROC is the process for the server connection. If PROC is nil, return
1770 all channel buffers on all servers."
1773 (and (erc-default-target)
1774 (erc-channel-p (erc-default-target))))
1777 (defun erc-buffer-list-with-nick (nick proc)
1778 "Return buffers containing NICK in the `erc-channel-users' list."
1779 (with-current-buffer (process-buffer proc)
1780 (let ((user (gethash (erc-downcase nick) erc-server-users)))
1782 (erc-server-user-buffers user)
1785 ;; Some local variables
1787 (defvar erc-default-recipients nil
1788 "List of default recipients of the current buffer.")
1789 (make-variable-buffer-local 'erc-default-recipients)
1791 (defvar erc-session-user-full-name nil
1792 "Full name of the user on the current server.")
1793 (make-variable-buffer-local 'erc-session-user-full-name)
1795 (defvar erc-channel-user-limit nil
1796 "Limit of users per channel.")
1797 (make-variable-buffer-local 'erc-channel-user-limit)
1799 (defvar erc-channel-key nil
1800 "Key needed to join channel.")
1801 (make-variable-buffer-local 'erc-channel-key)
1803 (defvar erc-invitation nil
1804 "Last invitation channel.")
1805 (make-variable-buffer-local 'erc-invitation)
1807 (defvar erc-away nil
1808 "Non-nil indicates that we are away.
1810 Use `erc-away-time' to access this if you might be in a channel
1811 buffer rather than a server buffer.")
1812 (make-variable-buffer-local 'erc-away)
1814 (defvar erc-channel-list nil
1815 "Server channel list.")
1816 (make-variable-buffer-local 'erc-channel-list)
1818 (defvar erc-bad-nick nil
1819 "Non-nil indicates that we got a `nick in use' error while connecting.")
1820 (make-variable-buffer-local 'erc-bad-nick)
1822 (defvar erc-logged-in nil
1823 "Non-nil indicates that we are logged in.")
1824 (make-variable-buffer-local 'erc-logged-in)
1826 (defvar erc-default-nicks nil
1827 "The local copy of `erc-nick' - the list of nicks to choose from.")
1828 (make-variable-buffer-local 'erc-default-nicks)
1830 (defvar erc-nick-change-attempt-count 0
1831 "Used to keep track of how many times an attempt at changing nick is made.")
1832 (make-variable-buffer-local 'erc-nick-change-attempt-count)
1834 (defun erc-migrate-modules (mods)
1835 "Migrate old names of ERC modules to new ones."
1836 ;; modify `transforms' to specify what needs to be changed
1837 ;; each item is in the format '(old . new)
1838 (let ((transforms '((pcomplete . completion))))
1840 (mapcar (lambda (m) (or (cdr (assoc m transforms)) m))
1843 (defcustom erc-modules '(netsplit fill button match track completion readonly
1844 networks ring autojoin noncommands irccontrols
1845 move-to-prompt stamp menu list)
1846 "A list of modules which ERC should enable.
1847 If you set the value of this without using `customize' remember to call
1848 \(erc-update-modules) after you change it. When using `customize', modules
1849 removed from the list will be disabled."
1851 ;; replace outdated names with their newer equivalents
1852 (erc-migrate-modules (symbol-value sym)))
1853 :initialize 'custom-initialize-default
1854 :set (lambda (sym val)
1855 ;; disable modules which have just been removed
1856 (when (and (boundp 'erc-modules) erc-modules val)
1857 (dolist (module erc-modules)
1858 (unless (member module val)
1859 (let ((f (intern-soft (format "erc-%s-mode" module))))
1860 (when (and (fboundp f) (boundp f) (symbol-value f))
1861 (message "Disabling `erc-%s'" module)
1864 ;; this test is for the case where erc hasn't been loaded yet
1865 (when (fboundp 'erc-update-modules)
1866 (erc-update-modules)))
1870 (const :tag "autoaway: Set away status automatically" autoaway)
1871 (const :tag "autojoin: Join channels automatically" autojoin)
1872 (const :tag "button: Buttonize URLs, nicknames, and other text" button)
1873 (const :tag "capab: Mark unidentified users on servers supporting CAPAB"
1875 (const :tag "completion: Complete nicknames and commands (programmable)"
1877 (const :tag "hecomplete: Complete nicknames and commands (obsolete, use \"completion\")" hecomplete)
1878 (const :tag "dcc: Provide Direct Client-to-Client support" dcc)
1879 (const :tag "fill: Wrap long lines" fill)
1880 (const :tag "identd: Launch an identd server on port 8113" identd)
1881 (const :tag "irccontrols: Highlight or remove IRC control characters"
1883 (const :tag "keep-place: Leave point above un-viewed text" keep-place)
1884 (const :tag "list: List channels in a separate buffer" list)
1885 (const :tag "log: Save buffers in logs" log)
1886 (const :tag "match: Highlight pals, fools, and other keywords" match)
1887 (const :tag "menu: Display a menu in ERC buffers" menu)
1888 (const :tag "move-to-prompt: Move to the prompt when typing text"
1890 (const :tag "netsplit: Detect netsplits" netsplit)
1891 (const :tag "networks: Provide data about IRC networks" networks)
1892 (const :tag "noncommands: Don't display non-IRC commands after evaluation"
1895 "notify: Notify when the online status of certain users changes"
1897 (const :tag "notifications: Send notifications on PRIVMSG or nickname mentions"
1899 (const :tag "page: Process CTCP PAGE requests from IRC" page)
1900 (const :tag "readonly: Make displayed lines read-only" readonly)
1901 (const :tag "replace: Replace text in messages" replace)
1902 (const :tag "ring: Enable an input history" ring)
1903 (const :tag "scrolltobottom: Scroll to the bottom of the buffer"
1905 (const :tag "services: Identify to Nickserv (IRC Services) automatically"
1907 (const :tag "smiley: Convert smileys to pretty icons" smiley)
1908 (const :tag "sound: Play sounds when you receive CTCP SOUND requests"
1910 (const :tag "stamp: Add timestamps to messages" stamp)
1911 (const :tag "spelling: Check spelling" spelling)
1912 (const :tag "track: Track channel activity in the mode-line" track)
1913 (const :tag "truncate: Truncate buffers to a certain size" truncate)
1914 (const :tag "unmorse: Translate morse code in messages" unmorse)
1915 (const :tag "xdcc: Act as an XDCC file-server" xdcc)
1916 (repeat :tag "Others" :inline t symbol))
1919 (defun erc-update-modules ()
1920 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1922 (dolist (mod erc-modules)
1923 (setq req (concat "erc-" (symbol-name mod)))
1925 ;; yuck. perhaps we should bring the filenames into sync?
1926 ((string= req "erc-capab-identify")
1927 (setq req "erc-capab"))
1928 ((string= req "erc-completion")
1929 (setq req "erc-pcomplete"))
1930 ((string= req "erc-pcomplete")
1931 (setq mod 'completion))
1932 ((string= req "erc-autojoin")
1933 (setq req "erc-join")))
1935 (require (intern req))
1937 (let ((sym (intern-soft (concat "erc-" (symbol-name mod) "-mode"))))
1940 (error "`%s' is not a known ERC module" mod))))))
1942 (defun erc-setup-buffer (buffer)
1943 "Consults `erc-join-buffer' to find out how to display `BUFFER'."
1944 (pcase erc-join-buffer
1946 (if (active-minibuffer-window)
1947 (display-buffer buffer)
1948 (switch-to-buffer-other-window buffer)))
1950 (display-buffer buffer))
1954 (when (or (not erc-reuse-frames)
1955 (not (get-buffer-window buffer t)))
1956 (let ((frame (make-frame (or erc-frame-alist
1957 default-frame-alist))))
1959 (select-frame frame))
1960 (switch-to-buffer buffer)
1961 (when erc-frame-dedicated-flag
1962 (set-window-dedicated-p (selected-window) t))))
1964 (if (active-minibuffer-window)
1965 (display-buffer buffer)
1966 (switch-to-buffer buffer)))))
1968 (defun erc-open (&optional server port nick full-name
1969 connect passwd tgt-list channel process)
1970 "Connect to SERVER on PORT as NICK with FULL-NAME.
1972 If CONNECT is non-nil, connect to the server. Otherwise assume
1973 already connected and just create a separate buffer for the new
1976 Use PASSWD as user password on the server. If TGT-LIST is
1977 non-nil, use it to initialize `erc-default-recipients'.
1979 Returns the buffer for the given server or channel."
1980 (let ((server-announced-name (when (and (boundp 'erc-session-server)
1981 (string= server erc-session-server))
1982 erc-server-announced-name))
1983 (connected-p (unless connect erc-server-connected))
1984 (buffer (erc-get-buffer-create server port channel))
1985 (old-buffer (current-buffer))
1988 (when connect (run-hook-with-args 'erc-before-connect server port nick))
1989 (erc-update-modules)
1991 (setq old-point (point))
1992 (let ((old-recon-count erc-server-reconnect-count))
1994 (setq erc-server-reconnect-count old-recon-count))
1995 (setq erc-server-announced-name server-announced-name)
1996 (setq erc-server-connected connected-p)
1997 ;; connection parameters
1998 (setq erc-server-process process)
1999 (setq erc-insert-marker (make-marker))
2000 (setq erc-input-marker (make-marker))
2001 ;; go to the end of the buffer and open a new line
2002 ;; (the buffer may have existed)
2003 (goto-char (point-max))
2005 (when (get-text-property (point) 'erc-prompt)
2006 (setq continued-session t)
2007 (set-marker erc-input-marker
2008 (or (next-single-property-change (point) 'erc-prompt)
2010 (unless continued-session
2011 (goto-char (point-max))
2013 (set-marker erc-insert-marker (point))
2014 ;; stack of default recipients
2015 (setq erc-default-recipients tgt-list)
2016 (setq erc-server-current-nick nil)
2017 ;; Initialize erc-server-users and erc-channel-users
2019 (progn ;; server buffer
2020 (setq erc-server-users
2021 (make-hash-table :test 'equal))
2022 (setq erc-channel-users nil))
2023 (progn ;; target buffer
2024 (setq erc-server-users nil)
2025 (setq erc-channel-users
2026 (make-hash-table :test 'equal))))
2027 ;; clear last incomplete line read
2028 (setq erc-server-filter-data nil)
2029 (setq erc-channel-topic "")
2030 ;; limit on the number of users on the channel (mode +l)
2031 (setq erc-channel-user-limit nil)
2032 (setq erc-channel-key nil)
2033 ;; last active buffer, defaults to this one
2034 (erc-set-active-buffer buffer)
2035 ;; last invitation channel
2036 (setq erc-invitation nil)
2037 ;; Server channel list
2038 (setq erc-channel-list ())
2039 ;; login-time 'nick in use' error
2040 (setq erc-bad-nick nil)
2041 ;; whether we have logged in
2042 (setq erc-logged-in nil)
2043 ;; The local copy of `erc-nick' - the list of nicks to choose
2044 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
2046 (setq erc-session-password
2051 (auth-source-search :host server
2054 ;; secrets.el wouldn’t accept a number
2055 :port (if (numberp port) (number-to-string port) port)
2056 :require '(:secret)))
2058 (if (functionp secret)
2061 ;; debug output buffer
2064 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
2066 (unless continued-session
2067 (goto-char (point-max))
2069 (if continued-session
2070 (goto-char old-point)
2071 (set-marker erc-insert-marker (point))
2072 (erc-display-prompt)
2073 (goto-char (point-max)))
2075 (erc-determine-parameters server port nick full-name)
2077 ;; Saving log file on exit
2078 (run-hook-with-args 'erc-connect-pre-hook buffer)
2081 (erc-server-connect erc-session-server erc-session-port buffer))
2082 (erc-update-mode-line)
2084 ;; Now display the buffer in a window as per user wishes.
2085 (unless (eq buffer old-buffer)
2087 ;; we can't log to debug buffer, it may not exist yet
2088 (message "erc: old buffer %s, switching to %s"
2090 (erc-setup-buffer buffer))
2094 (defun erc-initialize-log-marker (buffer)
2095 "Initialize the `erc-last-saved-position' marker to a sensible position.
2096 BUFFER is the current buffer."
2097 (with-current-buffer buffer
2098 (unless (markerp erc-last-saved-position)
2099 (setq erc-last-saved-position (make-marker))
2100 (move-marker erc-last-saved-position
2101 (1- (marker-position erc-insert-marker))))))
2103 ;; interactive startup
2105 (defvar erc-server-history-list nil
2106 "IRC server interactive selection history list.")
2108 (defvar erc-nick-history-list nil
2109 "Nickname interactive selection history list.")
2111 (defun erc-already-logged-in (server port nick)
2112 "Return the buffers corresponding to a NICK on PORT of a session SERVER.
2113 This is determined by looking for the appropriate buffer and checking
2114 whether the connection is still alive.
2115 If no buffer matches, return nil."
2118 (and (erc-server-process-alive)
2119 (string= erc-session-server server)
2120 (erc-port-equal erc-session-port port)
2121 (erc-current-nick-p nick)))))
2123 (defcustom erc-before-connect nil
2124 "Hook called before connecting to a server.
2125 This hook gets executed before `erc' actually invokes `erc-mode'
2126 with your input data. The functions in here get called with three
2127 parameters, SERVER, PORT and NICK."
2131 (defcustom erc-after-connect nil
2132 "Hook called after connecting to a server.
2133 This hook gets executed when an end of MOTD has been received. All
2134 functions in here get called with the parameters SERVER and NICK."
2139 (defun erc-select-read-args ()
2140 "Prompt the user for values of nick, server, port, and password."
2141 (let (user-input server port nick passwd)
2142 (setq user-input (read-from-minibuffer
2144 (erc-compute-server) nil nil 'erc-server-history-list))
2146 (if (string-match "\\(.*\\):\\(.*\\)\\'" user-input)
2147 (setq port (erc-string-to-port (match-string 2 user-input))
2148 user-input (match-string 1 user-input))
2150 (erc-string-to-port (read-from-minibuffer
2151 "IRC port: " (erc-port-to-string
2152 (erc-compute-port))))))
2154 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
2155 (setq nick (match-string 1 user-input)
2156 user-input (match-string 2 user-input))
2158 (if (erc-already-logged-in server port nick)
2159 (read-from-minibuffer
2160 (erc-format-message 'nick-in-use ?n nick)
2162 nil nil 'erc-nick-history-list)
2163 (read-from-minibuffer
2164 "Nickname: " (erc-compute-nick nick)
2165 nil nil 'erc-nick-history-list))))
2167 (setq server user-input)
2169 (setq passwd (if erc-prompt-for-password
2170 (if (and erc-password
2171 (y-or-n-p "Use the default password? "))
2173 (read-passwd "Password: "))
2175 (when (and passwd (string= "" passwd))
2178 (while (erc-already-logged-in server port nick)
2179 ;; hmm, this is a problem when using multiple connections to a bnc
2180 ;; with the same nick. Currently this code prevents using more than one
2181 ;; bnc with the same nick. actually it would be nice to have
2182 ;; bncs transparent, so that erc-compute-buffer-name displays
2183 ;; the server one is connected to.
2184 (setq nick (read-from-minibuffer
2185 (erc-format-message 'nick-in-use ?n nick)
2187 nil nil 'erc-nick-history-list)))
2188 (list :server server :port port :nick nick :password passwd)))
2191 (cl-defun erc (&key (server (erc-compute-server))
2192 (port (erc-compute-port))
2193 (nick (erc-compute-nick))
2195 (full-name (erc-compute-full-name)))
2196 "ERC is a powerful, modular, and extensible IRC client.
2197 This function is the main entry point for ERC.
2199 It permits you to select connection parameters, and then starts ERC.
2201 Non-interactively, it takes the keyword arguments
2202 (server (erc-compute-server))
2203 (port (erc-compute-port))
2204 (nick (erc-compute-nick))
2206 (full-name (erc-compute-full-name)))
2208 That is, if called with
2210 (erc :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
2212 then the server and full-name will be set to those values, whereas
2213 `erc-compute-port', `erc-compute-nick' and `erc-compute-full-name' will
2214 be invoked for the values of the other parameters."
2215 (interactive (erc-select-read-args))
2216 (erc-open server port nick full-name t password))
2219 (defalias 'erc-select 'erc)
2220 (defalias 'erc-ssl 'erc-tls)
2223 (defun erc-tls (&rest r)
2224 "Interactively select TLS connection parameters and run ERC.
2225 Arguments are the same as for `erc'."
2226 (interactive (let ((erc-default-port erc-default-port-tls))
2227 (erc-select-read-args)))
2228 (let ((erc-server-connect-function 'erc-open-tls-stream))
2231 (defun erc-open-tls-stream (name buffer host port)
2232 "Open an TLS stream to an IRC server.
2233 The process will be given the name NAME, its target buffer will be
2234 BUFFER. HOST and PORT specify the connection target."
2235 (open-network-stream name buffer host port
2239 ;;; Displaying error messages
2241 (defun erc-error (&rest args)
2242 "Pass ARGS to `format', and display the result as an error message.
2243 If `debug-on-error' is set to non-nil, then throw a real error with this
2244 message instead, to make debugging easier."
2246 (apply #'error args)
2247 (apply #'message args)
2250 ;;; Debugging the protocol
2252 (defvar erc-debug-irc-protocol nil
2253 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2255 The buffer is created if it doesn't exist.
2257 NOTE: If this variable is non-nil, and you kill the only
2258 visible \"*erc-protocol*\" buffer, it will be recreated shortly,
2259 but you won't see it.
2261 WARNING: Do not set this variable directly! Instead, use the
2262 function `erc-toggle-debug-irc-protocol' to toggle its value.")
2264 (declare-function erc-network-name "erc-networks" ())
2266 (defun erc-log-irc-protocol (string &optional outbound)
2267 "Append STRING to the buffer *erc-protocol*.
2269 This only has any effect if `erc-debug-irc-protocol' is non-nil.
2271 The buffer is created if it doesn't exist.
2273 If OUTBOUND is non-nil, STRING is being sent to the IRC server
2274 and appears in face `erc-input-face' in the buffer."
2275 (when erc-debug-irc-protocol
2276 (let ((network-name (or (ignore-errors (erc-network-name))
2278 (with-current-buffer (get-buffer-create "*erc-protocol*")
2280 (goto-char (point-max))
2281 (let ((inhibit-read-only t))
2282 (insert (if (not outbound)
2283 ;; Cope with the fact that string might
2284 ;; contain multiple lines of text.
2285 (let ((lines (delete "" (split-string string
2288 (dolist (line lines)
2289 (setq result (concat result network-name
2293 (concat network-name " >> " string
2296 (1- (length string))))
2298 'font-lock-face 'erc-input-face)))))
2299 (let ((orig-win (selected-window))
2300 (debug-buffer-window (get-buffer-window (current-buffer) t)))
2301 (when debug-buffer-window
2302 (select-window debug-buffer-window)
2303 (when (= 1 (count-lines (point) (point-max)))
2304 (goto-char (point-max))
2306 (select-window orig-win)))))))
2308 (defun erc-toggle-debug-irc-protocol (&optional arg)
2309 "Toggle the value of `erc-debug-irc-protocol'.
2311 If ARG is non-nil, show the *erc-protocol* buffer."
2313 (let* ((buf (get-buffer-create "*erc-protocol*")))
2314 (with-current-buffer buf
2315 (erc-view-mode-enter)
2316 (when (null (current-local-map))
2317 (let ((inhibit-read-only t))
2318 (insert (erc-make-notice "This buffer displays all IRC protocol traffic exchanged with each server.\n"))
2319 (insert (erc-make-notice "Kill this buffer to terminate protocol logging.\n\n")))
2320 (use-local-map (make-sparse-keymap))
2321 (local-set-key (kbd "t") 'erc-toggle-debug-irc-protocol))
2322 (add-hook 'kill-buffer-hook
2323 #'(lambda () (setq erc-debug-irc-protocol nil))
2325 (goto-char (point-max))
2326 (let ((inhibit-read-only t))
2327 (insert (erc-make-notice
2328 (format "IRC protocol logging %s at %s -- Press `t' to toggle logging.\n"
2329 (if erc-debug-irc-protocol "disabled" "enabled")
2330 (current-time-string))))))
2331 (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
2333 (not (get-buffer-window "*erc-protocol*" t)))
2334 (display-buffer buf t))
2335 (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
2336 (if erc-debug-irc-protocol "enabled" "disabled"))))
2342 (defun erc-send-action (tgt str &optional force)
2343 "Send CTCP ACTION information described by STR to TGT."
2344 (erc-send-ctcp-message tgt (format "ACTION %s" str) force)
2345 (erc-display-message
2346 nil 'input (current-buffer)
2347 'ACTION ?n (erc-current-nick) ?a str ?u "" ?h ""))
2349 ;; Display interface
2351 (defun erc-string-invisible-p (string)
2352 "Check whether STRING is invisible or not.
2353 I.e. any char in it has the `invisible' property set."
2354 (text-property-any 0 (length string) 'invisible t string))
2356 (defcustom erc-remove-parsed-property t
2357 "Whether to remove the erc-parsed text property after displaying a message.
2359 The default is to remove it, since it causes ERC to take up extra
2360 memory. If you have code that relies on this property, then set
2361 this option to nil."
2365 (defun erc-display-line-1 (string buffer)
2366 "Display STRING in `erc-mode' BUFFER.
2367 Auxiliary function used in `erc-display-line'. The line gets filtered to
2368 interpret the control characters. Then, `erc-insert-pre-hook' gets called.
2369 If `erc-insert-this' is still t, STRING gets inserted into the buffer.
2370 Afterwards, `erc-insert-modify' and `erc-insert-post-hook' get called.
2371 If STRING is nil, the function does nothing."
2373 (with-current-buffer (or buffer (process-buffer erc-server-process))
2374 (let ((insert-position (or (marker-position erc-insert-marker)
2376 (let ((string string) ;; FIXME! Can this be removed?
2377 (buffer-undo-list t)
2378 (inhibit-read-only t))
2379 (unless (string-match "\n$" string)
2380 (setq string (concat string "\n"))
2381 (when (erc-string-invisible-p string)
2382 (erc-put-text-properties 0 (length string)
2383 '(invisible intangible) string)))
2384 (erc-log (concat "erc-display-line: " string
2385 (format "(%S)" string) " in buffer "
2386 (format "%s" buffer)))
2387 (setq erc-insert-this t)
2388 (run-hook-with-args 'erc-insert-pre-hook string)
2389 (if (null erc-insert-this)
2390 ;; Leave erc-insert-this set to t as much as possible. Fran
2391 ;; Litterio <franl> has seen erc-insert-this set to nil while
2392 ;; erc-send-pre-hook is running, which should never happen. This
2394 (setq erc-insert-this t)
2395 (save-excursion ;; to restore point in the new buffer
2398 (goto-char insert-position)
2399 (insert-before-markers string)
2400 ;; run insertion hook, with point at restored location
2402 (narrow-to-region insert-position (point))
2403 (run-hooks 'erc-insert-modify-hook)
2404 (run-hooks 'erc-insert-post-hook)
2405 (when erc-remove-parsed-property
2406 (remove-text-properties (point-min) (point-max)
2407 '(erc-parsed nil))))))))
2408 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2410 insert-position))))))
2412 (defun erc-update-undo-list (shift)
2413 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2414 (unless (or (zerop shift) (atom buffer-undo-list))
2415 (let ((list buffer-undo-list) elt)
2417 (setq elt (car list))
2418 (cond ((integerp elt) ; POSITION
2419 (cl-incf (car list) shift))
2420 ((or (atom elt) ; nil, EXTENT
2421 ;; (eq t (car elt)) ; (t . TIME)
2422 (markerp (car elt))) ; (MARKER . DISTANCE)
2424 ((integerp (car elt)) ; (BEGIN . END)
2425 (cl-incf (car elt) shift)
2426 (cl-incf (cdr elt) shift))
2427 ((stringp (car elt)) ; (TEXT . POSITION)
2428 (cl-incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
2429 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2430 (let ((cons (nthcdr 3 elt)))
2431 (cl-incf (car cons) shift)
2432 (cl-incf (cdr cons) shift)))
2433 ((and (featurep 'xemacs)
2434 (extentp (car elt))) ; (EXTENT START END)
2435 (cl-incf (nth 1 elt) shift)
2436 (cl-incf (nth 2 elt) shift)))
2437 (setq list (cdr list))))))
2439 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2440 "Regexp which matches all valid characters in a IRC nickname.")
2442 (defun erc-is-valid-nick-p (nick)
2443 "Check if NICK is a valid IRC nickname."
2444 (string-match (concat "\\`" erc-valid-nick-regexp "\\'") nick))
2446 (defun erc-display-line (string &optional buffer)
2447 "Display STRING in the ERC BUFFER.
2448 All screen output must be done through this function. If BUFFER is nil
2449 or omitted, the default ERC buffer for the `erc-session-server' is used.
2450 The BUFFER can be an actual buffer, a list of buffers, `all' or `active'.
2451 If BUFFER = `all', the string is displayed in all the ERC buffers for the
2452 current session. `active' means the current active buffer
2453 \(`erc-active-buffer'). If the buffer can't be resolved, the current
2454 buffer is used. `erc-display-line-1' is used to display STRING.
2456 If STRING is nil, the function does nothing."
2457 (let ((inhibit-point-motion-hooks t)
2460 ((bufferp buffer) (list buffer))
2461 ((listp buffer) buffer)
2462 ((processp buffer) (list (process-buffer buffer)))
2464 ;; Hmm, or all of the same session server?
2465 (erc-buffer-list nil erc-server-process))
2466 ((and (eq 'active buffer) (erc-active-buffer))
2467 (list (erc-active-buffer)))
2468 ((erc-server-buffer-live-p)
2469 (list (process-buffer erc-server-process)))
2470 (t (list (current-buffer)))))
2471 (when (buffer-live-p buf)
2472 (erc-display-line-1 string buf)
2473 (push buf new-bufs)))
2474 (when (null new-bufs)
2475 (erc-display-line-1 string (if (erc-server-buffer-live-p)
2476 (process-buffer erc-server-process)
2477 (current-buffer))))))
2479 (defun erc-display-message-highlight (type string)
2480 "Highlight STRING according to TYPE, where erc-TYPE-face is an ERC face.
2482 See also `erc-make-notice'."
2483 (cond ((eq type 'notice)
2484 (erc-make-notice string))
2486 (erc-put-text-property
2488 'font-lock-face (or (intern-soft
2489 (concat "erc-" (symbol-name type) "-face"))
2494 (defvar erc-lurker-state nil
2495 "Track the time of the last PRIVMSG for each (server,nick) pair.
2497 This is implemented as a hash of hashes, where the outer key is
2498 the canonicalized server name (as returned by
2499 `erc-canonicalize-server-name') and the outer value is a hash
2500 table mapping nicks (as returned by `erc-lurker-maybe-trim') to
2501 the times of their most recently received PRIVMSG on any channel
2502 on the given server.")
2504 (defcustom erc-lurker-trim-nicks t
2505 "If t, trim trailing `erc-lurker-ignore-chars' from nicks.
2507 This causes e.g. nick and nick\\=` to be considered as the same
2508 individual for activity tracking and lurkiness detection
2513 (defcustom erc-lurker-ignore-chars "`_"
2514 "Characters at the end of a nick to strip for activity tracking purposes.
2516 See also `erc-lurker-trim-nicks'."
2520 (defun erc-lurker-maybe-trim (nick)
2521 "Maybe trim trailing `erc-lurker-ignore-chars' from NICK.
2523 Returns NICK unmodified unless `erc-lurker-trim-nicks' is
2525 (if erc-lurker-trim-nicks
2526 (replace-regexp-in-string
2528 (mapconcat (lambda (char)
2529 (regexp-quote (char-to-string char)))
2530 erc-lurker-ignore-chars ""))
2534 (defcustom erc-lurker-hide-list nil
2535 "List of IRC type messages to hide when sent by lurkers.
2537 A typical value would be \(\"JOIN\" \"PART\" \"QUIT\").
2538 See also `erc-lurker-p' and `erc-hide-list'."
2540 :type 'erc-message-type)
2542 (defcustom erc-lurker-threshold-time (* 60 60 24) ; 24h by default
2543 "Nicks from which no PRIVMSGs have been received within this
2544 interval (in units of seconds) are considered lurkers by
2545 `erc-lurker-p' and as a result their messages of types in
2546 `erc-lurker-hide-list' will be hidden."
2550 (defun erc-lurker-initialize ()
2551 "Initialize ERC lurker tracking functionality.
2553 This function adds `erc-lurker-update-status' to
2554 `erc-insert-pre-hook' in order to record the time of each nick's
2555 most recent PRIVMSG as well as initializing the state variable
2556 storing this information."
2557 (setq erc-lurker-state (make-hash-table :test 'equal))
2558 (add-hook 'erc-insert-pre-hook 'erc-lurker-update-status))
2560 (defun erc-lurker-cleanup ()
2561 "Remove all last PRIVMSG state older than `erc-lurker-threshold-time'.
2563 This should be called regularly to avoid excessive resource
2564 consumption for long-lived IRC or Emacs sessions."
2566 (lambda (server hash)
2568 (lambda (nick last-PRIVMSG-time)
2570 (> (float-time (time-subtract nil last-PRIVMSG-time))
2571 erc-lurker-threshold-time)
2572 (remhash nick hash)))
2574 (if (zerop (hash-table-count hash))
2575 (remhash server erc-lurker-state)))
2578 (defvar erc-lurker-cleanup-count 0
2579 "Internal counter variable for use with `erc-lurker-cleanup-interval'.")
2581 (defvar erc-lurker-cleanup-interval 100
2582 "Frequency of cleaning up stale erc-lurker state.
2584 `erc-lurker-update-status' calls `erc-lurker-cleanup' once for
2585 every `erc-lurker-cleanup-interval' updates to
2586 `erc-lurker-state'. This is designed to limit the memory
2587 consumption of lurker state during long Emacs sessions and/or ERC
2588 sessions with large numbers of incoming PRIVMSGs.")
2590 (defun erc-lurker-update-status (_message)
2591 "Update `erc-lurker-state' if necessary.
2593 This function is called from `erc-insert-pre-hook'. If the
2594 current message is a PRIVMSG, update `erc-lurker-state' to
2595 reflect the fact that its sender has issued a PRIVMSG at the
2596 current time. Otherwise, take no action.
2598 This function depends on the fact that `erc-display-message'
2599 dynamically binds `parsed', which is used to check if the current
2600 message is a PRIVMSG and to determine its sender. See also
2601 `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'.
2603 In order to limit memory consumption, this function also calls
2604 `erc-lurker-cleanup' once every `erc-lurker-cleanup-interval'
2605 updates of `erc-lurker-state'."
2606 (when (and (boundp 'parsed) (erc-response-p parsed))
2607 (let* ((command (erc-response.command parsed))
2609 (erc-lurker-maybe-trim
2610 (car (erc-parse-user (erc-response.sender parsed)))))
2612 (erc-canonicalize-server-name erc-server-announced-name)))
2613 (when (equal command "PRIVMSG")
2614 (when (>= (cl-incf erc-lurker-cleanup-count)
2615 erc-lurker-cleanup-interval)
2616 (setq erc-lurker-cleanup-count 0)
2617 (erc-lurker-cleanup))
2618 (unless (gethash server erc-lurker-state)
2619 (puthash server (make-hash-table :test 'equal) erc-lurker-state))
2620 (puthash sender (current-time)
2621 (gethash server erc-lurker-state))))))
2623 (defun erc-lurker-p (nick)
2624 "Predicate indicating NICK's lurking status on the current server.
2626 Lurking is the condition where NICK has issued no PRIVMSG on this
2627 server within `erc-lurker-threshold-time'. See also
2628 `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'."
2629 (unless erc-lurker-state (erc-lurker-initialize))
2631 (erc-canonicalize-server-name erc-server-announced-name))
2633 (gethash (erc-lurker-maybe-trim nick)
2634 (gethash server erc-lurker-state (make-hash-table)))))
2635 (or (null last-PRIVMSG-time)
2637 (time-subtract nil last-PRIVMSG-time))
2638 erc-lurker-threshold-time))))
2640 (defcustom erc-common-server-suffixes
2641 '(("openprojects.net\\'" . "OPN")
2642 ("freenode.net\\'" . "freenode")
2643 ("oftc.net\\'" . "OFTC"))
2644 "Alist of common server name suffixes.
2645 This variable is used in mode-line display to save screen
2646 real estate. Set it to nil if you want to avoid changing
2647 displayed hostnames."
2648 :group 'erc-mode-line-and-header
2651 (defun erc-canonicalize-server-name (server)
2652 "Return the canonical network name for SERVER if any,
2653 otherwise `erc-server-announced-name'. SERVER is matched against
2654 `erc-common-server-suffixes'."
2656 (or (cdar (erc-remove-if-not
2657 (lambda (net) (string-match (car net) server))
2658 erc-common-server-suffixes))
2659 erc-server-announced-name)))
2661 (defun erc-add-targets (scope target-list)
2663 (mapcar (lambda (targets) (member scope targets)) target-list)))
2664 (cdr (apply 'append (delete nil targets)))))
2666 (defun erc-hide-current-message-p (parsed)
2667 "Predicate indicating whether the parsed ERC response PARSED should be hidden.
2669 Messages are always hidden if the message type of PARSED appears in
2670 `erc-hide-list'. Message types that appear in `erc-network-hide-list'
2671 or `erc-channel-hide-list' are only hidden if the target matches
2672 the network or channel in the list. In addition, messages whose type
2673 is a member of `erc-lurker-hide-list' are hidden if `erc-lurker-p'
2675 (let* ((command (erc-response.command parsed))
2676 (sender (car (erc-parse-user (erc-response.sender parsed))))
2677 (channel (nth 1 (erc-response.command-args parsed)))
2678 (network (or (and (fboundp 'erc-network-name) (erc-network-name))
2679 (erc-shorten-server-name
2680 (or erc-server-announced-name
2681 erc-session-server))))
2683 (when erc-network-hide-list
2684 (erc-add-targets network erc-network-hide-list)))
2686 (apply 'append current-hide-list
2687 (when erc-channel-hide-list
2688 (erc-add-targets channel erc-channel-hide-list)))))
2689 (or (member command erc-hide-list)
2690 (member command current-hide-list)
2691 (and (member command erc-lurker-hide-list) (erc-lurker-p sender)))))
2693 (defun erc-display-message (parsed type buffer msg &rest args)
2694 "Display MSG in BUFFER.
2696 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2698 See also `erc-format-message' and `erc-display-line'."
2699 (let ((string (if (symbolp msg)
2700 (apply #'erc-format-message msg args)
2707 (mapc (lambda (type)
2709 (erc-display-message-highlight type string)))
2713 (erc-display-message-highlight type string))))
2715 (if (not (erc-response-p parsed))
2716 (erc-display-line string buffer)
2717 (unless (erc-hide-current-message-p parsed)
2718 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
2719 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2720 (when (erc-response.tags parsed)
2721 (erc-put-text-property 0 (length string) 'tags (erc-response.tags parsed)
2723 (erc-display-line string buffer)))))
2725 (defun erc-message-type-member (position list)
2726 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2728 This function relies on the erc-parsed text-property being
2730 (let ((prop-val (erc-get-parsed-vector position)))
2731 (and prop-val (member (erc-response.command prop-val) list))))
2733 (defvar erc-send-input-line-function 'erc-send-input-line)
2734 (make-variable-buffer-local 'erc-send-input-line-function)
2736 (defun erc-send-input-line (target line &optional force)
2737 "Send LINE to TARGET.
2739 See also `erc-server-send'."
2740 (setq line (format "PRIVMSG %s :%s"
2742 ;; If the line is empty, we still want to
2743 ;; send it - i.e. an empty pasted line.
2744 (if (string= line "\n")
2747 (erc-server-send line force target))
2749 (defun erc-get-arglist (fun)
2750 "Return the argument list of a function without the parens."
2751 (let ((arglist (format "%S" (erc-function-arglist fun))))
2752 (if (string-match "\\`(\\(.*\\))\\'" arglist)
2753 (match-string 1 arglist)
2756 (defun erc-command-no-process-p (str)
2757 "Return non-nil if STR is an ERC command that can be run when the process
2758 is not alive, nil otherwise."
2759 (let ((fun (erc-extract-command-from-line str)))
2762 (get (car fun) 'process-not-needed))))
2764 (defun erc-command-name (cmd)
2765 "For CMD being the function name of a ERC command, something like
2766 erc-cmd-FOO, this returns a string /FOO."
2767 (let ((command-name (symbol-name cmd)))
2768 (if (string-match "\\`erc-cmd-\\(.*\\)\\'" command-name)
2769 (concat "/" (match-string 1 command-name))
2772 (defun erc-process-input-line (line &optional force no-command)
2773 "Translate LINE to an RFC1459 command and send it based.
2774 Returns non-nil if the command is actually sent to the server, and nil
2777 If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
2778 it is passed to `erc-cmd-default'. If LINE is not a command (i.e. doesn't
2779 start with /<COMMAND>) then it is sent as a message.
2781 An optional FORCE argument forces sending the line when flood
2782 protection is in effect. The optional NO-COMMAND argument prohibits
2783 this function from interpreting the line as a command."
2784 (let ((command-list (erc-extract-command-from-line line)))
2785 (if (and command-list
2787 (let* ((cmd (nth 0 command-list))
2788 (args (nth 1 command-list)))
2793 (wrong-number-of-arguments
2794 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2795 ?c (erc-command-name cmd)
2796 ?u (or (erc-get-arglist cmd)
2799 (or (documentation cmd) "")))
2801 (let ((r (erc-default-target)))
2803 (funcall erc-send-input-line-function r line force)
2804 (erc-display-message nil 'error (current-buffer) 'no-target)
2807 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2808 ;; Input commands handlers
2809 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2811 (defun erc-cmd-AMSG (line)
2812 "Send LINE to all channels of the current server that you are on."
2813 (interactive "sSend to all channels you're on: ")
2814 (setq line (erc-trim-string line))
2815 (erc-with-all-buffers-of-server nil
2817 (erc-channel-p (erc-default-target)))
2818 (erc-send-message line)))
2819 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2821 (defun erc-cmd-SAY (line)
2822 "Send LINE to the current query or channel as a message, not a command.
2824 Use this when you want to send a message with a leading `/'. Note
2825 that since multi-line messages are never a command, you don't
2826 need this when pasting multiple lines of text."
2827 (if (string-match "^\\s-*$" line)
2829 (string-match "^ ?\\(.*\\)" line)
2830 (erc-process-input-line (match-string 1 line) nil t)))
2831 (put 'erc-cmd-SAY 'do-not-parse-args t)
2833 (defun erc-cmd-SET (line)
2834 "Set the variable named by the first word in LINE to some VALUE.
2835 VALUE is computed by evaluating the rest of LINE in Lisp."
2837 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2838 (let ((var (read (concat "erc-" (match-string 1 line))))
2839 (val (read (match-string 2 line))))
2842 (set var (eval val))
2843 (erc-display-message
2844 nil nil 'active (format "Set %S to %S" var val))
2846 (setq var (read (match-string 1 line)))
2849 (set var (eval val))
2850 (erc-display-message
2851 nil nil 'active (format "Set %S to %S" var val))
2853 (erc-display-message nil 'error 'active 'variable-not-bound)
2855 ((string-match "^\\s-*$" line)
2857 (concat "Available user variables:\n"
2862 (let ((val (symbol-value var)))
2863 (concat (format "%S:" var)
2865 (concat "\n" (pp-to-string val))
2866 (format " %S\n" val)))))
2867 (apropos-internal "^erc-" 'custom-variable-p))))
2871 (defalias 'erc-cmd-VAR 'erc-cmd-SET)
2872 (defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2873 (put 'erc-cmd-SET 'do-not-parse-args t)
2874 (put 'erc-cmd-SET 'process-not-needed t)
2876 (defun erc-cmd-default (line)
2879 Commands for which no erc-cmd-xxx exists, are tunneled through
2880 this function. LINE is sent to the server verbatim, and
2881 therefore has to contain the command itself as well."
2882 (erc-log (format "cmd: DEFAULT: %s" line))
2883 (erc-server-send (substring line 1))
2886 (defun erc-cmd-IGNORE (&optional user)
2887 "Ignore USER. This should be a regexp matching nick!user@host.
2888 If no USER argument is specified, list the contents of `erc-ignore-list'."
2890 (let ((quoted (regexp-quote user)))
2891 (when (and (not (string= user quoted))
2892 (y-or-n-p (format "Use regexp-quoted form (%s) instead? "
2896 (erc-make-notice (format "Now ignoring %s" user))
2898 (erc-with-server-buffer (add-to-list 'erc-ignore-list user)))
2899 (if (null (erc-with-server-buffer erc-ignore-list))
2900 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2901 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2902 (mapc #'(lambda (item)
2903 (erc-display-line (erc-make-notice item)
2905 (erc-with-server-buffer erc-ignore-list))))
2908 (defun erc-cmd-UNIGNORE (user)
2909 "Remove the user specified in USER from the ignore list."
2910 (let ((ignored-nick (car (erc-with-server-buffer
2911 (erc-member-ignore-case (regexp-quote user)
2912 erc-ignore-list)))))
2913 (unless ignored-nick
2914 (if (setq ignored-nick (erc-ignored-user-p user))
2915 (unless (y-or-n-p (format "Remove this regexp (%s)? "
2917 (setq ignored-nick nil))
2919 (erc-make-notice (format "%s is not currently ignored!" user))
2923 (erc-make-notice (format "No longer ignoring %s" user))
2925 (erc-with-server-buffer
2926 (setq erc-ignore-list (delete ignored-nick erc-ignore-list)))))
2929 (defun erc-cmd-CLEAR ()
2930 "Clear the window content."
2933 (put 'erc-cmd-CLEAR 'process-not-needed t)
2935 (defun erc-cmd-OPS ()
2936 "Show the ops in the current channel."
2939 (if erc-channel-users
2940 (maphash (lambda (_nick user-data)
2941 (let ((cuser (cdr user-data)))
2943 (erc-channel-user-op cuser))
2944 (setq ops (cons (erc-server-user-nickname
2948 (setq ops (sort ops 'string-lessp))
2950 (erc-display-message
2951 nil 'notice (current-buffer) 'ops
2952 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2953 ?o (mapconcat 'identity ops " "))
2954 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2957 (defun erc-cmd-COUNTRY (tld)
2958 "Display the country associated with the top level domain TLD."
2959 (require 'mail-extr)
2960 (let ((co (ignore-errors (what-domain tld))))
2962 (erc-display-message
2963 nil 'notice 'active 'country ?c co ?d tld)
2964 (erc-display-message
2965 nil 'notice 'active 'country-unknown ?d tld))
2967 (put 'erc-cmd-COUNTRY 'process-not-needed t)
2969 (defun erc-cmd-AWAY (line)
2970 "Mark the user as being away, the reason being indicated by LINE.
2971 If no reason is given, unset away status."
2972 (when (string-match "^\\s-*\\(.*\\)$" line)
2973 (let ((reason (match-string 1 line)))
2974 (erc-log (format "cmd: AWAY: %s" reason))
2976 (if (string= reason "")
2978 (concat "AWAY :" reason))))
2980 (put 'erc-cmd-AWAY 'do-not-parse-args t)
2982 (defun erc-cmd-GAWAY (line)
2983 "Mark the user as being away everywhere, the reason being indicated by LINE."
2984 ;; on all server buffers.
2985 (erc-with-all-buffers-of-server nil
2986 #'erc-open-server-buffer-p
2987 (erc-cmd-AWAY line)))
2988 (put 'erc-cmd-GAWAY 'do-not-parse-args t)
2990 (defun erc-cmd-CTCP (nick cmd &rest args)
2991 "Send a Client To Client Protocol message to NICK.
2993 CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2994 VERSION and so on. It is called with ARGS."
2995 (let ((str (concat cmd
2997 (concat " " (mapconcat #'identity args " "))))))
2998 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2999 (erc-send-ctcp-message nick str)
3002 (defun erc-cmd-HELP (&optional func)
3003 "Popup help information.
3005 If FUNC contains a valid function or variable, help about that
3006 will be displayed. If FUNC is empty, display an apropos about
3007 ERC commands. Otherwise, do `apropos' in the ERC namespace
3011 To find out about erc and bbdb, do
3014 For help about the WHOIS command, do:
3017 For a list of user commands (/join /part, ...):
3020 (let* ((sym (or (let ((sym (intern-soft
3021 (concat "erc-cmd-" (upcase func)))))
3022 (if (and sym (or (boundp sym) (fboundp sym)))
3025 (let ((sym (intern-soft func)))
3026 (if (and sym (or (boundp sym) (fboundp sym)))
3029 (let ((sym (intern-soft (concat "erc-" func))))
3030 (if (and sym (or (boundp sym) (fboundp sym)))
3035 ((boundp sym) (describe-variable sym))
3036 ((fboundp sym) (describe-function sym))
3038 (apropos-command (concat "erc-.*" func) nil
3041 (get x 'custom-type))))
3043 (apropos "erc-cmd-")
3044 (message "Type C-h m to get additional information about keybindings.")
3047 (defalias 'erc-cmd-H 'erc-cmd-HELP)
3048 (put 'erc-cmd-HELP 'process-not-needed t)
3050 (defun erc-server-join-channel (server channel &optional secret)
3051 (let* ((secret (or secret
3052 (plist-get (nth 0 (auth-source-search
3058 (password (if (functionp secret)
3061 (erc-log (format "cmd: JOIN: %s" channel))
3062 (erc-server-send (concat "JOIN " channel
3064 (concat " " password)
3067 (defun erc-cmd-JOIN (channel &optional key)
3068 "Join the channel given in CHANNEL, optionally with KEY.
3069 If CHANNEL is specified as \"-invite\", join the channel to which you
3070 were most recently invited. See also `invitation'."
3072 (if (string= (upcase channel) "-INVITE")
3074 (setq chnl erc-invitation)
3075 (erc-display-message nil 'error (current-buffer) 'no-invitation))
3076 (setq chnl (erc-ensure-channel-name channel)))
3078 ;; Prevent double joining of same channel on same server.
3079 (let ((joined-channels
3080 (mapcar #'(lambda (chanbuf)
3081 (with-current-buffer chanbuf (erc-default-target)))
3082 (erc-channel-list erc-server-process))))
3083 (if (erc-member-ignore-case chnl joined-channels)
3084 (switch-to-buffer (car (erc-member-ignore-case chnl
3086 (let ((server (with-current-buffer (process-buffer erc-server-process)
3087 (or erc-session-server erc-server-announced-name))))
3088 (erc-server-join-channel server chnl key))))))
3091 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
3092 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
3094 (defvar erc-channel-new-member-names nil
3095 "If non-nil, a names list is currently being received.
3097 If non-nil, this variable is a hash-table that associates
3098 received nicks with t.")
3099 (make-variable-buffer-local 'erc-channel-new-member-names)
3101 (defun erc-cmd-NAMES (&optional channel)
3102 "Display the users in CHANNEL.
3103 If CHANNEL is not specified, display the users in the current channel.
3104 This function clears the channel name list first, then sends the
3106 (let ((tgt (or (and (erc-channel-p channel) channel)
3107 (erc-default-target))))
3108 (if (and tgt (erc-channel-p tgt))
3110 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
3113 (erc-channel-begin-receiving-names))
3114 (erc-server-send (concat "NAMES " tgt)))
3115 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
3117 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
3119 (defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
3120 "Kick the user indicated in LINE from the current channel.
3121 LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
3122 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
3123 (if (string= "" reasonstring)
3124 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
3125 (if (erc-channel-p target)
3126 (let ((nick reason-or-nick))
3127 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
3128 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
3132 (let ((ch (erc-default-target)))
3133 (setq reasonstring (concat
3134 (if reason-or-nick (concat reason-or-nick " "))
3139 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
3141 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
3142 (erc-display-message nil 'error (current-buffer)
3143 'no-default-channel))
3146 (defvar erc-script-args nil)
3148 (defun erc-cmd-LOAD (line)
3149 "Load the script provided in the LINE.
3150 If LINE continues beyond the file name, the rest of
3151 it is put in a (local) variable `erc-script-args',
3152 which can be used in Emacs Lisp scripts.
3154 The optional FORCE argument is ignored here - you can't force loading
3155 a script after exceeding the flood threshold."
3157 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
3158 (let* ((file-to-find (match-string 1 line))
3159 (erc-script-args (match-string 2 line))
3160 (file (erc-find-file file-to-find erc-script-path)))
3161 (erc-log (format "cmd: LOAD: %s" file-to-find))
3164 (erc-display-message nil 'error (current-buffer)
3165 'cannot-find-file ?f file-to-find))
3166 ((not (file-readable-p file))
3167 (erc-display-message nil 'error (current-buffer)
3168 'cannot-read-file ?f file))
3170 (message "Loading `%s'..." file)
3171 (erc-load-script file)
3172 (message "Loading `%s'...done" file))))
3176 (defun erc-cmd-WHOIS (user &optional server)
3177 "Display whois information for USER.
3179 If SERVER is non-nil, use that, rather than the current server."
3180 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
3181 (let ((send (if server
3182 (format "WHOIS %s %s" user server)
3183 (format "WHOIS %s" user))))
3184 (erc-log (format "cmd: %s" send))
3185 (erc-server-send send)
3187 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
3189 (defun erc-cmd-WHOAMI ()
3190 "Display whois information about yourself."
3191 (erc-cmd-WHOIS (erc-current-nick))
3194 (defun erc-cmd-IDLE (nick)
3195 "Show the length of time NICK has been idle."
3196 (let ((origbuf (current-buffer))
3198 (erc-with-server-buffer
3199 (push (cons (erc-once-with-server-event
3200 311 (lambda (_proc parsed)
3202 (nth 1 (erc-response.command-args
3204 'erc-server-311-functions)
3206 (push (cons (erc-once-with-server-event
3207 312 (lambda (_proc parsed)
3209 (nth 1 (erc-response.command-args
3211 'erc-server-312-functions)
3213 (push (cons (erc-once-with-server-event
3214 318 (lambda (_proc parsed)
3216 (nth 1 (erc-response.command-args
3218 'erc-server-318-functions)
3220 (push (cons (erc-once-with-server-event
3221 319 (lambda (_proc parsed)
3223 (nth 1 (erc-response.command-args
3225 'erc-server-319-functions)
3227 (push (cons (erc-once-with-server-event
3228 320 (lambda (_proc parsed)
3230 (nth 1 (erc-response.command-args
3232 'erc-server-320-functions)
3234 (push (cons (erc-once-with-server-event
3235 330 (lambda (_proc parsed)
3237 (nth 1 (erc-response.command-args
3239 'erc-server-330-functions)
3241 (push (cons (erc-once-with-server-event
3243 (lambda (_proc parsed)
3247 (erc-response.command-args parsed)))))
3250 (format "%s has been idle for %s."
3251 (erc-string-no-properties nick)
3252 (erc-seconds-to-string idleseconds)))
3255 'erc-server-317-functions)
3258 ;; Send the WHOIS command.
3259 (erc-cmd-WHOIS nick)
3261 ;; Remove the uninterned symbols from the server hooks that did not run.
3262 (run-at-time 20 nil (lambda (buf symlist)
3263 (with-current-buffer buf
3264 (dolist (sym symlist)
3265 (let ((hooksym (cdr sym))
3266 (funcsym (car sym)))
3267 (remove-hook hooksym funcsym t)))))
3268 (current-buffer) symlist)))
3271 (defun erc-cmd-DESCRIBE (line)
3272 "Pose some action to a certain user.
3273 LINE has the format \"USER ACTION\"."
3276 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
3277 (let ((dst (match-string 1 line))
3278 (s (match-string 2 line)))
3279 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
3280 (erc-send-action dst s))
3283 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
3285 (defun erc-cmd-ME (line)
3286 "Send LINE as an action."
3288 ((string-match "^\\s-\\(.*\\)$" line)
3289 (let ((s (match-string 1 line)))
3290 (erc-log (format "cmd: ME: %s" s))
3291 (erc-send-action (erc-default-target) s))
3294 (put 'erc-cmd-ME 'do-not-parse-args t)
3296 (defun erc-cmd-ME\'S (line)
3297 "Do a /ME command, but add the string \" \\='s\" to the beginning."
3298 (erc-cmd-ME (concat " 's" line)))
3299 (put 'erc-cmd-ME\'S 'do-not-parse-args t)
3301 (defun erc-cmd-LASTLOG (line)
3302 "Show all lines in the current buffer matching the regexp LINE.
3304 If a match spreads across multiple lines, all those lines are shown.
3306 The lines are shown in a buffer named `*Occur*'.
3307 It serves as a menu to find any of the occurrences in this buffer.
3308 \\[describe-mode] in that buffer will explain how.
3310 If LINE contains upper case characters (excluding those preceded by `\\'),
3311 the matching is case-sensitive."
3314 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
3315 (put 'erc-cmd-LASTLOG 'process-not-needed t)
3317 (defun erc-send-message (line &optional force)
3318 "Send LINE to the current channel or user and display it.
3320 See also `erc-message' and `erc-display-line'."
3321 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
3323 (concat (erc-format-my-nick) line)
3325 ;; FIXME - treat multiline, run hooks, or remove me?
3328 (defun erc-cmd-MODE (line)
3329 "Change or display the mode value of a channel or user.
3330 The first word specifies the target. The rest is the mode string
3333 If only one word is given, display the mode of that target.
3335 A list of valid mode strings for Freenode may be found at
3336 URL `http://freenode.net/using_the_network.shtml'."
3338 ((string-match "^\\s-\\(.*\\)$" line)
3339 (let ((s (match-string 1 line)))
3340 (erc-log (format "cmd: MODE: %s" s))
3341 (erc-server-send (concat "MODE " line)))
3344 (put 'erc-cmd-MODE 'do-not-parse-args t)
3346 (defun erc-cmd-NOTICE (channel-or-user &rest message)
3347 "Send a notice to the channel or user given as the first word.
3348 The rest is the message to send."
3349 (erc-message "NOTICE" (concat channel-or-user " "
3350 (mapconcat #'identity message " "))))
3352 (defun erc-cmd-MSG (line)
3353 "Send a message to the channel or user given as the first word in LINE.
3355 The rest of LINE is the message to send."
3356 (erc-message "PRIVMSG" line))
3358 (defalias 'erc-cmd-M 'erc-cmd-MSG)
3359 (put 'erc-cmd-MSG 'do-not-parse-args t)
3361 (defun erc-cmd-SQUERY (line)
3362 "Send a Service Query to the service given as the first word in LINE.
3364 The rest of LINE is the message to send."
3365 (erc-message "SQUERY" line))
3367 (defun erc-cmd-NICK (nick)
3368 "Change current nickname to NICK."
3369 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
3370 (let ((nicklen (cdr (assoc "NICKLEN" (erc-with-server-buffer
3371 erc-server-parameters)))))
3372 (and nicklen (> (length nick) (string-to-number nicklen))
3373 (erc-display-message
3374 nil 'notice 'active 'nick-too-long
3375 ?i (length nick) ?l nicklen)))
3376 (erc-server-send (format "NICK %s" nick))
3378 (erc-set-current-nick nick)
3379 (erc-update-mode-line)
3380 (setq erc-bad-nick nil)))
3383 (defun erc-cmd-PART (line)
3384 "When LINE is an empty string, leave the current channel.
3385 Otherwise leave the channel indicated by LINE."
3387 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
3388 (let* ((ch (match-string 1 line))
3389 (msg (match-string 2 line))
3390 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3391 (erc-log (format "cmd: PART: %s: %s" ch reason))
3392 (erc-server-send (if (string= reason "")
3393 (format "PART %s" ch)
3394 (format "PART %s :%s" ch reason))
3397 ((string-match "^\\s-*\\(.*\\)$" line)
3398 (let* ((ch (erc-default-target))
3399 (msg (match-string 1 line))
3400 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3401 (if (and ch (erc-channel-p ch))
3403 (erc-log (format "cmd: PART: %s: %s" ch reason))
3404 (erc-server-send (if (string= reason "")
3405 (format "PART %s" ch)
3406 (format "PART %s :%s" ch reason))
3408 (erc-display-message nil 'error (current-buffer) 'no-target)))
3411 (put 'erc-cmd-PART 'do-not-parse-args t)
3413 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
3415 (defun erc-cmd-PING (recipient)
3417 (let ((time (format "%f" (erc-current-time))))
3418 (erc-log (format "cmd: PING: %s" time))
3419 (erc-cmd-CTCP recipient "PING" time)))
3421 (defun erc-cmd-QUOTE (line)
3422 "Send LINE directly to the server.
3423 All the text given as argument is sent to the sever as unmodified,
3424 just as you provided it. Use this command with care!"
3426 ((string-match "^ ?\\(.+\\)$" line)
3427 (erc-server-send (match-string 1 line)))
3429 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
3431 (defcustom erc-query-display 'window
3432 "Indicates how to display query buffers when using the /QUERY
3433 command to talk to someone.
3435 The default behavior is to display the message in a new window
3436 and bring it to the front. See the documentation for
3437 `erc-join-buffer' for a description of the available choices.
3439 See also `erc-auto-query' to decide how private messages from
3440 other people should be displayed."
3442 :type '(choice (const :tag "Split window and select" window)
3443 (const :tag "Split window, don't select" window-noselect)
3444 (const :tag "New frame" frame)
3445 (const :tag "Bury in new buffer" bury)
3446 (const :tag "Use current buffer" buffer)
3447 (const :tag "Use current buffer" t)))
3449 (defun erc-cmd-QUERY (&optional user)
3450 "Open a query with USER.
3451 The type of query window/frame/etc will depend on the value of
3452 `erc-query-display'.
3454 If USER is omitted, close the current query buffer if one exists
3455 - except this is broken now ;-)"
3457 (list (read-from-minibuffer "Start a query with: " nil)))
3458 (let ((session-buffer (erc-server-buffer))
3459 (erc-join-buffer erc-query-display))
3461 (erc-query user session-buffer)
3462 ;; currently broken, evil hack to display help anyway
3463 ;(erc-delete-query))))
3464 (signal 'wrong-number-of-arguments ""))))
3465 (defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3467 (defun erc-quit/part-reason-default ()
3468 "Default quit/part message."
3469 (format "\C-bERC\C-b (IRC client for Emacs %s)" emacs-version))
3472 (defun erc-quit-reason-normal (&optional s)
3473 "Normal quit message.
3475 If S is non-nil, it will be used as the quit reason."
3476 (or s (erc-quit/part-reason-default)))
3478 (defun erc-quit-reason-zippy (&optional s)
3479 "Zippy quit message.
3481 If S is non-nil, it will be used as the quit reason."
3484 (erc-replace-regexp-in-string "\n" "" (yow))
3485 (erc-quit/part-reason-default))))
3487 (make-obsolete 'erc-quit-reason-zippy "it will be removed." "24.4")
3489 (defun erc-quit-reason-various (s)
3490 "Choose a quit reason based on S (a string)."
3491 (when (featurep 'xemacs) (require 'poe))
3492 (let ((res (car (assoc-default (or s "")
3493 erc-quit-reason-various-alist 'string-match))))
3495 ((functionp res) (funcall res))
3498 (t (erc-quit/part-reason-default)))))
3500 (defun erc-part-reason-normal (&optional s)
3501 "Normal part message.
3503 If S is non-nil, it will be used as the part reason."
3504 (or s (erc-quit/part-reason-default)))
3506 (defun erc-part-reason-zippy (&optional s)
3507 "Zippy part message.
3509 If S is non-nil, it will be used as the quit reason."
3512 (erc-replace-regexp-in-string "\n" "" (yow))
3513 (erc-quit/part-reason-default))))
3515 (make-obsolete 'erc-part-reason-zippy "it will be removed." "24.4")
3517 (defun erc-part-reason-various (s)
3518 "Choose a part reason based on S (a string)."
3519 (when (featurep 'xemacs) (require 'poe))
3520 (let ((res (car (assoc-default (or s "")
3521 erc-part-reason-various-alist 'string-match))))
3523 ((functionp res) (funcall res))
3526 (t (erc-quit/part-reason-default)))))
3528 (defun erc-cmd-QUIT (reason)
3529 "Disconnect from the current server.
3530 If REASON is omitted, display a default quit message, otherwise display
3531 the message given by REASON."
3535 ((string-match "^\\s-*\\(.*\\)$" reason)
3536 (let* ((s (match-string 1 reason))
3537 (buffer (erc-server-buffer))
3538 (reason (funcall erc-quit-reason (if (equal s "") nil s)))
3540 (with-current-buffer (if (and buffer
3544 (erc-log (format "cmd: QUIT: %s" reason))
3545 (setq erc-server-quitting t)
3546 (erc-set-active-buffer (erc-server-buffer))
3547 (setq server-proc erc-server-process)
3548 (erc-server-send (format "QUIT :%s" reason)))
3549 (run-hook-with-args 'erc-quit-hook server-proc)
3550 (when erc-kill-queries-on-quit
3551 (erc-kill-query-buffers server-proc))
3552 ;; if the process has not been killed within 4 seconds, kill it
3555 (when (and (processp proc)
3556 (memq (process-status proc) '(run open)))
3557 (delete-process proc)))
3562 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3563 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3564 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3565 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3566 (put 'erc-cmd-QUIT 'process-not-needed t)
3568 (defun erc-cmd-GQUIT (reason)
3569 "Disconnect from all servers at once with the same quit REASON."
3570 (erc-with-all-buffers-of-server nil #'erc-open-server-buffer-p
3571 (erc-cmd-QUIT reason))
3572 (when erc-kill-queries-on-quit
3573 ;; if the query buffers have not been killed within 4 seconds,
3578 (dolist (buffer (erc-buffer-list (lambda (buf)
3579 (not (erc-server-buffer-p buf)))))
3580 (kill-buffer buffer)))))
3583 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3584 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3585 (put 'erc-cmd-GQUIT 'process-not-needed t)
3587 (defun erc-cmd-RECONNECT ()
3588 "Try to reconnect to the current IRC server."
3589 (let ((buffer (erc-server-buffer))
3591 (unless (buffer-live-p buffer)
3592 (setq buffer (current-buffer)))
3593 (with-current-buffer buffer
3594 (setq erc-server-quitting nil)
3595 (setq erc-server-reconnecting t)
3596 (setq erc-server-reconnect-count 0)
3597 (setq process (get-buffer-process (erc-server-buffer)))
3599 (delete-process process)
3600 (erc-server-reconnect))
3601 (setq erc-server-reconnecting nil)))
3603 (put 'erc-cmd-RECONNECT 'process-not-needed t)
3605 (defun erc-cmd-SERVER (server)
3606 "Connect to SERVER, leaving existing connection intact."
3607 (erc-log (format "cmd: SERVER: %s" server))
3609 (erc :server server :nick (erc-current-nick))
3611 (erc-error "Cannot find host %s." server)))
3613 (put 'erc-cmd-SERVER 'process-not-needed t)
3615 (defvar motif-version-string)
3616 (defvar gtk-version-string)
3618 (defun erc-cmd-SV ()
3619 "Say the current ERC and Emacs version into channel."
3620 (erc-send-message (format "I'm using ERC with %s %s (%s%s)%s."
3621 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3623 system-configuration
3625 (cond ((featurep 'motif)
3626 (concat ", " (substring
3627 motif-version-string 4)))
3629 (concat ", GTK+ Version "
3630 gtk-version-string))
3631 ((featurep 'x-toolkit) ", X toolkit")
3633 (if (and (boundp 'x-toolkit-scroll-bars)
3634 (memq x-toolkit-scroll-bars
3636 (format ", %s scroll bars"
3637 (capitalize (symbol-name
3638 x-toolkit-scroll-bars)))
3640 (if (featurep 'multi-tty) ", multi-tty" ""))
3641 (if erc-emacs-build-time
3642 (concat " of " erc-emacs-build-time)
3646 (defun erc-cmd-SM ()
3647 "Say the current ERC modes into channel."
3648 (erc-send-message (format "I'm using the following modules: %s!"
3652 (defun erc-cmd-DEOP (&rest people)
3653 "Remove the operator setting from user(s) given in PEOPLE."
3654 (when (> (length people) 0)
3655 (erc-server-send (concat "MODE " (erc-default-target)
3657 (make-string (length people) ?o)
3659 (mapconcat 'identity people " ")))
3662 (defun erc-cmd-OP (&rest people)
3663 "Add the operator setting to users(s) given in PEOPLE."
3664 (when (> (length people) 0)
3665 (erc-server-send (concat "MODE " (erc-default-target)
3667 (make-string (length people) ?o)
3669 (mapconcat 'identity people " ")))
3672 (defun erc-cmd-TIME (&optional line)
3673 "Request the current time and date from the current server."
3675 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3676 (let ((args (match-string 1 line)))
3677 (erc-log (format "cmd: TIME: %s" args))
3678 (erc-server-send (concat "TIME " args)))
3680 (t (erc-server-send "TIME"))))
3681 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3683 (defun erc-cmd-TOPIC (topic)
3684 "Set or request the topic for a channel.
3685 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3686 or the empty string.
3688 If no #CHANNEL is given, the default channel is used. If TOPIC is
3689 given, the channel topic is modified, otherwise the current topic will
3692 ;; /topic #channel TOPIC
3693 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3694 (let ((ch (match-string 1 topic))
3695 (topic (match-string 2 topic)))
3696 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3697 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3700 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3701 (let ((ch (match-string 1 topic)))
3702 (erc-server-send (format "TOPIC %s" ch) nil ch)
3705 ((string-match "^\\s-*$" topic)
3706 (let ((ch (erc-default-target)))
3707 (erc-server-send (format "TOPIC %s" ch) nil ch)
3710 ((string-match "^\\s-*\\(.*\\)$" topic)
3711 (let ((ch (erc-default-target))
3712 (topic (match-string 1 topic)))
3713 (if (and ch (erc-channel-p ch))
3715 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3716 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3717 (erc-display-message nil 'error (current-buffer) 'no-target)))
3720 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3721 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3723 (defun erc-cmd-APPENDTOPIC (topic)
3724 "Append TOPIC to the current channel topic, separated by a space."
3725 (let ((oldtopic erc-channel-topic))
3726 ;; display help when given no arguments
3727 (when (string-match "^\\s-*$" topic)
3728 (signal 'wrong-number-of-arguments nil))
3729 ;; strip trailing ^O
3730 (when (string-match "\\(.*\\)\C-o" oldtopic)
3731 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3732 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3733 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3735 (defun erc-cmd-CLEARTOPIC (&optional channel)
3736 "Clear the topic for a CHANNEL.
3737 If CHANNEL is not specified, clear the topic for the default channel."
3738 (interactive "sClear topic of channel (RET is current channel): ")
3739 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3741 (erc-server-send (format "TOPIC %s :" chnl))
3746 (defvar erc-channel-banlist nil
3747 "A list of bans seen for the current channel.
3749 Each ban is an alist of the form:
3752 The property `received-from-server' indicates whether
3753 or not the ban list has been requested from the server.")
3754 (make-variable-buffer-local 'erc-channel-banlist)
3755 (put 'erc-channel-banlist 'received-from-server nil)
3757 (defun erc-cmd-BANLIST ()
3758 "Pretty-print the contents of `erc-channel-banlist'.
3760 The ban list is fetched from the server if necessary."
3761 (let ((chnl (erc-default-target))
3762 (chnl-name (buffer-name)))
3765 ((not (erc-channel-p chnl))
3766 (erc-display-line (erc-make-notice "You're not on a channel\n")
3769 ((not (get 'erc-channel-banlist 'received-from-server))
3770 (let ((old-367-hook erc-server-367-functions))
3771 (setq erc-server-367-functions 'erc-banlist-store
3772 erc-channel-banlist nil)
3773 ;; fetch the ban list then callback
3774 (erc-with-server-buffer
3775 (erc-once-with-server-event
3777 (lambda (_proc _parsed)
3778 (with-current-buffer chnl-name
3779 (put 'erc-channel-banlist 'received-from-server t)
3780 (setq erc-server-367-functions old-367-hook)
3783 (erc-server-send (format "MODE %s b" chnl)))))
3785 ((null erc-channel-banlist)
3786 (erc-display-line (erc-make-notice
3787 (format "No bans for channel: %s\n" chnl))
3789 (put 'erc-channel-banlist 'received-from-server nil))
3792 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3794 (and (boundp 'fill-column)
3796 (1- (window-width))))
3797 (separator (make-string erc-fill-column ?=))
3799 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3800 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3803 (erc-make-notice (format "Ban list for channel: %s\n"
3804 (erc-default-target)))
3807 (erc-display-line separator 'active)
3808 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3809 (erc-display-line separator 'active)
3815 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3817 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3820 erc-channel-banlist)
3822 (erc-display-line (erc-make-notice "End of Ban list")
3824 (put 'erc-channel-banlist 'received-from-server nil)))))
3827 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3829 (defun erc-cmd-MASSUNBAN ()
3832 Unban all currently banned users in the current channel."
3833 (let ((chnl (erc-default-target)))
3836 ((not (erc-channel-p chnl))
3838 (erc-make-notice "You're not on a channel\n")
3841 ((not (get 'erc-channel-banlist 'received-from-server))
3842 (let ((old-367-hook erc-server-367-functions))
3843 (setq erc-server-367-functions 'erc-banlist-store)
3844 ;; fetch the ban list then callback
3845 (erc-with-server-buffer
3846 (erc-once-with-server-event
3848 (lambda (_proc _parsed)
3849 (with-current-buffer chnl
3850 (put 'erc-channel-banlist 'received-from-server t)
3851 (setq erc-server-367-functions old-367-hook)
3854 (erc-server-send (format "MODE %s b" chnl)))))
3856 (t (let ((bans (mapcar #'cdr erc-channel-banlist)))
3858 ;; Glob the bans into groups of three, and carry out the unban.
3859 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3863 (format "MODE %s -%s %s" (erc-default-target)
3864 (make-string (length x) ?b)
3865 (mapconcat 'identity x " "))))
3866 (erc-group-list bans 3))))
3869 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3871 ;;;; End of IRC commands
3873 (defun erc-ensure-channel-name (channel)
3874 "Return CHANNEL if it is a valid channel name.
3875 Eventually add a # in front of it, if that turns it into a valid channel name."
3876 (if (erc-channel-p channel)
3878 (concat "#" channel)))
3880 (defun erc-grab-region (start end)
3881 "Copy the region between START and END in a recreatable format.
3883 Converts all the IRC text properties in each line of the region
3884 into control codes and writes them to a separate buffer. The
3885 resulting text may be used directly as a script to generate this
3888 (erc-set-active-buffer (current-buffer))
3890 (let* ((cb (current-buffer))
3891 (buf (generate-new-buffer erc-grab-buffer-name))
3892 (region (buffer-substring start end))
3893 (lines (erc-split-multiline-safe region)))
3895 (dolist (line lines)
3896 (insert (concat line "\n")))
3898 (switch-to-buffer-other-window buf)))
3899 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3902 (defun erc-display-prompt (&optional buffer pos prompt face)
3903 "Display PROMPT in BUFFER at position POS.
3904 Display an ERC prompt in BUFFER.
3906 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3907 If BUFFER is nil, the `current-buffer' is used.
3908 If POS is nil, PROMPT will be displayed at `point'.
3909 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3910 `erc-prompt-face' will be used."
3911 (let* ((prompt (or prompt (erc-prompt)))
3913 (ob (current-buffer)))
3914 ;; We cannot use save-excursion because we move point, therefore
3915 ;; we resort to the ol' ob trick to restore this.
3916 (when (and buffer (bufferp buffer))
3917 (set-buffer buffer))
3919 ;; now save excursion again to store where point and mark are
3920 ;; in the current buffer
3922 (setq pos (or pos (point)))
3925 ;; Do not extend the text properties when typing at the end
3926 ;; of the prompt, but stuff typed in front of the prompt
3927 ;; shall remain part of the prompt.
3928 (setq prompt (erc-propertize prompt
3929 'start-open t ; XEmacs
3930 'rear-nonsticky t ; Emacs
3935 (erc-put-text-property 0 (1- (length prompt))
3936 'font-lock-face (or face 'erc-prompt-face)
3939 ;; Set the input marker
3940 (set-marker erc-input-marker (point)))
3942 ;; Now we are back at the old position. If the prompt was
3943 ;; inserted here or before us, advance point by the length of
3945 (when (or (not pos) (<= (point) pos))
3947 ;; Clear the undo buffer now, so the user can undo his stuff,
3948 ;; but not the stuff we did. Sneaky!
3949 (setq buffer-undo-list nil)
3952 ;; interactive operations
3954 (defun erc-input-message ()
3955 "Read input from the minibuffer."
3957 (let ((minibuffer-allow-text-properties t)
3958 (read-map minibuffer-local-map))
3959 (insert (read-from-minibuffer "Message: "
3960 (string (if (featurep 'xemacs)
3962 last-command-event))
3964 (erc-send-current-line)))
3966 (defvar erc-action-history-list ()
3967 "History list for interactive action input.")
3969 (defun erc-input-action ()
3970 "Interactively input a user action and send it to IRC."
3972 (erc-set-active-buffer (current-buffer))
3973 (let ((action (read-from-minibuffer
3974 "Action: " nil nil nil 'erc-action-history-list)))
3975 (if (not (string-match "^\\s-*$" action))
3976 (erc-send-action (erc-default-target) action))))
3978 (defun erc-join-channel (channel &optional key)
3981 If `point' is at the beginning of a channel name, use that as default."
3984 (let ((chnl (if (looking-at "\\([&#+!][^ \n]+\\)") (match-string 1) ""))
3985 (table (when (erc-server-buffer-live-p)
3986 (set-buffer (process-buffer erc-server-process))
3988 (completing-read "Join channel: " table nil nil nil nil chnl))
3989 (when (or current-prefix-arg erc-prompt-for-channel-key)
3990 (read-from-minibuffer "Channel key (RET for none): " nil))))
3991 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3993 (defun erc-part-from-channel (reason)
3994 "Part from the current channel and prompt for a REASON."
3997 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3999 (read-from-minibuffer (concat "Reason for leaving " (erc-default-target)
4001 (erc-cmd-PART (concat (erc-default-target)" " reason)))
4003 (defun erc-set-topic (topic)
4004 "Prompt for a TOPIC for the current channel."
4007 (read-from-minibuffer
4008 (concat "Set topic of " (erc-default-target) ": ")
4009 (when erc-channel-topic
4010 (let ((ss (split-string erc-channel-topic "\C-o")))
4011 (cons (apply #'concat (if (cdr ss) (butlast ss) ss))
4013 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
4014 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
4016 (defun erc-set-channel-limit (&optional limit)
4017 "Set a LIMIT for the current channel. Remove limit if nil.
4018 Prompt for one if called interactively."
4019 (interactive (list (read-from-minibuffer
4020 (format "Limit for %s (RET to remove limit): "
4021 (erc-default-target)))))
4022 (let ((tgt (erc-default-target)))
4023 (erc-server-send (if (and limit (>= (length limit) 1))
4024 (format "MODE %s +l %s" tgt limit)
4025 (format "MODE %s -l" tgt)))))
4027 (defun erc-set-channel-key (&optional key)
4028 "Set a KEY for the current channel. Remove key if nil.
4029 Prompt for one if called interactively."
4030 (interactive (list (read-from-minibuffer
4031 (format "Key for %s (RET to remove key): "
4032 (erc-default-target)))))
4033 (let ((tgt (erc-default-target)))
4034 (erc-server-send (if (and key (>= (length key) 1))
4035 (format "MODE %s +k %s" tgt key)
4036 (format "MODE %s -k" tgt)))))
4038 (defun erc-quit-server (reason)
4039 "Disconnect from current server after prompting for REASON.
4040 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
4041 (interactive (list (read-from-minibuffer
4042 (format "Reason for quitting %s: "
4043 (or erc-server-announced-name
4044 erc-session-server)))))
4045 (erc-cmd-QUIT reason))
4047 ;; Movement of point
4050 "Move `point' to the beginning of the current line.
4052 This places `point' just after the prompt, or at the beginning of the line."
4055 (when (get-text-property (point) 'erc-prompt)
4056 (goto-char erc-input-marker))
4059 (defun erc-kill-input ()
4060 "Kill current input line using `erc-bol' followed by `kill-line'."
4062 (when (and (erc-bol)
4063 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
4064 ;; there's nothing to kill
4065 (if (boundp 'erc-input-ring-index)
4066 (setq erc-input-ring-index nil))
4069 (defun erc-complete-word-at-point ()
4070 (run-hook-with-args-until-success 'erc-complete-functions))
4072 (define-obsolete-function-alias 'erc-complete-word 'completion-at-point "24.1")
4074 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4076 ;; IRC SERVER INPUT HANDLING
4078 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4080 ;;;; New Input parsing
4082 ; Stolen from ZenIRC. I just wanna test this code, so here is
4085 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
4086 erc-default-server-handler)
4087 "Default for server messages which aren't covered by `erc-server-hooks'."
4088 :group 'erc-server-hooks
4091 (defun erc-default-server-handler (proc parsed)
4092 "Default server handler.
4094 Displays PROC and PARSED appropriately using `erc-display-message'."
4095 (erc-display-message
4102 (setq res (append res (list x)))))
4107 (defvar erc-server-vectors
4108 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
4109 "List of received server messages which ERC does not specifically handle.
4110 See `erc-debug-missing-hooks'.")
4111 ;(make-variable-buffer-local 'erc-server-vectors)
4113 (defun erc-debug-missing-hooks (_proc parsed)
4114 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
4115 These vectors can be helpful when adding new server message handlers to ERC.
4116 See `erc-default-server-hook'."
4117 (nconc erc-server-vectors (list parsed))
4120 (defun erc-query (target server)
4121 "Open a query buffer on TARGET, using SERVER.
4122 To change how this query window is displayed, use `let' to bind
4123 `erc-join-buffer' before calling this."
4125 (buffer-live-p server)
4126 (set-buffer server))
4127 (error "Couldn't switch to server buffer"))
4128 (let ((buf (erc-open erc-session-server
4131 erc-session-user-full-name
4136 erc-server-process)))
4138 (error "Couldn't open query window"))
4139 (erc-update-mode-line)
4142 (defcustom erc-auto-query 'window-noselect
4143 "If non-nil, create a query buffer each time you receive a private message.
4144 If the buffer doesn't already exist, it is created.
4146 This can be set to a symbol, to control how the new query window
4147 should appear. The default behavior is to display the buffer in
4148 a new window, but not to select it. See the documentation for
4149 `erc-join-buffer' for a description of the available choices."
4151 :type '(choice (const :tag "Don't create query window" nil)
4152 (const :tag "Split window and select" window)
4153 (const :tag "Split window, don't select" window-noselect)
4154 (const :tag "New frame" frame)
4155 (const :tag "Bury in new buffer" bury)
4156 (const :tag "Use current buffer" buffer)
4157 (const :tag "Use current buffer" t)))
4159 (defcustom erc-query-on-unjoined-chan-privmsg t
4160 "If non-nil create query buffer on receiving any PRIVMSG at all.
4161 This includes PRIVMSGs directed to channels. If you are using an IRC
4162 bouncer, such as dircproxy, to keep a log of channels when you are
4163 disconnected, you should set this option to t."
4167 (defcustom erc-format-query-as-channel-p t
4168 "If non-nil, format text from others in a query buffer like in a channel,
4169 otherwise format like a private message."
4173 (defcustom erc-minibuffer-notice nil
4174 "If non-nil, print ERC notices for the user in the minibuffer.
4175 Only happens when the session buffer isn't visible."
4179 (defcustom erc-minibuffer-ignored nil
4180 "If non-nil, print a message in the minibuffer if we ignored something."
4184 (defun erc-wash-quit-reason (reason nick login host)
4185 "Remove duplicate text from quit REASON.
4186 Specifically in relation to NICK (user@host) information. Returns REASON
4187 unmodified if nothing can be removed.
4188 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
4189 \"Read error: 110\". The same applies for \"Ping Timeout\"."
4190 (setq nick (regexp-quote nick)
4191 login (regexp-quote login)
4192 host (regexp-quote host))
4193 (or (when (string-match (concat "^\\(Read error\\) to "
4194 nick "\\[" host "\\]: "
4197 (concat (match-string 1 reason) ": " (match-string 2 reason)))
4198 (when (string-match (concat "^\\(Ping timeout\\) for "
4199 nick "\\[" host "\\]$")
4201 (match-string 1 reason))
4204 (defun erc-nickname-in-use (nick reason)
4205 "If NICK is unavailable, tell the user the REASON.
4207 See also `erc-display-error-notice'."
4208 (if (or (not erc-try-new-nick-p)
4209 ;; how many default-nicks are left + one more try...
4210 (eq erc-nick-change-attempt-count
4211 (if (consp erc-nick)
4212 (+ (length erc-nick) 1)
4214 (erc-display-error-notice
4216 (format "Nickname %s is %s, try another." nick reason))
4217 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
4218 (let ((newnick (nth 1 erc-default-nicks))
4219 (nicklen (cdr (assoc "NICKLEN"
4220 (erc-with-server-buffer
4221 erc-server-parameters)))))
4222 (setq erc-bad-nick t)
4223 ;; try to use a different nick
4224 (if erc-default-nicks
4225 (setq erc-default-nicks (cdr erc-default-nicks)))
4227 (setq newnick (concat (truncate-string-to-width
4229 (if (and erc-server-connected nicklen)
4230 (- (string-to-number nicklen)
4231 (length erc-nick-uniquifier))
4232 ;; rfc2812 max nick length = 9
4233 ;; we must assume this is the
4234 ;; server's setting if we haven't
4235 ;; established a connection yet
4236 (- 9 (length erc-nick-uniquifier))))
4237 erc-nick-uniquifier)))
4238 (erc-cmd-NICK newnick)
4239 (erc-display-error-notice
4241 (format "Nickname %s is %s, trying %s"
4242 nick reason newnick)))))
4246 (defgroup erc-server-hooks nil
4247 "Server event callbacks.
4248 Every server event - like numeric replies - has its own hook.
4249 Those hooks are all called using `run-hook-with-args-until-success'.
4250 They receive as first argument the process object from where the event
4252 and as second argument the event parsed as a vector."
4255 (defun erc-display-server-message (_proc parsed)
4256 "Display the message sent by the server as a notice."
4257 (erc-display-message
4258 parsed 'notice 'active (erc-response.contents parsed)))
4260 (defun erc-auto-query (proc parsed)
4261 ;; FIXME: This needs more documentation, unless it's not a user function --
4262 ;; Lawrence 2004-01-08
4263 "Put this on `erc-server-PRIVMSG-functions'."
4264 (when erc-auto-query
4265 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
4266 (target (car (erc-response.command-args parsed)))
4267 (msg (erc-response.contents parsed))
4268 (query (if (not erc-query-on-unjoined-chan-privmsg)
4270 (if (erc-current-nick-p target)
4273 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
4274 (or erc-query-on-unjoined-chan-privmsg
4275 (string= target (erc-current-nick)))
4276 (not (erc-get-buffer query proc))
4277 (not (erc-is-message-ctcp-and-not-action-p msg))
4278 (let ((erc-query-display erc-auto-query))
4279 (erc-cmd-QUERY query))
4282 (defun erc-is-message-ctcp-p (message)
4283 "Check if MESSAGE is a CTCP message or not."
4284 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
4286 (defun erc-is-message-ctcp-and-not-action-p (message)
4287 "Check if MESSAGE is a CTCP message or not."
4288 (and (erc-is-message-ctcp-p message)
4289 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
4291 (defun erc-format-privmessage (nick msg privp msgp)
4292 "Format a PRIVMSG in an insertable fashion."
4293 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
4294 (mark-e (if msgp (if privp "*" ">") "-"))
4295 (str (format "%s%s%s %s" mark-s nick mark-e msg))
4296 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
4297 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
4298 ;; add text properties to text before the nick, the nick and after the nick
4299 (erc-put-text-property 0 (length mark-s) 'font-lock-face msg-face str)
4300 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
4301 'font-lock-face nick-face str)
4302 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
4303 'font-lock-face msg-face str)
4306 (defcustom erc-format-nick-function 'erc-format-nick
4307 "Function to format a nickname for message display."
4311 (defun erc-format-nick (&optional user _channel-data)
4312 "Return the nickname of USER.
4313 See also `erc-format-nick-function'."
4314 (when user (erc-server-user-nickname user)))
4316 (defun erc-get-user-mode-prefix (user)
4318 (cond ((erc-channel-user-owner-p user)
4319 (erc-propertize "~" 'help-echo "owner"))
4320 ((erc-channel-user-admin-p user)
4321 (erc-propertize "&" 'help-echo "admin"))
4322 ((erc-channel-user-op-p user)
4323 (erc-propertize "@" 'help-echo "operator"))
4324 ((erc-channel-user-halfop-p user)
4325 (erc-propertize "%" 'help-echo "half-op"))
4326 ((erc-channel-user-voice-p user)
4327 (erc-propertize "+" 'help-echo "voice"))
4330 (defun erc-format-@nick (&optional user _channel-data)
4331 "Format the nickname of USER showing if USER has a voice, is an
4332 operator, half-op, admin or owner. Owners have \"~\", admins have
4333 \"&\", operators have \"@\" and users with voice have \"+\" as a
4334 prefix. Use CHANNEL-DATA to determine op and voice status. See
4335 also `erc-format-nick-function'."
4337 (let ((nick (erc-server-user-nickname user)))
4338 (concat (erc-propertize
4339 (erc-get-user-mode-prefix nick)
4340 'font-lock-face 'erc-nick-prefix-face)
4343 (defun erc-format-my-nick ()
4344 "Return the beginning of this user's message, correctly propertized."
4345 (if erc-show-my-nick
4348 (nick (erc-current-nick))
4349 (mode (erc-get-user-mode-prefix nick)))
4351 (erc-propertize open 'font-lock-face 'erc-default-face)
4352 (erc-propertize mode 'font-lock-face 'erc-my-nick-prefix-face)
4353 (erc-propertize nick 'font-lock-face 'erc-my-nick-face)
4354 (erc-propertize close 'font-lock-face 'erc-default-face)))
4355 (let ((prefix "> "))
4356 (erc-propertize prefix 'font-lock-face 'erc-default-face))))
4358 (defun erc-echo-notice-in-default-buffer (s parsed buffer _sender)
4359 "Echos a private notice in the default buffer, namely the
4360 target buffer specified by BUFFER, or there is no target buffer,
4361 the server buffer. This function is designed to be added to
4362 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
4363 and always returns t."
4364 (erc-display-message parsed nil buffer s)
4367 (defun erc-echo-notice-in-target-buffer (s parsed buffer _sender)
4368 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
4369 function is designed to be added to either `erc-echo-notice-hook'
4370 or `erc-echo-notice-always-hook', and returns non-nil if BUFFER
4373 (progn (erc-display-message parsed nil buffer s) t)
4376 (defun erc-echo-notice-in-minibuffer (s _parsed _buffer _sender)
4377 "Echos a private notice in the minibuffer. This function is
4378 designed to be added to either `erc-echo-notice-hook' or
4379 `erc-echo-notice-always-hook', and always returns t."
4380 (message "%s" (concat "NOTICE: " s))
4383 (defun erc-echo-notice-in-server-buffer (s parsed _buffer _sender)
4384 "Echos a private notice in the server buffer. This function is
4385 designed to be added to either `erc-echo-notice-hook' or
4386 `erc-echo-notice-always-hook', and always returns t."
4387 (erc-display-message parsed nil nil s)
4390 (defun erc-echo-notice-in-active-non-server-buffer (s parsed _buffer _sender)
4391 "Echos a private notice in the active buffer if the active
4392 buffer is not the server buffer. This function is designed to be
4393 added to either `erc-echo-notice-hook' or
4394 `erc-echo-notice-always-hook', and returns non-nil if the active
4395 buffer is not the server buffer."
4396 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
4397 (progn (erc-display-message parsed nil 'active s) t)
4400 (defun erc-echo-notice-in-active-buffer (s parsed _buffer _sender)
4401 "Echos a private notice in the active buffer. This function is
4402 designed to be added to either `erc-echo-notice-hook' or
4403 `erc-echo-notice-always-hook', and always returns t."
4404 (erc-display-message parsed nil 'active s)
4407 (defun erc-echo-notice-in-user-buffers (s parsed _buffer sender)
4408 "Echos a private notice in all of the buffers for which SENDER
4409 is a member. This function is designed to be added to either
4410 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4411 returns non-nil if there is at least one buffer for which the
4414 See also: `erc-echo-notice-in-first-user-buffer',
4415 `erc-buffer-list-with-nick'."
4416 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4418 (progn (erc-display-message parsed nil buffers s) t)
4421 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
4422 "Echos a private notice in BUFFER and in all of the buffers for
4423 which SENDER is a member. This function is designed to be added
4424 to either `erc-echo-notice-hook' or
4425 `erc-echo-notice-always-hook', and returns non-nil if there is
4426 at least one buffer for which the sender is a member or the
4429 See also: `erc-echo-notice-in-user-buffers',
4430 `erc-buffer-list-with-nick'."
4431 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4432 (unless (memq buffer buffers) (push buffer buffers))
4433 (if buffers ;FIXME: How could it be nil?
4434 (progn (erc-display-message parsed nil buffers s) t)
4437 (defun erc-echo-notice-in-first-user-buffer (s parsed _buffer sender)
4438 "Echos a private notice in one of the buffers for which SENDER
4439 is a member. This function is designed to be added to either
4440 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4441 returns non-nil if there is at least one buffer for which the
4444 See also: `erc-echo-notice-in-user-buffers',
4445 `erc-buffer-list-with-nick'."
4446 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4448 (progn (erc-display-message parsed nil (car buffers) s) t)
4451 ;;; Ban manipulation
4453 (defun erc-banlist-store (proc parsed)
4454 "Record ban entries for a channel."
4455 (pcase-let ((`(,channel ,mask ,whoset)
4456 (cdr (erc-response.command-args parsed))))
4457 ;; Determine to which buffer the message corresponds
4458 (let ((buffer (erc-get-buffer channel proc)))
4459 (with-current-buffer buffer
4460 (unless (member (cons whoset mask) erc-channel-banlist)
4461 (setq erc-channel-banlist (cons (cons whoset mask)
4462 erc-channel-banlist))))))
4465 (defun erc-banlist-finished (proc parsed)
4466 "Record that we have received the banlist."
4467 (let* ((channel (nth 1 (erc-response.command-args parsed)))
4468 (buffer (erc-get-buffer channel proc)))
4469 (with-current-buffer buffer
4470 (put 'erc-channel-banlist 'received-from-server t)))
4471 t) ; suppress the 'end of banlist' message
4473 (defun erc-banlist-update (proc parsed)
4474 "Check MODE commands for bans and update the banlist appropriately."
4475 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
4476 (let* ((tgt (car (erc-response.command-args parsed)))
4477 (mode (erc-response.contents parsed))
4478 (whoset (erc-response.sender parsed))
4479 (buffer (erc-get-buffer tgt proc)))
4481 (with-current-buffer buffer
4482 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
4483 ((string-match "^\\([+-]\\)b" mode)
4486 ((string-match "^-" mode)
4487 ;; Remove the unbanned masks from the ban list
4488 (setq erc-channel-banlist
4491 (member (upcase (cdr y))
4493 (cdr (split-string mode)))))
4494 erc-channel-banlist)))
4495 ((string-match "^+" mode)
4496 ;; Add the banned mask(s) to the ban list
4499 (unless (member (cons whoset mask) erc-channel-banlist)
4500 (setq erc-channel-banlist
4501 (cons (cons whoset mask) erc-channel-banlist))))
4502 (cdr (split-string mode))))))))))
4505 ;; used for the banlist cmds
4506 (defun erc-group-list (list n)
4507 "Group LIST into sublists of length N."
4508 (cond ((null list) nil)
4509 ((null (nthcdr n list)) (list list))
4510 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
4515 (defun erc-handle-login ()
4516 "Handle the logging in process of connection."
4517 (unless erc-logged-in
4518 (setq erc-logged-in t)
4519 (message "Logging in as `%s'... done" (erc-current-nick))
4520 ;; execute a startup script
4521 (let ((f (erc-select-startup-file)))
4523 (erc-load-script f)))))
4525 (defun erc-connection-established (proc parsed)
4526 "Run just after connection.
4528 Set user modes and run `erc-after-connect' hook."
4529 (with-current-buffer (process-buffer proc)
4530 (unless erc-server-connected ; only once per session
4531 (let ((server (or erc-server-announced-name
4532 (erc-response.sender parsed)))
4533 (nick (car (erc-response.command-args parsed)))
4534 (buffer (process-buffer proc)))
4535 (setq erc-server-connected t)
4536 (setq erc-server-reconnect-count 0)
4537 (erc-update-mode-line)
4538 (erc-set-initial-user-mode nick buffer)
4539 (erc-server-setup-periodical-ping buffer)
4540 (run-hook-with-args 'erc-after-connect server nick)))))
4542 (defun erc-set-initial-user-mode (nick buffer)
4543 "If `erc-user-mode' is non-nil for NICK, set the user modes.
4544 The server buffer is given by BUFFER."
4545 (with-current-buffer buffer
4547 (let ((mode (if (functionp erc-user-mode)
4548 (funcall erc-user-mode)
4550 (when (stringp mode)
4551 (erc-log (format "changing mode for %s to %s" nick mode))
4552 (erc-server-send (format "MODE %s %s" nick mode)))))))
4554 (defun erc-display-error-notice (parsed string)
4555 "Display STRING as an error notice.
4557 See also `erc-display-message'."
4558 (erc-display-message
4559 parsed '(notice error) 'active string))
4561 (defun erc-process-ctcp-query (proc parsed nick login host)
4562 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4563 "Process a CTCP query."
4564 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4566 (if (> (length queries) 4)
4567 (erc-display-message
4568 parsed (list 'notice 'error) proc 'ctcp-too-many)
4569 (if (= 0 (length queries))
4570 (erc-display-message
4571 parsed (list 'notice 'error) proc
4572 'ctcp-empty ?n nick)
4574 (let* ((type (upcase (car (split-string (car queries)))))
4575 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4576 (if (and hook (boundp hook))
4577 (if (string-equal type "ACTION")
4578 (run-hook-with-args-until-success
4579 hook proc parsed nick login host
4580 (car (erc-response.command-args parsed))
4583 (if (erc-current-nick-p
4584 (car (erc-response.command-args parsed)))
4585 (erc-display-message
4586 parsed 'error 'active 'ctcp-request
4587 ?n nick ?u login ?h host ?r (car queries))
4588 (erc-display-message
4589 parsed 'error 'active 'ctcp-request-to
4590 ?n nick ?u login ?h host ?r (car queries)
4591 ?t (car (erc-response.command-args parsed)))))
4592 (run-hook-with-args-until-success
4593 hook proc nick login host
4594 (car (erc-response.command-args parsed))
4596 (erc-display-message
4597 parsed (list 'notice 'error) proc
4599 (setq queries (cdr queries)))))))
4601 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4603 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4604 "Respond to a CTCP ACTION query."
4605 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4606 (let ((s (match-string 1 msg))
4607 (buf (or (erc-get-buffer to proc)
4608 (erc-get-buffer nick proc)
4609 (process-buffer proc))))
4610 (erc-display-message
4612 'ACTION ?n nick ?u login ?h host ?a s))))
4614 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4616 (defun erc-ctcp-query-CLIENTINFO (_proc nick _login _host _to msg)
4617 "Respond to a CTCP CLIENTINFO query."
4618 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4619 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4620 (unless erc-disable-ctcp-replies
4621 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4624 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4625 (defun erc-ctcp-query-ECHO (_proc nick _login _host _to msg)
4626 "Respond to a CTCP ECHO query."
4627 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4628 (let ((s (match-string 1 msg)))
4629 (unless erc-disable-ctcp-replies
4630 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4633 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4634 (defun erc-ctcp-query-FINGER (_proc nick _login _host _to _msg)
4635 "Respond to a CTCP FINGER query."
4636 (unless erc-disable-ctcp-replies
4637 (let ((s (if erc-anonymous-login
4638 (format "FINGER I'm %s." (erc-current-nick))
4639 (format "FINGER %s (%s@%s)."
4643 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4645 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4646 (erc-send-ctcp-notice nick s)))
4649 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4650 (defun erc-ctcp-query-PING (_proc nick _login _host _to msg)
4651 "Respond to a CTCP PING query."
4652 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4653 (unless erc-disable-ctcp-replies
4654 (let ((arg (match-string 1 msg)))
4655 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4658 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4659 (defun erc-ctcp-query-TIME (_proc nick _login _host _to _msg)
4660 "Respond to a CTCP TIME query."
4661 (unless erc-disable-ctcp-replies
4662 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4665 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4666 (defun erc-ctcp-query-USERINFO (_proc nick _login _host _to _msg)
4667 "Respond to a CTCP USERINFO query."
4668 (unless erc-disable-ctcp-replies
4669 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4672 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4673 (defun erc-ctcp-query-VERSION (_proc nick _login _host _to _msg)
4674 "Respond to a CTCP VERSION query."
4675 (unless erc-disable-ctcp-replies
4676 (erc-send-ctcp-notice
4678 "VERSION \C-bERC\C-b - an IRC client for Emacs %s (\C-b%s\C-b)"
4680 erc-official-location)))
4683 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4684 "Process MSG as a CTCP reply."
4685 (let* ((type (car (split-string msg)))
4686 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4688 (run-hook-with-args-until-success
4689 hook proc nick login host
4690 (car (erc-response.command-args parsed)) msg)
4691 (erc-display-message
4692 parsed 'notice 'active
4693 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4695 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4696 (defun erc-ctcp-reply-ECHO (_proc nick _login _host _to msg)
4697 "Handle a CTCP ECHO reply."
4698 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4699 (let ((message (match-string 1 msg)))
4700 (erc-display-message
4701 nil '(notice action) 'active
4702 'CTCP-ECHO ?n nick ?m message)))
4705 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4706 (defun erc-ctcp-reply-CLIENTINFO (_proc nick _login _host _to msg)
4707 "Handle a CTCP CLIENTINFO reply."
4708 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4709 (let ((message (match-string 1 msg)))
4710 (erc-display-message
4712 'CTCP-CLIENTINFO ?n nick ?m message)))
4715 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4716 (defun erc-ctcp-reply-FINGER (_proc nick _login _host _to msg)
4717 "Handle a CTCP FINGER reply."
4718 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4719 (let ((message (match-string 1 msg)))
4720 (erc-display-message
4722 'CTCP-FINGER ?n nick ?m message)))
4725 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4726 (defun erc-ctcp-reply-PING (_proc nick _login _host _to msg)
4727 "Handle a CTCP PING reply."
4728 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4730 (let ((time (match-string 1 msg)))
4732 (let ((delta (erc-time-diff (string-to-number time)
4733 (erc-current-time))))
4734 (erc-display-message
4737 ?t (erc-sec-to-time delta)))
4739 (erc-display-message
4741 'bad-ping-response ?n nick ?t time))))))
4743 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4744 (defun erc-ctcp-reply-TIME (_proc nick _login _host _to msg)
4745 "Handle a CTCP TIME reply."
4746 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4747 (let ((message (match-string 1 msg)))
4748 (erc-display-message
4750 'CTCP-TIME ?n nick ?m message)))
4753 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4754 (defun erc-ctcp-reply-VERSION (_proc nick _login _host _to msg)
4755 "Handle a CTCP VERSION reply."
4756 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4757 (let ((message (match-string 1 msg)))
4758 (erc-display-message
4760 'CTCP-VERSION ?n nick ?m message)))
4763 (defun erc-process-away (proc away-p)
4764 "Toggle the away status of the user depending on the value of AWAY-P.
4766 If nil, set the user as away.
4767 If non-nil, return from being away."
4768 (let ((sessionbuf (process-buffer proc)))
4770 (with-current-buffer sessionbuf
4771 (when erc-away-nickname
4772 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4773 erc-away-nickname away-p))
4774 (erc-cmd-NICK (if away-p
4779 (setq erc-away (current-time)))
4781 (let ((away-time erc-away))
4782 ;; away must be set to NIL BEFORE sending anything to prevent
4783 ;; an infinite recursion
4785 (with-current-buffer (erc-active-buffer)
4786 (when erc-public-away-p
4788 (erc-default-target)
4790 (format "is back (gone for %s)"
4793 (erc-emacs-time-to-erc-time away-time)
4794 (erc-current-time))))
4796 (erc-update-mode-line)))
4798 ;;;; List of channel members handling
4800 (defun erc-channel-begin-receiving-names ()
4803 Used when a channel names list is about to be received. Should
4804 be called with the current buffer set to the channel buffer.
4806 See also `erc-channel-end-receiving-names'."
4807 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4809 (defun erc-channel-end-receiving-names ()
4812 Used to fix `erc-channel-users' after a channel names list has been
4813 received. Should be called with the current buffer set to the
4816 See also `erc-channel-begin-receiving-names'."
4817 (maphash (lambda (nick _user)
4818 (if (null (gethash nick erc-channel-new-member-names))
4819 (erc-remove-channel-user nick)))
4821 (setq erc-channel-new-member-names nil))
4823 (defun erc-parse-prefix ()
4824 "Return an alist of valid prefix character types and their representations.
4825 Example: (operator) o => @, (voiced) v => +."
4826 (let ((str (or (cdr (assoc "PREFIX" (erc-with-server-buffer
4827 erc-server-parameters)))
4828 ;; provide a sane default
4831 (when (string-match "^(\\([^)]+\\))\\(.+\\)$" str)
4832 (setq types (match-string 1 str)
4833 chars (match-string 2 str))
4834 (let ((len (min (length types) (length chars)))
4838 (setq alist (cons (cons (elt types i) (elt chars i))
4843 (defun erc-channel-receive-names (names-string)
4844 "This function is for internal use only.
4846 Update `erc-channel-users' according to NAMES-STRING.
4847 NAMES-STRING is a string listing some of the names on the
4849 (let* ((prefix (erc-parse-prefix))
4850 (voice-ch (cdr (assq ?v prefix)))
4851 (op-ch (cdr (assq ?o prefix)))
4852 (hop-ch (cdr (assq ?h prefix)))
4853 (adm-ch (cdr (assq ?a prefix)))
4854 (own-ch (cdr (assq ?q prefix)))
4855 (names (delete "" (split-string names-string)))
4856 name op voice halfop admin owner)
4857 (let ((erc-channel-members-changed-hook nil))
4858 (dolist (item names)
4861 (setq name item op 'off voice 'off halfop 'off admin 'off owner 'off)
4862 (if (rassq ch prefix)
4863 (if (= (length item) 1)
4865 (setq name (substring item 1))
4867 ((pred (eq voice-ch)) voice)
4868 ((pred (eq hop-ch)) halfop)
4869 ((pred (eq op-ch)) op)
4870 ((pred (eq adm-ch)) admin)
4871 ((pred (eq own-ch)) owner)
4872 (_ (message "Unknown prefix char `%S'" ch) voice))
4875 ;; If we didn't issue the NAMES request (consider two clients
4876 ;; talking to an IRC proxy), `erc-channel-begin-receiving-names'
4877 ;; will not have been called, so we have to do it here.
4878 (unless erc-channel-new-member-names
4879 (erc-channel-begin-receiving-names))
4880 (puthash (erc-downcase name) t
4881 erc-channel-new-member-names)
4882 (erc-update-current-channel-member
4883 name name t voice halfop op admin owner)))))
4884 (run-hooks 'erc-channel-members-changed-hook)))
4887 (defcustom erc-channel-members-changed-hook nil
4888 "This hook is called every time the variable `channel-members' changes.
4889 The buffer where the change happened is current while this hook is called."
4893 (defun erc-update-user-nick (nick &optional new-nick
4894 host login full-name info)
4895 "Update the stored user information for the user with nickname NICK.
4897 See also: `erc-update-user'."
4898 (erc-update-user (erc-get-server-user nick) new-nick
4899 host login full-name info))
4901 (defun erc-update-user (user &optional new-nick
4902 host login full-name info)
4903 "Update user info for USER. USER must be an erc-server-user
4904 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4905 non-nil and not equal to the existing values for USER are used to
4906 replace the stored values in USER.
4908 If, and only if, a change is made,
4909 `erc-channel-members-changed-hook' is run for each channel for
4910 which USER is a member, and t is returned."
4914 (not (equal (erc-server-user-nickname user)
4917 (erc-change-user-nickname user new-nick))
4919 (not (equal (erc-server-user-host user) host)))
4921 (setf (erc-server-user-host user) host))
4923 (not (equal (erc-server-user-login user) login)))
4925 (setf (erc-server-user-login user) login))
4926 (when (and full-name
4927 (not (equal (erc-server-user-full-name user)
4930 (setf (erc-server-user-full-name user) full-name))
4932 (not (equal (erc-server-user-info user) info)))
4934 (setf (erc-server-user-info user) info))
4936 (dolist (buf (erc-server-user-buffers user))
4937 (if (buffer-live-p buf)
4938 (with-current-buffer buf
4939 (run-hooks 'erc-channel-members-changed-hook))))))
4942 (defun erc-update-current-channel-member
4943 (nick new-nick &optional add voice halfop op admin owner host login full-name info
4944 update-message-time)
4945 "Update the stored user information for the user with nickname NICK.
4946 `erc-update-user' is called to handle changes to nickname,
4947 HOST, LOGIN, FULL-NAME, and INFO. If VOICE HALFOP OP ADMIN or OWNER
4948 are non-nil, they must be equal to either `on' or `off', in which
4949 case the status of the user in the current channel is changed accordingly.
4950 If UPDATE-MESSAGE-TIME is non-nil, the last-message-time of the user
4951 in the current channel is set to (current-time).
4953 If ADD is non-nil, the user will be added with the specified
4954 information if it is not already present in the user or channel
4957 If, and only if, changes are made, or the user is added,
4958 `erc-channel-members-updated-hook' is run, and t is returned.
4960 See also: `erc-update-user' and `erc-update-channel-member'."
4961 (let* (changed user-changed
4962 (channel-data (erc-get-channel-user nick))
4963 (cuser (cdr channel-data))
4964 (user (if channel-data (car channel-data)
4965 (erc-get-server-user nick))))
4968 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4970 (not (eq (erc-channel-user-voice cuser) voice)))
4972 (setf (erc-channel-user-voice cuser)
4973 (cond ((eq voice 'on) t)
4974 ((eq voice 'off) nil)
4977 (not (eq (erc-channel-user-halfop cuser) halfop)))
4979 (setf (erc-channel-user-halfop cuser)
4980 (cond ((eq halfop 'on) t)
4981 ((eq halfop 'off) nil)
4984 (not (eq (erc-channel-user-op cuser) op)))
4986 (setf (erc-channel-user-op cuser)
4987 (cond ((eq op 'on) t)
4991 (not (eq (erc-channel-user-admin cuser) admin)))
4993 (setf (erc-channel-user-admin cuser)
4994 (cond ((eq admin 'on) t)
4995 ((eq admin 'off) nil)
4998 (not (eq (erc-channel-user-owner cuser) owner)))
5000 (setf (erc-channel-user-owner cuser)
5001 (cond ((eq owner 'on) t)
5002 ((eq owner 'off) nil)
5004 (when update-message-time
5005 (setf (erc-channel-user-last-message-time cuser) (current-time)))
5007 (erc-update-user user new-nick
5008 host login full-name info)))
5012 (setq user (make-erc-server-user
5015 :full-name full-name
5018 :buffers (list (current-buffer))))
5019 (erc-add-server-user nick user))
5020 (setf (erc-server-user-buffers user)
5021 (cons (current-buffer)
5022 (erc-server-user-buffers user))))
5023 (setq cuser (make-erc-channel-user
5024 :voice (cond ((eq voice 'on) t)
5025 ((eq voice 'off) nil)
5027 :halfop (cond ((eq halfop 'on) t)
5028 ((eq halfop 'off) nil)
5030 :op (cond ((eq op 'on) t)
5033 :admin (cond ((eq admin 'on) t)
5034 ((eq admin 'off) nil)
5036 :owner (cond ((eq owner 'on) t)
5037 ((eq owner 'off) nil)
5040 (if update-message-time (current-time))))
5041 (puthash (erc-downcase nick) (cons user cuser)
5044 (when (and changed (null user-changed))
5045 (run-hooks 'erc-channel-members-changed-hook))
5046 (or changed user-changed add)))
5048 (defun erc-update-channel-member (channel nick new-nick
5049 &optional add voice halfop op admin owner host login
5050 full-name info update-message-time)
5051 "Update user and channel information for the user with
5052 nickname NICK in channel CHANNEL.
5054 See also: `erc-update-current-channel-member'."
5057 (erc-update-current-channel-member nick new-nick add voice halfop op admin owner host
5058 login full-name info
5059 update-message-time)))
5061 (defun erc-remove-current-channel-member (nick)
5062 "Remove NICK from current channel membership list.
5063 Runs `erc-channel-members-changed-hook'."
5064 (let ((channel-data (erc-get-channel-user nick)))
5066 (erc-remove-channel-user nick)
5067 (run-hooks 'erc-channel-members-changed-hook))))
5069 (defun erc-remove-channel-member (channel nick)
5070 "Remove NICK from CHANNEL's membership list.
5072 See also `erc-remove-current-channel-member'."
5075 (erc-remove-current-channel-member nick)))
5077 (defun erc-update-channel-topic (channel topic &optional modify)
5078 "Find a buffer for CHANNEL and set the TOPIC for it.
5080 If optional MODIFY is `append' or `prepend', then append or prepend the
5081 TOPIC string to the current topic."
5082 (erc-with-buffer (channel)
5083 (cond ((eq modify 'append)
5084 (setq erc-channel-topic (concat erc-channel-topic topic)))
5085 ((eq modify 'prepend)
5086 (setq erc-channel-topic (concat topic erc-channel-topic)))
5087 (t (setq erc-channel-topic topic)))
5088 (erc-update-mode-line-buffer (current-buffer))))
5090 (defun erc-set-modes (tgt mode-string)
5091 "Set the modes for the TGT provided as MODE-STRING."
5092 (let* ((modes (erc-parse-modes mode-string))
5093 (add-modes (nth 0 modes))
5094 ;; list of triples: (mode-char 'on/'off argument)
5095 (arg-modes (nth 2 modes)))
5096 (cond ((erc-channel-p tgt); channel modes
5097 (let ((buf (and erc-server-process
5098 (erc-get-buffer tgt erc-server-process))))
5100 (with-current-buffer buf
5101 (setq erc-channel-modes add-modes)
5102 (setq erc-channel-user-limit nil)
5103 (setq erc-channel-key nil)
5105 (let ((mode (nth 0 (car arg-modes)))
5106 (onoff (nth 1 (car arg-modes)))
5107 (arg (nth 2 (car arg-modes))))
5108 (cond ((string-match "^[Ll]" mode)
5109 (erc-update-channel-limit tgt onoff arg))
5110 ((string-match "^[Kk]" mode)
5111 (erc-update-channel-key tgt onoff arg))
5113 (setq arg-modes (cdr arg-modes)))
5114 (erc-update-mode-line-buffer buf)))))
5115 ;; we do not keep our nick's modes yet
5116 ;;(t (setq erc-user-modes add-modes))
5120 (defun erc-sort-strings (list-of-strings)
5121 "Sort LIST-OF-STRINGS in lexicographic order.
5124 (sort (copy-sequence list-of-strings) 'string<))
5126 (defun erc-parse-modes (mode-string)
5127 "Parse MODE-STRING into a list.
5129 Returns a list of three elements:
5131 (ADD-MODES REMOVE-MODES ARG-MODES).
5133 The add-modes and remove-modes are lists of single-character strings
5134 for modes without parameters to add and remove respectively. The
5135 arg-modes is a list of triples of the form:
5137 (MODE-CHAR ON/OFF ARGUMENT)."
5138 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
5139 (let ((chars (mapcar #'char-to-string (match-string 1 mode-string)))
5140 ;; arguments in channel modes
5141 (args-str (match-string 2 mode-string))
5145 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
5147 ;; make the argument list
5148 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
5149 (setq args (cons (match-string 1 args-str) args))
5150 (setq args-str (match-string 2 args-str)))
5151 (setq args (nreverse args))
5152 ;; collect what modes changed, and match them with arguments
5154 (cond ((string= (car chars) "+") (setq add-p t))
5155 ((string= (car chars) "-") (setq add-p nil))
5156 ((string-match "^[qaovhbQAOVHB]" (car chars))
5157 (setq arg-modes (cons (list (car chars)
5159 (if args (car args) nil))
5161 (if args (setq args (cdr args))))
5162 ((string-match "^[LlKk]" (car chars))
5163 (setq arg-modes (cons (list (car chars)
5165 (if (and add-p args)
5168 (if (and add-p args) (setq args (cdr args))))
5169 (add-p (setq add-modes (cons (car chars) add-modes)))
5170 (t (setq remove-modes (cons (car chars) remove-modes))))
5171 (setq chars (cdr chars)))
5172 (setq add-modes (nreverse add-modes))
5173 (setq remove-modes (nreverse remove-modes))
5174 (setq arg-modes (nreverse arg-modes))
5175 (list add-modes remove-modes arg-modes))
5178 (defun erc-update-modes (tgt mode-string &optional _nick _host _login)
5179 "Update the mode information for TGT, provided as MODE-STRING.
5180 Optional arguments: NICK, HOST and LOGIN - the attributes of the
5181 person who changed the modes."
5182 ;; FIXME: neither of nick, host, and login are used!
5183 (let* ((modes (erc-parse-modes mode-string))
5184 (add-modes (nth 0 modes))
5185 (remove-modes (nth 1 modes))
5186 ;; list of triples: (mode-char 'on/'off argument)
5187 (arg-modes (nth 2 modes)))
5188 ;; now parse the modes changes and do the updates
5189 (cond ((erc-channel-p tgt); channel modes
5190 (let ((buf (and erc-server-process
5191 (erc-get-buffer tgt erc-server-process))))
5193 ;; FIXME! This used to have an original buffer
5194 ;; variable, but it never switched back to the original
5195 ;; buffer. Is this wanted behavior?
5197 (if (not (boundp 'erc-channel-modes))
5198 (setq erc-channel-modes nil))
5200 (setq erc-channel-modes (delete (car remove-modes)
5202 remove-modes (cdr remove-modes)))
5204 (setq erc-channel-modes (cons (car add-modes)
5206 add-modes (cdr add-modes)))
5207 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
5209 (let ((mode (nth 0 (car arg-modes)))
5210 (onoff (nth 1 (car arg-modes)))
5211 (arg (nth 2 (car arg-modes))))
5212 (cond ((string-match "^[Vv]" mode)
5213 (erc-update-channel-member tgt arg arg nil onoff))
5214 ((string-match "^[hH]" mode)
5215 (erc-update-channel-member tgt arg arg nil nil onoff))
5216 ((string-match "^[oO]" mode)
5217 (erc-update-channel-member tgt arg arg nil nil nil onoff))
5218 ((string-match "^[aA]" mode)
5219 (erc-update-channel-member tgt arg arg nil nil nil nil onoff))
5220 ((string-match "^[qQ]" mode)
5221 (erc-update-channel-member tgt arg arg nil nil nil nil nil onoff))
5222 ((string-match "^[Ll]" mode)
5223 (erc-update-channel-limit tgt onoff arg))
5224 ((string-match "^[Kk]" mode)
5225 (erc-update-channel-key tgt onoff arg))
5226 (t nil)); only ops are tracked now
5227 (setq arg-modes (cdr arg-modes))))
5228 (erc-update-mode-line buf))))
5229 ;; nick modes - ignored at this point
5232 (defun erc-update-channel-limit (channel onoff n)
5233 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
5234 "Update CHANNEL's user limit to N."
5235 (if (or (not (eq onoff 'on))
5236 (and (stringp n) (string-match "^[0-9]+$" n)))
5239 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
5240 (t (setq erc-channel-user-limit nil))))))
5242 (defun erc-update-channel-key (channel onoff key)
5243 "Update CHANNEL's key to KEY if ONOFF is `on' or to nil if it's `off'."
5246 (cond ((eq onoff 'on) (setq erc-channel-key key))
5247 (t (setq erc-channel-key nil)))))
5249 (defun erc-handle-user-status-change (type nlh &optional l)
5250 "Handle changes in any user's status.
5252 So far, only nick change is handled.
5254 Generally, the TYPE argument is a symbol describing the change type, NLH is
5255 a list containing the original nickname, login name and hostname for the user,
5256 and L is a list containing additional TYPE-specific arguments.
5258 So far the following TYPE/L pairs are supported:
5262 nickname change `nick' (NEW-NICK)"
5263 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
5271 (defun erc-highlight-notice (s)
5272 "Highlight notice message S and return it.
5273 See also variable `erc-notice-highlight-type'."
5275 ((eq erc-notice-highlight-type 'prefix)
5276 (erc-put-text-property 0 (length erc-notice-prefix)
5277 'font-lock-face 'erc-notice-face s)
5279 ((eq erc-notice-highlight-type 'all)
5280 (erc-put-text-property 0 (length s) 'font-lock-face 'erc-notice-face s)
5284 (defun erc-make-notice (message)
5285 "Notify the user of MESSAGE."
5286 (when erc-minibuffer-notice
5287 (message "%s" message))
5288 (erc-highlight-notice (concat erc-notice-prefix message)))
5290 (defun erc-highlight-error (s)
5291 "Highlight error message S and return it."
5292 (erc-put-text-property 0 (length s) 'font-lock-face 'erc-error-face s)
5295 (defun erc-put-text-property (start end property value &optional object)
5296 "Set text-property for an object (usually a string).
5297 START and END define the characters covered.
5298 PROPERTY is the text-property set, usually the symbol `face'.
5299 VALUE is the value for the text-property, usually a face symbol such as
5300 the face `bold' or `erc-pal-face'.
5301 OBJECT is a string which will be modified and returned.
5302 OBJECT is modified without being copied first.
5304 You can redefine or `defadvice' this function in order to add
5305 EmacsSpeak support."
5306 (put-text-property start end property value object))
5308 (defun erc-list (thing)
5309 "Return THING if THING is a list, or a list with THING as its element."
5314 (defun erc-parse-user (string)
5315 "Parse STRING as a user specification (nick!login@host).
5317 Return a list of the three separate tokens."
5319 ((string-match "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$" string)
5320 (list (match-string 1 string)
5321 (match-string 2 string)
5322 (match-string 3 string)))
5323 ;; Some bogus bouncers send Nick!(null), try to live with that.
5324 ((string-match "^\\([^!\n]*\\)!\\(.*\\)$" string)
5325 (list (match-string 1 string)
5327 (match-string 2 string)))
5329 (list string "" ""))))
5331 (defun erc-extract-nick (string)
5332 "Return the nick corresponding to a user specification STRING.
5334 See also `erc-parse-user'."
5335 (car (erc-parse-user string)))
5337 (defun erc-put-text-properties (start end properties
5338 &optional object value-list)
5339 "Set text-properties for OBJECT.
5341 START and END describe positions in OBJECT.
5342 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
5343 each property to the corresponding value in VALUE-LIST."
5345 (setq value-list (mapcar (lambda (_x) t)
5347 (while (and properties value-list)
5348 (erc-put-text-property
5349 start end (pop properties) (pop value-list) object)))
5351 ;;; Input area handling:
5353 (defun erc-beg-of-input-line ()
5354 "Return the value of `point' at the beginning of the input line.
5356 Specifically, return the position of `erc-insert-marker'."
5357 (or (and (boundp 'erc-insert-marker)
5358 (markerp erc-insert-marker))
5359 (error "erc-insert-marker has no value, please report a bug"))
5360 (marker-position erc-insert-marker))
5362 (defun erc-end-of-input-line ()
5363 "Return the value of `point' at the end of the input line."
5366 (defvar erc-last-input-time 0
5367 "Time of last call to `erc-send-current-line'.
5368 If that function has never been called, the value is 0.")
5370 (defcustom erc-accidental-paste-threshold-seconds 0.2
5371 "Minimum time, in seconds, before sending new lines via IRC.
5372 If the value is a number, `erc-send-current-line' signals an error
5373 if its previous invocation was fewer than this many seconds ago.
5374 This is useful so that if you accidentally enter large amounts of text
5375 into the ERC buffer, that text is not sent to the IRC server.
5377 If the value is nil, `erc-send-current-line' always considers any
5378 submitted line to be intentional."
5381 :type '(choice number (other :tag "disabled" nil)))
5383 (defun erc-send-current-line ()
5384 "Parse current line and send it to IRC."
5386 (let ((now (float-time)))
5387 (if (or (not erc-accidental-paste-threshold-seconds)
5388 (< erc-accidental-paste-threshold-seconds
5389 (- now erc-last-input-time)))
5392 (if (< (point) (erc-beg-of-input-line))
5393 (erc-error "Point is not in the input area")
5394 (let ((inhibit-read-only t)
5395 (str (erc-user-input))
5396 (old-buf (current-buffer)))
5397 (if (and (not (erc-server-buffer-live-p))
5398 (not (erc-command-no-process-p str)))
5399 (erc-error "ERC: No process running")
5400 (erc-set-active-buffer (current-buffer))
5401 ;; Kill the input and the prompt
5402 (delete-region (erc-beg-of-input-line)
5403 (erc-end-of-input-line))
5405 (erc-send-input str)
5406 ;; Fix the buffer if the command didn't kill it
5407 (when (buffer-live-p old-buf)
5408 (with-current-buffer old-buf
5411 (goto-char (point-max))
5412 (when (processp erc-server-process)
5413 (set-marker (process-mark erc-server-process) (point)))
5414 (set-marker erc-insert-marker (point))
5415 (let ((buffer-modified (buffer-modified-p)))
5416 (erc-display-prompt)
5417 (set-buffer-modified-p buffer-modified))))))
5419 ;; Only when last hook has been run...
5420 (run-hook-with-args 'erc-send-completed-hook str))))
5421 (setq erc-last-input-time now))
5422 (switch-to-buffer "*ERC Accidental Paste Overflow*")
5423 (lwarn 'erc :warning
5424 "You seem to have accidentally pasted some text!"))))
5426 (defun erc-user-input ()
5427 "Return the input of the user in the current buffer."
5428 (buffer-substring-no-properties
5430 (erc-end-of-input-line)))
5432 (defvar erc-command-regexp "^/\\([A-Za-z']+\\)\\(\\s-+.*\\|\\s-*\\)$"
5433 "Regular expression used for matching commands in ERC.")
5435 (defun erc-send-input (input)
5436 "Treat INPUT as typed in by the user. It is assumed that the input
5437 and the prompt is already deleted.
5438 This returns non-nil only if we actually send anything."
5439 ;; Handle different kinds of inputs
5441 ;; Ignore empty input
5442 ((if erc-send-whitespace-lines
5444 (string-match "\\`[ \t\r\f\n]*\\'" input))
5445 (when erc-warn-about-blank-lines
5446 (message "Blank line - ignoring...")
5450 (defvar str) ;; FIXME: Make it obey the "erc-" prefix convention.
5452 (erc-insert-this t))
5453 (setq erc-send-this t)
5454 (run-hook-with-args 'erc-send-pre-hook input)
5456 (if (or (string-match "\n" str)
5457 (not (string-match erc-command-regexp str)))
5462 ;; Insert what has to be inserted for this.
5463 (erc-display-msg line)
5464 (erc-process-input-line (concat line "\n")
5465 (null erc-flood-protect) t))
5466 (or (and erc-flood-protect (erc-split-line line))
5468 (split-string str "\n"))
5469 (erc-process-input-line (concat str "\n") t nil))
5472 (defun erc-display-command (line)
5473 (when erc-insert-this
5474 (let ((insert-position (point)))
5475 (unless erc-hide-prompt
5476 (erc-display-prompt nil nil (erc-command-indicator)
5477 (and (erc-command-indicator)
5478 'erc-command-indicator-face)))
5479 (let ((beg (point)))
5481 (erc-put-text-property beg (point)
5482 'font-lock-face 'erc-command-indicator-face)
5484 (when (processp erc-server-process)
5485 (set-marker (process-mark erc-server-process) (point)))
5486 (set-marker erc-insert-marker (point))
5489 (narrow-to-region insert-position (point))
5490 (run-hooks 'erc-send-modify-hook)
5491 (run-hooks 'erc-send-post-hook))))))
5493 (defun erc-display-msg (line)
5494 "Display LINE as a message of the user to the current target at the
5496 (when erc-insert-this
5497 (let ((insert-position (point)))
5498 (insert (erc-format-my-nick))
5499 (let ((beg (point)))
5501 (erc-put-text-property beg (point)
5502 'font-lock-face 'erc-input-face))
5504 (when (processp erc-server-process)
5505 (set-marker (process-mark erc-server-process) (point)))
5506 (set-marker erc-insert-marker (point))
5509 (narrow-to-region insert-position (point))
5510 (run-hooks 'erc-send-modify-hook)
5511 (run-hooks 'erc-send-post-hook))))))
5513 (defun erc-command-symbol (command)
5514 "Return the ERC command symbol for COMMAND if it exists and is bound."
5515 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
5516 (when (fboundp cmd) cmd)))
5518 (defun erc-extract-command-from-line (line)
5519 "Extract command and args from the input LINE.
5520 If no command was given, return nil. If command matches, return a
5521 list of the form: (command args) where both elements are strings."
5522 (when (string-match erc-command-regexp line)
5523 (let* ((cmd (erc-command-symbol (match-string 1 line)))
5524 ;; note: return is nil, we apply this simply for side effects
5525 (_canon-defun (while (and cmd (symbolp (symbol-function cmd)))
5526 (setq cmd (symbol-function cmd))))
5527 (cmd-fun (or cmd #'erc-cmd-default))
5529 (if (get cmd-fun 'do-not-parse-args)
5530 (format "%s" (match-string 2 line))
5531 (delete "" (split-string (erc-trim-string
5532 (match-string 2 line)) " ")))
5534 (list cmd-fun arg))))
5536 (defun erc-split-multiline-safe (string)
5537 "Split STRING, containing multiple lines and return them in a list.
5538 Do it only for STRING as the complete input, do not carry unfinished
5539 strings over to the next call."
5544 (let ((i (string-match "\r?\n" string i0))
5545 (s (substring string i0)))
5546 (cond (i (setq l (cons (substring string i0 i) l))
5547 (setq i0 (match-end 0)))
5549 (setq l (cons s l))(setq doit nil))
5550 (t (setq doit nil)))))
5555 (defun erc-set-current-nick (nick)
5556 "Set the current nickname to NICK."
5557 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5560 (setq erc-server-current-nick nick)))
5562 (defun erc-current-nick ()
5563 "Return the current nickname."
5564 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5567 erc-server-current-nick))
5569 (defun erc-current-nick-p (nick)
5570 "Return non-nil if NICK is the current nickname."
5571 (erc-nick-equal-p nick (erc-current-nick)))
5573 (defun erc-nick-equal-p (nick1 nick2)
5574 "Return non-nil if NICK1 and NICK2 are the same.
5576 This matches strings according to the IRC protocol's case convention.
5578 See also `erc-downcase'."
5579 (string= (erc-downcase nick1)
5580 (erc-downcase nick2)))
5582 ;; default target handling
5584 (defun erc-default-target ()
5585 "Return the current default target (as a character string) or nil if none."
5586 (let ((tgt (car erc-default-recipients)))
5589 ((listp tgt) (cdr tgt))
5592 (defun erc-add-default-channel (channel)
5593 "Add CHANNEL to the default channel list."
5594 (let ((chl (downcase channel)))
5595 (setq erc-default-recipients
5596 (cons chl erc-default-recipients))))
5598 (defun erc-delete-default-channel (channel &optional buffer)
5599 "Delete CHANNEL from the default channel list."
5600 (with-current-buffer (if (and buffer
5604 (setq erc-default-recipients (delete (downcase channel)
5605 erc-default-recipients))))
5607 (defun erc-add-query (nickname)
5608 "Add QUERY'd NICKNAME to the default channel list.
5610 The previous default target of QUERY type gets removed."
5611 (let ((d1 (car erc-default-recipients))
5612 (d2 (cdr erc-default-recipients))
5613 (qt (cons 'QUERY (downcase nickname))))
5614 (setq erc-default-recipients (cons qt (if (and (listp d1)
5615 (eq (car d1) 'QUERY))
5617 erc-default-recipients)))))
5619 (defun erc-delete-query ()
5620 "Delete the topmost target if it is a QUERY."
5622 (let ((d1 (car erc-default-recipients))
5623 (d2 (cdr erc-default-recipients)))
5625 (eq (car d1) 'QUERY))
5626 (setq erc-default-recipients d2)
5627 (error "Current target is not a QUERY"))))
5629 (defun erc-ignored-user-p (spec)
5630 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5632 Takes a full SPEC of a user in the form \"nick!login@host\", and
5633 matches against all the regexp's in `erc-ignore-list'. If any
5634 match, returns that regexp."
5636 (dolist (ignored (erc-with-server-buffer erc-ignore-list))
5637 (if (string-match ignored spec)
5638 (throw 'found ignored)))))
5640 (defun erc-ignored-reply-p (msg tgt proc)
5641 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5642 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5644 Takes a message MSG to a channel and returns non-nil if the addressed
5645 user matches any regexp in `erc-ignore-reply-list'."
5646 (let ((target-nick (erc-message-target msg)))
5647 (if (not target-nick)
5649 (erc-with-buffer (tgt proc)
5650 (let ((user (erc-get-server-user target-nick)))
5652 (erc-list-match erc-ignore-reply-list
5653 (erc-user-spec user))))))))
5655 (defun erc-message-target (msg)
5656 "Return the addressed target in MSG.
5658 The addressed target is the string before the first colon in MSG."
5659 (if (string-match "^\\([^: \n]*\\):" msg)
5660 (match-string 1 msg)
5663 (defun erc-user-spec (user)
5664 "Create a nick!user@host spec from a user struct."
5665 (let ((nick (erc-server-user-nickname user))
5666 (host (erc-server-user-host user))
5667 (login (erc-server-user-login user)))
5668 (concat (or nick "")
5674 (defun erc-list-match (lst str)
5675 "Return non-nil if any regexp in LST matches STR."
5676 (memq nil (mapcar (lambda (regexp)
5677 (not (string-match regexp str)))
5682 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5683 "Toggle automatic CTCP replies (like VERSION and PING).
5685 If ARG is positive, turns CTCP replies on.
5687 If ARG is non-nil and not positive, turns CTCP replies off."
5689 (cond ((and (numberp arg) (> arg 0))
5690 (setq erc-disable-ctcp-replies t))
5691 (arg (setq erc-disable-ctcp-replies nil))
5692 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5693 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5695 (defun erc-toggle-flood-control (&optional arg)
5696 "Toggle use of flood control on sent messages.
5698 If ARG is positive, use flood control.
5699 If ARG is non-nil and not positive, do not use flood control.
5701 See `erc-server-flood-margin' for an explanation of the available
5702 flood control parameters."
5704 (cond ((and (numberp arg) (> arg 0))
5705 (setq erc-flood-protect t))
5706 (arg (setq erc-flood-protect nil))
5707 (t (setq erc-flood-protect (not erc-flood-protect))))
5708 (message "ERC flood control is %s"
5709 (cond (erc-flood-protect "ON")
5712 ;; Some useful channel and nick commands for fast key bindings
5714 (defun erc-invite-only-mode (&optional arg)
5715 "Turn on the invite only mode (+i) for the current channel.
5717 If ARG is non-nil, turn this mode off (-i).
5719 This command is sent even if excess flood is detected."
5721 (erc-set-active-buffer (current-buffer))
5722 (let ((tgt (erc-default-target)))
5723 (if (or (not tgt) (not (erc-channel-p tgt)))
5724 (erc-display-message nil 'error (current-buffer) 'no-target)
5725 (erc-load-irc-script-lines
5726 (list (concat "/mode " tgt (if arg " -i" " +i")))
5729 (defun erc-get-channel-mode-from-keypress (key)
5730 "Read a key sequence and call the corresponding channel mode function.
5731 After doing C-c C-o, type in a channel mode letter.
5734 RET lets you type more than one mode at a time.
5735 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5736 If \"k\" is pressed, `erc-set-channel-key' gets called.
5737 Anything else will be sent to `erc-toggle-channel-mode'."
5738 (interactive "kChannel mode (RET to set more than one): ")
5739 (when (featurep 'xemacs)
5740 (setq key (char-to-string (event-to-character (aref key 0)))))
5741 (cond ((equal key "\C-g")
5744 (erc-insert-mode-command))
5746 (call-interactively 'erc-set-channel-limit))
5748 (call-interactively 'erc-set-channel-key))
5749 (t (erc-toggle-channel-mode key))))
5751 (defun erc-toggle-channel-mode (mode &optional channel)
5752 "Toggle channel MODE.
5754 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5755 `erc-default-target'."
5757 (erc-set-active-buffer (current-buffer))
5758 (let ((tgt (or channel (erc-default-target))))
5759 (if (or (null tgt) (null (erc-channel-p tgt)))
5760 (erc-display-message nil 'error 'active 'no-target)
5761 (let* ((active (member mode erc-channel-modes))
5762 (newstate (if active "OFF" "ON")))
5763 (erc-log (format "%s: Toggle mode %s %s" tgt mode newstate))
5764 (message "Toggle channel mode %s %s" mode newstate)
5765 (erc-server-send (format "MODE %s %s%s"
5766 tgt (if active "-" "+") mode))))))
5768 (defun erc-insert-mode-command ()
5769 "Insert the line \"/mode <current target> \" at `point'."
5771 (let ((tgt (erc-default-target)))
5772 (if tgt (insert (concat "/mode " tgt " "))
5773 (erc-display-message nil 'error (current-buffer) 'no-target))))
5775 (defun erc-channel-names ()
5776 "Run \"/names #channel\" in the current channel."
5778 (erc-set-active-buffer (current-buffer))
5779 (let ((tgt (erc-default-target)))
5780 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5781 (erc-display-message nil 'error (current-buffer) 'no-target))))
5783 (defun erc-remove-text-properties-region (start end &optional object)
5784 "Clears the region (START,END) in OBJECT from all colors, etc."
5787 (let ((inhibit-read-only t))
5788 (set-text-properties start end nil object))))
5789 (put 'erc-remove-text-properties-region 'disabled t)
5791 ;; script execution and startup
5793 (defun erc-find-file (file &optional path)
5794 "Search for a FILE in the filesystem.
5795 First the `default-directory' is searched for FILE, then any directories
5796 specified in the list PATH.
5798 If FILE is found, return the path to it."
5799 (let ((filepath file))
5800 (if (file-readable-p filepath) filepath
5802 (progn (setq filepath (expand-file-name file (car path)))
5803 (not (file-readable-p filepath))))
5804 (setq path (cdr path)))
5805 (if path filepath nil))))
5807 (defun erc-select-startup-file ()
5808 "Select an ERC startup file.
5809 See also `erc-startup-file-list'."
5811 (dolist (f erc-startup-file-list)
5812 (setq f (convert-standard-filename f))
5813 (when (file-readable-p f)
5814 (throw 'found f)))))
5816 (defun erc-find-script-file (file)
5817 "Search for FILE in `default-directory', and any in `erc-script-path'."
5818 (erc-find-file file erc-script-path))
5820 (defun erc-load-script (file)
5821 "Load a script from FILE.
5823 FILE must be the full name, it is not searched in the
5824 `erc-script-path'. If the filename ends with `.el', then load it
5825 as an Emacs Lisp program. Otherwise, treat it as a regular IRC
5827 (erc-log (concat "erc-load-script: " file))
5829 ((string-match "\\.el\\'" file)
5832 (erc-load-irc-script file))))
5834 (defun erc-process-script-line (line &optional args)
5835 "Process an IRC script LINE.
5837 Does script-specific substitutions (script arguments, current nick,
5838 server, etc.) in LINE and returns it.
5840 Substitutions are: %C and %c = current target (channel or nick),
5841 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5842 where x is any other character;
5843 $* = the entire argument string, $1 = the first argument, $2 = the second,
5845 (if (not args) (setq args ""))
5846 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5847 (percent-regexp "\\(%.\\)")
5848 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5849 (tgt (erc-default-target))
5850 (server (and (boundp 'erc-session-server) erc-session-server))
5851 (nick (erc-current-nick))
5856 (if (not tgt) (setq tgt ""))
5857 (if (not server) (setq server ""))
5858 (if (not nick) (setq nick ""))
5859 ;; First, compute the argument list
5861 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5862 (setq arg-list (cons (match-string 1 tmp) arg-list))
5863 (setq tmp (match-string 2 tmp)))
5864 (setq arg-list (nreverse arg-list))
5865 (setq arg-num (length arg-list))
5866 ;; now do the substitution
5867 (setq tmp (string-match esc-regexp line))
5869 ;;(message "beginning of while: tmp=%S" tmp)
5870 (let* ((hd (substring line 0 tmp))
5873 (tail (substring line tmp)))
5874 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5875 (setq esc (match-string 1 tail))
5876 (setq tail (substring tail (match-end 1))))
5877 ((string-match (concat "^" percent-regexp) tail)
5878 (setq esc (match-string 1 tail))
5879 (setq tail (substring tail (match-end 1)))))
5880 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5881 (setq res (concat res hd))
5883 (cond ((string= esc "") "")
5884 ((string-match "^\\$\\*$" esc) args)
5885 ((string-match "^\\$\\([0-9]+\\)$" esc)
5886 (let ((n (string-to-number (match-string 1 esc))))
5887 (message "n = %S, integerp(n)=%S" n (integerp n))
5888 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5889 ((string-match "^%[Cc]$" esc) tgt)
5890 ((string-match "^%[Ss]$" esc) server)
5891 ((string-match "^%[Nn]$" esc) nick)
5892 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5893 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5894 (message "BUG IN ERC: esc=%S" esc)
5897 (setq tmp (string-match esc-regexp line))
5898 (setq res (concat res subst))
5899 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5901 (setq res (concat res line))
5904 (defun erc-load-irc-script (file &optional force)
5905 "Load an IRC script from FILE."
5906 (erc-log (concat "erc-load-script: " file))
5907 (let ((str (with-temp-buffer
5908 (insert-file-contents file)
5910 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5912 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5913 "Load IRC script LINES (a list of strings).
5915 If optional NOEXPAND is non-nil, do not expand script-specific
5916 sequences, process the lines verbatim. Use this for multiline
5918 (let* ((cb (current-buffer))
5920 (sp (or (erc-command-indicator) (erc-prompt)))
5921 (args (and (boundp 'erc-script-args) erc-script-args)))
5922 (if (and args (string-match "^ " args))
5923 (setq args (substring args 1)))
5924 ;; prepare the prompt string for echo
5925 (erc-put-text-property 0 (length sp)
5926 'font-lock-face 'erc-command-indicator-face sp)
5928 (setq s (car lines))
5929 (erc-log (concat "erc-load-script: CMD: " s))
5930 (unless (string-match "^\\s-*$" s)
5931 (let ((line (if noexpand s (erc-process-script-line s args))))
5932 (if (and (erc-process-input-line line force)
5935 (erc-put-text-property 0 (length line)
5936 'font-lock-face 'erc-input-face line)
5937 (erc-display-line (concat sp line) cb)))))
5938 (setq lines (cdr lines)))))
5943 "Perform user authentication at the IRC server."
5944 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5947 (or erc-system-name (system-name))
5949 erc-session-user-full-name))
5950 (if erc-session-password
5951 (erc-server-send (format "PASS %s" erc-session-password))
5952 (message "Logging in without password"))
5953 (erc-server-send (format "NICK %s" (erc-current-nick)))
5955 (format "USER %s %s %s :%s"
5957 (if erc-anonymous-login erc-email-userid (user-login-name))
5959 erc-session-user-full-name))
5960 (erc-update-mode-line))
5962 ;; connection properties' heuristics
5964 (defun erc-determine-parameters (&optional server port nick name)
5965 "Determine the connection and authentication parameters.
5966 Sets the buffer local variables:
5968 - `erc-session-connector'
5969 - `erc-session-server'
5970 - `erc-session-port'
5971 - `erc-session-full-name'
5972 - `erc-server-current-nick'"
5973 (setq erc-session-connector erc-server-connect-function
5974 erc-session-server (erc-compute-server server)
5975 erc-session-port (or port erc-default-port)
5976 erc-session-user-full-name (erc-compute-full-name name))
5977 (erc-set-current-nick (erc-compute-nick nick)))
5979 (defun erc-compute-server (&optional server)
5980 "Return an IRC server name.
5982 This tries a number of increasingly more default methods until a
5983 non-nil value is found.
5985 - SERVER (the argument passed to this function)
5986 - The `erc-server' option
5987 - The value of the IRCSERVER environment variable
5988 - The `erc-default-server' variable"
5991 (getenv "IRCSERVER")
5992 erc-default-server))
5994 (defun erc-compute-nick (&optional nick)
5995 "Return user's IRC nick.
5997 This tries a number of increasingly more default methods until a
5998 non-nil value is found.
6000 - NICK (the argument passed to this function)
6001 - The `erc-nick' option
6002 - The value of the IRCNICK environment variable
6003 - The result from the `user-login-name' function"
6005 (if (consp erc-nick) (car erc-nick) erc-nick)
6010 (defun erc-compute-full-name (&optional full-name)
6011 "Return user's full name.
6013 This tries a number of increasingly more default methods until a
6014 non-nil value is found.
6016 - FULL-NAME (the argument passed to this function)
6017 - The `erc-user-full-name' option
6018 - The value of the IRCNAME environment variable
6019 - The result from the `user-full-name' function"
6023 (if erc-anonymous-login "unknown" nil)
6026 (defun erc-compute-port (&optional port)
6027 "Return a port for an IRC server.
6029 This tries a number of increasingly more default methods until a
6030 non-nil value is found.
6032 - PORT (the argument passed to this function)
6033 - The `erc-port' option
6034 - The `erc-default-port' variable"
6035 (or port erc-port erc-default-port))
6039 (defun erc-string-to-emacs-time (string)
6040 "Convert the long number represented by STRING into an Emacs format.
6041 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
6042 (let* ((n (string-to-number (concat string ".0"))))
6043 (list (truncate (/ n 65536))
6044 (truncate (mod n 65536)))))
6046 (defalias 'erc-emacs-time-to-erc-time 'float-time)
6047 (defalias 'erc-current-time 'float-time)
6049 (defun erc-time-diff (t1 t2)
6050 "Return the time difference in seconds between T1 and T2."
6053 (defun erc-time-gt (t1 t2)
6054 "Check whether T1 > T2."
6057 (defun erc-sec-to-time (ns)
6058 "Convert NS to a time string HH:MM.SS."
6059 (setq ns (truncate ns))
6060 (format "%02d:%02d.%02d"
6065 (defun erc-seconds-to-string (seconds)
6066 "Convert a number of SECONDS into an English phrase."
6067 (let (days hours minutes format-args output)
6068 (setq days (/ seconds 86400)
6069 seconds (% seconds 86400)
6070 hours (/ seconds 3600)
6071 seconds (% seconds 3600)
6072 minutes (/ seconds 60)
6073 seconds (% seconds 60)
6074 format-args (if (> days 0)
6075 `("%d days, %d hours, %d minutes, %d seconds"
6076 ,days ,hours ,minutes ,seconds)
6078 `("%d hours, %d minutes, %d seconds"
6079 ,hours ,minutes ,seconds)
6081 `("%d minutes, %d seconds" ,minutes ,seconds)
6082 `("%d seconds" ,seconds))))
6083 output (apply #'format format-args))
6084 ;; Change all "1 units" to "1 unit".
6085 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
6086 (setq output (erc-replace-match-subexpression-in-string
6087 "" output (match-string 2 output) 2 (match-beginning 2))))
6093 (defconst erc-clientinfo-alist
6094 '(("ACTION" . "is used to inform about one's current activity")
6095 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
6096 ("ECHO" . "echoes its arguments back")
6097 ("FINGER" . "shows user's name, location, and idle time")
6098 ("PING" . "measures delay between peers")
6099 ("TIME" . "shows client-side time")
6100 ("USERINFO" . "shows information provided by a user")
6101 ("VERSION" . "shows client type and version"))
6102 "Alist of CTCP CLIENTINFO for ERC commands.")
6104 (defun erc-client-info (s)
6105 "Return CTCP CLIENTINFO on command S.
6106 If S is nil or an empty string then return general CLIENTINFO."
6107 (if (or (not s) (string= s ""))
6111 (concat (car e) " "))
6112 erc-clientinfo-alist))
6113 ": use CLIENTINFO <COMMAND> to get more specific information")
6114 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
6116 (concat s " " (cdr h))
6117 (concat s ": unknown command")))))
6121 (defun erc-directory-writable-p (dir)
6122 "Determine whether DIR is a writable directory.
6123 If it doesn't exist, create it."
6124 (unless (file-attributes dir) (make-directory dir))
6125 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
6127 (defun erc-kill-query-buffers (process)
6128 "Kill all buffers of PROCESS.
6129 Does nothing if PROCESS is not a process object."
6130 ;; here, we only want to match the channel buffers, to avoid
6131 ;; "selecting killed buffers" b0rkage.
6132 (when (processp process)
6133 (erc-with-all-buffers-of-server process
6135 (not (erc-server-buffer-p)))
6136 (kill-buffer (current-buffer)))))
6138 (defun erc-nick-at-point ()
6139 "Give information about the nickname at `point'.
6141 If called interactively, give a human readable message in the
6142 minibuffer. If called programmatically, return the corresponding
6143 entry of `channel-members'."
6145 (require 'thingatpt)
6146 (let* ((word (word-at-point))
6147 (channel-data (erc-get-channel-user word))
6148 (cuser (cdr channel-data))
6149 (user (if channel-data
6151 (erc-get-server-user word)))
6152 host login full-name nick voice halfop op admin owner)
6154 (setq nick (erc-server-user-nickname user)
6155 host (erc-server-user-host user)
6156 login (erc-server-user-login user)
6157 full-name (erc-server-user-full-name user))
6159 (setq voice (erc-channel-user-voice cuser)
6160 halfop (erc-channel-user-halfop cuser)
6161 op (erc-channel-user-op cuser)
6162 admin (erc-channel-user-admin cuser)
6163 owner (erc-channel-user-owner cuser))))
6164 (if (called-interactively-p 'interactive)
6165 (message "%s is %s@%s%s%s"
6167 (if full-name (format " (%s)" full-name) "")
6168 (if (or voice halfop op admin owner)
6169 (format " and is +%s%s%s%s%s on %s"
6175 (erc-default-target))
6179 (defun erc-away-time ()
6180 "Return non-nil if the current ERC process is set away.
6182 In particular, the time that we were set away is returned.
6183 See `current-time' for details on the time format."
6184 (erc-with-server-buffer erc-away))
6186 ;; Mode line handling
6188 (defcustom erc-mode-line-format "%S %a"
6189 "A string to be formatted and shown in the mode-line in `erc-mode'.
6191 The string is formatted using `format-spec' and the result is set as the value
6192 of `mode-line-buffer-identification'.
6194 The following characters are replaced:
6195 %a: String indicating away status or \"\" if you are not away
6196 %l: The estimated lag time to the server
6197 %m: The modes of the channel
6198 %n: The current nick name
6199 %N: The name of the network
6200 %o: The topic of the channel
6201 %p: The session port
6202 %t: The name of the target (channel, nickname, or servername:port)
6203 %s: In the server-buffer, this gets filled with the value of
6204 `erc-server-announced-name', in a channel, the value of
6205 (erc-default-target) also get concatenated.
6206 %S: In the server-buffer, this gets filled with the value of
6207 `erc-network', in a channel, the value of (erc-default-target)
6208 also get concatenated."
6209 :group 'erc-mode-line-and-header
6212 (defcustom erc-header-line-format "%n on %t (%m,%l) %o"
6213 "A string to be formatted and shown in the header-line in `erc-mode'.
6214 Only used starting in Emacs 21.
6216 Set this to nil if you do not want the header line to be
6219 See `erc-mode-line-format' for which characters are can be used."
6220 :group 'erc-mode-line-and-header
6221 :set (lambda (sym val)
6223 (when (fboundp 'erc-update-mode-line)
6224 (erc-update-mode-line nil)))
6225 :type '(choice (const :tag "Disabled" nil)
6228 (defcustom erc-header-line-uses-tabbar-p nil
6229 "Use tabbar mode instead of the header line to display the header."
6230 :group 'erc-mode-line-and-header
6233 (defcustom erc-header-line-uses-help-echo-p t
6234 "Show the contents of the header line in the echo area or as a tooltip
6235 when you move point into the header line."
6236 :group 'erc-mode-line-and-header
6239 (defcustom erc-header-line-face-method nil
6240 "Determine what method to use when colorizing the header line text.
6242 If nil, don't colorize the header text.
6243 If given a function, call it and use the resulting face name.
6244 Otherwise, use the `erc-header-line' face."
6245 :group 'erc-mode-line-and-header
6246 :type '(choice (const :tag "Don't colorize" nil)
6247 (const :tag "Use the erc-header-line face" t)
6248 (function :tag "Call a function")))
6250 (defcustom erc-show-channel-key-p t
6251 "Show the channel key in the header line."
6252 :group 'erc-paranoia
6255 (defcustom erc-mode-line-away-status-format
6256 "(AWAY since %a %b %d %H:%M) "
6257 "When you're away on a server, this is shown in the mode line.
6258 This should be a string with substitution variables recognized by
6259 `format-time-string'."
6260 :group 'erc-mode-line-and-header
6263 (defun erc-shorten-server-name (server-name)
6264 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
6265 (if (stringp server-name)
6267 (insert server-name)
6268 (let ((alist erc-common-server-suffixes))
6270 (goto-char (point-min))
6271 (if (re-search-forward (caar alist) nil t)
6272 (replace-match (cdar alist)))
6273 (setq alist (cdr alist))))
6276 (defun erc-format-target ()
6277 "Return the name of the target (channel or nickname or servername:port)."
6278 (let ((target (erc-default-target)))
6280 (concat (erc-shorten-server-name
6281 (or erc-server-announced-name
6282 erc-session-server))
6283 ":" (erc-port-to-string erc-session-port)))))
6285 (defun erc-format-target-and/or-server ()
6286 "Return the server name or the current target and server name combined."
6287 (let ((server-name (erc-shorten-server-name
6288 (or erc-server-announced-name
6289 erc-session-server))))
6290 (cond ((erc-default-target)
6291 (concat (erc-string-no-properties (erc-default-target))
6293 (server-name server-name)
6294 (t (buffer-name (current-buffer))))))
6296 (defun erc-format-network ()
6297 "Return the name of the network we are currently on."
6298 (let ((network (and (fboundp 'erc-network-name) (erc-network-name))))
6299 (if (and network (symbolp network))
6300 (symbol-name network)
6303 (defun erc-format-target-and/or-network ()
6304 "Return the network or the current target and network combined.
6305 If the name of the network is not available, then use the
6306 shortened server name instead."
6307 (let ((network-name (or (and (fboundp 'erc-network-name) (erc-network-name))
6308 (erc-shorten-server-name
6309 (or erc-server-announced-name
6310 erc-session-server)))))
6311 (when (and network-name (symbolp network-name))
6312 (setq network-name (symbol-name network-name)))
6313 (cond ((erc-default-target)
6314 (concat (erc-string-no-properties (erc-default-target))
6317 (not (get-buffer network-name)))
6318 (when erc-rename-buffers
6319 (rename-buffer network-name))
6321 (t (buffer-name (current-buffer))))))
6323 (defun erc-format-away-status ()
6324 "Return a formatted `erc-mode-line-away-status-format'
6325 if `erc-away' is non-nil."
6326 (let ((a (erc-away-time)))
6328 (format-time-string erc-mode-line-away-status-format a)
6331 (defun erc-format-channel-modes ()
6332 "Return the current channel's modes."
6333 (concat (apply #'concat
6334 "+" erc-channel-modes)
6335 (cond ((and erc-channel-user-limit erc-channel-key)
6336 (if erc-show-channel-key-p
6337 (format "lk %.0f %s" erc-channel-user-limit
6339 (format "kl %.0f" erc-channel-user-limit)))
6340 (erc-channel-user-limit
6341 ;; Emacs has no bignums
6342 (format "l %.0f" erc-channel-user-limit))
6344 (if erc-show-channel-key-p
6345 (format "k %s" erc-channel-key)
6349 (defun erc-format-lag-time ()
6350 "Return the estimated lag time to server, `erc-server-lag'."
6351 (let ((lag (erc-with-server-buffer erc-server-lag)))
6352 (cond (lag (format "lag:%.0f" lag))
6355 ;; erc-goodies is required at end of this file.
6356 (declare-function erc-controls-strip "erc-goodies" (str))
6358 (defvar tabbar--local-hlf)
6360 (defun erc-update-mode-line-buffer (buffer)
6361 "Update the mode line in a single ERC buffer BUFFER."
6362 (with-current-buffer buffer
6363 (let ((spec (format-spec-make
6364 ?a (erc-format-away-status)
6365 ?l (erc-format-lag-time)
6366 ?m (erc-format-channel-modes)
6367 ?n (or (erc-current-nick) "")
6368 ?N (erc-format-network)
6369 ?o (or (erc-controls-strip erc-channel-topic) "")
6370 ?p (erc-port-to-string erc-session-port)
6371 ?s (erc-format-target-and/or-server)
6372 ?S (erc-format-target-and/or-network)
6373 ?t (erc-format-target)))
6374 (process-status (cond ((and (erc-server-process-alive)
6375 (not erc-server-connected))
6377 ((erc-server-process-alive)
6381 (face (cond ((eq erc-header-line-face-method nil)
6383 ((functionp erc-header-line-face-method)
6384 (funcall erc-header-line-face-method))
6386 'erc-header-line))))
6387 (cond ((featurep 'xemacs)
6388 (setq modeline-buffer-identification
6389 (list (format-spec erc-mode-line-format spec)))
6390 (setq modeline-process (list process-status)))
6392 (setq mode-line-buffer-identification
6393 (list (format-spec erc-mode-line-format spec)))
6394 (setq mode-line-process (list process-status))))
6395 (when (boundp 'header-line-format)
6396 (let ((header (if erc-header-line-format
6397 (format-spec erc-header-line-format spec)
6399 (cond (erc-header-line-uses-tabbar-p
6400 (set (make-local-variable 'tabbar--local-hlf)
6402 (kill-local-variable 'header-line-format))
6404 (setq header-line-format nil))
6405 (erc-header-line-uses-help-echo-p
6406 (let ((help-echo (with-temp-buffer
6408 (fill-region (point-min) (point-max))
6410 (setq header-line-format
6411 (erc-replace-regexp-in-string
6415 (erc-propertize header 'help-echo help-echo
6417 (erc-propertize header 'help-echo help-echo))))))
6418 (t (setq header-line-format
6420 (erc-propertize header 'face face)
6422 (if (featurep 'xemacs)
6424 (force-mode-line-update))))
6426 (defun erc-update-mode-line (&optional buffer)
6427 "Update the mode line in BUFFER.
6429 If BUFFER is nil, update the mode line in all ERC buffers."
6430 (if (and buffer (bufferp buffer))
6431 (erc-update-mode-line-buffer buffer)
6432 (dolist (buf (erc-buffer-list))
6433 (when (buffer-live-p buf)
6434 (erc-update-mode-line-buffer buf)))))
6438 (defun erc-port-to-string (p)
6439 "Convert port P to a string.
6440 P may be an integer or a service name."
6445 (defun erc-string-to-port (s)
6446 "Convert string S to either an integer port number or a service name."
6449 (let ((n (string-to-number s)))
6454 (defun erc-version (&optional here)
6455 "Show the version number of ERC in the minibuffer.
6456 If optional argument HERE is non-nil, insert version number at point."
6458 (let ((version-string
6459 (format "ERC (IRC client for Emacs %s)" emacs-version)))
6461 (insert version-string)
6462 (if (called-interactively-p 'interactive)
6463 (message "%s" version-string)
6466 (defun erc-modes (&optional here)
6467 "Show the active ERC modes in the minibuffer.
6468 If optional argument HERE is non-nil, insert version number at point."
6471 (mapconcat 'identity
6472 (let (modes (case-fold-search nil))
6473 (dolist (var (apropos-internal "^erc-.*mode$"))
6474 (when (and (boundp var)
6476 (setq modes (cons (symbol-name var)
6482 (if (called-interactively-p 'interactive)
6483 (message "%s" string)
6486 (defun erc-trim-string (s)
6487 "Trim leading and trailing spaces off S."
6489 ((not (stringp s)) nil)
6490 ((string-match "^\\s-*$" s)
6492 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
6497 (defun erc-arrange-session-in-multiple-windows ()
6498 "Open a window for every non-server buffer related to `erc-session-server'.
6500 All windows are opened in the current frame."
6502 (unless erc-server-process
6503 (error "No erc-server-process found in current buffer"))
6504 (let ((bufs (erc-buffer-list nil erc-server-process)))
6506 (delete-other-windows)
6507 (switch-to-buffer (car bufs))
6508 (setq bufs (cdr bufs))
6512 (switch-to-buffer (car bufs))
6513 (setq bufs (cdr bufs))
6514 (balance-windows)))))
6516 (defun erc-popup-input-buffer ()
6517 "Provide an input buffer."
6519 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
6524 (list (symbol-name e)))
6525 (apropos-internal "-mode\\'" 'commandp))
6527 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
6529 (narrow-to-region (point) (point))
6530 (shrink-window-if-larger-than-buffer)))
6534 (defun erc-make-message-variable-name (catalog entry)
6535 "Create a variable name corresponding to CATALOG's ENTRY."
6536 (intern (concat "erc-message-"
6537 (symbol-name catalog) "-" (symbol-name entry))))
6539 (defun erc-define-catalog-entry (catalog entry format-spec)
6540 "Set CATALOG's ENTRY to FORMAT-SPEC."
6541 (set (erc-make-message-variable-name catalog entry)
6544 (defun erc-define-catalog (catalog entries)
6545 "Define a CATALOG according to ENTRIES."
6546 (dolist (entry entries)
6547 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
6551 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
6552 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
6553 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
6554 (cannot-find-file . "Cannot find file %f")
6555 (cannot-read-file . "Cannot read file %f")
6556 (connect . "Connecting to %S:%p... ")
6558 (country-unknown . "%d: No such domain")
6559 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
6560 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
6561 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
6562 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
6563 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
6565 . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
6566 (disconnected . "\n\nConnection failed! Re-establishing connection...\n")
6567 (disconnected-noreconnect
6568 . "\n\nConnection failed! Not re-establishing connection.\n")
6569 (finished . "\n\n*** ERC finished ***\n")
6570 (terminated . "\n\n*** ERC terminated: %e\n")
6571 (login . "Logging in as `%n'...")
6572 (nick-in-use . "%n is in use. Choose new nickname: ")
6574 . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
6575 (no-default-channel . "No default channel")
6576 (no-invitation . "You've got no invitation")
6577 (no-target . "No target")
6578 (ops . "%i operator%s: %o")
6579 (ops-none . "No operators in this channel.")
6580 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
6581 (variable-not-bound . "Variable not bound!")
6582 (ACTION . "* %n %a")
6583 (CTCP-CLIENTINFO . "Client info for %n: %m")
6584 (CTCP-ECHO . "Echo %n: %m")
6585 (CTCP-FINGER . "Finger info for %n: %m")
6586 (CTCP-PING . "Ping time to %n is %t")
6587 (CTCP-TIME . "Time by %n is %m")
6588 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
6589 (CTCP-VERSION . "Version for %n is %m")
6590 (ERROR . "==> ERROR from %s: %c\n")
6591 (INVITE . "%n (%u@%h) invites you to channel %c")
6592 (JOIN . "%n (%u@%h) has joined channel %c")
6593 (JOIN-you . "You have joined channel %c")
6594 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
6595 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
6596 (KICK-by-you . "You have kicked %k off channel %c: %r")
6597 (MODE . "%n (%u@%h) has changed mode for %t to %m")
6598 (MODE-nick . "%n has changed mode for %t to %m")
6599 (NICK . "%n (%u@%h) is now known as %N")
6600 (NICK-you . "Your new nickname is %N")
6601 (PART . erc-message-english-PART)
6602 (PING . "PING from server (last: %s sec. ago)")
6603 (PONG . "PONG from %h (%i second%s)")
6604 (QUIT . "%n (%u@%h) has quit: %r")
6605 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
6606 (WALLOPS . "Wallops from %n: %m")
6607 (s004 . "%s %v %U %C")
6608 (s221 . "User modes for %n: %m")
6609 (s252 . "%i operator(s) online")
6610 (s253 . "%i unknown connection(s)")
6611 (s254 . "%i channels formed")
6613 (s301 . "%n is AWAY: %r")
6614 (s303 . "Is online: %n")
6618 (s311 . "%n is %f (%u@%h)")
6619 (s312 . "%n is/was on server %s (%c)")
6620 (s313 . "%n is an IRC operator")
6621 (s314 . "%n was %f (%u@%h)")
6622 (s317 . "%n has been idle for %i")
6623 (s317-on-since . "%n has been idle for %i, on since %t")
6624 (s319 . "%n is on channel(s): %c")
6625 (s320 . "%n is an identified user")
6626 (s321 . "Channel Users Topic")
6627 (s322 . "%c [%u] %t")
6628 (s324 . "%c modes: %m")
6629 (s328 . "%c URL: %u")
6630 (s329 . "%c was created on %t")
6632 (s331 . "No topic is set for %c")
6633 (s332 . "Topic for %c: %T")
6634 (s333 . "%c: topic set by %n, %t")
6635 (s341 . "Inviting %n to channel %c")
6636 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
6637 (s353 . "Users on %c: %u")
6638 (s367 . "Ban for %b on %c")
6639 (s367-set-by . "Ban for %b on %c set by %s on %t")
6640 (s368 . "Banlist of %c ends.")
6641 (s379 . "%c: Forwarded to %f")
6642 (s391 . "The time at %s is %t")
6643 (s401 . "%n: No such nick/channel")
6644 (s403 . "%c: No such channel")
6645 (s404 . "%c: Cannot send to channel")
6646 (s405 . "%c: You have joined too many channels")
6647 (s406 . "%n: There was no such nickname")
6648 (s412 . "No text to send")
6649 (s421 . "%c: Unknown command")
6650 (s431 . "No nickname given")
6651 (s432 . "%n is an erroneous nickname")
6652 (s442 . "%c: You're not on that channel")
6653 (s445 . "SUMMON has been disabled")
6654 (s446 . "USERS has been disabled")
6655 (s451 . "You have not registered")
6656 (s461 . "%c: not enough parameters")
6657 (s462 . "Unauthorized command (already registered)")
6658 (s463 . "Your host isn't among the privileged")
6659 (s464 . "Password incorrect")
6660 (s465 . "You are banned from this server")
6661 (s474 . "You can't join %c because you're banned (+b)")
6662 (s475 . "You must specify the correct channel key (+k) to join %c")
6663 (s481 . "Permission Denied - You're not an IRC operator")
6664 (s482 . "You need to be a channel operator of %c to do that")
6665 (s483 . "You can't kill a server!")
6666 (s484 . "Your connection is restricted!")
6667 (s485 . "You're not the original channel operator")
6668 (s491 . "No O-lines for your host")
6669 (s501 . "Unknown MODE flag")
6670 (s502 . "You can't change modes for other users")
6673 (defun erc-message-english-PART (&rest args)
6674 "Format a proper PART message.
6676 This function is an example on what could be done with formatting
6678 (let ((nick (cadr (memq ?n args)))
6679 (user (cadr (memq ?u args)))
6680 (host (cadr (memq ?h args)))
6681 (channel (cadr (memq ?c args)))
6682 (reason (cadr (memq ?r args))))
6683 (if (string= nick (erc-current-nick))
6684 (format "You have left channel %s" channel)
6685 (format "%s (%s@%s) has left channel %s%s"
6686 nick user host channel
6687 (if (not (string= reason ""))
6689 (erc-replace-regexp-in-string "%" "%%" reason))
6693 (defvar erc-current-message-catalog 'english)
6694 (make-variable-buffer-local 'erc-current-message-catalog)
6696 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6697 "Retrieve ENTRY from CATALOG.
6699 If CATALOG is nil, `erc-current-message-catalog' is used.
6701 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6703 (unless catalog (setq catalog erc-current-message-catalog))
6704 (let ((var (erc-make-message-variable-name catalog entry)))
6707 (when (boundp (erc-make-message-variable-name 'english entry))
6708 (symbol-value (erc-make-message-variable-name 'english entry))))))
6710 (defun erc-format-message (msg &rest args)
6711 "Format MSG according to ARGS.
6713 See also `format-spec'."
6714 (when (eq (logand (length args) 1) 1) ; oddp
6715 (error "Obscure usage of this function appeared"))
6716 (let ((entry (erc-retrieve-catalog-entry msg)))
6718 (error "No format spec for message %s" msg))
6719 (when (functionp entry)
6720 (setq entry (apply entry args)))
6721 (format-spec entry (apply #'format-spec-make args))))
6723 ;;; Various hook functions
6725 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6727 (defcustom erc-kill-server-hook '(erc-kill-server)
6728 "Invoked whenever a server buffer is killed via `kill-buffer'."
6732 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6733 "Invoked whenever a channel-buffer is killed via `kill-buffer'."
6737 (defcustom erc-kill-buffer-hook nil
6738 "Hook run whenever a non-server or channel buffer is killed.
6740 See also `kill-buffer'."
6744 (defun erc-kill-buffer-function ()
6745 "Function to call when an ERC buffer is killed.
6746 This function should be on `kill-buffer-hook'.
6747 When the current buffer is in `erc-mode', this function will run
6748 one of the following hooks:
6749 `erc-kill-server-hook' if the server buffer was killed,
6750 `erc-kill-channel-hook' if a channel buffer was killed,
6751 or `erc-kill-buffer-hook' if any other buffer."
6752 (when (eq major-mode 'erc-mode)
6753 (erc-remove-channel-users)
6755 ((eq (erc-server-buffer) (current-buffer))
6756 (run-hooks 'erc-kill-server-hook))
6757 ((erc-channel-p (or (erc-default-target) (buffer-name)))
6758 (run-hooks 'erc-kill-channel-hook))
6760 (run-hooks 'erc-kill-buffer-hook)))))
6762 (defun erc-kill-server ()
6763 "Sends a QUIT command to the server when the server buffer is killed.
6764 This function should be on `erc-kill-server-hook'."
6765 (when (erc-server-process-alive)
6766 (setq erc-server-quitting t)
6767 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6769 (defun erc-kill-channel ()
6770 "Sends a PART command to the server when the channel buffer is killed.
6771 This function should be on `erc-kill-channel-hook'."
6772 (when (erc-server-process-alive)
6773 (let ((tgt (erc-default-target)))
6775 (erc-server-send (format "PART %s :%s" tgt
6776 (funcall erc-part-reason nil))
6779 ;;; Dealing with `erc-parsed'
6781 (defun erc-find-parsed-property ()
6782 "Find the next occurrence of the `erc-parsed' text property."
6783 (text-property-not-all (point-min) (point-max) 'erc-parsed nil))
6785 (defun erc-restore-text-properties ()
6786 "Restore the property `erc-parsed' for the region."
6787 (let ((parsed-posn (erc-find-parsed-property)))
6789 (point-min) (point-max)
6790 'erc-parsed (when parsed-posn (erc-get-parsed-vector parsed-posn)))))
6792 (defun erc-get-parsed-vector (point)
6793 "Return the whole parsed vector on POINT."
6794 (get-text-property point 'erc-parsed))
6796 (defun erc-get-parsed-vector-nick (vect)
6797 "Return nickname in the parsed vector VECT."
6798 (let* ((untreated-nick (and vect (erc-response.sender vect)))
6799 (maybe-nick (when untreated-nick
6800 (car (split-string untreated-nick "!")))))
6801 (when (and (not (null maybe-nick))
6802 (erc-is-valid-nick-p maybe-nick))
6805 (defun erc-get-parsed-vector-type (vect)
6806 "Return message type in the parsed vector VECT."
6808 (erc-response.command vect)))
6810 ;; Teach url.el how to open irc:// URLs with ERC.
6811 ;; To activate, customize `url-irc-function' to `url-irc-erc'.
6814 (defun erc-handle-irc-url (host port channel user password)
6815 "Use ERC to IRC on HOST:PORT in CHANNEL as USER with PASSWORD.
6816 If ERC is already connected to HOST:PORT, simply /join CHANNEL.
6817 Otherwise, connect to HOST:PORT as USER and /join CHANNEL."
6818 (let ((server-buffer
6819 (car (erc-buffer-filter
6821 (and (string-equal erc-session-server host)
6822 (= erc-session-port port)
6823 (erc-open-server-buffer-p)))))))
6824 (with-current-buffer (or server-buffer (current-buffer))
6825 (if (and server-buffer channel)
6826 (erc-cmd-JOIN channel)
6827 (erc-open host port (or user (erc-compute-nick)) (erc-compute-full-name)
6828 (not server-buffer) password nil channel
6830 (get-buffer-process server-buffer)))))))
6834 ;; Deprecated. We might eventually stop requiring the goodies automatically.
6835 ;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6836 ;; avoid a recursive require error when byte-compiling the entire package.
6837 (require 'erc-goodies)
6839 ;;; erc.el ends here
6842 ;; outline-regexp: ";;+"
6843 ;; indent-tabs-mode: t