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