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