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