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