Make CC Mode load cl-lib rather than cl in Emacs 26.
[emacs.git] / lisp / erc / erc.el
blob8547821f0800b77d59d7512a3fb3526efd2a546a
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
16 ;; Version: 5.3
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 <http://www.gnu.org/licenses/>.
33 ;;; Commentary:
35 ;; ERC is a powerful, modular, and extensible IRC client for Emacs.
37 ;; For more information, see the following URLs:
38 ;; * http://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 ;; (http://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.
48 ;; Configuration:
50 ;; Use M-x customize-group RET erc RET to get an overview
51 ;; of all the variables you can tweak.
53 ;; Usage:
55 ;; To connect to an IRC server, do
57 ;; M-x erc RET
59 ;; After you are connected to a server, you can use C-h m or have a look at
60 ;; the ERC menu.
62 ;;; History:
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'.")
68 ;;; Code:
70 (eval-when-compile (require 'cl-lib))
71 (require 'font-lock)
72 (require 'pp)
73 (require 'thingatpt)
74 (require 'auth-source)
75 (require 'erc-compat)
77 (defvar erc-official-location
78 "http://emacswiki.org/cgi-bin/wiki/ERC (mailing list: erc-discuss@gnu.org)"
79 "Location of the ERC client on the Internet.")
81 (defgroup erc nil
82 "Emacs Internet Relay Chat client."
83 :link '(url-link "http://www.emacswiki.org/cgi-bin/wiki/ERC")
84 :link '(custom-manual "(erc) Top")
85 :prefix "erc-"
86 :group 'applications)
88 (defgroup erc-buffers nil
89 "Creating new ERC buffers"
90 :group 'erc)
92 (defgroup erc-display nil
93 "Settings for how various things are displayed"
94 :group 'erc)
96 (defgroup erc-mode-line-and-header nil
97 "Displaying information in the mode-line and header"
98 :group 'erc-display)
100 (defgroup erc-ignore nil
101 "Ignoring certain messages"
102 :group 'erc)
104 (defgroup erc-lurker nil
105 "Hide specified message types sent by lurkers"
106 :version "24.3"
107 :group 'erc-ignore)
109 (defgroup erc-query nil
110 "Using separate buffers for private discussions"
111 :group 'erc)
113 (defgroup erc-quit-and-part nil
114 "Quitting and parting channels"
115 :group 'erc)
117 (defgroup erc-paranoia nil
118 "Know what is sent and received; control the display of sensitive data."
119 :group 'erc)
121 (defgroup erc-scripts nil
122 "Running scripts at startup and with /LOAD"
123 :group 'erc)
125 (require 'erc-backend)
127 ;; compatibility with older ERC releases
129 (define-obsolete-variable-alias 'erc-announced-server-name
130 'erc-server-announced-name "ERC 5.1")
131 (define-obsolete-variable-alias 'erc-process 'erc-server-process "ERC 5.1")
132 (define-obsolete-variable-alias 'erc-default-coding-system
133 'erc-server-coding-system "ERC 5.1")
135 (define-obsolete-function-alias 'erc-send-command
136 'erc-server-send "ERC 5.1")
138 ;; tunable connection and authentication parameters
140 (defcustom erc-server nil
141 "IRC server to use if one is not provided.
142 See function `erc-compute-server' for more details on connection
143 parameters and authentication."
144 :group 'erc
145 :type '(choice (const :tag "None" nil)
146 (string :tag "Server")))
148 (defcustom erc-port nil
149 "IRC port to use if not specified.
151 This can be either a string or a number."
152 :group 'erc
153 :type '(choice (const :tag "None" nil)
154 (integer :tag "Port number")
155 (string :tag "Port string")))
157 (defcustom erc-nick nil
158 "Nickname to use if one is not provided.
160 This can be either a string, or a list of strings.
161 In the latter case, if the first nick in the list is already in use,
162 other nicks are tried in the list order.
164 See function `erc-compute-nick' for more details on connection
165 parameters and authentication."
166 :group 'erc
167 :type '(choice (const :tag "None" nil)
168 (string :tag "Nickname")
169 (repeat (string :tag "Nickname"))))
171 (defcustom erc-nick-uniquifier "`"
172 "The string to append to the nick if it is already in use."
173 :group 'erc
174 :type 'string)
176 (defcustom erc-try-new-nick-p t
177 "If the nickname you chose isn't available, and this option is non-nil,
178 ERC should automatically attempt to connect with another nickname.
180 You can manually set another nickname with the /NICK command."
181 :group 'erc
182 :type 'boolean)
184 (defcustom erc-user-full-name nil
185 "User full name.
187 This can be either a string or a function to call.
189 See function `erc-compute-full-name' for more details on connection
190 parameters and authentication."
191 :group 'erc
192 :type '(choice (const :tag "No name" nil)
193 (string :tag "Name")
194 (function :tag "Get from function"))
195 :set (lambda (sym val)
196 (set sym (if (functionp val) (funcall val) val))))
198 (defcustom erc-rename-buffers nil
199 "Non-nil means rename buffers with network name, if available."
200 :version "24.5"
201 :group 'erc
202 :type 'boolean)
204 (defvar erc-password nil
205 "Password to use when authenticating to an IRC server.
206 It is not strictly necessary to provide this, since ERC will
207 prompt you for it.")
209 (defcustom erc-user-mode nil
210 "Initial user modes to be set after a connection is established."
211 :group 'erc
212 :type '(choice (const nil) string function))
215 (defcustom erc-prompt-for-password t
216 "Asks before using the default password, or whether to enter a new one."
217 :group 'erc
218 :type 'boolean)
220 (defcustom erc-warn-about-blank-lines t
221 "Warn the user if they attempt to send a blank line."
222 :group 'erc
223 :type 'boolean)
225 (defcustom erc-send-whitespace-lines nil
226 "If set to non-nil, send lines consisting of only whitespace."
227 :group 'erc
228 :type 'boolean)
230 (defcustom erc-hide-prompt nil
231 "If non-nil, do not display the prompt for commands.
233 \(A command is any input starting with a `/').
235 See also the variables `erc-prompt' and `erc-command-indicator'."
236 :group 'erc-display
237 :type 'boolean)
239 ;; tunable GUI stuff
241 (defcustom erc-show-my-nick t
242 "If non-nil, display one's own nickname when sending a message.
244 If non-nil, \"<nickname>\" will be shown.
245 If nil, only \"> \" will be shown."
246 :group 'erc-display
247 :type 'boolean)
249 (define-widget 'erc-message-type 'set
250 "A set of standard IRC Message types."
251 :args '((const "JOIN")
252 (const "KICK")
253 (const "NICK")
254 (const "PART")
255 (const "QUIT")
256 (const "MODE")
257 (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))
259 (defcustom erc-hide-list nil
260 "A global list of IRC message types to hide.
261 A typical value would be \(\"JOIN\" \"PART\" \"QUIT\")."
262 :group 'erc-ignore
263 :type 'erc-message-type)
265 (defcustom erc-network-hide-list nil
266 "A list of IRC networks to hide message types from.
267 A typical value would be \((\"freenode\" \"MODE\")
268 \(\"OFTC\" \"JOIN\" \"QUIT\"))."
269 :version "25.1"
270 :group 'erc-ignore
271 :type 'erc-message-type)
273 (defcustom erc-channel-hide-list nil
274 "A list of IRC channels to hide message types from.
275 A typical value would be \((\"#emacs\" \"QUIT\" \"JOIN\")
276 \(\"#erc\" \"NICK\")."
277 :version "25.1"
278 :group 'erc-ignore
279 :type 'erc-message-type)
281 (defvar erc-session-password nil
282 "The password used for the current session.")
283 (make-variable-buffer-local 'erc-session-password)
285 (defcustom erc-disconnected-hook nil
286 "Run this hook with arguments (NICK IP REASON) when disconnected.
287 This happens before automatic reconnection. Note, that
288 `erc-server-QUIT-functions' might not be run when we disconnect,
289 simply because we do not necessarily receive the QUIT event."
290 :group 'erc-hooks
291 :type 'hook)
293 (defcustom erc-complete-functions nil
294 "These functions get called when the user hits TAB in ERC.
295 Each function in turn is called until one returns non-nil to
296 indicate it has handled the input."
297 :group 'erc-hooks
298 :type 'hook)
300 (defcustom erc-join-hook nil
301 "Hook run when we join a channel. Hook functions are called
302 without arguments, with the current buffer set to the buffer of
303 the new channel.
305 See also `erc-server-JOIN-functions', `erc-part-hook'."
306 :group 'erc-hooks
307 :type 'hook)
309 (defcustom erc-quit-hook nil
310 "Hook run when processing a quit command directed at our nick.
312 The hook receives one argument, the current PROCESS.
313 See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
314 :group 'erc-hooks
315 :type 'hook)
317 (defcustom erc-part-hook nil
318 "Hook run when processing a PART message directed at our nick.
320 The hook receives one argument, the current BUFFER.
321 See also `erc-server-QUIT-functions', `erc-quit-hook' and
322 `erc-disconnected-hook'."
323 :group 'erc-hooks
324 :type 'hook)
326 (defcustom erc-kick-hook nil
327 "Hook run when processing a KICK message directed at our nick.
329 The hook receives one argument, the current BUFFER.
330 See also `erc-server-PART-functions' and `erc-part-hook'."
331 :group 'erc-hooks
332 :type 'hook)
334 (defcustom erc-nick-changed-functions nil
335 "List of functions run when your nick was successfully changed.
337 Each function should accept two arguments, NEW-NICK and OLD-NICK."
338 :group 'erc-hooks
339 :type 'hook)
341 (defcustom erc-connect-pre-hook '(erc-initialize-log-marker)
342 "Hook called just before `erc' calls `erc-connect'.
343 Functions are passed a buffer as the first argument."
344 :group 'erc-hooks
345 :type 'hook)
348 (defvar erc-channel-users nil
349 "A hash table of members in the current channel, which
350 associates nicknames with cons cells of the form:
351 \(USER . MEMBER-DATA) where USER is a pointer to an
352 erc-server-user struct, and MEMBER-DATA is a pointer to an
353 erc-channel-user struct.")
354 (make-variable-buffer-local 'erc-channel-users)
356 (defvar erc-server-users nil
357 "A hash table of users on the current server, which associates
358 nicknames with erc-server-user struct instances.")
359 (make-variable-buffer-local 'erc-server-users)
361 (defun erc-downcase (string)
362 "Convert STRING to IRC standard conforming downcase."
363 (let ((s (downcase string))
364 (c '((?\[ . ?\{)
365 (?\] . ?\})
366 (?\\ . ?\|)
367 (?~ . ?^))))
368 (save-match-data
369 (while (string-match "[]\\[~]" s)
370 (aset s (match-beginning 0)
371 (cdr (assq (aref s (match-beginning 0)) c)))))
374 (defmacro erc-with-server-buffer (&rest body)
375 "Execute BODY in the current ERC server buffer.
376 If no server buffer exists, return nil."
377 (declare (indent 0) (debug (body)))
378 (let ((buffer (make-symbol "buffer")))
379 `(let ((,buffer (erc-server-buffer)))
380 (when (buffer-live-p ,buffer)
381 (with-current-buffer ,buffer
382 ,@body)))))
384 (cl-defstruct (erc-server-user (:type vector) :named)
385 ;; User data
386 nickname host login full-name info
387 ;; Buffers
389 ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
390 ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
391 (buffers nil)
394 (cl-defstruct (erc-channel-user (:type vector) :named)
395 voice halfop op admin owner
396 ;; Last message time (in the form of the return value of
397 ;; (current-time)
399 ;; This is useful for ordered name completion.
400 (last-message-time nil))
402 (defsubst erc-get-channel-user (nick)
403 "Find the (USER . CHANNEL-DATA) element corresponding to NICK
404 in the current buffer's `erc-channel-users' hash table."
405 (gethash (erc-downcase nick) erc-channel-users))
407 (defsubst erc-get-server-user (nick)
408 "Find the USER corresponding to NICK in the current server's
409 `erc-server-users' hash table."
410 (erc-with-server-buffer
411 (gethash (erc-downcase nick) erc-server-users)))
413 (defsubst erc-add-server-user (nick user)
414 "This function is for internal use only.
416 Adds USER with nickname NICK to the `erc-server-users' hash table."
417 (erc-with-server-buffer
418 (puthash (erc-downcase nick) user erc-server-users)))
420 (defsubst erc-remove-server-user (nick)
421 "This function is for internal use only.
423 Removes the user with nickname NICK from the `erc-server-users'
424 hash table. This user is not removed from the
425 `erc-channel-users' lists of other buffers.
427 See also: `erc-remove-user'."
428 (erc-with-server-buffer
429 (remhash (erc-downcase nick) erc-server-users)))
431 (defun erc-change-user-nickname (user new-nick)
432 "This function is for internal use only.
434 Changes the nickname of USER to NEW-NICK in the
435 `erc-server-users' hash table. The `erc-channel-users' lists of
436 other buffers are also changed."
437 (let ((nick (erc-server-user-nickname user)))
438 (setf (erc-server-user-nickname user) new-nick)
439 (erc-with-server-buffer
440 (remhash (erc-downcase nick) erc-server-users)
441 (puthash (erc-downcase new-nick) user erc-server-users))
442 (dolist (buf (erc-server-user-buffers user))
443 (if (buffer-live-p buf)
444 (with-current-buffer buf
445 (let ((cdata (erc-get-channel-user nick)))
446 (remhash (erc-downcase nick) erc-channel-users)
447 (puthash (erc-downcase new-nick) cdata
448 erc-channel-users)))))))
450 (defun erc-remove-channel-user (nick)
451 "This function is for internal use only.
453 Removes the user with nickname NICK from the `erc-channel-users'
454 list for this channel. If this user is not in the
455 `erc-channel-users' list of any other buffers, the user is also
456 removed from the server's `erc-server-users' list.
458 See also: `erc-remove-server-user' and `erc-remove-user'."
459 (let ((channel-data (erc-get-channel-user nick)))
460 (when channel-data
461 (let ((user (car channel-data)))
462 (setf (erc-server-user-buffers user)
463 (delq (current-buffer)
464 (erc-server-user-buffers user)))
465 (remhash (erc-downcase nick) erc-channel-users)
466 (if (null (erc-server-user-buffers user))
467 (erc-remove-server-user nick))))))
469 (defun erc-remove-user (nick)
470 "This function is for internal use only.
472 Removes the user with nickname NICK from the `erc-server-users'
473 list as well as from all `erc-channel-users' lists.
475 See also: `erc-remove-server-user' and
476 `erc-remove-channel-user'."
477 (let ((user (erc-get-server-user nick)))
478 (when user
479 (let ((buffers (erc-server-user-buffers user)))
480 (dolist (buf buffers)
481 (if (buffer-live-p buf)
482 (with-current-buffer buf
483 (remhash (erc-downcase nick) erc-channel-users)
484 (run-hooks 'erc-channel-members-changed-hook)))))
485 (erc-remove-server-user nick))))
487 (defun erc-remove-channel-users ()
488 "This function is for internal use only.
490 Removes all users in the current channel. This is called by
491 `erc-server-PART' and `erc-server-QUIT'."
492 (when (and erc-server-connected
493 (erc-server-process-alive)
494 (hash-table-p erc-channel-users))
495 (maphash (lambda (nick _cdata)
496 (erc-remove-channel-user nick))
497 erc-channel-users)
498 (clrhash erc-channel-users)))
500 (defsubst erc-channel-user-owner-p (nick)
501 "Return non-nil if NICK is an owner of the current channel."
502 (and nick
503 (hash-table-p erc-channel-users)
504 (let ((cdata (erc-get-channel-user nick)))
505 (and cdata (cdr cdata)
506 (erc-channel-user-owner (cdr cdata))))))
508 (defsubst erc-channel-user-admin-p (nick)
509 "Return non-nil if NICK is an admin in the current channel."
510 (and nick
511 (hash-table-p erc-channel-users)
512 (let ((cdata (erc-get-channel-user nick)))
513 (and cdata (cdr cdata)
514 (erc-channel-user-admin (cdr cdata))))))
516 (defsubst erc-channel-user-op-p (nick)
517 "Return non-nil if NICK is an operator in the current channel."
518 (and nick
519 (hash-table-p erc-channel-users)
520 (let ((cdata (erc-get-channel-user nick)))
521 (and cdata (cdr cdata)
522 (erc-channel-user-op (cdr cdata))))))
524 (defsubst erc-channel-user-halfop-p (nick)
525 "Return non-nil if NICK is a half-operator in the current channel."
526 (and nick
527 (hash-table-p erc-channel-users)
528 (let ((cdata (erc-get-channel-user nick)))
529 (and cdata (cdr cdata)
530 (erc-channel-user-halfop (cdr cdata))))))
532 (defsubst erc-channel-user-voice-p (nick)
533 "Return non-nil if NICK has voice in the current channel."
534 (and nick
535 (hash-table-p erc-channel-users)
536 (let ((cdata (erc-get-channel-user nick)))
537 (and cdata (cdr cdata)
538 (erc-channel-user-voice (cdr cdata))))))
540 (defun erc-get-channel-user-list ()
541 "Return a list of users in the current channel. Each element
542 of the list is of the form (USER . CHANNEL-DATA), where USER is
543 an erc-server-user struct, and CHANNEL-DATA is either nil or an
544 erc-channel-user struct.
546 See also: `erc-sort-channel-users-by-activity'"
547 (let (users)
548 (if (hash-table-p erc-channel-users)
549 (maphash (lambda (_nick cdata)
550 (setq users (cons cdata users)))
551 erc-channel-users))
552 users))
554 (defun erc-get-server-nickname-list ()
555 "Return a list of known nicknames on the current server."
556 (erc-with-server-buffer
557 (let (nicks)
558 (when (hash-table-p erc-server-users)
559 (maphash (lambda (_n user)
560 (setq nicks
561 (cons (erc-server-user-nickname user)
562 nicks)))
563 erc-server-users)
564 nicks))))
566 (defun erc-get-channel-nickname-list ()
567 "Return a list of known nicknames on the current channel."
568 (let (nicks)
569 (when (hash-table-p erc-channel-users)
570 (maphash (lambda (_n cdata)
571 (setq nicks
572 (cons (erc-server-user-nickname (car cdata))
573 nicks)))
574 erc-channel-users)
575 nicks)))
577 (defun erc-get-server-nickname-alist ()
578 "Return an alist of known nicknames on the current server."
579 (erc-with-server-buffer
580 (let (nicks)
581 (when (hash-table-p erc-server-users)
582 (maphash (lambda (_n user)
583 (setq nicks
584 (cons (cons (erc-server-user-nickname user) nil)
585 nicks)))
586 erc-server-users)
587 nicks))))
589 (defun erc-get-channel-nickname-alist ()
590 "Return an alist of known nicknames on the current channel."
591 (let (nicks)
592 (when (hash-table-p erc-channel-users)
593 (maphash (lambda (_n cdata)
594 (setq nicks
595 (cons (cons (erc-server-user-nickname (car cdata)) nil)
596 nicks)))
597 erc-channel-users)
598 nicks)))
600 (defun erc-sort-channel-users-by-activity (list)
601 "Sort LIST such that users which have spoken most recently are listed first.
602 LIST must be of the form (USER . CHANNEL-DATA).
604 See also: `erc-get-channel-user-list'."
605 (sort list
606 (lambda (x y)
607 (when (and (cdr x) (cdr y))
608 (let ((tx (erc-channel-user-last-message-time (cdr x)))
609 (ty (erc-channel-user-last-message-time (cdr y))))
610 (and tx
611 (or (not ty)
612 (time-less-p ty tx))))))))
614 (defun erc-sort-channel-users-alphabetically (list)
615 "Sort LIST so that users' nicknames are in alphabetical order.
616 LIST must be of the form (USER . CHANNEL-DATA).
618 See also: `erc-get-channel-user-list'."
619 (sort list
620 (lambda (x y)
621 (when (and (cdr x) (cdr y))
622 (let ((nickx (downcase (erc-server-user-nickname (car x))))
623 (nicky (downcase (erc-server-user-nickname (car y)))))
624 (and nickx
625 (or (not nicky)
626 (string-lessp nickx nicky))))))))
628 (defvar erc-channel-topic nil
629 "A topic string for the channel. Should only be used in channel-buffers.")
630 (make-variable-buffer-local 'erc-channel-topic)
632 (defvar erc-channel-modes nil
633 "List of strings representing channel modes.
634 E.g. (\"i\" \"m\" \"s\" \"b Quake!*@*\")
635 \(not sure the ban list will be here, but why not)")
636 (make-variable-buffer-local 'erc-channel-modes)
638 (defvar erc-insert-marker nil
639 "The place where insertion of new text in erc buffers should happen.")
640 (make-variable-buffer-local 'erc-insert-marker)
642 (defvar erc-input-marker nil
643 "The marker where input should be inserted.")
644 (make-variable-buffer-local 'erc-input-marker)
646 (defun erc-string-no-properties (string)
647 "Return a copy of STRING will all text-properties removed."
648 (let ((newstring (copy-sequence string)))
649 (set-text-properties 0 (length newstring) nil newstring)
650 newstring))
652 (defcustom erc-prompt "ERC>"
653 "Prompt used by ERC. Trailing whitespace is not required."
654 :group 'erc-display
655 :type '(choice string function))
657 (defun erc-prompt ()
658 "Return the input prompt as a string.
660 See also the variable `erc-prompt'."
661 (let ((prompt (if (functionp erc-prompt)
662 (funcall erc-prompt)
663 erc-prompt)))
664 (if (> (length prompt) 0)
665 (concat prompt " ")
666 prompt)))
668 (defcustom erc-command-indicator nil
669 "Indicator used by ERC for showing commands.
671 If non-nil, this will be used in the ERC buffer to indicate
672 commands (i.e., input starting with a `/').
674 If nil, the prompt will be constructed from the variable `erc-prompt'."
675 :group 'erc-display
676 :type '(choice (const nil) string function))
678 (defun erc-command-indicator ()
679 "Return the command indicator prompt as a string.
681 This only has any meaning if the variable `erc-command-indicator' is non-nil."
682 (and erc-command-indicator
683 (let ((prompt (if (functionp erc-command-indicator)
684 (funcall erc-command-indicator)
685 erc-command-indicator)))
686 (if (> (length prompt) 0)
687 (concat prompt " ")
688 prompt))))
690 (defcustom erc-notice-prefix "*** "
691 "Prefix for all notices."
692 :group 'erc-display
693 :type 'string)
695 (defcustom erc-notice-highlight-type 'all
696 "Determines how to highlight notices.
697 See `erc-notice-prefix'.
699 The following values are allowed:
701 `prefix' - highlight notice prefix only
702 `all' - highlight the entire notice
704 Any other value disables notice's highlighting altogether."
705 :group 'erc-display
706 :type '(choice (const :tag "highlight notice prefix only" prefix)
707 (const :tag "highlight the entire notice" all)
708 (const :tag "don't highlight notices at all" nil)))
710 (defcustom erc-echo-notice-hook nil
711 "List of functions to call to echo a private notice.
712 Each function is called with four arguments, the string
713 to display, the parsed server message, the target buffer (or
714 nil), and the sender. The functions are called in order, until a
715 function evaluates to non-nil. These hooks are called after
716 those specified in `erc-echo-notice-always-hook'.
718 See also: `erc-echo-notice-always-hook',
719 `erc-echo-notice-in-default-buffer',
720 `erc-echo-notice-in-target-buffer',
721 `erc-echo-notice-in-minibuffer',
722 `erc-echo-notice-in-server-buffer',
723 `erc-echo-notice-in-active-non-server-buffer',
724 `erc-echo-notice-in-active-buffer',
725 `erc-echo-notice-in-user-buffers',
726 `erc-echo-notice-in-user-and-target-buffers',
727 `erc-echo-notice-in-first-user-buffer'"
728 :group 'erc-hooks
729 :type 'hook
730 :options '(erc-echo-notice-in-default-buffer
731 erc-echo-notice-in-target-buffer
732 erc-echo-notice-in-minibuffer
733 erc-echo-notice-in-server-buffer
734 erc-echo-notice-in-active-non-server-buffer
735 erc-echo-notice-in-active-buffer
736 erc-echo-notice-in-user-buffers
737 erc-echo-notice-in-user-and-target-buffers
738 erc-echo-notice-in-first-user-buffer))
740 (defcustom erc-echo-notice-always-hook
741 '(erc-echo-notice-in-default-buffer)
742 "List of functions to call to echo a private notice.
743 Each function is called with four arguments, the string
744 to display, the parsed server message, the target buffer (or
745 nil), and the sender. The functions are called in order, and all
746 functions are called. These hooks are called before those
747 specified in `erc-echo-notice-hook'.
749 See also: `erc-echo-notice-hook',
750 `erc-echo-notice-in-default-buffer',
751 `erc-echo-notice-in-target-buffer',
752 `erc-echo-notice-in-minibuffer',
753 `erc-echo-notice-in-server-buffer',
754 `erc-echo-notice-in-active-non-server-buffer',
755 `erc-echo-notice-in-active-buffer',
756 `erc-echo-notice-in-user-buffers',
757 `erc-echo-notice-in-user-and-target-buffers',
758 `erc-echo-notice-in-first-user-buffer'"
759 :group 'erc-hooks
760 :type 'hook
761 :options '(erc-echo-notice-in-default-buffer
762 erc-echo-notice-in-target-buffer
763 erc-echo-notice-in-minibuffer
764 erc-echo-notice-in-server-buffer
765 erc-echo-notice-in-active-non-server-buffer
766 erc-echo-notice-in-active-buffer
767 erc-echo-notice-in-user-buffers
768 erc-echo-notice-in-user-and-target-buffers
769 erc-echo-notice-in-first-user-buffer))
771 ;; other tunable parameters
773 (defcustom erc-whowas-on-nosuchnick nil
774 "If non-nil, do a whowas on a nick if no such nick."
775 :group 'erc
776 :type 'boolean)
778 (defcustom erc-verbose-server-ping nil
779 "If non-nil, show every time you get a PING or PONG from the server."
780 :group 'erc-paranoia
781 :type 'boolean)
783 (defcustom erc-public-away-p nil
784 "Let others know you are back when you are no longer marked away.
785 This happens in this form:
786 * <nick> is back (gone for <time>)
788 Many consider it impolite to do so automatically."
789 :group 'erc
790 :type 'boolean)
792 (defcustom erc-away-nickname nil
793 "The nickname to take when you are marked as being away."
794 :group 'erc
795 :type '(choice (const nil)
796 string))
798 (defcustom erc-paranoid nil
799 "If non-nil, then all incoming CTCP requests will be shown."
800 :group 'erc-paranoia
801 :type 'boolean)
803 (defcustom erc-disable-ctcp-replies nil
804 "Disable replies to CTCP requests that require a reply.
805 If non-nil, then all incoming CTCP requests that normally require
806 an automatic reply (like VERSION or PING) will be ignored. Good to
807 set if some hacker is trying to flood you away."
808 :group 'erc-paranoia
809 :type 'boolean)
811 (defcustom erc-anonymous-login t
812 "Be paranoid, don't give away your machine name."
813 :group 'erc-paranoia
814 :type 'boolean)
816 (defcustom erc-prompt-for-channel-key nil
817 "Prompt for channel key when using `erc-join-channel' interactively."
818 :group 'erc
819 :type 'boolean)
821 (defcustom erc-email-userid "user"
822 "Use this as your email user ID."
823 :group 'erc
824 :type 'string)
826 (defcustom erc-system-name nil
827 "Use this as the name of your system.
828 If nil, ERC will call `system-name' to get this information."
829 :group 'erc
830 :type '(choice (const :tag "Default system name" nil)
831 string))
833 (defcustom erc-ignore-list nil
834 "List of regexps matching user identifiers to ignore.
836 A user identifier has the form \"nick!login@host\". If an
837 identifier matches, the message from the person will not be
838 processed."
839 :group 'erc-ignore
840 :type '(repeat regexp))
841 (make-variable-buffer-local 'erc-ignore-list)
843 (defcustom erc-ignore-reply-list nil
844 "List of regexps matching user identifiers to ignore completely.
846 This differs from `erc-ignore-list' in that it also ignores any
847 messages directed at the user.
849 A user identifier has the form \"nick!login@host\".
851 If an identifier matches, or a message is addressed to a nick
852 whose identifier matches, the message will not be processed.
854 CAVEAT: ERC doesn't know about the user and host of anyone who
855 was already in the channel when you joined, but never said
856 anything, so it won't be able to match the user and host of those
857 people. You can update the ERC internal info using /WHO *."
858 :group 'erc-ignore
859 :type '(repeat regexp))
861 (defvar erc-flood-protect t
862 "If non-nil, flood protection is enabled.
863 Flooding is sending too much information to the server in too
864 short of an interval, which may cause the server to terminate the
865 connection.
867 See `erc-server-flood-margin' for other flood-related parameters.")
869 ;; Script parameters
871 (defcustom erc-startup-file-list
872 (list (concat erc-user-emacs-directory ".ercrc.el")
873 (concat erc-user-emacs-directory ".ercrc")
874 "~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
875 "List of files to try for a startup script.
876 The first existent and readable one will get executed.
878 If the filename ends with `.el' it is presumed to be an Emacs Lisp
879 script and it gets (load)ed. Otherwise it is treated as a bunch of
880 regular IRC commands."
881 :group 'erc-scripts
882 :type '(repeat file))
884 (defcustom erc-script-path nil
885 "List of directories to look for a script in /load command.
886 The script is first searched in the current directory, then in each
887 directory in the list."
888 :group 'erc-scripts
889 :type '(repeat directory))
891 (defcustom erc-script-echo t
892 "If non-nil, echo the IRC script commands locally."
893 :group 'erc-scripts
894 :type 'boolean)
896 (defvar erc-last-saved-position nil
897 "A marker containing the position the current buffer was last saved at.")
898 (make-variable-buffer-local 'erc-last-saved-position)
900 (defcustom erc-kill-buffer-on-part nil
901 "Kill the channel buffer on PART.
902 This variable should probably stay nil, as ERC can reuse buffers if
903 you rejoin them later."
904 :group 'erc-quit-and-part
905 :type 'boolean)
907 (defcustom erc-kill-queries-on-quit nil
908 "Kill all query (also channel) buffers of this server on QUIT.
909 See the variable `erc-kill-buffer-on-part' for details."
910 :group 'erc-quit-and-part
911 :type 'boolean)
913 (defcustom erc-kill-server-buffer-on-quit nil
914 "Kill the server buffer of the process on QUIT."
915 :group 'erc-quit-and-part
916 :type 'boolean)
918 (defcustom erc-quit-reason-various-alist nil
919 "Alist of possible arguments to the /quit command.
921 Each element has the form:
922 (REGEXP RESULT)
924 If REGEXP matches the argument to /quit, then its relevant RESULT
925 will be used. RESULT may be either a string, or a function. If
926 a function, it should return the quit message as a string.
928 If no elements match, then the empty string is used.
930 As an example:
931 (setq erc-quit-reason-various-alist
932 \\='((\"xmms\" dme:now-playing)
933 (\"version\" erc-quit-reason-normal)
934 (\"home\" \"Gone home !\")
935 (\"^$\" \"Default Reason\")))
936 If the user types \"/quit home\", then \"Gone home !\" will be used
937 as the quit message."
938 :group 'erc-quit-and-part
939 :type '(repeat (list regexp (choice (string) (function)))))
941 (defcustom erc-part-reason-various-alist nil
942 "Alist of possible arguments to the /part command.
944 Each element has the form:
945 (REGEXP RESULT)
947 If REGEXP matches the argument to /part, then its relevant RESULT
948 will be used. RESULT may be either a string, or a function. If
949 a function, it should return the part message as a string.
951 If no elements match, then the empty string is used.
953 As an example:
954 (setq erc-part-reason-various-alist
955 \\='((\"xmms\" dme:now-playing)
956 (\"version\" erc-part-reason-normal)
957 (\"home\" \"Gone home !\")
958 (\"^$\" \"Default Reason\")))
959 If the user types \"/part home\", then \"Gone home !\" will be used
960 as the part message."
961 :group 'erc-quit-and-part
962 :type '(repeat (list regexp (choice (string) (function)))))
964 (defcustom erc-quit-reason 'erc-quit-reason-normal
965 "A function which returns the reason for quitting.
967 The function is passed a single argument, the string typed by the
968 user after \"/quit\"."
969 :group 'erc-quit-and-part
970 :type '(choice (const erc-quit-reason-normal)
971 (const erc-quit-reason-various)
972 (symbol)))
974 (defcustom erc-part-reason 'erc-part-reason-normal
975 "A function which returns the reason for parting a channel.
977 The function is passed a single argument, the string typed by the
978 user after \"/PART\"."
979 :group 'erc-quit-and-part
980 :type '(choice (const erc-part-reason-normal)
981 (const erc-part-reason-various)
982 (symbol)))
984 (defvar erc-grab-buffer-name "*erc-grab*"
985 "The name of the buffer created by `erc-grab-region'.")
987 ;; variables available for IRC scripts
989 (defvar erc-user-information "ERC User"
990 "USER_INFORMATION IRC variable.")
992 ;; Hooks
994 (defgroup erc-hooks nil
995 "Hook variables for fancy customizations of ERC."
996 :group 'erc)
998 (defcustom erc-mode-hook nil
999 "Hook run after `erc-mode' setup is finished."
1000 :group 'erc-hooks
1001 :type 'hook
1002 :options '(erc-add-scroll-to-bottom))
1004 (defcustom erc-timer-hook nil
1005 "Put functions which should get called more or less periodically here.
1006 The idea is that servers always play ping pong with the client, and so there
1007 is no need for any idle-timer games with Emacs."
1008 :group 'erc-hooks
1009 :type 'hook)
1011 (defcustom erc-insert-pre-hook nil
1012 "Hook called first when some text is inserted through `erc-display-line'.
1013 It gets called with one argument, STRING.
1014 To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
1015 Filtering functions can set `erc-insert-this' to nil to avoid
1016 display of that particular string at all."
1017 :group 'erc-hooks
1018 :type 'hook)
1020 (defcustom erc-send-pre-hook nil
1021 "Hook called first when some text is sent through `erc-send-current-line'.
1022 It gets called with one argument, STRING.
1024 To change the text that will be sent, set the variable `str' which is
1025 used in `erc-send-current-line'.
1027 To change the text inserted into the buffer without changing the text
1028 that will be sent, use `erc-send-modify-hook' instead.
1030 Filtering functions can set `erc-send-this' to nil to avoid sending of
1031 that particular string at all and `erc-insert-this' to prevent
1032 inserting that particular string into the buffer.
1034 Note that it's useless to set `erc-send-this' to nil and
1035 `erc-insert-this' to t. ERC is sane enough to not insert the text
1036 anyway."
1037 :group 'erc-hooks
1038 :type 'hook)
1040 (defvar erc-insert-this t
1041 "Insert the text into the target buffer or not.
1042 Functions on `erc-insert-pre-hook' can set this variable to nil
1043 if they wish to avoid insertion of a particular string.")
1045 (defvar erc-send-this t
1046 "Send the text to the target or not.
1047 Functions on `erc-send-pre-hook' can set this variable to nil
1048 if they wish to avoid sending of a particular string.")
1050 (defcustom erc-insert-modify-hook ()
1051 "Insertion hook for functions that will change the text's appearance.
1052 This hook is called just after `erc-insert-pre-hook' when the value
1053 of `erc-insert-this' is t.
1054 While this hook is run, narrowing is in effect and `current-buffer' is
1055 the buffer where the text got inserted. One possible value to add here
1056 is `erc-fill'."
1057 :group 'erc-hooks
1058 :type 'hook)
1060 (defcustom erc-insert-post-hook nil
1061 "This hook is called just after `erc-insert-modify-hook'.
1062 At this point, all modifications from prior hook functions are done."
1063 :group 'erc-hooks
1064 :type 'hook
1065 :options '(erc-truncate-buffer
1066 erc-make-read-only
1067 erc-save-buffer-in-logs))
1069 (defcustom erc-send-modify-hook nil
1070 "Sending hook for functions that will change the text's appearance.
1071 This hook is called just after `erc-send-pre-hook' when the values
1072 of `erc-send-this' and `erc-insert-this' are both t.
1073 While this hook is run, narrowing is in effect and `current-buffer' is
1074 the buffer where the text got inserted.
1076 Note that no function in this hook can change the appearance of the
1077 text that is sent. Only changing the sent text's appearance on the
1078 sending user's screen is possible. One possible value to add here
1079 is `erc-fill'."
1080 :group 'erc-hooks
1081 :type 'hook)
1083 (defcustom erc-send-post-hook nil
1084 "This hook is called just after `erc-send-modify-hook'.
1085 At this point, all modifications from prior hook functions are done.
1086 NOTE: The functions on this hook are called _before_ sending a command
1087 to the server.
1089 This function is called with narrowing, ala `erc-send-modify-hook'."
1090 :group 'erc-hooks
1091 :type 'hook
1092 :options '(erc-make-read-only))
1094 (defcustom erc-send-completed-hook
1095 (when (fboundp 'emacspeak-auditory-icon)
1096 (list (byte-compile
1097 (lambda (_str)
1098 (emacspeak-auditory-icon 'select-object)))))
1099 "Hook called after a message has been parsed by ERC.
1101 The single argument to the functions is the unmodified string
1102 which the local user typed."
1103 :group 'erc-hooks
1104 :type 'hook)
1105 ;; mode-specific tables
1107 (defvar erc-mode-syntax-table
1108 (let ((syntax-table (make-syntax-table)))
1109 (modify-syntax-entry ?\" ". " syntax-table)
1110 (modify-syntax-entry ?\\ ". " syntax-table)
1111 (modify-syntax-entry ?' "w " syntax-table)
1112 ;; Make dabbrev-expand useful for nick names
1113 (modify-syntax-entry ?< "." syntax-table)
1114 (modify-syntax-entry ?> "." syntax-table)
1115 syntax-table)
1116 "Syntax table used while in ERC mode.")
1118 (defvar erc-mode-abbrev-table nil
1119 "Abbrev table used while in ERC mode.")
1120 (define-abbrev-table 'erc-mode-abbrev-table ())
1122 (defvar erc-mode-map
1123 (let ((map (make-sparse-keymap)))
1124 (define-key map "\C-m" 'erc-send-current-line)
1125 (define-key map "\C-a" 'erc-bol)
1126 (define-key map [home] 'erc-bol)
1127 (define-key map "\C-c\C-a" 'erc-bol)
1128 (define-key map "\C-c\C-b" 'erc-switch-to-buffer)
1129 (define-key map "\C-c\C-c" 'erc-toggle-interpret-controls)
1130 (define-key map "\C-c\C-d" 'erc-input-action)
1131 (define-key map "\C-c\C-e" 'erc-toggle-ctcp-autoresponse)
1132 (define-key map "\C-c\C-f" 'erc-toggle-flood-control)
1133 (define-key map "\C-c\C-i" 'erc-invite-only-mode)
1134 (define-key map "\C-c\C-j" 'erc-join-channel)
1135 (define-key map "\C-c\C-n" 'erc-channel-names)
1136 (define-key map "\C-c\C-o" 'erc-get-channel-mode-from-keypress)
1137 (define-key map "\C-c\C-p" 'erc-part-from-channel)
1138 (define-key map "\C-c\C-q" 'erc-quit-server)
1139 (define-key map "\C-c\C-r" 'erc-remove-text-properties-region)
1140 (define-key map "\C-c\C-t" 'erc-set-topic)
1141 (define-key map "\C-c\C-u" 'erc-kill-input)
1142 (define-key map "\C-c\C-x" 'erc-quit-server)
1143 (define-key map "\M-\t" 'ispell-complete-word)
1144 (define-key map "\t" 'completion-at-point)
1146 ;; Suppress `font-lock-fontify-block' key binding since it
1147 ;; destroys face properties.
1148 (define-key map [remap font-lock-fontify-block] 'undefined)
1150 map)
1151 "ERC keymap.")
1153 ;; Faces
1155 ; Honestly, I have a horrible sense of color and the "defaults" below
1156 ; are supposed to be really bad. But colors ARE required in IRC to
1157 ; convey different parts of conversation. If you think you know better
1158 ; defaults - send them to me.
1160 ;; Now colors are a bit nicer, at least to my eyes.
1161 ;; You may still want to change them to better fit your background.-- S.B.
1163 (defgroup erc-faces nil
1164 "Faces for ERC."
1165 :group 'erc)
1167 ;; FIXME faces should not end in "-face".
1168 (defface erc-default-face '((t))
1169 "ERC default face."
1170 :group 'erc-faces)
1172 (defface erc-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
1173 "ERC face used for user mode prefix."
1174 :group 'erc-faces)
1176 (defface erc-my-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
1177 "ERC face used for my user mode prefix."
1178 :group 'erc-faces)
1180 (defface erc-direct-msg-face '((t :foreground "IndianRed"))
1181 "ERC face used for messages you receive in the main erc buffer."
1182 :group 'erc-faces)
1184 (defface erc-header-line
1185 '((t :foreground "grey20" :background "grey90"))
1186 "ERC face used for the header line.
1188 This will only be used if `erc-header-line-face-method' is non-nil."
1189 :group 'erc-faces)
1191 (defface erc-input-face '((t :foreground "brown"))
1192 "ERC face used for your input."
1193 :group 'erc-faces)
1195 (defface erc-prompt-face
1196 '((t :weight bold :foreground "Black" :background "lightBlue2"))
1197 "ERC face for the prompt."
1198 :group 'erc-faces)
1200 (defface erc-command-indicator-face
1201 '((t :weight bold))
1202 "ERC face for the command indicator.
1203 See the variable `erc-command-indicator'."
1204 :group 'erc-faces)
1206 (defface erc-notice-face
1207 '((default :weight bold)
1208 (((class color) (min-colors 88)) :foreground "SlateBlue")
1209 (t :foreground "blue"))
1210 "ERC face for notices."
1211 :group 'erc-faces)
1213 (defface erc-action-face '((t :weight bold))
1214 "ERC face for actions generated by /ME."
1215 :group 'erc-faces)
1217 (defface erc-error-face '((t :foreground "red"))
1218 "ERC face for errors."
1219 :group 'erc-faces)
1221 ;; same default color as `erc-input-face'
1222 (defface erc-my-nick-face '((t :weight bold :foreground "brown"))
1223 "ERC face for your current nickname in messages sent by you.
1224 See also `erc-show-my-nick'."
1225 :group 'erc-faces)
1227 (defface erc-nick-default-face '((t :weight bold))
1228 "ERC nickname default face."
1229 :group 'erc-faces)
1231 (defface erc-nick-msg-face '((t :weight bold :foreground "IndianRed"))
1232 "ERC nickname face for private messages."
1233 :group 'erc-faces)
1235 ;; Debugging support
1237 (defvar erc-log-p nil
1238 "When set to t, generate debug messages in a separate debug buffer.")
1240 (defvar erc-debug-log-file (expand-file-name "ERC.debug")
1241 "Debug log file name.")
1243 (defvar erc-dbuf nil)
1244 (make-variable-buffer-local 'erc-dbuf)
1246 (defmacro define-erc-module (name alias doc enable-body disable-body
1247 &optional local-p)
1248 "Define a new minor mode using ERC conventions.
1249 Symbol NAME is the name of the module.
1250 Symbol ALIAS is the alias to use, or nil.
1251 DOC is the documentation string to use for the minor mode.
1252 ENABLE-BODY is a list of expressions used to enable the mode.
1253 DISABLE-BODY is a list of expressions used to disable the mode.
1254 If LOCAL-P is non-nil, the mode will be created as a buffer-local
1255 mode, rather than a global one.
1257 This will define a minor mode called erc-NAME-mode, possibly
1258 an alias erc-ALIAS-mode, as well as the helper functions
1259 erc-NAME-enable, and erc-NAME-disable.
1261 Example:
1263 ;;;###autoload (autoload \\='erc-replace-mode \"erc-replace\")
1264 (define-erc-module replace nil
1265 \"This mode replaces incoming text according to `erc-replace-alist'.\"
1266 ((add-hook \\='erc-insert-modify-hook
1267 \\='erc-replace-insert))
1268 ((remove-hook \\='erc-insert-modify-hook
1269 \\='erc-replace-insert)))"
1270 (declare (doc-string 3))
1271 (let* ((sn (symbol-name name))
1272 (mode (intern (format "erc-%s-mode" (downcase sn))))
1273 (group (intern (format "erc-%s" (downcase sn))))
1274 (enable (intern (format "erc-%s-enable" (downcase sn))))
1275 (disable (intern (format "erc-%s-disable" (downcase sn)))))
1276 `(progn
1277 (erc-define-minor-mode
1278 ,mode
1279 ,(format "Toggle ERC %S mode.
1280 With a prefix argument ARG, enable %s if ARG is positive,
1281 and disable it otherwise. If called from Lisp, enable the mode
1282 if ARG is omitted or nil.
1283 %s" name name doc)
1284 nil nil nil
1285 :global ,(not local-p) :group (quote ,group)
1286 (if ,mode
1287 (,enable)
1288 (,disable)))
1289 (defun ,enable ()
1290 ,(format "Enable ERC %S mode."
1291 name)
1292 (interactive)
1293 (add-to-list 'erc-modules (quote ,name))
1294 (setq ,mode t)
1295 ,@enable-body)
1296 (defun ,disable ()
1297 ,(format "Disable ERC %S mode."
1298 name)
1299 (interactive)
1300 (setq erc-modules (delq (quote ,name) erc-modules))
1301 (setq ,mode nil)
1302 ,@disable-body)
1303 ,(when (and alias (not (eq name alias)))
1304 `(defalias
1305 (quote
1306 ,(intern
1307 (format "erc-%s-mode"
1308 (downcase (symbol-name alias)))))
1309 (quote
1310 ,mode)))
1311 ;; For find-function and find-variable.
1312 (put ',mode 'definition-name ',name)
1313 (put ',enable 'definition-name ',name)
1314 (put ',disable 'definition-name ',name))))
1316 (defun erc-once-with-server-event (event f)
1317 "Run function F the next time EVENT occurs in the `current-buffer'.
1319 You should make sure that `current-buffer' is a server buffer.
1321 This function temporarily adds a function to EVENT's hook to call F with
1322 two arguments (`proc' and `parsed'). After F is called, the function is
1323 removed from EVENT's hook. F should return either nil
1324 or t, where nil indicates that the other functions on EVENT's hook
1325 should be run too, and t indicates that other functions should
1326 not be run.
1328 Please be sure to use this function in server-buffers. In
1329 channel-buffers it may not work at all, as it uses the LOCAL
1330 argument of `add-hook' and `remove-hook' to ensure multiserver
1331 capabilities."
1332 (unless (erc-server-buffer-p)
1333 (error
1334 "You should only run `erc-once-with-server-event' in a server buffer"))
1335 (let ((fun (make-symbol "fun"))
1336 (hook (erc-get-hook event)))
1337 (put fun 'erc-original-buffer (current-buffer))
1338 (fset fun (lambda (proc parsed)
1339 (with-current-buffer (get fun 'erc-original-buffer)
1340 (remove-hook hook fun t))
1341 (fmakunbound fun)
1342 (funcall f proc parsed)))
1343 (add-hook hook fun nil t)
1344 fun))
1346 (defsubst erc-log (string)
1347 "Logs STRING if logging is on (see `erc-log-p')."
1348 (when erc-log-p
1349 (erc-log-aux string)))
1351 (defun erc-server-buffer ()
1352 "Return the server buffer for the current buffer's process.
1353 The buffer-local variable `erc-server-process' is used to find
1354 the process buffer."
1355 (and (erc-server-buffer-live-p)
1356 (process-buffer erc-server-process)))
1358 (defun erc-server-buffer-live-p ()
1359 "Return t if the server buffer has not been killed."
1360 (and (processp erc-server-process)
1361 (buffer-live-p (process-buffer erc-server-process))))
1363 (defun erc-server-buffer-p (&optional buffer)
1364 "Return non-nil if argument BUFFER is an ERC server buffer.
1366 If BUFFER is nil, the current buffer is used."
1367 (with-current-buffer (or buffer (current-buffer))
1368 (and (eq major-mode 'erc-mode)
1369 (null (erc-default-target)))))
1371 (defun erc-open-server-buffer-p (&optional buffer)
1372 "Return non-nil if argument BUFFER is an ERC server buffer that
1373 has an open IRC process.
1375 If BUFFER is nil, the current buffer is used."
1376 (and (erc-server-buffer-p buffer)
1377 (erc-server-process-alive buffer)))
1379 (defun erc-query-buffer-p (&optional buffer)
1380 "Return non-nil if BUFFER is an ERC query buffer.
1381 If BUFFER is nil, the current buffer is used."
1382 (with-current-buffer (or buffer (current-buffer))
1383 (let ((target (erc-default-target)))
1384 (and (eq major-mode 'erc-mode)
1385 target
1386 (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
1388 (defun erc-ison-p (nick)
1389 "Return non-nil if NICK is online."
1390 (interactive "sNick: ")
1391 (erc-with-server-buffer
1392 (let ((erc-online-p 'unknown))
1393 (erc-once-with-server-event
1395 (lambda (_proc parsed)
1396 (let ((ison (split-string (aref parsed 3))))
1397 (setq erc-online-p (car (erc-member-ignore-case nick ison)))
1398 t)))
1399 (erc-server-send (format "ISON %s" nick))
1400 (while (eq erc-online-p 'unknown) (accept-process-output))
1401 (if (called-interactively-p 'interactive)
1402 (message "%s is %sonline"
1403 (or erc-online-p nick)
1404 (if erc-online-p "" "not "))
1405 erc-online-p))))
1407 (defun erc-log-aux (string)
1408 "Do the debug logging of STRING."
1409 (let ((cb (current-buffer))
1410 (point 1)
1411 (was-eob nil)
1412 (session-buffer (erc-server-buffer)))
1413 (if session-buffer
1414 (progn
1415 (set-buffer session-buffer)
1416 (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
1417 (progn
1418 (setq erc-dbuf (get-buffer-create
1419 (concat "*ERC-DEBUG: "
1420 erc-session-server "*")))))
1421 (set-buffer erc-dbuf)
1422 (setq point (point))
1423 (setq was-eob (eobp))
1424 (goto-char (point-max))
1425 (insert (concat "** " string "\n"))
1426 (if was-eob (goto-char (point-max))
1427 (goto-char point))
1428 (set-buffer cb))
1429 (message "ERC: ** %s" string))))
1431 ;; Last active buffer, to print server messages in the right place
1433 (defvar erc-active-buffer nil
1434 "The current active buffer, the one where the user typed the last command.
1435 Defaults to the server buffer, and should only be set in the
1436 server buffer.")
1437 (make-variable-buffer-local 'erc-active-buffer)
1439 (defun erc-active-buffer ()
1440 "Return the value of `erc-active-buffer' for the current server.
1441 Defaults to the server buffer."
1442 (erc-with-server-buffer
1443 (if (buffer-live-p erc-active-buffer)
1444 erc-active-buffer
1445 (setq erc-active-buffer (current-buffer)))))
1447 (defun erc-set-active-buffer (buffer)
1448 "Set the value of `erc-active-buffer' to BUFFER."
1449 (cond ((erc-server-buffer)
1450 (with-current-buffer (erc-server-buffer)
1451 (setq erc-active-buffer buffer)))
1452 (t (setq erc-active-buffer buffer))))
1454 ;; Mode activation routines
1456 (define-derived-mode erc-mode fundamental-mode "ERC"
1457 "Major mode for Emacs IRC."
1458 (setq local-abbrev-table erc-mode-abbrev-table)
1459 (when (boundp 'next-line-add-newlines)
1460 (set (make-local-variable 'next-line-add-newlines) nil))
1461 (setq line-move-ignore-invisible t)
1462 (set (make-local-variable 'paragraph-separate)
1463 (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
1464 (set (make-local-variable 'paragraph-start)
1465 (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
1466 (setq-local completion-ignore-case t)
1467 (add-hook 'completion-at-point-functions 'erc-complete-word-at-point nil t))
1469 ;; activation
1471 (defconst erc-default-server "irc.freenode.net"
1472 "IRC server to use if it cannot be detected otherwise.")
1474 (defconst erc-default-port 6667
1475 "IRC port to use if it cannot be detected otherwise.")
1477 (defconst erc-default-port-tls 6697
1478 "IRC port to use for encrypted connections if it cannot be
1479 detected otherwise.")
1481 (defcustom erc-join-buffer 'buffer
1482 "Determines how to display a newly created IRC buffer.
1484 The available choices are:
1486 `window' - in another window,
1487 `window-noselect' - in another window, but don't select that one,
1488 `frame' - in another frame,
1489 `bury' - bury it in a new buffer,
1490 `buffer' - in place of the current buffer,
1491 any other value - in place of the current buffer."
1492 :group 'erc-buffers
1493 :type '(choice (const :tag "Split window and select" window)
1494 (const :tag "Split window, don't select" window-noselect)
1495 (const :tag "New frame" frame)
1496 (const :tag "Bury in new buffer" bury)
1497 (const :tag "Use current buffer" buffer)
1498 (const :tag "Use current buffer" t)))
1500 (defcustom erc-frame-alist nil
1501 "Alist of frame parameters for creating erc frames.
1502 A value of nil means to use `default-frame-alist'."
1503 :group 'erc-buffers
1504 :type '(repeat (cons :format "%v"
1505 (symbol :tag "Parameter")
1506 (sexp :tag "Value"))))
1508 (defcustom erc-frame-dedicated-flag nil
1509 "Non-nil means the erc frames are dedicated to that buffer.
1510 This only has effect when `erc-join-buffer' is set to `frame'."
1511 :group 'erc-buffers
1512 :type 'boolean)
1514 (defcustom erc-reuse-frames t
1515 "Determines whether new frames are always created.
1516 Non-nil means that a new frame is not created to display an ERC
1517 buffer if there is already a window displaying it. This only has
1518 effect when `erc-join-buffer' is set to `frame'."
1519 :group 'erc-buffers
1520 :type 'boolean)
1522 (defun erc-channel-p (channel)
1523 "Return non-nil if CHANNEL seems to be an IRC channel name."
1524 (cond ((stringp channel)
1525 (memq (aref channel 0) '(?# ?& ?+ ?!)))
1526 ((and (bufferp channel) (buffer-live-p channel))
1527 (with-current-buffer channel
1528 (erc-channel-p (erc-default-target))))
1529 (t nil)))
1531 (defcustom erc-reuse-buffers t
1532 "If nil, create new buffers on joining a channel/query.
1533 If non-nil, a new buffer will only be created when you join
1534 channels with same names on different servers, or have query buffers
1535 open with nicks of the same name on different servers. Otherwise,
1536 the existing buffers will be reused."
1537 :group 'erc-buffers
1538 :type 'boolean)
1540 (defun erc-normalize-port (port)
1541 "Normalize the port specification PORT to integer form.
1542 PORT may be an integer, a string or a symbol. If it is a string or a
1543 symbol, it may have these values:
1544 * irc -> 194
1545 * ircs -> 994
1546 * ircd -> 6667
1547 * ircd-dalnet -> 7000"
1548 (cond
1549 ((symbolp port)
1550 (erc-normalize-port (symbol-name port)))
1551 ((stringp port)
1552 (let ((port-nr (string-to-number port)))
1553 (cond
1554 ((> port-nr 0)
1555 port-nr)
1556 ((string-equal port "irc")
1557 194)
1558 ((string-equal port "ircs")
1559 994)
1560 ((string-equal port "ircd")
1561 6667)
1562 ((string-equal port "ircd-dalnet")
1563 7000)
1565 nil))))
1566 ((numberp port)
1567 port)
1569 nil)))
1571 (defun erc-port-equal (a b)
1572 "Check whether ports A and B are equal."
1573 (= (erc-normalize-port a) (erc-normalize-port b)))
1575 (defun erc-generate-new-buffer-name (server port target)
1576 "Create a new buffer name based on the arguments."
1577 (when (numberp port) (setq port (number-to-string port)))
1578 (let ((buf-name (or target
1579 (or (let ((name (concat server ":" port)))
1580 (when (> (length name) 1)
1581 name))
1582 ;; This fallback should in fact never happen
1583 "*erc-server-buffer*")))
1584 buffer-name)
1585 ;; Reuse existing buffers, but not if the buffer is a connected server
1586 ;; buffer and not if its associated with a different server than the
1587 ;; current ERC buffer.
1588 ;; if buf-name is taken by a different connection (or by something !erc)
1589 ;; then see if "buf-name/server" meets the same criteria
1590 (dolist (candidate (list buf-name (concat buf-name "/" server)))
1591 (if (and (not buffer-name)
1592 erc-reuse-buffers
1593 (get-buffer candidate)
1594 (or target
1595 (with-current-buffer (get-buffer candidate)
1596 (and (erc-server-buffer-p)
1597 (not (erc-server-process-alive)))))
1598 (with-current-buffer (get-buffer candidate)
1599 (and (string= erc-session-server server)
1600 (erc-port-equal erc-session-port port))))
1601 (setq buffer-name candidate)))
1602 ;; if buffer-name is unset, neither candidate worked out for us,
1603 ;; fallback to the old <N> uniquification method:
1604 (or buffer-name (generate-new-buffer-name buf-name)) ))
1606 (defun erc-get-buffer-create (server port target)
1607 "Create a new buffer based on the arguments."
1608 (get-buffer-create (erc-generate-new-buffer-name server port target)))
1611 (defun erc-member-ignore-case (string list)
1612 "Return non-nil if STRING is a member of LIST.
1614 All strings are compared according to IRC protocol case rules, see
1615 `erc-downcase'."
1616 (setq string (erc-downcase string))
1617 (catch 'result
1618 (while list
1619 (if (string= string (erc-downcase (car list)))
1620 (throw 'result list)
1621 (setq list (cdr list))))))
1623 (defmacro erc-with-buffer (spec &rest body)
1624 "Execute BODY in the buffer associated with SPEC.
1626 SPEC should have the form
1628 (TARGET [PROCESS])
1630 If TARGET is a buffer, use it. Otherwise, use the buffer
1631 matching TARGET in the process specified by PROCESS.
1633 If PROCESS is nil, use the current `erc-server-process'.
1634 See `erc-get-buffer' for details.
1636 See also `with-current-buffer'.
1638 \(fn (TARGET [PROCESS]) BODY...)"
1639 (declare (indent 1) (debug ((form &optional form) body)))
1640 (let ((buf (make-symbol "buf"))
1641 (proc (make-symbol "proc"))
1642 (target (make-symbol "target"))
1643 (process (make-symbol "process")))
1644 `(let* ((,target ,(car spec))
1645 (,process ,(cadr spec))
1646 (,buf (if (bufferp ,target)
1647 ,target
1648 (let ((,proc (or ,process
1649 (and (processp erc-server-process)
1650 erc-server-process))))
1651 (if (and ,target ,proc)
1652 (erc-get-buffer ,target ,proc))))))
1653 (when (buffer-live-p ,buf)
1654 (with-current-buffer ,buf
1655 ,@body)))))
1657 (defun erc-get-buffer (target &optional proc)
1658 "Return the buffer matching TARGET in the process PROC.
1659 If PROC is not supplied, all processes are searched."
1660 (let ((downcased-target (erc-downcase target)))
1661 (catch 'buffer
1662 (erc-buffer-filter
1663 (lambda ()
1664 (let ((current (erc-default-target)))
1665 (and (stringp current)
1666 (string-equal downcased-target (erc-downcase current))
1667 (throw 'buffer (current-buffer)))))
1668 proc))))
1670 (defun erc--buffer-p (buf predicate proc)
1671 (with-current-buffer buf
1672 (and (derived-mode-p 'erc-mode)
1673 (or (not proc)
1674 (eq proc erc-server-process))
1675 (funcall predicate)
1676 buf)))
1678 (defun erc-buffer-filter (predicate &optional proc)
1679 "Return a list of `erc-mode' buffers matching certain criteria.
1680 PREDICATE is a function executed with each buffer, if it returns t, that buffer
1681 is considered a valid match.
1683 PROC is either an `erc-server-process', identifying a certain
1684 server connection, or nil which means all open connections."
1685 (save-excursion
1686 (delq
1688 (mapcar (lambda (buf)
1689 (when (buffer-live-p buf)
1690 (erc--buffer-p buf predicate proc)))
1691 (buffer-list)))))
1693 (defun erc-buffer-list (&optional predicate proc)
1694 "Return a list of ERC buffers.
1695 PREDICATE is a function which executes with every buffer satisfying
1696 the predicate. If PREDICATE is passed as nil, return a list of all ERC
1697 buffers. If PROC is given, the buffers local variable `erc-server-process'
1698 needs to match PROC."
1699 (unless predicate
1700 (setq predicate (lambda () t)))
1701 (erc-buffer-filter predicate proc))
1703 (defmacro erc-with-all-buffers-of-server (process pred &rest forms)
1704 "Execute FORMS in all buffers which have same process as this server.
1705 FORMS will be evaluated in all buffers having the process PROCESS and
1706 where PRED matches or in all buffers of the server process if PRED is
1707 nil."
1708 (declare (indent 1) (debug (form form body)))
1709 ;; Make the evaluation have the correct order
1710 (let ((pre (make-symbol "pre"))
1711 (pro (make-symbol "pro")))
1712 `(let* ((,pro ,process)
1713 (,pre ,pred)
1714 (res (mapcar (lambda (buffer)
1715 (with-current-buffer buffer
1716 ,@forms))
1717 (erc-buffer-list ,pre
1718 ,pro))))
1719 ;; Silence the byte-compiler by binding the result of mapcar to
1720 ;; a variable.
1721 (ignore res)
1722 res)))
1724 (define-obsolete-function-alias 'erc-iswitchb 'erc-switch-to-buffer "25.1")
1725 (defun erc-switch-to-buffer (&optional arg)
1726 "Prompt for a ERC buffer to switch to.
1727 When invoked with prefix argument, use all erc buffers. Without prefix
1728 ARG, allow only buffers related to same session server.
1729 If `erc-track-mode' is in enabled, put the last element of
1730 `erc-modified-channels-alist' in front of the buffer list."
1731 (interactive "P")
1732 (switch-to-buffer
1733 (read-buffer "Switch to ERC buffer: "
1734 (when (boundp 'erc-modified-channels-alist)
1735 (buffer-name (caar (last erc-modified-channels-alist))))
1737 ;; Only allow ERC buffers in the same session.
1738 (let ((proc (unless arg erc-server-process)))
1739 (lambda (bufname)
1740 (let ((buf (if (consp bufname)
1741 (cdr bufname) (get-buffer bufname))))
1742 (when buf
1743 (erc--buffer-p buf (lambda () t) proc)
1744 (with-current-buffer buf
1745 (and (derived-mode-p 'erc-mode)
1746 (or (null proc)
1747 (eq proc erc-server-process)))))))))))
1749 (defun erc-channel-list (proc)
1750 "Return a list of channel buffers.
1751 PROC is the process for the server connection. If PROC is nil, return
1752 all channel buffers on all servers."
1753 (erc-buffer-filter
1754 (lambda ()
1755 (and (erc-default-target)
1756 (erc-channel-p (erc-default-target))))
1757 proc))
1759 (defun erc-buffer-list-with-nick (nick proc)
1760 "Return buffers containing NICK in the `erc-channel-users' list."
1761 (with-current-buffer (process-buffer proc)
1762 (let ((user (gethash (erc-downcase nick) erc-server-users)))
1763 (if user
1764 (erc-server-user-buffers user)
1765 nil))))
1767 ;; Some local variables
1769 (defvar erc-default-recipients nil
1770 "List of default recipients of the current buffer.")
1771 (make-variable-buffer-local 'erc-default-recipients)
1773 (defvar erc-session-user-full-name nil
1774 "Full name of the user on the current server.")
1775 (make-variable-buffer-local 'erc-session-user-full-name)
1777 (defvar erc-channel-user-limit nil
1778 "Limit of users per channel.")
1779 (make-variable-buffer-local 'erc-channel-user-limit)
1781 (defvar erc-channel-key nil
1782 "Key needed to join channel.")
1783 (make-variable-buffer-local 'erc-channel-key)
1785 (defvar erc-invitation nil
1786 "Last invitation channel.")
1787 (make-variable-buffer-local 'erc-invitation)
1789 (defvar erc-away nil
1790 "Non-nil indicates that we are away.
1792 Use `erc-away-time' to access this if you might be in a channel
1793 buffer rather than a server buffer.")
1794 (make-variable-buffer-local 'erc-away)
1796 (defvar erc-channel-list nil
1797 "Server channel list.")
1798 (make-variable-buffer-local 'erc-channel-list)
1800 (defvar erc-bad-nick nil
1801 "Non-nil indicates that we got a `nick in use' error while connecting.")
1802 (make-variable-buffer-local 'erc-bad-nick)
1804 (defvar erc-logged-in nil
1805 "Non-nil indicates that we are logged in.")
1806 (make-variable-buffer-local 'erc-logged-in)
1808 (defvar erc-default-nicks nil
1809 "The local copy of `erc-nick' - the list of nicks to choose from.")
1810 (make-variable-buffer-local 'erc-default-nicks)
1812 (defvar erc-nick-change-attempt-count 0
1813 "Used to keep track of how many times an attempt at changing nick is made.")
1814 (make-variable-buffer-local 'erc-nick-change-attempt-count)
1816 (defun erc-migrate-modules (mods)
1817 "Migrate old names of ERC modules to new ones."
1818 ;; modify `transforms' to specify what needs to be changed
1819 ;; each item is in the format '(old . new)
1820 (let ((transforms '((pcomplete . completion))))
1821 (erc-delete-dups
1822 (mapcar (lambda (m) (or (cdr (assoc m transforms)) m))
1823 mods))))
1825 (defcustom erc-modules '(netsplit fill button match track completion readonly
1826 networks ring autojoin noncommands irccontrols
1827 move-to-prompt stamp menu list)
1828 "A list of modules which ERC should enable.
1829 If you set the value of this without using `customize' remember to call
1830 \(erc-update-modules) after you change it. When using `customize', modules
1831 removed from the list will be disabled."
1832 :get (lambda (sym)
1833 ;; replace outdated names with their newer equivalents
1834 (erc-migrate-modules (symbol-value sym)))
1835 :set (lambda (sym val)
1836 ;; disable modules which have just been removed
1837 (when (and (boundp 'erc-modules) erc-modules val)
1838 (dolist (module erc-modules)
1839 (unless (member module val)
1840 (let ((f (intern-soft (format "erc-%s-mode" module))))
1841 (when (and (fboundp f) (boundp f) (symbol-value f))
1842 (message "Disabling `erc-%s'" module)
1843 (funcall f 0))))))
1844 (set sym val)
1845 ;; this test is for the case where erc hasn't been loaded yet
1846 (when (fboundp 'erc-update-modules)
1847 (erc-update-modules)))
1848 :type
1849 '(set
1850 :greedy t
1851 (const :tag "autoaway: Set away status automatically" autoaway)
1852 (const :tag "autojoin: Join channels automatically" autojoin)
1853 (const :tag "button: Buttonize URLs, nicknames, and other text" button)
1854 (const :tag "capab: Mark unidentified users on servers supporting CAPAB"
1855 capab-identify)
1856 (const :tag "completion: Complete nicknames and commands (programmable)"
1857 completion)
1858 (const :tag "hecomplete: Complete nicknames and commands (obsolete, use \"completion\")" hecomplete)
1859 (const :tag "dcc: Provide Direct Client-to-Client support" dcc)
1860 (const :tag "fill: Wrap long lines" fill)
1861 (const :tag "identd: Launch an identd server on port 8113" identd)
1862 (const :tag "irccontrols: Highlight or remove IRC control characters"
1863 irccontrols)
1864 (const :tag "keep-place: Leave point above un-viewed text" keep-place)
1865 (const :tag "list: List channels in a separate buffer" list)
1866 (const :tag "log: Save buffers in logs" log)
1867 (const :tag "match: Highlight pals, fools, and other keywords" match)
1868 (const :tag "menu: Display a menu in ERC buffers" menu)
1869 (const :tag "move-to-prompt: Move to the prompt when typing text"
1870 move-to-prompt)
1871 (const :tag "netsplit: Detect netsplits" netsplit)
1872 (const :tag "networks: Provide data about IRC networks" networks)
1873 (const :tag "noncommands: Don't display non-IRC commands after evaluation"
1874 noncommands)
1875 (const :tag
1876 "notify: Notify when the online status of certain users changes"
1877 notify)
1878 (const :tag "notifications: Send notifications on PRIVMSG or nickname mentions"
1879 notifications)
1880 (const :tag "page: Process CTCP PAGE requests from IRC" page)
1881 (const :tag "readonly: Make displayed lines read-only" readonly)
1882 (const :tag "replace: Replace text in messages" replace)
1883 (const :tag "ring: Enable an input history" ring)
1884 (const :tag "scrolltobottom: Scroll to the bottom of the buffer"
1885 scrolltobottom)
1886 (const :tag "services: Identify to Nickserv (IRC Services) automatically"
1887 services)
1888 (const :tag "smiley: Convert smileys to pretty icons" smiley)
1889 (const :tag "sound: Play sounds when you receive CTCP SOUND requests"
1890 sound)
1891 (const :tag "stamp: Add timestamps to messages" stamp)
1892 (const :tag "spelling: Check spelling" spelling)
1893 (const :tag "track: Track channel activity in the mode-line" track)
1894 (const :tag "truncate: Truncate buffers to a certain size" truncate)
1895 (const :tag "unmorse: Translate morse code in messages" unmorse)
1896 (const :tag "xdcc: Act as an XDCC file-server" xdcc)
1897 (repeat :tag "Others" :inline t symbol))
1898 :group 'erc)
1900 (defun erc-update-modules ()
1901 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1902 (let (req)
1903 (dolist (mod erc-modules)
1904 (setq req (concat "erc-" (symbol-name mod)))
1905 (cond
1906 ;; yuck. perhaps we should bring the filenames into sync?
1907 ((string= req "erc-capab-identify")
1908 (setq req "erc-capab"))
1909 ((string= req "erc-completion")
1910 (setq req "erc-pcomplete"))
1911 ((string= req "erc-pcomplete")
1912 (setq mod 'completion))
1913 ((string= req "erc-autojoin")
1914 (setq req "erc-join")))
1915 (condition-case nil
1916 (require (intern req))
1917 (error nil))
1918 (let ((sym (intern-soft (concat "erc-" (symbol-name mod) "-mode"))))
1919 (if (fboundp sym)
1920 (funcall sym 1)
1921 (error "`%s' is not a known ERC module" mod))))))
1923 (defun erc-setup-buffer (buffer)
1924 "Consults `erc-join-buffer' to find out how to display `BUFFER'."
1925 (pcase erc-join-buffer
1926 (`window
1927 (if (active-minibuffer-window)
1928 (display-buffer buffer)
1929 (switch-to-buffer-other-window buffer)))
1930 (`window-noselect
1931 (display-buffer buffer))
1932 (`bury
1933 nil)
1934 (`frame
1935 (when (or (not erc-reuse-frames)
1936 (not (get-buffer-window buffer t)))
1937 (let ((frame (make-frame (or erc-frame-alist
1938 default-frame-alist))))
1939 (raise-frame frame)
1940 (select-frame frame))
1941 (switch-to-buffer buffer)
1942 (when erc-frame-dedicated-flag
1943 (set-window-dedicated-p (selected-window) t))))
1945 (if (active-minibuffer-window)
1946 (display-buffer buffer)
1947 (switch-to-buffer buffer)))))
1949 (defun erc-open (&optional server port nick full-name
1950 connect passwd tgt-list channel process)
1951 "Connect to SERVER on PORT as NICK with FULL-NAME.
1953 If CONNECT is non-nil, connect to the server. Otherwise assume
1954 already connected and just create a separate buffer for the new
1955 target CHANNEL.
1957 Use PASSWD as user password on the server. If TGT-LIST is
1958 non-nil, use it to initialize `erc-default-recipients'.
1960 Returns the buffer for the given server or channel."
1961 (let ((server-announced-name (when (and (boundp 'erc-session-server)
1962 (string= server erc-session-server))
1963 erc-server-announced-name))
1964 (connected-p (unless connect erc-server-connected))
1965 (buffer (erc-get-buffer-create server port channel))
1966 (old-buffer (current-buffer))
1967 old-point
1968 continued-session)
1969 (when connect (run-hook-with-args 'erc-before-connect server port nick))
1970 (erc-update-modules)
1971 (set-buffer buffer)
1972 (setq old-point (point))
1973 (let ((old-recon-count erc-server-reconnect-count))
1974 (erc-mode)
1975 (setq erc-server-reconnect-count old-recon-count))
1976 (setq erc-server-announced-name server-announced-name)
1977 (setq erc-server-connected connected-p)
1978 ;; connection parameters
1979 (setq erc-server-process process)
1980 (setq erc-insert-marker (make-marker))
1981 (setq erc-input-marker (make-marker))
1982 ;; go to the end of the buffer and open a new line
1983 ;; (the buffer may have existed)
1984 (goto-char (point-max))
1985 (forward-line 0)
1986 (when (get-text-property (point) 'erc-prompt)
1987 (setq continued-session t)
1988 (set-marker erc-input-marker
1989 (or (next-single-property-change (point) 'erc-prompt)
1990 (point-max))))
1991 (unless continued-session
1992 (goto-char (point-max))
1993 (insert "\n"))
1994 (set-marker erc-insert-marker (point))
1995 ;; stack of default recipients
1996 (setq erc-default-recipients tgt-list)
1997 (setq erc-server-current-nick nil)
1998 ;; Initialize erc-server-users and erc-channel-users
1999 (if connect
2000 (progn ;; server buffer
2001 (setq erc-server-users
2002 (make-hash-table :test 'equal))
2003 (setq erc-channel-users nil))
2004 (progn ;; target buffer
2005 (setq erc-server-users nil)
2006 (setq erc-channel-users
2007 (make-hash-table :test 'equal))))
2008 ;; clear last incomplete line read
2009 (setq erc-server-filter-data nil)
2010 (setq erc-channel-topic "")
2011 ;; limit on the number of users on the channel (mode +l)
2012 (setq erc-channel-user-limit nil)
2013 (setq erc-channel-key nil)
2014 ;; last active buffer, defaults to this one
2015 (erc-set-active-buffer buffer)
2016 ;; last invitation channel
2017 (setq erc-invitation nil)
2018 ;; Server channel list
2019 (setq erc-channel-list ())
2020 ;; login-time 'nick in use' error
2021 (setq erc-bad-nick nil)
2022 ;; whether we have logged in
2023 (setq erc-logged-in nil)
2024 ;; The local copy of `erc-nick' - the list of nicks to choose
2025 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
2026 ;; password stuff
2027 (setq erc-session-password
2028 (or passwd
2029 (let ((secret
2030 (plist-get
2031 (nth 0
2032 (auth-source-search :host server
2033 :max 1
2034 :user nick
2035 ;; secrets.el wouldn’t accept a number
2036 :port (if (numberp port) (number-to-string port) port)
2037 :require '(:secret)))
2038 :secret)))
2039 (if (functionp secret)
2040 (funcall secret)
2041 secret))))
2042 ;; debug output buffer
2043 (setq erc-dbuf
2044 (when erc-log-p
2045 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
2046 ;; set up prompt
2047 (unless continued-session
2048 (goto-char (point-max))
2049 (insert "\n"))
2050 (if continued-session
2051 (goto-char old-point)
2052 (set-marker erc-insert-marker (point))
2053 (erc-display-prompt)
2054 (goto-char (point-max)))
2056 (erc-determine-parameters server port nick full-name)
2058 ;; Saving log file on exit
2059 (run-hook-with-args 'erc-connect-pre-hook buffer)
2061 (when connect
2062 (erc-server-connect erc-session-server erc-session-port buffer))
2063 (erc-update-mode-line)
2065 ;; Now display the buffer in a window as per user wishes.
2066 (unless (eq buffer old-buffer)
2067 (when erc-log-p
2068 ;; we can't log to debug buffer, it may not exist yet
2069 (message "erc: old buffer %s, switching to %s"
2070 old-buffer buffer))
2071 (erc-setup-buffer buffer))
2073 buffer))
2075 (defun erc-initialize-log-marker (buffer)
2076 "Initialize the `erc-last-saved-position' marker to a sensible position.
2077 BUFFER is the current buffer."
2078 (with-current-buffer buffer
2079 (unless (markerp erc-last-saved-position)
2080 (setq erc-last-saved-position (make-marker))
2081 (move-marker erc-last-saved-position
2082 (1- (marker-position erc-insert-marker))))))
2084 ;; interactive startup
2086 (defvar erc-server-history-list nil
2087 "IRC server interactive selection history list.")
2089 (defvar erc-nick-history-list nil
2090 "Nickname interactive selection history list.")
2092 (defun erc-already-logged-in (server port nick)
2093 "Return the buffers corresponding to a NICK on PORT of a session SERVER.
2094 This is determined by looking for the appropriate buffer and checking
2095 whether the connection is still alive.
2096 If no buffer matches, return nil."
2097 (erc-buffer-list
2098 (lambda ()
2099 (and (erc-server-process-alive)
2100 (string= erc-session-server server)
2101 (erc-port-equal erc-session-port port)
2102 (erc-current-nick-p nick)))))
2104 (defcustom erc-before-connect nil
2105 "Hook called before connecting to a server.
2106 This hook gets executed before `erc' actually invokes `erc-mode'
2107 with your input data. The functions in here get called with three
2108 parameters, SERVER, PORT and NICK."
2109 :group 'erc-hooks
2110 :type 'hook)
2112 (defcustom erc-after-connect nil
2113 "Hook called after connecting to a server.
2114 This hook gets executed when an end of MOTD has been received. All
2115 functions in here get called with the parameters SERVER and NICK."
2116 :group 'erc-hooks
2117 :type 'hook)
2119 ;;;###autoload
2120 (defun erc-select-read-args ()
2121 "Prompt the user for values of nick, server, port, and password."
2122 (let (user-input server port nick passwd)
2123 (setq user-input (read-from-minibuffer
2124 "IRC server: "
2125 (erc-compute-server) nil nil 'erc-server-history-list))
2127 (if (string-match "\\(.*\\):\\(.*\\)\\'" user-input)
2128 (setq port (erc-string-to-port (match-string 2 user-input))
2129 user-input (match-string 1 user-input))
2130 (setq port
2131 (erc-string-to-port (read-from-minibuffer
2132 "IRC port: " (erc-port-to-string
2133 (erc-compute-port))))))
2135 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
2136 (setq nick (match-string 1 user-input)
2137 user-input (match-string 2 user-input))
2138 (setq nick
2139 (if (erc-already-logged-in server port nick)
2140 (read-from-minibuffer
2141 (erc-format-message 'nick-in-use ?n nick)
2142 nick
2143 nil nil 'erc-nick-history-list)
2144 (read-from-minibuffer
2145 "Nickname: " (erc-compute-nick nick)
2146 nil nil 'erc-nick-history-list))))
2148 (setq server user-input)
2150 (setq passwd (if erc-prompt-for-password
2151 (if (and erc-password
2152 (y-or-n-p "Use the default password? "))
2153 erc-password
2154 (read-passwd "Password: "))
2155 erc-password))
2156 (when (and passwd (string= "" passwd))
2157 (setq passwd nil))
2159 (while (erc-already-logged-in server port nick)
2160 ;; hmm, this is a problem when using multiple connections to a bnc
2161 ;; with the same nick. Currently this code prevents using more than one
2162 ;; bnc with the same nick. actually it would be nice to have
2163 ;; bncs transparent, so that erc-compute-buffer-name displays
2164 ;; the server one is connected to.
2165 (setq nick (read-from-minibuffer
2166 (erc-format-message 'nick-in-use ?n nick)
2167 nick
2168 nil nil 'erc-nick-history-list)))
2169 (list :server server :port port :nick nick :password passwd)))
2171 ;;;###autoload
2172 (cl-defun erc (&key (server (erc-compute-server))
2173 (port (erc-compute-port))
2174 (nick (erc-compute-nick))
2175 password
2176 (full-name (erc-compute-full-name)))
2177 "ERC is a powerful, modular, and extensible IRC client.
2178 This function is the main entry point for ERC.
2180 It permits you to select connection parameters, and then starts ERC.
2182 Non-interactively, it takes the keyword arguments
2183 (server (erc-compute-server))
2184 (port (erc-compute-port))
2185 (nick (erc-compute-nick))
2186 password
2187 (full-name (erc-compute-full-name)))
2189 That is, if called with
2191 (erc :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
2193 then the server and full-name will be set to those values, whereas
2194 `erc-compute-port', `erc-compute-nick' and `erc-compute-full-name' will
2195 be invoked for the values of the other parameters."
2196 (interactive (erc-select-read-args))
2197 (erc-open server port nick full-name t password))
2199 ;;;###autoload
2200 (defalias 'erc-select 'erc)
2201 (defalias 'erc-ssl 'erc-tls)
2203 ;;;###autoload
2204 (defun erc-tls (&rest r)
2205 "Interactively select TLS connection parameters and run ERC.
2206 Arguments are the same as for `erc'."
2207 (interactive (let ((erc-default-port erc-default-port-tls))
2208 (erc-select-read-args)))
2209 (let ((erc-server-connect-function 'erc-open-tls-stream))
2210 (apply #'erc r)))
2212 (defun erc-open-tls-stream (name buffer host port)
2213 "Open an TLS stream to an IRC server.
2214 The process will be given the name NAME, its target buffer will be
2215 BUFFER. HOST and PORT specify the connection target."
2216 (open-network-stream name buffer host port
2217 :nowait t
2218 :type 'tls))
2220 ;;; Displaying error messages
2222 (defun erc-error (&rest args)
2223 "Pass ARGS to `format', and display the result as an error message.
2224 If `debug-on-error' is set to non-nil, then throw a real error with this
2225 message instead, to make debugging easier."
2226 (if debug-on-error
2227 (apply #'error args)
2228 (apply #'message args)
2229 (beep)))
2231 ;;; Debugging the protocol
2233 (defvar erc-debug-irc-protocol nil
2234 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2236 The buffer is created if it doesn't exist.
2238 NOTE: If this variable is non-nil, and you kill the only
2239 visible \"*erc-protocol*\" buffer, it will be recreated shortly,
2240 but you won't see it.
2242 WARNING: Do not set this variable directly! Instead, use the
2243 function `erc-toggle-debug-irc-protocol' to toggle its value.")
2245 (declare-function erc-network-name "erc-networks" ())
2247 (defun erc-log-irc-protocol (string &optional outbound)
2248 "Append STRING to the buffer *erc-protocol*.
2250 This only has any effect if `erc-debug-irc-protocol' is non-nil.
2252 The buffer is created if it doesn't exist.
2254 If OUTBOUND is non-nil, STRING is being sent to the IRC server
2255 and appears in face `erc-input-face' in the buffer."
2256 (when erc-debug-irc-protocol
2257 (let ((network-name (or (ignore-errors (erc-network-name))
2258 "???")))
2259 (with-current-buffer (get-buffer-create "*erc-protocol*")
2260 (save-excursion
2261 (goto-char (point-max))
2262 (let ((inhibit-read-only t))
2263 (insert (if (not outbound)
2264 ;; Cope with the fact that string might
2265 ;; contain multiple lines of text.
2266 (let ((lines (delete "" (split-string string
2267 "\n\\|\r\n")))
2268 (result ""))
2269 (dolist (line lines)
2270 (setq result (concat result network-name
2271 " << " line "\n")))
2272 result)
2273 (erc-propertize
2274 (concat network-name " >> " string
2275 (if (/= ?\n
2276 (aref string
2277 (1- (length string))))
2278 "\n"))
2279 'font-lock-face 'erc-input-face)))))
2280 (let ((orig-win (selected-window))
2281 (debug-buffer-window (get-buffer-window (current-buffer) t)))
2282 (when debug-buffer-window
2283 (select-window debug-buffer-window)
2284 (when (= 1 (count-lines (point) (point-max)))
2285 (goto-char (point-max))
2286 (recenter -1))
2287 (select-window orig-win)))))))
2289 (defun erc-toggle-debug-irc-protocol (&optional arg)
2290 "Toggle the value of `erc-debug-irc-protocol'.
2292 If ARG is non-nil, show the *erc-protocol* buffer."
2293 (interactive "P")
2294 (let* ((buf (get-buffer-create "*erc-protocol*")))
2295 (with-current-buffer buf
2296 (erc-view-mode-enter)
2297 (when (null (current-local-map))
2298 (let ((inhibit-read-only t))
2299 (insert (erc-make-notice "This buffer displays all IRC protocol traffic exchanged with each server.\n"))
2300 (insert (erc-make-notice "Kill this buffer to terminate protocol logging.\n\n")))
2301 (use-local-map (make-sparse-keymap))
2302 (local-set-key (kbd "t") 'erc-toggle-debug-irc-protocol))
2303 (add-hook 'kill-buffer-hook
2304 #'(lambda () (setq erc-debug-irc-protocol nil))
2305 nil 'local)
2306 (goto-char (point-max))
2307 (let ((inhibit-read-only t))
2308 (insert (erc-make-notice
2309 (format "IRC protocol logging %s at %s -- Press `t' to toggle logging.\n"
2310 (if erc-debug-irc-protocol "disabled" "enabled")
2311 (current-time-string))))))
2312 (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
2313 (if (and arg
2314 (not (get-buffer-window "*erc-protocol*" t)))
2315 (display-buffer buf t))
2316 (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
2317 (if erc-debug-irc-protocol "enabled" "disabled"))))
2319 ;;; I/O interface
2321 ;; send interface
2323 (defun erc-send-action (tgt str &optional force)
2324 "Send CTCP ACTION information described by STR to TGT."
2325 (erc-send-ctcp-message tgt (format "ACTION %s" str) force)
2326 (erc-display-message
2327 nil 'input (current-buffer)
2328 'ACTION ?n (erc-current-nick) ?a str ?u "" ?h ""))
2330 ;; Display interface
2332 (defun erc-string-invisible-p (string)
2333 "Check whether STRING is invisible or not.
2334 I.e. any char in it has the `invisible' property set."
2335 (text-property-any 0 (length string) 'invisible t string))
2337 (defcustom erc-remove-parsed-property t
2338 "Whether to remove the erc-parsed text property after displaying a message.
2340 The default is to remove it, since it causes ERC to take up extra
2341 memory. If you have code that relies on this property, then set
2342 this option to nil."
2343 :type 'boolean
2344 :group 'erc)
2346 (defun erc-display-line-1 (string buffer)
2347 "Display STRING in `erc-mode' BUFFER.
2348 Auxiliary function used in `erc-display-line'. The line gets filtered to
2349 interpret the control characters. Then, `erc-insert-pre-hook' gets called.
2350 If `erc-insert-this' is still t, STRING gets inserted into the buffer.
2351 Afterwards, `erc-insert-modify' and `erc-insert-post-hook' get called.
2352 If STRING is nil, the function does nothing."
2353 (when string
2354 (with-current-buffer (or buffer (process-buffer erc-server-process))
2355 (let ((insert-position (or (marker-position erc-insert-marker)
2356 (point-max))))
2357 (let ((string string) ;; FIXME! Can this be removed?
2358 (buffer-undo-list t)
2359 (inhibit-read-only t))
2360 (unless (string-match "\n$" string)
2361 (setq string (concat string "\n"))
2362 (when (erc-string-invisible-p string)
2363 (erc-put-text-properties 0 (length string)
2364 '(invisible intangible) string)))
2365 (erc-log (concat "erc-display-line: " string
2366 (format "(%S)" string) " in buffer "
2367 (format "%s" buffer)))
2368 (setq erc-insert-this t)
2369 (run-hook-with-args 'erc-insert-pre-hook string)
2370 (if (null erc-insert-this)
2371 ;; Leave erc-insert-this set to t as much as possible. Fran
2372 ;; Litterio <franl> has seen erc-insert-this set to nil while
2373 ;; erc-send-pre-hook is running, which should never happen. This
2374 ;; may cure it.
2375 (setq erc-insert-this t)
2376 (save-excursion ;; to restore point in the new buffer
2377 (save-restriction
2378 (widen)
2379 (goto-char insert-position)
2380 (insert-before-markers string)
2381 ;; run insertion hook, with point at restored location
2382 (save-restriction
2383 (narrow-to-region insert-position (point))
2384 (run-hooks 'erc-insert-modify-hook)
2385 (run-hooks 'erc-insert-post-hook)
2386 (when erc-remove-parsed-property
2387 (remove-text-properties (point-min) (point-max)
2388 '(erc-parsed nil))))))))
2389 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2390 (point-max))
2391 insert-position))))))
2393 (defun erc-update-undo-list (shift)
2394 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2395 (unless (or (zerop shift) (atom buffer-undo-list))
2396 (let ((list buffer-undo-list) elt)
2397 (while list
2398 (setq elt (car list))
2399 (cond ((integerp elt) ; POSITION
2400 (cl-incf (car list) shift))
2401 ((or (atom elt) ; nil, EXTENT
2402 ;; (eq t (car elt)) ; (t . TIME)
2403 (markerp (car elt))) ; (MARKER . DISTANCE)
2404 nil)
2405 ((integerp (car elt)) ; (BEGIN . END)
2406 (cl-incf (car elt) shift)
2407 (cl-incf (cdr elt) shift))
2408 ((stringp (car elt)) ; (TEXT . POSITION)
2409 (cl-incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
2410 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2411 (let ((cons (nthcdr 3 elt)))
2412 (cl-incf (car cons) shift)
2413 (cl-incf (cdr cons) shift)))
2414 ((and (featurep 'xemacs)
2415 (extentp (car elt))) ; (EXTENT START END)
2416 (cl-incf (nth 1 elt) shift)
2417 (cl-incf (nth 2 elt) shift)))
2418 (setq list (cdr list))))))
2420 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2421 "Regexp which matches all valid characters in a IRC nickname.")
2423 (defun erc-is-valid-nick-p (nick)
2424 "Check if NICK is a valid IRC nickname."
2425 (string-match (concat "\\`" erc-valid-nick-regexp "\\'") nick))
2427 (defun erc-display-line (string &optional buffer)
2428 "Display STRING in the ERC BUFFER.
2429 All screen output must be done through this function. If BUFFER is nil
2430 or omitted, the default ERC buffer for the `erc-session-server' is used.
2431 The BUFFER can be an actual buffer, a list of buffers, `all' or `active'.
2432 If BUFFER = `all', the string is displayed in all the ERC buffers for the
2433 current session. `active' means the current active buffer
2434 \(`erc-active-buffer'). If the buffer can't be resolved, the current
2435 buffer is used. `erc-display-line-1' is used to display STRING.
2437 If STRING is nil, the function does nothing."
2438 (let ((inhibit-point-motion-hooks t)
2439 new-bufs)
2440 (dolist (buf (cond
2441 ((bufferp buffer) (list buffer))
2442 ((listp buffer) buffer)
2443 ((processp buffer) (list (process-buffer buffer)))
2444 ((eq 'all buffer)
2445 ;; Hmm, or all of the same session server?
2446 (erc-buffer-list nil erc-server-process))
2447 ((and (eq 'active buffer) (erc-active-buffer))
2448 (list (erc-active-buffer)))
2449 ((erc-server-buffer-live-p)
2450 (list (process-buffer erc-server-process)))
2451 (t (list (current-buffer)))))
2452 (when (buffer-live-p buf)
2453 (erc-display-line-1 string buf)
2454 (push buf new-bufs)))
2455 (when (null new-bufs)
2456 (erc-display-line-1 string (if (erc-server-buffer-live-p)
2457 (process-buffer erc-server-process)
2458 (current-buffer))))))
2460 (defun erc-display-message-highlight (type string)
2461 "Highlight STRING according to TYPE, where erc-TYPE-face is an ERC face.
2463 See also `erc-make-notice'."
2464 (cond ((eq type 'notice)
2465 (erc-make-notice string))
2467 (erc-put-text-property
2468 0 (length string)
2469 'font-lock-face (or (intern-soft
2470 (concat "erc-" (symbol-name type) "-face"))
2471 "erc-default-face")
2472 string)
2473 string)))
2475 (defvar erc-lurker-state nil
2476 "Track the time of the last PRIVMSG for each (server,nick) pair.
2478 This is implemented as a hash of hashes, where the outer key is
2479 the canonicalized server name (as returned by
2480 `erc-canonicalize-server-name') and the outer value is a hash
2481 table mapping nicks (as returned by `erc-lurker-maybe-trim') to
2482 the times of their most recently received PRIVMSG on any channel
2483 on the given server.")
2485 (defcustom erc-lurker-trim-nicks t
2486 "If t, trim trailing `erc-lurker-ignore-chars' from nicks.
2488 This causes e.g. nick and nick\\=` to be considered as the same
2489 individual for activity tracking and lurkiness detection
2490 purposes."
2491 :group 'erc-lurker
2492 :type 'boolean)
2494 (defcustom erc-lurker-ignore-chars "`_"
2495 "Characters at the end of a nick to strip for activity tracking purposes.
2497 See also `erc-lurker-trim-nicks'."
2498 :group 'erc-lurker
2499 :type 'string)
2501 (defun erc-lurker-maybe-trim (nick)
2502 "Maybe trim trailing `erc-lurker-ignore-chars' from NICK.
2504 Returns NICK unmodified unless `erc-lurker-trim-nicks' is
2505 non-nil."
2506 (if erc-lurker-trim-nicks
2507 (replace-regexp-in-string
2508 (format "[%s]"
2509 (mapconcat (lambda (char)
2510 (regexp-quote (char-to-string char)))
2511 erc-lurker-ignore-chars ""))
2512 "" nick)
2513 nick))
2515 (defcustom erc-lurker-hide-list nil
2516 "List of IRC type messages to hide when sent by lurkers.
2518 A typical value would be \(\"JOIN\" \"PART\" \"QUIT\").
2519 See also `erc-lurker-p' and `erc-hide-list'."
2520 :group 'erc-lurker
2521 :type 'erc-message-type)
2523 (defcustom erc-lurker-threshold-time (* 60 60 24) ; 24h by default
2524 "Nicks from which no PRIVMSGs have been received within this
2525 interval (in units of seconds) are considered lurkers by
2526 `erc-lurker-p' and as a result their messages of types in
2527 `erc-lurker-hide-list' will be hidden."
2528 :group 'erc-lurker
2529 :type 'integer)
2531 (defun erc-lurker-initialize ()
2532 "Initialize ERC lurker tracking functionality.
2534 This function adds `erc-lurker-update-status' to
2535 `erc-insert-pre-hook' in order to record the time of each nick's
2536 most recent PRIVMSG as well as initializing the state variable
2537 storing this information."
2538 (setq erc-lurker-state (make-hash-table :test 'equal))
2539 (add-hook 'erc-insert-pre-hook 'erc-lurker-update-status))
2541 (defun erc-lurker-cleanup ()
2542 "Remove all last PRIVMSG state older than `erc-lurker-threshold-time'.
2544 This should be called regularly to avoid excessive resource
2545 consumption for long-lived IRC or Emacs sessions."
2546 (maphash
2547 (lambda (server hash)
2548 (maphash
2549 (lambda (nick last-PRIVMSG-time)
2550 (when
2551 (> (float-time (time-subtract
2552 (current-time)
2553 last-PRIVMSG-time))
2554 erc-lurker-threshold-time)
2555 (remhash nick hash)))
2556 hash)
2557 (if (zerop (hash-table-count hash))
2558 (remhash server erc-lurker-state)))
2559 erc-lurker-state))
2561 (defvar erc-lurker-cleanup-count 0
2562 "Internal counter variable for use with `erc-lurker-cleanup-interval'.")
2564 (defvar erc-lurker-cleanup-interval 100
2565 "Frequency of cleaning up stale erc-lurker state.
2567 `erc-lurker-update-status' calls `erc-lurker-cleanup' once for
2568 every `erc-lurker-cleanup-interval' updates to
2569 `erc-lurker-state'. This is designed to limit the memory
2570 consumption of lurker state during long Emacs sessions and/or ERC
2571 sessions with large numbers of incoming PRIVMSGs.")
2573 (defun erc-lurker-update-status (_message)
2574 "Update `erc-lurker-state' if necessary.
2576 This function is called from `erc-insert-pre-hook'. If the
2577 current message is a PRIVMSG, update `erc-lurker-state' to
2578 reflect the fact that its sender has issued a PRIVMSG at the
2579 current time. Otherwise, take no action.
2581 This function depends on the fact that `erc-display-message'
2582 dynamically binds `parsed', which is used to check if the current
2583 message is a PRIVMSG and to determine its sender. See also
2584 `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'.
2586 In order to limit memory consumption, this function also calls
2587 `erc-lurker-cleanup' once every `erc-lurker-cleanup-interval'
2588 updates of `erc-lurker-state'."
2589 (when (and (boundp 'parsed) (erc-response-p parsed))
2590 (let* ((command (erc-response.command parsed))
2591 (sender
2592 (erc-lurker-maybe-trim
2593 (car (erc-parse-user (erc-response.sender parsed)))))
2594 (server
2595 (erc-canonicalize-server-name erc-server-announced-name)))
2596 (when (equal command "PRIVMSG")
2597 (when (>= (cl-incf erc-lurker-cleanup-count)
2598 erc-lurker-cleanup-interval)
2599 (setq erc-lurker-cleanup-count 0)
2600 (erc-lurker-cleanup))
2601 (unless (gethash server erc-lurker-state)
2602 (puthash server (make-hash-table :test 'equal) erc-lurker-state))
2603 (puthash sender (current-time)
2604 (gethash server erc-lurker-state))))))
2606 (defun erc-lurker-p (nick)
2607 "Predicate indicating NICK's lurking status on the current server.
2609 Lurking is the condition where NICK has issued no PRIVMSG on this
2610 server within `erc-lurker-threshold-time'. See also
2611 `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'."
2612 (unless erc-lurker-state (erc-lurker-initialize))
2613 (let* ((server
2614 (erc-canonicalize-server-name erc-server-announced-name))
2615 (last-PRIVMSG-time
2616 (gethash (erc-lurker-maybe-trim nick)
2617 (gethash server erc-lurker-state (make-hash-table)))))
2618 (or (null last-PRIVMSG-time)
2619 (> (float-time
2620 (time-subtract (current-time) last-PRIVMSG-time))
2621 erc-lurker-threshold-time))))
2623 (defcustom erc-common-server-suffixes
2624 '(("openprojects.net\\'" . "OPN")
2625 ("freenode.net\\'" . "freenode")
2626 ("oftc.net\\'" . "OFTC"))
2627 "Alist of common server name suffixes.
2628 This variable is used in mode-line display to save screen
2629 real estate. Set it to nil if you want to avoid changing
2630 displayed hostnames."
2631 :group 'erc-mode-line-and-header
2632 :type 'alist)
2634 (defun erc-canonicalize-server-name (server)
2635 "Return the canonical network name for SERVER if any,
2636 otherwise `erc-server-announced-name'. SERVER is matched against
2637 `erc-common-server-suffixes'."
2638 (when server
2639 (or (cdar (erc-remove-if-not
2640 (lambda (net) (string-match (car net) server))
2641 erc-common-server-suffixes))
2642 erc-server-announced-name)))
2644 (defun erc-add-targets (scope target-list)
2645 (let ((targets
2646 (mapcar (lambda (targets) (member scope targets)) target-list)))
2647 (cdr (apply 'append (delete nil targets)))))
2649 (defun erc-hide-current-message-p (parsed)
2650 "Predicate indicating whether the parsed ERC response PARSED should be hidden.
2652 Messages are always hidden if the message type of PARSED appears in
2653 `erc-hide-list'. Message types that appear in `erc-network-hide-list'
2654 or `erc-channel-hide-list' are are only hidden if the target matches
2655 the network or channel in the list. In addition, messages whose type
2656 is a member of `erc-lurker-hide-list' are hidden if `erc-lurker-p'
2657 returns non-nil."
2658 (let* ((command (erc-response.command parsed))
2659 (sender (car (erc-parse-user (erc-response.sender parsed))))
2660 (channel (nth 1 (erc-response.command-args parsed)))
2661 (network (or (and (fboundp 'erc-network-name) (erc-network-name))
2662 (erc-shorten-server-name
2663 (or erc-server-announced-name
2664 erc-session-server))))
2665 (current-hide-list
2666 (when erc-network-hide-list
2667 (erc-add-targets network erc-network-hide-list)))
2668 (current-hide-list
2669 (apply 'append current-hide-list
2670 (when erc-channel-hide-list
2671 (erc-add-targets channel erc-channel-hide-list)))))
2672 (or (member command erc-hide-list)
2673 (member command current-hide-list)
2674 (and (member command erc-lurker-hide-list) (erc-lurker-p sender)))))
2676 (defun erc-display-message (parsed type buffer msg &rest args)
2677 "Display MSG in BUFFER.
2679 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2681 See also `erc-format-message' and `erc-display-line'."
2682 (let ((string (if (symbolp msg)
2683 (apply #'erc-format-message msg args)
2684 msg)))
2685 (setq string
2686 (cond
2687 ((null type)
2688 string)
2689 ((listp type)
2690 (mapc (lambda (type)
2691 (setq string
2692 (erc-display-message-highlight type string)))
2693 type)
2694 string)
2695 ((symbolp type)
2696 (erc-display-message-highlight type string))))
2698 (if (not (erc-response-p parsed))
2699 (erc-display-line string buffer)
2700 (unless (erc-hide-current-message-p parsed)
2701 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
2702 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2703 (when (erc-response.tags parsed)
2704 (erc-put-text-property 0 (length string) 'tags (erc-response.tags parsed)
2705 string))
2706 (erc-display-line string buffer)))))
2708 (defun erc-message-type-member (position list)
2709 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2711 This function relies on the erc-parsed text-property being
2712 present."
2713 (let ((prop-val (erc-get-parsed-vector position)))
2714 (and prop-val (member (erc-response.command prop-val) list))))
2716 (defvar erc-send-input-line-function 'erc-send-input-line)
2717 (make-variable-buffer-local 'erc-send-input-line-function)
2719 (defun erc-send-input-line (target line &optional force)
2720 "Send LINE to TARGET.
2722 See also `erc-server-send'."
2723 (setq line (format "PRIVMSG %s :%s"
2724 target
2725 ;; If the line is empty, we still want to
2726 ;; send it - i.e. an empty pasted line.
2727 (if (string= line "\n")
2728 " \n"
2729 line)))
2730 (erc-server-send line force target))
2732 (defun erc-get-arglist (fun)
2733 "Return the argument list of a function without the parens."
2734 (let ((arglist (format "%S" (erc-function-arglist fun))))
2735 (if (string-match "\\`(\\(.*\\))\\'" arglist)
2736 (match-string 1 arglist)
2737 arglist)))
2739 (defun erc-command-no-process-p (str)
2740 "Return non-nil if STR is an ERC command that can be run when the process
2741 is not alive, nil otherwise."
2742 (let ((fun (erc-extract-command-from-line str)))
2743 (and fun
2744 (symbolp (car fun))
2745 (get (car fun) 'process-not-needed))))
2747 (defun erc-command-name (cmd)
2748 "For CMD being the function name of a ERC command, something like
2749 erc-cmd-FOO, this returns a string /FOO."
2750 (let ((command-name (symbol-name cmd)))
2751 (if (string-match "\\`erc-cmd-\\(.*\\)\\'" command-name)
2752 (concat "/" (match-string 1 command-name))
2753 command-name)))
2755 (defun erc-process-input-line (line &optional force no-command)
2756 "Translate LINE to an RFC1459 command and send it based.
2757 Returns non-nil if the command is actually sent to the server, and nil
2758 otherwise.
2760 If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
2761 it is passed to `erc-cmd-default'. If LINE is not a command (i.e. doesn't
2762 start with /<COMMAND>) then it is sent as a message.
2764 An optional FORCE argument forces sending the line when flood
2765 protection is in effect. The optional NO-COMMAND argument prohibits
2766 this function from interpreting the line as a command."
2767 (let ((command-list (erc-extract-command-from-line line)))
2768 (if (and command-list
2769 (not no-command))
2770 (let* ((cmd (nth 0 command-list))
2771 (args (nth 1 command-list)))
2772 (condition-case nil
2773 (if (listp args)
2774 (apply cmd args)
2775 (funcall cmd args))
2776 (wrong-number-of-arguments
2777 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2778 ?c (erc-command-name cmd)
2779 ?u (or (erc-get-arglist cmd)
2781 ?d (format "%s\n"
2782 (or (documentation cmd) "")))
2783 nil)))
2784 (let ((r (erc-default-target)))
2785 (if r
2786 (funcall erc-send-input-line-function r line force)
2787 (erc-display-message nil 'error (current-buffer) 'no-target)
2788 nil)))))
2790 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2791 ;; Input commands handlers
2792 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2794 (defun erc-cmd-AMSG (line)
2795 "Send LINE to all channels of the current server that you are on."
2796 (interactive "sSend to all channels you're on: ")
2797 (setq line (erc-trim-string line))
2798 (erc-with-all-buffers-of-server nil
2799 (lambda ()
2800 (erc-channel-p (erc-default-target)))
2801 (erc-send-message line)))
2802 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2804 (defun erc-cmd-SAY (line)
2805 "Send LINE to the current query or channel as a message, not a command.
2807 Use this when you want to send a message with a leading `/'. Note
2808 that since multi-line messages are never a command, you don't
2809 need this when pasting multiple lines of text."
2810 (if (string-match "^\\s-*$" line)
2812 (string-match "^ ?\\(.*\\)" line)
2813 (erc-process-input-line (match-string 1 line) nil t)))
2814 (put 'erc-cmd-SAY 'do-not-parse-args t)
2816 (defun erc-cmd-SET (line)
2817 "Set the variable named by the first word in LINE to some VALUE.
2818 VALUE is computed by evaluating the rest of LINE in Lisp."
2819 (cond
2820 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2821 (let ((var (read (concat "erc-" (match-string 1 line))))
2822 (val (read (match-string 2 line))))
2823 (if (boundp var)
2824 (progn
2825 (set var (eval val))
2826 (erc-display-message
2827 nil nil 'active (format "Set %S to %S" var val))
2829 (setq var (read (match-string 1 line)))
2830 (if (boundp var)
2831 (progn
2832 (set var (eval val))
2833 (erc-display-message
2834 nil nil 'active (format "Set %S to %S" var val))
2836 (erc-display-message nil 'error 'active 'variable-not-bound)
2837 nil))))
2838 ((string-match "^\\s-*$" line)
2839 (erc-display-line
2840 (concat "Available user variables:\n"
2841 (apply
2842 #'concat
2843 (mapcar
2844 (lambda (var)
2845 (let ((val (symbol-value var)))
2846 (concat (format "%S:" var)
2847 (if (consp val)
2848 (concat "\n" (pp-to-string val))
2849 (format " %S\n" val)))))
2850 (apropos-internal "^erc-" 'custom-variable-p))))
2851 (current-buffer))
2853 (t nil)))
2854 (defalias 'erc-cmd-VAR 'erc-cmd-SET)
2855 (defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2856 (put 'erc-cmd-SET 'do-not-parse-args t)
2857 (put 'erc-cmd-SET 'process-not-needed t)
2859 (defun erc-cmd-default (line)
2860 "Fallback command.
2862 Commands for which no erc-cmd-xxx exists, are tunneled through
2863 this function. LINE is sent to the server verbatim, and
2864 therefore has to contain the command itself as well."
2865 (erc-log (format "cmd: DEFAULT: %s" line))
2866 (erc-server-send (substring line 1))
2869 (defun erc-cmd-IGNORE (&optional user)
2870 "Ignore USER. This should be a regexp matching nick!user@host.
2871 If no USER argument is specified, list the contents of `erc-ignore-list'."
2872 (if user
2873 (let ((quoted (regexp-quote user)))
2874 (when (and (not (string= user quoted))
2875 (y-or-n-p (format "Use regexp-quoted form (%s) instead? "
2876 quoted)))
2877 (setq user quoted))
2878 (erc-display-line
2879 (erc-make-notice (format "Now ignoring %s" user))
2880 'active)
2881 (erc-with-server-buffer (add-to-list 'erc-ignore-list user)))
2882 (if (null (erc-with-server-buffer erc-ignore-list))
2883 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2884 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2885 (mapc #'(lambda (item)
2886 (erc-display-line (erc-make-notice item)
2887 'active))
2888 (erc-with-server-buffer erc-ignore-list))))
2891 (defun erc-cmd-UNIGNORE (user)
2892 "Remove the user specified in USER from the ignore list."
2893 (let ((ignored-nick (car (erc-with-server-buffer
2894 (erc-member-ignore-case (regexp-quote user)
2895 erc-ignore-list)))))
2896 (unless ignored-nick
2897 (if (setq ignored-nick (erc-ignored-user-p user))
2898 (unless (y-or-n-p (format "Remove this regexp (%s)? "
2899 ignored-nick))
2900 (setq ignored-nick nil))
2901 (erc-display-line
2902 (erc-make-notice (format "%s is not currently ignored!" user))
2903 'active)))
2904 (when ignored-nick
2905 (erc-display-line
2906 (erc-make-notice (format "No longer ignoring %s" user))
2907 'active)
2908 (erc-with-server-buffer
2909 (setq erc-ignore-list (delete ignored-nick erc-ignore-list)))))
2912 (defun erc-cmd-CLEAR ()
2913 "Clear the window content."
2914 (recenter 0)
2916 (put 'erc-cmd-CLEAR 'process-not-needed t)
2918 (defun erc-cmd-OPS ()
2919 "Show the ops in the current channel."
2920 (interactive)
2921 (let ((ops nil))
2922 (if erc-channel-users
2923 (maphash (lambda (_nick user-data)
2924 (let ((cuser (cdr user-data)))
2925 (if (and cuser
2926 (erc-channel-user-op cuser))
2927 (setq ops (cons (erc-server-user-nickname
2928 (car user-data))
2929 ops)))))
2930 erc-channel-users))
2931 (setq ops (sort ops 'string-lessp))
2932 (if ops
2933 (erc-display-message
2934 nil 'notice (current-buffer) 'ops
2935 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2936 ?o (mapconcat 'identity ops " "))
2937 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2940 (defun erc-cmd-COUNTRY (tld)
2941 "Display the country associated with the top level domain TLD."
2942 (require 'mail-extr)
2943 (let ((co (ignore-errors (what-domain tld))))
2944 (if co
2945 (erc-display-message
2946 nil 'notice 'active 'country ?c co ?d tld)
2947 (erc-display-message
2948 nil 'notice 'active 'country-unknown ?d tld))
2950 (put 'erc-cmd-COUNTRY 'process-not-needed t)
2952 (defun erc-cmd-AWAY (line)
2953 "Mark the user as being away, the reason being indicated by LINE.
2954 If no reason is given, unset away status."
2955 (when (string-match "^\\s-*\\(.*\\)$" line)
2956 (let ((reason (match-string 1 line)))
2957 (erc-log (format "cmd: AWAY: %s" reason))
2958 (erc-server-send
2959 (if (string= reason "")
2960 "AWAY"
2961 (concat "AWAY :" reason))))
2963 (put 'erc-cmd-AWAY 'do-not-parse-args t)
2965 (defun erc-cmd-GAWAY (line)
2966 "Mark the user as being away everywhere, the reason being indicated by LINE."
2967 ;; on all server buffers.
2968 (erc-with-all-buffers-of-server nil
2969 #'erc-open-server-buffer-p
2970 (erc-cmd-AWAY line)))
2971 (put 'erc-cmd-GAWAY 'do-not-parse-args t)
2973 (defun erc-cmd-CTCP (nick cmd &rest args)
2974 "Send a Client To Client Protocol message to NICK.
2976 CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2977 VERSION and so on. It is called with ARGS."
2978 (let ((str (concat cmd
2979 (when args
2980 (concat " " (mapconcat #'identity args " "))))))
2981 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2982 (erc-send-ctcp-message nick str)
2985 (defun erc-cmd-HELP (&optional func)
2986 "Popup help information.
2988 If FUNC contains a valid function or variable, help about that
2989 will be displayed. If FUNC is empty, display an apropos about
2990 ERC commands. Otherwise, do `apropos' in the ERC namespace
2991 \(\"erc-.*LINE\").
2993 Examples:
2994 To find out about erc and bbdb, do
2995 /help bbdb.*
2997 For help about the WHOIS command, do:
2998 /help whois
3000 For a list of user commands (/join /part, ...):
3001 /help."
3002 (if func
3003 (let* ((sym (or (let ((sym (intern-soft
3004 (concat "erc-cmd-" (upcase func)))))
3005 (if (and sym (or (boundp sym) (fboundp sym)))
3007 nil))
3008 (let ((sym (intern-soft func)))
3009 (if (and sym (or (boundp sym) (fboundp sym)))
3011 nil))
3012 (let ((sym (intern-soft (concat "erc-" func))))
3013 (if (and sym (or (boundp sym) (fboundp sym)))
3015 nil)))))
3016 (if sym
3017 (cond
3018 ((boundp sym) (describe-variable sym))
3019 ((fboundp sym) (describe-function sym))
3020 (t nil))
3021 (apropos-command (concat "erc-.*" func) nil
3022 (lambda (x)
3023 (or (commandp x)
3024 (get x 'custom-type))))
3026 (apropos "erc-cmd-")
3027 (message "Type C-h m to get additional information about keybindings.")
3030 (defalias 'erc-cmd-H 'erc-cmd-HELP)
3031 (put 'erc-cmd-HELP 'process-not-needed t)
3033 (defun erc-server-join-channel (server channel &optional secret)
3034 (let* ((secret (or secret
3035 (plist-get (nth 0 (auth-source-search
3036 :max 1
3037 :host server
3038 :port "irc"
3039 :user channel))
3040 :secret)))
3041 (password (if (functionp secret)
3042 (funcall secret)
3043 secret)))
3044 (erc-log (format "cmd: JOIN: %s" channel))
3045 (erc-server-send (concat "JOIN " channel
3046 (if password
3047 (concat " " password)
3048 "")))))
3050 (defun erc-cmd-JOIN (channel &optional key)
3051 "Join the channel given in CHANNEL, optionally with KEY.
3052 If CHANNEL is specified as \"-invite\", join the channel to which you
3053 were most recently invited. See also `invitation'."
3054 (let (chnl)
3055 (if (string= (upcase channel) "-INVITE")
3056 (if erc-invitation
3057 (setq chnl erc-invitation)
3058 (erc-display-message nil 'error (current-buffer) 'no-invitation))
3059 (setq chnl (erc-ensure-channel-name channel)))
3060 (when chnl
3061 ;; Prevent double joining of same channel on same server.
3062 (let ((joined-channels
3063 (mapcar #'(lambda (chanbuf)
3064 (with-current-buffer chanbuf (erc-default-target)))
3065 (erc-channel-list erc-server-process))))
3066 (if (erc-member-ignore-case chnl joined-channels)
3067 (switch-to-buffer (car (erc-member-ignore-case chnl
3068 joined-channels)))
3069 (let ((server (with-current-buffer (process-buffer erc-server-process)
3070 (or erc-session-server erc-server-announced-name))))
3071 (erc-server-join-channel server chnl key))))))
3074 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
3075 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
3077 (defvar erc-channel-new-member-names nil
3078 "If non-nil, a names list is currently being received.
3080 If non-nil, this variable is a hash-table that associates
3081 received nicks with t.")
3082 (make-variable-buffer-local 'erc-channel-new-member-names)
3084 (defun erc-cmd-NAMES (&optional channel)
3085 "Display the users in CHANNEL.
3086 If CHANNEL is not specified, display the users in the current channel.
3087 This function clears the channel name list first, then sends the
3088 command."
3089 (let ((tgt (or (and (erc-channel-p channel) channel)
3090 (erc-default-target))))
3091 (if (and tgt (erc-channel-p tgt))
3092 (progn
3093 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
3094 (erc-with-buffer
3095 (tgt)
3096 (erc-channel-begin-receiving-names))
3097 (erc-server-send (concat "NAMES " tgt)))
3098 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
3100 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
3102 (defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
3103 "Kick the user indicated in LINE from the current channel.
3104 LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
3105 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
3106 (if (string= "" reasonstring)
3107 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
3108 (if (erc-channel-p target)
3109 (let ((nick reason-or-nick))
3110 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
3111 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
3112 nil target)
3114 (when target
3115 (let ((ch (erc-default-target)))
3116 (setq reasonstring (concat
3117 (if reason-or-nick (concat reason-or-nick " "))
3118 reasonstring))
3119 (if ch
3120 (progn
3121 (erc-log
3122 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
3123 (erc-server-send
3124 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
3125 (erc-display-message nil 'error (current-buffer)
3126 'no-default-channel))
3127 t)))))
3129 (defvar erc-script-args nil)
3131 (defun erc-cmd-LOAD (line)
3132 "Load the script provided in the LINE.
3133 If LINE continues beyond the file name, the rest of
3134 it is put in a (local) variable `erc-script-args',
3135 which can be used in Emacs Lisp scripts.
3137 The optional FORCE argument is ignored here - you can't force loading
3138 a script after exceeding the flood threshold."
3139 (cond
3140 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
3141 (let* ((file-to-find (match-string 1 line))
3142 (erc-script-args (match-string 2 line))
3143 (file (erc-find-file file-to-find erc-script-path)))
3144 (erc-log (format "cmd: LOAD: %s" file-to-find))
3145 (cond
3146 ((not file)
3147 (erc-display-message nil 'error (current-buffer)
3148 'cannot-find-file ?f file-to-find))
3149 ((not (file-readable-p file))
3150 (erc-display-message nil 'error (current-buffer)
3151 'cannot-read-file ?f file))
3153 (message "Loading `%s'..." file)
3154 (erc-load-script file)
3155 (message "Loading `%s'...done" file))))
3157 (t nil)))
3159 (defun erc-cmd-WHOIS (user &optional server)
3160 "Display whois information for USER.
3162 If SERVER is non-nil, use that, rather than the current server."
3163 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
3164 (let ((send (if server
3165 (format "WHOIS %s %s" user server)
3166 (format "WHOIS %s" user))))
3167 (erc-log (format "cmd: %s" send))
3168 (erc-server-send send)
3170 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
3172 (defun erc-cmd-WHOAMI ()
3173 "Display whois information about yourself."
3174 (erc-cmd-WHOIS (erc-current-nick))
3177 (defun erc-cmd-IDLE (nick)
3178 "Show the length of time NICK has been idle."
3179 (let ((origbuf (current-buffer))
3180 symlist)
3181 (erc-with-server-buffer
3182 (push (cons (erc-once-with-server-event
3183 311 (lambda (_proc parsed)
3184 (string= nick
3185 (nth 1 (erc-response.command-args
3186 parsed)))))
3187 'erc-server-311-functions)
3188 symlist)
3189 (push (cons (erc-once-with-server-event
3190 312 (lambda (_proc parsed)
3191 (string= nick
3192 (nth 1 (erc-response.command-args
3193 parsed)))))
3194 'erc-server-312-functions)
3195 symlist)
3196 (push (cons (erc-once-with-server-event
3197 318 (lambda (_proc parsed)
3198 (string= nick
3199 (nth 1 (erc-response.command-args
3200 parsed)))))
3201 'erc-server-318-functions)
3202 symlist)
3203 (push (cons (erc-once-with-server-event
3204 319 (lambda (_proc parsed)
3205 (string= nick
3206 (nth 1 (erc-response.command-args
3207 parsed)))))
3208 'erc-server-319-functions)
3209 symlist)
3210 (push (cons (erc-once-with-server-event
3211 320 (lambda (_proc parsed)
3212 (string= nick
3213 (nth 1 (erc-response.command-args
3214 parsed)))))
3215 'erc-server-320-functions)
3216 symlist)
3217 (push (cons (erc-once-with-server-event
3218 330 (lambda (_proc parsed)
3219 (string= nick
3220 (nth 1 (erc-response.command-args
3221 parsed)))))
3222 'erc-server-330-functions)
3223 symlist)
3224 (push (cons (erc-once-with-server-event
3226 (lambda (_proc parsed)
3227 (let ((idleseconds
3228 (string-to-number
3229 (cl-third
3230 (erc-response.command-args parsed)))))
3231 (erc-display-line
3232 (erc-make-notice
3233 (format "%s has been idle for %s."
3234 (erc-string-no-properties nick)
3235 (erc-seconds-to-string idleseconds)))
3236 origbuf)
3237 t)))
3238 'erc-server-317-functions)
3239 symlist)
3241 ;; Send the WHOIS command.
3242 (erc-cmd-WHOIS nick)
3244 ;; Remove the uninterned symbols from the server hooks that did not run.
3245 (run-at-time 20 nil (lambda (buf symlist)
3246 (with-current-buffer buf
3247 (dolist (sym symlist)
3248 (let ((hooksym (cdr sym))
3249 (funcsym (car sym)))
3250 (remove-hook hooksym funcsym t)))))
3251 (current-buffer) symlist)))
3254 (defun erc-cmd-DESCRIBE (line)
3255 "Pose some action to a certain user.
3256 LINE has the format \"USER ACTION\"."
3257 (cond
3258 ((string-match
3259 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
3260 (let ((dst (match-string 1 line))
3261 (s (match-string 2 line)))
3262 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
3263 (erc-send-action dst s))
3265 (t nil)))
3266 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
3268 (defun erc-cmd-ME (line)
3269 "Send LINE as an action."
3270 (cond
3271 ((string-match "^\\s-\\(.*\\)$" line)
3272 (let ((s (match-string 1 line)))
3273 (erc-log (format "cmd: ME: %s" s))
3274 (erc-send-action (erc-default-target) s))
3276 (t nil)))
3277 (put 'erc-cmd-ME 'do-not-parse-args t)
3279 (defun erc-cmd-ME\'S (line)
3280 "Do a /ME command, but add the string \" \\='s\" to the beginning."
3281 (erc-cmd-ME (concat " 's" line)))
3282 (put 'erc-cmd-ME\'S 'do-not-parse-args t)
3284 (defun erc-cmd-LASTLOG (line)
3285 "Show all lines in the current buffer matching the regexp LINE.
3287 If a match spreads across multiple lines, all those lines are shown.
3289 The lines are shown in a buffer named `*Occur*'.
3290 It serves as a menu to find any of the occurrences in this buffer.
3291 \\[describe-mode] in that buffer will explain how.
3293 If LINE contains upper case characters (excluding those preceded by `\\'),
3294 the matching is case-sensitive."
3295 (occur line)
3297 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
3298 (put 'erc-cmd-LASTLOG 'process-not-needed t)
3300 (defun erc-send-message (line &optional force)
3301 "Send LINE to the current channel or user and display it.
3303 See also `erc-message' and `erc-display-line'."
3304 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
3305 (erc-display-line
3306 (concat (erc-format-my-nick) line)
3307 (current-buffer))
3308 ;; FIXME - treat multiline, run hooks, or remove me?
3311 (defun erc-cmd-MODE (line)
3312 "Change or display the mode value of a channel or user.
3313 The first word specifies the target. The rest is the mode string
3314 to send.
3316 If only one word is given, display the mode of that target.
3318 A list of valid mode strings for Freenode may be found at
3319 URL `http://freenode.net/using_the_network.shtml'."
3320 (cond
3321 ((string-match "^\\s-\\(.*\\)$" line)
3322 (let ((s (match-string 1 line)))
3323 (erc-log (format "cmd: MODE: %s" s))
3324 (erc-server-send (concat "MODE " line)))
3326 (t nil)))
3327 (put 'erc-cmd-MODE 'do-not-parse-args t)
3329 (defun erc-cmd-NOTICE (channel-or-user &rest message)
3330 "Send a notice to the channel or user given as the first word.
3331 The rest is the message to send."
3332 (erc-message "NOTICE" (concat channel-or-user " "
3333 (mapconcat #'identity message " "))))
3335 (defun erc-cmd-MSG (line)
3336 "Send a message to the channel or user given as the first word in LINE.
3338 The rest of LINE is the message to send."
3339 (erc-message "PRIVMSG" line))
3341 (defalias 'erc-cmd-M 'erc-cmd-MSG)
3342 (put 'erc-cmd-MSG 'do-not-parse-args t)
3344 (defun erc-cmd-SQUERY (line)
3345 "Send a Service Query to the service given as the first word in LINE.
3347 The rest of LINE is the message to send."
3348 (erc-message "SQUERY" line))
3350 (defun erc-cmd-NICK (nick)
3351 "Change current nickname to NICK."
3352 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
3353 (let ((nicklen (cdr (assoc "NICKLEN" (erc-with-server-buffer
3354 erc-server-parameters)))))
3355 (and nicklen (> (length nick) (string-to-number nicklen))
3356 (erc-display-message
3357 nil 'notice 'active 'nick-too-long
3358 ?i (length nick) ?l nicklen)))
3359 (erc-server-send (format "NICK %s" nick))
3360 (cond (erc-bad-nick
3361 (erc-set-current-nick nick)
3362 (erc-update-mode-line)
3363 (setq erc-bad-nick nil)))
3366 (defun erc-cmd-PART (line)
3367 "When LINE is an empty string, leave the current channel.
3368 Otherwise leave the channel indicated by LINE."
3369 (cond
3370 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
3371 (let* ((ch (match-string 1 line))
3372 (msg (match-string 2 line))
3373 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3374 (erc-log (format "cmd: PART: %s: %s" ch reason))
3375 (erc-server-send (if (string= reason "")
3376 (format "PART %s" ch)
3377 (format "PART %s :%s" ch reason))
3378 nil ch))
3380 ((string-match "^\\s-*\\(.*\\)$" line)
3381 (let* ((ch (erc-default-target))
3382 (msg (match-string 1 line))
3383 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3384 (if (and ch (erc-channel-p ch))
3385 (progn
3386 (erc-log (format "cmd: PART: %s: %s" ch reason))
3387 (erc-server-send (if (string= reason "")
3388 (format "PART %s" ch)
3389 (format "PART %s :%s" ch reason))
3390 nil ch))
3391 (erc-display-message nil 'error (current-buffer) 'no-target)))
3393 (t nil)))
3394 (put 'erc-cmd-PART 'do-not-parse-args t)
3396 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
3398 (defun erc-cmd-PING (recipient)
3399 "Ping RECIPIENT."
3400 (let ((time (format "%f" (erc-current-time))))
3401 (erc-log (format "cmd: PING: %s" time))
3402 (erc-cmd-CTCP recipient "PING" time)))
3404 (defun erc-cmd-QUOTE (line)
3405 "Send LINE directly to the server.
3406 All the text given as argument is sent to the sever as unmodified,
3407 just as you provided it. Use this command with care!"
3408 (cond
3409 ((string-match "^ ?\\(.+\\)$" line)
3410 (erc-server-send (match-string 1 line)))
3411 (t nil)))
3412 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
3414 (defcustom erc-query-display 'window
3415 "Indicates how to display query buffers when using the /QUERY
3416 command to talk to someone.
3418 The default behavior is to display the message in a new window
3419 and bring it to the front. See the documentation for
3420 `erc-join-buffer' for a description of the available choices.
3422 See also `erc-auto-query' to decide how private messages from
3423 other people should be displayed."
3424 :group 'erc-query
3425 :type '(choice (const :tag "Split window and select" window)
3426 (const :tag "Split window, don't select" window-noselect)
3427 (const :tag "New frame" frame)
3428 (const :tag "Bury in new buffer" bury)
3429 (const :tag "Use current buffer" buffer)
3430 (const :tag "Use current buffer" t)))
3432 (defun erc-cmd-QUERY (&optional user)
3433 "Open a query with USER.
3434 The type of query window/frame/etc will depend on the value of
3435 `erc-query-display'.
3437 If USER is omitted, close the current query buffer if one exists
3438 - except this is broken now ;-)"
3439 (interactive
3440 (list (read-from-minibuffer "Start a query with: " nil)))
3441 (let ((session-buffer (erc-server-buffer))
3442 (erc-join-buffer erc-query-display))
3443 (if user
3444 (erc-query user session-buffer)
3445 ;; currently broken, evil hack to display help anyway
3446 ;(erc-delete-query))))
3447 (signal 'wrong-number-of-arguments ""))))
3448 (defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3450 (defun erc-quit/part-reason-default ()
3451 "Default quit/part message."
3452 (format "\C-bERC\C-b (IRC client for Emacs %s)" emacs-version))
3455 (defun erc-quit-reason-normal (&optional s)
3456 "Normal quit message.
3458 If S is non-nil, it will be used as the quit reason."
3459 (or s (erc-quit/part-reason-default)))
3461 (defun erc-quit-reason-zippy (&optional s)
3462 "Zippy quit message.
3464 If S is non-nil, it will be used as the quit reason."
3465 (or s
3466 (if (fboundp 'yow)
3467 (erc-replace-regexp-in-string "\n" "" (yow))
3468 (erc-quit/part-reason-default))))
3470 (make-obsolete 'erc-quit-reason-zippy "it will be removed." "24.4")
3472 (defun erc-quit-reason-various (s)
3473 "Choose a quit reason based on S (a string)."
3474 (when (featurep 'xemacs) (require 'poe))
3475 (let ((res (car (assoc-default (or s "")
3476 erc-quit-reason-various-alist 'string-match))))
3477 (cond
3478 ((functionp res) (funcall res))
3479 ((stringp res) res)
3480 (s s)
3481 (t (erc-quit/part-reason-default)))))
3483 (defun erc-part-reason-normal (&optional s)
3484 "Normal part message.
3486 If S is non-nil, it will be used as the part reason."
3487 (or s (erc-quit/part-reason-default)))
3489 (defun erc-part-reason-zippy (&optional s)
3490 "Zippy part message.
3492 If S is non-nil, it will be used as the quit reason."
3493 (or s
3494 (if (fboundp 'yow)
3495 (erc-replace-regexp-in-string "\n" "" (yow))
3496 (erc-quit/part-reason-default))))
3498 (make-obsolete 'erc-part-reason-zippy "it will be removed." "24.4")
3500 (defun erc-part-reason-various (s)
3501 "Choose a part reason based on S (a string)."
3502 (when (featurep 'xemacs) (require 'poe))
3503 (let ((res (car (assoc-default (or s "")
3504 erc-part-reason-various-alist 'string-match))))
3505 (cond
3506 ((functionp res) (funcall res))
3507 ((stringp res) res)
3508 (s s)
3509 (t (erc-quit/part-reason-default)))))
3511 (defun erc-cmd-QUIT (reason)
3512 "Disconnect from the current server.
3513 If REASON is omitted, display a default quit message, otherwise display
3514 the message given by REASON."
3515 (unless reason
3516 (setq reason ""))
3517 (cond
3518 ((string-match "^\\s-*\\(.*\\)$" reason)
3519 (let* ((s (match-string 1 reason))
3520 (buffer (erc-server-buffer))
3521 (reason (funcall erc-quit-reason (if (equal s "") nil s)))
3522 server-proc)
3523 (with-current-buffer (if (and buffer
3524 (bufferp buffer))
3525 buffer
3526 (current-buffer))
3527 (erc-log (format "cmd: QUIT: %s" reason))
3528 (setq erc-server-quitting t)
3529 (erc-set-active-buffer (erc-server-buffer))
3530 (setq server-proc erc-server-process)
3531 (erc-server-send (format "QUIT :%s" reason)))
3532 (run-hook-with-args 'erc-quit-hook server-proc)
3533 (when erc-kill-queries-on-quit
3534 (erc-kill-query-buffers server-proc))
3535 ;; if the process has not been killed within 4 seconds, kill it
3536 (run-at-time 4 nil
3537 (lambda (proc)
3538 (when (and (processp proc)
3539 (memq (process-status proc) '(run open)))
3540 (delete-process proc)))
3541 server-proc))
3543 (t nil)))
3545 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3546 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3547 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3548 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3549 (put 'erc-cmd-QUIT 'process-not-needed t)
3551 (defun erc-cmd-GQUIT (reason)
3552 "Disconnect from all servers at once with the same quit REASON."
3553 (erc-with-all-buffers-of-server nil #'erc-open-server-buffer-p
3554 (erc-cmd-QUIT reason))
3555 (when erc-kill-queries-on-quit
3556 ;; if the query buffers have not been killed within 4 seconds,
3557 ;; kill them
3558 (run-at-time
3559 4 nil
3560 (lambda ()
3561 (dolist (buffer (erc-buffer-list (lambda (buf)
3562 (not (erc-server-buffer-p buf)))))
3563 (kill-buffer buffer)))))
3566 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3567 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3568 (put 'erc-cmd-GQUIT 'process-not-needed t)
3570 (defun erc-cmd-RECONNECT ()
3571 "Try to reconnect to the current IRC server."
3572 (let ((buffer (erc-server-buffer))
3573 (process nil))
3574 (unless (buffer-live-p buffer)
3575 (setq buffer (current-buffer)))
3576 (with-current-buffer buffer
3577 (setq erc-server-quitting nil)
3578 (setq erc-server-reconnecting t)
3579 (setq erc-server-reconnect-count 0)
3580 (setq process (get-buffer-process (erc-server-buffer)))
3581 (if process
3582 (delete-process process)
3583 (erc-server-reconnect))
3584 (setq erc-server-reconnecting nil)))
3586 (put 'erc-cmd-RECONNECT 'process-not-needed t)
3588 (defun erc-cmd-SERVER (server)
3589 "Connect to SERVER, leaving existing connection intact."
3590 (erc-log (format "cmd: SERVER: %s" server))
3591 (condition-case nil
3592 (erc :server server :nick (erc-current-nick))
3593 (error
3594 (erc-error "Cannot find host %s." server)))
3596 (put 'erc-cmd-SERVER 'process-not-needed t)
3598 (defvar motif-version-string)
3599 (defvar gtk-version-string)
3601 (defun erc-cmd-SV ()
3602 "Say the current ERC and Emacs version into channel."
3603 (erc-send-message (format "I'm using ERC with %s %s (%s%s)%s."
3604 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3605 emacs-version
3606 system-configuration
3607 (concat
3608 (cond ((featurep 'motif)
3609 (concat ", " (substring
3610 motif-version-string 4)))
3611 ((featurep 'gtk)
3612 (concat ", GTK+ Version "
3613 gtk-version-string))
3614 ((featurep 'x-toolkit) ", X toolkit")
3615 (t ""))
3616 (if (and (boundp 'x-toolkit-scroll-bars)
3617 (memq x-toolkit-scroll-bars
3618 '(xaw xaw3d)))
3619 (format ", %s scroll bars"
3620 (capitalize (symbol-name
3621 x-toolkit-scroll-bars)))
3623 (if (featurep 'multi-tty) ", multi-tty" ""))
3624 (if erc-emacs-build-time
3625 (concat " of " erc-emacs-build-time)
3626 "")))
3629 (defun erc-cmd-SM ()
3630 "Say the current ERC modes into channel."
3631 (erc-send-message (format "I'm using the following modules: %s!"
3632 (erc-modes)))
3635 (defun erc-cmd-DEOP (&rest people)
3636 "Remove the operator setting from user(s) given in PEOPLE."
3637 (when (> (length people) 0)
3638 (erc-server-send (concat "MODE " (erc-default-target)
3639 " -"
3640 (make-string (length people) ?o)
3642 (mapconcat 'identity people " ")))
3645 (defun erc-cmd-OP (&rest people)
3646 "Add the operator setting to users(s) given in PEOPLE."
3647 (when (> (length people) 0)
3648 (erc-server-send (concat "MODE " (erc-default-target)
3649 " +"
3650 (make-string (length people) ?o)
3652 (mapconcat 'identity people " ")))
3655 (defun erc-cmd-TIME (&optional line)
3656 "Request the current time and date from the current server."
3657 (cond
3658 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3659 (let ((args (match-string 1 line)))
3660 (erc-log (format "cmd: TIME: %s" args))
3661 (erc-server-send (concat "TIME " args)))
3663 (t (erc-server-send "TIME"))))
3664 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3666 (defun erc-cmd-TOPIC (topic)
3667 "Set or request the topic for a channel.
3668 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3669 or the empty string.
3671 If no #CHANNEL is given, the default channel is used. If TOPIC is
3672 given, the channel topic is modified, otherwise the current topic will
3673 be displayed."
3674 (cond
3675 ;; /topic #channel TOPIC
3676 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3677 (let ((ch (match-string 1 topic))
3678 (topic (match-string 2 topic)))
3679 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3680 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3682 ;; /topic #channel
3683 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3684 (let ((ch (match-string 1 topic)))
3685 (erc-server-send (format "TOPIC %s" ch) nil ch)
3687 ;; /topic
3688 ((string-match "^\\s-*$" topic)
3689 (let ((ch (erc-default-target)))
3690 (erc-server-send (format "TOPIC %s" ch) nil ch)
3692 ;; /topic TOPIC
3693 ((string-match "^\\s-*\\(.*\\)$" topic)
3694 (let ((ch (erc-default-target))
3695 (topic (match-string 1 topic)))
3696 (if (and ch (erc-channel-p ch))
3697 (progn
3698 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3699 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3700 (erc-display-message nil 'error (current-buffer) 'no-target)))
3702 (t nil)))
3703 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3704 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3706 (defun erc-cmd-APPENDTOPIC (topic)
3707 "Append TOPIC to the current channel topic, separated by a space."
3708 (let ((oldtopic erc-channel-topic))
3709 ;; display help when given no arguments
3710 (when (string-match "^\\s-*$" topic)
3711 (signal 'wrong-number-of-arguments nil))
3712 ;; strip trailing ^O
3713 (when (string-match "\\(.*\\)\C-o" oldtopic)
3714 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3715 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3716 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3718 (defun erc-cmd-CLEARTOPIC (&optional channel)
3719 "Clear the topic for a CHANNEL.
3720 If CHANNEL is not specified, clear the topic for the default channel."
3721 (interactive "sClear topic of channel (RET is current channel): ")
3722 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3723 (when chnl
3724 (erc-server-send (format "TOPIC %s :" chnl))
3725 t)))
3727 ;;; Banlists
3729 (defvar erc-channel-banlist nil
3730 "A list of bans seen for the current channel.
3732 Each ban is an alist of the form:
3733 (WHOSET . MASK)
3735 The property `received-from-server' indicates whether
3736 or not the ban list has been requested from the server.")
3737 (make-variable-buffer-local 'erc-channel-banlist)
3738 (put 'erc-channel-banlist 'received-from-server nil)
3740 (defun erc-cmd-BANLIST ()
3741 "Pretty-print the contents of `erc-channel-banlist'.
3743 The ban list is fetched from the server if necessary."
3744 (let ((chnl (erc-default-target))
3745 (chnl-name (buffer-name)))
3747 (cond
3748 ((not (erc-channel-p chnl))
3749 (erc-display-line (erc-make-notice "You're not on a channel\n")
3750 'active))
3752 ((not (get 'erc-channel-banlist 'received-from-server))
3753 (let ((old-367-hook erc-server-367-functions))
3754 (setq erc-server-367-functions 'erc-banlist-store
3755 erc-channel-banlist nil)
3756 ;; fetch the ban list then callback
3757 (erc-with-server-buffer
3758 (erc-once-with-server-event
3760 (lambda (_proc _parsed)
3761 (with-current-buffer chnl-name
3762 (put 'erc-channel-banlist 'received-from-server t)
3763 (setq erc-server-367-functions old-367-hook)
3764 (erc-cmd-BANLIST)
3765 t)))
3766 (erc-server-send (format "MODE %s b" chnl)))))
3768 ((null erc-channel-banlist)
3769 (erc-display-line (erc-make-notice
3770 (format "No bans for channel: %s\n" chnl))
3771 'active)
3772 (put 'erc-channel-banlist 'received-from-server nil))
3775 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3776 erc-fill-column)
3777 (and (boundp 'fill-column)
3778 fill-column)
3779 (1- (window-width))))
3780 (separator (make-string erc-fill-column ?=))
3781 (fmt (concat
3782 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3783 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3785 (erc-display-line
3786 (erc-make-notice (format "Ban list for channel: %s\n"
3787 (erc-default-target)))
3788 'active)
3790 (erc-display-line separator 'active)
3791 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3792 (erc-display-line separator 'active)
3794 (mapc
3795 (lambda (x)
3796 (erc-display-line
3797 (format fmt
3798 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3799 (if (car x)
3800 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3801 ""))
3802 'active))
3803 erc-channel-banlist)
3805 (erc-display-line (erc-make-notice "End of Ban list")
3806 'active)
3807 (put 'erc-channel-banlist 'received-from-server nil)))))
3810 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3812 (defun erc-cmd-MASSUNBAN ()
3813 "Mass Unban.
3815 Unban all currently banned users in the current channel."
3816 (let ((chnl (erc-default-target)))
3817 (cond
3819 ((not (erc-channel-p chnl))
3820 (erc-display-line
3821 (erc-make-notice "You're not on a channel\n")
3822 'active))
3824 ((not (get 'erc-channel-banlist 'received-from-server))
3825 (let ((old-367-hook erc-server-367-functions))
3826 (setq erc-server-367-functions 'erc-banlist-store)
3827 ;; fetch the ban list then callback
3828 (erc-with-server-buffer
3829 (erc-once-with-server-event
3831 (lambda (_proc _parsed)
3832 (with-current-buffer chnl
3833 (put 'erc-channel-banlist 'received-from-server t)
3834 (setq erc-server-367-functions old-367-hook)
3835 (erc-cmd-MASSUNBAN)
3836 t)))
3837 (erc-server-send (format "MODE %s b" chnl)))))
3839 (t (let ((bans (mapcar #'cdr erc-channel-banlist)))
3840 (when bans
3841 ;; Glob the bans into groups of three, and carry out the unban.
3842 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3843 (mapc
3844 (lambda (x)
3845 (erc-server-send
3846 (format "MODE %s -%s %s" (erc-default-target)
3847 (make-string (length x) ?b)
3848 (mapconcat 'identity x " "))))
3849 (erc-group-list bans 3))))
3850 t))))
3852 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3854 ;;;; End of IRC commands
3856 (defun erc-ensure-channel-name (channel)
3857 "Return CHANNEL if it is a valid channel name.
3858 Eventually add a # in front of it, if that turns it into a valid channel name."
3859 (if (erc-channel-p channel)
3860 channel
3861 (concat "#" channel)))
3863 (defun erc-grab-region (start end)
3864 "Copy the region between START and END in a recreatable format.
3866 Converts all the IRC text properties in each line of the region
3867 into control codes and writes them to a separate buffer. The
3868 resulting text may be used directly as a script to generate this
3869 text again."
3870 (interactive "r")
3871 (erc-set-active-buffer (current-buffer))
3872 (save-excursion
3873 (let* ((cb (current-buffer))
3874 (buf (generate-new-buffer erc-grab-buffer-name))
3875 (region (buffer-substring start end))
3876 (lines (erc-split-multiline-safe region)))
3877 (set-buffer buf)
3878 (dolist (line lines)
3879 (insert (concat line "\n")))
3880 (set-buffer cb)
3881 (switch-to-buffer-other-window buf)))
3882 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3883 (ding))
3885 (defun erc-display-prompt (&optional buffer pos prompt face)
3886 "Display PROMPT in BUFFER at position POS.
3887 Display an ERC prompt in BUFFER.
3889 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3890 If BUFFER is nil, the `current-buffer' is used.
3891 If POS is nil, PROMPT will be displayed at `point'.
3892 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3893 `erc-prompt-face' will be used."
3894 (let* ((prompt (or prompt (erc-prompt)))
3895 (l (length prompt))
3896 (ob (current-buffer)))
3897 ;; We cannot use save-excursion because we move point, therefore
3898 ;; we resort to the ol' ob trick to restore this.
3899 (when (and buffer (bufferp buffer))
3900 (set-buffer buffer))
3902 ;; now save excursion again to store where point and mark are
3903 ;; in the current buffer
3904 (save-excursion
3905 (setq pos (or pos (point)))
3906 (goto-char pos)
3907 (when (> l 0)
3908 ;; Do not extend the text properties when typing at the end
3909 ;; of the prompt, but stuff typed in front of the prompt
3910 ;; shall remain part of the prompt.
3911 (setq prompt (erc-propertize prompt
3912 'start-open t ; XEmacs
3913 'rear-nonsticky t ; Emacs
3914 'erc-prompt t
3915 'field t
3916 'front-sticky t
3917 'read-only t))
3918 (erc-put-text-property 0 (1- (length prompt))
3919 'font-lock-face (or face 'erc-prompt-face)
3920 prompt)
3921 (insert prompt))
3922 ;; Set the input marker
3923 (set-marker erc-input-marker (point)))
3925 ;; Now we are back at the old position. If the prompt was
3926 ;; inserted here or before us, advance point by the length of
3927 ;; the prompt.
3928 (when (or (not pos) (<= (point) pos))
3929 (forward-char l))
3930 ;; Clear the undo buffer now, so the user can undo his stuff,
3931 ;; but not the stuff we did. Sneaky!
3932 (setq buffer-undo-list nil)
3933 (set-buffer ob)))
3935 ;; interactive operations
3937 (defun erc-input-message ()
3938 "Read input from the minibuffer."
3939 (interactive)
3940 (let ((minibuffer-allow-text-properties t)
3941 (read-map minibuffer-local-map))
3942 (insert (read-from-minibuffer "Message: "
3943 (string (if (featurep 'xemacs)
3944 last-command-char
3945 last-command-event))
3946 read-map))
3947 (erc-send-current-line)))
3949 (defvar erc-action-history-list ()
3950 "History list for interactive action input.")
3952 (defun erc-input-action ()
3953 "Interactively input a user action and send it to IRC."
3954 (interactive "")
3955 (erc-set-active-buffer (current-buffer))
3956 (let ((action (read-from-minibuffer
3957 "Action: " nil nil nil 'erc-action-history-list)))
3958 (if (not (string-match "^\\s-*$" action))
3959 (erc-send-action (erc-default-target) action))))
3961 (defun erc-join-channel (channel &optional key)
3962 "Join CHANNEL.
3964 If `point' is at the beginning of a channel name, use that as default."
3965 (interactive
3966 (list
3967 (let ((chnl (if (looking-at "\\([&#+!][^ \n]+\\)") (match-string 1) ""))
3968 (table (when (erc-server-buffer-live-p)
3969 (set-buffer (process-buffer erc-server-process))
3970 erc-channel-list)))
3971 (completing-read "Join channel: " table nil nil nil nil chnl))
3972 (when (or current-prefix-arg erc-prompt-for-channel-key)
3973 (read-from-minibuffer "Channel key (RET for none): " nil))))
3974 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3976 (defun erc-part-from-channel (reason)
3977 "Part from the current channel and prompt for a REASON."
3978 (interactive
3979 (list
3980 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3981 reason
3982 (read-from-minibuffer (concat "Reason for leaving " (erc-default-target)
3983 ": ")))))
3984 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3986 (defun erc-set-topic (topic)
3987 "Prompt for a TOPIC for the current channel."
3988 (interactive
3989 (list
3990 (read-from-minibuffer
3991 (concat "Set topic of " (erc-default-target) ": ")
3992 (when erc-channel-topic
3993 (let ((ss (split-string erc-channel-topic "\C-o")))
3994 (cons (apply #'concat (if (cdr ss) (butlast ss) ss))
3995 0))))))
3996 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3997 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3999 (defun erc-set-channel-limit (&optional limit)
4000 "Set a LIMIT for the current channel. Remove limit if nil.
4001 Prompt for one if called interactively."
4002 (interactive (list (read-from-minibuffer
4003 (format "Limit for %s (RET to remove limit): "
4004 (erc-default-target)))))
4005 (let ((tgt (erc-default-target)))
4006 (erc-server-send (if (and limit (>= (length limit) 1))
4007 (format "MODE %s +l %s" tgt limit)
4008 (format "MODE %s -l" tgt)))))
4010 (defun erc-set-channel-key (&optional key)
4011 "Set a KEY for the current channel. Remove key if nil.
4012 Prompt for one if called interactively."
4013 (interactive (list (read-from-minibuffer
4014 (format "Key for %s (RET to remove key): "
4015 (erc-default-target)))))
4016 (let ((tgt (erc-default-target)))
4017 (erc-server-send (if (and key (>= (length key) 1))
4018 (format "MODE %s +k %s" tgt key)
4019 (format "MODE %s -k" tgt)))))
4021 (defun erc-quit-server (reason)
4022 "Disconnect from current server after prompting for REASON.
4023 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
4024 (interactive (list (read-from-minibuffer
4025 (format "Reason for quitting %s: "
4026 (or erc-server-announced-name
4027 erc-session-server)))))
4028 (erc-cmd-QUIT reason))
4030 ;; Movement of point
4032 (defun erc-bol ()
4033 "Move `point' to the beginning of the current line.
4035 This places `point' just after the prompt, or at the beginning of the line."
4036 (interactive)
4037 (forward-line 0)
4038 (when (get-text-property (point) 'erc-prompt)
4039 (goto-char erc-input-marker))
4040 (point))
4042 (defun erc-kill-input ()
4043 "Kill current input line using `erc-bol' followed by `kill-line'."
4044 (interactive)
4045 (when (and (erc-bol)
4046 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
4047 ;; there's nothing to kill
4048 (if (boundp 'erc-input-ring-index)
4049 (setq erc-input-ring-index nil))
4050 (kill-line)))
4052 (defun erc-complete-word-at-point ()
4053 (run-hook-with-args-until-success 'erc-complete-functions))
4055 (define-obsolete-function-alias 'erc-complete-word 'completion-at-point "24.1")
4057 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4059 ;; IRC SERVER INPUT HANDLING
4061 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4063 ;;;; New Input parsing
4065 ; Stolen from ZenIRC. I just wanna test this code, so here is
4066 ; experiment area.
4068 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
4069 erc-default-server-handler)
4070 "Default for server messages which aren't covered by `erc-server-hooks'."
4071 :group 'erc-server-hooks
4072 :type 'hook)
4074 (defun erc-default-server-handler (proc parsed)
4075 "Default server handler.
4077 Displays PROC and PARSED appropriately using `erc-display-message'."
4078 (erc-display-message
4079 parsed 'notice proc
4080 (mapconcat
4081 'identity
4082 (let (res)
4083 (mapc #'(lambda (x)
4084 (if (stringp x)
4085 (setq res (append res (list x)))))
4086 parsed)
4087 res)
4088 " ")))
4090 (defvar erc-server-vectors
4091 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
4092 "List of received server messages which ERC does not specifically handle.
4093 See `erc-debug-missing-hooks'.")
4094 ;(make-variable-buffer-local 'erc-server-vectors)
4096 (defun erc-debug-missing-hooks (_proc parsed)
4097 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
4098 These vectors can be helpful when adding new server message handlers to ERC.
4099 See `erc-default-server-hook'."
4100 (nconc erc-server-vectors (list parsed))
4101 nil)
4103 (defun erc-query (target server)
4104 "Open a query buffer on TARGET, using SERVER.
4105 To change how this query window is displayed, use `let' to bind
4106 `erc-join-buffer' before calling this."
4107 (unless (and server
4108 (buffer-live-p server)
4109 (set-buffer server))
4110 (error "Couldn't switch to server buffer"))
4111 (let ((buf (erc-open erc-session-server
4112 erc-session-port
4113 (erc-current-nick)
4114 erc-session-user-full-name
4117 (list target)
4118 target
4119 erc-server-process)))
4120 (unless buf
4121 (error "Couldn't open query window"))
4122 (erc-update-mode-line)
4123 buf))
4125 (defcustom erc-auto-query 'window-noselect
4126 "If non-nil, create a query buffer each time you receive a private message.
4127 If the buffer doesn't already exist, it is created.
4129 This can be set to a symbol, to control how the new query window
4130 should appear. The default behavior is to display the buffer in
4131 a new window, but not to select it. See the documentation for
4132 `erc-join-buffer' for a description of the available choices."
4133 :group 'erc-query
4134 :type '(choice (const :tag "Don't create query window" nil)
4135 (const :tag "Split window and select" window)
4136 (const :tag "Split window, don't select" window-noselect)
4137 (const :tag "New frame" frame)
4138 (const :tag "Bury in new buffer" bury)
4139 (const :tag "Use current buffer" buffer)
4140 (const :tag "Use current buffer" t)))
4142 (defcustom erc-query-on-unjoined-chan-privmsg t
4143 "If non-nil create query buffer on receiving any PRIVMSG at all.
4144 This includes PRIVMSGs directed to channels. If you are using an IRC
4145 bouncer, such as dircproxy, to keep a log of channels when you are
4146 disconnected, you should set this option to t."
4147 :group 'erc-query
4148 :type 'boolean)
4150 (defcustom erc-format-query-as-channel-p t
4151 "If non-nil, format text from others in a query buffer like in a channel,
4152 otherwise format like a private message."
4153 :group 'erc-query
4154 :type 'boolean)
4156 (defcustom erc-minibuffer-notice nil
4157 "If non-nil, print ERC notices for the user in the minibuffer.
4158 Only happens when the session buffer isn't visible."
4159 :group 'erc-display
4160 :type 'boolean)
4162 (defcustom erc-minibuffer-ignored nil
4163 "If non-nil, print a message in the minibuffer if we ignored something."
4164 :group 'erc-ignore
4165 :type 'boolean)
4167 (defun erc-wash-quit-reason (reason nick login host)
4168 "Remove duplicate text from quit REASON.
4169 Specifically in relation to NICK (user@host) information. Returns REASON
4170 unmodified if nothing can be removed.
4171 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
4172 \"Read error: 110\". The same applies for \"Ping Timeout\"."
4173 (setq nick (regexp-quote nick)
4174 login (regexp-quote login)
4175 host (regexp-quote host))
4176 (or (when (string-match (concat "^\\(Read error\\) to "
4177 nick "\\[" host "\\]: "
4178 "\\(.+\\)$")
4179 reason)
4180 (concat (match-string 1 reason) ": " (match-string 2 reason)))
4181 (when (string-match (concat "^\\(Ping timeout\\) for "
4182 nick "\\[" host "\\]$")
4183 reason)
4184 (match-string 1 reason))
4185 reason))
4187 (defun erc-nickname-in-use (nick reason)
4188 "If NICK is unavailable, tell the user the REASON.
4190 See also `erc-display-error-notice'."
4191 (if (or (not erc-try-new-nick-p)
4192 ;; how many default-nicks are left + one more try...
4193 (eq erc-nick-change-attempt-count
4194 (if (consp erc-nick)
4195 (+ (length erc-nick) 1)
4196 1)))
4197 (erc-display-error-notice
4199 (format "Nickname %s is %s, try another." nick reason))
4200 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
4201 (let ((newnick (nth 1 erc-default-nicks))
4202 (nicklen (cdr (assoc "NICKLEN"
4203 (erc-with-server-buffer
4204 erc-server-parameters)))))
4205 (setq erc-bad-nick t)
4206 ;; try to use a different nick
4207 (if erc-default-nicks
4208 (setq erc-default-nicks (cdr erc-default-nicks)))
4209 (if (not newnick)
4210 (setq newnick (concat (truncate-string-to-width
4211 nick
4212 (if (and erc-server-connected nicklen)
4213 (- (string-to-number nicklen)
4214 (length erc-nick-uniquifier))
4215 ;; rfc2812 max nick length = 9
4216 ;; we must assume this is the
4217 ;; server's setting if we haven't
4218 ;; established a connection yet
4219 (- 9 (length erc-nick-uniquifier))))
4220 erc-nick-uniquifier)))
4221 (erc-cmd-NICK newnick)
4222 (erc-display-error-notice
4224 (format "Nickname %s is %s, trying %s"
4225 nick reason newnick)))))
4227 ;;; Server messages
4229 (defgroup erc-server-hooks nil
4230 "Server event callbacks.
4231 Every server event - like numeric replies - has its own hook.
4232 Those hooks are all called using `run-hook-with-args-until-success'.
4233 They receive as first argument the process object from where the event
4234 originated from,
4235 and as second argument the event parsed as a vector."
4236 :group 'erc-hooks)
4238 (defun erc-display-server-message (_proc parsed)
4239 "Display the message sent by the server as a notice."
4240 (erc-display-message
4241 parsed 'notice 'active (erc-response.contents parsed)))
4243 (defun erc-auto-query (proc parsed)
4244 ;; FIXME: This needs more documentation, unless it's not a user function --
4245 ;; Lawrence 2004-01-08
4246 "Put this on `erc-server-PRIVMSG-functions'."
4247 (when erc-auto-query
4248 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
4249 (target (car (erc-response.command-args parsed)))
4250 (msg (erc-response.contents parsed))
4251 (query (if (not erc-query-on-unjoined-chan-privmsg)
4252 nick
4253 (if (erc-current-nick-p target)
4254 nick
4255 target))))
4256 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
4257 (or erc-query-on-unjoined-chan-privmsg
4258 (string= target (erc-current-nick)))
4259 (not (erc-get-buffer query proc))
4260 (not (erc-is-message-ctcp-and-not-action-p msg))
4261 (let ((erc-query-display erc-auto-query))
4262 (erc-cmd-QUERY query))
4263 nil))))
4265 (defun erc-is-message-ctcp-p (message)
4266 "Check if MESSAGE is a CTCP message or not."
4267 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
4269 (defun erc-is-message-ctcp-and-not-action-p (message)
4270 "Check if MESSAGE is a CTCP message or not."
4271 (and (erc-is-message-ctcp-p message)
4272 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
4274 (defun erc-format-privmessage (nick msg privp msgp)
4275 "Format a PRIVMSG in an insertable fashion."
4276 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
4277 (mark-e (if msgp (if privp "*" ">") "-"))
4278 (str (format "%s%s%s %s" mark-s nick mark-e msg))
4279 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
4280 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
4281 ;; add text properties to text before the nick, the nick and after the nick
4282 (erc-put-text-property 0 (length mark-s) 'font-lock-face msg-face str)
4283 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
4284 'font-lock-face nick-face str)
4285 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
4286 'font-lock-face msg-face str)
4287 str))
4289 (defcustom erc-format-nick-function 'erc-format-nick
4290 "Function to format a nickname for message display."
4291 :group 'erc-display
4292 :type 'function)
4294 (defun erc-format-nick (&optional user _channel-data)
4295 "Return the nickname of USER.
4296 See also `erc-format-nick-function'."
4297 (when user (erc-server-user-nickname user)))
4299 (defun erc-get-user-mode-prefix (user)
4300 (when user
4301 (cond ((erc-channel-user-owner-p user)
4302 (erc-propertize "~" 'help-echo "owner"))
4303 ((erc-channel-user-admin-p user)
4304 (erc-propertize "&" 'help-echo "admin"))
4305 ((erc-channel-user-op-p user)
4306 (erc-propertize "@" 'help-echo "operator"))
4307 ((erc-channel-user-halfop-p user)
4308 (erc-propertize "%" 'help-echo "half-op"))
4309 ((erc-channel-user-voice-p user)
4310 (erc-propertize "+" 'help-echo "voice"))
4311 (t ""))))
4313 (defun erc-format-@nick (&optional user _channel-data)
4314 "Format the nickname of USER showing if USER has a voice, is an
4315 operator, half-op, admin or owner. Owners have \"~\", admins have
4316 \"&\", operators have \"@\" and users with voice have \"+\" as a
4317 prefix. Use CHANNEL-DATA to determine op and voice status. See
4318 also `erc-format-nick-function'."
4319 (when user
4320 (let ((nick (erc-server-user-nickname user)))
4321 (concat (erc-propertize
4322 (erc-get-user-mode-prefix nick)
4323 'font-lock-face 'erc-nick-prefix-face)
4324 nick))))
4326 (defun erc-format-my-nick ()
4327 "Return the beginning of this user's message, correctly propertized."
4328 (if erc-show-my-nick
4329 (let* ((open "<")
4330 (close "> ")
4331 (nick (erc-current-nick))
4332 (mode (erc-get-user-mode-prefix nick)))
4333 (concat
4334 (erc-propertize open 'font-lock-face 'erc-default-face)
4335 (erc-propertize mode 'font-lock-face 'erc-my-nick-prefix-face)
4336 (erc-propertize nick 'font-lock-face 'erc-my-nick-face)
4337 (erc-propertize close 'font-lock-face 'erc-default-face)))
4338 (let ((prefix "> "))
4339 (erc-propertize prefix 'font-lock-face 'erc-default-face))))
4341 (defun erc-echo-notice-in-default-buffer (s parsed buffer _sender)
4342 "Echos a private notice in the default buffer, namely the
4343 target buffer specified by BUFFER, or there is no target buffer,
4344 the server buffer. This function is designed to be added to
4345 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
4346 and always returns t."
4347 (erc-display-message parsed nil buffer s)
4350 (defun erc-echo-notice-in-target-buffer (s parsed buffer _sender)
4351 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
4352 function is designed to be added to either `erc-echo-notice-hook'
4353 or `erc-echo-notice-always-hook', and returns non-nil if BUFFER
4354 is non-nil."
4355 (if buffer
4356 (progn (erc-display-message parsed nil buffer s) t)
4357 nil))
4359 (defun erc-echo-notice-in-minibuffer (s _parsed _buffer _sender)
4360 "Echos a private notice in the minibuffer. This function is
4361 designed to be added to either `erc-echo-notice-hook' or
4362 `erc-echo-notice-always-hook', and always returns t."
4363 (message "%s" (concat "NOTICE: " s))
4366 (defun erc-echo-notice-in-server-buffer (s parsed _buffer _sender)
4367 "Echos a private notice in the server buffer. This function is
4368 designed to be added to either `erc-echo-notice-hook' or
4369 `erc-echo-notice-always-hook', and always returns t."
4370 (erc-display-message parsed nil nil s)
4373 (defun erc-echo-notice-in-active-non-server-buffer (s parsed _buffer _sender)
4374 "Echos a private notice in the active buffer if the active
4375 buffer is not the server buffer. This function is designed to be
4376 added to either `erc-echo-notice-hook' or
4377 `erc-echo-notice-always-hook', and returns non-nil if the active
4378 buffer is not the server buffer."
4379 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
4380 (progn (erc-display-message parsed nil 'active s) t)
4381 nil))
4383 (defun erc-echo-notice-in-active-buffer (s parsed _buffer _sender)
4384 "Echos a private notice in the active 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 'active s)
4390 (defun erc-echo-notice-in-user-buffers (s parsed _buffer sender)
4391 "Echos a private notice in all of the buffers for which SENDER
4392 is a member. This function is designed to be added to either
4393 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4394 returns non-nil if there is at least one buffer for which the
4395 sender is a member.
4397 See also: `erc-echo-notice-in-first-user-buffer',
4398 `erc-buffer-list-with-nick'."
4399 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4400 (if buffers
4401 (progn (erc-display-message parsed nil buffers s) t)
4402 nil)))
4404 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
4405 "Echos a private notice in BUFFER and in all of the buffers for
4406 which SENDER is a member. This function is designed to be added
4407 to either `erc-echo-notice-hook' or
4408 `erc-echo-notice-always-hook', and returns non-nil if there is
4409 at least one buffer for which the sender is a member or the
4410 default target.
4412 See also: `erc-echo-notice-in-user-buffers',
4413 `erc-buffer-list-with-nick'."
4414 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4415 (unless (memq buffer buffers) (push buffer buffers))
4416 (if buffers ;FIXME: How could it be nil?
4417 (progn (erc-display-message parsed nil buffers s) t)
4418 nil)))
4420 (defun erc-echo-notice-in-first-user-buffer (s parsed _buffer sender)
4421 "Echos a private notice in one of the buffers for which SENDER
4422 is a member. This function is designed to be added to either
4423 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4424 returns non-nil if there is at least one buffer for which the
4425 sender is a member.
4427 See also: `erc-echo-notice-in-user-buffers',
4428 `erc-buffer-list-with-nick'."
4429 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4430 (if buffers
4431 (progn (erc-display-message parsed nil (car buffers) s) t)
4432 nil)))
4434 ;;; Ban manipulation
4436 (defun erc-banlist-store (proc parsed)
4437 "Record ban entries for a channel."
4438 (pcase-let ((`(,channel ,mask ,whoset)
4439 (cdr (erc-response.command-args parsed))))
4440 ;; Determine to which buffer the message corresponds
4441 (let ((buffer (erc-get-buffer channel proc)))
4442 (with-current-buffer buffer
4443 (unless (member (cons whoset mask) erc-channel-banlist)
4444 (setq erc-channel-banlist (cons (cons whoset mask)
4445 erc-channel-banlist))))))
4446 nil)
4448 (defun erc-banlist-finished (proc parsed)
4449 "Record that we have received the banlist."
4450 (let* ((channel (nth 1 (erc-response.command-args parsed)))
4451 (buffer (erc-get-buffer channel proc)))
4452 (with-current-buffer buffer
4453 (put 'erc-channel-banlist 'received-from-server t)))
4454 t) ; suppress the 'end of banlist' message
4456 (defun erc-banlist-update (proc parsed)
4457 "Check MODE commands for bans and update the banlist appropriately."
4458 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
4459 (let* ((tgt (car (erc-response.command-args parsed)))
4460 (mode (erc-response.contents parsed))
4461 (whoset (erc-response.sender parsed))
4462 (buffer (erc-get-buffer tgt proc)))
4463 (when buffer
4464 (with-current-buffer buffer
4465 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
4466 ((string-match "^\\([+-]\\)b" mode)
4467 ;; This is a ban
4468 (cond
4469 ((string-match "^-" mode)
4470 ;; Remove the unbanned masks from the ban list
4471 (setq erc-channel-banlist
4472 (erc-delete-if
4473 #'(lambda (y)
4474 (member (upcase (cdr y))
4475 (mapcar #'upcase
4476 (cdr (split-string mode)))))
4477 erc-channel-banlist)))
4478 ((string-match "^+" mode)
4479 ;; Add the banned mask(s) to the ban list
4480 (mapc
4481 (lambda (mask)
4482 (unless (member (cons whoset mask) erc-channel-banlist)
4483 (setq erc-channel-banlist
4484 (cons (cons whoset mask) erc-channel-banlist))))
4485 (cdr (split-string mode))))))))))
4486 nil)
4488 ;; used for the banlist cmds
4489 (defun erc-group-list (list n)
4490 "Group LIST into sublists of length N."
4491 (cond ((null list) nil)
4492 ((null (nthcdr n list)) (list list))
4493 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
4496 ;;; MOTD numreplies
4498 (defun erc-handle-login ()
4499 "Handle the logging in process of connection."
4500 (unless erc-logged-in
4501 (setq erc-logged-in t)
4502 (message "Logging in as `%s'... done" (erc-current-nick))
4503 ;; execute a startup script
4504 (let ((f (erc-select-startup-file)))
4505 (when f
4506 (erc-load-script f)))))
4508 (defun erc-connection-established (proc parsed)
4509 "Run just after connection.
4511 Set user modes and run `erc-after-connect' hook."
4512 (with-current-buffer (process-buffer proc)
4513 (unless erc-server-connected ; only once per session
4514 (let ((server (or erc-server-announced-name
4515 (erc-response.sender parsed)))
4516 (nick (car (erc-response.command-args parsed)))
4517 (buffer (process-buffer proc)))
4518 (setq erc-server-connected t)
4519 (setq erc-server-reconnect-count 0)
4520 (erc-update-mode-line)
4521 (erc-set-initial-user-mode nick buffer)
4522 (erc-server-setup-periodical-ping buffer)
4523 (run-hook-with-args 'erc-after-connect server nick)))))
4525 (defun erc-set-initial-user-mode (nick buffer)
4526 "If `erc-user-mode' is non-nil for NICK, set the user modes.
4527 The server buffer is given by BUFFER."
4528 (with-current-buffer buffer
4529 (when erc-user-mode
4530 (let ((mode (if (functionp erc-user-mode)
4531 (funcall erc-user-mode)
4532 erc-user-mode)))
4533 (when (stringp mode)
4534 (erc-log (format "changing mode for %s to %s" nick mode))
4535 (erc-server-send (format "MODE %s %s" nick mode)))))))
4537 (defun erc-display-error-notice (parsed string)
4538 "Display STRING as an error notice.
4540 See also `erc-display-message'."
4541 (erc-display-message
4542 parsed '(notice error) 'active string))
4544 (defun erc-process-ctcp-query (proc parsed nick login host)
4545 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4546 "Process a CTCP query."
4547 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4548 "\C-a"))))
4549 (if (> (length queries) 4)
4550 (erc-display-message
4551 parsed (list 'notice 'error) proc 'ctcp-too-many)
4552 (if (= 0 (length queries))
4553 (erc-display-message
4554 parsed (list 'notice 'error) proc
4555 'ctcp-empty ?n nick)
4556 (while queries
4557 (let* ((type (upcase (car (split-string (car queries)))))
4558 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4559 (if (and hook (boundp hook))
4560 (if (string-equal type "ACTION")
4561 (run-hook-with-args-until-success
4562 hook proc parsed nick login host
4563 (car (erc-response.command-args parsed))
4564 (car queries))
4565 (when erc-paranoid
4566 (if (erc-current-nick-p
4567 (car (erc-response.command-args parsed)))
4568 (erc-display-message
4569 parsed 'error 'active 'ctcp-request
4570 ?n nick ?u login ?h host ?r (car queries))
4571 (erc-display-message
4572 parsed 'error 'active 'ctcp-request-to
4573 ?n nick ?u login ?h host ?r (car queries)
4574 ?t (car (erc-response.command-args parsed)))))
4575 (run-hook-with-args-until-success
4576 hook proc nick login host
4577 (car (erc-response.command-args parsed))
4578 (car queries)))
4579 (erc-display-message
4580 parsed (list 'notice 'error) proc
4581 'undefined-ctcp)))
4582 (setq queries (cdr queries)))))))
4584 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4586 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4587 "Respond to a CTCP ACTION query."
4588 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4589 (let ((s (match-string 1 msg))
4590 (buf (or (erc-get-buffer to proc)
4591 (erc-get-buffer nick proc)
4592 (process-buffer proc))))
4593 (erc-display-message
4594 parsed 'action buf
4595 'ACTION ?n nick ?u login ?h host ?a s))))
4597 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4599 (defun erc-ctcp-query-CLIENTINFO (_proc nick _login _host _to msg)
4600 "Respond to a CTCP CLIENTINFO query."
4601 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4602 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4603 (unless erc-disable-ctcp-replies
4604 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4605 nil)
4607 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4608 (defun erc-ctcp-query-ECHO (_proc nick _login _host _to msg)
4609 "Respond to a CTCP ECHO query."
4610 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4611 (let ((s (match-string 1 msg)))
4612 (unless erc-disable-ctcp-replies
4613 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4614 nil)
4616 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4617 (defun erc-ctcp-query-FINGER (_proc nick _login _host _to _msg)
4618 "Respond to a CTCP FINGER query."
4619 (unless erc-disable-ctcp-replies
4620 (let ((s (if erc-anonymous-login
4621 (format "FINGER I'm %s." (erc-current-nick))
4622 (format "FINGER %s (%s@%s)."
4623 (user-full-name)
4624 (user-login-name)
4625 (system-name))))
4626 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4627 (when (> ns 0)
4628 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4629 (erc-send-ctcp-notice nick s)))
4630 nil)
4632 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4633 (defun erc-ctcp-query-PING (_proc nick _login _host _to msg)
4634 "Respond to a CTCP PING query."
4635 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4636 (unless erc-disable-ctcp-replies
4637 (let ((arg (match-string 1 msg)))
4638 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4639 nil)
4641 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4642 (defun erc-ctcp-query-TIME (_proc nick _login _host _to _msg)
4643 "Respond to a CTCP TIME query."
4644 (unless erc-disable-ctcp-replies
4645 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4646 nil)
4648 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4649 (defun erc-ctcp-query-USERINFO (_proc nick _login _host _to _msg)
4650 "Respond to a CTCP USERINFO query."
4651 (unless erc-disable-ctcp-replies
4652 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4653 nil)
4655 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4656 (defun erc-ctcp-query-VERSION (_proc nick _login _host _to _msg)
4657 "Respond to a CTCP VERSION query."
4658 (unless erc-disable-ctcp-replies
4659 (erc-send-ctcp-notice
4660 nick (format
4661 "VERSION \C-bERC\C-b - an IRC client for Emacs %s (\C-b%s\C-b)"
4662 emacs-version
4663 erc-official-location)))
4664 nil)
4666 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4667 "Process MSG as a CTCP reply."
4668 (let* ((type (car (split-string msg)))
4669 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4670 (if (boundp hook)
4671 (run-hook-with-args-until-success
4672 hook proc nick login host
4673 (car (erc-response.command-args parsed)) msg)
4674 (erc-display-message
4675 parsed 'notice 'active
4676 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4678 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4679 (defun erc-ctcp-reply-ECHO (_proc nick _login _host _to msg)
4680 "Handle a CTCP ECHO reply."
4681 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4682 (let ((message (match-string 1 msg)))
4683 (erc-display-message
4684 nil '(notice action) 'active
4685 'CTCP-ECHO ?n nick ?m message)))
4686 nil)
4688 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4689 (defun erc-ctcp-reply-CLIENTINFO (_proc nick _login _host _to msg)
4690 "Handle a CTCP CLIENTINFO reply."
4691 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4692 (let ((message (match-string 1 msg)))
4693 (erc-display-message
4694 nil 'notice 'active
4695 'CTCP-CLIENTINFO ?n nick ?m message)))
4696 nil)
4698 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4699 (defun erc-ctcp-reply-FINGER (_proc nick _login _host _to msg)
4700 "Handle a CTCP FINGER reply."
4701 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4702 (let ((message (match-string 1 msg)))
4703 (erc-display-message
4704 nil 'notice 'active
4705 'CTCP-FINGER ?n nick ?m message)))
4706 nil)
4708 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4709 (defun erc-ctcp-reply-PING (_proc nick _login _host _to msg)
4710 "Handle a CTCP PING reply."
4711 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4713 (let ((time (match-string 1 msg)))
4714 (condition-case nil
4715 (let ((delta (erc-time-diff (string-to-number time)
4716 (erc-current-time))))
4717 (erc-display-message
4718 nil 'notice 'active
4719 'CTCP-PING ?n nick
4720 ?t (erc-sec-to-time delta)))
4721 (range-error
4722 (erc-display-message
4723 nil 'error 'active
4724 'bad-ping-response ?n nick ?t time))))))
4726 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4727 (defun erc-ctcp-reply-TIME (_proc nick _login _host _to msg)
4728 "Handle a CTCP TIME reply."
4729 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4730 (let ((message (match-string 1 msg)))
4731 (erc-display-message
4732 nil 'notice 'active
4733 'CTCP-TIME ?n nick ?m message)))
4734 nil)
4736 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4737 (defun erc-ctcp-reply-VERSION (_proc nick _login _host _to msg)
4738 "Handle a CTCP VERSION reply."
4739 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4740 (let ((message (match-string 1 msg)))
4741 (erc-display-message
4742 nil 'notice 'active
4743 'CTCP-VERSION ?n nick ?m message)))
4744 nil)
4746 (defun erc-process-away (proc away-p)
4747 "Toggle the away status of the user depending on the value of AWAY-P.
4749 If nil, set the user as away.
4750 If non-nil, return from being away."
4751 (let ((sessionbuf (process-buffer proc)))
4752 (when sessionbuf
4753 (with-current-buffer sessionbuf
4754 (when erc-away-nickname
4755 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4756 erc-away-nickname away-p))
4757 (erc-cmd-NICK (if away-p
4758 erc-away-nickname
4759 erc-nick)))
4760 (cond
4761 (away-p
4762 (setq erc-away (current-time)))
4764 (let ((away-time erc-away))
4765 ;; away must be set to NIL BEFORE sending anything to prevent
4766 ;; an infinite recursion
4767 (setq erc-away nil)
4768 (with-current-buffer (erc-active-buffer)
4769 (when erc-public-away-p
4770 (erc-send-action
4771 (erc-default-target)
4772 (if away-time
4773 (format "is back (gone for %s)"
4774 (erc-sec-to-time
4775 (erc-time-diff
4776 (erc-emacs-time-to-erc-time away-time)
4777 (erc-current-time))))
4778 "is back")))))))))
4779 (erc-update-mode-line)))
4781 ;;;; List of channel members handling
4783 (defun erc-channel-begin-receiving-names ()
4784 "Internal function.
4786 Used when a channel names list is about to be received. Should
4787 be called with the current buffer set to the channel buffer.
4789 See also `erc-channel-end-receiving-names'."
4790 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4792 (defun erc-channel-end-receiving-names ()
4793 "Internal function.
4795 Used to fix `erc-channel-users' after a channel names list has been
4796 received. Should be called with the current buffer set to the
4797 channel buffer.
4799 See also `erc-channel-begin-receiving-names'."
4800 (maphash (lambda (nick _user)
4801 (if (null (gethash nick erc-channel-new-member-names))
4802 (erc-remove-channel-user nick)))
4803 erc-channel-users)
4804 (setq erc-channel-new-member-names nil))
4806 (defun erc-parse-prefix ()
4807 "Return an alist of valid prefix character types and their representations.
4808 Example: (operator) o => @, (voiced) v => +."
4809 (let ((str (or (cdr (assoc "PREFIX" (erc-with-server-buffer
4810 erc-server-parameters)))
4811 ;; provide a sane default
4812 "(qaohv)~&@%+"))
4813 types chars)
4814 (when (string-match "^(\\([^)]+\\))\\(.+\\)$" str)
4815 (setq types (match-string 1 str)
4816 chars (match-string 2 str))
4817 (let ((len (min (length types) (length chars)))
4818 (i 0)
4819 (alist nil))
4820 (while (< i len)
4821 (setq alist (cons (cons (elt types i) (elt chars i))
4822 alist))
4823 (setq i (1+ i)))
4824 alist))))
4826 (defun erc-channel-receive-names (names-string)
4827 "This function is for internal use only.
4829 Update `erc-channel-users' according to NAMES-STRING.
4830 NAMES-STRING is a string listing some of the names on the
4831 channel."
4832 (let* ((prefix (erc-parse-prefix))
4833 (voice-ch (cdr (assq ?v prefix)))
4834 (op-ch (cdr (assq ?o prefix)))
4835 (hop-ch (cdr (assq ?h prefix)))
4836 (adm-ch (cdr (assq ?a prefix)))
4837 (own-ch (cdr (assq ?q prefix)))
4838 (names (delete "" (split-string names-string)))
4839 name op voice halfop admin owner)
4840 (let ((erc-channel-members-changed-hook nil))
4841 (dolist (item names)
4842 (let ((updatep t)
4843 (ch (aref item 0)))
4844 (setq name item op 'off voice 'off halfop 'off admin 'off owner 'off)
4845 (if (rassq ch prefix)
4846 (if (= (length item) 1)
4847 (setq updatep nil)
4848 (setq name (substring item 1))
4849 (setf (pcase ch
4850 ((pred (eq voice-ch)) voice)
4851 ((pred (eq hop-ch)) halfop)
4852 ((pred (eq op-ch)) op)
4853 ((pred (eq adm-ch)) admin)
4854 ((pred (eq own-ch)) owner)
4855 (_ (message "Unknown prefix char `%S'" ch) voice))
4856 'on)))
4857 (when updatep
4858 ;; If we didn't issue the NAMES request (consider two clients
4859 ;; talking to an IRC proxy), `erc-channel-begin-receiving-names'
4860 ;; will not have been called, so we have to do it here.
4861 (unless erc-channel-new-member-names
4862 (erc-channel-begin-receiving-names))
4863 (puthash (erc-downcase name) t
4864 erc-channel-new-member-names)
4865 (erc-update-current-channel-member
4866 name name t voice halfop op admin owner)))))
4867 (run-hooks 'erc-channel-members-changed-hook)))
4870 (defcustom erc-channel-members-changed-hook nil
4871 "This hook is called every time the variable `channel-members' changes.
4872 The buffer where the change happened is current while this hook is called."
4873 :group 'erc-hooks
4874 :type 'hook)
4876 (defun erc-update-user-nick (nick &optional new-nick
4877 host login full-name info)
4878 "Update the stored user information for the user with nickname NICK.
4880 See also: `erc-update-user'."
4881 (erc-update-user (erc-get-server-user nick) new-nick
4882 host login full-name info))
4884 (defun erc-update-user (user &optional new-nick
4885 host login full-name info)
4886 "Update user info for USER. USER must be an erc-server-user
4887 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4888 non-nil and not equal to the existing values for USER are used to
4889 replace the stored values in USER.
4891 If, and only if, a change is made,
4892 `erc-channel-members-changed-hook' is run for each channel for
4893 which USER is a member, and t is returned."
4894 (let (changed)
4895 (when user
4896 (when (and new-nick
4897 (not (equal (erc-server-user-nickname user)
4898 new-nick)))
4899 (setq changed t)
4900 (erc-change-user-nickname user new-nick))
4901 (when (and host
4902 (not (equal (erc-server-user-host user) host)))
4903 (setq changed t)
4904 (setf (erc-server-user-host user) host))
4905 (when (and login
4906 (not (equal (erc-server-user-login user) login)))
4907 (setq changed t)
4908 (setf (erc-server-user-login user) login))
4909 (when (and full-name
4910 (not (equal (erc-server-user-full-name user)
4911 full-name)))
4912 (setq changed t)
4913 (setf (erc-server-user-full-name user) full-name))
4914 (when (and info
4915 (not (equal (erc-server-user-info user) info)))
4916 (setq changed t)
4917 (setf (erc-server-user-info user) info))
4918 (if changed
4919 (dolist (buf (erc-server-user-buffers user))
4920 (if (buffer-live-p buf)
4921 (with-current-buffer buf
4922 (run-hooks 'erc-channel-members-changed-hook))))))
4923 changed))
4925 (defun erc-update-current-channel-member
4926 (nick new-nick &optional add voice halfop op admin owner host login full-name info
4927 update-message-time)
4928 "Update the stored user information for the user with nickname NICK.
4929 `erc-update-user' is called to handle changes to nickname,
4930 HOST, LOGIN, FULL-NAME, and INFO. If VOICE HALFOP OP ADMIN or OWNER
4931 are non-nil, they must be equal to either `on' or `off', in which
4932 case the status of the user in the current channel is changed accordingly.
4933 If UPDATE-MESSAGE-TIME is non-nil, the last-message-time of the user
4934 in the current channel is set to (current-time).
4936 If ADD is non-nil, the user will be added with the specified
4937 information if it is not already present in the user or channel
4938 lists.
4940 If, and only if, changes are made, or the user is added,
4941 `erc-channel-members-updated-hook' is run, and t is returned.
4943 See also: `erc-update-user' and `erc-update-channel-member'."
4944 (let* (changed user-changed
4945 (channel-data (erc-get-channel-user nick))
4946 (cuser (cdr channel-data))
4947 (user (if channel-data (car channel-data)
4948 (erc-get-server-user nick))))
4949 (if cuser
4950 (progn
4951 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4952 (when (and voice
4953 (not (eq (erc-channel-user-voice cuser) voice)))
4954 (setq changed t)
4955 (setf (erc-channel-user-voice cuser)
4956 (cond ((eq voice 'on) t)
4957 ((eq voice 'off) nil)
4958 (t voice))))
4959 (when (and halfop
4960 (not (eq (erc-channel-user-halfop cuser) halfop)))
4961 (setq changed t)
4962 (setf (erc-channel-user-halfop cuser)
4963 (cond ((eq halfop 'on) t)
4964 ((eq halfop 'off) nil)
4965 (t halfop))))
4966 (when (and op
4967 (not (eq (erc-channel-user-op cuser) op)))
4968 (setq changed t)
4969 (setf (erc-channel-user-op cuser)
4970 (cond ((eq op 'on) t)
4971 ((eq op 'off) nil)
4972 (t op))))
4973 (when (and admin
4974 (not (eq (erc-channel-user-admin cuser) admin)))
4975 (setq changed t)
4976 (setf (erc-channel-user-admin cuser)
4977 (cond ((eq admin 'on) t)
4978 ((eq admin 'off) nil)
4979 (t admin))))
4980 (when (and owner
4981 (not (eq (erc-channel-user-owner cuser) owner)))
4982 (setq changed t)
4983 (setf (erc-channel-user-owner cuser)
4984 (cond ((eq owner 'on) t)
4985 ((eq owner 'off) nil)
4986 (t owner))))
4987 (when update-message-time
4988 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4989 (setq user-changed
4990 (erc-update-user user new-nick
4991 host login full-name info)))
4992 (when add
4993 (if (null user)
4994 (progn
4995 (setq user (make-erc-server-user
4996 :nickname nick
4997 :host host
4998 :full-name full-name
4999 :login login
5000 :info info
5001 :buffers (list (current-buffer))))
5002 (erc-add-server-user nick user))
5003 (setf (erc-server-user-buffers user)
5004 (cons (current-buffer)
5005 (erc-server-user-buffers user))))
5006 (setq cuser (make-erc-channel-user
5007 :voice (cond ((eq voice 'on) t)
5008 ((eq voice 'off) nil)
5009 (t voice))
5010 :halfop (cond ((eq halfop 'on) t)
5011 ((eq halfop 'off) nil)
5012 (t halfop))
5013 :op (cond ((eq op 'on) t)
5014 ((eq op 'off) nil)
5015 (t op))
5016 :admin (cond ((eq admin 'on) t)
5017 ((eq admin 'off) nil)
5018 (t admin))
5019 :owner (cond ((eq owner 'on) t)
5020 ((eq owner 'off) nil)
5021 (t owner))
5022 :last-message-time
5023 (if update-message-time (current-time))))
5024 (puthash (erc-downcase nick) (cons user cuser)
5025 erc-channel-users)
5026 (setq changed t)))
5027 (when (and changed (null user-changed))
5028 (run-hooks 'erc-channel-members-changed-hook))
5029 (or changed user-changed add)))
5031 (defun erc-update-channel-member (channel nick new-nick
5032 &optional add voice halfop op admin owner host login
5033 full-name info update-message-time)
5034 "Update user and channel information for the user with
5035 nickname NICK in channel CHANNEL.
5037 See also: `erc-update-current-channel-member'."
5038 (erc-with-buffer
5039 (channel)
5040 (erc-update-current-channel-member nick new-nick add voice halfop op admin owner host
5041 login full-name info
5042 update-message-time)))
5044 (defun erc-remove-current-channel-member (nick)
5045 "Remove NICK from current channel membership list.
5046 Runs `erc-channel-members-changed-hook'."
5047 (let ((channel-data (erc-get-channel-user nick)))
5048 (when channel-data
5049 (erc-remove-channel-user nick)
5050 (run-hooks 'erc-channel-members-changed-hook))))
5052 (defun erc-remove-channel-member (channel nick)
5053 "Remove NICK from CHANNEL's membership list.
5055 See also `erc-remove-current-channel-member'."
5056 (erc-with-buffer
5057 (channel)
5058 (erc-remove-current-channel-member nick)))
5060 (defun erc-update-channel-topic (channel topic &optional modify)
5061 "Find a buffer for CHANNEL and set the TOPIC for it.
5063 If optional MODIFY is `append' or `prepend', then append or prepend the
5064 TOPIC string to the current topic."
5065 (erc-with-buffer (channel)
5066 (cond ((eq modify 'append)
5067 (setq erc-channel-topic (concat erc-channel-topic topic)))
5068 ((eq modify 'prepend)
5069 (setq erc-channel-topic (concat topic erc-channel-topic)))
5070 (t (setq erc-channel-topic topic)))
5071 (erc-update-mode-line-buffer (current-buffer))))
5073 (defun erc-set-modes (tgt mode-string)
5074 "Set the modes for the TGT provided as MODE-STRING."
5075 (let* ((modes (erc-parse-modes mode-string))
5076 (add-modes (nth 0 modes))
5077 ;; list of triples: (mode-char 'on/'off argument)
5078 (arg-modes (nth 2 modes)))
5079 (cond ((erc-channel-p tgt); channel modes
5080 (let ((buf (and erc-server-process
5081 (erc-get-buffer tgt erc-server-process))))
5082 (when buf
5083 (with-current-buffer buf
5084 (setq erc-channel-modes add-modes)
5085 (setq erc-channel-user-limit nil)
5086 (setq erc-channel-key nil)
5087 (while arg-modes
5088 (let ((mode (nth 0 (car arg-modes)))
5089 (onoff (nth 1 (car arg-modes)))
5090 (arg (nth 2 (car arg-modes))))
5091 (cond ((string-match "^[Ll]" mode)
5092 (erc-update-channel-limit tgt onoff arg))
5093 ((string-match "^[Kk]" mode)
5094 (erc-update-channel-key tgt onoff arg))
5095 (t nil)))
5096 (setq arg-modes (cdr arg-modes)))
5097 (erc-update-mode-line-buffer buf)))))
5098 ;; we do not keep our nick's modes yet
5099 ;;(t (setq erc-user-modes add-modes))
5103 (defun erc-sort-strings (list-of-strings)
5104 "Sort LIST-OF-STRINGS in lexicographic order.
5106 Side-effect free."
5107 (sort (copy-sequence list-of-strings) 'string<))
5109 (defun erc-parse-modes (mode-string)
5110 "Parse MODE-STRING into a list.
5112 Returns a list of three elements:
5114 (ADD-MODES REMOVE-MODES ARG-MODES).
5116 The add-modes and remove-modes are lists of single-character strings
5117 for modes without parameters to add and remove respectively. The
5118 arg-modes is a list of triples of the form:
5120 (MODE-CHAR ON/OFF ARGUMENT)."
5121 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
5122 (let ((chars (mapcar #'char-to-string (match-string 1 mode-string)))
5123 ;; arguments in channel modes
5124 (args-str (match-string 2 mode-string))
5125 (args nil)
5126 (add-modes nil)
5127 (remove-modes nil)
5128 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
5129 (add-p t))
5130 ;; make the argument list
5131 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
5132 (setq args (cons (match-string 1 args-str) args))
5133 (setq args-str (match-string 2 args-str)))
5134 (setq args (nreverse args))
5135 ;; collect what modes changed, and match them with arguments
5136 (while chars
5137 (cond ((string= (car chars) "+") (setq add-p t))
5138 ((string= (car chars) "-") (setq add-p nil))
5139 ((string-match "^[qaovhbQAOVHB]" (car chars))
5140 (setq arg-modes (cons (list (car chars)
5141 (if add-p 'on 'off)
5142 (if args (car args) nil))
5143 arg-modes))
5144 (if args (setq args (cdr args))))
5145 ((string-match "^[LlKk]" (car chars))
5146 (setq arg-modes (cons (list (car chars)
5147 (if add-p 'on 'off)
5148 (if (and add-p args)
5149 (car args) nil))
5150 arg-modes))
5151 (if (and add-p args) (setq args (cdr args))))
5152 (add-p (setq add-modes (cons (car chars) add-modes)))
5153 (t (setq remove-modes (cons (car chars) remove-modes))))
5154 (setq chars (cdr chars)))
5155 (setq add-modes (nreverse add-modes))
5156 (setq remove-modes (nreverse remove-modes))
5157 (setq arg-modes (nreverse arg-modes))
5158 (list add-modes remove-modes arg-modes))
5159 nil))
5161 (defun erc-update-modes (tgt mode-string &optional _nick _host _login)
5162 "Update the mode information for TGT, provided as MODE-STRING.
5163 Optional arguments: NICK, HOST and LOGIN - the attributes of the
5164 person who changed the modes."
5165 ;; FIXME: neither of nick, host, and login are used!
5166 (let* ((modes (erc-parse-modes mode-string))
5167 (add-modes (nth 0 modes))
5168 (remove-modes (nth 1 modes))
5169 ;; list of triples: (mode-char 'on/'off argument)
5170 (arg-modes (nth 2 modes)))
5171 ;; now parse the modes changes and do the updates
5172 (cond ((erc-channel-p tgt); channel modes
5173 (let ((buf (and erc-server-process
5174 (erc-get-buffer tgt erc-server-process))))
5175 (when buf
5176 ;; FIXME! This used to have an original buffer
5177 ;; variable, but it never switched back to the original
5178 ;; buffer. Is this wanted behavior?
5179 (set-buffer buf)
5180 (if (not (boundp 'erc-channel-modes))
5181 (setq erc-channel-modes nil))
5182 (while remove-modes
5183 (setq erc-channel-modes (delete (car remove-modes)
5184 erc-channel-modes)
5185 remove-modes (cdr remove-modes)))
5186 (while add-modes
5187 (setq erc-channel-modes (cons (car add-modes)
5188 erc-channel-modes)
5189 add-modes (cdr add-modes)))
5190 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
5191 (while arg-modes
5192 (let ((mode (nth 0 (car arg-modes)))
5193 (onoff (nth 1 (car arg-modes)))
5194 (arg (nth 2 (car arg-modes))))
5195 (cond ((string-match "^[Vv]" mode)
5196 (erc-update-channel-member tgt arg arg nil onoff))
5197 ((string-match "^[hH]" mode)
5198 (erc-update-channel-member tgt arg arg nil nil onoff))
5199 ((string-match "^[oO]" mode)
5200 (erc-update-channel-member tgt arg arg nil nil nil onoff))
5201 ((string-match "^[aA]" mode)
5202 (erc-update-channel-member tgt arg arg nil nil nil nil onoff))
5203 ((string-match "^[qQ]" mode)
5204 (erc-update-channel-member tgt arg arg nil nil nil nil nil onoff))
5205 ((string-match "^[Ll]" mode)
5206 (erc-update-channel-limit tgt onoff arg))
5207 ((string-match "^[Kk]" mode)
5208 (erc-update-channel-key tgt onoff arg))
5209 (t nil)); only ops are tracked now
5210 (setq arg-modes (cdr arg-modes))))
5211 (erc-update-mode-line buf))))
5212 ;; nick modes - ignored at this point
5213 (t nil))))
5215 (defun erc-update-channel-limit (channel onoff n)
5216 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
5217 "Update CHANNEL's user limit to N."
5218 (if (or (not (eq onoff 'on))
5219 (and (stringp n) (string-match "^[0-9]+$" n)))
5220 (erc-with-buffer
5221 (channel)
5222 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
5223 (t (setq erc-channel-user-limit nil))))))
5225 (defun erc-update-channel-key (channel onoff key)
5226 "Update CHANNEL's key to KEY if ONOFF is `on' or to nil if it's `off'."
5227 (erc-with-buffer
5228 (channel)
5229 (cond ((eq onoff 'on) (setq erc-channel-key key))
5230 (t (setq erc-channel-key nil)))))
5232 (defun erc-handle-user-status-change (type nlh &optional l)
5233 "Handle changes in any user's status.
5235 So far, only nick change is handled.
5237 Generally, the TYPE argument is a symbol describing the change type, NLH is
5238 a list containing the original nickname, login name and hostname for the user,
5239 and L is a list containing additional TYPE-specific arguments.
5241 So far the following TYPE/L pairs are supported:
5243 Event TYPE L
5245 nickname change `nick' (NEW-NICK)"
5246 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
5247 (cond
5248 ;; nickname change
5249 ((equal type 'nick)
5252 nil)))
5254 (defun erc-highlight-notice (s)
5255 "Highlight notice message S and return it.
5256 See also variable `erc-notice-highlight-type'."
5257 (cond
5258 ((eq erc-notice-highlight-type 'prefix)
5259 (erc-put-text-property 0 (length erc-notice-prefix)
5260 'font-lock-face 'erc-notice-face s)
5262 ((eq erc-notice-highlight-type 'all)
5263 (erc-put-text-property 0 (length s) 'font-lock-face 'erc-notice-face s)
5265 (t s)))
5267 (defun erc-make-notice (message)
5268 "Notify the user of MESSAGE."
5269 (when erc-minibuffer-notice
5270 (message "%s" message))
5271 (erc-highlight-notice (concat erc-notice-prefix message)))
5273 (defun erc-highlight-error (s)
5274 "Highlight error message S and return it."
5275 (erc-put-text-property 0 (length s) 'font-lock-face 'erc-error-face s)
5278 (defun erc-put-text-property (start end property value &optional object)
5279 "Set text-property for an object (usually a string).
5280 START and END define the characters covered.
5281 PROPERTY is the text-property set, usually the symbol `face'.
5282 VALUE is the value for the text-property, usually a face symbol such as
5283 the face `bold' or `erc-pal-face'.
5284 OBJECT is a string which will be modified and returned.
5285 OBJECT is modified without being copied first.
5287 You can redefine or `defadvice' this function in order to add
5288 EmacsSpeak support."
5289 (put-text-property start end property value object))
5291 (defun erc-list (thing)
5292 "Return THING if THING is a list, or a list with THING as its element."
5293 (if (listp thing)
5294 thing
5295 (list thing)))
5297 (defun erc-parse-user (string)
5298 "Parse STRING as a user specification (nick!login@host).
5300 Return a list of the three separate tokens."
5301 (cond
5302 ((string-match "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$" string)
5303 (list (match-string 1 string)
5304 (match-string 2 string)
5305 (match-string 3 string)))
5306 ;; Some bogus bouncers send Nick!(null), try to live with that.
5307 ((string-match "^\\([^!\n]*\\)!\\(.*\\)$" string)
5308 (list (match-string 1 string)
5310 (match-string 2 string)))
5312 (list string "" ""))))
5314 (defun erc-extract-nick (string)
5315 "Return the nick corresponding to a user specification STRING.
5317 See also `erc-parse-user'."
5318 (car (erc-parse-user string)))
5320 (defun erc-put-text-properties (start end properties
5321 &optional object value-list)
5322 "Set text-properties for OBJECT.
5324 START and END describe positions in OBJECT.
5325 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
5326 each property to the corresponding value in VALUE-LIST."
5327 (unless value-list
5328 (setq value-list (mapcar (lambda (_x) t)
5329 properties)))
5330 (while (and properties value-list)
5331 (erc-put-text-property
5332 start end (pop properties) (pop value-list) object)))
5334 ;;; Input area handling:
5336 (defun erc-beg-of-input-line ()
5337 "Return the value of `point' at the beginning of the input line.
5339 Specifically, return the position of `erc-insert-marker'."
5340 (or (and (boundp 'erc-insert-marker)
5341 (markerp erc-insert-marker))
5342 (error "erc-insert-marker has no value, please report a bug"))
5343 (marker-position erc-insert-marker))
5345 (defun erc-end-of-input-line ()
5346 "Return the value of `point' at the end of the input line."
5347 (point-max))
5349 (defvar erc-last-input-time 0
5350 "Time of last call to `erc-send-current-line'.
5351 If that function has never been called, the value is 0.")
5353 (defcustom erc-accidental-paste-threshold-seconds 0.2
5354 "Minimum time, in seconds, before sending new lines via IRC.
5355 If the value is a number, `erc-send-current-line' signals an error
5356 if its previous invocation was fewer than this many seconds ago.
5357 This is useful so that if you accidentally enter large amounts of text
5358 into the ERC buffer, that text is not sent to the IRC server.
5360 If the value is nil, `erc-send-current-line' always considers any
5361 submitted line to be intentional."
5362 :group 'erc
5363 :version "26.1"
5364 :type '(choice number (other :tag "disabled" nil)))
5366 (defun erc-send-current-line ()
5367 "Parse current line and send it to IRC."
5368 (interactive)
5369 (let ((now (float-time)))
5370 (if (or (not erc-accidental-paste-threshold-seconds)
5371 (< erc-accidental-paste-threshold-seconds
5372 (- now erc-last-input-time)))
5373 (save-restriction
5374 (widen)
5375 (if (< (point) (erc-beg-of-input-line))
5376 (erc-error "Point is not in the input area")
5377 (let ((inhibit-read-only t)
5378 (str (erc-user-input))
5379 (old-buf (current-buffer)))
5380 (if (and (not (erc-server-buffer-live-p))
5381 (not (erc-command-no-process-p str)))
5382 (erc-error "ERC: No process running")
5383 (erc-set-active-buffer (current-buffer))
5384 ;; Kill the input and the prompt
5385 (delete-region (erc-beg-of-input-line)
5386 (erc-end-of-input-line))
5387 (unwind-protect
5388 (erc-send-input str)
5389 ;; Fix the buffer if the command didn't kill it
5390 (when (buffer-live-p old-buf)
5391 (with-current-buffer old-buf
5392 (save-restriction
5393 (widen)
5394 (goto-char (point-max))
5395 (when (processp erc-server-process)
5396 (set-marker (process-mark erc-server-process) (point)))
5397 (set-marker erc-insert-marker (point))
5398 (let ((buffer-modified (buffer-modified-p)))
5399 (erc-display-prompt)
5400 (set-buffer-modified-p buffer-modified))))))
5402 ;; Only when last hook has been run...
5403 (run-hook-with-args 'erc-send-completed-hook str))))
5404 (setq erc-last-input-time now))
5405 (switch-to-buffer "*ERC Accidental Paste Overflow*")
5406 (lwarn 'erc :warning
5407 "You seem to have accidentally pasted some text!"))))
5409 (defun erc-user-input ()
5410 "Return the input of the user in the current buffer."
5411 (buffer-substring-no-properties
5412 erc-input-marker
5413 (erc-end-of-input-line)))
5415 (defvar erc-command-regexp "^/\\([A-Za-z']+\\)\\(\\s-+.*\\|\\s-*\\)$"
5416 "Regular expression used for matching commands in ERC.")
5418 (defun erc-send-input (input)
5419 "Treat INPUT as typed in by the user. It is assumed that the input
5420 and the prompt is already deleted.
5421 This returns non-nil only if we actually send anything."
5422 ;; Handle different kinds of inputs
5423 (cond
5424 ;; Ignore empty input
5425 ((if erc-send-whitespace-lines
5426 (string= input "")
5427 (string-match "\\`[ \t\r\f\n]*\\'" input))
5428 (when erc-warn-about-blank-lines
5429 (message "Blank line - ignoring...")
5430 (beep))
5431 nil)
5433 (defvar str) ;; FIXME: Make it obey the "erc-" prefix convention.
5434 (let ((str input)
5435 (erc-insert-this t))
5436 (setq erc-send-this t)
5437 (run-hook-with-args 'erc-send-pre-hook input)
5438 (when erc-send-this
5439 (if (or (string-match "\n" str)
5440 (not (string-match erc-command-regexp str)))
5441 (mapc
5442 (lambda (line)
5443 (mapc
5444 (lambda (line)
5445 ;; Insert what has to be inserted for this.
5446 (erc-display-msg line)
5447 (erc-process-input-line (concat line "\n")
5448 (null erc-flood-protect) t))
5449 (or (and erc-flood-protect (erc-split-line line))
5450 (list line))))
5451 (split-string str "\n"))
5452 (erc-process-input-line (concat str "\n") t nil))
5453 t)))))
5455 (defun erc-display-command (line)
5456 (when erc-insert-this
5457 (let ((insert-position (point)))
5458 (unless erc-hide-prompt
5459 (erc-display-prompt nil nil (erc-command-indicator)
5460 (and (erc-command-indicator)
5461 'erc-command-indicator-face)))
5462 (let ((beg (point)))
5463 (insert line)
5464 (erc-put-text-property beg (point)
5465 'font-lock-face 'erc-command-indicator-face)
5466 (insert "\n"))
5467 (when (processp erc-server-process)
5468 (set-marker (process-mark erc-server-process) (point)))
5469 (set-marker erc-insert-marker (point))
5470 (save-excursion
5471 (save-restriction
5472 (narrow-to-region insert-position (point))
5473 (run-hooks 'erc-send-modify-hook)
5474 (run-hooks 'erc-send-post-hook))))))
5476 (defun erc-display-msg (line)
5477 "Display LINE as a message of the user to the current target at the
5478 current position."
5479 (when erc-insert-this
5480 (let ((insert-position (point)))
5481 (insert (erc-format-my-nick))
5482 (let ((beg (point)))
5483 (insert line)
5484 (erc-put-text-property beg (point)
5485 'font-lock-face 'erc-input-face))
5486 (insert "\n")
5487 (when (processp erc-server-process)
5488 (set-marker (process-mark erc-server-process) (point)))
5489 (set-marker erc-insert-marker (point))
5490 (save-excursion
5491 (save-restriction
5492 (narrow-to-region insert-position (point))
5493 (run-hooks 'erc-send-modify-hook)
5494 (run-hooks 'erc-send-post-hook))))))
5496 (defun erc-command-symbol (command)
5497 "Return the ERC command symbol for COMMAND if it exists and is bound."
5498 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
5499 (when (fboundp cmd) cmd)))
5501 (defun erc-extract-command-from-line (line)
5502 "Extract command and args from the input LINE.
5503 If no command was given, return nil. If command matches, return a
5504 list of the form: (command args) where both elements are strings."
5505 (when (string-match erc-command-regexp line)
5506 (let* ((cmd (erc-command-symbol (match-string 1 line)))
5507 ;; note: return is nil, we apply this simply for side effects
5508 (_canon-defun (while (and cmd (symbolp (symbol-function cmd)))
5509 (setq cmd (symbol-function cmd))))
5510 (cmd-fun (or cmd #'erc-cmd-default))
5511 (arg (if cmd
5512 (if (get cmd-fun 'do-not-parse-args)
5513 (format "%s" (match-string 2 line))
5514 (delete "" (split-string (erc-trim-string
5515 (match-string 2 line)) " ")))
5516 line)))
5517 (list cmd-fun arg))))
5519 (defun erc-split-multiline-safe (string)
5520 "Split STRING, containing multiple lines and return them in a list.
5521 Do it only for STRING as the complete input, do not carry unfinished
5522 strings over to the next call."
5523 (let ((l ())
5524 (i0 0)
5525 (doit t))
5526 (while doit
5527 (let ((i (string-match "\r?\n" string i0))
5528 (s (substring string i0)))
5529 (cond (i (setq l (cons (substring string i0 i) l))
5530 (setq i0 (match-end 0)))
5531 ((> (length s) 0)
5532 (setq l (cons s l))(setq doit nil))
5533 (t (setq doit nil)))))
5534 (nreverse l)))
5536 ;; nick handling
5538 (defun erc-set-current-nick (nick)
5539 "Set the current nickname to NICK."
5540 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5541 (erc-server-buffer)
5542 (current-buffer))
5543 (setq erc-server-current-nick nick)))
5545 (defun erc-current-nick ()
5546 "Return the current nickname."
5547 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5548 (erc-server-buffer)
5549 (current-buffer))
5550 erc-server-current-nick))
5552 (defun erc-current-nick-p (nick)
5553 "Return non-nil if NICK is the current nickname."
5554 (erc-nick-equal-p nick (erc-current-nick)))
5556 (defun erc-nick-equal-p (nick1 nick2)
5557 "Return non-nil if NICK1 and NICK2 are the same.
5559 This matches strings according to the IRC protocol's case convention.
5561 See also `erc-downcase'."
5562 (string= (erc-downcase nick1)
5563 (erc-downcase nick2)))
5565 ;; default target handling
5567 (defun erc-default-target ()
5568 "Return the current default target (as a character string) or nil if none."
5569 (let ((tgt (car erc-default-recipients)))
5570 (cond
5571 ((not tgt) nil)
5572 ((listp tgt) (cdr tgt))
5573 (t tgt))))
5575 (defun erc-add-default-channel (channel)
5576 "Add CHANNEL to the default channel list."
5577 (let ((chl (downcase channel)))
5578 (setq erc-default-recipients
5579 (cons chl erc-default-recipients))))
5581 (defun erc-delete-default-channel (channel &optional buffer)
5582 "Delete CHANNEL from the default channel list."
5583 (with-current-buffer (if (and buffer
5584 (bufferp buffer))
5585 buffer
5586 (current-buffer))
5587 (setq erc-default-recipients (delete (downcase channel)
5588 erc-default-recipients))))
5590 (defun erc-add-query (nickname)
5591 "Add QUERY'd NICKNAME to the default channel list.
5593 The previous default target of QUERY type gets removed."
5594 (let ((d1 (car erc-default-recipients))
5595 (d2 (cdr erc-default-recipients))
5596 (qt (cons 'QUERY (downcase nickname))))
5597 (setq erc-default-recipients (cons qt (if (and (listp d1)
5598 (eq (car d1) 'QUERY))
5600 erc-default-recipients)))))
5602 (defun erc-delete-query ()
5603 "Delete the topmost target if it is a QUERY."
5605 (let ((d1 (car erc-default-recipients))
5606 (d2 (cdr erc-default-recipients)))
5607 (if (and (listp d1)
5608 (eq (car d1) 'QUERY))
5609 (setq erc-default-recipients d2)
5610 (error "Current target is not a QUERY"))))
5612 (defun erc-ignored-user-p (spec)
5613 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5615 Takes a full SPEC of a user in the form \"nick!login@host\", and
5616 matches against all the regexp's in `erc-ignore-list'. If any
5617 match, returns that regexp."
5618 (catch 'found
5619 (dolist (ignored (erc-with-server-buffer erc-ignore-list))
5620 (if (string-match ignored spec)
5621 (throw 'found ignored)))))
5623 (defun erc-ignored-reply-p (msg tgt proc)
5624 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5625 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5627 Takes a message MSG to a channel and returns non-nil if the addressed
5628 user matches any regexp in `erc-ignore-reply-list'."
5629 (let ((target-nick (erc-message-target msg)))
5630 (if (not target-nick)
5632 (erc-with-buffer (tgt proc)
5633 (let ((user (erc-get-server-user target-nick)))
5634 (when user
5635 (erc-list-match erc-ignore-reply-list
5636 (erc-user-spec user))))))))
5638 (defun erc-message-target (msg)
5639 "Return the addressed target in MSG.
5641 The addressed target is the string before the first colon in MSG."
5642 (if (string-match "^\\([^: \n]*\\):" msg)
5643 (match-string 1 msg)
5644 nil))
5646 (defun erc-user-spec (user)
5647 "Create a nick!user@host spec from a user struct."
5648 (let ((nick (erc-server-user-nickname user))
5649 (host (erc-server-user-host user))
5650 (login (erc-server-user-login user)))
5651 (concat (or nick "")
5653 (or login "")
5655 (or host ""))))
5657 (defun erc-list-match (lst str)
5658 "Return non-nil if any regexp in LST matches STR."
5659 (memq nil (mapcar (lambda (regexp)
5660 (not (string-match regexp str)))
5661 lst)))
5663 ;; other "toggles"
5665 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5666 "Toggle automatic CTCP replies (like VERSION and PING).
5668 If ARG is positive, turns CTCP replies on.
5670 If ARG is non-nil and not positive, turns CTCP replies off."
5671 (interactive "P")
5672 (cond ((and (numberp arg) (> arg 0))
5673 (setq erc-disable-ctcp-replies t))
5674 (arg (setq erc-disable-ctcp-replies nil))
5675 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5676 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5678 (defun erc-toggle-flood-control (&optional arg)
5679 "Toggle use of flood control on sent messages.
5681 If ARG is positive, use flood control.
5682 If ARG is non-nil and not positive, do not use flood control.
5684 See `erc-server-flood-margin' for an explanation of the available
5685 flood control parameters."
5686 (interactive "P")
5687 (cond ((and (numberp arg) (> arg 0))
5688 (setq erc-flood-protect t))
5689 (arg (setq erc-flood-protect nil))
5690 (t (setq erc-flood-protect (not erc-flood-protect))))
5691 (message "ERC flood control is %s"
5692 (cond (erc-flood-protect "ON")
5693 (t "OFF"))))
5695 ;; Some useful channel and nick commands for fast key bindings
5697 (defun erc-invite-only-mode (&optional arg)
5698 "Turn on the invite only mode (+i) for the current channel.
5700 If ARG is non-nil, turn this mode off (-i).
5702 This command is sent even if excess flood is detected."
5703 (interactive "P")
5704 (erc-set-active-buffer (current-buffer))
5705 (let ((tgt (erc-default-target)))
5706 (if (or (not tgt) (not (erc-channel-p tgt)))
5707 (erc-display-message nil 'error (current-buffer) 'no-target)
5708 (erc-load-irc-script-lines
5709 (list (concat "/mode " tgt (if arg " -i" " +i")))
5710 t))))
5712 (defun erc-get-channel-mode-from-keypress (key)
5713 "Read a key sequence and call the corresponding channel mode function.
5714 After doing C-c C-o, type in a channel mode letter.
5716 C-g means quit.
5717 RET lets you type more than one mode at a time.
5718 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5719 If \"k\" is pressed, `erc-set-channel-key' gets called.
5720 Anything else will be sent to `erc-toggle-channel-mode'."
5721 (interactive "kChannel mode (RET to set more than one): ")
5722 (when (featurep 'xemacs)
5723 (setq key (char-to-string (event-to-character (aref key 0)))))
5724 (cond ((equal key "\C-g")
5725 (keyboard-quit))
5726 ((equal key "\C-m")
5727 (erc-insert-mode-command))
5728 ((equal key "l")
5729 (call-interactively 'erc-set-channel-limit))
5730 ((equal key "k")
5731 (call-interactively 'erc-set-channel-key))
5732 (t (erc-toggle-channel-mode key))))
5734 (defun erc-toggle-channel-mode (mode &optional channel)
5735 "Toggle channel MODE.
5737 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5738 `erc-default-target'."
5739 (interactive "P")
5740 (erc-set-active-buffer (current-buffer))
5741 (let ((tgt (or channel (erc-default-target))))
5742 (if (or (null tgt) (null (erc-channel-p tgt)))
5743 (erc-display-message nil 'error 'active 'no-target)
5744 (let* ((active (member mode erc-channel-modes))
5745 (newstate (if active "OFF" "ON")))
5746 (erc-log (format "%s: Toggle mode %s %s" tgt mode newstate))
5747 (message "Toggle channel mode %s %s" mode newstate)
5748 (erc-server-send (format "MODE %s %s%s"
5749 tgt (if active "-" "+") mode))))))
5751 (defun erc-insert-mode-command ()
5752 "Insert the line \"/mode <current target> \" at `point'."
5753 (interactive)
5754 (let ((tgt (erc-default-target)))
5755 (if tgt (insert (concat "/mode " tgt " "))
5756 (erc-display-message nil 'error (current-buffer) 'no-target))))
5758 (defun erc-channel-names ()
5759 "Run \"/names #channel\" in the current channel."
5760 (interactive)
5761 (erc-set-active-buffer (current-buffer))
5762 (let ((tgt (erc-default-target)))
5763 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5764 (erc-display-message nil 'error (current-buffer) 'no-target))))
5766 (defun erc-remove-text-properties-region (start end &optional object)
5767 "Clears the region (START,END) in OBJECT from all colors, etc."
5768 (interactive "r")
5769 (save-excursion
5770 (let ((inhibit-read-only t))
5771 (set-text-properties start end nil object))))
5772 (put 'erc-remove-text-properties-region 'disabled t)
5774 ;; script execution and startup
5776 (defun erc-find-file (file &optional path)
5777 "Search for a FILE in the filesystem.
5778 First the `default-directory' is searched for FILE, then any directories
5779 specified in the list PATH.
5781 If FILE is found, return the path to it."
5782 (let ((filepath file))
5783 (if (file-readable-p filepath) filepath
5784 (while (and path
5785 (progn (setq filepath (expand-file-name file (car path)))
5786 (not (file-readable-p filepath))))
5787 (setq path (cdr path)))
5788 (if path filepath nil))))
5790 (defun erc-select-startup-file ()
5791 "Select an ERC startup file.
5792 See also `erc-startup-file-list'."
5793 (catch 'found
5794 (dolist (f erc-startup-file-list)
5795 (setq f (convert-standard-filename f))
5796 (when (file-readable-p f)
5797 (throw 'found f)))))
5799 (defun erc-find-script-file (file)
5800 "Search for FILE in `default-directory', and any in `erc-script-path'."
5801 (erc-find-file file erc-script-path))
5803 (defun erc-load-script (file)
5804 "Load a script from FILE.
5806 FILE must be the full name, it is not searched in the
5807 `erc-script-path'. If the filename ends with `.el', then load it
5808 as an Emacs Lisp program. Otherwise, treat it as a regular IRC
5809 script."
5810 (erc-log (concat "erc-load-script: " file))
5811 (cond
5812 ((string-match "\\.el\\'" file)
5813 (load file))
5815 (erc-load-irc-script file))))
5817 (defun erc-process-script-line (line &optional args)
5818 "Process an IRC script LINE.
5820 Does script-specific substitutions (script arguments, current nick,
5821 server, etc.) in LINE and returns it.
5823 Substitutions are: %C and %c = current target (channel or nick),
5824 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5825 where x is any other character;
5826 $* = the entire argument string, $1 = the first argument, $2 = the second,
5827 and so on."
5828 (if (not args) (setq args ""))
5829 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5830 (percent-regexp "\\(%.\\)")
5831 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5832 (tgt (erc-default-target))
5833 (server (and (boundp 'erc-session-server) erc-session-server))
5834 (nick (erc-current-nick))
5835 (res "")
5836 (tmp nil)
5837 (arg-list nil)
5838 (arg-num 0))
5839 (if (not tgt) (setq tgt ""))
5840 (if (not server) (setq server ""))
5841 (if (not nick) (setq nick ""))
5842 ;; First, compute the argument list
5843 (setq tmp args)
5844 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5845 (setq arg-list (cons (match-string 1 tmp) arg-list))
5846 (setq tmp (match-string 2 tmp)))
5847 (setq arg-list (nreverse arg-list))
5848 (setq arg-num (length arg-list))
5849 ;; now do the substitution
5850 (setq tmp (string-match esc-regexp line))
5851 (while tmp
5852 ;;(message "beginning of while: tmp=%S" tmp)
5853 (let* ((hd (substring line 0 tmp))
5854 (esc "")
5855 (subst "")
5856 (tail (substring line tmp)))
5857 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5858 (setq esc (match-string 1 tail))
5859 (setq tail (substring tail (match-end 1))))
5860 ((string-match (concat "^" percent-regexp) tail)
5861 (setq esc (match-string 1 tail))
5862 (setq tail (substring tail (match-end 1)))))
5863 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5864 (setq res (concat res hd))
5865 (setq subst
5866 (cond ((string= esc "") "")
5867 ((string-match "^\\$\\*$" esc) args)
5868 ((string-match "^\\$\\([0-9]+\\)$" esc)
5869 (let ((n (string-to-number (match-string 1 esc))))
5870 (message "n = %S, integerp(n)=%S" n (integerp n))
5871 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5872 ((string-match "^%[Cc]$" esc) tgt)
5873 ((string-match "^%[Ss]$" esc) server)
5874 ((string-match "^%[Nn]$" esc) nick)
5875 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5876 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5877 (message "BUG IN ERC: esc=%S" esc)
5878 "")))
5879 (setq line tail)
5880 (setq tmp (string-match esc-regexp line))
5881 (setq res (concat res subst))
5882 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5884 (setq res (concat res line))
5885 res))
5887 (defun erc-load-irc-script (file &optional force)
5888 "Load an IRC script from FILE."
5889 (erc-log (concat "erc-load-script: " file))
5890 (let ((str (with-temp-buffer
5891 (insert-file-contents file)
5892 (buffer-string))))
5893 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5895 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5896 "Load IRC script LINES (a list of strings).
5898 If optional NOEXPAND is non-nil, do not expand script-specific
5899 sequences, process the lines verbatim. Use this for multiline
5900 user input."
5901 (let* ((cb (current-buffer))
5902 (s "")
5903 (sp (or (erc-command-indicator) (erc-prompt)))
5904 (args (and (boundp 'erc-script-args) erc-script-args)))
5905 (if (and args (string-match "^ " args))
5906 (setq args (substring args 1)))
5907 ;; prepare the prompt string for echo
5908 (erc-put-text-property 0 (length sp)
5909 'font-lock-face 'erc-command-indicator-face sp)
5910 (while lines
5911 (setq s (car lines))
5912 (erc-log (concat "erc-load-script: CMD: " s))
5913 (unless (string-match "^\\s-*$" s)
5914 (let ((line (if noexpand s (erc-process-script-line s args))))
5915 (if (and (erc-process-input-line line force)
5916 erc-script-echo)
5917 (progn
5918 (erc-put-text-property 0 (length line)
5919 'font-lock-face 'erc-input-face line)
5920 (erc-display-line (concat sp line) cb)))))
5921 (setq lines (cdr lines)))))
5923 ;; authentication
5925 (defun erc-login ()
5926 "Perform user authentication at the IRC server."
5927 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5928 (erc-current-nick)
5929 (user-login-name)
5930 (or erc-system-name (system-name))
5931 erc-session-server
5932 erc-session-user-full-name))
5933 (if erc-session-password
5934 (erc-server-send (format "PASS %s" erc-session-password))
5935 (message "Logging in without password"))
5936 (erc-server-send (format "NICK %s" (erc-current-nick)))
5937 (erc-server-send
5938 (format "USER %s %s %s :%s"
5939 ;; hacked - S.B.
5940 (if erc-anonymous-login erc-email-userid (user-login-name))
5941 "0" "*"
5942 erc-session-user-full-name))
5943 (erc-update-mode-line))
5945 ;; connection properties' heuristics
5947 (defun erc-determine-parameters (&optional server port nick name)
5948 "Determine the connection and authentication parameters.
5949 Sets the buffer local variables:
5951 - `erc-session-connector'
5952 - `erc-session-server'
5953 - `erc-session-port'
5954 - `erc-session-full-name'
5955 - `erc-server-current-nick'"
5956 (setq erc-session-connector erc-server-connect-function
5957 erc-session-server (erc-compute-server server)
5958 erc-session-port (or port erc-default-port)
5959 erc-session-user-full-name (erc-compute-full-name name))
5960 (erc-set-current-nick (erc-compute-nick nick)))
5962 (defun erc-compute-server (&optional server)
5963 "Return an IRC server name.
5965 This tries a number of increasingly more default methods until a
5966 non-nil value is found.
5968 - SERVER (the argument passed to this function)
5969 - The `erc-server' option
5970 - The value of the IRCSERVER environment variable
5971 - The `erc-default-server' variable"
5972 (or server
5973 erc-server
5974 (getenv "IRCSERVER")
5975 erc-default-server))
5977 (defun erc-compute-nick (&optional nick)
5978 "Return user's IRC nick.
5980 This tries a number of increasingly more default methods until a
5981 non-nil value is found.
5983 - NICK (the argument passed to this function)
5984 - The `erc-nick' option
5985 - The value of the IRCNICK environment variable
5986 - The result from the `user-login-name' function"
5987 (or nick
5988 (if (consp erc-nick) (car erc-nick) erc-nick)
5989 (getenv "IRCNICK")
5990 (user-login-name)))
5993 (defun erc-compute-full-name (&optional full-name)
5994 "Return user's full name.
5996 This tries a number of increasingly more default methods until a
5997 non-nil value is found.
5999 - FULL-NAME (the argument passed to this function)
6000 - The `erc-user-full-name' option
6001 - The value of the IRCNAME environment variable
6002 - The result from the `user-full-name' function"
6003 (or full-name
6004 erc-user-full-name
6005 (getenv "IRCNAME")
6006 (if erc-anonymous-login "unknown" nil)
6007 (user-full-name)))
6009 (defun erc-compute-port (&optional port)
6010 "Return a port for an IRC server.
6012 This tries a number of increasingly more default methods until a
6013 non-nil value is found.
6015 - PORT (the argument passed to this function)
6016 - The `erc-port' option
6017 - The `erc-default-port' variable"
6018 (or port erc-port erc-default-port))
6020 ;; time routines
6022 (defun erc-string-to-emacs-time (string)
6023 "Convert the long number represented by STRING into an Emacs format.
6024 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
6025 (let* ((n (string-to-number (concat string ".0"))))
6026 (list (truncate (/ n 65536))
6027 (truncate (mod n 65536)))))
6029 (defalias 'erc-emacs-time-to-erc-time 'float-time)
6030 (defalias 'erc-current-time 'float-time)
6032 (defun erc-time-diff (t1 t2)
6033 "Return the time difference in seconds between T1 and T2."
6034 (abs (- t2 t1)))
6036 (defun erc-time-gt (t1 t2)
6037 "Check whether T1 > T2."
6038 (> t1 t2))
6040 (defun erc-sec-to-time (ns)
6041 "Convert NS to a time string HH:MM.SS."
6042 (setq ns (truncate ns))
6043 (format "%02d:%02d.%02d"
6044 (/ ns 3600)
6045 (/ (% ns 3600) 60)
6046 (% ns 60)))
6048 (defun erc-seconds-to-string (seconds)
6049 "Convert a number of SECONDS into an English phrase."
6050 (let (days hours minutes format-args output)
6051 (setq days (/ seconds 86400)
6052 seconds (% seconds 86400)
6053 hours (/ seconds 3600)
6054 seconds (% seconds 3600)
6055 minutes (/ seconds 60)
6056 seconds (% seconds 60)
6057 format-args (if (> days 0)
6058 `("%d days, %d hours, %d minutes, %d seconds"
6059 ,days ,hours ,minutes ,seconds)
6060 (if (> hours 0)
6061 `("%d hours, %d minutes, %d seconds"
6062 ,hours ,minutes ,seconds)
6063 (if (> minutes 0)
6064 `("%d minutes, %d seconds" ,minutes ,seconds)
6065 `("%d seconds" ,seconds))))
6066 output (apply #'format format-args))
6067 ;; Change all "1 units" to "1 unit".
6068 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
6069 (setq output (erc-replace-match-subexpression-in-string
6070 "" output (match-string 2 output) 2 (match-beginning 2))))
6071 output))
6074 ;; info
6076 (defconst erc-clientinfo-alist
6077 '(("ACTION" . "is used to inform about one's current activity")
6078 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
6079 ("ECHO" . "echoes its arguments back")
6080 ("FINGER" . "shows user's name, location, and idle time")
6081 ("PING" . "measures delay between peers")
6082 ("TIME" . "shows client-side time")
6083 ("USERINFO" . "shows information provided by a user")
6084 ("VERSION" . "shows client type and version"))
6085 "Alist of CTCP CLIENTINFO for ERC commands.")
6087 (defun erc-client-info (s)
6088 "Return CTCP CLIENTINFO on command S.
6089 If S is nil or an empty string then return general CLIENTINFO."
6090 (if (or (not s) (string= s ""))
6091 (concat
6092 (apply #'concat
6093 (mapcar (lambda (e)
6094 (concat (car e) " "))
6095 erc-clientinfo-alist))
6096 ": use CLIENTINFO <COMMAND> to get more specific information")
6097 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
6098 (if h
6099 (concat s " " (cdr h))
6100 (concat s ": unknown command")))))
6102 ;; Hook functions
6104 (defun erc-directory-writable-p (dir)
6105 "Determine whether DIR is a writable directory.
6106 If it doesn't exist, create it."
6107 (unless (file-attributes dir) (make-directory dir))
6108 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
6110 (defun erc-kill-query-buffers (process)
6111 "Kill all buffers of PROCESS.
6112 Does nothing if PROCESS is not a process object."
6113 ;; here, we only want to match the channel buffers, to avoid
6114 ;; "selecting killed buffers" b0rkage.
6115 (when (processp process)
6116 (erc-with-all-buffers-of-server process
6117 (lambda ()
6118 (not (erc-server-buffer-p)))
6119 (kill-buffer (current-buffer)))))
6121 (defun erc-nick-at-point ()
6122 "Give information about the nickname at `point'.
6124 If called interactively, give a human readable message in the
6125 minibuffer. If called programmatically, return the corresponding
6126 entry of `channel-members'."
6127 (interactive)
6128 (require 'thingatpt)
6129 (let* ((word (word-at-point))
6130 (channel-data (erc-get-channel-user word))
6131 (cuser (cdr channel-data))
6132 (user (if channel-data
6133 (car channel-data)
6134 (erc-get-server-user word)))
6135 host login full-name nick voice halfop op admin owner)
6136 (when user
6137 (setq nick (erc-server-user-nickname user)
6138 host (erc-server-user-host user)
6139 login (erc-server-user-login user)
6140 full-name (erc-server-user-full-name user))
6141 (if cuser
6142 (setq voice (erc-channel-user-voice cuser)
6143 halfop (erc-channel-user-halfop cuser)
6144 op (erc-channel-user-op cuser)
6145 admin (erc-channel-user-admin cuser)
6146 owner (erc-channel-user-owner cuser))))
6147 (if (called-interactively-p 'interactive)
6148 (message "%s is %s@%s%s%s"
6149 nick login host
6150 (if full-name (format " (%s)" full-name) "")
6151 (if (or voice halfop op admin owner)
6152 (format " and is +%s%s%s%s%s on %s"
6153 (if voice "v" "")
6154 (if halfop "h" "")
6155 (if op "o" "")
6156 (if admin "a" "")
6157 (if owner "q" "")
6158 (erc-default-target))
6159 ""))
6160 user)))
6162 (defun erc-away-time ()
6163 "Return non-nil if the current ERC process is set away.
6165 In particular, the time that we were set away is returned.
6166 See `current-time' for details on the time format."
6167 (erc-with-server-buffer erc-away))
6169 ;; Mode line handling
6171 (defcustom erc-mode-line-format "%S %a"
6172 "A string to be formatted and shown in the mode-line in `erc-mode'.
6174 The string is formatted using `format-spec' and the result is set as the value
6175 of `mode-line-buffer-identification'.
6177 The following characters are replaced:
6178 %a: String indicating away status or \"\" if you are not away
6179 %l: The estimated lag time to the server
6180 %m: The modes of the channel
6181 %n: The current nick name
6182 %N: The name of the network
6183 %o: The topic of the channel
6184 %p: The session port
6185 %t: The name of the target (channel, nickname, or servername:port)
6186 %s: In the server-buffer, this gets filled with the value of
6187 `erc-server-announced-name', in a channel, the value of
6188 (erc-default-target) also get concatenated.
6189 %S: In the server-buffer, this gets filled with the value of
6190 `erc-network', in a channel, the value of (erc-default-target)
6191 also get concatenated."
6192 :group 'erc-mode-line-and-header
6193 :type 'string)
6195 (defcustom erc-header-line-format "%n on %t (%m,%l) %o"
6196 "A string to be formatted and shown in the header-line in `erc-mode'.
6197 Only used starting in Emacs 21.
6199 Set this to nil if you do not want the header line to be
6200 displayed.
6202 See `erc-mode-line-format' for which characters are can be used."
6203 :group 'erc-mode-line-and-header
6204 :set (lambda (sym val)
6205 (set sym val)
6206 (when (fboundp 'erc-update-mode-line)
6207 (erc-update-mode-line nil)))
6208 :type '(choice (const :tag "Disabled" nil)
6209 string))
6211 (defcustom erc-header-line-uses-tabbar-p nil
6212 "Use tabbar mode instead of the header line to display the header."
6213 :group 'erc-mode-line-and-header
6214 :type 'boolean)
6216 (defcustom erc-header-line-uses-help-echo-p t
6217 "Show the contents of the header line in the echo area or as a tooltip
6218 when you move point into the header line."
6219 :group 'erc-mode-line-and-header
6220 :type 'boolean)
6222 (defcustom erc-header-line-face-method nil
6223 "Determine what method to use when colorizing the header line text.
6225 If nil, don't colorize the header text.
6226 If given a function, call it and use the resulting face name.
6227 Otherwise, use the `erc-header-line' face."
6228 :group 'erc-mode-line-and-header
6229 :type '(choice (const :tag "Don't colorize" nil)
6230 (const :tag "Use the erc-header-line face" t)
6231 (function :tag "Call a function")))
6233 (defcustom erc-show-channel-key-p t
6234 "Show the channel key in the header line."
6235 :group 'erc-paranoia
6236 :type 'boolean)
6238 (defcustom erc-mode-line-away-status-format
6239 "(AWAY since %a %b %d %H:%M) "
6240 "When you're away on a server, this is shown in the mode line.
6241 This should be a string with substitution variables recognized by
6242 `format-time-string'."
6243 :group 'erc-mode-line-and-header
6244 :type 'string)
6246 (defun erc-shorten-server-name (server-name)
6247 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
6248 (if (stringp server-name)
6249 (with-temp-buffer
6250 (insert server-name)
6251 (let ((alist erc-common-server-suffixes))
6252 (while alist
6253 (goto-char (point-min))
6254 (if (re-search-forward (caar alist) nil t)
6255 (replace-match (cdar alist)))
6256 (setq alist (cdr alist))))
6257 (buffer-string))))
6259 (defun erc-format-target ()
6260 "Return the name of the target (channel or nickname or servername:port)."
6261 (let ((target (erc-default-target)))
6262 (or target
6263 (concat (erc-shorten-server-name
6264 (or erc-server-announced-name
6265 erc-session-server))
6266 ":" (erc-port-to-string erc-session-port)))))
6268 (defun erc-format-target-and/or-server ()
6269 "Return the server name or the current target and server name combined."
6270 (let ((server-name (erc-shorten-server-name
6271 (or erc-server-announced-name
6272 erc-session-server))))
6273 (cond ((erc-default-target)
6274 (concat (erc-string-no-properties (erc-default-target))
6275 "@" server-name))
6276 (server-name server-name)
6277 (t (buffer-name (current-buffer))))))
6279 (defun erc-format-network ()
6280 "Return the name of the network we are currently on."
6281 (let ((network (and (fboundp 'erc-network-name) (erc-network-name))))
6282 (if (and network (symbolp network))
6283 (symbol-name network)
6284 "")))
6286 (defun erc-format-target-and/or-network ()
6287 "Return the network or the current target and network combined.
6288 If the name of the network is not available, then use the
6289 shortened server name instead."
6290 (let ((network-name (or (and (fboundp 'erc-network-name) (erc-network-name))
6291 (erc-shorten-server-name
6292 (or erc-server-announced-name
6293 erc-session-server)))))
6294 (when (and network-name (symbolp network-name))
6295 (setq network-name (symbol-name network-name)))
6296 (cond ((erc-default-target)
6297 (concat (erc-string-no-properties (erc-default-target))
6298 "@" network-name))
6299 ((and network-name
6300 (not (get-buffer network-name)))
6301 (when erc-rename-buffers
6302 (rename-buffer network-name))
6303 network-name)
6304 (t (buffer-name (current-buffer))))))
6306 (defun erc-format-away-status ()
6307 "Return a formatted `erc-mode-line-away-status-format'
6308 if `erc-away' is non-nil."
6309 (let ((a (erc-away-time)))
6310 (if a
6311 (format-time-string erc-mode-line-away-status-format a)
6312 "")))
6314 (defun erc-format-channel-modes ()
6315 "Return the current channel's modes."
6316 (concat (apply #'concat
6317 "+" erc-channel-modes)
6318 (cond ((and erc-channel-user-limit erc-channel-key)
6319 (if erc-show-channel-key-p
6320 (format "lk %.0f %s" erc-channel-user-limit
6321 erc-channel-key)
6322 (format "kl %.0f" erc-channel-user-limit)))
6323 (erc-channel-user-limit
6324 ;; Emacs has no bignums
6325 (format "l %.0f" erc-channel-user-limit))
6326 (erc-channel-key
6327 (if erc-show-channel-key-p
6328 (format "k %s" erc-channel-key)
6329 "k"))
6330 (t nil))))
6332 (defun erc-format-lag-time ()
6333 "Return the estimated lag time to server, `erc-server-lag'."
6334 (let ((lag (erc-with-server-buffer erc-server-lag)))
6335 (cond (lag (format "lag:%.0f" lag))
6336 (t ""))))
6338 ;; erc-goodies is required at end of this file.
6339 (declare-function erc-controls-strip "erc-goodies" (str))
6341 (defvar tabbar--local-hlf)
6343 (defun erc-update-mode-line-buffer (buffer)
6344 "Update the mode line in a single ERC buffer BUFFER."
6345 (with-current-buffer buffer
6346 (let ((spec (format-spec-make
6347 ?a (erc-format-away-status)
6348 ?l (erc-format-lag-time)
6349 ?m (erc-format-channel-modes)
6350 ?n (or (erc-current-nick) "")
6351 ?N (erc-format-network)
6352 ?o (or (erc-controls-strip erc-channel-topic) "")
6353 ?p (erc-port-to-string erc-session-port)
6354 ?s (erc-format-target-and/or-server)
6355 ?S (erc-format-target-and/or-network)
6356 ?t (erc-format-target)))
6357 (process-status (cond ((and (erc-server-process-alive)
6358 (not erc-server-connected))
6359 ":connecting")
6360 ((erc-server-process-alive)
6363 ": CLOSED")))
6364 (face (cond ((eq erc-header-line-face-method nil)
6365 nil)
6366 ((functionp erc-header-line-face-method)
6367 (funcall erc-header-line-face-method))
6369 'erc-header-line))))
6370 (cond ((featurep 'xemacs)
6371 (setq modeline-buffer-identification
6372 (list (format-spec erc-mode-line-format spec)))
6373 (setq modeline-process (list process-status)))
6375 (setq mode-line-buffer-identification
6376 (list (format-spec erc-mode-line-format spec)))
6377 (setq mode-line-process (list process-status))))
6378 (when (boundp 'header-line-format)
6379 (let ((header (if erc-header-line-format
6380 (format-spec erc-header-line-format spec)
6381 nil)))
6382 (cond (erc-header-line-uses-tabbar-p
6383 (set (make-local-variable 'tabbar--local-hlf)
6384 header-line-format)
6385 (kill-local-variable 'header-line-format))
6386 ((null header)
6387 (setq header-line-format nil))
6388 (erc-header-line-uses-help-echo-p
6389 (let ((help-echo (with-temp-buffer
6390 (insert header)
6391 (fill-region (point-min) (point-max))
6392 (buffer-string))))
6393 (setq header-line-format
6394 (erc-replace-regexp-in-string
6396 "%%"
6397 (if face
6398 (erc-propertize header 'help-echo help-echo
6399 'face face)
6400 (erc-propertize header 'help-echo help-echo))))))
6401 (t (setq header-line-format
6402 (if face
6403 (erc-propertize header 'face face)
6404 header)))))))
6405 (if (featurep 'xemacs)
6406 (redraw-modeline)
6407 (force-mode-line-update))))
6409 (defun erc-update-mode-line (&optional buffer)
6410 "Update the mode line in BUFFER.
6412 If BUFFER is nil, update the mode line in all ERC buffers."
6413 (if (and buffer (bufferp buffer))
6414 (erc-update-mode-line-buffer buffer)
6415 (dolist (buf (erc-buffer-list))
6416 (when (buffer-live-p buf)
6417 (erc-update-mode-line-buffer buf)))))
6419 ;; Miscellaneous
6421 (defun erc-port-to-string (p)
6422 "Convert port P to a string.
6423 P may be an integer or a service name."
6424 (if (integerp p)
6425 (int-to-string p)
6428 (defun erc-string-to-port (s)
6429 "Convert string S to either an integer port number or a service name."
6430 (if (numberp s)
6432 (let ((n (string-to-number s)))
6433 (if (= n 0)
6435 n))))
6437 (defun erc-version (&optional here)
6438 "Show the version number of ERC in the minibuffer.
6439 If optional argument HERE is non-nil, insert version number at point."
6440 (interactive "P")
6441 (let ((version-string
6442 (format "ERC (IRC client for Emacs %s)" emacs-version)))
6443 (if here
6444 (insert version-string)
6445 (if (called-interactively-p 'interactive)
6446 (message "%s" version-string)
6447 version-string))))
6449 (defun erc-modes (&optional here)
6450 "Show the active ERC modes in the minibuffer.
6451 If optional argument HERE is non-nil, insert version number at point."
6452 (interactive "P")
6453 (let ((string
6454 (mapconcat 'identity
6455 (let (modes (case-fold-search nil))
6456 (dolist (var (apropos-internal "^erc-.*mode$"))
6457 (when (and (boundp var)
6458 (symbol-value var))
6459 (setq modes (cons (symbol-name var)
6460 modes))))
6461 modes)
6462 ", ")))
6463 (if here
6464 (insert string)
6465 (if (called-interactively-p 'interactive)
6466 (message "%s" string)
6467 string))))
6469 (defun erc-trim-string (s)
6470 "Trim leading and trailing spaces off S."
6471 (cond
6472 ((not (stringp s)) nil)
6473 ((string-match "^\\s-*$" s)
6475 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
6476 (match-string 1 s))
6478 s)))
6480 (defun erc-arrange-session-in-multiple-windows ()
6481 "Open a window for every non-server buffer related to `erc-session-server'.
6483 All windows are opened in the current frame."
6484 (interactive)
6485 (unless erc-server-process
6486 (error "No erc-server-process found in current buffer"))
6487 (let ((bufs (erc-buffer-list nil erc-server-process)))
6488 (when bufs
6489 (delete-other-windows)
6490 (switch-to-buffer (car bufs))
6491 (setq bufs (cdr bufs))
6492 (while bufs
6493 (split-window)
6494 (other-window 1)
6495 (switch-to-buffer (car bufs))
6496 (setq bufs (cdr bufs))
6497 (balance-windows)))))
6499 (defun erc-popup-input-buffer ()
6500 "Provide an input buffer."
6501 (interactive)
6502 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
6503 (mode (intern
6504 (completing-read
6505 "Mode: "
6506 (mapcar (lambda (e)
6507 (list (symbol-name e)))
6508 (apropos-internal "-mode\\'" 'commandp))
6509 nil t))))
6510 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
6511 (funcall mode)
6512 (narrow-to-region (point) (point))
6513 (shrink-window-if-larger-than-buffer)))
6515 ;;; Message catalog
6517 (defun erc-make-message-variable-name (catalog entry)
6518 "Create a variable name corresponding to CATALOG's ENTRY."
6519 (intern (concat "erc-message-"
6520 (symbol-name catalog) "-" (symbol-name entry))))
6522 (defun erc-define-catalog-entry (catalog entry format-spec)
6523 "Set CATALOG's ENTRY to FORMAT-SPEC."
6524 (set (erc-make-message-variable-name catalog entry)
6525 format-spec))
6527 (defun erc-define-catalog (catalog entries)
6528 "Define a CATALOG according to ENTRIES."
6529 (dolist (entry entries)
6530 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
6532 (erc-define-catalog
6533 'english
6534 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
6535 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
6536 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
6537 (cannot-find-file . "Cannot find file %f")
6538 (cannot-read-file . "Cannot read file %f")
6539 (connect . "Connecting to %S:%p... ")
6540 (country . "%c")
6541 (country-unknown . "%d: No such domain")
6542 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
6543 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
6544 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
6545 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
6546 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
6547 (flood-strict-mode
6548 . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
6549 (disconnected . "\n\nConnection failed! Re-establishing connection...\n")
6550 (disconnected-noreconnect
6551 . "\n\nConnection failed! Not re-establishing connection.\n")
6552 (finished . "\n\n*** ERC finished ***\n")
6553 (terminated . "\n\n*** ERC terminated: %e\n")
6554 (login . "Logging in as `%n'...")
6555 (nick-in-use . "%n is in use. Choose new nickname: ")
6556 (nick-too-long
6557 . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
6558 (no-default-channel . "No default channel")
6559 (no-invitation . "You've got no invitation")
6560 (no-target . "No target")
6561 (ops . "%i operator%s: %o")
6562 (ops-none . "No operators in this channel.")
6563 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
6564 (variable-not-bound . "Variable not bound!")
6565 (ACTION . "* %n %a")
6566 (CTCP-CLIENTINFO . "Client info for %n: %m")
6567 (CTCP-ECHO . "Echo %n: %m")
6568 (CTCP-FINGER . "Finger info for %n: %m")
6569 (CTCP-PING . "Ping time to %n is %t")
6570 (CTCP-TIME . "Time by %n is %m")
6571 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
6572 (CTCP-VERSION . "Version for %n is %m")
6573 (ERROR . "==> ERROR from %s: %c\n")
6574 (INVITE . "%n (%u@%h) invites you to channel %c")
6575 (JOIN . "%n (%u@%h) has joined channel %c")
6576 (JOIN-you . "You have joined channel %c")
6577 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
6578 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
6579 (KICK-by-you . "You have kicked %k off channel %c: %r")
6580 (MODE . "%n (%u@%h) has changed mode for %t to %m")
6581 (MODE-nick . "%n has changed mode for %t to %m")
6582 (NICK . "%n (%u@%h) is now known as %N")
6583 (NICK-you . "Your new nickname is %N")
6584 (PART . erc-message-english-PART)
6585 (PING . "PING from server (last: %s sec. ago)")
6586 (PONG . "PONG from %h (%i second%s)")
6587 (QUIT . "%n (%u@%h) has quit: %r")
6588 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
6589 (WALLOPS . "Wallops from %n: %m")
6590 (s004 . "%s %v %U %C")
6591 (s221 . "User modes for %n: %m")
6592 (s252 . "%i operator(s) online")
6593 (s253 . "%i unknown connection(s)")
6594 (s254 . "%i channels formed")
6595 (s275 . "%n %m")
6596 (s301 . "%n is AWAY: %r")
6597 (s303 . "Is online: %n")
6598 (s305 . "%m")
6599 (s306 . "%m")
6600 (s307 . "%n %m")
6601 (s311 . "%n is %f (%u@%h)")
6602 (s312 . "%n is/was on server %s (%c)")
6603 (s313 . "%n is an IRC operator")
6604 (s314 . "%n was %f (%u@%h)")
6605 (s317 . "%n has been idle for %i")
6606 (s317-on-since . "%n has been idle for %i, on since %t")
6607 (s319 . "%n is on channel(s): %c")
6608 (s320 . "%n is an identified user")
6609 (s321 . "Channel Users Topic")
6610 (s322 . "%c [%u] %t")
6611 (s324 . "%c modes: %m")
6612 (s328 . "%c URL: %u")
6613 (s329 . "%c was created on %t")
6614 (s330 . "%n %a %i")
6615 (s331 . "No topic is set for %c")
6616 (s332 . "Topic for %c: %T")
6617 (s333 . "%c: topic set by %n, %t")
6618 (s341 . "Inviting %n to channel %c")
6619 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
6620 (s353 . "Users on %c: %u")
6621 (s367 . "Ban for %b on %c")
6622 (s367-set-by . "Ban for %b on %c set by %s on %t")
6623 (s368 . "Banlist of %c ends.")
6624 (s379 . "%c: Forwarded to %f")
6625 (s391 . "The time at %s is %t")
6626 (s401 . "%n: No such nick/channel")
6627 (s403 . "%c: No such channel")
6628 (s404 . "%c: Cannot send to channel")
6629 (s405 . "%c: You have joined too many channels")
6630 (s406 . "%n: There was no such nickname")
6631 (s412 . "No text to send")
6632 (s421 . "%c: Unknown command")
6633 (s431 . "No nickname given")
6634 (s432 . "%n is an erroneous nickname")
6635 (s442 . "%c: You're not on that channel")
6636 (s445 . "SUMMON has been disabled")
6637 (s446 . "USERS has been disabled")
6638 (s451 . "You have not registered")
6639 (s461 . "%c: not enough parameters")
6640 (s462 . "Unauthorized command (already registered)")
6641 (s463 . "Your host isn't among the privileged")
6642 (s464 . "Password incorrect")
6643 (s465 . "You are banned from this server")
6644 (s474 . "You can't join %c because you're banned (+b)")
6645 (s475 . "You must specify the correct channel key (+k) to join %c")
6646 (s481 . "Permission Denied - You're not an IRC operator")
6647 (s482 . "You need to be a channel operator of %c to do that")
6648 (s483 . "You can't kill a server!")
6649 (s484 . "Your connection is restricted!")
6650 (s485 . "You're not the original channel operator")
6651 (s491 . "No O-lines for your host")
6652 (s501 . "Unknown MODE flag")
6653 (s502 . "You can't change modes for other users")
6654 (s671 . "%n %a")))
6656 (defun erc-message-english-PART (&rest args)
6657 "Format a proper PART message.
6659 This function is an example on what could be done with formatting
6660 functions."
6661 (let ((nick (cadr (memq ?n args)))
6662 (user (cadr (memq ?u args)))
6663 (host (cadr (memq ?h args)))
6664 (channel (cadr (memq ?c args)))
6665 (reason (cadr (memq ?r args))))
6666 (if (string= nick (erc-current-nick))
6667 (format "You have left channel %s" channel)
6668 (format "%s (%s@%s) has left channel %s%s"
6669 nick user host channel
6670 (if (not (string= reason ""))
6671 (format ": %s"
6672 (erc-replace-regexp-in-string "%" "%%" reason))
6673 "")))))
6676 (defvar erc-current-message-catalog 'english)
6677 (make-variable-buffer-local 'erc-current-message-catalog)
6679 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6680 "Retrieve ENTRY from CATALOG.
6682 If CATALOG is nil, `erc-current-message-catalog' is used.
6684 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6685 english, catalog."
6686 (unless catalog (setq catalog erc-current-message-catalog))
6687 (let ((var (erc-make-message-variable-name catalog entry)))
6688 (if (boundp var)
6689 (symbol-value var)
6690 (when (boundp (erc-make-message-variable-name 'english entry))
6691 (symbol-value (erc-make-message-variable-name 'english entry))))))
6693 (defun erc-format-message (msg &rest args)
6694 "Format MSG according to ARGS.
6696 See also `format-spec'."
6697 (when (eq (logand (length args) 1) 1) ; oddp
6698 (error "Obscure usage of this function appeared"))
6699 (let ((entry (erc-retrieve-catalog-entry msg)))
6700 (when (not entry)
6701 (error "No format spec for message %s" msg))
6702 (when (functionp entry)
6703 (setq entry (apply entry args)))
6704 (format-spec entry (apply #'format-spec-make args))))
6706 ;;; Various hook functions
6708 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6710 (defcustom erc-kill-server-hook '(erc-kill-server)
6711 "Invoked whenever a server buffer is killed via `kill-buffer'."
6712 :group 'erc-hooks
6713 :type 'hook)
6715 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6716 "Invoked whenever a channel-buffer is killed via `kill-buffer'."
6717 :group 'erc-hooks
6718 :type 'hook)
6720 (defcustom erc-kill-buffer-hook nil
6721 "Hook run whenever a non-server or channel buffer is killed.
6723 See also `kill-buffer'."
6724 :group 'erc-hooks
6725 :type 'hook)
6727 (defun erc-kill-buffer-function ()
6728 "Function to call when an ERC buffer is killed.
6729 This function should be on `kill-buffer-hook'.
6730 When the current buffer is in `erc-mode', this function will run
6731 one of the following hooks:
6732 `erc-kill-server-hook' if the server buffer was killed,
6733 `erc-kill-channel-hook' if a channel buffer was killed,
6734 or `erc-kill-buffer-hook' if any other buffer."
6735 (when (eq major-mode 'erc-mode)
6736 (erc-remove-channel-users)
6737 (cond
6738 ((eq (erc-server-buffer) (current-buffer))
6739 (run-hooks 'erc-kill-server-hook))
6740 ((erc-channel-p (or (erc-default-target) (buffer-name)))
6741 (run-hooks 'erc-kill-channel-hook))
6743 (run-hooks 'erc-kill-buffer-hook)))))
6745 (defun erc-kill-server ()
6746 "Sends a QUIT command to the server when the server buffer is killed.
6747 This function should be on `erc-kill-server-hook'."
6748 (when (erc-server-process-alive)
6749 (setq erc-server-quitting t)
6750 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6752 (defun erc-kill-channel ()
6753 "Sends a PART command to the server when the channel buffer is killed.
6754 This function should be on `erc-kill-channel-hook'."
6755 (when (erc-server-process-alive)
6756 (let ((tgt (erc-default-target)))
6757 (if tgt
6758 (erc-server-send (format "PART %s :%s" tgt
6759 (funcall erc-part-reason nil))
6760 nil tgt)))))
6762 ;;; Dealing with `erc-parsed'
6764 (defun erc-find-parsed-property ()
6765 "Find the next occurrence of the `erc-parsed' text property."
6766 (text-property-not-all (point-min) (point-max) 'erc-parsed nil))
6768 (defun erc-restore-text-properties ()
6769 "Restore the property `erc-parsed' for the region."
6770 (let ((parsed-posn (erc-find-parsed-property)))
6771 (put-text-property
6772 (point-min) (point-max)
6773 'erc-parsed (when parsed-posn (erc-get-parsed-vector parsed-posn)))))
6775 (defun erc-get-parsed-vector (point)
6776 "Return the whole parsed vector on POINT."
6777 (get-text-property point 'erc-parsed))
6779 (defun erc-get-parsed-vector-nick (vect)
6780 "Return nickname in the parsed vector VECT."
6781 (let* ((untreated-nick (and vect (erc-response.sender vect)))
6782 (maybe-nick (when untreated-nick
6783 (car (split-string untreated-nick "!")))))
6784 (when (and (not (null maybe-nick))
6785 (erc-is-valid-nick-p maybe-nick))
6786 untreated-nick)))
6788 (defun erc-get-parsed-vector-type (vect)
6789 "Return message type in the parsed vector VECT."
6790 (and vect
6791 (erc-response.command vect)))
6793 ;; Teach url.el how to open irc:// URLs with ERC.
6794 ;; To activate, customize `url-irc-function' to `url-irc-erc'.
6796 ;;;###autoload
6797 (defun erc-handle-irc-url (host port channel user password)
6798 "Use ERC to IRC on HOST:PORT in CHANNEL as USER with PASSWORD.
6799 If ERC is already connected to HOST:PORT, simply /join CHANNEL.
6800 Otherwise, connect to HOST:PORT as USER and /join CHANNEL."
6801 (let ((server-buffer
6802 (car (erc-buffer-filter
6803 (lambda ()
6804 (and (string-equal erc-session-server host)
6805 (= erc-session-port port)
6806 (erc-open-server-buffer-p)))))))
6807 (with-current-buffer (or server-buffer (current-buffer))
6808 (if (and server-buffer channel)
6809 (erc-cmd-JOIN channel)
6810 (erc-open host port (or user (erc-compute-nick)) (erc-compute-full-name)
6811 (not server-buffer) password nil channel
6812 (when server-buffer
6813 (get-buffer-process server-buffer)))))))
6815 (provide 'erc)
6817 ;; Deprecated. We might eventually stop requiring the goodies automatically.
6818 ;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6819 ;; avoid a recursive require error when byte-compiling the entire package.
6820 (require 'erc-goodies)
6822 ;;; erc.el ends here
6824 ;; Local Variables:
6825 ;; outline-regexp: ";;+"
6826 ;; indent-tabs-mode: t
6827 ;; tab-width: 8
6828 ;; End: