(PC-expand-many-files): Try be more careful when parsing the shell's output.
[emacs.git] / lisp / erc / erc.el
blob63ff60d762e380726231cf087383f766b1412923
1 ;; erc.el --- An Emacs Internet Relay Chat client
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
5 ;; Copyright (C) 2004 Brian Palmer
7 ;; Author: Alexander L. Belikoff (alexander@belikoff.net)
8 ;; Contributors: Sergey Berezin (sergey.berezin@cs.cmu.edu),
9 ;; Mario Lang (mlang@delysid.org),
10 ;; Alex Schroeder (alex@gnu.org)
11 ;; Andreas Fuchs (afs@void.at)
12 ;; Gergely Nagy (algernon@midgard.debian.net)
13 ;; David Edmondson (dme@dme.org)
14 ;; Maintainer: Mario Lang (mlang@delysid.org)
15 ;; Keywords: IRC, chat, client, Internet
17 ;; This file is part of GNU Emacs.
19 ;; GNU Emacs is free software; you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation; either version 2, or (at your option)
22 ;; any later version.
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs; see the file COPYING. If not, write to the
31 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
32 ;; Boston, MA 02110-1301, USA.
34 ;;; Commentary:
36 ;; ERC is an IRC client for Emacs.
38 ;; For more information, see the following URLs:
39 ;; * http://sf.net/projects/erc/
40 ;; * http://www.emacswiki.org/cgi-bin/wiki.pl?EmacsIRCClient
42 ;; Jul-26-2001. erc.el is now in CVS on SourceForge. I invite everyone
43 ;; who wants to hack it to contact me <mlang@delysid.org> in order to
44 ;; get write access on the CVS.
46 ;; Installation:
48 ;; Put erc.el in your load-path, and put (require 'erc) in your .emacs.
50 ;; Configuration:
52 ;; Use M-x customize-group RET erc RET to get an overview
53 ;; of all the variables you can tweak.
55 ;; Usage:
57 ;; To connect to an IRC server, do
59 ;; M-x erc-select RET
61 ;; After you are connected to a server, you can use C-h m or have a look at
62 ;; the IRC menu.
64 ;;; History:
67 ;;; Code:
69 (defconst erc-version-string "Version 5.1.2"
70 "ERC version. This is used by function `erc-version'.")
72 (eval-when-compile (require 'cl))
73 (require 'font-lock)
74 (require 'pp)
75 (require 'thingatpt)
76 (require 'erc-compat)
77 (require 'erc-menu)
79 (defvar erc-official-location
80 "http://erc.sf.net (comments mailto://mlang@delysid.org)"
81 "Location of the ERC client on the Internet.")
83 (defgroup erc nil
84 "Emacs Internet Relay Chat client."
85 :link '(url-link "http://www.emacswiki.org/cgi-bin/wiki.pl?EmacsIRCClient")
86 :prefix "erc-"
87 :group 'applications)
89 (defgroup erc-buffers nil
90 "Creating new ERC buffers"
91 :group 'erc)
93 (defgroup erc-display nil
94 "Settings for how various things are displayed"
95 :group 'erc)
97 (defgroup erc-mode-line-and-header nil
98 "Displaying information in the mode-line and header"
99 :group 'erc-display)
101 (defgroup erc-ignore nil
102 "Ignoring certain messages"
103 :group 'erc)
105 (defgroup erc-query nil
106 "Using separate buffers for private discussions"
107 :group 'erc)
109 (defgroup erc-quit-and-part nil
110 "Quitting and parting channels"
111 :group 'erc)
113 (defgroup erc-paranoia nil
114 "Know what is sent and received; control the display of sensitive data."
115 :group 'erc)
117 (defgroup erc-scripts nil
118 "Running scripts at startup and with /LOAD"
119 :group 'erc)
121 (require 'erc-backend)
123 ;; compatibility with older ERC releases
125 (if (fboundp 'defvaralias)
126 (progn
127 (defvaralias 'erc-announced-server-name 'erc-server-announced-name)
128 (erc-make-obsolete-variable 'erc-announced-server-name
129 'erc-server-announced-name
130 "ERC 5.1")
131 (defvaralias 'erc-process 'erc-server-process)
132 (erc-make-obsolete-variable 'erc-process 'erc-server-process "ERC 5.1")
133 (defvaralias 'erc-default-coding-system 'erc-server-coding-system)
134 (erc-make-obsolete-variable 'erc-default-coding-system
135 'erc-server-coding-system
136 "ERC 5.1"))
137 (message (concat "ERC: The function `defvaralias' is not bound. See the "
138 "NEWS file for variable name changes since ERC 5.0.4.")))
140 (defalias 'erc-send-command 'erc-server-send)
141 (erc-make-obsolete 'erc-send-command 'erc-server-send "ERC 5.1")
143 ;; tunable connection and authentication parameters
145 (defcustom erc-server nil
146 "IRC server to use.
147 See function `erc-compute-server' for more details on connection
148 parameters and authentication."
149 :group 'erc
150 :type '(choice (const nil) string))
152 (defcustom erc-port nil
153 "IRC port to use."
154 :group 'erc
155 :type '(choice (const nil) number string))
157 (defcustom erc-nick nil
158 "Nickname to use.
160 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 nil)
168 (string :tag "Nickname")
169 (repeat string)))
171 (defcustom erc-nick-uniquifier "`"
172 "The character to append to the nick if it is already in use."
173 :group 'erc
174 :type 'string)
176 (defcustom erc-manual-set-nick-on-bad-nick-p nil
177 "If the nickname you chose isn't available, ERC should not automatically
178 attempt to set another nickname. You can manually set another nickname with
179 the /NICK command."
180 :group 'erc
181 :type 'boolean)
183 (defcustom erc-user-full-name nil
184 "User full name.
186 See function `erc-compute-full-name' for more details on connection
187 parameters and authentication."
188 :group 'erc
189 :type '(choice (const nil) string function)
190 :set (lambda (sym val)
191 (if (functionp val)
192 (set sym (funcall val))
193 (set sym val))))
195 (defvar erc-password nil
196 "ERC password to use in authentication (not necessary).")
198 (defcustom erc-user-mode nil
199 "Initial user modes to be set after a connection is established."
200 :group 'erc
201 :type '(choice (const nil) string function))
204 (defcustom erc-prompt-for-password t
205 "Asks before using the default password, or whether to enter a new one."
206 :group 'erc
207 :type 'boolean)
209 (defcustom erc-warn-about-blank-lines t
210 "Warn the user if they attempt to send a blank line."
211 :group 'erc
212 :type 'boolean)
214 (defcustom erc-send-whitespace-lines nil
215 "If set to non-nil, send lines consisting of only whitespace."
216 :group 'erc
217 :type 'boolean)
219 (defcustom erc-hide-prompt nil
220 "If non-nil, do not display the prompt for commands.
222 \(A command is any input starting with a '/').
224 See also the variables `erc-prompt' and `erc-command-indicator'."
225 :group 'erc-display
226 :type 'boolean)
228 ;; tunable GUI stuff
230 (defcustom erc-show-my-nick t
231 "If non-nil, display one's own nickname when sending a message.
233 If non-nil, \"<nickname>\" will be shown.
234 If nil, only \"> \" will be shown."
235 :group 'erc-display
236 :type 'boolean)
238 (define-widget 'erc-message-type 'set
239 "A set of standard IRC Message types."
240 :args '((const "JOIN")
241 (const "KICK")
242 (const "NICK")
243 (const "PART")
244 (const "QUIT")
245 (const "MODE")
246 (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))
248 (defcustom erc-hide-list nil
249 "*List of IRC type messages to hide.
250 A typical value would be '(\"JOIN\" \"PART\" \"QUIT\")."
251 :group 'erc-ignore
252 :type 'erc-message-type)
254 (defvar erc-session-password nil
255 "The password used for the current session.")
256 (make-variable-buffer-local 'erc-session-password)
258 (defcustom erc-disconnected-hook nil
259 "Run this hook with arguments (NICK IP REASON) when disconnected.
260 This happens before automatic reconnection. Note, that
261 `erc-server-QUIT-functions' might not be run when we disconnect,
262 simply because we do not necessarily receive the QUIT event."
263 :group 'erc-hooks
264 :type 'hook)
266 (defcustom erc-complete-functions nil
267 "These functions get called when the user hits TAB in ERC.
268 Each function in turn is called until one returns non-nil to
269 indicate it has handled the input."
270 :group 'erc-hooks
271 :type 'hook)
273 (defcustom erc-join-hook nil
274 "Hook run when we join a channel. Hook functions are called
275 without arguments, with the current buffer set to the buffer of
276 the new channel.
278 See also `erc-server-JOIN-functions', `erc-part-hook'."
279 :group 'erc-hooks
280 :type 'hook)
282 (defcustom erc-quit-hook nil
283 "Hook run when processing a quit command directed at our nick.
285 The hook receives one argument, the current PROCESS.
286 See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
287 :group 'erc-hooks
288 :type 'hook)
290 (defcustom erc-part-hook nil
291 "Hook run when processing a PART message directed at our nick.
293 The hook receives one argument, the current BUFFER.
294 See also `erc-server-QUIT-functions', `erc-quit-hook' and
295 `erc-disconnected-hook'."
296 :group 'erc-hooks
297 :type 'hook)
299 (defcustom erc-kick-hook nil
300 "Hook run when processing a KICK message directed at our nick.
302 The hook receives one argument, the current BUFFER.
303 See also `erc-server-PART-functions' and `erc-part-hook'."
304 :group 'erc-hooks
305 :type 'hook)
307 (defcustom erc-nick-changed-functions nil
308 "List of functions run when your nick was successfully changed.
310 Each function should accept two arguments, NEW-NICK and OLD-NICK."
311 :group 'erc-hooks
312 :type 'hook)
314 (defcustom erc-connect-pre-hook '(erc-initialize-log-marker)
315 "Hook called just before `erc' calls `erc-connect'.
316 Functions are run in the buffer-to-be."
317 :group 'erc-hooks
318 :type 'hook)
321 (defvar erc-channel-users nil
322 "A hash table of members in the current channel, which
323 associates nicknames with cons cells of the form:
324 \(USER . MEMBER-DATA) where USER is a pointer to an
325 erc-server-user struct, and MEMBER-DATA is a pointer to an
326 erc-channel-user struct.")
327 (make-variable-buffer-local 'erc-channel-users)
329 (defvar erc-server-users nil
330 "A hash table of users on the current server, which associates
331 nicknames with erc-server-user struct instances.")
332 (make-variable-buffer-local 'erc-server-users)
334 (defun erc-downcase (string)
335 "Convert STRING to IRC standard conforming downcase."
336 (let ((s (downcase string))
337 (c '((?\[ . ?\{)
338 (?\] . ?\})
339 (?\\ . ?\|)
340 (?~ . ?^))))
341 (save-match-data
342 (while (string-match "[]\\[~]" s)
343 (aset s (match-beginning 0)
344 (cdr (assq (aref s (match-beginning 0)) c)))))
347 (defstruct (erc-server-user (:type vector) :named)
348 ;; User data
349 nickname host login full-name info
350 ;; Buffers
352 ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
353 ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
354 (buffers nil)
357 (defstruct (erc-channel-user (:type vector) :named)
358 op voice
359 ;; Last message time (in the form of the return value of
360 ;; (current-time)
362 ;; This is useful for ordered name completion.
363 (last-message-time nil))
365 (defsubst erc-get-channel-user (nick)
366 "Finds the (USER . CHANNEL-DATA) element corresponding to NICK
367 in the current buffer's `erc-channel-users' hash table."
368 (gethash (erc-downcase nick) erc-channel-users))
370 (defsubst erc-get-server-user (nick)
371 "Finds the USER corresponding to NICK in the current server's
372 `erc-server-users' hash table."
373 (with-current-buffer (process-buffer erc-server-process)
374 (gethash (erc-downcase nick) erc-server-users)))
376 (defsubst erc-add-server-user (nick user)
377 "This function is for internal use only.
379 Adds USER with nickname NICK to the `erc-server-users' hash table."
380 (with-current-buffer (process-buffer erc-server-process)
381 (puthash (erc-downcase nick) user erc-server-users)))
383 (defsubst erc-remove-server-user (nick)
384 "This function is for internal use only.
386 Removes the user with nickname NICK from the `erc-server-users'
387 hash table. This user is not removed from the
388 `erc-channel-users' lists of other buffers.
390 See also: `erc-remove-user'."
391 (with-current-buffer (process-buffer erc-server-process)
392 (remhash (erc-downcase nick) erc-server-users)))
394 (defun erc-change-user-nickname (user new-nick)
395 "This function is for internal use only.
397 Changes the nickname of USER to NEW-NICK in the
398 `erc-server-users' hash table. The `erc-channel-users' lists of
399 other buffers are also changed."
400 (let ((nick (erc-server-user-nickname user)))
401 (setf (erc-server-user-nickname user) new-nick)
402 (with-current-buffer (process-buffer erc-server-process)
403 (remhash (erc-downcase nick) erc-server-users)
404 (puthash (erc-downcase new-nick) user erc-server-users))
405 (dolist (buf (erc-server-user-buffers user))
406 (if (buffer-live-p buf)
407 (with-current-buffer buf
408 (let ((cdata (erc-get-channel-user nick)))
409 (remhash (erc-downcase nick) erc-channel-users)
410 (puthash (erc-downcase new-nick) cdata
411 erc-channel-users)))))))
413 (defun erc-remove-channel-user (nick)
414 "This function is for internal use only.
416 Removes the user with nickname NICK from the `erc-channel-users'
417 list for this channel. If this user is not in the
418 `erc-channel-users' list of any other buffers, the user is also
419 removed from the server's `erc-server-users' list.
421 See also: `erc-remove-server-user' and `erc-remove-user'."
422 (let ((channel-data (erc-get-channel-user nick)))
423 (when channel-data
424 (let ((user (car channel-data)))
425 (setf (erc-server-user-buffers user)
426 (delq (current-buffer)
427 (erc-server-user-buffers user)))
428 (remhash (erc-downcase nick) erc-channel-users)
429 (if (null (erc-server-user-buffers user))
430 (erc-remove-server-user nick))))))
432 (defun erc-remove-user (nick)
433 "This function is for internal use only.
435 Removes the user with nickname NICK from the `erc-server-users'
436 list as well as from all `erc-channel-users' lists.
438 See also: `erc-remove-server-user' and
439 `erc-remove-channel-user'."
440 (let ((user (erc-get-server-user nick)))
441 (when user
442 (let ((buffers (erc-server-user-buffers user)))
443 (dolist (buf buffers)
444 (if (buffer-live-p buf)
445 (with-current-buffer buf
446 (remhash (erc-downcase nick) erc-channel-users)
447 (run-hooks 'erc-channel-members-changed-hook)))))
448 (erc-remove-server-user nick))))
450 (defun erc-remove-channel-users ()
451 "This function is for internal use only.
453 Removes all users in the current channel. This is called by
454 `erc-server-PART' and `erc-server-QUIT'."
455 (when (and erc-server-connected
456 (erc-server-process-alive)
457 (hash-table-p erc-channel-users))
458 (maphash (lambda (nick cdata)
459 (erc-remove-channel-user nick))
460 erc-channel-users)
461 (clrhash erc-channel-users)))
463 (defsubst erc-channel-user-op-p (nick)
464 "Return `t' if NICK is an operator in the current channel."
465 (and nick
466 (hash-table-p erc-channel-users)
467 (let ((cdata (erc-get-channel-user nick)))
468 (and cdata (cdr cdata)
469 (erc-channel-user-op (cdr cdata))))))
471 (defsubst erc-channel-user-voice-p (nick)
472 "Return `t' if NICK has voice in the current channel."
473 (and nick
474 (hash-table-p erc-channel-users)
475 (let ((cdata (erc-get-channel-user nick)))
476 (and cdata (cdr cdata)
477 (erc-channel-user-voice (cdr cdata))))))
479 (defun erc-get-channel-user-list ()
480 "Returns a list of users in the current channel. Each element
481 of the list is of the form (USER . CHANNEL-DATA), where USER is
482 an erc-server-user struct, and CHANNEL-DATA is either `nil' or an
483 erc-channel-user struct.
485 See also: `erc-sort-channel-users-by-activity'"
486 (let (users)
487 (if (hash-table-p erc-channel-users)
488 (maphash (lambda (nick cdata)
489 (setq users (cons cdata users)))
490 erc-channel-users))
491 users))
493 (defun erc-get-server-nickname-list ()
494 "Returns a list of known nicknames on the current server."
495 (if (erc-server-process-alive)
496 (with-current-buffer (erc-server-buffer)
497 (let (nicks)
498 (when (hash-table-p erc-server-users)
499 (maphash (lambda (n user)
500 (setq nicks
501 (cons (erc-server-user-nickname user)
502 nicks)))
503 erc-server-users)
504 nicks)))))
506 (defun erc-get-channel-nickname-list ()
507 "Returns a list of known nicknames on the current channel."
508 (let (nicks)
509 (when (hash-table-p erc-channel-users)
510 (maphash (lambda (n cdata)
511 (setq nicks
512 (cons (erc-server-user-nickname (car cdata))
513 nicks)))
514 erc-channel-users)
515 nicks)))
517 (defun erc-get-server-nickname-alist ()
518 "Returns an alist of known nicknames on the current server."
519 (if (erc-server-process-alive)
520 (with-current-buffer (erc-server-buffer)
521 (let (nicks)
522 (when (hash-table-p erc-server-users)
523 (maphash (lambda (n user)
524 (setq nicks
525 (cons (cons (erc-server-user-nickname user) nil)
526 nicks)))
527 erc-server-users)
528 nicks)))))
530 (defun erc-get-channel-nickname-alist ()
531 "Returns an alist of known nicknames on the current channel."
532 (let (nicks)
533 (when (hash-table-p erc-channel-users)
534 (maphash (lambda (n cdata)
535 (setq nicks
536 (cons (cons (erc-server-user-nickname (car cdata)) nil)
537 nicks)))
538 erc-channel-users)
539 nicks)))
541 (defun erc-sort-channel-users-by-activity (list)
542 "Sorts LIST such that users which have spoken most recently are
543 listed first. LIST must be of the form (USER . CHANNEL-DATA).
545 See also: `erc-get-channel-user-list'."
546 (sort list
547 (lambda (x y)
548 (when (and
549 (cdr x) (cdr y))
550 (let ((tx (erc-channel-user-last-message-time (cdr x)))
551 (ty (erc-channel-user-last-message-time (cdr y))))
552 (if tx
553 (if ty
554 (time-less-p ty tx)
556 nil))))))
558 (defun erc-sort-channel-users-alphabetically (list)
559 "Sort LIST so that users' nicknames are in alphabetical order.
560 LIST must be of the form (USER . CHANNEL-DATA).
562 See also: `erc-get-channel-user-list'."
563 (sort list
564 (lambda (x y)
565 (when (and
566 (cdr x) (cdr y))
567 (let ((nickx (downcase (erc-server-user-nickname (car x))))
568 (nicky (downcase (erc-server-user-nickname (car y)))))
569 (if nickx
570 (if nicky
571 (string-lessp nickx nicky)
573 nil))))))
575 (defvar erc-channel-topic nil
576 "A topic string for the channel. Should only be used in channel-buffers.")
577 (make-variable-buffer-local 'erc-channel-topic)
579 (defvar erc-channel-modes nil
580 "List of strings representing channel modes.
581 E.g. '(\"i\" \"m\" \"s\" \"b Quake!*@*\")
582 \(not sure the ban list will be here, but why not)")
583 (make-variable-buffer-local 'erc-channel-modes)
585 (defvar erc-insert-marker nil
586 "The place where insertion of new text in erc buffers should happen.")
587 (make-variable-buffer-local 'erc-insert-marker)
589 (defvar erc-input-marker nil
590 "The marker where input should be inserted.")
591 (make-variable-buffer-local 'erc-input-marker)
593 (defun erc-string-no-properties (string)
594 "Return a copy of STRING will all text-properties removed."
595 (let ((newstring (copy-sequence string)))
596 (set-text-properties 0 (length newstring) nil newstring)
597 newstring))
599 (defcustom erc-prompt "ERC>"
600 "Prompt used by ERC. Trailing whitespace is not required."
601 :group 'erc-display
602 :type '(choice string function))
604 (defun erc-prompt ()
605 "Return the input prompt as a string.
607 See also the variable `erc-prompt'."
608 (let ((prompt (if (functionp erc-prompt)
609 (funcall erc-prompt)
610 erc-prompt)))
611 (if (> (length prompt) 0)
612 (concat prompt " ")
613 prompt)))
615 (defcustom erc-command-indicator nil
616 "Indicator used by ERC for showing commands.
618 If non-nil, this will be used in the ERC buffer to indicate
619 commands (i.e., input starting with a '/').
621 If nil, the prompt will be constructed from the variable `erc-prompt'."
622 :group 'erc-display
623 :type '(choice (const nil) string function))
625 (defun erc-command-indicator ()
626 "Return the command indicator prompt as a string.
628 This only has any meaning if the variable `erc-command-indicator' is non-nil."
629 (and erc-command-indicator
630 (let ((prompt (if (functionp erc-command-indicator)
631 (funcall erc-command-indicator)
632 erc-command-indicator)))
633 (if (> (length prompt) 0)
634 (concat prompt " ")
635 prompt))))
637 (defcustom erc-notice-prefix "*** "
638 "*Prefix for all notices."
639 :group 'erc-display
640 :type 'string)
642 (defcustom erc-notice-highlight-type 'all
643 "*Determines how to highlight notices.
644 See `erc-notice-prefix'.
646 The following values are allowed:
648 'prefix - highlight notice prefix only
649 'all - highlight the entire notice
651 Any other value disables notice's highlighting altogether."
652 :group 'erc-display
653 :type '(choice (const :tag "highlight notice prefix only" prefix)
654 (const :tag "highlight the entire notice" all)
655 (const :tag "don't highlight notices at all" nil)))
657 (defcustom erc-echo-notice-hook nil
658 "*Specifies a list of functions to call to echo a private
659 notice. Each function is called with four arguments, the string
660 to display, the parsed server message, the target buffer (or
661 nil), and the sender. The functions are called in order, until a
662 function evaluates to non-nil. These hooks are called after
663 those specified in `erc-echo-notice-always-hook'.
665 See also: `erc-echo-notice-always-hook',
666 `erc-echo-notice-in-default-buffer',
667 `erc-echo-notice-in-target-buffer',
668 `erc-echo-notice-in-minibuffer',
669 `erc-echo-notice-in-server-buffer',
670 `erc-echo-notice-in-active-non-server-buffer',
671 `erc-echo-notice-in-active-buffer',
672 `erc-echo-notice-in-user-buffers',
673 `erc-echo-notice-in-user-and-target-buffers',
674 `erc-echo-notice-in-first-user-buffer'"
675 :group 'erc-hooks
676 :type 'hook
677 :options '(erc-echo-notice-in-default-buffer
678 erc-echo-notice-in-target-buffer
679 erc-echo-notice-in-minibuffer
680 erc-echo-notice-in-server-buffer
681 erc-echo-notice-in-active-non-server-buffer
682 erc-echo-notice-in-active-buffer
683 erc-echo-notice-in-user-buffers
684 erc-echo-notice-in-user-and-target-buffers
685 erc-echo-notice-in-first-user-buffer))
687 (defcustom erc-echo-notice-always-hook
688 '(erc-echo-notice-in-default-buffer)
689 "*Specifies a list of functions to call to echo a private
690 notice. Each function is called with four arguments, the string
691 to display, the parsed server message, the target buffer (or
692 nil), and the sender. The functions are called in order, and all
693 functions are called. These hooks are called before those
694 specified in `erc-echo-notice-hook'.
696 See also: `erc-echo-notice-hook',
697 `erc-echo-notice-in-default-buffer',
698 `erc-echo-notice-in-target-buffer',
699 `erc-echo-notice-in-minibuffer',
700 `erc-echo-notice-in-server-buffer',
701 `erc-echo-notice-in-active-non-server-buffer',
702 `erc-echo-notice-in-active-buffer',
703 `erc-echo-notice-in-user-buffers',
704 `erc-echo-notice-in-user-and-target-buffers',
705 `erc-echo-notice-in-first-user-buffer'"
706 :group 'erc-hooks
707 :type 'hook
708 :options '(erc-echo-notice-in-default-buffer
709 erc-echo-notice-in-target-buffer
710 erc-echo-notice-in-minibuffer
711 erc-echo-notice-in-server-buffer
712 erc-echo-notice-in-active-non-server-buffer
713 erc-echo-notice-in-active-buffer
714 erc-echo-notice-in-user-buffers
715 erc-echo-notice-in-user-and-target-buffers
716 erc-echo-notice-in-first-user-buffer))
718 ;; other tunable parameters
720 (defcustom erc-whowas-on-nosuchnick nil
721 "*If non-nil, do a whowas on a nick if no such nick."
722 :group 'erc
723 :type 'boolean)
725 (defcustom erc-verbose-server-ping nil
726 "*If non-nil, show every time you get a PING or PONG from the server."
727 :group 'erc-paranoia
728 :type 'boolean)
730 (defcustom erc-public-away-p nil
731 "*Let others know you are back when you are no longer marked away.
732 This happens in this form:
733 * <nick> is back (gone for <time>)
735 Many consider it impolite to do so automatically."
736 :group 'erc
737 :type 'boolean)
739 (defcustom erc-away-nickname nil
740 "*The nickname to take when you are marked as being away."
741 :group 'erc
742 :type '(choice (const nil)
743 string))
745 (defcustom erc-paranoid nil
746 "If non-nil, then all incoming CTCP requests will be shown."
747 :group 'erc-paranoia
748 :type 'boolean)
750 (defcustom erc-disable-ctcp-replies nil
751 "Disable replies to CTCP requests that require a reply.
752 If non-nil, then all incoming CTCP requests that normally require
753 an automatic reply (like VERSION or PING) will be ignored. Good to
754 set if some hacker is trying to flood you away."
755 :group 'erc-paranoia
756 :type 'boolean)
758 (defcustom erc-anonymous-login t
759 "Be paranoid, don't give away your machine name."
760 :group 'erc-paranoia
761 :type 'boolean)
763 (defcustom erc-prompt-for-channel-key nil
764 "Prompt for channel key when using `erc-join-channel' interactively"
765 :group 'erc
766 :type 'boolean)
768 (defcustom erc-email-userid "user"
769 "Use this as your email user ID."
770 :group 'erc
771 :type 'string)
773 (defcustom erc-ignore-list nil
774 "*List of regexps matching user identifiers to ignore.
776 A user identifier has the form \"nick!login@host\". If an
777 identifier matches, the message from the person will not be
778 processed."
779 :group 'erc-ignore
780 :type '(repeat regexp))
781 (make-variable-buffer-local 'erc-ignore-list)
783 (defcustom erc-ignore-reply-list nil
784 "*List of regexps matching user identifiers to ignore completely.
786 This differs from `erc-ignore-list' in that it also ignores any
787 messages directed at the user.
789 A user identifier has the form \"nick!login@host\".
791 If an identifier matches, or a message is addressed to a nick
792 whose identifier matches, the message will not be processed.
794 CAVEAT: ERC doesn't know about the user and host of anyone who
795 was already in the channel when you joined, but never said
796 anything, so it won't be able to match the user and host of those
797 people. You can update the ERC internal info using /WHO *."
798 :group 'erc-ignore
799 :type '(repeat regexp))
801 (defvar erc-flood-protect t
802 "*If non-nil, flood protection is enabled.
803 Flooding is sending too much information to the server in too
804 short of an interval, which may cause the server to terminate the
805 connection.
807 See `erc-server-flood-margin' for other flood-related parameters.")
809 ;; Script parameters
811 (defcustom erc-startup-file-list
812 '("~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
813 "List of files to try for a startup script.
814 The first existent and readable one will get executed.
816 If the filename ends with `.el' it is presumed to be an emacs-lisp
817 script and it gets (load)ed. Otherwise is is treated as a bunch of
818 regular IRC commands"
819 :group 'erc-scripts
820 :type '(repeat file))
822 (defcustom erc-script-path nil
823 "List of directories to look for a script in /load command.
824 The script is first searched in the current directory, then in each
825 directory in the list."
826 :group 'erc-scripts
827 :type '(repeat directory))
829 (defcustom erc-script-echo t
830 "*If not-NIL, echo the IRC script commands locally."
831 :group 'erc-scripts
832 :type 'boolean)
834 (defvar erc-last-saved-position nil
835 "A marker containing the position the current buffer was last saved at.")
836 (make-variable-buffer-local 'erc-last-saved-position)
838 (defcustom erc-kill-buffer-on-part nil
839 "Kill the channel buffer on PART.
840 This variable should probably stay nil, as ERC can reuse buffers if
841 you rejoin them later."
842 :group 'erc-quit-and-part
843 :type 'boolean)
845 (defcustom erc-kill-queries-on-quit nil
846 "Kill all query (also channel) buffers of this server on QUIT.
847 See the variable `erc-kill-buffer-on-part' for details."
848 :group 'erc-quit-and-part
849 :type 'boolean)
851 (defcustom erc-kill-server-buffer-on-quit nil
852 "Kill the server buffer of the process on QUIT."
853 :group 'erc-quit-and-part
854 :type 'boolean)
856 (defcustom erc-quit-reason-various-alist nil
857 "Alist of possible arguments to the /quit command.
859 Each element has the form:
860 (REGEXP RESULT)
862 If REGEXP matches the argument to /quit, then its relevant RESULT
863 will be used. RESULT may be either a string, or a function. If
864 a function, it should return the quit message as a string.
866 If no elements match, then the empty string is used.
868 As an example:
869 (setq erc-quit-reason-various-alist
870 '((\"zippy\" erc-quit-reason-zippy)
871 (\"xmms\" dme:now-playing)
872 (\"version\" erc-quit-reason-normal)
873 (\"home\" \"Gone home !\")
874 (\"\" \"Default Reason\")))
875 If the user types \"/quit zippy\", then a Zippy the Pinhead quotation
876 will be used as the quit message."
877 :group 'erc-quit-and-part
878 :type '(repeat (list regexp (choice (string) (function)))))
880 (defcustom erc-part-reason-various-alist nil
881 "Alist of possible arguments to the /part command.
883 Each element has the form:
884 (REGEXP RESULT)
886 If REGEXP matches the argument to /part, then its relevant RESULT
887 will be used. RESULT may be either a string, or a function. If
888 a function, it should return the part message as a string.
890 If no elements match, then the empty string is used.
892 As an example:
893 (setq erc-part-reason-various-alist
894 '((\"zippy\" erc-part-reason-zippy)
895 (\"xmms\" dme:now-playing)
896 (\"version\" erc-part-reason-normal)
897 (\"home\" \"Gone home !\")
898 (\"\" \"Default Reason\")))
899 If the user types \"/part zippy\", then a Zippy the Pinhead quotation
900 will be used as the part message."
901 :group 'erc-quit-and-part
902 :type '(repeat (list regexp (choice (string) (function)))))
904 (defcustom erc-quit-reason 'erc-quit-reason-normal
905 "*A function which returns the reason for quitting.
907 The function is passed a single argument, the string typed by the
908 user after \"/quit\"."
909 :group 'erc-quit-and-part
910 :type '(choice (const erc-quit-reason-normal)
911 (const erc-quit-reason-zippy)
912 (const erc-quit-reason-various)
913 (symbol)))
915 (defcustom erc-part-reason 'erc-part-reason-normal
916 "A function which returns the reason for parting a channel.
918 The function is passed a single argument, the string typed by the
919 user after \"/PART\"."
920 :group 'erc-quit-and-part
921 :type '(choice (const erc-part-reason-normal)
922 (const erc-part-reason-zippy)
923 (const erc-part-reason-various)
924 (symbol)))
926 (defvar erc-grab-buffer-name "*erc-grab*"
927 "The name of the buffer created by `erc-grab-region'.")
929 ;; variables available for IRC scripts
931 (defvar erc-user-information "ERC User"
932 "USER_INFORMATION IRC variable.")
934 ;; Hooks
936 (defgroup erc-hooks nil
937 "Hook variables for fancy customizations of ERC."
938 :group 'erc)
940 (defcustom erc-mode-hook nil
941 "Hook run after `erc-mode' setup is finished."
942 :group 'erc-hooks
943 :type 'hook
944 :options '(erc-add-scroll-to-bottom))
946 (defcustom erc-timer-hook nil
947 "Put functions which should get called more or less periodically here.
948 The idea is that servers always play ping pong with the client, and so there
949 is no need for any idle-timer games with Emacs."
950 :group 'erc-hooks
951 :type 'hook)
953 (defcustom erc-insert-pre-hook nil
954 "Hook called first when some text is inserted through `erc-display-line'.
955 It gets called with one argument, STRING.
956 To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
957 Filtering functions can set `erc-insert-this' to nil to avoid
958 display of that particular string at all."
959 :group 'erc-hooks
960 :type 'hook)
962 (defcustom erc-send-pre-hook nil
963 "Hook called first when some text is sent through `erc-send-current-line'.
964 It gets called with one argument, STRING.
966 To change the text that will be sent, set the variable STR which is
967 used in `erc-send-current-line'.
969 To change the text inserted into the buffer without changing the text
970 that will be sent, use `erc-send-modify-hook' instead.
972 Filtering functions can set `erc-send-this' to nil to avoid sending of
973 that particular string at all and `erc-insert-this' to prevent
974 inserting that particular string into the buffer.
976 Note that it's useless to set `erc-send-this' to nil and
977 `erc-insert-this' to t. ERC is sane enough to not insert the text
978 anyway."
979 :group 'erc-hooks
980 :type 'hook)
982 (defvar erc-insert-this t
983 "Insert the text into the target buffer or not.
984 Functions on `erc-insert-pre-hook' can set this variable to nil
985 if they wish to avoid insertion of a particular string.")
987 (defvar erc-send-this t
988 "Send the text to the target or not.
989 Functions on `erc-send-pre-hook' can set this variable to nil
990 if they wish to avoid sending of a particular string.")
992 (defcustom erc-insert-modify-hook ()
993 "Insertion hook for functions that will change the text's appearance.
994 This hook is called just after `erc-insert-pre-hook' when the value
995 of `erc-insert-this' is t.
996 While this hook is run, narrowing is in effect and `current-buffer' is
997 the buffer where the text got inserted. One possible value to add here
998 is `erc-fill'."
999 :group 'erc-hooks
1000 :type 'hook)
1002 (defcustom erc-insert-post-hook nil
1003 "This hook is called just after `erc-insert-modify-hook'.
1004 At this point, all modifications from prior hook functions are done."
1005 :group 'erc-hooks
1006 :type 'hook
1007 :options '(erc-truncate-buffer
1008 erc-make-read-only
1009 erc-save-buffer-in-logs))
1011 (defcustom erc-send-modify-hook nil
1012 "Sending hook for functions that will change the text's appearance.
1013 This hook is called just after `erc-send-pre-hook' when the values
1014 of `erc-send-this' and `erc-insert-this' are both t.
1015 While this hook is run, narrowing is in effect and `current-buffer' is
1016 the buffer where the text got inserted.
1018 Note that no function in this hook can change the appearance of the
1019 text that is sent. Only changing the sent text's appearance on the
1020 sending user's screen is possible. One possible value to add here
1021 is `erc-fill'."
1022 :group 'erc-hooks
1023 :type 'hook)
1025 (defcustom erc-send-post-hook nil
1026 "This hook is called just after `erc-send-modify-hook'.
1027 At this point, all modifications from prior hook functions are done.
1028 NOTE: The functions on this hook are called _before_ sending a command
1029 to the server.
1031 This function is called with narrowing, ala `erc-send-modify-hook'"
1032 :group 'erc-hooks
1033 :type 'hook
1034 :options '(erc-make-read-only))
1036 (defcustom erc-send-completed-hook
1037 (when (featurep 'emacspeak)
1038 (list (byte-compile
1039 (lambda (str)
1040 (emacspeak-auditory-icon 'select-object)))))
1041 "Hook called after a message has been parsed by ERC.
1043 The single argument to the functions is the unmodified string
1044 which the local user typed."
1045 :group 'erc-hooks
1046 :type 'hook)
1047 ;; mode-specific tables
1049 (defvar erc-mode-syntax-table
1050 (let ((syntax-table (make-syntax-table)))
1051 (modify-syntax-entry ?\" ". " syntax-table)
1052 (modify-syntax-entry ?\\ ". " syntax-table)
1053 (modify-syntax-entry ?' "w " syntax-table)
1054 ;; Make dabbrev-expand useful for nick names
1055 (modify-syntax-entry ?< "." syntax-table)
1056 (modify-syntax-entry ?> "." syntax-table)
1057 syntax-table)
1058 "Syntax table used while in ERC mode.")
1060 (defvar erc-mode-abbrev-table nil
1061 "Abbrev table used while in ERC mode.")
1062 (define-abbrev-table 'erc-mode-abbrev-table ())
1064 (defvar erc-mode-map
1065 (let ((map (make-sparse-keymap)))
1066 (define-key map "\C-m" 'erc-send-current-line)
1067 (define-key map "\C-a" 'erc-bol)
1068 (define-key map [home] 'erc-bol)
1069 (define-key map "\C-c\C-a" 'erc-bol)
1070 (define-key map "\C-c\C-b" 'erc-iswitchb)
1071 (define-key map "\C-c\C-c" 'erc-toggle-interpret-controls)
1072 (define-key map "\C-c\C-d" 'erc-input-action)
1073 (define-key map "\C-c\C-e" 'erc-toggle-ctcp-autoresponse)
1074 (define-key map "\C-c\C-f" 'erc-toggle-flood-control)
1075 (define-key map "\C-c\C-i" 'erc-invite-only-mode)
1076 (define-key map "\C-c\C-j" 'erc-join-channel)
1077 (define-key map "\C-c\C-n" 'erc-channel-names)
1078 (define-key map "\C-c\C-o" 'erc-get-channel-mode-from-keypress)
1079 (define-key map "\C-c\C-p" 'erc-part-from-channel)
1080 (define-key map "\C-c\C-q" 'erc-quit-server)
1081 (define-key map "\C-c\C-r" 'erc-remove-text-properties-region)
1082 (define-key map "\C-c\C-t" 'erc-set-topic)
1083 (define-key map "\C-c\C-u" 'erc-kill-input)
1084 (define-key map "\M-\t" 'ispell-complete-word)
1085 (define-key map "\t" 'erc-complete-word)
1087 ;; Suppress `font-lock-fontify-block' key binding since it
1088 ;; destroys face properties.
1089 (if (fboundp 'command-remapping)
1090 (define-key map [remap font-lock-fontify-block] 'undefined)
1091 (substitute-key-definition
1092 'font-lock-fontify-block 'undefined map global-map))
1094 map)
1095 "ERC keymap.")
1097 ;; Faces
1099 ; Honestly, I have a horrible sense of color and the "defaults" below
1100 ; are supposed to be really bad. But colors ARE required in IRC to
1101 ; convey different parts of conversation. If you think you know better
1102 ; defaults - send them to me.
1104 ;; Now colors are a bit nicer, at least to my eyes.
1105 ;; You may still want to change them to better fit your background.-- S.B.
1107 (defgroup erc-faces nil
1108 "Faces for ERC."
1109 :group 'erc)
1111 (defface erc-default-face '((t))
1112 "ERC default face."
1113 :group 'erc-faces)
1115 (defface erc-direct-msg-face '((t (:foreground "IndianRed")))
1116 "ERC face used for messages you receive in the main erc buffer."
1117 :group 'erc-faces)
1119 (defface erc-input-face '((t (:foreground "brown")))
1120 "ERC face used for your input."
1121 :group 'erc-faces)
1123 (defface erc-prompt-face
1124 '((t (:bold t :foreground "Black" :background"lightBlue2")))
1125 "ERC face for the prompt."
1126 :group 'erc-faces)
1128 (defface erc-command-indicator-face
1129 '((t (:bold t)))
1130 "ERC face for the command indicator.
1131 See the variable `erc-command-indicator'."
1132 :group 'erc-faces)
1134 (defface erc-notice-face '((t (:bold t :foreground "SlateBlue")))
1135 "ERC face for notices."
1136 :group 'erc-faces)
1138 (defface erc-action-face '((t (:bold t)))
1139 "ERC face for actions generated by /ME."
1140 :group 'erc-faces)
1142 (defface erc-error-face '((t (:foreground "red")))
1143 "ERC face for errors."
1144 :group 'erc-faces)
1146 (defface erc-nick-default-face '((t (:bold t)))
1147 "ERC nickname default face."
1148 :group 'erc-faces)
1150 (defface erc-nick-msg-face '((t (:bold t :foreground "IndianRed")))
1151 "ERC nickname face for private messages."
1152 :group 'erc-faces)
1154 ;; Debugging support
1156 (defvar erc-log-p nil
1157 "When set to t, generate debug messages in a separate debug buffer.")
1159 (defvar erc-debug-log-file (expand-file-name "ERC.debug")
1160 "Debug log file name.")
1162 (defvar erc-dbuf nil)
1163 (make-variable-buffer-local 'erc-dbuf)
1165 (defmacro define-erc-module (name alias doc enable-body disable-body
1166 &optional local-p)
1167 "Define a new minor mode using ERC conventions.
1168 Symbol NAME is the name of the module.
1169 Symbol ALIAS is the alias to use, or nil.
1170 DOC is the documentation string to use for the minor mode.
1171 ENABLE-BODY is a list of expressions used to enable the mode.
1172 DISABLE-BODY is a list of expressions used to disable the mode.
1173 If LOCAL-P is non-nil, the mode will be created as a buffer-local
1174 mode. Rather than a global one.
1176 This will define a minor mode called erc-NAME-mode, possibly
1177 an alias erc-ALIAS-mode, as well as the helper functions
1178 erc-NAME-enable, and erc-NAME-disable.
1180 Example:
1182 ;;;###autoload (autoload 'erc-replace-mode \"erc-replace\")
1183 (define-erc-module replace nil
1184 \"This mode replaces incoming text according to `erc-replace-alist'.\"
1185 ((add-hook 'erc-insert-modify-hook
1186 'erc-replace-insert))
1187 ((remove-hook 'erc-insert-modify-hook
1188 'erc-replace-insert)))"
1189 (let* ((sn (symbol-name name))
1190 (mode (intern (format "erc-%s-mode" (downcase sn))))
1191 (group (intern (format "erc-%s" (downcase sn))))
1192 (enable (intern (format "erc-%s-enable" (downcase sn))))
1193 (disable (intern (format "erc-%s-disable" (downcase sn)))))
1194 `(progn
1195 (erc-define-minor-mode
1196 ,mode
1197 ,(format "Toggle ERC %S mode.
1198 With arg, turn ERC %S mode on if and only if arg is positive.
1199 %s" name name doc)
1200 nil nil nil
1201 :global ,(not local-p) :group (quote ,group)
1202 (if ,mode
1203 (,enable)
1204 (,disable)))
1205 (defun ,enable ()
1206 ,(format "Enable ERC %S mode."
1207 name)
1208 (interactive)
1209 (add-to-list 'erc-modules (quote ,name))
1210 (setq ,mode t)
1211 ,@enable-body)
1212 (defun ,disable ()
1213 ,(format "Disable ERC %S mode."
1214 name)
1215 (interactive)
1216 (setq erc-modules (delq (quote ,name) erc-modules))
1217 (setq ,mode nil)
1218 ,@disable-body)
1219 ,(when (and alias (not (eq name alias)))
1220 `(defalias
1221 (quote
1222 ,(intern
1223 (format "erc-%s-mode"
1224 (downcase (symbol-name alias)))))
1225 (quote
1226 ,mode))))))
1228 (put 'define-erc-module 'doc-string-elt 3)
1230 (defun erc-once-with-server-event (event &rest forms)
1231 "Execute FORMS the next time EVENT occurs in the `current-buffer'.
1233 You should make sure that `current-buffer' is a server buffer.
1235 This function temporarily adds a function to EVENT's hook to
1236 execute FORMS. After FORMS are run, the function is removed from
1237 EVENT's hook. The last expression of FORMS should be either nil
1238 or t. nil indicates that the other functions on EVENT's hook
1239 should be run too, and t indicates that other functions should
1240 not be run.
1242 Please be sure to use this function in server-buffers. In
1243 channel-buffers it may not work at all, as it uses the LOCAL
1244 argument of `add-hook' and `remove-hook' to ensure multiserver
1245 capabilities."
1246 (unless (erc-server-buffer-p)
1247 (error
1248 "You should only run `erc-once-with-server-event' in a server buffer"))
1249 (let ((fun (erc-gensym))
1250 (hook (erc-get-hook event)))
1251 (put fun 'erc-original-buffer (current-buffer))
1252 (fset fun `(lambda (proc parsed)
1253 (with-current-buffer (get ',fun 'erc-original-buffer)
1254 (remove-hook ',hook ',fun t))
1255 (fmakunbound ',fun)
1256 ,@forms))
1257 (add-hook hook fun nil t)
1258 fun))
1260 (defun erc-once-with-server-event-global (event &rest forms)
1261 "Execute FORMS the next time EVENT occurs in any server buffer.
1263 This function temporarily prepends a function to EVENT's hook to
1264 execute FORMS. After FORMS are run, the function is removed from
1265 EVENT's hook. The last expression of FORMS should be either nil
1266 or t. nil indicates that the other functions on EVENT's hook
1267 should be run too, and t indicates that other functions should
1268 not be run.
1270 When FORMS execute, the current buffer is the server buffer associated with the
1271 connection over which the data was received that triggered EVENT."
1272 (let ((fun (erc-gensym))
1273 (hook (erc-get-hook event)))
1274 (fset fun `(lambda (proc parsed)
1275 (remove-hook ',hook ',fun)
1276 (fmakunbound ',fun)
1277 ,@forms))
1278 (add-hook hook fun nil nil)
1279 fun))
1281 (defmacro erc-log (string)
1282 "Logs STRING if logging is on (see `erc-log-p')."
1283 `(when erc-log-p
1284 (erc-log-aux ,string)))
1286 (defun erc-server-buffer ()
1287 "Return the server buffer for the current buffer's process.
1288 The buffer-local variable `erc-server-process' is used to find
1289 the process buffer."
1290 (and (erc-server-buffer-live-p)
1291 (process-buffer erc-server-process)))
1293 (defun erc-server-buffer-live-p ()
1294 "Return t if the buffer associated with `erc-server-process'
1295 has not been killed."
1296 (and (processp erc-server-process)
1297 (buffer-live-p (process-buffer erc-server-process))))
1299 (defun erc-server-buffer-p (&optional buffer)
1300 "Return non-nil if argument BUFFER is an ERC server buffer.
1302 If BUFFER is nil, the current buffer is used."
1303 (with-current-buffer (or buffer (current-buffer))
1304 (and (eq major-mode 'erc-mode)
1305 (null (erc-default-target)))))
1307 (defun erc-query-buffer-p (&optional buffer)
1308 "Return non-nil if BUFFER is an ERC query buffer.
1309 If BUFFER is nil, the current buffer is used."
1310 (with-current-buffer (or buffer (current-buffer))
1311 (let ((target (erc-default-target)))
1312 (and (eq major-mode 'erc-mode)
1313 target
1314 (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
1316 (defun erc-ison-p (nick)
1317 "Return non-nil if NICK is online."
1318 (interactive "sNick: ")
1319 (with-current-buffer (erc-server-buffer)
1320 (let ((erc-online-p 'unknown))
1321 (erc-once-with-server-event
1323 `(let ((ison (split-string (aref parsed 3))))
1324 (setq erc-online-p (car (erc-member-ignore-case ,nick ison)))
1326 (erc-server-send (format "ISON %s" nick))
1327 (while (eq erc-online-p 'unknown) (accept-process-output))
1328 (if (interactive-p)
1329 (message "%s is %sonline"
1330 (or erc-online-p nick)
1331 (if erc-online-p "" "not "))
1332 erc-online-p))))
1334 (defun erc-log-aux (string)
1335 "Do the debug logging of STRING."
1336 (let ((cb (current-buffer))
1337 (point 1)
1338 (was-eob nil)
1339 (session-buffer (erc-server-buffer)))
1340 (if session-buffer
1341 (progn
1342 (set-buffer session-buffer)
1343 (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
1344 (progn
1345 (setq erc-dbuf (get-buffer-create
1346 (concat "*ERC-DEBUG: "
1347 erc-session-server "*")))))
1348 (set-buffer erc-dbuf)
1349 (setq point (point))
1350 (setq was-eob (eobp))
1351 (goto-char (point-max))
1352 (insert (concat "** " string "\n"))
1353 (if was-eob (goto-char (point-max))
1354 (goto-char point))
1355 (set-buffer cb))
1356 (message "ERC: ** %s" string))))
1358 ;; Last active buffer, to print server messages in the right place
1360 (defvar erc-active-buffer nil
1361 "The current active buffer, the one where the user typed the last command.
1362 Defaults to the server buffer, and should only be set in the
1363 server buffer")
1364 (make-variable-buffer-local 'erc-active-buffer)
1366 (defun erc-active-buffer ()
1367 "Return the value of `erc-active-buffer' for the current server.
1368 Defaults to the server buffer."
1369 (with-current-buffer (erc-server-buffer) erc-active-buffer))
1371 (defun erc-set-active-buffer (buffer)
1372 "Set the value of `erc-active-buffer' to BUFFER."
1373 (cond ((erc-server-buffer)
1374 (with-current-buffer (erc-server-buffer)
1375 (setq erc-active-buffer buffer)))
1376 (t (setq erc-active-buffer buffer))))
1378 ;; Mode activation routines
1380 (defun erc-mode ()
1381 "Major mode for Emacs IRC.
1382 Special commands:
1384 \\{erc-mode-map}
1386 Turning on `erc-mode' runs the hook `erc-mode-hook'."
1387 (kill-all-local-variables)
1388 (use-local-map erc-mode-map)
1389 (setq mode-name "ERC"
1390 major-mode 'erc-mode
1391 local-abbrev-table erc-mode-abbrev-table)
1392 (set-syntax-table erc-mode-syntax-table)
1393 (when (boundp 'next-line-add-newlines)
1394 (set (make-local-variable 'next-line-add-newlines) nil))
1395 (setq line-move-ignore-invisible t)
1396 (set (make-local-variable 'paragraph-separate)
1397 (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
1398 (set (make-local-variable 'paragraph-start)
1399 (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
1400 ;; Run the mode hooks
1401 (run-hooks 'erc-mode-hook))
1403 ;; activation
1405 (defconst erc-default-server "irc.freenode.net"
1406 "IRC server to use if it cannot be detected otherwise.")
1408 (defconst erc-default-port "ircd"
1409 "IRC port to use if it cannot be detected otherwise.")
1411 (defcustom erc-join-buffer 'buffer
1412 "Determines how to display the newly created IRC buffer.
1413 'window - in another window,
1414 'window-noselect - in another window, but don't select that one,
1415 'frame - in another frame,
1416 'bury - bury it in a new buffer,
1417 any other value - in place of the current buffer"
1418 :group 'erc-buffers
1419 :type '(choice (const window)
1420 (const window-noselect)
1421 (const frame)
1422 (const bury)
1423 (const buffer)))
1425 (defcustom erc-frame-alist nil
1426 "*Alist of frame parameters for creating erc frames.
1427 A value of `nil means to use `default-frame-alist'."
1428 :group 'erc-buffers
1429 :type '(repeat (cons :format "%v"
1430 (symbol :tag "Parameter")
1431 (sexp :tag "Value"))))
1433 (defcustom erc-frame-dedicated-flag nil
1434 "*Non-nil means the erc frames are dedicated to that buffer.
1435 This only has effect when `erc-join-buffer' is set to `frame'."
1436 :group 'erc-buffers
1437 :type 'boolean)
1439 (defun erc-channel-p (channel)
1440 "Return non-nil if CHANNEL seems to be an IRC channel name."
1441 (cond ((stringp channel)
1442 (memq (aref channel 0) '(?# ?& ?+ ?!)))
1443 ((and (bufferp channel) (buffer-live-p channel))
1444 (with-current-buffer channel
1445 (erc-channel-p (erc-default-target))))
1446 (t nil)))
1448 (defcustom erc-reuse-buffers t
1449 "*If nil, create new buffers on joining a channel/query.
1450 If non-nil, a new buffer will only be created when you join
1451 channels with same names on different servers, or have query buffers
1452 open with nicks of the same name on different servers. Otherwise,
1453 the existing buffers will be reused."
1454 :group 'erc-buffers
1455 :type 'boolean)
1457 (defun erc-normalize-port (port)
1458 "Normalize the port specification PORT to integer form.
1459 PORT may be an integer, a string or a symbol. If it is a string or a
1460 symbol, it may have these values:
1461 * irc -> 194
1462 * ircs -> 994
1463 * ircd -> 6667
1464 * ircd-dalnet -> 7000"
1465 (cond
1466 ((symbolp port)
1467 (erc-normalize-port (symbol-name port)))
1468 ((stringp port)
1469 (let ((port-nr (string-to-number port)))
1470 (cond
1471 ((> port-nr 0)
1472 port-nr)
1473 ((string-equal port "irc")
1474 194)
1475 ((string-equal port "ircs")
1476 994)
1477 ((string-equal port "ircd")
1478 6667)
1479 ((string-equal port "ircd-dalnet")
1480 7000)
1482 nil))))
1483 ((numberp port)
1484 port)
1486 nil)))
1488 (defun erc-port-equal (a b)
1489 "Check whether ports A and B are equal."
1490 (= (erc-normalize-port a) (erc-normalize-port b)))
1492 (defun erc-generate-new-buffer-name (server port target &optional proc)
1493 "Create a new buffer name based on the arguments."
1494 (when (numberp port) (setq port (number-to-string port)))
1495 (let* ((buf-name (or target
1496 (or (let ((name (concat server ":" port)))
1497 (when (> (length name) 1)
1498 name))
1499 ; This fallback should in fact never happen
1500 "*erc-server-buffer*"))))
1501 ;; Reuse existing buffers, but not if the buffer is a connected server
1502 ;; buffer and not if its associated with a different server than the
1503 ;; current ERC buffer.
1504 (if (and erc-reuse-buffers
1505 (get-buffer buf-name)
1506 (or target
1507 (with-current-buffer (get-buffer buf-name)
1508 (and (erc-server-buffer-p)
1509 (not erc-server-connected))))
1510 (with-current-buffer (get-buffer buf-name)
1511 (and (string= erc-session-server server)
1512 (erc-port-equal erc-session-port port))))
1513 buf-name
1514 (generate-new-buffer-name buf-name))))
1516 (defun erc-get-buffer-create (server port target &optional proc)
1517 "Create a new buffer based on the arguments."
1518 (get-buffer-create (erc-generate-new-buffer-name server port target proc)))
1521 (defun erc-member-ignore-case (string list)
1522 "Return non-nil if STRING is a member of LIST.
1524 All strings are compared according to IRC protocol case rules, see
1525 `erc-downcase'."
1526 (setq string (erc-downcase string))
1527 (catch 'result
1528 (while list
1529 (if (string= string (erc-downcase (car list)))
1530 (throw 'result list) (setq list (cdr list))))))
1532 (defmacro erc-with-buffer (spec &rest body)
1533 "Execute BODY in the buffer associated with SPEC.
1535 SPEC should have the form
1537 (TARGET [PROCESS])
1539 If TARGET is a buffer, use it. Otherwise, use the buffer
1540 matching TARGET in the process specified by PROCESS.
1542 If PROCESS is nil, use the current `erc-server-process'
1543 See `erc-get-buffer' for details.
1545 See also `with-current-buffer'.
1547 \(fn (TARGET [PROCESS]) BODY...)"
1548 (let ((buf (erc-gensym))
1549 (proc (erc-gensym))
1550 (target (erc-gensym))
1551 (process (erc-gensym)))
1552 `(let* ((,target ,(car spec))
1553 (,process ,(cadr spec))
1554 (,buf (if (bufferp ,target)
1555 ,target
1556 (let ((,proc (or ,process
1557 (and (processp erc-server-process)
1558 erc-server-process))))
1559 (if (and ,target ,proc)
1560 (erc-get-buffer ,target ,proc))))))
1561 (when ,buf
1562 (with-current-buffer ,buf
1563 ,@body)))))
1564 (put 'erc-with-buffer 'lisp-indent-function 1)
1565 (put 'erc-with-buffer 'edebug-form-spec '((form &optional form) body))
1567 (defun erc-get-buffer (target &optional proc)
1568 "Return the buffer matching TARGET in the process PROC.
1569 If PROC is not supplied, all processes are searched."
1570 (let ((downcased-target (erc-downcase target)))
1571 (catch 'buffer
1572 (erc-buffer-filter
1573 (lambda ()
1574 (let ((current (erc-default-target)))
1575 (and (stringp current)
1576 (string-equal downcased-target (erc-downcase current))
1577 (throw 'buffer (current-buffer)))))
1578 proc))))
1580 (defun erc-buffer-filter (predicate &optional proc)
1581 "Return a list of `erc-mode' buffers matching certain criteria.
1582 PREDICATE is a function executed with each buffer, if it returns t, that buffer
1583 is considered a valid match.
1585 PROC is either an `erc-server-process', identifying a certain
1586 server connection, or nil which means all open connections."
1587 (save-excursion
1588 (delq
1590 (mapcar (lambda (buf)
1591 (with-current-buffer buf
1592 (and (eq major-mode 'erc-mode)
1593 (or (not proc)
1594 (eq proc erc-server-process))
1595 (funcall predicate)
1596 buf)))
1597 (buffer-list)))))
1599 (defun erc-buffer-list (&optional predicate proc)
1600 "Return a list of ERC buffers.
1601 PREDICATE is a function which executes with every buffer satisfying
1602 the predicate. If PREDICATE is passed as nil, return a list of all ERC
1603 buffers. If PROC is given, the buffers local variable `erc-server-process'
1604 needs to match PROC."
1605 (unless predicate
1606 (setq predicate (lambda () t)))
1607 (erc-buffer-filter predicate proc))
1609 (defmacro erc-with-all-buffers-of-server (process pred &rest forms)
1610 "Execute FORMS in all buffers which have same process as this server.
1611 FORMS will be evaluated in all buffers having the process PROCESS and
1612 where PRED matches or in all buffers of the server process if PRED is
1613 nil."
1614 ;; Make the evaluation have the correct order
1615 (let ((pre (erc-gensym))
1616 (pro (erc-gensym)))
1617 `(let ((,pro ,process)
1618 (,pre ,pred))
1619 (mapcar (lambda (buffer)
1620 (with-current-buffer buffer
1621 ,@forms))
1622 (erc-buffer-list ,pre
1623 ,pro)))))
1624 (put 'erc-with-all-buffers-of-server 'lisp-indent-function 1)
1625 (put 'erc-with-all-buffers-of-server 'edebug-form-spec '(form form body))
1627 (defun erc-iswitchb (&optional arg)
1628 "Use `iswitchb-read-buffer' to prompt for a ERC buffer to switch to.
1629 When invoked with prefix argument, use all erc buffers. Without prefix
1630 ARG, allow only buffers related to same session server.
1631 If `erc-track-mode' is in enabled, put the last element of
1632 `erc-modified-channels-alist' in front of the buffer list.
1634 Due to some yet unresolved reason, global function `iswitchb-mode'
1635 needs to be active for this function to work."
1636 (interactive "P")
1637 (eval-when-compile
1638 (require 'iswitchb))
1639 (let ((iswitchb-make-buflist-hook
1640 (lambda ()
1641 (setq iswitchb-temp-buflist
1642 (mapcar 'buffer-name
1643 (erc-buffer-list
1645 (when (and arg (boundp 'erc-server-process))
1646 erc-server-process)))))))
1647 (switch-to-buffer
1648 (iswitchb-read-buffer
1649 "Switch-to: "
1650 (if (boundp 'erc-modified-channels-alist)
1651 (buffer-name (caar (last erc-modified-channels-alist)))
1652 nil)
1653 t))))
1655 (defun erc-channel-list (proc)
1656 "Return a list of channel buffers.
1657 PROC is the process for the server connection. If PROC is nil, return
1658 all channel buffers on all servers."
1659 (erc-buffer-filter
1660 (lambda ()
1661 (and (erc-default-target)
1662 (erc-channel-p (erc-default-target))))
1663 proc))
1665 (defun erc-buffer-list-with-nick (nick proc)
1666 "Return buffers containing NICK in the `erc-channel-users' list."
1667 (with-current-buffer (process-buffer proc)
1668 (let ((user (gethash (erc-downcase nick) erc-server-users)))
1669 (if user
1670 (erc-server-user-buffers user)
1671 nil))))
1673 ;; Some local variables
1675 (defvar erc-default-recipients nil
1676 "List of default recipients of the current buffer.")
1677 (make-variable-buffer-local 'erc-default-recipients)
1679 (defvar erc-session-user-full-name nil
1680 "Full name of the user on the current server.")
1681 (make-variable-buffer-local 'erc-session-user-full-name)
1683 (defvar erc-channel-user-limit nil
1684 "Limit of users per channel.")
1685 (make-variable-buffer-local 'erc-channel-user-limit)
1687 (defvar erc-channel-key nil
1688 "Key needed to join channel.")
1689 (make-variable-buffer-local 'erc-channel-key)
1691 (defvar erc-invitation nil
1692 "Last invitation channel.")
1693 (make-variable-buffer-local 'erc-invitation)
1695 (defvar erc-away nil
1696 "Non-nil indicates that we are away.")
1697 (make-variable-buffer-local 'erc-away)
1699 (defvar erc-channel-list nil
1700 "Server channel list.")
1701 (make-variable-buffer-local 'erc-channel-list)
1703 (defvar erc-bad-nick nil
1704 "Non-nil indicates that we got a `nick in use' error while connecting.")
1705 (make-variable-buffer-local 'erc-bad-nick)
1707 (defvar erc-logged-in nil
1708 "Non-nil indicates that we are logged in.")
1709 (make-variable-buffer-local 'erc-logged-in)
1711 (defvar erc-default-nicks nil
1712 "The local copy of `erc-nick' - the list of nicks to choose from.")
1713 (make-variable-buffer-local 'erc-default-nicks)
1715 (defvar erc-nick-change-attempt-count 0
1716 "Used to keep track of how many times an attempt at changing nick is made.")
1717 (make-variable-buffer-local 'erc-nick-change-attempt-count)
1719 (defun erc-migrate-modules (mods)
1720 "Migrate old names of ERC modules to new ones."
1721 ;; modify `transforms' to specify what needs to be changed
1722 ;; each item is in the format '(old . new)
1723 (let ((transforms '((pcomplete . completion))))
1724 (erc-delete-dups
1725 (mapcar (lambda (m) (or (cdr (assoc m transforms)) m))
1726 mods))))
1728 (defcustom erc-modules '(netsplit fill button match track completion readonly
1729 ring autojoin noncommands irccontrols
1730 stamp list)
1731 "A list of modules which erc should enable.
1732 If you set the value of this without using `customize' remember to call
1733 \(erc-update-modules) after you change it. When using `customize', modules
1734 removed from the list will be disabled."
1735 :get (lambda (sym)
1736 ;; replace outdated names with their newer equivalents
1737 (erc-migrate-modules (symbol-value sym)))
1738 :set (lambda (sym val)
1739 ;; disable modules which have just been removed
1740 (when (and (boundp 'erc-modules) erc-modules val)
1741 (dolist (module erc-modules)
1742 (unless (member module val)
1743 (let ((f (intern-soft (format "erc-%s-mode" module))))
1744 (when (and (fboundp f) (boundp f) (symbol-value f))
1745 (message "Disabling `erc-%s'" module)
1746 (funcall f 0))))))
1747 (set sym val)
1748 ;; this test is for the case where erc hasn't been loaded yet
1749 (when (fboundp 'erc-update-modules)
1750 (erc-update-modules)))
1751 :type
1752 '(set
1753 :greedy t
1754 (const :tag "Set away status automatically" autoaway)
1755 (const :tag "Join channels automatically" autojoin)
1756 (const :tag "Integrate with Big Brother Database" bbdb)
1757 (const :tag "Buttonize URLs, nicknames, and other text" button)
1758 (const :tag "Wrap long lines" fill)
1759 (const :tag "Highlight or remove IRC control characters"
1760 irccontrols)
1761 (const :tag "Save buffers in logs" log)
1762 (const :tag "Highlight pals, fools, and other keywords" match)
1763 (const :tag "Detect netsplits" netsplit)
1764 (const :tag "Don't display non-IRC commands after evaluation"
1765 noncommands)
1766 (const :tag
1767 "Notify when the online status of certain users changes"
1768 notify)
1769 (const :tag "Complete nicknames and commands (programmable)"
1770 completion)
1771 (const :tag "Complete nicknames and commands (old)" hecomplete)
1772 (const :tag "Make displayed lines read-only" readonly)
1773 (const :tag "Replace text in messages" replace)
1774 (const :tag "Enable an input history" ring)
1775 (const :tag "Scroll to the bottom of the buffer" scrolltobottom)
1776 (const :tag "Identify to Nickserv (IRC Services) automatically"
1777 services)
1778 (const :tag "Convert smileys to pretty icons" smiley)
1779 (const :tag "Play sounds when you receive CTCP SOUND requests"
1780 sound)
1781 (const :tag "Add timestamps to messages" stamp)
1782 (const :tag "Check spelling" spelling)
1783 (const :tag "Track channel activity in the mode-line" track)
1784 (const :tag "Truncate buffers to a certain size" truncate)
1785 (const :tag "Translate morse code in messages" unmorse)
1786 (repeat :tag "Others" :inline t symbol))
1787 :group 'erc)
1789 (defun erc-update-modules ()
1790 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1791 (let (req)
1792 (dolist (mod erc-modules)
1793 (setq req (concat "erc-" (symbol-name mod)))
1794 (cond
1795 ;; yuck. perhaps we should bring the filenames into sync?
1796 ((string= req "erc-completion")
1797 (setq req "erc-pcomplete"))
1798 ((string= req "erc-pcomplete")
1799 (setq mod 'completion))
1800 ((string= req "erc-autojoin")
1801 (setq req "erc-join")))
1802 (condition-case nil
1803 (require (intern req))
1804 (error nil))
1805 (funcall (or (intern-soft (concat "erc-" (symbol-name mod) "-mode"))
1806 (error "`%s' is not a known ERC module" mod))
1807 1))))
1809 (defun erc-setup-buffer (buffer)
1810 "Consults `erc-join-buffer' to find out how to display `BUFFER'."
1811 (cond ((eq erc-join-buffer 'window)
1812 (if (active-minibuffer-window)
1813 (display-buffer buffer)
1814 (switch-to-buffer-other-window buffer)))
1815 ((eq erc-join-buffer 'window-noselect)
1816 (display-buffer buffer))
1817 ((eq erc-join-buffer 'bury)
1818 nil)
1819 ((eq erc-join-buffer 'frame)
1820 (funcall '(lambda (frame)
1821 (raise-frame frame)
1822 (select-frame frame))
1823 (make-frame (or erc-frame-alist
1824 default-frame-alist)))
1825 (switch-to-buffer buffer)
1826 (when erc-frame-dedicated-flag
1827 (set-window-dedicated-p (selected-window) t)))
1829 (if (active-minibuffer-window)
1830 (display-buffer buffer)
1831 (switch-to-buffer buffer)))))
1833 (defun erc (&optional server port nick full-name
1834 connect passwd tgt-list channel process)
1835 "ERC is a powerful, modular, and extensible IRC client.
1837 Connect to SERVER on PORT as NICK with FULL-NAME.
1839 If CONNECT is non-nil, connect to the server. Otherwise assume
1840 already connected and just create a separate buffer for the new
1841 target CHANNEL.
1843 Use PASSWD as user password on the server. If TGT-LIST is
1844 non-nil, use it to initialise `erc-default-recipients'.
1846 Returns the buffer for the given server or channel."
1847 (let ((server-announced-name (when (and (boundp 'erc-session-server)
1848 (string= server erc-session-server))
1849 erc-server-announced-name))
1850 (connected-p (unless connect erc-server-connected))
1851 (buffer (erc-get-buffer-create server port channel))
1852 (old-buffer (current-buffer))
1853 continued-session)
1854 (erc-update-modules)
1855 (set-buffer buffer)
1856 (erc-mode)
1857 (setq erc-server-announced-name server-announced-name)
1858 (setq erc-server-connected connected-p)
1859 ;; connection parameters
1860 (setq erc-server-process process)
1861 (setq erc-insert-marker (make-marker))
1862 (setq erc-input-marker (make-marker))
1863 ;; go to the end of the buffer and open a new line
1864 ;; (the buffer may have existed)
1865 (goto-char (point-max))
1866 (forward-line 0)
1867 (when (get-text-property (point) 'erc-prompt)
1868 (setq continued-session t)
1869 (set-marker erc-input-marker
1870 (or (next-single-property-change (point) 'erc-prompt)
1871 (point-max))))
1872 (unless continued-session
1873 (goto-char (point-max))
1874 (insert "\n"))
1875 (set-marker erc-insert-marker (point))
1876 ;; stack of default recipients
1877 (setq erc-default-recipients tgt-list)
1878 (setq erc-server-current-nick nil)
1879 ;; Initialize erc-server-users and erc-channel-users
1880 (if connect
1881 (progn ;; server buffer
1882 (setq erc-server-users
1883 (make-hash-table :test 'equal))
1884 (setq erc-channel-users nil))
1885 (progn ;; target buffer
1886 (setq erc-server-users nil)
1887 (setq erc-channel-users
1888 (make-hash-table :test 'equal))))
1889 ;; clear last incomplete line read
1890 (setq erc-server-filter-data nil)
1891 (setq erc-channel-topic "")
1892 ;; limit on the number of users on the channel (mode +l)
1893 (setq erc-channel-user-limit nil)
1894 (setq erc-channel-key nil)
1895 ;; last active buffer, defaults to this one
1896 (erc-set-active-buffer buffer)
1897 ;; last invitation channel
1898 (setq erc-invitation nil)
1899 ;; away flag
1900 ;; Should only be used in session-buffers
1901 (setq erc-away (let ((serverbuf (erc-server-buffer)))
1902 (and serverbuf (with-current-buffer serverbuf erc-away))))
1903 ;; Server channel list
1904 (setq erc-channel-list ())
1905 ;; login-time 'nick in use' error
1906 (setq erc-bad-nick nil)
1907 ;; whether we have logged in
1908 (setq erc-logged-in nil)
1909 ;; The local copy of `erc-nick' - the list of nicks to choose
1910 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
1911 ;; password stuff
1912 (setq erc-session-password passwd)
1913 ;; debug output buffer
1914 (setq erc-dbuf
1915 (when erc-log-p
1916 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
1917 (erc-determine-parameters server port nick full-name)
1919 ;; Saving log file on exit
1920 (run-hooks 'erc-connect-pre-hook)
1922 (when connect
1923 (erc-server-connect erc-session-server erc-session-port))
1924 (erc-update-mode-line)
1925 (set-marker erc-insert-marker (point))
1926 (unless continued-session
1927 (goto-char (point-max))
1928 (insert "\n"))
1929 (set-marker (process-mark erc-server-process) (point))
1930 (if continued-session
1931 (goto-char (point-max))
1932 (set-marker erc-insert-marker (point))
1933 (erc-display-prompt)
1934 (goto-char (point-max)))
1936 ;; Now display the buffer in a window as per user wishes.
1937 (unless (eq buffer old-buffer)
1938 (when erc-log-p
1939 ;; we can't log to debug buffer, it may not exist yet
1940 (message "erc: old buffer %s, switching to %s"
1941 old-buffer buffer))
1942 (erc-setup-buffer buffer))
1944 buffer))
1946 (defun erc-initialize-log-marker ()
1947 "Initialize the `erc-last-saved-position' marker to a sensible position."
1948 (setq erc-last-saved-position (make-marker))
1949 (move-marker erc-last-saved-position
1950 (1- (marker-position erc-insert-marker))))
1952 ;; interactive startup
1954 (defvar erc-server-history-list nil
1955 "IRC server interactive selection history list.")
1957 (defvar erc-nick-history-list nil
1958 "Nickname interactive selection history list.")
1960 (defun erc-already-logged-in (server port nick)
1961 "Return the buffers corresponding to a NICK on PORT of a session SERVER.
1962 This is determined by looking for the appropriate buffer and checking
1963 whether the connection is still alive.
1964 If no buffer matches, return nil."
1965 (erc-buffer-list
1966 (lambda ()
1967 (and (erc-server-process-alive)
1968 (string= erc-session-server server)
1969 (erc-port-equal erc-session-port port)
1970 (erc-current-nick-p nick)))))
1972 (if (not (fboundp 'read-passwd))
1973 (defun read-passwd (prompt)
1974 "Substitute for read-passwd in early emacsen"
1975 (read-from-minibuffer prompt)))
1977 (defcustom erc-before-connect nil
1978 "Hook called before connecting to a server.
1979 This hook gets executed before `erc-select' actually invokes `erc-mode'
1980 with your input data. The functions in here get called with three
1981 parameters, SERVER, PORT and NICK."
1982 :group 'erc-hooks
1983 :type 'hook)
1985 (defcustom erc-after-connect nil
1986 "Hook called after connecting to a server.
1987 This hook gets executed when an end of MOTD has been received. All
1988 functions in here get called with the parameters SERVER and NICK."
1989 :group 'erc-hooks
1990 :type 'hook)
1992 ;;;###autoload
1993 (defun erc-select-read-args ()
1994 "Prompt the user for values of nick, server, port, and password."
1995 (let (user-input server port nick passwd)
1996 (setq user-input (read-from-minibuffer
1997 "IRC server: "
1998 (erc-compute-server) nil nil 'erc-server-history-list))
2000 (if (string-match "\\(.*\\):\\(.*\\)\\'" user-input)
2001 (setq port (erc-string-to-port (match-string 2 user-input))
2002 user-input (match-string 1 user-input))
2003 (setq port
2004 (erc-string-to-port (read-from-minibuffer
2005 "IRC port: " (erc-port-to-string
2006 (erc-compute-port))))))
2008 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
2009 (setq nick (match-string 1 user-input)
2010 user-input (match-string 2 user-input))
2011 (setq nick
2012 (if (erc-already-logged-in server port nick)
2013 (read-from-minibuffer
2014 (erc-format-message 'nick-in-use ?n nick)
2015 nick
2016 nil nil 'erc-nick-history-list)
2017 (read-from-minibuffer
2018 "Nickname: " (erc-compute-nick nick)
2019 nil nil 'erc-nick-history-list))))
2021 (setq server user-input)
2023 (setq passwd (if erc-prompt-for-password
2024 (if (and erc-password
2025 (y-or-n-p "Use the default password? "))
2026 erc-password
2027 (read-passwd "Password: "))
2028 erc-password))
2029 (when (and passwd (string= "" passwd))
2030 (setq passwd nil))
2032 (while (erc-already-logged-in server port nick)
2033 ;; hmm, this is a problem when using multiple connections to a bnc
2034 ;; with the same nick. Currently this code prevents using more than one
2035 ;; bnc with the same nick. actually it would be nice to have
2036 ;; bncs transparent, so that erc-compute-buffer-name displays
2037 ;; the server one is connected to.
2038 (setq nick (read-from-minibuffer
2039 (erc-format-message 'nick-in-use ?n nick)
2040 nick
2041 nil nil 'erc-nick-history-list)))
2042 (list :server server :port port :nick nick :password passwd)))
2044 ;;;###autoload
2045 (defun* erc-select (&key (server (erc-compute-server))
2046 (port (erc-compute-port))
2047 (nick (erc-compute-nick))
2048 password
2049 (full-name (erc-compute-full-name)))
2050 "Select connection parameters and run ERC.
2051 Non-interactively, it takes keyword arguments
2052 (server (erc-compute-server))
2053 (port (erc-compute-port))
2054 (nick (erc-compute-nick))
2055 password
2056 (full-name (erc-compute-full-name)))
2058 That is, if called with
2059 (erc-select :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
2060 server and full-name will be set to those values, whereas
2061 erc-compute-port, erc-compute-nick and erc-compute-full-name will
2062 be invoked for those parameters' values"
2063 (interactive (erc-select-read-args))
2065 (run-hook-with-args 'erc-before-connect server port nick)
2066 (erc server port nick erc-user-full-name t password))
2069 (defun erc-select-ssl (&rest r)
2070 "Interactively select SSL connection parameters and run ERC.
2071 Arguments are as to erc-select."
2072 (interactive (erc-select-read-args))
2073 (let ((erc-server-connect-function 'erc-open-ssl-stream))
2074 (apply 'erc-select r)))
2076 (defun erc-open-ssl-stream (name buffer host port)
2077 "Open an SSL stream to an IRC server.
2078 The process will be given the name NAME, its target buffer will be
2079 BUFFER. HOST and PORT specify the connection target."
2080 (when (require 'tls)
2081 (let ((proc (open-tls-stream name buffer host port)))
2082 ;; Ugly hack, but it works for now. Problem is it is
2083 ;; very hard to detect when ssl is established, because s_client
2084 ;; doesn't give any CONNECTIONESTABLISHED kind of message, and
2085 ;; most IRC servers send nothing and wait for you to identify.
2086 ;; Disabled when switching to tls.el -- jas
2087 ;(sit-for 5)
2088 proc)))
2090 ;;; Debugging the protocol
2092 (defvar erc-debug-irc-protocol nil
2093 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2095 The buffer is created if it doesn't exist.
2097 NOTE: If this variable is non-nil, and you kill the the only
2098 visible \"*erc-protocol*\" buffer, it will be recreated shortly,
2099 but you won't see it.
2101 WARNING: Do not set this variable directly! Instead, use the
2102 function `erc-toggle-debug-irc-protocol' to toggle its value.")
2104 (defun erc-log-irc-protocol (string &optional outbound)
2105 "Append STRING to the buffer *erc-protocol*.
2107 This only has any effect if `erc-debug-irc-protocol' is non-nil.
2109 The buffer is created if it doesn't exist.
2111 If OUTBOUND is non-nil, STRING is being sent to the IRC server
2112 and appears in erc-input-face in the buffer."
2113 (when erc-debug-irc-protocol
2114 (let ((network-name (or (ignore-errors (erc-network-name))
2115 "???")))
2116 (with-current-buffer (get-buffer-create "*erc-protocol*")
2117 (save-excursion
2118 (goto-char (point-max))
2119 (let ((inhibit-read-only t))
2120 (insert (if (not outbound)
2121 ;; Cope with the fact that string might
2122 ;; contain multiple lines of text.
2123 (let ((lines (delete "" (split-string string
2124 "\n\\|\r\n")))
2125 (result ""))
2126 (dolist (line lines)
2127 (setq result (concat result network-name
2128 " << " line "\n")))
2129 result)
2130 (erc-propertize
2131 (concat network-name " >> " string
2132 (if (/= ?\n
2133 (aref string
2134 (1- (length string))))
2135 "\n"))
2136 'face 'erc-input-face)))))
2137 (let ((orig-win (selected-window))
2138 (debug-buffer-window (get-buffer-window (current-buffer) t)))
2139 (when debug-buffer-window
2140 (select-window debug-buffer-window)
2141 (when (= 1 (count-lines (point) (point-max)))
2142 (goto-char (point-max))
2143 (recenter -1))
2144 (select-window orig-win)))))))
2146 (defun erc-toggle-debug-irc-protocol (&optional arg)
2147 "Toggle the value of `erc-debug-irc-protocol'.
2149 If ARG is non-nil, show the *erc-protocol* buffer."
2150 (interactive "P")
2151 (let* ((buf (get-buffer-create "*erc-protocol*")))
2152 (with-current-buffer buf
2153 (erc-view-mode-enter 1)
2154 (when (null (current-local-map))
2155 (let ((inhibit-read-only t))
2156 (insert (erc-make-notice "This buffer displays all IRC protocol traffic exchanged with each server.\n"))
2157 (insert (erc-make-notice "Kill this buffer to terminate protocol logging.\n\n")))
2158 (use-local-map (make-sparse-keymap))
2159 (local-set-key (kbd "RET") 'erc-toggle-debug-irc-protocol))
2160 (add-hook 'kill-buffer-hook
2161 #'(lambda () (setq erc-debug-irc-protocol nil))
2162 nil 'local)
2163 (goto-char (point-max))
2164 (let ((inhibit-read-only t))
2165 (insert (erc-make-notice
2166 (format "IRC protocol logging %s at %s -- Press ENTER to toggle logging.\n"
2167 (if erc-debug-irc-protocol "disabled" "enabled")
2168 (current-time-string))))))
2169 (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
2170 (if (and arg
2171 (not (get-buffer-window "*erc-protocol*" t)))
2172 (display-buffer buf t))
2173 (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
2174 (if erc-debug-irc-protocol "enabled" "disabled"))))
2176 ;;; I/O interface
2178 ;; send interface
2180 (defun erc-send-action (tgt str &optional force)
2181 "Send CTCP ACTION information described by STR to TGT."
2182 (erc-send-ctcp-message tgt (format "ACTION %s" str) force)
2183 (erc-display-message
2184 nil 'input (current-buffer)
2185 'ACTION ?n (erc-current-nick) ?a str ?u "" ?h ""))
2187 ;; Display interface
2189 (defun erc-string-invisible-p (string)
2190 "Check whether STRING is invisible or not.
2191 I.e. any char in it has the `invisible' property set."
2192 (text-property-any 0 (length string) 'invisible t string))
2194 (defun erc-display-line-1 (string buffer)
2195 "Display STRING in `erc-mode' BUFFER.
2196 Auxiliary function used in `erc-display-line'. The line gets filtered to
2197 interpret the control characters. Then, `erc-insert-pre-hook' gets called.
2198 If `erc-insert-this' is still t, STRING gets inserted into the buffer.
2199 Afterwards, `erc-insert-modify' and `erc-insert-post-hook' get called.
2200 If STRING is nil, the function does nothing."
2201 (when string
2202 (save-excursion
2203 (set-buffer (or buffer (process-buffer erc-server-process)))
2204 (let ((insert-position (or (marker-position erc-insert-marker)
2205 (point-max))))
2206 (let ((string string) ;; FIXME! Can this be removed?
2207 (buffer-undo-list t)
2208 (inhibit-read-only t))
2209 (unless (string-match "\n$" string)
2210 (setq string (concat string "\n"))
2211 (when (erc-string-invisible-p string)
2212 (erc-put-text-properties 0 (length string) string
2213 '(invisible intangible))))
2214 (erc-log (concat "erc-display-line: " string
2215 (format "(%S)" string) " in buffer "
2216 (format "%s" buffer)))
2217 (setq erc-insert-this t)
2218 (run-hook-with-args 'erc-insert-pre-hook string)
2219 (if (null erc-insert-this)
2220 ;; Leave erc-insert-this set to t as much as possible. Fran
2221 ;; Litterio <franl> has seen erc-insert-this set to nil while
2222 ;; erc-send-pre-hook is running, which should never happen. This
2223 ;; may cure it.
2224 (setq erc-insert-this t)
2225 (save-excursion ;; to restore point in the new buffer
2226 (save-restriction
2227 (widen)
2228 (goto-char insert-position)
2229 (insert-before-markers string)
2230 ;; run insertion hook, with point at restored location
2231 (save-restriction
2232 (narrow-to-region insert-position (point))
2233 (run-hooks 'erc-insert-modify-hook)
2234 (run-hooks 'erc-insert-post-hook))))))
2235 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2236 (point-max))
2237 insert-position))))))
2239 (defun erc-update-undo-list (shift)
2240 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2241 (unless (or (zerop shift) (atom buffer-undo-list))
2242 (let ((list buffer-undo-list) elt)
2243 (while list
2244 (setq elt (car list))
2245 (cond ((integerp elt) ; POSITION
2246 (incf (car list) shift))
2247 ((or (atom elt) ; nil, EXTENT
2248 ;; (eq t (car elt)) ; (t HIGH . LOW)
2249 (markerp (car elt))) ; (MARKER . DISTANCE)
2250 nil)
2251 ((integerp (car elt)) ; (BEGIN . END)
2252 (incf (car elt) shift)
2253 (incf (cdr elt) shift))
2254 ((stringp (car elt)) ; (TEXT . POSITION)
2255 (incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
2256 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2257 (let ((cons (nthcdr 3 elt)))
2258 (incf (car cons) shift)
2259 (incf (cdr cons) shift)))
2260 ((and (featurep 'xemacs)
2261 (extentp (car elt))) ; (EXTENT START END)
2262 (incf (nth 1 elt) shift)
2263 (incf (nth 2 elt) shift)))
2264 (setq list (cdr list))))))
2266 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2267 "Regexp which matches all legal characters in a IRC nickname.")
2269 (defun erc-is-valid-nick-p (nick)
2270 "Check if NICK is a valid IRC nickname."
2271 (string-match (concat "^" erc-valid-nick-regexp "$") nick))
2273 (defun erc-display-line (string &optional buffer)
2274 "Display STRING in the ERC BUFFER.
2275 All screen output must be done through this function. If BUFFER is nil
2276 or omitted, the default ERC buffer for the `erc-session-server' is used.
2277 The BUFFER can be an actual buffer, a list of buffers, 'all or 'active.
2278 If BUFFER = 'all, the string is displayed in all the ERC buffers for the
2279 current session. 'active means the current active buffer
2280 \(`erc-active-buffer'). If the buffer can't be resolved, the current
2281 buffer is used. `erc-display-line-1' is used to display STRING.
2283 If STRING is nil, the function does nothing."
2284 (let ((inhibit-point-motion-hooks t)
2285 new-bufs)
2286 (dolist (buf (cond
2287 ((bufferp buffer) (list buffer))
2288 ((listp buffer) buffer)
2289 ((processp buffer) (list (process-buffer buffer)))
2290 ((eq 'all buffer)
2291 (and (boundp 'erc-server-process)
2292 ;; Hmm, or all of the same session server?
2293 (erc-buffer-list nil erc-server-process)))
2294 ((and (eq 'active buffer) (erc-active-buffer))
2295 (list (erc-active-buffer)))
2296 ((erc-server-buffer-live-p)
2297 (list (process-buffer erc-server-process)))
2298 (t (list (current-buffer)))))
2299 (when (buffer-live-p buf)
2300 (erc-display-line-1 string buf)
2301 (add-to-list 'new-bufs buf)))
2302 (when (null new-bufs)
2303 (if (erc-server-buffer-live-p)
2304 (erc-display-line-1 string (process-buffer erc-server-process))
2305 (erc-display-line-1 string (current-buffer))))))
2307 (defun erc-display-message-highlight (type string)
2308 "Highlight STRING according to TYPE, where erc-TYPE-face is an erc face.
2310 See also `erc-make-notice'"
2311 (cond ((eq type 'notice)
2312 (erc-make-notice string))
2314 (erc-put-text-property
2315 0 (length string)
2316 'face (or (intern-soft
2317 (concat "erc-" (symbol-name type) "-face"))
2318 "erc-default-face")
2319 string)
2320 string)))
2322 (defun erc-display-message (parsed type buffer msg &rest args)
2323 "Display MSG in BUFFER.
2325 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2327 See also `erc-format-message' and `erc-display-line'."
2328 (let ((string (if (symbolp msg)
2329 (apply 'erc-format-message msg args)
2330 msg)))
2331 (setq string
2332 (cond
2333 ((listp type)
2334 (mapc (lambda (type)
2335 (setq string
2336 (erc-display-message-highlight type string)))
2337 type)
2338 string)
2339 ((symbolp type)
2340 (erc-display-message-highlight type string))))
2342 (if (not (erc-response-p parsed))
2343 (erc-display-line string buffer)
2344 (unless (member (erc-response.command parsed) erc-hide-list)
2345 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
2346 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2347 (erc-display-line string buffer)))))
2349 (defun erc-message-type-member (position list)
2350 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2352 This function relies on the erc-parsed text-property being
2353 present."
2354 (let ((prop-val (get-text-property position 'erc-parsed)))
2355 (and prop-val (member (erc-response.command prop-val) list))))
2357 (defvar erc-send-input-line-function 'erc-send-input-line)
2358 (make-variable-buffer-local 'erc-send-input-line-function)
2360 (defun erc-send-input-line (target line &optional force)
2361 "Send LINE to TARGET.
2363 See also `erc-server-send'."
2364 (setq line (format "PRIVMSG %s :%s"
2365 target
2366 ;; If the line is empty, we still want to
2367 ;; send it - i.e. an empty pasted line.
2368 (if (string= line "\n")
2369 " \n"
2370 line)))
2371 (erc-server-send line force target))
2373 (defun erc-get-arglist (fun)
2374 "Return the argument list of a function without the parens."
2375 (let ((arglist (format "%S" (erc-function-arglist fun))))
2376 (if (string-match "^(\\(.*\\))$" arglist)
2377 (match-string 1 arglist)
2378 arglist)))
2380 (defun erc-command-name (cmd)
2381 "For CMD being the function name of a ERC command, something like
2382 erc-cmd-FOO, this returns a string /FOO."
2383 (let ((command-name (symbol-name cmd)))
2384 (if (string-match "^erc-cmd-\\(.*\\)$" command-name)
2385 (concat "/" (match-string 1 command-name))
2386 command-name)))
2388 (defun erc-process-input-line (line &optional force no-command)
2389 "Translate LINE to an RFC1459 command and send it based.
2390 Returns non-nil if the command is actually sent to the server, and nil
2391 otherwise.
2393 If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
2394 it is passed to `erc-cmd-default'. If LINE is not a command (ie. doesn't
2395 start with /<COMMAND>) then it is sent as a message.
2397 An optional FORCE argument forces sending the line when flood
2398 protection is in effect. The optional NO-COMMAND argument prohibits
2399 this function from interpreting the line as a command."
2400 (let ((command-list (erc-extract-command-from-line line)))
2401 (if (and command-list
2402 (not no-command))
2403 (let* ((cmd (nth 0 command-list))
2404 (args (nth 1 command-list)))
2405 (condition-case nil
2406 (if (listp args)
2407 (apply cmd args)
2408 (funcall cmd args))
2409 (wrong-number-of-arguments
2410 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2411 ?c (erc-command-name cmd)
2412 ?u (or (erc-get-arglist cmd)
2414 ?d (format "%s\n"
2415 (or (documentation cmd) "")))
2416 nil)))
2417 (let ((r (erc-default-target)))
2418 (if r
2419 (funcall erc-send-input-line-function r line force)
2420 (erc-display-message nil 'error (current-buffer) 'no-target)
2421 nil)))))
2423 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2424 ;; Input commands handlers
2425 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2427 (defun erc-cmd-AMSG (line)
2428 "Send LINE to all channels of the current server that you are on."
2429 (interactive "sSend to all channels you're on: ")
2430 (setq line (erc-trim-string line))
2431 (erc-with-all-buffers-of-server nil
2432 (lambda ()
2433 (erc-channel-p (erc-default-target)))
2434 (erc-send-message line)))
2435 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2437 (defun erc-cmd-SAY (line)
2438 "Send LINE to the current query or channel as a message, not a command.
2440 Use this when you want to send a message with a leading '/'. Note
2441 that since multi-line messages are never a command, you don't
2442 need this when pasting multiple lines of text."
2443 (if (string-match "^\\s-*$" line)
2445 (string-match "^ ?\\(.*\\)" line)
2446 (erc-process-input-line (match-string 1 line) nil t)))
2447 (put 'erc-cmd-SAY 'do-not-parse-args t)
2449 (defun erc-cmd-SET (line)
2450 "Set the variable named by the first word in LINE to some VALUE.
2451 VALUE is computed by evaluating the rest of LINE in Lisp."
2452 (cond
2453 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2454 (let ((var (read (concat "erc-" (match-string 1 line))))
2455 (val (read (match-string 2 line))))
2456 (if (boundp var)
2457 (progn
2458 (set var (eval val))
2459 (erc-display-message
2460 nil nil 'active (format "Set %S to %S" var val))
2462 (setq var (read (match-string 1 line)))
2463 (if (boundp var)
2464 (progn
2465 (set var (eval val))
2466 (erc-display-message
2467 nil nil 'active (format "Set %S to %S" var val))
2469 (erc-display-message nil 'error 'active 'variable-not-bound)
2470 nil))))
2471 ((string-match "^\\s-*$" line)
2472 (erc-display-line
2473 (concat "Available user variables:\n"
2474 (apply
2475 'concat
2476 (mapcar
2477 (lambda (var)
2478 (let ((val (symbol-value var)))
2479 (concat (format "%S:" var)
2480 (if (consp val)
2481 (concat "\n" (pp-to-string val))
2482 (format " %S\n" val)))))
2483 (apropos-internal "^erc-" 'user-variable-p))))
2484 (current-buffer)) t)
2485 (t nil)))
2486 (defalias 'erc-cmd-VAR 'erc-cmd-SET)
2487 (defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2488 (put 'erc-cmd-SET 'do-not-parse-args t)
2490 (defun erc-cmd-default (line)
2491 "Fallback command.
2493 Commands for which no erc-cmd-xxx exists, are tunnelled through
2494 this function. LINE is sent to the server verbatim, and
2495 therefore has to contain the command itself as well."
2496 (erc-log (format "cmd: DEFAULT: %s" line))
2497 (erc-server-send (substring line 1))
2500 (defun erc-cmd-IGNORE (&optional user)
2501 "Ignore USER. This should be a regexp matching nick!user@host.
2502 If no USER argument is specified, list the contents of `erc-ignore-list'."
2503 (if user
2504 (progn
2505 (erc-display-line
2506 (erc-make-notice (format "Now ignoring %s" user))
2507 'active)
2508 (with-current-buffer (erc-server-buffer)
2509 (add-to-list 'erc-ignore-list user)))
2510 (if (null (with-current-buffer (erc-server-buffer) erc-ignore-list))
2511 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2512 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2513 (mapc #'(lambda (item)
2514 (erc-display-line (erc-make-notice item)
2515 'active))
2516 (with-current-buffer (erc-server-buffer) erc-ignore-list))))
2519 (defun erc-cmd-UNIGNORE (user)
2520 "Remove the user specified in USER from the ignore list."
2521 (let ((ignored-nick (car (with-current-buffer (erc-server-buffer)
2522 (erc-member-ignore-case user erc-ignore-list)))))
2523 (if (null ignored-nick)
2524 (erc-display-line
2525 (erc-make-notice (format "%s is not currently ignored!" user))
2526 'active)
2527 (erc-display-line
2528 (erc-make-notice (format "No longer ignoring %s" user))
2529 'active))
2530 (with-current-buffer (erc-server-buffer)
2531 (setq erc-ignore-list (delete ignored-nick erc-ignore-list))))
2534 (defun erc-cmd-CLEAR ()
2535 "Clear the window content."
2536 (recenter 0)
2539 (defun erc-cmd-OPS ()
2540 "Show the ops in the current channel."
2541 (interactive)
2542 (let ((ops nil))
2543 (if erc-channel-users
2544 (maphash (lambda (nick user-data)
2545 (let ((cuser (cdr user-data)))
2546 (if (and cuser
2547 (erc-channel-user-op cuser))
2548 (setq ops (cons (erc-server-user-nickname
2549 (car user-data))
2550 ops)))))
2551 erc-channel-users))
2552 (setq ops (sort ops 'string-lessp))
2553 (if ops
2554 (erc-display-message
2555 nil 'notice (current-buffer) 'ops
2556 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2557 ?o (mapconcat 'identity ops " "))
2558 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2561 (defun erc-cmd-COUNTRY (tld)
2562 "Display the country associated with the top level domain TLD."
2563 (require 'mail-extr)
2564 (let ((co (ignore-errors (what-domain tld))))
2565 (if co
2566 (erc-display-message
2567 nil 'notice 'active 'country ?c co ?d tld)
2568 (erc-display-message
2569 nil 'notice 'active 'country-unknown ?d tld))
2572 (defun erc-cmd-AWAY (line)
2573 "Mark the user as being away, the reason being indicated by LINE.
2574 If no reason is given, unset away status."
2575 (when (string-match "^\\s-*\\(.*\\)$" line)
2576 (let ((reason (match-string 1 line)))
2577 (erc-log (format "cmd: AWAY: %s" reason))
2578 (erc-server-send
2579 (if (string= reason "")
2580 "AWAY"
2581 (concat "AWAY :" reason))))
2583 (put 'erc-cmd-AWAY 'do-not-parse-args t)
2585 (defun erc-cmd-GAWAY (line)
2586 "Mark the user as being away everywhere, the reason being indicated by LINE."
2587 ;; on all server buffers.
2588 (erc-with-all-buffers-of-server nil
2589 #'erc-server-buffer-p
2590 (erc-cmd-AWAY line)))
2591 (put 'erc-cmd-GAWAY 'do-not-parse-args t)
2593 (defun erc-cmd-CTCP (nick cmd &rest args)
2594 "Send a Client To Client Protocol message to NICK.
2596 CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2597 VERSION and so on. It is called with ARGS."
2598 (let ((str (concat cmd
2599 (when args
2600 (concat " " (mapconcat #'identity args " "))))))
2601 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2602 (erc-send-ctcp-message nick str)
2605 (defun erc-cmd-HELP (&optional func)
2606 "Popup help information.
2608 If FUNC contains a valid function or variable, help about that
2609 will be displayed. If FUNC is empty, display an apropos about
2610 erc commands. Otherwise, do apropos in the erc namespace
2611 \(\"erc-.*LINE\"\).
2613 Examples:
2614 To find out about erc and bbdb, do
2615 /help bbdb.*
2617 For help about the WHOIS command, do:
2618 /help whois
2620 For a list of user commands (/join /part, ...):
2621 /help."
2622 (if func
2623 (let* ((sym (or (let ((sym (intern-soft
2624 (concat "erc-cmd-" (upcase func)))))
2625 (if (and sym (or (boundp sym) (fboundp sym)))
2627 nil))
2628 (let ((sym (intern-soft func)))
2629 (if (and sym (or (boundp sym) (fboundp sym)))
2631 nil))
2632 (let ((sym (intern-soft (concat "erc-" func))))
2633 (if (and sym (or (boundp sym) (fboundp sym)))
2635 nil)))))
2636 (if sym
2637 (cond
2638 ((boundp sym) (describe-variable sym))
2639 ((fboundp sym) (describe-function sym))
2640 (t nil))
2641 (apropos-command (concat "erc-.*" func) nil
2642 (lambda (x)
2643 (or (commandp x)
2644 (get x 'custom-type))))
2646 (apropos "erc-cmd-")
2647 (message "Type C-h m to get additional information about keybindings.")
2650 (defalias 'erc-cmd-H 'erc-cmd-HELP)
2652 (defun erc-cmd-JOIN (channel &optional key)
2653 "Join the channel given in CHANNEL, optionally with KEY.
2654 If CHANNEL is specified as \"-invite\", join the channel to which you
2655 were most recently invited. See also `invitation'."
2656 (let (chnl)
2657 (if (string= (upcase channel) "-INVITE")
2658 (if erc-invitation
2659 (setq chnl erc-invitation)
2660 (erc-display-message nil 'error (current-buffer) 'no-invitation))
2661 (setq chnl (erc-ensure-channel-name channel)))
2662 (when chnl
2663 ;; Prevent double joining of same channel on same server.
2664 (let ((joined-channels
2665 (mapcar #'(lambda (chanbuf)
2666 (with-current-buffer chanbuf (erc-default-target)))
2667 (erc-channel-list erc-server-process))))
2668 (if (erc-member-ignore-case chnl joined-channels)
2669 (switch-to-buffer (car (erc-member-ignore-case chnl
2670 joined-channels)))
2671 (erc-log (format "cmd: JOIN: %s" chnl))
2672 (if (and chnl key)
2673 (erc-server-send (format "JOIN %s %s" chnl key))
2674 (erc-server-send (format "JOIN %s" chnl)))))))
2677 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
2678 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
2680 (defvar erc-channel-new-member-names nil
2681 "If non-nil, a names list is currently being received.
2683 If non-nil, this variable is a hash-table that associates
2684 received nicks with t.")
2685 (make-variable-buffer-local 'erc-channel-new-member-names)
2687 (defun erc-cmd-NAMES (&optional channel)
2688 "Display the users in CHANNEL.
2689 If CHANNEL is not specified, display the users in the current channel.
2690 This function clears the channel name list first, then sends the
2691 command."
2692 (let ((tgt (or (and (erc-channel-p channel) channel)
2693 (erc-default-target))))
2694 (if (and tgt (erc-channel-p tgt))
2695 (progn
2696 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
2697 (erc-with-buffer
2698 (tgt)
2699 (erc-channel-begin-receiving-names))
2700 (erc-server-send (concat "NAMES " tgt)))
2701 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
2703 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
2705 (defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
2706 "Kick the user indicated in LINE from the current channel.
2707 LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
2708 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
2709 (if (string= "" reasonstring)
2710 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
2711 (if (erc-channel-p target)
2712 (let ((nick reason-or-nick))
2713 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
2714 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
2715 nil target)
2717 (when target
2718 (let ((ch (erc-default-target)))
2719 (setq reasonstring (concat
2720 (if reason-or-nick (concat reason-or-nick " "))
2721 reasonstring))
2722 (if ch
2723 (progn
2724 (erc-log
2725 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
2726 (erc-server-send
2727 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
2728 (erc-display-message nil 'error (current-buffer)
2729 'no-default-channel))
2730 t)))))
2732 (defvar erc-script-args nil)
2734 (defun erc-cmd-LOAD (line)
2735 "Load the script provided in the LINE.
2736 If LINE continues beyond the file name,
2737 the rest of it is put in a (local) variable
2738 `erc-script-args', which can be used in elisp scripts.
2740 The optional FORCE argument is ignored here - you can't force loading
2741 a script after exceeding the flood threshold."
2742 (cond
2743 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
2744 (let* ((file-to-find (match-string 1 line))
2745 (erc-script-args (match-string 2 line))
2746 (file (erc-find-file file-to-find erc-script-path)))
2747 (erc-log (format "cmd: LOAD: %s" file-to-find))
2748 (cond
2749 ((not file)
2750 (erc-display-message nil 'error (current-buffer)
2751 'cannot-find-file ?f file-to-find))
2752 ((not (file-readable-p file))
2753 (erc-display-message nil 'error (current-buffer)
2754 'cannot-read-file ?f file))
2756 (message "Loading \'%s\'..." file)
2757 (erc-load-script file)
2758 (message "Loading \'%s\'...done" file))))
2760 (t nil)))
2762 (defun erc-cmd-WHOIS (user &optional server)
2763 "Display whois information for USER.
2765 If SERVER is non-nil, use that, rather than the current server."
2766 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
2767 (let ((send (if server
2768 (format "WHOIS %s %s" user server)
2769 (format "WHOIS %s" user))))
2770 (erc-log (format "cmd: %s" send))
2771 (erc-server-send send)
2773 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
2775 (defun erc-cmd-WHOAMI ()
2776 "Display whois information about yourself."
2777 (erc-cmd-WHOIS (erc-current-nick))
2780 (defun erc-cmd-IDLE (nick)
2781 "Show the length of time NICK has been idle."
2782 (let ((serverbuf (erc-server-buffer))
2783 (origbuf (current-buffer))
2784 symlist)
2785 (with-current-buffer serverbuf
2786 (add-to-list 'symlist
2787 (cons (erc-once-with-server-event
2788 311 `(string= ,nick
2789 (second
2790 (erc-response.command-args parsed))))
2791 'erc-server-311-functions))
2792 (add-to-list 'symlist
2793 (cons (erc-once-with-server-event
2794 312 `(string= ,nick
2795 (second
2796 (erc-response.command-args parsed))))
2797 'erc-server-312-functions))
2798 (add-to-list 'symlist
2799 (cons (erc-once-with-server-event
2800 318 `(string= ,nick
2801 (second
2802 (erc-response.command-args parsed))))
2803 'erc-server-318-functions))
2804 (add-to-list 'symlist
2805 (cons (erc-once-with-server-event
2806 319 `(string= ,nick
2807 (second
2808 (erc-response.command-args parsed))))
2809 'erc-server-319-functions))
2810 (add-to-list 'symlist
2811 (cons (erc-once-with-server-event
2812 320 `(string= ,nick
2813 (second
2814 (erc-response.command-args parsed))))
2815 'erc-server-320-functions))
2816 (add-to-list 'symlist
2817 (cons (erc-once-with-server-event
2818 330 `(string= ,nick
2819 (second
2820 (erc-response.command-args parsed))))
2821 'erc-server-330-functions))
2822 (add-to-list 'symlist
2823 (cons (erc-once-with-server-event
2825 `(let ((idleseconds
2826 (string-to-number
2827 (third
2828 (erc-response.command-args parsed)))))
2829 (erc-display-line
2830 (erc-make-notice
2831 (format "%s has been idle for %s."
2832 (erc-string-no-properties ,nick)
2833 (erc-seconds-to-string idleseconds)))
2834 ,origbuf))
2836 'erc-server-317-functions))
2838 ;; Send the WHOIS command.
2839 (erc-cmd-WHOIS nick)
2841 ;; Remove the uninterned symbols from the server hooks that did not run.
2842 (run-at-time 20 nil `(lambda ()
2843 (with-current-buffer ,(current-buffer)
2844 (dolist (sym ',symlist)
2845 (let ((hooksym (cdr sym))
2846 (funcsym (car sym)))
2847 (remove-hook hooksym funcsym t))))))))
2850 (defun erc-cmd-DESCRIBE (line)
2851 "Pose some action to a certain user.
2852 LINE has the format \"USER ACTION\"."
2853 (cond
2854 ((string-match
2855 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
2856 (let ((dst (match-string 1 line))
2857 (s (match-string 2 line)))
2858 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
2859 (erc-send-action dst s))
2861 (t nil)))
2862 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
2864 (defun erc-cmd-ME (line)
2865 "Send LINE as an action."
2866 (cond
2867 ((string-match "^\\s-\\(.*\\)$" line)
2868 (let ((s (match-string 1 line)))
2869 (erc-log (format "cmd: ME: %s" s))
2870 (erc-send-action (erc-default-target) s))
2872 (t nil)))
2873 (put 'erc-cmd-ME 'do-not-parse-args t)
2875 (defun erc-cmd-LASTLOG (line)
2876 "Show all lines in the current buffer matching the regexp LINE.
2878 If a match spreads across multiple lines, all those lines are shown.
2880 The lines are shown in a buffer named `*Occur*'.
2881 It serves as a menu to find any of the occurrences in this buffer.
2882 \\[describe-mode] in that buffer will explain how.
2884 If LINE contains upper case characters (excluding those preceded by `\'),
2885 the matching is case-sensitive."
2886 (occur line)
2888 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
2890 (defun erc-send-message (line &optional force)
2891 "Send LINE to the current channel or user and display it.
2893 See also `erc-message' and `erc-display-line'."
2894 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
2895 (erc-display-line
2896 (concat (erc-format-my-nick) line)
2897 (current-buffer))
2898 ;; FIXME - treat multiline, run hooks, or remove me?
2901 (defun erc-cmd-MODE (line)
2902 "Change or display the mode value of a channel or user.
2903 The first word specifies the target. The rest is the mode string
2904 to send.
2906 If only one word is given, display the mode of that target.
2908 A list of valid mode strings for Freenode may be found at
2909 `http://freenode.net/using_the_network.shtml'."
2910 (cond
2911 ((string-match "^\\s-\\(.*\\)$" line)
2912 (let ((s (match-string 1 line)))
2913 (erc-log (format "cmd: MODE: %s" s))
2914 (erc-server-send (concat "MODE " line)))
2916 (t nil)))
2917 (put 'erc-cmd-MODE 'do-not-parse-args t)
2919 (defun erc-cmd-NOTICE (channel-or-user &rest message)
2920 "Send a notice to the channel or user given as the first word.
2921 The rest is the message to send."
2922 (erc-message "NOTICE" (concat channel-or-user " "
2923 (mapconcat #'identity message " "))))
2925 (defun erc-cmd-MSG (line)
2926 "Send a message to the channel or user given as the first word in LINE.
2928 The rest of LINE is the message to send."
2929 (erc-message "PRIVMSG" line))
2931 (defalias 'erc-cmd-M 'erc-cmd-MSG)
2932 (put 'erc-cmd-MSG 'do-not-parse-args t)
2934 (defun erc-cmd-SQUERY (line)
2935 "Send a Service Query to the service given as the first word in LINE.
2937 The rest of LINE is the message to send."
2938 (erc-message "SQUERY" line))
2940 (defun erc-cmd-NICK (nick)
2941 "Change current nickname to NICK."
2942 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
2943 (let ((nicklen (cdr (assoc "NICKLEN" (with-current-buffer (erc-server-buffer)
2944 erc-server-parameters)))))
2945 (and nicklen (> (length nick) (string-to-number nicklen))
2946 (erc-display-message
2947 nil 'notice 'active 'nick-too-long
2948 ?i (length nick) ?l nicklen)))
2949 (erc-server-send (format "NICK %s" nick))
2950 (cond (erc-bad-nick
2951 (erc-set-current-nick nick)
2952 (erc-update-mode-line)
2953 (setq erc-bad-nick nil)))
2956 (defun erc-cmd-PART (line)
2957 "When LINE is an empty string, leave the current channel.
2958 Otherwise leave the channel indicated by LINE."
2959 (cond
2960 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
2961 (let* ((ch (match-string 1 line))
2962 (msg (match-string 2 line))
2963 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
2964 (erc-log (format "cmd: PART: %s: %s" ch reason))
2965 (erc-server-send (if (string= reason "")
2966 (format "PART %s" ch)
2967 (format "PART %s :%s" ch reason))
2968 nil ch))
2970 ((string-match "^\\s-*\\(.*\\)$" line)
2971 (let* ((ch (erc-default-target))
2972 (msg (match-string 1 line))
2973 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
2974 (if (and ch (erc-channel-p ch))
2975 (progn
2976 (erc-log (format "cmd: PART: %s: %s" ch reason))
2977 (erc-server-send (if (string= reason "")
2978 (format "PART %s" ch)
2979 (format "PART %s :%s" ch reason))
2980 nil ch))
2981 (erc-display-message nil 'error (current-buffer) 'no-target)))
2983 (t nil)))
2984 (put 'erc-cmd-PART 'do-not-parse-args t)
2986 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
2988 (defun erc-cmd-PING (recipient)
2989 "Ping RECIPIENT."
2990 (let ((time (format "%f" (erc-current-time))))
2991 (erc-log (format "cmd: PING: %s" time))
2992 (erc-cmd-CTCP recipient "PING" time)))
2994 (defun erc-cmd-QUOTE (line)
2995 "Send LINE directly to the server.
2996 All the text given as argument is sent to the sever as unmodified,
2997 just as you provided it. Use this command with care!"
2998 (cond
2999 ((string-match "^\\s-\\(.+\\)$" line)
3000 (erc-server-send (match-string 1 line)))
3001 (t nil)))
3002 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
3004 (defun erc-cmd-QUERY (&optional user)
3005 "Open a query with USER.
3006 The type of query window/frame/etc will depend on the value of
3007 `erc-join-buffer'. If USER is omitted, close the current query buffer if one
3008 exists - except this is broken now ;-)"
3009 (interactive
3010 (list (read-from-minibuffer "Start a query with: " nil)))
3011 (let ((session-buffer (erc-server-buffer)))
3012 (if user
3013 (erc-query user session-buffer)
3014 ;; currently broken, evil hack to display help anyway
3015 ;(erc-delete-query))))
3016 (signal 'wrong-number-of-arguments ""))))
3017 (defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3019 (defun erc-quit-reason-normal (&optional s)
3020 "Normal quit message.
3022 If S is non-nil, it will be used as the quit reason."
3023 (or s
3024 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3025 erc-version-string) ; erc-official-location)
3028 (defun erc-quit-reason-zippy (&optional s)
3029 "Zippy quit message.
3031 If S is non-nil, it will be used as the quit reason."
3032 (or s
3033 (erc-replace-regexp-in-string "\n" "" (yow))))
3035 (defun erc-quit-reason-various (s)
3036 "Choose a quit reason based on S (a string)."
3037 (when (featurep 'xemacs) (require 'poe))
3038 (let ((res (car (assoc-default (or s "")
3039 erc-quit-reason-various-alist 'string-match))))
3040 (cond
3041 ((functionp res) (funcall res))
3042 ((stringp res) res)
3043 ;; hopefully never reached
3044 (s))))
3046 (defun erc-part-reason-normal (&optional s)
3047 "Normal part message.
3049 If S is non-nil, it will be used as the quit reason."
3050 (or s
3051 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3052 erc-version-string) ; erc-official-location)
3055 (defun erc-part-reason-zippy (&optional s)
3056 "Zippy part message.
3058 If S is non-nil, it will be used as the quit reason."
3059 (or s
3060 (erc-replace-regexp-in-string "\n" "" (yow))))
3062 (defun erc-part-reason-various (s)
3063 "Choose a part reason based on S (a string)."
3064 (when (featurep 'xemacs) (require 'poe))
3065 (let ((res (car (assoc-default (or s "")
3066 erc-part-reason-various-alist 'string-match))))
3067 (cond
3068 ((functionp res) (funcall res))
3069 ((stringp res) res)
3070 (s))))
3072 (defun erc-cmd-QUIT (reason)
3073 "Disconnect from the current server.
3074 If REASON is omitted, display a default quit message, otherwise display
3075 the message given by REASON."
3076 (unless reason
3077 (setq reason ""))
3078 (cond
3079 ((string-match "^\\s-*\\(.*\\)$" reason)
3080 (let* ((s (match-string 1 reason))
3081 (buffer (erc-server-buffer))
3082 (reason (funcall erc-quit-reason (if (equal s "") nil s))))
3083 (with-current-buffer (if (and buffer
3084 (bufferp buffer))
3085 buffer
3086 (current-buffer))
3087 (erc-log (format "cmd: QUIT: %s" reason))
3088 (setq erc-server-quitting t)
3089 (erc-set-active-buffer (erc-server-buffer))
3090 (erc-server-send (format "QUIT :%s" reason)))
3091 (run-hook-with-args 'erc-quit-hook erc-server-process)
3092 (when erc-kill-queries-on-quit
3093 (erc-kill-query-buffers erc-server-process)))
3095 (t nil)))
3097 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3098 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3099 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3100 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3102 (defun erc-cmd-GQUIT (reason)
3103 "Disconnect from all servers at once with the same quit REASON."
3104 (erc-with-all-buffers-of-server nil #'(lambda ()
3105 (and (erc-server-buffer-p)
3106 (erc-server-process-alive)))
3107 (erc-cmd-QUIT reason)))
3109 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3110 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3112 (defun erc-cmd-SERVER (server)
3113 "Connect to SERVER, leaving existing connection intact."
3114 (erc-log (format "cmd: SERVER: %s" server))
3115 (condition-case nil
3116 (erc-select :server server :nick (erc-current-nick))
3117 (error
3118 (message "Cannot find host %s." server)
3119 (beep)))
3122 (eval-when-compile
3123 (defvar motif-version-string)
3124 (defvar gtk-version-string))
3126 (defun erc-cmd-SV ()
3127 "Say the current ERC and Emacs version into channel."
3128 (erc-send-message (format "I'm using ERC %s with %s %s (%s%s%s)!"
3129 erc-version-string
3130 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3131 emacs-version
3132 system-configuration
3133 (concat
3134 (cond ((featurep 'motif)
3135 (concat ", " (substring
3136 motif-version-string 4)))
3137 ((featurep 'gtk)
3138 (concat ", GTK+ Version "
3139 gtk-version-string))
3140 ((featurep 'mac-carbon) ", Mac Carbon")
3141 ((featurep 'x-toolkit) ", X toolkit")
3142 (t ""))
3143 (if (and (boundp 'x-toolkit-scroll-bars)
3144 (memq x-toolkit-scroll-bars
3145 '(xaw xaw3d)))
3146 (format ", %s scroll bars"
3147 (capitalize (symbol-name
3148 x-toolkit-scroll-bars)))
3150 (if (featurep 'multi-tty) ", multi-tty" ""))
3151 (concat ", built " erc-emacs-build-time)))
3154 (defun erc-cmd-SM ()
3155 "Say the current ERC modes into channel."
3156 (erc-send-message (format "I'm using the following modules: %s!"
3157 (erc-modes)))
3160 (defun erc-cmd-DEOP (&rest people)
3161 "Remove the operator setting from user(s) given in PEOPLE."
3162 (when (> (length people) 0)
3163 (erc-server-send (concat "MODE " (erc-default-target)
3164 " -"
3165 (make-string (length people) ?o)
3167 (mapconcat 'identity people " ")))
3170 (defun erc-cmd-OP (&rest people)
3171 "Add the operator setting to users(s) given in PEOPLE."
3172 (when (> (length people) 0)
3173 (erc-server-send (concat "MODE " (erc-default-target)
3174 " +"
3175 (make-string (length people) ?o)
3177 (mapconcat 'identity people " ")))
3180 (defun erc-cmd-TIME (&optional line)
3181 "Request the current time and date from the current server."
3182 (cond
3183 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3184 (let ((args (match-string 1 line)))
3185 (erc-log (format "cmd: TIME: %s" args))
3186 (erc-server-send (concat "TIME " args)))
3188 (t (erc-server-send "TIME"))))
3189 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3191 (defun erc-cmd-TOPIC (topic)
3192 "Set or request the topic for a channel.
3193 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3194 or the empty string.
3196 If no #CHANNEL is given, the default channel is used. If TOPIC is
3197 given, the channel topic is modified, otherwise the current topic will
3198 be displayed."
3199 (cond
3200 ;; /topic #channel TOPIC
3201 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3202 (let ((ch (match-string 1 topic))
3203 (topic (match-string 2 topic)))
3204 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3205 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3207 ;; /topic #channel
3208 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3209 (let ((ch (match-string 1 topic)))
3210 (erc-server-send (format "TOPIC %s" ch) nil ch)
3212 ;; /topic
3213 ((string-match "^\\s-*$" topic)
3214 (let ((ch (erc-default-target)))
3215 (erc-server-send (format "TOPIC %s" ch) nil ch)
3217 ;; /topic TOPIC
3218 ((string-match "^\\s-*\\(.*\\)$" topic)
3219 (let ((ch (erc-default-target))
3220 (topic (match-string 1 topic)))
3221 (if (and ch (erc-channel-p ch))
3222 (progn
3223 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3224 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3225 (erc-display-message nil 'error (current-buffer) 'no-target)))
3227 (t nil)))
3228 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3229 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3231 (defun erc-cmd-APPENDTOPIC (topic)
3232 "Append TOPIC to the current channel topic, separated by a space."
3233 (let ((oldtopic erc-channel-topic))
3234 ;; display help when given no arguments
3235 (when (string-match "^\\s-*$" topic)
3236 (signal 'wrong-number-of-arguments nil))
3237 ;; strip trailing ^O
3238 (when (string-match "\\(.*\\)\C-o" oldtopic)
3239 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3240 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3241 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3243 (defun erc-cmd-CLEARTOPIC (&optional channel)
3244 "Clear the topic for a CHANNEL.
3245 If CHANNEL is not specified, clear the topic for the default channel."
3246 (interactive "sClear topic of channel (RET is current channel): ")
3247 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3248 (when chnl
3249 (erc-server-send (format "TOPIC %s :" chnl))
3250 t)))
3252 ;;; Banlists
3254 (defvar erc-channel-banlist nil
3255 "A list of bans seen for the current channel.
3257 Each ban is an alist of the form:
3258 (WHOSET . MASK)
3260 The property `received-from-server' indicates whether
3261 or not the ban list has been requested from the server.")
3262 (make-variable-buffer-local 'erc-channel-banlist)
3263 (put 'erc-channel-banlist 'received-from-server nil)
3265 (defun erc-cmd-BANLIST ()
3266 "Pretty-print the contents of `erc-channel-banlist'.
3268 The ban list is fetched from the server if necessary."
3269 (let ((chnl (erc-default-target))
3270 (chnl-name (buffer-name)))
3272 (cond
3273 ((not (erc-channel-p chnl))
3274 (erc-display-line (erc-make-notice "You're not on a channel\n")
3275 'active))
3277 ((not (get 'erc-channel-banlist 'received-from-server))
3278 (let ((old-367-hook erc-server-367-functions))
3279 (setq erc-server-367-functions 'erc-banlist-store
3280 erc-channel-banlist nil)
3281 ;; fetch the ban list then callback
3282 (with-current-buffer (erc-server-buffer)
3283 (erc-once-with-server-event
3285 `(with-current-buffer ,chnl-name
3286 (put 'erc-channel-banlist 'received-from-server t)
3287 (setq erc-server-367-functions ',old-367-hook)
3288 (erc-cmd-BANLIST)
3290 (erc-server-send (format "MODE %s b" chnl)))))
3292 ((null erc-channel-banlist)
3293 (erc-display-line (erc-make-notice
3294 (format "No bans for channel: %s\n" chnl))
3295 'active)
3296 (put 'erc-channel-banlist 'received-from-server nil))
3299 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3300 erc-fill-column)
3301 (and (boundp 'fill-column)
3302 fill-column)
3303 (1- (window-width))))
3304 (separator (make-string erc-fill-column (string-to-char "=")))
3305 (fmt (concat
3306 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3307 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3309 (erc-display-line
3310 (erc-make-notice (format "Ban list for channel: %s\n"
3311 (erc-default-target)))
3312 'active)
3314 (erc-display-line separator 'active)
3315 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3316 (erc-display-line separator 'active)
3318 (mapc
3319 (lambda (x)
3320 (erc-display-line
3321 (format fmt
3322 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3323 (if (car x)
3324 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3325 ""))
3326 'active))
3327 erc-channel-banlist)
3329 (erc-display-line (erc-make-notice "End of Ban list")
3330 'active)
3331 (put 'erc-channel-banlist 'received-from-server nil)))))
3334 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3336 (defun erc-cmd-MASSUNBAN ()
3337 "Mass Unban.
3339 Unban all currently banned users in the current channel."
3340 (let ((chnl (erc-default-target)))
3341 (cond
3343 ((not (erc-channel-p chnl))
3344 (erc-display-line
3345 (erc-make-notice "You're not on a channel\n")
3346 'active))
3348 ((not (get 'erc-channel-banlist 'received-from-server))
3349 (let ((old-367-hook erc-server-367-functions))
3350 (setq erc-server-367-functions 'erc-banlist-store)
3351 ;; fetch the ban list then callback
3352 (with-current-buffer (erc-server-buffer)
3353 (erc-once-with-server-event
3355 `(with-current-buffer ,chnl
3356 (put 'erc-channel-banlist 'received-from-server t)
3357 (setq erc-server-367-functions ,old-367-hook)
3358 (erc-cmd-MASSUNBAN)
3360 (erc-server-send (format "MODE %s b" chnl)))))
3362 (t (let ((bans (mapcar 'cdr erc-channel-banlist)))
3363 (when bans
3364 ;; Glob the bans into groups of three, and carry out the unban.
3365 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3366 (mapc
3367 (lambda (x)
3368 (erc-server-send
3369 (format "MODE %s -%s %s" (erc-default-target)
3370 (make-string (length x) (string-to-char "b"))
3371 (mapconcat 'identity x " "))))
3372 (erc-group-list bans 3))))
3373 t))))
3375 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3377 ;;;; End of IRC commands
3379 (defun erc-ensure-channel-name (channel)
3380 "Return CHANNEL if it is a valid channel name.
3381 Eventually add a # in front of it, if that turns it into a valid channel name."
3382 (if (erc-channel-p channel)
3383 channel
3384 (concat "#" channel)))
3386 (defun erc-grab-region (start end)
3387 "Copy the region between START and END in a recreatable format.
3389 Converts all the IRC text properties in each line of the region
3390 into control codes and writes them to a separate buffer. The
3391 resulting text may be used directly as a script to generate this
3392 text again."
3393 (interactive "r")
3394 (erc-set-active-buffer (current-buffer))
3395 (save-excursion
3396 (let* ((cb (current-buffer))
3397 (buf (generate-new-buffer erc-grab-buffer-name))
3398 (region (buffer-substring start end))
3399 (lines (erc-split-multiline-safe region)))
3400 (set-buffer buf)
3401 (dolist (line lines)
3402 (insert (concat line "\n")))
3403 (set-buffer cb)
3404 (switch-to-buffer-other-window buf)))
3405 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3406 (ding))
3408 (defun erc-display-prompt (&optional buffer pos prompt face)
3409 "Display PROMPT in BUFFER at position POS.
3410 Display an ERC prompt in BUFFER.
3412 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3413 If BUFFER is nil, the `current-buffer' is used.
3414 If POS is nil, PROMPT will be displayed at `point'.
3415 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3416 `erc-prompt-face' will be used."
3417 (let* ((prompt (or prompt (erc-prompt)))
3418 (l (length prompt))
3419 (ob (current-buffer)))
3420 ;; We cannot use save-excursion because we move point, therefore
3421 ;; we resort to the ol' ob trick to restore this.
3422 (when (and buffer (bufferp buffer))
3423 (set-buffer buffer))
3425 ;; now save excursion again to store where point and mark are
3426 ;; in the current buffer
3427 (save-excursion
3428 (setq pos (or pos (point)))
3429 (goto-char pos)
3430 (when (> l 0)
3431 ;; Do not extend the text properties when typing at the end
3432 ;; of the prompt, but stuff typed in front of the prompt
3433 ;; shall remain part of the prompt.
3434 (setq prompt (erc-propertize prompt
3435 'start-open t ; XEmacs
3436 'rear-nonsticky t ; Emacs
3437 'erc-prompt t
3438 'front-sticky t
3439 'read-only t))
3440 (erc-put-text-property 0 (1- (length prompt))
3441 'face (or face 'erc-prompt-face)
3442 prompt)
3443 (insert prompt))
3444 ;; Set the input marker
3445 (set-marker erc-input-marker (point)))
3447 ;; Now we are back at the old position. If the prompt was
3448 ;; inserted here or before us, advance point by the length of
3449 ;; the prompt.
3450 (when (or (not pos) (<= (point) pos))
3451 (forward-char l))
3452 ;; Clear the undo buffer now, so the user can undo his stuff,
3453 ;; but not the stuff we did. Sneaky!
3454 (setq buffer-undo-list nil)
3455 (set-buffer ob)))
3457 ;; interactive operations
3459 (defun erc-input-message ()
3460 "Read input from the minibuffer."
3461 (interactive)
3462 (let ((minibuffer-allow-text-properties t)
3463 (read-map minibuffer-local-map))
3464 (insert (read-from-minibuffer "Message: "
3465 (string last-command-char) read-map))
3466 (erc-send-current-line)))
3468 (defvar erc-action-history-list ()
3469 "History list for interactive action input.")
3471 (defun erc-input-action ()
3472 "Interactively input a user action and send it to IRC."
3473 (interactive "")
3474 (erc-set-active-buffer (current-buffer))
3475 (let ((action (read-from-minibuffer
3476 "Action: " nil nil nil 'erc-action-history-list)))
3477 (if (not (string-match "^\\s-*$" action))
3478 (erc-send-action (erc-default-target) action))))
3480 (defun erc-join-channel (channel &optional key)
3481 "Join CHANNEL.
3483 If `point' is at the beginning of a channel name, use that as default."
3484 (interactive
3485 (list
3486 (let ((chnl (if (looking-at "\\([&#+!][^ ]+\\)") (match-string 1) ""))
3487 (table (when (erc-server-buffer-live-p)
3488 (set-buffer (process-buffer erc-server-process))
3489 erc-channel-list)))
3490 (completing-read "Join channel: " table nil nil nil nil chnl))
3491 (when erc-prompt-for-channel-key
3492 (read-from-minibuffer "Channel key (RET for none): " nil))))
3493 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3495 (defun erc-part-from-channel (reason)
3496 "Part from the current channel and prompt for a REASON."
3497 (interactive
3498 (list
3499 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3500 reason
3501 (read-from-minibuffer (concat "Leave " (erc-default-target)
3502 ", Reason? ")
3503 (cons "No reason" 0)))))
3504 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3506 (defun erc-set-topic (topic)
3507 "Prompt for a TOPIC for the current channel."
3508 (interactive
3509 (list
3510 (read-from-minibuffer
3511 (concat "Set topic of " (erc-default-target) ": ")
3512 (when erc-channel-topic
3513 (cons (apply 'concat (butlast (split-string erc-channel-topic "\C-o")))
3514 0)))))
3515 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3516 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3518 (defun erc-set-channel-limit (&optional limit)
3519 "Set a LIMIT for the current channel. Remove limit if nil.
3520 Prompt for one if called interactively."
3521 (interactive (list (read-from-minibuffer
3522 (format "Limit for %s (RET to remove limit): "
3523 (erc-default-target)))))
3524 (let ((tgt (erc-default-target)))
3525 (if (and limit (>= (length limit) 1))
3526 (erc-server-send (format "MODE %s +l %s" tgt limit))
3527 (erc-server-send (format "MODE %s -l" tgt)))))
3529 (defun erc-set-channel-key (&optional key)
3530 "Set a KEY for the current channel. Remove key if nil.
3531 Prompt for one if called interactively."
3532 (interactive (list (read-from-minibuffer
3533 (format "Key for %s (RET to remove key): "
3534 (erc-default-target)))))
3535 (let ((tgt (erc-default-target)))
3536 (if (and key (>= (length key) 1))
3537 (erc-server-send (format "MODE %s +k %s" tgt key))
3538 (erc-server-send (format "MODE %s -k" tgt)))))
3540 (defun erc-quit-server (reason)
3541 "Disconnect from current server after prompting for REASON.
3542 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
3543 (interactive (list (read-from-minibuffer
3544 (format "Reason for quitting %s: "
3545 (or erc-server-announced-name
3546 erc-session-server)))))
3547 (erc-cmd-QUIT reason))
3549 ;; Movement of point
3551 (defun erc-bol ()
3552 "Move `point' to the beginning of the current line.
3554 This places `point' just after the prompt, or at the beginning of the line."
3555 (interactive)
3556 (forward-line 0)
3557 (when (get-text-property (point) 'erc-prompt)
3558 (goto-char erc-input-marker))
3559 (point))
3561 (defun erc-kill-input ()
3562 "Kill current input line using `erc-bol' followed by `kill-line'."
3563 (interactive)
3564 (when (and (erc-bol)
3565 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
3566 ;; there's nothing to kill
3567 (if (boundp 'erc-input-ring-index)
3568 (setq erc-input-ring-index nil))
3569 (kill-line)))
3571 (defun erc-complete-word ()
3572 "Complete the word before point.
3574 This function uses `erc-complete-functions'."
3575 (interactive)
3576 (unless (run-hook-with-args-until-success 'erc-complete-functions)
3577 (beep)))
3579 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3581 ;; IRC SERVER INPUT HANDLING
3583 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3585 ;;;; New Input parsing
3587 ; Stolen from ZenIRC. I just wanna test this code, so here is
3588 ; experiment area.
3590 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
3591 erc-default-server-handler)
3592 "*Default for server messages which aren't covered by `erc-server-hooks'."
3593 :group 'erc-server-hooks
3594 :type 'hook)
3596 (defun erc-default-server-handler (proc parsed)
3597 "Default server handler.
3599 Displays PROC and PARSED appropriately using `erc-display-message'."
3600 (erc-display-message
3601 parsed 'notice proc
3602 (mapconcat
3603 'identity
3604 (let (res)
3605 (mapc #'(lambda (x)
3606 (if (stringp x)
3607 (setq res (append res (list x)))))
3608 parsed)
3609 res)
3610 " ")))
3612 (defvar erc-server-vectors
3613 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
3614 "List of received server messages which ERC does not specifically handle.
3615 See `erc-debug-missing-hooks'.")
3616 ;(make-variable-buffer-local 'erc-server-vectors)
3618 (defun erc-debug-missing-hooks (proc parsed)
3619 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
3620 These vectors can be helpful when adding new server message handlers to ERC.
3621 See `erc-default-server-hook'."
3622 (nconc erc-server-vectors (list parsed))
3623 nil)
3625 (defun erc-query (target server)
3626 "Open a query buffer on TARGET, using SERVER.
3627 To change how this query window is displayed, use `let' to bind
3628 `erc-join-buffer' before calling this."
3629 (unless (and server
3630 (buffer-live-p server)
3631 (set-buffer server))
3632 (error "Couldn't switch to server buffer"))
3633 (let ((buf (erc erc-session-server
3634 erc-session-port
3635 (erc-current-nick)
3636 erc-session-user-full-name
3639 (list target)
3640 target
3641 erc-server-process)))
3642 (unless buf
3643 (error "Couldn't open query window"))
3644 (erc-update-mode-line)
3645 buf))
3647 (defcustom erc-auto-query nil
3648 "If non-nil, create a query buffer each time you receive a private message.
3650 If the buffer doesn't already exist it is created. This can be
3651 set to a symbol, to control how the new query window should
3652 appear. See the documentation for `erc-join-buffer' for
3653 available choices."
3654 :group 'erc-query
3655 :type '(choice (const nil)
3656 (const buffer)
3657 (const window)
3658 (const window-noselect)
3659 (const bury)
3660 (const frame)))
3662 (defcustom erc-query-on-unjoined-chan-privmsg t
3663 "If non-nil create query buffer on receiving any PRIVMSG at all.
3664 This includes PRIVMSGs directed to channels. If you are using an IRC
3665 bouncer, such as dircproxy, to keep a log of channels when you are
3666 disconnected, you should set this option to t."
3667 :group 'erc-query
3668 :type 'boolean)
3670 (defcustom erc-format-query-as-channel-p t
3671 "If non-nil, format text from others in a query buffer like in a channel,
3672 otherwise format like a private message."
3673 :group 'erc-query
3674 :type 'boolean)
3676 (defcustom erc-minibuffer-notice nil
3677 "If non-nil, print ERC notices for the user in the minibuffer.
3678 Only happens when the session buffer isn't visible."
3679 :group 'erc-display
3680 :type 'boolean)
3682 (defcustom erc-minibuffer-ignored nil
3683 "If non-nil, print a message in the minibuffer if we ignored something."
3684 :group 'erc-ignore
3685 :type 'boolean)
3687 (defun erc-wash-quit-reason (reason nick login host)
3688 "Remove duplicate text from quit REASON.
3689 Specifically in relation to NICK (user@host) information. Returns REASON
3690 unmodified if nothing can be removed.
3691 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
3692 \"Read error: 110\". The same applies for \"Ping Timeout\"."
3693 (setq nick (regexp-quote nick)
3694 login (regexp-quote login)
3695 host (regexp-quote host))
3696 (or (when (string-match (concat "^\\(Read error\\) to "
3697 nick "\\[" host "\\]: "
3698 "\\(.+\\)$") reason)
3699 (concat (match-string 1 reason) ": " (match-string 2 reason)))
3700 (when (string-match (concat "^\\(Ping timeout\\) for "
3701 nick "\\[" host "\\]$") reason)
3702 (match-string 1 reason))
3703 reason))
3705 (defun erc-nickname-in-use (nick reason)
3706 "If NICK is unavailable, tell the user the REASON.
3708 See also `erc-display-error-notice'."
3709 (if (or erc-manual-set-nick-on-bad-nick-p
3710 ;; how many default-nicks are left + one more try...
3711 (eq erc-nick-change-attempt-count
3712 (if (consp erc-nick)
3713 (+ (length erc-nick) 1)
3714 1)))
3715 (erc-display-error-notice
3717 (format "Nickname %s is %s, try another." nick reason))
3718 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
3719 (let ((newnick (nth 1 erc-default-nicks))
3720 (nicklen (cdr (assoc "NICKLEN"
3721 (with-current-buffer (erc-server-buffer)
3722 erc-server-parameters)))))
3723 (setq erc-bad-nick t)
3724 ;; try to use a different nick
3725 (if erc-default-nicks
3726 (setq erc-default-nicks (cdr erc-default-nicks)))
3727 (if (not newnick)
3728 (setq newnick (concat (truncate-string-to-width
3729 nick
3730 (if (and erc-server-connected nicklen)
3731 (- (string-to-number nicklen) 1)
3732 ;; rfc2812 max nick length = 9
3733 ;; we must assume this is the
3734 ;; server's setting if we haven't
3735 ;; established a connection yet
3737 erc-nick-uniquifier)))
3738 (erc-cmd-NICK newnick)
3739 (erc-display-error-notice
3741 (format "Nickname %s is %s, trying %s"
3742 nick reason newnick)))))
3744 ;;; Server messages
3746 (defgroup erc-server-hooks nil
3747 "Server event callbacks.
3748 Every server event - like numeric replies - has it's own hook.
3749 Those hooks are all called using `run-hook-with-args-until-success'.
3750 They receive as first argument the process object from where the event
3751 originated from,
3752 and as second argument the event parsed as a vector."
3753 :group 'erc-hooks)
3755 (defun erc-display-server-message (proc parsed)
3756 "Display the message sent by the server as a notice."
3757 (erc-display-message
3758 parsed 'notice 'active (erc-response.contents parsed)))
3760 (defun erc-auto-query (proc parsed)
3761 ;; FIXME: This needs more documentation, unless it's not a user function --
3762 ;; Lawrence 2004-01-08
3763 "Put this on `erc-server-PRIVMSG-functions'."
3764 (when erc-auto-query
3765 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
3766 (target (car (erc-response.command-args parsed)))
3767 (msg (erc-response.contents parsed))
3768 (query (if (not erc-query-on-unjoined-chan-privmsg)
3769 nick
3770 (if (erc-current-nick-p target)
3771 nick
3772 target))))
3773 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
3774 (or erc-query-on-unjoined-chan-privmsg
3775 (string= target (erc-current-nick)))
3776 (not (erc-get-buffer query proc))
3777 (not (erc-is-message-ctcp-and-not-action-p msg))
3778 (let ((erc-join-buffer erc-auto-query))
3779 (erc-cmd-QUERY query))
3780 nil))))
3782 (defun erc-is-message-ctcp-p (message)
3783 "Check if MESSAGE is a CTCP message or not."
3784 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
3786 (defun erc-is-message-ctcp-and-not-action-p (message)
3787 "Check if MESSAGE is a CTCP message or not."
3788 (and (erc-is-message-ctcp-p message)
3789 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
3791 (defun erc-format-privmessage (nick msg privp msgp)
3792 "Format a PRIVMSG in an insertible fashion."
3793 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
3794 (mark-e (if msgp (if privp "*" ">") "-"))
3795 (str (format "%s%s%s %s" mark-s nick mark-e msg))
3796 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
3797 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
3798 ;; add text properties to text before the nick, the nick and after the nick
3799 (erc-put-text-property 0 (length mark-s) 'face msg-face str)
3800 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
3801 'face nick-face str)
3802 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
3803 'face msg-face str)
3804 str))
3806 (defcustom erc-format-nick-function 'erc-format-nick
3807 "*Function to format a nickname for message display."
3808 :group 'erc-display
3809 :type 'function)
3811 (defun erc-format-nick (&optional user channel-data)
3812 "Standard nickname formatting function. Only returns the value
3813 of NICK."
3814 (if user
3815 (erc-server-user-nickname user)))
3817 (defun erc-format-@nick (&optional user channel-data)
3818 "Format a nickname such that @ or + are prefix for the NICK
3819 if OP or VOICE are t respectively."
3820 (if user
3821 (let (op voice)
3822 (if channel-data
3823 (setq op (erc-channel-user-op channel-data)
3824 voice (erc-channel-user-voice channel-data)))
3825 (concat (if voice "+" "")
3826 (if op "@" "")
3827 (erc-server-user-nickname user)))))
3829 (defun erc-format-my-nick ()
3830 "Return the beginning of this user's message, correctly propertized"
3831 (if erc-show-my-nick
3832 (let ((open "<")
3833 (close "> ")
3834 (nick (erc-current-nick)))
3835 (concat
3836 (erc-propertize open 'face 'erc-default-face)
3837 (erc-propertize nick 'face 'erc-nick-default-face)
3838 (erc-propertize close 'face 'erc-default-face)))
3839 (let ((prefix "> "))
3840 (erc-propertize prefix 'face 'erc-default-face))))
3842 (defun erc-echo-notice-in-default-buffer (s parsed buffer sender)
3843 "Echos a private notice in the default buffer, namely the
3844 target buffer specified by BUFFER, or there is no target buffer,
3845 the server buffer. This function is designed to be added to
3846 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
3847 and always returns t."
3848 (erc-display-message parsed nil buffer s)
3851 (defun erc-echo-notice-in-target-buffer (s parsed buffer sender)
3852 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
3853 function is designed to be added to either `erc-echo-notice-hook'
3854 or `erc-echo-notice-always-hook', and returns non-nil iff BUFFER
3855 is non-nil."
3856 (if buffer
3857 (progn (erc-display-message parsed nil buffer s) t)
3858 nil))
3860 (defun erc-echo-notice-in-minibuffer (s parsed buffer sender)
3861 "Echos a private notice in the minibuffer. This function is
3862 designed to be added to either `erc-echo-notice-hook' or
3863 `erc-echo-notice-always-hook', and always returns t."
3864 (message "%s" (concat "NOTICE: " s))
3867 (defun erc-echo-notice-in-server-buffer (s parsed buffer sender)
3868 "Echos a private notice in the server buffer. This function is
3869 designed to be added to either `erc-echo-notice-hook' or
3870 `erc-echo-notice-always-hook', and always returns t."
3871 (erc-display-message parsed nil nil s)
3874 (defun erc-echo-notice-in-active-non-server-buffer (s parsed buffer sender)
3875 "Echos a private notice in the active buffer if the active
3876 buffer is not the server buffer. This function is designed to be
3877 added to either `erc-echo-notice-hook' or
3878 `erc-echo-notice-always-hook', and returns non-nil iff the active
3879 buffer is not the server buffer."
3880 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
3881 (progn (erc-display-message parsed nil 'active s) t)
3882 nil))
3884 (defun erc-echo-notice-in-active-buffer (s parsed buffer sender)
3885 "Echos a private notice in the active buffer. This function is
3886 designed to be added to either `erc-echo-notice-hook' or
3887 `erc-echo-notice-always-hook', and always returns t."
3888 (erc-display-message parsed nil 'active s)
3891 (defun erc-echo-notice-in-user-buffers (s parsed buffer sender)
3892 "Echos a private notice in all of the buffers for which SENDER
3893 is a member. This function is designed to be added to either
3894 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
3895 returns non-nil iff there is at least one buffer for which the
3896 sender is a member.
3898 See also: `erc-echo-notice-in-first-user-buffer',
3899 `erc-buffer-list-with-nick'"
3900 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3901 (if buffers
3902 (progn (erc-display-message parsed nil buffers s) t)
3903 nil)))
3905 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
3906 "Echos a private notice in BUFFER and in all of the buffers for
3907 which SENDER is a member. This function is designed to be added
3908 to either `erc-echo-notice-hook' or
3909 `erc-echo-notice-always-hook', and returns non-nil iff there is
3910 at least one buffer for which the sender is a member or the
3911 default target.
3913 See also: `erc-echo-notice-in-user-buffers',
3914 `erc-buffer-list-with-nick'"
3915 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3916 (add-to-list 'buffers buffer)
3917 (if buffers
3918 (progn (erc-display-message parsed nil buffers s) t)
3919 nil)))
3921 (defun erc-echo-notice-in-first-user-buffer (s parsed buffer sender)
3922 "Echos a private notice in one of the buffers for which SENDER
3923 is a member. This function is designed to be added to either
3924 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
3925 returns non-nil iff there is at least one buffer for which the
3926 sender is a member.
3928 See also: `erc-echo-notice-in-user-buffers',
3929 `erc-buffer-list-with-nick'"
3930 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
3931 (if buffers
3932 (progn (erc-display-message parsed nil (car buffers) s) t)
3933 nil)))
3935 ;;; Ban manipulation
3937 (defun erc-banlist-store (proc parsed)
3938 "Record ban entries for a channel."
3939 (multiple-value-bind (channel mask whoset)
3940 (cdr (erc-response.command-args parsed))
3941 ;; Determine to which buffer the message corresponds
3942 (let ((buffer (erc-get-buffer channel proc)))
3943 (with-current-buffer buffer
3944 (unless (member (cons whoset mask) erc-channel-banlist)
3945 (setq erc-channel-banlist (cons (cons whoset mask)
3946 erc-channel-banlist))))))
3947 nil)
3949 (defun erc-banlist-finished (proc parsed)
3950 "Record that we have received the banlist."
3951 (let* ((channel (second (erc-response.command-args parsed)))
3952 (buffer (erc-get-buffer channel proc)))
3953 (with-current-buffer buffer
3954 (put 'erc-channel-banlist 'received-from-server t)))
3955 t) ; suppress the 'end of banlist' message
3957 (defun erc-banlist-update (proc parsed)
3958 "Check MODE commands for bans and update the banlist appropriately."
3959 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
3960 (let* ((tgt (first (erc-response.command-args parsed)))
3961 (mode (erc-response.contents parsed))
3962 (whoset (erc-response.sender parsed))
3963 (buffer (erc-get-buffer tgt proc)))
3964 (when buffer
3965 (with-current-buffer buffer
3966 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
3967 ((string-match "^\\([+-]\\)b" mode)
3968 ;; This is a ban
3969 (cond
3970 ((string-match "^-" mode)
3971 ;; Remove the unbanned masks from the ban list
3972 (setq erc-channel-banlist
3973 (erc-delete-if
3974 #'(lambda (y)
3975 (member (upcase (cdr y))
3976 (mapcar #'upcase
3977 (cdr (split-string mode)))))
3978 erc-channel-banlist)))
3979 ((string-match "^+" mode)
3980 ;; Add the banned mask(s) to the ban list
3981 (mapc
3982 (lambda (mask)
3983 (unless (member (cons whoset mask) erc-channel-banlist)
3984 (setq erc-channel-banlist
3985 (cons (cons whoset mask) erc-channel-banlist))))
3986 (cdr (split-string mode))))))))))
3987 nil)
3989 ;; used for the banlist cmds
3990 (defun erc-group-list (list n)
3991 "Group LIST into sublists of length N."
3992 (cond ((null list) nil)
3993 ((null (nthcdr n list)) (list list))
3994 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
3997 ;;; MOTD numreplies
3999 (defun erc-handle-login ()
4000 "Handle the logging in process of connection."
4001 (unless erc-logged-in
4002 (setq erc-logged-in t)
4003 (message "Logging in as \'%s\'... done" (erc-current-nick))
4004 ;; execute a startup script
4005 (let ((f (erc-select-startup-file)))
4006 (when f
4007 (erc-load-script f)))))
4009 (defun erc-connection-established (proc parsed)
4010 "Run just after connection.
4012 Set user modes and run `erc-after-connect hook'."
4013 (unless erc-server-connected ; only once per session
4014 (let ((server (or erc-server-announced-name (erc-response.sender parsed)))
4015 (nick (car (erc-response.command-args parsed ))))
4016 (setq erc-server-connected t)
4017 (erc-update-mode-line)
4018 (erc-set-initial-user-mode nick)
4019 (erc-server-setup-periodical-server-ping)
4020 (run-hook-with-args 'erc-after-connect server nick))))
4022 (defun erc-set-initial-user-mode (nick)
4023 "If `erc-user-mode' is non-nil for NICK, set the user modes."
4024 (when erc-user-mode
4025 (let ((mode (if (functionp erc-user-mode)
4026 (funcall erc-user-mode)
4027 erc-user-mode)))
4028 (when (stringp mode)
4029 (erc-log (format "changing mode for %s to %s" nick mode))
4030 (erc-server-send (format "MODE %s %s" nick mode))))))
4032 (defun erc-display-error-notice (parsed string)
4033 "Display STRING as an error notice.
4035 See also `erc-display-message'."
4036 (erc-display-message
4037 parsed '(notice error) 'active string))
4039 (defun erc-process-ctcp-query (proc parsed nick login host)
4040 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4041 "Process a CTCP query."
4042 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4043 "\C-a"))))
4044 (if (> (length queries) 4)
4045 (erc-display-message
4046 parsed (list 'notice 'error) proc 'ctcp-too-many)
4047 (if (= 0 (length queries))
4048 (erc-display-message
4049 parsed (list 'notice 'error) proc
4050 'ctcp-empty ?n nick)
4051 (while queries
4052 (let* ((type (upcase (car (split-string (car queries)))))
4053 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4054 (if (and hook (boundp hook))
4055 (if (string-equal type "ACTION")
4056 (run-hook-with-args-until-success
4057 hook proc parsed nick login host
4058 (car (erc-response.command-args parsed))
4059 (car queries))
4060 (when erc-paranoid
4061 (if (erc-current-nick-p
4062 (car (erc-response.command-args parsed)))
4063 (erc-display-message
4064 parsed 'error 'active 'ctcp-request
4065 ?n nick ?u login ?h host ?r (car queries))
4066 (erc-display-message
4067 parsed 'error 'active 'ctcp-request-to
4068 ?n nick ?u login ?h host ?r (car queries)
4069 ?t (car (erc-response.command-args parsed)))))
4070 (run-hook-with-args-until-success
4071 hook proc nick login host
4072 (car (erc-response.command-args parsed))
4073 (car queries)))
4074 (erc-display-message
4075 parsed (list 'notice 'error) proc
4076 'undefined-ctcp)))
4077 (setq queries (cdr queries)))))))
4079 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4081 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4082 "Respond to a CTCP ACTION query."
4083 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4084 (let ((s (match-string 1 msg))
4085 (buf (or (erc-get-buffer to proc)
4086 (erc-get-buffer nick proc)
4087 (process-buffer proc))))
4088 (erc-display-message
4089 parsed 'action buf
4090 'ACTION ?n nick ?u login ?h host ?a s))))
4092 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4094 (defun erc-ctcp-query-CLIENTINFO (proc nick login host to msg)
4095 "Respond to a CTCP CLIENTINFO query."
4096 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4097 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4098 (unless erc-disable-ctcp-replies
4099 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4100 nil)
4102 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4103 (defun erc-ctcp-query-ECHO (proc nick login host to msg)
4104 "Respond to a CTCP ECHO query."
4105 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4106 (let ((s (match-string 1 msg)))
4107 (unless erc-disable-ctcp-replies
4108 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4109 nil)
4111 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4112 (defun erc-ctcp-query-FINGER (proc nick login host to msg)
4113 "Respond to a CTCP FINGER query."
4114 (unless erc-disable-ctcp-replies
4115 (let ((s (if erc-anonymous-login
4116 (format "FINGER I'm %s." (erc-current-nick))
4117 (format "FINGER %s (%s@%s)."
4118 (user-full-name)
4119 (user-login-name)
4120 (system-name))))
4121 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4122 (when (> ns 0)
4123 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4124 (erc-send-ctcp-notice nick s)))
4125 nil)
4127 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4128 (defun erc-ctcp-query-PING (proc nick login host to msg)
4129 "Respond to a CTCP PING query."
4130 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4131 (unless erc-disable-ctcp-replies
4132 (let ((arg (match-string 1 msg)))
4133 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4134 nil)
4136 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4137 (defun erc-ctcp-query-TIME (proc nick login host to msg)
4138 "Respond to a CTCP TIME query."
4139 (unless erc-disable-ctcp-replies
4140 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4141 nil)
4143 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4144 (defun erc-ctcp-query-USERINFO (proc nick login host to msg)
4145 "Respond to a CTCP USERINFO query."
4146 (unless erc-disable-ctcp-replies
4147 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4148 nil)
4150 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4151 (defun erc-ctcp-query-VERSION (proc nick login host to msg)
4152 "Respond to a CTCP VERSION query."
4153 (unless erc-disable-ctcp-replies
4154 (erc-send-ctcp-notice
4155 nick (format
4156 "VERSION \C-bERC\C-b %s - an IRC client for emacs (\C-b%s\C-b)"
4157 erc-version-string
4158 erc-official-location)))
4159 nil)
4161 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4162 "Process MSG as a CTCP reply."
4163 (let* ((type (car (split-string msg)))
4164 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4165 (if (boundp hook)
4166 (run-hook-with-args-until-success
4167 hook proc nick login host
4168 (car (erc-response.command-args parsed)) msg)
4169 (erc-display-message
4170 parsed 'notice 'active
4171 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4173 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4174 (defun erc-ctcp-reply-ECHO (proc nick login host to msg)
4175 "Handle a CTCP ECHO reply."
4176 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4177 (let ((message (match-string 1 msg)))
4178 (erc-display-message
4179 nil '(notice action) 'active
4180 'CTCP-ECHO ?n nick ?m message)))
4181 nil)
4183 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4184 (defun erc-ctcp-reply-CLIENTINFO (proc nick login host to msg)
4185 "Handle a CTCP CLIENTINFO reply."
4186 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4187 (let ((message (match-string 1 msg)))
4188 (erc-display-message
4189 nil 'notice 'active
4190 'CTCP-CLIENTINFO ?n nick ?m message)))
4191 nil)
4193 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4194 (defun erc-ctcp-reply-FINGER (proc nick login host to msg)
4195 "Handle a CTCP FINGER reply."
4196 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4197 (let ((message (match-string 1 msg)))
4198 (erc-display-message
4199 nil 'notice 'active
4200 'CTCP-FINGER ?n nick ?m message)))
4201 nil)
4203 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4204 (defun erc-ctcp-reply-PING (proc nick login host to msg)
4205 "Handle a CTCP PING reply."
4206 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4208 (let ((time (match-string 1 msg)))
4209 (condition-case nil
4210 (let ((delta (erc-time-diff (string-to-number time)
4211 (erc-current-time))))
4212 (erc-display-message
4213 nil 'notice 'active
4214 'CTCP-PING ?n nick
4215 ?t (erc-sec-to-time delta)))
4216 (range-error
4217 (erc-display-message
4218 nil 'error 'active
4219 'bad-ping-response ?n nick ?t time))))))
4221 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4222 (defun erc-ctcp-reply-TIME (proc nick login host to msg)
4223 "Handle a CTCP TIME reply."
4224 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4225 (let ((message (match-string 1 msg)))
4226 (erc-display-message
4227 nil 'notice 'active
4228 'CTCP-TIME ?n nick ?m message)))
4229 nil)
4231 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4232 (defun erc-ctcp-reply-VERSION (proc nick login host to msg)
4233 "Handle a CTCP VERSION reply."
4234 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4235 (let ((message (match-string 1 msg)))
4236 (erc-display-message
4237 nil 'notice 'active
4238 'CTCP-VERSION ?n nick ?m message)))
4239 nil)
4241 (defun erc-process-away (proc away-p)
4242 ;; FIXME: This docstring is AWFUL -- Lawrence 2004-01-08
4243 "Process the user being away, or returning from an away break."
4244 (let ((sessionbuf (process-buffer proc)))
4245 (when sessionbuf
4246 (with-current-buffer sessionbuf
4247 (when erc-away-nickname
4248 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4249 erc-away-nickname away-p))
4250 (erc-cmd-NICK (if away-p
4251 erc-away-nickname
4252 erc-nick)))
4253 (cond
4254 (away-p
4255 (erc-with-all-buffers-of-server proc nil
4256 (setq erc-away (current-time))))
4258 (let ((away-time erc-away))
4259 ;; away must be set to NIL BEFORE sending anything to prevent
4260 ;; an infinite recursion
4261 (erc-with-all-buffers-of-server proc nil
4262 (setq erc-away nil))
4263 (save-excursion
4264 (set-buffer (erc-active-buffer))
4265 (when erc-public-away-p
4266 (erc-send-action
4267 (erc-default-target)
4268 (if away-time
4269 (format "is back (gone for %s)"
4270 (erc-sec-to-time
4271 (erc-time-diff
4272 (erc-emacs-time-to-erc-time away-time)
4273 (erc-current-time))))
4274 "is back")))))))))
4275 (erc-update-mode-line)))
4277 ;;;; List of channel members handling
4279 (defun erc-channel-begin-receiving-names ()
4280 "Internal function.
4282 Used when a channel names list is about to be received. Should
4283 be called with the current buffer set to the channel buffer.
4285 See also `erc-channel-end-receiving-names'."
4286 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4288 (defun erc-channel-end-receiving-names ()
4289 "Internal function.
4291 Used to fix `erc-channel-users' after a channel names list has been
4292 received. Should be called with the current buffer set to the
4293 channel buffer.
4295 See also `erc-channel-begin-receiving-names'."
4296 (maphash (lambda (nick user)
4297 (if (null (gethash nick erc-channel-new-member-names))
4298 (erc-remove-channel-user nick)))
4299 erc-channel-users)
4300 (setq erc-channel-new-member-names nil))
4302 (defun erc-channel-receive-names (names-string)
4303 "This function is for internal use only.
4305 Update `erc-channel-users' according to NAMES-STRING.
4306 NAMES-STRING is a string listing some of the names on the
4307 channel."
4308 (let (names name op voice)
4309 ;; We need to delete "" because in XEmacs, (split-string "a ")
4310 ;; returns ("a" "").
4311 (setq names (delete "" (split-string names-string)))
4312 (let ((erc-channel-members-changed-hook nil))
4313 (dolist (item names)
4314 (cond ((string-match "^@\\(.*\\)$" item)
4315 (setq name (match-string 1 item)
4316 op 'on
4317 voice 'off))
4318 ((string-match "^+\\(.*\\)$" item)
4319 (setq name (match-string 1 item)
4320 op 'off
4321 voice 'on))
4322 (t (setq name item
4323 op 'off
4324 voice 'off)))
4325 (puthash (erc-downcase name) t
4326 erc-channel-new-member-names)
4327 (erc-update-current-channel-member
4328 name name t op voice)))
4329 (run-hooks 'erc-channel-members-changed-hook)))
4331 (defcustom erc-channel-members-changed-hook nil
4332 "*This hook is called every time the variable `channel-members' changes.
4333 The buffer where the change happened is current while this hook is called."
4334 :group 'erc-hooks
4335 :type 'hook)
4337 (defun erc-update-user-nick (nick &optional new-nick
4338 host login full-name info)
4339 "Updates the stored user information for the user with nickname
4340 NICK.
4342 See also: `erc-update-user'"
4343 (erc-update-user (erc-get-server-user nick) new-nick
4344 host login full-name info))
4346 (defun erc-update-user (user &optional new-nick
4347 host login full-name info)
4348 "Update user info for USER. USER must be an erc-server-user
4349 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4350 non-nil and not equal to the existing values for USER are used to
4351 replace the stored values in USER.
4353 If, any only if a change is made,
4354 `erc-channel-members-changed-hook' is run for each channel for
4355 which USER is a member, and `t' is returned."
4356 (let (changed)
4357 (when user
4358 (when (and new-nick
4359 (not (equal (erc-server-user-nickname user)
4360 new-nick)))
4361 (setq changed t)
4362 (erc-change-user-nickname user new-nick))
4363 (when (and host
4364 (not (equal (erc-server-user-host user) host)))
4365 (setq changed t)
4366 (setf (erc-server-user-host user) host))
4367 (when (and login
4368 (not (equal (erc-server-user-login user) login)))
4369 (setq changed t)
4370 (setf (erc-server-user-login user) login))
4371 (when (and full-name
4372 (not (equal (erc-server-user-full-name user)
4373 full-name)))
4374 (setq changed t)
4375 (setf (erc-server-user-full-name user) full-name))
4376 (when (and info
4377 (not (equal (erc-server-user-info user) info)))
4378 (setq changed t)
4379 (setf (erc-server-user-info user) info))
4380 (if changed
4381 (dolist (buf (erc-server-user-buffers user))
4382 (if (buffer-live-p buf)
4383 (with-current-buffer buf
4384 (run-hooks 'erc-channel-members-changed-hook))))))
4385 changed))
4387 (defun erc-update-current-channel-member
4388 (nick new-nick &optional add op voice host login full-name info
4389 update-message-time)
4390 "Updates the stored user information for the user with nickname
4391 NICK. `erc-update-user' is called to handle changes to nickname,
4392 host, login, full-name, and info. If `op' or `voice' are
4393 non-nil, they must be equal to either `on' or `off', in which
4394 case the operator or voice status of USER in the current channel
4395 is changed accordingly. If `update-message-time' is non-nil, the
4396 last-message-time of the user in the current channel is set
4397 to (current-time).
4399 If ADD is non-nil, the user will be added with the specified
4400 information if it is not already present in the user or channel
4401 lists.
4403 If, and only if, changes are made, or the user is added,
4404 `erc-channel-members-updated-hook' is run, and `t' is returned.
4406 See also: `erc-update-user' and `erc-update-channel-member'."
4407 (let* (changed user-changed
4408 (channel-data (erc-get-channel-user nick))
4409 (cuser (if channel-data (cdr channel-data)))
4410 (user (if channel-data (car channel-data)
4411 (erc-get-server-user nick))))
4412 (if cuser
4413 (progn
4414 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4415 (when (and op
4416 (not (eq (erc-channel-user-op cuser) op)))
4417 (setq changed t)
4418 (setf (erc-channel-user-op cuser)
4419 (cond ((eq op 'on) t)
4420 ((eq op 'off) nil)
4421 (t op))))
4422 (when (and voice
4423 (not (eq (erc-channel-user-voice cuser) voice)))
4424 (setq changed t)
4425 (setf (erc-channel-user-voice cuser)
4426 (cond ((eq voice 'on) t)
4427 ((eq voice 'off) nil)
4428 (t voice))))
4429 (when update-message-time
4430 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4431 (setq user-changed
4432 (erc-update-user user new-nick
4433 host login full-name info)))
4434 (when add
4435 (if (null user)
4436 (progn
4437 (setq user (make-erc-server-user
4438 :nickname nick
4439 :host host
4440 :full-name full-name
4441 :login login
4442 :info info
4443 :buffers (list (current-buffer))))
4444 (erc-add-server-user nick user))
4445 (setf (erc-server-user-buffers user)
4446 (cons (current-buffer)
4447 (erc-server-user-buffers user))))
4448 (setq cuser (make-erc-channel-user
4449 :op (cond ((eq op 'on) t)
4450 ((eq op 'off) nil)
4451 (t op))
4452 :voice (cond ((eq voice 'on) t)
4453 ((eq voice 'off) nil)
4454 (t voice))
4455 :last-message-time
4456 (if update-message-time (current-time))))
4457 (puthash (erc-downcase nick) (cons user cuser)
4458 erc-channel-users)
4459 (setq changed t)))
4460 (when (and changed (null user-changed))
4461 (run-hooks 'erc-channel-members-changed-hook))
4462 (or changed user-changed add)))
4464 (defun erc-update-channel-member (channel nick new-nick
4465 &optional add op voice host login
4466 full-name info update-message-time)
4467 "Updates user and channel information for the user with
4468 nickname NICK in channel CHANNEL.
4470 See also: `erc-update-current-channel-member'"
4471 (erc-with-buffer
4472 (channel)
4473 (erc-update-current-channel-member nick new-nick add op voice host
4474 login full-name info
4475 update-message-time)))
4477 (defun erc-remove-current-channel-member (nick)
4478 "Remove NICK from current channel membership list. Runs
4479 `erc-channel-members-changed-hook'."
4480 (let ((channel-data (erc-get-channel-user nick)))
4481 (when channel-data
4482 (erc-remove-channel-user nick)
4483 (run-hooks 'erc-channel-members-changed-hook))))
4485 (defun erc-remove-channel-member (channel nick)
4486 "Remove NICK from CHANNEL's membership list.
4488 See also `erc-remove-current-channel-member'."
4489 (erc-with-buffer
4490 (channel)
4491 (erc-remove-current-channel-member nick)))
4493 (defun erc-update-channel-topic (channel topic &optional modify)
4494 "Find a buffer for CHANNEL and set the TOPIC for it.
4496 If optional MODIFY is 'append or 'prepend, then append or prepend the
4497 TOPIC string to the current topic."
4498 (erc-with-buffer (channel)
4499 (cond ((eq modify 'append)
4500 (setq erc-channel-topic (concat erc-channel-topic topic)))
4501 ((eq modify 'prepend)
4502 (setq erc-channel-topic (concat topic erc-channel-topic)))
4503 (t (setq erc-channel-topic topic)))
4504 (erc-update-mode-line-buffer (current-buffer))))
4506 (defun erc-set-modes (tgt mode-string)
4507 "Set the modes for the TGT provided as MODE-STRING."
4508 (let* ((modes (erc-parse-modes mode-string))
4509 (add-modes (nth 0 modes))
4510 (remove-modes (nth 1 modes))
4511 ;; list of triples: (mode-char 'on/'off argument)
4512 (arg-modes (nth 2 modes)))
4513 (cond ((erc-channel-p tgt); channel modes
4514 (let ((buf (and (boundp 'erc-server-process) erc-server-process
4515 (erc-get-buffer tgt erc-server-process))))
4516 (when buf
4517 (with-current-buffer buf
4518 (setq erc-channel-modes add-modes)
4519 (setq erc-channel-user-limit nil)
4520 (setq erc-channel-key nil)
4521 (while arg-modes
4522 (let ((mode (nth 0 (car arg-modes)))
4523 (onoff (nth 1 (car arg-modes)))
4524 (arg (nth 2 (car arg-modes))))
4525 (cond ((string-match "^[Ll]" mode)
4526 (erc-update-channel-limit tgt onoff arg))
4527 ((string-match "^[Kk]" mode)
4528 (erc-update-channel-key tgt onoff arg))
4529 (t nil)))
4530 (setq arg-modes (cdr arg-modes)))
4531 (erc-update-mode-line-buffer buf)))))
4532 ;; we do not keep our nick's modes yet
4533 ;;(t (setq erc-user-modes add-modes))
4537 (defun erc-sort-strings (list-of-strings)
4538 "Sort LIST-OF-STRINGS in lexicographic order.
4540 Side-effect free."
4541 (sort (copy-sequence list-of-strings) 'string<))
4543 (defun erc-parse-modes (mode-string)
4544 "Parse MODE-STRING into a list.
4546 Returns a list of three elements:
4548 (ADD-MODES REMOVE-MODES ARG-MODES).
4550 The add-modes and remove-modes are lists of single-character strings
4551 for modes without parameters to add and remove respectively. The
4552 arg-modes is a list of triples of the form:
4554 (MODE-CHAR ON/OFF ARGUMENT)."
4555 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
4556 (let ((chars (mapcar 'char-to-string (match-string 1 mode-string)))
4557 ;; arguments in channel modes
4558 (args-str (match-string 2 mode-string))
4559 (args nil)
4560 (add-modes nil)
4561 (remove-modes nil)
4562 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
4563 (add-p t))
4564 ;; make the argument list
4565 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
4566 (setq args (cons (match-string 1 args-str) args))
4567 (setq args-str (match-string 2 args-str)))
4568 (setq args (nreverse args))
4569 ;; collect what modes changed, and match them with arguments
4570 (while chars
4571 (cond ((string= (car chars) "+") (setq add-p t))
4572 ((string= (car chars) "-") (setq add-p nil))
4573 ((string-match "^[ovbOVB]" (car chars))
4574 (setq arg-modes (cons (list (car chars)
4575 (if add-p 'on 'off)
4576 (if args (car args) nil))
4577 arg-modes))
4578 (if args (setq args (cdr args))))
4579 ((string-match "^[LlKk]" (car chars))
4580 (setq arg-modes (cons (list (car chars)
4581 (if add-p 'on 'off)
4582 (if (and add-p args)
4583 (car args) nil))
4584 arg-modes))
4585 (if (and add-p args) (setq args (cdr args))))
4586 (add-p (setq add-modes (cons (car chars) add-modes)))
4587 (t (setq remove-modes (cons (car chars) remove-modes))))
4588 (setq chars (cdr chars)))
4589 (setq add-modes (nreverse add-modes))
4590 (setq remove-modes (nreverse remove-modes))
4591 (setq arg-modes (nreverse arg-modes))
4592 (list add-modes remove-modes arg-modes))
4593 nil))
4595 (defun erc-update-modes (tgt mode-string &optional nick host login)
4596 "Update the mode information for TGT, provided as MODE-STRING.
4597 Optional arguments: NICK, HOST and LOGIN - the attributes of the
4598 person who changed the modes."
4599 (let* ((modes (erc-parse-modes mode-string))
4600 (add-modes (nth 0 modes))
4601 (remove-modes (nth 1 modes))
4602 ;; list of triples: (mode-char 'on/'off argument)
4603 (arg-modes (nth 2 modes)))
4604 ;; now parse the modes changes and do the updates
4605 (cond ((erc-channel-p tgt); channel modes
4606 (let ((buf (and (boundp 'erc-server-process) erc-server-process
4607 (erc-get-buffer tgt erc-server-process))))
4608 (when buf
4609 ;; FIXME! This used to have an original buffer
4610 ;; variable, but it never switched back to the original
4611 ;; buffer. Is this wanted behavior?
4612 (set-buffer buf)
4613 (if (not (boundp 'erc-channel-modes))
4614 (setq erc-channel-modes nil))
4615 (while remove-modes
4616 (setq erc-channel-modes (delete (car remove-modes)
4617 erc-channel-modes)
4618 remove-modes (cdr remove-modes)))
4619 (while add-modes
4620 (setq erc-channel-modes (cons (car add-modes)
4621 erc-channel-modes)
4622 add-modes (cdr add-modes)))
4623 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
4624 (while arg-modes
4625 (let ((mode (nth 0 (car arg-modes)))
4626 (onoff (nth 1 (car arg-modes)))
4627 (arg (nth 2 (car arg-modes))))
4628 (cond ((string-match "^[oO]" mode)
4629 (erc-update-channel-member tgt arg arg nil onoff))
4630 ((string-match "^[Vv]" mode)
4631 (erc-update-channel-member tgt arg arg nil nil
4632 onoff))
4633 ((string-match "^[Ll]" mode)
4634 (erc-update-channel-limit tgt onoff arg))
4635 ((string-match "^[Kk]" mode)
4636 (erc-update-channel-key tgt onoff arg))
4637 (t nil)); only ops are tracked now
4638 (setq arg-modes (cdr arg-modes))))
4639 (erc-update-mode-line buf))))
4640 ;; nick modes - ignored at this point
4641 (t nil))))
4643 (defun erc-update-channel-limit (channel onoff n)
4644 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
4645 "Update CHANNEL's user limit to N."
4646 (if (or (not (eq onoff 'on))
4647 (and (stringp n) (string-match "^[0-9]+$" n)))
4648 (erc-with-buffer
4649 (channel)
4650 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
4651 (t (setq erc-channel-user-limit nil))))))
4653 (defun erc-update-channel-key (channel onoff key)
4654 "Update CHANNEL's key to KEY if ONOFF is 'on or to nil if it's 'off."
4655 (erc-with-buffer
4656 (channel)
4657 (cond ((eq onoff 'on) (setq erc-channel-key key))
4658 (t (setq erc-channel-key nil)))))
4660 (defun erc-handle-user-status-change (type nlh &optional l)
4661 "Handle changes in any user's status.
4663 So far, only nick change is handled.
4665 Generally, the TYPE argument is a symbol describing the change type, NLH is
4666 a list containing the original nickname, login name and hostname for the user,
4667 and L is a list containing additional TYPE-specific arguments.
4669 So far the following TYPE/L pairs are supported:
4671 Event TYPE L
4673 nickname change 'nick (NEW-NICK)"
4674 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
4675 (cond
4676 ;; nickname change
4677 ((equal type 'nick)
4680 nil)))
4682 (defun erc-highlight-notice (s)
4683 "Highlight notice message S and return it.
4684 See also variable `erc-notice-highlight-type'"
4685 (cond
4686 ((eq erc-notice-highlight-type 'prefix)
4687 (erc-put-text-property 0 (length erc-notice-prefix)
4688 'face 'erc-notice-face s)
4690 ((eq erc-notice-highlight-type 'all)
4691 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
4693 (t s)))
4695 (defun erc-make-notice (message)
4696 "Notify the user of MESSAGE."
4697 (when erc-minibuffer-notice
4698 (message "%s" message))
4699 (erc-highlight-notice (concat erc-notice-prefix message)))
4701 (defun erc-highlight-error (s)
4702 "Highlight error message S and return it."
4703 (erc-put-text-property 0 (length s) 'face 'erc-error-face s)
4706 (defun erc-put-text-property (start end property value &optional object)
4707 "Set text-property for an object (usually a string).
4708 START and END define the characters covered.
4709 PROPERTY is the text-property set, usually the symbol `face'.
4710 VALUE is the value for the text-property, usually a face symbol such as
4711 the face `bold' or `erc-pal-face'.
4712 OBJECT is a string which will be modified and returned.
4713 OBJECT is modified without being copied first.
4715 You can redefine or `defadvice' this function in order to add
4716 EmacsSpeak support."
4717 (put-text-property start end property value object))
4719 (defun erc-list (thing)
4720 "Return THING if THING is a list, or a list with THING as its element."
4721 (if (listp thing)
4722 thing
4723 (list thing)))
4725 (defun erc-parse-user (string)
4726 "Parse STRING as a user specification (nick!login@host).
4728 Return a list of the three separate tokens."
4729 (cond
4730 ((string-match "^\\([^!]*\\)!\\([^@]*\\)@\\(.*\\)$" string)
4731 (list (match-string 1 string)
4732 (match-string 2 string)
4733 (match-string 3 string)))
4734 ;; Some bogus bouncers send Nick!(null), try to live with that.
4735 ((string-match "^\\([^!]*\\)!\\(.*\\)$" string)
4736 (list (match-string 1 string)
4738 (match-string 2 string)))
4740 (list string "" ""))))
4742 (defun erc-extract-nick (string)
4743 "Return the nick corresponding to a user specification STRING.
4745 See also `erc-parse-user'."
4746 (car (erc-parse-user string)))
4748 (defun erc-put-text-properties (start end properties
4749 &optional object value-list)
4750 "Set text-properties for OBJECT.
4752 START and END describe positions in OBJECT.
4753 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
4754 each property to the corresponding value in VALUE-LIST."
4755 (unless value-list
4756 (setq value-list (mapcar (lambda (x)
4758 properties)))
4759 (mapcar* (lambda (prop value)
4760 (erc-put-text-property start end prop value object))
4761 properties value-list))
4763 ;;; Input area handling:
4765 (defun erc-beg-of-input-line ()
4766 "Return the value of `point' at the beginning of the input line.
4768 Specifically, return the position of `erc-insert-marker'."
4769 (or (and (boundp 'erc-insert-marker)
4770 (markerp erc-insert-marker))
4771 (error "erc-insert-marker has no value, please report a bug"))
4772 (marker-position erc-insert-marker))
4774 (defun erc-end-of-input-line ()
4775 "Return the value of `point' at the end of the input line."
4776 (point-max))
4778 (defun erc-send-current-line ()
4779 "Parse current line and send it to IRC."
4780 (interactive)
4781 (save-restriction
4782 (widen)
4783 (cond
4784 ((< (point) (erc-beg-of-input-line))
4785 (message "Point is not in the input area")
4786 (beep))
4787 ((not (erc-server-buffer-live-p))
4788 (message "ERC: No process running")
4789 (beep))
4791 (erc-set-active-buffer (current-buffer))
4792 (let ((inhibit-read-only t)
4793 (str (erc-user-input))
4794 (old-buf (current-buffer)))
4796 ;; Kill the input and the prompt
4797 (delete-region (erc-beg-of-input-line)
4798 (erc-end-of-input-line))
4800 (unwind-protect
4801 (erc-send-input str)
4802 ;; Fix the buffer if the command didn't kill it
4803 (when (buffer-live-p old-buf)
4804 (with-current-buffer old-buf
4805 (save-restriction
4806 (widen)
4807 (goto-char (point-max))
4808 (set-marker (process-mark erc-server-process) (point))
4809 (set-marker erc-insert-marker (point))
4810 (let ((buffer-modified (buffer-modified-p)))
4811 (erc-display-prompt)
4812 (set-buffer-modified-p buffer-modified))))))
4814 ;; Only when last hook has been run...
4815 (run-hook-with-args 'erc-send-completed-hook str))))))
4817 (defun erc-user-input ()
4818 "Return the input of the user in the current buffer."
4819 (buffer-substring
4820 erc-input-marker
4821 (erc-end-of-input-line)))
4823 (defun erc-send-input (input)
4824 "Treat INPUT as typed in by the user. It is assumed that the input
4825 and the prompt is already deleted.
4826 This returns non-nil only iff we actually send anything."
4827 ;; Handle different kinds of inputs
4828 (cond
4829 ;; Ignore empty input
4830 ((if erc-send-whitespace-lines
4831 (string= input "")
4832 (string-match "\\`[ \t\r\f\n]*\\'" input))
4833 (when erc-warn-about-blank-lines
4834 (message "Blank line - ignoring...")
4835 (beep))
4836 nil)
4838 (let ((str input)
4839 (erc-insert-this t))
4840 (setq erc-send-this t)
4841 (run-hook-with-args 'erc-send-pre-hook input)
4842 (when erc-send-this
4843 (if (or (string-match "\n" str)
4844 (not (char-equal (aref str 0) ?/)))
4845 (mapc
4846 (lambda (line)
4847 (mapc
4848 (lambda (line)
4849 ;; Insert what has to be inserted for this.
4850 (erc-display-msg line)
4851 (erc-process-input-line (concat line "\n")
4852 (null erc-flood-protect) t))
4853 (erc-split-line line)))
4854 (split-string str "\n"))
4855 ;; Insert the prompt along with the command.
4856 (erc-display-command str)
4857 (erc-process-input-line (concat str "\n") t nil))
4858 t)))))
4860 (defun erc-display-command (line)
4861 (when erc-insert-this
4862 (let ((insert-position (point)))
4863 (unless erc-hide-prompt
4864 (erc-display-prompt nil nil (erc-command-indicator)
4865 (and (erc-command-indicator)
4866 'erc-command-indicator-face)))
4867 (let ((beg (point)))
4868 (insert line)
4869 (erc-put-text-property beg (point)
4870 'face 'erc-command-indicator-face)
4871 (insert "\n"))
4872 (set-marker (process-mark erc-server-process) (point))
4873 (set-marker erc-insert-marker (point))
4874 (save-excursion
4875 (save-restriction
4876 (narrow-to-region insert-position (point))
4877 (run-hooks 'erc-send-modify-hook)
4878 (run-hooks 'erc-send-post-hook))))))
4880 (defun erc-display-msg (line)
4881 "Display LINE as a message of the user to the current target at the
4882 current position."
4883 (when erc-insert-this
4884 (let ((insert-position (point)))
4885 (insert (erc-format-my-nick))
4886 (let ((beg (point)))
4887 (insert line)
4888 (erc-put-text-property beg (point)
4889 'face 'erc-input-face))
4890 (insert "\n")
4891 (set-marker (process-mark erc-server-process) (point))
4892 (set-marker erc-insert-marker (point))
4893 (save-excursion
4894 (save-restriction
4895 (narrow-to-region insert-position (point))
4896 (run-hooks 'erc-send-modify-hook)
4897 (run-hooks 'erc-send-post-hook))))))
4899 (defun erc-command-symbol (command)
4900 "Return the erc command symbol for COMMAND if it exists and is bound."
4901 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
4902 (when (fboundp cmd) cmd)))
4904 (defun erc-extract-command-from-line (line)
4905 "Extract command and args from the input LINE.
4906 If no command was given, return nil. If command matches, return a
4907 list of the form: (command args) where both elements are strings."
4908 (when (string-match "^/\\([A-Za-z]+\\)\\(\\s-+.*\\|\\s-*\\)$" line)
4909 (let* ((cmd (erc-command-symbol (match-string 1 line)))
4910 ;; note: return is nil, we apply this simply for side effects
4911 (canon-defun (while (and cmd (symbolp (symbol-function cmd)))
4912 (setq cmd (symbol-function cmd))))
4913 (cmd-fun (or cmd #'erc-cmd-default))
4914 (arg (if cmd
4915 (if (get cmd-fun 'do-not-parse-args)
4916 (format "%s" (match-string 2 line))
4917 (delete "" (split-string (erc-trim-string
4918 (match-string 2 line)) " ")))
4919 line)))
4920 (list cmd-fun arg))))
4922 (defun erc-split-multiline-safe (string)
4923 "Split STRING, containing multiple lines and return them in a list.
4924 Do it only for STRING as the complete input, do not carry unfinished
4925 strings over to the next call."
4926 (let ((l ())
4927 (i0 0)
4928 (doit t))
4929 (while doit
4930 (let ((i (string-match "\r?\n" string i0))
4931 (s (substring string i0)))
4932 (cond (i (setq l (cons (substring string i0 i) l))
4933 (setq i0 (match-end 0)))
4934 ((> (length s) 0)
4935 (setq l (cons s l))(setq doit nil))
4936 (t (setq doit nil)))))
4937 (nreverse l)))
4939 ;; nick handling
4941 (defun erc-set-current-nick (nick)
4942 "Set the current nickname to NICK."
4943 (with-current-buffer (or (erc-server-buffer)
4944 (current-buffer))
4945 (setq erc-server-current-nick nick)))
4947 (defun erc-current-nick ()
4948 "Return the current nickname."
4949 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
4950 (erc-server-buffer)
4951 (current-buffer))
4952 erc-server-current-nick))
4954 (defun erc-current-nick-p (nick)
4955 "Return non-nil if NICK is the current nickname."
4956 (erc-nick-equal-p nick (erc-current-nick)))
4958 (defun erc-nick-equal-p (nick1 nick2)
4959 "Return non-nil if NICK1 and NICK2 are the same.
4961 This matches strings according to the IRC protocol's case convention.
4963 See also `erc-downcase'."
4964 (string= (erc-downcase nick1)
4965 (erc-downcase nick2)))
4967 ;; default target handling
4969 (defun erc-default-target ()
4970 "Return the current default target (as a character string) or nil if none."
4971 (let ((tgt (car erc-default-recipients)))
4972 (cond
4973 ((not tgt) nil)
4974 ((listp tgt) (cdr tgt))
4975 (t tgt))))
4977 (defun erc-add-default-channel (channel)
4978 "Add CHANNEL to the default channel list."
4980 (let ((d1 (car erc-default-recipients))
4981 (d2 (cdr erc-default-recipients))
4982 (chl (downcase channel)))
4983 (setq erc-default-recipients
4984 (cons chl erc-default-recipients))))
4986 (defun erc-delete-default-channel (channel &optional buffer)
4987 "Delete CHANNEL from the default channel list."
4988 (let ((ob (current-buffer)))
4989 (with-current-buffer (if (and buffer
4990 (bufferp buffer))
4991 buffer
4992 (current-buffer))
4993 (setq erc-default-recipients (delete (downcase channel)
4994 erc-default-recipients)))))
4996 (defun erc-add-query (nickname)
4997 "Add QUERY'd NICKNAME to the default channel list.
4999 The previous default target of QUERY type gets removed"
5000 (let ((d1 (car erc-default-recipients))
5001 (d2 (cdr erc-default-recipients))
5002 (qt (cons 'QUERY (downcase nickname))))
5003 (if (and (listp d1)
5004 (eq (car d1) 'QUERY))
5005 (setq erc-default-recipients (cons qt d2))
5006 (setq erc-default-recipients (cons qt erc-default-recipients)))))
5008 (defun erc-delete-query ()
5009 "Delete the topmost target if it is a QUERY."
5011 (let ((d1 (car erc-default-recipients))
5012 (d2 (cdr erc-default-recipients)))
5013 (if (and (listp d1)
5014 (eq (car d1) 'QUERY))
5015 (setq erc-default-recipients d2)
5016 (error "Current target is not a QUERY"))))
5018 (defun erc-ignored-user-p (spec)
5019 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5021 Takes a full SPEC of a user in the form \"nick!login@host\", and
5022 matches against all the regexp's in `erc-ignore-list'. If any
5023 match, returns that regexp."
5024 (dolist (ignored (with-current-buffer (erc-server-buffer) erc-ignore-list))
5025 (if (string-match ignored spec)
5026 ;; We have `require'd cl, so we can return from the block named nil
5027 (return ignored))))
5029 (defun erc-ignored-reply-p (msg tgt proc)
5030 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5031 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5033 Takes a message MSG to a channel and returns non-nil if the addressed
5034 user matches any regexp in `erc-ignore-reply-list'."
5035 (let ((target-nick (erc-message-target msg)))
5036 (if (not target-nick)
5038 (erc-with-buffer (tgt proc)
5039 (let ((user (erc-get-server-user target-nick)))
5040 (when user
5041 (erc-list-match erc-ignore-reply-list
5042 (erc-user-spec user))))))))
5044 (defun erc-message-target (msg)
5045 "Return the addressed target in MSG.
5047 The addressed target is the string before the first colon in MSG."
5048 (if (string-match "^\\([^: ]*\\):" msg)
5049 (match-string 1 msg)
5050 nil))
5052 (defun erc-user-spec (user)
5053 "Create a nick!user@host spec from a user struct."
5054 (let ((nick (erc-server-user-nickname user))
5055 (host (erc-server-user-host user))
5056 (login (erc-server-user-login user)))
5057 (concat (if nick
5058 nick
5061 (if login
5062 login
5065 (if host
5066 host
5067 ""))))
5069 (defun erc-list-match (lst str)
5070 "Return non-nil if any regexp in LST matches STR."
5071 (memq nil (mapcar (lambda (regexp)
5072 (not (string-match regexp str)))
5073 lst)))
5075 ;; other "toggles"
5077 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5078 "Toggle automatic CTCP replies (like VERSION and PING).
5080 If ARG is positive, turns CTCP replies on.
5082 If ARG is non-nil and not positive, turns CTCP replies off."
5083 (interactive "P")
5084 (cond ((and (numberp arg) (> arg 0))
5085 (setq erc-disable-ctcp-replies t))
5086 (arg (setq erc-disable-ctcp-replies nil))
5087 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5088 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5090 (defun erc-toggle-flood-control (&optional arg)
5091 "Toggle use of flood control on sent messages.
5093 If ARG is non-nil, use flood control.
5094 If ARG is nil, do not use flood control.
5096 See `erc-server-flood-margin' for an explanation of the available
5097 flood control parameters."
5098 (interactive "P")
5099 (setq erc-flood-protect arg)
5100 (message "ERC flood control is %s"
5101 (cond (erc-flood-protect "ON")
5102 (t "OFF"))))
5104 ;; Some useful channel and nick commands for fast key bindings
5106 (defun erc-invite-only-mode (&optional arg)
5107 "Turn on the invite only mode (+i) for the current channel.
5109 If ARG is non-nil, turn this mode off (-i).
5111 This command is sent even if excess flood is detected."
5112 (interactive "P")
5113 (erc-set-active-buffer (current-buffer))
5114 (let ((tgt (erc-default-target))
5115 (erc-force-send t))
5116 (cond ((or (not tgt) (not (erc-channel-p tgt)))
5117 (erc-display-message nil 'error (current-buffer) 'no-target))
5118 (arg (erc-load-irc-script-lines (list (concat "/mode " tgt " -i"))
5120 (t (erc-load-irc-script-lines (list (concat "/mode " tgt " +i"))
5121 t)))))
5123 (defun erc-get-channel-mode-from-keypress (key)
5124 "Read a key sequence and call the corresponding channel mode function.
5125 After doing C-c C-o type in a channel mode letter.
5127 C-g means quit.
5128 RET let's you type more than one mode at a time.
5129 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5130 If \"k\" is pressed, `erc-set-channel-key' gets called.
5131 Anything else will be sent to `erc-toggle-channel-mode'."
5132 (interactive "kChannel mode (RET to set more than one): ")
5133 (when (featurep 'xemacs)
5134 (setq key (char-to-string (event-to-character (aref key 0)))))
5135 (cond ((equal key "\C-g")
5136 (keyboard-quit))
5137 ((equal key "\C-m")
5138 (erc-insert-mode-command))
5139 ((equal key "l")
5140 (call-interactively 'erc-set-channel-limit))
5141 ((equal key "k")
5142 (call-interactively 'erc-set-channel-key))
5143 (t (erc-toggle-channel-mode key))))
5145 (defun erc-toggle-channel-mode (mode &optional channel)
5146 "Toggle channel MODE.
5148 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5149 `erc-default-target'."
5150 (interactive "P")
5151 (erc-set-active-buffer (current-buffer))
5152 (let ((tgt (or channel (erc-default-target)))
5153 (erc-force-send t))
5154 (cond ((or (null tgt) (null (erc-channel-p tgt)))
5155 (erc-display-message nil 'error 'active 'no-target))
5156 ((member mode erc-channel-modes)
5157 (erc-log (format "%s: Toggle mode %s OFF" tgt mode))
5158 (message "Toggle channel mode %s OFF" mode)
5159 (erc-server-send (format "MODE %s -%s" tgt mode)))
5160 (t (erc-log (format "%s: Toggle channel mode %s ON" tgt mode))
5161 (message "Toggle channel mode %s ON" mode)
5162 (erc-server-send (format "MODE %s +%s" tgt mode))))))
5164 (defun erc-insert-mode-command ()
5165 "Insert the line \"/mode <current target> \" at `point'."
5166 (interactive)
5167 (let ((tgt (erc-default-target)))
5168 (if tgt (insert (concat "/mode " tgt " "))
5169 (erc-display-message nil 'error (current-buffer) 'no-target))))
5171 (defun erc-channel-names ()
5172 "Run \"/names #channel\" in the current channel."
5173 (interactive)
5174 (erc-set-active-buffer (current-buffer))
5175 (let ((tgt (erc-default-target)))
5176 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5177 (erc-display-message nil 'error (current-buffer) 'no-target))))
5179 (defun erc-remove-text-properties-region (start end &optional object)
5180 "Clears the region (START,END) in OBJECT from all colors, etc."
5181 (interactive "r")
5182 (save-excursion
5183 (let ((inhibit-read-only t))
5184 (set-text-properties start end nil object))))
5186 ;; script execution and startup
5188 (defun erc-find-file (file &optional path)
5189 "Search for a FILE in the filesystem.
5190 First the `default-directory' is searched for FILE, then any directories
5191 specified in the list PATH.
5193 If FILE is found, return the path to it."
5194 (let ((filepath file))
5195 (if (file-readable-p filepath) filepath
5196 (progn
5197 (while (and path
5198 (progn (setq filepath (expand-file-name file (car path)))
5199 (not (file-readable-p filepath))))
5200 (setq path (cdr path)))
5201 (if path filepath nil)))))
5203 (defun erc-select-startup-file ()
5204 "Select an ERC startup file.
5205 See also `erc-startup-file-list'."
5206 (let ((l erc-startup-file-list)
5207 (f nil))
5208 (while (and (not f) l)
5209 (if (file-readable-p (car l))
5210 (setq f (car l)))
5211 (setq l (cdr l)))
5214 (defun erc-find-script-file (file)
5215 "Search for FILE in `default-directory', and any in `erc-script-path'."
5216 (erc-find-file file erc-script-path))
5218 (defun erc-load-script (file)
5219 "Load a script from FILE.
5221 FILE must be the full name, it is not searched in the
5222 `erc-script-path'. If the filename ends with `.el', then load it
5223 as a emacs-lisp program. Otherwise, treat it as a regular IRC
5224 script"
5225 (erc-log (concat "erc-load-script: " file))
5226 (cond
5227 ((string-match "\\.el$" file)
5228 (load file))
5230 (erc-load-irc-script file))))
5232 (defun erc-process-script-line (line &optional args)
5233 "Process an IRC script LINE.
5235 Does script-specific substitutions (script arguments, current nick,
5236 server, etc.) in LINE and returns it.
5238 Substitutions are: %C and %c = current target (channel or nick),
5239 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5240 where x is any other character;
5241 $* = the entire argument string, $1 = the first argument, $2 = the second,
5242 end so on."
5243 (if (not args) (setq args ""))
5244 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5245 (percent-regexp "\\(%.\\)")
5246 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5247 (tgt (erc-default-target))
5248 (server (and (boundp 'erc-session-server) erc-session-server))
5249 (nick (erc-current-nick))
5250 (res "")
5251 (tmp nil)
5252 (arg-list nil)
5253 (arg-num 0))
5254 (if (not tgt) (setq tgt ""))
5255 (if (not server) (setq server ""))
5256 (if (not nick) (setq nick ""))
5257 ;; First, compute the argument list
5258 (setq tmp args)
5259 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5260 (setq arg-list (cons (match-string 1 tmp) arg-list))
5261 (setq tmp (match-string 2 tmp)))
5262 (setq arg-list (nreverse arg-list))
5263 (setq arg-num (length arg-list))
5264 ;; now do the substitution
5265 (setq tmp (string-match esc-regexp line))
5266 (while tmp
5267 ;;(message "beginning of while: tmp=%S" tmp)
5268 (let* ((hd (substring line 0 tmp))
5269 (esc "")
5270 (subst "")
5271 (tail (substring line tmp)))
5272 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5273 (setq esc (match-string 1 tail))
5274 (setq tail (substring tail (match-end 1))))
5275 ((string-match (concat "^" percent-regexp) tail)
5276 (setq esc (match-string 1 tail))
5277 (setq tail (substring tail (match-end 1)))))
5278 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5279 (setq res (concat res hd))
5280 (setq subst
5281 (cond ((string= esc "") "")
5282 ((string-match "^\\$\\*$" esc) args)
5283 ((string-match "^\\$\\([0-9]+\\)$" esc)
5284 (let ((n (string-to-number (match-string 1 esc))))
5285 (message "n = %S, integerp(n)=%S" n (integerp n))
5286 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5287 ((string-match "^%[Cc]$" esc) tgt)
5288 ((string-match "^%[Ss]$" esc) server)
5289 ((string-match "^%[Nn]$" esc) nick)
5290 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5291 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5292 (message "BUG IN ERC: esc=%S" esc)
5293 "")))
5294 (setq line tail)
5295 (setq tmp (string-match esc-regexp line))
5296 (setq res (concat res subst))
5297 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5299 (setq res (concat res line))
5300 res))
5302 (defun erc-load-irc-script (file &optional force)
5303 "Load an IRC script from FILE."
5304 (erc-log (concat "erc-load-script: " file))
5305 (let ((str (with-temp-buffer
5306 (insert-file-contents file)
5307 (buffer-string))))
5308 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5310 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5311 "Load IRC script LINES (a list of strings).
5313 If optional NOEXPAND is non-nil, do not expand script-specific
5314 sequences, process the lines verbatim. Use this for multiline
5315 user input."
5316 (let* ((cb (current-buffer))
5317 (pnt (point))
5318 (s "")
5319 (sp (or (erc-command-indicator) (erc-prompt)))
5320 (args (and (boundp 'erc-script-args) erc-script-args)))
5321 (if (and args (string-match "^ " args))
5322 (setq args (substring args 1)))
5323 ;; prepare the prompt string for echo
5324 (erc-put-text-property 0 (length sp)
5325 'face 'erc-command-indicator-face sp)
5326 (while lines
5327 (setq s (car lines))
5328 (erc-log (concat "erc-load-script: CMD: " s))
5329 (unless (string-match "^\\s-*$" s)
5330 (let ((line (if noexpand s (erc-process-script-line s args))))
5331 (if (and (erc-process-input-line line force)
5332 erc-script-echo)
5333 (progn
5334 (erc-put-text-property 0 (length line)
5335 'face 'erc-input-face line)
5336 (erc-display-line (concat sp line) cb)))))
5337 (setq lines (cdr lines)))))
5339 ;; authentication
5341 (defun erc-login ()
5342 "Perform user authentication at the IRC server."
5343 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5344 (erc-current-nick)
5345 (user-login-name)
5346 (system-name)
5347 erc-session-server
5348 erc-session-user-full-name))
5349 (if erc-session-password
5350 (erc-server-send (format "PASS %s" erc-session-password))
5351 (message "Logging in without password"))
5352 (erc-server-send (format "NICK %s" (erc-current-nick)))
5353 (erc-server-send
5354 (format "USER %s %s %s :%s"
5355 ;; hacked - S.B.
5356 (if erc-anonymous-login erc-email-userid (user-login-name))
5357 "0" "*"
5358 erc-session-user-full-name))
5359 (erc-update-mode-line))
5361 ;; connection properties' heuristics
5363 (defun erc-determine-parameters (&optional server port nick name)
5364 "Determine the connection and authentication parameters.
5365 Sets the buffer local variables:
5367 - erc-session-server
5368 - erc-session-port
5369 - erc-session-full-name
5370 - erc-server-current-nick"
5371 (setq erc-session-server (erc-compute-server server)
5372 erc-session-port (or port erc-default-port)
5373 erc-session-user-full-name (erc-compute-full-name name))
5374 (erc-set-current-nick (erc-compute-nick nick)))
5376 (defun erc-compute-server (&optional server)
5377 "Return an IRC server name.
5379 Tries a number of increasingly more default methods until a non-nil value is
5380 found:
5382 - SERVER
5383 - `erc-server'
5384 - The value of the IRCSERVER environment variable
5385 - `erc-default-server'."
5386 (or server
5387 erc-server
5388 (getenv "IRCSERVER")
5389 erc-default-server))
5391 (defun erc-compute-nick (&optional nick)
5392 "Return user's NICK.
5394 Tries a number of increasingly more default methods until a non-nil value is
5395 found:
5397 - NICK
5398 - `erc-nick'
5399 - The value of the IRCNICK environment variable
5400 - via the function `user-login-name'."
5401 (or nick
5402 (if (consp erc-nick) (car erc-nick) erc-nick)
5403 (getenv "IRCNICK")
5404 (user-login-name)))
5407 (defun erc-compute-full-name (&optional full-name)
5408 "Return user's FULL-NAME.
5410 Tries a number of increasingly more default methods until a non-nil value is
5411 found:
5413 - FULL-NAME
5414 - `erc-user-full-name'
5415 - The value of the IRCNAME environment variable
5416 - via the function `user-full-name'."
5417 (or full-name
5418 erc-user-full-name
5419 (getenv "IRCNAME")
5420 (if erc-anonymous-login "unknown" nil)
5421 (user-full-name)))
5423 (defun erc-compute-port (&optional port)
5424 "Return a port for an IRC server.
5426 Tries a number of increasingly more default methods until a non-nil
5427 value is found:
5429 - PORT
5430 - \"ircd\"."
5431 (or port erc-port "ircd"))
5433 ;; time routines
5435 (defun erc-string-to-emacs-time (string)
5436 "Convert the long number represented by STRING into an Emacs format.
5437 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
5438 (let* ((n (string-to-number (concat string ".0"))))
5439 (list (truncate (/ n 65536))
5440 (truncate (mod n 65536)))))
5442 (defun erc-emacs-time-to-erc-time (time)
5443 "Convert Emacs TIME to a number of seconds since the epoch."
5444 (when time
5445 (+ (* (nth 0 time) 65536.0) (nth 1 time))))
5446 ; (round (+ (* (nth 0 tm) 65536.0) (nth 1 tm))))
5448 (defun erc-current-time ()
5449 "Return the `current-time' as a number of seconds since the epoch.
5451 See also `erc-emacs-time-to-erc-time'."
5452 (erc-emacs-time-to-erc-time (current-time)))
5454 (defun erc-time-diff (t1 t2)
5455 "Return the time difference in seconds between T1 and T2."
5456 (abs (- t2 t1)))
5458 (defun erc-time-gt (t1 t2)
5459 "Check whether T1 > T2."
5460 (> t1 t2))
5462 (defun erc-sec-to-time (ns)
5463 "Convert NS to a time string HH:MM.SS."
5464 (setq ns (truncate ns))
5465 (format "%02d:%02d.%02d"
5466 (/ ns 3600)
5467 (/ (% ns 3600) 60)
5468 (% ns 60)))
5470 (defun erc-seconds-to-string (seconds)
5471 "Convert a number of SECONDS into an English phrase."
5472 (let (days hours minutes format-args output)
5473 (setq days (/ seconds 86400)
5474 seconds (% seconds 86400)
5475 hours (/ seconds 3600)
5476 seconds (% seconds 3600)
5477 minutes (/ seconds 60)
5478 seconds (% seconds 60)
5479 format-args (if (> days 0)
5480 `("%d days, %d hours, %d minutes, %d seconds"
5481 ,days ,hours ,minutes ,seconds)
5482 (if (> hours 0)
5483 `("%d hours, %d minutes, %d seconds"
5484 ,hours ,minutes ,seconds)
5485 (if (> minutes 0)
5486 `("%d minutes, %d seconds" ,minutes ,seconds)
5487 `("%d seconds" ,seconds))))
5488 output (apply 'format format-args))
5489 ;; Change all "1 units" to "1 unit".
5490 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
5491 (setq output (erc-replace-match-subexpression-in-string
5492 "" output (match-string 2 output) 2 (match-beginning 2))))
5493 output))
5496 ;; info
5498 (defconst erc-clientinfo-alist
5499 '(("ACTION" . "is used to inform about one's current activity")
5500 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
5501 ("ECHO" . "echoes its arguments back")
5502 ("FINGER" . "shows user's name, location, and idle time")
5503 ("PING" . "measures delay between peers")
5504 ("TIME" . "shows client-side time")
5505 ("USERINFO" . "shows information provided by a user")
5506 ("VERSION" . "shows client type and version"))
5507 "Alist of CTCP CLIENTINFO for ERC commands.")
5509 (defun erc-client-info (s)
5510 "Return CTCP CLIENTINFO on command S.
5511 If S is NIL or an empty string then return general CLIENTINFO"
5512 (if (or (not s) (string= s ""))
5513 (concat
5514 (apply #'concat
5515 (mapcar (lambda (e)
5516 (concat (car e) " "))
5517 erc-clientinfo-alist))
5518 ": use CLIENTINFO <COMMAND> to get more specific information")
5519 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
5520 (if h
5521 (concat s " " (cdr h))
5522 (concat s ": unknown command")))))
5524 ;; Hook functions
5526 (defun erc-directory-writable-p (dir)
5527 "Determine whether DIR is a writable directory.
5528 If it doesn't exist, create it."
5529 (unless (file-attributes dir) (make-directory dir))
5530 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
5532 (defun erc-kill-query-buffers (process)
5533 "Kill all buffers of PROCESS."
5534 ;; here, we only want to match the channel buffers, to avoid
5535 ;; "selecting killed buffers" b0rkage.
5536 (erc-with-all-buffers-of-server process
5537 (lambda ()
5538 (not (erc-server-buffer-p)))
5539 (kill-buffer (current-buffer))))
5541 (defun erc-nick-at-point ()
5542 "Give information about the nickname at `point'.
5544 If called interactively, give a human readable message in the
5545 minibuffer. If called programatically, return the corresponding
5546 entry of `channel-members'."
5547 (interactive)
5548 (require 'thingatpt)
5549 (let* ((word (word-at-point))
5550 (channel-data (erc-get-channel-user word))
5551 (cuser (cdr channel-data))
5552 (user (if channel-data
5553 (car channel-data)
5554 (erc-get-server-user word)))
5555 host login full-name info nick op voice)
5556 (when user
5557 (setq nick (erc-server-user-nickname user)
5558 host (erc-server-user-host user)
5559 login (erc-server-user-login user)
5560 full-name (erc-server-user-full-name user)
5561 info (erc-server-user-info user))
5562 (if cuser
5563 (setq op (erc-channel-user-op cuser)
5564 voice (erc-channel-user-voice cuser)))
5565 (if (interactive-p)
5566 (message "%s is %s@%s%s%s"
5567 nick login host
5568 (if full-name (format " (%s)" full-name) "")
5569 (if (or op voice)
5570 (format " and is +%s%s on %s"
5571 (if op "o" "")
5572 (if voice "v" "")
5573 (erc-default-target))
5574 ""))
5575 user))))
5577 (defun erc-away-p ()
5578 "Return t if the current ERC process is set away."
5579 (save-excursion
5580 (and (erc-server-buffer-live-p)
5581 (set-buffer (process-buffer erc-server-process))
5582 erc-away)))
5584 ;; Mode line handling
5586 (defcustom erc-mode-line-format "%s %a"
5587 "A string to be formatted and shown in the mode-line in `erc-mode'.
5589 The string is formatted using `format-spec' and the result is set as the value
5590 of `mode-line-buffer-identification'.
5592 The following characters are replaced:
5593 %a: String indicating away status or \"\" if you are not away
5594 %m: The modes of the channel
5595 %n: The current nick name
5596 %o: The topic of the channel
5597 %p: The session port
5598 %t: The name of the target (channel, nickname, or servername:port)
5599 %s: In the server-buffer, this gets filled with the value of
5600 `erc-server-announced-name', in a channel, the value of
5601 (erc-default-target) also get concatenated."
5602 :group 'erc-mode-line-and-header
5603 :type 'string)
5605 (defcustom erc-header-line-format "[IRC] %n on %t %m %o"
5606 "A string to be formatted and shown in the header-line in `erc-mode'.
5607 Only used in Emacs 21.
5609 See `erc-mode-line-format' for which characters are can be used."
5610 :group 'erc-mode-line-and-header
5611 :type 'string)
5613 (defcustom erc-header-line-uses-help-echo-p t
5614 "Show the contents of the header line in the echo area or as a tooltip
5615 when you move point into the header line."
5616 :group 'erc-mode-line-and-header
5617 :type 'boolean)
5619 (defcustom erc-show-channel-key-p t
5620 "Show the the channel key in the header line."
5621 :group 'erc-paranoia
5622 :type 'boolean)
5624 (defcustom erc-common-server-suffixes
5625 '(("openprojects.net$" . "OPN")
5626 ("freenode.net$" . "OPN"))
5627 "Alist of common server name suffixes.
5628 This variable is used in mode-line display to save screen
5629 real estate. Set it to nil if you want to avoid changing
5630 displayed hostnames."
5631 :group 'erc-mode-line-and-header
5632 :type 'alist)
5634 (defcustom erc-mode-line-away-status-format
5635 "(AWAY since %a %b %d %H:%M) "
5636 "When you're away on a server, this is shown in the mode line.
5637 This should be a string with substitution variables recognized by
5638 format-time-message."
5639 :group 'erc-mode-line-and-header
5640 :type 'string)
5642 (defun erc-shorten-server-name (server-name)
5643 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
5644 (if (stringp server-name)
5645 (with-temp-buffer
5646 (insert server-name)
5647 (let ((alist erc-common-server-suffixes))
5648 (while alist
5649 (goto-char (point-min))
5650 (if (re-search-forward (caar alist) nil t)
5651 (replace-match (cdar alist)))
5652 (setq alist (cdr alist))))
5653 (buffer-string))))
5655 (defun erc-format-target ()
5656 "Return the name of the target (channel or nickname or servername:port)."
5657 (let ((target (erc-default-target)))
5658 (or target
5659 (concat (erc-shorten-server-name
5660 (or erc-server-announced-name
5661 erc-session-server))
5662 ":" (erc-port-to-string erc-session-port)))))
5664 (defun erc-format-target-and/or-server ()
5665 "Return the server name or the current target and server name combined."
5666 (let ((server-name (erc-shorten-server-name
5667 (or erc-server-announced-name
5668 erc-session-server))))
5669 (cond ((erc-default-target)
5670 (concat (erc-string-no-properties (erc-default-target))
5671 "@" server-name))
5672 (server-name server-name)
5673 (t (buffer-name (current-buffer))))))
5675 (defun erc-format-away-status ()
5676 "Return a formatted `erc-mode-line-away-status-format'
5677 if `erc-away' is non-nil."
5678 (let ((a (when (erc-server-buffer-live-p)
5679 (with-current-buffer (process-buffer erc-server-process)
5680 erc-away))))
5681 (if a
5682 (format-time-string erc-mode-line-away-status-format a)
5683 "")))
5685 (defun erc-format-channel-modes ()
5686 "Return the current channel's modes and the estimated lag."
5687 (let ((lag (when (erc-server-buffer-live-p)
5688 (with-current-buffer (process-buffer erc-server-process)
5689 erc-server-lag))))
5690 (concat (apply 'concat
5691 "(+" erc-channel-modes)
5692 (cond ((and erc-channel-user-limit erc-channel-key)
5693 (if erc-show-channel-key-p
5694 (format "lk %.0f %s" erc-channel-user-limit
5695 erc-channel-key)
5696 (format "kl %.0f" erc-channel-user-limit)))
5697 (erc-channel-user-limit
5698 ;; Emacs has no bignums
5699 (format "l %.0f" erc-channel-user-limit))
5700 (erc-channel-key
5701 (if erc-show-channel-key-p
5702 (format "k %s" erc-channel-key)
5703 "k"))
5704 (t ""))
5705 (if lag (format ",lag:%.0f" lag) "")
5706 ")")))
5708 (defun erc-update-mode-line-buffer (buffer)
5709 "Update the mode line in a single ERC buffer BUFFER."
5710 (with-current-buffer buffer
5711 (let ((spec (format-spec-make
5712 ?a (erc-format-away-status)
5713 ?m (erc-format-channel-modes)
5714 ?n (or (erc-current-nick) "")
5715 ?o (erc-controls-strip erc-channel-topic)
5716 ?p (erc-port-to-string erc-session-port)
5717 ?s (erc-format-target-and/or-server)
5718 ?t (erc-format-target)))
5719 (process-status (cond ((and (erc-server-process-alive)
5720 (not erc-server-connected))
5721 ":connecting")
5722 ((erc-server-process-alive)
5725 ": CLOSED"))))
5726 (cond ((featurep 'xemacs)
5727 (setq modeline-buffer-identification
5728 (list (format-spec erc-mode-line-format spec)))
5729 (setq modeline-process (list process-status)))
5731 (setq mode-line-buffer-identification
5732 (list (format-spec erc-mode-line-format spec)))
5733 (setq mode-line-process (list process-status))))
5734 (when (boundp 'header-line-format)
5735 (let ((header (if erc-header-line-format
5736 (format-spec erc-header-line-format spec)
5737 nil)))
5738 (cond ((null header)
5739 (setq header-line-format nil))
5740 (erc-header-line-uses-help-echo-p
5741 (let ((help-echo (with-temp-buffer
5742 (insert header)
5743 (fill-region (point-min) (point-max))
5744 (buffer-string))))
5745 (setq header-line-format
5746 (erc-replace-regexp-in-string
5748 "%%"
5749 (erc-propertize header 'help-echo help-echo)))))
5750 (t (setq header-line-format header))))))
5751 (if (featurep 'xemacs)
5752 (redraw-modeline)
5753 (force-mode-line-update))))
5755 (defun erc-update-mode-line (&optional buffer)
5756 "Update the mode line in BUFFER.
5758 If BUFFER is nil, update the mode line in all ERC buffers."
5759 (if (and buffer (bufferp buffer))
5760 (erc-update-mode-line-buffer buffer)
5761 (dolist (buf (erc-buffer-list))
5762 (when (buffer-live-p buf)
5763 (erc-update-mode-line-buffer buf)))))
5765 ;; Miscellaneous
5767 (defun erc-port-to-string (p)
5768 "Convert port P to a string.
5769 P may be an integer or a service name."
5770 (if (integerp p)
5771 (int-to-string p)
5774 (defun erc-string-to-port (s)
5775 "Convert string S to either an integer port number or a service name."
5776 (let ((n (string-to-number s)))
5777 (if (= n 0)
5779 n)))
5781 (defun erc-version (&optional here)
5782 "Show the version number of ERC in the minibuffer.
5783 If optional argument HERE is non-nil, insert version number at point."
5784 (interactive "P")
5785 (let ((version-string
5786 (format "ERC %s (GNU Emacs %s)" erc-version-string emacs-version)))
5787 (if here
5788 (insert version-string)
5789 (if (interactive-p)
5790 (message "%s" version-string)
5791 version-string))))
5793 (defun erc-version-modules (&optional here)
5794 "Show the version numbers of all loaded ERC modules in the minibuffer.
5795 If optional argument HERE is non-nil, insert version number at point."
5796 (interactive "P")
5797 (let ((version-string
5798 (mapconcat 'identity
5799 (let (versions (case-fold-search nil))
5800 (dolist (var (apropos-internal "^erc-.*version$"))
5801 (when (and (boundp var)
5802 (stringp (symbol-value var)))
5803 (setq versions (cons (format "%S: %s"
5804 var (symbol-value var))
5805 versions))))
5806 versions) ", ")))
5807 (if here
5808 (insert version-string)
5809 (if (interactive-p)
5810 (message "%s" version-string)
5811 version-string))))
5813 (defun erc-modes (&optional here)
5814 "Show the active ERC modes in the minibuffer.
5815 If optional argument HERE is non-nil, insert version number at point."
5816 (interactive "P")
5817 (let ((string
5818 (mapconcat 'identity
5819 (let (modes (case-fold-search nil))
5820 (dolist (var (apropos-internal "^erc-.*mode$"))
5821 (when (and (boundp var)
5822 (symbol-value var))
5823 (setq modes (cons (symbol-name var)
5824 modes))))
5825 modes)
5826 ", ")))
5827 (if here
5828 (insert string)
5829 (if (interactive-p)
5830 (message "%s" string)
5831 string))))
5833 (defun erc-latest-version ()
5834 "Retrieve the latest erc.el version from CVS."
5835 (interactive)
5836 (if (ignore-errors (require 'url))
5837 (progn
5838 (switch-to-buffer (get-buffer-create "*erc.el latest version*"))
5839 (delete-region (point-min) (point-max))
5840 (kill-all-local-variables)
5841 (url-insert-file-contents (concat
5842 "http://cvs.sourceforge.net/viewcvs.py/"
5843 "*checkout*/erc/erc/erc.el?content-type"
5844 "=text%2Fplain&rev=HEAD"))
5845 (emacs-lisp-mode)
5846 (current-buffer))
5847 (error "URL needs to be installed")))
5849 (defun erc-ediff-latest-version ()
5850 "Ediff your installed erc.el with the latest CVS version.
5851 See also `erc-latest-version'."
5852 (interactive)
5853 (let ((current (locate-library "erc.el")))
5854 (if current
5855 (ediff-buffers (find-file current)
5856 (erc-latest-version))
5857 (error "You do not appear to have the uncompiled erc.el file"))))
5859 (defun erc-trim-string (s)
5860 "Trim leading and trailing spaces off S."
5861 (cond
5862 ((not (stringp s)) nil)
5863 ((string-match "^\\s-*$" s)
5865 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
5866 (match-string 1 s))
5868 s)))
5870 (defun erc-arrange-session-in-multiple-windows ()
5871 "Open a window for every non-server buffer related to `erc-session-server'.
5873 All windows are opened in the current frame."
5874 (interactive)
5875 (unless (boundp 'erc-server-process)
5876 (error "No erc-process found in current buffer"))
5877 (let ((bufs (erc-buffer-list nil erc-server-process)))
5878 (when bufs
5879 (delete-other-windows)
5880 (switch-to-buffer (car bufs))
5881 (setq bufs (cdr bufs))
5882 (while bufs
5883 (split-window)
5884 (switch-to-buffer-other-window (car bufs))
5885 (setq bufs (cdr bufs))
5886 (balance-windows)))))
5888 (defun erc-popup-input-buffer ()
5889 "Provide a input buffer."
5890 (interactive)
5891 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
5892 (mode (intern
5893 (completing-read
5894 "Mode: "
5895 (mapcar (lambda (e)
5896 (list (symbol-name e)))
5897 (apropos-internal "-mode$" 'commandp))
5898 nil t))))
5899 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
5900 (funcall mode)
5901 (narrow-to-region (point) (point))
5902 (shrink-window-if-larger-than-buffer)))
5904 ;;; Message catalog
5906 (defun erc-make-message-variable-name (catalog entry)
5907 "Create a variable name corresponding to CATALOG's ENTRY."
5908 (intern (concat "erc-message-"
5909 (symbol-name catalog) "-" (symbol-name entry))))
5911 (defun erc-define-catalog-entry (catalog entry format-spec)
5912 "Set CATALOG's ENTRY to FORMAT-SPEC."
5913 (set (erc-make-message-variable-name catalog entry)
5914 format-spec))
5916 (defun erc-define-catalog (catalog entries)
5917 "Define a CATALOG according to ENTRIES."
5918 (dolist (entry entries)
5919 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
5921 (erc-define-catalog
5922 'english
5923 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
5924 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
5925 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
5926 (cannot-find-file . "Cannot find file %f")
5927 (cannot-read-file . "Cannot read file %f")
5928 (connect . "Connecting to %S:%p... ")
5929 (country . "%c")
5930 (country-unknown . "%d: No such domain")
5931 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
5932 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
5933 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
5934 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
5935 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
5936 (flood-strict-mode . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
5937 (disconnected . "Connection failed! Re-establishing connection...")
5938 (disconnected-noreconnect . "Connection failed! Not re-establishing connection.")
5939 (login . "Logging in as \'%n\'...")
5940 (nick-in-use . "%n is in use. Choose new nickname: ")
5941 (nick-too-long . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
5942 (no-default-channel . "No default channel")
5943 (no-invitation . "You've got no invitation")
5944 (no-target . "No target")
5945 (ops . "%i operator%s: %o")
5946 (ops-none . "No operators in this channel.")
5947 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
5948 (variable-not-bound . "Variable not bound!")
5949 (ACTION . "* %n %a")
5950 (CTCP-CLIENTINFO . "Client info for %n: %m")
5951 (CTCP-ECHO . "Echo %n: %m")
5952 (CTCP-FINGER . "Finger info for %n: %m")
5953 (CTCP-PING . "Ping time to %n is %t")
5954 (CTCP-TIME . "Time by %n is %m")
5955 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
5956 (CTCP-VERSION . "Version for %n is %m")
5957 (ERROR . "==> ERROR from %s: %c\n")
5958 (INVITE . "%n (%u@%h) invites you to channel %c")
5959 (JOIN . "%n (%u@%h) has joined channel %c")
5960 (JOIN-you . "You have joined channel %c")
5961 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
5962 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
5963 (KICK-by-you . "You have kicked %k off channel %c: %r")
5964 (MODE . "%n (%u@%h) has changed mode for %t to %m")
5965 (MODE-nick . "%n has changed mode for %t to %m")
5966 (NICK . "%n (%u@%h) is now known as %N")
5967 (NICK-you . "Your new nickname is %N")
5968 (PART . erc-message-english-PART)
5969 (PING . "PING from server (last: %s sec. ago)")
5970 (PONG . "PONG from %h (%i second%s)")
5971 (QUIT . "%n (%u@%h) has quit: %r")
5972 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
5973 (WALLOPS . "Wallops from %n: %m")
5974 (s004 . "%s %v %U %C")
5975 (s221 . "User modes for %n: %m")
5976 (s252 . "%i operator(s) online")
5977 (s253 . "%i unknown connection(s)")
5978 (s254 . "%i channels formed")
5979 (s301 . "%n is AWAY: %r")
5980 (s303 . "Is online: %n")
5981 (s305 . "%m")
5982 (s306 . "%m")
5983 (s311 . "%n is %f (%u@%h)")
5984 (s312 . "%n is/was on server %s (%c)")
5985 (s313 . "%n is an IRC operator")
5986 (s314 . "%n was %f (%u@%h)")
5987 (s317 . "%n has been idle for %i")
5988 (s317-on-since . "%n has been idle for %i, on since %t")
5989 (s319 . "%n is on channel(s): %c")
5990 (s320 . "%n is an identified user")
5991 (s321 . "Channel Users Topic")
5992 (s322 . "%c [%u] %t")
5993 (s324 . "%c modes: %m")
5994 (s329 . "%c was created on %t")
5995 (s330 . "%n %a %i")
5996 (s331 . "No topic is set for %c")
5997 (s332 . "Topic for %c: %T")
5998 (s333 . "%c: topic set by %n, %t")
5999 (s341 . "Inviting %n to channel %c")
6000 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
6001 (s353 . "Users on %c: %u")
6002 (s367 . "Ban on %b on %c set by %s on %t (Use /banlist!)")
6003 (s368 . "Banlist of %c ends.")
6004 (s379 . "%c: Forwarded to %f")
6005 (s391 . "The time at %s is %t")
6006 (s401 . "%n: No such nick/channel")
6007 (s403 . "%c: No such channel")
6008 (s404 . "%c: Cannot send to channel")
6009 (s405 . "%c: You have joined too many channels")
6010 (s406 . "%n: There was no such nickname")
6011 (s412 . "No text to send")
6012 (s421 . "%c: Unknown command")
6013 (s431 . "No nickname given")
6014 (s432 . "%n is an erroneous nickname")
6015 (s442 . "%c: You're not on that channel")
6016 (s445 . "SUMMON has been disabled")
6017 (s446 . "USERS has been disabled")
6018 (s451 . "You have not registered")
6019 (s461 . "%c: not enough parameters")
6020 (s462 . "Unauthorized command (already registered)")
6021 (s463 . "Your host isn't among the privileged")
6022 (s464 . "Password incorrect")
6023 (s465 . "You are banned from this server")
6024 (s474 . "You can't join %c because you're banned (+b)")
6025 (s475 . "You must specify the correct channel key (+k) to join %c")
6026 (s481 . "Permission Denied - You're not an IRC operator")
6027 (s482 . "You need to be a channel operator of %c to do that")
6028 (s483 . "You can't kill a server!")
6029 (s484 . "Your connection is restricted!")
6030 (s485 . "You're not the original channel operator")
6031 (s491 . "No O-lines for your host")
6032 (s501 . "Unknown MODE flag")
6033 (s502 . "You can't change modes for other users")))
6035 (defun erc-message-english-PART (&rest args)
6036 "Format a proper PART message.
6038 This function is an example on what could be done with formatting
6039 functions."
6040 (let ((nick (cadr (memq ?n args)))
6041 (user (cadr (memq ?u args)))
6042 (host (cadr (memq ?h args)))
6043 (channel (cadr (memq ?c args)))
6044 (reason (cadr (memq ?r args))))
6045 (if (string= nick (erc-current-nick))
6046 (format "You have left channel %s" channel)
6047 (format "%s (%s@%s) has left channel %s%s"
6048 nick user host channel
6049 (if (not (string= reason ""))
6050 (format ": %s" reason)
6051 "")))))
6054 (defvar erc-current-message-catalog 'english)
6055 (make-variable-buffer-local 'erc-current-message-catalog)
6057 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6058 "Retrieve ENTRY from CATALOG.
6060 If CATALOG is nil, `erc-current-message-catalog' is used.
6062 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6063 english, catalog."
6064 (unless catalog (setq catalog erc-current-message-catalog))
6065 (let ((var (erc-make-message-variable-name catalog entry)))
6066 (if (boundp var)
6067 (symbol-value var)
6068 (when (boundp (erc-make-message-variable-name 'english entry))
6069 (symbol-value (erc-make-message-variable-name 'english entry))))))
6071 (defun erc-format-message (msg &rest args)
6072 "Format MSG according to ARGS.
6074 See also `format-spec'."
6075 (when (eq (logand (length args) 1) 1) ; oddp
6076 (error "Obscure usage of this function appeared"))
6077 (let ((entry (erc-retrieve-catalog-entry msg)))
6078 (when (not entry)
6079 (error "No format spec for message %s" msg))
6080 (when (functionp entry)
6081 (setq entry (apply entry args)))
6082 (format-spec entry (apply 'format-spec-make args))))
6084 ;;; Various hook functions
6086 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6088 (defcustom erc-kill-server-hook '(erc-kill-server)
6089 "*Invoked whenever a server-buffer is killed via `kill-buffer'."
6090 :group 'erc-hooks
6091 :type 'hook)
6093 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6094 "*Invoked whenever a channel-buffer is killed via `kill-buffer'."
6095 :group 'erc-hooks
6096 :type 'hook)
6098 (defcustom erc-kill-buffer-hook nil
6099 "*Hook run whenever a non-server or channel buffer is killed.
6101 See also `kill-buffer'."
6102 :group 'erc-hooks
6103 :type 'hook)
6105 (defun erc-kill-buffer-function ()
6106 "Function to call when an ERC buffer is killed.
6107 This function should be on `kill-buffer-hook'.
6108 When the current buffer is in `erc-mode', this function will run
6109 one of the following hooks:
6110 `erc-kill-server-hook' if the server buffer was killed,
6111 `erc-kill-channel-hook' if a channel buffer was killed,
6112 or `erc-kill-buffer-hook' if any other buffer."
6113 (when (eq major-mode 'erc-mode)
6114 (erc-remove-channel-users)
6115 (cond
6116 ((eq (erc-server-buffer) (current-buffer))
6117 (run-hooks 'erc-kill-server-hook))
6118 ((erc-channel-p (erc-default-target))
6119 (run-hooks 'erc-kill-channel-hook))
6121 (run-hooks 'erc-kill-buffer-hook)))))
6123 (defun erc-kill-server ()
6124 "Sends a QUIT command to the server when the server buffer is killed.
6125 This function should be on `erc-kill-server-hook'."
6126 (when (erc-server-process-alive)
6127 (setq erc-server-quitting t)
6128 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6130 (defun erc-kill-channel ()
6131 "Sends a PART command to the server when the channel buffer is killed.
6132 This function should be on `erc-kill-channel-hook'."
6133 (when (erc-server-process-alive)
6134 (let ((tgt (erc-default-target)))
6135 (erc-server-send (format "PART %s :%s" tgt
6136 (funcall erc-part-reason nil))
6137 nil tgt))))
6139 ;;; Dealing with `erc-parsed'
6141 (defun erc-get-parsed-vector (point)
6142 "Return the whole parsed vector on POINT."
6143 (get-text-property point 'erc-parsed))
6145 (defun erc-get-parsed-vector-nick (vect)
6146 "Return nickname in the parsed vector VECT."
6147 (let* ((untreated-nick (and vect (erc-response.sender vect)))
6148 (maybe-nick (when untreated-nick
6149 (car (split-string untreated-nick "!")))))
6150 (when (and (not (null maybe-nick))
6151 (erc-is-valid-nick-p maybe-nick))
6152 untreated-nick)))
6154 (defun erc-get-parsed-vector-type (vect)
6155 "Return message type in the parsed vector VECT."
6156 (and vect
6157 (erc-response.command vect)))
6159 (provide 'erc)
6161 ;;; Deprecated. We might eventually stop requiring the goodies automatically.
6162 ;;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6163 ;;; avoid a recursive require error when byte-compiling the entire package.
6164 (require 'erc-goodies)
6166 ;;; erc.el ends here
6168 ;; Local Variables:
6169 ;; outline-regexp: ";;+"
6170 ;; indent-tabs-mode: t
6171 ;; tab-width: 8
6172 ;; End:
6174 ;; arch-tag: d19587f6-627e-48c1-8d86-58595fa3eca3