* erc.el (erc-invite-only-mode, erc-toggle-channel-mode):
[bpt/emacs.git] / lisp / erc / erc.el
CommitLineData
d0fcaff5 1;; erc.el --- An Emacs Internet Relay Chat client -*- lexical-binding:t -*-
597993cf 2
ab422c4d 3;; Copyright (C) 1997-2013 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)
df5d5f59 12;; Maintainer: FSF
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")
597993cf
MB
84 :prefix "erc-"
85 :group 'applications)
86
87(defgroup erc-buffers nil
88 "Creating new ERC buffers"
89 :group 'erc)
90
91(defgroup erc-display nil
92 "Settings for how various things are displayed"
93 :group 'erc)
94
95(defgroup erc-mode-line-and-header nil
96 "Displaying information in the mode-line and header"
97 :group 'erc-display)
98
99(defgroup erc-ignore nil
100 "Ignoring certain messages"
101 :group 'erc)
102
487a247f
JF
103(defgroup erc-lurker nil
104 "Hide specified message types sent by lurkers"
d1a1c7e6 105 :version "24.3"
487a247f
JF
106 :group 'erc-ignore)
107
597993cf
MB
108(defgroup erc-query nil
109 "Using separate buffers for private discussions"
110 :group 'erc)
111
112(defgroup erc-quit-and-part nil
113 "Quitting and parting channels"
114 :group 'erc)
115
116(defgroup erc-paranoia nil
117 "Know what is sent and received; control the display of sensitive data."
118 :group 'erc)
119
120(defgroup erc-scripts nil
121 "Running scripts at startup and with /LOAD"
122 :group 'erc)
123
124(require 'erc-backend)
125
126;; compatibility with older ERC releases
127
d0fcaff5
SM
128(define-obsolete-variable-alias 'erc-announced-server-name
129 'erc-server-announced-name "ERC 5.1")
130(define-obsolete-variable-alias 'erc-process 'erc-server-process "ERC 5.1")
131(define-obsolete-variable-alias 'erc-default-coding-system
132 'erc-server-coding-system "ERC 5.1")
597993cf 133
59f7af81
CY
134(define-obsolete-function-alias 'erc-send-command
135 'erc-server-send "ERC 5.1")
597993cf
MB
136
137;; tunable connection and authentication parameters
138
139(defcustom erc-server nil
0b6bb130 140 "IRC server to use if one is not provided.
597993cf
MB
141See function `erc-compute-server' for more details on connection
142parameters and authentication."
143 :group 'erc
0b6bb130
MB
144 :type '(choice (const :tag "None" nil)
145 (string :tag "Server")))
597993cf
MB
146
147(defcustom erc-port nil
0b6bb130
MB
148 "IRC port to use if not specified.
149
150This can be either a string or a number."
597993cf 151 :group 'erc
0b6bb130 152 :type '(choice (const :tag "None" nil)
f2c05698
MB
153 (integer :tag "Port number")
154 (string :tag "Port string")))
597993cf
MB
155
156(defcustom erc-nick nil
0b6bb130 157 "Nickname to use if one is not provided.
597993cf 158
0b6bb130 159This can be either a string, or a list of strings.
597993cf
MB
160In the latter case, if the first nick in the list is already in use,
161other nicks are tried in the list order.
162
163See function `erc-compute-nick' for more details on connection
164parameters and authentication."
165 :group 'erc
0b6bb130 166 :type '(choice (const :tag "None" nil)
597993cf 167 (string :tag "Nickname")
0b6bb130 168 (repeat (string :tag "Nickname"))))
597993cf
MB
169
170(defcustom erc-nick-uniquifier "`"
0b6bb130 171 "The string to append to the nick if it is already in use."
597993cf
MB
172 :group 'erc
173 :type 'string)
174
0b6bb130
MB
175(defcustom erc-try-new-nick-p t
176 "If the nickname you chose isn't available, and this option is non-nil,
177ERC should automatically attempt to connect with another nickname.
178
179You can manually set another nickname with the /NICK command."
597993cf
MB
180 :group 'erc
181 :type 'boolean)
182
183(defcustom erc-user-full-name nil
184 "User full name.
185
0b6bb130
MB
186This can be either a string or a function to call.
187
597993cf
MB
188See function `erc-compute-full-name' for more details on connection
189parameters and authentication."
190 :group 'erc
0b6bb130
MB
191 :type '(choice (const :tag "No name" nil)
192 (string :tag "Name")
193 (function :tag "Get from function"))
597993cf 194 :set (lambda (sym val)
d0fcaff5 195 (set sym (if (functionp val) (funcall val) val))))
597993cf
MB
196
197(defvar erc-password nil
0b6bb130
MB
198 "Password to use when authenticating to an IRC server.
199It is not strictly necessary to provide this, since ERC will
200prompt you for it.")
597993cf
MB
201
202(defcustom erc-user-mode nil
203 "Initial user modes to be set after a connection is established."
204 :group 'erc
205 :type '(choice (const nil) string function))
206
207
208(defcustom erc-prompt-for-password t
209 "Asks before using the default password, or whether to enter a new one."
210 :group 'erc
211 :type 'boolean)
212
213(defcustom erc-warn-about-blank-lines t
214 "Warn the user if they attempt to send a blank line."
215 :group 'erc
216 :type 'boolean)
217
218(defcustom erc-send-whitespace-lines nil
219 "If set to non-nil, send lines consisting of only whitespace."
220 :group 'erc
221 :type 'boolean)
222
223(defcustom erc-hide-prompt nil
224 "If non-nil, do not display the prompt for commands.
225
226\(A command is any input starting with a '/').
227
228See also the variables `erc-prompt' and `erc-command-indicator'."
229 :group 'erc-display
230 :type 'boolean)
231
232;; tunable GUI stuff
233
234(defcustom erc-show-my-nick t
235 "If non-nil, display one's own nickname when sending a message.
236
237If non-nil, \"<nickname>\" will be shown.
238If nil, only \"> \" will be shown."
239 :group 'erc-display
240 :type 'boolean)
241
242(define-widget 'erc-message-type 'set
243 "A set of standard IRC Message types."
244 :args '((const "JOIN")
245 (const "KICK")
246 (const "NICK")
247 (const "PART")
248 (const "QUIT")
249 (const "MODE")
250 (repeat :inline t :tag "Others" (string :tag "IRC Message Type"))))
251
252(defcustom erc-hide-list nil
fb7ada5f 253 "List of IRC type messages to hide.
597993cf
MB
254A typical value would be '(\"JOIN\" \"PART\" \"QUIT\")."
255 :group 'erc-ignore
256 :type 'erc-message-type)
257
258(defvar erc-session-password nil
259 "The password used for the current session.")
260(make-variable-buffer-local 'erc-session-password)
261
262(defcustom erc-disconnected-hook nil
263 "Run this hook with arguments (NICK IP REASON) when disconnected.
264This happens before automatic reconnection. Note, that
265`erc-server-QUIT-functions' might not be run when we disconnect,
266simply because we do not necessarily receive the QUIT event."
267 :group 'erc-hooks
268 :type 'hook)
269
270(defcustom erc-complete-functions nil
271 "These functions get called when the user hits TAB in ERC.
272Each function in turn is called until one returns non-nil to
273indicate it has handled the input."
274 :group 'erc-hooks
275 :type 'hook)
276
277(defcustom erc-join-hook nil
278 "Hook run when we join a channel. Hook functions are called
279without arguments, with the current buffer set to the buffer of
280the new channel.
281
282See also `erc-server-JOIN-functions', `erc-part-hook'."
283 :group 'erc-hooks
284 :type 'hook)
285
286(defcustom erc-quit-hook nil
287 "Hook run when processing a quit command directed at our nick.
288
289The hook receives one argument, the current PROCESS.
290See also `erc-server-QUIT-functions' and `erc-disconnected-hook'."
291 :group 'erc-hooks
292 :type 'hook)
293
294(defcustom erc-part-hook nil
295 "Hook run when processing a PART message directed at our nick.
296
297The hook receives one argument, the current BUFFER.
298See also `erc-server-QUIT-functions', `erc-quit-hook' and
299`erc-disconnected-hook'."
300 :group 'erc-hooks
301 :type 'hook)
302
303(defcustom erc-kick-hook nil
304 "Hook run when processing a KICK message directed at our nick.
305
306The hook receives one argument, the current BUFFER.
307See also `erc-server-PART-functions' and `erc-part-hook'."
308 :group 'erc-hooks
309 :type 'hook)
310
311(defcustom erc-nick-changed-functions nil
312 "List of functions run when your nick was successfully changed.
313
314Each function should accept two arguments, NEW-NICK and OLD-NICK."
315 :group 'erc-hooks
316 :type 'hook)
317
318(defcustom erc-connect-pre-hook '(erc-initialize-log-marker)
319 "Hook called just before `erc' calls `erc-connect'.
ff59d266 320Functions are passed a buffer as the first argument."
597993cf
MB
321 :group 'erc-hooks
322 :type 'hook)
323
324
325(defvar erc-channel-users nil
326 "A hash table of members in the current channel, which
327associates nicknames with cons cells of the form:
328\(USER . MEMBER-DATA) where USER is a pointer to an
329erc-server-user struct, and MEMBER-DATA is a pointer to an
330erc-channel-user struct.")
331(make-variable-buffer-local 'erc-channel-users)
332
333(defvar erc-server-users nil
334 "A hash table of users on the current server, which associates
335nicknames with erc-server-user struct instances.")
336(make-variable-buffer-local 'erc-server-users)
337
338(defun erc-downcase (string)
339 "Convert STRING to IRC standard conforming downcase."
340 (let ((s (downcase string))
341 (c '((?\[ . ?\{)
342 (?\] . ?\})
343 (?\\ . ?\|)
344 (?~ . ?^))))
345 (save-match-data
346 (while (string-match "[]\\[~]" s)
347 (aset s (match-beginning 0)
348 (cdr (assq (aref s (match-beginning 0)) c)))))
349 s))
350
01d9e862
MB
351(defmacro erc-with-server-buffer (&rest body)
352 "Execute BODY in the current ERC server buffer.
353If no server buffer exists, return nil."
8599b23a 354 (declare (indent 0) (debug (body)))
01d9e862
MB
355 (let ((buffer (make-symbol "buffer")))
356 `(let ((,buffer (erc-server-buffer)))
357 (when (buffer-live-p ,buffer)
358 (with-current-buffer ,buffer
359 ,@body)))))
01d9e862 360
19dc7206 361(cl-defstruct (erc-server-user (:type vector) :named)
597993cf
MB
362 ;; User data
363 nickname host login full-name info
364 ;; Buffers
365 ;;
366 ;; This is an alist of the form (BUFFER . CHANNEL-DATA), where
367 ;; CHANNEL-DATA is either nil or an erc-channel-user struct.
368 (buffers nil)
369 )
370
19dc7206 371(cl-defstruct (erc-channel-user (:type vector) :named)
597993cf
MB
372 op voice
373 ;; Last message time (in the form of the return value of
374 ;; (current-time)
375 ;;
376 ;; This is useful for ordered name completion.
377 (last-message-time nil))
378
379(defsubst erc-get-channel-user (nick)
d0fcaff5 380 "Find the (USER . CHANNEL-DATA) element corresponding to NICK
597993cf
MB
381in the current buffer's `erc-channel-users' hash table."
382 (gethash (erc-downcase nick) erc-channel-users))
383
384(defsubst erc-get-server-user (nick)
d0fcaff5 385 "Find the USER corresponding to NICK in the current server's
597993cf 386`erc-server-users' hash table."
ff59d266 387 (erc-with-server-buffer
597993cf
MB
388 (gethash (erc-downcase nick) erc-server-users)))
389
390(defsubst erc-add-server-user (nick user)
391 "This function is for internal use only.
392
393Adds USER with nickname NICK to the `erc-server-users' hash table."
ff59d266 394 (erc-with-server-buffer
597993cf
MB
395 (puthash (erc-downcase nick) user erc-server-users)))
396
397(defsubst erc-remove-server-user (nick)
398 "This function is for internal use only.
399
400Removes the user with nickname NICK from the `erc-server-users'
401hash table. This user is not removed from the
402`erc-channel-users' lists of other buffers.
403
404See also: `erc-remove-user'."
ff59d266 405 (erc-with-server-buffer
597993cf
MB
406 (remhash (erc-downcase nick) erc-server-users)))
407
408(defun erc-change-user-nickname (user new-nick)
409 "This function is for internal use only.
410
411Changes the nickname of USER to NEW-NICK in the
412`erc-server-users' hash table. The `erc-channel-users' lists of
413other buffers are also changed."
414 (let ((nick (erc-server-user-nickname user)))
415 (setf (erc-server-user-nickname user) new-nick)
ff59d266 416 (erc-with-server-buffer
597993cf
MB
417 (remhash (erc-downcase nick) erc-server-users)
418 (puthash (erc-downcase new-nick) user erc-server-users))
419 (dolist (buf (erc-server-user-buffers user))
420 (if (buffer-live-p buf)
421 (with-current-buffer buf
422 (let ((cdata (erc-get-channel-user nick)))
423 (remhash (erc-downcase nick) erc-channel-users)
424 (puthash (erc-downcase new-nick) cdata
425 erc-channel-users)))))))
426
427(defun erc-remove-channel-user (nick)
428 "This function is for internal use only.
429
430Removes the user with nickname NICK from the `erc-channel-users'
431list for this channel. If this user is not in the
432`erc-channel-users' list of any other buffers, the user is also
433removed from the server's `erc-server-users' list.
434
435See also: `erc-remove-server-user' and `erc-remove-user'."
436 (let ((channel-data (erc-get-channel-user nick)))
437 (when channel-data
438 (let ((user (car channel-data)))
439 (setf (erc-server-user-buffers user)
440 (delq (current-buffer)
441 (erc-server-user-buffers user)))
442 (remhash (erc-downcase nick) erc-channel-users)
443 (if (null (erc-server-user-buffers user))
444 (erc-remove-server-user nick))))))
445
446(defun erc-remove-user (nick)
447 "This function is for internal use only.
448
449Removes the user with nickname NICK from the `erc-server-users'
450list as well as from all `erc-channel-users' lists.
451
452See also: `erc-remove-server-user' and
453`erc-remove-channel-user'."
454 (let ((user (erc-get-server-user nick)))
455 (when user
456 (let ((buffers (erc-server-user-buffers user)))
457 (dolist (buf buffers)
458 (if (buffer-live-p buf)
459 (with-current-buffer buf
460 (remhash (erc-downcase nick) erc-channel-users)
461 (run-hooks 'erc-channel-members-changed-hook)))))
462 (erc-remove-server-user nick))))
463
464(defun erc-remove-channel-users ()
465 "This function is for internal use only.
466
467Removes all users in the current channel. This is called by
468`erc-server-PART' and `erc-server-QUIT'."
469 (when (and erc-server-connected
470 (erc-server-process-alive)
471 (hash-table-p erc-channel-users))
d0fcaff5 472 (maphash (lambda (nick _cdata)
597993cf
MB
473 (erc-remove-channel-user nick))
474 erc-channel-users)
475 (clrhash erc-channel-users)))
476
477(defsubst erc-channel-user-op-p (nick)
a3d2f0a3 478 "Return t if NICK is an operator in the current channel."
597993cf
MB
479 (and nick
480 (hash-table-p erc-channel-users)
481 (let ((cdata (erc-get-channel-user nick)))
482 (and cdata (cdr cdata)
483 (erc-channel-user-op (cdr cdata))))))
484
485(defsubst erc-channel-user-voice-p (nick)
a3d2f0a3 486 "Return t if NICK has voice in the current channel."
597993cf
MB
487 (and nick
488 (hash-table-p erc-channel-users)
489 (let ((cdata (erc-get-channel-user nick)))
490 (and cdata (cdr cdata)
491 (erc-channel-user-voice (cdr cdata))))))
492
493(defun erc-get-channel-user-list ()
d0fcaff5 494 "Return a list of users in the current channel. Each element
597993cf 495of the list is of the form (USER . CHANNEL-DATA), where USER is
d0fcaff5 496an erc-server-user struct, and CHANNEL-DATA is either nil or an
597993cf
MB
497erc-channel-user struct.
498
499See also: `erc-sort-channel-users-by-activity'"
500 (let (users)
501 (if (hash-table-p erc-channel-users)
d0fcaff5 502 (maphash (lambda (_nick cdata)
597993cf
MB
503 (setq users (cons cdata users)))
504 erc-channel-users))
505 users))
506
507(defun erc-get-server-nickname-list ()
d0fcaff5 508 "Return a list of known nicknames on the current server."
ff59d266
MB
509 (erc-with-server-buffer
510 (let (nicks)
511 (when (hash-table-p erc-server-users)
d0fcaff5 512 (maphash (lambda (_n user)
ff59d266
MB
513 (setq nicks
514 (cons (erc-server-user-nickname user)
515 nicks)))
516 erc-server-users)
517 nicks))))
597993cf
MB
518
519(defun erc-get-channel-nickname-list ()
d0fcaff5 520 "Return a list of known nicknames on the current channel."
597993cf
MB
521 (let (nicks)
522 (when (hash-table-p erc-channel-users)
d0fcaff5 523 (maphash (lambda (_n cdata)
597993cf
MB
524 (setq nicks
525 (cons (erc-server-user-nickname (car cdata))
526 nicks)))
527 erc-channel-users)
528 nicks)))
529
530(defun erc-get-server-nickname-alist ()
d0fcaff5 531 "Return an alist of known nicknames on the current server."
ff59d266
MB
532 (erc-with-server-buffer
533 (let (nicks)
534 (when (hash-table-p erc-server-users)
d0fcaff5 535 (maphash (lambda (_n user)
ff59d266
MB
536 (setq nicks
537 (cons (cons (erc-server-user-nickname user) nil)
538 nicks)))
539 erc-server-users)
540 nicks))))
597993cf
MB
541
542(defun erc-get-channel-nickname-alist ()
d0fcaff5 543 "Return an alist of known nicknames on the current channel."
597993cf
MB
544 (let (nicks)
545 (when (hash-table-p erc-channel-users)
d0fcaff5 546 (maphash (lambda (_n cdata)
597993cf
MB
547 (setq nicks
548 (cons (cons (erc-server-user-nickname (car cdata)) nil)
549 nicks)))
550 erc-channel-users)
551 nicks)))
552
553(defun erc-sort-channel-users-by-activity (list)
d0fcaff5
SM
554 "Sort LIST such that users which have spoken most recently are listed first.
555LIST must be of the form (USER . CHANNEL-DATA).
597993cf
MB
556
557See also: `erc-get-channel-user-list'."
558 (sort list
559 (lambda (x y)
d0fcaff5 560 (when (and (cdr x) (cdr y))
597993cf
MB
561 (let ((tx (erc-channel-user-last-message-time (cdr x)))
562 (ty (erc-channel-user-last-message-time (cdr y))))
d0fcaff5
SM
563 (and tx
564 (or (not ty)
565 (time-less-p ty tx))))))))
597993cf
MB
566
567(defun erc-sort-channel-users-alphabetically (list)
568 "Sort LIST so that users' nicknames are in alphabetical order.
569LIST must be of the form (USER . CHANNEL-DATA).
570
571See also: `erc-get-channel-user-list'."
572 (sort list
573 (lambda (x y)
d0fcaff5 574 (when (and (cdr x) (cdr y))
597993cf
MB
575 (let ((nickx (downcase (erc-server-user-nickname (car x))))
576 (nicky (downcase (erc-server-user-nickname (car y)))))
d0fcaff5
SM
577 (and nickx
578 (or (not nicky)
579 (string-lessp nickx nicky))))))))
597993cf
MB
580
581(defvar erc-channel-topic nil
582 "A topic string for the channel. Should only be used in channel-buffers.")
583(make-variable-buffer-local 'erc-channel-topic)
584
585(defvar erc-channel-modes nil
586 "List of strings representing channel modes.
587E.g. '(\"i\" \"m\" \"s\" \"b Quake!*@*\")
588\(not sure the ban list will be here, but why not)")
589(make-variable-buffer-local 'erc-channel-modes)
590
591(defvar erc-insert-marker nil
592 "The place where insertion of new text in erc buffers should happen.")
593(make-variable-buffer-local 'erc-insert-marker)
594
595(defvar erc-input-marker nil
596 "The marker where input should be inserted.")
597(make-variable-buffer-local 'erc-input-marker)
598
599(defun erc-string-no-properties (string)
600 "Return a copy of STRING will all text-properties removed."
601 (let ((newstring (copy-sequence string)))
602 (set-text-properties 0 (length newstring) nil newstring)
603 newstring))
604
605(defcustom erc-prompt "ERC>"
606 "Prompt used by ERC. Trailing whitespace is not required."
607 :group 'erc-display
608 :type '(choice string function))
609
610(defun erc-prompt ()
611 "Return the input prompt as a string.
612
613See also the variable `erc-prompt'."
614 (let ((prompt (if (functionp erc-prompt)
615 (funcall erc-prompt)
616 erc-prompt)))
617 (if (> (length prompt) 0)
618 (concat prompt " ")
619 prompt)))
620
621(defcustom erc-command-indicator nil
622 "Indicator used by ERC for showing commands.
623
624If non-nil, this will be used in the ERC buffer to indicate
625commands (i.e., input starting with a '/').
626
627If nil, the prompt will be constructed from the variable `erc-prompt'."
628 :group 'erc-display
629 :type '(choice (const nil) string function))
630
631(defun erc-command-indicator ()
632 "Return the command indicator prompt as a string.
633
634This only has any meaning if the variable `erc-command-indicator' is non-nil."
635 (and erc-command-indicator
636 (let ((prompt (if (functionp erc-command-indicator)
637 (funcall erc-command-indicator)
638 erc-command-indicator)))
639 (if (> (length prompt) 0)
640 (concat prompt " ")
641 prompt))))
642
643(defcustom erc-notice-prefix "*** "
fb7ada5f 644 "Prefix for all notices."
597993cf
MB
645 :group 'erc-display
646 :type 'string)
647
648(defcustom erc-notice-highlight-type 'all
fb7ada5f 649 "Determines how to highlight notices.
597993cf
MB
650See `erc-notice-prefix'.
651
652The following values are allowed:
653
654 'prefix - highlight notice prefix only
655 'all - highlight the entire notice
656
657Any other value disables notice's highlighting altogether."
658 :group 'erc-display
659 :type '(choice (const :tag "highlight notice prefix only" prefix)
660 (const :tag "highlight the entire notice" all)
661 (const :tag "don't highlight notices at all" nil)))
662
663(defcustom erc-echo-notice-hook nil
d0fcaff5
SM
664 "List of functions to call to echo a private notice.
665Each function is called with four arguments, the string
597993cf
MB
666to display, the parsed server message, the target buffer (or
667nil), and the sender. The functions are called in order, until a
668function evaluates to non-nil. These hooks are called after
669those specified in `erc-echo-notice-always-hook'.
670
671See also: `erc-echo-notice-always-hook',
672`erc-echo-notice-in-default-buffer',
673`erc-echo-notice-in-target-buffer',
674`erc-echo-notice-in-minibuffer',
675`erc-echo-notice-in-server-buffer',
676`erc-echo-notice-in-active-non-server-buffer',
677`erc-echo-notice-in-active-buffer',
678`erc-echo-notice-in-user-buffers',
679`erc-echo-notice-in-user-and-target-buffers',
680`erc-echo-notice-in-first-user-buffer'"
681 :group 'erc-hooks
682 :type 'hook
683 :options '(erc-echo-notice-in-default-buffer
684 erc-echo-notice-in-target-buffer
685 erc-echo-notice-in-minibuffer
686 erc-echo-notice-in-server-buffer
687 erc-echo-notice-in-active-non-server-buffer
688 erc-echo-notice-in-active-buffer
689 erc-echo-notice-in-user-buffers
690 erc-echo-notice-in-user-and-target-buffers
691 erc-echo-notice-in-first-user-buffer))
692
693(defcustom erc-echo-notice-always-hook
694 '(erc-echo-notice-in-default-buffer)
d0fcaff5
SM
695 "List of functions to call to echo a private notice.
696Each function is called with four arguments, the string
597993cf
MB
697to display, the parsed server message, the target buffer (or
698nil), and the sender. The functions are called in order, and all
699functions are called. These hooks are called before those
700specified in `erc-echo-notice-hook'.
701
702See also: `erc-echo-notice-hook',
703`erc-echo-notice-in-default-buffer',
704`erc-echo-notice-in-target-buffer',
705`erc-echo-notice-in-minibuffer',
706`erc-echo-notice-in-server-buffer',
707`erc-echo-notice-in-active-non-server-buffer',
708`erc-echo-notice-in-active-buffer',
709`erc-echo-notice-in-user-buffers',
710`erc-echo-notice-in-user-and-target-buffers',
711`erc-echo-notice-in-first-user-buffer'"
712 :group 'erc-hooks
713 :type 'hook
714 :options '(erc-echo-notice-in-default-buffer
715 erc-echo-notice-in-target-buffer
716 erc-echo-notice-in-minibuffer
717 erc-echo-notice-in-server-buffer
718 erc-echo-notice-in-active-non-server-buffer
719 erc-echo-notice-in-active-buffer
720 erc-echo-notice-in-user-buffers
721 erc-echo-notice-in-user-and-target-buffers
722 erc-echo-notice-in-first-user-buffer))
723
724;; other tunable parameters
725
726(defcustom erc-whowas-on-nosuchnick nil
fb7ada5f 727 "If non-nil, do a whowas on a nick if no such nick."
597993cf
MB
728 :group 'erc
729 :type 'boolean)
730
731(defcustom erc-verbose-server-ping nil
fb7ada5f 732 "If non-nil, show every time you get a PING or PONG from the server."
597993cf
MB
733 :group 'erc-paranoia
734 :type 'boolean)
735
736(defcustom erc-public-away-p nil
fb7ada5f 737 "Let others know you are back when you are no longer marked away.
597993cf
MB
738This happens in this form:
739* <nick> is back (gone for <time>)
740
741Many consider it impolite to do so automatically."
742 :group 'erc
743 :type 'boolean)
744
745(defcustom erc-away-nickname nil
fb7ada5f 746 "The nickname to take when you are marked as being away."
597993cf
MB
747 :group 'erc
748 :type '(choice (const nil)
749 string))
750
751(defcustom erc-paranoid nil
752 "If non-nil, then all incoming CTCP requests will be shown."
753 :group 'erc-paranoia
754 :type 'boolean)
755
756(defcustom erc-disable-ctcp-replies nil
757 "Disable replies to CTCP requests that require a reply.
758If non-nil, then all incoming CTCP requests that normally require
759an automatic reply (like VERSION or PING) will be ignored. Good to
760set if some hacker is trying to flood you away."
761 :group 'erc-paranoia
762 :type 'boolean)
763
764(defcustom erc-anonymous-login t
765 "Be paranoid, don't give away your machine name."
766 :group 'erc-paranoia
767 :type 'boolean)
768
769(defcustom erc-prompt-for-channel-key nil
a3d2f0a3 770 "Prompt for channel key when using `erc-join-channel' interactively."
597993cf
MB
771 :group 'erc
772 :type 'boolean)
773
774(defcustom erc-email-userid "user"
775 "Use this as your email user ID."
776 :group 'erc
777 :type 'string)
778
6904f7fe
MB
779(defcustom erc-system-name nil
780 "Use this as the name of your system.
781If nil, ERC will call `system-name' to get this information."
782 :group 'erc
783 :type '(choice (const :tag "Default system name" nil)
784 string))
785
597993cf 786(defcustom erc-ignore-list nil
fb7ada5f 787 "List of regexps matching user identifiers to ignore.
597993cf
MB
788
789A user identifier has the form \"nick!login@host\". If an
790identifier matches, the message from the person will not be
791processed."
792 :group 'erc-ignore
793 :type '(repeat regexp))
794(make-variable-buffer-local 'erc-ignore-list)
795
796(defcustom erc-ignore-reply-list nil
fb7ada5f 797 "List of regexps matching user identifiers to ignore completely.
597993cf
MB
798
799This differs from `erc-ignore-list' in that it also ignores any
800messages directed at the user.
801
802A user identifier has the form \"nick!login@host\".
803
804If an identifier matches, or a message is addressed to a nick
805whose identifier matches, the message will not be processed.
806
807CAVEAT: ERC doesn't know about the user and host of anyone who
808was already in the channel when you joined, but never said
809anything, so it won't be able to match the user and host of those
a3d2f0a3 810people. You can update the ERC internal info using /WHO *."
597993cf
MB
811 :group 'erc-ignore
812 :type '(repeat regexp))
813
814(defvar erc-flood-protect t
fb7ada5f 815 "If non-nil, flood protection is enabled.
597993cf
MB
816Flooding is sending too much information to the server in too
817short of an interval, which may cause the server to terminate the
818connection.
819
820See `erc-server-flood-margin' for other flood-related parameters.")
821
822;; Script parameters
823
824(defcustom erc-startup-file-list
526dc846
MO
825 (list (concat erc-user-emacs-directory ".ercrc.el")
826 (concat erc-user-emacs-directory ".ercrc")
827 "~/.ercrc.el" "~/.ercrc" ".ercrc.el" ".ercrc")
597993cf
MB
828 "List of files to try for a startup script.
829The first existent and readable one will get executed.
830
a3d2f0a3 831If the filename ends with `.el' it is presumed to be an Emacs Lisp
680a9905 832script and it gets (load)ed. Otherwise it is treated as a bunch of
a3d2f0a3 833regular IRC commands."
597993cf
MB
834 :group 'erc-scripts
835 :type '(repeat file))
836
837(defcustom erc-script-path nil
838 "List of directories to look for a script in /load command.
839The script is first searched in the current directory, then in each
840directory in the list."
841 :group 'erc-scripts
842 :type '(repeat directory))
843
844(defcustom erc-script-echo t
fb7ada5f 845 "If non-nil, echo the IRC script commands locally."
597993cf
MB
846 :group 'erc-scripts
847 :type 'boolean)
848
849(defvar erc-last-saved-position nil
850 "A marker containing the position the current buffer was last saved at.")
851(make-variable-buffer-local 'erc-last-saved-position)
852
853(defcustom erc-kill-buffer-on-part nil
854 "Kill the channel buffer on PART.
855This variable should probably stay nil, as ERC can reuse buffers if
856you rejoin them later."
857 :group 'erc-quit-and-part
858 :type 'boolean)
859
860(defcustom erc-kill-queries-on-quit nil
861 "Kill all query (also channel) buffers of this server on QUIT.
862See the variable `erc-kill-buffer-on-part' for details."
863 :group 'erc-quit-and-part
864 :type 'boolean)
865
866(defcustom erc-kill-server-buffer-on-quit nil
867 "Kill the server buffer of the process on QUIT."
868 :group 'erc-quit-and-part
869 :type 'boolean)
870
871(defcustom erc-quit-reason-various-alist nil
872 "Alist of possible arguments to the /quit command.
873
874Each element has the form:
875 (REGEXP RESULT)
876
877If REGEXP matches the argument to /quit, then its relevant RESULT
878will be used. RESULT may be either a string, or a function. If
879a function, it should return the quit message as a string.
880
881If no elements match, then the empty string is used.
882
883As an example:
884 (setq erc-quit-reason-various-alist
dc2f2f99 885 '((\"xmms\" dme:now-playing)
597993cf
MB
886 (\"version\" erc-quit-reason-normal)
887 (\"home\" \"Gone home !\")
0b6bb130 888 (\"^$\" \"Default Reason\")))
dc2f2f99
GM
889If the user types \"/quit home\", then \"Gone home !\" will be used
890as the quit message."
597993cf
MB
891 :group 'erc-quit-and-part
892 :type '(repeat (list regexp (choice (string) (function)))))
893
894(defcustom erc-part-reason-various-alist nil
895 "Alist of possible arguments to the /part command.
896
897Each element has the form:
898 (REGEXP RESULT)
899
900If REGEXP matches the argument to /part, then its relevant RESULT
901will be used. RESULT may be either a string, or a function. If
902a function, it should return the part message as a string.
903
904If no elements match, then the empty string is used.
905
906As an example:
907 (setq erc-part-reason-various-alist
dc2f2f99 908 '((\"xmms\" dme:now-playing)
597993cf
MB
909 (\"version\" erc-part-reason-normal)
910 (\"home\" \"Gone home !\")
0b6bb130 911 (\"^$\" \"Default Reason\")))
dc2f2f99
GM
912If the user types \"/part home\", then \"Gone home !\" will be used
913as the part message."
597993cf
MB
914 :group 'erc-quit-and-part
915 :type '(repeat (list regexp (choice (string) (function)))))
916
917(defcustom erc-quit-reason 'erc-quit-reason-normal
fb7ada5f 918 "A function which returns the reason for quitting.
597993cf
MB
919
920The function is passed a single argument, the string typed by the
921user after \"/quit\"."
922 :group 'erc-quit-and-part
923 :type '(choice (const erc-quit-reason-normal)
597993cf
MB
924 (const erc-quit-reason-various)
925 (symbol)))
926
927(defcustom erc-part-reason 'erc-part-reason-normal
928 "A function which returns the reason for parting a channel.
929
930The function is passed a single argument, the string typed by the
931user after \"/PART\"."
932 :group 'erc-quit-and-part
933 :type '(choice (const erc-part-reason-normal)
597993cf
MB
934 (const erc-part-reason-various)
935 (symbol)))
936
937(defvar erc-grab-buffer-name "*erc-grab*"
938 "The name of the buffer created by `erc-grab-region'.")
939
940;; variables available for IRC scripts
941
942(defvar erc-user-information "ERC User"
943 "USER_INFORMATION IRC variable.")
944
945;; Hooks
946
947(defgroup erc-hooks nil
948 "Hook variables for fancy customizations of ERC."
949 :group 'erc)
950
951(defcustom erc-mode-hook nil
952 "Hook run after `erc-mode' setup is finished."
953 :group 'erc-hooks
954 :type 'hook
955 :options '(erc-add-scroll-to-bottom))
956
957(defcustom erc-timer-hook nil
958 "Put functions which should get called more or less periodically here.
959The idea is that servers always play ping pong with the client, and so there
960is no need for any idle-timer games with Emacs."
961 :group 'erc-hooks
962 :type 'hook)
963
964(defcustom erc-insert-pre-hook nil
965 "Hook called first when some text is inserted through `erc-display-line'.
966It gets called with one argument, STRING.
967To be able to modify the inserted text, use `erc-insert-modify-hook' instead.
968Filtering functions can set `erc-insert-this' to nil to avoid
969display of that particular string at all."
970 :group 'erc-hooks
971 :type 'hook)
972
973(defcustom erc-send-pre-hook nil
974 "Hook called first when some text is sent through `erc-send-current-line'.
975It gets called with one argument, STRING.
976
977To change the text that will be sent, set the variable STR which is
978used in `erc-send-current-line'.
979
980To change the text inserted into the buffer without changing the text
981that will be sent, use `erc-send-modify-hook' instead.
982
983Filtering functions can set `erc-send-this' to nil to avoid sending of
984that particular string at all and `erc-insert-this' to prevent
985inserting that particular string into the buffer.
986
987Note that it's useless to set `erc-send-this' to nil and
988`erc-insert-this' to t. ERC is sane enough to not insert the text
989anyway."
990 :group 'erc-hooks
991 :type 'hook)
992
993(defvar erc-insert-this t
994 "Insert the text into the target buffer or not.
995Functions on `erc-insert-pre-hook' can set this variable to nil
996if they wish to avoid insertion of a particular string.")
997
998(defvar erc-send-this t
999 "Send the text to the target or not.
1000Functions on `erc-send-pre-hook' can set this variable to nil
1001if they wish to avoid sending of a particular string.")
1002
1003(defcustom erc-insert-modify-hook ()
1004 "Insertion hook for functions that will change the text's appearance.
1005This hook is called just after `erc-insert-pre-hook' when the value
1006of `erc-insert-this' is t.
1007While this hook is run, narrowing is in effect and `current-buffer' is
1008the buffer where the text got inserted. One possible value to add here
1009is `erc-fill'."
1010 :group 'erc-hooks
1011 :type 'hook)
1012
1013(defcustom erc-insert-post-hook nil
1014 "This hook is called just after `erc-insert-modify-hook'.
1015At this point, all modifications from prior hook functions are done."
1016 :group 'erc-hooks
1017 :type 'hook
1018 :options '(erc-truncate-buffer
1019 erc-make-read-only
1020 erc-save-buffer-in-logs))
1021
1022(defcustom erc-send-modify-hook nil
1023 "Sending hook for functions that will change the text's appearance.
1024This hook is called just after `erc-send-pre-hook' when the values
1025of `erc-send-this' and `erc-insert-this' are both t.
1026While this hook is run, narrowing is in effect and `current-buffer' is
1027the buffer where the text got inserted.
1028
1029Note that no function in this hook can change the appearance of the
1030text that is sent. Only changing the sent text's appearance on the
1031sending user's screen is possible. One possible value to add here
1032is `erc-fill'."
1033 :group 'erc-hooks
1034 :type 'hook)
1035
1036(defcustom erc-send-post-hook nil
1037 "This hook is called just after `erc-send-modify-hook'.
1038At this point, all modifications from prior hook functions are done.
1039NOTE: The functions on this hook are called _before_ sending a command
1040to the server.
1041
a3d2f0a3 1042This function is called with narrowing, ala `erc-send-modify-hook'."
597993cf
MB
1043 :group 'erc-hooks
1044 :type 'hook
1045 :options '(erc-make-read-only))
1046
1047(defcustom erc-send-completed-hook
d0fcaff5 1048 (when (fboundp 'emacspeak-auditory-icon)
597993cf 1049 (list (byte-compile
d0fcaff5 1050 (lambda (_str)
597993cf
MB
1051 (emacspeak-auditory-icon 'select-object)))))
1052 "Hook called after a message has been parsed by ERC.
1053
1054The single argument to the functions is the unmodified string
1055which the local user typed."
1056 :group 'erc-hooks
1057 :type 'hook)
1058;; mode-specific tables
1059
1060(defvar erc-mode-syntax-table
1061 (let ((syntax-table (make-syntax-table)))
1062 (modify-syntax-entry ?\" ". " syntax-table)
1063 (modify-syntax-entry ?\\ ". " syntax-table)
1064 (modify-syntax-entry ?' "w " syntax-table)
1065 ;; Make dabbrev-expand useful for nick names
1066 (modify-syntax-entry ?< "." syntax-table)
1067 (modify-syntax-entry ?> "." syntax-table)
1068 syntax-table)
1069 "Syntax table used while in ERC mode.")
1070
1071(defvar erc-mode-abbrev-table nil
1072 "Abbrev table used while in ERC mode.")
1073(define-abbrev-table 'erc-mode-abbrev-table ())
1074
1075(defvar erc-mode-map
1076 (let ((map (make-sparse-keymap)))
1077 (define-key map "\C-m" 'erc-send-current-line)
1078 (define-key map "\C-a" 'erc-bol)
1079 (define-key map [home] 'erc-bol)
1080 (define-key map "\C-c\C-a" 'erc-bol)
1081 (define-key map "\C-c\C-b" 'erc-iswitchb)
1082 (define-key map "\C-c\C-c" 'erc-toggle-interpret-controls)
1083 (define-key map "\C-c\C-d" 'erc-input-action)
1084 (define-key map "\C-c\C-e" 'erc-toggle-ctcp-autoresponse)
1085 (define-key map "\C-c\C-f" 'erc-toggle-flood-control)
1086 (define-key map "\C-c\C-i" 'erc-invite-only-mode)
1087 (define-key map "\C-c\C-j" 'erc-join-channel)
1088 (define-key map "\C-c\C-n" 'erc-channel-names)
1089 (define-key map "\C-c\C-o" 'erc-get-channel-mode-from-keypress)
1090 (define-key map "\C-c\C-p" 'erc-part-from-channel)
1091 (define-key map "\C-c\C-q" 'erc-quit-server)
1092 (define-key map "\C-c\C-r" 'erc-remove-text-properties-region)
1093 (define-key map "\C-c\C-t" 'erc-set-topic)
1094 (define-key map "\C-c\C-u" 'erc-kill-input)
2a927019 1095 (define-key map "\C-c\C-x" 'erc-quit-server)
597993cf 1096 (define-key map "\M-\t" 'ispell-complete-word)
d4aa710a 1097 (define-key map "\t" 'completion-at-point)
597993cf
MB
1098
1099 ;; Suppress `font-lock-fontify-block' key binding since it
1100 ;; destroys face properties.
d0fcaff5 1101 (define-key map [remap font-lock-fontify-block] 'undefined)
597993cf
MB
1102
1103 map)
1104 "ERC keymap.")
1105
1106;; Faces
1107
1108; Honestly, I have a horrible sense of color and the "defaults" below
1109; are supposed to be really bad. But colors ARE required in IRC to
1110; convey different parts of conversation. If you think you know better
1111; defaults - send them to me.
1112
1113;; Now colors are a bit nicer, at least to my eyes.
1114;; You may still want to change them to better fit your background.-- S.B.
1115
1116(defgroup erc-faces nil
1117 "Faces for ERC."
1118 :group 'erc)
1119
1120(defface erc-default-face '((t))
1121 "ERC default face."
1122 :group 'erc-faces)
1123
4b56d0fe 1124(defface erc-direct-msg-face '((t :foreground "IndianRed"))
597993cf
MB
1125 "ERC face used for messages you receive in the main erc buffer."
1126 :group 'erc-faces)
1127
c89e5cd7 1128(defface erc-header-line
4b56d0fe 1129 '((t :foreground "grey20" :background "grey90"))
c89e5cd7
MB
1130 "ERC face used for the header line.
1131
1132This will only be used if `erc-header-line-face-method' is non-nil."
1133 :group 'erc-faces)
1134
4b56d0fe 1135(defface erc-input-face '((t :foreground "brown"))
597993cf
MB
1136 "ERC face used for your input."
1137 :group 'erc-faces)
1138
1139(defface erc-prompt-face
4b56d0fe 1140 '((t :weight bold :foreground "Black" :background "lightBlue2"))
597993cf
MB
1141 "ERC face for the prompt."
1142 :group 'erc-faces)
1143
1144(defface erc-command-indicator-face
4b56d0fe 1145 '((t :weight bold))
597993cf
MB
1146 "ERC face for the command indicator.
1147See the variable `erc-command-indicator'."
1148 :group 'erc-faces)
1149
5e56b3fb 1150(defface erc-notice-face
4b56d0fe
CY
1151 '((default :weight bold)
1152 (((class color) (min-colors 88)) :foreground "SlateBlue")
1153 (t :foreground "blue"))
597993cf
MB
1154 "ERC face for notices."
1155 :group 'erc-faces)
1156
4b56d0fe 1157(defface erc-action-face '((t :weight bold))
597993cf
MB
1158 "ERC face for actions generated by /ME."
1159 :group 'erc-faces)
1160
4b56d0fe 1161(defface erc-error-face '((t :foreground "red"))
597993cf
MB
1162 "ERC face for errors."
1163 :group 'erc-faces)
1164
83dc6995 1165;; same default color as `erc-input-face'
4b56d0fe 1166(defface erc-my-nick-face '((t :weight bold :foreground "brown"))
83dc6995
MB
1167 "ERC face for your current nickname in messages sent by you.
1168See also `erc-show-my-nick'."
1169 :group 'erc-faces)
1170
4b56d0fe 1171(defface erc-nick-default-face '((t :weight bold))
597993cf
MB
1172 "ERC nickname default face."
1173 :group 'erc-faces)
1174
4b56d0fe 1175(defface erc-nick-msg-face '((t :weight bold :foreground "IndianRed"))
597993cf
MB
1176 "ERC nickname face for private messages."
1177 :group 'erc-faces)
1178
1179;; Debugging support
1180
1181(defvar erc-log-p nil
1182 "When set to t, generate debug messages in a separate debug buffer.")
1183
1184(defvar erc-debug-log-file (expand-file-name "ERC.debug")
1185 "Debug log file name.")
1186
1187(defvar erc-dbuf nil)
1188(make-variable-buffer-local 'erc-dbuf)
1189
1190(defmacro define-erc-module (name alias doc enable-body disable-body
1191 &optional local-p)
1192 "Define a new minor mode using ERC conventions.
1193Symbol NAME is the name of the module.
1194Symbol ALIAS is the alias to use, or nil.
1195DOC is the documentation string to use for the minor mode.
1196ENABLE-BODY is a list of expressions used to enable the mode.
1197DISABLE-BODY is a list of expressions used to disable the mode.
1198If LOCAL-P is non-nil, the mode will be created as a buffer-local
a3d2f0a3 1199mode, rather than a global one.
597993cf
MB
1200
1201This will define a minor mode called erc-NAME-mode, possibly
1202an alias erc-ALIAS-mode, as well as the helper functions
1203erc-NAME-enable, and erc-NAME-disable.
1204
1205Example:
1206
1207 ;;;###autoload (autoload 'erc-replace-mode \"erc-replace\")
1208 (define-erc-module replace nil
1209 \"This mode replaces incoming text according to `erc-replace-alist'.\"
1210 ((add-hook 'erc-insert-modify-hook
1211 'erc-replace-insert))
1212 ((remove-hook 'erc-insert-modify-hook
1213 'erc-replace-insert)))"
8599b23a 1214 (declare (doc-string 3))
597993cf
MB
1215 (let* ((sn (symbol-name name))
1216 (mode (intern (format "erc-%s-mode" (downcase sn))))
1217 (group (intern (format "erc-%s" (downcase sn))))
1218 (enable (intern (format "erc-%s-enable" (downcase sn))))
1219 (disable (intern (format "erc-%s-disable" (downcase sn)))))
1220 `(progn
1221 (erc-define-minor-mode
1222 ,mode
1223 ,(format "Toggle ERC %S mode.
ac6c8639
CY
1224With a prefix argument ARG, enable %s if ARG is positive,
1225and disable it otherwise. If called from Lisp, enable the mode
1226if ARG is omitted or nil.
597993cf
MB
1227%s" name name doc)
1228 nil nil nil
1229 :global ,(not local-p) :group (quote ,group)
1230 (if ,mode
1231 (,enable)
1232 (,disable)))
1233 (defun ,enable ()
1234 ,(format "Enable ERC %S mode."
1235 name)
1236 (interactive)
1237 (add-to-list 'erc-modules (quote ,name))
1238 (setq ,mode t)
1239 ,@enable-body)
1240 (defun ,disable ()
1241 ,(format "Disable ERC %S mode."
1242 name)
1243 (interactive)
1244 (setq erc-modules (delq (quote ,name) erc-modules))
1245 (setq ,mode nil)
1246 ,@disable-body)
1247 ,(when (and alias (not (eq name alias)))
1248 `(defalias
1249 (quote
1250 ,(intern
1251 (format "erc-%s-mode"
1252 (downcase (symbol-name alias)))))
1253 (quote
2e3ef421
MB
1254 ,mode)))
1255 ;; For find-function and find-variable.
1256 (put ',mode 'definition-name ',name)
1257 (put ',enable 'definition-name ',name)
1258 (put ',disable 'definition-name ',name))))
597993cf 1259
d0fcaff5
SM
1260(defun erc-once-with-server-event (event f)
1261 "Run function F the next time EVENT occurs in the `current-buffer'.
597993cf
MB
1262
1263You should make sure that `current-buffer' is a server buffer.
1264
d0fcaff5
SM
1265This function temporarily adds a function to EVENT's hook to call F with
1266two arguments (`proc' and `parsed'). After F is called, the function is
1267removed from EVENT's hook. F should return either nil
a3d2f0a3 1268or t, where nil indicates that the other functions on EVENT's hook
597993cf
MB
1269should be run too, and t indicates that other functions should
1270not be run.
1271
1272Please be sure to use this function in server-buffers. In
1273channel-buffers it may not work at all, as it uses the LOCAL
1274argument of `add-hook' and `remove-hook' to ensure multiserver
1275capabilities."
1276 (unless (erc-server-buffer-p)
1277 (error
1278 "You should only run `erc-once-with-server-event' in a server buffer"))
ff59d266 1279 (let ((fun (make-symbol "fun"))
597993cf 1280 (hook (erc-get-hook event)))
d0fcaff5
SM
1281 (put fun 'erc-original-buffer (current-buffer))
1282 (fset fun (lambda (proc parsed)
1283 (with-current-buffer (get fun 'erc-original-buffer)
1284 (remove-hook hook fun t))
1285 (fmakunbound fun)
1286 (funcall f proc parsed)))
1287 (add-hook hook fun nil t)
1288 fun))
597993cf 1289
8599b23a 1290(defsubst erc-log (string)
597993cf 1291 "Logs STRING if logging is on (see `erc-log-p')."
8599b23a
SM
1292 (when erc-log-p
1293 (erc-log-aux string)))
597993cf
MB
1294
1295(defun erc-server-buffer ()
1296 "Return the server buffer for the current buffer's process.
1297The buffer-local variable `erc-server-process' is used to find
1298the process buffer."
1299 (and (erc-server-buffer-live-p)
1300 (process-buffer erc-server-process)))
1301
1302(defun erc-server-buffer-live-p ()
83dc6995 1303 "Return t if the server buffer has not been killed."
597993cf
MB
1304 (and (processp erc-server-process)
1305 (buffer-live-p (process-buffer erc-server-process))))
1306
1307(defun erc-server-buffer-p (&optional buffer)
1308 "Return non-nil if argument BUFFER is an ERC server buffer.
1309
1310If BUFFER is nil, the current buffer is used."
1311 (with-current-buffer (or buffer (current-buffer))
1312 (and (eq major-mode 'erc-mode)
1313 (null (erc-default-target)))))
1314
a022856b 1315(defun erc-open-server-buffer-p (&optional buffer)
01d9e862
MB
1316 "Return non-nil if argument BUFFER is an ERC server buffer that
1317has an open IRC process.
1318
1319If BUFFER is nil, the current buffer is used."
a022856b
GM
1320 (and (erc-server-buffer-p buffer)
1321 (erc-server-process-alive buffer)))
01d9e862 1322
597993cf
MB
1323(defun erc-query-buffer-p (&optional buffer)
1324 "Return non-nil if BUFFER is an ERC query buffer.
1325If BUFFER is nil, the current buffer is used."
1326 (with-current-buffer (or buffer (current-buffer))
1327 (let ((target (erc-default-target)))
1328 (and (eq major-mode 'erc-mode)
1329 target
1330 (not (memq (aref target 0) '(?# ?& ?+ ?!)))))))
1331
1332(defun erc-ison-p (nick)
1333 "Return non-nil if NICK is online."
1334 (interactive "sNick: ")
ff59d266 1335 (erc-with-server-buffer
597993cf
MB
1336 (let ((erc-online-p 'unknown))
1337 (erc-once-with-server-event
1338 303
d0fcaff5
SM
1339 (lambda (_proc parsed)
1340 (let ((ison (split-string (aref parsed 3))))
1341 (setq erc-online-p (car (erc-member-ignore-case nick ison)))
1342 t)))
597993cf
MB
1343 (erc-server-send (format "ISON %s" nick))
1344 (while (eq erc-online-p 'unknown) (accept-process-output))
19dc7206 1345 (if (called-interactively-p 'interactive)
597993cf
MB
1346 (message "%s is %sonline"
1347 (or erc-online-p nick)
1348 (if erc-online-p "" "not "))
1349 erc-online-p))))
1350
1351(defun erc-log-aux (string)
1352 "Do the debug logging of STRING."
1353 (let ((cb (current-buffer))
1354 (point 1)
1355 (was-eob nil)
1356 (session-buffer (erc-server-buffer)))
1357 (if session-buffer
1358 (progn
1359 (set-buffer session-buffer)
1360 (if (not (and erc-dbuf (bufferp erc-dbuf) (buffer-live-p erc-dbuf)))
1361 (progn
1362 (setq erc-dbuf (get-buffer-create
1363 (concat "*ERC-DEBUG: "
1364 erc-session-server "*")))))
1365 (set-buffer erc-dbuf)
1366 (setq point (point))
1367 (setq was-eob (eobp))
1368 (goto-char (point-max))
1369 (insert (concat "** " string "\n"))
1370 (if was-eob (goto-char (point-max))
1371 (goto-char point))
1372 (set-buffer cb))
1373 (message "ERC: ** %s" string))))
1374
1375;; Last active buffer, to print server messages in the right place
1376
1377(defvar erc-active-buffer nil
1378 "The current active buffer, the one where the user typed the last command.
1379Defaults to the server buffer, and should only be set in the
a3d2f0a3 1380server buffer.")
597993cf
MB
1381(make-variable-buffer-local 'erc-active-buffer)
1382
1383(defun erc-active-buffer ()
1384 "Return the value of `erc-active-buffer' for the current server.
1385Defaults to the server buffer."
ff59d266 1386 (erc-with-server-buffer
0b6bb130 1387 (if (buffer-live-p erc-active-buffer)
2e3ef421
MB
1388 erc-active-buffer
1389 (setq erc-active-buffer (current-buffer)))))
597993cf
MB
1390
1391(defun erc-set-active-buffer (buffer)
1392 "Set the value of `erc-active-buffer' to BUFFER."
1393 (cond ((erc-server-buffer)
1394 (with-current-buffer (erc-server-buffer)
1395 (setq erc-active-buffer buffer)))
1396 (t (setq erc-active-buffer buffer))))
1397
1398;; Mode activation routines
1399
4d789d84
SM
1400(define-derived-mode erc-mode fundamental-mode "ERC"
1401 "Major mode for Emacs IRC."
1402 (setq local-abbrev-table erc-mode-abbrev-table)
597993cf
MB
1403 (when (boundp 'next-line-add-newlines)
1404 (set (make-local-variable 'next-line-add-newlines) nil))
1405 (setq line-move-ignore-invisible t)
1406 (set (make-local-variable 'paragraph-separate)
1407 (concat "\C-l\\|\\(^" (regexp-quote (erc-prompt)) "\\)"))
1408 (set (make-local-variable 'paragraph-start)
d4aa710a
SM
1409 (concat "\\(" (regexp-quote (erc-prompt)) "\\)"))
1410 (add-hook 'completion-at-point-functions 'erc-complete-word-at-point nil t))
597993cf
MB
1411
1412;; activation
1413
1414(defconst erc-default-server "irc.freenode.net"
1415 "IRC server to use if it cannot be detected otherwise.")
1416
526dc846 1417(defconst erc-default-port 6667
597993cf
MB
1418 "IRC port to use if it cannot be detected otherwise.")
1419
1420(defcustom erc-join-buffer 'buffer
5e56b3fb
MO
1421 "Determines how to display a newly created IRC buffer.
1422
1423The available choices are:
1424
1425 'window - in another window,
1426 'window-noselect - in another window, but don't select that one,
1427 'frame - in another frame,
1428 'bury - bury it in a new buffer,
1429 'buffer - in place of the current buffer,
1430 any other value - in place of the current buffer."
597993cf 1431 :group 'erc-buffers
5e56b3fb
MO
1432 :type '(choice (const :tag "Split window and select" window)
1433 (const :tag "Split window, don't select" window-noselect)
1434 (const :tag "New frame" frame)
1435 (const :tag "Bury in new buffer" bury)
1436 (const :tag "Use current buffer" buffer)
1437 (const :tag "Use current buffer" t)))
597993cf
MB
1438
1439(defcustom erc-frame-alist nil
fb7ada5f 1440 "Alist of frame parameters for creating erc frames.
a3d2f0a3 1441A value of nil means to use `default-frame-alist'."
597993cf
MB
1442 :group 'erc-buffers
1443 :type '(repeat (cons :format "%v"
1444 (symbol :tag "Parameter")
1445 (sexp :tag "Value"))))
1446
1447(defcustom erc-frame-dedicated-flag nil
fb7ada5f 1448 "Non-nil means the erc frames are dedicated to that buffer.
597993cf
MB
1449This only has effect when `erc-join-buffer' is set to `frame'."
1450 :group 'erc-buffers
1451 :type 'boolean)
1452
526dc846 1453(defcustom erc-reuse-frames t
fb7ada5f 1454 "Determines whether new frames are always created.
526dc846
MO
1455Non-nil means that a new frame is not created to display an ERC
1456buffer if there is already a window displaying it. This only has
1457effect when `erc-join-buffer' is set to `frame'."
1458 :group 'erc-buffers
1459 :type 'boolean)
1460
597993cf
MB
1461(defun erc-channel-p (channel)
1462 "Return non-nil if CHANNEL seems to be an IRC channel name."
1463 (cond ((stringp channel)
1464 (memq (aref channel 0) '(?# ?& ?+ ?!)))
1465 ((and (bufferp channel) (buffer-live-p channel))
1466 (with-current-buffer channel
1467 (erc-channel-p (erc-default-target))))
1468 (t nil)))
1469
1470(defcustom erc-reuse-buffers t
fb7ada5f 1471 "If nil, create new buffers on joining a channel/query.
597993cf
MB
1472If non-nil, a new buffer will only be created when you join
1473channels with same names on different servers, or have query buffers
1474open with nicks of the same name on different servers. Otherwise,
1475the existing buffers will be reused."
1476 :group 'erc-buffers
1477 :type 'boolean)
1478
1479(defun erc-normalize-port (port)
1480 "Normalize the port specification PORT to integer form.
1481PORT may be an integer, a string or a symbol. If it is a string or a
1482symbol, it may have these values:
1483* irc -> 194
1484* ircs -> 994
1485* ircd -> 6667
1486* ircd-dalnet -> 7000"
1487 (cond
1488 ((symbolp port)
1489 (erc-normalize-port (symbol-name port)))
1490 ((stringp port)
1491 (let ((port-nr (string-to-number port)))
1492 (cond
1493 ((> port-nr 0)
1494 port-nr)
1495 ((string-equal port "irc")
1496 194)
1497 ((string-equal port "ircs")
1498 994)
1499 ((string-equal port "ircd")
1500 6667)
1501 ((string-equal port "ircd-dalnet")
1502 7000)
1503 (t
1504 nil))))
1505 ((numberp port)
1506 port)
1507 (t
1508 nil)))
1509
1510(defun erc-port-equal (a b)
1511 "Check whether ports A and B are equal."
1512 (= (erc-normalize-port a) (erc-normalize-port b)))
1513
d0fcaff5 1514(defun erc-generate-new-buffer-name (server port target)
597993cf
MB
1515 "Create a new buffer name based on the arguments."
1516 (when (numberp port) (setq port (number-to-string port)))
c1e57b47
VD
1517 (let ((buf-name (or target
1518 (or (let ((name (concat server ":" port)))
1519 (when (> (length name) 1)
1520 name))
1521 ;; This fallback should in fact never happen
1522 "*erc-server-buffer*")))
1523 buffer-name)
597993cf
MB
1524 ;; Reuse existing buffers, but not if the buffer is a connected server
1525 ;; buffer and not if its associated with a different server than the
1526 ;; current ERC buffer.
c1e57b47
VD
1527 ;; if buf-name is taken by a different connection (or by something !erc)
1528 ;; then see if "buf-name/server" meets the same criteria
53964682 1529 (dolist (candidate (list buf-name (concat buf-name "/" server)))
c1e57b47
VD
1530 (if (and (not buffer-name)
1531 erc-reuse-buffers
1532 (get-buffer candidate)
1533 (or target
1534 (with-current-buffer (get-buffer candidate)
1535 (and (erc-server-buffer-p)
1536 (not (erc-server-process-alive)))))
1537 (with-current-buffer (get-buffer candidate)
1538 (and (string= erc-session-server server)
1539 (erc-port-equal erc-session-port port))))
1540 (setq buffer-name candidate)))
1541 ;; if buffer-name is unset, neither candidate worked out for us,
1542 ;; fallback to the old <N> uniquification method:
1543 (or buffer-name (generate-new-buffer-name buf-name)) ))
597993cf 1544
d0fcaff5 1545(defun erc-get-buffer-create (server port target)
597993cf 1546 "Create a new buffer based on the arguments."
d0fcaff5 1547 (get-buffer-create (erc-generate-new-buffer-name server port target)))
597993cf
MB
1548
1549
1550(defun erc-member-ignore-case (string list)
1551 "Return non-nil if STRING is a member of LIST.
1552
1553All strings are compared according to IRC protocol case rules, see
1554`erc-downcase'."
1555 (setq string (erc-downcase string))
1556 (catch 'result
1557 (while list
1558 (if (string= string (erc-downcase (car list)))
83dc6995
MB
1559 (throw 'result list)
1560 (setq list (cdr list))))))
597993cf
MB
1561
1562(defmacro erc-with-buffer (spec &rest body)
1563 "Execute BODY in the buffer associated with SPEC.
1564
1565SPEC should have the form
1566
1567 (TARGET [PROCESS])
1568
1569If TARGET is a buffer, use it. Otherwise, use the buffer
1570matching TARGET in the process specified by PROCESS.
1571
a3d2f0a3 1572If PROCESS is nil, use the current `erc-server-process'.
597993cf
MB
1573See `erc-get-buffer' for details.
1574
1575See also `with-current-buffer'.
1576
1577\(fn (TARGET [PROCESS]) BODY...)"
8599b23a 1578 (declare (indent 1) (debug ((form &optional form) body)))
ff59d266
MB
1579 (let ((buf (make-symbol "buf"))
1580 (proc (make-symbol "proc"))
1581 (target (make-symbol "target"))
1582 (process (make-symbol "process")))
597993cf
MB
1583 `(let* ((,target ,(car spec))
1584 (,process ,(cadr spec))
1585 (,buf (if (bufferp ,target)
1586 ,target
1587 (let ((,proc (or ,process
1588 (and (processp erc-server-process)
1589 erc-server-process))))
1590 (if (and ,target ,proc)
1591 (erc-get-buffer ,target ,proc))))))
ff59d266 1592 (when (buffer-live-p ,buf)
597993cf
MB
1593 (with-current-buffer ,buf
1594 ,@body)))))
597993cf
MB
1595
1596(defun erc-get-buffer (target &optional proc)
1597 "Return the buffer matching TARGET in the process PROC.
1598If PROC is not supplied, all processes are searched."
1599 (let ((downcased-target (erc-downcase target)))
1600 (catch 'buffer
1601 (erc-buffer-filter
1602 (lambda ()
1603 (let ((current (erc-default-target)))
1604 (and (stringp current)
1605 (string-equal downcased-target (erc-downcase current))
1606 (throw 'buffer (current-buffer)))))
1607 proc))))
1608
1609(defun erc-buffer-filter (predicate &optional proc)
1610 "Return a list of `erc-mode' buffers matching certain criteria.
1611PREDICATE is a function executed with each buffer, if it returns t, that buffer
1612is considered a valid match.
1613
1614PROC is either an `erc-server-process', identifying a certain
1615server connection, or nil which means all open connections."
1616 (save-excursion
1617 (delq
1618 nil
1619 (mapcar (lambda (buf)
0b6bb130
MB
1620 (when (buffer-live-p buf)
1621 (with-current-buffer buf
1622 (and (eq major-mode 'erc-mode)
1623 (or (not proc)
1624 (eq proc erc-server-process))
1625 (funcall predicate)
1626 buf))))
597993cf
MB
1627 (buffer-list)))))
1628
1629(defun erc-buffer-list (&optional predicate proc)
1630 "Return a list of ERC buffers.
1631PREDICATE is a function which executes with every buffer satisfying
1632the predicate. If PREDICATE is passed as nil, return a list of all ERC
1633buffers. If PROC is given, the buffers local variable `erc-server-process'
1634needs to match PROC."
1635 (unless predicate
1636 (setq predicate (lambda () t)))
1637 (erc-buffer-filter predicate proc))
1638
1639(defmacro erc-with-all-buffers-of-server (process pred &rest forms)
1640 "Execute FORMS in all buffers which have same process as this server.
1641FORMS will be evaluated in all buffers having the process PROCESS and
1642where PRED matches or in all buffers of the server process if PRED is
1643nil."
8599b23a 1644 (declare (indent 1) (debug (form form body)))
597993cf 1645 ;; Make the evaluation have the correct order
ff59d266
MB
1646 (let ((pre (make-symbol "pre"))
1647 (pro (make-symbol "pro")))
88406d6e
MO
1648 `(let* ((,pro ,process)
1649 (,pre ,pred)
1650 (res (mapcar (lambda (buffer)
1651 (with-current-buffer buffer
1652 ,@forms))
1653 (erc-buffer-list ,pre
1654 ,pro))))
1655 ;; Silence the byte-compiler by binding the result of mapcar to
1656 ;; a variable.
1657 res)))
597993cf 1658
e3abf9d9 1659;; (iswitchb-mode) will autoload iswitchb.el
5ba3c59a
GM
1660(defvar iswitchb-temp-buflist)
1661(declare-function iswitchb-read-buffer "iswitchb"
1662 (prompt &optional default require-match start matches-set))
d0fcaff5 1663(defvar iswitchb-make-buflist-hook)
5ba3c59a 1664
597993cf
MB
1665(defun erc-iswitchb (&optional arg)
1666 "Use `iswitchb-read-buffer' to prompt for a ERC buffer to switch to.
1667When invoked with prefix argument, use all erc buffers. Without prefix
1668ARG, allow only buffers related to same session server.
1669If `erc-track-mode' is in enabled, put the last element of
1670`erc-modified-channels-alist' in front of the buffer list.
1671
1672Due to some yet unresolved reason, global function `iswitchb-mode'
1673needs to be active for this function to work."
1674 (interactive "P")
5ba3c59a 1675 (let ((enabled (bound-and-true-p iswitchb-mode)))
16e62ecc 1676 (or enabled (iswitchb-mode 1))
10dc9f9e
MB
1677 (unwind-protect
1678 (let ((iswitchb-make-buflist-hook
1679 (lambda ()
1680 (setq iswitchb-temp-buflist
1681 (mapcar 'buffer-name
1682 (erc-buffer-list
1683 nil
1684 (when arg erc-server-process)))))))
1685 (switch-to-buffer
1686 (iswitchb-read-buffer
1687 "Switch-to: "
1688 (if (boundp 'erc-modified-channels-alist)
1689 (buffer-name (caar (last erc-modified-channels-alist)))
1690 nil)
1691 t)))
1692 (or enabled (iswitchb-mode -1)))))
597993cf
MB
1693
1694(defun erc-channel-list (proc)
1695 "Return a list of channel buffers.
1696PROC is the process for the server connection. If PROC is nil, return
1697all channel buffers on all servers."
1698 (erc-buffer-filter
1699 (lambda ()
1700 (and (erc-default-target)
1701 (erc-channel-p (erc-default-target))))
1702 proc))
1703
1704(defun erc-buffer-list-with-nick (nick proc)
1705 "Return buffers containing NICK in the `erc-channel-users' list."
1706 (with-current-buffer (process-buffer proc)
1707 (let ((user (gethash (erc-downcase nick) erc-server-users)))
1708 (if user
1709 (erc-server-user-buffers user)
1710 nil))))
1711
1712;; Some local variables
1713
1714(defvar erc-default-recipients nil
1715 "List of default recipients of the current buffer.")
1716(make-variable-buffer-local 'erc-default-recipients)
1717
1718(defvar erc-session-user-full-name nil
1719 "Full name of the user on the current server.")
1720(make-variable-buffer-local 'erc-session-user-full-name)
1721
1722(defvar erc-channel-user-limit nil
1723 "Limit of users per channel.")
1724(make-variable-buffer-local 'erc-channel-user-limit)
1725
1726(defvar erc-channel-key nil
1727 "Key needed to join channel.")
1728(make-variable-buffer-local 'erc-channel-key)
1729
1730(defvar erc-invitation nil
1731 "Last invitation channel.")
1732(make-variable-buffer-local 'erc-invitation)
1733
1734(defvar erc-away nil
ff59d266
MB
1735 "Non-nil indicates that we are away.
1736
1737Use `erc-away-time' to access this if you might be in a channel
1738buffer rather than a server buffer.")
597993cf
MB
1739(make-variable-buffer-local 'erc-away)
1740
1741(defvar erc-channel-list nil
1742 "Server channel list.")
1743(make-variable-buffer-local 'erc-channel-list)
1744
1745(defvar erc-bad-nick nil
1746 "Non-nil indicates that we got a `nick in use' error while connecting.")
1747(make-variable-buffer-local 'erc-bad-nick)
1748
1749(defvar erc-logged-in nil
1750 "Non-nil indicates that we are logged in.")
1751(make-variable-buffer-local 'erc-logged-in)
1752
1753(defvar erc-default-nicks nil
1754 "The local copy of `erc-nick' - the list of nicks to choose from.")
1755(make-variable-buffer-local 'erc-default-nicks)
1756
1757(defvar erc-nick-change-attempt-count 0
1758 "Used to keep track of how many times an attempt at changing nick is made.")
1759(make-variable-buffer-local 'erc-nick-change-attempt-count)
1760
c6b99621
MB
1761(defun erc-migrate-modules (mods)
1762 "Migrate old names of ERC modules to new ones."
1763 ;; modify `transforms' to specify what needs to be changed
9cc8d0b6
MB
1764 ;; each item is in the format '(old . new)
1765 (let ((transforms '((pcomplete . completion))))
1766 (erc-delete-dups
1767 (mapcar (lambda (m) (or (cdr (assoc m transforms)) m))
1768 mods))))
1769
1770(defcustom erc-modules '(netsplit fill button match track completion readonly
5e56b3fb
MO
1771 networks ring autojoin noncommands irccontrols
1772 move-to-prompt stamp menu list)
a3d2f0a3 1773 "A list of modules which ERC should enable.
597993cf
MB
1774If you set the value of this without using `customize' remember to call
1775\(erc-update-modules) after you change it. When using `customize', modules
1776removed from the list will be disabled."
c6b99621
MB
1777 :get (lambda (sym)
1778 ;; replace outdated names with their newer equivalents
1779 (erc-migrate-modules (symbol-value sym)))
597993cf
MB
1780 :set (lambda (sym val)
1781 ;; disable modules which have just been removed
1782 (when (and (boundp 'erc-modules) erc-modules val)
1783 (dolist (module erc-modules)
1784 (unless (member module val)
1785 (let ((f (intern-soft (format "erc-%s-mode" module))))
1786 (when (and (fboundp f) (boundp f) (symbol-value f))
1787 (message "Disabling `erc-%s'" module)
1788 (funcall f 0))))))
c6b99621 1789 (set sym val)
597993cf
MB
1790 ;; this test is for the case where erc hasn't been loaded yet
1791 (when (fboundp 'erc-update-modules)
1792 (erc-update-modules)))
9cc8d0b6
MB
1793 :type
1794 '(set
1795 :greedy t
ff59d266
MB
1796 (const :tag "autoaway: Set away status automatically" autoaway)
1797 (const :tag "autojoin: Join channels automatically" autojoin)
1798 (const :tag "button: Buttonize URLs, nicknames, and other text" button)
1799 (const :tag "capab: Mark unidentified users on servers supporting CAPAB"
1800 capab-identify)
1801 (const :tag "completion: Complete nicknames and commands (programmable)"
1802 completion)
6e5e9b70 1803 (const :tag "hecomplete: Complete nicknames and commands (obsolete, use \"completion\")" hecomplete)
5e56b3fb 1804 (const :tag "dcc: Provide Direct Client-to-Client support" dcc)
ff59d266
MB
1805 (const :tag "fill: Wrap long lines" fill)
1806 (const :tag "identd: Launch an identd server on port 8113" identd)
1807 (const :tag "irccontrols: Highlight or remove IRC control characters"
9cc8d0b6 1808 irccontrols)
5e56b3fb
MO
1809 (const :tag "keep-place: Leave point above un-viewed text" keep-place)
1810 (const :tag "list: List channels in a separate buffer" list)
ff59d266
MB
1811 (const :tag "log: Save buffers in logs" log)
1812 (const :tag "match: Highlight pals, fools, and other keywords" match)
1813 (const :tag "menu: Display a menu in ERC buffers" menu)
5e56b3fb
MO
1814 (const :tag "move-to-prompt: Move to the prompt when typing text"
1815 move-to-prompt)
ff59d266 1816 (const :tag "netsplit: Detect netsplits" netsplit)
5e56b3fb 1817 (const :tag "networks: Provide data about IRC networks" networks)
ff59d266 1818 (const :tag "noncommands: Don't display non-IRC commands after evaluation"
9cc8d0b6 1819 noncommands)
ff59d266
MB
1820 (const :tag
1821 "notify: Notify when the online status of certain users changes"
9cc8d0b6 1822 notify)
6e5e9b70
GM
1823 (const :tag "notifications: Send notifications on PRIVMSG or nickname mentions"
1824 notifications)
ff59d266
MB
1825 (const :tag "page: Process CTCP PAGE requests from IRC" page)
1826 (const :tag "readonly: Make displayed lines read-only" readonly)
1827 (const :tag "replace: Replace text in messages" replace)
1828 (const :tag "ring: Enable an input history" ring)
1829 (const :tag "scrolltobottom: Scroll to the bottom of the buffer"
1830 scrolltobottom)
1831 (const :tag "services: Identify to Nickserv (IRC Services) automatically"
9cc8d0b6 1832 services)
ff59d266
MB
1833 (const :tag "smiley: Convert smileys to pretty icons" smiley)
1834 (const :tag "sound: Play sounds when you receive CTCP SOUND requests"
9cc8d0b6 1835 sound)
ff59d266
MB
1836 (const :tag "stamp: Add timestamps to messages" stamp)
1837 (const :tag "spelling: Check spelling" spelling)
1838 (const :tag "track: Track channel activity in the mode-line" track)
1839 (const :tag "truncate: Truncate buffers to a certain size" truncate)
1840 (const :tag "unmorse: Translate morse code in messages" unmorse)
5e56b3fb 1841 (const :tag "xdcc: Act as an XDCC file-server" xdcc)
9cc8d0b6 1842 (repeat :tag "Others" :inline t symbol))
597993cf
MB
1843 :group 'erc)
1844
1845(defun erc-update-modules ()
1846 "Run this to enable erc-foo-mode for all modules in `erc-modules'."
1847 (let (req)
1848 (dolist (mod erc-modules)
1849 (setq req (concat "erc-" (symbol-name mod)))
1850 (cond
1851 ;; yuck. perhaps we should bring the filenames into sync?
6904f7fe
MB
1852 ((string= req "erc-capab-identify")
1853 (setq req "erc-capab"))
597993cf 1854 ((string= req "erc-completion")
9cc8d0b6 1855 (setq req "erc-pcomplete"))
c6b99621 1856 ((string= req "erc-pcomplete")
c6b99621
MB
1857 (setq mod 'completion))
1858 ((string= req "erc-autojoin")
9cc8d0b6 1859 (setq req "erc-join")))
597993cf
MB
1860 (condition-case nil
1861 (require (intern req))
1862 (error nil))
83dc6995
MB
1863 (let ((sym (intern-soft (concat "erc-" (symbol-name mod) "-mode"))))
1864 (if (fboundp sym)
1865 (funcall sym 1)
1866 (error "`%s' is not a known ERC module" mod))))))
597993cf
MB
1867
1868(defun erc-setup-buffer (buffer)
1869 "Consults `erc-join-buffer' to find out how to display `BUFFER'."
d0fcaff5
SM
1870 (pcase erc-join-buffer
1871 (`window
1872 (if (active-minibuffer-window)
1873 (display-buffer buffer)
1874 (switch-to-buffer-other-window buffer)))
1875 (`window-noselect
1876 (display-buffer buffer))
1877 (`bury
1878 nil)
1879 (`frame
1880 (when (or (not erc-reuse-frames)
1881 (not (get-buffer-window buffer t)))
1882 (let ((frame (make-frame (or erc-frame-alist
1883 default-frame-alist))))
1884 (raise-frame frame)
1885 (select-frame frame))
1886 (switch-to-buffer buffer)
1887 (when erc-frame-dedicated-flag
1888 (set-window-dedicated-p (selected-window) t))))
1889 (_
1890 (if (active-minibuffer-window)
1891 (display-buffer buffer)
1892 (switch-to-buffer buffer)))))
597993cf 1893
83dc6995
MB
1894(defun erc-open (&optional server port nick full-name
1895 connect passwd tgt-list channel process)
ff59d266 1896 "Connect to SERVER on PORT as NICK with FULL-NAME.
597993cf
MB
1897
1898If CONNECT is non-nil, connect to the server. Otherwise assume
1899already connected and just create a separate buffer for the new
1900target CHANNEL.
1901
1902Use PASSWD as user password on the server. If TGT-LIST is
2c5a0575 1903non-nil, use it to initialize `erc-default-recipients'.
597993cf
MB
1904
1905Returns the buffer for the given server or channel."
1906 (let ((server-announced-name (when (and (boundp 'erc-session-server)
1907 (string= server erc-session-server))
1908 erc-server-announced-name))
1909 (connected-p (unless connect erc-server-connected))
1910 (buffer (erc-get-buffer-create server port channel))
1911 (old-buffer (current-buffer))
10dc9f9e 1912 old-point
597993cf 1913 continued-session)
6904f7fe 1914 (when connect (run-hook-with-args 'erc-before-connect server port nick))
597993cf
MB
1915 (erc-update-modules)
1916 (set-buffer buffer)
10dc9f9e 1917 (setq old-point (point))
597993cf
MB
1918 (erc-mode)
1919 (setq erc-server-announced-name server-announced-name)
1920 (setq erc-server-connected connected-p)
1921 ;; connection parameters
1922 (setq erc-server-process process)
1923 (setq erc-insert-marker (make-marker))
1924 (setq erc-input-marker (make-marker))
1925 ;; go to the end of the buffer and open a new line
1926 ;; (the buffer may have existed)
1927 (goto-char (point-max))
1928 (forward-line 0)
1929 (when (get-text-property (point) 'erc-prompt)
1930 (setq continued-session t)
1931 (set-marker erc-input-marker
1932 (or (next-single-property-change (point) 'erc-prompt)
1933 (point-max))))
1934 (unless continued-session
1935 (goto-char (point-max))
1936 (insert "\n"))
1937 (set-marker erc-insert-marker (point))
1938 ;; stack of default recipients
1939 (setq erc-default-recipients tgt-list)
1940 (setq erc-server-current-nick nil)
1941 ;; Initialize erc-server-users and erc-channel-users
1942 (if connect
1943 (progn ;; server buffer
1944 (setq erc-server-users
1945 (make-hash-table :test 'equal))
1946 (setq erc-channel-users nil))
1947 (progn ;; target buffer
1948 (setq erc-server-users nil)
1949 (setq erc-channel-users
1950 (make-hash-table :test 'equal))))
1951 ;; clear last incomplete line read
1952 (setq erc-server-filter-data nil)
1953 (setq erc-channel-topic "")
1954 ;; limit on the number of users on the channel (mode +l)
1955 (setq erc-channel-user-limit nil)
1956 (setq erc-channel-key nil)
1957 ;; last active buffer, defaults to this one
1958 (erc-set-active-buffer buffer)
1959 ;; last invitation channel
1960 (setq erc-invitation nil)
597993cf
MB
1961 ;; Server channel list
1962 (setq erc-channel-list ())
1963 ;; login-time 'nick in use' error
1964 (setq erc-bad-nick nil)
1965 ;; whether we have logged in
1966 (setq erc-logged-in nil)
1967 ;; The local copy of `erc-nick' - the list of nicks to choose
1968 (setq erc-default-nicks (if (consp erc-nick) erc-nick (list erc-nick)))
1969 ;; password stuff
d0fcaff5
SM
1970 (setq erc-session-password
1971 (or passwd
1972 (let ((secret
1973 (plist-get
1974 (nth 0
1975 (auth-source-search :host server
1976 :max 1
1977 :user nick
1978 :port port
1979 :require '(:secret)))
1980 :secret)))
1981 (if (functionp secret)
1982 (funcall secret)
1983 secret))))
597993cf
MB
1984 ;; debug output buffer
1985 (setq erc-dbuf
1986 (when erc-log-p
1987 (get-buffer-create (concat "*ERC-DEBUG: " server "*"))))
ff59d266 1988 ;; set up prompt
597993cf
MB
1989 (unless continued-session
1990 (goto-char (point-max))
1991 (insert "\n"))
e7559e30 1992 (if continued-session
83dc6995 1993 (goto-char old-point)
597993cf
MB
1994 (set-marker erc-insert-marker (point))
1995 (erc-display-prompt)
1996 (goto-char (point-max)))
1997
ff59d266
MB
1998 (erc-determine-parameters server port nick full-name)
1999
2000 ;; Saving log file on exit
2001 (run-hook-with-args 'erc-connect-pre-hook buffer)
2002
2003 (when connect
2004 (erc-server-connect erc-session-server erc-session-port buffer))
2005 (erc-update-mode-line)
2006
597993cf
MB
2007 ;; Now display the buffer in a window as per user wishes.
2008 (unless (eq buffer old-buffer)
2009 (when erc-log-p
2010 ;; we can't log to debug buffer, it may not exist yet
2011 (message "erc: old buffer %s, switching to %s"
2012 old-buffer buffer))
2013 (erc-setup-buffer buffer))
2014
2015 buffer))
2016
ff59d266
MB
2017(defun erc-initialize-log-marker (buffer)
2018 "Initialize the `erc-last-saved-position' marker to a sensible position.
2019BUFFER is the current buffer."
2020 (with-current-buffer buffer
597993cf
MB
2021 (setq erc-last-saved-position (make-marker))
2022 (move-marker erc-last-saved-position
ff59d266 2023 (1- (marker-position erc-insert-marker)))))
597993cf
MB
2024
2025;; interactive startup
2026
2027(defvar erc-server-history-list nil
2028 "IRC server interactive selection history list.")
2029
2030(defvar erc-nick-history-list nil
2031 "Nickname interactive selection history list.")
2032
2033(defun erc-already-logged-in (server port nick)
2034 "Return the buffers corresponding to a NICK on PORT of a session SERVER.
2035This is determined by looking for the appropriate buffer and checking
2036whether the connection is still alive.
2037If no buffer matches, return nil."
2038 (erc-buffer-list
2039 (lambda ()
2040 (and (erc-server-process-alive)
2041 (string= erc-session-server server)
2042 (erc-port-equal erc-session-port port)
2043 (erc-current-nick-p nick)))))
2044
597993cf
MB
2045(defcustom erc-before-connect nil
2046 "Hook called before connecting to a server.
83dc6995 2047This hook gets executed before `erc' actually invokes `erc-mode'
597993cf
MB
2048with your input data. The functions in here get called with three
2049parameters, SERVER, PORT and NICK."
2050 :group 'erc-hooks
2051 :type 'hook)
2052
2053(defcustom erc-after-connect nil
2054 "Hook called after connecting to a server.
8c08b57a 2055This hook gets executed when an end of MOTD has been received. All
597993cf
MB
2056functions in here get called with the parameters SERVER and NICK."
2057 :group 'erc-hooks
2058 :type 'hook)
2059
2060;;;###autoload
2061(defun erc-select-read-args ()
2062 "Prompt the user for values of nick, server, port, and password."
2063 (let (user-input server port nick passwd)
2064 (setq user-input (read-from-minibuffer
2065 "IRC server: "
2066 (erc-compute-server) nil nil 'erc-server-history-list))
2067
2068 (if (string-match "\\(.*\\):\\(.*\\)\\'" user-input)
2069 (setq port (erc-string-to-port (match-string 2 user-input))
2070 user-input (match-string 1 user-input))
2071 (setq port
2072 (erc-string-to-port (read-from-minibuffer
2073 "IRC port: " (erc-port-to-string
2074 (erc-compute-port))))))
2075
2076 (if (string-match "\\`\\(.*\\)@\\(.*\\)" user-input)
2077 (setq nick (match-string 1 user-input)
2078 user-input (match-string 2 user-input))
2079 (setq nick
2080 (if (erc-already-logged-in server port nick)
2081 (read-from-minibuffer
2082 (erc-format-message 'nick-in-use ?n nick)
2083 nick
2084 nil nil 'erc-nick-history-list)
2085 (read-from-minibuffer
2086 "Nickname: " (erc-compute-nick nick)
2087 nil nil 'erc-nick-history-list))))
2088
2089 (setq server user-input)
2090
2091 (setq passwd (if erc-prompt-for-password
2092 (if (and erc-password
2093 (y-or-n-p "Use the default password? "))
2094 erc-password
2095 (read-passwd "Password: "))
2096 erc-password))
2097 (when (and passwd (string= "" passwd))
2098 (setq passwd nil))
2099
2100 (while (erc-already-logged-in server port nick)
2101 ;; hmm, this is a problem when using multiple connections to a bnc
2102 ;; with the same nick. Currently this code prevents using more than one
2103 ;; bnc with the same nick. actually it would be nice to have
2104 ;; bncs transparent, so that erc-compute-buffer-name displays
2105 ;; the server one is connected to.
2106 (setq nick (read-from-minibuffer
2107 (erc-format-message 'nick-in-use ?n nick)
2108 nick
2109 nil nil 'erc-nick-history-list)))
2110 (list :server server :port port :nick nick :password passwd)))
2111
2112;;;###autoload
19dc7206
SM
2113(cl-defun erc (&key (server (erc-compute-server))
2114 (port (erc-compute-port))
2115 (nick (erc-compute-nick))
2116 password
2117 (full-name (erc-compute-full-name)))
ff59d266
MB
2118 "ERC is a powerful, modular, and extensible IRC client.
2119This function is the main entry point for ERC.
2120
2121It permits you to select connection parameters, and then starts ERC.
2122
2123Non-interactively, it takes the keyword arguments
597993cf
MB
2124 (server (erc-compute-server))
2125 (port (erc-compute-port))
2126 (nick (erc-compute-nick))
2127 password
2128 (full-name (erc-compute-full-name)))
2129
2130That is, if called with
0b6bb130 2131
83dc6995 2132 (erc :server \"irc.freenode.net\" :full-name \"Harry S Truman\")
0b6bb130 2133
ff59d266 2134then the server and full-name will be set to those values, whereas
0b6bb130
MB
2135`erc-compute-port', `erc-compute-nick' and `erc-compute-full-name' will
2136be invoked for the values of the other parameters."
597993cf 2137 (interactive (erc-select-read-args))
83dc6995 2138 (erc-open server port nick full-name t password))
597993cf 2139
ff59d266 2140;;;###autoload
83dc6995 2141(defalias 'erc-select 'erc)
c4db078b 2142(defalias 'erc-ssl 'erc-tls)
597993cf 2143
5063dddc 2144;;;###autoload
526dc846
MO
2145(defun erc-tls (&rest r)
2146 "Interactively select TLS connection parameters and run ERC.
2147Arguments are the same as for `erc'."
2148 (interactive (erc-select-read-args))
2149 (let ((erc-server-connect-function 'erc-open-tls-stream))
2150 (apply 'erc r)))
2151
2152(defun erc-open-tls-stream (name buffer host port)
2153 "Open an TLS stream to an IRC server.
2154The process will be given the name NAME, its target buffer will be
2155BUFFER. HOST and PORT specify the connection target."
c4db078b 2156 (open-network-stream name buffer host port
5c62d133 2157 :type 'tls))
526dc846
MO
2158
2159;;; Displaying error messages
2160
2161(defun erc-error (&rest args)
2162 "Pass ARGS to `format', and display the result as an error message.
2163If `debug-on-error' is set to non-nil, then throw a real error with this
2164message instead, to make debugging easier."
2165 (if debug-on-error
2166 (apply #'error args)
2167 (apply #'message args)
2168 (beep)))
2169
597993cf
MB
2170;;; Debugging the protocol
2171
2172(defvar erc-debug-irc-protocol nil
2173 "If non-nil, log all IRC protocol traffic to the buffer \"*erc-protocol*\".
2174
2175The buffer is created if it doesn't exist.
2176
a3d2f0a3 2177NOTE: If this variable is non-nil, and you kill the only
597993cf
MB
2178visible \"*erc-protocol*\" buffer, it will be recreated shortly,
2179but you won't see it.
2180
2181WARNING: Do not set this variable directly! Instead, use the
2182function `erc-toggle-debug-irc-protocol' to toggle its value.")
2183
f29263b3
DN
2184(declare-function erc-network-name "erc-networks" ())
2185
597993cf
MB
2186(defun erc-log-irc-protocol (string &optional outbound)
2187 "Append STRING to the buffer *erc-protocol*.
2188
2189This only has any effect if `erc-debug-irc-protocol' is non-nil.
2190
2191The buffer is created if it doesn't exist.
2192
2193If OUTBOUND is non-nil, STRING is being sent to the IRC server
a3d2f0a3 2194and appears in face `erc-input-face' in the buffer."
597993cf
MB
2195 (when erc-debug-irc-protocol
2196 (let ((network-name (or (ignore-errors (erc-network-name))
2197 "???")))
2198 (with-current-buffer (get-buffer-create "*erc-protocol*")
2199 (save-excursion
2200 (goto-char (point-max))
2201 (let ((inhibit-read-only t))
2202 (insert (if (not outbound)
2203 ;; Cope with the fact that string might
2204 ;; contain multiple lines of text.
2205 (let ((lines (delete "" (split-string string
2206 "\n\\|\r\n")))
2207 (result ""))
2208 (dolist (line lines)
2209 (setq result (concat result network-name
2210 " << " line "\n")))
2211 result)
2212 (erc-propertize
2213 (concat network-name " >> " string
2214 (if (/= ?\n
2215 (aref string
2216 (1- (length string))))
2217 "\n"))
2218 'face 'erc-input-face)))))
2219 (let ((orig-win (selected-window))
2220 (debug-buffer-window (get-buffer-window (current-buffer) t)))
2221 (when debug-buffer-window
2222 (select-window debug-buffer-window)
2223 (when (= 1 (count-lines (point) (point-max)))
2224 (goto-char (point-max))
2225 (recenter -1))
2226 (select-window orig-win)))))))
2227
2228(defun erc-toggle-debug-irc-protocol (&optional arg)
2229 "Toggle the value of `erc-debug-irc-protocol'.
2230
2231If ARG is non-nil, show the *erc-protocol* buffer."
2232 (interactive "P")
2233 (let* ((buf (get-buffer-create "*erc-protocol*")))
2234 (with-current-buffer buf
645e533a 2235 (erc-view-mode-enter)
597993cf
MB
2236 (when (null (current-local-map))
2237 (let ((inhibit-read-only t))
2238 (insert (erc-make-notice "This buffer displays all IRC protocol traffic exchanged with each server.\n"))
2239 (insert (erc-make-notice "Kill this buffer to terminate protocol logging.\n\n")))
2240 (use-local-map (make-sparse-keymap))
5997e340 2241 (local-set-key (kbd "t") 'erc-toggle-debug-irc-protocol))
597993cf
MB
2242 (add-hook 'kill-buffer-hook
2243 #'(lambda () (setq erc-debug-irc-protocol nil))
2244 nil 'local)
2245 (goto-char (point-max))
2246 (let ((inhibit-read-only t))
2247 (insert (erc-make-notice
5997e340 2248 (format "IRC protocol logging %s at %s -- Press `t' to toggle logging.\n"
597993cf
MB
2249 (if erc-debug-irc-protocol "disabled" "enabled")
2250 (current-time-string))))))
2251 (setq erc-debug-irc-protocol (not erc-debug-irc-protocol))
2252 (if (and arg
2253 (not (get-buffer-window "*erc-protocol*" t)))
2254 (display-buffer buf t))
2255 (message "IRC protocol traffic logging %s (see buffer *erc-protocol*)."
2256 (if erc-debug-irc-protocol "enabled" "disabled"))))
2257
2258;;; I/O interface
2259
2260;; send interface
2261
2262(defun erc-send-action (tgt str &optional force)
2263 "Send CTCP ACTION information described by STR to TGT."
2264 (erc-send-ctcp-message tgt (format "ACTION %s" str) force)
2265 (erc-display-message
2266 nil 'input (current-buffer)
2267 'ACTION ?n (erc-current-nick) ?a str ?u "" ?h ""))
2268
2269;; Display interface
2270
2271(defun erc-string-invisible-p (string)
2272 "Check whether STRING is invisible or not.
2273I.e. any char in it has the `invisible' property set."
2274 (text-property-any 0 (length string) 'invisible t string))
2275
5e56b3fb
MO
2276(defcustom erc-remove-parsed-property t
2277 "Whether to remove the erc-parsed text property after displaying a message.
2278
2279The default is to remove it, since it causes ERC to take up extra
2280memory. If you have code that relies on this property, then set
2281this option to nil."
2282 :type 'boolean
2283 :group 'erc)
2284
597993cf
MB
2285(defun erc-display-line-1 (string buffer)
2286 "Display STRING in `erc-mode' BUFFER.
8c08b57a 2287Auxiliary function used in `erc-display-line'. The line gets filtered to
597993cf
MB
2288interpret the control characters. Then, `erc-insert-pre-hook' gets called.
2289If `erc-insert-this' is still t, STRING gets inserted into the buffer.
2290Afterwards, `erc-insert-modify' and `erc-insert-post-hook' get called.
2291If STRING is nil, the function does nothing."
2292 (when string
937e6a56 2293 (with-current-buffer (or buffer (process-buffer erc-server-process))
597993cf
MB
2294 (let ((insert-position (or (marker-position erc-insert-marker)
2295 (point-max))))
2296 (let ((string string) ;; FIXME! Can this be removed?
2297 (buffer-undo-list t)
2298 (inhibit-read-only t))
2299 (unless (string-match "\n$" string)
2300 (setq string (concat string "\n"))
2301 (when (erc-string-invisible-p string)
40ef8242
MO
2302 (erc-put-text-properties 0 (length string)
2303 '(invisible intangible) string)))
597993cf
MB
2304 (erc-log (concat "erc-display-line: " string
2305 (format "(%S)" string) " in buffer "
2306 (format "%s" buffer)))
2307 (setq erc-insert-this t)
2308 (run-hook-with-args 'erc-insert-pre-hook string)
2309 (if (null erc-insert-this)
2310 ;; Leave erc-insert-this set to t as much as possible. Fran
2311 ;; Litterio <franl> has seen erc-insert-this set to nil while
2312 ;; erc-send-pre-hook is running, which should never happen. This
2313 ;; may cure it.
2314 (setq erc-insert-this t)
2315 (save-excursion ;; to restore point in the new buffer
2316 (save-restriction
2317 (widen)
2318 (goto-char insert-position)
2319 (insert-before-markers string)
2320 ;; run insertion hook, with point at restored location
2321 (save-restriction
2322 (narrow-to-region insert-position (point))
2323 (run-hooks 'erc-insert-modify-hook)
5e56b3fb
MO
2324 (run-hooks 'erc-insert-post-hook)
2325 (when erc-remove-parsed-property
2326 (remove-text-properties (point-min) (point-max)
2327 '(erc-parsed nil))))))))
597993cf
MB
2328 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2329 (point-max))
2330 insert-position))))))
2331
2332(defun erc-update-undo-list (shift)
2333 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2334 (unless (or (zerop shift) (atom buffer-undo-list))
2335 (let ((list buffer-undo-list) elt)
2336 (while list
2337 (setq elt (car list))
2338 (cond ((integerp elt) ; POSITION
19dc7206 2339 (cl-incf (car list) shift))
597993cf 2340 ((or (atom elt) ; nil, EXTENT
3be70dff 2341 ;; (eq t (car elt)) ; (t . TIME)
597993cf
MB
2342 (markerp (car elt))) ; (MARKER . DISTANCE)
2343 nil)
2344 ((integerp (car elt)) ; (BEGIN . END)
19dc7206
SM
2345 (cl-incf (car elt) shift)
2346 (cl-incf (cdr elt) shift))
597993cf 2347 ((stringp (car elt)) ; (TEXT . POSITION)
19dc7206 2348 (cl-incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
597993cf
MB
2349 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2350 (let ((cons (nthcdr 3 elt)))
19dc7206
SM
2351 (cl-incf (car cons) shift)
2352 (cl-incf (cdr cons) shift)))
597993cf
MB
2353 ((and (featurep 'xemacs)
2354 (extentp (car elt))) ; (EXTENT START END)
19dc7206
SM
2355 (cl-incf (nth 1 elt) shift)
2356 (cl-incf (nth 2 elt) shift)))
597993cf
MB
2357 (setq list (cdr list))))))
2358
2359(defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
4f9c00e2 2360 "Regexp which matches all valid characters in a IRC nickname.")
597993cf
MB
2361
2362(defun erc-is-valid-nick-p (nick)
2363 "Check if NICK is a valid IRC nickname."
2364 (string-match (concat "^" erc-valid-nick-regexp "$") nick))
2365
2366(defun erc-display-line (string &optional buffer)
2367 "Display STRING in the ERC BUFFER.
2368All screen output must be done through this function. If BUFFER is nil
2369or omitted, the default ERC buffer for the `erc-session-server' is used.
2370The BUFFER can be an actual buffer, a list of buffers, 'all or 'active.
2371If BUFFER = 'all, the string is displayed in all the ERC buffers for the
2372current session. 'active means the current active buffer
8c08b57a
JB
2373\(`erc-active-buffer'). If the buffer can't be resolved, the current
2374buffer is used. `erc-display-line-1' is used to display STRING.
597993cf
MB
2375
2376If STRING is nil, the function does nothing."
2377 (let ((inhibit-point-motion-hooks t)
2378 new-bufs)
2379 (dolist (buf (cond
2380 ((bufferp buffer) (list buffer))
2381 ((listp buffer) buffer)
2382 ((processp buffer) (list (process-buffer buffer)))
2383 ((eq 'all buffer)
83dc6995
MB
2384 ;; Hmm, or all of the same session server?
2385 (erc-buffer-list nil erc-server-process))
597993cf
MB
2386 ((and (eq 'active buffer) (erc-active-buffer))
2387 (list (erc-active-buffer)))
2388 ((erc-server-buffer-live-p)
2389 (list (process-buffer erc-server-process)))
2390 (t (list (current-buffer)))))
2391 (when (buffer-live-p buf)
2392 (erc-display-line-1 string buf)
d0fcaff5 2393 (push buf new-bufs)))
597993cf 2394 (when (null new-bufs)
d0fcaff5
SM
2395 (erc-display-line-1 string (if (erc-server-buffer-live-p)
2396 (process-buffer erc-server-process)
2397 (current-buffer))))))
597993cf
MB
2398
2399(defun erc-display-message-highlight (type string)
a3d2f0a3 2400 "Highlight STRING according to TYPE, where erc-TYPE-face is an ERC face.
597993cf 2401
a3d2f0a3 2402See also `erc-make-notice'."
597993cf
MB
2403 (cond ((eq type 'notice)
2404 (erc-make-notice string))
2405 (t
2406 (erc-put-text-property
2407 0 (length string)
2408 'face (or (intern-soft
2409 (concat "erc-" (symbol-name type) "-face"))
2410 "erc-default-face")
2411 string)
2412 string)))
2413
487a247f
JF
2414(defvar erc-lurker-state nil
2415 "Track the time of the last PRIVMSG for each (server,nick) pair.
2416
2417This is implemented as a hash of hashes, where the outer key is
2418the canonicalized server name (as returned by
2419`erc-canonicalize-server-name') and the outer value is a hash
2420table mapping nicks (as returned by `erc-lurker-maybe-trim') to
2421the times of their most recently received PRIVMSG on any channel
2422on the given server.")
2423
2424(defcustom erc-lurker-trim-nicks t
2425 "If t, trim trailing `erc-lurker-ignore-chars' from nicks.
2426
2427This causes e.g. nick and nick` to be considered as the same
2428individual for activity tracking and lurkiness detection
2429purposes."
2430 :group 'erc-lurker
2431 :type 'boolean)
2432
19dc7206
SM
2433(defcustom erc-lurker-ignore-chars "`_"
2434 "Characters at the end of a nick to strip for activity tracking purposes.
2435
2436See also `erc-lurker-trim-nicks'."
2437 :group 'erc-lurker
2438 :type 'string)
2439
487a247f
JF
2440(defun erc-lurker-maybe-trim (nick)
2441 "Maybe trim trailing `erc-lurker-ignore-chars' from NICK.
2442
2443Returns NICK unmodified unless `erc-lurker-trim-nicks' is
2444non-nil."
2445 (if erc-lurker-trim-nicks
2446 (replace-regexp-in-string
2447 (format "[%s]"
2448 (mapconcat (lambda (char)
2449 (regexp-quote (char-to-string char)))
2450 erc-lurker-ignore-chars ""))
2451 "" nick)
2452 nick))
2453
487a247f
JF
2454(defcustom erc-lurker-hide-list nil
2455 "List of IRC type messages to hide when sent by lurkers.
2456
2457A typical value would be '(\"JOIN\" \"PART\" \"QUIT\").
2458See also `erc-lurker-p' and `erc-hide-list'."
2459 :group 'erc-lurker
2460 :type 'erc-message-type)
2461
2462(defcustom erc-lurker-threshold-time (* 60 60 24) ; 24h by default
2463 "Nicks from which no PRIVMSGs have been received within this
2464interval (in units of seconds) are considered lurkers by
2465`erc-lurker-p' and as a result their messages of types in
2466`erc-lurker-hide-list' will be hidden."
2467 :group 'erc-lurker
2468 :type 'integer)
2469
2470(defun erc-lurker-initialize ()
2471 "Initialize ERC lurker tracking functionality.
2472
2473This function adds `erc-lurker-update-status' to
2474`erc-insert-pre-hook' in order to record the time of each nick's
2475most recent PRIVMSG as well as initializing the state variable
2476storing this information."
2477 (setq erc-lurker-state (make-hash-table :test 'equal))
2478 (add-hook 'erc-insert-pre-hook 'erc-lurker-update-status))
2479
2480(defun erc-lurker-cleanup ()
2481 "Remove all last PRIVMSG state older than `erc-lurker-threshold-time'.
2482
2483This should be called regularly to avoid excessive resource
2484consumption for long-lived IRC or Emacs sessions."
2485 (maphash
2486 (lambda (server hash)
2487 (maphash
2488 (lambda (nick last-PRIVMSG-time)
2489 (when
21859ebc
EH
2490 (> (float-time (time-subtract
2491 (current-time)
2492 last-PRIVMSG-time))
487a247f
JF
2493 erc-lurker-threshold-time)
2494 (remhash nick hash)))
2495 hash)
2496 (if (zerop (hash-table-count hash))
2497 (remhash server erc-lurker-state)))
2498 erc-lurker-state))
2499
2500(defvar erc-lurker-cleanup-count 0
2501 "Internal counter variable for use with `erc-lurker-cleanup-interval'.")
2502
2503(defvar erc-lurker-cleanup-interval 100
d0fcaff5 2504 "Frequency of cleaning up stale erc-lurker state.
487a247f
JF
2505
2506`erc-lurker-update-status' calls `erc-lurker-cleanup' once for
2507every `erc-lurker-cleanup-interval' updates to
2508`erc-lurker-state'. This is designed to limit the memory
2509consumption of lurker state during long Emacs sessions and/or ERC
2510sessions with large numbers of incoming PRIVMSGs.")
2511
d0fcaff5 2512(defun erc-lurker-update-status (_message)
487a247f
JF
2513 "Update `erc-lurker-state' if necessary.
2514
2515This function is called from `erc-insert-pre-hook'. If the
2516current message is a PRIVMSG, update `erc-lurker-state' to
2517reflect the fact that its sender has issued a PRIVMSG at the
2518current time. Otherwise, take no action.
2519
2520This function depends on the fact that `erc-display-message'
2521dynamically binds `parsed', which is used to check if the current
2522message is a PRIVMSG and to determine its sender. See also
2523`erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'.
2524
2525In order to limit memory consumption, this function also calls
2526`erc-lurker-cleanup' once every `erc-lurker-cleanup-interval'
2527updates of `erc-lurker-state'."
2528 (when (and (boundp 'parsed) (erc-response-p parsed))
2529 (let* ((command (erc-response.command parsed))
2530 (sender
2531 (erc-lurker-maybe-trim
2532 (car (erc-parse-user (erc-response.sender parsed)))))
2533 (server
2534 (erc-canonicalize-server-name erc-server-announced-name)))
2535 (when (equal command "PRIVMSG")
19dc7206
SM
2536 (when (>= (cl-incf erc-lurker-cleanup-count)
2537 erc-lurker-cleanup-interval)
487a247f
JF
2538 (setq erc-lurker-cleanup-count 0)
2539 (erc-lurker-cleanup))
2540 (unless (gethash server erc-lurker-state)
2541 (puthash server (make-hash-table :test 'equal) erc-lurker-state))
2542 (puthash sender (current-time)
2543 (gethash server erc-lurker-state))))))
2544
2545(defun erc-lurker-p (nick)
2546 "Predicate indicating NICK's lurking status on the current server.
2547
2548Lurking is the condition where NICK has issued no PRIVMSG on this
2549server within `erc-lurker-threshold-time'. See also
2550`erc-lurker-trim-nicks' and `erc-lurker-ignore-chars'."
2551 (unless erc-lurker-state (erc-lurker-initialize))
2552 (let* ((server
2553 (erc-canonicalize-server-name erc-server-announced-name))
2554 (last-PRIVMSG-time
2555 (gethash (erc-lurker-maybe-trim nick)
2556 (gethash server erc-lurker-state (make-hash-table)))))
2557 (or (null last-PRIVMSG-time)
21859ebc 2558 (> (float-time
487a247f
JF
2559 (time-subtract (current-time) last-PRIVMSG-time))
2560 erc-lurker-threshold-time))))
2561
19dc7206
SM
2562(defcustom erc-common-server-suffixes
2563 '(("openprojects.net$" . "OPN")
2564 ("freenode.net$" . "freenode")
2565 ("oftc.net$" . "OFTC"))
2566 "Alist of common server name suffixes.
2567This variable is used in mode-line display to save screen
2568real estate. Set it to nil if you want to avoid changing
2569displayed hostnames."
2570 :group 'erc-mode-line-and-header
2571 :type 'alist)
2572
487a247f 2573(defun erc-canonicalize-server-name (server)
d0fcaff5 2574 "Return the canonical network name for SERVER if any,
487a247f
JF
2575otherwise `erc-server-announced-name'. SERVER is matched against
2576`erc-common-server-suffixes'."
2577 (when server
2578 (or (cdar (erc-remove-if-not
2579 (lambda (net) (string-match (car net) server))
2580 erc-common-server-suffixes))
2581 erc-server-announced-name)))
2582
2583(defun erc-hide-current-message-p (parsed)
2584 "Predicate indicating whether the parsed ERC response PARSED should be hidden.
2585
2586Messages are always hidden if the message type of PARSED appears in
2587`erc-hide-list'. In addition, messages whose type is a member of
2588`erc-lurker-hide-list' are hidden if `erc-lurker-p' returns true."
2589 (let* ((command (erc-response.command parsed))
2590 (sender (car (erc-parse-user (erc-response.sender parsed)))))
2591 (or (member command erc-hide-list)
2592 (and (member command erc-lurker-hide-list) (erc-lurker-p sender)))))
2593
597993cf
MB
2594(defun erc-display-message (parsed type buffer msg &rest args)
2595 "Display MSG in BUFFER.
2596
2597ARGS, PARSED, and TYPE are used to format MSG sensibly.
2598
2599See also `erc-format-message' and `erc-display-line'."
2600 (let ((string (if (symbolp msg)
2601 (apply 'erc-format-message msg args)
2602 msg)))
2603 (setq string
2604 (cond
f2c05698
MB
2605 ((null type)
2606 string)
597993cf
MB
2607 ((listp type)
2608 (mapc (lambda (type)
2609 (setq string
2610 (erc-display-message-highlight type string)))
2611 type)
2612 string)
2613 ((symbolp type)
2614 (erc-display-message-highlight type string))))
2615
2616 (if (not (erc-response-p parsed))
2617 (erc-display-line string buffer)
487a247f 2618 (unless (erc-hide-current-message-p parsed)
f2c05698 2619 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
597993cf
MB
2620 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2621 (erc-display-line string buffer)))))
2622
2623(defun erc-message-type-member (position list)
2624 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2625
2626This function relies on the erc-parsed text-property being
2627present."
ff59d266 2628 (let ((prop-val (erc-get-parsed-vector position)))
597993cf
MB
2629 (and prop-val (member (erc-response.command prop-val) list))))
2630
2631(defvar erc-send-input-line-function 'erc-send-input-line)
2632(make-variable-buffer-local 'erc-send-input-line-function)
2633
2634(defun erc-send-input-line (target line &optional force)
2635 "Send LINE to TARGET.
2636
2637See also `erc-server-send'."
2638 (setq line (format "PRIVMSG %s :%s"
2639 target
2640 ;; If the line is empty, we still want to
2641 ;; send it - i.e. an empty pasted line.
2642 (if (string= line "\n")
2643 " \n"
2644 line)))
2645 (erc-server-send line force target))
2646
2647(defun erc-get-arglist (fun)
2648 "Return the argument list of a function without the parens."
2649 (let ((arglist (format "%S" (erc-function-arglist fun))))
2650 (if (string-match "^(\\(.*\\))$" arglist)
2651 (match-string 1 arglist)
2652 arglist)))
2653
526dc846
MO
2654(defun erc-command-no-process-p (str)
2655 "Return non-nil if STR is an ERC command that can be run when the process
2656is not alive, nil otherwise."
2657 (let ((fun (erc-extract-command-from-line str)))
2658 (and fun
2659 (symbolp (car fun))
2660 (get (car fun) 'process-not-needed))))
2661
597993cf
MB
2662(defun erc-command-name (cmd)
2663 "For CMD being the function name of a ERC command, something like
2664erc-cmd-FOO, this returns a string /FOO."
2665 (let ((command-name (symbol-name cmd)))
2666 (if (string-match "^erc-cmd-\\(.*\\)$" command-name)
2667 (concat "/" (match-string 1 command-name))
2668 command-name)))
2669
2670(defun erc-process-input-line (line &optional force no-command)
2671 "Translate LINE to an RFC1459 command and send it based.
2672Returns non-nil if the command is actually sent to the server, and nil
2673otherwise.
2674
2675If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
a3d2f0a3 2676it is passed to `erc-cmd-default'. If LINE is not a command (i.e. doesn't
597993cf
MB
2677start with /<COMMAND>) then it is sent as a message.
2678
2679An optional FORCE argument forces sending the line when flood
2680protection is in effect. The optional NO-COMMAND argument prohibits
2681this function from interpreting the line as a command."
2682 (let ((command-list (erc-extract-command-from-line line)))
2683 (if (and command-list
2684 (not no-command))
2685 (let* ((cmd (nth 0 command-list))
2686 (args (nth 1 command-list)))
2687 (condition-case nil
2688 (if (listp args)
2689 (apply cmd args)
2690 (funcall cmd args))
2691 (wrong-number-of-arguments
2692 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2693 ?c (erc-command-name cmd)
2694 ?u (or (erc-get-arglist cmd)
2695 "")
2696 ?d (format "%s\n"
2697 (or (documentation cmd) "")))
2698 nil)))
2699 (let ((r (erc-default-target)))
2700 (if r
2701 (funcall erc-send-input-line-function r line force)
2702 (erc-display-message nil 'error (current-buffer) 'no-target)
2703 nil)))))
2704
2705;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2706;; Input commands handlers
2707;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2708
2709(defun erc-cmd-AMSG (line)
2710 "Send LINE to all channels of the current server that you are on."
2711 (interactive "sSend to all channels you're on: ")
2712 (setq line (erc-trim-string line))
2713 (erc-with-all-buffers-of-server nil
2714 (lambda ()
2715 (erc-channel-p (erc-default-target)))
2716 (erc-send-message line)))
2717(put 'erc-cmd-AMSG 'do-not-parse-args t)
2718
2719(defun erc-cmd-SAY (line)
2720 "Send LINE to the current query or channel as a message, not a command.
2721
2722Use this when you want to send a message with a leading '/'. Note
2723that since multi-line messages are never a command, you don't
2724need this when pasting multiple lines of text."
2725 (if (string-match "^\\s-*$" line)
2726 nil
2727 (string-match "^ ?\\(.*\\)" line)
2728 (erc-process-input-line (match-string 1 line) nil t)))
2729(put 'erc-cmd-SAY 'do-not-parse-args t)
2730
2731(defun erc-cmd-SET (line)
2732 "Set the variable named by the first word in LINE to some VALUE.
2733VALUE is computed by evaluating the rest of LINE in Lisp."
2734 (cond
2735 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2736 (let ((var (read (concat "erc-" (match-string 1 line))))
2737 (val (read (match-string 2 line))))
2738 (if (boundp var)
2739 (progn
2740 (set var (eval val))
2741 (erc-display-message
2742 nil nil 'active (format "Set %S to %S" var val))
2743 t)
2744 (setq var (read (match-string 1 line)))
2745 (if (boundp var)
2746 (progn
2747 (set var (eval val))
2748 (erc-display-message
2749 nil nil 'active (format "Set %S to %S" var val))
2750 t)
2751 (erc-display-message nil 'error 'active 'variable-not-bound)
2752 nil))))
2753 ((string-match "^\\s-*$" line)
2754 (erc-display-line
2755 (concat "Available user variables:\n"
2756 (apply
2757 'concat
2758 (mapcar
2759 (lambda (var)
2760 (let ((val (symbol-value var)))
2761 (concat (format "%S:" var)
2762 (if (consp val)
2763 (concat "\n" (pp-to-string val))
2764 (format " %S\n" val)))))
b4d3bc10 2765 (apropos-internal "^erc-" 'custom-variable-p))))
597993cf
MB
2766 (current-buffer)) t)
2767 (t nil)))
2768(defalias 'erc-cmd-VAR 'erc-cmd-SET)
2769(defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2770(put 'erc-cmd-SET 'do-not-parse-args t)
526dc846 2771(put 'erc-cmd-SET 'process-not-needed t)
597993cf
MB
2772
2773(defun erc-cmd-default (line)
2774 "Fallback command.
2775
ee7683eb 2776Commands for which no erc-cmd-xxx exists, are tunneled through
597993cf
MB
2777this function. LINE is sent to the server verbatim, and
2778therefore has to contain the command itself as well."
2779 (erc-log (format "cmd: DEFAULT: %s" line))
2780 (erc-server-send (substring line 1))
2781 t)
2782
2783(defun erc-cmd-IGNORE (&optional user)
2784 "Ignore USER. This should be a regexp matching nick!user@host.
2785If no USER argument is specified, list the contents of `erc-ignore-list'."
2786 (if user
83dc6995
MB
2787 (let ((quoted (regexp-quote user)))
2788 (when (and (not (string= user quoted))
2789 (y-or-n-p (format "Use regexp-quoted form (%s) instead? "
2790 quoted)))
2791 (setq user quoted))
597993cf
MB
2792 (erc-display-line
2793 (erc-make-notice (format "Now ignoring %s" user))
2794 'active)
ff59d266
MB
2795 (erc-with-server-buffer (add-to-list 'erc-ignore-list user)))
2796 (if (null (erc-with-server-buffer erc-ignore-list))
597993cf
MB
2797 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2798 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2799 (mapc #'(lambda (item)
2800 (erc-display-line (erc-make-notice item)
2801 'active))
ff59d266 2802 (erc-with-server-buffer erc-ignore-list))))
597993cf
MB
2803 t)
2804
2805(defun erc-cmd-UNIGNORE (user)
2806 "Remove the user specified in USER from the ignore list."
ff59d266 2807 (let ((ignored-nick (car (erc-with-server-buffer
83dc6995
MB
2808 (erc-member-ignore-case (regexp-quote user)
2809 erc-ignore-list)))))
2810 (unless ignored-nick
2811 (if (setq ignored-nick (erc-ignored-user-p user))
2812 (unless (y-or-n-p (format "Remove this regexp (%s)? "
2813 ignored-nick))
2814 (setq ignored-nick nil))
597993cf
MB
2815 (erc-display-line
2816 (erc-make-notice (format "%s is not currently ignored!" user))
83dc6995
MB
2817 'active)))
2818 (when ignored-nick
597993cf
MB
2819 (erc-display-line
2820 (erc-make-notice (format "No longer ignoring %s" user))
83dc6995 2821 'active)
ff59d266 2822 (erc-with-server-buffer
83dc6995 2823 (setq erc-ignore-list (delete ignored-nick erc-ignore-list)))))
597993cf
MB
2824 t)
2825
2826(defun erc-cmd-CLEAR ()
2827 "Clear the window content."
2828 (recenter 0)
2829 t)
526dc846 2830(put 'erc-cmd-CLEAR 'process-not-needed t)
597993cf
MB
2831
2832(defun erc-cmd-OPS ()
2833 "Show the ops in the current channel."
2834 (interactive)
2835 (let ((ops nil))
2836 (if erc-channel-users
d0fcaff5 2837 (maphash (lambda (_nick user-data)
597993cf
MB
2838 (let ((cuser (cdr user-data)))
2839 (if (and cuser
2840 (erc-channel-user-op cuser))
2841 (setq ops (cons (erc-server-user-nickname
2842 (car user-data))
2843 ops)))))
2844 erc-channel-users))
2845 (setq ops (sort ops 'string-lessp))
2846 (if ops
2847 (erc-display-message
2848 nil 'notice (current-buffer) 'ops
2849 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2850 ?o (mapconcat 'identity ops " "))
2851 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2852 t)
2853
2854(defun erc-cmd-COUNTRY (tld)
2855 "Display the country associated with the top level domain TLD."
2856 (require 'mail-extr)
2857 (let ((co (ignore-errors (what-domain tld))))
2858 (if co
2859 (erc-display-message
2860 nil 'notice 'active 'country ?c co ?d tld)
2861 (erc-display-message
2862 nil 'notice 'active 'country-unknown ?d tld))
2863 t))
526dc846 2864(put 'erc-cmd-COUNTRY 'process-not-needed t)
597993cf
MB
2865
2866(defun erc-cmd-AWAY (line)
2867 "Mark the user as being away, the reason being indicated by LINE.
2868If no reason is given, unset away status."
2869 (when (string-match "^\\s-*\\(.*\\)$" line)
2870 (let ((reason (match-string 1 line)))
2871 (erc-log (format "cmd: AWAY: %s" reason))
2872 (erc-server-send
2873 (if (string= reason "")
2874 "AWAY"
2875 (concat "AWAY :" reason))))
2876 t))
2877(put 'erc-cmd-AWAY 'do-not-parse-args t)
2878
2879(defun erc-cmd-GAWAY (line)
2880 "Mark the user as being away everywhere, the reason being indicated by LINE."
2881 ;; on all server buffers.
2882 (erc-with-all-buffers-of-server nil
ff59d266
MB
2883 #'erc-open-server-buffer-p
2884 (erc-cmd-AWAY line)))
597993cf
MB
2885(put 'erc-cmd-GAWAY 'do-not-parse-args t)
2886
2887(defun erc-cmd-CTCP (nick cmd &rest args)
2888 "Send a Client To Client Protocol message to NICK.
2889
2890CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2891VERSION and so on. It is called with ARGS."
2892 (let ((str (concat cmd
2893 (when args
2894 (concat " " (mapconcat #'identity args " "))))))
2895 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2896 (erc-send-ctcp-message nick str)
2897 t))
2898
2899(defun erc-cmd-HELP (&optional func)
2900 "Popup help information.
2901
2902If FUNC contains a valid function or variable, help about that
2903will be displayed. If FUNC is empty, display an apropos about
a3d2f0a3 2904ERC commands. Otherwise, do `apropos' in the ERC namespace
597993cf
MB
2905\(\"erc-.*LINE\"\).
2906
2907Examples:
2908To find out about erc and bbdb, do
2909 /help bbdb.*
2910
2911For help about the WHOIS command, do:
2912 /help whois
2913
2914For a list of user commands (/join /part, ...):
2915 /help."
2916 (if func
2917 (let* ((sym (or (let ((sym (intern-soft
2918 (concat "erc-cmd-" (upcase func)))))
2919 (if (and sym (or (boundp sym) (fboundp sym)))
2920 sym
2921 nil))
2922 (let ((sym (intern-soft func)))
2923 (if (and sym (or (boundp sym) (fboundp sym)))
2924 sym
2925 nil))
2926 (let ((sym (intern-soft (concat "erc-" func))))
2927 (if (and sym (or (boundp sym) (fboundp sym)))
2928 sym
2929 nil)))))
2930 (if sym
2931 (cond
2932 ((boundp sym) (describe-variable sym))
2933 ((fboundp sym) (describe-function sym))
2934 (t nil))
2935 (apropos-command (concat "erc-.*" func) nil
2936 (lambda (x)
2937 (or (commandp x)
2938 (get x 'custom-type))))
2939 t))
2940 (apropos "erc-cmd-")
2941 (message "Type C-h m to get additional information about keybindings.")
2942 t))
2943
2944(defalias 'erc-cmd-H 'erc-cmd-HELP)
526dc846 2945(put 'erc-cmd-HELP 'process-not-needed t)
597993cf
MB
2946
2947(defun erc-cmd-JOIN (channel &optional key)
2948 "Join the channel given in CHANNEL, optionally with KEY.
2949If CHANNEL is specified as \"-invite\", join the channel to which you
2950were most recently invited. See also `invitation'."
2951 (let (chnl)
2952 (if (string= (upcase channel) "-INVITE")
2953 (if erc-invitation
2954 (setq chnl erc-invitation)
2955 (erc-display-message nil 'error (current-buffer) 'no-invitation))
2956 (setq chnl (erc-ensure-channel-name channel)))
2957 (when chnl
2958 ;; Prevent double joining of same channel on same server.
2959 (let ((joined-channels
2960 (mapcar #'(lambda (chanbuf)
2961 (with-current-buffer chanbuf (erc-default-target)))
2962 (erc-channel-list erc-server-process))))
2963 (if (erc-member-ignore-case chnl joined-channels)
2964 (switch-to-buffer (car (erc-member-ignore-case chnl
2965 joined-channels)))
2966 (erc-log (format "cmd: JOIN: %s" chnl))
d0fcaff5
SM
2967 (erc-server-send (if (and chnl key)
2968 (format "JOIN %s %s" chnl key)
2969 (format "JOIN %s" chnl)))))))
597993cf
MB
2970 t)
2971
2972(defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
2973(defalias 'erc-cmd-J 'erc-cmd-JOIN)
2974
2975(defvar erc-channel-new-member-names nil
2976 "If non-nil, a names list is currently being received.
2977
2978If non-nil, this variable is a hash-table that associates
2979received nicks with t.")
2980(make-variable-buffer-local 'erc-channel-new-member-names)
2981
2982(defun erc-cmd-NAMES (&optional channel)
2983 "Display the users in CHANNEL.
2984If CHANNEL is not specified, display the users in the current channel.
2985This function clears the channel name list first, then sends the
2986command."
2987 (let ((tgt (or (and (erc-channel-p channel) channel)
2988 (erc-default-target))))
2989 (if (and tgt (erc-channel-p tgt))
2990 (progn
2991 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
2992 (erc-with-buffer
2993 (tgt)
2994 (erc-channel-begin-receiving-names))
2995 (erc-server-send (concat "NAMES " tgt)))
2996 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
2997 t)
2998(defalias 'erc-cmd-N 'erc-cmd-NAMES)
2999
3000(defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
3001 "Kick the user indicated in LINE from the current channel.
3002LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
3003 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
3004 (if (string= "" reasonstring)
3005 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
3006 (if (erc-channel-p target)
3007 (let ((nick reason-or-nick))
3008 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
3009 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
3010 nil target)
3011 t)
3012 (when target
3013 (let ((ch (erc-default-target)))
3014 (setq reasonstring (concat
3015 (if reason-or-nick (concat reason-or-nick " "))
3016 reasonstring))
3017 (if ch
3018 (progn
3019 (erc-log
3020 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
3021 (erc-server-send
3022 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
3023 (erc-display-message nil 'error (current-buffer)
3024 'no-default-channel))
3025 t)))))
3026
3027(defvar erc-script-args nil)
3028
3029(defun erc-cmd-LOAD (line)
3030 "Load the script provided in the LINE.
a3d2f0a3
JB
3031If LINE continues beyond the file name, the rest of
3032it is put in a (local) variable `erc-script-args',
3033which can be used in Emacs Lisp scripts.
597993cf
MB
3034
3035The optional FORCE argument is ignored here - you can't force loading
3036a script after exceeding the flood threshold."
3037 (cond
3038 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
3039 (let* ((file-to-find (match-string 1 line))
3040 (erc-script-args (match-string 2 line))
3041 (file (erc-find-file file-to-find erc-script-path)))
3042 (erc-log (format "cmd: LOAD: %s" file-to-find))
3043 (cond
3044 ((not file)
3045 (erc-display-message nil 'error (current-buffer)
3046 'cannot-find-file ?f file-to-find))
3047 ((not (file-readable-p file))
3048 (erc-display-message nil 'error (current-buffer)
3049 'cannot-read-file ?f file))
3050 (t
3051 (message "Loading \'%s\'..." file)
3052 (erc-load-script file)
3053 (message "Loading \'%s\'...done" file))))
3054 t)
3055 (t nil)))
3056
3057(defun erc-cmd-WHOIS (user &optional server)
3058 "Display whois information for USER.
3059
3060If SERVER is non-nil, use that, rather than the current server."
3061 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
3062 (let ((send (if server
3063 (format "WHOIS %s %s" user server)
3064 (format "WHOIS %s" user))))
3065 (erc-log (format "cmd: %s" send))
3066 (erc-server-send send)
3067 t))
3068(defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
3069
3070(defun erc-cmd-WHOAMI ()
3071 "Display whois information about yourself."
3072 (erc-cmd-WHOIS (erc-current-nick))
3073 t)
3074
3075(defun erc-cmd-IDLE (nick)
3076 "Show the length of time NICK has been idle."
ff59d266 3077 (let ((origbuf (current-buffer))
597993cf 3078 symlist)
ff59d266 3079 (erc-with-server-buffer
d0fcaff5
SM
3080 (push (cons (erc-once-with-server-event
3081 311 (lambda (_proc parsed)
3082 (string= nick
3083 (nth 1 (erc-response.command-args
3084 parsed)))))
3085 'erc-server-311-functions)
3086 symlist)
3087 (push (cons (erc-once-with-server-event
3088 312 (lambda (_proc parsed)
3089 (string= nick
3090 (nth 1 (erc-response.command-args
3091 parsed)))))
3092 'erc-server-312-functions)
3093 symlist)
3094 (push (cons (erc-once-with-server-event
3095 318 (lambda (_proc parsed)
3096 (string= nick
3097 (nth 1 (erc-response.command-args
3098 parsed)))))
3099 'erc-server-318-functions)
3100 symlist)
3101 (push (cons (erc-once-with-server-event
3102 319 (lambda (_proc parsed)
3103 (string= nick
3104 (nth 1 (erc-response.command-args
3105 parsed)))))
3106 'erc-server-319-functions)
3107 symlist)
3108 (push (cons (erc-once-with-server-event
3109 320 (lambda (_proc parsed)
3110 (string= nick
3111 (nth 1 (erc-response.command-args
3112 parsed)))))
3113 'erc-server-320-functions)
3114 symlist)
3115 (push (cons (erc-once-with-server-event
3116 330 (lambda (_proc parsed)
3117 (string= nick
3118 (nth 1 (erc-response.command-args
3119 parsed)))))
3120 'erc-server-330-functions)
3121 symlist)
3122 (push (cons (erc-once-with-server-event
3123 317
3124 (lambda (_proc parsed)
3125 (let ((idleseconds
3126 (string-to-number
3127 (cl-third
3128 (erc-response.command-args parsed)))))
3129 (erc-display-line
3130 (erc-make-notice
3131 (format "%s has been idle for %s."
3132 (erc-string-no-properties nick)
3133 (erc-seconds-to-string idleseconds)))
3134 origbuf)
3135 t)))
3136 'erc-server-317-functions)
3137 symlist)
3138
3139 ;; Send the WHOIS command.
3140 (erc-cmd-WHOIS nick)
3141
3142 ;; Remove the uninterned symbols from the server hooks that did not run.
3143 (run-at-time 20 nil (lambda (buf symlist)
3144 (with-current-buffer buf
3145 (dolist (sym symlist)
3146 (let ((hooksym (cdr sym))
3147 (funcsym (car sym)))
3148 (remove-hook hooksym funcsym t)))))
3149 (current-buffer) symlist)))
597993cf
MB
3150 t)
3151
3152(defun erc-cmd-DESCRIBE (line)
3153 "Pose some action to a certain user.
3154LINE has the format \"USER ACTION\"."
3155 (cond
3156 ((string-match
3157 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
3158 (let ((dst (match-string 1 line))
3159 (s (match-string 2 line)))
3160 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
3161 (erc-send-action dst s))
3162 t)
3163 (t nil)))
3164(put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
3165
3166(defun erc-cmd-ME (line)
3167 "Send LINE as an action."
3168 (cond
3169 ((string-match "^\\s-\\(.*\\)$" line)
3170 (let ((s (match-string 1 line)))
3171 (erc-log (format "cmd: ME: %s" s))
3172 (erc-send-action (erc-default-target) s))
3173 t)
3174 (t nil)))
3175(put 'erc-cmd-ME 'do-not-parse-args t)
3176
b6675b2d
MO
3177(defun erc-cmd-ME\'S (line)
3178 "Do a /ME command, but add the string \" 's\" to the beginning."
3179 (erc-cmd-ME (concat " 's" line)))
3180(put 'erc-cmd-ME\'S 'do-not-parse-args t)
3181
597993cf
MB
3182(defun erc-cmd-LASTLOG (line)
3183 "Show all lines in the current buffer matching the regexp LINE.
3184
3185If a match spreads across multiple lines, all those lines are shown.
3186
3187The lines are shown in a buffer named `*Occur*'.
3188It serves as a menu to find any of the occurrences in this buffer.
3189\\[describe-mode] in that buffer will explain how.
3190
3191If LINE contains upper case characters (excluding those preceded by `\'),
3192the matching is case-sensitive."
3193 (occur line)
3194 t)
3195(put 'erc-cmd-LASTLOG 'do-not-parse-args t)
526dc846 3196(put 'erc-cmd-LASTLOG 'process-not-needed t)
597993cf
MB
3197
3198(defun erc-send-message (line &optional force)
3199 "Send LINE to the current channel or user and display it.
3200
3201See also `erc-message' and `erc-display-line'."
3202 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
3203 (erc-display-line
3204 (concat (erc-format-my-nick) line)
3205 (current-buffer))
3206 ;; FIXME - treat multiline, run hooks, or remove me?
3207 t)
3208
3209(defun erc-cmd-MODE (line)
3210 "Change or display the mode value of a channel or user.
3211The first word specifies the target. The rest is the mode string
3212to send.
3213
3214If only one word is given, display the mode of that target.
3215
3216A list of valid mode strings for Freenode may be found at
fbf370dc 3217URL `http://freenode.net/using_the_network.shtml'."
597993cf
MB
3218 (cond
3219 ((string-match "^\\s-\\(.*\\)$" line)
3220 (let ((s (match-string 1 line)))
3221 (erc-log (format "cmd: MODE: %s" s))
3222 (erc-server-send (concat "MODE " line)))
3223 t)
3224 (t nil)))
3225(put 'erc-cmd-MODE 'do-not-parse-args t)
3226
3227(defun erc-cmd-NOTICE (channel-or-user &rest message)
3228 "Send a notice to the channel or user given as the first word.
3229The rest is the message to send."
3230 (erc-message "NOTICE" (concat channel-or-user " "
3231 (mapconcat #'identity message " "))))
3232
3233(defun erc-cmd-MSG (line)
3234 "Send a message to the channel or user given as the first word in LINE.
3235
3236The rest of LINE is the message to send."
3237 (erc-message "PRIVMSG" line))
3238
3239(defalias 'erc-cmd-M 'erc-cmd-MSG)
3240(put 'erc-cmd-MSG 'do-not-parse-args t)
3241
3242(defun erc-cmd-SQUERY (line)
3243 "Send a Service Query to the service given as the first word in LINE.
3244
3245The rest of LINE is the message to send."
3246 (erc-message "SQUERY" line))
3247
3248(defun erc-cmd-NICK (nick)
3249 "Change current nickname to NICK."
3250 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
ff59d266
MB
3251 (let ((nicklen (cdr (assoc "NICKLEN" (erc-with-server-buffer
3252 erc-server-parameters)))))
597993cf
MB
3253 (and nicklen (> (length nick) (string-to-number nicklen))
3254 (erc-display-message
3255 nil 'notice 'active 'nick-too-long
3256 ?i (length nick) ?l nicklen)))
3257 (erc-server-send (format "NICK %s" nick))
3258 (cond (erc-bad-nick
3259 (erc-set-current-nick nick)
3260 (erc-update-mode-line)
3261 (setq erc-bad-nick nil)))
3262 t)
3263
3264(defun erc-cmd-PART (line)
3265 "When LINE is an empty string, leave the current channel.
3266Otherwise leave the channel indicated by LINE."
3267 (cond
3268 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
3269 (let* ((ch (match-string 1 line))
3270 (msg (match-string 2 line))
3271 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3272 (erc-log (format "cmd: PART: %s: %s" ch reason))
3273 (erc-server-send (if (string= reason "")
3274 (format "PART %s" ch)
3275 (format "PART %s :%s" ch reason))
3276 nil ch))
3277 t)
3278 ((string-match "^\\s-*\\(.*\\)$" line)
3279 (let* ((ch (erc-default-target))
3280 (msg (match-string 1 line))
3281 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3282 (if (and ch (erc-channel-p ch))
3283 (progn
3284 (erc-log (format "cmd: PART: %s: %s" ch reason))
3285 (erc-server-send (if (string= reason "")
3286 (format "PART %s" ch)
3287 (format "PART %s :%s" ch reason))
3288 nil ch))
3289 (erc-display-message nil 'error (current-buffer) 'no-target)))
3290 t)
3291 (t nil)))
3292(put 'erc-cmd-PART 'do-not-parse-args t)
3293
3294(defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
3295
3296(defun erc-cmd-PING (recipient)
3297 "Ping RECIPIENT."
3298 (let ((time (format "%f" (erc-current-time))))
3299 (erc-log (format "cmd: PING: %s" time))
3300 (erc-cmd-CTCP recipient "PING" time)))
3301
3302(defun erc-cmd-QUOTE (line)
3303 "Send LINE directly to the server.
3304All the text given as argument is sent to the sever as unmodified,
3305just as you provided it. Use this command with care!"
3306 (cond
c89e5cd7 3307 ((string-match "^ ?\\(.+\\)$" line)
597993cf
MB
3308 (erc-server-send (match-string 1 line)))
3309 (t nil)))
3310(put 'erc-cmd-QUOTE 'do-not-parse-args t)
3311
5e56b3fb
MO
3312(defcustom erc-query-display 'window
3313 "Indicates how to display query buffers when using the /QUERY
3314command to talk to someone.
3315
3316The default behavior is to display the message in a new window
3317and bring it to the front. See the documentation for
3318`erc-join-buffer' for a description of the available choices.
3319
3320See also `erc-auto-query' to decide how private messages from
3321other people should be displayed."
3322 :group 'erc-query
3323 :type '(choice (const :tag "Split window and select" window)
3324 (const :tag "Split window, don't select" window-noselect)
3325 (const :tag "New frame" frame)
3326 (const :tag "Bury in new buffer" bury)
3327 (const :tag "Use current buffer" buffer)
3328 (const :tag "Use current buffer" t)))
3329
597993cf
MB
3330(defun erc-cmd-QUERY (&optional user)
3331 "Open a query with USER.
3332The type of query window/frame/etc will depend on the value of
5e56b3fb
MO
3333`erc-query-display'.
3334
3335If USER is omitted, close the current query buffer if one exists
3336- except this is broken now ;-)"
597993cf
MB
3337 (interactive
3338 (list (read-from-minibuffer "Start a query with: " nil)))
5e56b3fb
MO
3339 (let ((session-buffer (erc-server-buffer))
3340 (erc-join-buffer erc-query-display))
597993cf
MB
3341 (if user
3342 (erc-query user session-buffer)
3343 ;; currently broken, evil hack to display help anyway
3344 ;(erc-delete-query))))
3345 (signal 'wrong-number-of-arguments ""))))
3346(defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3347
3348(defun erc-quit-reason-normal (&optional s)
3349 "Normal quit message.
3350
3351If S is non-nil, it will be used as the quit reason."
3352 (or s
3353 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3354 erc-version-string) ; erc-official-location)
3355 ))
3356
3357(defun erc-quit-reason-zippy (&optional s)
3358 "Zippy quit message.
3359
3360If S is non-nil, it will be used as the quit reason."
3361 (or s
dc2f2f99
GM
3362 (if (fboundp 'yow)
3363 (erc-replace-regexp-in-string "\n" "" (yow))
3364 (erc-quit-reason-normal))))
3365
3366(make-obsolete 'erc-quit-reason-zippy "it will be removed." "24.4")
597993cf
MB
3367
3368(defun erc-quit-reason-various (s)
3369 "Choose a quit reason based on S (a string)."
3370 (when (featurep 'xemacs) (require 'poe))
3371 (let ((res (car (assoc-default (or s "")
3372 erc-quit-reason-various-alist 'string-match))))
3373 (cond
3374 ((functionp res) (funcall res))
3375 ((stringp res) res)
0b6bb130
MB
3376 (s s)
3377 (t (erc-quit-reason-normal)))))
597993cf
MB
3378
3379(defun erc-part-reason-normal (&optional s)
3380 "Normal part message.
3381
3382If S is non-nil, it will be used as the quit reason."
3383 (or s
3384 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3385 erc-version-string) ; erc-official-location)
3386 ))
3387
3388(defun erc-part-reason-zippy (&optional s)
3389 "Zippy part message.
3390
3391If S is non-nil, it will be used as the quit reason."
3392 (or s
dc2f2f99
GM
3393 (if (fboundp 'yow)
3394 (erc-replace-regexp-in-string "\n" "" (yow))
3395 (erc-part-reason-normal))))
3396
3397(make-obsolete 'erc-part-reason-zippy "it will be removed." "24.4")
597993cf
MB
3398
3399(defun erc-part-reason-various (s)
3400 "Choose a part reason based on S (a string)."
3401 (when (featurep 'xemacs) (require 'poe))
3402 (let ((res (car (assoc-default (or s "")
3403 erc-part-reason-various-alist 'string-match))))
3404 (cond
3405 ((functionp res) (funcall res))
3406 ((stringp res) res)
0b6bb130
MB
3407 (s s)
3408 (t (erc-part-reason-normal)))))
597993cf
MB
3409
3410(defun erc-cmd-QUIT (reason)
3411 "Disconnect from the current server.
3412If REASON is omitted, display a default quit message, otherwise display
3413the message given by REASON."
3414 (unless reason
3415 (setq reason ""))
3416 (cond
3417 ((string-match "^\\s-*\\(.*\\)$" reason)
3418 (let* ((s (match-string 1 reason))
3419 (buffer (erc-server-buffer))
ff59d266
MB
3420 (reason (funcall erc-quit-reason (if (equal s "") nil s)))
3421 server-proc)
597993cf
MB
3422 (with-current-buffer (if (and buffer
3423 (bufferp buffer))
3424 buffer
3425 (current-buffer))
3426 (erc-log (format "cmd: QUIT: %s" reason))
3427 (setq erc-server-quitting t)
3428 (erc-set-active-buffer (erc-server-buffer))
ff59d266 3429 (setq server-proc erc-server-process)
597993cf 3430 (erc-server-send (format "QUIT :%s" reason)))
ff59d266 3431 (run-hook-with-args 'erc-quit-hook server-proc)
597993cf 3432 (when erc-kill-queries-on-quit
ff59d266
MB
3433 (erc-kill-query-buffers server-proc))
3434 ;; if the process has not been killed within 4 seconds, kill it
3435 (run-at-time 4 nil
3436 (lambda (proc)
3437 (when (and (processp proc)
3438 (memq (process-status proc) '(run open)))
3439 (delete-process proc)))
3440 server-proc))
597993cf
MB
3441 t)
3442 (t nil)))
3443
3444(defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3445(defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3446(defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3447(put 'erc-cmd-QUIT 'do-not-parse-args t)
526dc846 3448(put 'erc-cmd-QUIT 'process-not-needed t)
597993cf
MB
3449
3450(defun erc-cmd-GQUIT (reason)
3451 "Disconnect from all servers at once with the same quit REASON."
ff59d266 3452 (erc-with-all-buffers-of-server nil #'erc-open-server-buffer-p
526dc846
MO
3453 (erc-cmd-QUIT reason))
3454 (when erc-kill-queries-on-quit
3455 ;; if the query buffers have not been killed within 4 seconds,
3456 ;; kill them
3457 (run-at-time
3458 4 nil
3459 (lambda ()
3460 (dolist (buffer (erc-buffer-list (lambda (buf)
3461 (not (erc-server-buffer-p buf)))))
3462 (kill-buffer buffer)))))
3463 t)
597993cf
MB
3464
3465(defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3466(put 'erc-cmd-GQUIT 'do-not-parse-args t)
526dc846 3467(put 'erc-cmd-GQUIT 'process-not-needed t)
597993cf 3468
10dc9f9e
MB
3469(defun erc-cmd-RECONNECT ()
3470 "Try to reconnect to the current IRC server."
526dc846 3471 (let ((buffer (erc-server-buffer))
ff59d266 3472 (process nil))
526dc846
MO
3473 (unless (buffer-live-p buffer)
3474 (setq buffer (current-buffer)))
3475 (with-current-buffer buffer
ff59d266
MB
3476 (setq erc-server-quitting nil)
3477 (setq erc-server-reconnecting t)
3478 (setq erc-server-reconnect-count 0)
3479 (setq process (get-buffer-process (erc-server-buffer)))
3480 (if process
3481 (delete-process process)
3482 (erc-server-reconnect))
3483 (setq erc-server-reconnecting nil)))
10dc9f9e 3484 t)
526dc846 3485(put 'erc-cmd-RECONNECT 'process-not-needed t)
10dc9f9e 3486
597993cf
MB
3487(defun erc-cmd-SERVER (server)
3488 "Connect to SERVER, leaving existing connection intact."
3489 (erc-log (format "cmd: SERVER: %s" server))
3490 (condition-case nil
83dc6995 3491 (erc :server server :nick (erc-current-nick))
597993cf 3492 (error
526dc846 3493 (erc-error "Cannot find host %s." server)))
597993cf 3494 t)
526dc846 3495(put 'erc-cmd-SERVER 'process-not-needed t)
597993cf 3496
07da87e9
GM
3497(defvar motif-version-string)
3498(defvar gtk-version-string)
597993cf
MB
3499
3500(defun erc-cmd-SV ()
3501 "Say the current ERC and Emacs version into channel."
21bc768b 3502 (erc-send-message (format "I'm using ERC %s with %s %s (%s%s) of %s."
597993cf
MB
3503 erc-version-string
3504 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3505 emacs-version
3506 system-configuration
3507 (concat
3508 (cond ((featurep 'motif)
3509 (concat ", " (substring
3510 motif-version-string 4)))
3511 ((featurep 'gtk)
3512 (concat ", GTK+ Version "
3513 gtk-version-string))
597993cf
MB
3514 ((featurep 'x-toolkit) ", X toolkit")
3515 (t ""))
3516 (if (and (boundp 'x-toolkit-scroll-bars)
3517 (memq x-toolkit-scroll-bars
3518 '(xaw xaw3d)))
3519 (format ", %s scroll bars"
3520 (capitalize (symbol-name
3521 x-toolkit-scroll-bars)))
3522 "")
3523 (if (featurep 'multi-tty) ", multi-tty" ""))
21bc768b 3524 erc-emacs-build-time))
597993cf
MB
3525 t)
3526
3527(defun erc-cmd-SM ()
3528 "Say the current ERC modes into channel."
3529 (erc-send-message (format "I'm using the following modules: %s!"
3530 (erc-modes)))
3531 t)
3532
597993cf
MB
3533(defun erc-cmd-DEOP (&rest people)
3534 "Remove the operator setting from user(s) given in PEOPLE."
3535 (when (> (length people) 0)
3536 (erc-server-send (concat "MODE " (erc-default-target)
3537 " -"
3538 (make-string (length people) ?o)
3539 " "
3540 (mapconcat 'identity people " ")))
3541 t))
3542
3543(defun erc-cmd-OP (&rest people)
3544 "Add the operator setting to users(s) given in PEOPLE."
3545 (when (> (length people) 0)
3546 (erc-server-send (concat "MODE " (erc-default-target)
3547 " +"
3548 (make-string (length people) ?o)
3549 " "
3550 (mapconcat 'identity people " ")))
3551 t))
3552
3553(defun erc-cmd-TIME (&optional line)
3554 "Request the current time and date from the current server."
3555 (cond
3556 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3557 (let ((args (match-string 1 line)))
3558 (erc-log (format "cmd: TIME: %s" args))
3559 (erc-server-send (concat "TIME " args)))
3560 t)
3561 (t (erc-server-send "TIME"))))
3562(defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3563
3564(defun erc-cmd-TOPIC (topic)
3565 "Set or request the topic for a channel.
3566LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3567or the empty string.
3568
3569If no #CHANNEL is given, the default channel is used. If TOPIC is
3570given, the channel topic is modified, otherwise the current topic will
3571be displayed."
3572 (cond
3573 ;; /topic #channel TOPIC
3574 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3575 (let ((ch (match-string 1 topic))
3576 (topic (match-string 2 topic)))
3577 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3578 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3579 t)
3580 ;; /topic #channel
3581 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3582 (let ((ch (match-string 1 topic)))
3583 (erc-server-send (format "TOPIC %s" ch) nil ch)
3584 t))
3585 ;; /topic
3586 ((string-match "^\\s-*$" topic)
3587 (let ((ch (erc-default-target)))
3588 (erc-server-send (format "TOPIC %s" ch) nil ch)
3589 t))
3590 ;; /topic TOPIC
3591 ((string-match "^\\s-*\\(.*\\)$" topic)
3592 (let ((ch (erc-default-target))
3593 (topic (match-string 1 topic)))
3594 (if (and ch (erc-channel-p ch))
3595 (progn
3596 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3597 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3598 (erc-display-message nil 'error (current-buffer) 'no-target)))
3599 t)
3600 (t nil)))
3601(defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3602(put 'erc-cmd-TOPIC 'do-not-parse-args t)
3603
3604(defun erc-cmd-APPENDTOPIC (topic)
3605 "Append TOPIC to the current channel topic, separated by a space."
3606 (let ((oldtopic erc-channel-topic))
3607 ;; display help when given no arguments
3608 (when (string-match "^\\s-*$" topic)
3609 (signal 'wrong-number-of-arguments nil))
3610 ;; strip trailing ^O
3611 (when (string-match "\\(.*\\)\C-o" oldtopic)
3612 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3613(defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3614(put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3615
3616(defun erc-cmd-CLEARTOPIC (&optional channel)
3617 "Clear the topic for a CHANNEL.
3618If CHANNEL is not specified, clear the topic for the default channel."
3619 (interactive "sClear topic of channel (RET is current channel): ")
3620 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3621 (when chnl
3622 (erc-server-send (format "TOPIC %s :" chnl))
3623 t)))
3624
3625;;; Banlists
3626
3627(defvar erc-channel-banlist nil
3628 "A list of bans seen for the current channel.
3629
3630Each ban is an alist of the form:
3631 (WHOSET . MASK)
3632
3633The property `received-from-server' indicates whether
3634or not the ban list has been requested from the server.")
3635(make-variable-buffer-local 'erc-channel-banlist)
3636(put 'erc-channel-banlist 'received-from-server nil)
3637
3638(defun erc-cmd-BANLIST ()
3639 "Pretty-print the contents of `erc-channel-banlist'.
3640
3641The ban list is fetched from the server if necessary."
3642 (let ((chnl (erc-default-target))
3643 (chnl-name (buffer-name)))
3644
3645 (cond
3646 ((not (erc-channel-p chnl))
3647 (erc-display-line (erc-make-notice "You're not on a channel\n")
3648 'active))
3649
3650 ((not (get 'erc-channel-banlist 'received-from-server))
3651 (let ((old-367-hook erc-server-367-functions))
3652 (setq erc-server-367-functions 'erc-banlist-store
3653 erc-channel-banlist nil)
3654 ;; fetch the ban list then callback
ff59d266 3655 (erc-with-server-buffer
597993cf
MB
3656 (erc-once-with-server-event
3657 368
d0fcaff5
SM
3658 (lambda (_proc _parsed)
3659 (with-current-buffer chnl-name
597993cf 3660 (put 'erc-channel-banlist 'received-from-server t)
d0fcaff5 3661 (setq erc-server-367-functions old-367-hook)
597993cf 3662 (erc-cmd-BANLIST)
d0fcaff5 3663 t)))
597993cf
MB
3664 (erc-server-send (format "MODE %s b" chnl)))))
3665
3666 ((null erc-channel-banlist)
3667 (erc-display-line (erc-make-notice
3668 (format "No bans for channel: %s\n" chnl))
3669 'active)
3670 (put 'erc-channel-banlist 'received-from-server nil))
3671
3672 (t
3673 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3674 erc-fill-column)
3675 (and (boundp 'fill-column)
3676 fill-column)
3677 (1- (window-width))))
a3d2f0a3 3678 (separator (make-string erc-fill-column ?=))
597993cf
MB
3679 (fmt (concat
3680 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3681 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3682
3683 (erc-display-line
3684 (erc-make-notice (format "Ban list for channel: %s\n"
3685 (erc-default-target)))
3686 'active)
3687
3688 (erc-display-line separator 'active)
3689 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3690 (erc-display-line separator 'active)
3691
3692 (mapc
3693 (lambda (x)
3694 (erc-display-line
3695 (format fmt
3696 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3697 (if (car x)
3698 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3699 ""))
3700 'active))
3701 erc-channel-banlist)
3702
3703 (erc-display-line (erc-make-notice "End of Ban list")
3704 'active)
3705 (put 'erc-channel-banlist 'received-from-server nil)))))
3706 t)
3707
3708(defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3709
3710(defun erc-cmd-MASSUNBAN ()
3711 "Mass Unban.
3712
3713Unban all currently banned users in the current channel."
3714 (let ((chnl (erc-default-target)))
3715 (cond
3716
3717 ((not (erc-channel-p chnl))
3718 (erc-display-line
3719 (erc-make-notice "You're not on a channel\n")
3720 'active))
3721
3722 ((not (get 'erc-channel-banlist 'received-from-server))
3723 (let ((old-367-hook erc-server-367-functions))
3724 (setq erc-server-367-functions 'erc-banlist-store)
d0fcaff5
SM
3725 ;; fetch the ban list then callback
3726 (erc-with-server-buffer
3727 (erc-once-with-server-event
3728 368
3729 (lambda (_proc _parsed)
3730 (with-current-buffer chnl
3731 (put 'erc-channel-banlist 'received-from-server t)
3732 (setq erc-server-367-functions old-367-hook)
3733 (erc-cmd-MASSUNBAN)
3734 t)))
3735 (erc-server-send (format "MODE %s b" chnl)))))
597993cf
MB
3736
3737 (t (let ((bans (mapcar 'cdr erc-channel-banlist)))
d0fcaff5
SM
3738 (when bans
3739 ;; Glob the bans into groups of three, and carry out the unban.
3740 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3741 (mapc
3742 (lambda (x)
3743 (erc-server-send
3744 (format "MODE %s -%s %s" (erc-default-target)
3745 (make-string (length x) ?b)
597993cf 3746 (mapconcat 'identity x " "))))
d0fcaff5 3747 (erc-group-list bans 3))))
597993cf
MB
3748 t))))
3749
3750(defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3751
3752;;;; End of IRC commands
3753
3754(defun erc-ensure-channel-name (channel)
3755 "Return CHANNEL if it is a valid channel name.
3756Eventually add a # in front of it, if that turns it into a valid channel name."
3757 (if (erc-channel-p channel)
3758 channel
3759 (concat "#" channel)))
3760
3761(defun erc-grab-region (start end)
3762 "Copy the region between START and END in a recreatable format.
3763
3764Converts all the IRC text properties in each line of the region
3765into control codes and writes them to a separate buffer. The
3766resulting text may be used directly as a script to generate this
3767text again."
3768 (interactive "r")
3769 (erc-set-active-buffer (current-buffer))
3770 (save-excursion
3771 (let* ((cb (current-buffer))
3772 (buf (generate-new-buffer erc-grab-buffer-name))
3773 (region (buffer-substring start end))
3774 (lines (erc-split-multiline-safe region)))
3775 (set-buffer buf)
3776 (dolist (line lines)
3777 (insert (concat line "\n")))
3778 (set-buffer cb)
3779 (switch-to-buffer-other-window buf)))
3780 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3781 (ding))
3782
3783(defun erc-display-prompt (&optional buffer pos prompt face)
3784 "Display PROMPT in BUFFER at position POS.
3785Display an ERC prompt in BUFFER.
3786
3787If PROMPT is nil, one is constructed with the function `erc-prompt'.
3788If BUFFER is nil, the `current-buffer' is used.
3789If POS is nil, PROMPT will be displayed at `point'.
3790If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3791`erc-prompt-face' will be used."
3792 (let* ((prompt (or prompt (erc-prompt)))
3793 (l (length prompt))
3794 (ob (current-buffer)))
3795 ;; We cannot use save-excursion because we move point, therefore
3796 ;; we resort to the ol' ob trick to restore this.
3797 (when (and buffer (bufferp buffer))
3798 (set-buffer buffer))
3799
3800 ;; now save excursion again to store where point and mark are
3801 ;; in the current buffer
3802 (save-excursion
3803 (setq pos (or pos (point)))
3804 (goto-char pos)
3805 (when (> l 0)
3806 ;; Do not extend the text properties when typing at the end
3807 ;; of the prompt, but stuff typed in front of the prompt
3808 ;; shall remain part of the prompt.
3809 (setq prompt (erc-propertize prompt
3810 'start-open t ; XEmacs
3811 'rear-nonsticky t ; Emacs
3812 'erc-prompt t
ec3e5f73 3813 'field t
597993cf
MB
3814 'front-sticky t
3815 'read-only t))
3816 (erc-put-text-property 0 (1- (length prompt))
3817 'face (or face 'erc-prompt-face)
3818 prompt)
3819 (insert prompt))
3820 ;; Set the input marker
3821 (set-marker erc-input-marker (point)))
3822
3823 ;; Now we are back at the old position. If the prompt was
3824 ;; inserted here or before us, advance point by the length of
3825 ;; the prompt.
3826 (when (or (not pos) (<= (point) pos))
3827 (forward-char l))
3828 ;; Clear the undo buffer now, so the user can undo his stuff,
3829 ;; but not the stuff we did. Sneaky!
3830 (setq buffer-undo-list nil)
3831 (set-buffer ob)))
3832
3833;; interactive operations
3834
3835(defun erc-input-message ()
3836 "Read input from the minibuffer."
3837 (interactive)
3838 (let ((minibuffer-allow-text-properties t)
3839 (read-map minibuffer-local-map))
3840 (insert (read-from-minibuffer "Message: "
64bd06a8
GM
3841 (string (if (featurep 'xemacs)
3842 last-command-char
3843 last-command-event)) read-map))
597993cf
MB
3844 (erc-send-current-line)))
3845
3846(defvar erc-action-history-list ()
3847 "History list for interactive action input.")
3848
3849(defun erc-input-action ()
3850 "Interactively input a user action and send it to IRC."
3851 (interactive "")
3852 (erc-set-active-buffer (current-buffer))
3853 (let ((action (read-from-minibuffer
3854 "Action: " nil nil nil 'erc-action-history-list)))
3855 (if (not (string-match "^\\s-*$" action))
3856 (erc-send-action (erc-default-target) action))))
3857
3858(defun erc-join-channel (channel &optional key)
3859 "Join CHANNEL.
3860
3861If `point' is at the beginning of a channel name, use that as default."
3862 (interactive
3863 (list
21bc768b 3864 (let ((chnl (if (looking-at "\\([&#+!][^ \n]+\\)") (match-string 1) ""))
597993cf
MB
3865 (table (when (erc-server-buffer-live-p)
3866 (set-buffer (process-buffer erc-server-process))
3867 erc-channel-list)))
3868 (completing-read "Join channel: " table nil nil nil nil chnl))
b6675b2d 3869 (when (or current-prefix-arg erc-prompt-for-channel-key)
597993cf
MB
3870 (read-from-minibuffer "Channel key (RET for none): " nil))))
3871 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3872
3873(defun erc-part-from-channel (reason)
3874 "Part from the current channel and prompt for a REASON."
3875 (interactive
3876 (list
3877 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3878 reason
83dc6995
MB
3879 (read-from-minibuffer (concat "Reason for leaving " (erc-default-target)
3880 ": ")))))
597993cf
MB
3881 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3882
3883(defun erc-set-topic (topic)
3884 "Prompt for a TOPIC for the current channel."
3885 (interactive
3886 (list
3887 (read-from-minibuffer
3888 (concat "Set topic of " (erc-default-target) ": ")
3889 (when erc-channel-topic
5e56b3fb
MO
3890 (let ((ss (split-string erc-channel-topic "\C-o")))
3891 (cons (apply 'concat (if (cdr ss) (butlast ss) ss))
3892 0))))))
597993cf
MB
3893 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3894 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3895
3896(defun erc-set-channel-limit (&optional limit)
3897 "Set a LIMIT for the current channel. Remove limit if nil.
3898Prompt for one if called interactively."
3899 (interactive (list (read-from-minibuffer
3900 (format "Limit for %s (RET to remove limit): "
3901 (erc-default-target)))))
3902 (let ((tgt (erc-default-target)))
d0fcaff5
SM
3903 (erc-server-send (if (and limit (>= (length limit) 1))
3904 (format "MODE %s +l %s" tgt limit)
3905 (format "MODE %s -l" tgt)))))
597993cf
MB
3906
3907(defun erc-set-channel-key (&optional key)
3908 "Set a KEY for the current channel. Remove key if nil.
3909Prompt for one if called interactively."
3910 (interactive (list (read-from-minibuffer
3911 (format "Key for %s (RET to remove key): "
3912 (erc-default-target)))))
3913 (let ((tgt (erc-default-target)))
d0fcaff5
SM
3914 (erc-server-send (if (and key (>= (length key) 1))
3915 (format "MODE %s +k %s" tgt key)
3916 (format "MODE %s -k" tgt)))))
597993cf
MB
3917
3918(defun erc-quit-server (reason)
3919 "Disconnect from current server after prompting for REASON.
3920`erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
3921 (interactive (list (read-from-minibuffer
3922 (format "Reason for quitting %s: "
3923 (or erc-server-announced-name
3924 erc-session-server)))))
3925 (erc-cmd-QUIT reason))
3926
3927;; Movement of point
3928
3929(defun erc-bol ()
3930 "Move `point' to the beginning of the current line.
3931
3932This places `point' just after the prompt, or at the beginning of the line."
3933 (interactive)
3934 (forward-line 0)
3935 (when (get-text-property (point) 'erc-prompt)
3936 (goto-char erc-input-marker))
3937 (point))
3938
3939(defun erc-kill-input ()
3940 "Kill current input line using `erc-bol' followed by `kill-line'."
3941 (interactive)
3942 (when (and (erc-bol)
3943 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
3944 ;; there's nothing to kill
3945 (if (boundp 'erc-input-ring-index)
3946 (setq erc-input-ring-index nil))
3947 (kill-line)))
3948
d4aa710a
SM
3949(defun erc-complete-word-at-point ()
3950 (run-hook-with-args-until-success 'erc-complete-functions))
597993cf 3951
d4aa710a 3952(define-obsolete-function-alias 'erc-complete-word 'completion-at-point "24.1")
597993cf
MB
3953
3954;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3955;;
3956;; IRC SERVER INPUT HANDLING
3957;;
3958;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3959
3960;;;; New Input parsing
3961
3962; Stolen from ZenIRC. I just wanna test this code, so here is
3963; experiment area.
3964
3965(defcustom erc-default-server-hook '(erc-debug-missing-hooks
3966 erc-default-server-handler)
fb7ada5f 3967 "Default for server messages which aren't covered by `erc-server-hooks'."
597993cf
MB
3968 :group 'erc-server-hooks
3969 :type 'hook)
3970
3971(defun erc-default-server-handler (proc parsed)
3972 "Default server handler.
3973
3974Displays PROC and PARSED appropriately using `erc-display-message'."
3975 (erc-display-message
3976 parsed 'notice proc
3977 (mapconcat
3978 'identity
3979 (let (res)
3980 (mapc #'(lambda (x)
3981 (if (stringp x)
3982 (setq res (append res (list x)))))
3983 parsed)
3984 res)
3985 " ")))
3986
3987(defvar erc-server-vectors
3988 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
3989 "List of received server messages which ERC does not specifically handle.
3990See `erc-debug-missing-hooks'.")
3991;(make-variable-buffer-local 'erc-server-vectors)
3992
d0fcaff5 3993(defun erc-debug-missing-hooks (_proc parsed)
597993cf
MB
3994 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
3995These vectors can be helpful when adding new server message handlers to ERC.
3996See `erc-default-server-hook'."
3997 (nconc erc-server-vectors (list parsed))
3998 nil)
3999
4000(defun erc-query (target server)
4001 "Open a query buffer on TARGET, using SERVER.
4002To change how this query window is displayed, use `let' to bind
4003`erc-join-buffer' before calling this."
4004 (unless (and server
4005 (buffer-live-p server)
4006 (set-buffer server))
4007 (error "Couldn't switch to server buffer"))
83dc6995
MB
4008 (let ((buf (erc-open erc-session-server
4009 erc-session-port
4010 (erc-current-nick)
4011 erc-session-user-full-name
4012 nil
4013 nil
4014 (list target)
4015 target
4016 erc-server-process)))
597993cf
MB
4017 (unless buf
4018 (error "Couldn't open query window"))
4019 (erc-update-mode-line)
4020 buf))
4021
5e56b3fb 4022(defcustom erc-auto-query 'window-noselect
597993cf 4023 "If non-nil, create a query buffer each time you receive a private message.
5e56b3fb 4024If the buffer doesn't already exist, it is created.
597993cf 4025
5e56b3fb
MO
4026This can be set to a symbol, to control how the new query window
4027should appear. The default behavior is to display the buffer in
4028a new window, but not to select it. See the documentation for
4029`erc-join-buffer' for a description of the available choices."
597993cf 4030 :group 'erc-query
5e56b3fb
MO
4031 :type '(choice (const :tag "Don't create query window" nil)
4032 (const :tag "Split window and select" window)
4033 (const :tag "Split window, don't select" window-noselect)
4034 (const :tag "New frame" frame)
4035 (const :tag "Bury in new buffer" bury)
4036 (const :tag "Use current buffer" buffer)
4037 (const :tag "Use current buffer" t)))
597993cf
MB
4038
4039(defcustom erc-query-on-unjoined-chan-privmsg t
4040 "If non-nil create query buffer on receiving any PRIVMSG at all.
4041This includes PRIVMSGs directed to channels. If you are using an IRC
4042bouncer, such as dircproxy, to keep a log of channels when you are
4043disconnected, you should set this option to t."
4044 :group 'erc-query
4045 :type 'boolean)
4046
4047(defcustom erc-format-query-as-channel-p t
4048 "If non-nil, format text from others in a query buffer like in a channel,
4049otherwise format like a private message."
4050 :group 'erc-query
4051 :type 'boolean)
4052
4053(defcustom erc-minibuffer-notice nil
4054 "If non-nil, print ERC notices for the user in the minibuffer.
4055Only happens when the session buffer isn't visible."
4056 :group 'erc-display
4057 :type 'boolean)
4058
4059(defcustom erc-minibuffer-ignored nil
4060 "If non-nil, print a message in the minibuffer if we ignored something."
4061 :group 'erc-ignore
4062 :type 'boolean)
4063
4064(defun erc-wash-quit-reason (reason nick login host)
4065 "Remove duplicate text from quit REASON.
4066Specifically in relation to NICK (user@host) information. Returns REASON
4067unmodified if nothing can be removed.
4068E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
4069\"Read error: 110\". The same applies for \"Ping Timeout\"."
4070 (setq nick (regexp-quote nick)
4071 login (regexp-quote login)
4072 host (regexp-quote host))
4073 (or (when (string-match (concat "^\\(Read error\\) to "
4074 nick "\\[" host "\\]: "
4075 "\\(.+\\)$") reason)
4076 (concat (match-string 1 reason) ": " (match-string 2 reason)))
4077 (when (string-match (concat "^\\(Ping timeout\\) for "
4078 nick "\\[" host "\\]$") reason)
4079 (match-string 1 reason))
4080 reason))
4081
4082(defun erc-nickname-in-use (nick reason)
4083 "If NICK is unavailable, tell the user the REASON.
4084
4085See also `erc-display-error-notice'."
0b6bb130 4086 (if (or (not erc-try-new-nick-p)
597993cf
MB
4087 ;; how many default-nicks are left + one more try...
4088 (eq erc-nick-change-attempt-count
4089 (if (consp erc-nick)
4090 (+ (length erc-nick) 1)
4091 1)))
4092 (erc-display-error-notice
4093 nil
4094 (format "Nickname %s is %s, try another." nick reason))
4095 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
4096 (let ((newnick (nth 1 erc-default-nicks))
4097 (nicklen (cdr (assoc "NICKLEN"
ff59d266 4098 (erc-with-server-buffer
597993cf
MB
4099 erc-server-parameters)))))
4100 (setq erc-bad-nick t)
4101 ;; try to use a different nick
4102 (if erc-default-nicks
4103 (setq erc-default-nicks (cdr erc-default-nicks)))
4104 (if (not newnick)
4105 (setq newnick (concat (truncate-string-to-width
4106 nick
4107 (if (and erc-server-connected nicklen)
0b6bb130
MB
4108 (- (string-to-number nicklen)
4109 (length erc-nick-uniquifier))
597993cf
MB
4110 ;; rfc2812 max nick length = 9
4111 ;; we must assume this is the
4112 ;; server's setting if we haven't
4113 ;; established a connection yet
0b6bb130 4114 (- 9 (length erc-nick-uniquifier))))
597993cf
MB
4115 erc-nick-uniquifier)))
4116 (erc-cmd-NICK newnick)
4117 (erc-display-error-notice
4118 nil
4119 (format "Nickname %s is %s, trying %s"
4120 nick reason newnick)))))
4121
4122;;; Server messages
4123
4124(defgroup erc-server-hooks nil
4125 "Server event callbacks.
a3d2f0a3 4126Every server event - like numeric replies - has its own hook.
597993cf
MB
4127Those hooks are all called using `run-hook-with-args-until-success'.
4128They receive as first argument the process object from where the event
4129originated from,
4130and as second argument the event parsed as a vector."
4131 :group 'erc-hooks)
4132
d0fcaff5 4133(defun erc-display-server-message (_proc parsed)
597993cf
MB
4134 "Display the message sent by the server as a notice."
4135 (erc-display-message
4136 parsed 'notice 'active (erc-response.contents parsed)))
4137
4138(defun erc-auto-query (proc parsed)
4139 ;; FIXME: This needs more documentation, unless it's not a user function --
4140 ;; Lawrence 2004-01-08
4141 "Put this on `erc-server-PRIVMSG-functions'."
4142 (when erc-auto-query
4143 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
4144 (target (car (erc-response.command-args parsed)))
4145 (msg (erc-response.contents parsed))
4146 (query (if (not erc-query-on-unjoined-chan-privmsg)
4147 nick
4148 (if (erc-current-nick-p target)
4149 nick
4150 target))))
4151 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
4152 (or erc-query-on-unjoined-chan-privmsg
4153 (string= target (erc-current-nick)))
4154 (not (erc-get-buffer query proc))
4155 (not (erc-is-message-ctcp-and-not-action-p msg))
2a927019 4156 (let ((erc-query-display erc-auto-query))
597993cf
MB
4157 (erc-cmd-QUERY query))
4158 nil))))
4159
4160(defun erc-is-message-ctcp-p (message)
4161 "Check if MESSAGE is a CTCP message or not."
4162 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
4163
4164(defun erc-is-message-ctcp-and-not-action-p (message)
4165 "Check if MESSAGE is a CTCP message or not."
4166 (and (erc-is-message-ctcp-p message)
4167 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
4168
4169(defun erc-format-privmessage (nick msg privp msgp)
53964682 4170 "Format a PRIVMSG in an insertable fashion."
597993cf
MB
4171 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
4172 (mark-e (if msgp (if privp "*" ">") "-"))
4173 (str (format "%s%s%s %s" mark-s nick mark-e msg))
4174 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
4175 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
4176 ;; add text properties to text before the nick, the nick and after the nick
4177 (erc-put-text-property 0 (length mark-s) 'face msg-face str)
4178 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
4179 'face nick-face str)
4180 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
4181 'face msg-face str)
4182 str))
4183
4184(defcustom erc-format-nick-function 'erc-format-nick
fb7ada5f 4185 "Function to format a nickname for message display."
597993cf
MB
4186 :group 'erc-display
4187 :type 'function)
4188
d0fcaff5 4189(defun erc-format-nick (&optional user _channel-data)
83dc6995
MB
4190 "Return the nickname of USER.
4191See also `erc-format-nick-function'."
4192 (when user (erc-server-user-nickname user)))
597993cf
MB
4193
4194(defun erc-format-@nick (&optional user channel-data)
83dc6995
MB
4195 "Format the nickname of USER showing if USER is an operator or has voice.
4196Operators have \"@\" and users with voice have \"+\" as a prefix.
4197Use CHANNEL-DATA to determine op and voice status.
4198See also `erc-format-nick-function'."
4199 (when user
4200 (let ((op (and channel-data (erc-channel-user-op channel-data) "@"))
4201 (voice (and channel-data (erc-channel-user-voice channel-data) "+")))
4202 (concat voice op (erc-server-user-nickname user)))))
597993cf
MB
4203
4204(defun erc-format-my-nick ()
a3d2f0a3 4205 "Return the beginning of this user's message, correctly propertized."
597993cf
MB
4206 (if erc-show-my-nick
4207 (let ((open "<")
4208 (close "> ")
4209 (nick (erc-current-nick)))
4210 (concat
4211 (erc-propertize open 'face 'erc-default-face)
83dc6995 4212 (erc-propertize nick 'face 'erc-my-nick-face)
597993cf
MB
4213 (erc-propertize close 'face 'erc-default-face)))
4214 (let ((prefix "> "))
4215 (erc-propertize prefix 'face 'erc-default-face))))
4216
d0fcaff5 4217(defun erc-echo-notice-in-default-buffer (s parsed buffer _sender)
597993cf
MB
4218 "Echos a private notice in the default buffer, namely the
4219target buffer specified by BUFFER, or there is no target buffer,
4220the server buffer. This function is designed to be added to
4221either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
4222and always returns t."
4223 (erc-display-message parsed nil buffer s)
4224 t)
4225
d0fcaff5 4226(defun erc-echo-notice-in-target-buffer (s parsed buffer _sender)
597993cf
MB
4227 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
4228function is designed to be added to either `erc-echo-notice-hook'
81bb49ce 4229or `erc-echo-notice-always-hook', and returns non-nil if BUFFER
597993cf
MB
4230is non-nil."
4231 (if buffer
4232 (progn (erc-display-message parsed nil buffer s) t)
4233 nil))
4234
d0fcaff5 4235(defun erc-echo-notice-in-minibuffer (s _parsed _buffer _sender)
597993cf
MB
4236 "Echos a private notice in the minibuffer. This function is
4237designed to be added to either `erc-echo-notice-hook' or
4238`erc-echo-notice-always-hook', and always returns t."
4239 (message "%s" (concat "NOTICE: " s))
4240 t)
4241
d0fcaff5 4242(defun erc-echo-notice-in-server-buffer (s parsed _buffer _sender)
597993cf
MB
4243 "Echos a private notice in the server buffer. This function is
4244designed to be added to either `erc-echo-notice-hook' or
4245`erc-echo-notice-always-hook', and always returns t."
4246 (erc-display-message parsed nil nil s)
4247 t)
4248
d0fcaff5 4249(defun erc-echo-notice-in-active-non-server-buffer (s parsed _buffer _sender)
597993cf
MB
4250 "Echos a private notice in the active buffer if the active
4251buffer is not the server buffer. This function is designed to be
4252added to either `erc-echo-notice-hook' or
81bb49ce 4253`erc-echo-notice-always-hook', and returns non-nil if the active
597993cf
MB
4254buffer is not the server buffer."
4255 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
4256 (progn (erc-display-message parsed nil 'active s) t)
4257 nil))
4258
d0fcaff5 4259(defun erc-echo-notice-in-active-buffer (s parsed _buffer _sender)
597993cf
MB
4260 "Echos a private notice in the active buffer. This function is
4261designed to be added to either `erc-echo-notice-hook' or
4262`erc-echo-notice-always-hook', and always returns t."
4263 (erc-display-message parsed nil 'active s)
4264 t)
4265
d0fcaff5 4266(defun erc-echo-notice-in-user-buffers (s parsed _buffer sender)
597993cf
MB
4267 "Echos a private notice in all of the buffers for which SENDER
4268is a member. This function is designed to be added to either
4269`erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
81bb49ce 4270returns non-nil if there is at least one buffer for which the
597993cf
MB
4271sender is a member.
4272
4273See also: `erc-echo-notice-in-first-user-buffer',
a3d2f0a3 4274`erc-buffer-list-with-nick'."
597993cf
MB
4275 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4276 (if buffers
4277 (progn (erc-display-message parsed nil buffers s) t)
4278 nil)))
4279
4280(defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
4281 "Echos a private notice in BUFFER and in all of the buffers for
4282which SENDER is a member. This function is designed to be added
4283to either `erc-echo-notice-hook' or
81bb49ce 4284`erc-echo-notice-always-hook', and returns non-nil if there is
597993cf
MB
4285at least one buffer for which the sender is a member or the
4286default target.
4287
4288See also: `erc-echo-notice-in-user-buffers',
a3d2f0a3 4289`erc-buffer-list-with-nick'."
597993cf 4290 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
d0fcaff5
SM
4291 (unless (memq buffer buffers) (push buffer buffers))
4292 (if buffers ;FIXME: How could it be nil?
597993cf
MB
4293 (progn (erc-display-message parsed nil buffers s) t)
4294 nil)))
4295
d0fcaff5 4296(defun erc-echo-notice-in-first-user-buffer (s parsed _buffer sender)
597993cf
MB
4297 "Echos a private notice in one of the buffers for which SENDER
4298is a member. This function is designed to be added to either
4299`erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
81bb49ce 4300returns non-nil if there is at least one buffer for which the
597993cf
MB
4301sender is a member.
4302
4303See also: `erc-echo-notice-in-user-buffers',
a3d2f0a3 4304`erc-buffer-list-with-nick'."
597993cf
MB
4305 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4306 (if buffers
4307 (progn (erc-display-message parsed nil (car buffers) s) t)
4308 nil)))
4309
4310;;; Ban manipulation
4311
4312(defun erc-banlist-store (proc parsed)
4313 "Record ban entries for a channel."
19dc7206
SM
4314 (pcase-let ((`(,channel ,mask ,whoset)
4315 (cdr (erc-response.command-args parsed))))
597993cf
MB
4316 ;; Determine to which buffer the message corresponds
4317 (let ((buffer (erc-get-buffer channel proc)))
4318 (with-current-buffer buffer
4319 (unless (member (cons whoset mask) erc-channel-banlist)
4320 (setq erc-channel-banlist (cons (cons whoset mask)
4321 erc-channel-banlist))))))
4322 nil)
4323
4324(defun erc-banlist-finished (proc parsed)
4325 "Record that we have received the banlist."
19dc7206 4326 (let* ((channel (nth 1 (erc-response.command-args parsed)))
597993cf
MB
4327 (buffer (erc-get-buffer channel proc)))
4328 (with-current-buffer buffer
4329 (put 'erc-channel-banlist 'received-from-server t)))
4330 t) ; suppress the 'end of banlist' message
4331
4332(defun erc-banlist-update (proc parsed)
4333 "Check MODE commands for bans and update the banlist appropriately."
4334 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
19dc7206 4335 (let* ((tgt (car (erc-response.command-args parsed)))
597993cf
MB
4336 (mode (erc-response.contents parsed))
4337 (whoset (erc-response.sender parsed))
4338 (buffer (erc-get-buffer tgt proc)))
4339 (when buffer
4340 (with-current-buffer buffer
4341 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
4342 ((string-match "^\\([+-]\\)b" mode)
4343 ;; This is a ban
4344 (cond
4345 ((string-match "^-" mode)
4346 ;; Remove the unbanned masks from the ban list
4347 (setq erc-channel-banlist
4348 (erc-delete-if
4349 #'(lambda (y)
4350 (member (upcase (cdr y))
4351 (mapcar #'upcase
4352 (cdr (split-string mode)))))
4353 erc-channel-banlist)))
4354 ((string-match "^+" mode)
4355 ;; Add the banned mask(s) to the ban list
4356 (mapc
4357 (lambda (mask)
4358 (unless (member (cons whoset mask) erc-channel-banlist)
4359 (setq erc-channel-banlist
4360 (cons (cons whoset mask) erc-channel-banlist))))
4361 (cdr (split-string mode))))))))))
4362 nil)
4363
4364;; used for the banlist cmds
4365(defun erc-group-list (list n)
4366 "Group LIST into sublists of length N."
4367 (cond ((null list) nil)
4368 ((null (nthcdr n list)) (list list))
4369 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
4370
4371
4372;;; MOTD numreplies
4373
4374(defun erc-handle-login ()
4375 "Handle the logging in process of connection."
4376 (unless erc-logged-in
4377 (setq erc-logged-in t)
4378 (message "Logging in as \'%s\'... done" (erc-current-nick))
4379 ;; execute a startup script
4380 (let ((f (erc-select-startup-file)))
4381 (when f
4382 (erc-load-script f)))))
4383
4384(defun erc-connection-established (proc parsed)
4385 "Run just after connection.
4386
a3d2f0a3 4387Set user modes and run `erc-after-connect' hook."
ff59d266
MB
4388 (with-current-buffer (process-buffer proc)
4389 (unless erc-server-connected ; only once per session
4390 (let ((server (or erc-server-announced-name
4391 (erc-response.sender parsed)))
4392 (nick (car (erc-response.command-args parsed)))
4393 (buffer (process-buffer proc)))
4394 (setq erc-server-connected t)
4395 (erc-update-mode-line)
4396 (erc-set-initial-user-mode nick buffer)
4397 (erc-server-setup-periodical-ping buffer)
4398 (run-hook-with-args 'erc-after-connect server nick)))))
4399
4400(defun erc-set-initial-user-mode (nick buffer)
4401 "If `erc-user-mode' is non-nil for NICK, set the user modes.
4402The server buffer is given by BUFFER."
4403 (with-current-buffer buffer
4404 (when erc-user-mode
4405 (let ((mode (if (functionp erc-user-mode)
4406 (funcall erc-user-mode)
4407 erc-user-mode)))
4408 (when (stringp mode)
4409 (erc-log (format "changing mode for %s to %s" nick mode))
4410 (erc-server-send (format "MODE %s %s" nick mode)))))))
597993cf
MB
4411
4412(defun erc-display-error-notice (parsed string)
4413 "Display STRING as an error notice.
4414
4415See also `erc-display-message'."
4416 (erc-display-message
4417 parsed '(notice error) 'active string))
4418
4419(defun erc-process-ctcp-query (proc parsed nick login host)
4420 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4421 "Process a CTCP query."
4422 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4423 "\C-a"))))
4424 (if (> (length queries) 4)
4425 (erc-display-message
4426 parsed (list 'notice 'error) proc 'ctcp-too-many)
4427 (if (= 0 (length queries))
4428 (erc-display-message
4429 parsed (list 'notice 'error) proc
4430 'ctcp-empty ?n nick)
4431 (while queries
4432 (let* ((type (upcase (car (split-string (car queries)))))
4433 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4434 (if (and hook (boundp hook))
4435 (if (string-equal type "ACTION")
4436 (run-hook-with-args-until-success
4437 hook proc parsed nick login host
4438 (car (erc-response.command-args parsed))
4439 (car queries))
4440 (when erc-paranoid
4441 (if (erc-current-nick-p
4442 (car (erc-response.command-args parsed)))
4443 (erc-display-message
4444 parsed 'error 'active 'ctcp-request
4445 ?n nick ?u login ?h host ?r (car queries))
4446 (erc-display-message
4447 parsed 'error 'active 'ctcp-request-to
4448 ?n nick ?u login ?h host ?r (car queries)
4449 ?t (car (erc-response.command-args parsed)))))
4450 (run-hook-with-args-until-success
4451 hook proc nick login host
4452 (car (erc-response.command-args parsed))
4453 (car queries)))
4454 (erc-display-message
4455 parsed (list 'notice 'error) proc
4456 'undefined-ctcp)))
4457 (setq queries (cdr queries)))))))
4458
4459(defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4460
4461(defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4462 "Respond to a CTCP ACTION query."
4463 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4464 (let ((s (match-string 1 msg))
4465 (buf (or (erc-get-buffer to proc)
4466 (erc-get-buffer nick proc)
4467 (process-buffer proc))))
4468 (erc-display-message
4469 parsed 'action buf
4470 'ACTION ?n nick ?u login ?h host ?a s))))
4471
4472(defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4473
d0fcaff5 4474(defun erc-ctcp-query-CLIENTINFO (_proc nick _login _host _to msg)
597993cf
MB
4475 "Respond to a CTCP CLIENTINFO query."
4476 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4477 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4478 (unless erc-disable-ctcp-replies
4479 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4480 nil)
4481
4482(defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
d0fcaff5 4483(defun erc-ctcp-query-ECHO (_proc nick _login _host _to msg)
597993cf
MB
4484 "Respond to a CTCP ECHO query."
4485 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4486 (let ((s (match-string 1 msg)))
4487 (unless erc-disable-ctcp-replies
4488 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4489 nil)
4490
4491(defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
d0fcaff5 4492(defun erc-ctcp-query-FINGER (_proc nick _login _host _to _msg)
597993cf
MB
4493 "Respond to a CTCP FINGER query."
4494 (unless erc-disable-ctcp-replies
4495 (let ((s (if erc-anonymous-login
4496 (format "FINGER I'm %s." (erc-current-nick))
4497 (format "FINGER %s (%s@%s)."
4498 (user-full-name)
4499 (user-login-name)
4500 (system-name))))
4501 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4502 (when (> ns 0)
4503 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4504 (erc-send-ctcp-notice nick s)))
4505 nil)
4506
4507(defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
d0fcaff5 4508(defun erc-ctcp-query-PING (_proc nick _login _host _to msg)
597993cf
MB
4509 "Respond to a CTCP PING query."
4510 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4511 (unless erc-disable-ctcp-replies
4512 (let ((arg (match-string 1 msg)))
4513 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4514 nil)
4515
4516(defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
d0fcaff5 4517(defun erc-ctcp-query-TIME (_proc nick _login _host _to _msg)
597993cf
MB
4518 "Respond to a CTCP TIME query."
4519 (unless erc-disable-ctcp-replies
4520 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4521 nil)
4522
4523(defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
d0fcaff5 4524(defun erc-ctcp-query-USERINFO (_proc nick _login _host _to _msg)
597993cf
MB
4525 "Respond to a CTCP USERINFO query."
4526 (unless erc-disable-ctcp-replies
4527 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4528 nil)
4529
4530(defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
d0fcaff5 4531(defun erc-ctcp-query-VERSION (_proc nick _login _host _to _msg)
597993cf
MB
4532 "Respond to a CTCP VERSION query."
4533 (unless erc-disable-ctcp-replies
4534 (erc-send-ctcp-notice
4535 nick (format
4536 "VERSION \C-bERC\C-b %s - an IRC client for emacs (\C-b%s\C-b)"
4537 erc-version-string
4538 erc-official-location)))
4539 nil)
4540
4541(defun erc-process-ctcp-reply (proc parsed nick login host msg)
4542 "Process MSG as a CTCP reply."
4543 (let* ((type (car (split-string msg)))
4544 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4545 (if (boundp hook)
4546 (run-hook-with-args-until-success
4547 hook proc nick login host
4548 (car (erc-response.command-args parsed)) msg)
4549 (erc-display-message
4550 parsed 'notice 'active
4551 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4552
4553(defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
d0fcaff5 4554(defun erc-ctcp-reply-ECHO (_proc nick _login _host _to msg)
597993cf
MB
4555 "Handle a CTCP ECHO reply."
4556 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4557 (let ((message (match-string 1 msg)))
4558 (erc-display-message
4559 nil '(notice action) 'active
4560 'CTCP-ECHO ?n nick ?m message)))
4561 nil)
4562
4563(defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
d0fcaff5 4564(defun erc-ctcp-reply-CLIENTINFO (_proc nick _login _host _to msg)
597993cf
MB
4565 "Handle a CTCP CLIENTINFO reply."
4566 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4567 (let ((message (match-string 1 msg)))
4568 (erc-display-message
4569 nil 'notice 'active
4570 'CTCP-CLIENTINFO ?n nick ?m message)))
4571 nil)
4572
4573(defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
d0fcaff5 4574(defun erc-ctcp-reply-FINGER (_proc nick _login _host _to msg)
597993cf
MB
4575 "Handle a CTCP FINGER reply."
4576 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4577 (let ((message (match-string 1 msg)))
4578 (erc-display-message
4579 nil 'notice 'active
4580 'CTCP-FINGER ?n nick ?m message)))
4581 nil)
4582
4583(defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
d0fcaff5 4584(defun erc-ctcp-reply-PING (_proc nick _login _host _to msg)
597993cf
MB
4585 "Handle a CTCP PING reply."
4586 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4587 nil
4588 (let ((time (match-string 1 msg)))
4589 (condition-case nil
4590 (let ((delta (erc-time-diff (string-to-number time)
4591 (erc-current-time))))
4592 (erc-display-message
4593 nil 'notice 'active
4594 'CTCP-PING ?n nick
4595 ?t (erc-sec-to-time delta)))
4596 (range-error
4597 (erc-display-message
4598 nil 'error 'active
4599 'bad-ping-response ?n nick ?t time))))))
4600
4601(defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
d0fcaff5 4602(defun erc-ctcp-reply-TIME (_proc nick _login _host _to msg)
597993cf
MB
4603 "Handle a CTCP TIME reply."
4604 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4605 (let ((message (match-string 1 msg)))
4606 (erc-display-message
4607 nil 'notice 'active
4608 'CTCP-TIME ?n nick ?m message)))
4609 nil)
4610
4611(defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
d0fcaff5 4612(defun erc-ctcp-reply-VERSION (_proc nick _login _host _to msg)
597993cf
MB
4613 "Handle a CTCP VERSION reply."
4614 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4615 (let ((message (match-string 1 msg)))
4616 (erc-display-message
4617 nil 'notice 'active
4618 'CTCP-VERSION ?n nick ?m message)))
4619 nil)
4620
4621(defun erc-process-away (proc away-p)
6904f7fe
MB
4622 "Toggle the away status of the user depending on the value of AWAY-P.
4623
4624If nil, set the user as away.
4625If non-nil, return from being away."
597993cf
MB
4626 (let ((sessionbuf (process-buffer proc)))
4627 (when sessionbuf
4628 (with-current-buffer sessionbuf
4629 (when erc-away-nickname
4630 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4631 erc-away-nickname away-p))
4632 (erc-cmd-NICK (if away-p
4633 erc-away-nickname
4634 erc-nick)))
4635 (cond
4636 (away-p
ff59d266 4637 (setq erc-away (current-time)))
597993cf
MB
4638 (t
4639 (let ((away-time erc-away))
4640 ;; away must be set to NIL BEFORE sending anything to prevent
4641 ;; an infinite recursion
ff59d266 4642 (setq erc-away nil)
937e6a56 4643 (with-current-buffer (erc-active-buffer)
597993cf
MB
4644 (when erc-public-away-p
4645 (erc-send-action
4646 (erc-default-target)
4647 (if away-time
4648 (format "is back (gone for %s)"
4649 (erc-sec-to-time
4650 (erc-time-diff
4651 (erc-emacs-time-to-erc-time away-time)
4652 (erc-current-time))))
4653 "is back")))))))))
4654 (erc-update-mode-line)))
4655
4656;;;; List of channel members handling
4657
4658(defun erc-channel-begin-receiving-names ()
4659 "Internal function.
4660
4661Used when a channel names list is about to be received. Should
4662be called with the current buffer set to the channel buffer.
4663
4664See also `erc-channel-end-receiving-names'."
4665 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4666
4667(defun erc-channel-end-receiving-names ()
4668 "Internal function.
4669
4670Used to fix `erc-channel-users' after a channel names list has been
4671received. Should be called with the current buffer set to the
4672channel buffer.
4673
4674See also `erc-channel-begin-receiving-names'."
d0fcaff5 4675 (maphash (lambda (nick _user)
597993cf
MB
4676 (if (null (gethash nick erc-channel-new-member-names))
4677 (erc-remove-channel-user nick)))
4678 erc-channel-users)
4679 (setq erc-channel-new-member-names nil))
4680
526dc846
MO
4681(defun erc-parse-prefix ()
4682 "Return an alist of valid prefix character types and their representations.
4683Example: (operator) o => @, (voiced) v => +."
4684 (let ((str (or (cdr (assoc "PREFIX" (erc-with-server-buffer
4685 erc-server-parameters)))
4686 ;; provide a sane default
4687 "(ov)@+"))
4688 types chars)
4689 (when (string-match "^(\\([^)]+\\))\\(.+\\)$" str)
4690 (setq types (match-string 1 str)
4691 chars (match-string 2 str))
4692 (let ((len (min (length types) (length chars)))
4693 (i 0)
4694 (alist nil))
4695 (while (< i len)
4696 (setq alist (cons (cons (elt types i) (elt chars i))
4697 alist))
4698 (setq i (1+ i)))
4699 alist))))
4700
597993cf
MB
4701(defun erc-channel-receive-names (names-string)
4702 "This function is for internal use only.
4703
4704Update `erc-channel-users' according to NAMES-STRING.
4705NAMES-STRING is a string listing some of the names on the
4706channel."
526dc846
MO
4707 (let (prefix op-ch voice-ch names name op voice)
4708 (setq prefix (erc-parse-prefix))
4709 (setq op-ch (cdr (assq ?o prefix))
4710 voice-ch (cdr (assq ?v prefix)))
4711 ;; We need to delete "" because in XEmacs, (split-string "a ")
4712 ;; returns ("a" "").
4713 (setq names (delete "" (split-string names-string)))
4714 (let ((erc-channel-members-changed-hook nil))
4715 (dolist (item names)
d0fcaff5 4716 (let ((updatep t))
526dc846
MO
4717 (if (rassq (elt item 0) prefix)
4718 (cond ((= (length item) 1)
4719 (setq updatep nil))
4720 ((eq (elt item 0) op-ch)
4721 (setq name (substring item 1)
4722 op 'on
4723 voice 'off))
4724 ((eq (elt item 0) voice-ch)
4725 (setq name (substring item 1)
4726 op 'off
4727 voice 'on))
4728 (t (setq name (substring item 1)
4729 op 'off
4730 voice 'off)))
4731 (setq name item
4732 op 'off
4733 voice 'off))
4734 (when updatep
4735 (puthash (erc-downcase name) t
4736 erc-channel-new-member-names)
4737 (erc-update-current-channel-member
4738 name name t op voice)))))
597993cf
MB
4739 (run-hooks 'erc-channel-members-changed-hook)))
4740
4741(defcustom erc-channel-members-changed-hook nil
fb7ada5f 4742 "This hook is called every time the variable `channel-members' changes.
597993cf
MB
4743The buffer where the change happened is current while this hook is called."
4744 :group 'erc-hooks
4745 :type 'hook)
4746
4747(defun erc-update-user-nick (nick &optional new-nick
4748 host login full-name info)
d0fcaff5 4749 "Update the stored user information for the user with nickname NICK.
597993cf 4750
a3d2f0a3 4751See also: `erc-update-user'."
597993cf
MB
4752 (erc-update-user (erc-get-server-user nick) new-nick
4753 host login full-name info))
4754
4755(defun erc-update-user (user &optional new-nick
4756 host login full-name info)
4757 "Update user info for USER. USER must be an erc-server-user
4758struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4759non-nil and not equal to the existing values for USER are used to
4760replace the stored values in USER.
4761
a3d2f0a3 4762If, and only if, a change is made,
597993cf 4763`erc-channel-members-changed-hook' is run for each channel for
a3d2f0a3 4764which USER is a member, and t is returned."
597993cf
MB
4765 (let (changed)
4766 (when user
4767 (when (and new-nick
4768 (not (equal (erc-server-user-nickname user)
4769 new-nick)))
4770 (setq changed t)
4771 (erc-change-user-nickname user new-nick))
4772 (when (and host
4773 (not (equal (erc-server-user-host user) host)))
4774 (setq changed t)
4775 (setf (erc-server-user-host user) host))
4776 (when (and login
4777 (not (equal (erc-server-user-login user) login)))
4778 (setq changed t)
4779 (setf (erc-server-user-login user) login))
4780 (when (and full-name
4781 (not (equal (erc-server-user-full-name user)
4782 full-name)))
4783 (setq changed t)
4784 (setf (erc-server-user-full-name user) full-name))
4785 (when (and info
4786 (not (equal (erc-server-user-info user) info)))
4787 (setq changed t)
4788 (setf (erc-server-user-info user) info))
4789 (if changed
4790 (dolist (buf (erc-server-user-buffers user))
4791 (if (buffer-live-p buf)
4792 (with-current-buffer buf
4793 (run-hooks 'erc-channel-members-changed-hook))))))
4794 changed))
4795
4796(defun erc-update-current-channel-member
4797 (nick new-nick &optional add op voice host login full-name info
4798 update-message-time)
d0fcaff5
SM
4799 "Update the stored user information for the user with nickname NICK.
4800`erc-update-user' is called to handle changes to nickname,
a3d2f0a3
JB
4801HOST, LOGIN, FULL-NAME, and INFO. If OP or VOICE are non-nil,
4802they must be equal to either `on' or `off', in which case the
4803operator or voice status of the user in the current channel is
4804changed accordingly. If UPDATE-MESSAGE-TIME is non-nil, the
597993cf
MB
4805last-message-time of the user in the current channel is set
4806to (current-time).
4807
4808If ADD is non-nil, the user will be added with the specified
4809information if it is not already present in the user or channel
4810lists.
4811
4812If, and only if, changes are made, or the user is added,
a3d2f0a3 4813`erc-channel-members-updated-hook' is run, and t is returned.
597993cf
MB
4814
4815See also: `erc-update-user' and `erc-update-channel-member'."
4816 (let* (changed user-changed
4817 (channel-data (erc-get-channel-user nick))
d0fcaff5 4818 (cuser (cdr channel-data))
597993cf
MB
4819 (user (if channel-data (car channel-data)
4820 (erc-get-server-user nick))))
4821 (if cuser
4822 (progn
4823 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4824 (when (and op
4825 (not (eq (erc-channel-user-op cuser) op)))
4826 (setq changed t)
4827 (setf (erc-channel-user-op cuser)
4828 (cond ((eq op 'on) t)
526dc846
MO
4829 ((eq op 'off) nil)
4830 (t op))))
597993cf
MB
4831 (when (and voice
4832 (not (eq (erc-channel-user-voice cuser) voice)))
4833 (setq changed t)
4834 (setf (erc-channel-user-voice cuser)
4835 (cond ((eq voice 'on) t)
526dc846
MO
4836 ((eq voice 'off) nil)
4837 (t voice))))
597993cf
MB
4838 (when update-message-time
4839 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4840 (setq user-changed
4841 (erc-update-user user new-nick
4842 host login full-name info)))
4843 (when add
4844 (if (null user)
4845 (progn
4846 (setq user (make-erc-server-user
4847 :nickname nick
4848 :host host
4849 :full-name full-name
4850 :login login
4851 :info info
4852 :buffers (list (current-buffer))))
4853 (erc-add-server-user nick user))
4854 (setf (erc-server-user-buffers user)
4855 (cons (current-buffer)
4856 (erc-server-user-buffers user))))
4857 (setq cuser (make-erc-channel-user
4858 :op (cond ((eq op 'on) t)
526dc846
MO
4859 ((eq op 'off) nil)
4860 (t op))
597993cf 4861 :voice (cond ((eq voice 'on) t)
526dc846
MO
4862 ((eq voice 'off) nil)
4863 (t voice))
597993cf
MB
4864 :last-message-time
4865 (if update-message-time (current-time))))
4866 (puthash (erc-downcase nick) (cons user cuser)
4867 erc-channel-users)
4868 (setq changed t)))
4869 (when (and changed (null user-changed))
4870 (run-hooks 'erc-channel-members-changed-hook))
4871 (or changed user-changed add)))
4872
4873(defun erc-update-channel-member (channel nick new-nick
4874 &optional add op voice host login
4875 full-name info update-message-time)
d0fcaff5 4876 "Update user and channel information for the user with
597993cf
MB
4877nickname NICK in channel CHANNEL.
4878
a3d2f0a3 4879See also: `erc-update-current-channel-member'."
597993cf
MB
4880 (erc-with-buffer
4881 (channel)
4882 (erc-update-current-channel-member nick new-nick add op voice host
4883 login full-name info
4884 update-message-time)))
4885
4886(defun erc-remove-current-channel-member (nick)
a3d2f0a3
JB
4887 "Remove NICK from current channel membership list.
4888Runs `erc-channel-members-changed-hook'."
597993cf
MB
4889 (let ((channel-data (erc-get-channel-user nick)))
4890 (when channel-data
4891 (erc-remove-channel-user nick)
4892 (run-hooks 'erc-channel-members-changed-hook))))
4893
4894(defun erc-remove-channel-member (channel nick)
4895 "Remove NICK from CHANNEL's membership list.
4896
4897See also `erc-remove-current-channel-member'."
4898 (erc-with-buffer
4899 (channel)
4900 (erc-remove-current-channel-member nick)))
4901
4902(defun erc-update-channel-topic (channel topic &optional modify)
4903 "Find a buffer for CHANNEL and set the TOPIC for it.
4904
4905If optional MODIFY is 'append or 'prepend, then append or prepend the
4906TOPIC string to the current topic."
4907 (erc-with-buffer (channel)
4908 (cond ((eq modify 'append)
4909 (setq erc-channel-topic (concat erc-channel-topic topic)))
4910 ((eq modify 'prepend)
4911 (setq erc-channel-topic (concat topic erc-channel-topic)))
4912 (t (setq erc-channel-topic topic)))
4913 (erc-update-mode-line-buffer (current-buffer))))
4914
4915(defun erc-set-modes (tgt mode-string)
4916 "Set the modes for the TGT provided as MODE-STRING."
4917 (let* ((modes (erc-parse-modes mode-string))
4918 (add-modes (nth 0 modes))
597993cf
MB
4919 ;; list of triples: (mode-char 'on/'off argument)
4920 (arg-modes (nth 2 modes)))
4921 (cond ((erc-channel-p tgt); channel modes
83dc6995 4922 (let ((buf (and erc-server-process
597993cf
MB
4923 (erc-get-buffer tgt erc-server-process))))
4924 (when buf
4925 (with-current-buffer buf
4926 (setq erc-channel-modes add-modes)
4927 (setq erc-channel-user-limit nil)
4928 (setq erc-channel-key nil)
4929 (while arg-modes
4930 (let ((mode (nth 0 (car arg-modes)))
4931 (onoff (nth 1 (car arg-modes)))
4932 (arg (nth 2 (car arg-modes))))
4933 (cond ((string-match "^[Ll]" mode)
4934 (erc-update-channel-limit tgt onoff arg))
4935 ((string-match "^[Kk]" mode)
4936 (erc-update-channel-key tgt onoff arg))
4937 (t nil)))
4938 (setq arg-modes (cdr arg-modes)))
4939 (erc-update-mode-line-buffer buf)))))
4940 ;; we do not keep our nick's modes yet
4941 ;;(t (setq erc-user-modes add-modes))
4942 )
4943 ))
4944
4945(defun erc-sort-strings (list-of-strings)
4946 "Sort LIST-OF-STRINGS in lexicographic order.
4947
4948Side-effect free."
4949 (sort (copy-sequence list-of-strings) 'string<))
4950
4951(defun erc-parse-modes (mode-string)
4952 "Parse MODE-STRING into a list.
4953
4954Returns a list of three elements:
4955
4956 (ADD-MODES REMOVE-MODES ARG-MODES).
4957
4958The add-modes and remove-modes are lists of single-character strings
4959for modes without parameters to add and remove respectively. The
4960arg-modes is a list of triples of the form:
4961
4962 (MODE-CHAR ON/OFF ARGUMENT)."
4963 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
4964 (let ((chars (mapcar 'char-to-string (match-string 1 mode-string)))
4965 ;; arguments in channel modes
4966 (args-str (match-string 2 mode-string))
4967 (args nil)
4968 (add-modes nil)
4969 (remove-modes nil)
4970 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
4971 (add-p t))
4972 ;; make the argument list
4973 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
4974 (setq args (cons (match-string 1 args-str) args))
4975 (setq args-str (match-string 2 args-str)))
4976 (setq args (nreverse args))
4977 ;; collect what modes changed, and match them with arguments
4978 (while chars
4979 (cond ((string= (car chars) "+") (setq add-p t))
4980 ((string= (car chars) "-") (setq add-p nil))
4981 ((string-match "^[ovbOVB]" (car chars))
4982 (setq arg-modes (cons (list (car chars)
4983 (if add-p 'on 'off)
4984 (if args (car args) nil))
4985 arg-modes))
4986 (if args (setq args (cdr args))))
4987 ((string-match "^[LlKk]" (car chars))
4988 (setq arg-modes (cons (list (car chars)
4989 (if add-p 'on 'off)
4990 (if (and add-p args)
4991 (car args) nil))
4992 arg-modes))
4993 (if (and add-p args) (setq args (cdr args))))
4994 (add-p (setq add-modes (cons (car chars) add-modes)))
4995 (t (setq remove-modes (cons (car chars) remove-modes))))
4996 (setq chars (cdr chars)))
4997 (setq add-modes (nreverse add-modes))
4998 (setq remove-modes (nreverse remove-modes))
4999 (setq arg-modes (nreverse arg-modes))
5000 (list add-modes remove-modes arg-modes))
5001 nil))
5002
5003(defun erc-update-modes (tgt mode-string &optional nick host login)
5004 "Update the mode information for TGT, provided as MODE-STRING.
5005Optional arguments: NICK, HOST and LOGIN - the attributes of the
5006person who changed the modes."
d0fcaff5 5007 ;; FIXME: neither of nick, host, and login are used!
597993cf
MB
5008 (let* ((modes (erc-parse-modes mode-string))
5009 (add-modes (nth 0 modes))
5010 (remove-modes (nth 1 modes))
5011 ;; list of triples: (mode-char 'on/'off argument)
5012 (arg-modes (nth 2 modes)))
5013 ;; now parse the modes changes and do the updates
5014 (cond ((erc-channel-p tgt); channel modes
83dc6995 5015 (let ((buf (and erc-server-process
597993cf
MB
5016 (erc-get-buffer tgt erc-server-process))))
5017 (when buf
5018 ;; FIXME! This used to have an original buffer
5019 ;; variable, but it never switched back to the original
5020 ;; buffer. Is this wanted behavior?
5021 (set-buffer buf)
5022 (if (not (boundp 'erc-channel-modes))
5023 (setq erc-channel-modes nil))
5024 (while remove-modes
5025 (setq erc-channel-modes (delete (car remove-modes)
5026 erc-channel-modes)
5027 remove-modes (cdr remove-modes)))
5028 (while add-modes
5029 (setq erc-channel-modes (cons (car add-modes)
5030 erc-channel-modes)
5031 add-modes (cdr add-modes)))
5032 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
5033 (while arg-modes
5034 (let ((mode (nth 0 (car arg-modes)))
5035 (onoff (nth 1 (car arg-modes)))
5036 (arg (nth 2 (car arg-modes))))
5037 (cond ((string-match "^[oO]" mode)
5038 (erc-update-channel-member tgt arg arg nil onoff))
5039 ((string-match "^[Vv]" mode)
5040 (erc-update-channel-member tgt arg arg nil nil
5041 onoff))
5042 ((string-match "^[Ll]" mode)
5043 (erc-update-channel-limit tgt onoff arg))
5044 ((string-match "^[Kk]" mode)
5045 (erc-update-channel-key tgt onoff arg))
5046 (t nil)); only ops are tracked now
5047 (setq arg-modes (cdr arg-modes))))
5048 (erc-update-mode-line buf))))
5049 ;; nick modes - ignored at this point
5050 (t nil))))
5051
5052(defun erc-update-channel-limit (channel onoff n)
5053 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
5054 "Update CHANNEL's user limit to N."
5055 (if (or (not (eq onoff 'on))
5056 (and (stringp n) (string-match "^[0-9]+$" n)))
5057 (erc-with-buffer
5058 (channel)
5059 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
5060 (t (setq erc-channel-user-limit nil))))))
5061
5062(defun erc-update-channel-key (channel onoff key)
5063 "Update CHANNEL's key to KEY if ONOFF is 'on or to nil if it's 'off."
5064 (erc-with-buffer
5065 (channel)
5066 (cond ((eq onoff 'on) (setq erc-channel-key key))
5067 (t (setq erc-channel-key nil)))))
5068
5069(defun erc-handle-user-status-change (type nlh &optional l)
5070 "Handle changes in any user's status.
5071
5072So far, only nick change is handled.
5073
5074Generally, the TYPE argument is a symbol describing the change type, NLH is
5075a list containing the original nickname, login name and hostname for the user,
5076and L is a list containing additional TYPE-specific arguments.
5077
5078So far the following TYPE/L pairs are supported:
5079
5080 Event TYPE L
5081
5082 nickname change 'nick (NEW-NICK)"
5083 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
5084 (cond
5085 ;; nickname change
5086 ((equal type 'nick)
5087 t)
5088 (t
5089 nil)))
5090
5091(defun erc-highlight-notice (s)
5092 "Highlight notice message S and return it.
a3d2f0a3 5093See also variable `erc-notice-highlight-type'."
597993cf
MB
5094 (cond
5095 ((eq erc-notice-highlight-type 'prefix)
5096 (erc-put-text-property 0 (length erc-notice-prefix)
5097 'face 'erc-notice-face s)
5098 s)
5099 ((eq erc-notice-highlight-type 'all)
5100 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
5101 s)
5102 (t s)))
5103
5104(defun erc-make-notice (message)
5105 "Notify the user of MESSAGE."
5106 (when erc-minibuffer-notice
5107 (message "%s" message))
5108 (erc-highlight-notice (concat erc-notice-prefix message)))
5109
5110(defun erc-highlight-error (s)
5111 "Highlight error message S and return it."
5112 (erc-put-text-property 0 (length s) 'face 'erc-error-face s)
5113 s)
5114
5115(defun erc-put-text-property (start end property value &optional object)
5116 "Set text-property for an object (usually a string).
5117START and END define the characters covered.
5118PROPERTY is the text-property set, usually the symbol `face'.
5119VALUE is the value for the text-property, usually a face symbol such as
5120the face `bold' or `erc-pal-face'.
5121OBJECT is a string which will be modified and returned.
5122OBJECT is modified without being copied first.
5123
5124You can redefine or `defadvice' this function in order to add
5125EmacsSpeak support."
5126 (put-text-property start end property value object))
5127
5128(defun erc-list (thing)
5129 "Return THING if THING is a list, or a list with THING as its element."
5130 (if (listp thing)
5131 thing
5132 (list thing)))
5133
5134(defun erc-parse-user (string)
5135 "Parse STRING as a user specification (nick!login@host).
5136
5137Return a list of the three separate tokens."
5138 (cond
21bc768b 5139 ((string-match "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$" string)
597993cf
MB
5140 (list (match-string 1 string)
5141 (match-string 2 string)
5142 (match-string 3 string)))
5143 ;; Some bogus bouncers send Nick!(null), try to live with that.
21bc768b 5144 ((string-match "^\\([^!\n]*\\)!\\(.*\\)$" string)
597993cf
MB
5145 (list (match-string 1 string)
5146 ""
5147 (match-string 2 string)))
5148 (t
5149 (list string "" ""))))
5150
5151(defun erc-extract-nick (string)
5152 "Return the nick corresponding to a user specification STRING.
5153
5154See also `erc-parse-user'."
5155 (car (erc-parse-user string)))
5156
5157(defun erc-put-text-properties (start end properties
5158 &optional object value-list)
5159 "Set text-properties for OBJECT.
5160
5161START and END describe positions in OBJECT.
5162If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
5163each property to the corresponding value in VALUE-LIST."
5164 (unless value-list
d0fcaff5 5165 (setq value-list (mapcar (lambda (_x) t)
597993cf 5166 properties)))
40ef8242
MO
5167 (while (and properties value-list)
5168 (erc-put-text-property
5169 start end (pop properties) (pop value-list) object)))
597993cf
MB
5170
5171;;; Input area handling:
5172
5173(defun erc-beg-of-input-line ()
5174 "Return the value of `point' at the beginning of the input line.
5175
5176Specifically, return the position of `erc-insert-marker'."
5177 (or (and (boundp 'erc-insert-marker)
5178 (markerp erc-insert-marker))
5179 (error "erc-insert-marker has no value, please report a bug"))
5180 (marker-position erc-insert-marker))
5181
5182(defun erc-end-of-input-line ()
5183 "Return the value of `point' at the end of the input line."
5184 (point-max))
5185
21859ebc
EH
5186(defvar erc-last-input-time 0
5187 "Time of last call to `erc-send-current-line'.
5188If that function has never been called, the value is 0.")
5189
5190(defcustom erc-accidental-paste-threshold-seconds nil
5191 "Minimum time, in seconds, before sending new lines via IRC.
5192If the value is a number, `erc-send-current-line' signals an
5193error if its previous invocation was less than this much time
5194ago. This is useful so that if you accidentally enter large
5195amounts of text into the ERC buffer, that text is not sent to the
5196IRC server.
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: