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