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