Update ELisp manual to match 'string-collate-equalp' doc string
[emacs.git] / lisp / erc / erc.el
blob1a126a80343dfd75b9c8cda2c601248a6b77d1ab
1 ;; erc.el --- An Emacs Internet Relay Chat client -*- lexical-binding:t -*-
3 ;; Copyright (C) 1997-2016 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 (defcustom erc-join-buffer 'buffer
1478 "Determines how to display a newly created IRC buffer.
1480 The available choices are:
1482 `window' - in another window,
1483 `window-noselect' - in another window, but don't select that one,
1484 `frame' - in another frame,
1485 `bury' - bury it in a new buffer,
1486 `buffer' - in place of the current buffer,
1487 any other value - in place of the current buffer."
1488 :group 'erc-buffers
1489 :type '(choice (const :tag "Split window and select" window)
1490 (const :tag "Split window, don't select" window-noselect)
1491 (const :tag "New frame" frame)
1492 (const :tag "Bury in new buffer" bury)
1493 (const :tag "Use current buffer" buffer)
1494 (const :tag "Use current buffer" t)))
1496 (defcustom erc-frame-alist nil
1497 "Alist of frame parameters for creating erc frames.
1498 A value of nil means to use `default-frame-alist'."
1499 :group 'erc-buffers
1500 :type '(repeat (cons :format "%v"
1501 (symbol :tag "Parameter")
1502 (sexp :tag "Value"))))
1504 (defcustom erc-frame-dedicated-flag nil
1505 "Non-nil means the erc frames are dedicated to that buffer.
1506 This only has effect when `erc-join-buffer' is set to `frame'."
1507 :group 'erc-buffers
1508 :type 'boolean)
1510 (defcustom erc-reuse-frames t
1511 "Determines whether new frames are always created.
1512 Non-nil means that a new frame is not created to display an ERC
1513 buffer if there is already a window displaying it. This only has
1514 effect when `erc-join-buffer' is set to `frame'."
1515 :group 'erc-buffers
1516 :type 'boolean)
1518 (defun erc-channel-p (channel)
1519 "Return non-nil if CHANNEL seems to be an IRC channel name."
1520 (cond ((stringp channel)
1521 (memq (aref channel 0) '(?# ?& ?+ ?!)))
1522 ((and (bufferp channel) (buffer-live-p channel))
1523 (with-current-buffer channel
1524 (erc-channel-p (erc-default-target))))
1525 (t nil)))
1527 (defcustom erc-reuse-buffers t
1528 "If nil, create new buffers on joining a channel/query.
1529 If non-nil, a new buffer will only be created when you join
1530 channels with same names on different servers, or have query buffers
1531 open with nicks of the same name on different servers. Otherwise,
1532 the existing buffers will be reused."
1533 :group 'erc-buffers
1534 :type 'boolean)
1536 (defun erc-normalize-port (port)
1537 "Normalize the port specification PORT to integer form.
1538 PORT may be an integer, a string or a symbol. If it is a string or a
1539 symbol, it may have these values:
1540 * irc -> 194
1541 * ircs -> 994
1542 * ircd -> 6667
1543 * ircd-dalnet -> 7000"
1544 (cond
1545 ((symbolp port)
1546 (erc-normalize-port (symbol-name port)))
1547 ((stringp port)
1548 (let ((port-nr (string-to-number port)))
1549 (cond
1550 ((> port-nr 0)
1551 port-nr)
1552 ((string-equal port "irc")
1553 194)
1554 ((string-equal port "ircs")
1555 994)
1556 ((string-equal port "ircd")
1557 6667)
1558 ((string-equal port "ircd-dalnet")
1559 7000)
1561 nil))))
1562 ((numberp port)
1563 port)
1565 nil)))
1567 (defun erc-port-equal (a b)
1568 "Check whether ports A and B are equal."
1569 (= (erc-normalize-port a) (erc-normalize-port b)))
1571 (defun erc-generate-new-buffer-name (server port target)
1572 "Create a new buffer name based on the arguments."
1573 (when (numberp port) (setq port (number-to-string port)))
1574 (let ((buf-name (or target
1575 (or (let ((name (concat server ":" port)))
1576 (when (> (length name) 1)
1577 name))
1578 ;; This fallback should in fact never happen
1579 "*erc-server-buffer*")))
1580 buffer-name)
1581 ;; Reuse existing buffers, but not if the buffer is a connected server
1582 ;; buffer and not if its associated with a different server than the
1583 ;; current ERC buffer.
1584 ;; if buf-name is taken by a different connection (or by something !erc)
1585 ;; then see if "buf-name/server" meets the same criteria
1586 (dolist (candidate (list buf-name (concat buf-name "/" server)))
1587 (if (and (not buffer-name)
1588 erc-reuse-buffers
1589 (get-buffer candidate)
1590 (or target
1591 (with-current-buffer (get-buffer candidate)
1592 (and (erc-server-buffer-p)
1593 (not (erc-server-process-alive)))))
1594 (with-current-buffer (get-buffer candidate)
1595 (and (string= erc-session-server server)
1596 (erc-port-equal erc-session-port port))))
1597 (setq buffer-name candidate)))
1598 ;; if buffer-name is unset, neither candidate worked out for us,
1599 ;; fallback to the old <N> uniquification method:
1600 (or buffer-name (generate-new-buffer-name buf-name)) ))
1602 (defun erc-get-buffer-create (server port target)
1603 "Create a new buffer based on the arguments."
1604 (get-buffer-create (erc-generate-new-buffer-name server port target)))
1607 (defun erc-member-ignore-case (string list)
1608 "Return non-nil if STRING is a member of LIST.
1610 All strings are compared according to IRC protocol case rules, see
1611 `erc-downcase'."
1612 (setq string (erc-downcase string))
1613 (catch 'result
1614 (while list
1615 (if (string= string (erc-downcase (car list)))
1616 (throw 'result list)
1617 (setq list (cdr list))))))
1619 (defmacro erc-with-buffer (spec &rest body)
1620 "Execute BODY in the buffer associated with SPEC.
1622 SPEC should have the form
1624 (TARGET [PROCESS])
1626 If TARGET is a buffer, use it. Otherwise, use the buffer
1627 matching TARGET in the process specified by PROCESS.
1629 If PROCESS is nil, use the current `erc-server-process'.
1630 See `erc-get-buffer' for details.
1632 See also `with-current-buffer'.
1634 \(fn (TARGET [PROCESS]) BODY...)"
1635 (declare (indent 1) (debug ((form &optional form) body)))
1636 (let ((buf (make-symbol "buf"))
1637 (proc (make-symbol "proc"))
1638 (target (make-symbol "target"))
1639 (process (make-symbol "process")))
1640 `(let* ((,target ,(car spec))
1641 (,process ,(cadr spec))
1642 (,buf (if (bufferp ,target)
1643 ,target
1644 (let ((,proc (or ,process
1645 (and (processp erc-server-process)
1646 erc-server-process))))
1647 (if (and ,target ,proc)
1648 (erc-get-buffer ,target ,proc))))))
1649 (when (buffer-live-p ,buf)
1650 (with-current-buffer ,buf
1651 ,@body)))))
1653 (defun erc-get-buffer (target &optional proc)
1654 "Return the buffer matching TARGET in the process PROC.
1655 If PROC is not supplied, all processes are searched."
1656 (let ((downcased-target (erc-downcase target)))
1657 (catch 'buffer
1658 (erc-buffer-filter
1659 (lambda ()
1660 (let ((current (erc-default-target)))
1661 (and (stringp current)
1662 (string-equal downcased-target (erc-downcase current))
1663 (throw 'buffer (current-buffer)))))
1664 proc))))
1666 (defun erc--buffer-p (buf predicate proc)
1667 (with-current-buffer buf
1668 (and (derived-mode-p 'erc-mode)
1669 (or (not proc)
1670 (eq proc erc-server-process))
1671 (funcall predicate)
1672 buf)))
1674 (defun erc-buffer-filter (predicate &optional proc)
1675 "Return a list of `erc-mode' buffers matching certain criteria.
1676 PREDICATE is a function executed with each buffer, if it returns t, that buffer
1677 is considered a valid match.
1679 PROC is either an `erc-server-process', identifying a certain
1680 server connection, or nil which means all open connections."
1681 (save-excursion
1682 (delq
1684 (mapcar (lambda (buf)
1685 (when (buffer-live-p buf)
1686 (erc--buffer-p buf predicate proc)))
1687 (buffer-list)))))
1689 (defun erc-buffer-list (&optional predicate proc)
1690 "Return a list of ERC buffers.
1691 PREDICATE is a function which executes with every buffer satisfying
1692 the predicate. If PREDICATE is passed as nil, return a list of all ERC
1693 buffers. If PROC is given, the buffers local variable `erc-server-process'
1694 needs to match PROC."
1695 (unless predicate
1696 (setq predicate (lambda () t)))
1697 (erc-buffer-filter predicate proc))
1699 (defmacro erc-with-all-buffers-of-server (process pred &rest forms)
1700 "Execute FORMS in all buffers which have same process as this server.
1701 FORMS will be evaluated in all buffers having the process PROCESS and
1702 where PRED matches or in all buffers of the server process if PRED is
1703 nil."
1704 (declare (indent 1) (debug (form form body)))
1705 ;; Make the evaluation have the correct order
1706 (let ((pre (make-symbol "pre"))
1707 (pro (make-symbol "pro")))
1708 `(let* ((,pro ,process)
1709 (,pre ,pred)
1710 (res (mapcar (lambda (buffer)
1711 (with-current-buffer buffer
1712 ,@forms))
1713 (erc-buffer-list ,pre
1714 ,pro))))
1715 ;; Silence the byte-compiler by binding the result of mapcar to
1716 ;; a variable.
1717 (ignore res)
1718 res)))
1720 (define-obsolete-function-alias 'erc-iswitchb 'erc-switch-to-buffer "25.1")
1721 (defun erc-switch-to-buffer (&optional arg)
1722 "Prompt for a ERC buffer to switch to.
1723 When invoked with prefix argument, use all erc buffers. Without prefix
1724 ARG, allow only buffers related to same session server.
1725 If `erc-track-mode' is in enabled, put the last element of
1726 `erc-modified-channels-alist' in front of the buffer list."
1727 (interactive "P")
1728 (switch-to-buffer
1729 (read-buffer "Switch to ERC buffer: "
1730 (when (boundp 'erc-modified-channels-alist)
1731 (buffer-name (caar (last erc-modified-channels-alist))))
1733 ;; Only allow ERC buffers in the same session.
1734 (let ((proc (unless arg erc-server-process)))
1735 (lambda (bufname)
1736 (let ((buf (if (consp bufname)
1737 (cdr bufname) (get-buffer bufname))))
1738 (when buf
1739 (erc--buffer-p buf (lambda () t) proc)
1740 (with-current-buffer buf
1741 (and (derived-mode-p 'erc-mode)
1742 (or (null proc)
1743 (eq proc erc-server-process)))))))))))
1745 (defun erc-channel-list (proc)
1746 "Return a list of channel buffers.
1747 PROC is the process for the server connection. If PROC is nil, return
1748 all channel buffers on all servers."
1749 (erc-buffer-filter
1750 (lambda ()
1751 (and (erc-default-target)
1752 (erc-channel-p (erc-default-target))))
1753 proc))
1755 (defun erc-buffer-list-with-nick (nick proc)
1756 "Return buffers containing NICK in the `erc-channel-users' list."
1757 (with-current-buffer (process-buffer proc)
1758 (let ((user (gethash (erc-downcase nick) erc-server-users)))
1759 (if user
1760 (erc-server-user-buffers user)
1761 nil))))
1763 ;; Some local variables
1765 (defvar erc-default-recipients nil
1766 "List of default recipients of the current buffer.")
1767 (make-variable-buffer-local 'erc-default-recipients)
1769 (defvar erc-session-user-full-name nil
1770 "Full name of the user on the current server.")
1771 (make-variable-buffer-local 'erc-session-user-full-name)
1773 (defvar erc-channel-user-limit nil
1774 "Limit of users per channel.")
1775 (make-variable-buffer-local 'erc-channel-user-limit)
1777 (defvar erc-channel-key nil
1778 "Key needed to join channel.")
1779 (make-variable-buffer-local 'erc-channel-key)
1781 (defvar erc-invitation nil
1782 "Last invitation channel.")
1783 (make-variable-buffer-local 'erc-invitation)
1785 (defvar erc-away nil
1786 "Non-nil indicates that we are away.
1788 Use `erc-away-time' to access this if you might be in a channel
1789 buffer rather than a server buffer.")
1790 (make-variable-buffer-local 'erc-away)
1792 (defvar erc-channel-list nil
1793 "Server channel list.")
1794 (make-variable-buffer-local 'erc-channel-list)
1796 (defvar erc-bad-nick nil
1797 "Non-nil indicates that we got a `nick in use' error while connecting.")
1798 (make-variable-buffer-local 'erc-bad-nick)
1800 (defvar erc-logged-in nil
1801 "Non-nil indicates that we are logged in.")
1802 (make-variable-buffer-local 'erc-logged-in)
1804 (defvar erc-default-nicks nil
1805 "The local copy of `erc-nick' - the list of nicks to choose from.")
1806 (make-variable-buffer-local 'erc-default-nicks)
1808 (defvar erc-nick-change-attempt-count 0
1809 "Used to keep track of how many times an attempt at changing nick is made.")
1810 (make-variable-buffer-local 'erc-nick-change-attempt-count)
1812 (defun erc-migrate-modules (mods)
1813 "Migrate old names of ERC modules to new ones."
1814 ;; modify `transforms' to specify what needs to be changed
1815 ;; each item is in the format '(old . new)
1816 (let ((transforms '((pcomplete . completion))))
1817 (erc-delete-dups
1818 (mapcar (lambda (m) (or (cdr (assoc m transforms)) m))
1819 mods))))
1821 (defcustom erc-modules '(netsplit fill button match track completion readonly
1822 networks ring autojoin noncommands irccontrols
1823 move-to-prompt stamp menu list)
1824 "A list of modules which ERC should enable.
1825 If you set the value of this without using `customize' remember to call
1826 \(erc-update-modules) after you change it. When using `customize', modules
1827 removed from the list will be disabled."
1828 :get (lambda (sym)
1829 ;; replace outdated names with their newer equivalents
1830 (erc-migrate-modules (symbol-value sym)))
1831 :set (lambda (sym val)
1832 ;; disable modules which have just been removed
1833 (when (and (boundp 'erc-modules) erc-modules val)
1834 (dolist (module erc-modules)
1835 (unless (member module val)
1836 (let ((f (intern-soft (format "erc-%s-mode" module))))
1837 (when (and (fboundp f) (boundp f) (symbol-value f))
1838 (message "Disabling `erc-%s'" module)
1839 (funcall f 0))))))
1840 (set sym val)
1841 ;; this test is for the case where erc hasn't been loaded yet
1842 (when (fboundp 'erc-update-modules)
1843 (erc-update-modules)))
1844 :type
1845 '(set
1846 :greedy t
1847 (const :tag "autoaway: Set away status automatically" autoaway)
1848 (const :tag "autojoin: Join channels automatically" autojoin)
1849 (const :tag "button: Buttonize URLs, nicknames, and other text" button)
1850 (const :tag "capab: Mark unidentified users on servers supporting CAPAB"
1851 capab-identify)
1852 (const :tag "completion: Complete nicknames and commands (programmable)"
1853 completion)
1854 (const :tag "hecomplete: Complete nicknames and commands (obsolete, use \"completion\")" hecomplete)
1855 (const :tag "dcc: Provide Direct Client-to-Client support" dcc)
1856 (const :tag "fill: Wrap long lines" fill)
1857 (const :tag "identd: Launch an identd server on port 8113" identd)
1858 (const :tag "irccontrols: Highlight or remove IRC control characters"
1859 irccontrols)
1860 (const :tag "keep-place: Leave point above un-viewed text" keep-place)
1861 (const :tag "list: List channels in a separate buffer" list)
1862 (const :tag "log: Save buffers in logs" log)
1863 (const :tag "match: Highlight pals, fools, and other keywords" match)
1864 (const :tag "menu: Display a menu in ERC buffers" menu)
1865 (const :tag "move-to-prompt: Move to the prompt when typing text"
1866 move-to-prompt)
1867 (const :tag "netsplit: Detect netsplits" netsplit)
1868 (const :tag "networks: Provide data about IRC networks" networks)
1869 (const :tag "noncommands: Don't display non-IRC commands after evaluation"
1870 noncommands)
1871 (const :tag
1872 "notify: Notify when the online status of certain users changes"
1873 notify)
1874 (const :tag "notifications: Send notifications on PRIVMSG or nickname mentions"
1875 notifications)
1876 (const :tag "page: Process CTCP PAGE requests from IRC" page)
1877 (const :tag "readonly: Make displayed lines read-only" readonly)
1878 (const :tag "replace: Replace text in messages" replace)
1879 (const :tag "ring: Enable an input history" ring)
1880 (const :tag "scrolltobottom: Scroll to the bottom of the buffer"
1881 scrolltobottom)
1882 (const :tag "services: Identify to Nickserv (IRC Services) automatically"
1883 services)
1884 (const :tag "smiley: Convert smileys to pretty icons" smiley)
1885 (const :tag "sound: Play sounds when you receive CTCP SOUND requests"
1886 sound)
1887 (const :tag "stamp: Add timestamps to messages" stamp)
1888 (const :tag "spelling: Check spelling" spelling)
1889 (const :tag "track: Track channel activity in the mode-line" track)
1890 (const :tag "truncate: Truncate buffers to a certain size" truncate)
1891 (const :tag "unmorse: Translate morse code in messages" unmorse)
1892 (const :tag "xdcc: Act as an XDCC file-server" xdcc)
1893 (repeat :tag "Others" :inline t symbol))
1894 :group 'erc)
1896 (defun erc-update-modules ()
1897 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1898 (let (req)
1899 (dolist (mod erc-modules)
1900 (setq req (concat "erc-" (symbol-name mod)))
1901 (cond
1902 ;; yuck. perhaps we should bring the filenames into sync?
1903 ((string= req "erc-capab-identify")
1904 (setq req "erc-capab"))
1905 ((string= req "erc-completion")
1906 (setq req "erc-pcomplete"))
1907 ((string= req "erc-pcomplete")
1908 (setq mod 'completion))
1909 ((string= req "erc-autojoin")
1910 (setq req "erc-join")))
1911 (condition-case nil
1912 (require (intern req))
1913 (error nil))
1914 (let ((sym (intern-soft (concat "erc-" (symbol-name mod) "-mode"))))
1915 (if (fboundp sym)
1916 (funcall sym 1)
1917 (error "`%s' is not a known ERC module" mod))))))
1919 (defun erc-setup-buffer (buffer)
1920 "Consults `erc-join-buffer' to find out how to display `BUFFER'."
1921 (pcase erc-join-buffer
1922 (`window
1923 (if (active-minibuffer-window)
1924 (display-buffer buffer)
1925 (switch-to-buffer-other-window buffer)))
1926 (`window-noselect
1927 (display-buffer buffer))
1928 (`bury
1929 nil)
1930 (`frame
1931 (when (or (not erc-reuse-frames)
1932 (not (get-buffer-window buffer t)))
1933 (let ((frame (make-frame (or erc-frame-alist
1934 default-frame-alist))))
1935 (raise-frame frame)
1936 (select-frame frame))
1937 (switch-to-buffer buffer)
1938 (when erc-frame-dedicated-flag
1939 (set-window-dedicated-p (selected-window) t))))
1941 (if (active-minibuffer-window)
1942 (display-buffer buffer)
1943 (switch-to-buffer buffer)))))
1945 (defun erc-open (&optional server port nick full-name
1946 connect passwd tgt-list channel process)
1947 "Connect to SERVER on PORT as NICK with FULL-NAME.
1949 If CONNECT is non-nil, connect to the server. Otherwise assume
1950 already connected and just create a separate buffer for the new
1951 target CHANNEL.
1953 Use PASSWD as user password on the server. If TGT-LIST is
1954 non-nil, use it to initialize `erc-default-recipients'.
1956 Returns the buffer for the given server or channel."
1957 (let ((server-announced-name (when (and (boundp 'erc-session-server)
1958 (string= server erc-session-server))
1959 erc-server-announced-name))
1960 (connected-p (unless connect erc-server-connected))
1961 (buffer (erc-get-buffer-create server port channel))
1962 (old-buffer (current-buffer))
1963 old-point
1964 continued-session)
1965 (when connect (run-hook-with-args 'erc-before-connect server port nick))
1966 (erc-update-modules)
1967 (set-buffer buffer)
1968 (setq old-point (point))
1969 (let ((old-recon-count erc-server-reconnect-count))
1970 (erc-mode)
1971 (setq erc-server-reconnect-count old-recon-count))
1972 (setq erc-server-announced-name server-announced-name)
1973 (setq erc-server-connected connected-p)
1974 ;; connection parameters
1975 (setq erc-server-process process)
1976 (setq erc-insert-marker (make-marker))
1977 (setq erc-input-marker (make-marker))
1978 ;; go to the end of the buffer and open a new line
1979 ;; (the buffer may have existed)
1980 (goto-char (point-max))
1981 (forward-line 0)
1982 (when (get-text-property (point) 'erc-prompt)
1983 (setq continued-session t)
1984 (set-marker erc-input-marker
1985 (or (next-single-property-change (point) 'erc-prompt)
1986 (point-max))))
1987 (unless continued-session
1988 (goto-char (point-max))
1989 (insert "\n"))
1990 (set-marker erc-insert-marker (point))
1991 ;; stack of default recipients
1992 (setq erc-default-recipients tgt-list)
1993 (setq erc-server-current-nick nil)
1994 ;; Initialize erc-server-users and erc-channel-users
1995 (if connect
1996 (progn ;; server buffer
1997 (setq erc-server-users
1998 (make-hash-table :test 'equal))
1999 (setq erc-channel-users nil))
2000 (progn ;; target buffer
2001 (setq erc-server-users nil)
2002 (setq erc-channel-users
2003 (make-hash-table :test 'equal))))
2004 ;; clear last incomplete line read
2005 (setq erc-server-filter-data nil)
2006 (setq erc-channel-topic "")
2007 ;; limit on the number of users on the channel (mode +l)
2008 (setq erc-channel-user-limit nil)
2009 (setq erc-channel-key nil)
2010 ;; last active buffer, defaults to this one
2011 (erc-set-active-buffer buffer)
2012 ;; last invitation channel
2013 (setq erc-invitation nil)
2014 ;; Server channel list
2015 (setq erc-channel-list ())
2016 ;; login-time 'nick in use' error
2017 (setq erc-bad-nick nil)
2018 ;; whether we have logged in
2019 (setq erc-logged-in nil)
2020 ;; The local copy of `erc-nick' - the list of nicks to choose
2021 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
2022 ;; password stuff
2023 (setq erc-session-password
2024 (or passwd
2025 (let ((secret
2026 (plist-get
2027 (nth 0
2028 (auth-source-search :host server
2029 :max 1
2030 :user nick
2031 ;; secrets.el wouldn’t accept a number
2032 :port (if (numberp port) (number-to-string port) port)
2033 :require '(:secret)))
2034 :secret)))
2035 (if (functionp secret)
2036 (funcall secret)
2037 secret))))
2038 ;; debug output buffer
2039 (setq erc-dbuf
2040 (when erc-log-p
2041 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
2042 ;; set up prompt
2043 (unless continued-session
2044 (goto-char (point-max))
2045 (insert "\n"))
2046 (if continued-session
2047 (goto-char old-point)
2048 (set-marker erc-insert-marker (point))
2049 (erc-display-prompt)
2050 (goto-char (point-max)))
2052 (erc-determine-parameters server port nick full-name)
2054 ;; Saving log file on exit
2055 (run-hook-with-args 'erc-connect-pre-hook buffer)
2057 (when connect
2058 (erc-server-connect erc-session-server erc-session-port buffer))
2059 (erc-update-mode-line)
2061 ;; Now display the buffer in a window as per user wishes.
2062 (unless (eq buffer old-buffer)
2063 (when erc-log-p
2064 ;; we can't log to debug buffer, it may not exist yet
2065 (message "erc: old buffer %s, switching to %s"
2066 old-buffer buffer))
2067 (erc-setup-buffer buffer))
2069 buffer))
2071 (defun erc-initialize-log-marker (buffer)
2072 "Initialize the `erc-last-saved-position' marker to a sensible position.
2073 BUFFER is the current buffer."
2074 (with-current-buffer buffer
2075 (unless (markerp erc-last-saved-position)
2076 (setq erc-last-saved-position (make-marker))
2077 (move-marker erc-last-saved-position
2078 (1- (marker-position erc-insert-marker))))))
2080 ;; interactive startup
2082 (defvar erc-server-history-list nil
2083 "IRC server interactive selection history list.")
2085 (defvar erc-nick-history-list nil
2086 "Nickname interactive selection history list.")
2088 (defun erc-already-logged-in (server port nick)
2089 "Return the buffers corresponding to a NICK on PORT of a session SERVER.
2090 This is determined by looking for the appropriate buffer and checking
2091 whether the connection is still alive.
2092 If no buffer matches, return nil."
2093 (erc-buffer-list
2094 (lambda ()
2095 (and (erc-server-process-alive)
2096 (string= erc-session-server server)
2097 (erc-port-equal erc-session-port port)
2098 (erc-current-nick-p nick)))))
2100 (defcustom erc-before-connect nil
2101 "Hook called before connecting to a server.
2102 This hook gets executed before `erc' actually invokes `erc-mode'
2103 with your input data. The functions in here get called with three
2104 parameters, SERVER, PORT and NICK."
2105 :group 'erc-hooks
2106 :type 'hook)
2108 (defcustom erc-after-connect nil
2109 "Hook called after connecting to a server.
2110 This hook gets executed when an end of MOTD has been received. All
2111 functions in here get called with the parameters SERVER and NICK."
2112 :group 'erc-hooks
2113 :type 'hook)
2115 ;;;###autoload
2116 (defun erc-select-read-args ()
2117 "Prompt the user for values of nick, server, port, and password."
2118 (let (user-input server port nick passwd)
2119 (setq user-input (read-from-minibuffer
2120 "IRC server: "
2121 (erc-compute-server) nil nil 'erc-server-history-list))
2123 (if (string-match "\\(.*\\):\\(.*\\)\\'" user-input)
2124 (setq port (erc-string-to-port (match-string 2 user-input))
2125 user-input (match-string 1 user-input))
2126 (setq port
2127 (erc-string-to-port (read-from-minibuffer
2128 "IRC port: " (erc-port-to-string
2129 (erc-compute-port))))))
2131 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
2132 (setq nick (match-string 1 user-input)
2133 user-input (match-string 2 user-input))
2134 (setq nick
2135 (if (erc-already-logged-in server port nick)
2136 (read-from-minibuffer
2137 (erc-format-message 'nick-in-use ?n nick)
2138 nick
2139 nil nil 'erc-nick-history-list)
2140 (read-from-minibuffer
2141 "Nickname: " (erc-compute-nick nick)
2142 nil nil 'erc-nick-history-list))))
2144 (setq server user-input)
2146 (setq passwd (if erc-prompt-for-password
2147 (if (and erc-password
2148 (y-or-n-p "Use the default password? "))
2149 erc-password
2150 (read-passwd "Password: "))
2151 erc-password))
2152 (when (and passwd (string= "" passwd))
2153 (setq passwd nil))
2155 (while (erc-already-logged-in server port nick)
2156 ;; hmm, this is a problem when using multiple connections to a bnc
2157 ;; with the same nick. Currently this code prevents using more than one
2158 ;; bnc with the same nick. actually it would be nice to have
2159 ;; bncs transparent, so that erc-compute-buffer-name displays
2160 ;; the server one is connected to.
2161 (setq nick (read-from-minibuffer
2162 (erc-format-message 'nick-in-use ?n nick)
2163 nick
2164 nil nil 'erc-nick-history-list)))
2165 (list :server server :port port :nick nick :password passwd)))
2167 ;;;###autoload
2168 (cl-defun erc (&key (server (erc-compute-server))
2169 (port (erc-compute-port))
2170 (nick (erc-compute-nick))
2171 password
2172 (full-name (erc-compute-full-name)))
2173 "ERC is a powerful, modular, and extensible IRC client.
2174 This function is the main entry point for ERC.
2176 It permits you to select connection parameters, and then starts ERC.
2178 Non-interactively, it takes the keyword arguments
2179 (server (erc-compute-server))
2180 (port (erc-compute-port))
2181 (nick (erc-compute-nick))
2182 password
2183 (full-name (erc-compute-full-name)))
2185 That is, if called with
2187 (erc :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
2189 then the server and full-name will be set to those values, whereas
2190 `erc-compute-port', `erc-compute-nick' and `erc-compute-full-name' will
2191 be invoked for the values of the other parameters."
2192 (interactive (erc-select-read-args))
2193 (erc-open server port nick full-name t password))
2195 ;;;###autoload
2196 (defalias 'erc-select 'erc)
2197 (defalias 'erc-ssl 'erc-tls)
2199 ;;;###autoload
2200 (defun erc-tls (&rest r)
2201 "Interactively select TLS connection parameters and run ERC.
2202 Arguments are the same as for `erc'."
2203 (interactive (erc-select-read-args))
2204 (let ((erc-server-connect-function 'erc-open-tls-stream))
2205 (apply #'erc r)))
2207 (defun erc-open-tls-stream (name buffer host port)
2208 "Open an TLS stream to an IRC server.
2209 The process will be given the name NAME, its target buffer will be
2210 BUFFER. HOST and PORT specify the connection target."
2211 (open-network-stream name buffer host port
2212 :nowait t
2213 :type 'tls))
2215 ;;; Displaying error messages
2217 (defun erc-error (&rest args)
2218 "Pass ARGS to `format', and display the result as an error message.
2219 If `debug-on-error' is set to non-nil, then throw a real error with this
2220 message instead, to make debugging easier."
2221 (if debug-on-error
2222 (apply #'error args)
2223 (apply #'message args)
2224 (beep)))
2226 ;;; Debugging the protocol
2228 (defvar erc-debug-irc-protocol nil
2229 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2231 The buffer is created if it doesn't exist.
2233 NOTE: If this variable is non-nil, and you kill the only
2234 visible \"*erc-protocol*\" buffer, it will be recreated shortly,
2235 but you won't see it.
2237 WARNING: Do not set this variable directly! Instead, use the
2238 function `erc-toggle-debug-irc-protocol' to toggle its value.")
2240 (declare-function erc-network-name "erc-networks" ())
2242 (defun erc-log-irc-protocol (string &optional outbound)
2243 "Append STRING to the buffer *erc-protocol*.
2245 This only has any effect if `erc-debug-irc-protocol' is non-nil.
2247 The buffer is created if it doesn't exist.
2249 If OUTBOUND is non-nil, STRING is being sent to the IRC server
2250 and appears in face `erc-input-face' in the buffer."
2251 (when erc-debug-irc-protocol
2252 (let ((network-name (or (ignore-errors (erc-network-name))
2253 "???")))
2254 (with-current-buffer (get-buffer-create "*erc-protocol*")
2255 (save-excursion
2256 (goto-char (point-max))
2257 (let ((inhibit-read-only t))
2258 (insert (if (not outbound)
2259 ;; Cope with the fact that string might
2260 ;; contain multiple lines of text.
2261 (let ((lines (delete "" (split-string string
2262 "\n\\|\r\n")))
2263 (result ""))
2264 (dolist (line lines)
2265 (setq result (concat result network-name
2266 " << " line "\n")))
2267 result)
2268 (erc-propertize
2269 (concat network-name " >> " string
2270 (if (/= ?\n
2271 (aref string
2272 (1- (length string))))
2273 "\n"))
2274 'face 'erc-input-face)))))
2275 (let ((orig-win (selected-window))
2276 (debug-buffer-window (get-buffer-window (current-buffer) t)))
2277 (when debug-buffer-window
2278 (select-window debug-buffer-window)
2279 (when (= 1 (count-lines (point) (point-max)))
2280 (goto-char (point-max))
2281 (recenter -1))
2282 (select-window orig-win)))))))
2284 (defun erc-toggle-debug-irc-protocol (&optional arg)
2285 "Toggle the value of `erc-debug-irc-protocol'.
2287 If ARG is non-nil, show the *erc-protocol* buffer."
2288 (interactive "P")
2289 (let* ((buf (get-buffer-create "*erc-protocol*")))
2290 (with-current-buffer buf
2291 (erc-view-mode-enter)
2292 (when (null (current-local-map))
2293 (let ((inhibit-read-only t))
2294 (insert (erc-make-notice "This buffer displays all IRC protocol traffic exchanged with each server.\n"))
2295 (insert (erc-make-notice "Kill this buffer to terminate protocol logging.\n\n")))
2296 (use-local-map (make-sparse-keymap))
2297 (local-set-key (kbd "t") 'erc-toggle-debug-irc-protocol))
2298 (add-hook 'kill-buffer-hook
2299 #'(lambda () (setq erc-debug-irc-protocol nil))
2300 nil 'local)
2301 (goto-char (point-max))
2302 (let ((inhibit-read-only t))
2303 (insert (erc-make-notice
2304 (format "IRC protocol logging %s at %s -- Press `t' to toggle logging.\n"
2305 (if erc-debug-irc-protocol "disabled" "enabled")
2306 (current-time-string))))))
2307 (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
2308 (if (and arg
2309 (not (get-buffer-window "*erc-protocol*" t)))
2310 (display-buffer buf t))
2311 (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
2312 (if erc-debug-irc-protocol "enabled" "disabled"))))
2314 ;;; I/O interface
2316 ;; send interface
2318 (defun erc-send-action (tgt str &optional force)
2319 "Send CTCP ACTION information described by STR to TGT."
2320 (erc-send-ctcp-message tgt (format "ACTION %s" str) force)
2321 (erc-display-message
2322 nil 'input (current-buffer)
2323 'ACTION ?n (erc-current-nick) ?a str ?u "" ?h ""))
2325 ;; Display interface
2327 (defun erc-string-invisible-p (string)
2328 "Check whether STRING is invisible or not.
2329 I.e. any char in it has the `invisible' property set."
2330 (text-property-any 0 (length string) 'invisible t string))
2332 (defcustom erc-remove-parsed-property t
2333 "Whether to remove the erc-parsed text property after displaying a message.
2335 The default is to remove it, since it causes ERC to take up extra
2336 memory. If you have code that relies on this property, then set
2337 this option to nil."
2338 :type 'boolean
2339 :group 'erc)
2341 (defun erc-display-line-1 (string buffer)
2342 "Display STRING in `erc-mode' BUFFER.
2343 Auxiliary function used in `erc-display-line'. The line gets filtered to
2344 interpret the control characters. Then, `erc-insert-pre-hook' gets called.
2345 If `erc-insert-this' is still t, STRING gets inserted into the buffer.
2346 Afterwards, `erc-insert-modify' and `erc-insert-post-hook' get called.
2347 If STRING is nil, the function does nothing."
2348 (when string
2349 (with-current-buffer (or buffer (process-buffer erc-server-process))
2350 (let ((insert-position (or (marker-position erc-insert-marker)
2351 (point-max))))
2352 (let ((string string) ;; FIXME! Can this be removed?
2353 (buffer-undo-list t)
2354 (inhibit-read-only t))
2355 (unless (string-match "\n$" string)
2356 (setq string (concat string "\n"))
2357 (when (erc-string-invisible-p string)
2358 (erc-put-text-properties 0 (length string)
2359 '(invisible intangible) string)))
2360 (erc-log (concat "erc-display-line: " string
2361 (format "(%S)" string) " in buffer "
2362 (format "%s" buffer)))
2363 (setq erc-insert-this t)
2364 (run-hook-with-args 'erc-insert-pre-hook string)
2365 (if (null erc-insert-this)
2366 ;; Leave erc-insert-this set to t as much as possible. Fran
2367 ;; Litterio <franl> has seen erc-insert-this set to nil while
2368 ;; erc-send-pre-hook is running, which should never happen. This
2369 ;; may cure it.
2370 (setq erc-insert-this t)
2371 (save-excursion ;; to restore point in the new buffer
2372 (save-restriction
2373 (widen)
2374 (goto-char insert-position)
2375 (insert-before-markers string)
2376 ;; run insertion hook, with point at restored location
2377 (save-restriction
2378 (narrow-to-region insert-position (point))
2379 (run-hooks 'erc-insert-modify-hook)
2380 (run-hooks 'erc-insert-post-hook)
2381 (when erc-remove-parsed-property
2382 (remove-text-properties (point-min) (point-max)
2383 '(erc-parsed nil))))))))
2384 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2385 (point-max))
2386 insert-position))))))
2388 (defun erc-update-undo-list (shift)
2389 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2390 (unless (or (zerop shift) (atom buffer-undo-list))
2391 (let ((list buffer-undo-list) elt)
2392 (while list
2393 (setq elt (car list))
2394 (cond ((integerp elt) ; POSITION
2395 (cl-incf (car list) shift))
2396 ((or (atom elt) ; nil, EXTENT
2397 ;; (eq t (car elt)) ; (t . TIME)
2398 (markerp (car elt))) ; (MARKER . DISTANCE)
2399 nil)
2400 ((integerp (car elt)) ; (BEGIN . END)
2401 (cl-incf (car elt) shift)
2402 (cl-incf (cdr elt) shift))
2403 ((stringp (car elt)) ; (TEXT . POSITION)
2404 (cl-incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
2405 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2406 (let ((cons (nthcdr 3 elt)))
2407 (cl-incf (car cons) shift)
2408 (cl-incf (cdr cons) shift)))
2409 ((and (featurep 'xemacs)
2410 (extentp (car elt))) ; (EXTENT START END)
2411 (cl-incf (nth 1 elt) shift)
2412 (cl-incf (nth 2 elt) shift)))
2413 (setq list (cdr list))))))
2415 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2416 "Regexp which matches all valid characters in a IRC nickname.")
2418 (defun erc-is-valid-nick-p (nick)
2419 "Check if NICK is a valid IRC nickname."
2420 (string-match (concat "\\`" erc-valid-nick-regexp "\\'") nick))
2422 (defun erc-display-line (string &optional buffer)
2423 "Display STRING in the ERC BUFFER.
2424 All screen output must be done through this function. If BUFFER is nil
2425 or omitted, the default ERC buffer for the `erc-session-server' is used.
2426 The BUFFER can be an actual buffer, a list of buffers, `all' or `active'.
2427 If BUFFER = `all', the string is displayed in all the ERC buffers for the
2428 current session. `active' means the current active buffer
2429 \(`erc-active-buffer'). If the buffer can't be resolved, the current
2430 buffer is used. `erc-display-line-1' is used to display STRING.
2432 If STRING is nil, the function does nothing."
2433 (let ((inhibit-point-motion-hooks t)
2434 new-bufs)
2435 (dolist (buf (cond
2436 ((bufferp buffer) (list buffer))
2437 ((listp buffer) buffer)
2438 ((processp buffer) (list (process-buffer buffer)))
2439 ((eq 'all buffer)
2440 ;; Hmm, or all of the same session server?
2441 (erc-buffer-list nil erc-server-process))
2442 ((and (eq 'active buffer) (erc-active-buffer))
2443 (list (erc-active-buffer)))
2444 ((erc-server-buffer-live-p)
2445 (list (process-buffer erc-server-process)))
2446 (t (list (current-buffer)))))
2447 (when (buffer-live-p buf)
2448 (erc-display-line-1 string buf)
2449 (push buf new-bufs)))
2450 (when (null new-bufs)
2451 (erc-display-line-1 string (if (erc-server-buffer-live-p)
2452 (process-buffer erc-server-process)
2453 (current-buffer))))))
2455 (defun erc-display-message-highlight (type string)
2456 "Highlight STRING according to TYPE, where erc-TYPE-face is an ERC face.
2458 See also `erc-make-notice'."
2459 (cond ((eq type 'notice)
2460 (erc-make-notice string))
2462 (erc-put-text-property
2463 0 (length string)
2464 'face (or (intern-soft
2465 (concat "erc-" (symbol-name type) "-face"))
2466 "erc-default-face")
2467 string)
2468 string)))
2470 (defvar erc-lurker-state nil
2471 "Track the time of the last PRIVMSG for each (server,nick) pair.
2473 This is implemented as a hash of hashes, where the outer key is
2474 the canonicalized server name (as returned by
2475 `erc-canonicalize-server-name') and the outer value is a hash
2476 table mapping nicks (as returned by `erc-lurker-maybe-trim') to
2477 the times of their most recently received PRIVMSG on any channel
2478 on the given server.")
2480 (defcustom erc-lurker-trim-nicks t
2481 "If t, trim trailing `erc-lurker-ignore-chars' from nicks.
2483 This causes e.g. nick and nick\\=` to be considered as the same
2484 individual for activity tracking and lurkiness detection
2485 purposes."
2486 :group 'erc-lurker
2487 :type 'boolean)
2489 (defcustom erc-lurker-ignore-chars "`_"
2490 "Characters at the end of a nick to strip for activity tracking purposes.
2492 See also `erc-lurker-trim-nicks'."
2493 :group 'erc-lurker
2494 :type 'string)
2496 (defun erc-lurker-maybe-trim (nick)
2497 "Maybe trim trailing `erc-lurker-ignore-chars' from NICK.
2499 Returns NICK unmodified unless `erc-lurker-trim-nicks' is
2500 non-nil."
2501 (if erc-lurker-trim-nicks
2502 (replace-regexp-in-string
2503 (format "[%s]"
2504 (mapconcat (lambda (char)
2505 (regexp-quote (char-to-string char)))
2506 erc-lurker-ignore-chars ""))
2507 "" nick)
2508 nick))
2510 (defcustom erc-lurker-hide-list nil
2511 "List of IRC type messages to hide when sent by lurkers.
2513 A typical value would be \(\"JOIN\" \"PART\" \"QUIT\").
2514 See also `erc-lurker-p' and `erc-hide-list'."
2515 :group 'erc-lurker
2516 :type 'erc-message-type)
2518 (defcustom erc-lurker-threshold-time (* 60 60 24) ; 24h by default
2519 "Nicks from which no PRIVMSGs have been received within this
2520 interval (in units of seconds) are considered lurkers by
2521 `erc-lurker-p' and as a result their messages of types in
2522 `erc-lurker-hide-list' will be hidden."
2523 :group 'erc-lurker
2524 :type 'integer)
2526 (defun erc-lurker-initialize ()
2527 "Initialize ERC lurker tracking functionality.
2529 This function adds `erc-lurker-update-status' to
2530 `erc-insert-pre-hook' in order to record the time of each nick's
2531 most recent PRIVMSG as well as initializing the state variable
2532 storing this information."
2533 (setq erc-lurker-state (make-hash-table :test 'equal))
2534 (add-hook 'erc-insert-pre-hook 'erc-lurker-update-status))
2536 (defun erc-lurker-cleanup ()
2537 "Remove all last PRIVMSG state older than `erc-lurker-threshold-time'.
2539 This should be called regularly to avoid excessive resource
2540 consumption for long-lived IRC or Emacs sessions."
2541 (maphash
2542 (lambda (server hash)
2543 (maphash
2544 (lambda (nick last-PRIVMSG-time)
2545 (when
2546 (> (float-time (time-subtract
2547 (current-time)
2548 last-PRIVMSG-time))
2549 erc-lurker-threshold-time)
2550 (remhash nick hash)))
2551 hash)
2552 (if (zerop (hash-table-count hash))
2553 (remhash server erc-lurker-state)))
2554 erc-lurker-state))
2556 (defvar erc-lurker-cleanup-count 0
2557 "Internal counter variable for use with `erc-lurker-cleanup-interval'.")
2559 (defvar erc-lurker-cleanup-interval 100
2560 "Frequency of cleaning up stale erc-lurker state.
2562 `erc-lurker-update-status' calls `erc-lurker-cleanup' once for
2563 every `erc-lurker-cleanup-interval' updates to
2564 `erc-lurker-state'. This is designed to limit the memory
2565 consumption of lurker state during long Emacs sessions and/or ERC
2566 sessions with large numbers of incoming PRIVMSGs.")
2568 (defun erc-lurker-update-status (_message)
2569 "Update `erc-lurker-state' if necessary.
2571 This function is called from `erc-insert-pre-hook'. If the
2572 current message is a PRIVMSG, update `erc-lurker-state' to
2573 reflect the fact that its sender has issued a PRIVMSG at the
2574 current time. Otherwise, take no action.
2576 This function depends on the fact that `erc-display-message'
2577 dynamically binds `parsed', which is used to check if the current
2578 message is a PRIVMSG and to determine its sender. See also
2579 `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'.
2581 In order to limit memory consumption, this function also calls
2582 `erc-lurker-cleanup' once every `erc-lurker-cleanup-interval'
2583 updates of `erc-lurker-state'."
2584 (when (and (boundp 'parsed) (erc-response-p parsed))
2585 (let* ((command (erc-response.command parsed))
2586 (sender
2587 (erc-lurker-maybe-trim
2588 (car (erc-parse-user (erc-response.sender parsed)))))
2589 (server
2590 (erc-canonicalize-server-name erc-server-announced-name)))
2591 (when (equal command "PRIVMSG")
2592 (when (>= (cl-incf erc-lurker-cleanup-count)
2593 erc-lurker-cleanup-interval)
2594 (setq erc-lurker-cleanup-count 0)
2595 (erc-lurker-cleanup))
2596 (unless (gethash server erc-lurker-state)
2597 (puthash server (make-hash-table :test 'equal) erc-lurker-state))
2598 (puthash sender (current-time)
2599 (gethash server erc-lurker-state))))))
2601 (defun erc-lurker-p (nick)
2602 "Predicate indicating NICK's lurking status on the current server.
2604 Lurking is the condition where NICK has issued no PRIVMSG on this
2605 server within `erc-lurker-threshold-time'. See also
2606 `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'."
2607 (unless erc-lurker-state (erc-lurker-initialize))
2608 (let* ((server
2609 (erc-canonicalize-server-name erc-server-announced-name))
2610 (last-PRIVMSG-time
2611 (gethash (erc-lurker-maybe-trim nick)
2612 (gethash server erc-lurker-state (make-hash-table)))))
2613 (or (null last-PRIVMSG-time)
2614 (> (float-time
2615 (time-subtract (current-time) last-PRIVMSG-time))
2616 erc-lurker-threshold-time))))
2618 (defcustom erc-common-server-suffixes
2619 '(("openprojects.net\\'" . "OPN")
2620 ("freenode.net\\'" . "freenode")
2621 ("oftc.net\\'" . "OFTC"))
2622 "Alist of common server name suffixes.
2623 This variable is used in mode-line display to save screen
2624 real estate. Set it to nil if you want to avoid changing
2625 displayed hostnames."
2626 :group 'erc-mode-line-and-header
2627 :type 'alist)
2629 (defun erc-canonicalize-server-name (server)
2630 "Return the canonical network name for SERVER if any,
2631 otherwise `erc-server-announced-name'. SERVER is matched against
2632 `erc-common-server-suffixes'."
2633 (when server
2634 (or (cdar (erc-remove-if-not
2635 (lambda (net) (string-match (car net) server))
2636 erc-common-server-suffixes))
2637 erc-server-announced-name)))
2639 (defun erc-add-targets (scope target-list)
2640 (let ((targets
2641 (mapcar (lambda (targets) (member scope targets)) target-list)))
2642 (cdr (apply 'append (delete nil targets)))))
2644 (defun erc-hide-current-message-p (parsed)
2645 "Predicate indicating whether the parsed ERC response PARSED should be hidden.
2647 Messages are always hidden if the message type of PARSED appears in
2648 `erc-hide-list'. Message types that appear in `erc-network-hide-list'
2649 or `erc-channel-hide-list' are are only hidden if the target matches
2650 the network or channel in the list. In addition, messages whose type
2651 is a member of `erc-lurker-hide-list' are hidden if `erc-lurker-p'
2652 returns non-nil."
2653 (let* ((command (erc-response.command parsed))
2654 (sender (car (erc-parse-user (erc-response.sender parsed))))
2655 (channel (nth 1 (erc-response.command-args parsed)))
2656 (network (or (and (fboundp 'erc-network-name) (erc-network-name))
2657 (erc-shorten-server-name
2658 (or erc-server-announced-name
2659 erc-session-server))))
2660 (current-hide-list
2661 (when erc-network-hide-list
2662 (erc-add-targets network erc-network-hide-list)))
2663 (current-hide-list
2664 (apply 'append current-hide-list
2665 (when erc-channel-hide-list
2666 (erc-add-targets channel erc-channel-hide-list)))))
2667 (or (member command erc-hide-list)
2668 (member command current-hide-list)
2669 (and (member command erc-lurker-hide-list) (erc-lurker-p sender)))))
2671 (defun erc-display-message (parsed type buffer msg &rest args)
2672 "Display MSG in BUFFER.
2674 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2676 See also `erc-format-message' and `erc-display-line'."
2677 (let ((string (if (symbolp msg)
2678 (apply #'erc-format-message msg args)
2679 msg)))
2680 (setq string
2681 (cond
2682 ((null type)
2683 string)
2684 ((listp type)
2685 (mapc (lambda (type)
2686 (setq string
2687 (erc-display-message-highlight type string)))
2688 type)
2689 string)
2690 ((symbolp type)
2691 (erc-display-message-highlight type string))))
2693 (if (not (erc-response-p parsed))
2694 (erc-display-line string buffer)
2695 (unless (erc-hide-current-message-p parsed)
2696 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
2697 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2698 (erc-display-line string buffer)))))
2700 (defun erc-message-type-member (position list)
2701 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2703 This function relies on the erc-parsed text-property being
2704 present."
2705 (let ((prop-val (erc-get-parsed-vector position)))
2706 (and prop-val (member (erc-response.command prop-val) list))))
2708 (defvar erc-send-input-line-function 'erc-send-input-line)
2709 (make-variable-buffer-local 'erc-send-input-line-function)
2711 (defun erc-send-input-line (target line &optional force)
2712 "Send LINE to TARGET.
2714 See also `erc-server-send'."
2715 (setq line (format "PRIVMSG %s :%s"
2716 target
2717 ;; If the line is empty, we still want to
2718 ;; send it - i.e. an empty pasted line.
2719 (if (string= line "\n")
2720 " \n"
2721 line)))
2722 (erc-server-send line force target))
2724 (defun erc-get-arglist (fun)
2725 "Return the argument list of a function without the parens."
2726 (let ((arglist (format "%S" (erc-function-arglist fun))))
2727 (if (string-match "\\`(\\(.*\\))\\'" arglist)
2728 (match-string 1 arglist)
2729 arglist)))
2731 (defun erc-command-no-process-p (str)
2732 "Return non-nil if STR is an ERC command that can be run when the process
2733 is not alive, nil otherwise."
2734 (let ((fun (erc-extract-command-from-line str)))
2735 (and fun
2736 (symbolp (car fun))
2737 (get (car fun) 'process-not-needed))))
2739 (defun erc-command-name (cmd)
2740 "For CMD being the function name of a ERC command, something like
2741 erc-cmd-FOO, this returns a string /FOO."
2742 (let ((command-name (symbol-name cmd)))
2743 (if (string-match "\\`erc-cmd-\\(.*\\)\\'" command-name)
2744 (concat "/" (match-string 1 command-name))
2745 command-name)))
2747 (defun erc-process-input-line (line &optional force no-command)
2748 "Translate LINE to an RFC1459 command and send it based.
2749 Returns non-nil if the command is actually sent to the server, and nil
2750 otherwise.
2752 If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
2753 it is passed to `erc-cmd-default'. If LINE is not a command (i.e. doesn't
2754 start with /<COMMAND>) then it is sent as a message.
2756 An optional FORCE argument forces sending the line when flood
2757 protection is in effect. The optional NO-COMMAND argument prohibits
2758 this function from interpreting the line as a command."
2759 (let ((command-list (erc-extract-command-from-line line)))
2760 (if (and command-list
2761 (not no-command))
2762 (let* ((cmd (nth 0 command-list))
2763 (args (nth 1 command-list)))
2764 (condition-case nil
2765 (if (listp args)
2766 (apply cmd args)
2767 (funcall cmd args))
2768 (wrong-number-of-arguments
2769 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2770 ?c (erc-command-name cmd)
2771 ?u (or (erc-get-arglist cmd)
2773 ?d (format "%s\n"
2774 (or (documentation cmd) "")))
2775 nil)))
2776 (let ((r (erc-default-target)))
2777 (if r
2778 (funcall erc-send-input-line-function r line force)
2779 (erc-display-message nil 'error (current-buffer) 'no-target)
2780 nil)))))
2782 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2783 ;; Input commands handlers
2784 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2786 (defun erc-cmd-AMSG (line)
2787 "Send LINE to all channels of the current server that you are on."
2788 (interactive "sSend to all channels you're on: ")
2789 (setq line (erc-trim-string line))
2790 (erc-with-all-buffers-of-server nil
2791 (lambda ()
2792 (erc-channel-p (erc-default-target)))
2793 (erc-send-message line)))
2794 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2796 (defun erc-cmd-SAY (line)
2797 "Send LINE to the current query or channel as a message, not a command.
2799 Use this when you want to send a message with a leading `/'. Note
2800 that since multi-line messages are never a command, you don't
2801 need this when pasting multiple lines of text."
2802 (if (string-match "^\\s-*$" line)
2804 (string-match "^ ?\\(.*\\)" line)
2805 (erc-process-input-line (match-string 1 line) nil t)))
2806 (put 'erc-cmd-SAY 'do-not-parse-args t)
2808 (defun erc-cmd-SET (line)
2809 "Set the variable named by the first word in LINE to some VALUE.
2810 VALUE is computed by evaluating the rest of LINE in Lisp."
2811 (cond
2812 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2813 (let ((var (read (concat "erc-" (match-string 1 line))))
2814 (val (read (match-string 2 line))))
2815 (if (boundp var)
2816 (progn
2817 (set var (eval val))
2818 (erc-display-message
2819 nil nil 'active (format "Set %S to %S" var val))
2821 (setq var (read (match-string 1 line)))
2822 (if (boundp var)
2823 (progn
2824 (set var (eval val))
2825 (erc-display-message
2826 nil nil 'active (format "Set %S to %S" var val))
2828 (erc-display-message nil 'error 'active 'variable-not-bound)
2829 nil))))
2830 ((string-match "^\\s-*$" line)
2831 (erc-display-line
2832 (concat "Available user variables:\n"
2833 (apply
2834 #'concat
2835 (mapcar
2836 (lambda (var)
2837 (let ((val (symbol-value var)))
2838 (concat (format "%S:" var)
2839 (if (consp val)
2840 (concat "\n" (pp-to-string val))
2841 (format " %S\n" val)))))
2842 (apropos-internal "^erc-" 'custom-variable-p))))
2843 (current-buffer))
2845 (t nil)))
2846 (defalias 'erc-cmd-VAR 'erc-cmd-SET)
2847 (defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2848 (put 'erc-cmd-SET 'do-not-parse-args t)
2849 (put 'erc-cmd-SET 'process-not-needed t)
2851 (defun erc-cmd-default (line)
2852 "Fallback command.
2854 Commands for which no erc-cmd-xxx exists, are tunneled through
2855 this function. LINE is sent to the server verbatim, and
2856 therefore has to contain the command itself as well."
2857 (erc-log (format "cmd: DEFAULT: %s" line))
2858 (erc-server-send (substring line 1))
2861 (defun erc-cmd-IGNORE (&optional user)
2862 "Ignore USER. This should be a regexp matching nick!user@host.
2863 If no USER argument is specified, list the contents of `erc-ignore-list'."
2864 (if user
2865 (let ((quoted (regexp-quote user)))
2866 (when (and (not (string= user quoted))
2867 (y-or-n-p (format "Use regexp-quoted form (%s) instead? "
2868 quoted)))
2869 (setq user quoted))
2870 (erc-display-line
2871 (erc-make-notice (format "Now ignoring %s" user))
2872 'active)
2873 (erc-with-server-buffer (add-to-list 'erc-ignore-list user)))
2874 (if (null (erc-with-server-buffer erc-ignore-list))
2875 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2876 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2877 (mapc #'(lambda (item)
2878 (erc-display-line (erc-make-notice item)
2879 'active))
2880 (erc-with-server-buffer erc-ignore-list))))
2883 (defun erc-cmd-UNIGNORE (user)
2884 "Remove the user specified in USER from the ignore list."
2885 (let ((ignored-nick (car (erc-with-server-buffer
2886 (erc-member-ignore-case (regexp-quote user)
2887 erc-ignore-list)))))
2888 (unless ignored-nick
2889 (if (setq ignored-nick (erc-ignored-user-p user))
2890 (unless (y-or-n-p (format "Remove this regexp (%s)? "
2891 ignored-nick))
2892 (setq ignored-nick nil))
2893 (erc-display-line
2894 (erc-make-notice (format "%s is not currently ignored!" user))
2895 'active)))
2896 (when ignored-nick
2897 (erc-display-line
2898 (erc-make-notice (format "No longer ignoring %s" user))
2899 'active)
2900 (erc-with-server-buffer
2901 (setq erc-ignore-list (delete ignored-nick erc-ignore-list)))))
2904 (defun erc-cmd-CLEAR ()
2905 "Clear the window content."
2906 (recenter 0)
2908 (put 'erc-cmd-CLEAR 'process-not-needed t)
2910 (defun erc-cmd-OPS ()
2911 "Show the ops in the current channel."
2912 (interactive)
2913 (let ((ops nil))
2914 (if erc-channel-users
2915 (maphash (lambda (_nick user-data)
2916 (let ((cuser (cdr user-data)))
2917 (if (and cuser
2918 (erc-channel-user-op cuser))
2919 (setq ops (cons (erc-server-user-nickname
2920 (car user-data))
2921 ops)))))
2922 erc-channel-users))
2923 (setq ops (sort ops 'string-lessp))
2924 (if ops
2925 (erc-display-message
2926 nil 'notice (current-buffer) 'ops
2927 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2928 ?o (mapconcat 'identity ops " "))
2929 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2932 (defun erc-cmd-COUNTRY (tld)
2933 "Display the country associated with the top level domain TLD."
2934 (require 'mail-extr)
2935 (let ((co (ignore-errors (what-domain tld))))
2936 (if co
2937 (erc-display-message
2938 nil 'notice 'active 'country ?c co ?d tld)
2939 (erc-display-message
2940 nil 'notice 'active 'country-unknown ?d tld))
2942 (put 'erc-cmd-COUNTRY 'process-not-needed t)
2944 (defun erc-cmd-AWAY (line)
2945 "Mark the user as being away, the reason being indicated by LINE.
2946 If no reason is given, unset away status."
2947 (when (string-match "^\\s-*\\(.*\\)$" line)
2948 (let ((reason (match-string 1 line)))
2949 (erc-log (format "cmd: AWAY: %s" reason))
2950 (erc-server-send
2951 (if (string= reason "")
2952 "AWAY"
2953 (concat "AWAY :" reason))))
2955 (put 'erc-cmd-AWAY 'do-not-parse-args t)
2957 (defun erc-cmd-GAWAY (line)
2958 "Mark the user as being away everywhere, the reason being indicated by LINE."
2959 ;; on all server buffers.
2960 (erc-with-all-buffers-of-server nil
2961 #'erc-open-server-buffer-p
2962 (erc-cmd-AWAY line)))
2963 (put 'erc-cmd-GAWAY 'do-not-parse-args t)
2965 (defun erc-cmd-CTCP (nick cmd &rest args)
2966 "Send a Client To Client Protocol message to NICK.
2968 CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2969 VERSION and so on. It is called with ARGS."
2970 (let ((str (concat cmd
2971 (when args
2972 (concat " " (mapconcat #'identity args " "))))))
2973 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2974 (erc-send-ctcp-message nick str)
2977 (defun erc-cmd-HELP (&optional func)
2978 "Popup help information.
2980 If FUNC contains a valid function or variable, help about that
2981 will be displayed. If FUNC is empty, display an apropos about
2982 ERC commands. Otherwise, do `apropos' in the ERC namespace
2983 \(\"erc-.*LINE\").
2985 Examples:
2986 To find out about erc and bbdb, do
2987 /help bbdb.*
2989 For help about the WHOIS command, do:
2990 /help whois
2992 For a list of user commands (/join /part, ...):
2993 /help."
2994 (if func
2995 (let* ((sym (or (let ((sym (intern-soft
2996 (concat "erc-cmd-" (upcase func)))))
2997 (if (and sym (or (boundp sym) (fboundp sym)))
2999 nil))
3000 (let ((sym (intern-soft func)))
3001 (if (and sym (or (boundp sym) (fboundp sym)))
3003 nil))
3004 (let ((sym (intern-soft (concat "erc-" func))))
3005 (if (and sym (or (boundp sym) (fboundp sym)))
3007 nil)))))
3008 (if sym
3009 (cond
3010 ((boundp sym) (describe-variable sym))
3011 ((fboundp sym) (describe-function sym))
3012 (t nil))
3013 (apropos-command (concat "erc-.*" func) nil
3014 (lambda (x)
3015 (or (commandp x)
3016 (get x 'custom-type))))
3018 (apropos "erc-cmd-")
3019 (message "Type C-h m to get additional information about keybindings.")
3022 (defalias 'erc-cmd-H 'erc-cmd-HELP)
3023 (put 'erc-cmd-HELP 'process-not-needed t)
3025 (defun erc-cmd-JOIN (channel &optional key)
3026 "Join the channel given in CHANNEL, optionally with KEY.
3027 If CHANNEL is specified as \"-invite\", join the channel to which you
3028 were most recently invited. See also `invitation'."
3029 (let (chnl)
3030 (if (string= (upcase channel) "-INVITE")
3031 (if erc-invitation
3032 (setq chnl erc-invitation)
3033 (erc-display-message nil 'error (current-buffer) 'no-invitation))
3034 (setq chnl (erc-ensure-channel-name channel)))
3035 (when chnl
3036 ;; Prevent double joining of same channel on same server.
3037 (let ((joined-channels
3038 (mapcar #'(lambda (chanbuf)
3039 (with-current-buffer chanbuf (erc-default-target)))
3040 (erc-channel-list erc-server-process))))
3041 (if (erc-member-ignore-case chnl joined-channels)
3042 (switch-to-buffer (car (erc-member-ignore-case chnl
3043 joined-channels)))
3044 (erc-log (format "cmd: JOIN: %s" chnl))
3045 (erc-server-send (if (and chnl key)
3046 (format "JOIN %s %s" chnl key)
3047 (format "JOIN %s" chnl)))))))
3050 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
3051 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
3053 (defvar erc-channel-new-member-names nil
3054 "If non-nil, a names list is currently being received.
3056 If non-nil, this variable is a hash-table that associates
3057 received nicks with t.")
3058 (make-variable-buffer-local 'erc-channel-new-member-names)
3060 (defun erc-cmd-NAMES (&optional channel)
3061 "Display the users in CHANNEL.
3062 If CHANNEL is not specified, display the users in the current channel.
3063 This function clears the channel name list first, then sends the
3064 command."
3065 (let ((tgt (or (and (erc-channel-p channel) channel)
3066 (erc-default-target))))
3067 (if (and tgt (erc-channel-p tgt))
3068 (progn
3069 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
3070 (erc-with-buffer
3071 (tgt)
3072 (erc-channel-begin-receiving-names))
3073 (erc-server-send (concat "NAMES " tgt)))
3074 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
3076 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
3078 (defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
3079 "Kick the user indicated in LINE from the current channel.
3080 LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
3081 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
3082 (if (string= "" reasonstring)
3083 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
3084 (if (erc-channel-p target)
3085 (let ((nick reason-or-nick))
3086 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
3087 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
3088 nil target)
3090 (when target
3091 (let ((ch (erc-default-target)))
3092 (setq reasonstring (concat
3093 (if reason-or-nick (concat reason-or-nick " "))
3094 reasonstring))
3095 (if ch
3096 (progn
3097 (erc-log
3098 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
3099 (erc-server-send
3100 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
3101 (erc-display-message nil 'error (current-buffer)
3102 'no-default-channel))
3103 t)))))
3105 (defvar erc-script-args nil)
3107 (defun erc-cmd-LOAD (line)
3108 "Load the script provided in the LINE.
3109 If LINE continues beyond the file name, the rest of
3110 it is put in a (local) variable `erc-script-args',
3111 which can be used in Emacs Lisp scripts.
3113 The optional FORCE argument is ignored here - you can't force loading
3114 a script after exceeding the flood threshold."
3115 (cond
3116 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
3117 (let* ((file-to-find (match-string 1 line))
3118 (erc-script-args (match-string 2 line))
3119 (file (erc-find-file file-to-find erc-script-path)))
3120 (erc-log (format "cmd: LOAD: %s" file-to-find))
3121 (cond
3122 ((not file)
3123 (erc-display-message nil 'error (current-buffer)
3124 'cannot-find-file ?f file-to-find))
3125 ((not (file-readable-p file))
3126 (erc-display-message nil 'error (current-buffer)
3127 'cannot-read-file ?f file))
3129 (message "Loading `%s'..." file)
3130 (erc-load-script file)
3131 (message "Loading `%s'...done" file))))
3133 (t nil)))
3135 (defun erc-cmd-WHOIS (user &optional server)
3136 "Display whois information for USER.
3138 If SERVER is non-nil, use that, rather than the current server."
3139 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
3140 (let ((send (if server
3141 (format "WHOIS %s %s" user server)
3142 (format "WHOIS %s" user))))
3143 (erc-log (format "cmd: %s" send))
3144 (erc-server-send send)
3146 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
3148 (defun erc-cmd-WHOAMI ()
3149 "Display whois information about yourself."
3150 (erc-cmd-WHOIS (erc-current-nick))
3153 (defun erc-cmd-IDLE (nick)
3154 "Show the length of time NICK has been idle."
3155 (let ((origbuf (current-buffer))
3156 symlist)
3157 (erc-with-server-buffer
3158 (push (cons (erc-once-with-server-event
3159 311 (lambda (_proc parsed)
3160 (string= nick
3161 (nth 1 (erc-response.command-args
3162 parsed)))))
3163 'erc-server-311-functions)
3164 symlist)
3165 (push (cons (erc-once-with-server-event
3166 312 (lambda (_proc parsed)
3167 (string= nick
3168 (nth 1 (erc-response.command-args
3169 parsed)))))
3170 'erc-server-312-functions)
3171 symlist)
3172 (push (cons (erc-once-with-server-event
3173 318 (lambda (_proc parsed)
3174 (string= nick
3175 (nth 1 (erc-response.command-args
3176 parsed)))))
3177 'erc-server-318-functions)
3178 symlist)
3179 (push (cons (erc-once-with-server-event
3180 319 (lambda (_proc parsed)
3181 (string= nick
3182 (nth 1 (erc-response.command-args
3183 parsed)))))
3184 'erc-server-319-functions)
3185 symlist)
3186 (push (cons (erc-once-with-server-event
3187 320 (lambda (_proc parsed)
3188 (string= nick
3189 (nth 1 (erc-response.command-args
3190 parsed)))))
3191 'erc-server-320-functions)
3192 symlist)
3193 (push (cons (erc-once-with-server-event
3194 330 (lambda (_proc parsed)
3195 (string= nick
3196 (nth 1 (erc-response.command-args
3197 parsed)))))
3198 'erc-server-330-functions)
3199 symlist)
3200 (push (cons (erc-once-with-server-event
3202 (lambda (_proc parsed)
3203 (let ((idleseconds
3204 (string-to-number
3205 (cl-third
3206 (erc-response.command-args parsed)))))
3207 (erc-display-line
3208 (erc-make-notice
3209 (format "%s has been idle for %s."
3210 (erc-string-no-properties nick)
3211 (erc-seconds-to-string idleseconds)))
3212 origbuf)
3213 t)))
3214 'erc-server-317-functions)
3215 symlist)
3217 ;; Send the WHOIS command.
3218 (erc-cmd-WHOIS nick)
3220 ;; Remove the uninterned symbols from the server hooks that did not run.
3221 (run-at-time 20 nil (lambda (buf symlist)
3222 (with-current-buffer buf
3223 (dolist (sym symlist)
3224 (let ((hooksym (cdr sym))
3225 (funcsym (car sym)))
3226 (remove-hook hooksym funcsym t)))))
3227 (current-buffer) symlist)))
3230 (defun erc-cmd-DESCRIBE (line)
3231 "Pose some action to a certain user.
3232 LINE has the format \"USER ACTION\"."
3233 (cond
3234 ((string-match
3235 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
3236 (let ((dst (match-string 1 line))
3237 (s (match-string 2 line)))
3238 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
3239 (erc-send-action dst s))
3241 (t nil)))
3242 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
3244 (defun erc-cmd-ME (line)
3245 "Send LINE as an action."
3246 (cond
3247 ((string-match "^\\s-\\(.*\\)$" line)
3248 (let ((s (match-string 1 line)))
3249 (erc-log (format "cmd: ME: %s" s))
3250 (erc-send-action (erc-default-target) s))
3252 (t nil)))
3253 (put 'erc-cmd-ME 'do-not-parse-args t)
3255 (defun erc-cmd-ME\'S (line)
3256 "Do a /ME command, but add the string \" \\='s\" to the beginning."
3257 (erc-cmd-ME (concat " 's" line)))
3258 (put 'erc-cmd-ME\'S 'do-not-parse-args t)
3260 (defun erc-cmd-LASTLOG (line)
3261 "Show all lines in the current buffer matching the regexp LINE.
3263 If a match spreads across multiple lines, all those lines are shown.
3265 The lines are shown in a buffer named `*Occur*'.
3266 It serves as a menu to find any of the occurrences in this buffer.
3267 \\[describe-mode] in that buffer will explain how.
3269 If LINE contains upper case characters (excluding those preceded by `\\'),
3270 the matching is case-sensitive."
3271 (occur line)
3273 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
3274 (put 'erc-cmd-LASTLOG 'process-not-needed t)
3276 (defun erc-send-message (line &optional force)
3277 "Send LINE to the current channel or user and display it.
3279 See also `erc-message' and `erc-display-line'."
3280 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
3281 (erc-display-line
3282 (concat (erc-format-my-nick) line)
3283 (current-buffer))
3284 ;; FIXME - treat multiline, run hooks, or remove me?
3287 (defun erc-cmd-MODE (line)
3288 "Change or display the mode value of a channel or user.
3289 The first word specifies the target. The rest is the mode string
3290 to send.
3292 If only one word is given, display the mode of that target.
3294 A list of valid mode strings for Freenode may be found at
3295 URL `http://freenode.net/using_the_network.shtml'."
3296 (cond
3297 ((string-match "^\\s-\\(.*\\)$" line)
3298 (let ((s (match-string 1 line)))
3299 (erc-log (format "cmd: MODE: %s" s))
3300 (erc-server-send (concat "MODE " line)))
3302 (t nil)))
3303 (put 'erc-cmd-MODE 'do-not-parse-args t)
3305 (defun erc-cmd-NOTICE (channel-or-user &rest message)
3306 "Send a notice to the channel or user given as the first word.
3307 The rest is the message to send."
3308 (erc-message "NOTICE" (concat channel-or-user " "
3309 (mapconcat #'identity message " "))))
3311 (defun erc-cmd-MSG (line)
3312 "Send a message to the channel or user given as the first word in LINE.
3314 The rest of LINE is the message to send."
3315 (erc-message "PRIVMSG" line))
3317 (defalias 'erc-cmd-M 'erc-cmd-MSG)
3318 (put 'erc-cmd-MSG 'do-not-parse-args t)
3320 (defun erc-cmd-SQUERY (line)
3321 "Send a Service Query to the service given as the first word in LINE.
3323 The rest of LINE is the message to send."
3324 (erc-message "SQUERY" line))
3326 (defun erc-cmd-NICK (nick)
3327 "Change current nickname to NICK."
3328 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
3329 (let ((nicklen (cdr (assoc "NICKLEN" (erc-with-server-buffer
3330 erc-server-parameters)))))
3331 (and nicklen (> (length nick) (string-to-number nicklen))
3332 (erc-display-message
3333 nil 'notice 'active 'nick-too-long
3334 ?i (length nick) ?l nicklen)))
3335 (erc-server-send (format "NICK %s" nick))
3336 (cond (erc-bad-nick
3337 (erc-set-current-nick nick)
3338 (erc-update-mode-line)
3339 (setq erc-bad-nick nil)))
3342 (defun erc-cmd-PART (line)
3343 "When LINE is an empty string, leave the current channel.
3344 Otherwise leave the channel indicated by LINE."
3345 (cond
3346 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
3347 (let* ((ch (match-string 1 line))
3348 (msg (match-string 2 line))
3349 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3350 (erc-log (format "cmd: PART: %s: %s" ch reason))
3351 (erc-server-send (if (string= reason "")
3352 (format "PART %s" ch)
3353 (format "PART %s :%s" ch reason))
3354 nil ch))
3356 ((string-match "^\\s-*\\(.*\\)$" line)
3357 (let* ((ch (erc-default-target))
3358 (msg (match-string 1 line))
3359 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3360 (if (and ch (erc-channel-p ch))
3361 (progn
3362 (erc-log (format "cmd: PART: %s: %s" ch reason))
3363 (erc-server-send (if (string= reason "")
3364 (format "PART %s" ch)
3365 (format "PART %s :%s" ch reason))
3366 nil ch))
3367 (erc-display-message nil 'error (current-buffer) 'no-target)))
3369 (t nil)))
3370 (put 'erc-cmd-PART 'do-not-parse-args t)
3372 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
3374 (defun erc-cmd-PING (recipient)
3375 "Ping RECIPIENT."
3376 (let ((time (format "%f" (erc-current-time))))
3377 (erc-log (format "cmd: PING: %s" time))
3378 (erc-cmd-CTCP recipient "PING" time)))
3380 (defun erc-cmd-QUOTE (line)
3381 "Send LINE directly to the server.
3382 All the text given as argument is sent to the sever as unmodified,
3383 just as you provided it. Use this command with care!"
3384 (cond
3385 ((string-match "^ ?\\(.+\\)$" line)
3386 (erc-server-send (match-string 1 line)))
3387 (t nil)))
3388 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
3390 (defcustom erc-query-display 'window
3391 "Indicates how to display query buffers when using the /QUERY
3392 command to talk to someone.
3394 The default behavior is to display the message in a new window
3395 and bring it to the front. See the documentation for
3396 `erc-join-buffer' for a description of the available choices.
3398 See also `erc-auto-query' to decide how private messages from
3399 other people should be displayed."
3400 :group 'erc-query
3401 :type '(choice (const :tag "Split window and select" window)
3402 (const :tag "Split window, don't select" window-noselect)
3403 (const :tag "New frame" frame)
3404 (const :tag "Bury in new buffer" bury)
3405 (const :tag "Use current buffer" buffer)
3406 (const :tag "Use current buffer" t)))
3408 (defun erc-cmd-QUERY (&optional user)
3409 "Open a query with USER.
3410 The type of query window/frame/etc will depend on the value of
3411 `erc-query-display'.
3413 If USER is omitted, close the current query buffer if one exists
3414 - except this is broken now ;-)"
3415 (interactive
3416 (list (read-from-minibuffer "Start a query with: " nil)))
3417 (let ((session-buffer (erc-server-buffer))
3418 (erc-join-buffer erc-query-display))
3419 (if user
3420 (erc-query user session-buffer)
3421 ;; currently broken, evil hack to display help anyway
3422 ;(erc-delete-query))))
3423 (signal 'wrong-number-of-arguments ""))))
3424 (defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3426 (defun erc-quit/part-reason-default ()
3427 "Default quit/part message."
3428 (format "\C-bERC\C-b (IRC client for Emacs %s)" emacs-version))
3431 (defun erc-quit-reason-normal (&optional s)
3432 "Normal quit message.
3434 If S is non-nil, it will be used as the quit reason."
3435 (or s (erc-quit/part-reason-default)))
3437 (defun erc-quit-reason-zippy (&optional s)
3438 "Zippy quit message.
3440 If S is non-nil, it will be used as the quit reason."
3441 (or s
3442 (if (fboundp 'yow)
3443 (erc-replace-regexp-in-string "\n" "" (yow))
3444 (erc-quit/part-reason-default))))
3446 (make-obsolete 'erc-quit-reason-zippy "it will be removed." "24.4")
3448 (defun erc-quit-reason-various (s)
3449 "Choose a quit reason based on S (a string)."
3450 (when (featurep 'xemacs) (require 'poe))
3451 (let ((res (car (assoc-default (or s "")
3452 erc-quit-reason-various-alist 'string-match))))
3453 (cond
3454 ((functionp res) (funcall res))
3455 ((stringp res) res)
3456 (s s)
3457 (t (erc-quit/part-reason-default)))))
3459 (defun erc-part-reason-normal (&optional s)
3460 "Normal part message.
3462 If S is non-nil, it will be used as the part reason."
3463 (or s (erc-quit/part-reason-default)))
3465 (defun erc-part-reason-zippy (&optional s)
3466 "Zippy part message.
3468 If S is non-nil, it will be used as the quit reason."
3469 (or s
3470 (if (fboundp 'yow)
3471 (erc-replace-regexp-in-string "\n" "" (yow))
3472 (erc-quit/part-reason-default))))
3474 (make-obsolete 'erc-part-reason-zippy "it will be removed." "24.4")
3476 (defun erc-part-reason-various (s)
3477 "Choose a part reason based on S (a string)."
3478 (when (featurep 'xemacs) (require 'poe))
3479 (let ((res (car (assoc-default (or s "")
3480 erc-part-reason-various-alist 'string-match))))
3481 (cond
3482 ((functionp res) (funcall res))
3483 ((stringp res) res)
3484 (s s)
3485 (t (erc-quit/part-reason-default)))))
3487 (defun erc-cmd-QUIT (reason)
3488 "Disconnect from the current server.
3489 If REASON is omitted, display a default quit message, otherwise display
3490 the message given by REASON."
3491 (unless reason
3492 (setq reason ""))
3493 (cond
3494 ((string-match "^\\s-*\\(.*\\)$" reason)
3495 (let* ((s (match-string 1 reason))
3496 (buffer (erc-server-buffer))
3497 (reason (funcall erc-quit-reason (if (equal s "") nil s)))
3498 server-proc)
3499 (with-current-buffer (if (and buffer
3500 (bufferp buffer))
3501 buffer
3502 (current-buffer))
3503 (erc-log (format "cmd: QUIT: %s" reason))
3504 (setq erc-server-quitting t)
3505 (erc-set-active-buffer (erc-server-buffer))
3506 (setq server-proc erc-server-process)
3507 (erc-server-send (format "QUIT :%s" reason)))
3508 (run-hook-with-args 'erc-quit-hook server-proc)
3509 (when erc-kill-queries-on-quit
3510 (erc-kill-query-buffers server-proc))
3511 ;; if the process has not been killed within 4 seconds, kill it
3512 (run-at-time 4 nil
3513 (lambda (proc)
3514 (when (and (processp proc)
3515 (memq (process-status proc) '(run open)))
3516 (delete-process proc)))
3517 server-proc))
3519 (t nil)))
3521 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3522 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3523 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3524 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3525 (put 'erc-cmd-QUIT 'process-not-needed t)
3527 (defun erc-cmd-GQUIT (reason)
3528 "Disconnect from all servers at once with the same quit REASON."
3529 (erc-with-all-buffers-of-server nil #'erc-open-server-buffer-p
3530 (erc-cmd-QUIT reason))
3531 (when erc-kill-queries-on-quit
3532 ;; if the query buffers have not been killed within 4 seconds,
3533 ;; kill them
3534 (run-at-time
3535 4 nil
3536 (lambda ()
3537 (dolist (buffer (erc-buffer-list (lambda (buf)
3538 (not (erc-server-buffer-p buf)))))
3539 (kill-buffer buffer)))))
3542 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3543 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3544 (put 'erc-cmd-GQUIT 'process-not-needed t)
3546 (defun erc-cmd-RECONNECT ()
3547 "Try to reconnect to the current IRC server."
3548 (let ((buffer (erc-server-buffer))
3549 (process nil))
3550 (unless (buffer-live-p buffer)
3551 (setq buffer (current-buffer)))
3552 (with-current-buffer buffer
3553 (setq erc-server-quitting nil)
3554 (setq erc-server-reconnecting t)
3555 (setq erc-server-reconnect-count 0)
3556 (setq process (get-buffer-process (erc-server-buffer)))
3557 (if process
3558 (delete-process process)
3559 (erc-server-reconnect))
3560 (setq erc-server-reconnecting nil)))
3562 (put 'erc-cmd-RECONNECT 'process-not-needed t)
3564 (defun erc-cmd-SERVER (server)
3565 "Connect to SERVER, leaving existing connection intact."
3566 (erc-log (format "cmd: SERVER: %s" server))
3567 (condition-case nil
3568 (erc :server server :nick (erc-current-nick))
3569 (error
3570 (erc-error "Cannot find host %s." server)))
3572 (put 'erc-cmd-SERVER 'process-not-needed t)
3574 (defvar motif-version-string)
3575 (defvar gtk-version-string)
3577 (defun erc-cmd-SV ()
3578 "Say the current ERC and Emacs version into channel."
3579 (erc-send-message (format "I'm using ERC with %s %s (%s%s) of %s."
3580 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3581 emacs-version
3582 system-configuration
3583 (concat
3584 (cond ((featurep 'motif)
3585 (concat ", " (substring
3586 motif-version-string 4)))
3587 ((featurep 'gtk)
3588 (concat ", GTK+ Version "
3589 gtk-version-string))
3590 ((featurep 'x-toolkit) ", X toolkit")
3591 (t ""))
3592 (if (and (boundp 'x-toolkit-scroll-bars)
3593 (memq x-toolkit-scroll-bars
3594 '(xaw xaw3d)))
3595 (format ", %s scroll bars"
3596 (capitalize (symbol-name
3597 x-toolkit-scroll-bars)))
3599 (if (featurep 'multi-tty) ", multi-tty" ""))
3600 erc-emacs-build-time))
3603 (defun erc-cmd-SM ()
3604 "Say the current ERC modes into channel."
3605 (erc-send-message (format "I'm using the following modules: %s!"
3606 (erc-modes)))
3609 (defun erc-cmd-DEOP (&rest people)
3610 "Remove the operator setting from user(s) given in PEOPLE."
3611 (when (> (length people) 0)
3612 (erc-server-send (concat "MODE " (erc-default-target)
3613 " -"
3614 (make-string (length people) ?o)
3616 (mapconcat 'identity people " ")))
3619 (defun erc-cmd-OP (&rest people)
3620 "Add the operator setting to users(s) given in PEOPLE."
3621 (when (> (length people) 0)
3622 (erc-server-send (concat "MODE " (erc-default-target)
3623 " +"
3624 (make-string (length people) ?o)
3626 (mapconcat 'identity people " ")))
3629 (defun erc-cmd-TIME (&optional line)
3630 "Request the current time and date from the current server."
3631 (cond
3632 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3633 (let ((args (match-string 1 line)))
3634 (erc-log (format "cmd: TIME: %s" args))
3635 (erc-server-send (concat "TIME " args)))
3637 (t (erc-server-send "TIME"))))
3638 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3640 (defun erc-cmd-TOPIC (topic)
3641 "Set or request the topic for a channel.
3642 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3643 or the empty string.
3645 If no #CHANNEL is given, the default channel is used. If TOPIC is
3646 given, the channel topic is modified, otherwise the current topic will
3647 be displayed."
3648 (cond
3649 ;; /topic #channel TOPIC
3650 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3651 (let ((ch (match-string 1 topic))
3652 (topic (match-string 2 topic)))
3653 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3654 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3656 ;; /topic #channel
3657 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3658 (let ((ch (match-string 1 topic)))
3659 (erc-server-send (format "TOPIC %s" ch) nil ch)
3661 ;; /topic
3662 ((string-match "^\\s-*$" topic)
3663 (let ((ch (erc-default-target)))
3664 (erc-server-send (format "TOPIC %s" ch) nil ch)
3666 ;; /topic TOPIC
3667 ((string-match "^\\s-*\\(.*\\)$" topic)
3668 (let ((ch (erc-default-target))
3669 (topic (match-string 1 topic)))
3670 (if (and ch (erc-channel-p ch))
3671 (progn
3672 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3673 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3674 (erc-display-message nil 'error (current-buffer) 'no-target)))
3676 (t nil)))
3677 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3678 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3680 (defun erc-cmd-APPENDTOPIC (topic)
3681 "Append TOPIC to the current channel topic, separated by a space."
3682 (let ((oldtopic erc-channel-topic))
3683 ;; display help when given no arguments
3684 (when (string-match "^\\s-*$" topic)
3685 (signal 'wrong-number-of-arguments nil))
3686 ;; strip trailing ^O
3687 (when (string-match "\\(.*\\)\C-o" oldtopic)
3688 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3689 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3690 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3692 (defun erc-cmd-CLEARTOPIC (&optional channel)
3693 "Clear the topic for a CHANNEL.
3694 If CHANNEL is not specified, clear the topic for the default channel."
3695 (interactive "sClear topic of channel (RET is current channel): ")
3696 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3697 (when chnl
3698 (erc-server-send (format "TOPIC %s :" chnl))
3699 t)))
3701 ;;; Banlists
3703 (defvar erc-channel-banlist nil
3704 "A list of bans seen for the current channel.
3706 Each ban is an alist of the form:
3707 (WHOSET . MASK)
3709 The property `received-from-server' indicates whether
3710 or not the ban list has been requested from the server.")
3711 (make-variable-buffer-local 'erc-channel-banlist)
3712 (put 'erc-channel-banlist 'received-from-server nil)
3714 (defun erc-cmd-BANLIST ()
3715 "Pretty-print the contents of `erc-channel-banlist'.
3717 The ban list is fetched from the server if necessary."
3718 (let ((chnl (erc-default-target))
3719 (chnl-name (buffer-name)))
3721 (cond
3722 ((not (erc-channel-p chnl))
3723 (erc-display-line (erc-make-notice "You're not on a channel\n")
3724 'active))
3726 ((not (get 'erc-channel-banlist 'received-from-server))
3727 (let ((old-367-hook erc-server-367-functions))
3728 (setq erc-server-367-functions 'erc-banlist-store
3729 erc-channel-banlist nil)
3730 ;; fetch the ban list then callback
3731 (erc-with-server-buffer
3732 (erc-once-with-server-event
3734 (lambda (_proc _parsed)
3735 (with-current-buffer chnl-name
3736 (put 'erc-channel-banlist 'received-from-server t)
3737 (setq erc-server-367-functions old-367-hook)
3738 (erc-cmd-BANLIST)
3739 t)))
3740 (erc-server-send (format "MODE %s b" chnl)))))
3742 ((null erc-channel-banlist)
3743 (erc-display-line (erc-make-notice
3744 (format "No bans for channel: %s\n" chnl))
3745 'active)
3746 (put 'erc-channel-banlist 'received-from-server nil))
3749 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3750 erc-fill-column)
3751 (and (boundp 'fill-column)
3752 fill-column)
3753 (1- (window-width))))
3754 (separator (make-string erc-fill-column ?=))
3755 (fmt (concat
3756 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3757 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3759 (erc-display-line
3760 (erc-make-notice (format "Ban list for channel: %s\n"
3761 (erc-default-target)))
3762 'active)
3764 (erc-display-line separator 'active)
3765 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3766 (erc-display-line separator 'active)
3768 (mapc
3769 (lambda (x)
3770 (erc-display-line
3771 (format fmt
3772 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3773 (if (car x)
3774 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3775 ""))
3776 'active))
3777 erc-channel-banlist)
3779 (erc-display-line (erc-make-notice "End of Ban list")
3780 'active)
3781 (put 'erc-channel-banlist 'received-from-server nil)))))
3784 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3786 (defun erc-cmd-MASSUNBAN ()
3787 "Mass Unban.
3789 Unban all currently banned users in the current channel."
3790 (let ((chnl (erc-default-target)))
3791 (cond
3793 ((not (erc-channel-p chnl))
3794 (erc-display-line
3795 (erc-make-notice "You're not on a channel\n")
3796 'active))
3798 ((not (get 'erc-channel-banlist 'received-from-server))
3799 (let ((old-367-hook erc-server-367-functions))
3800 (setq erc-server-367-functions 'erc-banlist-store)
3801 ;; fetch the ban list then callback
3802 (erc-with-server-buffer
3803 (erc-once-with-server-event
3805 (lambda (_proc _parsed)
3806 (with-current-buffer chnl
3807 (put 'erc-channel-banlist 'received-from-server t)
3808 (setq erc-server-367-functions old-367-hook)
3809 (erc-cmd-MASSUNBAN)
3810 t)))
3811 (erc-server-send (format "MODE %s b" chnl)))))
3813 (t (let ((bans (mapcar #'cdr erc-channel-banlist)))
3814 (when bans
3815 ;; Glob the bans into groups of three, and carry out the unban.
3816 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3817 (mapc
3818 (lambda (x)
3819 (erc-server-send
3820 (format "MODE %s -%s %s" (erc-default-target)
3821 (make-string (length x) ?b)
3822 (mapconcat 'identity x " "))))
3823 (erc-group-list bans 3))))
3824 t))))
3826 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3828 ;;;; End of IRC commands
3830 (defun erc-ensure-channel-name (channel)
3831 "Return CHANNEL if it is a valid channel name.
3832 Eventually add a # in front of it, if that turns it into a valid channel name."
3833 (if (erc-channel-p channel)
3834 channel
3835 (concat "#" channel)))
3837 (defun erc-grab-region (start end)
3838 "Copy the region between START and END in a recreatable format.
3840 Converts all the IRC text properties in each line of the region
3841 into control codes and writes them to a separate buffer. The
3842 resulting text may be used directly as a script to generate this
3843 text again."
3844 (interactive "r")
3845 (erc-set-active-buffer (current-buffer))
3846 (save-excursion
3847 (let* ((cb (current-buffer))
3848 (buf (generate-new-buffer erc-grab-buffer-name))
3849 (region (buffer-substring start end))
3850 (lines (erc-split-multiline-safe region)))
3851 (set-buffer buf)
3852 (dolist (line lines)
3853 (insert (concat line "\n")))
3854 (set-buffer cb)
3855 (switch-to-buffer-other-window buf)))
3856 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3857 (ding))
3859 (defun erc-display-prompt (&optional buffer pos prompt face)
3860 "Display PROMPT in BUFFER at position POS.
3861 Display an ERC prompt in BUFFER.
3863 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3864 If BUFFER is nil, the `current-buffer' is used.
3865 If POS is nil, PROMPT will be displayed at `point'.
3866 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3867 `erc-prompt-face' will be used."
3868 (let* ((prompt (or prompt (erc-prompt)))
3869 (l (length prompt))
3870 (ob (current-buffer)))
3871 ;; We cannot use save-excursion because we move point, therefore
3872 ;; we resort to the ol' ob trick to restore this.
3873 (when (and buffer (bufferp buffer))
3874 (set-buffer buffer))
3876 ;; now save excursion again to store where point and mark are
3877 ;; in the current buffer
3878 (save-excursion
3879 (setq pos (or pos (point)))
3880 (goto-char pos)
3881 (when (> l 0)
3882 ;; Do not extend the text properties when typing at the end
3883 ;; of the prompt, but stuff typed in front of the prompt
3884 ;; shall remain part of the prompt.
3885 (setq prompt (erc-propertize prompt
3886 'start-open t ; XEmacs
3887 'rear-nonsticky t ; Emacs
3888 'erc-prompt t
3889 'field t
3890 'front-sticky t
3891 'read-only t))
3892 (erc-put-text-property 0 (1- (length prompt))
3893 'face (or face 'erc-prompt-face)
3894 prompt)
3895 (insert prompt))
3896 ;; Set the input marker
3897 (set-marker erc-input-marker (point)))
3899 ;; Now we are back at the old position. If the prompt was
3900 ;; inserted here or before us, advance point by the length of
3901 ;; the prompt.
3902 (when (or (not pos) (<= (point) pos))
3903 (forward-char l))
3904 ;; Clear the undo buffer now, so the user can undo his stuff,
3905 ;; but not the stuff we did. Sneaky!
3906 (setq buffer-undo-list nil)
3907 (set-buffer ob)))
3909 ;; interactive operations
3911 (defun erc-input-message ()
3912 "Read input from the minibuffer."
3913 (interactive)
3914 (let ((minibuffer-allow-text-properties t)
3915 (read-map minibuffer-local-map))
3916 (insert (read-from-minibuffer "Message: "
3917 (string (if (featurep 'xemacs)
3918 last-command-char
3919 last-command-event))
3920 read-map))
3921 (erc-send-current-line)))
3923 (defvar erc-action-history-list ()
3924 "History list for interactive action input.")
3926 (defun erc-input-action ()
3927 "Interactively input a user action and send it to IRC."
3928 (interactive "")
3929 (erc-set-active-buffer (current-buffer))
3930 (let ((action (read-from-minibuffer
3931 "Action: " nil nil nil 'erc-action-history-list)))
3932 (if (not (string-match "^\\s-*$" action))
3933 (erc-send-action (erc-default-target) action))))
3935 (defun erc-join-channel (channel &optional key)
3936 "Join CHANNEL.
3938 If `point' is at the beginning of a channel name, use that as default."
3939 (interactive
3940 (list
3941 (let ((chnl (if (looking-at "\\([&#+!][^ \n]+\\)") (match-string 1) ""))
3942 (table (when (erc-server-buffer-live-p)
3943 (set-buffer (process-buffer erc-server-process))
3944 erc-channel-list)))
3945 (completing-read "Join channel: " table nil nil nil nil chnl))
3946 (when (or current-prefix-arg erc-prompt-for-channel-key)
3947 (read-from-minibuffer "Channel key (RET for none): " nil))))
3948 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3950 (defun erc-part-from-channel (reason)
3951 "Part from the current channel and prompt for a REASON."
3952 (interactive
3953 (list
3954 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3955 reason
3956 (read-from-minibuffer (concat "Reason for leaving " (erc-default-target)
3957 ": ")))))
3958 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3960 (defun erc-set-topic (topic)
3961 "Prompt for a TOPIC for the current channel."
3962 (interactive
3963 (list
3964 (read-from-minibuffer
3965 (concat "Set topic of " (erc-default-target) ": ")
3966 (when erc-channel-topic
3967 (let ((ss (split-string erc-channel-topic "\C-o")))
3968 (cons (apply #'concat (if (cdr ss) (butlast ss) ss))
3969 0))))))
3970 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3971 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3973 (defun erc-set-channel-limit (&optional limit)
3974 "Set a LIMIT for the current channel. Remove limit if nil.
3975 Prompt for one if called interactively."
3976 (interactive (list (read-from-minibuffer
3977 (format "Limit for %s (RET to remove limit): "
3978 (erc-default-target)))))
3979 (let ((tgt (erc-default-target)))
3980 (erc-server-send (if (and limit (>= (length limit) 1))
3981 (format "MODE %s +l %s" tgt limit)
3982 (format "MODE %s -l" tgt)))))
3984 (defun erc-set-channel-key (&optional key)
3985 "Set a KEY for the current channel. Remove key if nil.
3986 Prompt for one if called interactively."
3987 (interactive (list (read-from-minibuffer
3988 (format "Key for %s (RET to remove key): "
3989 (erc-default-target)))))
3990 (let ((tgt (erc-default-target)))
3991 (erc-server-send (if (and key (>= (length key) 1))
3992 (format "MODE %s +k %s" tgt key)
3993 (format "MODE %s -k" tgt)))))
3995 (defun erc-quit-server (reason)
3996 "Disconnect from current server after prompting for REASON.
3997 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
3998 (interactive (list (read-from-minibuffer
3999 (format "Reason for quitting %s: "
4000 (or erc-server-announced-name
4001 erc-session-server)))))
4002 (erc-cmd-QUIT reason))
4004 ;; Movement of point
4006 (defun erc-bol ()
4007 "Move `point' to the beginning of the current line.
4009 This places `point' just after the prompt, or at the beginning of the line."
4010 (interactive)
4011 (forward-line 0)
4012 (when (get-text-property (point) 'erc-prompt)
4013 (goto-char erc-input-marker))
4014 (point))
4016 (defun erc-kill-input ()
4017 "Kill current input line using `erc-bol' followed by `kill-line'."
4018 (interactive)
4019 (when (and (erc-bol)
4020 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
4021 ;; there's nothing to kill
4022 (if (boundp 'erc-input-ring-index)
4023 (setq erc-input-ring-index nil))
4024 (kill-line)))
4026 (defun erc-complete-word-at-point ()
4027 (run-hook-with-args-until-success 'erc-complete-functions))
4029 (define-obsolete-function-alias 'erc-complete-word 'completion-at-point "24.1")
4031 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4033 ;; IRC SERVER INPUT HANDLING
4035 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4037 ;;;; New Input parsing
4039 ; Stolen from ZenIRC. I just wanna test this code, so here is
4040 ; experiment area.
4042 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
4043 erc-default-server-handler)
4044 "Default for server messages which aren't covered by `erc-server-hooks'."
4045 :group 'erc-server-hooks
4046 :type 'hook)
4048 (defun erc-default-server-handler (proc parsed)
4049 "Default server handler.
4051 Displays PROC and PARSED appropriately using `erc-display-message'."
4052 (erc-display-message
4053 parsed 'notice proc
4054 (mapconcat
4055 'identity
4056 (let (res)
4057 (mapc #'(lambda (x)
4058 (if (stringp x)
4059 (setq res (append res (list x)))))
4060 parsed)
4061 res)
4062 " ")))
4064 (defvar erc-server-vectors
4065 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
4066 "List of received server messages which ERC does not specifically handle.
4067 See `erc-debug-missing-hooks'.")
4068 ;(make-variable-buffer-local 'erc-server-vectors)
4070 (defun erc-debug-missing-hooks (_proc parsed)
4071 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
4072 These vectors can be helpful when adding new server message handlers to ERC.
4073 See `erc-default-server-hook'."
4074 (nconc erc-server-vectors (list parsed))
4075 nil)
4077 (defun erc-query (target server)
4078 "Open a query buffer on TARGET, using SERVER.
4079 To change how this query window is displayed, use `let' to bind
4080 `erc-join-buffer' before calling this."
4081 (unless (and server
4082 (buffer-live-p server)
4083 (set-buffer server))
4084 (error "Couldn't switch to server buffer"))
4085 (let ((buf (erc-open erc-session-server
4086 erc-session-port
4087 (erc-current-nick)
4088 erc-session-user-full-name
4091 (list target)
4092 target
4093 erc-server-process)))
4094 (unless buf
4095 (error "Couldn't open query window"))
4096 (erc-update-mode-line)
4097 buf))
4099 (defcustom erc-auto-query 'window-noselect
4100 "If non-nil, create a query buffer each time you receive a private message.
4101 If the buffer doesn't already exist, it is created.
4103 This can be set to a symbol, to control how the new query window
4104 should appear. The default behavior is to display the buffer in
4105 a new window, but not to select it. See the documentation for
4106 `erc-join-buffer' for a description of the available choices."
4107 :group 'erc-query
4108 :type '(choice (const :tag "Don't create query window" nil)
4109 (const :tag "Split window and select" window)
4110 (const :tag "Split window, don't select" window-noselect)
4111 (const :tag "New frame" frame)
4112 (const :tag "Bury in new buffer" bury)
4113 (const :tag "Use current buffer" buffer)
4114 (const :tag "Use current buffer" t)))
4116 (defcustom erc-query-on-unjoined-chan-privmsg t
4117 "If non-nil create query buffer on receiving any PRIVMSG at all.
4118 This includes PRIVMSGs directed to channels. If you are using an IRC
4119 bouncer, such as dircproxy, to keep a log of channels when you are
4120 disconnected, you should set this option to t."
4121 :group 'erc-query
4122 :type 'boolean)
4124 (defcustom erc-format-query-as-channel-p t
4125 "If non-nil, format text from others in a query buffer like in a channel,
4126 otherwise format like a private message."
4127 :group 'erc-query
4128 :type 'boolean)
4130 (defcustom erc-minibuffer-notice nil
4131 "If non-nil, print ERC notices for the user in the minibuffer.
4132 Only happens when the session buffer isn't visible."
4133 :group 'erc-display
4134 :type 'boolean)
4136 (defcustom erc-minibuffer-ignored nil
4137 "If non-nil, print a message in the minibuffer if we ignored something."
4138 :group 'erc-ignore
4139 :type 'boolean)
4141 (defun erc-wash-quit-reason (reason nick login host)
4142 "Remove duplicate text from quit REASON.
4143 Specifically in relation to NICK (user@host) information. Returns REASON
4144 unmodified if nothing can be removed.
4145 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
4146 \"Read error: 110\". The same applies for \"Ping Timeout\"."
4147 (setq nick (regexp-quote nick)
4148 login (regexp-quote login)
4149 host (regexp-quote host))
4150 (or (when (string-match (concat "^\\(Read error\\) to "
4151 nick "\\[" host "\\]: "
4152 "\\(.+\\)$")
4153 reason)
4154 (concat (match-string 1 reason) ": " (match-string 2 reason)))
4155 (when (string-match (concat "^\\(Ping timeout\\) for "
4156 nick "\\[" host "\\]$")
4157 reason)
4158 (match-string 1 reason))
4159 reason))
4161 (defun erc-nickname-in-use (nick reason)
4162 "If NICK is unavailable, tell the user the REASON.
4164 See also `erc-display-error-notice'."
4165 (if (or (not erc-try-new-nick-p)
4166 ;; how many default-nicks are left + one more try...
4167 (eq erc-nick-change-attempt-count
4168 (if (consp erc-nick)
4169 (+ (length erc-nick) 1)
4170 1)))
4171 (erc-display-error-notice
4173 (format "Nickname %s is %s, try another." nick reason))
4174 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
4175 (let ((newnick (nth 1 erc-default-nicks))
4176 (nicklen (cdr (assoc "NICKLEN"
4177 (erc-with-server-buffer
4178 erc-server-parameters)))))
4179 (setq erc-bad-nick t)
4180 ;; try to use a different nick
4181 (if erc-default-nicks
4182 (setq erc-default-nicks (cdr erc-default-nicks)))
4183 (if (not newnick)
4184 (setq newnick (concat (truncate-string-to-width
4185 nick
4186 (if (and erc-server-connected nicklen)
4187 (- (string-to-number nicklen)
4188 (length erc-nick-uniquifier))
4189 ;; rfc2812 max nick length = 9
4190 ;; we must assume this is the
4191 ;; server's setting if we haven't
4192 ;; established a connection yet
4193 (- 9 (length erc-nick-uniquifier))))
4194 erc-nick-uniquifier)))
4195 (erc-cmd-NICK newnick)
4196 (erc-display-error-notice
4198 (format "Nickname %s is %s, trying %s"
4199 nick reason newnick)))))
4201 ;;; Server messages
4203 (defgroup erc-server-hooks nil
4204 "Server event callbacks.
4205 Every server event - like numeric replies - has its own hook.
4206 Those hooks are all called using `run-hook-with-args-until-success'.
4207 They receive as first argument the process object from where the event
4208 originated from,
4209 and as second argument the event parsed as a vector."
4210 :group 'erc-hooks)
4212 (defun erc-display-server-message (_proc parsed)
4213 "Display the message sent by the server as a notice."
4214 (erc-display-message
4215 parsed 'notice 'active (erc-response.contents parsed)))
4217 (defun erc-auto-query (proc parsed)
4218 ;; FIXME: This needs more documentation, unless it's not a user function --
4219 ;; Lawrence 2004-01-08
4220 "Put this on `erc-server-PRIVMSG-functions'."
4221 (when erc-auto-query
4222 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
4223 (target (car (erc-response.command-args parsed)))
4224 (msg (erc-response.contents parsed))
4225 (query (if (not erc-query-on-unjoined-chan-privmsg)
4226 nick
4227 (if (erc-current-nick-p target)
4228 nick
4229 target))))
4230 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
4231 (or erc-query-on-unjoined-chan-privmsg
4232 (string= target (erc-current-nick)))
4233 (not (erc-get-buffer query proc))
4234 (not (erc-is-message-ctcp-and-not-action-p msg))
4235 (let ((erc-query-display erc-auto-query))
4236 (erc-cmd-QUERY query))
4237 nil))))
4239 (defun erc-is-message-ctcp-p (message)
4240 "Check if MESSAGE is a CTCP message or not."
4241 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
4243 (defun erc-is-message-ctcp-and-not-action-p (message)
4244 "Check if MESSAGE is a CTCP message or not."
4245 (and (erc-is-message-ctcp-p message)
4246 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
4248 (defun erc-format-privmessage (nick msg privp msgp)
4249 "Format a PRIVMSG in an insertable fashion."
4250 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
4251 (mark-e (if msgp (if privp "*" ">") "-"))
4252 (str (format "%s%s%s %s" mark-s nick mark-e msg))
4253 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
4254 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
4255 ;; add text properties to text before the nick, the nick and after the nick
4256 (erc-put-text-property 0 (length mark-s) 'face msg-face str)
4257 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
4258 'face nick-face str)
4259 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
4260 'face msg-face str)
4261 str))
4263 (defcustom erc-format-nick-function 'erc-format-nick
4264 "Function to format a nickname for message display."
4265 :group 'erc-display
4266 :type 'function)
4268 (defun erc-format-nick (&optional user _channel-data)
4269 "Return the nickname of USER.
4270 See also `erc-format-nick-function'."
4271 (when user (erc-server-user-nickname user)))
4273 (defun erc-get-user-mode-prefix (user)
4274 (when user
4275 (cond ((erc-channel-user-owner-p user)
4276 (erc-propertize "~" 'help-echo "owner"))
4277 ((erc-channel-user-admin-p user)
4278 (erc-propertize "&" 'help-echo "admin"))
4279 ((erc-channel-user-op-p user)
4280 (erc-propertize "@" 'help-echo "operator"))
4281 ((erc-channel-user-halfop-p user)
4282 (erc-propertize "%" 'help-echo "half-op"))
4283 ((erc-channel-user-voice-p user)
4284 (erc-propertize "+" 'help-echo "voice"))
4285 (t ""))))
4287 (defun erc-format-@nick (&optional user _channel-data)
4288 "Format the nickname of USER showing if USER has a voice, is an
4289 operator, half-op, admin or owner. Owners have \"~\", admins have
4290 \"&\", operators have \"@\" and users with voice have \"+\" as a
4291 prefix. Use CHANNEL-DATA to determine op and voice status. See
4292 also `erc-format-nick-function'."
4293 (when user
4294 (let ((nick (erc-server-user-nickname user)))
4295 (concat (erc-propertize
4296 (erc-get-user-mode-prefix nick)
4297 'face 'erc-nick-prefix-face)
4298 nick))))
4300 (defun erc-format-my-nick ()
4301 "Return the beginning of this user's message, correctly propertized."
4302 (if erc-show-my-nick
4303 (let* ((open "<")
4304 (close "> ")
4305 (nick (erc-current-nick))
4306 (mode (erc-get-user-mode-prefix nick)))
4307 (concat
4308 (erc-propertize open 'face 'erc-default-face)
4309 (erc-propertize mode 'face 'erc-my-nick-prefix-face)
4310 (erc-propertize nick 'face 'erc-my-nick-face)
4311 (erc-propertize close 'face 'erc-default-face)))
4312 (let ((prefix "> "))
4313 (erc-propertize prefix 'face 'erc-default-face))))
4315 (defun erc-echo-notice-in-default-buffer (s parsed buffer _sender)
4316 "Echos a private notice in the default buffer, namely the
4317 target buffer specified by BUFFER, or there is no target buffer,
4318 the server buffer. This function is designed to be added to
4319 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
4320 and always returns t."
4321 (erc-display-message parsed nil buffer s)
4324 (defun erc-echo-notice-in-target-buffer (s parsed buffer _sender)
4325 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
4326 function is designed to be added to either `erc-echo-notice-hook'
4327 or `erc-echo-notice-always-hook', and returns non-nil if BUFFER
4328 is non-nil."
4329 (if buffer
4330 (progn (erc-display-message parsed nil buffer s) t)
4331 nil))
4333 (defun erc-echo-notice-in-minibuffer (s _parsed _buffer _sender)
4334 "Echos a private notice in the minibuffer. This function is
4335 designed to be added to either `erc-echo-notice-hook' or
4336 `erc-echo-notice-always-hook', and always returns t."
4337 (message "%s" (concat "NOTICE: " s))
4340 (defun erc-echo-notice-in-server-buffer (s parsed _buffer _sender)
4341 "Echos a private notice in the server buffer. This function is
4342 designed to be added to either `erc-echo-notice-hook' or
4343 `erc-echo-notice-always-hook', and always returns t."
4344 (erc-display-message parsed nil nil s)
4347 (defun erc-echo-notice-in-active-non-server-buffer (s parsed _buffer _sender)
4348 "Echos a private notice in the active buffer if the active
4349 buffer is not the server buffer. This function is designed to be
4350 added to either `erc-echo-notice-hook' or
4351 `erc-echo-notice-always-hook', and returns non-nil if the active
4352 buffer is not the server buffer."
4353 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
4354 (progn (erc-display-message parsed nil 'active s) t)
4355 nil))
4357 (defun erc-echo-notice-in-active-buffer (s parsed _buffer _sender)
4358 "Echos a private notice in the active buffer. This function is
4359 designed to be added to either `erc-echo-notice-hook' or
4360 `erc-echo-notice-always-hook', and always returns t."
4361 (erc-display-message parsed nil 'active s)
4364 (defun erc-echo-notice-in-user-buffers (s parsed _buffer sender)
4365 "Echos a private notice in all of the buffers for which SENDER
4366 is a member. This function is designed to be added to either
4367 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4368 returns non-nil if there is at least one buffer for which the
4369 sender is a member.
4371 See also: `erc-echo-notice-in-first-user-buffer',
4372 `erc-buffer-list-with-nick'."
4373 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4374 (if buffers
4375 (progn (erc-display-message parsed nil buffers s) t)
4376 nil)))
4378 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
4379 "Echos a private notice in BUFFER and in all of the buffers for
4380 which SENDER is a member. This function is designed to be added
4381 to either `erc-echo-notice-hook' or
4382 `erc-echo-notice-always-hook', and returns non-nil if there is
4383 at least one buffer for which the sender is a member or the
4384 default target.
4386 See also: `erc-echo-notice-in-user-buffers',
4387 `erc-buffer-list-with-nick'."
4388 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4389 (unless (memq buffer buffers) (push buffer buffers))
4390 (if buffers ;FIXME: How could it be nil?
4391 (progn (erc-display-message parsed nil buffers s) t)
4392 nil)))
4394 (defun erc-echo-notice-in-first-user-buffer (s parsed _buffer sender)
4395 "Echos a private notice in one of the buffers for which SENDER
4396 is a member. This function is designed to be added to either
4397 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4398 returns non-nil if there is at least one buffer for which the
4399 sender is a member.
4401 See also: `erc-echo-notice-in-user-buffers',
4402 `erc-buffer-list-with-nick'."
4403 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4404 (if buffers
4405 (progn (erc-display-message parsed nil (car buffers) s) t)
4406 nil)))
4408 ;;; Ban manipulation
4410 (defun erc-banlist-store (proc parsed)
4411 "Record ban entries for a channel."
4412 (pcase-let ((`(,channel ,mask ,whoset)
4413 (cdr (erc-response.command-args parsed))))
4414 ;; Determine to which buffer the message corresponds
4415 (let ((buffer (erc-get-buffer channel proc)))
4416 (with-current-buffer buffer
4417 (unless (member (cons whoset mask) erc-channel-banlist)
4418 (setq erc-channel-banlist (cons (cons whoset mask)
4419 erc-channel-banlist))))))
4420 nil)
4422 (defun erc-banlist-finished (proc parsed)
4423 "Record that we have received the banlist."
4424 (let* ((channel (nth 1 (erc-response.command-args parsed)))
4425 (buffer (erc-get-buffer channel proc)))
4426 (with-current-buffer buffer
4427 (put 'erc-channel-banlist 'received-from-server t)))
4428 t) ; suppress the 'end of banlist' message
4430 (defun erc-banlist-update (proc parsed)
4431 "Check MODE commands for bans and update the banlist appropriately."
4432 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
4433 (let* ((tgt (car (erc-response.command-args parsed)))
4434 (mode (erc-response.contents parsed))
4435 (whoset (erc-response.sender parsed))
4436 (buffer (erc-get-buffer tgt proc)))
4437 (when buffer
4438 (with-current-buffer buffer
4439 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
4440 ((string-match "^\\([+-]\\)b" mode)
4441 ;; This is a ban
4442 (cond
4443 ((string-match "^-" mode)
4444 ;; Remove the unbanned masks from the ban list
4445 (setq erc-channel-banlist
4446 (erc-delete-if
4447 #'(lambda (y)
4448 (member (upcase (cdr y))
4449 (mapcar #'upcase
4450 (cdr (split-string mode)))))
4451 erc-channel-banlist)))
4452 ((string-match "^+" mode)
4453 ;; Add the banned mask(s) to the ban list
4454 (mapc
4455 (lambda (mask)
4456 (unless (member (cons whoset mask) erc-channel-banlist)
4457 (setq erc-channel-banlist
4458 (cons (cons whoset mask) erc-channel-banlist))))
4459 (cdr (split-string mode))))))))))
4460 nil)
4462 ;; used for the banlist cmds
4463 (defun erc-group-list (list n)
4464 "Group LIST into sublists of length N."
4465 (cond ((null list) nil)
4466 ((null (nthcdr n list)) (list list))
4467 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
4470 ;;; MOTD numreplies
4472 (defun erc-handle-login ()
4473 "Handle the logging in process of connection."
4474 (unless erc-logged-in
4475 (setq erc-logged-in t)
4476 (message "Logging in as `%s'... done" (erc-current-nick))
4477 ;; execute a startup script
4478 (let ((f (erc-select-startup-file)))
4479 (when f
4480 (erc-load-script f)))))
4482 (defun erc-connection-established (proc parsed)
4483 "Run just after connection.
4485 Set user modes and run `erc-after-connect' hook."
4486 (with-current-buffer (process-buffer proc)
4487 (unless erc-server-connected ; only once per session
4488 (let ((server (or erc-server-announced-name
4489 (erc-response.sender parsed)))
4490 (nick (car (erc-response.command-args parsed)))
4491 (buffer (process-buffer proc)))
4492 (setq erc-server-connected t)
4493 (setq erc-server-reconnect-count 0)
4494 (erc-update-mode-line)
4495 (erc-set-initial-user-mode nick buffer)
4496 (erc-server-setup-periodical-ping buffer)
4497 (run-hook-with-args 'erc-after-connect server nick)))))
4499 (defun erc-set-initial-user-mode (nick buffer)
4500 "If `erc-user-mode' is non-nil for NICK, set the user modes.
4501 The server buffer is given by BUFFER."
4502 (with-current-buffer buffer
4503 (when erc-user-mode
4504 (let ((mode (if (functionp erc-user-mode)
4505 (funcall erc-user-mode)
4506 erc-user-mode)))
4507 (when (stringp mode)
4508 (erc-log (format "changing mode for %s to %s" nick mode))
4509 (erc-server-send (format "MODE %s %s" nick mode)))))))
4511 (defun erc-display-error-notice (parsed string)
4512 "Display STRING as an error notice.
4514 See also `erc-display-message'."
4515 (erc-display-message
4516 parsed '(notice error) 'active string))
4518 (defun erc-process-ctcp-query (proc parsed nick login host)
4519 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4520 "Process a CTCP query."
4521 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4522 "\C-a"))))
4523 (if (> (length queries) 4)
4524 (erc-display-message
4525 parsed (list 'notice 'error) proc 'ctcp-too-many)
4526 (if (= 0 (length queries))
4527 (erc-display-message
4528 parsed (list 'notice 'error) proc
4529 'ctcp-empty ?n nick)
4530 (while queries
4531 (let* ((type (upcase (car (split-string (car queries)))))
4532 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4533 (if (and hook (boundp hook))
4534 (if (string-equal type "ACTION")
4535 (run-hook-with-args-until-success
4536 hook proc parsed nick login host
4537 (car (erc-response.command-args parsed))
4538 (car queries))
4539 (when erc-paranoid
4540 (if (erc-current-nick-p
4541 (car (erc-response.command-args parsed)))
4542 (erc-display-message
4543 parsed 'error 'active 'ctcp-request
4544 ?n nick ?u login ?h host ?r (car queries))
4545 (erc-display-message
4546 parsed 'error 'active 'ctcp-request-to
4547 ?n nick ?u login ?h host ?r (car queries)
4548 ?t (car (erc-response.command-args parsed)))))
4549 (run-hook-with-args-until-success
4550 hook proc nick login host
4551 (car (erc-response.command-args parsed))
4552 (car queries)))
4553 (erc-display-message
4554 parsed (list 'notice 'error) proc
4555 'undefined-ctcp)))
4556 (setq queries (cdr queries)))))))
4558 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4560 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4561 "Respond to a CTCP ACTION query."
4562 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4563 (let ((s (match-string 1 msg))
4564 (buf (or (erc-get-buffer to proc)
4565 (erc-get-buffer nick proc)
4566 (process-buffer proc))))
4567 (erc-display-message
4568 parsed 'action buf
4569 'ACTION ?n nick ?u login ?h host ?a s))))
4571 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4573 (defun erc-ctcp-query-CLIENTINFO (_proc nick _login _host _to msg)
4574 "Respond to a CTCP CLIENTINFO query."
4575 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4576 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4577 (unless erc-disable-ctcp-replies
4578 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4579 nil)
4581 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4582 (defun erc-ctcp-query-ECHO (_proc nick _login _host _to msg)
4583 "Respond to a CTCP ECHO query."
4584 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4585 (let ((s (match-string 1 msg)))
4586 (unless erc-disable-ctcp-replies
4587 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4588 nil)
4590 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4591 (defun erc-ctcp-query-FINGER (_proc nick _login _host _to _msg)
4592 "Respond to a CTCP FINGER query."
4593 (unless erc-disable-ctcp-replies
4594 (let ((s (if erc-anonymous-login
4595 (format "FINGER I'm %s." (erc-current-nick))
4596 (format "FINGER %s (%s@%s)."
4597 (user-full-name)
4598 (user-login-name)
4599 (system-name))))
4600 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4601 (when (> ns 0)
4602 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4603 (erc-send-ctcp-notice nick s)))
4604 nil)
4606 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4607 (defun erc-ctcp-query-PING (_proc nick _login _host _to msg)
4608 "Respond to a CTCP PING query."
4609 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4610 (unless erc-disable-ctcp-replies
4611 (let ((arg (match-string 1 msg)))
4612 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4613 nil)
4615 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4616 (defun erc-ctcp-query-TIME (_proc nick _login _host _to _msg)
4617 "Respond to a CTCP TIME query."
4618 (unless erc-disable-ctcp-replies
4619 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4620 nil)
4622 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4623 (defun erc-ctcp-query-USERINFO (_proc nick _login _host _to _msg)
4624 "Respond to a CTCP USERINFO query."
4625 (unless erc-disable-ctcp-replies
4626 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4627 nil)
4629 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4630 (defun erc-ctcp-query-VERSION (_proc nick _login _host _to _msg)
4631 "Respond to a CTCP VERSION query."
4632 (unless erc-disable-ctcp-replies
4633 (erc-send-ctcp-notice
4634 nick (format
4635 "VERSION \C-bERC\C-b - an IRC client for Emacs %s (\C-b%s\C-b)"
4636 emacs-version
4637 erc-official-location)))
4638 nil)
4640 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4641 "Process MSG as a CTCP reply."
4642 (let* ((type (car (split-string msg)))
4643 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4644 (if (boundp hook)
4645 (run-hook-with-args-until-success
4646 hook proc nick login host
4647 (car (erc-response.command-args parsed)) msg)
4648 (erc-display-message
4649 parsed 'notice 'active
4650 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4652 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4653 (defun erc-ctcp-reply-ECHO (_proc nick _login _host _to msg)
4654 "Handle a CTCP ECHO reply."
4655 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4656 (let ((message (match-string 1 msg)))
4657 (erc-display-message
4658 nil '(notice action) 'active
4659 'CTCP-ECHO ?n nick ?m message)))
4660 nil)
4662 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4663 (defun erc-ctcp-reply-CLIENTINFO (_proc nick _login _host _to msg)
4664 "Handle a CTCP CLIENTINFO reply."
4665 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4666 (let ((message (match-string 1 msg)))
4667 (erc-display-message
4668 nil 'notice 'active
4669 'CTCP-CLIENTINFO ?n nick ?m message)))
4670 nil)
4672 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4673 (defun erc-ctcp-reply-FINGER (_proc nick _login _host _to msg)
4674 "Handle a CTCP FINGER reply."
4675 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4676 (let ((message (match-string 1 msg)))
4677 (erc-display-message
4678 nil 'notice 'active
4679 'CTCP-FINGER ?n nick ?m message)))
4680 nil)
4682 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4683 (defun erc-ctcp-reply-PING (_proc nick _login _host _to msg)
4684 "Handle a CTCP PING reply."
4685 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4687 (let ((time (match-string 1 msg)))
4688 (condition-case nil
4689 (let ((delta (erc-time-diff (string-to-number time)
4690 (erc-current-time))))
4691 (erc-display-message
4692 nil 'notice 'active
4693 'CTCP-PING ?n nick
4694 ?t (erc-sec-to-time delta)))
4695 (range-error
4696 (erc-display-message
4697 nil 'error 'active
4698 'bad-ping-response ?n nick ?t time))))))
4700 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4701 (defun erc-ctcp-reply-TIME (_proc nick _login _host _to msg)
4702 "Handle a CTCP TIME reply."
4703 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4704 (let ((message (match-string 1 msg)))
4705 (erc-display-message
4706 nil 'notice 'active
4707 'CTCP-TIME ?n nick ?m message)))
4708 nil)
4710 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4711 (defun erc-ctcp-reply-VERSION (_proc nick _login _host _to msg)
4712 "Handle a CTCP VERSION reply."
4713 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4714 (let ((message (match-string 1 msg)))
4715 (erc-display-message
4716 nil 'notice 'active
4717 'CTCP-VERSION ?n nick ?m message)))
4718 nil)
4720 (defun erc-process-away (proc away-p)
4721 "Toggle the away status of the user depending on the value of AWAY-P.
4723 If nil, set the user as away.
4724 If non-nil, return from being away."
4725 (let ((sessionbuf (process-buffer proc)))
4726 (when sessionbuf
4727 (with-current-buffer sessionbuf
4728 (when erc-away-nickname
4729 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4730 erc-away-nickname away-p))
4731 (erc-cmd-NICK (if away-p
4732 erc-away-nickname
4733 erc-nick)))
4734 (cond
4735 (away-p
4736 (setq erc-away (current-time)))
4738 (let ((away-time erc-away))
4739 ;; away must be set to NIL BEFORE sending anything to prevent
4740 ;; an infinite recursion
4741 (setq erc-away nil)
4742 (with-current-buffer (erc-active-buffer)
4743 (when erc-public-away-p
4744 (erc-send-action
4745 (erc-default-target)
4746 (if away-time
4747 (format "is back (gone for %s)"
4748 (erc-sec-to-time
4749 (erc-time-diff
4750 (erc-emacs-time-to-erc-time away-time)
4751 (erc-current-time))))
4752 "is back")))))))))
4753 (erc-update-mode-line)))
4755 ;;;; List of channel members handling
4757 (defun erc-channel-begin-receiving-names ()
4758 "Internal function.
4760 Used when a channel names list is about to be received. Should
4761 be called with the current buffer set to the channel buffer.
4763 See also `erc-channel-end-receiving-names'."
4764 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4766 (defun erc-channel-end-receiving-names ()
4767 "Internal function.
4769 Used to fix `erc-channel-users' after a channel names list has been
4770 received. Should be called with the current buffer set to the
4771 channel buffer.
4773 See also `erc-channel-begin-receiving-names'."
4774 (maphash (lambda (nick _user)
4775 (if (null (gethash nick erc-channel-new-member-names))
4776 (erc-remove-channel-user nick)))
4777 erc-channel-users)
4778 (setq erc-channel-new-member-names nil))
4780 (defun erc-parse-prefix ()
4781 "Return an alist of valid prefix character types and their representations.
4782 Example: (operator) o => @, (voiced) v => +."
4783 (let ((str (or (cdr (assoc "PREFIX" (erc-with-server-buffer
4784 erc-server-parameters)))
4785 ;; provide a sane default
4786 "(qaohv)~&@%+"))
4787 types chars)
4788 (when (string-match "^(\\([^)]+\\))\\(.+\\)$" str)
4789 (setq types (match-string 1 str)
4790 chars (match-string 2 str))
4791 (let ((len (min (length types) (length chars)))
4792 (i 0)
4793 (alist nil))
4794 (while (< i len)
4795 (setq alist (cons (cons (elt types i) (elt chars i))
4796 alist))
4797 (setq i (1+ i)))
4798 alist))))
4800 (defun erc-channel-receive-names (names-string)
4801 "This function is for internal use only.
4803 Update `erc-channel-users' according to NAMES-STRING.
4804 NAMES-STRING is a string listing some of the names on the
4805 channel."
4806 (let* ((prefix (erc-parse-prefix))
4807 (voice-ch (cdr (assq ?v prefix)))
4808 (op-ch (cdr (assq ?o prefix)))
4809 (hop-ch (cdr (assq ?h prefix)))
4810 (adm-ch (cdr (assq ?a prefix)))
4811 (own-ch (cdr (assq ?q prefix)))
4812 (names (delete "" (split-string names-string)))
4813 name op voice halfop admin owner)
4814 (let ((erc-channel-members-changed-hook nil))
4815 (dolist (item names)
4816 (let ((updatep t)
4817 (ch (aref item 0)))
4818 (setq name item op 'off voice 'off halfop 'off admin 'off owner 'off)
4819 (if (rassq ch prefix)
4820 (if (= (length item) 1)
4821 (setq updatep nil)
4822 (setq name (substring item 1))
4823 (setf (pcase ch
4824 ((pred (eq voice-ch)) voice)
4825 ((pred (eq hop-ch)) halfop)
4826 ((pred (eq op-ch)) op)
4827 ((pred (eq adm-ch)) admin)
4828 ((pred (eq own-ch)) owner)
4829 (_ (message "Unknown prefix char `%S'" ch) voice))
4830 'on)))
4831 (when updatep
4832 ;; If we didn't issue the NAMES request (consider two clients
4833 ;; talking to an IRC proxy), `erc-channel-begin-receiving-names'
4834 ;; will not have been called, so we have to do it here.
4835 (unless erc-channel-new-member-names
4836 (erc-channel-begin-receiving-names))
4837 (puthash (erc-downcase name) t
4838 erc-channel-new-member-names)
4839 (erc-update-current-channel-member
4840 name name t voice halfop op admin owner)))))
4841 (run-hooks 'erc-channel-members-changed-hook)))
4844 (defcustom erc-channel-members-changed-hook nil
4845 "This hook is called every time the variable `channel-members' changes.
4846 The buffer where the change happened is current while this hook is called."
4847 :group 'erc-hooks
4848 :type 'hook)
4850 (defun erc-update-user-nick (nick &optional new-nick
4851 host login full-name info)
4852 "Update the stored user information for the user with nickname NICK.
4854 See also: `erc-update-user'."
4855 (erc-update-user (erc-get-server-user nick) new-nick
4856 host login full-name info))
4858 (defun erc-update-user (user &optional new-nick
4859 host login full-name info)
4860 "Update user info for USER. USER must be an erc-server-user
4861 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4862 non-nil and not equal to the existing values for USER are used to
4863 replace the stored values in USER.
4865 If, and only if, a change is made,
4866 `erc-channel-members-changed-hook' is run for each channel for
4867 which USER is a member, and t is returned."
4868 (let (changed)
4869 (when user
4870 (when (and new-nick
4871 (not (equal (erc-server-user-nickname user)
4872 new-nick)))
4873 (setq changed t)
4874 (erc-change-user-nickname user new-nick))
4875 (when (and host
4876 (not (equal (erc-server-user-host user) host)))
4877 (setq changed t)
4878 (setf (erc-server-user-host user) host))
4879 (when (and login
4880 (not (equal (erc-server-user-login user) login)))
4881 (setq changed t)
4882 (setf (erc-server-user-login user) login))
4883 (when (and full-name
4884 (not (equal (erc-server-user-full-name user)
4885 full-name)))
4886 (setq changed t)
4887 (setf (erc-server-user-full-name user) full-name))
4888 (when (and info
4889 (not (equal (erc-server-user-info user) info)))
4890 (setq changed t)
4891 (setf (erc-server-user-info user) info))
4892 (if changed
4893 (dolist (buf (erc-server-user-buffers user))
4894 (if (buffer-live-p buf)
4895 (with-current-buffer buf
4896 (run-hooks 'erc-channel-members-changed-hook))))))
4897 changed))
4899 (defun erc-update-current-channel-member
4900 (nick new-nick &optional add voice halfop op admin owner host login full-name info
4901 update-message-time)
4902 "Update the stored user information for the user with nickname NICK.
4903 `erc-update-user' is called to handle changes to nickname,
4904 HOST, LOGIN, FULL-NAME, and INFO. If VOICE HALFOP OP ADMIN or OWNER
4905 are non-nil, they must be equal to either `on' or `off', in which
4906 case the status of the user in the current channel is changed accordingly.
4907 If UPDATE-MESSAGE-TIME is non-nil, the last-message-time of the user
4908 in the current channel is set to (current-time).
4910 If ADD is non-nil, the user will be added with the specified
4911 information if it is not already present in the user or channel
4912 lists.
4914 If, and only if, changes are made, or the user is added,
4915 `erc-channel-members-updated-hook' is run, and t is returned.
4917 See also: `erc-update-user' and `erc-update-channel-member'."
4918 (let* (changed user-changed
4919 (channel-data (erc-get-channel-user nick))
4920 (cuser (cdr channel-data))
4921 (user (if channel-data (car channel-data)
4922 (erc-get-server-user nick))))
4923 (if cuser
4924 (progn
4925 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4926 (when (and voice
4927 (not (eq (erc-channel-user-voice cuser) voice)))
4928 (setq changed t)
4929 (setf (erc-channel-user-voice cuser)
4930 (cond ((eq voice 'on) t)
4931 ((eq voice 'off) nil)
4932 (t voice))))
4933 (when (and halfop
4934 (not (eq (erc-channel-user-halfop cuser) halfop)))
4935 (setq changed t)
4936 (setf (erc-channel-user-halfop cuser)
4937 (cond ((eq halfop 'on) t)
4938 ((eq halfop 'off) nil)
4939 (t halfop))))
4940 (when (and op
4941 (not (eq (erc-channel-user-op cuser) op)))
4942 (setq changed t)
4943 (setf (erc-channel-user-op cuser)
4944 (cond ((eq op 'on) t)
4945 ((eq op 'off) nil)
4946 (t op))))
4947 (when (and admin
4948 (not (eq (erc-channel-user-admin cuser) admin)))
4949 (setq changed t)
4950 (setf (erc-channel-user-admin cuser)
4951 (cond ((eq admin 'on) t)
4952 ((eq admin 'off) nil)
4953 (t admin))))
4954 (when (and owner
4955 (not (eq (erc-channel-user-owner cuser) owner)))
4956 (setq changed t)
4957 (setf (erc-channel-user-owner cuser)
4958 (cond ((eq owner 'on) t)
4959 ((eq owner 'off) nil)
4960 (t owner))))
4961 (when update-message-time
4962 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4963 (setq user-changed
4964 (erc-update-user user new-nick
4965 host login full-name info)))
4966 (when add
4967 (if (null user)
4968 (progn
4969 (setq user (make-erc-server-user
4970 :nickname nick
4971 :host host
4972 :full-name full-name
4973 :login login
4974 :info info
4975 :buffers (list (current-buffer))))
4976 (erc-add-server-user nick user))
4977 (setf (erc-server-user-buffers user)
4978 (cons (current-buffer)
4979 (erc-server-user-buffers user))))
4980 (setq cuser (make-erc-channel-user
4981 :voice (cond ((eq voice 'on) t)
4982 ((eq voice 'off) nil)
4983 (t voice))
4984 :halfop (cond ((eq halfop 'on) t)
4985 ((eq halfop 'off) nil)
4986 (t halfop))
4987 :op (cond ((eq op 'on) t)
4988 ((eq op 'off) nil)
4989 (t op))
4990 :admin (cond ((eq admin 'on) t)
4991 ((eq admin 'off) nil)
4992 (t admin))
4993 :owner (cond ((eq owner 'on) t)
4994 ((eq owner 'off) nil)
4995 (t owner))
4996 :last-message-time
4997 (if update-message-time (current-time))))
4998 (puthash (erc-downcase nick) (cons user cuser)
4999 erc-channel-users)
5000 (setq changed t)))
5001 (when (and changed (null user-changed))
5002 (run-hooks 'erc-channel-members-changed-hook))
5003 (or changed user-changed add)))
5005 (defun erc-update-channel-member (channel nick new-nick
5006 &optional add voice halfop op admin owner host login
5007 full-name info update-message-time)
5008 "Update user and channel information for the user with
5009 nickname NICK in channel CHANNEL.
5011 See also: `erc-update-current-channel-member'."
5012 (erc-with-buffer
5013 (channel)
5014 (erc-update-current-channel-member nick new-nick add voice halfop op admin owner host
5015 login full-name info
5016 update-message-time)))
5018 (defun erc-remove-current-channel-member (nick)
5019 "Remove NICK from current channel membership list.
5020 Runs `erc-channel-members-changed-hook'."
5021 (let ((channel-data (erc-get-channel-user nick)))
5022 (when channel-data
5023 (erc-remove-channel-user nick)
5024 (run-hooks 'erc-channel-members-changed-hook))))
5026 (defun erc-remove-channel-member (channel nick)
5027 "Remove NICK from CHANNEL's membership list.
5029 See also `erc-remove-current-channel-member'."
5030 (erc-with-buffer
5031 (channel)
5032 (erc-remove-current-channel-member nick)))
5034 (defun erc-update-channel-topic (channel topic &optional modify)
5035 "Find a buffer for CHANNEL and set the TOPIC for it.
5037 If optional MODIFY is `append' or `prepend', then append or prepend the
5038 TOPIC string to the current topic."
5039 (erc-with-buffer (channel)
5040 (cond ((eq modify 'append)
5041 (setq erc-channel-topic (concat erc-channel-topic topic)))
5042 ((eq modify 'prepend)
5043 (setq erc-channel-topic (concat topic erc-channel-topic)))
5044 (t (setq erc-channel-topic topic)))
5045 (erc-update-mode-line-buffer (current-buffer))))
5047 (defun erc-set-modes (tgt mode-string)
5048 "Set the modes for the TGT provided as MODE-STRING."
5049 (let* ((modes (erc-parse-modes mode-string))
5050 (add-modes (nth 0 modes))
5051 ;; list of triples: (mode-char 'on/'off argument)
5052 (arg-modes (nth 2 modes)))
5053 (cond ((erc-channel-p tgt); channel modes
5054 (let ((buf (and erc-server-process
5055 (erc-get-buffer tgt erc-server-process))))
5056 (when buf
5057 (with-current-buffer buf
5058 (setq erc-channel-modes add-modes)
5059 (setq erc-channel-user-limit nil)
5060 (setq erc-channel-key nil)
5061 (while arg-modes
5062 (let ((mode (nth 0 (car arg-modes)))
5063 (onoff (nth 1 (car arg-modes)))
5064 (arg (nth 2 (car arg-modes))))
5065 (cond ((string-match "^[Ll]" mode)
5066 (erc-update-channel-limit tgt onoff arg))
5067 ((string-match "^[Kk]" mode)
5068 (erc-update-channel-key tgt onoff arg))
5069 (t nil)))
5070 (setq arg-modes (cdr arg-modes)))
5071 (erc-update-mode-line-buffer buf)))))
5072 ;; we do not keep our nick's modes yet
5073 ;;(t (setq erc-user-modes add-modes))
5077 (defun erc-sort-strings (list-of-strings)
5078 "Sort LIST-OF-STRINGS in lexicographic order.
5080 Side-effect free."
5081 (sort (copy-sequence list-of-strings) 'string<))
5083 (defun erc-parse-modes (mode-string)
5084 "Parse MODE-STRING into a list.
5086 Returns a list of three elements:
5088 (ADD-MODES REMOVE-MODES ARG-MODES).
5090 The add-modes and remove-modes are lists of single-character strings
5091 for modes without parameters to add and remove respectively. The
5092 arg-modes is a list of triples of the form:
5094 (MODE-CHAR ON/OFF ARGUMENT)."
5095 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
5096 (let ((chars (mapcar #'char-to-string (match-string 1 mode-string)))
5097 ;; arguments in channel modes
5098 (args-str (match-string 2 mode-string))
5099 (args nil)
5100 (add-modes nil)
5101 (remove-modes nil)
5102 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
5103 (add-p t))
5104 ;; make the argument list
5105 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
5106 (setq args (cons (match-string 1 args-str) args))
5107 (setq args-str (match-string 2 args-str)))
5108 (setq args (nreverse args))
5109 ;; collect what modes changed, and match them with arguments
5110 (while chars
5111 (cond ((string= (car chars) "+") (setq add-p t))
5112 ((string= (car chars) "-") (setq add-p nil))
5113 ((string-match "^[qaovhbQAOVHB]" (car chars))
5114 (setq arg-modes (cons (list (car chars)
5115 (if add-p 'on 'off)
5116 (if args (car args) nil))
5117 arg-modes))
5118 (if args (setq args (cdr args))))
5119 ((string-match "^[LlKk]" (car chars))
5120 (setq arg-modes (cons (list (car chars)
5121 (if add-p 'on 'off)
5122 (if (and add-p args)
5123 (car args) nil))
5124 arg-modes))
5125 (if (and add-p args) (setq args (cdr args))))
5126 (add-p (setq add-modes (cons (car chars) add-modes)))
5127 (t (setq remove-modes (cons (car chars) remove-modes))))
5128 (setq chars (cdr chars)))
5129 (setq add-modes (nreverse add-modes))
5130 (setq remove-modes (nreverse remove-modes))
5131 (setq arg-modes (nreverse arg-modes))
5132 (list add-modes remove-modes arg-modes))
5133 nil))
5135 (defun erc-update-modes (tgt mode-string &optional _nick _host _login)
5136 "Update the mode information for TGT, provided as MODE-STRING.
5137 Optional arguments: NICK, HOST and LOGIN - the attributes of the
5138 person who changed the modes."
5139 ;; FIXME: neither of nick, host, and login are used!
5140 (let* ((modes (erc-parse-modes mode-string))
5141 (add-modes (nth 0 modes))
5142 (remove-modes (nth 1 modes))
5143 ;; list of triples: (mode-char 'on/'off argument)
5144 (arg-modes (nth 2 modes)))
5145 ;; now parse the modes changes and do the updates
5146 (cond ((erc-channel-p tgt); channel modes
5147 (let ((buf (and erc-server-process
5148 (erc-get-buffer tgt erc-server-process))))
5149 (when buf
5150 ;; FIXME! This used to have an original buffer
5151 ;; variable, but it never switched back to the original
5152 ;; buffer. Is this wanted behavior?
5153 (set-buffer buf)
5154 (if (not (boundp 'erc-channel-modes))
5155 (setq erc-channel-modes nil))
5156 (while remove-modes
5157 (setq erc-channel-modes (delete (car remove-modes)
5158 erc-channel-modes)
5159 remove-modes (cdr remove-modes)))
5160 (while add-modes
5161 (setq erc-channel-modes (cons (car add-modes)
5162 erc-channel-modes)
5163 add-modes (cdr add-modes)))
5164 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
5165 (while arg-modes
5166 (let ((mode (nth 0 (car arg-modes)))
5167 (onoff (nth 1 (car arg-modes)))
5168 (arg (nth 2 (car arg-modes))))
5169 (cond ((string-match "^[Vv]" mode)
5170 (erc-update-channel-member tgt arg arg nil onoff))
5171 ((string-match "^[hH]" mode)
5172 (erc-update-channel-member tgt arg arg nil nil onoff))
5173 ((string-match "^[oO]" mode)
5174 (erc-update-channel-member tgt arg arg nil nil nil onoff))
5175 ((string-match "^[aA]" mode)
5176 (erc-update-channel-member tgt arg arg nil nil nil nil onoff))
5177 ((string-match "^[qQ]" mode)
5178 (erc-update-channel-member tgt arg arg nil nil nil nil nil onoff))
5179 ((string-match "^[Ll]" mode)
5180 (erc-update-channel-limit tgt onoff arg))
5181 ((string-match "^[Kk]" mode)
5182 (erc-update-channel-key tgt onoff arg))
5183 (t nil)); only ops are tracked now
5184 (setq arg-modes (cdr arg-modes))))
5185 (erc-update-mode-line buf))))
5186 ;; nick modes - ignored at this point
5187 (t nil))))
5189 (defun erc-update-channel-limit (channel onoff n)
5190 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
5191 "Update CHANNEL's user limit to N."
5192 (if (or (not (eq onoff 'on))
5193 (and (stringp n) (string-match "^[0-9]+$" n)))
5194 (erc-with-buffer
5195 (channel)
5196 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
5197 (t (setq erc-channel-user-limit nil))))))
5199 (defun erc-update-channel-key (channel onoff key)
5200 "Update CHANNEL's key to KEY if ONOFF is `on' or to nil if it's `off'."
5201 (erc-with-buffer
5202 (channel)
5203 (cond ((eq onoff 'on) (setq erc-channel-key key))
5204 (t (setq erc-channel-key nil)))))
5206 (defun erc-handle-user-status-change (type nlh &optional l)
5207 "Handle changes in any user's status.
5209 So far, only nick change is handled.
5211 Generally, the TYPE argument is a symbol describing the change type, NLH is
5212 a list containing the original nickname, login name and hostname for the user,
5213 and L is a list containing additional TYPE-specific arguments.
5215 So far the following TYPE/L pairs are supported:
5217 Event TYPE L
5219 nickname change `nick' (NEW-NICK)"
5220 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
5221 (cond
5222 ;; nickname change
5223 ((equal type 'nick)
5226 nil)))
5228 (defun erc-highlight-notice (s)
5229 "Highlight notice message S and return it.
5230 See also variable `erc-notice-highlight-type'."
5231 (cond
5232 ((eq erc-notice-highlight-type 'prefix)
5233 (erc-put-text-property 0 (length erc-notice-prefix)
5234 'face 'erc-notice-face s)
5236 ((eq erc-notice-highlight-type 'all)
5237 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
5239 (t s)))
5241 (defun erc-make-notice (message)
5242 "Notify the user of MESSAGE."
5243 (when erc-minibuffer-notice
5244 (message "%s" message))
5245 (erc-highlight-notice (concat erc-notice-prefix message)))
5247 (defun erc-highlight-error (s)
5248 "Highlight error message S and return it."
5249 (erc-put-text-property 0 (length s) 'face 'erc-error-face s)
5252 (defun erc-put-text-property (start end property value &optional object)
5253 "Set text-property for an object (usually a string).
5254 START and END define the characters covered.
5255 PROPERTY is the text-property set, usually the symbol `face'.
5256 VALUE is the value for the text-property, usually a face symbol such as
5257 the face `bold' or `erc-pal-face'.
5258 OBJECT is a string which will be modified and returned.
5259 OBJECT is modified without being copied first.
5261 You can redefine or `defadvice' this function in order to add
5262 EmacsSpeak support."
5263 (put-text-property start end property value object))
5265 (defun erc-list (thing)
5266 "Return THING if THING is a list, or a list with THING as its element."
5267 (if (listp thing)
5268 thing
5269 (list thing)))
5271 (defun erc-parse-user (string)
5272 "Parse STRING as a user specification (nick!login@host).
5274 Return a list of the three separate tokens."
5275 (cond
5276 ((string-match "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$" string)
5277 (list (match-string 1 string)
5278 (match-string 2 string)
5279 (match-string 3 string)))
5280 ;; Some bogus bouncers send Nick!(null), try to live with that.
5281 ((string-match "^\\([^!\n]*\\)!\\(.*\\)$" string)
5282 (list (match-string 1 string)
5284 (match-string 2 string)))
5286 (list string "" ""))))
5288 (defun erc-extract-nick (string)
5289 "Return the nick corresponding to a user specification STRING.
5291 See also `erc-parse-user'."
5292 (car (erc-parse-user string)))
5294 (defun erc-put-text-properties (start end properties
5295 &optional object value-list)
5296 "Set text-properties for OBJECT.
5298 START and END describe positions in OBJECT.
5299 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
5300 each property to the corresponding value in VALUE-LIST."
5301 (unless value-list
5302 (setq value-list (mapcar (lambda (_x) t)
5303 properties)))
5304 (while (and properties value-list)
5305 (erc-put-text-property
5306 start end (pop properties) (pop value-list) object)))
5308 ;;; Input area handling:
5310 (defun erc-beg-of-input-line ()
5311 "Return the value of `point' at the beginning of the input line.
5313 Specifically, return the position of `erc-insert-marker'."
5314 (or (and (boundp 'erc-insert-marker)
5315 (markerp erc-insert-marker))
5316 (error "erc-insert-marker has no value, please report a bug"))
5317 (marker-position erc-insert-marker))
5319 (defun erc-end-of-input-line ()
5320 "Return the value of `point' at the end of the input line."
5321 (point-max))
5323 (defvar erc-last-input-time 0
5324 "Time of last call to `erc-send-current-line'.
5325 If that function has never been called, the value is 0.")
5327 (defcustom erc-accidental-paste-threshold-seconds nil
5328 "Minimum time, in seconds, before sending new lines via IRC.
5329 If the value is a number, `erc-send-current-line' signals an error
5330 if its previous invocation was fewer than this many seconds ago.
5331 This is useful so that if you accidentally enter large amounts of text
5332 into the ERC buffer, that text is not sent to the IRC server.
5334 If the value is nil, `erc-send-current-line' always considers any
5335 submitted line to be intentional."
5336 :group 'erc
5337 :version "24.4"
5338 :type '(choice number (other :tag "disabled" nil)))
5340 (defun erc-send-current-line ()
5341 "Parse current line and send it to IRC."
5342 (interactive)
5343 (let ((now (float-time)))
5344 (if (or (not erc-accidental-paste-threshold-seconds)
5345 (< erc-accidental-paste-threshold-seconds
5346 (- now erc-last-input-time)))
5347 (save-restriction
5348 (widen)
5349 (if (< (point) (erc-beg-of-input-line))
5350 (erc-error "Point is not in the input area")
5351 (let ((inhibit-read-only t)
5352 (str (erc-user-input))
5353 (old-buf (current-buffer)))
5354 (if (and (not (erc-server-buffer-live-p))
5355 (not (erc-command-no-process-p str)))
5356 (erc-error "ERC: No process running")
5357 (erc-set-active-buffer (current-buffer))
5358 ;; Kill the input and the prompt
5359 (delete-region (erc-beg-of-input-line)
5360 (erc-end-of-input-line))
5361 (unwind-protect
5362 (erc-send-input str)
5363 ;; Fix the buffer if the command didn't kill it
5364 (when (buffer-live-p old-buf)
5365 (with-current-buffer old-buf
5366 (save-restriction
5367 (widen)
5368 (goto-char (point-max))
5369 (when (processp erc-server-process)
5370 (set-marker (process-mark erc-server-process) (point)))
5371 (set-marker erc-insert-marker (point))
5372 (let ((buffer-modified (buffer-modified-p)))
5373 (erc-display-prompt)
5374 (set-buffer-modified-p buffer-modified))))))
5376 ;; Only when last hook has been run...
5377 (run-hook-with-args 'erc-send-completed-hook str))))
5378 (setq erc-last-input-time now))
5379 (switch-to-buffer "*ERC Accidental Paste Overflow*")
5380 (lwarn 'erc :warning
5381 "You seem to have accidentally pasted some text!"))))
5383 (defun erc-user-input ()
5384 "Return the input of the user in the current buffer."
5385 (buffer-substring-no-properties
5386 erc-input-marker
5387 (erc-end-of-input-line)))
5389 (defvar erc-command-regexp "^/\\([A-Za-z']+\\)\\(\\s-+.*\\|\\s-*\\)$"
5390 "Regular expression used for matching commands in ERC.")
5392 (defun erc-send-input (input)
5393 "Treat INPUT as typed in by the user. It is assumed that the input
5394 and the prompt is already deleted.
5395 This returns non-nil only if we actually send anything."
5396 ;; Handle different kinds of inputs
5397 (cond
5398 ;; Ignore empty input
5399 ((if erc-send-whitespace-lines
5400 (string= input "")
5401 (string-match "\\`[ \t\r\f\n]*\\'" input))
5402 (when erc-warn-about-blank-lines
5403 (message "Blank line - ignoring...")
5404 (beep))
5405 nil)
5407 (defvar str) ;; FIXME: Make it obey the "erc-" prefix convention.
5408 (let ((str input)
5409 (erc-insert-this t))
5410 (setq erc-send-this t)
5411 (run-hook-with-args 'erc-send-pre-hook input)
5412 (when erc-send-this
5413 (if (or (string-match "\n" str)
5414 (not (string-match erc-command-regexp str)))
5415 (mapc
5416 (lambda (line)
5417 (mapc
5418 (lambda (line)
5419 ;; Insert what has to be inserted for this.
5420 (erc-display-msg line)
5421 (erc-process-input-line (concat line "\n")
5422 (null erc-flood-protect) t))
5423 (or (and erc-flood-protect (erc-split-line line))
5424 (list line))))
5425 (split-string str "\n"))
5426 (erc-process-input-line (concat str "\n") t nil))
5427 t)))))
5429 (defun erc-display-command (line)
5430 (when erc-insert-this
5431 (let ((insert-position (point)))
5432 (unless erc-hide-prompt
5433 (erc-display-prompt nil nil (erc-command-indicator)
5434 (and (erc-command-indicator)
5435 'erc-command-indicator-face)))
5436 (let ((beg (point)))
5437 (insert line)
5438 (erc-put-text-property beg (point)
5439 'face 'erc-command-indicator-face)
5440 (insert "\n"))
5441 (when (processp erc-server-process)
5442 (set-marker (process-mark erc-server-process) (point)))
5443 (set-marker erc-insert-marker (point))
5444 (save-excursion
5445 (save-restriction
5446 (narrow-to-region insert-position (point))
5447 (run-hooks 'erc-send-modify-hook)
5448 (run-hooks 'erc-send-post-hook))))))
5450 (defun erc-display-msg (line)
5451 "Display LINE as a message of the user to the current target at the
5452 current position."
5453 (when erc-insert-this
5454 (let ((insert-position (point)))
5455 (insert (erc-format-my-nick))
5456 (let ((beg (point)))
5457 (insert line)
5458 (erc-put-text-property beg (point)
5459 'face 'erc-input-face))
5460 (insert "\n")
5461 (when (processp erc-server-process)
5462 (set-marker (process-mark erc-server-process) (point)))
5463 (set-marker erc-insert-marker (point))
5464 (save-excursion
5465 (save-restriction
5466 (narrow-to-region insert-position (point))
5467 (run-hooks 'erc-send-modify-hook)
5468 (run-hooks 'erc-send-post-hook))))))
5470 (defun erc-command-symbol (command)
5471 "Return the ERC command symbol for COMMAND if it exists and is bound."
5472 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
5473 (when (fboundp cmd) cmd)))
5475 (defun erc-extract-command-from-line (line)
5476 "Extract command and args from the input LINE.
5477 If no command was given, return nil. If command matches, return a
5478 list of the form: (command args) where both elements are strings."
5479 (when (string-match erc-command-regexp line)
5480 (let* ((cmd (erc-command-symbol (match-string 1 line)))
5481 ;; note: return is nil, we apply this simply for side effects
5482 (_canon-defun (while (and cmd (symbolp (symbol-function cmd)))
5483 (setq cmd (symbol-function cmd))))
5484 (cmd-fun (or cmd #'erc-cmd-default))
5485 (arg (if cmd
5486 (if (get cmd-fun 'do-not-parse-args)
5487 (format "%s" (match-string 2 line))
5488 (delete "" (split-string (erc-trim-string
5489 (match-string 2 line)) " ")))
5490 line)))
5491 (list cmd-fun arg))))
5493 (defun erc-split-multiline-safe (string)
5494 "Split STRING, containing multiple lines and return them in a list.
5495 Do it only for STRING as the complete input, do not carry unfinished
5496 strings over to the next call."
5497 (let ((l ())
5498 (i0 0)
5499 (doit t))
5500 (while doit
5501 (let ((i (string-match "\r?\n" string i0))
5502 (s (substring string i0)))
5503 (cond (i (setq l (cons (substring string i0 i) l))
5504 (setq i0 (match-end 0)))
5505 ((> (length s) 0)
5506 (setq l (cons s l))(setq doit nil))
5507 (t (setq doit nil)))))
5508 (nreverse l)))
5510 ;; nick handling
5512 (defun erc-set-current-nick (nick)
5513 "Set the current nickname to NICK."
5514 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5515 (erc-server-buffer)
5516 (current-buffer))
5517 (setq erc-server-current-nick nick)))
5519 (defun erc-current-nick ()
5520 "Return the current nickname."
5521 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5522 (erc-server-buffer)
5523 (current-buffer))
5524 erc-server-current-nick))
5526 (defun erc-current-nick-p (nick)
5527 "Return non-nil if NICK is the current nickname."
5528 (erc-nick-equal-p nick (erc-current-nick)))
5530 (defun erc-nick-equal-p (nick1 nick2)
5531 "Return non-nil if NICK1 and NICK2 are the same.
5533 This matches strings according to the IRC protocol's case convention.
5535 See also `erc-downcase'."
5536 (string= (erc-downcase nick1)
5537 (erc-downcase nick2)))
5539 ;; default target handling
5541 (defun erc-default-target ()
5542 "Return the current default target (as a character string) or nil if none."
5543 (let ((tgt (car erc-default-recipients)))
5544 (cond
5545 ((not tgt) nil)
5546 ((listp tgt) (cdr tgt))
5547 (t tgt))))
5549 (defun erc-add-default-channel (channel)
5550 "Add CHANNEL to the default channel list."
5551 (let ((chl (downcase channel)))
5552 (setq erc-default-recipients
5553 (cons chl erc-default-recipients))))
5555 (defun erc-delete-default-channel (channel &optional buffer)
5556 "Delete CHANNEL from the default channel list."
5557 (with-current-buffer (if (and buffer
5558 (bufferp buffer))
5559 buffer
5560 (current-buffer))
5561 (setq erc-default-recipients (delete (downcase channel)
5562 erc-default-recipients))))
5564 (defun erc-add-query (nickname)
5565 "Add QUERY'd NICKNAME to the default channel list.
5567 The previous default target of QUERY type gets removed."
5568 (let ((d1 (car erc-default-recipients))
5569 (d2 (cdr erc-default-recipients))
5570 (qt (cons 'QUERY (downcase nickname))))
5571 (setq erc-default-recipients (cons qt (if (and (listp d1)
5572 (eq (car d1) 'QUERY))
5574 erc-default-recipients)))))
5576 (defun erc-delete-query ()
5577 "Delete the topmost target if it is a QUERY."
5579 (let ((d1 (car erc-default-recipients))
5580 (d2 (cdr erc-default-recipients)))
5581 (if (and (listp d1)
5582 (eq (car d1) 'QUERY))
5583 (setq erc-default-recipients d2)
5584 (error "Current target is not a QUERY"))))
5586 (defun erc-ignored-user-p (spec)
5587 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5589 Takes a full SPEC of a user in the form \"nick!login@host\", and
5590 matches against all the regexp's in `erc-ignore-list'. If any
5591 match, returns that regexp."
5592 (catch 'found
5593 (dolist (ignored (erc-with-server-buffer erc-ignore-list))
5594 (if (string-match ignored spec)
5595 (throw 'found ignored)))))
5597 (defun erc-ignored-reply-p (msg tgt proc)
5598 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5599 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5601 Takes a message MSG to a channel and returns non-nil if the addressed
5602 user matches any regexp in `erc-ignore-reply-list'."
5603 (let ((target-nick (erc-message-target msg)))
5604 (if (not target-nick)
5606 (erc-with-buffer (tgt proc)
5607 (let ((user (erc-get-server-user target-nick)))
5608 (when user
5609 (erc-list-match erc-ignore-reply-list
5610 (erc-user-spec user))))))))
5612 (defun erc-message-target (msg)
5613 "Return the addressed target in MSG.
5615 The addressed target is the string before the first colon in MSG."
5616 (if (string-match "^\\([^: \n]*\\):" msg)
5617 (match-string 1 msg)
5618 nil))
5620 (defun erc-user-spec (user)
5621 "Create a nick!user@host spec from a user struct."
5622 (let ((nick (erc-server-user-nickname user))
5623 (host (erc-server-user-host user))
5624 (login (erc-server-user-login user)))
5625 (concat (or nick "")
5627 (or login "")
5629 (or host ""))))
5631 (defun erc-list-match (lst str)
5632 "Return non-nil if any regexp in LST matches STR."
5633 (memq nil (mapcar (lambda (regexp)
5634 (not (string-match regexp str)))
5635 lst)))
5637 ;; other "toggles"
5639 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5640 "Toggle automatic CTCP replies (like VERSION and PING).
5642 If ARG is positive, turns CTCP replies on.
5644 If ARG is non-nil and not positive, turns CTCP replies off."
5645 (interactive "P")
5646 (cond ((and (numberp arg) (> arg 0))
5647 (setq erc-disable-ctcp-replies t))
5648 (arg (setq erc-disable-ctcp-replies nil))
5649 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5650 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5652 (defun erc-toggle-flood-control (&optional arg)
5653 "Toggle use of flood control on sent messages.
5655 If ARG is positive, use flood control.
5656 If ARG is non-nil and not positive, do not use flood control.
5658 See `erc-server-flood-margin' for an explanation of the available
5659 flood control parameters."
5660 (interactive "P")
5661 (cond ((and (numberp arg) (> arg 0))
5662 (setq erc-flood-protect t))
5663 (arg (setq erc-flood-protect nil))
5664 (t (setq erc-flood-protect (not erc-flood-protect))))
5665 (message "ERC flood control is %s"
5666 (cond (erc-flood-protect "ON")
5667 (t "OFF"))))
5669 ;; Some useful channel and nick commands for fast key bindings
5671 (defun erc-invite-only-mode (&optional arg)
5672 "Turn on the invite only mode (+i) for the current channel.
5674 If ARG is non-nil, turn this mode off (-i).
5676 This command is sent even if excess flood is detected."
5677 (interactive "P")
5678 (erc-set-active-buffer (current-buffer))
5679 (let ((tgt (erc-default-target)))
5680 (if (or (not tgt) (not (erc-channel-p tgt)))
5681 (erc-display-message nil 'error (current-buffer) 'no-target)
5682 (erc-load-irc-script-lines
5683 (list (concat "/mode " tgt (if arg " -i" " +i")))
5684 t))))
5686 (defun erc-get-channel-mode-from-keypress (key)
5687 "Read a key sequence and call the corresponding channel mode function.
5688 After doing C-c C-o, type in a channel mode letter.
5690 C-g means quit.
5691 RET lets you type more than one mode at a time.
5692 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5693 If \"k\" is pressed, `erc-set-channel-key' gets called.
5694 Anything else will be sent to `erc-toggle-channel-mode'."
5695 (interactive "kChannel mode (RET to set more than one): ")
5696 (when (featurep 'xemacs)
5697 (setq key (char-to-string (event-to-character (aref key 0)))))
5698 (cond ((equal key "\C-g")
5699 (keyboard-quit))
5700 ((equal key "\C-m")
5701 (erc-insert-mode-command))
5702 ((equal key "l")
5703 (call-interactively 'erc-set-channel-limit))
5704 ((equal key "k")
5705 (call-interactively 'erc-set-channel-key))
5706 (t (erc-toggle-channel-mode key))))
5708 (defun erc-toggle-channel-mode (mode &optional channel)
5709 "Toggle channel MODE.
5711 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5712 `erc-default-target'."
5713 (interactive "P")
5714 (erc-set-active-buffer (current-buffer))
5715 (let ((tgt (or channel (erc-default-target))))
5716 (if (or (null tgt) (null (erc-channel-p tgt)))
5717 (erc-display-message nil 'error 'active 'no-target)
5718 (let* ((active (member mode erc-channel-modes))
5719 (newstate (if active "OFF" "ON")))
5720 (erc-log (format "%s: Toggle mode %s %s" tgt mode newstate))
5721 (message "Toggle channel mode %s %s" mode newstate)
5722 (erc-server-send (format "MODE %s %s%s"
5723 tgt (if active "-" "+") mode))))))
5725 (defun erc-insert-mode-command ()
5726 "Insert the line \"/mode <current target> \" at `point'."
5727 (interactive)
5728 (let ((tgt (erc-default-target)))
5729 (if tgt (insert (concat "/mode " tgt " "))
5730 (erc-display-message nil 'error (current-buffer) 'no-target))))
5732 (defun erc-channel-names ()
5733 "Run \"/names #channel\" in the current channel."
5734 (interactive)
5735 (erc-set-active-buffer (current-buffer))
5736 (let ((tgt (erc-default-target)))
5737 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5738 (erc-display-message nil 'error (current-buffer) 'no-target))))
5740 (defun erc-remove-text-properties-region (start end &optional object)
5741 "Clears the region (START,END) in OBJECT from all colors, etc."
5742 (interactive "r")
5743 (save-excursion
5744 (let ((inhibit-read-only t))
5745 (set-text-properties start end nil object))))
5746 (put 'erc-remove-text-properties-region 'disabled t)
5748 ;; script execution and startup
5750 (defun erc-find-file (file &optional path)
5751 "Search for a FILE in the filesystem.
5752 First the `default-directory' is searched for FILE, then any directories
5753 specified in the list PATH.
5755 If FILE is found, return the path to it."
5756 (let ((filepath file))
5757 (if (file-readable-p filepath) filepath
5758 (while (and path
5759 (progn (setq filepath (expand-file-name file (car path)))
5760 (not (file-readable-p filepath))))
5761 (setq path (cdr path)))
5762 (if path filepath nil))))
5764 (defun erc-select-startup-file ()
5765 "Select an ERC startup file.
5766 See also `erc-startup-file-list'."
5767 (catch 'found
5768 (dolist (f erc-startup-file-list)
5769 (setq f (convert-standard-filename f))
5770 (when (file-readable-p f)
5771 (throw 'found f)))))
5773 (defun erc-find-script-file (file)
5774 "Search for FILE in `default-directory', and any in `erc-script-path'."
5775 (erc-find-file file erc-script-path))
5777 (defun erc-load-script (file)
5778 "Load a script from FILE.
5780 FILE must be the full name, it is not searched in the
5781 `erc-script-path'. If the filename ends with `.el', then load it
5782 as an Emacs Lisp program. Otherwise, treat it as a regular IRC
5783 script."
5784 (erc-log (concat "erc-load-script: " file))
5785 (cond
5786 ((string-match "\\.el\\'" file)
5787 (load file))
5789 (erc-load-irc-script file))))
5791 (defun erc-process-script-line (line &optional args)
5792 "Process an IRC script LINE.
5794 Does script-specific substitutions (script arguments, current nick,
5795 server, etc.) in LINE and returns it.
5797 Substitutions are: %C and %c = current target (channel or nick),
5798 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5799 where x is any other character;
5800 $* = the entire argument string, $1 = the first argument, $2 = the second,
5801 and so on."
5802 (if (not args) (setq args ""))
5803 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5804 (percent-regexp "\\(%.\\)")
5805 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5806 (tgt (erc-default-target))
5807 (server (and (boundp 'erc-session-server) erc-session-server))
5808 (nick (erc-current-nick))
5809 (res "")
5810 (tmp nil)
5811 (arg-list nil)
5812 (arg-num 0))
5813 (if (not tgt) (setq tgt ""))
5814 (if (not server) (setq server ""))
5815 (if (not nick) (setq nick ""))
5816 ;; First, compute the argument list
5817 (setq tmp args)
5818 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5819 (setq arg-list (cons (match-string 1 tmp) arg-list))
5820 (setq tmp (match-string 2 tmp)))
5821 (setq arg-list (nreverse arg-list))
5822 (setq arg-num (length arg-list))
5823 ;; now do the substitution
5824 (setq tmp (string-match esc-regexp line))
5825 (while tmp
5826 ;;(message "beginning of while: tmp=%S" tmp)
5827 (let* ((hd (substring line 0 tmp))
5828 (esc "")
5829 (subst "")
5830 (tail (substring line tmp)))
5831 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5832 (setq esc (match-string 1 tail))
5833 (setq tail (substring tail (match-end 1))))
5834 ((string-match (concat "^" percent-regexp) tail)
5835 (setq esc (match-string 1 tail))
5836 (setq tail (substring tail (match-end 1)))))
5837 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5838 (setq res (concat res hd))
5839 (setq subst
5840 (cond ((string= esc "") "")
5841 ((string-match "^\\$\\*$" esc) args)
5842 ((string-match "^\\$\\([0-9]+\\)$" esc)
5843 (let ((n (string-to-number (match-string 1 esc))))
5844 (message "n = %S, integerp(n)=%S" n (integerp n))
5845 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5846 ((string-match "^%[Cc]$" esc) tgt)
5847 ((string-match "^%[Ss]$" esc) server)
5848 ((string-match "^%[Nn]$" esc) nick)
5849 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5850 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5851 (message "BUG IN ERC: esc=%S" esc)
5852 "")))
5853 (setq line tail)
5854 (setq tmp (string-match esc-regexp line))
5855 (setq res (concat res subst))
5856 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5858 (setq res (concat res line))
5859 res))
5861 (defun erc-load-irc-script (file &optional force)
5862 "Load an IRC script from FILE."
5863 (erc-log (concat "erc-load-script: " file))
5864 (let ((str (with-temp-buffer
5865 (insert-file-contents file)
5866 (buffer-string))))
5867 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5869 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5870 "Load IRC script LINES (a list of strings).
5872 If optional NOEXPAND is non-nil, do not expand script-specific
5873 sequences, process the lines verbatim. Use this for multiline
5874 user input."
5875 (let* ((cb (current-buffer))
5876 (s "")
5877 (sp (or (erc-command-indicator) (erc-prompt)))
5878 (args (and (boundp 'erc-script-args) erc-script-args)))
5879 (if (and args (string-match "^ " args))
5880 (setq args (substring args 1)))
5881 ;; prepare the prompt string for echo
5882 (erc-put-text-property 0 (length sp)
5883 'face 'erc-command-indicator-face sp)
5884 (while lines
5885 (setq s (car lines))
5886 (erc-log (concat "erc-load-script: CMD: " s))
5887 (unless (string-match "^\\s-*$" s)
5888 (let ((line (if noexpand s (erc-process-script-line s args))))
5889 (if (and (erc-process-input-line line force)
5890 erc-script-echo)
5891 (progn
5892 (erc-put-text-property 0 (length line)
5893 'face 'erc-input-face line)
5894 (erc-display-line (concat sp line) cb)))))
5895 (setq lines (cdr lines)))))
5897 ;; authentication
5899 (defun erc-login ()
5900 "Perform user authentication at the IRC server."
5901 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5902 (erc-current-nick)
5903 (user-login-name)
5904 (or erc-system-name (system-name))
5905 erc-session-server
5906 erc-session-user-full-name))
5907 (if erc-session-password
5908 (erc-server-send (format "PASS %s" erc-session-password))
5909 (message "Logging in without password"))
5910 (erc-server-send (format "NICK %s" (erc-current-nick)))
5911 (erc-server-send
5912 (format "USER %s %s %s :%s"
5913 ;; hacked - S.B.
5914 (if erc-anonymous-login erc-email-userid (user-login-name))
5915 "0" "*"
5916 erc-session-user-full-name))
5917 (erc-update-mode-line))
5919 ;; connection properties' heuristics
5921 (defun erc-determine-parameters (&optional server port nick name)
5922 "Determine the connection and authentication parameters.
5923 Sets the buffer local variables:
5925 - `erc-session-connector'
5926 - `erc-session-server'
5927 - `erc-session-port'
5928 - `erc-session-full-name'
5929 - `erc-server-current-nick'"
5930 (setq erc-session-connector erc-server-connect-function
5931 erc-session-server (erc-compute-server server)
5932 erc-session-port (or port erc-default-port)
5933 erc-session-user-full-name (erc-compute-full-name name))
5934 (erc-set-current-nick (erc-compute-nick nick)))
5936 (defun erc-compute-server (&optional server)
5937 "Return an IRC server name.
5939 This tries a number of increasingly more default methods until a
5940 non-nil value is found.
5942 - SERVER (the argument passed to this function)
5943 - The `erc-server' option
5944 - The value of the IRCSERVER environment variable
5945 - The `erc-default-server' variable"
5946 (or server
5947 erc-server
5948 (getenv "IRCSERVER")
5949 erc-default-server))
5951 (defun erc-compute-nick (&optional nick)
5952 "Return user's IRC nick.
5954 This tries a number of increasingly more default methods until a
5955 non-nil value is found.
5957 - NICK (the argument passed to this function)
5958 - The `erc-nick' option
5959 - The value of the IRCNICK environment variable
5960 - The result from the `user-login-name' function"
5961 (or nick
5962 (if (consp erc-nick) (car erc-nick) erc-nick)
5963 (getenv "IRCNICK")
5964 (user-login-name)))
5967 (defun erc-compute-full-name (&optional full-name)
5968 "Return user's full name.
5970 This tries a number of increasingly more default methods until a
5971 non-nil value is found.
5973 - FULL-NAME (the argument passed to this function)
5974 - The `erc-user-full-name' option
5975 - The value of the IRCNAME environment variable
5976 - The result from the `user-full-name' function"
5977 (or full-name
5978 erc-user-full-name
5979 (getenv "IRCNAME")
5980 (if erc-anonymous-login "unknown" nil)
5981 (user-full-name)))
5983 (defun erc-compute-port (&optional port)
5984 "Return a port for an IRC server.
5986 This tries a number of increasingly more default methods until a
5987 non-nil value is found.
5989 - PORT (the argument passed to this function)
5990 - The `erc-port' option
5991 - The `erc-default-port' variable"
5992 (or port erc-port erc-default-port))
5994 ;; time routines
5996 (defun erc-string-to-emacs-time (string)
5997 "Convert the long number represented by STRING into an Emacs format.
5998 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
5999 (let* ((n (string-to-number (concat string ".0"))))
6000 (list (truncate (/ n 65536))
6001 (truncate (mod n 65536)))))
6003 (defalias 'erc-emacs-time-to-erc-time
6004 (if (featurep 'xemacs) 'time-to-seconds 'float-time))
6006 (defalias 'erc-current-time 'erc-emacs-time-to-erc-time)
6008 (defun erc-time-diff (t1 t2)
6009 "Return the time difference in seconds between T1 and T2."
6010 (abs (- t2 t1)))
6012 (defun erc-time-gt (t1 t2)
6013 "Check whether T1 > T2."
6014 (> t1 t2))
6016 (defun erc-sec-to-time (ns)
6017 "Convert NS to a time string HH:MM.SS."
6018 (setq ns (truncate ns))
6019 (format "%02d:%02d.%02d"
6020 (/ ns 3600)
6021 (/ (% ns 3600) 60)
6022 (% ns 60)))
6024 (defun erc-seconds-to-string (seconds)
6025 "Convert a number of SECONDS into an English phrase."
6026 (let (days hours minutes format-args output)
6027 (setq days (/ seconds 86400)
6028 seconds (% seconds 86400)
6029 hours (/ seconds 3600)
6030 seconds (% seconds 3600)
6031 minutes (/ seconds 60)
6032 seconds (% seconds 60)
6033 format-args (if (> days 0)
6034 `("%d days, %d hours, %d minutes, %d seconds"
6035 ,days ,hours ,minutes ,seconds)
6036 (if (> hours 0)
6037 `("%d hours, %d minutes, %d seconds"
6038 ,hours ,minutes ,seconds)
6039 (if (> minutes 0)
6040 `("%d minutes, %d seconds" ,minutes ,seconds)
6041 `("%d seconds" ,seconds))))
6042 output (apply #'format format-args))
6043 ;; Change all "1 units" to "1 unit".
6044 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
6045 (setq output (erc-replace-match-subexpression-in-string
6046 "" output (match-string 2 output) 2 (match-beginning 2))))
6047 output))
6050 ;; info
6052 (defconst erc-clientinfo-alist
6053 '(("ACTION" . "is used to inform about one's current activity")
6054 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
6055 ("ECHO" . "echoes its arguments back")
6056 ("FINGER" . "shows user's name, location, and idle time")
6057 ("PING" . "measures delay between peers")
6058 ("TIME" . "shows client-side time")
6059 ("USERINFO" . "shows information provided by a user")
6060 ("VERSION" . "shows client type and version"))
6061 "Alist of CTCP CLIENTINFO for ERC commands.")
6063 (defun erc-client-info (s)
6064 "Return CTCP CLIENTINFO on command S.
6065 If S is nil or an empty string then return general CLIENTINFO."
6066 (if (or (not s) (string= s ""))
6067 (concat
6068 (apply #'concat
6069 (mapcar (lambda (e)
6070 (concat (car e) " "))
6071 erc-clientinfo-alist))
6072 ": use CLIENTINFO <COMMAND> to get more specific information")
6073 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
6074 (if h
6075 (concat s " " (cdr h))
6076 (concat s ": unknown command")))))
6078 ;; Hook functions
6080 (defun erc-directory-writable-p (dir)
6081 "Determine whether DIR is a writable directory.
6082 If it doesn't exist, create it."
6083 (unless (file-attributes dir) (make-directory dir))
6084 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
6086 (defun erc-kill-query-buffers (process)
6087 "Kill all buffers of PROCESS.
6088 Does nothing if PROCESS is not a process object."
6089 ;; here, we only want to match the channel buffers, to avoid
6090 ;; "selecting killed buffers" b0rkage.
6091 (when (processp process)
6092 (erc-with-all-buffers-of-server process
6093 (lambda ()
6094 (not (erc-server-buffer-p)))
6095 (kill-buffer (current-buffer)))))
6097 (defun erc-nick-at-point ()
6098 "Give information about the nickname at `point'.
6100 If called interactively, give a human readable message in the
6101 minibuffer. If called programmatically, return the corresponding
6102 entry of `channel-members'."
6103 (interactive)
6104 (require 'thingatpt)
6105 (let* ((word (word-at-point))
6106 (channel-data (erc-get-channel-user word))
6107 (cuser (cdr channel-data))
6108 (user (if channel-data
6109 (car channel-data)
6110 (erc-get-server-user word)))
6111 host login full-name nick voice halfop op admin owner)
6112 (when user
6113 (setq nick (erc-server-user-nickname user)
6114 host (erc-server-user-host user)
6115 login (erc-server-user-login user)
6116 full-name (erc-server-user-full-name user))
6117 (if cuser
6118 (setq voice (erc-channel-user-voice cuser)
6119 halfop (erc-channel-user-halfop cuser)
6120 op (erc-channel-user-op cuser)
6121 admin (erc-channel-user-admin cuser)
6122 owner (erc-channel-user-owner cuser))))
6123 (if (called-interactively-p 'interactive)
6124 (message "%s is %s@%s%s%s"
6125 nick login host
6126 (if full-name (format " (%s)" full-name) "")
6127 (if (or voice halfop op admin owner)
6128 (format " and is +%s%s%s%s%s on %s"
6129 (if voice "v" "")
6130 (if halfop "h" "")
6131 (if op "o" "")
6132 (if admin "a" "")
6133 (if owner "q" "")
6134 (erc-default-target))
6135 ""))
6136 user)))
6138 (defun erc-away-time ()
6139 "Return non-nil if the current ERC process is set away.
6141 In particular, the time that we were set away is returned.
6142 See `current-time' for details on the time format."
6143 (erc-with-server-buffer erc-away))
6145 ;; Mode line handling
6147 (defcustom erc-mode-line-format "%S %a"
6148 "A string to be formatted and shown in the mode-line in `erc-mode'.
6150 The string is formatted using `format-spec' and the result is set as the value
6151 of `mode-line-buffer-identification'.
6153 The following characters are replaced:
6154 %a: String indicating away status or \"\" if you are not away
6155 %l: The estimated lag time to the server
6156 %m: The modes of the channel
6157 %n: The current nick name
6158 %N: The name of the network
6159 %o: The topic of the channel
6160 %p: The session port
6161 %t: The name of the target (channel, nickname, or servername:port)
6162 %s: In the server-buffer, this gets filled with the value of
6163 `erc-server-announced-name', in a channel, the value of
6164 (erc-default-target) also get concatenated.
6165 %S: In the server-buffer, this gets filled with the value of
6166 `erc-network', in a channel, the value of (erc-default-target)
6167 also get concatenated."
6168 :group 'erc-mode-line-and-header
6169 :type 'string)
6171 (defcustom erc-header-line-format "%n on %t (%m,%l) %o"
6172 "A string to be formatted and shown in the header-line in `erc-mode'.
6173 Only used starting in Emacs 21.
6175 Set this to nil if you do not want the header line to be
6176 displayed.
6178 See `erc-mode-line-format' for which characters are can be used."
6179 :group 'erc-mode-line-and-header
6180 :set (lambda (sym val)
6181 (set sym val)
6182 (when (fboundp 'erc-update-mode-line)
6183 (erc-update-mode-line nil)))
6184 :type '(choice (const :tag "Disabled" nil)
6185 string))
6187 (defcustom erc-header-line-uses-tabbar-p nil
6188 "Use tabbar mode instead of the header line to display the header."
6189 :group 'erc-mode-line-and-header
6190 :type 'boolean)
6192 (defcustom erc-header-line-uses-help-echo-p t
6193 "Show the contents of the header line in the echo area or as a tooltip
6194 when you move point into the header line."
6195 :group 'erc-mode-line-and-header
6196 :type 'boolean)
6198 (defcustom erc-header-line-face-method nil
6199 "Determine what method to use when colorizing the header line text.
6201 If nil, don't colorize the header text.
6202 If given a function, call it and use the resulting face name.
6203 Otherwise, use the `erc-header-line' face."
6204 :group 'erc-mode-line-and-header
6205 :type '(choice (const :tag "Don't colorize" nil)
6206 (const :tag "Use the erc-header-line face" t)
6207 (function :tag "Call a function")))
6209 (defcustom erc-show-channel-key-p t
6210 "Show the channel key in the header line."
6211 :group 'erc-paranoia
6212 :type 'boolean)
6214 (defcustom erc-mode-line-away-status-format
6215 "(AWAY since %a %b %d %H:%M) "
6216 "When you're away on a server, this is shown in the mode line.
6217 This should be a string with substitution variables recognized by
6218 `format-time-string'."
6219 :group 'erc-mode-line-and-header
6220 :type 'string)
6222 (defun erc-shorten-server-name (server-name)
6223 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
6224 (if (stringp server-name)
6225 (with-temp-buffer
6226 (insert server-name)
6227 (let ((alist erc-common-server-suffixes))
6228 (while alist
6229 (goto-char (point-min))
6230 (if (re-search-forward (caar alist) nil t)
6231 (replace-match (cdar alist)))
6232 (setq alist (cdr alist))))
6233 (buffer-string))))
6235 (defun erc-format-target ()
6236 "Return the name of the target (channel or nickname or servername:port)."
6237 (let ((target (erc-default-target)))
6238 (or target
6239 (concat (erc-shorten-server-name
6240 (or erc-server-announced-name
6241 erc-session-server))
6242 ":" (erc-port-to-string erc-session-port)))))
6244 (defun erc-format-target-and/or-server ()
6245 "Return the server name or the current target and server name combined."
6246 (let ((server-name (erc-shorten-server-name
6247 (or erc-server-announced-name
6248 erc-session-server))))
6249 (cond ((erc-default-target)
6250 (concat (erc-string-no-properties (erc-default-target))
6251 "@" server-name))
6252 (server-name server-name)
6253 (t (buffer-name (current-buffer))))))
6255 (defun erc-format-network ()
6256 "Return the name of the network we are currently on."
6257 (let ((network (and (fboundp 'erc-network-name) (erc-network-name))))
6258 (if (and network (symbolp network))
6259 (symbol-name network)
6260 "")))
6262 (defun erc-format-target-and/or-network ()
6263 "Return the network or the current target and network combined.
6264 If the name of the network is not available, then use the
6265 shortened server name instead."
6266 (let ((network-name (or (and (fboundp 'erc-network-name) (erc-network-name))
6267 (erc-shorten-server-name
6268 (or erc-server-announced-name
6269 erc-session-server)))))
6270 (when (and network-name (symbolp network-name))
6271 (setq network-name (symbol-name network-name)))
6272 (cond ((erc-default-target)
6273 (concat (erc-string-no-properties (erc-default-target))
6274 "@" network-name))
6275 ((and network-name
6276 (not (get-buffer network-name)))
6277 (when erc-rename-buffers
6278 (rename-buffer network-name))
6279 network-name)
6280 (t (buffer-name (current-buffer))))))
6282 (defun erc-format-away-status ()
6283 "Return a formatted `erc-mode-line-away-status-format'
6284 if `erc-away' is non-nil."
6285 (let ((a (erc-away-time)))
6286 (if a
6287 (format-time-string erc-mode-line-away-status-format a)
6288 "")))
6290 (defun erc-format-channel-modes ()
6291 "Return the current channel's modes."
6292 (concat (apply #'concat
6293 "+" erc-channel-modes)
6294 (cond ((and erc-channel-user-limit erc-channel-key)
6295 (if erc-show-channel-key-p
6296 (format "lk %.0f %s" erc-channel-user-limit
6297 erc-channel-key)
6298 (format "kl %.0f" erc-channel-user-limit)))
6299 (erc-channel-user-limit
6300 ;; Emacs has no bignums
6301 (format "l %.0f" erc-channel-user-limit))
6302 (erc-channel-key
6303 (if erc-show-channel-key-p
6304 (format "k %s" erc-channel-key)
6305 "k"))
6306 (t nil))))
6308 (defun erc-format-lag-time ()
6309 "Return the estimated lag time to server, `erc-server-lag'."
6310 (let ((lag (erc-with-server-buffer erc-server-lag)))
6311 (cond (lag (format "lag:%.0f" lag))
6312 (t ""))))
6314 ;; erc-goodies is required at end of this file.
6315 (declare-function erc-controls-strip "erc-goodies" (str))
6317 (defvar tabbar--local-hlf)
6319 (defun erc-update-mode-line-buffer (buffer)
6320 "Update the mode line in a single ERC buffer BUFFER."
6321 (with-current-buffer buffer
6322 (let ((spec (format-spec-make
6323 ?a (erc-format-away-status)
6324 ?l (erc-format-lag-time)
6325 ?m (erc-format-channel-modes)
6326 ?n (or (erc-current-nick) "")
6327 ?N (erc-format-network)
6328 ?o (or (erc-controls-strip erc-channel-topic) "")
6329 ?p (erc-port-to-string erc-session-port)
6330 ?s (erc-format-target-and/or-server)
6331 ?S (erc-format-target-and/or-network)
6332 ?t (erc-format-target)))
6333 (process-status (cond ((and (erc-server-process-alive)
6334 (not erc-server-connected))
6335 ":connecting")
6336 ((erc-server-process-alive)
6339 ": CLOSED")))
6340 (face (cond ((eq erc-header-line-face-method nil)
6341 nil)
6342 ((functionp erc-header-line-face-method)
6343 (funcall erc-header-line-face-method))
6345 'erc-header-line))))
6346 (cond ((featurep 'xemacs)
6347 (setq modeline-buffer-identification
6348 (list (format-spec erc-mode-line-format spec)))
6349 (setq modeline-process (list process-status)))
6351 (setq mode-line-buffer-identification
6352 (list (format-spec erc-mode-line-format spec)))
6353 (setq mode-line-process (list process-status))))
6354 (when (boundp 'header-line-format)
6355 (let ((header (if erc-header-line-format
6356 (format-spec erc-header-line-format spec)
6357 nil)))
6358 (cond (erc-header-line-uses-tabbar-p
6359 (set (make-local-variable 'tabbar--local-hlf)
6360 header-line-format)
6361 (kill-local-variable 'header-line-format))
6362 ((null header)
6363 (setq header-line-format nil))
6364 (erc-header-line-uses-help-echo-p
6365 (let ((help-echo (with-temp-buffer
6366 (insert header)
6367 (fill-region (point-min) (point-max))
6368 (buffer-string))))
6369 (setq header-line-format
6370 (erc-replace-regexp-in-string
6372 "%%"
6373 (if face
6374 (erc-propertize header 'help-echo help-echo
6375 'face face)
6376 (erc-propertize header 'help-echo help-echo))))))
6377 (t (setq header-line-format
6378 (if face
6379 (erc-propertize header 'face face)
6380 header)))))))
6381 (if (featurep 'xemacs)
6382 (redraw-modeline)
6383 (force-mode-line-update))))
6385 (defun erc-update-mode-line (&optional buffer)
6386 "Update the mode line in BUFFER.
6388 If BUFFER is nil, update the mode line in all ERC buffers."
6389 (if (and buffer (bufferp buffer))
6390 (erc-update-mode-line-buffer buffer)
6391 (dolist (buf (erc-buffer-list))
6392 (when (buffer-live-p buf)
6393 (erc-update-mode-line-buffer buf)))))
6395 ;; Miscellaneous
6397 (defun erc-port-to-string (p)
6398 "Convert port P to a string.
6399 P may be an integer or a service name."
6400 (if (integerp p)
6401 (int-to-string p)
6404 (defun erc-string-to-port (s)
6405 "Convert string S to either an integer port number or a service name."
6406 (if (numberp s)
6408 (let ((n (string-to-number s)))
6409 (if (= n 0)
6411 n))))
6413 (defun erc-version (&optional here)
6414 "Show the version number of ERC in the minibuffer.
6415 If optional argument HERE is non-nil, insert version number at point."
6416 (interactive "P")
6417 (let ((version-string
6418 (format "ERC (IRC client for Emacs %s)" emacs-version)))
6419 (if here
6420 (insert version-string)
6421 (if (called-interactively-p 'interactive)
6422 (message "%s" version-string)
6423 version-string))))
6425 (defun erc-modes (&optional here)
6426 "Show the active ERC modes in the minibuffer.
6427 If optional argument HERE is non-nil, insert version number at point."
6428 (interactive "P")
6429 (let ((string
6430 (mapconcat 'identity
6431 (let (modes (case-fold-search nil))
6432 (dolist (var (apropos-internal "^erc-.*mode$"))
6433 (when (and (boundp var)
6434 (symbol-value var))
6435 (setq modes (cons (symbol-name var)
6436 modes))))
6437 modes)
6438 ", ")))
6439 (if here
6440 (insert string)
6441 (if (called-interactively-p 'interactive)
6442 (message "%s" string)
6443 string))))
6445 (defun erc-trim-string (s)
6446 "Trim leading and trailing spaces off S."
6447 (cond
6448 ((not (stringp s)) nil)
6449 ((string-match "^\\s-*$" s)
6451 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
6452 (match-string 1 s))
6454 s)))
6456 (defun erc-arrange-session-in-multiple-windows ()
6457 "Open a window for every non-server buffer related to `erc-session-server'.
6459 All windows are opened in the current frame."
6460 (interactive)
6461 (unless erc-server-process
6462 (error "No erc-server-process found in current buffer"))
6463 (let ((bufs (erc-buffer-list nil erc-server-process)))
6464 (when bufs
6465 (delete-other-windows)
6466 (switch-to-buffer (car bufs))
6467 (setq bufs (cdr bufs))
6468 (while bufs
6469 (split-window)
6470 (other-window 1)
6471 (switch-to-buffer (car bufs))
6472 (setq bufs (cdr bufs))
6473 (balance-windows)))))
6475 (defun erc-popup-input-buffer ()
6476 "Provide an input buffer."
6477 (interactive)
6478 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
6479 (mode (intern
6480 (completing-read
6481 "Mode: "
6482 (mapcar (lambda (e)
6483 (list (symbol-name e)))
6484 (apropos-internal "-mode\\'" 'commandp))
6485 nil t))))
6486 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
6487 (funcall mode)
6488 (narrow-to-region (point) (point))
6489 (shrink-window-if-larger-than-buffer)))
6491 ;;; Message catalog
6493 (defun erc-make-message-variable-name (catalog entry)
6494 "Create a variable name corresponding to CATALOG's ENTRY."
6495 (intern (concat "erc-message-"
6496 (symbol-name catalog) "-" (symbol-name entry))))
6498 (defun erc-define-catalog-entry (catalog entry format-spec)
6499 "Set CATALOG's ENTRY to FORMAT-SPEC."
6500 (set (erc-make-message-variable-name catalog entry)
6501 format-spec))
6503 (defun erc-define-catalog (catalog entries)
6504 "Define a CATALOG according to ENTRIES."
6505 (dolist (entry entries)
6506 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
6508 (erc-define-catalog
6509 'english
6510 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
6511 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
6512 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
6513 (cannot-find-file . "Cannot find file %f")
6514 (cannot-read-file . "Cannot read file %f")
6515 (connect . "Connecting to %S:%p... ")
6516 (country . "%c")
6517 (country-unknown . "%d: No such domain")
6518 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
6519 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
6520 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
6521 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
6522 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
6523 (flood-strict-mode
6524 . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
6525 (disconnected . "\n\nConnection failed! Re-establishing connection...\n")
6526 (disconnected-noreconnect
6527 . "\n\nConnection failed! Not re-establishing connection.\n")
6528 (finished . "\n\n*** ERC finished ***\n")
6529 (terminated . "\n\n*** ERC terminated: %e\n")
6530 (login . "Logging in as `%n'...")
6531 (nick-in-use . "%n is in use. Choose new nickname: ")
6532 (nick-too-long
6533 . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
6534 (no-default-channel . "No default channel")
6535 (no-invitation . "You've got no invitation")
6536 (no-target . "No target")
6537 (ops . "%i operator%s: %o")
6538 (ops-none . "No operators in this channel.")
6539 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
6540 (variable-not-bound . "Variable not bound!")
6541 (ACTION . "* %n %a")
6542 (CTCP-CLIENTINFO . "Client info for %n: %m")
6543 (CTCP-ECHO . "Echo %n: %m")
6544 (CTCP-FINGER . "Finger info for %n: %m")
6545 (CTCP-PING . "Ping time to %n is %t")
6546 (CTCP-TIME . "Time by %n is %m")
6547 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
6548 (CTCP-VERSION . "Version for %n is %m")
6549 (ERROR . "==> ERROR from %s: %c\n")
6550 (INVITE . "%n (%u@%h) invites you to channel %c")
6551 (JOIN . "%n (%u@%h) has joined channel %c")
6552 (JOIN-you . "You have joined channel %c")
6553 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
6554 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
6555 (KICK-by-you . "You have kicked %k off channel %c: %r")
6556 (MODE . "%n (%u@%h) has changed mode for %t to %m")
6557 (MODE-nick . "%n has changed mode for %t to %m")
6558 (NICK . "%n (%u@%h) is now known as %N")
6559 (NICK-you . "Your new nickname is %N")
6560 (PART . erc-message-english-PART)
6561 (PING . "PING from server (last: %s sec. ago)")
6562 (PONG . "PONG from %h (%i second%s)")
6563 (QUIT . "%n (%u@%h) has quit: %r")
6564 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
6565 (WALLOPS . "Wallops from %n: %m")
6566 (s004 . "%s %v %U %C")
6567 (s221 . "User modes for %n: %m")
6568 (s252 . "%i operator(s) online")
6569 (s253 . "%i unknown connection(s)")
6570 (s254 . "%i channels formed")
6571 (s275 . "%n %m")
6572 (s301 . "%n is AWAY: %r")
6573 (s303 . "Is online: %n")
6574 (s305 . "%m")
6575 (s306 . "%m")
6576 (s307 . "%n %m")
6577 (s311 . "%n is %f (%u@%h)")
6578 (s312 . "%n is/was on server %s (%c)")
6579 (s313 . "%n is an IRC operator")
6580 (s314 . "%n was %f (%u@%h)")
6581 (s317 . "%n has been idle for %i")
6582 (s317-on-since . "%n has been idle for %i, on since %t")
6583 (s319 . "%n is on channel(s): %c")
6584 (s320 . "%n is an identified user")
6585 (s321 . "Channel Users Topic")
6586 (s322 . "%c [%u] %t")
6587 (s324 . "%c modes: %m")
6588 (s328 . "%c URL: %u")
6589 (s329 . "%c was created on %t")
6590 (s330 . "%n %a %i")
6591 (s331 . "No topic is set for %c")
6592 (s332 . "Topic for %c: %T")
6593 (s333 . "%c: topic set by %n, %t")
6594 (s341 . "Inviting %n to channel %c")
6595 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
6596 (s353 . "Users on %c: %u")
6597 (s367 . "Ban for %b on %c")
6598 (s367-set-by . "Ban for %b on %c set by %s on %t")
6599 (s368 . "Banlist of %c ends.")
6600 (s379 . "%c: Forwarded to %f")
6601 (s391 . "The time at %s is %t")
6602 (s401 . "%n: No such nick/channel")
6603 (s403 . "%c: No such channel")
6604 (s404 . "%c: Cannot send to channel")
6605 (s405 . "%c: You have joined too many channels")
6606 (s406 . "%n: There was no such nickname")
6607 (s412 . "No text to send")
6608 (s421 . "%c: Unknown command")
6609 (s431 . "No nickname given")
6610 (s432 . "%n is an erroneous nickname")
6611 (s442 . "%c: You're not on that channel")
6612 (s445 . "SUMMON has been disabled")
6613 (s446 . "USERS has been disabled")
6614 (s451 . "You have not registered")
6615 (s461 . "%c: not enough parameters")
6616 (s462 . "Unauthorized command (already registered)")
6617 (s463 . "Your host isn't among the privileged")
6618 (s464 . "Password incorrect")
6619 (s465 . "You are banned from this server")
6620 (s474 . "You can't join %c because you're banned (+b)")
6621 (s475 . "You must specify the correct channel key (+k) to join %c")
6622 (s481 . "Permission Denied - You're not an IRC operator")
6623 (s482 . "You need to be a channel operator of %c to do that")
6624 (s483 . "You can't kill a server!")
6625 (s484 . "Your connection is restricted!")
6626 (s485 . "You're not the original channel operator")
6627 (s491 . "No O-lines for your host")
6628 (s501 . "Unknown MODE flag")
6629 (s502 . "You can't change modes for other users")
6630 (s671 . "%n %a")))
6632 (defun erc-message-english-PART (&rest args)
6633 "Format a proper PART message.
6635 This function is an example on what could be done with formatting
6636 functions."
6637 (let ((nick (cadr (memq ?n args)))
6638 (user (cadr (memq ?u args)))
6639 (host (cadr (memq ?h args)))
6640 (channel (cadr (memq ?c args)))
6641 (reason (cadr (memq ?r args))))
6642 (if (string= nick (erc-current-nick))
6643 (format "You have left channel %s" channel)
6644 (format "%s (%s@%s) has left channel %s%s"
6645 nick user host channel
6646 (if (not (string= reason ""))
6647 (format ": %s"
6648 (erc-replace-regexp-in-string "%" "%%" reason))
6649 "")))))
6652 (defvar erc-current-message-catalog 'english)
6653 (make-variable-buffer-local 'erc-current-message-catalog)
6655 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6656 "Retrieve ENTRY from CATALOG.
6658 If CATALOG is nil, `erc-current-message-catalog' is used.
6660 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6661 english, catalog."
6662 (unless catalog (setq catalog erc-current-message-catalog))
6663 (let ((var (erc-make-message-variable-name catalog entry)))
6664 (if (boundp var)
6665 (symbol-value var)
6666 (when (boundp (erc-make-message-variable-name 'english entry))
6667 (symbol-value (erc-make-message-variable-name 'english entry))))))
6669 (defun erc-format-message (msg &rest args)
6670 "Format MSG according to ARGS.
6672 See also `format-spec'."
6673 (when (eq (logand (length args) 1) 1) ; oddp
6674 (error "Obscure usage of this function appeared"))
6675 (let ((entry (erc-retrieve-catalog-entry msg)))
6676 (when (not entry)
6677 (error "No format spec for message %s" msg))
6678 (when (functionp entry)
6679 (setq entry (apply entry args)))
6680 (format-spec entry (apply #'format-spec-make args))))
6682 ;;; Various hook functions
6684 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6686 (defcustom erc-kill-server-hook '(erc-kill-server)
6687 "Invoked whenever a server buffer is killed via `kill-buffer'."
6688 :group 'erc-hooks
6689 :type 'hook)
6691 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6692 "Invoked whenever a channel-buffer is killed via `kill-buffer'."
6693 :group 'erc-hooks
6694 :type 'hook)
6696 (defcustom erc-kill-buffer-hook nil
6697 "Hook run whenever a non-server or channel buffer is killed.
6699 See also `kill-buffer'."
6700 :group 'erc-hooks
6701 :type 'hook)
6703 (defun erc-kill-buffer-function ()
6704 "Function to call when an ERC buffer is killed.
6705 This function should be on `kill-buffer-hook'.
6706 When the current buffer is in `erc-mode', this function will run
6707 one of the following hooks:
6708 `erc-kill-server-hook' if the server buffer was killed,
6709 `erc-kill-channel-hook' if a channel buffer was killed,
6710 or `erc-kill-buffer-hook' if any other buffer."
6711 (when (eq major-mode 'erc-mode)
6712 (erc-remove-channel-users)
6713 (cond
6714 ((eq (erc-server-buffer) (current-buffer))
6715 (run-hooks 'erc-kill-server-hook))
6716 ((erc-channel-p (or (erc-default-target) (buffer-name)))
6717 (run-hooks 'erc-kill-channel-hook))
6719 (run-hooks 'erc-kill-buffer-hook)))))
6721 (defun erc-kill-server ()
6722 "Sends a QUIT command to the server when the server buffer is killed.
6723 This function should be on `erc-kill-server-hook'."
6724 (when (erc-server-process-alive)
6725 (setq erc-server-quitting t)
6726 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6728 (defun erc-kill-channel ()
6729 "Sends a PART command to the server when the channel buffer is killed.
6730 This function should be on `erc-kill-channel-hook'."
6731 (when (erc-server-process-alive)
6732 (let ((tgt (erc-default-target)))
6733 (erc-server-send (format "PART %s :%s" tgt
6734 (funcall erc-part-reason nil))
6735 nil tgt))))
6737 ;;; Dealing with `erc-parsed'
6739 (defun erc-find-parsed-property ()
6740 "Find the next occurrence of the `erc-parsed' text property."
6741 (text-property-not-all (point-min) (point-max) 'erc-parsed nil))
6743 (defun erc-restore-text-properties ()
6744 "Restore the property `erc-parsed' for the region."
6745 (let ((parsed-posn (erc-find-parsed-property)))
6746 (put-text-property
6747 (point-min) (point-max)
6748 'erc-parsed (when parsed-posn (erc-get-parsed-vector parsed-posn)))))
6750 (defun erc-get-parsed-vector (point)
6751 "Return the whole parsed vector on POINT."
6752 (get-text-property point 'erc-parsed))
6754 (defun erc-get-parsed-vector-nick (vect)
6755 "Return nickname in the parsed vector VECT."
6756 (let* ((untreated-nick (and vect (erc-response.sender vect)))
6757 (maybe-nick (when untreated-nick
6758 (car (split-string untreated-nick "!")))))
6759 (when (and (not (null maybe-nick))
6760 (erc-is-valid-nick-p maybe-nick))
6761 untreated-nick)))
6763 (defun erc-get-parsed-vector-type (vect)
6764 "Return message type in the parsed vector VECT."
6765 (and vect
6766 (erc-response.command vect)))
6768 ;; Teach url.el how to open irc:// URLs with ERC.
6769 ;; To activate, customize `url-irc-function' to `url-irc-erc'.
6771 ;;;###autoload
6772 (defun erc-handle-irc-url (host port channel user password)
6773 "Use ERC to IRC on HOST:PORT in CHANNEL as USER with PASSWORD.
6774 If ERC is already connected to HOST:PORT, simply /join CHANNEL.
6775 Otherwise, connect to HOST:PORT as USER and /join CHANNEL."
6776 (let ((server-buffer
6777 (car (erc-buffer-filter
6778 (lambda ()
6779 (and (string-equal erc-session-server host)
6780 (= erc-session-port port)
6781 (erc-open-server-buffer-p)))))))
6782 (with-current-buffer (or server-buffer (current-buffer))
6783 (if (and server-buffer channel)
6784 (erc-cmd-JOIN channel)
6785 (erc-open host port (or user (erc-compute-nick)) (erc-compute-full-name)
6786 (not server-buffer) password nil channel
6787 (when server-buffer
6788 (get-buffer-process server-buffer)))))))
6790 (provide 'erc)
6792 ;; Deprecated. We might eventually stop requiring the goodies automatically.
6793 ;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6794 ;; avoid a recursive require error when byte-compiling the entire package.
6795 (require 'erc-goodies)
6797 ;;; erc.el ends here
6799 ;; Local Variables:
6800 ;; outline-regexp: ";;+"
6801 ;; indent-tabs-mode: t
6802 ;; tab-width: 8
6803 ;; End: