In strings, prefer plain ` and ' to \` and \'
[emacs.git] / lisp / erc / erc.el
blob460accd3be35c229a8867cbc443535b0774ab4ad
1 ;; erc.el --- An Emacs Internet Relay Chat client -*- lexical-binding:t -*-
3 ;; Copyright (C) 1997-2015 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 :group 'erc-ignore
270 :type 'erc-message-type)
272 (defcustom erc-channel-hide-list nil
273 "A list of IRC channels to hide message types from.
274 A typical value would be '((\"#emacs\" \"QUIT\" \JOIN\")
275 (\"#erc\" \"NICK\")."
276 :group 'erc-ignore
277 :type 'erc-message-type)
279 (defvar erc-session-password nil
280 "The password used for the current session.")
281 (make-variable-buffer-local 'erc-session-password)
283 (defcustom erc-disconnected-hook nil
284 "Run this hook with arguments (NICK IP REASON) when disconnected.
285 This happens before automatic reconnection. Note, that
286 `erc-server-QUIT-functions' might not be run when we disconnect,
287 simply because we do not necessarily receive the QUIT event."
288 :group 'erc-hooks
289 :type 'hook)
291 (defcustom erc-complete-functions nil
292 "These functions get called when the user hits TAB in ERC.
293 Each function in turn is called until one returns non-nil to
294 indicate it has handled the input."
295 :group 'erc-hooks
296 :type 'hook)
298 (defcustom erc-join-hook nil
299 "Hook run when we join a channel. Hook functions are called
300 without arguments, with the current buffer set to the buffer of
301 the new channel.
303 See also `erc-server-JOIN-functions', `erc-part-hook'."
304 :group 'erc-hooks
305 :type 'hook)
307 (defcustom erc-quit-hook nil
308 "Hook run when processing a quit command directed at our nick.
310 The hook receives one argument, the current PROCESS.
311 See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
312 :group 'erc-hooks
313 :type 'hook)
315 (defcustom erc-part-hook nil
316 "Hook run when processing a PART message directed at our nick.
318 The hook receives one argument, the current BUFFER.
319 See also `erc-server-QUIT-functions', `erc-quit-hook' and
320 `erc-disconnected-hook'."
321 :group 'erc-hooks
322 :type 'hook)
324 (defcustom erc-kick-hook nil
325 "Hook run when processing a KICK message directed at our nick.
327 The hook receives one argument, the current BUFFER.
328 See also `erc-server-PART-functions' and `erc-part-hook'."
329 :group 'erc-hooks
330 :type 'hook)
332 (defcustom erc-nick-changed-functions nil
333 "List of functions run when your nick was successfully changed.
335 Each function should accept two arguments, NEW-NICK and OLD-NICK."
336 :group 'erc-hooks
337 :type 'hook)
339 (defcustom erc-connect-pre-hook '(erc-initialize-log-marker)
340 "Hook called just before `erc' calls `erc-connect'.
341 Functions are passed a buffer as the first argument."
342 :group 'erc-hooks
343 :type 'hook)
346 (defvar erc-channel-users nil
347 "A hash table of members in the current channel, which
348 associates nicknames with cons cells of the form:
349 \(USER . MEMBER-DATA) where USER is a pointer to an
350 erc-server-user struct, and MEMBER-DATA is a pointer to an
351 erc-channel-user struct.")
352 (make-variable-buffer-local 'erc-channel-users)
354 (defvar erc-server-users nil
355 "A hash table of users on the current server, which associates
356 nicknames with erc-server-user struct instances.")
357 (make-variable-buffer-local 'erc-server-users)
359 (defun erc-downcase (string)
360 "Convert STRING to IRC standard conforming downcase."
361 (let ((s (downcase string))
362 (c '((?\[ . ?\{)
363 (?\] . ?\})
364 (?\\ . ?\|)
365 (?~ . ?^))))
366 (save-match-data
367 (while (string-match "[]\\[~]" s)
368 (aset s (match-beginning 0)
369 (cdr (assq (aref s (match-beginning 0)) c)))))
372 (defmacro erc-with-server-buffer (&rest body)
373 "Execute BODY in the current ERC server buffer.
374 If no server buffer exists, return nil."
375 (declare (indent 0) (debug (body)))
376 (let ((buffer (make-symbol "buffer")))
377 `(let ((,buffer (erc-server-buffer)))
378 (when (buffer-live-p ,buffer)
379 (with-current-buffer ,buffer
380 ,@body)))))
382 (cl-defstruct (erc-server-user (:type vector) :named)
383 ;; User data
384 nickname host login full-name info
385 ;; Buffers
387 ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
388 ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
389 (buffers nil)
392 (cl-defstruct (erc-channel-user (:type vector) :named)
393 voice halfop op admin owner
394 ;; Last message time (in the form of the return value of
395 ;; (current-time)
397 ;; This is useful for ordered name completion.
398 (last-message-time nil))
400 (defsubst erc-get-channel-user (nick)
401 "Find the (USER . CHANNEL-DATA) element corresponding to NICK
402 in the current buffer's `erc-channel-users' hash table."
403 (gethash (erc-downcase nick) erc-channel-users))
405 (defsubst erc-get-server-user (nick)
406 "Find the USER corresponding to NICK in the current server's
407 `erc-server-users' hash table."
408 (erc-with-server-buffer
409 (gethash (erc-downcase nick) erc-server-users)))
411 (defsubst erc-add-server-user (nick user)
412 "This function is for internal use only.
414 Adds USER with nickname NICK to the `erc-server-users' hash table."
415 (erc-with-server-buffer
416 (puthash (erc-downcase nick) user erc-server-users)))
418 (defsubst erc-remove-server-user (nick)
419 "This function is for internal use only.
421 Removes the user with nickname NICK from the `erc-server-users'
422 hash table. This user is not removed from the
423 `erc-channel-users' lists of other buffers.
425 See also: `erc-remove-user'."
426 (erc-with-server-buffer
427 (remhash (erc-downcase nick) erc-server-users)))
429 (defun erc-change-user-nickname (user new-nick)
430 "This function is for internal use only.
432 Changes the nickname of USER to NEW-NICK in the
433 `erc-server-users' hash table. The `erc-channel-users' lists of
434 other buffers are also changed."
435 (let ((nick (erc-server-user-nickname user)))
436 (setf (erc-server-user-nickname user) new-nick)
437 (erc-with-server-buffer
438 (remhash (erc-downcase nick) erc-server-users)
439 (puthash (erc-downcase new-nick) user erc-server-users))
440 (dolist (buf (erc-server-user-buffers user))
441 (if (buffer-live-p buf)
442 (with-current-buffer buf
443 (let ((cdata (erc-get-channel-user nick)))
444 (remhash (erc-downcase nick) erc-channel-users)
445 (puthash (erc-downcase new-nick) cdata
446 erc-channel-users)))))))
448 (defun erc-remove-channel-user (nick)
449 "This function is for internal use only.
451 Removes the user with nickname NICK from the `erc-channel-users'
452 list for this channel. If this user is not in the
453 `erc-channel-users' list of any other buffers, the user is also
454 removed from the server's `erc-server-users' list.
456 See also: `erc-remove-server-user' and `erc-remove-user'."
457 (let ((channel-data (erc-get-channel-user nick)))
458 (when channel-data
459 (let ((user (car channel-data)))
460 (setf (erc-server-user-buffers user)
461 (delq (current-buffer)
462 (erc-server-user-buffers user)))
463 (remhash (erc-downcase nick) erc-channel-users)
464 (if (null (erc-server-user-buffers user))
465 (erc-remove-server-user nick))))))
467 (defun erc-remove-user (nick)
468 "This function is for internal use only.
470 Removes the user with nickname NICK from the `erc-server-users'
471 list as well as from all `erc-channel-users' lists.
473 See also: `erc-remove-server-user' and
474 `erc-remove-channel-user'."
475 (let ((user (erc-get-server-user nick)))
476 (when user
477 (let ((buffers (erc-server-user-buffers user)))
478 (dolist (buf buffers)
479 (if (buffer-live-p buf)
480 (with-current-buffer buf
481 (remhash (erc-downcase nick) erc-channel-users)
482 (run-hooks 'erc-channel-members-changed-hook)))))
483 (erc-remove-server-user nick))))
485 (defun erc-remove-channel-users ()
486 "This function is for internal use only.
488 Removes all users in the current channel. This is called by
489 `erc-server-PART' and `erc-server-QUIT'."
490 (when (and erc-server-connected
491 (erc-server-process-alive)
492 (hash-table-p erc-channel-users))
493 (maphash (lambda (nick _cdata)
494 (erc-remove-channel-user nick))
495 erc-channel-users)
496 (clrhash erc-channel-users)))
498 (defsubst erc-channel-user-owner-p (nick)
499 "Return non-nil if NICK is an owner of the current channel."
500 (and nick
501 (hash-table-p erc-channel-users)
502 (let ((cdata (erc-get-channel-user nick)))
503 (and cdata (cdr cdata)
504 (erc-channel-user-owner (cdr cdata))))))
506 (defsubst erc-channel-user-admin-p (nick)
507 "Return non-nil if NICK is an admin in the current channel."
508 (and nick
509 (hash-table-p erc-channel-users)
510 (let ((cdata (erc-get-channel-user nick)))
511 (and cdata (cdr cdata)
512 (erc-channel-user-admin (cdr cdata))))))
514 (defsubst erc-channel-user-op-p (nick)
515 "Return non-nil if NICK is an operator in the current channel."
516 (and nick
517 (hash-table-p erc-channel-users)
518 (let ((cdata (erc-get-channel-user nick)))
519 (and cdata (cdr cdata)
520 (erc-channel-user-op (cdr cdata))))))
522 (defsubst erc-channel-user-halfop-p (nick)
523 "Return non-nil if NICK is a half-operator in the current channel."
524 (and nick
525 (hash-table-p erc-channel-users)
526 (let ((cdata (erc-get-channel-user nick)))
527 (and cdata (cdr cdata)
528 (erc-channel-user-halfop (cdr cdata))))))
530 (defsubst erc-channel-user-voice-p (nick)
531 "Return non-nil if NICK has voice in the current channel."
532 (and nick
533 (hash-table-p erc-channel-users)
534 (let ((cdata (erc-get-channel-user nick)))
535 (and cdata (cdr cdata)
536 (erc-channel-user-voice (cdr cdata))))))
538 (defun erc-get-channel-user-list ()
539 "Return a list of users in the current channel. Each element
540 of the list is of the form (USER . CHANNEL-DATA), where USER is
541 an erc-server-user struct, and CHANNEL-DATA is either nil or an
542 erc-channel-user struct.
544 See also: `erc-sort-channel-users-by-activity'"
545 (let (users)
546 (if (hash-table-p erc-channel-users)
547 (maphash (lambda (_nick cdata)
548 (setq users (cons cdata users)))
549 erc-channel-users))
550 users))
552 (defun erc-get-server-nickname-list ()
553 "Return a list of known nicknames on the current server."
554 (erc-with-server-buffer
555 (let (nicks)
556 (when (hash-table-p erc-server-users)
557 (maphash (lambda (_n user)
558 (setq nicks
559 (cons (erc-server-user-nickname user)
560 nicks)))
561 erc-server-users)
562 nicks))))
564 (defun erc-get-channel-nickname-list ()
565 "Return a list of known nicknames on the current channel."
566 (let (nicks)
567 (when (hash-table-p erc-channel-users)
568 (maphash (lambda (_n cdata)
569 (setq nicks
570 (cons (erc-server-user-nickname (car cdata))
571 nicks)))
572 erc-channel-users)
573 nicks)))
575 (defun erc-get-server-nickname-alist ()
576 "Return an alist of known nicknames on the current server."
577 (erc-with-server-buffer
578 (let (nicks)
579 (when (hash-table-p erc-server-users)
580 (maphash (lambda (_n user)
581 (setq nicks
582 (cons (cons (erc-server-user-nickname user) nil)
583 nicks)))
584 erc-server-users)
585 nicks))))
587 (defun erc-get-channel-nickname-alist ()
588 "Return an alist of known nicknames on the current channel."
589 (let (nicks)
590 (when (hash-table-p erc-channel-users)
591 (maphash (lambda (_n cdata)
592 (setq nicks
593 (cons (cons (erc-server-user-nickname (car cdata)) nil)
594 nicks)))
595 erc-channel-users)
596 nicks)))
598 (defun erc-sort-channel-users-by-activity (list)
599 "Sort LIST such that users which have spoken most recently are listed first.
600 LIST must be of the form (USER . CHANNEL-DATA).
602 See also: `erc-get-channel-user-list'."
603 (sort list
604 (lambda (x y)
605 (when (and (cdr x) (cdr y))
606 (let ((tx (erc-channel-user-last-message-time (cdr x)))
607 (ty (erc-channel-user-last-message-time (cdr y))))
608 (and tx
609 (or (not ty)
610 (time-less-p ty tx))))))))
612 (defun erc-sort-channel-users-alphabetically (list)
613 "Sort LIST so that users' nicknames are in alphabetical order.
614 LIST must be of the form (USER . CHANNEL-DATA).
616 See also: `erc-get-channel-user-list'."
617 (sort list
618 (lambda (x y)
619 (when (and (cdr x) (cdr y))
620 (let ((nickx (downcase (erc-server-user-nickname (car x))))
621 (nicky (downcase (erc-server-user-nickname (car y)))))
622 (and nickx
623 (or (not nicky)
624 (string-lessp nickx nicky))))))))
626 (defvar erc-channel-topic nil
627 "A topic string for the channel. Should only be used in channel-buffers.")
628 (make-variable-buffer-local 'erc-channel-topic)
630 (defvar erc-channel-modes nil
631 "List of strings representing channel modes.
632 E.g. '(\"i\" \"m\" \"s\" \"b Quake!*@*\")
633 \(not sure the ban list will be here, but why not)")
634 (make-variable-buffer-local 'erc-channel-modes)
636 (defvar erc-insert-marker nil
637 "The place where insertion of new text in erc buffers should happen.")
638 (make-variable-buffer-local 'erc-insert-marker)
640 (defvar erc-input-marker nil
641 "The marker where input should be inserted.")
642 (make-variable-buffer-local 'erc-input-marker)
644 (defun erc-string-no-properties (string)
645 "Return a copy of STRING will all text-properties removed."
646 (let ((newstring (copy-sequence string)))
647 (set-text-properties 0 (length newstring) nil newstring)
648 newstring))
650 (defcustom erc-prompt "ERC>"
651 "Prompt used by ERC. Trailing whitespace is not required."
652 :group 'erc-display
653 :type '(choice string function))
655 (defun erc-prompt ()
656 "Return the input prompt as a string.
658 See also the variable `erc-prompt'."
659 (let ((prompt (if (functionp erc-prompt)
660 (funcall erc-prompt)
661 erc-prompt)))
662 (if (> (length prompt) 0)
663 (concat prompt " ")
664 prompt)))
666 (defcustom erc-command-indicator nil
667 "Indicator used by ERC for showing commands.
669 If non-nil, this will be used in the ERC buffer to indicate
670 commands (i.e., input starting with a '/').
672 If nil, the prompt will be constructed from the variable `erc-prompt'."
673 :group 'erc-display
674 :type '(choice (const nil) string function))
676 (defun erc-command-indicator ()
677 "Return the command indicator prompt as a string.
679 This only has any meaning if the variable `erc-command-indicator' is non-nil."
680 (and erc-command-indicator
681 (let ((prompt (if (functionp erc-command-indicator)
682 (funcall erc-command-indicator)
683 erc-command-indicator)))
684 (if (> (length prompt) 0)
685 (concat prompt " ")
686 prompt))))
688 (defcustom erc-notice-prefix "*** "
689 "Prefix for all notices."
690 :group 'erc-display
691 :type 'string)
693 (defcustom erc-notice-highlight-type 'all
694 "Determines how to highlight notices.
695 See `erc-notice-prefix'.
697 The following values are allowed:
699 'prefix - highlight notice prefix only
700 'all - highlight the entire notice
702 Any other value disables notice's highlighting altogether."
703 :group 'erc-display
704 :type '(choice (const :tag "highlight notice prefix only" prefix)
705 (const :tag "highlight the entire notice" all)
706 (const :tag "don't highlight notices at all" nil)))
708 (defcustom erc-echo-notice-hook nil
709 "List of functions to call to echo a private notice.
710 Each function is called with four arguments, the string
711 to display, the parsed server message, the target buffer (or
712 nil), and the sender. The functions are called in order, until a
713 function evaluates to non-nil. These hooks are called after
714 those specified in `erc-echo-notice-always-hook'.
716 See also: `erc-echo-notice-always-hook',
717 `erc-echo-notice-in-default-buffer',
718 `erc-echo-notice-in-target-buffer',
719 `erc-echo-notice-in-minibuffer',
720 `erc-echo-notice-in-server-buffer',
721 `erc-echo-notice-in-active-non-server-buffer',
722 `erc-echo-notice-in-active-buffer',
723 `erc-echo-notice-in-user-buffers',
724 `erc-echo-notice-in-user-and-target-buffers',
725 `erc-echo-notice-in-first-user-buffer'"
726 :group 'erc-hooks
727 :type 'hook
728 :options '(erc-echo-notice-in-default-buffer
729 erc-echo-notice-in-target-buffer
730 erc-echo-notice-in-minibuffer
731 erc-echo-notice-in-server-buffer
732 erc-echo-notice-in-active-non-server-buffer
733 erc-echo-notice-in-active-buffer
734 erc-echo-notice-in-user-buffers
735 erc-echo-notice-in-user-and-target-buffers
736 erc-echo-notice-in-first-user-buffer))
738 (defcustom erc-echo-notice-always-hook
739 '(erc-echo-notice-in-default-buffer)
740 "List of functions to call to echo a private notice.
741 Each function is called with four arguments, the string
742 to display, the parsed server message, the target buffer (or
743 nil), and the sender. The functions are called in order, and all
744 functions are called. These hooks are called before those
745 specified in `erc-echo-notice-hook'.
747 See also: `erc-echo-notice-hook',
748 `erc-echo-notice-in-default-buffer',
749 `erc-echo-notice-in-target-buffer',
750 `erc-echo-notice-in-minibuffer',
751 `erc-echo-notice-in-server-buffer',
752 `erc-echo-notice-in-active-non-server-buffer',
753 `erc-echo-notice-in-active-buffer',
754 `erc-echo-notice-in-user-buffers',
755 `erc-echo-notice-in-user-and-target-buffers',
756 `erc-echo-notice-in-first-user-buffer'"
757 :group 'erc-hooks
758 :type 'hook
759 :options '(erc-echo-notice-in-default-buffer
760 erc-echo-notice-in-target-buffer
761 erc-echo-notice-in-minibuffer
762 erc-echo-notice-in-server-buffer
763 erc-echo-notice-in-active-non-server-buffer
764 erc-echo-notice-in-active-buffer
765 erc-echo-notice-in-user-buffers
766 erc-echo-notice-in-user-and-target-buffers
767 erc-echo-notice-in-first-user-buffer))
769 ;; other tunable parameters
771 (defcustom erc-whowas-on-nosuchnick nil
772 "If non-nil, do a whowas on a nick if no such nick."
773 :group 'erc
774 :type 'boolean)
776 (defcustom erc-verbose-server-ping nil
777 "If non-nil, show every time you get a PING or PONG from the server."
778 :group 'erc-paranoia
779 :type 'boolean)
781 (defcustom erc-public-away-p nil
782 "Let others know you are back when you are no longer marked away.
783 This happens in this form:
784 * <nick> is back (gone for <time>)
786 Many consider it impolite to do so automatically."
787 :group 'erc
788 :type 'boolean)
790 (defcustom erc-away-nickname nil
791 "The nickname to take when you are marked as being away."
792 :group 'erc
793 :type '(choice (const nil)
794 string))
796 (defcustom erc-paranoid nil
797 "If non-nil, then all incoming CTCP requests will be shown."
798 :group 'erc-paranoia
799 :type 'boolean)
801 (defcustom erc-disable-ctcp-replies nil
802 "Disable replies to CTCP requests that require a reply.
803 If non-nil, then all incoming CTCP requests that normally require
804 an automatic reply (like VERSION or PING) will be ignored. Good to
805 set if some hacker is trying to flood you away."
806 :group 'erc-paranoia
807 :type 'boolean)
809 (defcustom erc-anonymous-login t
810 "Be paranoid, don't give away your machine name."
811 :group 'erc-paranoia
812 :type 'boolean)
814 (defcustom erc-prompt-for-channel-key nil
815 "Prompt for channel key when using `erc-join-channel' interactively."
816 :group 'erc
817 :type 'boolean)
819 (defcustom erc-email-userid "user"
820 "Use this as your email user ID."
821 :group 'erc
822 :type 'string)
824 (defcustom erc-system-name nil
825 "Use this as the name of your system.
826 If nil, ERC will call `system-name' to get this information."
827 :group 'erc
828 :type '(choice (const :tag "Default system name" nil)
829 string))
831 (defcustom erc-ignore-list nil
832 "List of regexps matching user identifiers to ignore.
834 A user identifier has the form \"nick!login@host\". If an
835 identifier matches, the message from the person will not be
836 processed."
837 :group 'erc-ignore
838 :type '(repeat regexp))
839 (make-variable-buffer-local 'erc-ignore-list)
841 (defcustom erc-ignore-reply-list nil
842 "List of regexps matching user identifiers to ignore completely.
844 This differs from `erc-ignore-list' in that it also ignores any
845 messages directed at the user.
847 A user identifier has the form \"nick!login@host\".
849 If an identifier matches, or a message is addressed to a nick
850 whose identifier matches, the message will not be processed.
852 CAVEAT: ERC doesn't know about the user and host of anyone who
853 was already in the channel when you joined, but never said
854 anything, so it won't be able to match the user and host of those
855 people. You can update the ERC internal info using /WHO *."
856 :group 'erc-ignore
857 :type '(repeat regexp))
859 (defvar erc-flood-protect t
860 "If non-nil, flood protection is enabled.
861 Flooding is sending too much information to the server in too
862 short of an interval, which may cause the server to terminate the
863 connection.
865 See `erc-server-flood-margin' for other flood-related parameters.")
867 ;; Script parameters
869 (defcustom erc-startup-file-list
870 (list (concat erc-user-emacs-directory ".ercrc.el")
871 (concat erc-user-emacs-directory ".ercrc")
872 "~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
873 "List of files to try for a startup script.
874 The first existent and readable one will get executed.
876 If the filename ends with `.el' it is presumed to be an Emacs Lisp
877 script and it gets (load)ed. Otherwise it is treated as a bunch of
878 regular IRC commands."
879 :group 'erc-scripts
880 :type '(repeat file))
882 (defcustom erc-script-path nil
883 "List of directories to look for a script in /load command.
884 The script is first searched in the current directory, then in each
885 directory in the list."
886 :group 'erc-scripts
887 :type '(repeat directory))
889 (defcustom erc-script-echo t
890 "If non-nil, echo the IRC script commands locally."
891 :group 'erc-scripts
892 :type 'boolean)
894 (defvar erc-last-saved-position nil
895 "A marker containing the position the current buffer was last saved at.")
896 (make-variable-buffer-local 'erc-last-saved-position)
898 (defcustom erc-kill-buffer-on-part nil
899 "Kill the channel buffer on PART.
900 This variable should probably stay nil, as ERC can reuse buffers if
901 you rejoin them later."
902 :group 'erc-quit-and-part
903 :type 'boolean)
905 (defcustom erc-kill-queries-on-quit nil
906 "Kill all query (also channel) buffers of this server on QUIT.
907 See the variable `erc-kill-buffer-on-part' for details."
908 :group 'erc-quit-and-part
909 :type 'boolean)
911 (defcustom erc-kill-server-buffer-on-quit nil
912 "Kill the server buffer of the process on QUIT."
913 :group 'erc-quit-and-part
914 :type 'boolean)
916 (defcustom erc-quit-reason-various-alist nil
917 "Alist of possible arguments to the /quit command.
919 Each element has the form:
920 (REGEXP RESULT)
922 If REGEXP matches the argument to /quit, then its relevant RESULT
923 will be used. RESULT may be either a string, or a function. If
924 a function, it should return the quit message as a string.
926 If no elements match, then the empty string is used.
928 As an example:
929 (setq erc-quit-reason-various-alist
930 '((\"xmms\" dme:now-playing)
931 (\"version\" erc-quit-reason-normal)
932 (\"home\" \"Gone home !\")
933 (\"^$\" \"Default Reason\")))
934 If the user types \"/quit home\", then \"Gone home !\" will be used
935 as the quit message."
936 :group 'erc-quit-and-part
937 :type '(repeat (list regexp (choice (string) (function)))))
939 (defcustom erc-part-reason-various-alist nil
940 "Alist of possible arguments to the /part command.
942 Each element has the form:
943 (REGEXP RESULT)
945 If REGEXP matches the argument to /part, then its relevant RESULT
946 will be used. RESULT may be either a string, or a function. If
947 a function, it should return the part message as a string.
949 If no elements match, then the empty string is used.
951 As an example:
952 (setq erc-part-reason-various-alist
953 '((\"xmms\" dme:now-playing)
954 (\"version\" erc-part-reason-normal)
955 (\"home\" \"Gone home !\")
956 (\"^$\" \"Default Reason\")))
957 If the user types \"/part home\", then \"Gone home !\" will be used
958 as the part message."
959 :group 'erc-quit-and-part
960 :type '(repeat (list regexp (choice (string) (function)))))
962 (defcustom erc-quit-reason 'erc-quit-reason-normal
963 "A function which returns the reason for quitting.
965 The function is passed a single argument, the string typed by the
966 user after \"/quit\"."
967 :group 'erc-quit-and-part
968 :type '(choice (const erc-quit-reason-normal)
969 (const erc-quit-reason-various)
970 (symbol)))
972 (defcustom erc-part-reason 'erc-part-reason-normal
973 "A function which returns the reason for parting a channel.
975 The function is passed a single argument, the string typed by the
976 user after \"/PART\"."
977 :group 'erc-quit-and-part
978 :type '(choice (const erc-part-reason-normal)
979 (const erc-part-reason-various)
980 (symbol)))
982 (defvar erc-grab-buffer-name "*erc-grab*"
983 "The name of the buffer created by `erc-grab-region'.")
985 ;; variables available for IRC scripts
987 (defvar erc-user-information "ERC User"
988 "USER_INFORMATION IRC variable.")
990 ;; Hooks
992 (defgroup erc-hooks nil
993 "Hook variables for fancy customizations of ERC."
994 :group 'erc)
996 (defcustom erc-mode-hook nil
997 "Hook run after `erc-mode' setup is finished."
998 :group 'erc-hooks
999 :type 'hook
1000 :options '(erc-add-scroll-to-bottom))
1002 (defcustom erc-timer-hook nil
1003 "Put functions which should get called more or less periodically here.
1004 The idea is that servers always play ping pong with the client, and so there
1005 is no need for any idle-timer games with Emacs."
1006 :group 'erc-hooks
1007 :type 'hook)
1009 (defcustom erc-insert-pre-hook nil
1010 "Hook called first when some text is inserted through `erc-display-line'.
1011 It gets called with one argument, STRING.
1012 To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
1013 Filtering functions can set `erc-insert-this' to nil to avoid
1014 display of that particular string at all."
1015 :group 'erc-hooks
1016 :type 'hook)
1018 (defcustom erc-send-pre-hook nil
1019 "Hook called first when some text is sent through `erc-send-current-line'.
1020 It gets called with one argument, STRING.
1022 To change the text that will be sent, set the variable `str' which is
1023 used in `erc-send-current-line'.
1025 To change the text inserted into the buffer without changing the text
1026 that will be sent, use `erc-send-modify-hook' instead.
1028 Filtering functions can set `erc-send-this' to nil to avoid sending of
1029 that particular string at all and `erc-insert-this' to prevent
1030 inserting that particular string into the buffer.
1032 Note that it's useless to set `erc-send-this' to nil and
1033 `erc-insert-this' to t. ERC is sane enough to not insert the text
1034 anyway."
1035 :group 'erc-hooks
1036 :type 'hook)
1038 (defvar erc-insert-this t
1039 "Insert the text into the target buffer or not.
1040 Functions on `erc-insert-pre-hook' can set this variable to nil
1041 if they wish to avoid insertion of a particular string.")
1043 (defvar erc-send-this t
1044 "Send the text to the target or not.
1045 Functions on `erc-send-pre-hook' can set this variable to nil
1046 if they wish to avoid sending of a particular string.")
1048 (defcustom erc-insert-modify-hook ()
1049 "Insertion hook for functions that will change the text's appearance.
1050 This hook is called just after `erc-insert-pre-hook' when the value
1051 of `erc-insert-this' is t.
1052 While this hook is run, narrowing is in effect and `current-buffer' is
1053 the buffer where the text got inserted. One possible value to add here
1054 is `erc-fill'."
1055 :group 'erc-hooks
1056 :type 'hook)
1058 (defcustom erc-insert-post-hook nil
1059 "This hook is called just after `erc-insert-modify-hook'.
1060 At this point, all modifications from prior hook functions are done."
1061 :group 'erc-hooks
1062 :type 'hook
1063 :options '(erc-truncate-buffer
1064 erc-make-read-only
1065 erc-save-buffer-in-logs))
1067 (defcustom erc-send-modify-hook nil
1068 "Sending hook for functions that will change the text's appearance.
1069 This hook is called just after `erc-send-pre-hook' when the values
1070 of `erc-send-this' and `erc-insert-this' are both t.
1071 While this hook is run, narrowing is in effect and `current-buffer' is
1072 the buffer where the text got inserted.
1074 Note that no function in this hook can change the appearance of the
1075 text that is sent. Only changing the sent text's appearance on the
1076 sending user's screen is possible. One possible value to add here
1077 is `erc-fill'."
1078 :group 'erc-hooks
1079 :type 'hook)
1081 (defcustom erc-send-post-hook nil
1082 "This hook is called just after `erc-send-modify-hook'.
1083 At this point, all modifications from prior hook functions are done.
1084 NOTE: The functions on this hook are called _before_ sending a command
1085 to the server.
1087 This function is called with narrowing, ala `erc-send-modify-hook'."
1088 :group 'erc-hooks
1089 :type 'hook
1090 :options '(erc-make-read-only))
1092 (defcustom erc-send-completed-hook
1093 (when (fboundp 'emacspeak-auditory-icon)
1094 (list (byte-compile
1095 (lambda (_str)
1096 (emacspeak-auditory-icon 'select-object)))))
1097 "Hook called after a message has been parsed by ERC.
1099 The single argument to the functions is the unmodified string
1100 which the local user typed."
1101 :group 'erc-hooks
1102 :type 'hook)
1103 ;; mode-specific tables
1105 (defvar erc-mode-syntax-table
1106 (let ((syntax-table (make-syntax-table)))
1107 (modify-syntax-entry ?\" ". " syntax-table)
1108 (modify-syntax-entry ?\\ ". " syntax-table)
1109 (modify-syntax-entry ?' "w " syntax-table)
1110 ;; Make dabbrev-expand useful for nick names
1111 (modify-syntax-entry ?< "." syntax-table)
1112 (modify-syntax-entry ?> "." syntax-table)
1113 syntax-table)
1114 "Syntax table used while in ERC mode.")
1116 (defvar erc-mode-abbrev-table nil
1117 "Abbrev table used while in ERC mode.")
1118 (define-abbrev-table 'erc-mode-abbrev-table ())
1120 (defvar erc-mode-map
1121 (let ((map (make-sparse-keymap)))
1122 (define-key map "\C-m" 'erc-send-current-line)
1123 (define-key map "\C-a" 'erc-bol)
1124 (define-key map [home] 'erc-bol)
1125 (define-key map "\C-c\C-a" 'erc-bol)
1126 (define-key map "\C-c\C-b" 'erc-switch-to-buffer)
1127 (define-key map "\C-c\C-c" 'erc-toggle-interpret-controls)
1128 (define-key map "\C-c\C-d" 'erc-input-action)
1129 (define-key map "\C-c\C-e" 'erc-toggle-ctcp-autoresponse)
1130 (define-key map "\C-c\C-f" 'erc-toggle-flood-control)
1131 (define-key map "\C-c\C-i" 'erc-invite-only-mode)
1132 (define-key map "\C-c\C-j" 'erc-join-channel)
1133 (define-key map "\C-c\C-n" 'erc-channel-names)
1134 (define-key map "\C-c\C-o" 'erc-get-channel-mode-from-keypress)
1135 (define-key map "\C-c\C-p" 'erc-part-from-channel)
1136 (define-key map "\C-c\C-q" 'erc-quit-server)
1137 (define-key map "\C-c\C-r" 'erc-remove-text-properties-region)
1138 (define-key map "\C-c\C-t" 'erc-set-topic)
1139 (define-key map "\C-c\C-u" 'erc-kill-input)
1140 (define-key map "\C-c\C-x" 'erc-quit-server)
1141 (define-key map "\M-\t" 'ispell-complete-word)
1142 (define-key map "\t" 'completion-at-point)
1144 ;; Suppress `font-lock-fontify-block' key binding since it
1145 ;; destroys face properties.
1146 (define-key map [remap font-lock-fontify-block] 'undefined)
1148 map)
1149 "ERC keymap.")
1151 ;; Faces
1153 ; Honestly, I have a horrible sense of color and the "defaults" below
1154 ; are supposed to be really bad. But colors ARE required in IRC to
1155 ; convey different parts of conversation. If you think you know better
1156 ; defaults - send them to me.
1158 ;; Now colors are a bit nicer, at least to my eyes.
1159 ;; You may still want to change them to better fit your background.-- S.B.
1161 (defgroup erc-faces nil
1162 "Faces for ERC."
1163 :group 'erc)
1165 ;; FIXME faces should not end in "-face".
1166 (defface erc-default-face '((t))
1167 "ERC default face."
1168 :group 'erc-faces)
1170 (defface erc-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
1171 "ERC face used for user mode prefix."
1172 :group 'erc-faces)
1174 (defface erc-my-nick-prefix-face '((t :inherit erc-nick-default-face :weight bold))
1175 "ERC face used for my user mode prefix."
1176 :group 'erc-faces)
1178 (defface erc-direct-msg-face '((t :foreground "IndianRed"))
1179 "ERC face used for messages you receive in the main erc buffer."
1180 :group 'erc-faces)
1182 (defface erc-header-line
1183 '((t :foreground "grey20" :background "grey90"))
1184 "ERC face used for the header line.
1186 This will only be used if `erc-header-line-face-method' is non-nil."
1187 :group 'erc-faces)
1189 (defface erc-input-face '((t :foreground "brown"))
1190 "ERC face used for your input."
1191 :group 'erc-faces)
1193 (defface erc-prompt-face
1194 '((t :weight bold :foreground "Black" :background "lightBlue2"))
1195 "ERC face for the prompt."
1196 :group 'erc-faces)
1198 (defface erc-command-indicator-face
1199 '((t :weight bold))
1200 "ERC face for the command indicator.
1201 See the variable `erc-command-indicator'."
1202 :group 'erc-faces)
1204 (defface erc-notice-face
1205 '((default :weight bold)
1206 (((class color) (min-colors 88)) :foreground "SlateBlue")
1207 (t :foreground "blue"))
1208 "ERC face for notices."
1209 :group 'erc-faces)
1211 (defface erc-action-face '((t :weight bold))
1212 "ERC face for actions generated by /ME."
1213 :group 'erc-faces)
1215 (defface erc-error-face '((t :foreground "red"))
1216 "ERC face for errors."
1217 :group 'erc-faces)
1219 ;; same default color as `erc-input-face'
1220 (defface erc-my-nick-face '((t :weight bold :foreground "brown"))
1221 "ERC face for your current nickname in messages sent by you.
1222 See also `erc-show-my-nick'."
1223 :group 'erc-faces)
1225 (defface erc-nick-default-face '((t :weight bold))
1226 "ERC nickname default face."
1227 :group 'erc-faces)
1229 (defface erc-nick-msg-face '((t :weight bold :foreground "IndianRed"))
1230 "ERC nickname face for private messages."
1231 :group 'erc-faces)
1233 ;; Debugging support
1235 (defvar erc-log-p nil
1236 "When set to t, generate debug messages in a separate debug buffer.")
1238 (defvar erc-debug-log-file (expand-file-name "ERC.debug")
1239 "Debug log file name.")
1241 (defvar erc-dbuf nil)
1242 (make-variable-buffer-local 'erc-dbuf)
1244 (defmacro define-erc-module (name alias doc enable-body disable-body
1245 &optional local-p)
1246 "Define a new minor mode using ERC conventions.
1247 Symbol NAME is the name of the module.
1248 Symbol ALIAS is the alias to use, or nil.
1249 DOC is the documentation string to use for the minor mode.
1250 ENABLE-BODY is a list of expressions used to enable the mode.
1251 DISABLE-BODY is a list of expressions used to disable the mode.
1252 If LOCAL-P is non-nil, the mode will be created as a buffer-local
1253 mode, rather than a global one.
1255 This will define a minor mode called erc-NAME-mode, possibly
1256 an alias erc-ALIAS-mode, as well as the helper functions
1257 erc-NAME-enable, and erc-NAME-disable.
1259 Example:
1261 ;;;###autoload (autoload 'erc-replace-mode \"erc-replace\")
1262 (define-erc-module replace nil
1263 \"This mode replaces incoming text according to `erc-replace-alist'.\"
1264 ((add-hook 'erc-insert-modify-hook
1265 'erc-replace-insert))
1266 ((remove-hook 'erc-insert-modify-hook
1267 'erc-replace-insert)))"
1268 (declare (doc-string 3))
1269 (let* ((sn (symbol-name name))
1270 (mode (intern (format "erc-%s-mode" (downcase sn))))
1271 (group (intern (format "erc-%s" (downcase sn))))
1272 (enable (intern (format "erc-%s-enable" (downcase sn))))
1273 (disable (intern (format "erc-%s-disable" (downcase sn)))))
1274 `(progn
1275 (erc-define-minor-mode
1276 ,mode
1277 ,(format "Toggle ERC %S mode.
1278 With a prefix argument ARG, enable %s if ARG is positive,
1279 and disable it otherwise. If called from Lisp, enable the mode
1280 if ARG is omitted or nil.
1281 %s" name name doc)
1282 nil nil nil
1283 :global ,(not local-p) :group (quote ,group)
1284 (if ,mode
1285 (,enable)
1286 (,disable)))
1287 (defun ,enable ()
1288 ,(format "Enable ERC %S mode."
1289 name)
1290 (interactive)
1291 (add-to-list 'erc-modules (quote ,name))
1292 (setq ,mode t)
1293 ,@enable-body)
1294 (defun ,disable ()
1295 ,(format "Disable ERC %S mode."
1296 name)
1297 (interactive)
1298 (setq erc-modules (delq (quote ,name) erc-modules))
1299 (setq ,mode nil)
1300 ,@disable-body)
1301 ,(when (and alias (not (eq name alias)))
1302 `(defalias
1303 (quote
1304 ,(intern
1305 (format "erc-%s-mode"
1306 (downcase (symbol-name alias)))))
1307 (quote
1308 ,mode)))
1309 ;; For find-function and find-variable.
1310 (put ',mode 'definition-name ',name)
1311 (put ',enable 'definition-name ',name)
1312 (put ',disable 'definition-name ',name))))
1314 (defun erc-once-with-server-event (event f)
1315 "Run function F the next time EVENT occurs in the `current-buffer'.
1317 You should make sure that `current-buffer' is a server buffer.
1319 This function temporarily adds a function to EVENT's hook to call F with
1320 two arguments (`proc' and `parsed'). After F is called, the function is
1321 removed from EVENT's hook. F should return either nil
1322 or t, where nil indicates that the other functions on EVENT's hook
1323 should be run too, and t indicates that other functions should
1324 not be run.
1326 Please be sure to use this function in server-buffers. In
1327 channel-buffers it may not work at all, as it uses the LOCAL
1328 argument of `add-hook' and `remove-hook' to ensure multiserver
1329 capabilities."
1330 (unless (erc-server-buffer-p)
1331 (error
1332 "You should only run `erc-once-with-server-event' in a server buffer"))
1333 (let ((fun (make-symbol "fun"))
1334 (hook (erc-get-hook event)))
1335 (put fun 'erc-original-buffer (current-buffer))
1336 (fset fun (lambda (proc parsed)
1337 (with-current-buffer (get fun 'erc-original-buffer)
1338 (remove-hook hook fun t))
1339 (fmakunbound fun)
1340 (funcall f proc parsed)))
1341 (add-hook hook fun nil t)
1342 fun))
1344 (defsubst erc-log (string)
1345 "Logs STRING if logging is on (see `erc-log-p')."
1346 (when erc-log-p
1347 (erc-log-aux string)))
1349 (defun erc-server-buffer ()
1350 "Return the server buffer for the current buffer's process.
1351 The buffer-local variable `erc-server-process' is used to find
1352 the process buffer."
1353 (and (erc-server-buffer-live-p)
1354 (process-buffer erc-server-process)))
1356 (defun erc-server-buffer-live-p ()
1357 "Return t if the server buffer has not been killed."
1358 (and (processp erc-server-process)
1359 (buffer-live-p (process-buffer erc-server-process))))
1361 (defun erc-server-buffer-p (&optional buffer)
1362 "Return non-nil if argument BUFFER is an ERC server buffer.
1364 If BUFFER is nil, the current buffer is used."
1365 (with-current-buffer (or buffer (current-buffer))
1366 (and (eq major-mode 'erc-mode)
1367 (null (erc-default-target)))))
1369 (defun erc-open-server-buffer-p (&optional buffer)
1370 "Return non-nil if argument BUFFER is an ERC server buffer that
1371 has an open IRC process.
1373 If BUFFER is nil, the current buffer is used."
1374 (and (erc-server-buffer-p buffer)
1375 (erc-server-process-alive buffer)))
1377 (defun erc-query-buffer-p (&optional buffer)
1378 "Return non-nil if BUFFER is an ERC query buffer.
1379 If BUFFER is nil, the current buffer is used."
1380 (with-current-buffer (or buffer (current-buffer))
1381 (let ((target (erc-default-target)))
1382 (and (eq major-mode 'erc-mode)
1383 target
1384 (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
1386 (defun erc-ison-p (nick)
1387 "Return non-nil if NICK is online."
1388 (interactive "sNick: ")
1389 (erc-with-server-buffer
1390 (let ((erc-online-p 'unknown))
1391 (erc-once-with-server-event
1393 (lambda (_proc parsed)
1394 (let ((ison (split-string (aref parsed 3))))
1395 (setq erc-online-p (car (erc-member-ignore-case nick ison)))
1396 t)))
1397 (erc-server-send (format "ISON %s" nick))
1398 (while (eq erc-online-p 'unknown) (accept-process-output))
1399 (if (called-interactively-p 'interactive)
1400 (message "%s is %sonline"
1401 (or erc-online-p nick)
1402 (if erc-online-p "" "not "))
1403 erc-online-p))))
1405 (defun erc-log-aux (string)
1406 "Do the debug logging of STRING."
1407 (let ((cb (current-buffer))
1408 (point 1)
1409 (was-eob nil)
1410 (session-buffer (erc-server-buffer)))
1411 (if session-buffer
1412 (progn
1413 (set-buffer session-buffer)
1414 (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
1415 (progn
1416 (setq erc-dbuf (get-buffer-create
1417 (concat "*ERC-DEBUG: "
1418 erc-session-server "*")))))
1419 (set-buffer erc-dbuf)
1420 (setq point (point))
1421 (setq was-eob (eobp))
1422 (goto-char (point-max))
1423 (insert (concat "** " string "\n"))
1424 (if was-eob (goto-char (point-max))
1425 (goto-char point))
1426 (set-buffer cb))
1427 (message "ERC: ** %s" string))))
1429 ;; Last active buffer, to print server messages in the right place
1431 (defvar erc-active-buffer nil
1432 "The current active buffer, the one where the user typed the last command.
1433 Defaults to the server buffer, and should only be set in the
1434 server buffer.")
1435 (make-variable-buffer-local 'erc-active-buffer)
1437 (defun erc-active-buffer ()
1438 "Return the value of `erc-active-buffer' for the current server.
1439 Defaults to the server buffer."
1440 (erc-with-server-buffer
1441 (if (buffer-live-p erc-active-buffer)
1442 erc-active-buffer
1443 (setq erc-active-buffer (current-buffer)))))
1445 (defun erc-set-active-buffer (buffer)
1446 "Set the value of `erc-active-buffer' to BUFFER."
1447 (cond ((erc-server-buffer)
1448 (with-current-buffer (erc-server-buffer)
1449 (setq erc-active-buffer buffer)))
1450 (t (setq erc-active-buffer buffer))))
1452 ;; Mode activation routines
1454 (define-derived-mode erc-mode fundamental-mode "ERC"
1455 "Major mode for Emacs IRC."
1456 (setq local-abbrev-table erc-mode-abbrev-table)
1457 (when (boundp 'next-line-add-newlines)
1458 (set (make-local-variable 'next-line-add-newlines) nil))
1459 (setq line-move-ignore-invisible t)
1460 (set (make-local-variable 'paragraph-separate)
1461 (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
1462 (set (make-local-variable 'paragraph-start)
1463 (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
1464 (add-hook 'completion-at-point-functions 'erc-complete-word-at-point nil t))
1466 ;; activation
1468 (defconst erc-default-server "irc.freenode.net"
1469 "IRC server to use if it cannot be detected otherwise.")
1471 (defconst erc-default-port 6667
1472 "IRC port to use if it cannot be detected otherwise.")
1474 (defcustom erc-join-buffer 'buffer
1475 "Determines how to display a newly created IRC buffer.
1477 The available choices are:
1479 'window - in another window,
1480 'window-noselect - in another window, but don't select that one,
1481 'frame - in another frame,
1482 'bury - bury it in a new buffer,
1483 'buffer - in place of the current buffer,
1484 any other value - in place of the current buffer."
1485 :group 'erc-buffers
1486 :type '(choice (const :tag "Split window and select" window)
1487 (const :tag "Split window, don't select" window-noselect)
1488 (const :tag "New frame" frame)
1489 (const :tag "Bury in new buffer" bury)
1490 (const :tag "Use current buffer" buffer)
1491 (const :tag "Use current buffer" t)))
1493 (defcustom erc-frame-alist nil
1494 "Alist of frame parameters for creating erc frames.
1495 A value of nil means to use `default-frame-alist'."
1496 :group 'erc-buffers
1497 :type '(repeat (cons :format "%v"
1498 (symbol :tag "Parameter")
1499 (sexp :tag "Value"))))
1501 (defcustom erc-frame-dedicated-flag nil
1502 "Non-nil means the erc frames are dedicated to that buffer.
1503 This only has effect when `erc-join-buffer' is set to `frame'."
1504 :group 'erc-buffers
1505 :type 'boolean)
1507 (defcustom erc-reuse-frames t
1508 "Determines whether new frames are always created.
1509 Non-nil means that a new frame is not created to display an ERC
1510 buffer if there is already a window displaying it. This only has
1511 effect when `erc-join-buffer' is set to `frame'."
1512 :group 'erc-buffers
1513 :type 'boolean)
1515 (defun erc-channel-p (channel)
1516 "Return non-nil if CHANNEL seems to be an IRC channel name."
1517 (cond ((stringp channel)
1518 (memq (aref channel 0) '(?# ?& ?+ ?!)))
1519 ((and (bufferp channel) (buffer-live-p channel))
1520 (with-current-buffer channel
1521 (erc-channel-p (erc-default-target))))
1522 (t nil)))
1524 (defcustom erc-reuse-buffers t
1525 "If nil, create new buffers on joining a channel/query.
1526 If non-nil, a new buffer will only be created when you join
1527 channels with same names on different servers, or have query buffers
1528 open with nicks of the same name on different servers. Otherwise,
1529 the existing buffers will be reused."
1530 :group 'erc-buffers
1531 :type 'boolean)
1533 (defun erc-normalize-port (port)
1534 "Normalize the port specification PORT to integer form.
1535 PORT may be an integer, a string or a symbol. If it is a string or a
1536 symbol, it may have these values:
1537 * irc -> 194
1538 * ircs -> 994
1539 * ircd -> 6667
1540 * ircd-dalnet -> 7000"
1541 (cond
1542 ((symbolp port)
1543 (erc-normalize-port (symbol-name port)))
1544 ((stringp port)
1545 (let ((port-nr (string-to-number port)))
1546 (cond
1547 ((> port-nr 0)
1548 port-nr)
1549 ((string-equal port "irc")
1550 194)
1551 ((string-equal port "ircs")
1552 994)
1553 ((string-equal port "ircd")
1554 6667)
1555 ((string-equal port "ircd-dalnet")
1556 7000)
1558 nil))))
1559 ((numberp port)
1560 port)
1562 nil)))
1564 (defun erc-port-equal (a b)
1565 "Check whether ports A and B are equal."
1566 (= (erc-normalize-port a) (erc-normalize-port b)))
1568 (defun erc-generate-new-buffer-name (server port target)
1569 "Create a new buffer name based on the arguments."
1570 (when (numberp port) (setq port (number-to-string port)))
1571 (let ((buf-name (or target
1572 (or (let ((name (concat server ":" port)))
1573 (when (> (length name) 1)
1574 name))
1575 ;; This fallback should in fact never happen
1576 "*erc-server-buffer*")))
1577 buffer-name)
1578 ;; Reuse existing buffers, but not if the buffer is a connected server
1579 ;; buffer and not if its associated with a different server than the
1580 ;; current ERC buffer.
1581 ;; if buf-name is taken by a different connection (or by something !erc)
1582 ;; then see if "buf-name/server" meets the same criteria
1583 (dolist (candidate (list buf-name (concat buf-name "/" server)))
1584 (if (and (not buffer-name)
1585 erc-reuse-buffers
1586 (get-buffer candidate)
1587 (or target
1588 (with-current-buffer (get-buffer candidate)
1589 (and (erc-server-buffer-p)
1590 (not (erc-server-process-alive)))))
1591 (with-current-buffer (get-buffer candidate)
1592 (and (string= erc-session-server server)
1593 (erc-port-equal erc-session-port port))))
1594 (setq buffer-name candidate)))
1595 ;; if buffer-name is unset, neither candidate worked out for us,
1596 ;; fallback to the old <N> uniquification method:
1597 (or buffer-name (generate-new-buffer-name buf-name)) ))
1599 (defun erc-get-buffer-create (server port target)
1600 "Create a new buffer based on the arguments."
1601 (get-buffer-create (erc-generate-new-buffer-name server port target)))
1604 (defun erc-member-ignore-case (string list)
1605 "Return non-nil if STRING is a member of LIST.
1607 All strings are compared according to IRC protocol case rules, see
1608 `erc-downcase'."
1609 (setq string (erc-downcase string))
1610 (catch 'result
1611 (while list
1612 (if (string= string (erc-downcase (car list)))
1613 (throw 'result list)
1614 (setq list (cdr list))))))
1616 (defmacro erc-with-buffer (spec &rest body)
1617 "Execute BODY in the buffer associated with SPEC.
1619 SPEC should have the form
1621 (TARGET [PROCESS])
1623 If TARGET is a buffer, use it. Otherwise, use the buffer
1624 matching TARGET in the process specified by PROCESS.
1626 If PROCESS is nil, use the current `erc-server-process'.
1627 See `erc-get-buffer' for details.
1629 See also `with-current-buffer'.
1631 \(fn (TARGET [PROCESS]) BODY...)"
1632 (declare (indent 1) (debug ((form &optional form) body)))
1633 (let ((buf (make-symbol "buf"))
1634 (proc (make-symbol "proc"))
1635 (target (make-symbol "target"))
1636 (process (make-symbol "process")))
1637 `(let* ((,target ,(car spec))
1638 (,process ,(cadr spec))
1639 (,buf (if (bufferp ,target)
1640 ,target
1641 (let ((,proc (or ,process
1642 (and (processp erc-server-process)
1643 erc-server-process))))
1644 (if (and ,target ,proc)
1645 (erc-get-buffer ,target ,proc))))))
1646 (when (buffer-live-p ,buf)
1647 (with-current-buffer ,buf
1648 ,@body)))))
1650 (defun erc-get-buffer (target &optional proc)
1651 "Return the buffer matching TARGET in the process PROC.
1652 If PROC is not supplied, all processes are searched."
1653 (let ((downcased-target (erc-downcase target)))
1654 (catch 'buffer
1655 (erc-buffer-filter
1656 (lambda ()
1657 (let ((current (erc-default-target)))
1658 (and (stringp current)
1659 (string-equal downcased-target (erc-downcase current))
1660 (throw 'buffer (current-buffer)))))
1661 proc))))
1663 (defun erc--buffer-p (buf predicate proc)
1664 (with-current-buffer buf
1665 (and (derived-mode-p 'erc-mode)
1666 (or (not proc)
1667 (eq proc erc-server-process))
1668 (funcall predicate)
1669 buf)))
1671 (defun erc-buffer-filter (predicate &optional proc)
1672 "Return a list of `erc-mode' buffers matching certain criteria.
1673 PREDICATE is a function executed with each buffer, if it returns t, that buffer
1674 is considered a valid match.
1676 PROC is either an `erc-server-process', identifying a certain
1677 server connection, or nil which means all open connections."
1678 (save-excursion
1679 (delq
1681 (mapcar (lambda (buf)
1682 (when (buffer-live-p buf)
1683 (erc--buffer-p buf predicate proc)))
1684 (buffer-list)))))
1686 (defun erc-buffer-list (&optional predicate proc)
1687 "Return a list of ERC buffers.
1688 PREDICATE is a function which executes with every buffer satisfying
1689 the predicate. If PREDICATE is passed as nil, return a list of all ERC
1690 buffers. If PROC is given, the buffers local variable `erc-server-process'
1691 needs to match PROC."
1692 (unless predicate
1693 (setq predicate (lambda () t)))
1694 (erc-buffer-filter predicate proc))
1696 (defmacro erc-with-all-buffers-of-server (process pred &rest forms)
1697 "Execute FORMS in all buffers which have same process as this server.
1698 FORMS will be evaluated in all buffers having the process PROCESS and
1699 where PRED matches or in all buffers of the server process if PRED is
1700 nil."
1701 (declare (indent 1) (debug (form form body)))
1702 ;; Make the evaluation have the correct order
1703 (let ((pre (make-symbol "pre"))
1704 (pro (make-symbol "pro")))
1705 `(let* ((,pro ,process)
1706 (,pre ,pred)
1707 (res (mapcar (lambda (buffer)
1708 (with-current-buffer buffer
1709 ,@forms))
1710 (erc-buffer-list ,pre
1711 ,pro))))
1712 ;; Silence the byte-compiler by binding the result of mapcar to
1713 ;; a variable.
1714 (ignore res)
1715 res)))
1717 (define-obsolete-function-alias 'erc-iswitchb 'erc-switch-to-buffer "25.1")
1718 (defun erc-switch-to-buffer (&optional arg)
1719 "Prompt for a ERC buffer to switch to.
1720 When invoked with prefix argument, use all erc buffers. Without prefix
1721 ARG, allow only buffers related to same session server.
1722 If `erc-track-mode' is in enabled, put the last element of
1723 `erc-modified-channels-alist' in front of the buffer list."
1724 (interactive "P")
1725 (switch-to-buffer
1726 (read-buffer "Switch to ERC buffer: "
1727 (when (boundp 'erc-modified-channels-alist)
1728 (buffer-name (caar (last erc-modified-channels-alist))))
1730 ;; Only allow ERC buffers in the same session.
1731 (let ((proc (unless arg erc-server-process)))
1732 (lambda (bufname)
1733 (let ((buf (if (consp bufname)
1734 (cdr bufname) (get-buffer bufname))))
1735 (when buf
1736 (erc--buffer-p buf (lambda () t) proc)
1737 (with-current-buffer buf
1738 (and (derived-mode-p 'erc-mode)
1739 (or (null proc)
1740 (eq proc erc-server-process)))))))))))
1742 (defun erc-channel-list (proc)
1743 "Return a list of channel buffers.
1744 PROC is the process for the server connection. If PROC is nil, return
1745 all channel buffers on all servers."
1746 (erc-buffer-filter
1747 (lambda ()
1748 (and (erc-default-target)
1749 (erc-channel-p (erc-default-target))))
1750 proc))
1752 (defun erc-buffer-list-with-nick (nick proc)
1753 "Return buffers containing NICK in the `erc-channel-users' list."
1754 (with-current-buffer (process-buffer proc)
1755 (let ((user (gethash (erc-downcase nick) erc-server-users)))
1756 (if user
1757 (erc-server-user-buffers user)
1758 nil))))
1760 ;; Some local variables
1762 (defvar erc-default-recipients nil
1763 "List of default recipients of the current buffer.")
1764 (make-variable-buffer-local 'erc-default-recipients)
1766 (defvar erc-session-user-full-name nil
1767 "Full name of the user on the current server.")
1768 (make-variable-buffer-local 'erc-session-user-full-name)
1770 (defvar erc-channel-user-limit nil
1771 "Limit of users per channel.")
1772 (make-variable-buffer-local 'erc-channel-user-limit)
1774 (defvar erc-channel-key nil
1775 "Key needed to join channel.")
1776 (make-variable-buffer-local 'erc-channel-key)
1778 (defvar erc-invitation nil
1779 "Last invitation channel.")
1780 (make-variable-buffer-local 'erc-invitation)
1782 (defvar erc-away nil
1783 "Non-nil indicates that we are away.
1785 Use `erc-away-time' to access this if you might be in a channel
1786 buffer rather than a server buffer.")
1787 (make-variable-buffer-local 'erc-away)
1789 (defvar erc-channel-list nil
1790 "Server channel list.")
1791 (make-variable-buffer-local 'erc-channel-list)
1793 (defvar erc-bad-nick nil
1794 "Non-nil indicates that we got a `nick in use' error while connecting.")
1795 (make-variable-buffer-local 'erc-bad-nick)
1797 (defvar erc-logged-in nil
1798 "Non-nil indicates that we are logged in.")
1799 (make-variable-buffer-local 'erc-logged-in)
1801 (defvar erc-default-nicks nil
1802 "The local copy of `erc-nick' - the list of nicks to choose from.")
1803 (make-variable-buffer-local 'erc-default-nicks)
1805 (defvar erc-nick-change-attempt-count 0
1806 "Used to keep track of how many times an attempt at changing nick is made.")
1807 (make-variable-buffer-local 'erc-nick-change-attempt-count)
1809 (defun erc-migrate-modules (mods)
1810 "Migrate old names of ERC modules to new ones."
1811 ;; modify `transforms' to specify what needs to be changed
1812 ;; each item is in the format '(old . new)
1813 (let ((transforms '((pcomplete . completion))))
1814 (erc-delete-dups
1815 (mapcar (lambda (m) (or (cdr (assoc m transforms)) m))
1816 mods))))
1818 (defcustom erc-modules '(netsplit fill button match track completion readonly
1819 networks ring autojoin noncommands irccontrols
1820 move-to-prompt stamp menu list)
1821 "A list of modules which ERC should enable.
1822 If you set the value of this without using `customize' remember to call
1823 \(erc-update-modules) after you change it. When using `customize', modules
1824 removed from the list will be disabled."
1825 :get (lambda (sym)
1826 ;; replace outdated names with their newer equivalents
1827 (erc-migrate-modules (symbol-value sym)))
1828 :set (lambda (sym val)
1829 ;; disable modules which have just been removed
1830 (when (and (boundp 'erc-modules) erc-modules val)
1831 (dolist (module erc-modules)
1832 (unless (member module val)
1833 (let ((f (intern-soft (format "erc-%s-mode" module))))
1834 (when (and (fboundp f) (boundp f) (symbol-value f))
1835 (message "Disabling `erc-%s'" module)
1836 (funcall f 0))))))
1837 (set sym val)
1838 ;; this test is for the case where erc hasn't been loaded yet
1839 (when (fboundp 'erc-update-modules)
1840 (erc-update-modules)))
1841 :type
1842 '(set
1843 :greedy t
1844 (const :tag "autoaway: Set away status automatically" autoaway)
1845 (const :tag "autojoin: Join channels automatically" autojoin)
1846 (const :tag "button: Buttonize URLs, nicknames, and other text" button)
1847 (const :tag "capab: Mark unidentified users on servers supporting CAPAB"
1848 capab-identify)
1849 (const :tag "completion: Complete nicknames and commands (programmable)"
1850 completion)
1851 (const :tag "hecomplete: Complete nicknames and commands (obsolete, use \"completion\")" hecomplete)
1852 (const :tag "dcc: Provide Direct Client-to-Client support" dcc)
1853 (const :tag "fill: Wrap long lines" fill)
1854 (const :tag "identd: Launch an identd server on port 8113" identd)
1855 (const :tag "irccontrols: Highlight or remove IRC control characters"
1856 irccontrols)
1857 (const :tag "keep-place: Leave point above un-viewed text" keep-place)
1858 (const :tag "list: List channels in a separate buffer" list)
1859 (const :tag "log: Save buffers in logs" log)
1860 (const :tag "match: Highlight pals, fools, and other keywords" match)
1861 (const :tag "menu: Display a menu in ERC buffers" menu)
1862 (const :tag "move-to-prompt: Move to the prompt when typing text"
1863 move-to-prompt)
1864 (const :tag "netsplit: Detect netsplits" netsplit)
1865 (const :tag "networks: Provide data about IRC networks" networks)
1866 (const :tag "noncommands: Don't display non-IRC commands after evaluation"
1867 noncommands)
1868 (const :tag
1869 "notify: Notify when the online status of certain users changes"
1870 notify)
1871 (const :tag "notifications: Send notifications on PRIVMSG or nickname mentions"
1872 notifications)
1873 (const :tag "page: Process CTCP PAGE requests from IRC" page)
1874 (const :tag "readonly: Make displayed lines read-only" readonly)
1875 (const :tag "replace: Replace text in messages" replace)
1876 (const :tag "ring: Enable an input history" ring)
1877 (const :tag "scrolltobottom: Scroll to the bottom of the buffer"
1878 scrolltobottom)
1879 (const :tag "services: Identify to Nickserv (IRC Services) automatically"
1880 services)
1881 (const :tag "smiley: Convert smileys to pretty icons" smiley)
1882 (const :tag "sound: Play sounds when you receive CTCP SOUND requests"
1883 sound)
1884 (const :tag "stamp: Add timestamps to messages" stamp)
1885 (const :tag "spelling: Check spelling" spelling)
1886 (const :tag "track: Track channel activity in the mode-line" track)
1887 (const :tag "truncate: Truncate buffers to a certain size" truncate)
1888 (const :tag "unmorse: Translate morse code in messages" unmorse)
1889 (const :tag "xdcc: Act as an XDCC file-server" xdcc)
1890 (repeat :tag "Others" :inline t symbol))
1891 :group 'erc)
1893 (defun erc-update-modules ()
1894 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1895 (let (req)
1896 (dolist (mod erc-modules)
1897 (setq req (concat "erc-" (symbol-name mod)))
1898 (cond
1899 ;; yuck. perhaps we should bring the filenames into sync?
1900 ((string= req "erc-capab-identify")
1901 (setq req "erc-capab"))
1902 ((string= req "erc-completion")
1903 (setq req "erc-pcomplete"))
1904 ((string= req "erc-pcomplete")
1905 (setq mod 'completion))
1906 ((string= req "erc-autojoin")
1907 (setq req "erc-join")))
1908 (condition-case nil
1909 (require (intern req))
1910 (error nil))
1911 (let ((sym (intern-soft (concat "erc-" (symbol-name mod) "-mode"))))
1912 (if (fboundp sym)
1913 (funcall sym 1)
1914 (error "`%s' is not a known ERC module" mod))))))
1916 (defun erc-setup-buffer (buffer)
1917 "Consults `erc-join-buffer' to find out how to display `BUFFER'."
1918 (pcase erc-join-buffer
1919 (`window
1920 (if (active-minibuffer-window)
1921 (display-buffer buffer)
1922 (switch-to-buffer-other-window buffer)))
1923 (`window-noselect
1924 (display-buffer buffer))
1925 (`bury
1926 nil)
1927 (`frame
1928 (when (or (not erc-reuse-frames)
1929 (not (get-buffer-window buffer t)))
1930 (let ((frame (make-frame (or erc-frame-alist
1931 default-frame-alist))))
1932 (raise-frame frame)
1933 (select-frame frame))
1934 (switch-to-buffer buffer)
1935 (when erc-frame-dedicated-flag
1936 (set-window-dedicated-p (selected-window) t))))
1938 (if (active-minibuffer-window)
1939 (display-buffer buffer)
1940 (switch-to-buffer buffer)))))
1942 (defun erc-open (&optional server port nick full-name
1943 connect passwd tgt-list channel process)
1944 "Connect to SERVER on PORT as NICK with FULL-NAME.
1946 If CONNECT is non-nil, connect to the server. Otherwise assume
1947 already connected and just create a separate buffer for the new
1948 target CHANNEL.
1950 Use PASSWD as user password on the server. If TGT-LIST is
1951 non-nil, use it to initialize `erc-default-recipients'.
1953 Returns the buffer for the given server or channel."
1954 (let ((server-announced-name (when (and (boundp 'erc-session-server)
1955 (string= server erc-session-server))
1956 erc-server-announced-name))
1957 (connected-p (unless connect erc-server-connected))
1958 (buffer (erc-get-buffer-create server port channel))
1959 (old-buffer (current-buffer))
1960 old-point
1961 continued-session)
1962 (when connect (run-hook-with-args 'erc-before-connect server port nick))
1963 (erc-update-modules)
1964 (set-buffer buffer)
1965 (setq old-point (point))
1966 (erc-mode)
1967 (setq erc-server-announced-name server-announced-name)
1968 (setq erc-server-connected connected-p)
1969 ;; connection parameters
1970 (setq erc-server-process process)
1971 (setq erc-insert-marker (make-marker))
1972 (setq erc-input-marker (make-marker))
1973 ;; go to the end of the buffer and open a new line
1974 ;; (the buffer may have existed)
1975 (goto-char (point-max))
1976 (forward-line 0)
1977 (when (get-text-property (point) 'erc-prompt)
1978 (setq continued-session t)
1979 (set-marker erc-input-marker
1980 (or (next-single-property-change (point) 'erc-prompt)
1981 (point-max))))
1982 (unless continued-session
1983 (goto-char (point-max))
1984 (insert "\n"))
1985 (set-marker erc-insert-marker (point))
1986 ;; stack of default recipients
1987 (setq erc-default-recipients tgt-list)
1988 (setq erc-server-current-nick nil)
1989 ;; Initialize erc-server-users and erc-channel-users
1990 (if connect
1991 (progn ;; server buffer
1992 (setq erc-server-users
1993 (make-hash-table :test 'equal))
1994 (setq erc-channel-users nil))
1995 (progn ;; target buffer
1996 (setq erc-server-users nil)
1997 (setq erc-channel-users
1998 (make-hash-table :test 'equal))))
1999 ;; clear last incomplete line read
2000 (setq erc-server-filter-data nil)
2001 (setq erc-channel-topic "")
2002 ;; limit on the number of users on the channel (mode +l)
2003 (setq erc-channel-user-limit nil)
2004 (setq erc-channel-key nil)
2005 ;; last active buffer, defaults to this one
2006 (erc-set-active-buffer buffer)
2007 ;; last invitation channel
2008 (setq erc-invitation nil)
2009 ;; Server channel list
2010 (setq erc-channel-list ())
2011 ;; login-time 'nick in use' error
2012 (setq erc-bad-nick nil)
2013 ;; whether we have logged in
2014 (setq erc-logged-in nil)
2015 ;; The local copy of `erc-nick' - the list of nicks to choose
2016 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
2017 ;; password stuff
2018 (setq erc-session-password
2019 (or passwd
2020 (let ((secret
2021 (plist-get
2022 (nth 0
2023 (auth-source-search :host server
2024 :max 1
2025 :user nick
2026 :port port
2027 :require '(:secret)))
2028 :secret)))
2029 (if (functionp secret)
2030 (funcall secret)
2031 secret))))
2032 ;; debug output buffer
2033 (setq erc-dbuf
2034 (when erc-log-p
2035 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
2036 ;; set up prompt
2037 (unless continued-session
2038 (goto-char (point-max))
2039 (insert "\n"))
2040 (if continued-session
2041 (goto-char old-point)
2042 (set-marker erc-insert-marker (point))
2043 (erc-display-prompt)
2044 (goto-char (point-max)))
2046 (erc-determine-parameters server port nick full-name)
2048 ;; Saving log file on exit
2049 (run-hook-with-args 'erc-connect-pre-hook buffer)
2051 (when connect
2052 (erc-server-connect erc-session-server erc-session-port buffer))
2053 (erc-update-mode-line)
2055 ;; Now display the buffer in a window as per user wishes.
2056 (unless (eq buffer old-buffer)
2057 (when erc-log-p
2058 ;; we can't log to debug buffer, it may not exist yet
2059 (message "erc: old buffer %s, switching to %s"
2060 old-buffer buffer))
2061 (erc-setup-buffer buffer))
2063 buffer))
2065 (defun erc-initialize-log-marker (buffer)
2066 "Initialize the `erc-last-saved-position' marker to a sensible position.
2067 BUFFER is the current buffer."
2068 (with-current-buffer buffer
2069 (unless (markerp erc-last-saved-position)
2070 (setq erc-last-saved-position (make-marker))
2071 (move-marker erc-last-saved-position
2072 (1- (marker-position erc-insert-marker))))))
2074 ;; interactive startup
2076 (defvar erc-server-history-list nil
2077 "IRC server interactive selection history list.")
2079 (defvar erc-nick-history-list nil
2080 "Nickname interactive selection history list.")
2082 (defun erc-already-logged-in (server port nick)
2083 "Return the buffers corresponding to a NICK on PORT of a session SERVER.
2084 This is determined by looking for the appropriate buffer and checking
2085 whether the connection is still alive.
2086 If no buffer matches, return nil."
2087 (erc-buffer-list
2088 (lambda ()
2089 (and (erc-server-process-alive)
2090 (string= erc-session-server server)
2091 (erc-port-equal erc-session-port port)
2092 (erc-current-nick-p nick)))))
2094 (defcustom erc-before-connect nil
2095 "Hook called before connecting to a server.
2096 This hook gets executed before `erc' actually invokes `erc-mode'
2097 with your input data. The functions in here get called with three
2098 parameters, SERVER, PORT and NICK."
2099 :group 'erc-hooks
2100 :type 'hook)
2102 (defcustom erc-after-connect nil
2103 "Hook called after connecting to a server.
2104 This hook gets executed when an end of MOTD has been received. All
2105 functions in here get called with the parameters SERVER and NICK."
2106 :group 'erc-hooks
2107 :type 'hook)
2109 ;;;###autoload
2110 (defun erc-select-read-args ()
2111 "Prompt the user for values of nick, server, port, and password."
2112 (let (user-input server port nick passwd)
2113 (setq user-input (read-from-minibuffer
2114 "IRC server: "
2115 (erc-compute-server) nil nil 'erc-server-history-list))
2117 (if (string-match "\\(.*\\):\\(.*\\)\\'" user-input)
2118 (setq port (erc-string-to-port (match-string 2 user-input))
2119 user-input (match-string 1 user-input))
2120 (setq port
2121 (erc-string-to-port (read-from-minibuffer
2122 "IRC port: " (erc-port-to-string
2123 (erc-compute-port))))))
2125 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
2126 (setq nick (match-string 1 user-input)
2127 user-input (match-string 2 user-input))
2128 (setq nick
2129 (if (erc-already-logged-in server port nick)
2130 (read-from-minibuffer
2131 (erc-format-message 'nick-in-use ?n nick)
2132 nick
2133 nil nil 'erc-nick-history-list)
2134 (read-from-minibuffer
2135 "Nickname: " (erc-compute-nick nick)
2136 nil nil 'erc-nick-history-list))))
2138 (setq server user-input)
2140 (setq passwd (if erc-prompt-for-password
2141 (if (and erc-password
2142 (y-or-n-p "Use the default password? "))
2143 erc-password
2144 (read-passwd "Password: "))
2145 erc-password))
2146 (when (and passwd (string= "" passwd))
2147 (setq passwd nil))
2149 (while (erc-already-logged-in server port nick)
2150 ;; hmm, this is a problem when using multiple connections to a bnc
2151 ;; with the same nick. Currently this code prevents using more than one
2152 ;; bnc with the same nick. actually it would be nice to have
2153 ;; bncs transparent, so that erc-compute-buffer-name displays
2154 ;; the server one is connected to.
2155 (setq nick (read-from-minibuffer
2156 (erc-format-message 'nick-in-use ?n nick)
2157 nick
2158 nil nil 'erc-nick-history-list)))
2159 (list :server server :port port :nick nick :password passwd)))
2161 ;;;###autoload
2162 (cl-defun erc (&key (server (erc-compute-server))
2163 (port (erc-compute-port))
2164 (nick (erc-compute-nick))
2165 password
2166 (full-name (erc-compute-full-name)))
2167 "ERC is a powerful, modular, and extensible IRC client.
2168 This function is the main entry point for ERC.
2170 It permits you to select connection parameters, and then starts ERC.
2172 Non-interactively, it takes the keyword arguments
2173 (server (erc-compute-server))
2174 (port (erc-compute-port))
2175 (nick (erc-compute-nick))
2176 password
2177 (full-name (erc-compute-full-name)))
2179 That is, if called with
2181 (erc :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
2183 then the server and full-name will be set to those values, whereas
2184 `erc-compute-port', `erc-compute-nick' and `erc-compute-full-name' will
2185 be invoked for the values of the other parameters."
2186 (interactive (erc-select-read-args))
2187 (erc-open server port nick full-name t password))
2189 ;;;###autoload
2190 (defalias 'erc-select 'erc)
2191 (defalias 'erc-ssl 'erc-tls)
2193 ;;;###autoload
2194 (defun erc-tls (&rest r)
2195 "Interactively select TLS connection parameters and run ERC.
2196 Arguments are the same as for `erc'."
2197 (interactive (erc-select-read-args))
2198 (let ((erc-server-connect-function 'erc-open-tls-stream))
2199 (apply #'erc r)))
2201 (defun erc-open-tls-stream (name buffer host port)
2202 "Open an TLS stream to an IRC server.
2203 The process will be given the name NAME, its target buffer will be
2204 BUFFER. HOST and PORT specify the connection target."
2205 (open-network-stream name buffer host port
2206 :type 'tls))
2208 ;;; Displaying error messages
2210 (defun erc-error (&rest args)
2211 "Pass ARGS to `format', and display the result as an error message.
2212 If `debug-on-error' is set to non-nil, then throw a real error with this
2213 message instead, to make debugging easier."
2214 (if debug-on-error
2215 (apply #'error args)
2216 (apply #'message args)
2217 (beep)))
2219 ;;; Debugging the protocol
2221 (defvar erc-debug-irc-protocol nil
2222 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2224 The buffer is created if it doesn't exist.
2226 NOTE: If this variable is non-nil, and you kill the only
2227 visible \"*erc-protocol*\" buffer, it will be recreated shortly,
2228 but you won't see it.
2230 WARNING: Do not set this variable directly! Instead, use the
2231 function `erc-toggle-debug-irc-protocol' to toggle its value.")
2233 (declare-function erc-network-name "erc-networks" ())
2235 (defun erc-log-irc-protocol (string &optional outbound)
2236 "Append STRING to the buffer *erc-protocol*.
2238 This only has any effect if `erc-debug-irc-protocol' is non-nil.
2240 The buffer is created if it doesn't exist.
2242 If OUTBOUND is non-nil, STRING is being sent to the IRC server
2243 and appears in face `erc-input-face' in the buffer."
2244 (when erc-debug-irc-protocol
2245 (let ((network-name (or (ignore-errors (erc-network-name))
2246 "???")))
2247 (with-current-buffer (get-buffer-create "*erc-protocol*")
2248 (save-excursion
2249 (goto-char (point-max))
2250 (let ((inhibit-read-only t))
2251 (insert (if (not outbound)
2252 ;; Cope with the fact that string might
2253 ;; contain multiple lines of text.
2254 (let ((lines (delete "" (split-string string
2255 "\n\\|\r\n")))
2256 (result ""))
2257 (dolist (line lines)
2258 (setq result (concat result network-name
2259 " << " line "\n")))
2260 result)
2261 (erc-propertize
2262 (concat network-name " >> " string
2263 (if (/= ?\n
2264 (aref string
2265 (1- (length string))))
2266 "\n"))
2267 'face 'erc-input-face)))))
2268 (let ((orig-win (selected-window))
2269 (debug-buffer-window (get-buffer-window (current-buffer) t)))
2270 (when debug-buffer-window
2271 (select-window debug-buffer-window)
2272 (when (= 1 (count-lines (point) (point-max)))
2273 (goto-char (point-max))
2274 (recenter -1))
2275 (select-window orig-win)))))))
2277 (defun erc-toggle-debug-irc-protocol (&optional arg)
2278 "Toggle the value of `erc-debug-irc-protocol'.
2280 If ARG is non-nil, show the *erc-protocol* buffer."
2281 (interactive "P")
2282 (let* ((buf (get-buffer-create "*erc-protocol*")))
2283 (with-current-buffer buf
2284 (erc-view-mode-enter)
2285 (when (null (current-local-map))
2286 (let ((inhibit-read-only t))
2287 (insert (erc-make-notice "This buffer displays all IRC protocol traffic exchanged with each server.\n"))
2288 (insert (erc-make-notice "Kill this buffer to terminate protocol logging.\n\n")))
2289 (use-local-map (make-sparse-keymap))
2290 (local-set-key (kbd "t") 'erc-toggle-debug-irc-protocol))
2291 (add-hook 'kill-buffer-hook
2292 #'(lambda () (setq erc-debug-irc-protocol nil))
2293 nil 'local)
2294 (goto-char (point-max))
2295 (let ((inhibit-read-only t))
2296 (insert (erc-make-notice
2297 (format "IRC protocol logging %s at %s -- Press `t' to toggle logging.\n"
2298 (if erc-debug-irc-protocol "disabled" "enabled")
2299 (current-time-string))))))
2300 (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
2301 (if (and arg
2302 (not (get-buffer-window "*erc-protocol*" t)))
2303 (display-buffer buf t))
2304 (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
2305 (if erc-debug-irc-protocol "enabled" "disabled"))))
2307 ;;; I/O interface
2309 ;; send interface
2311 (defun erc-send-action (tgt str &optional force)
2312 "Send CTCP ACTION information described by STR to TGT."
2313 (erc-send-ctcp-message tgt (format "ACTION %s" str) force)
2314 (erc-display-message
2315 nil 'input (current-buffer)
2316 'ACTION ?n (erc-current-nick) ?a str ?u "" ?h ""))
2318 ;; Display interface
2320 (defun erc-string-invisible-p (string)
2321 "Check whether STRING is invisible or not.
2322 I.e. any char in it has the `invisible' property set."
2323 (text-property-any 0 (length string) 'invisible t string))
2325 (defcustom erc-remove-parsed-property t
2326 "Whether to remove the erc-parsed text property after displaying a message.
2328 The default is to remove it, since it causes ERC to take up extra
2329 memory. If you have code that relies on this property, then set
2330 this option to nil."
2331 :type 'boolean
2332 :group 'erc)
2334 (defun erc-display-line-1 (string buffer)
2335 "Display STRING in `erc-mode' BUFFER.
2336 Auxiliary function used in `erc-display-line'. The line gets filtered to
2337 interpret the control characters. Then, `erc-insert-pre-hook' gets called.
2338 If `erc-insert-this' is still t, STRING gets inserted into the buffer.
2339 Afterwards, `erc-insert-modify' and `erc-insert-post-hook' get called.
2340 If STRING is nil, the function does nothing."
2341 (when string
2342 (with-current-buffer (or buffer (process-buffer erc-server-process))
2343 (let ((insert-position (or (marker-position erc-insert-marker)
2344 (point-max))))
2345 (let ((string string) ;; FIXME! Can this be removed?
2346 (buffer-undo-list t)
2347 (inhibit-read-only t))
2348 (unless (string-match "\n$" string)
2349 (setq string (concat string "\n"))
2350 (when (erc-string-invisible-p string)
2351 (erc-put-text-properties 0 (length string)
2352 '(invisible intangible) string)))
2353 (erc-log (concat "erc-display-line: " string
2354 (format "(%S)" string) " in buffer "
2355 (format "%s" buffer)))
2356 (setq erc-insert-this t)
2357 (run-hook-with-args 'erc-insert-pre-hook string)
2358 (if (null erc-insert-this)
2359 ;; Leave erc-insert-this set to t as much as possible. Fran
2360 ;; Litterio <franl> has seen erc-insert-this set to nil while
2361 ;; erc-send-pre-hook is running, which should never happen. This
2362 ;; may cure it.
2363 (setq erc-insert-this t)
2364 (save-excursion ;; to restore point in the new buffer
2365 (save-restriction
2366 (widen)
2367 (goto-char insert-position)
2368 (insert-before-markers string)
2369 ;; run insertion hook, with point at restored location
2370 (save-restriction
2371 (narrow-to-region insert-position (point))
2372 (run-hooks 'erc-insert-modify-hook)
2373 (run-hooks 'erc-insert-post-hook)
2374 (when erc-remove-parsed-property
2375 (remove-text-properties (point-min) (point-max)
2376 '(erc-parsed nil))))))))
2377 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2378 (point-max))
2379 insert-position))))))
2381 (defun erc-update-undo-list (shift)
2382 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2383 (unless (or (zerop shift) (atom buffer-undo-list))
2384 (let ((list buffer-undo-list) elt)
2385 (while list
2386 (setq elt (car list))
2387 (cond ((integerp elt) ; POSITION
2388 (cl-incf (car list) shift))
2389 ((or (atom elt) ; nil, EXTENT
2390 ;; (eq t (car elt)) ; (t . TIME)
2391 (markerp (car elt))) ; (MARKER . DISTANCE)
2392 nil)
2393 ((integerp (car elt)) ; (BEGIN . END)
2394 (cl-incf (car elt) shift)
2395 (cl-incf (cdr elt) shift))
2396 ((stringp (car elt)) ; (TEXT . POSITION)
2397 (cl-incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
2398 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2399 (let ((cons (nthcdr 3 elt)))
2400 (cl-incf (car cons) shift)
2401 (cl-incf (cdr cons) shift)))
2402 ((and (featurep 'xemacs)
2403 (extentp (car elt))) ; (EXTENT START END)
2404 (cl-incf (nth 1 elt) shift)
2405 (cl-incf (nth 2 elt) shift)))
2406 (setq list (cdr list))))))
2408 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2409 "Regexp which matches all valid characters in a IRC nickname.")
2411 (defun erc-is-valid-nick-p (nick)
2412 "Check if NICK is a valid IRC nickname."
2413 (string-match (concat "\\`" erc-valid-nick-regexp "\\'") nick))
2415 (defun erc-display-line (string &optional buffer)
2416 "Display STRING in the ERC BUFFER.
2417 All screen output must be done through this function. If BUFFER is nil
2418 or omitted, the default ERC buffer for the `erc-session-server' is used.
2419 The BUFFER can be an actual buffer, a list of buffers, 'all or 'active.
2420 If BUFFER = 'all, the string is displayed in all the ERC buffers for the
2421 current session. 'active means the current active buffer
2422 \(`erc-active-buffer'). If the buffer can't be resolved, the current
2423 buffer is used. `erc-display-line-1' is used to display STRING.
2425 If STRING is nil, the function does nothing."
2426 (let ((inhibit-point-motion-hooks t)
2427 new-bufs)
2428 (dolist (buf (cond
2429 ((bufferp buffer) (list buffer))
2430 ((listp buffer) buffer)
2431 ((processp buffer) (list (process-buffer buffer)))
2432 ((eq 'all buffer)
2433 ;; Hmm, or all of the same session server?
2434 (erc-buffer-list nil erc-server-process))
2435 ((and (eq 'active buffer) (erc-active-buffer))
2436 (list (erc-active-buffer)))
2437 ((erc-server-buffer-live-p)
2438 (list (process-buffer erc-server-process)))
2439 (t (list (current-buffer)))))
2440 (when (buffer-live-p buf)
2441 (erc-display-line-1 string buf)
2442 (push buf new-bufs)))
2443 (when (null new-bufs)
2444 (erc-display-line-1 string (if (erc-server-buffer-live-p)
2445 (process-buffer erc-server-process)
2446 (current-buffer))))))
2448 (defun erc-display-message-highlight (type string)
2449 "Highlight STRING according to TYPE, where erc-TYPE-face is an ERC face.
2451 See also `erc-make-notice'."
2452 (cond ((eq type 'notice)
2453 (erc-make-notice string))
2455 (erc-put-text-property
2456 0 (length string)
2457 'face (or (intern-soft
2458 (concat "erc-" (symbol-name type) "-face"))
2459 "erc-default-face")
2460 string)
2461 string)))
2463 (defvar erc-lurker-state nil
2464 "Track the time of the last PRIVMSG for each (server,nick) pair.
2466 This is implemented as a hash of hashes, where the outer key is
2467 the canonicalized server name (as returned by
2468 `erc-canonicalize-server-name') and the outer value is a hash
2469 table mapping nicks (as returned by `erc-lurker-maybe-trim') to
2470 the times of their most recently received PRIVMSG on any channel
2471 on the given server.")
2473 (defcustom erc-lurker-trim-nicks t
2474 "If t, trim trailing `erc-lurker-ignore-chars' from nicks.
2476 This causes e.g. nick and nick\\=` to be considered as the same
2477 individual for activity tracking and lurkiness detection
2478 purposes."
2479 :group 'erc-lurker
2480 :type 'boolean)
2482 (defcustom erc-lurker-ignore-chars "`_"
2483 "Characters at the end of a nick to strip for activity tracking purposes.
2485 See also `erc-lurker-trim-nicks'."
2486 :group 'erc-lurker
2487 :type 'string)
2489 (defun erc-lurker-maybe-trim (nick)
2490 "Maybe trim trailing `erc-lurker-ignore-chars' from NICK.
2492 Returns NICK unmodified unless `erc-lurker-trim-nicks' is
2493 non-nil."
2494 (if erc-lurker-trim-nicks
2495 (replace-regexp-in-string
2496 (format "[%s]"
2497 (mapconcat (lambda (char)
2498 (regexp-quote (char-to-string char)))
2499 erc-lurker-ignore-chars ""))
2500 "" nick)
2501 nick))
2503 (defcustom erc-lurker-hide-list nil
2504 "List of IRC type messages to hide when sent by lurkers.
2506 A typical value would be '(\"JOIN\" \"PART\" \"QUIT\").
2507 See also `erc-lurker-p' and `erc-hide-list'."
2508 :group 'erc-lurker
2509 :type 'erc-message-type)
2511 (defcustom erc-lurker-threshold-time (* 60 60 24) ; 24h by default
2512 "Nicks from which no PRIVMSGs have been received within this
2513 interval (in units of seconds) are considered lurkers by
2514 `erc-lurker-p' and as a result their messages of types in
2515 `erc-lurker-hide-list' will be hidden."
2516 :group 'erc-lurker
2517 :type 'integer)
2519 (defun erc-lurker-initialize ()
2520 "Initialize ERC lurker tracking functionality.
2522 This function adds `erc-lurker-update-status' to
2523 `erc-insert-pre-hook' in order to record the time of each nick's
2524 most recent PRIVMSG as well as initializing the state variable
2525 storing this information."
2526 (setq erc-lurker-state (make-hash-table :test 'equal))
2527 (add-hook 'erc-insert-pre-hook 'erc-lurker-update-status))
2529 (defun erc-lurker-cleanup ()
2530 "Remove all last PRIVMSG state older than `erc-lurker-threshold-time'.
2532 This should be called regularly to avoid excessive resource
2533 consumption for long-lived IRC or Emacs sessions."
2534 (maphash
2535 (lambda (server hash)
2536 (maphash
2537 (lambda (nick last-PRIVMSG-time)
2538 (when
2539 (> (float-time (time-subtract
2540 (current-time)
2541 last-PRIVMSG-time))
2542 erc-lurker-threshold-time)
2543 (remhash nick hash)))
2544 hash)
2545 (if (zerop (hash-table-count hash))
2546 (remhash server erc-lurker-state)))
2547 erc-lurker-state))
2549 (defvar erc-lurker-cleanup-count 0
2550 "Internal counter variable for use with `erc-lurker-cleanup-interval'.")
2552 (defvar erc-lurker-cleanup-interval 100
2553 "Frequency of cleaning up stale erc-lurker state.
2555 `erc-lurker-update-status' calls `erc-lurker-cleanup' once for
2556 every `erc-lurker-cleanup-interval' updates to
2557 `erc-lurker-state'. This is designed to limit the memory
2558 consumption of lurker state during long Emacs sessions and/or ERC
2559 sessions with large numbers of incoming PRIVMSGs.")
2561 (defun erc-lurker-update-status (_message)
2562 "Update `erc-lurker-state' if necessary.
2564 This function is called from `erc-insert-pre-hook'. If the
2565 current message is a PRIVMSG, update `erc-lurker-state' to
2566 reflect the fact that its sender has issued a PRIVMSG at the
2567 current time. Otherwise, take no action.
2569 This function depends on the fact that `erc-display-message'
2570 dynamically binds `parsed', which is used to check if the current
2571 message is a PRIVMSG and to determine its sender. See also
2572 `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'.
2574 In order to limit memory consumption, this function also calls
2575 `erc-lurker-cleanup' once every `erc-lurker-cleanup-interval'
2576 updates of `erc-lurker-state'."
2577 (when (and (boundp 'parsed) (erc-response-p parsed))
2578 (let* ((command (erc-response.command parsed))
2579 (sender
2580 (erc-lurker-maybe-trim
2581 (car (erc-parse-user (erc-response.sender parsed)))))
2582 (server
2583 (erc-canonicalize-server-name erc-server-announced-name)))
2584 (when (equal command "PRIVMSG")
2585 (when (>= (cl-incf erc-lurker-cleanup-count)
2586 erc-lurker-cleanup-interval)
2587 (setq erc-lurker-cleanup-count 0)
2588 (erc-lurker-cleanup))
2589 (unless (gethash server erc-lurker-state)
2590 (puthash server (make-hash-table :test 'equal) erc-lurker-state))
2591 (puthash sender (current-time)
2592 (gethash server erc-lurker-state))))))
2594 (defun erc-lurker-p (nick)
2595 "Predicate indicating NICK's lurking status on the current server.
2597 Lurking is the condition where NICK has issued no PRIVMSG on this
2598 server within `erc-lurker-threshold-time'. See also
2599 `erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'."
2600 (unless erc-lurker-state (erc-lurker-initialize))
2601 (let* ((server
2602 (erc-canonicalize-server-name erc-server-announced-name))
2603 (last-PRIVMSG-time
2604 (gethash (erc-lurker-maybe-trim nick)
2605 (gethash server erc-lurker-state (make-hash-table)))))
2606 (or (null last-PRIVMSG-time)
2607 (> (float-time
2608 (time-subtract (current-time) last-PRIVMSG-time))
2609 erc-lurker-threshold-time))))
2611 (defcustom erc-common-server-suffixes
2612 '(("openprojects.net\\'" . "OPN")
2613 ("freenode.net\\'" . "freenode")
2614 ("oftc.net\\'" . "OFTC"))
2615 "Alist of common server name suffixes.
2616 This variable is used in mode-line display to save screen
2617 real estate. Set it to nil if you want to avoid changing
2618 displayed hostnames."
2619 :group 'erc-mode-line-and-header
2620 :type 'alist)
2622 (defun erc-canonicalize-server-name (server)
2623 "Return the canonical network name for SERVER if any,
2624 otherwise `erc-server-announced-name'. SERVER is matched against
2625 `erc-common-server-suffixes'."
2626 (when server
2627 (or (cdar (erc-remove-if-not
2628 (lambda (net) (string-match (car net) server))
2629 erc-common-server-suffixes))
2630 erc-server-announced-name)))
2632 (defun erc-add-targets (scope target-list)
2633 (let ((targets
2634 (mapcar (lambda (targets) (member scope targets)) target-list)))
2635 (cdr (apply 'append (delete nil targets)))))
2637 (defun erc-hide-current-message-p (parsed)
2638 "Predicate indicating whether the parsed ERC response PARSED should be hidden.
2640 Messages are always hidden if the message type of PARSED appears in
2641 `erc-hide-list'. Message types that appear in `erc-network-hide-list'
2642 or `erc-channel-hide-list' are are only hidden if the target matches
2643 the network or channel in the list. In addition, messages whose type
2644 is a member of `erc-lurker-hide-list' are hidden if `erc-lurker-p'
2645 returns non-nil."
2646 (let* ((command (erc-response.command parsed))
2647 (sender (car (erc-parse-user (erc-response.sender parsed))))
2648 (channel (nth 1 (erc-response.command-args parsed)))
2649 (network (or (and (fboundp 'erc-network-name) (erc-network-name))
2650 (erc-shorten-server-name
2651 (or erc-server-announced-name
2652 erc-session-server))))
2653 (current-hide-list
2654 (when erc-network-hide-list
2655 (erc-add-targets network erc-network-hide-list)))
2656 (current-hide-list
2657 (apply 'append current-hide-list
2658 (when erc-channel-hide-list
2659 (erc-add-targets channel erc-channel-hide-list)))))
2660 (or (member command erc-hide-list)
2661 (member command current-hide-list)
2662 (and (member command erc-lurker-hide-list) (erc-lurker-p sender)))))
2664 (defun erc-display-message (parsed type buffer msg &rest args)
2665 "Display MSG in BUFFER.
2667 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2669 See also `erc-format-message' and `erc-display-line'."
2670 (let ((string (if (symbolp msg)
2671 (apply #'erc-format-message msg args)
2672 msg)))
2673 (setq string
2674 (cond
2675 ((null type)
2676 string)
2677 ((listp type)
2678 (mapc (lambda (type)
2679 (setq string
2680 (erc-display-message-highlight type string)))
2681 type)
2682 string)
2683 ((symbolp type)
2684 (erc-display-message-highlight type string))))
2686 (if (not (erc-response-p parsed))
2687 (erc-display-line string buffer)
2688 (unless (erc-hide-current-message-p parsed)
2689 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
2690 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2691 (erc-display-line string buffer)))))
2693 (defun erc-message-type-member (position list)
2694 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2696 This function relies on the erc-parsed text-property being
2697 present."
2698 (let ((prop-val (erc-get-parsed-vector position)))
2699 (and prop-val (member (erc-response.command prop-val) list))))
2701 (defvar erc-send-input-line-function 'erc-send-input-line)
2702 (make-variable-buffer-local 'erc-send-input-line-function)
2704 (defun erc-send-input-line (target line &optional force)
2705 "Send LINE to TARGET.
2707 See also `erc-server-send'."
2708 (setq line (format "PRIVMSG %s :%s"
2709 target
2710 ;; If the line is empty, we still want to
2711 ;; send it - i.e. an empty pasted line.
2712 (if (string= line "\n")
2713 " \n"
2714 line)))
2715 (erc-server-send line force target))
2717 (defun erc-get-arglist (fun)
2718 "Return the argument list of a function without the parens."
2719 (let ((arglist (format "%S" (erc-function-arglist fun))))
2720 (if (string-match "\\`(\\(.*\\))\\'" arglist)
2721 (match-string 1 arglist)
2722 arglist)))
2724 (defun erc-command-no-process-p (str)
2725 "Return non-nil if STR is an ERC command that can be run when the process
2726 is not alive, nil otherwise."
2727 (let ((fun (erc-extract-command-from-line str)))
2728 (and fun
2729 (symbolp (car fun))
2730 (get (car fun) 'process-not-needed))))
2732 (defun erc-command-name (cmd)
2733 "For CMD being the function name of a ERC command, something like
2734 erc-cmd-FOO, this returns a string /FOO."
2735 (let ((command-name (symbol-name cmd)))
2736 (if (string-match "\\`erc-cmd-\\(.*\\)\\'" command-name)
2737 (concat "/" (match-string 1 command-name))
2738 command-name)))
2740 (defun erc-process-input-line (line &optional force no-command)
2741 "Translate LINE to an RFC1459 command and send it based.
2742 Returns non-nil if the command is actually sent to the server, and nil
2743 otherwise.
2745 If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
2746 it is passed to `erc-cmd-default'. If LINE is not a command (i.e. doesn't
2747 start with /<COMMAND>) then it is sent as a message.
2749 An optional FORCE argument forces sending the line when flood
2750 protection is in effect. The optional NO-COMMAND argument prohibits
2751 this function from interpreting the line as a command."
2752 (let ((command-list (erc-extract-command-from-line line)))
2753 (if (and command-list
2754 (not no-command))
2755 (let* ((cmd (nth 0 command-list))
2756 (args (nth 1 command-list)))
2757 (condition-case nil
2758 (if (listp args)
2759 (apply cmd args)
2760 (funcall cmd args))
2761 (wrong-number-of-arguments
2762 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2763 ?c (erc-command-name cmd)
2764 ?u (or (erc-get-arglist cmd)
2766 ?d (format "%s\n"
2767 (or (documentation cmd) "")))
2768 nil)))
2769 (let ((r (erc-default-target)))
2770 (if r
2771 (funcall erc-send-input-line-function r line force)
2772 (erc-display-message nil 'error (current-buffer) 'no-target)
2773 nil)))))
2775 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2776 ;; Input commands handlers
2777 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2779 (defun erc-cmd-AMSG (line)
2780 "Send LINE to all channels of the current server that you are on."
2781 (interactive "sSend to all channels you're on: ")
2782 (setq line (erc-trim-string line))
2783 (erc-with-all-buffers-of-server nil
2784 (lambda ()
2785 (erc-channel-p (erc-default-target)))
2786 (erc-send-message line)))
2787 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2789 (defun erc-cmd-SAY (line)
2790 "Send LINE to the current query or channel as a message, not a command.
2792 Use this when you want to send a message with a leading '/'. Note
2793 that since multi-line messages are never a command, you don't
2794 need this when pasting multiple lines of text."
2795 (if (string-match "^\\s-*$" line)
2797 (string-match "^ ?\\(.*\\)" line)
2798 (erc-process-input-line (match-string 1 line) nil t)))
2799 (put 'erc-cmd-SAY 'do-not-parse-args t)
2801 (defun erc-cmd-SET (line)
2802 "Set the variable named by the first word in LINE to some VALUE.
2803 VALUE is computed by evaluating the rest of LINE in Lisp."
2804 (cond
2805 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2806 (let ((var (read (concat "erc-" (match-string 1 line))))
2807 (val (read (match-string 2 line))))
2808 (if (boundp var)
2809 (progn
2810 (set var (eval val))
2811 (erc-display-message
2812 nil nil 'active (format "Set %S to %S" var val))
2814 (setq var (read (match-string 1 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 (erc-display-message nil 'error 'active 'variable-not-bound)
2822 nil))))
2823 ((string-match "^\\s-*$" line)
2824 (erc-display-line
2825 (concat "Available user variables:\n"
2826 (apply
2827 #'concat
2828 (mapcar
2829 (lambda (var)
2830 (let ((val (symbol-value var)))
2831 (concat (format "%S:" var)
2832 (if (consp val)
2833 (concat "\n" (pp-to-string val))
2834 (format " %S\n" val)))))
2835 (apropos-internal "^erc-" 'custom-variable-p))))
2836 (current-buffer))
2838 (t nil)))
2839 (defalias 'erc-cmd-VAR 'erc-cmd-SET)
2840 (defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2841 (put 'erc-cmd-SET 'do-not-parse-args t)
2842 (put 'erc-cmd-SET 'process-not-needed t)
2844 (defun erc-cmd-default (line)
2845 "Fallback command.
2847 Commands for which no erc-cmd-xxx exists, are tunneled through
2848 this function. LINE is sent to the server verbatim, and
2849 therefore has to contain the command itself as well."
2850 (erc-log (format "cmd: DEFAULT: %s" line))
2851 (erc-server-send (substring line 1))
2854 (defun erc-cmd-IGNORE (&optional user)
2855 "Ignore USER. This should be a regexp matching nick!user@host.
2856 If no USER argument is specified, list the contents of `erc-ignore-list'."
2857 (if user
2858 (let ((quoted (regexp-quote user)))
2859 (when (and (not (string= user quoted))
2860 (y-or-n-p (format "Use regexp-quoted form (%s) instead? "
2861 quoted)))
2862 (setq user quoted))
2863 (erc-display-line
2864 (erc-make-notice (format "Now ignoring %s" user))
2865 'active)
2866 (erc-with-server-buffer (add-to-list 'erc-ignore-list user)))
2867 (if (null (erc-with-server-buffer erc-ignore-list))
2868 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2869 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2870 (mapc #'(lambda (item)
2871 (erc-display-line (erc-make-notice item)
2872 'active))
2873 (erc-with-server-buffer erc-ignore-list))))
2876 (defun erc-cmd-UNIGNORE (user)
2877 "Remove the user specified in USER from the ignore list."
2878 (let ((ignored-nick (car (erc-with-server-buffer
2879 (erc-member-ignore-case (regexp-quote user)
2880 erc-ignore-list)))))
2881 (unless ignored-nick
2882 (if (setq ignored-nick (erc-ignored-user-p user))
2883 (unless (y-or-n-p (format "Remove this regexp (%s)? "
2884 ignored-nick))
2885 (setq ignored-nick nil))
2886 (erc-display-line
2887 (erc-make-notice (format "%s is not currently ignored!" user))
2888 'active)))
2889 (when ignored-nick
2890 (erc-display-line
2891 (erc-make-notice (format "No longer ignoring %s" user))
2892 'active)
2893 (erc-with-server-buffer
2894 (setq erc-ignore-list (delete ignored-nick erc-ignore-list)))))
2897 (defun erc-cmd-CLEAR ()
2898 "Clear the window content."
2899 (recenter 0)
2901 (put 'erc-cmd-CLEAR 'process-not-needed t)
2903 (defun erc-cmd-OPS ()
2904 "Show the ops in the current channel."
2905 (interactive)
2906 (let ((ops nil))
2907 (if erc-channel-users
2908 (maphash (lambda (_nick user-data)
2909 (let ((cuser (cdr user-data)))
2910 (if (and cuser
2911 (erc-channel-user-op cuser))
2912 (setq ops (cons (erc-server-user-nickname
2913 (car user-data))
2914 ops)))))
2915 erc-channel-users))
2916 (setq ops (sort ops 'string-lessp))
2917 (if ops
2918 (erc-display-message
2919 nil 'notice (current-buffer) 'ops
2920 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2921 ?o (mapconcat 'identity ops " "))
2922 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2925 (defun erc-cmd-COUNTRY (tld)
2926 "Display the country associated with the top level domain TLD."
2927 (require 'mail-extr)
2928 (let ((co (ignore-errors (what-domain tld))))
2929 (if co
2930 (erc-display-message
2931 nil 'notice 'active 'country ?c co ?d tld)
2932 (erc-display-message
2933 nil 'notice 'active 'country-unknown ?d tld))
2935 (put 'erc-cmd-COUNTRY 'process-not-needed t)
2937 (defun erc-cmd-AWAY (line)
2938 "Mark the user as being away, the reason being indicated by LINE.
2939 If no reason is given, unset away status."
2940 (when (string-match "^\\s-*\\(.*\\)$" line)
2941 (let ((reason (match-string 1 line)))
2942 (erc-log (format "cmd: AWAY: %s" reason))
2943 (erc-server-send
2944 (if (string= reason "")
2945 "AWAY"
2946 (concat "AWAY :" reason))))
2948 (put 'erc-cmd-AWAY 'do-not-parse-args t)
2950 (defun erc-cmd-GAWAY (line)
2951 "Mark the user as being away everywhere, the reason being indicated by LINE."
2952 ;; on all server buffers.
2953 (erc-with-all-buffers-of-server nil
2954 #'erc-open-server-buffer-p
2955 (erc-cmd-AWAY line)))
2956 (put 'erc-cmd-GAWAY 'do-not-parse-args t)
2958 (defun erc-cmd-CTCP (nick cmd &rest args)
2959 "Send a Client To Client Protocol message to NICK.
2961 CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2962 VERSION and so on. It is called with ARGS."
2963 (let ((str (concat cmd
2964 (when args
2965 (concat " " (mapconcat #'identity args " "))))))
2966 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2967 (erc-send-ctcp-message nick str)
2970 (defun erc-cmd-HELP (&optional func)
2971 "Popup help information.
2973 If FUNC contains a valid function or variable, help about that
2974 will be displayed. If FUNC is empty, display an apropos about
2975 ERC commands. Otherwise, do `apropos' in the ERC namespace
2976 \(\"erc-.*LINE\"\).
2978 Examples:
2979 To find out about erc and bbdb, do
2980 /help bbdb.*
2982 For help about the WHOIS command, do:
2983 /help whois
2985 For a list of user commands (/join /part, ...):
2986 /help."
2987 (if func
2988 (let* ((sym (or (let ((sym (intern-soft
2989 (concat "erc-cmd-" (upcase func)))))
2990 (if (and sym (or (boundp sym) (fboundp sym)))
2992 nil))
2993 (let ((sym (intern-soft func)))
2994 (if (and sym (or (boundp sym) (fboundp sym)))
2996 nil))
2997 (let ((sym (intern-soft (concat "erc-" func))))
2998 (if (and sym (or (boundp sym) (fboundp sym)))
3000 nil)))))
3001 (if sym
3002 (cond
3003 ((boundp sym) (describe-variable sym))
3004 ((fboundp sym) (describe-function sym))
3005 (t nil))
3006 (apropos-command (concat "erc-.*" func) nil
3007 (lambda (x)
3008 (or (commandp x)
3009 (get x 'custom-type))))
3011 (apropos "erc-cmd-")
3012 (message "Type C-h m to get additional information about keybindings.")
3015 (defalias 'erc-cmd-H 'erc-cmd-HELP)
3016 (put 'erc-cmd-HELP 'process-not-needed t)
3018 (defun erc-cmd-JOIN (channel &optional key)
3019 "Join the channel given in CHANNEL, optionally with KEY.
3020 If CHANNEL is specified as \"-invite\", join the channel to which you
3021 were most recently invited. See also `invitation'."
3022 (let (chnl)
3023 (if (string= (upcase channel) "-INVITE")
3024 (if erc-invitation
3025 (setq chnl erc-invitation)
3026 (erc-display-message nil 'error (current-buffer) 'no-invitation))
3027 (setq chnl (erc-ensure-channel-name channel)))
3028 (when chnl
3029 ;; Prevent double joining of same channel on same server.
3030 (let ((joined-channels
3031 (mapcar #'(lambda (chanbuf)
3032 (with-current-buffer chanbuf (erc-default-target)))
3033 (erc-channel-list erc-server-process))))
3034 (if (erc-member-ignore-case chnl joined-channels)
3035 (switch-to-buffer (car (erc-member-ignore-case chnl
3036 joined-channels)))
3037 (erc-log (format "cmd: JOIN: %s" chnl))
3038 (erc-server-send (if (and chnl key)
3039 (format "JOIN %s %s" chnl key)
3040 (format "JOIN %s" chnl)))))))
3043 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
3044 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
3046 (defvar erc-channel-new-member-names nil
3047 "If non-nil, a names list is currently being received.
3049 If non-nil, this variable is a hash-table that associates
3050 received nicks with t.")
3051 (make-variable-buffer-local 'erc-channel-new-member-names)
3053 (defun erc-cmd-NAMES (&optional channel)
3054 "Display the users in CHANNEL.
3055 If CHANNEL is not specified, display the users in the current channel.
3056 This function clears the channel name list first, then sends the
3057 command."
3058 (let ((tgt (or (and (erc-channel-p channel) channel)
3059 (erc-default-target))))
3060 (if (and tgt (erc-channel-p tgt))
3061 (progn
3062 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
3063 (erc-with-buffer
3064 (tgt)
3065 (erc-channel-begin-receiving-names))
3066 (erc-server-send (concat "NAMES " tgt)))
3067 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
3069 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
3071 (defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
3072 "Kick the user indicated in LINE from the current channel.
3073 LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
3074 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
3075 (if (string= "" reasonstring)
3076 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
3077 (if (erc-channel-p target)
3078 (let ((nick reason-or-nick))
3079 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
3080 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
3081 nil target)
3083 (when target
3084 (let ((ch (erc-default-target)))
3085 (setq reasonstring (concat
3086 (if reason-or-nick (concat reason-or-nick " "))
3087 reasonstring))
3088 (if ch
3089 (progn
3090 (erc-log
3091 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
3092 (erc-server-send
3093 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
3094 (erc-display-message nil 'error (current-buffer)
3095 'no-default-channel))
3096 t)))))
3098 (defvar erc-script-args nil)
3100 (defun erc-cmd-LOAD (line)
3101 "Load the script provided in the LINE.
3102 If LINE continues beyond the file name, the rest of
3103 it is put in a (local) variable `erc-script-args',
3104 which can be used in Emacs Lisp scripts.
3106 The optional FORCE argument is ignored here - you can't force loading
3107 a script after exceeding the flood threshold."
3108 (cond
3109 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
3110 (let* ((file-to-find (match-string 1 line))
3111 (erc-script-args (match-string 2 line))
3112 (file (erc-find-file file-to-find erc-script-path)))
3113 (erc-log (format "cmd: LOAD: %s" file-to-find))
3114 (cond
3115 ((not file)
3116 (erc-display-message nil 'error (current-buffer)
3117 'cannot-find-file ?f file-to-find))
3118 ((not (file-readable-p file))
3119 (erc-display-message nil 'error (current-buffer)
3120 'cannot-read-file ?f file))
3122 (message "Loading \'%s\'..." file)
3123 (erc-load-script file)
3124 (message "Loading \'%s\'...done" file))))
3126 (t nil)))
3128 (defun erc-cmd-WHOIS (user &optional server)
3129 "Display whois information for USER.
3131 If SERVER is non-nil, use that, rather than the current server."
3132 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
3133 (let ((send (if server
3134 (format "WHOIS %s %s" user server)
3135 (format "WHOIS %s" user))))
3136 (erc-log (format "cmd: %s" send))
3137 (erc-server-send send)
3139 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
3141 (defun erc-cmd-WHOAMI ()
3142 "Display whois information about yourself."
3143 (erc-cmd-WHOIS (erc-current-nick))
3146 (defun erc-cmd-IDLE (nick)
3147 "Show the length of time NICK has been idle."
3148 (let ((origbuf (current-buffer))
3149 symlist)
3150 (erc-with-server-buffer
3151 (push (cons (erc-once-with-server-event
3152 311 (lambda (_proc parsed)
3153 (string= nick
3154 (nth 1 (erc-response.command-args
3155 parsed)))))
3156 'erc-server-311-functions)
3157 symlist)
3158 (push (cons (erc-once-with-server-event
3159 312 (lambda (_proc parsed)
3160 (string= nick
3161 (nth 1 (erc-response.command-args
3162 parsed)))))
3163 'erc-server-312-functions)
3164 symlist)
3165 (push (cons (erc-once-with-server-event
3166 318 (lambda (_proc parsed)
3167 (string= nick
3168 (nth 1 (erc-response.command-args
3169 parsed)))))
3170 'erc-server-318-functions)
3171 symlist)
3172 (push (cons (erc-once-with-server-event
3173 319 (lambda (_proc parsed)
3174 (string= nick
3175 (nth 1 (erc-response.command-args
3176 parsed)))))
3177 'erc-server-319-functions)
3178 symlist)
3179 (push (cons (erc-once-with-server-event
3180 320 (lambda (_proc parsed)
3181 (string= nick
3182 (nth 1 (erc-response.command-args
3183 parsed)))))
3184 'erc-server-320-functions)
3185 symlist)
3186 (push (cons (erc-once-with-server-event
3187 330 (lambda (_proc parsed)
3188 (string= nick
3189 (nth 1 (erc-response.command-args
3190 parsed)))))
3191 'erc-server-330-functions)
3192 symlist)
3193 (push (cons (erc-once-with-server-event
3195 (lambda (_proc parsed)
3196 (let ((idleseconds
3197 (string-to-number
3198 (cl-third
3199 (erc-response.command-args parsed)))))
3200 (erc-display-line
3201 (erc-make-notice
3202 (format "%s has been idle for %s."
3203 (erc-string-no-properties nick)
3204 (erc-seconds-to-string idleseconds)))
3205 origbuf)
3206 t)))
3207 'erc-server-317-functions)
3208 symlist)
3210 ;; Send the WHOIS command.
3211 (erc-cmd-WHOIS nick)
3213 ;; Remove the uninterned symbols from the server hooks that did not run.
3214 (run-at-time 20 nil (lambda (buf symlist)
3215 (with-current-buffer buf
3216 (dolist (sym symlist)
3217 (let ((hooksym (cdr sym))
3218 (funcsym (car sym)))
3219 (remove-hook hooksym funcsym t)))))
3220 (current-buffer) symlist)))
3223 (defun erc-cmd-DESCRIBE (line)
3224 "Pose some action to a certain user.
3225 LINE has the format \"USER ACTION\"."
3226 (cond
3227 ((string-match
3228 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
3229 (let ((dst (match-string 1 line))
3230 (s (match-string 2 line)))
3231 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
3232 (erc-send-action dst s))
3234 (t nil)))
3235 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
3237 (defun erc-cmd-ME (line)
3238 "Send LINE as an action."
3239 (cond
3240 ((string-match "^\\s-\\(.*\\)$" line)
3241 (let ((s (match-string 1 line)))
3242 (erc-log (format "cmd: ME: %s" s))
3243 (erc-send-action (erc-default-target) s))
3245 (t nil)))
3246 (put 'erc-cmd-ME 'do-not-parse-args t)
3248 (defun erc-cmd-ME\'S (line)
3249 "Do a /ME command, but add the string \" 's\" to the beginning."
3250 (erc-cmd-ME (concat " 's" line)))
3251 (put 'erc-cmd-ME\'S 'do-not-parse-args t)
3253 (defun erc-cmd-LASTLOG (line)
3254 "Show all lines in the current buffer matching the regexp LINE.
3256 If a match spreads across multiple lines, all those lines are shown.
3258 The lines are shown in a buffer named `*Occur*'.
3259 It serves as a menu to find any of the occurrences in this buffer.
3260 \\[describe-mode] in that buffer will explain how.
3262 If LINE contains upper case characters (excluding those preceded by ‘\\’),
3263 the matching is case-sensitive."
3264 (occur line)
3266 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
3267 (put 'erc-cmd-LASTLOG 'process-not-needed t)
3269 (defun erc-send-message (line &optional force)
3270 "Send LINE to the current channel or user and display it.
3272 See also `erc-message' and `erc-display-line'."
3273 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
3274 (erc-display-line
3275 (concat (erc-format-my-nick) line)
3276 (current-buffer))
3277 ;; FIXME - treat multiline, run hooks, or remove me?
3280 (defun erc-cmd-MODE (line)
3281 "Change or display the mode value of a channel or user.
3282 The first word specifies the target. The rest is the mode string
3283 to send.
3285 If only one word is given, display the mode of that target.
3287 A list of valid mode strings for Freenode may be found at
3288 URL `http://freenode.net/using_the_network.shtml'."
3289 (cond
3290 ((string-match "^\\s-\\(.*\\)$" line)
3291 (let ((s (match-string 1 line)))
3292 (erc-log (format "cmd: MODE: %s" s))
3293 (erc-server-send (concat "MODE " line)))
3295 (t nil)))
3296 (put 'erc-cmd-MODE 'do-not-parse-args t)
3298 (defun erc-cmd-NOTICE (channel-or-user &rest message)
3299 "Send a notice to the channel or user given as the first word.
3300 The rest is the message to send."
3301 (erc-message "NOTICE" (concat channel-or-user " "
3302 (mapconcat #'identity message " "))))
3304 (defun erc-cmd-MSG (line)
3305 "Send a message to the channel or user given as the first word in LINE.
3307 The rest of LINE is the message to send."
3308 (erc-message "PRIVMSG" line))
3310 (defalias 'erc-cmd-M 'erc-cmd-MSG)
3311 (put 'erc-cmd-MSG 'do-not-parse-args t)
3313 (defun erc-cmd-SQUERY (line)
3314 "Send a Service Query to the service given as the first word in LINE.
3316 The rest of LINE is the message to send."
3317 (erc-message "SQUERY" line))
3319 (defun erc-cmd-NICK (nick)
3320 "Change current nickname to NICK."
3321 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
3322 (let ((nicklen (cdr (assoc "NICKLEN" (erc-with-server-buffer
3323 erc-server-parameters)))))
3324 (and nicklen (> (length nick) (string-to-number nicklen))
3325 (erc-display-message
3326 nil 'notice 'active 'nick-too-long
3327 ?i (length nick) ?l nicklen)))
3328 (erc-server-send (format "NICK %s" nick))
3329 (cond (erc-bad-nick
3330 (erc-set-current-nick nick)
3331 (erc-update-mode-line)
3332 (setq erc-bad-nick nil)))
3335 (defun erc-cmd-PART (line)
3336 "When LINE is an empty string, leave the current channel.
3337 Otherwise leave the channel indicated by LINE."
3338 (cond
3339 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
3340 (let* ((ch (match-string 1 line))
3341 (msg (match-string 2 line))
3342 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3343 (erc-log (format "cmd: PART: %s: %s" ch reason))
3344 (erc-server-send (if (string= reason "")
3345 (format "PART %s" ch)
3346 (format "PART %s :%s" ch reason))
3347 nil ch))
3349 ((string-match "^\\s-*\\(.*\\)$" line)
3350 (let* ((ch (erc-default-target))
3351 (msg (match-string 1 line))
3352 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3353 (if (and ch (erc-channel-p ch))
3354 (progn
3355 (erc-log (format "cmd: PART: %s: %s" ch reason))
3356 (erc-server-send (if (string= reason "")
3357 (format "PART %s" ch)
3358 (format "PART %s :%s" ch reason))
3359 nil ch))
3360 (erc-display-message nil 'error (current-buffer) 'no-target)))
3362 (t nil)))
3363 (put 'erc-cmd-PART 'do-not-parse-args t)
3365 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
3367 (defun erc-cmd-PING (recipient)
3368 "Ping RECIPIENT."
3369 (let ((time (format "%f" (erc-current-time))))
3370 (erc-log (format "cmd: PING: %s" time))
3371 (erc-cmd-CTCP recipient "PING" time)))
3373 (defun erc-cmd-QUOTE (line)
3374 "Send LINE directly to the server.
3375 All the text given as argument is sent to the sever as unmodified,
3376 just as you provided it. Use this command with care!"
3377 (cond
3378 ((string-match "^ ?\\(.+\\)$" line)
3379 (erc-server-send (match-string 1 line)))
3380 (t nil)))
3381 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
3383 (defcustom erc-query-display 'window
3384 "Indicates how to display query buffers when using the /QUERY
3385 command to talk to someone.
3387 The default behavior is to display the message in a new window
3388 and bring it to the front. See the documentation for
3389 `erc-join-buffer' for a description of the available choices.
3391 See also `erc-auto-query' to decide how private messages from
3392 other people should be displayed."
3393 :group 'erc-query
3394 :type '(choice (const :tag "Split window and select" window)
3395 (const :tag "Split window, don't select" window-noselect)
3396 (const :tag "New frame" frame)
3397 (const :tag "Bury in new buffer" bury)
3398 (const :tag "Use current buffer" buffer)
3399 (const :tag "Use current buffer" t)))
3401 (defun erc-cmd-QUERY (&optional user)
3402 "Open a query with USER.
3403 The type of query window/frame/etc will depend on the value of
3404 `erc-query-display'.
3406 If USER is omitted, close the current query buffer if one exists
3407 - except this is broken now ;-)"
3408 (interactive
3409 (list (read-from-minibuffer "Start a query with: " nil)))
3410 (let ((session-buffer (erc-server-buffer))
3411 (erc-join-buffer erc-query-display))
3412 (if user
3413 (erc-query user session-buffer)
3414 ;; currently broken, evil hack to display help anyway
3415 ;(erc-delete-query))))
3416 (signal 'wrong-number-of-arguments ""))))
3417 (defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3419 (defun erc-quit/part-reason-default ()
3420 "Default quit/part message."
3421 (format "\C-bERC\C-b (IRC client for Emacs %s)" emacs-version))
3424 (defun erc-quit-reason-normal (&optional s)
3425 "Normal quit message.
3427 If S is non-nil, it will be used as the quit reason."
3428 (or s (erc-quit/part-reason-default)))
3430 (defun erc-quit-reason-zippy (&optional s)
3431 "Zippy quit message.
3433 If S is non-nil, it will be used as the quit reason."
3434 (or s
3435 (if (fboundp 'yow)
3436 (erc-replace-regexp-in-string "\n" "" (yow))
3437 (erc-quit/part-reason-default))))
3439 (make-obsolete 'erc-quit-reason-zippy "it will be removed." "24.4")
3441 (defun erc-quit-reason-various (s)
3442 "Choose a quit reason based on S (a string)."
3443 (when (featurep 'xemacs) (require 'poe))
3444 (let ((res (car (assoc-default (or s "")
3445 erc-quit-reason-various-alist 'string-match))))
3446 (cond
3447 ((functionp res) (funcall res))
3448 ((stringp res) res)
3449 (s s)
3450 (t (erc-quit/part-reason-default)))))
3452 (defun erc-part-reason-normal (&optional s)
3453 "Normal part message.
3455 If S is non-nil, it will be used as the part reason."
3456 (or s (erc-quit/part-reason-default)))
3458 (defun erc-part-reason-zippy (&optional s)
3459 "Zippy part message.
3461 If S is non-nil, it will be used as the quit reason."
3462 (or s
3463 (if (fboundp 'yow)
3464 (erc-replace-regexp-in-string "\n" "" (yow))
3465 (erc-quit/part-reason-default))))
3467 (make-obsolete 'erc-part-reason-zippy "it will be removed." "24.4")
3469 (defun erc-part-reason-various (s)
3470 "Choose a part reason based on S (a string)."
3471 (when (featurep 'xemacs) (require 'poe))
3472 (let ((res (car (assoc-default (or s "")
3473 erc-part-reason-various-alist 'string-match))))
3474 (cond
3475 ((functionp res) (funcall res))
3476 ((stringp res) res)
3477 (s s)
3478 (t (erc-quit/part-reason-default)))))
3480 (defun erc-cmd-QUIT (reason)
3481 "Disconnect from the current server.
3482 If REASON is omitted, display a default quit message, otherwise display
3483 the message given by REASON."
3484 (unless reason
3485 (setq reason ""))
3486 (cond
3487 ((string-match "^\\s-*\\(.*\\)$" reason)
3488 (let* ((s (match-string 1 reason))
3489 (buffer (erc-server-buffer))
3490 (reason (funcall erc-quit-reason (if (equal s "") nil s)))
3491 server-proc)
3492 (with-current-buffer (if (and buffer
3493 (bufferp buffer))
3494 buffer
3495 (current-buffer))
3496 (erc-log (format "cmd: QUIT: %s" reason))
3497 (setq erc-server-quitting t)
3498 (erc-set-active-buffer (erc-server-buffer))
3499 (setq server-proc erc-server-process)
3500 (erc-server-send (format "QUIT :%s" reason)))
3501 (run-hook-with-args 'erc-quit-hook server-proc)
3502 (when erc-kill-queries-on-quit
3503 (erc-kill-query-buffers server-proc))
3504 ;; if the process has not been killed within 4 seconds, kill it
3505 (run-at-time 4 nil
3506 (lambda (proc)
3507 (when (and (processp proc)
3508 (memq (process-status proc) '(run open)))
3509 (delete-process proc)))
3510 server-proc))
3512 (t nil)))
3514 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3515 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3516 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3517 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3518 (put 'erc-cmd-QUIT 'process-not-needed t)
3520 (defun erc-cmd-GQUIT (reason)
3521 "Disconnect from all servers at once with the same quit REASON."
3522 (erc-with-all-buffers-of-server nil #'erc-open-server-buffer-p
3523 (erc-cmd-QUIT reason))
3524 (when erc-kill-queries-on-quit
3525 ;; if the query buffers have not been killed within 4 seconds,
3526 ;; kill them
3527 (run-at-time
3528 4 nil
3529 (lambda ()
3530 (dolist (buffer (erc-buffer-list (lambda (buf)
3531 (not (erc-server-buffer-p buf)))))
3532 (kill-buffer buffer)))))
3535 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3536 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3537 (put 'erc-cmd-GQUIT 'process-not-needed t)
3539 (defun erc-cmd-RECONNECT ()
3540 "Try to reconnect to the current IRC server."
3541 (let ((buffer (erc-server-buffer))
3542 (process nil))
3543 (unless (buffer-live-p buffer)
3544 (setq buffer (current-buffer)))
3545 (with-current-buffer buffer
3546 (setq erc-server-quitting nil)
3547 (setq erc-server-reconnecting t)
3548 (setq erc-server-reconnect-count 0)
3549 (setq process (get-buffer-process (erc-server-buffer)))
3550 (if process
3551 (delete-process process)
3552 (erc-server-reconnect))
3553 (setq erc-server-reconnecting nil)))
3555 (put 'erc-cmd-RECONNECT 'process-not-needed t)
3557 (defun erc-cmd-SERVER (server)
3558 "Connect to SERVER, leaving existing connection intact."
3559 (erc-log (format "cmd: SERVER: %s" server))
3560 (condition-case nil
3561 (erc :server server :nick (erc-current-nick))
3562 (error
3563 (erc-error "Cannot find host %s." server)))
3565 (put 'erc-cmd-SERVER 'process-not-needed t)
3567 (defvar motif-version-string)
3568 (defvar gtk-version-string)
3570 (defun erc-cmd-SV ()
3571 "Say the current ERC and Emacs version into channel."
3572 (erc-send-message (format "I'm using ERC with %s %s (%s%s) of %s."
3573 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3574 emacs-version
3575 system-configuration
3576 (concat
3577 (cond ((featurep 'motif)
3578 (concat ", " (substring
3579 motif-version-string 4)))
3580 ((featurep 'gtk)
3581 (concat ", GTK+ Version "
3582 gtk-version-string))
3583 ((featurep 'x-toolkit) ", X toolkit")
3584 (t ""))
3585 (if (and (boundp 'x-toolkit-scroll-bars)
3586 (memq x-toolkit-scroll-bars
3587 '(xaw xaw3d)))
3588 (format ", %s scroll bars"
3589 (capitalize (symbol-name
3590 x-toolkit-scroll-bars)))
3592 (if (featurep 'multi-tty) ", multi-tty" ""))
3593 erc-emacs-build-time))
3596 (defun erc-cmd-SM ()
3597 "Say the current ERC modes into channel."
3598 (erc-send-message (format "I'm using the following modules: %s!"
3599 (erc-modes)))
3602 (defun erc-cmd-DEOP (&rest people)
3603 "Remove the operator setting from user(s) given in PEOPLE."
3604 (when (> (length people) 0)
3605 (erc-server-send (concat "MODE " (erc-default-target)
3606 " -"
3607 (make-string (length people) ?o)
3609 (mapconcat 'identity people " ")))
3612 (defun erc-cmd-OP (&rest people)
3613 "Add the operator setting to users(s) given in PEOPLE."
3614 (when (> (length people) 0)
3615 (erc-server-send (concat "MODE " (erc-default-target)
3616 " +"
3617 (make-string (length people) ?o)
3619 (mapconcat 'identity people " ")))
3622 (defun erc-cmd-TIME (&optional line)
3623 "Request the current time and date from the current server."
3624 (cond
3625 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3626 (let ((args (match-string 1 line)))
3627 (erc-log (format "cmd: TIME: %s" args))
3628 (erc-server-send (concat "TIME " args)))
3630 (t (erc-server-send "TIME"))))
3631 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3633 (defun erc-cmd-TOPIC (topic)
3634 "Set or request the topic for a channel.
3635 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3636 or the empty string.
3638 If no #CHANNEL is given, the default channel is used. If TOPIC is
3639 given, the channel topic is modified, otherwise the current topic will
3640 be displayed."
3641 (cond
3642 ;; /topic #channel TOPIC
3643 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3644 (let ((ch (match-string 1 topic))
3645 (topic (match-string 2 topic)))
3646 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3647 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3649 ;; /topic #channel
3650 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3651 (let ((ch (match-string 1 topic)))
3652 (erc-server-send (format "TOPIC %s" ch) nil ch)
3654 ;; /topic
3655 ((string-match "^\\s-*$" topic)
3656 (let ((ch (erc-default-target)))
3657 (erc-server-send (format "TOPIC %s" ch) nil ch)
3659 ;; /topic TOPIC
3660 ((string-match "^\\s-*\\(.*\\)$" topic)
3661 (let ((ch (erc-default-target))
3662 (topic (match-string 1 topic)))
3663 (if (and ch (erc-channel-p ch))
3664 (progn
3665 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3666 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3667 (erc-display-message nil 'error (current-buffer) 'no-target)))
3669 (t nil)))
3670 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3671 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3673 (defun erc-cmd-APPENDTOPIC (topic)
3674 "Append TOPIC to the current channel topic, separated by a space."
3675 (let ((oldtopic erc-channel-topic))
3676 ;; display help when given no arguments
3677 (when (string-match "^\\s-*$" topic)
3678 (signal 'wrong-number-of-arguments nil))
3679 ;; strip trailing ^O
3680 (when (string-match "\\(.*\\)\C-o" oldtopic)
3681 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3682 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3683 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3685 (defun erc-cmd-CLEARTOPIC (&optional channel)
3686 "Clear the topic for a CHANNEL.
3687 If CHANNEL is not specified, clear the topic for the default channel."
3688 (interactive "sClear topic of channel (RET is current channel): ")
3689 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3690 (when chnl
3691 (erc-server-send (format "TOPIC %s :" chnl))
3692 t)))
3694 ;;; Banlists
3696 (defvar erc-channel-banlist nil
3697 "A list of bans seen for the current channel.
3699 Each ban is an alist of the form:
3700 (WHOSET . MASK)
3702 The property `received-from-server' indicates whether
3703 or not the ban list has been requested from the server.")
3704 (make-variable-buffer-local 'erc-channel-banlist)
3705 (put 'erc-channel-banlist 'received-from-server nil)
3707 (defun erc-cmd-BANLIST ()
3708 "Pretty-print the contents of `erc-channel-banlist'.
3710 The ban list is fetched from the server if necessary."
3711 (let ((chnl (erc-default-target))
3712 (chnl-name (buffer-name)))
3714 (cond
3715 ((not (erc-channel-p chnl))
3716 (erc-display-line (erc-make-notice "You're not on a channel\n")
3717 'active))
3719 ((not (get 'erc-channel-banlist 'received-from-server))
3720 (let ((old-367-hook erc-server-367-functions))
3721 (setq erc-server-367-functions 'erc-banlist-store
3722 erc-channel-banlist nil)
3723 ;; fetch the ban list then callback
3724 (erc-with-server-buffer
3725 (erc-once-with-server-event
3727 (lambda (_proc _parsed)
3728 (with-current-buffer chnl-name
3729 (put 'erc-channel-banlist 'received-from-server t)
3730 (setq erc-server-367-functions old-367-hook)
3731 (erc-cmd-BANLIST)
3732 t)))
3733 (erc-server-send (format "MODE %s b" chnl)))))
3735 ((null erc-channel-banlist)
3736 (erc-display-line (erc-make-notice
3737 (format "No bans for channel: %s\n" chnl))
3738 'active)
3739 (put 'erc-channel-banlist 'received-from-server nil))
3742 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3743 erc-fill-column)
3744 (and (boundp 'fill-column)
3745 fill-column)
3746 (1- (window-width))))
3747 (separator (make-string erc-fill-column ?=))
3748 (fmt (concat
3749 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3750 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3752 (erc-display-line
3753 (erc-make-notice (format "Ban list for channel: %s\n"
3754 (erc-default-target)))
3755 'active)
3757 (erc-display-line separator 'active)
3758 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3759 (erc-display-line separator 'active)
3761 (mapc
3762 (lambda (x)
3763 (erc-display-line
3764 (format fmt
3765 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3766 (if (car x)
3767 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3768 ""))
3769 'active))
3770 erc-channel-banlist)
3772 (erc-display-line (erc-make-notice "End of Ban list")
3773 'active)
3774 (put 'erc-channel-banlist 'received-from-server nil)))))
3777 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3779 (defun erc-cmd-MASSUNBAN ()
3780 "Mass Unban.
3782 Unban all currently banned users in the current channel."
3783 (let ((chnl (erc-default-target)))
3784 (cond
3786 ((not (erc-channel-p chnl))
3787 (erc-display-line
3788 (erc-make-notice "You're not on a channel\n")
3789 'active))
3791 ((not (get 'erc-channel-banlist 'received-from-server))
3792 (let ((old-367-hook erc-server-367-functions))
3793 (setq erc-server-367-functions 'erc-banlist-store)
3794 ;; fetch the ban list then callback
3795 (erc-with-server-buffer
3796 (erc-once-with-server-event
3798 (lambda (_proc _parsed)
3799 (with-current-buffer chnl
3800 (put 'erc-channel-banlist 'received-from-server t)
3801 (setq erc-server-367-functions old-367-hook)
3802 (erc-cmd-MASSUNBAN)
3803 t)))
3804 (erc-server-send (format "MODE %s b" chnl)))))
3806 (t (let ((bans (mapcar #'cdr erc-channel-banlist)))
3807 (when bans
3808 ;; Glob the bans into groups of three, and carry out the unban.
3809 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3810 (mapc
3811 (lambda (x)
3812 (erc-server-send
3813 (format "MODE %s -%s %s" (erc-default-target)
3814 (make-string (length x) ?b)
3815 (mapconcat 'identity x " "))))
3816 (erc-group-list bans 3))))
3817 t))))
3819 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3821 ;;;; End of IRC commands
3823 (defun erc-ensure-channel-name (channel)
3824 "Return CHANNEL if it is a valid channel name.
3825 Eventually add a # in front of it, if that turns it into a valid channel name."
3826 (if (erc-channel-p channel)
3827 channel
3828 (concat "#" channel)))
3830 (defun erc-grab-region (start end)
3831 "Copy the region between START and END in a recreatable format.
3833 Converts all the IRC text properties in each line of the region
3834 into control codes and writes them to a separate buffer. The
3835 resulting text may be used directly as a script to generate this
3836 text again."
3837 (interactive "r")
3838 (erc-set-active-buffer (current-buffer))
3839 (save-excursion
3840 (let* ((cb (current-buffer))
3841 (buf (generate-new-buffer erc-grab-buffer-name))
3842 (region (buffer-substring start end))
3843 (lines (erc-split-multiline-safe region)))
3844 (set-buffer buf)
3845 (dolist (line lines)
3846 (insert (concat line "\n")))
3847 (set-buffer cb)
3848 (switch-to-buffer-other-window buf)))
3849 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3850 (ding))
3852 (defun erc-display-prompt (&optional buffer pos prompt face)
3853 "Display PROMPT in BUFFER at position POS.
3854 Display an ERC prompt in BUFFER.
3856 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3857 If BUFFER is nil, the `current-buffer' is used.
3858 If POS is nil, PROMPT will be displayed at `point'.
3859 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3860 `erc-prompt-face' will be used."
3861 (let* ((prompt (or prompt (erc-prompt)))
3862 (l (length prompt))
3863 (ob (current-buffer)))
3864 ;; We cannot use save-excursion because we move point, therefore
3865 ;; we resort to the ol' ob trick to restore this.
3866 (when (and buffer (bufferp buffer))
3867 (set-buffer buffer))
3869 ;; now save excursion again to store where point and mark are
3870 ;; in the current buffer
3871 (save-excursion
3872 (setq pos (or pos (point)))
3873 (goto-char pos)
3874 (when (> l 0)
3875 ;; Do not extend the text properties when typing at the end
3876 ;; of the prompt, but stuff typed in front of the prompt
3877 ;; shall remain part of the prompt.
3878 (setq prompt (erc-propertize prompt
3879 'start-open t ; XEmacs
3880 'rear-nonsticky t ; Emacs
3881 'erc-prompt t
3882 'field t
3883 'front-sticky t
3884 'read-only t))
3885 (erc-put-text-property 0 (1- (length prompt))
3886 'face (or face 'erc-prompt-face)
3887 prompt)
3888 (insert prompt))
3889 ;; Set the input marker
3890 (set-marker erc-input-marker (point)))
3892 ;; Now we are back at the old position. If the prompt was
3893 ;; inserted here or before us, advance point by the length of
3894 ;; the prompt.
3895 (when (or (not pos) (<= (point) pos))
3896 (forward-char l))
3897 ;; Clear the undo buffer now, so the user can undo his stuff,
3898 ;; but not the stuff we did. Sneaky!
3899 (setq buffer-undo-list nil)
3900 (set-buffer ob)))
3902 ;; interactive operations
3904 (defun erc-input-message ()
3905 "Read input from the minibuffer."
3906 (interactive)
3907 (let ((minibuffer-allow-text-properties t)
3908 (read-map minibuffer-local-map))
3909 (insert (read-from-minibuffer "Message: "
3910 (string (if (featurep 'xemacs)
3911 last-command-char
3912 last-command-event))
3913 read-map))
3914 (erc-send-current-line)))
3916 (defvar erc-action-history-list ()
3917 "History list for interactive action input.")
3919 (defun erc-input-action ()
3920 "Interactively input a user action and send it to IRC."
3921 (interactive "")
3922 (erc-set-active-buffer (current-buffer))
3923 (let ((action (read-from-minibuffer
3924 "Action: " nil nil nil 'erc-action-history-list)))
3925 (if (not (string-match "^\\s-*$" action))
3926 (erc-send-action (erc-default-target) action))))
3928 (defun erc-join-channel (channel &optional key)
3929 "Join CHANNEL.
3931 If `point' is at the beginning of a channel name, use that as default."
3932 (interactive
3933 (list
3934 (let ((chnl (if (looking-at "\\([&#+!][^ \n]+\\)") (match-string 1) ""))
3935 (table (when (erc-server-buffer-live-p)
3936 (set-buffer (process-buffer erc-server-process))
3937 erc-channel-list)))
3938 (completing-read "Join channel: " table nil nil nil nil chnl))
3939 (when (or current-prefix-arg erc-prompt-for-channel-key)
3940 (read-from-minibuffer "Channel key (RET for none): " nil))))
3941 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3943 (defun erc-part-from-channel (reason)
3944 "Part from the current channel and prompt for a REASON."
3945 (interactive
3946 (list
3947 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3948 reason
3949 (read-from-minibuffer (concat "Reason for leaving " (erc-default-target)
3950 ": ")))))
3951 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3953 (defun erc-set-topic (topic)
3954 "Prompt for a TOPIC for the current channel."
3955 (interactive
3956 (list
3957 (read-from-minibuffer
3958 (concat "Set topic of " (erc-default-target) ": ")
3959 (when erc-channel-topic
3960 (let ((ss (split-string erc-channel-topic "\C-o")))
3961 (cons (apply #'concat (if (cdr ss) (butlast ss) ss))
3962 0))))))
3963 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3964 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3966 (defun erc-set-channel-limit (&optional limit)
3967 "Set a LIMIT for the current channel. Remove limit if nil.
3968 Prompt for one if called interactively."
3969 (interactive (list (read-from-minibuffer
3970 (format "Limit for %s (RET to remove limit): "
3971 (erc-default-target)))))
3972 (let ((tgt (erc-default-target)))
3973 (erc-server-send (if (and limit (>= (length limit) 1))
3974 (format "MODE %s +l %s" tgt limit)
3975 (format "MODE %s -l" tgt)))))
3977 (defun erc-set-channel-key (&optional key)
3978 "Set a KEY for the current channel. Remove key if nil.
3979 Prompt for one if called interactively."
3980 (interactive (list (read-from-minibuffer
3981 (format "Key for %s (RET to remove key): "
3982 (erc-default-target)))))
3983 (let ((tgt (erc-default-target)))
3984 (erc-server-send (if (and key (>= (length key) 1))
3985 (format "MODE %s +k %s" tgt key)
3986 (format "MODE %s -k" tgt)))))
3988 (defun erc-quit-server (reason)
3989 "Disconnect from current server after prompting for REASON.
3990 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
3991 (interactive (list (read-from-minibuffer
3992 (format "Reason for quitting %s: "
3993 (or erc-server-announced-name
3994 erc-session-server)))))
3995 (erc-cmd-QUIT reason))
3997 ;; Movement of point
3999 (defun erc-bol ()
4000 "Move `point' to the beginning of the current line.
4002 This places `point' just after the prompt, or at the beginning of the line."
4003 (interactive)
4004 (forward-line 0)
4005 (when (get-text-property (point) 'erc-prompt)
4006 (goto-char erc-input-marker))
4007 (point))
4009 (defun erc-kill-input ()
4010 "Kill current input line using `erc-bol' followed by `kill-line'."
4011 (interactive)
4012 (when (and (erc-bol)
4013 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
4014 ;; there's nothing to kill
4015 (if (boundp 'erc-input-ring-index)
4016 (setq erc-input-ring-index nil))
4017 (kill-line)))
4019 (defun erc-complete-word-at-point ()
4020 (run-hook-with-args-until-success 'erc-complete-functions))
4022 (define-obsolete-function-alias 'erc-complete-word 'completion-at-point "24.1")
4024 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4026 ;; IRC SERVER INPUT HANDLING
4028 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4030 ;;;; New Input parsing
4032 ; Stolen from ZenIRC. I just wanna test this code, so here is
4033 ; experiment area.
4035 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
4036 erc-default-server-handler)
4037 "Default for server messages which aren't covered by `erc-server-hooks'."
4038 :group 'erc-server-hooks
4039 :type 'hook)
4041 (defun erc-default-server-handler (proc parsed)
4042 "Default server handler.
4044 Displays PROC and PARSED appropriately using `erc-display-message'."
4045 (erc-display-message
4046 parsed 'notice proc
4047 (mapconcat
4048 'identity
4049 (let (res)
4050 (mapc #'(lambda (x)
4051 (if (stringp x)
4052 (setq res (append res (list x)))))
4053 parsed)
4054 res)
4055 " ")))
4057 (defvar erc-server-vectors
4058 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
4059 "List of received server messages which ERC does not specifically handle.
4060 See `erc-debug-missing-hooks'.")
4061 ;(make-variable-buffer-local 'erc-server-vectors)
4063 (defun erc-debug-missing-hooks (_proc parsed)
4064 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
4065 These vectors can be helpful when adding new server message handlers to ERC.
4066 See `erc-default-server-hook'."
4067 (nconc erc-server-vectors (list parsed))
4068 nil)
4070 (defun erc-query (target server)
4071 "Open a query buffer on TARGET, using SERVER.
4072 To change how this query window is displayed, use `let' to bind
4073 `erc-join-buffer' before calling this."
4074 (unless (and server
4075 (buffer-live-p server)
4076 (set-buffer server))
4077 (error "Couldn't switch to server buffer"))
4078 (let ((buf (erc-open erc-session-server
4079 erc-session-port
4080 (erc-current-nick)
4081 erc-session-user-full-name
4084 (list target)
4085 target
4086 erc-server-process)))
4087 (unless buf
4088 (error "Couldn't open query window"))
4089 (erc-update-mode-line)
4090 buf))
4092 (defcustom erc-auto-query 'window-noselect
4093 "If non-nil, create a query buffer each time you receive a private message.
4094 If the buffer doesn't already exist, it is created.
4096 This can be set to a symbol, to control how the new query window
4097 should appear. The default behavior is to display the buffer in
4098 a new window, but not to select it. See the documentation for
4099 `erc-join-buffer' for a description of the available choices."
4100 :group 'erc-query
4101 :type '(choice (const :tag "Don't create query window" nil)
4102 (const :tag "Split window and select" window)
4103 (const :tag "Split window, don't select" window-noselect)
4104 (const :tag "New frame" frame)
4105 (const :tag "Bury in new buffer" bury)
4106 (const :tag "Use current buffer" buffer)
4107 (const :tag "Use current buffer" t)))
4109 (defcustom erc-query-on-unjoined-chan-privmsg t
4110 "If non-nil create query buffer on receiving any PRIVMSG at all.
4111 This includes PRIVMSGs directed to channels. If you are using an IRC
4112 bouncer, such as dircproxy, to keep a log of channels when you are
4113 disconnected, you should set this option to t."
4114 :group 'erc-query
4115 :type 'boolean)
4117 (defcustom erc-format-query-as-channel-p t
4118 "If non-nil, format text from others in a query buffer like in a channel,
4119 otherwise format like a private message."
4120 :group 'erc-query
4121 :type 'boolean)
4123 (defcustom erc-minibuffer-notice nil
4124 "If non-nil, print ERC notices for the user in the minibuffer.
4125 Only happens when the session buffer isn't visible."
4126 :group 'erc-display
4127 :type 'boolean)
4129 (defcustom erc-minibuffer-ignored nil
4130 "If non-nil, print a message in the minibuffer if we ignored something."
4131 :group 'erc-ignore
4132 :type 'boolean)
4134 (defun erc-wash-quit-reason (reason nick login host)
4135 "Remove duplicate text from quit REASON.
4136 Specifically in relation to NICK (user@host) information. Returns REASON
4137 unmodified if nothing can be removed.
4138 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
4139 \"Read error: 110\". The same applies for \"Ping Timeout\"."
4140 (setq nick (regexp-quote nick)
4141 login (regexp-quote login)
4142 host (regexp-quote host))
4143 (or (when (string-match (concat "^\\(Read error\\) to "
4144 nick "\\[" host "\\]: "
4145 "\\(.+\\)$")
4146 reason)
4147 (concat (match-string 1 reason) ": " (match-string 2 reason)))
4148 (when (string-match (concat "^\\(Ping timeout\\) for "
4149 nick "\\[" host "\\]$")
4150 reason)
4151 (match-string 1 reason))
4152 reason))
4154 (defun erc-nickname-in-use (nick reason)
4155 "If NICK is unavailable, tell the user the REASON.
4157 See also `erc-display-error-notice'."
4158 (if (or (not erc-try-new-nick-p)
4159 ;; how many default-nicks are left + one more try...
4160 (eq erc-nick-change-attempt-count
4161 (if (consp erc-nick)
4162 (+ (length erc-nick) 1)
4163 1)))
4164 (erc-display-error-notice
4166 (format "Nickname %s is %s, try another." nick reason))
4167 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
4168 (let ((newnick (nth 1 erc-default-nicks))
4169 (nicklen (cdr (assoc "NICKLEN"
4170 (erc-with-server-buffer
4171 erc-server-parameters)))))
4172 (setq erc-bad-nick t)
4173 ;; try to use a different nick
4174 (if erc-default-nicks
4175 (setq erc-default-nicks (cdr erc-default-nicks)))
4176 (if (not newnick)
4177 (setq newnick (concat (truncate-string-to-width
4178 nick
4179 (if (and erc-server-connected nicklen)
4180 (- (string-to-number nicklen)
4181 (length erc-nick-uniquifier))
4182 ;; rfc2812 max nick length = 9
4183 ;; we must assume this is the
4184 ;; server's setting if we haven't
4185 ;; established a connection yet
4186 (- 9 (length erc-nick-uniquifier))))
4187 erc-nick-uniquifier)))
4188 (erc-cmd-NICK newnick)
4189 (erc-display-error-notice
4191 (format "Nickname %s is %s, trying %s"
4192 nick reason newnick)))))
4194 ;;; Server messages
4196 (defgroup erc-server-hooks nil
4197 "Server event callbacks.
4198 Every server event - like numeric replies - has its own hook.
4199 Those hooks are all called using `run-hook-with-args-until-success'.
4200 They receive as first argument the process object from where the event
4201 originated from,
4202 and as second argument the event parsed as a vector."
4203 :group 'erc-hooks)
4205 (defun erc-display-server-message (_proc parsed)
4206 "Display the message sent by the server as a notice."
4207 (erc-display-message
4208 parsed 'notice 'active (erc-response.contents parsed)))
4210 (defun erc-auto-query (proc parsed)
4211 ;; FIXME: This needs more documentation, unless it's not a user function --
4212 ;; Lawrence 2004-01-08
4213 "Put this on `erc-server-PRIVMSG-functions'."
4214 (when erc-auto-query
4215 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
4216 (target (car (erc-response.command-args parsed)))
4217 (msg (erc-response.contents parsed))
4218 (query (if (not erc-query-on-unjoined-chan-privmsg)
4219 nick
4220 (if (erc-current-nick-p target)
4221 nick
4222 target))))
4223 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
4224 (or erc-query-on-unjoined-chan-privmsg
4225 (string= target (erc-current-nick)))
4226 (not (erc-get-buffer query proc))
4227 (not (erc-is-message-ctcp-and-not-action-p msg))
4228 (let ((erc-query-display erc-auto-query))
4229 (erc-cmd-QUERY query))
4230 nil))))
4232 (defun erc-is-message-ctcp-p (message)
4233 "Check if MESSAGE is a CTCP message or not."
4234 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
4236 (defun erc-is-message-ctcp-and-not-action-p (message)
4237 "Check if MESSAGE is a CTCP message or not."
4238 (and (erc-is-message-ctcp-p message)
4239 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
4241 (defun erc-format-privmessage (nick msg privp msgp)
4242 "Format a PRIVMSG in an insertable fashion."
4243 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
4244 (mark-e (if msgp (if privp "*" ">") "-"))
4245 (str (format "%s%s%s %s" mark-s nick mark-e msg))
4246 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
4247 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
4248 ;; add text properties to text before the nick, the nick and after the nick
4249 (erc-put-text-property 0 (length mark-s) 'face msg-face str)
4250 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
4251 'face nick-face str)
4252 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
4253 'face msg-face str)
4254 str))
4256 (defcustom erc-format-nick-function 'erc-format-nick
4257 "Function to format a nickname for message display."
4258 :group 'erc-display
4259 :type 'function)
4261 (defun erc-format-nick (&optional user _channel-data)
4262 "Return the nickname of USER.
4263 See also `erc-format-nick-function'."
4264 (when user (erc-server-user-nickname user)))
4266 (defun erc-get-user-mode-prefix (user)
4267 (when user
4268 (cond ((erc-channel-user-owner-p user)
4269 (erc-propertize "~" 'help-echo "owner"))
4270 ((erc-channel-user-admin-p user)
4271 (erc-propertize "&" 'help-echo "admin"))
4272 ((erc-channel-user-op-p user)
4273 (erc-propertize "@" 'help-echo "operator"))
4274 ((erc-channel-user-halfop-p user)
4275 (erc-propertize "%" 'help-echo "half-op"))
4276 ((erc-channel-user-voice-p user)
4277 (erc-propertize "+" 'help-echo "voice"))
4278 (t ""))))
4280 (defun erc-format-@nick (&optional user _channel-data)
4281 "Format the nickname of USER showing if USER has a voice, is an
4282 operator, half-op, admin or owner. Owners have \"~\", admins have
4283 \"&\", operators have \"@\" and users with voice have \"+\" as a
4284 prefix. Use CHANNEL-DATA to determine op and voice status. See
4285 also `erc-format-nick-function'."
4286 (when user
4287 (let ((nick (erc-server-user-nickname user)))
4288 (concat (erc-propertize
4289 (erc-get-user-mode-prefix nick)
4290 'face 'erc-nick-prefix-face)
4291 nick))))
4293 (defun erc-format-my-nick ()
4294 "Return the beginning of this user's message, correctly propertized."
4295 (if erc-show-my-nick
4296 (let* ((open "<")
4297 (close "> ")
4298 (nick (erc-current-nick))
4299 (mode (erc-get-user-mode-prefix nick)))
4300 (concat
4301 (erc-propertize open 'face 'erc-default-face)
4302 (erc-propertize mode 'face 'erc-my-nick-prefix-face)
4303 (erc-propertize nick 'face 'erc-my-nick-face)
4304 (erc-propertize close 'face 'erc-default-face)))
4305 (let ((prefix "> "))
4306 (erc-propertize prefix 'face 'erc-default-face))))
4308 (defun erc-echo-notice-in-default-buffer (s parsed buffer _sender)
4309 "Echos a private notice in the default buffer, namely the
4310 target buffer specified by BUFFER, or there is no target buffer,
4311 the server buffer. This function is designed to be added to
4312 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
4313 and always returns t."
4314 (erc-display-message parsed nil buffer s)
4317 (defun erc-echo-notice-in-target-buffer (s parsed buffer _sender)
4318 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
4319 function is designed to be added to either `erc-echo-notice-hook'
4320 or `erc-echo-notice-always-hook', and returns non-nil if BUFFER
4321 is non-nil."
4322 (if buffer
4323 (progn (erc-display-message parsed nil buffer s) t)
4324 nil))
4326 (defun erc-echo-notice-in-minibuffer (s _parsed _buffer _sender)
4327 "Echos a private notice in the minibuffer. This function is
4328 designed to be added to either `erc-echo-notice-hook' or
4329 `erc-echo-notice-always-hook', and always returns t."
4330 (message "%s" (concat "NOTICE: " s))
4333 (defun erc-echo-notice-in-server-buffer (s parsed _buffer _sender)
4334 "Echos a private notice in the server buffer. 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 (erc-display-message parsed nil nil s)
4340 (defun erc-echo-notice-in-active-non-server-buffer (s parsed _buffer _sender)
4341 "Echos a private notice in the active buffer if the active
4342 buffer is not the server buffer. This function is designed to be
4343 added to either `erc-echo-notice-hook' or
4344 `erc-echo-notice-always-hook', and returns non-nil if the active
4345 buffer is not the server buffer."
4346 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
4347 (progn (erc-display-message parsed nil 'active s) t)
4348 nil))
4350 (defun erc-echo-notice-in-active-buffer (s parsed _buffer _sender)
4351 "Echos a private notice in the active buffer. This function is
4352 designed to be added to either `erc-echo-notice-hook' or
4353 `erc-echo-notice-always-hook', and always returns t."
4354 (erc-display-message parsed nil 'active s)
4357 (defun erc-echo-notice-in-user-buffers (s parsed _buffer sender)
4358 "Echos a private notice in all of the buffers for which SENDER
4359 is a member. This function is designed to be added to either
4360 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4361 returns non-nil if there is at least one buffer for which the
4362 sender is a member.
4364 See also: `erc-echo-notice-in-first-user-buffer',
4365 `erc-buffer-list-with-nick'."
4366 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4367 (if buffers
4368 (progn (erc-display-message parsed nil buffers s) t)
4369 nil)))
4371 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
4372 "Echos a private notice in BUFFER and in all of the buffers for
4373 which SENDER is a member. This function is designed to be added
4374 to either `erc-echo-notice-hook' or
4375 `erc-echo-notice-always-hook', and returns non-nil if there is
4376 at least one buffer for which the sender is a member or the
4377 default target.
4379 See also: `erc-echo-notice-in-user-buffers',
4380 `erc-buffer-list-with-nick'."
4381 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4382 (unless (memq buffer buffers) (push buffer buffers))
4383 (if buffers ;FIXME: How could it be nil?
4384 (progn (erc-display-message parsed nil buffers s) t)
4385 nil)))
4387 (defun erc-echo-notice-in-first-user-buffer (s parsed _buffer sender)
4388 "Echos a private notice in one of the buffers for which SENDER
4389 is a member. This function is designed to be added to either
4390 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4391 returns non-nil if there is at least one buffer for which the
4392 sender is a member.
4394 See also: `erc-echo-notice-in-user-buffers',
4395 `erc-buffer-list-with-nick'."
4396 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4397 (if buffers
4398 (progn (erc-display-message parsed nil (car buffers) s) t)
4399 nil)))
4401 ;;; Ban manipulation
4403 (defun erc-banlist-store (proc parsed)
4404 "Record ban entries for a channel."
4405 (pcase-let ((`(,channel ,mask ,whoset)
4406 (cdr (erc-response.command-args parsed))))
4407 ;; Determine to which buffer the message corresponds
4408 (let ((buffer (erc-get-buffer channel proc)))
4409 (with-current-buffer buffer
4410 (unless (member (cons whoset mask) erc-channel-banlist)
4411 (setq erc-channel-banlist (cons (cons whoset mask)
4412 erc-channel-banlist))))))
4413 nil)
4415 (defun erc-banlist-finished (proc parsed)
4416 "Record that we have received the banlist."
4417 (let* ((channel (nth 1 (erc-response.command-args parsed)))
4418 (buffer (erc-get-buffer channel proc)))
4419 (with-current-buffer buffer
4420 (put 'erc-channel-banlist 'received-from-server t)))
4421 t) ; suppress the 'end of banlist' message
4423 (defun erc-banlist-update (proc parsed)
4424 "Check MODE commands for bans and update the banlist appropriately."
4425 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
4426 (let* ((tgt (car (erc-response.command-args parsed)))
4427 (mode (erc-response.contents parsed))
4428 (whoset (erc-response.sender parsed))
4429 (buffer (erc-get-buffer tgt proc)))
4430 (when buffer
4431 (with-current-buffer buffer
4432 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
4433 ((string-match "^\\([+-]\\)b" mode)
4434 ;; This is a ban
4435 (cond
4436 ((string-match "^-" mode)
4437 ;; Remove the unbanned masks from the ban list
4438 (setq erc-channel-banlist
4439 (erc-delete-if
4440 #'(lambda (y)
4441 (member (upcase (cdr y))
4442 (mapcar #'upcase
4443 (cdr (split-string mode)))))
4444 erc-channel-banlist)))
4445 ((string-match "^+" mode)
4446 ;; Add the banned mask(s) to the ban list
4447 (mapc
4448 (lambda (mask)
4449 (unless (member (cons whoset mask) erc-channel-banlist)
4450 (setq erc-channel-banlist
4451 (cons (cons whoset mask) erc-channel-banlist))))
4452 (cdr (split-string mode))))))))))
4453 nil)
4455 ;; used for the banlist cmds
4456 (defun erc-group-list (list n)
4457 "Group LIST into sublists of length N."
4458 (cond ((null list) nil)
4459 ((null (nthcdr n list)) (list list))
4460 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
4463 ;;; MOTD numreplies
4465 (defun erc-handle-login ()
4466 "Handle the logging in process of connection."
4467 (unless erc-logged-in
4468 (setq erc-logged-in t)
4469 (message "Logging in as \'%s\'... done" (erc-current-nick))
4470 ;; execute a startup script
4471 (let ((f (erc-select-startup-file)))
4472 (when f
4473 (erc-load-script f)))))
4475 (defun erc-connection-established (proc parsed)
4476 "Run just after connection.
4478 Set user modes and run `erc-after-connect' hook."
4479 (with-current-buffer (process-buffer proc)
4480 (unless erc-server-connected ; only once per session
4481 (let ((server (or erc-server-announced-name
4482 (erc-response.sender parsed)))
4483 (nick (car (erc-response.command-args parsed)))
4484 (buffer (process-buffer proc)))
4485 (setq erc-server-connected t)
4486 (erc-update-mode-line)
4487 (erc-set-initial-user-mode nick buffer)
4488 (erc-server-setup-periodical-ping buffer)
4489 (run-hook-with-args 'erc-after-connect server nick)))))
4491 (defun erc-set-initial-user-mode (nick buffer)
4492 "If `erc-user-mode' is non-nil for NICK, set the user modes.
4493 The server buffer is given by BUFFER."
4494 (with-current-buffer buffer
4495 (when erc-user-mode
4496 (let ((mode (if (functionp erc-user-mode)
4497 (funcall erc-user-mode)
4498 erc-user-mode)))
4499 (when (stringp mode)
4500 (erc-log (format "changing mode for %s to %s" nick mode))
4501 (erc-server-send (format "MODE %s %s" nick mode)))))))
4503 (defun erc-display-error-notice (parsed string)
4504 "Display STRING as an error notice.
4506 See also `erc-display-message'."
4507 (erc-display-message
4508 parsed '(notice error) 'active string))
4510 (defun erc-process-ctcp-query (proc parsed nick login host)
4511 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4512 "Process a CTCP query."
4513 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4514 "\C-a"))))
4515 (if (> (length queries) 4)
4516 (erc-display-message
4517 parsed (list 'notice 'error) proc 'ctcp-too-many)
4518 (if (= 0 (length queries))
4519 (erc-display-message
4520 parsed (list 'notice 'error) proc
4521 'ctcp-empty ?n nick)
4522 (while queries
4523 (let* ((type (upcase (car (split-string (car queries)))))
4524 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4525 (if (and hook (boundp hook))
4526 (if (string-equal type "ACTION")
4527 (run-hook-with-args-until-success
4528 hook proc parsed nick login host
4529 (car (erc-response.command-args parsed))
4530 (car queries))
4531 (when erc-paranoid
4532 (if (erc-current-nick-p
4533 (car (erc-response.command-args parsed)))
4534 (erc-display-message
4535 parsed 'error 'active 'ctcp-request
4536 ?n nick ?u login ?h host ?r (car queries))
4537 (erc-display-message
4538 parsed 'error 'active 'ctcp-request-to
4539 ?n nick ?u login ?h host ?r (car queries)
4540 ?t (car (erc-response.command-args parsed)))))
4541 (run-hook-with-args-until-success
4542 hook proc nick login host
4543 (car (erc-response.command-args parsed))
4544 (car queries)))
4545 (erc-display-message
4546 parsed (list 'notice 'error) proc
4547 'undefined-ctcp)))
4548 (setq queries (cdr queries)))))))
4550 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4552 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4553 "Respond to a CTCP ACTION query."
4554 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4555 (let ((s (match-string 1 msg))
4556 (buf (or (erc-get-buffer to proc)
4557 (erc-get-buffer nick proc)
4558 (process-buffer proc))))
4559 (erc-display-message
4560 parsed 'action buf
4561 'ACTION ?n nick ?u login ?h host ?a s))))
4563 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4565 (defun erc-ctcp-query-CLIENTINFO (_proc nick _login _host _to msg)
4566 "Respond to a CTCP CLIENTINFO query."
4567 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4568 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4569 (unless erc-disable-ctcp-replies
4570 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4571 nil)
4573 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4574 (defun erc-ctcp-query-ECHO (_proc nick _login _host _to msg)
4575 "Respond to a CTCP ECHO query."
4576 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4577 (let ((s (match-string 1 msg)))
4578 (unless erc-disable-ctcp-replies
4579 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4580 nil)
4582 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4583 (defun erc-ctcp-query-FINGER (_proc nick _login _host _to _msg)
4584 "Respond to a CTCP FINGER query."
4585 (unless erc-disable-ctcp-replies
4586 (let ((s (if erc-anonymous-login
4587 (format "FINGER I'm %s." (erc-current-nick))
4588 (format "FINGER %s (%s@%s)."
4589 (user-full-name)
4590 (user-login-name)
4591 (system-name))))
4592 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4593 (when (> ns 0)
4594 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4595 (erc-send-ctcp-notice nick s)))
4596 nil)
4598 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4599 (defun erc-ctcp-query-PING (_proc nick _login _host _to msg)
4600 "Respond to a CTCP PING query."
4601 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4602 (unless erc-disable-ctcp-replies
4603 (let ((arg (match-string 1 msg)))
4604 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4605 nil)
4607 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4608 (defun erc-ctcp-query-TIME (_proc nick _login _host _to _msg)
4609 "Respond to a CTCP TIME query."
4610 (unless erc-disable-ctcp-replies
4611 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4612 nil)
4614 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4615 (defun erc-ctcp-query-USERINFO (_proc nick _login _host _to _msg)
4616 "Respond to a CTCP USERINFO query."
4617 (unless erc-disable-ctcp-replies
4618 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4619 nil)
4621 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4622 (defun erc-ctcp-query-VERSION (_proc nick _login _host _to _msg)
4623 "Respond to a CTCP VERSION query."
4624 (unless erc-disable-ctcp-replies
4625 (erc-send-ctcp-notice
4626 nick (format
4627 "VERSION \C-bERC\C-b - an IRC client for Emacs %s (\C-b%s\C-b)"
4628 emacs-version
4629 erc-official-location)))
4630 nil)
4632 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4633 "Process MSG as a CTCP reply."
4634 (let* ((type (car (split-string msg)))
4635 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4636 (if (boundp hook)
4637 (run-hook-with-args-until-success
4638 hook proc nick login host
4639 (car (erc-response.command-args parsed)) msg)
4640 (erc-display-message
4641 parsed 'notice 'active
4642 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4644 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4645 (defun erc-ctcp-reply-ECHO (_proc nick _login _host _to msg)
4646 "Handle a CTCP ECHO reply."
4647 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4648 (let ((message (match-string 1 msg)))
4649 (erc-display-message
4650 nil '(notice action) 'active
4651 'CTCP-ECHO ?n nick ?m message)))
4652 nil)
4654 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4655 (defun erc-ctcp-reply-CLIENTINFO (_proc nick _login _host _to msg)
4656 "Handle a CTCP CLIENTINFO reply."
4657 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4658 (let ((message (match-string 1 msg)))
4659 (erc-display-message
4660 nil 'notice 'active
4661 'CTCP-CLIENTINFO ?n nick ?m message)))
4662 nil)
4664 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4665 (defun erc-ctcp-reply-FINGER (_proc nick _login _host _to msg)
4666 "Handle a CTCP FINGER reply."
4667 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4668 (let ((message (match-string 1 msg)))
4669 (erc-display-message
4670 nil 'notice 'active
4671 'CTCP-FINGER ?n nick ?m message)))
4672 nil)
4674 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4675 (defun erc-ctcp-reply-PING (_proc nick _login _host _to msg)
4676 "Handle a CTCP PING reply."
4677 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4679 (let ((time (match-string 1 msg)))
4680 (condition-case nil
4681 (let ((delta (erc-time-diff (string-to-number time)
4682 (erc-current-time))))
4683 (erc-display-message
4684 nil 'notice 'active
4685 'CTCP-PING ?n nick
4686 ?t (erc-sec-to-time delta)))
4687 (range-error
4688 (erc-display-message
4689 nil 'error 'active
4690 'bad-ping-response ?n nick ?t time))))))
4692 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4693 (defun erc-ctcp-reply-TIME (_proc nick _login _host _to msg)
4694 "Handle a CTCP TIME reply."
4695 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4696 (let ((message (match-string 1 msg)))
4697 (erc-display-message
4698 nil 'notice 'active
4699 'CTCP-TIME ?n nick ?m message)))
4700 nil)
4702 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4703 (defun erc-ctcp-reply-VERSION (_proc nick _login _host _to msg)
4704 "Handle a CTCP VERSION reply."
4705 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4706 (let ((message (match-string 1 msg)))
4707 (erc-display-message
4708 nil 'notice 'active
4709 'CTCP-VERSION ?n nick ?m message)))
4710 nil)
4712 (defun erc-process-away (proc away-p)
4713 "Toggle the away status of the user depending on the value of AWAY-P.
4715 If nil, set the user as away.
4716 If non-nil, return from being away."
4717 (let ((sessionbuf (process-buffer proc)))
4718 (when sessionbuf
4719 (with-current-buffer sessionbuf
4720 (when erc-away-nickname
4721 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4722 erc-away-nickname away-p))
4723 (erc-cmd-NICK (if away-p
4724 erc-away-nickname
4725 erc-nick)))
4726 (cond
4727 (away-p
4728 (setq erc-away (current-time)))
4730 (let ((away-time erc-away))
4731 ;; away must be set to NIL BEFORE sending anything to prevent
4732 ;; an infinite recursion
4733 (setq erc-away nil)
4734 (with-current-buffer (erc-active-buffer)
4735 (when erc-public-away-p
4736 (erc-send-action
4737 (erc-default-target)
4738 (if away-time
4739 (format "is back (gone for %s)"
4740 (erc-sec-to-time
4741 (erc-time-diff
4742 (erc-emacs-time-to-erc-time away-time)
4743 (erc-current-time))))
4744 "is back")))))))))
4745 (erc-update-mode-line)))
4747 ;;;; List of channel members handling
4749 (defun erc-channel-begin-receiving-names ()
4750 "Internal function.
4752 Used when a channel names list is about to be received. Should
4753 be called with the current buffer set to the channel buffer.
4755 See also `erc-channel-end-receiving-names'."
4756 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4758 (defun erc-channel-end-receiving-names ()
4759 "Internal function.
4761 Used to fix `erc-channel-users' after a channel names list has been
4762 received. Should be called with the current buffer set to the
4763 channel buffer.
4765 See also `erc-channel-begin-receiving-names'."
4766 (maphash (lambda (nick _user)
4767 (if (null (gethash nick erc-channel-new-member-names))
4768 (erc-remove-channel-user nick)))
4769 erc-channel-users)
4770 (setq erc-channel-new-member-names nil))
4772 (defun erc-parse-prefix ()
4773 "Return an alist of valid prefix character types and their representations.
4774 Example: (operator) o => @, (voiced) v => +."
4775 (let ((str (or (cdr (assoc "PREFIX" (erc-with-server-buffer
4776 erc-server-parameters)))
4777 ;; provide a sane default
4778 "(qaohv)~&@%+"))
4779 types chars)
4780 (when (string-match "^(\\([^)]+\\))\\(.+\\)$" str)
4781 (setq types (match-string 1 str)
4782 chars (match-string 2 str))
4783 (let ((len (min (length types) (length chars)))
4784 (i 0)
4785 (alist nil))
4786 (while (< i len)
4787 (setq alist (cons (cons (elt types i) (elt chars i))
4788 alist))
4789 (setq i (1+ i)))
4790 alist))))
4792 (defun erc-channel-receive-names (names-string)
4793 "This function is for internal use only.
4795 Update `erc-channel-users' according to NAMES-STRING.
4796 NAMES-STRING is a string listing some of the names on the
4797 channel."
4798 (let* ((prefix (erc-parse-prefix))
4799 (voice-ch (cdr (assq ?v prefix)))
4800 (op-ch (cdr (assq ?o prefix)))
4801 (hop-ch (cdr (assq ?h prefix)))
4802 (adm-ch (cdr (assq ?a prefix)))
4803 (own-ch (cdr (assq ?q prefix)))
4804 (names (delete "" (split-string names-string)))
4805 name op voice halfop admin owner)
4806 (let ((erc-channel-members-changed-hook nil))
4807 (dolist (item names)
4808 (let ((updatep t)
4809 (ch (aref item 0)))
4810 (setq name item op 'off voice 'off halfop 'off admin 'off owner 'off)
4811 (if (rassq ch prefix)
4812 (if (= (length item) 1)
4813 (setq updatep nil)
4814 (setq name (substring item 1))
4815 (setf (pcase ch
4816 ((pred (eq voice-ch)) voice)
4817 ((pred (eq hop-ch)) halfop)
4818 ((pred (eq op-ch)) op)
4819 ((pred (eq adm-ch)) admin)
4820 ((pred (eq own-ch)) owner)
4821 (_ (error "Unknown prefix char `%S'" ch) voice))
4822 'on)))
4823 (when updatep
4824 (puthash (erc-downcase name) t
4825 erc-channel-new-member-names)
4826 (erc-update-current-channel-member
4827 name name t voice halfop op admin owner)))))
4828 (run-hooks 'erc-channel-members-changed-hook)))
4831 (defcustom erc-channel-members-changed-hook nil
4832 "This hook is called every time the variable `channel-members' changes.
4833 The buffer where the change happened is current while this hook is called."
4834 :group 'erc-hooks
4835 :type 'hook)
4837 (defun erc-update-user-nick (nick &optional new-nick
4838 host login full-name info)
4839 "Update the stored user information for the user with nickname NICK.
4841 See also: `erc-update-user'."
4842 (erc-update-user (erc-get-server-user nick) new-nick
4843 host login full-name info))
4845 (defun erc-update-user (user &optional new-nick
4846 host login full-name info)
4847 "Update user info for USER. USER must be an erc-server-user
4848 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4849 non-nil and not equal to the existing values for USER are used to
4850 replace the stored values in USER.
4852 If, and only if, a change is made,
4853 `erc-channel-members-changed-hook' is run for each channel for
4854 which USER is a member, and t is returned."
4855 (let (changed)
4856 (when user
4857 (when (and new-nick
4858 (not (equal (erc-server-user-nickname user)
4859 new-nick)))
4860 (setq changed t)
4861 (erc-change-user-nickname user new-nick))
4862 (when (and host
4863 (not (equal (erc-server-user-host user) host)))
4864 (setq changed t)
4865 (setf (erc-server-user-host user) host))
4866 (when (and login
4867 (not (equal (erc-server-user-login user) login)))
4868 (setq changed t)
4869 (setf (erc-server-user-login user) login))
4870 (when (and full-name
4871 (not (equal (erc-server-user-full-name user)
4872 full-name)))
4873 (setq changed t)
4874 (setf (erc-server-user-full-name user) full-name))
4875 (when (and info
4876 (not (equal (erc-server-user-info user) info)))
4877 (setq changed t)
4878 (setf (erc-server-user-info user) info))
4879 (if changed
4880 (dolist (buf (erc-server-user-buffers user))
4881 (if (buffer-live-p buf)
4882 (with-current-buffer buf
4883 (run-hooks 'erc-channel-members-changed-hook))))))
4884 changed))
4886 (defun erc-update-current-channel-member
4887 (nick new-nick &optional add voice halfop op admin owner host login full-name info
4888 update-message-time)
4889 "Update the stored user information for the user with nickname NICK.
4890 `erc-update-user' is called to handle changes to nickname,
4891 HOST, LOGIN, FULL-NAME, and INFO. If VOICE HALFOP OP ADMIN or OWNER
4892 are non-nil, they must be equal to either `on' or `off', in which
4893 case the status of the user in the current channel is changed accordingly.
4894 If UPDATE-MESSAGE-TIME is non-nil, the last-message-time of the user
4895 in the current channel is set to (current-time).
4897 If ADD is non-nil, the user will be added with the specified
4898 information if it is not already present in the user or channel
4899 lists.
4901 If, and only if, changes are made, or the user is added,
4902 `erc-channel-members-updated-hook' is run, and t is returned.
4904 See also: `erc-update-user' and `erc-update-channel-member'."
4905 (let* (changed user-changed
4906 (channel-data (erc-get-channel-user nick))
4907 (cuser (cdr channel-data))
4908 (user (if channel-data (car channel-data)
4909 (erc-get-server-user nick))))
4910 (if cuser
4911 (progn
4912 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4913 (when (and voice
4914 (not (eq (erc-channel-user-voice cuser) voice)))
4915 (setq changed t)
4916 (setf (erc-channel-user-voice cuser)
4917 (cond ((eq voice 'on) t)
4918 ((eq voice 'off) nil)
4919 (t voice))))
4920 (when (and halfop
4921 (not (eq (erc-channel-user-halfop cuser) halfop)))
4922 (setq changed t)
4923 (setf (erc-channel-user-halfop cuser)
4924 (cond ((eq halfop 'on) t)
4925 ((eq halfop 'off) nil)
4926 (t halfop))))
4927 (when (and op
4928 (not (eq (erc-channel-user-op cuser) op)))
4929 (setq changed t)
4930 (setf (erc-channel-user-op cuser)
4931 (cond ((eq op 'on) t)
4932 ((eq op 'off) nil)
4933 (t op))))
4934 (when (and admin
4935 (not (eq (erc-channel-user-admin cuser) admin)))
4936 (setq changed t)
4937 (setf (erc-channel-user-admin cuser)
4938 (cond ((eq admin 'on) t)
4939 ((eq admin 'off) nil)
4940 (t admin))))
4941 (when (and owner
4942 (not (eq (erc-channel-user-owner cuser) owner)))
4943 (setq changed t)
4944 (setf (erc-channel-user-owner cuser)
4945 (cond ((eq owner 'on) t)
4946 ((eq owner 'off) nil)
4947 (t owner))))
4948 (when update-message-time
4949 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4950 (setq user-changed
4951 (erc-update-user user new-nick
4952 host login full-name info)))
4953 (when add
4954 (if (null user)
4955 (progn
4956 (setq user (make-erc-server-user
4957 :nickname nick
4958 :host host
4959 :full-name full-name
4960 :login login
4961 :info info
4962 :buffers (list (current-buffer))))
4963 (erc-add-server-user nick user))
4964 (setf (erc-server-user-buffers user)
4965 (cons (current-buffer)
4966 (erc-server-user-buffers user))))
4967 (setq cuser (make-erc-channel-user
4968 :voice (cond ((eq voice 'on) t)
4969 ((eq voice 'off) nil)
4970 (t voice))
4971 :halfop (cond ((eq halfop 'on) t)
4972 ((eq halfop 'off) nil)
4973 (t halfop))
4974 :op (cond ((eq op 'on) t)
4975 ((eq op 'off) nil)
4976 (t op))
4977 :admin (cond ((eq admin 'on) t)
4978 ((eq admin 'off) nil)
4979 (t admin))
4980 :owner (cond ((eq owner 'on) t)
4981 ((eq owner 'off) nil)
4982 (t owner))
4983 :last-message-time
4984 (if update-message-time (current-time))))
4985 (puthash (erc-downcase nick) (cons user cuser)
4986 erc-channel-users)
4987 (setq changed t)))
4988 (when (and changed (null user-changed))
4989 (run-hooks 'erc-channel-members-changed-hook))
4990 (or changed user-changed add)))
4992 (defun erc-update-channel-member (channel nick new-nick
4993 &optional add voice halfop op admin owner host login
4994 full-name info update-message-time)
4995 "Update user and channel information for the user with
4996 nickname NICK in channel CHANNEL.
4998 See also: `erc-update-current-channel-member'."
4999 (erc-with-buffer
5000 (channel)
5001 (erc-update-current-channel-member nick new-nick add voice halfop op admin owner host
5002 login full-name info
5003 update-message-time)))
5005 (defun erc-remove-current-channel-member (nick)
5006 "Remove NICK from current channel membership list.
5007 Runs `erc-channel-members-changed-hook'."
5008 (let ((channel-data (erc-get-channel-user nick)))
5009 (when channel-data
5010 (erc-remove-channel-user nick)
5011 (run-hooks 'erc-channel-members-changed-hook))))
5013 (defun erc-remove-channel-member (channel nick)
5014 "Remove NICK from CHANNEL's membership list.
5016 See also `erc-remove-current-channel-member'."
5017 (erc-with-buffer
5018 (channel)
5019 (erc-remove-current-channel-member nick)))
5021 (defun erc-update-channel-topic (channel topic &optional modify)
5022 "Find a buffer for CHANNEL and set the TOPIC for it.
5024 If optional MODIFY is 'append or 'prepend, then append or prepend the
5025 TOPIC string to the current topic."
5026 (erc-with-buffer (channel)
5027 (cond ((eq modify 'append)
5028 (setq erc-channel-topic (concat erc-channel-topic topic)))
5029 ((eq modify 'prepend)
5030 (setq erc-channel-topic (concat topic erc-channel-topic)))
5031 (t (setq erc-channel-topic topic)))
5032 (erc-update-mode-line-buffer (current-buffer))))
5034 (defun erc-set-modes (tgt mode-string)
5035 "Set the modes for the TGT provided as MODE-STRING."
5036 (let* ((modes (erc-parse-modes mode-string))
5037 (add-modes (nth 0 modes))
5038 ;; list of triples: (mode-char 'on/'off argument)
5039 (arg-modes (nth 2 modes)))
5040 (cond ((erc-channel-p tgt); channel modes
5041 (let ((buf (and erc-server-process
5042 (erc-get-buffer tgt erc-server-process))))
5043 (when buf
5044 (with-current-buffer buf
5045 (setq erc-channel-modes add-modes)
5046 (setq erc-channel-user-limit nil)
5047 (setq erc-channel-key nil)
5048 (while arg-modes
5049 (let ((mode (nth 0 (car arg-modes)))
5050 (onoff (nth 1 (car arg-modes)))
5051 (arg (nth 2 (car arg-modes))))
5052 (cond ((string-match "^[Ll]" mode)
5053 (erc-update-channel-limit tgt onoff arg))
5054 ((string-match "^[Kk]" mode)
5055 (erc-update-channel-key tgt onoff arg))
5056 (t nil)))
5057 (setq arg-modes (cdr arg-modes)))
5058 (erc-update-mode-line-buffer buf)))))
5059 ;; we do not keep our nick's modes yet
5060 ;;(t (setq erc-user-modes add-modes))
5064 (defun erc-sort-strings (list-of-strings)
5065 "Sort LIST-OF-STRINGS in lexicographic order.
5067 Side-effect free."
5068 (sort (copy-sequence list-of-strings) 'string<))
5070 (defun erc-parse-modes (mode-string)
5071 "Parse MODE-STRING into a list.
5073 Returns a list of three elements:
5075 (ADD-MODES REMOVE-MODES ARG-MODES).
5077 The add-modes and remove-modes are lists of single-character strings
5078 for modes without parameters to add and remove respectively. The
5079 arg-modes is a list of triples of the form:
5081 (MODE-CHAR ON/OFF ARGUMENT)."
5082 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
5083 (let ((chars (mapcar #'char-to-string (match-string 1 mode-string)))
5084 ;; arguments in channel modes
5085 (args-str (match-string 2 mode-string))
5086 (args nil)
5087 (add-modes nil)
5088 (remove-modes nil)
5089 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
5090 (add-p t))
5091 ;; make the argument list
5092 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
5093 (setq args (cons (match-string 1 args-str) args))
5094 (setq args-str (match-string 2 args-str)))
5095 (setq args (nreverse args))
5096 ;; collect what modes changed, and match them with arguments
5097 (while chars
5098 (cond ((string= (car chars) "+") (setq add-p t))
5099 ((string= (car chars) "-") (setq add-p nil))
5100 ((string-match "^[qaovhbQAOVHB]" (car chars))
5101 (setq arg-modes (cons (list (car chars)
5102 (if add-p 'on 'off)
5103 (if args (car args) nil))
5104 arg-modes))
5105 (if args (setq args (cdr args))))
5106 ((string-match "^[LlKk]" (car chars))
5107 (setq arg-modes (cons (list (car chars)
5108 (if add-p 'on 'off)
5109 (if (and add-p args)
5110 (car args) nil))
5111 arg-modes))
5112 (if (and add-p args) (setq args (cdr args))))
5113 (add-p (setq add-modes (cons (car chars) add-modes)))
5114 (t (setq remove-modes (cons (car chars) remove-modes))))
5115 (setq chars (cdr chars)))
5116 (setq add-modes (nreverse add-modes))
5117 (setq remove-modes (nreverse remove-modes))
5118 (setq arg-modes (nreverse arg-modes))
5119 (list add-modes remove-modes arg-modes))
5120 nil))
5122 (defun erc-update-modes (tgt mode-string &optional _nick _host _login)
5123 "Update the mode information for TGT, provided as MODE-STRING.
5124 Optional arguments: NICK, HOST and LOGIN - the attributes of the
5125 person who changed the modes."
5126 ;; FIXME: neither of nick, host, and login are used!
5127 (let* ((modes (erc-parse-modes mode-string))
5128 (add-modes (nth 0 modes))
5129 (remove-modes (nth 1 modes))
5130 ;; list of triples: (mode-char 'on/'off argument)
5131 (arg-modes (nth 2 modes)))
5132 ;; now parse the modes changes and do the updates
5133 (cond ((erc-channel-p tgt); channel modes
5134 (let ((buf (and erc-server-process
5135 (erc-get-buffer tgt erc-server-process))))
5136 (when buf
5137 ;; FIXME! This used to have an original buffer
5138 ;; variable, but it never switched back to the original
5139 ;; buffer. Is this wanted behavior?
5140 (set-buffer buf)
5141 (if (not (boundp 'erc-channel-modes))
5142 (setq erc-channel-modes nil))
5143 (while remove-modes
5144 (setq erc-channel-modes (delete (car remove-modes)
5145 erc-channel-modes)
5146 remove-modes (cdr remove-modes)))
5147 (while add-modes
5148 (setq erc-channel-modes (cons (car add-modes)
5149 erc-channel-modes)
5150 add-modes (cdr add-modes)))
5151 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
5152 (while arg-modes
5153 (let ((mode (nth 0 (car arg-modes)))
5154 (onoff (nth 1 (car arg-modes)))
5155 (arg (nth 2 (car arg-modes))))
5156 (cond ((string-match "^[Vv]" mode)
5157 (erc-update-channel-member tgt arg arg nil onoff))
5158 ((string-match "^[hH]" mode)
5159 (erc-update-channel-member tgt arg arg nil nil onoff))
5160 ((string-match "^[oO]" mode)
5161 (erc-update-channel-member tgt arg arg nil nil nil onoff))
5162 ((string-match "^[aA]" mode)
5163 (erc-update-channel-member tgt arg arg nil nil nil nil onoff))
5164 ((string-match "^[qQ]" mode)
5165 (erc-update-channel-member tgt arg arg nil nil nil nil nil onoff))
5166 ((string-match "^[Ll]" mode)
5167 (erc-update-channel-limit tgt onoff arg))
5168 ((string-match "^[Kk]" mode)
5169 (erc-update-channel-key tgt onoff arg))
5170 (t nil)); only ops are tracked now
5171 (setq arg-modes (cdr arg-modes))))
5172 (erc-update-mode-line buf))))
5173 ;; nick modes - ignored at this point
5174 (t nil))))
5176 (defun erc-update-channel-limit (channel onoff n)
5177 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
5178 "Update CHANNEL's user limit to N."
5179 (if (or (not (eq onoff 'on))
5180 (and (stringp n) (string-match "^[0-9]+$" n)))
5181 (erc-with-buffer
5182 (channel)
5183 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
5184 (t (setq erc-channel-user-limit nil))))))
5186 (defun erc-update-channel-key (channel onoff key)
5187 "Update CHANNEL's key to KEY if ONOFF is 'on or to nil if it's 'off."
5188 (erc-with-buffer
5189 (channel)
5190 (cond ((eq onoff 'on) (setq erc-channel-key key))
5191 (t (setq erc-channel-key nil)))))
5193 (defun erc-handle-user-status-change (type nlh &optional l)
5194 "Handle changes in any user's status.
5196 So far, only nick change is handled.
5198 Generally, the TYPE argument is a symbol describing the change type, NLH is
5199 a list containing the original nickname, login name and hostname for the user,
5200 and L is a list containing additional TYPE-specific arguments.
5202 So far the following TYPE/L pairs are supported:
5204 Event TYPE L
5206 nickname change 'nick (NEW-NICK)"
5207 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
5208 (cond
5209 ;; nickname change
5210 ((equal type 'nick)
5213 nil)))
5215 (defun erc-highlight-notice (s)
5216 "Highlight notice message S and return it.
5217 See also variable `erc-notice-highlight-type'."
5218 (cond
5219 ((eq erc-notice-highlight-type 'prefix)
5220 (erc-put-text-property 0 (length erc-notice-prefix)
5221 'face 'erc-notice-face s)
5223 ((eq erc-notice-highlight-type 'all)
5224 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
5226 (t s)))
5228 (defun erc-make-notice (message)
5229 "Notify the user of MESSAGE."
5230 (when erc-minibuffer-notice
5231 (message "%s" message))
5232 (erc-highlight-notice (concat erc-notice-prefix message)))
5234 (defun erc-highlight-error (s)
5235 "Highlight error message S and return it."
5236 (erc-put-text-property 0 (length s) 'face 'erc-error-face s)
5239 (defun erc-put-text-property (start end property value &optional object)
5240 "Set text-property for an object (usually a string).
5241 START and END define the characters covered.
5242 PROPERTY is the text-property set, usually the symbol `face'.
5243 VALUE is the value for the text-property, usually a face symbol such as
5244 the face `bold' or `erc-pal-face'.
5245 OBJECT is a string which will be modified and returned.
5246 OBJECT is modified without being copied first.
5248 You can redefine or `defadvice' this function in order to add
5249 EmacsSpeak support."
5250 (put-text-property start end property value object))
5252 (defun erc-list (thing)
5253 "Return THING if THING is a list, or a list with THING as its element."
5254 (if (listp thing)
5255 thing
5256 (list thing)))
5258 (defun erc-parse-user (string)
5259 "Parse STRING as a user specification (nick!login@host).
5261 Return a list of the three separate tokens."
5262 (cond
5263 ((string-match "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$" string)
5264 (list (match-string 1 string)
5265 (match-string 2 string)
5266 (match-string 3 string)))
5267 ;; Some bogus bouncers send Nick!(null), try to live with that.
5268 ((string-match "^\\([^!\n]*\\)!\\(.*\\)$" string)
5269 (list (match-string 1 string)
5271 (match-string 2 string)))
5273 (list string "" ""))))
5275 (defun erc-extract-nick (string)
5276 "Return the nick corresponding to a user specification STRING.
5278 See also `erc-parse-user'."
5279 (car (erc-parse-user string)))
5281 (defun erc-put-text-properties (start end properties
5282 &optional object value-list)
5283 "Set text-properties for OBJECT.
5285 START and END describe positions in OBJECT.
5286 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
5287 each property to the corresponding value in VALUE-LIST."
5288 (unless value-list
5289 (setq value-list (mapcar (lambda (_x) t)
5290 properties)))
5291 (while (and properties value-list)
5292 (erc-put-text-property
5293 start end (pop properties) (pop value-list) object)))
5295 ;;; Input area handling:
5297 (defun erc-beg-of-input-line ()
5298 "Return the value of `point' at the beginning of the input line.
5300 Specifically, return the position of `erc-insert-marker'."
5301 (or (and (boundp 'erc-insert-marker)
5302 (markerp erc-insert-marker))
5303 (error "erc-insert-marker has no value, please report a bug"))
5304 (marker-position erc-insert-marker))
5306 (defun erc-end-of-input-line ()
5307 "Return the value of `point' at the end of the input line."
5308 (point-max))
5310 (defvar erc-last-input-time 0
5311 "Time of last call to `erc-send-current-line'.
5312 If that function has never been called, the value is 0.")
5314 (defcustom erc-accidental-paste-threshold-seconds nil
5315 "Minimum time, in seconds, before sending new lines via IRC.
5316 If the value is a number, `erc-send-current-line' signals an error
5317 if its previous invocation was fewer than this many seconds ago.
5318 This is useful so that if you accidentally enter large amounts of text
5319 into the ERC buffer, that text is not sent to the IRC server.
5321 If the value is nil, `erc-send-current-line' always considers any
5322 submitted line to be intentional."
5323 :group 'erc
5324 :version "24.4"
5325 :type '(choice number (other :tag "disabled" nil)))
5327 (defun erc-send-current-line ()
5328 "Parse current line and send it to IRC."
5329 (interactive)
5330 (let ((now (float-time)))
5331 (if (or (not erc-accidental-paste-threshold-seconds)
5332 (< erc-accidental-paste-threshold-seconds
5333 (- now erc-last-input-time)))
5334 (save-restriction
5335 (widen)
5336 (if (< (point) (erc-beg-of-input-line))
5337 (erc-error "Point is not in the input area")
5338 (let ((inhibit-read-only t)
5339 (str (erc-user-input))
5340 (old-buf (current-buffer)))
5341 (if (and (not (erc-server-buffer-live-p))
5342 (not (erc-command-no-process-p str)))
5343 (erc-error "ERC: No process running")
5344 (erc-set-active-buffer (current-buffer))
5345 ;; Kill the input and the prompt
5346 (delete-region (erc-beg-of-input-line)
5347 (erc-end-of-input-line))
5348 (unwind-protect
5349 (erc-send-input str)
5350 ;; Fix the buffer if the command didn't kill it
5351 (when (buffer-live-p old-buf)
5352 (with-current-buffer old-buf
5353 (save-restriction
5354 (widen)
5355 (goto-char (point-max))
5356 (when (processp erc-server-process)
5357 (set-marker (process-mark erc-server-process) (point)))
5358 (set-marker erc-insert-marker (point))
5359 (let ((buffer-modified (buffer-modified-p)))
5360 (erc-display-prompt)
5361 (set-buffer-modified-p buffer-modified))))))
5363 ;; Only when last hook has been run...
5364 (run-hook-with-args 'erc-send-completed-hook str))))
5365 (setq erc-last-input-time now))
5366 (switch-to-buffer "*ERC Accidental Paste Overflow*")
5367 (lwarn 'erc :warning
5368 "You seem to have accidentally pasted some text!"))))
5370 (defun erc-user-input ()
5371 "Return the input of the user in the current buffer."
5372 (buffer-substring-no-properties
5373 erc-input-marker
5374 (erc-end-of-input-line)))
5376 (defvar erc-command-regexp "^/\\([A-Za-z']+\\)\\(\\s-+.*\\|\\s-*\\)$"
5377 "Regular expression used for matching commands in ERC.")
5379 (defun erc-send-input (input)
5380 "Treat INPUT as typed in by the user. It is assumed that the input
5381 and the prompt is already deleted.
5382 This returns non-nil only if we actually send anything."
5383 ;; Handle different kinds of inputs
5384 (cond
5385 ;; Ignore empty input
5386 ((if erc-send-whitespace-lines
5387 (string= input "")
5388 (string-match "\\`[ \t\r\f\n]*\\'" input))
5389 (when erc-warn-about-blank-lines
5390 (message "Blank line - ignoring...")
5391 (beep))
5392 nil)
5394 (defvar str) ;; FIXME: Make it obey the "erc-" prefix convention.
5395 (let ((str input)
5396 (erc-insert-this t))
5397 (setq erc-send-this t)
5398 (run-hook-with-args 'erc-send-pre-hook input)
5399 (when erc-send-this
5400 (if (or (string-match "\n" str)
5401 (not (string-match erc-command-regexp str)))
5402 (mapc
5403 (lambda (line)
5404 (mapc
5405 (lambda (line)
5406 ;; Insert what has to be inserted for this.
5407 (erc-display-msg line)
5408 (erc-process-input-line (concat line "\n")
5409 (null erc-flood-protect) t))
5410 (or (and erc-flood-protect (erc-split-line line))
5411 (list line))))
5412 (split-string str "\n"))
5413 (erc-process-input-line (concat str "\n") t nil))
5414 t)))))
5416 (defun erc-display-command (line)
5417 (when erc-insert-this
5418 (let ((insert-position (point)))
5419 (unless erc-hide-prompt
5420 (erc-display-prompt nil nil (erc-command-indicator)
5421 (and (erc-command-indicator)
5422 'erc-command-indicator-face)))
5423 (let ((beg (point)))
5424 (insert line)
5425 (erc-put-text-property beg (point)
5426 'face 'erc-command-indicator-face)
5427 (insert "\n"))
5428 (when (processp erc-server-process)
5429 (set-marker (process-mark erc-server-process) (point)))
5430 (set-marker erc-insert-marker (point))
5431 (save-excursion
5432 (save-restriction
5433 (narrow-to-region insert-position (point))
5434 (run-hooks 'erc-send-modify-hook)
5435 (run-hooks 'erc-send-post-hook))))))
5437 (defun erc-display-msg (line)
5438 "Display LINE as a message of the user to the current target at the
5439 current position."
5440 (when erc-insert-this
5441 (let ((insert-position (point)))
5442 (insert (erc-format-my-nick))
5443 (let ((beg (point)))
5444 (insert line)
5445 (erc-put-text-property beg (point)
5446 'face 'erc-input-face))
5447 (insert "\n")
5448 (when (processp erc-server-process)
5449 (set-marker (process-mark erc-server-process) (point)))
5450 (set-marker erc-insert-marker (point))
5451 (save-excursion
5452 (save-restriction
5453 (narrow-to-region insert-position (point))
5454 (run-hooks 'erc-send-modify-hook)
5455 (run-hooks 'erc-send-post-hook))))))
5457 (defun erc-command-symbol (command)
5458 "Return the ERC command symbol for COMMAND if it exists and is bound."
5459 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
5460 (when (fboundp cmd) cmd)))
5462 (defun erc-extract-command-from-line (line)
5463 "Extract command and args from the input LINE.
5464 If no command was given, return nil. If command matches, return a
5465 list of the form: (command args) where both elements are strings."
5466 (when (string-match erc-command-regexp line)
5467 (let* ((cmd (erc-command-symbol (match-string 1 line)))
5468 ;; note: return is nil, we apply this simply for side effects
5469 (_canon-defun (while (and cmd (symbolp (symbol-function cmd)))
5470 (setq cmd (symbol-function cmd))))
5471 (cmd-fun (or cmd #'erc-cmd-default))
5472 (arg (if cmd
5473 (if (get cmd-fun 'do-not-parse-args)
5474 (format "%s" (match-string 2 line))
5475 (delete "" (split-string (erc-trim-string
5476 (match-string 2 line)) " ")))
5477 line)))
5478 (list cmd-fun arg))))
5480 (defun erc-split-multiline-safe (string)
5481 "Split STRING, containing multiple lines and return them in a list.
5482 Do it only for STRING as the complete input, do not carry unfinished
5483 strings over to the next call."
5484 (let ((l ())
5485 (i0 0)
5486 (doit t))
5487 (while doit
5488 (let ((i (string-match "\r?\n" string i0))
5489 (s (substring string i0)))
5490 (cond (i (setq l (cons (substring string i0 i) l))
5491 (setq i0 (match-end 0)))
5492 ((> (length s) 0)
5493 (setq l (cons s l))(setq doit nil))
5494 (t (setq doit nil)))))
5495 (nreverse l)))
5497 ;; nick handling
5499 (defun erc-set-current-nick (nick)
5500 "Set the current nickname to NICK."
5501 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5502 (erc-server-buffer)
5503 (current-buffer))
5504 (setq erc-server-current-nick nick)))
5506 (defun erc-current-nick ()
5507 "Return the current nickname."
5508 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5509 (erc-server-buffer)
5510 (current-buffer))
5511 erc-server-current-nick))
5513 (defun erc-current-nick-p (nick)
5514 "Return non-nil if NICK is the current nickname."
5515 (erc-nick-equal-p nick (erc-current-nick)))
5517 (defun erc-nick-equal-p (nick1 nick2)
5518 "Return non-nil if NICK1 and NICK2 are the same.
5520 This matches strings according to the IRC protocol's case convention.
5522 See also `erc-downcase'."
5523 (string= (erc-downcase nick1)
5524 (erc-downcase nick2)))
5526 ;; default target handling
5528 (defun erc-default-target ()
5529 "Return the current default target (as a character string) or nil if none."
5530 (let ((tgt (car erc-default-recipients)))
5531 (cond
5532 ((not tgt) nil)
5533 ((listp tgt) (cdr tgt))
5534 (t tgt))))
5536 (defun erc-add-default-channel (channel)
5537 "Add CHANNEL to the default channel list."
5538 (let ((chl (downcase channel)))
5539 (setq erc-default-recipients
5540 (cons chl erc-default-recipients))))
5542 (defun erc-delete-default-channel (channel &optional buffer)
5543 "Delete CHANNEL from the default channel list."
5544 (with-current-buffer (if (and buffer
5545 (bufferp buffer))
5546 buffer
5547 (current-buffer))
5548 (setq erc-default-recipients (delete (downcase channel)
5549 erc-default-recipients))))
5551 (defun erc-add-query (nickname)
5552 "Add QUERY'd NICKNAME to the default channel list.
5554 The previous default target of QUERY type gets removed."
5555 (let ((d1 (car erc-default-recipients))
5556 (d2 (cdr erc-default-recipients))
5557 (qt (cons 'QUERY (downcase nickname))))
5558 (setq erc-default-recipients (cons qt (if (and (listp d1)
5559 (eq (car d1) 'QUERY))
5561 erc-default-recipients)))))
5563 (defun erc-delete-query ()
5564 "Delete the topmost target if it is a QUERY."
5566 (let ((d1 (car erc-default-recipients))
5567 (d2 (cdr erc-default-recipients)))
5568 (if (and (listp d1)
5569 (eq (car d1) 'QUERY))
5570 (setq erc-default-recipients d2)
5571 (error "Current target is not a QUERY"))))
5573 (defun erc-ignored-user-p (spec)
5574 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5576 Takes a full SPEC of a user in the form \"nick!login@host\", and
5577 matches against all the regexp's in `erc-ignore-list'. If any
5578 match, returns that regexp."
5579 (catch 'found
5580 (dolist (ignored (erc-with-server-buffer erc-ignore-list))
5581 (if (string-match ignored spec)
5582 (throw 'found ignored)))))
5584 (defun erc-ignored-reply-p (msg tgt proc)
5585 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5586 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5588 Takes a message MSG to a channel and returns non-nil if the addressed
5589 user matches any regexp in `erc-ignore-reply-list'."
5590 (let ((target-nick (erc-message-target msg)))
5591 (if (not target-nick)
5593 (erc-with-buffer (tgt proc)
5594 (let ((user (erc-get-server-user target-nick)))
5595 (when user
5596 (erc-list-match erc-ignore-reply-list
5597 (erc-user-spec user))))))))
5599 (defun erc-message-target (msg)
5600 "Return the addressed target in MSG.
5602 The addressed target is the string before the first colon in MSG."
5603 (if (string-match "^\\([^: \n]*\\):" msg)
5604 (match-string 1 msg)
5605 nil))
5607 (defun erc-user-spec (user)
5608 "Create a nick!user@host spec from a user struct."
5609 (let ((nick (erc-server-user-nickname user))
5610 (host (erc-server-user-host user))
5611 (login (erc-server-user-login user)))
5612 (concat (or nick "")
5614 (or login "")
5616 (or host ""))))
5618 (defun erc-list-match (lst str)
5619 "Return non-nil if any regexp in LST matches STR."
5620 (memq nil (mapcar (lambda (regexp)
5621 (not (string-match regexp str)))
5622 lst)))
5624 ;; other "toggles"
5626 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5627 "Toggle automatic CTCP replies (like VERSION and PING).
5629 If ARG is positive, turns CTCP replies on.
5631 If ARG is non-nil and not positive, turns CTCP replies off."
5632 (interactive "P")
5633 (cond ((and (numberp arg) (> arg 0))
5634 (setq erc-disable-ctcp-replies t))
5635 (arg (setq erc-disable-ctcp-replies nil))
5636 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5637 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5639 (defun erc-toggle-flood-control (&optional arg)
5640 "Toggle use of flood control on sent messages.
5642 If ARG is positive, use flood control.
5643 If ARG is non-nil and not positive, do not use flood control.
5645 See `erc-server-flood-margin' for an explanation of the available
5646 flood control parameters."
5647 (interactive "P")
5648 (cond ((and (numberp arg) (> arg 0))
5649 (setq erc-flood-protect t))
5650 (arg (setq erc-flood-protect nil))
5651 (t (setq erc-flood-protect (not erc-flood-protect))))
5652 (message "ERC flood control is %s"
5653 (cond (erc-flood-protect "ON")
5654 (t "OFF"))))
5656 ;; Some useful channel and nick commands for fast key bindings
5658 (defun erc-invite-only-mode (&optional arg)
5659 "Turn on the invite only mode (+i) for the current channel.
5661 If ARG is non-nil, turn this mode off (-i).
5663 This command is sent even if excess flood is detected."
5664 (interactive "P")
5665 (erc-set-active-buffer (current-buffer))
5666 (let ((tgt (erc-default-target)))
5667 (if (or (not tgt) (not (erc-channel-p tgt)))
5668 (erc-display-message nil 'error (current-buffer) 'no-target)
5669 (erc-load-irc-script-lines
5670 (list (concat "/mode " tgt (if arg " -i" " +i")))
5671 t))))
5673 (defun erc-get-channel-mode-from-keypress (key)
5674 "Read a key sequence and call the corresponding channel mode function.
5675 After doing C-c C-o, type in a channel mode letter.
5677 C-g means quit.
5678 RET lets you type more than one mode at a time.
5679 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5680 If \"k\" is pressed, `erc-set-channel-key' gets called.
5681 Anything else will be sent to `erc-toggle-channel-mode'."
5682 (interactive "kChannel mode (RET to set more than one): ")
5683 (when (featurep 'xemacs)
5684 (setq key (char-to-string (event-to-character (aref key 0)))))
5685 (cond ((equal key "\C-g")
5686 (keyboard-quit))
5687 ((equal key "\C-m")
5688 (erc-insert-mode-command))
5689 ((equal key "l")
5690 (call-interactively 'erc-set-channel-limit))
5691 ((equal key "k")
5692 (call-interactively 'erc-set-channel-key))
5693 (t (erc-toggle-channel-mode key))))
5695 (defun erc-toggle-channel-mode (mode &optional channel)
5696 "Toggle channel MODE.
5698 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5699 `erc-default-target'."
5700 (interactive "P")
5701 (erc-set-active-buffer (current-buffer))
5702 (let ((tgt (or channel (erc-default-target))))
5703 (if (or (null tgt) (null (erc-channel-p tgt)))
5704 (erc-display-message nil 'error 'active 'no-target)
5705 (let* ((active (member mode erc-channel-modes))
5706 (newstate (if active "OFF" "ON")))
5707 (erc-log (format "%s: Toggle mode %s %s" tgt mode newstate))
5708 (message "Toggle channel mode %s %s" mode newstate)
5709 (erc-server-send (format "MODE %s %s%s"
5710 tgt (if active "-" "+") mode))))))
5712 (defun erc-insert-mode-command ()
5713 "Insert the line \"/mode <current target> \" at `point'."
5714 (interactive)
5715 (let ((tgt (erc-default-target)))
5716 (if tgt (insert (concat "/mode " tgt " "))
5717 (erc-display-message nil 'error (current-buffer) 'no-target))))
5719 (defun erc-channel-names ()
5720 "Run \"/names #channel\" in the current channel."
5721 (interactive)
5722 (erc-set-active-buffer (current-buffer))
5723 (let ((tgt (erc-default-target)))
5724 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5725 (erc-display-message nil 'error (current-buffer) 'no-target))))
5727 (defun erc-remove-text-properties-region (start end &optional object)
5728 "Clears the region (START,END) in OBJECT from all colors, etc."
5729 (interactive "r")
5730 (save-excursion
5731 (let ((inhibit-read-only t))
5732 (set-text-properties start end nil object))))
5733 (put 'erc-remove-text-properties-region 'disabled t)
5735 ;; script execution and startup
5737 (defun erc-find-file (file &optional path)
5738 "Search for a FILE in the filesystem.
5739 First the `default-directory' is searched for FILE, then any directories
5740 specified in the list PATH.
5742 If FILE is found, return the path to it."
5743 (let ((filepath file))
5744 (if (file-readable-p filepath) filepath
5745 (while (and path
5746 (progn (setq filepath (expand-file-name file (car path)))
5747 (not (file-readable-p filepath))))
5748 (setq path (cdr path)))
5749 (if path filepath nil))))
5751 (defun erc-select-startup-file ()
5752 "Select an ERC startup file.
5753 See also `erc-startup-file-list'."
5754 (catch 'found
5755 (dolist (f erc-startup-file-list)
5756 (setq f (convert-standard-filename f))
5757 (when (file-readable-p f)
5758 (throw 'found f)))))
5760 (defun erc-find-script-file (file)
5761 "Search for FILE in `default-directory', and any in `erc-script-path'."
5762 (erc-find-file file erc-script-path))
5764 (defun erc-load-script (file)
5765 "Load a script from FILE.
5767 FILE must be the full name, it is not searched in the
5768 `erc-script-path'. If the filename ends with `.el', then load it
5769 as an Emacs Lisp program. Otherwise, treat it as a regular IRC
5770 script."
5771 (erc-log (concat "erc-load-script: " file))
5772 (cond
5773 ((string-match "\\.el\\'" file)
5774 (load file))
5776 (erc-load-irc-script file))))
5778 (defun erc-process-script-line (line &optional args)
5779 "Process an IRC script LINE.
5781 Does script-specific substitutions (script arguments, current nick,
5782 server, etc.) in LINE and returns it.
5784 Substitutions are: %C and %c = current target (channel or nick),
5785 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5786 where x is any other character;
5787 $* = the entire argument string, $1 = the first argument, $2 = the second,
5788 and so on."
5789 (if (not args) (setq args ""))
5790 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5791 (percent-regexp "\\(%.\\)")
5792 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5793 (tgt (erc-default-target))
5794 (server (and (boundp 'erc-session-server) erc-session-server))
5795 (nick (erc-current-nick))
5796 (res "")
5797 (tmp nil)
5798 (arg-list nil)
5799 (arg-num 0))
5800 (if (not tgt) (setq tgt ""))
5801 (if (not server) (setq server ""))
5802 (if (not nick) (setq nick ""))
5803 ;; First, compute the argument list
5804 (setq tmp args)
5805 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5806 (setq arg-list (cons (match-string 1 tmp) arg-list))
5807 (setq tmp (match-string 2 tmp)))
5808 (setq arg-list (nreverse arg-list))
5809 (setq arg-num (length arg-list))
5810 ;; now do the substitution
5811 (setq tmp (string-match esc-regexp line))
5812 (while tmp
5813 ;;(message "beginning of while: tmp=%S" tmp)
5814 (let* ((hd (substring line 0 tmp))
5815 (esc "")
5816 (subst "")
5817 (tail (substring line tmp)))
5818 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5819 (setq esc (match-string 1 tail))
5820 (setq tail (substring tail (match-end 1))))
5821 ((string-match (concat "^" percent-regexp) tail)
5822 (setq esc (match-string 1 tail))
5823 (setq tail (substring tail (match-end 1)))))
5824 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5825 (setq res (concat res hd))
5826 (setq subst
5827 (cond ((string= esc "") "")
5828 ((string-match "^\\$\\*$" esc) args)
5829 ((string-match "^\\$\\([0-9]+\\)$" esc)
5830 (let ((n (string-to-number (match-string 1 esc))))
5831 (message "n = %S, integerp(n)=%S" n (integerp n))
5832 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5833 ((string-match "^%[Cc]$" esc) tgt)
5834 ((string-match "^%[Ss]$" esc) server)
5835 ((string-match "^%[Nn]$" esc) nick)
5836 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5837 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5838 (message "BUG IN ERC: esc=%S" esc)
5839 "")))
5840 (setq line tail)
5841 (setq tmp (string-match esc-regexp line))
5842 (setq res (concat res subst))
5843 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5845 (setq res (concat res line))
5846 res))
5848 (defun erc-load-irc-script (file &optional force)
5849 "Load an IRC script from FILE."
5850 (erc-log (concat "erc-load-script: " file))
5851 (let ((str (with-temp-buffer
5852 (insert-file-contents file)
5853 (buffer-string))))
5854 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5856 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5857 "Load IRC script LINES (a list of strings).
5859 If optional NOEXPAND is non-nil, do not expand script-specific
5860 sequences, process the lines verbatim. Use this for multiline
5861 user input."
5862 (let* ((cb (current-buffer))
5863 (s "")
5864 (sp (or (erc-command-indicator) (erc-prompt)))
5865 (args (and (boundp 'erc-script-args) erc-script-args)))
5866 (if (and args (string-match "^ " args))
5867 (setq args (substring args 1)))
5868 ;; prepare the prompt string for echo
5869 (erc-put-text-property 0 (length sp)
5870 'face 'erc-command-indicator-face sp)
5871 (while lines
5872 (setq s (car lines))
5873 (erc-log (concat "erc-load-script: CMD: " s))
5874 (unless (string-match "^\\s-*$" s)
5875 (let ((line (if noexpand s (erc-process-script-line s args))))
5876 (if (and (erc-process-input-line line force)
5877 erc-script-echo)
5878 (progn
5879 (erc-put-text-property 0 (length line)
5880 'face 'erc-input-face line)
5881 (erc-display-line (concat sp line) cb)))))
5882 (setq lines (cdr lines)))))
5884 ;; authentication
5886 (defun erc-login ()
5887 "Perform user authentication at the IRC server."
5888 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5889 (erc-current-nick)
5890 (user-login-name)
5891 (or erc-system-name (system-name))
5892 erc-session-server
5893 erc-session-user-full-name))
5894 (if erc-session-password
5895 (erc-server-send (format "PASS %s" erc-session-password))
5896 (message "Logging in without password"))
5897 (erc-server-send (format "NICK %s" (erc-current-nick)))
5898 (erc-server-send
5899 (format "USER %s %s %s :%s"
5900 ;; hacked - S.B.
5901 (if erc-anonymous-login erc-email-userid (user-login-name))
5902 "0" "*"
5903 erc-session-user-full-name))
5904 (erc-update-mode-line))
5906 ;; connection properties' heuristics
5908 (defun erc-determine-parameters (&optional server port nick name)
5909 "Determine the connection and authentication parameters.
5910 Sets the buffer local variables:
5912 - `erc-session-connector'
5913 - `erc-session-server'
5914 - `erc-session-port'
5915 - `erc-session-full-name'
5916 - `erc-server-current-nick'"
5917 (setq erc-session-connector erc-server-connect-function
5918 erc-session-server (erc-compute-server server)
5919 erc-session-port (or port erc-default-port)
5920 erc-session-user-full-name (erc-compute-full-name name))
5921 (erc-set-current-nick (erc-compute-nick nick)))
5923 (defun erc-compute-server (&optional server)
5924 "Return an IRC server name.
5926 This tries a number of increasingly more default methods until a
5927 non-nil value is found.
5929 - SERVER (the argument passed to this function)
5930 - The `erc-server' option
5931 - The value of the IRCSERVER environment variable
5932 - The `erc-default-server' variable"
5933 (or server
5934 erc-server
5935 (getenv "IRCSERVER")
5936 erc-default-server))
5938 (defun erc-compute-nick (&optional nick)
5939 "Return user's IRC nick.
5941 This tries a number of increasingly more default methods until a
5942 non-nil value is found.
5944 - NICK (the argument passed to this function)
5945 - The `erc-nick' option
5946 - The value of the IRCNICK environment variable
5947 - The result from the `user-login-name' function"
5948 (or nick
5949 (if (consp erc-nick) (car erc-nick) erc-nick)
5950 (getenv "IRCNICK")
5951 (user-login-name)))
5954 (defun erc-compute-full-name (&optional full-name)
5955 "Return user's full name.
5957 This tries a number of increasingly more default methods until a
5958 non-nil value is found.
5960 - FULL-NAME (the argument passed to this function)
5961 - The `erc-user-full-name' option
5962 - The value of the IRCNAME environment variable
5963 - The result from the `user-full-name' function"
5964 (or full-name
5965 erc-user-full-name
5966 (getenv "IRCNAME")
5967 (if erc-anonymous-login "unknown" nil)
5968 (user-full-name)))
5970 (defun erc-compute-port (&optional port)
5971 "Return a port for an IRC server.
5973 This tries a number of increasingly more default methods until a
5974 non-nil value is found.
5976 - PORT (the argument passed to this function)
5977 - The `erc-port' option
5978 - The `erc-default-port' variable"
5979 (or port erc-port erc-default-port))
5981 ;; time routines
5983 (defun erc-string-to-emacs-time (string)
5984 "Convert the long number represented by STRING into an Emacs format.
5985 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
5986 (let* ((n (string-to-number (concat string ".0"))))
5987 (list (truncate (/ n 65536))
5988 (truncate (mod n 65536)))))
5990 (defalias 'erc-emacs-time-to-erc-time
5991 (if (featurep 'xemacs) 'time-to-seconds 'float-time))
5993 (defalias 'erc-current-time 'erc-emacs-time-to-erc-time)
5995 (defun erc-time-diff (t1 t2)
5996 "Return the time difference in seconds between T1 and T2."
5997 (abs (- t2 t1)))
5999 (defun erc-time-gt (t1 t2)
6000 "Check whether T1 > T2."
6001 (> t1 t2))
6003 (defun erc-sec-to-time (ns)
6004 "Convert NS to a time string HH:MM.SS."
6005 (setq ns (truncate ns))
6006 (format "%02d:%02d.%02d"
6007 (/ ns 3600)
6008 (/ (% ns 3600) 60)
6009 (% ns 60)))
6011 (defun erc-seconds-to-string (seconds)
6012 "Convert a number of SECONDS into an English phrase."
6013 (let (days hours minutes format-args output)
6014 (setq days (/ seconds 86400)
6015 seconds (% seconds 86400)
6016 hours (/ seconds 3600)
6017 seconds (% seconds 3600)
6018 minutes (/ seconds 60)
6019 seconds (% seconds 60)
6020 format-args (if (> days 0)
6021 `("%d days, %d hours, %d minutes, %d seconds"
6022 ,days ,hours ,minutes ,seconds)
6023 (if (> hours 0)
6024 `("%d hours, %d minutes, %d seconds"
6025 ,hours ,minutes ,seconds)
6026 (if (> minutes 0)
6027 `("%d minutes, %d seconds" ,minutes ,seconds)
6028 `("%d seconds" ,seconds))))
6029 output (apply #'format format-args))
6030 ;; Change all "1 units" to "1 unit".
6031 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
6032 (setq output (erc-replace-match-subexpression-in-string
6033 "" output (match-string 2 output) 2 (match-beginning 2))))
6034 output))
6037 ;; info
6039 (defconst erc-clientinfo-alist
6040 '(("ACTION" . "is used to inform about one's current activity")
6041 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
6042 ("ECHO" . "echoes its arguments back")
6043 ("FINGER" . "shows user's name, location, and idle time")
6044 ("PING" . "measures delay between peers")
6045 ("TIME" . "shows client-side time")
6046 ("USERINFO" . "shows information provided by a user")
6047 ("VERSION" . "shows client type and version"))
6048 "Alist of CTCP CLIENTINFO for ERC commands.")
6050 (defun erc-client-info (s)
6051 "Return CTCP CLIENTINFO on command S.
6052 If S is nil or an empty string then return general CLIENTINFO."
6053 (if (or (not s) (string= s ""))
6054 (concat
6055 (apply #'concat
6056 (mapcar (lambda (e)
6057 (concat (car e) " "))
6058 erc-clientinfo-alist))
6059 ": use CLIENTINFO <COMMAND> to get more specific information")
6060 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
6061 (if h
6062 (concat s " " (cdr h))
6063 (concat s ": unknown command")))))
6065 ;; Hook functions
6067 (defun erc-directory-writable-p (dir)
6068 "Determine whether DIR is a writable directory.
6069 If it doesn't exist, create it."
6070 (unless (file-attributes dir) (make-directory dir))
6071 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
6073 (defun erc-kill-query-buffers (process)
6074 "Kill all buffers of PROCESS."
6075 ;; here, we only want to match the channel buffers, to avoid
6076 ;; "selecting killed buffers" b0rkage.
6077 (erc-with-all-buffers-of-server process
6078 (lambda ()
6079 (not (erc-server-buffer-p)))
6080 (kill-buffer (current-buffer))))
6082 (defun erc-nick-at-point ()
6083 "Give information about the nickname at `point'.
6085 If called interactively, give a human readable message in the
6086 minibuffer. If called programmatically, return the corresponding
6087 entry of `channel-members'."
6088 (interactive)
6089 (require 'thingatpt)
6090 (let* ((word (word-at-point))
6091 (channel-data (erc-get-channel-user word))
6092 (cuser (cdr channel-data))
6093 (user (if channel-data
6094 (car channel-data)
6095 (erc-get-server-user word)))
6096 host login full-name nick voice halfop op admin owner)
6097 (when user
6098 (setq nick (erc-server-user-nickname user)
6099 host (erc-server-user-host user)
6100 login (erc-server-user-login user)
6101 full-name (erc-server-user-full-name user))
6102 (if cuser
6103 (setq voice (erc-channel-user-voice cuser)
6104 halfop (erc-channel-user-halfop cuser)
6105 op (erc-channel-user-op cuser)
6106 admin (erc-channel-user-admin cuser)
6107 owner (erc-channel-user-owner cuser))))
6108 (if (called-interactively-p 'interactive)
6109 (message "%s is %s@%s%s%s"
6110 nick login host
6111 (if full-name (format " (%s)" full-name) "")
6112 (if (or voice halfop op admin owner)
6113 (format " and is +%s%s%s%s%s on %s"
6114 (if voice "v" "")
6115 (if halfop "h" "")
6116 (if op "o" "")
6117 (if admin "a" "")
6118 (if owner "q" "")
6119 (erc-default-target))
6120 ""))
6121 user)))
6123 (defun erc-away-time ()
6124 "Return non-nil if the current ERC process is set away.
6126 In particular, the time that we were set away is returned.
6127 See `current-time' for details on the time format."
6128 (erc-with-server-buffer erc-away))
6130 ;; Mode line handling
6132 (defcustom erc-mode-line-format "%S %a"
6133 "A string to be formatted and shown in the mode-line in `erc-mode'.
6135 The string is formatted using `format-spec' and the result is set as the value
6136 of `mode-line-buffer-identification'.
6138 The following characters are replaced:
6139 %a: String indicating away status or \"\" if you are not away
6140 %l: The estimated lag time to the server
6141 %m: The modes of the channel
6142 %n: The current nick name
6143 %N: The name of the network
6144 %o: The topic of the channel
6145 %p: The session port
6146 %t: The name of the target (channel, nickname, or servername:port)
6147 %s: In the server-buffer, this gets filled with the value of
6148 `erc-server-announced-name', in a channel, the value of
6149 (erc-default-target) also get concatenated.
6150 %S: In the server-buffer, this gets filled with the value of
6151 `erc-network', in a channel, the value of (erc-default-target)
6152 also get concatenated."
6153 :group 'erc-mode-line-and-header
6154 :type 'string)
6156 (defcustom erc-header-line-format "%n on %t (%m,%l) %o"
6157 "A string to be formatted and shown in the header-line in `erc-mode'.
6158 Only used starting in Emacs 21.
6160 Set this to nil if you do not want the header line to be
6161 displayed.
6163 See `erc-mode-line-format' for which characters are can be used."
6164 :group 'erc-mode-line-and-header
6165 :set (lambda (sym val)
6166 (set sym val)
6167 (when (fboundp 'erc-update-mode-line)
6168 (erc-update-mode-line nil)))
6169 :type '(choice (const :tag "Disabled" nil)
6170 string))
6172 (defcustom erc-header-line-uses-tabbar-p nil
6173 "Use tabbar mode instead of the header line to display the header."
6174 :group 'erc-mode-line-and-header
6175 :type 'boolean)
6177 (defcustom erc-header-line-uses-help-echo-p t
6178 "Show the contents of the header line in the echo area or as a tooltip
6179 when you move point into the header line."
6180 :group 'erc-mode-line-and-header
6181 :type 'boolean)
6183 (defcustom erc-header-line-face-method nil
6184 "Determine what method to use when colorizing the header line text.
6186 If nil, don't colorize the header text.
6187 If given a function, call it and use the resulting face name.
6188 Otherwise, use the `erc-header-line' face."
6189 :group 'erc-mode-line-and-header
6190 :type '(choice (const :tag "Don't colorize" nil)
6191 (const :tag "Use the erc-header-line face" t)
6192 (function :tag "Call a function")))
6194 (defcustom erc-show-channel-key-p t
6195 "Show the channel key in the header line."
6196 :group 'erc-paranoia
6197 :type 'boolean)
6199 (defcustom erc-mode-line-away-status-format
6200 "(AWAY since %a %b %d %H:%M) "
6201 "When you're away on a server, this is shown in the mode line.
6202 This should be a string with substitution variables recognized by
6203 `format-time-string'."
6204 :group 'erc-mode-line-and-header
6205 :type 'string)
6207 (defun erc-shorten-server-name (server-name)
6208 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
6209 (if (stringp server-name)
6210 (with-temp-buffer
6211 (insert server-name)
6212 (let ((alist erc-common-server-suffixes))
6213 (while alist
6214 (goto-char (point-min))
6215 (if (re-search-forward (caar alist) nil t)
6216 (replace-match (cdar alist)))
6217 (setq alist (cdr alist))))
6218 (buffer-string))))
6220 (defun erc-format-target ()
6221 "Return the name of the target (channel or nickname or servername:port)."
6222 (let ((target (erc-default-target)))
6223 (or target
6224 (concat (erc-shorten-server-name
6225 (or erc-server-announced-name
6226 erc-session-server))
6227 ":" (erc-port-to-string erc-session-port)))))
6229 (defun erc-format-target-and/or-server ()
6230 "Return the server name or the current target and server name combined."
6231 (let ((server-name (erc-shorten-server-name
6232 (or erc-server-announced-name
6233 erc-session-server))))
6234 (cond ((erc-default-target)
6235 (concat (erc-string-no-properties (erc-default-target))
6236 "@" server-name))
6237 (server-name server-name)
6238 (t (buffer-name (current-buffer))))))
6240 (defun erc-format-network ()
6241 "Return the name of the network we are currently on."
6242 (let ((network (and (fboundp 'erc-network-name) (erc-network-name))))
6243 (if (and network (symbolp network))
6244 (symbol-name network)
6245 "")))
6247 (defun erc-format-target-and/or-network ()
6248 "Return the network or the current target and network combined.
6249 If the name of the network is not available, then use the
6250 shortened server name instead."
6251 (let ((network-name (or (and (fboundp 'erc-network-name) (erc-network-name))
6252 (erc-shorten-server-name
6253 (or erc-server-announced-name
6254 erc-session-server)))))
6255 (when (and network-name (symbolp network-name))
6256 (setq network-name (symbol-name network-name)))
6257 (cond ((erc-default-target)
6258 (concat (erc-string-no-properties (erc-default-target))
6259 "@" network-name))
6260 ((and network-name
6261 (not (get-buffer network-name)))
6262 (when erc-rename-buffers
6263 (rename-buffer network-name))
6264 network-name)
6265 (t (buffer-name (current-buffer))))))
6267 (defun erc-format-away-status ()
6268 "Return a formatted `erc-mode-line-away-status-format'
6269 if `erc-away' is non-nil."
6270 (let ((a (erc-away-time)))
6271 (if a
6272 (format-time-string erc-mode-line-away-status-format a)
6273 "")))
6275 (defun erc-format-channel-modes ()
6276 "Return the current channel's modes."
6277 (concat (apply #'concat
6278 "+" erc-channel-modes)
6279 (cond ((and erc-channel-user-limit erc-channel-key)
6280 (if erc-show-channel-key-p
6281 (format "lk %.0f %s" erc-channel-user-limit
6282 erc-channel-key)
6283 (format "kl %.0f" erc-channel-user-limit)))
6284 (erc-channel-user-limit
6285 ;; Emacs has no bignums
6286 (format "l %.0f" erc-channel-user-limit))
6287 (erc-channel-key
6288 (if erc-show-channel-key-p
6289 (format "k %s" erc-channel-key)
6290 "k"))
6291 (t nil))))
6293 (defun erc-format-lag-time ()
6294 "Return the estimated lag time to server, `erc-server-lag'."
6295 (let ((lag (erc-with-server-buffer erc-server-lag)))
6296 (cond (lag (format "lag:%.0f" lag))
6297 (t ""))))
6299 ;; erc-goodies is required at end of this file.
6300 (declare-function erc-controls-strip "erc-goodies" (str))
6302 (defvar tabbar--local-hlf)
6304 (defun erc-update-mode-line-buffer (buffer)
6305 "Update the mode line in a single ERC buffer BUFFER."
6306 (with-current-buffer buffer
6307 (let ((spec (format-spec-make
6308 ?a (erc-format-away-status)
6309 ?l (erc-format-lag-time)
6310 ?m (erc-format-channel-modes)
6311 ?n (or (erc-current-nick) "")
6312 ?N (erc-format-network)
6313 ?o (or (erc-controls-strip erc-channel-topic) "")
6314 ?p (erc-port-to-string erc-session-port)
6315 ?s (erc-format-target-and/or-server)
6316 ?S (erc-format-target-and/or-network)
6317 ?t (erc-format-target)))
6318 (process-status (cond ((and (erc-server-process-alive)
6319 (not erc-server-connected))
6320 ":connecting")
6321 ((erc-server-process-alive)
6324 ": CLOSED")))
6325 (face (cond ((eq erc-header-line-face-method nil)
6326 nil)
6327 ((functionp erc-header-line-face-method)
6328 (funcall erc-header-line-face-method))
6330 'erc-header-line))))
6331 (cond ((featurep 'xemacs)
6332 (setq modeline-buffer-identification
6333 (list (format-spec erc-mode-line-format spec)))
6334 (setq modeline-process (list process-status)))
6336 (setq mode-line-buffer-identification
6337 (list (format-spec erc-mode-line-format spec)))
6338 (setq mode-line-process (list process-status))))
6339 (when (boundp 'header-line-format)
6340 (let ((header (if erc-header-line-format
6341 (format-spec erc-header-line-format spec)
6342 nil)))
6343 (cond (erc-header-line-uses-tabbar-p
6344 (set (make-local-variable 'tabbar--local-hlf)
6345 header-line-format)
6346 (kill-local-variable 'header-line-format))
6347 ((null header)
6348 (setq header-line-format nil))
6349 (erc-header-line-uses-help-echo-p
6350 (let ((help-echo (with-temp-buffer
6351 (insert header)
6352 (fill-region (point-min) (point-max))
6353 (buffer-string))))
6354 (setq header-line-format
6355 (erc-replace-regexp-in-string
6357 "%%"
6358 (if face
6359 (erc-propertize header 'help-echo help-echo
6360 'face face)
6361 (erc-propertize header 'help-echo help-echo))))))
6362 (t (setq header-line-format
6363 (if face
6364 (erc-propertize header 'face face)
6365 header)))))))
6366 (if (featurep 'xemacs)
6367 (redraw-modeline)
6368 (force-mode-line-update))))
6370 (defun erc-update-mode-line (&optional buffer)
6371 "Update the mode line in BUFFER.
6373 If BUFFER is nil, update the mode line in all ERC buffers."
6374 (if (and buffer (bufferp buffer))
6375 (erc-update-mode-line-buffer buffer)
6376 (dolist (buf (erc-buffer-list))
6377 (when (buffer-live-p buf)
6378 (erc-update-mode-line-buffer buf)))))
6380 ;; Miscellaneous
6382 (defun erc-port-to-string (p)
6383 "Convert port P to a string.
6384 P may be an integer or a service name."
6385 (if (integerp p)
6386 (int-to-string p)
6389 (defun erc-string-to-port (s)
6390 "Convert string S to either an integer port number or a service name."
6391 (if (numberp s)
6393 (let ((n (string-to-number s)))
6394 (if (= n 0)
6396 n))))
6398 (defun erc-version (&optional here)
6399 "Show the version number of ERC in the minibuffer.
6400 If optional argument HERE is non-nil, insert version number at point."
6401 (interactive "P")
6402 (let ((version-string
6403 (format "ERC (IRC client for Emacs %s)" emacs-version)))
6404 (if here
6405 (insert version-string)
6406 (if (called-interactively-p 'interactive)
6407 (message "%s" version-string)
6408 version-string))))
6410 (defun erc-modes (&optional here)
6411 "Show the active ERC modes in the minibuffer.
6412 If optional argument HERE is non-nil, insert version number at point."
6413 (interactive "P")
6414 (let ((string
6415 (mapconcat 'identity
6416 (let (modes (case-fold-search nil))
6417 (dolist (var (apropos-internal "^erc-.*mode$"))
6418 (when (and (boundp var)
6419 (symbol-value var))
6420 (setq modes (cons (symbol-name var)
6421 modes))))
6422 modes)
6423 ", ")))
6424 (if here
6425 (insert string)
6426 (if (called-interactively-p 'interactive)
6427 (message "%s" string)
6428 string))))
6430 (defun erc-trim-string (s)
6431 "Trim leading and trailing spaces off S."
6432 (cond
6433 ((not (stringp s)) nil)
6434 ((string-match "^\\s-*$" s)
6436 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
6437 (match-string 1 s))
6439 s)))
6441 (defun erc-arrange-session-in-multiple-windows ()
6442 "Open a window for every non-server buffer related to `erc-session-server'.
6444 All windows are opened in the current frame."
6445 (interactive)
6446 (unless erc-server-process
6447 (error "No erc-server-process found in current buffer"))
6448 (let ((bufs (erc-buffer-list nil erc-server-process)))
6449 (when bufs
6450 (delete-other-windows)
6451 (switch-to-buffer (car bufs))
6452 (setq bufs (cdr bufs))
6453 (while bufs
6454 (split-window)
6455 (other-window 1)
6456 (switch-to-buffer (car bufs))
6457 (setq bufs (cdr bufs))
6458 (balance-windows)))))
6460 (defun erc-popup-input-buffer ()
6461 "Provide an input buffer."
6462 (interactive)
6463 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
6464 (mode (intern
6465 (completing-read
6466 "Mode: "
6467 (mapcar (lambda (e)
6468 (list (symbol-name e)))
6469 (apropos-internal "-mode\\'" 'commandp))
6470 nil t))))
6471 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
6472 (funcall mode)
6473 (narrow-to-region (point) (point))
6474 (shrink-window-if-larger-than-buffer)))
6476 ;;; Message catalog
6478 (defun erc-make-message-variable-name (catalog entry)
6479 "Create a variable name corresponding to CATALOG's ENTRY."
6480 (intern (concat "erc-message-"
6481 (symbol-name catalog) "-" (symbol-name entry))))
6483 (defun erc-define-catalog-entry (catalog entry format-spec)
6484 "Set CATALOG's ENTRY to FORMAT-SPEC."
6485 (set (erc-make-message-variable-name catalog entry)
6486 format-spec))
6488 (defun erc-define-catalog (catalog entries)
6489 "Define a CATALOG according to ENTRIES."
6490 (dolist (entry entries)
6491 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
6493 (erc-define-catalog
6494 'english
6495 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
6496 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
6497 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
6498 (cannot-find-file . "Cannot find file %f")
6499 (cannot-read-file . "Cannot read file %f")
6500 (connect . "Connecting to %S:%p... ")
6501 (country . "%c")
6502 (country-unknown . "%d: No such domain")
6503 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
6504 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
6505 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
6506 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
6507 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
6508 (flood-strict-mode
6509 . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
6510 (disconnected . "\n\nConnection failed! Re-establishing connection...\n")
6511 (disconnected-noreconnect
6512 . "\n\nConnection failed! Not re-establishing connection.\n")
6513 (finished . "\n\n*** ERC finished ***\n")
6514 (terminated . "\n\n*** ERC terminated: %e\n")
6515 (login . "Logging in as \'%n\'...")
6516 (nick-in-use . "%n is in use. Choose new nickname: ")
6517 (nick-too-long
6518 . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
6519 (no-default-channel . "No default channel")
6520 (no-invitation . "You've got no invitation")
6521 (no-target . "No target")
6522 (ops . "%i operator%s: %o")
6523 (ops-none . "No operators in this channel.")
6524 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
6525 (variable-not-bound . "Variable not bound!")
6526 (ACTION . "* %n %a")
6527 (CTCP-CLIENTINFO . "Client info for %n: %m")
6528 (CTCP-ECHO . "Echo %n: %m")
6529 (CTCP-FINGER . "Finger info for %n: %m")
6530 (CTCP-PING . "Ping time to %n is %t")
6531 (CTCP-TIME . "Time by %n is %m")
6532 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
6533 (CTCP-VERSION . "Version for %n is %m")
6534 (ERROR . "==> ERROR from %s: %c\n")
6535 (INVITE . "%n (%u@%h) invites you to channel %c")
6536 (JOIN . "%n (%u@%h) has joined channel %c")
6537 (JOIN-you . "You have joined channel %c")
6538 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
6539 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
6540 (KICK-by-you . "You have kicked %k off channel %c: %r")
6541 (MODE . "%n (%u@%h) has changed mode for %t to %m")
6542 (MODE-nick . "%n has changed mode for %t to %m")
6543 (NICK . "%n (%u@%h) is now known as %N")
6544 (NICK-you . "Your new nickname is %N")
6545 (PART . erc-message-english-PART)
6546 (PING . "PING from server (last: %s sec. ago)")
6547 (PONG . "PONG from %h (%i second%s)")
6548 (QUIT . "%n (%u@%h) has quit: %r")
6549 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
6550 (WALLOPS . "Wallops from %n: %m")
6551 (s004 . "%s %v %U %C")
6552 (s221 . "User modes for %n: %m")
6553 (s252 . "%i operator(s) online")
6554 (s253 . "%i unknown connection(s)")
6555 (s254 . "%i channels formed")
6556 (s275 . "%n %m")
6557 (s301 . "%n is AWAY: %r")
6558 (s303 . "Is online: %n")
6559 (s305 . "%m")
6560 (s306 . "%m")
6561 (s307 . "%n %m")
6562 (s311 . "%n is %f (%u@%h)")
6563 (s312 . "%n is/was on server %s (%c)")
6564 (s313 . "%n is an IRC operator")
6565 (s314 . "%n was %f (%u@%h)")
6566 (s317 . "%n has been idle for %i")
6567 (s317-on-since . "%n has been idle for %i, on since %t")
6568 (s319 . "%n is on channel(s): %c")
6569 (s320 . "%n is an identified user")
6570 (s321 . "Channel Users Topic")
6571 (s322 . "%c [%u] %t")
6572 (s324 . "%c modes: %m")
6573 (s328 . "%c URL: %u")
6574 (s329 . "%c was created on %t")
6575 (s330 . "%n %a %i")
6576 (s331 . "No topic is set for %c")
6577 (s332 . "Topic for %c: %T")
6578 (s333 . "%c: topic set by %n, %t")
6579 (s341 . "Inviting %n to channel %c")
6580 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
6581 (s353 . "Users on %c: %u")
6582 (s367 . "Ban for %b on %c")
6583 (s367-set-by . "Ban for %b on %c set by %s on %t")
6584 (s368 . "Banlist of %c ends.")
6585 (s379 . "%c: Forwarded to %f")
6586 (s391 . "The time at %s is %t")
6587 (s401 . "%n: No such nick/channel")
6588 (s403 . "%c: No such channel")
6589 (s404 . "%c: Cannot send to channel")
6590 (s405 . "%c: You have joined too many channels")
6591 (s406 . "%n: There was no such nickname")
6592 (s412 . "No text to send")
6593 (s421 . "%c: Unknown command")
6594 (s431 . "No nickname given")
6595 (s432 . "%n is an erroneous nickname")
6596 (s442 . "%c: You're not on that channel")
6597 (s445 . "SUMMON has been disabled")
6598 (s446 . "USERS has been disabled")
6599 (s451 . "You have not registered")
6600 (s461 . "%c: not enough parameters")
6601 (s462 . "Unauthorized command (already registered)")
6602 (s463 . "Your host isn't among the privileged")
6603 (s464 . "Password incorrect")
6604 (s465 . "You are banned from this server")
6605 (s474 . "You can't join %c because you're banned (+b)")
6606 (s475 . "You must specify the correct channel key (+k) to join %c")
6607 (s481 . "Permission Denied - You're not an IRC operator")
6608 (s482 . "You need to be a channel operator of %c to do that")
6609 (s483 . "You can't kill a server!")
6610 (s484 . "Your connection is restricted!")
6611 (s485 . "You're not the original channel operator")
6612 (s491 . "No O-lines for your host")
6613 (s501 . "Unknown MODE flag")
6614 (s502 . "You can't change modes for other users")
6615 (s671 . "%n %a")))
6617 (defun erc-message-english-PART (&rest args)
6618 "Format a proper PART message.
6620 This function is an example on what could be done with formatting
6621 functions."
6622 (let ((nick (cadr (memq ?n args)))
6623 (user (cadr (memq ?u args)))
6624 (host (cadr (memq ?h args)))
6625 (channel (cadr (memq ?c args)))
6626 (reason (cadr (memq ?r args))))
6627 (if (string= nick (erc-current-nick))
6628 (format "You have left channel %s" channel)
6629 (format "%s (%s@%s) has left channel %s%s"
6630 nick user host channel
6631 (if (not (string= reason ""))
6632 (format ": %s"
6633 (erc-replace-regexp-in-string "%" "%%" reason))
6634 "")))))
6637 (defvar erc-current-message-catalog 'english)
6638 (make-variable-buffer-local 'erc-current-message-catalog)
6640 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6641 "Retrieve ENTRY from CATALOG.
6643 If CATALOG is nil, `erc-current-message-catalog' is used.
6645 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6646 english, catalog."
6647 (unless catalog (setq catalog erc-current-message-catalog))
6648 (let ((var (erc-make-message-variable-name catalog entry)))
6649 (if (boundp var)
6650 (symbol-value var)
6651 (when (boundp (erc-make-message-variable-name 'english entry))
6652 (symbol-value (erc-make-message-variable-name 'english entry))))))
6654 (defun erc-format-message (msg &rest args)
6655 "Format MSG according to ARGS.
6657 See also `format-spec'."
6658 (when (eq (logand (length args) 1) 1) ; oddp
6659 (error "Obscure usage of this function appeared"))
6660 (let ((entry (erc-retrieve-catalog-entry msg)))
6661 (when (not entry)
6662 (error "No format spec for message %s" msg))
6663 (when (functionp entry)
6664 (setq entry (apply entry args)))
6665 (format-spec entry (apply #'format-spec-make args))))
6667 ;;; Various hook functions
6669 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6671 (defcustom erc-kill-server-hook '(erc-kill-server)
6672 "Invoked whenever a server buffer is killed via `kill-buffer'."
6673 :group 'erc-hooks
6674 :type 'hook)
6676 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6677 "Invoked whenever a channel-buffer is killed via `kill-buffer'."
6678 :group 'erc-hooks
6679 :type 'hook)
6681 (defcustom erc-kill-buffer-hook nil
6682 "Hook run whenever a non-server or channel buffer is killed.
6684 See also `kill-buffer'."
6685 :group 'erc-hooks
6686 :type 'hook)
6688 (defun erc-kill-buffer-function ()
6689 "Function to call when an ERC buffer is killed.
6690 This function should be on `kill-buffer-hook'.
6691 When the current buffer is in `erc-mode', this function will run
6692 one of the following hooks:
6693 `erc-kill-server-hook' if the server buffer was killed,
6694 `erc-kill-channel-hook' if a channel buffer was killed,
6695 or `erc-kill-buffer-hook' if any other buffer."
6696 (when (eq major-mode 'erc-mode)
6697 (erc-remove-channel-users)
6698 (cond
6699 ((eq (erc-server-buffer) (current-buffer))
6700 (run-hooks 'erc-kill-server-hook))
6701 ((erc-channel-p (erc-default-target))
6702 (run-hooks 'erc-kill-channel-hook))
6704 (run-hooks 'erc-kill-buffer-hook)))))
6706 (defun erc-kill-server ()
6707 "Sends a QUIT command to the server when the server buffer is killed.
6708 This function should be on `erc-kill-server-hook'."
6709 (when (erc-server-process-alive)
6710 (setq erc-server-quitting t)
6711 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6713 (defun erc-kill-channel ()
6714 "Sends a PART command to the server when the channel buffer is killed.
6715 This function should be on `erc-kill-channel-hook'."
6716 (when (erc-server-process-alive)
6717 (let ((tgt (erc-default-target)))
6718 (erc-server-send (format "PART %s :%s" tgt
6719 (funcall erc-part-reason nil))
6720 nil tgt))))
6722 ;;; Dealing with `erc-parsed'
6724 (defun erc-find-parsed-property ()
6725 "Find the next occurrence of the `erc-parsed' text property."
6726 (text-property-not-all (point-min) (point-max) 'erc-parsed nil))
6728 (defun erc-restore-text-properties ()
6729 "Restore the property 'erc-parsed for the region."
6730 (let ((parsed-posn (erc-find-parsed-property)))
6731 (put-text-property
6732 (point-min) (point-max)
6733 'erc-parsed (when parsed-posn (erc-get-parsed-vector parsed-posn)))))
6735 (defun erc-get-parsed-vector (point)
6736 "Return the whole parsed vector on POINT."
6737 (get-text-property point 'erc-parsed))
6739 (defun erc-get-parsed-vector-nick (vect)
6740 "Return nickname in the parsed vector VECT."
6741 (let* ((untreated-nick (and vect (erc-response.sender vect)))
6742 (maybe-nick (when untreated-nick
6743 (car (split-string untreated-nick "!")))))
6744 (when (and (not (null maybe-nick))
6745 (erc-is-valid-nick-p maybe-nick))
6746 untreated-nick)))
6748 (defun erc-get-parsed-vector-type (vect)
6749 "Return message type in the parsed vector VECT."
6750 (and vect
6751 (erc-response.command vect)))
6753 ;; Teach url.el how to open irc:// URLs with ERC.
6754 ;; To activate, customize `url-irc-function' to `url-irc-erc'.
6756 ;;;###autoload
6757 (defun erc-handle-irc-url (host port channel user password)
6758 "Use ERC to IRC on HOST:PORT in CHANNEL as USER with PASSWORD.
6759 If ERC is already connected to HOST:PORT, simply /join CHANNEL.
6760 Otherwise, connect to HOST:PORT as USER and /join CHANNEL."
6761 (let ((server-buffer
6762 (car (erc-buffer-filter
6763 (lambda ()
6764 (and (string-equal erc-session-server host)
6765 (= erc-session-port port)
6766 (erc-open-server-buffer-p)))))))
6767 (with-current-buffer (or server-buffer (current-buffer))
6768 (if (and server-buffer channel)
6769 (erc-cmd-JOIN channel)
6770 (erc-open host port (or user (erc-compute-nick)) (erc-compute-full-name)
6771 (not server-buffer) password nil channel
6772 (when server-buffer
6773 (get-buffer-process server-buffer)))))))
6775 (provide 'erc)
6777 ;; Deprecated. We might eventually stop requiring the goodies automatically.
6778 ;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6779 ;; avoid a recursive require error when byte-compiling the entire package.
6780 (require 'erc-goodies)
6782 ;;; erc.el ends here
6784 ;; Local Variables:
6785 ;; outline-regexp: ";;+"
6786 ;; indent-tabs-mode: t
6787 ;; tab-width: 8
6788 ;; End: