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