]> code.delx.au - gnu-emacs/blob - lisp/erc/erc.el
Merge from emacs-24; up to 2014-07-09T02:04:12Z!rgm@gnu.org
[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 ;; Version: 5.3
16
17 ;; This file is part of GNU Emacs.
18
19 ;; GNU Emacs is free software: you can redistribute it and/or modify
20 ;; it under the terms of the GNU General Public License as published by
21 ;; the Free Software Foundation, either version 3 of the License, or
22 ;; (at your option) any later version.
23
24 ;; GNU Emacs is distributed in the hope that it will be useful,
25 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ;; GNU General Public License for more details.
28
29 ;; You should have received a copy of the GNU General Public License
30 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31
32 ;;; Commentary:
33
34 ;; ERC is a powerful, modular, and extensible IRC client for Emacs.
35
36 ;; For more information, see the following URLs:
37 ;; * http://sv.gnu.org/projects/erc/
38 ;; * http://www.emacswiki.org/cgi-bin/wiki/ERC
39
40 ;; As of 2006-06-13, ERC development is now hosted on Savannah
41 ;; (http://sv.gnu.org/projects/erc). I invite everyone who wants to
42 ;; hack on it to contact me <mwolson@gnu.org> in order to get write
43 ;; access to the shared Arch archive.
44
45 ;; Installation:
46
47 ;; Put erc.el in your load-path, and put (require 'erc) in your .emacs.
48
49 ;; Configuration:
50
51 ;; Use M-x customize-group RET erc RET to get an overview
52 ;; of all the variables you can tweak.
53
54 ;; Usage:
55
56 ;; To connect to an IRC server, do
57 ;;
58 ;; M-x erc RET
59 ;;
60 ;; After you are connected to a server, you can use C-h m or have a look at
61 ;; the ERC menu.
62
63 ;;; History:
64 ;;
65
66 ;;; Code:
67
68 (defconst erc-version-string "Version 5.3"
69 "ERC version. This is used by function `erc-version'.")
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-reason-normal (&optional s)
3384 "Normal quit message.
3385
3386 If S is non-nil, it will be used as the quit reason."
3387 (or s
3388 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3389 erc-version-string) ; erc-official-location)
3390 ))
3391
3392 (defun erc-quit-reason-zippy (&optional s)
3393 "Zippy quit message.
3394
3395 If S is non-nil, it will be used as the quit reason."
3396 (or s
3397 (if (fboundp 'yow)
3398 (erc-replace-regexp-in-string "\n" "" (yow))
3399 (erc-quit-reason-normal))))
3400
3401 (make-obsolete 'erc-quit-reason-zippy "it will be removed." "24.4")
3402
3403 (defun erc-quit-reason-various (s)
3404 "Choose a quit reason based on S (a string)."
3405 (when (featurep 'xemacs) (require 'poe))
3406 (let ((res (car (assoc-default (or s "")
3407 erc-quit-reason-various-alist 'string-match))))
3408 (cond
3409 ((functionp res) (funcall res))
3410 ((stringp res) res)
3411 (s s)
3412 (t (erc-quit-reason-normal)))))
3413
3414 (defun erc-part-reason-normal (&optional s)
3415 "Normal part message.
3416
3417 If S is non-nil, it will be used as the quit reason."
3418 (or s
3419 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3420 erc-version-string) ; erc-official-location)
3421 ))
3422
3423 (defun erc-part-reason-zippy (&optional s)
3424 "Zippy part message.
3425
3426 If S is non-nil, it will be used as the quit reason."
3427 (or s
3428 (if (fboundp 'yow)
3429 (erc-replace-regexp-in-string "\n" "" (yow))
3430 (erc-part-reason-normal))))
3431
3432 (make-obsolete 'erc-part-reason-zippy "it will be removed." "24.4")
3433
3434 (defun erc-part-reason-various (s)
3435 "Choose a part reason based on S (a string)."
3436 (when (featurep 'xemacs) (require 'poe))
3437 (let ((res (car (assoc-default (or s "")
3438 erc-part-reason-various-alist 'string-match))))
3439 (cond
3440 ((functionp res) (funcall res))
3441 ((stringp res) res)
3442 (s s)
3443 (t (erc-part-reason-normal)))))
3444
3445 (defun erc-cmd-QUIT (reason)
3446 "Disconnect from the current server.
3447 If REASON is omitted, display a default quit message, otherwise display
3448 the message given by REASON."
3449 (unless reason
3450 (setq reason ""))
3451 (cond
3452 ((string-match "^\\s-*\\(.*\\)$" reason)
3453 (let* ((s (match-string 1 reason))
3454 (buffer (erc-server-buffer))
3455 (reason (funcall erc-quit-reason (if (equal s "") nil s)))
3456 server-proc)
3457 (with-current-buffer (if (and buffer
3458 (bufferp buffer))
3459 buffer
3460 (current-buffer))
3461 (erc-log (format "cmd: QUIT: %s" reason))
3462 (setq erc-server-quitting t)
3463 (erc-set-active-buffer (erc-server-buffer))
3464 (setq server-proc erc-server-process)
3465 (erc-server-send (format "QUIT :%s" reason)))
3466 (run-hook-with-args 'erc-quit-hook server-proc)
3467 (when erc-kill-queries-on-quit
3468 (erc-kill-query-buffers server-proc))
3469 ;; if the process has not been killed within 4 seconds, kill it
3470 (run-at-time 4 nil
3471 (lambda (proc)
3472 (when (and (processp proc)
3473 (memq (process-status proc) '(run open)))
3474 (delete-process proc)))
3475 server-proc))
3476 t)
3477 (t nil)))
3478
3479 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3480 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3481 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3482 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3483 (put 'erc-cmd-QUIT 'process-not-needed t)
3484
3485 (defun erc-cmd-GQUIT (reason)
3486 "Disconnect from all servers at once with the same quit REASON."
3487 (erc-with-all-buffers-of-server nil #'erc-open-server-buffer-p
3488 (erc-cmd-QUIT reason))
3489 (when erc-kill-queries-on-quit
3490 ;; if the query buffers have not been killed within 4 seconds,
3491 ;; kill them
3492 (run-at-time
3493 4 nil
3494 (lambda ()
3495 (dolist (buffer (erc-buffer-list (lambda (buf)
3496 (not (erc-server-buffer-p buf)))))
3497 (kill-buffer buffer)))))
3498 t)
3499
3500 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3501 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3502 (put 'erc-cmd-GQUIT 'process-not-needed t)
3503
3504 (defun erc-cmd-RECONNECT ()
3505 "Try to reconnect to the current IRC server."
3506 (let ((buffer (erc-server-buffer))
3507 (process nil))
3508 (unless (buffer-live-p buffer)
3509 (setq buffer (current-buffer)))
3510 (with-current-buffer buffer
3511 (setq erc-server-quitting nil)
3512 (setq erc-server-reconnecting t)
3513 (setq erc-server-reconnect-count 0)
3514 (setq process (get-buffer-process (erc-server-buffer)))
3515 (if process
3516 (delete-process process)
3517 (erc-server-reconnect))
3518 (setq erc-server-reconnecting nil)))
3519 t)
3520 (put 'erc-cmd-RECONNECT 'process-not-needed t)
3521
3522 (defun erc-cmd-SERVER (server)
3523 "Connect to SERVER, leaving existing connection intact."
3524 (erc-log (format "cmd: SERVER: %s" server))
3525 (condition-case nil
3526 (erc :server server :nick (erc-current-nick))
3527 (error
3528 (erc-error "Cannot find host %s." server)))
3529 t)
3530 (put 'erc-cmd-SERVER 'process-not-needed t)
3531
3532 (defvar motif-version-string)
3533 (defvar gtk-version-string)
3534
3535 (defun erc-cmd-SV ()
3536 "Say the current ERC and Emacs version into channel."
3537 (erc-send-message (format "I'm using ERC %s with %s %s (%s%s) of %s."
3538 erc-version-string
3539 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3540 emacs-version
3541 system-configuration
3542 (concat
3543 (cond ((featurep 'motif)
3544 (concat ", " (substring
3545 motif-version-string 4)))
3546 ((featurep 'gtk)
3547 (concat ", GTK+ Version "
3548 gtk-version-string))
3549 ((featurep 'x-toolkit) ", X toolkit")
3550 (t ""))
3551 (if (and (boundp 'x-toolkit-scroll-bars)
3552 (memq x-toolkit-scroll-bars
3553 '(xaw xaw3d)))
3554 (format ", %s scroll bars"
3555 (capitalize (symbol-name
3556 x-toolkit-scroll-bars)))
3557 "")
3558 (if (featurep 'multi-tty) ", multi-tty" ""))
3559 erc-emacs-build-time))
3560 t)
3561
3562 (defun erc-cmd-SM ()
3563 "Say the current ERC modes into channel."
3564 (erc-send-message (format "I'm using the following modules: %s!"
3565 (erc-modes)))
3566 t)
3567
3568 (defun erc-cmd-DEOP (&rest people)
3569 "Remove the operator setting from user(s) given in PEOPLE."
3570 (when (> (length people) 0)
3571 (erc-server-send (concat "MODE " (erc-default-target)
3572 " -"
3573 (make-string (length people) ?o)
3574 " "
3575 (mapconcat 'identity people " ")))
3576 t))
3577
3578 (defun erc-cmd-OP (&rest people)
3579 "Add the operator setting to users(s) given in PEOPLE."
3580 (when (> (length people) 0)
3581 (erc-server-send (concat "MODE " (erc-default-target)
3582 " +"
3583 (make-string (length people) ?o)
3584 " "
3585 (mapconcat 'identity people " ")))
3586 t))
3587
3588 (defun erc-cmd-TIME (&optional line)
3589 "Request the current time and date from the current server."
3590 (cond
3591 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3592 (let ((args (match-string 1 line)))
3593 (erc-log (format "cmd: TIME: %s" args))
3594 (erc-server-send (concat "TIME " args)))
3595 t)
3596 (t (erc-server-send "TIME"))))
3597 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3598
3599 (defun erc-cmd-TOPIC (topic)
3600 "Set or request the topic for a channel.
3601 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3602 or the empty string.
3603
3604 If no #CHANNEL is given, the default channel is used. If TOPIC is
3605 given, the channel topic is modified, otherwise the current topic will
3606 be displayed."
3607 (cond
3608 ;; /topic #channel TOPIC
3609 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3610 (let ((ch (match-string 1 topic))
3611 (topic (match-string 2 topic)))
3612 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3613 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3614 t)
3615 ;; /topic #channel
3616 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3617 (let ((ch (match-string 1 topic)))
3618 (erc-server-send (format "TOPIC %s" ch) nil ch)
3619 t))
3620 ;; /topic
3621 ((string-match "^\\s-*$" topic)
3622 (let ((ch (erc-default-target)))
3623 (erc-server-send (format "TOPIC %s" ch) nil ch)
3624 t))
3625 ;; /topic TOPIC
3626 ((string-match "^\\s-*\\(.*\\)$" topic)
3627 (let ((ch (erc-default-target))
3628 (topic (match-string 1 topic)))
3629 (if (and ch (erc-channel-p ch))
3630 (progn
3631 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3632 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3633 (erc-display-message nil 'error (current-buffer) 'no-target)))
3634 t)
3635 (t nil)))
3636 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3637 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3638
3639 (defun erc-cmd-APPENDTOPIC (topic)
3640 "Append TOPIC to the current channel topic, separated by a space."
3641 (let ((oldtopic erc-channel-topic))
3642 ;; display help when given no arguments
3643 (when (string-match "^\\s-*$" topic)
3644 (signal 'wrong-number-of-arguments nil))
3645 ;; strip trailing ^O
3646 (when (string-match "\\(.*\\)\C-o" oldtopic)
3647 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3648 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3649 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3650
3651 (defun erc-cmd-CLEARTOPIC (&optional channel)
3652 "Clear the topic for a CHANNEL.
3653 If CHANNEL is not specified, clear the topic for the default channel."
3654 (interactive "sClear topic of channel (RET is current channel): ")
3655 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3656 (when chnl
3657 (erc-server-send (format "TOPIC %s :" chnl))
3658 t)))
3659
3660 ;;; Banlists
3661
3662 (defvar erc-channel-banlist nil
3663 "A list of bans seen for the current channel.
3664
3665 Each ban is an alist of the form:
3666 (WHOSET . MASK)
3667
3668 The property `received-from-server' indicates whether
3669 or not the ban list has been requested from the server.")
3670 (make-variable-buffer-local 'erc-channel-banlist)
3671 (put 'erc-channel-banlist 'received-from-server nil)
3672
3673 (defun erc-cmd-BANLIST ()
3674 "Pretty-print the contents of `erc-channel-banlist'.
3675
3676 The ban list is fetched from the server if necessary."
3677 (let ((chnl (erc-default-target))
3678 (chnl-name (buffer-name)))
3679
3680 (cond
3681 ((not (erc-channel-p chnl))
3682 (erc-display-line (erc-make-notice "You're not on a channel\n")
3683 'active))
3684
3685 ((not (get 'erc-channel-banlist 'received-from-server))
3686 (let ((old-367-hook erc-server-367-functions))
3687 (setq erc-server-367-functions 'erc-banlist-store
3688 erc-channel-banlist nil)
3689 ;; fetch the ban list then callback
3690 (erc-with-server-buffer
3691 (erc-once-with-server-event
3692 368
3693 (lambda (_proc _parsed)
3694 (with-current-buffer chnl-name
3695 (put 'erc-channel-banlist 'received-from-server t)
3696 (setq erc-server-367-functions old-367-hook)
3697 (erc-cmd-BANLIST)
3698 t)))
3699 (erc-server-send (format "MODE %s b" chnl)))))
3700
3701 ((null erc-channel-banlist)
3702 (erc-display-line (erc-make-notice
3703 (format "No bans for channel: %s\n" chnl))
3704 'active)
3705 (put 'erc-channel-banlist 'received-from-server nil))
3706
3707 (t
3708 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3709 erc-fill-column)
3710 (and (boundp 'fill-column)
3711 fill-column)
3712 (1- (window-width))))
3713 (separator (make-string erc-fill-column ?=))
3714 (fmt (concat
3715 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3716 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3717
3718 (erc-display-line
3719 (erc-make-notice (format "Ban list for channel: %s\n"
3720 (erc-default-target)))
3721 'active)
3722
3723 (erc-display-line separator 'active)
3724 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3725 (erc-display-line separator 'active)
3726
3727 (mapc
3728 (lambda (x)
3729 (erc-display-line
3730 (format fmt
3731 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3732 (if (car x)
3733 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3734 ""))
3735 'active))
3736 erc-channel-banlist)
3737
3738 (erc-display-line (erc-make-notice "End of Ban list")
3739 'active)
3740 (put 'erc-channel-banlist 'received-from-server nil)))))
3741 t)
3742
3743 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3744
3745 (defun erc-cmd-MASSUNBAN ()
3746 "Mass Unban.
3747
3748 Unban all currently banned users in the current channel."
3749 (let ((chnl (erc-default-target)))
3750 (cond
3751
3752 ((not (erc-channel-p chnl))
3753 (erc-display-line
3754 (erc-make-notice "You're not on a channel\n")
3755 'active))
3756
3757 ((not (get 'erc-channel-banlist 'received-from-server))
3758 (let ((old-367-hook erc-server-367-functions))
3759 (setq erc-server-367-functions 'erc-banlist-store)
3760 ;; fetch the ban list then callback
3761 (erc-with-server-buffer
3762 (erc-once-with-server-event
3763 368
3764 (lambda (_proc _parsed)
3765 (with-current-buffer chnl
3766 (put 'erc-channel-banlist 'received-from-server t)
3767 (setq erc-server-367-functions old-367-hook)
3768 (erc-cmd-MASSUNBAN)
3769 t)))
3770 (erc-server-send (format "MODE %s b" chnl)))))
3771
3772 (t (let ((bans (mapcar 'cdr erc-channel-banlist)))
3773 (when bans
3774 ;; Glob the bans into groups of three, and carry out the unban.
3775 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3776 (mapc
3777 (lambda (x)
3778 (erc-server-send
3779 (format "MODE %s -%s %s" (erc-default-target)
3780 (make-string (length x) ?b)
3781 (mapconcat 'identity x " "))))
3782 (erc-group-list bans 3))))
3783 t))))
3784
3785 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3786
3787 ;;;; End of IRC commands
3788
3789 (defun erc-ensure-channel-name (channel)
3790 "Return CHANNEL if it is a valid channel name.
3791 Eventually add a # in front of it, if that turns it into a valid channel name."
3792 (if (erc-channel-p channel)
3793 channel
3794 (concat "#" channel)))
3795
3796 (defun erc-grab-region (start end)
3797 "Copy the region between START and END in a recreatable format.
3798
3799 Converts all the IRC text properties in each line of the region
3800 into control codes and writes them to a separate buffer. The
3801 resulting text may be used directly as a script to generate this
3802 text again."
3803 (interactive "r")
3804 (erc-set-active-buffer (current-buffer))
3805 (save-excursion
3806 (let* ((cb (current-buffer))
3807 (buf (generate-new-buffer erc-grab-buffer-name))
3808 (region (buffer-substring start end))
3809 (lines (erc-split-multiline-safe region)))
3810 (set-buffer buf)
3811 (dolist (line lines)
3812 (insert (concat line "\n")))
3813 (set-buffer cb)
3814 (switch-to-buffer-other-window buf)))
3815 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3816 (ding))
3817
3818 (defun erc-display-prompt (&optional buffer pos prompt face)
3819 "Display PROMPT in BUFFER at position POS.
3820 Display an ERC prompt in BUFFER.
3821
3822 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3823 If BUFFER is nil, the `current-buffer' is used.
3824 If POS is nil, PROMPT will be displayed at `point'.
3825 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3826 `erc-prompt-face' will be used."
3827 (let* ((prompt (or prompt (erc-prompt)))
3828 (l (length prompt))
3829 (ob (current-buffer)))
3830 ;; We cannot use save-excursion because we move point, therefore
3831 ;; we resort to the ol' ob trick to restore this.
3832 (when (and buffer (bufferp buffer))
3833 (set-buffer buffer))
3834
3835 ;; now save excursion again to store where point and mark are
3836 ;; in the current buffer
3837 (save-excursion
3838 (setq pos (or pos (point)))
3839 (goto-char pos)
3840 (when (> l 0)
3841 ;; Do not extend the text properties when typing at the end
3842 ;; of the prompt, but stuff typed in front of the prompt
3843 ;; shall remain part of the prompt.
3844 (setq prompt (erc-propertize prompt
3845 'start-open t ; XEmacs
3846 'rear-nonsticky t ; Emacs
3847 'erc-prompt t
3848 'field t
3849 'front-sticky t
3850 'read-only t))
3851 (erc-put-text-property 0 (1- (length prompt))
3852 'face (or face 'erc-prompt-face)
3853 prompt)
3854 (insert prompt))
3855 ;; Set the input marker
3856 (set-marker erc-input-marker (point)))
3857
3858 ;; Now we are back at the old position. If the prompt was
3859 ;; inserted here or before us, advance point by the length of
3860 ;; the prompt.
3861 (when (or (not pos) (<= (point) pos))
3862 (forward-char l))
3863 ;; Clear the undo buffer now, so the user can undo his stuff,
3864 ;; but not the stuff we did. Sneaky!
3865 (setq buffer-undo-list nil)
3866 (set-buffer ob)))
3867
3868 ;; interactive operations
3869
3870 (defun erc-input-message ()
3871 "Read input from the minibuffer."
3872 (interactive)
3873 (let ((minibuffer-allow-text-properties t)
3874 (read-map minibuffer-local-map))
3875 (insert (read-from-minibuffer "Message: "
3876 (string (if (featurep 'xemacs)
3877 last-command-char
3878 last-command-event))
3879 read-map))
3880 (erc-send-current-line)))
3881
3882 (defvar erc-action-history-list ()
3883 "History list for interactive action input.")
3884
3885 (defun erc-input-action ()
3886 "Interactively input a user action and send it to IRC."
3887 (interactive "")
3888 (erc-set-active-buffer (current-buffer))
3889 (let ((action (read-from-minibuffer
3890 "Action: " nil nil nil 'erc-action-history-list)))
3891 (if (not (string-match "^\\s-*$" action))
3892 (erc-send-action (erc-default-target) action))))
3893
3894 (defun erc-join-channel (channel &optional key)
3895 "Join CHANNEL.
3896
3897 If `point' is at the beginning of a channel name, use that as default."
3898 (interactive
3899 (list
3900 (let ((chnl (if (looking-at "\\([&#+!][^ \n]+\\)") (match-string 1) ""))
3901 (table (when (erc-server-buffer-live-p)
3902 (set-buffer (process-buffer erc-server-process))
3903 erc-channel-list)))
3904 (completing-read "Join channel: " table nil nil nil nil chnl))
3905 (when (or current-prefix-arg erc-prompt-for-channel-key)
3906 (read-from-minibuffer "Channel key (RET for none): " nil))))
3907 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3908
3909 (defun erc-part-from-channel (reason)
3910 "Part from the current channel and prompt for a REASON."
3911 (interactive
3912 (list
3913 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3914 reason
3915 (read-from-minibuffer (concat "Reason for leaving " (erc-default-target)
3916 ": ")))))
3917 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3918
3919 (defun erc-set-topic (topic)
3920 "Prompt for a TOPIC for the current channel."
3921 (interactive
3922 (list
3923 (read-from-minibuffer
3924 (concat "Set topic of " (erc-default-target) ": ")
3925 (when erc-channel-topic
3926 (let ((ss (split-string erc-channel-topic "\C-o")))
3927 (cons (apply 'concat (if (cdr ss) (butlast ss) ss))
3928 0))))))
3929 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3930 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3931
3932 (defun erc-set-channel-limit (&optional limit)
3933 "Set a LIMIT for the current channel. Remove limit if nil.
3934 Prompt for one if called interactively."
3935 (interactive (list (read-from-minibuffer
3936 (format "Limit for %s (RET to remove limit): "
3937 (erc-default-target)))))
3938 (let ((tgt (erc-default-target)))
3939 (erc-server-send (if (and limit (>= (length limit) 1))
3940 (format "MODE %s +l %s" tgt limit)
3941 (format "MODE %s -l" tgt)))))
3942
3943 (defun erc-set-channel-key (&optional key)
3944 "Set a KEY for the current channel. Remove key if nil.
3945 Prompt for one if called interactively."
3946 (interactive (list (read-from-minibuffer
3947 (format "Key for %s (RET to remove key): "
3948 (erc-default-target)))))
3949 (let ((tgt (erc-default-target)))
3950 (erc-server-send (if (and key (>= (length key) 1))
3951 (format "MODE %s +k %s" tgt key)
3952 (format "MODE %s -k" tgt)))))
3953
3954 (defun erc-quit-server (reason)
3955 "Disconnect from current server after prompting for REASON.
3956 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
3957 (interactive (list (read-from-minibuffer
3958 (format "Reason for quitting %s: "
3959 (or erc-server-announced-name
3960 erc-session-server)))))
3961 (erc-cmd-QUIT reason))
3962
3963 ;; Movement of point
3964
3965 (defun erc-bol ()
3966 "Move `point' to the beginning of the current line.
3967
3968 This places `point' just after the prompt, or at the beginning of the line."
3969 (interactive)
3970 (forward-line 0)
3971 (when (get-text-property (point) 'erc-prompt)
3972 (goto-char erc-input-marker))
3973 (point))
3974
3975 (defun erc-kill-input ()
3976 "Kill current input line using `erc-bol' followed by `kill-line'."
3977 (interactive)
3978 (when (and (erc-bol)
3979 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
3980 ;; there's nothing to kill
3981 (if (boundp 'erc-input-ring-index)
3982 (setq erc-input-ring-index nil))
3983 (kill-line)))
3984
3985 (defun erc-complete-word-at-point ()
3986 (run-hook-with-args-until-success 'erc-complete-functions))
3987
3988 (define-obsolete-function-alias 'erc-complete-word 'completion-at-point "24.1")
3989
3990 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3991 ;;
3992 ;; IRC SERVER INPUT HANDLING
3993 ;;
3994 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3995
3996 ;;;; New Input parsing
3997
3998 ; Stolen from ZenIRC. I just wanna test this code, so here is
3999 ; experiment area.
4000
4001 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
4002 erc-default-server-handler)
4003 "Default for server messages which aren't covered by `erc-server-hooks'."
4004 :group 'erc-server-hooks
4005 :type 'hook)
4006
4007 (defun erc-default-server-handler (proc parsed)
4008 "Default server handler.
4009
4010 Displays PROC and PARSED appropriately using `erc-display-message'."
4011 (erc-display-message
4012 parsed 'notice proc
4013 (mapconcat
4014 'identity
4015 (let (res)
4016 (mapc #'(lambda (x)
4017 (if (stringp x)
4018 (setq res (append res (list x)))))
4019 parsed)
4020 res)
4021 " ")))
4022
4023 (defvar erc-server-vectors
4024 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
4025 "List of received server messages which ERC does not specifically handle.
4026 See `erc-debug-missing-hooks'.")
4027 ;(make-variable-buffer-local 'erc-server-vectors)
4028
4029 (defun erc-debug-missing-hooks (_proc parsed)
4030 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
4031 These vectors can be helpful when adding new server message handlers to ERC.
4032 See `erc-default-server-hook'."
4033 (nconc erc-server-vectors (list parsed))
4034 nil)
4035
4036 (defun erc-query (target server)
4037 "Open a query buffer on TARGET, using SERVER.
4038 To change how this query window is displayed, use `let' to bind
4039 `erc-join-buffer' before calling this."
4040 (unless (and server
4041 (buffer-live-p server)
4042 (set-buffer server))
4043 (error "Couldn't switch to server buffer"))
4044 (let ((buf (erc-open erc-session-server
4045 erc-session-port
4046 (erc-current-nick)
4047 erc-session-user-full-name
4048 nil
4049 nil
4050 (list target)
4051 target
4052 erc-server-process)))
4053 (unless buf
4054 (error "Couldn't open query window"))
4055 (erc-update-mode-line)
4056 buf))
4057
4058 (defcustom erc-auto-query 'window-noselect
4059 "If non-nil, create a query buffer each time you receive a private message.
4060 If the buffer doesn't already exist, it is created.
4061
4062 This can be set to a symbol, to control how the new query window
4063 should appear. The default behavior is to display the buffer in
4064 a new window, but not to select it. See the documentation for
4065 `erc-join-buffer' for a description of the available choices."
4066 :group 'erc-query
4067 :type '(choice (const :tag "Don't create query window" nil)
4068 (const :tag "Split window and select" window)
4069 (const :tag "Split window, don't select" window-noselect)
4070 (const :tag "New frame" frame)
4071 (const :tag "Bury in new buffer" bury)
4072 (const :tag "Use current buffer" buffer)
4073 (const :tag "Use current buffer" t)))
4074
4075 (defcustom erc-query-on-unjoined-chan-privmsg t
4076 "If non-nil create query buffer on receiving any PRIVMSG at all.
4077 This includes PRIVMSGs directed to channels. If you are using an IRC
4078 bouncer, such as dircproxy, to keep a log of channels when you are
4079 disconnected, you should set this option to t."
4080 :group 'erc-query
4081 :type 'boolean)
4082
4083 (defcustom erc-format-query-as-channel-p t
4084 "If non-nil, format text from others in a query buffer like in a channel,
4085 otherwise format like a private message."
4086 :group 'erc-query
4087 :type 'boolean)
4088
4089 (defcustom erc-minibuffer-notice nil
4090 "If non-nil, print ERC notices for the user in the minibuffer.
4091 Only happens when the session buffer isn't visible."
4092 :group 'erc-display
4093 :type 'boolean)
4094
4095 (defcustom erc-minibuffer-ignored nil
4096 "If non-nil, print a message in the minibuffer if we ignored something."
4097 :group 'erc-ignore
4098 :type 'boolean)
4099
4100 (defun erc-wash-quit-reason (reason nick login host)
4101 "Remove duplicate text from quit REASON.
4102 Specifically in relation to NICK (user@host) information. Returns REASON
4103 unmodified if nothing can be removed.
4104 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
4105 \"Read error: 110\". The same applies for \"Ping Timeout\"."
4106 (setq nick (regexp-quote nick)
4107 login (regexp-quote login)
4108 host (regexp-quote host))
4109 (or (when (string-match (concat "^\\(Read error\\) to "
4110 nick "\\[" host "\\]: "
4111 "\\(.+\\)$")
4112 reason)
4113 (concat (match-string 1 reason) ": " (match-string 2 reason)))
4114 (when (string-match (concat "^\\(Ping timeout\\) for "
4115 nick "\\[" host "\\]$")
4116 reason)
4117 (match-string 1 reason))
4118 reason))
4119
4120 (defun erc-nickname-in-use (nick reason)
4121 "If NICK is unavailable, tell the user the REASON.
4122
4123 See also `erc-display-error-notice'."
4124 (if (or (not erc-try-new-nick-p)
4125 ;; how many default-nicks are left + one more try...
4126 (eq erc-nick-change-attempt-count
4127 (if (consp erc-nick)
4128 (+ (length erc-nick) 1)
4129 1)))
4130 (erc-display-error-notice
4131 nil
4132 (format "Nickname %s is %s, try another." nick reason))
4133 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
4134 (let ((newnick (nth 1 erc-default-nicks))
4135 (nicklen (cdr (assoc "NICKLEN"
4136 (erc-with-server-buffer
4137 erc-server-parameters)))))
4138 (setq erc-bad-nick t)
4139 ;; try to use a different nick
4140 (if erc-default-nicks
4141 (setq erc-default-nicks (cdr erc-default-nicks)))
4142 (if (not newnick)
4143 (setq newnick (concat (truncate-string-to-width
4144 nick
4145 (if (and erc-server-connected nicklen)
4146 (- (string-to-number nicklen)
4147 (length erc-nick-uniquifier))
4148 ;; rfc2812 max nick length = 9
4149 ;; we must assume this is the
4150 ;; server's setting if we haven't
4151 ;; established a connection yet
4152 (- 9 (length erc-nick-uniquifier))))
4153 erc-nick-uniquifier)))
4154 (erc-cmd-NICK newnick)
4155 (erc-display-error-notice
4156 nil
4157 (format "Nickname %s is %s, trying %s"
4158 nick reason newnick)))))
4159
4160 ;;; Server messages
4161
4162 (defgroup erc-server-hooks nil
4163 "Server event callbacks.
4164 Every server event - like numeric replies - has its own hook.
4165 Those hooks are all called using `run-hook-with-args-until-success'.
4166 They receive as first argument the process object from where the event
4167 originated from,
4168 and as second argument the event parsed as a vector."
4169 :group 'erc-hooks)
4170
4171 (defun erc-display-server-message (_proc parsed)
4172 "Display the message sent by the server as a notice."
4173 (erc-display-message
4174 parsed 'notice 'active (erc-response.contents parsed)))
4175
4176 (defun erc-auto-query (proc parsed)
4177 ;; FIXME: This needs more documentation, unless it's not a user function --
4178 ;; Lawrence 2004-01-08
4179 "Put this on `erc-server-PRIVMSG-functions'."
4180 (when erc-auto-query
4181 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
4182 (target (car (erc-response.command-args parsed)))
4183 (msg (erc-response.contents parsed))
4184 (query (if (not erc-query-on-unjoined-chan-privmsg)
4185 nick
4186 (if (erc-current-nick-p target)
4187 nick
4188 target))))
4189 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
4190 (or erc-query-on-unjoined-chan-privmsg
4191 (string= target (erc-current-nick)))
4192 (not (erc-get-buffer query proc))
4193 (not (erc-is-message-ctcp-and-not-action-p msg))
4194 (let ((erc-query-display erc-auto-query))
4195 (erc-cmd-QUERY query))
4196 nil))))
4197
4198 (defun erc-is-message-ctcp-p (message)
4199 "Check if MESSAGE is a CTCP message or not."
4200 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
4201
4202 (defun erc-is-message-ctcp-and-not-action-p (message)
4203 "Check if MESSAGE is a CTCP message or not."
4204 (and (erc-is-message-ctcp-p message)
4205 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
4206
4207 (defun erc-format-privmessage (nick msg privp msgp)
4208 "Format a PRIVMSG in an insertable fashion."
4209 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
4210 (mark-e (if msgp (if privp "*" ">") "-"))
4211 (str (format "%s%s%s %s" mark-s nick mark-e msg))
4212 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
4213 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
4214 ;; add text properties to text before the nick, the nick and after the nick
4215 (erc-put-text-property 0 (length mark-s) 'face msg-face str)
4216 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
4217 'face nick-face str)
4218 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
4219 'face msg-face str)
4220 str))
4221
4222 (defcustom erc-format-nick-function 'erc-format-nick
4223 "Function to format a nickname for message display."
4224 :group 'erc-display
4225 :type 'function)
4226
4227 (defun erc-format-nick (&optional user _channel-data)
4228 "Return the nickname of USER.
4229 See also `erc-format-nick-function'."
4230 (let ((nick (erc-server-user-nickname user)))
4231 (concat (erc-propertize
4232 (erc-get-user-mode-prefix nick)
4233 'face 'erc-nick-prefix-face)
4234 nick)))
4235
4236 (defun erc-get-user-mode-prefix (user)
4237 (when user
4238 (cond ((erc-channel-user-owner-p user)
4239 (erc-propertize "~" 'help-echo "owner"))
4240 ((erc-channel-user-admin-p user)
4241 (erc-propertize "&" 'help-echo "admin"))
4242 ((erc-channel-user-op-p user)
4243 (erc-propertize "@" 'help-echo "operator"))
4244 ((erc-channel-user-halfop-p user)
4245 (erc-propertize "%" 'help-echo "half-op"))
4246 ((erc-channel-user-voice-p user)
4247 (erc-propertize "+" 'help-echo "voice"))
4248 (t ""))))
4249
4250 (defun erc-format-@nick (&optional user channel-data)
4251 "Format the nickname of USER showing if USER has a voice, is an
4252 operator, half-op, admin or owner. Owners have \"~\", admins have
4253 \"&\", operators have \"@\" and users with voice have \"+\" as a
4254 prefix. Use CHANNEL-DATA to determine op and voice status. See
4255 also `erc-format-nick-function'."
4256 (when user
4257 (let ((nick (erc-server-user-nickname user)))
4258 (concat (erc-propertize
4259 (erc-get-user-mode-prefix nick)
4260 'face 'erc-nick-prefix-face)
4261 nick))))
4262
4263 (defun erc-format-my-nick ()
4264 "Return the beginning of this user's message, correctly propertized."
4265 (if erc-show-my-nick
4266 (let* ((open "<")
4267 (close "> ")
4268 (nick (erc-current-nick))
4269 (mode (erc-get-user-mode-prefix nick)))
4270 (concat
4271 (erc-propertize open 'face 'erc-default-face)
4272 (erc-propertize mode 'face 'erc-my-nick-prefix-face)
4273 (erc-propertize nick 'face 'erc-my-nick-face)
4274 (erc-propertize close 'face 'erc-default-face)))
4275 (let ((prefix "> "))
4276 (erc-propertize prefix 'face 'erc-default-face))))
4277
4278 (defun erc-echo-notice-in-default-buffer (s parsed buffer _sender)
4279 "Echos a private notice in the default buffer, namely the
4280 target buffer specified by BUFFER, or there is no target buffer,
4281 the server buffer. This function is designed to be added to
4282 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
4283 and always returns t."
4284 (erc-display-message parsed nil buffer s)
4285 t)
4286
4287 (defun erc-echo-notice-in-target-buffer (s parsed buffer _sender)
4288 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
4289 function is designed to be added to either `erc-echo-notice-hook'
4290 or `erc-echo-notice-always-hook', and returns non-nil if BUFFER
4291 is non-nil."
4292 (if buffer
4293 (progn (erc-display-message parsed nil buffer s) t)
4294 nil))
4295
4296 (defun erc-echo-notice-in-minibuffer (s _parsed _buffer _sender)
4297 "Echos a private notice in the minibuffer. This function is
4298 designed to be added to either `erc-echo-notice-hook' or
4299 `erc-echo-notice-always-hook', and always returns t."
4300 (message "%s" (concat "NOTICE: " s))
4301 t)
4302
4303 (defun erc-echo-notice-in-server-buffer (s parsed _buffer _sender)
4304 "Echos a private notice in the server buffer. This function is
4305 designed to be added to either `erc-echo-notice-hook' or
4306 `erc-echo-notice-always-hook', and always returns t."
4307 (erc-display-message parsed nil nil s)
4308 t)
4309
4310 (defun erc-echo-notice-in-active-non-server-buffer (s parsed _buffer _sender)
4311 "Echos a private notice in the active buffer if the active
4312 buffer is not the server buffer. This function is designed to be
4313 added to either `erc-echo-notice-hook' or
4314 `erc-echo-notice-always-hook', and returns non-nil if the active
4315 buffer is not the server buffer."
4316 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
4317 (progn (erc-display-message parsed nil 'active s) t)
4318 nil))
4319
4320 (defun erc-echo-notice-in-active-buffer (s parsed _buffer _sender)
4321 "Echos a private notice in the active buffer. This function is
4322 designed to be added to either `erc-echo-notice-hook' or
4323 `erc-echo-notice-always-hook', and always returns t."
4324 (erc-display-message parsed nil 'active s)
4325 t)
4326
4327 (defun erc-echo-notice-in-user-buffers (s parsed _buffer sender)
4328 "Echos a private notice in all of the buffers for which SENDER
4329 is a member. This function is designed to be added to either
4330 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4331 returns non-nil if there is at least one buffer for which the
4332 sender is a member.
4333
4334 See also: `erc-echo-notice-in-first-user-buffer',
4335 `erc-buffer-list-with-nick'."
4336 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4337 (if buffers
4338 (progn (erc-display-message parsed nil buffers s) t)
4339 nil)))
4340
4341 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
4342 "Echos a private notice in BUFFER and in all of the buffers for
4343 which SENDER is a member. This function is designed to be added
4344 to either `erc-echo-notice-hook' or
4345 `erc-echo-notice-always-hook', and returns non-nil if there is
4346 at least one buffer for which the sender is a member or the
4347 default target.
4348
4349 See also: `erc-echo-notice-in-user-buffers',
4350 `erc-buffer-list-with-nick'."
4351 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4352 (unless (memq buffer buffers) (push buffer buffers))
4353 (if buffers ;FIXME: How could it be nil?
4354 (progn (erc-display-message parsed nil buffers s) t)
4355 nil)))
4356
4357 (defun erc-echo-notice-in-first-user-buffer (s parsed _buffer sender)
4358 "Echos a private notice in one of the buffers for which SENDER
4359 is a member. This function is designed to be added to either
4360 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4361 returns non-nil if there is at least one buffer for which the
4362 sender is a member.
4363
4364 See also: `erc-echo-notice-in-user-buffers',
4365 `erc-buffer-list-with-nick'."
4366 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4367 (if buffers
4368 (progn (erc-display-message parsed nil (car buffers) s) t)
4369 nil)))
4370
4371 ;;; Ban manipulation
4372
4373 (defun erc-banlist-store (proc parsed)
4374 "Record ban entries for a channel."
4375 (pcase-let ((`(,channel ,mask ,whoset)
4376 (cdr (erc-response.command-args parsed))))
4377 ;; Determine to which buffer the message corresponds
4378 (let ((buffer (erc-get-buffer channel proc)))
4379 (with-current-buffer buffer
4380 (unless (member (cons whoset mask) erc-channel-banlist)
4381 (setq erc-channel-banlist (cons (cons whoset mask)
4382 erc-channel-banlist))))))
4383 nil)
4384
4385 (defun erc-banlist-finished (proc parsed)
4386 "Record that we have received the banlist."
4387 (let* ((channel (nth 1 (erc-response.command-args parsed)))
4388 (buffer (erc-get-buffer channel proc)))
4389 (with-current-buffer buffer
4390 (put 'erc-channel-banlist 'received-from-server t)))
4391 t) ; suppress the 'end of banlist' message
4392
4393 (defun erc-banlist-update (proc parsed)
4394 "Check MODE commands for bans and update the banlist appropriately."
4395 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
4396 (let* ((tgt (car (erc-response.command-args parsed)))
4397 (mode (erc-response.contents parsed))
4398 (whoset (erc-response.sender parsed))
4399 (buffer (erc-get-buffer tgt proc)))
4400 (when buffer
4401 (with-current-buffer buffer
4402 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
4403 ((string-match "^\\([+-]\\)b" mode)
4404 ;; This is a ban
4405 (cond
4406 ((string-match "^-" mode)
4407 ;; Remove the unbanned masks from the ban list
4408 (setq erc-channel-banlist
4409 (erc-delete-if
4410 #'(lambda (y)
4411 (member (upcase (cdr y))
4412 (mapcar #'upcase
4413 (cdr (split-string mode)))))
4414 erc-channel-banlist)))
4415 ((string-match "^+" mode)
4416 ;; Add the banned mask(s) to the ban list
4417 (mapc
4418 (lambda (mask)
4419 (unless (member (cons whoset mask) erc-channel-banlist)
4420 (setq erc-channel-banlist
4421 (cons (cons whoset mask) erc-channel-banlist))))
4422 (cdr (split-string mode))))))))))
4423 nil)
4424
4425 ;; used for the banlist cmds
4426 (defun erc-group-list (list n)
4427 "Group LIST into sublists of length N."
4428 (cond ((null list) nil)
4429 ((null (nthcdr n list)) (list list))
4430 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
4431
4432
4433 ;;; MOTD numreplies
4434
4435 (defun erc-handle-login ()
4436 "Handle the logging in process of connection."
4437 (unless erc-logged-in
4438 (setq erc-logged-in t)
4439 (message "Logging in as \'%s\'... done" (erc-current-nick))
4440 ;; execute a startup script
4441 (let ((f (erc-select-startup-file)))
4442 (when f
4443 (erc-load-script f)))))
4444
4445 (defun erc-connection-established (proc parsed)
4446 "Run just after connection.
4447
4448 Set user modes and run `erc-after-connect' hook."
4449 (with-current-buffer (process-buffer proc)
4450 (unless erc-server-connected ; only once per session
4451 (let ((server (or erc-server-announced-name
4452 (erc-response.sender parsed)))
4453 (nick (car (erc-response.command-args parsed)))
4454 (buffer (process-buffer proc)))
4455 (setq erc-server-connected t)
4456 (erc-update-mode-line)
4457 (erc-set-initial-user-mode nick buffer)
4458 (erc-server-setup-periodical-ping buffer)
4459 (run-hook-with-args 'erc-after-connect server nick)))))
4460
4461 (defun erc-set-initial-user-mode (nick buffer)
4462 "If `erc-user-mode' is non-nil for NICK, set the user modes.
4463 The server buffer is given by BUFFER."
4464 (with-current-buffer buffer
4465 (when erc-user-mode
4466 (let ((mode (if (functionp erc-user-mode)
4467 (funcall erc-user-mode)
4468 erc-user-mode)))
4469 (when (stringp mode)
4470 (erc-log (format "changing mode for %s to %s" nick mode))
4471 (erc-server-send (format "MODE %s %s" nick mode)))))))
4472
4473 (defun erc-display-error-notice (parsed string)
4474 "Display STRING as an error notice.
4475
4476 See also `erc-display-message'."
4477 (erc-display-message
4478 parsed '(notice error) 'active string))
4479
4480 (defun erc-process-ctcp-query (proc parsed nick login host)
4481 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4482 "Process a CTCP query."
4483 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4484 "\C-a"))))
4485 (if (> (length queries) 4)
4486 (erc-display-message
4487 parsed (list 'notice 'error) proc 'ctcp-too-many)
4488 (if (= 0 (length queries))
4489 (erc-display-message
4490 parsed (list 'notice 'error) proc
4491 'ctcp-empty ?n nick)
4492 (while queries
4493 (let* ((type (upcase (car (split-string (car queries)))))
4494 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4495 (if (and hook (boundp hook))
4496 (if (string-equal type "ACTION")
4497 (run-hook-with-args-until-success
4498 hook proc parsed nick login host
4499 (car (erc-response.command-args parsed))
4500 (car queries))
4501 (when erc-paranoid
4502 (if (erc-current-nick-p
4503 (car (erc-response.command-args parsed)))
4504 (erc-display-message
4505 parsed 'error 'active 'ctcp-request
4506 ?n nick ?u login ?h host ?r (car queries))
4507 (erc-display-message
4508 parsed 'error 'active 'ctcp-request-to
4509 ?n nick ?u login ?h host ?r (car queries)
4510 ?t (car (erc-response.command-args parsed)))))
4511 (run-hook-with-args-until-success
4512 hook proc nick login host
4513 (car (erc-response.command-args parsed))
4514 (car queries)))
4515 (erc-display-message
4516 parsed (list 'notice 'error) proc
4517 'undefined-ctcp)))
4518 (setq queries (cdr queries)))))))
4519
4520 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4521
4522 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4523 "Respond to a CTCP ACTION query."
4524 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4525 (let ((s (match-string 1 msg))
4526 (buf (or (erc-get-buffer to proc)
4527 (erc-get-buffer nick proc)
4528 (process-buffer proc))))
4529 (erc-display-message
4530 parsed 'action buf
4531 'ACTION ?n nick ?u login ?h host ?a s))))
4532
4533 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4534
4535 (defun erc-ctcp-query-CLIENTINFO (_proc nick _login _host _to msg)
4536 "Respond to a CTCP CLIENTINFO query."
4537 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4538 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4539 (unless erc-disable-ctcp-replies
4540 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4541 nil)
4542
4543 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4544 (defun erc-ctcp-query-ECHO (_proc nick _login _host _to msg)
4545 "Respond to a CTCP ECHO query."
4546 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4547 (let ((s (match-string 1 msg)))
4548 (unless erc-disable-ctcp-replies
4549 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4550 nil)
4551
4552 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4553 (defun erc-ctcp-query-FINGER (_proc nick _login _host _to _msg)
4554 "Respond to a CTCP FINGER query."
4555 (unless erc-disable-ctcp-replies
4556 (let ((s (if erc-anonymous-login
4557 (format "FINGER I'm %s." (erc-current-nick))
4558 (format "FINGER %s (%s@%s)."
4559 (user-full-name)
4560 (user-login-name)
4561 (system-name))))
4562 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4563 (when (> ns 0)
4564 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4565 (erc-send-ctcp-notice nick s)))
4566 nil)
4567
4568 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4569 (defun erc-ctcp-query-PING (_proc nick _login _host _to msg)
4570 "Respond to a CTCP PING query."
4571 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4572 (unless erc-disable-ctcp-replies
4573 (let ((arg (match-string 1 msg)))
4574 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4575 nil)
4576
4577 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4578 (defun erc-ctcp-query-TIME (_proc nick _login _host _to _msg)
4579 "Respond to a CTCP TIME query."
4580 (unless erc-disable-ctcp-replies
4581 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4582 nil)
4583
4584 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4585 (defun erc-ctcp-query-USERINFO (_proc nick _login _host _to _msg)
4586 "Respond to a CTCP USERINFO query."
4587 (unless erc-disable-ctcp-replies
4588 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4589 nil)
4590
4591 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4592 (defun erc-ctcp-query-VERSION (_proc nick _login _host _to _msg)
4593 "Respond to a CTCP VERSION query."
4594 (unless erc-disable-ctcp-replies
4595 (erc-send-ctcp-notice
4596 nick (format
4597 "VERSION \C-bERC\C-b %s - an IRC client for emacs (\C-b%s\C-b)"
4598 erc-version-string
4599 erc-official-location)))
4600 nil)
4601
4602 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4603 "Process MSG as a CTCP reply."
4604 (let* ((type (car (split-string msg)))
4605 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4606 (if (boundp hook)
4607 (run-hook-with-args-until-success
4608 hook proc nick login host
4609 (car (erc-response.command-args parsed)) msg)
4610 (erc-display-message
4611 parsed 'notice 'active
4612 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4613
4614 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4615 (defun erc-ctcp-reply-ECHO (_proc nick _login _host _to msg)
4616 "Handle a CTCP ECHO reply."
4617 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4618 (let ((message (match-string 1 msg)))
4619 (erc-display-message
4620 nil '(notice action) 'active
4621 'CTCP-ECHO ?n nick ?m message)))
4622 nil)
4623
4624 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4625 (defun erc-ctcp-reply-CLIENTINFO (_proc nick _login _host _to msg)
4626 "Handle a CTCP CLIENTINFO reply."
4627 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4628 (let ((message (match-string 1 msg)))
4629 (erc-display-message
4630 nil 'notice 'active
4631 'CTCP-CLIENTINFO ?n nick ?m message)))
4632 nil)
4633
4634 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4635 (defun erc-ctcp-reply-FINGER (_proc nick _login _host _to msg)
4636 "Handle a CTCP FINGER reply."
4637 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4638 (let ((message (match-string 1 msg)))
4639 (erc-display-message
4640 nil 'notice 'active
4641 'CTCP-FINGER ?n nick ?m message)))
4642 nil)
4643
4644 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4645 (defun erc-ctcp-reply-PING (_proc nick _login _host _to msg)
4646 "Handle a CTCP PING reply."
4647 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4648 nil
4649 (let ((time (match-string 1 msg)))
4650 (condition-case nil
4651 (let ((delta (erc-time-diff (string-to-number time)
4652 (erc-current-time))))
4653 (erc-display-message
4654 nil 'notice 'active
4655 'CTCP-PING ?n nick
4656 ?t (erc-sec-to-time delta)))
4657 (range-error
4658 (erc-display-message
4659 nil 'error 'active
4660 'bad-ping-response ?n nick ?t time))))))
4661
4662 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4663 (defun erc-ctcp-reply-TIME (_proc nick _login _host _to msg)
4664 "Handle a CTCP TIME reply."
4665 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4666 (let ((message (match-string 1 msg)))
4667 (erc-display-message
4668 nil 'notice 'active
4669 'CTCP-TIME ?n nick ?m message)))
4670 nil)
4671
4672 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4673 (defun erc-ctcp-reply-VERSION (_proc nick _login _host _to msg)
4674 "Handle a CTCP VERSION reply."
4675 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4676 (let ((message (match-string 1 msg)))
4677 (erc-display-message
4678 nil 'notice 'active
4679 'CTCP-VERSION ?n nick ?m message)))
4680 nil)
4681
4682 (defun erc-process-away (proc away-p)
4683 "Toggle the away status of the user depending on the value of AWAY-P.
4684
4685 If nil, set the user as away.
4686 If non-nil, return from being away."
4687 (let ((sessionbuf (process-buffer proc)))
4688 (when sessionbuf
4689 (with-current-buffer sessionbuf
4690 (when erc-away-nickname
4691 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4692 erc-away-nickname away-p))
4693 (erc-cmd-NICK (if away-p
4694 erc-away-nickname
4695 erc-nick)))
4696 (cond
4697 (away-p
4698 (setq erc-away (current-time)))
4699 (t
4700 (let ((away-time erc-away))
4701 ;; away must be set to NIL BEFORE sending anything to prevent
4702 ;; an infinite recursion
4703 (setq erc-away nil)
4704 (with-current-buffer (erc-active-buffer)
4705 (when erc-public-away-p
4706 (erc-send-action
4707 (erc-default-target)
4708 (if away-time
4709 (format "is back (gone for %s)"
4710 (erc-sec-to-time
4711 (erc-time-diff
4712 (erc-emacs-time-to-erc-time away-time)
4713 (erc-current-time))))
4714 "is back")))))))))
4715 (erc-update-mode-line)))
4716
4717 ;;;; List of channel members handling
4718
4719 (defun erc-channel-begin-receiving-names ()
4720 "Internal function.
4721
4722 Used when a channel names list is about to be received. Should
4723 be called with the current buffer set to the channel buffer.
4724
4725 See also `erc-channel-end-receiving-names'."
4726 (setq erc-channel-new-member-names (make-hash-table :test 'equal)))
4727
4728 (defun erc-channel-end-receiving-names ()
4729 "Internal function.
4730
4731 Used to fix `erc-channel-users' after a channel names list has been
4732 received. Should be called with the current buffer set to the
4733 channel buffer.
4734
4735 See also `erc-channel-begin-receiving-names'."
4736 (maphash (lambda (nick _user)
4737 (if (null (gethash nick erc-channel-new-member-names))
4738 (erc-remove-channel-user nick)))
4739 erc-channel-users)
4740 (setq erc-channel-new-member-names nil))
4741
4742 (defun erc-parse-prefix ()
4743 "Return an alist of valid prefix character types and their representations.
4744 Example: (operator) o => @, (voiced) v => +."
4745 (let ((str (or (cdr (assoc "PREFIX" (erc-with-server-buffer
4746 erc-server-parameters)))
4747 ;; provide a sane default
4748 "(qaohv)~&@%+"))
4749 types chars)
4750 (when (string-match "^(\\([^)]+\\))\\(.+\\)$" str)
4751 (setq types (match-string 1 str)
4752 chars (match-string 2 str))
4753 (let ((len (min (length types) (length chars)))
4754 (i 0)
4755 (alist nil))
4756 (while (< i len)
4757 (setq alist (cons (cons (elt types i) (elt chars i))
4758 alist))
4759 (setq i (1+ i)))
4760 alist))))
4761
4762 (defun erc-channel-receive-names (names-string)
4763 "This function is for internal use only.
4764
4765 Update `erc-channel-users' according to NAMES-STRING.
4766 NAMES-STRING is a string listing some of the names on the
4767 channel."
4768 (let* ((prefix (erc-parse-prefix))
4769 (voice-ch (cdr (assq ?v prefix)))
4770 (op-ch (cdr (assq ?o prefix)))
4771 (hop-ch (cdr (assq ?h prefix)))
4772 (adm-ch (cdr (assq ?a prefix)))
4773 (own-ch (cdr (assq ?q prefix)))
4774 names name op voice halfop admin owner)
4775 (setq names (delete "" (split-string names-string)))
4776 (let ((erc-channel-members-changed-hook nil))
4777 (dolist (item names)
4778 (let ((updatep t))
4779 (setq name item op 'off voice 'off halfop 'off admin 'off owner 'off)
4780 (if (rassq (elt item 0) prefix)
4781 (if (= (length item) 1)
4782 (setq updatep nil)
4783 (setq name (substring item 1))
4784 (setf (pcase (aref item 0)
4785 ((pred (eq voice-ch)) voice)
4786 ((pred (eq hop-ch)) halfop)
4787 ((pred (eq op-ch)) op)
4788 ((pred (eq adm-ch)) admin)
4789 ((pred (eq own-ch)) owner))
4790 'on)))
4791 (when updatep
4792 (puthash (erc-downcase name) t
4793 erc-channel-new-member-names)
4794 (erc-update-current-channel-member
4795 name name t voice halfop op admin owner)))))
4796 (run-hooks 'erc-channel-members-changed-hook)))
4797
4798
4799 (defcustom erc-channel-members-changed-hook nil
4800 "This hook is called every time the variable `channel-members' changes.
4801 The buffer where the change happened is current while this hook is called."
4802 :group 'erc-hooks
4803 :type 'hook)
4804
4805 (defun erc-update-user-nick (nick &optional new-nick
4806 host login full-name info)
4807 "Update the stored user information for the user with nickname NICK.
4808
4809 See also: `erc-update-user'."
4810 (erc-update-user (erc-get-server-user nick) new-nick
4811 host login full-name info))
4812
4813 (defun erc-update-user (user &optional new-nick
4814 host login full-name info)
4815 "Update user info for USER. USER must be an erc-server-user
4816 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4817 non-nil and not equal to the existing values for USER are used to
4818 replace the stored values in USER.
4819
4820 If, and only if, a change is made,
4821 `erc-channel-members-changed-hook' is run for each channel for
4822 which USER is a member, and t is returned."
4823 (let (changed)
4824 (when user
4825 (when (and new-nick
4826 (not (equal (erc-server-user-nickname user)
4827 new-nick)))
4828 (setq changed t)
4829 (erc-change-user-nickname user new-nick))
4830 (when (and host
4831 (not (equal (erc-server-user-host user) host)))
4832 (setq changed t)
4833 (setf (erc-server-user-host user) host))
4834 (when (and login
4835 (not (equal (erc-server-user-login user) login)))
4836 (setq changed t)
4837 (setf (erc-server-user-login user) login))
4838 (when (and full-name
4839 (not (equal (erc-server-user-full-name user)
4840 full-name)))
4841 (setq changed t)
4842 (setf (erc-server-user-full-name user) full-name))
4843 (when (and info
4844 (not (equal (erc-server-user-info user) info)))
4845 (setq changed t)
4846 (setf (erc-server-user-info user) info))
4847 (if changed
4848 (dolist (buf (erc-server-user-buffers user))
4849 (if (buffer-live-p buf)
4850 (with-current-buffer buf
4851 (run-hooks 'erc-channel-members-changed-hook))))))
4852 changed))
4853
4854 (defun erc-update-current-channel-member
4855 (nick new-nick &optional add voice halfop op admin owner host login full-name info
4856 update-message-time)
4857 "Update the stored user information for the user with nickname NICK.
4858 `erc-update-user' is called to handle changes to nickname,
4859 HOST, LOGIN, FULL-NAME, and INFO. If VOICE HALFOP OP ADMIN or OWNER
4860 are non-nil, they must be equal to either `on' or `off', in which
4861 case the status of the user in the current channel is changed accordingly.
4862 If UPDATE-MESSAGE-TIME is non-nil, the last-message-time of the user
4863 in the current channel is set to (current-time).
4864
4865 If ADD is non-nil, the user will be added with the specified
4866 information if it is not already present in the user or channel
4867 lists.
4868
4869 If, and only if, changes are made, or the user is added,
4870 `erc-channel-members-updated-hook' is run, and t is returned.
4871
4872 See also: `erc-update-user' and `erc-update-channel-member'."
4873 (let* (changed user-changed
4874 (channel-data (erc-get-channel-user nick))
4875 (cuser (cdr channel-data))
4876 (user (if channel-data (car channel-data)
4877 (erc-get-server-user nick))))
4878 (if cuser
4879 (progn
4880 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4881 (when (and voice
4882 (not (eq (erc-channel-user-voice cuser) voice)))
4883 (setq changed t)
4884 (setf (erc-channel-user-voice cuser)
4885 (cond ((eq voice 'on) t)
4886 ((eq voice 'off) nil)
4887 (t voice))))
4888 (when (and halfop
4889 (not (eq (erc-channel-user-halfop cuser) halfop)))
4890 (setq changed t)
4891 (setf (erc-channel-user-halfop cuser)
4892 (cond ((eq halfop 'on) t)
4893 ((eq halfop 'off) nil)
4894 (t halfop))))
4895 (when (and op
4896 (not (eq (erc-channel-user-op cuser) op)))
4897 (setq changed t)
4898 (setf (erc-channel-user-op cuser)
4899 (cond ((eq op 'on) t)
4900 ((eq op 'off) nil)
4901 (t op))))
4902 (when (and admin
4903 (not (eq (erc-channel-user-admin cuser) admin)))
4904 (setq changed t)
4905 (setf (erc-channel-user-admin cuser)
4906 (cond ((eq admin 'on) t)
4907 ((eq admin 'off) nil)
4908 (t admin))))
4909 (when (and owner
4910 (not (eq (erc-channel-user-owner cuser) owner)))
4911 (setq changed t)
4912 (setf (erc-channel-user-owner cuser)
4913 (cond ((eq owner 'on) t)
4914 ((eq owner 'off) nil)
4915 (t owner))))
4916 (when update-message-time
4917 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4918 (setq user-changed
4919 (erc-update-user user new-nick
4920 host login full-name info)))
4921 (when add
4922 (if (null user)
4923 (progn
4924 (setq user (make-erc-server-user
4925 :nickname nick
4926 :host host
4927 :full-name full-name
4928 :login login
4929 :info info
4930 :buffers (list (current-buffer))))
4931 (erc-add-server-user nick user))
4932 (setf (erc-server-user-buffers user)
4933 (cons (current-buffer)
4934 (erc-server-user-buffers user))))
4935 (setq cuser (make-erc-channel-user
4936 :voice (cond ((eq voice 'on) t)
4937 ((eq voice 'off) nil)
4938 (t voice))
4939 :halfop (cond ((eq halfop 'on) t)
4940 ((eq halfop 'off) nil)
4941 (t halfop))
4942 :op (cond ((eq op 'on) t)
4943 ((eq op 'off) nil)
4944 (t op))
4945 :admin (cond ((eq admin 'on) t)
4946 ((eq admin 'off) nil)
4947 (t admin))
4948 :owner (cond ((eq owner 'on) t)
4949 ((eq owner 'off) nil)
4950 (t owner))
4951 :last-message-time
4952 (if update-message-time (current-time))))
4953 (puthash (erc-downcase nick) (cons user cuser)
4954 erc-channel-users)
4955 (setq changed t)))
4956 (when (and changed (null user-changed))
4957 (run-hooks 'erc-channel-members-changed-hook))
4958 (or changed user-changed add)))
4959
4960 (defun erc-update-channel-member (channel nick new-nick
4961 &optional add voice halfop op admin owner host login
4962 full-name info update-message-time)
4963 "Update user and channel information for the user with
4964 nickname NICK in channel CHANNEL.
4965
4966 See also: `erc-update-current-channel-member'."
4967 (erc-with-buffer
4968 (channel)
4969 (erc-update-current-channel-member nick new-nick add voice halfop op admin owner host
4970 login full-name info
4971 update-message-time)))
4972
4973 (defun erc-remove-current-channel-member (nick)
4974 "Remove NICK from current channel membership list.
4975 Runs `erc-channel-members-changed-hook'."
4976 (let ((channel-data (erc-get-channel-user nick)))
4977 (when channel-data
4978 (erc-remove-channel-user nick)
4979 (run-hooks 'erc-channel-members-changed-hook))))
4980
4981 (defun erc-remove-channel-member (channel nick)
4982 "Remove NICK from CHANNEL's membership list.
4983
4984 See also `erc-remove-current-channel-member'."
4985 (erc-with-buffer
4986 (channel)
4987 (erc-remove-current-channel-member nick)))
4988
4989 (defun erc-update-channel-topic (channel topic &optional modify)
4990 "Find a buffer for CHANNEL and set the TOPIC for it.
4991
4992 If optional MODIFY is 'append or 'prepend, then append or prepend the
4993 TOPIC string to the current topic."
4994 (erc-with-buffer (channel)
4995 (cond ((eq modify 'append)
4996 (setq erc-channel-topic (concat erc-channel-topic topic)))
4997 ((eq modify 'prepend)
4998 (setq erc-channel-topic (concat topic erc-channel-topic)))
4999 (t (setq erc-channel-topic topic)))
5000 (erc-update-mode-line-buffer (current-buffer))))
5001
5002 (defun erc-set-modes (tgt mode-string)
5003 "Set the modes for the TGT provided as MODE-STRING."
5004 (let* ((modes (erc-parse-modes mode-string))
5005 (add-modes (nth 0 modes))
5006 ;; list of triples: (mode-char 'on/'off argument)
5007 (arg-modes (nth 2 modes)))
5008 (cond ((erc-channel-p tgt); channel modes
5009 (let ((buf (and erc-server-process
5010 (erc-get-buffer tgt erc-server-process))))
5011 (when buf
5012 (with-current-buffer buf
5013 (setq erc-channel-modes add-modes)
5014 (setq erc-channel-user-limit nil)
5015 (setq erc-channel-key nil)
5016 (while arg-modes
5017 (let ((mode (nth 0 (car arg-modes)))
5018 (onoff (nth 1 (car arg-modes)))
5019 (arg (nth 2 (car arg-modes))))
5020 (cond ((string-match "^[Ll]" mode)
5021 (erc-update-channel-limit tgt onoff arg))
5022 ((string-match "^[Kk]" mode)
5023 (erc-update-channel-key tgt onoff arg))
5024 (t nil)))
5025 (setq arg-modes (cdr arg-modes)))
5026 (erc-update-mode-line-buffer buf)))))
5027 ;; we do not keep our nick's modes yet
5028 ;;(t (setq erc-user-modes add-modes))
5029 )
5030 ))
5031
5032 (defun erc-sort-strings (list-of-strings)
5033 "Sort LIST-OF-STRINGS in lexicographic order.
5034
5035 Side-effect free."
5036 (sort (copy-sequence list-of-strings) 'string<))
5037
5038 (defun erc-parse-modes (mode-string)
5039 "Parse MODE-STRING into a list.
5040
5041 Returns a list of three elements:
5042
5043 (ADD-MODES REMOVE-MODES ARG-MODES).
5044
5045 The add-modes and remove-modes are lists of single-character strings
5046 for modes without parameters to add and remove respectively. The
5047 arg-modes is a list of triples of the form:
5048
5049 (MODE-CHAR ON/OFF ARGUMENT)."
5050 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
5051 (let ((chars (mapcar 'char-to-string (match-string 1 mode-string)))
5052 ;; arguments in channel modes
5053 (args-str (match-string 2 mode-string))
5054 (args nil)
5055 (add-modes nil)
5056 (remove-modes nil)
5057 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
5058 (add-p t))
5059 ;; make the argument list
5060 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
5061 (setq args (cons (match-string 1 args-str) args))
5062 (setq args-str (match-string 2 args-str)))
5063 (setq args (nreverse args))
5064 ;; collect what modes changed, and match them with arguments
5065 (while chars
5066 (cond ((string= (car chars) "+") (setq add-p t))
5067 ((string= (car chars) "-") (setq add-p nil))
5068 ((string-match "^[qaovhbQAOVHB]" (car chars))
5069 (setq arg-modes (cons (list (car chars)
5070 (if add-p 'on 'off)
5071 (if args (car args) nil))
5072 arg-modes))
5073 (if args (setq args (cdr args))))
5074 ((string-match "^[LlKk]" (car chars))
5075 (setq arg-modes (cons (list (car chars)
5076 (if add-p 'on 'off)
5077 (if (and add-p args)
5078 (car args) nil))
5079 arg-modes))
5080 (if (and add-p args) (setq args (cdr args))))
5081 (add-p (setq add-modes (cons (car chars) add-modes)))
5082 (t (setq remove-modes (cons (car chars) remove-modes))))
5083 (setq chars (cdr chars)))
5084 (setq add-modes (nreverse add-modes))
5085 (setq remove-modes (nreverse remove-modes))
5086 (setq arg-modes (nreverse arg-modes))
5087 (list add-modes remove-modes arg-modes))
5088 nil))
5089
5090 (defun erc-update-modes (tgt mode-string &optional nick host login)
5091 "Update the mode information for TGT, provided as MODE-STRING.
5092 Optional arguments: NICK, HOST and LOGIN - the attributes of the
5093 person who changed the modes."
5094 ;; FIXME: neither of nick, host, and login are used!
5095 (let* ((modes (erc-parse-modes mode-string))
5096 (add-modes (nth 0 modes))
5097 (remove-modes (nth 1 modes))
5098 ;; list of triples: (mode-char 'on/'off argument)
5099 (arg-modes (nth 2 modes)))
5100 ;; now parse the modes changes and do the updates
5101 (cond ((erc-channel-p tgt); channel modes
5102 (let ((buf (and erc-server-process
5103 (erc-get-buffer tgt erc-server-process))))
5104 (when buf
5105 ;; FIXME! This used to have an original buffer
5106 ;; variable, but it never switched back to the original
5107 ;; buffer. Is this wanted behavior?
5108 (set-buffer buf)
5109 (if (not (boundp 'erc-channel-modes))
5110 (setq erc-channel-modes nil))
5111 (while remove-modes
5112 (setq erc-channel-modes (delete (car remove-modes)
5113 erc-channel-modes)
5114 remove-modes (cdr remove-modes)))
5115 (while add-modes
5116 (setq erc-channel-modes (cons (car add-modes)
5117 erc-channel-modes)
5118 add-modes (cdr add-modes)))
5119 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
5120 (while arg-modes
5121 (let ((mode (nth 0 (car arg-modes)))
5122 (onoff (nth 1 (car arg-modes)))
5123 (arg (nth 2 (car arg-modes))))
5124 (cond ((string-match "^[Vv]" mode)
5125 (erc-update-channel-member tgt arg arg nil onoff))
5126 ((string-match "^[hH]" mode)
5127 (erc-update-channel-member tgt arg arg nil nil onoff))
5128 ((string-match "^[oO]" mode)
5129 (erc-update-channel-member tgt arg arg nil nil nil onoff))
5130 ((string-match "^[aA]" mode)
5131 (erc-update-channel-member tgt arg arg nil nil nil nil onoff))
5132 ((string-match "^[qQ]" mode)
5133 (erc-update-channel-member tgt arg arg nil nil nil nil nil onoff))
5134 ((string-match "^[Ll]" mode)
5135 (erc-update-channel-limit tgt onoff arg))
5136 ((string-match "^[Kk]" mode)
5137 (erc-update-channel-key tgt onoff arg))
5138 (t nil)); only ops are tracked now
5139 (setq arg-modes (cdr arg-modes))))
5140 (erc-update-mode-line buf))))
5141 ;; nick modes - ignored at this point
5142 (t nil))))
5143
5144 (defun erc-update-channel-limit (channel onoff n)
5145 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
5146 "Update CHANNEL's user limit to N."
5147 (if (or (not (eq onoff 'on))
5148 (and (stringp n) (string-match "^[0-9]+$" n)))
5149 (erc-with-buffer
5150 (channel)
5151 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
5152 (t (setq erc-channel-user-limit nil))))))
5153
5154 (defun erc-update-channel-key (channel onoff key)
5155 "Update CHANNEL's key to KEY if ONOFF is 'on or to nil if it's 'off."
5156 (erc-with-buffer
5157 (channel)
5158 (cond ((eq onoff 'on) (setq erc-channel-key key))
5159 (t (setq erc-channel-key nil)))))
5160
5161 (defun erc-handle-user-status-change (type nlh &optional l)
5162 "Handle changes in any user's status.
5163
5164 So far, only nick change is handled.
5165
5166 Generally, the TYPE argument is a symbol describing the change type, NLH is
5167 a list containing the original nickname, login name and hostname for the user,
5168 and L is a list containing additional TYPE-specific arguments.
5169
5170 So far the following TYPE/L pairs are supported:
5171
5172 Event TYPE L
5173
5174 nickname change 'nick (NEW-NICK)"
5175 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
5176 (cond
5177 ;; nickname change
5178 ((equal type 'nick)
5179 t)
5180 (t
5181 nil)))
5182
5183 (defun erc-highlight-notice (s)
5184 "Highlight notice message S and return it.
5185 See also variable `erc-notice-highlight-type'."
5186 (cond
5187 ((eq erc-notice-highlight-type 'prefix)
5188 (erc-put-text-property 0 (length erc-notice-prefix)
5189 'face 'erc-notice-face s)
5190 s)
5191 ((eq erc-notice-highlight-type 'all)
5192 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
5193 s)
5194 (t s)))
5195
5196 (defun erc-make-notice (message)
5197 "Notify the user of MESSAGE."
5198 (when erc-minibuffer-notice
5199 (message "%s" message))
5200 (erc-highlight-notice (concat erc-notice-prefix message)))
5201
5202 (defun erc-highlight-error (s)
5203 "Highlight error message S and return it."
5204 (erc-put-text-property 0 (length s) 'face 'erc-error-face s)
5205 s)
5206
5207 (defun erc-put-text-property (start end property value &optional object)
5208 "Set text-property for an object (usually a string).
5209 START and END define the characters covered.
5210 PROPERTY is the text-property set, usually the symbol `face'.
5211 VALUE is the value for the text-property, usually a face symbol such as
5212 the face `bold' or `erc-pal-face'.
5213 OBJECT is a string which will be modified and returned.
5214 OBJECT is modified without being copied first.
5215
5216 You can redefine or `defadvice' this function in order to add
5217 EmacsSpeak support."
5218 (put-text-property start end property value object))
5219
5220 (defun erc-list (thing)
5221 "Return THING if THING is a list, or a list with THING as its element."
5222 (if (listp thing)
5223 thing
5224 (list thing)))
5225
5226 (defun erc-parse-user (string)
5227 "Parse STRING as a user specification (nick!login@host).
5228
5229 Return a list of the three separate tokens."
5230 (cond
5231 ((string-match "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$" string)
5232 (list (match-string 1 string)
5233 (match-string 2 string)
5234 (match-string 3 string)))
5235 ;; Some bogus bouncers send Nick!(null), try to live with that.
5236 ((string-match "^\\([^!\n]*\\)!\\(.*\\)$" string)
5237 (list (match-string 1 string)
5238 ""
5239 (match-string 2 string)))
5240 (t
5241 (list string "" ""))))
5242
5243 (defun erc-extract-nick (string)
5244 "Return the nick corresponding to a user specification STRING.
5245
5246 See also `erc-parse-user'."
5247 (car (erc-parse-user string)))
5248
5249 (defun erc-put-text-properties (start end properties
5250 &optional object value-list)
5251 "Set text-properties for OBJECT.
5252
5253 START and END describe positions in OBJECT.
5254 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
5255 each property to the corresponding value in VALUE-LIST."
5256 (unless value-list
5257 (setq value-list (mapcar (lambda (_x) t)
5258 properties)))
5259 (while (and properties value-list)
5260 (erc-put-text-property
5261 start end (pop properties) (pop value-list) object)))
5262
5263 ;;; Input area handling:
5264
5265 (defun erc-beg-of-input-line ()
5266 "Return the value of `point' at the beginning of the input line.
5267
5268 Specifically, return the position of `erc-insert-marker'."
5269 (or (and (boundp 'erc-insert-marker)
5270 (markerp erc-insert-marker))
5271 (error "erc-insert-marker has no value, please report a bug"))
5272 (marker-position erc-insert-marker))
5273
5274 (defun erc-end-of-input-line ()
5275 "Return the value of `point' at the end of the input line."
5276 (point-max))
5277
5278 (defvar erc-last-input-time 0
5279 "Time of last call to `erc-send-current-line'.
5280 If that function has never been called, the value is 0.")
5281
5282 (defcustom erc-accidental-paste-threshold-seconds nil
5283 "Minimum time, in seconds, before sending new lines via IRC.
5284 If the value is a number, `erc-send-current-line' signals an error
5285 if its previous invocation was fewer than this many seconds ago.
5286 This is useful so that if you accidentally enter large amounts of text
5287 into the ERC buffer, that text is not sent to the IRC server.
5288
5289 If the value is nil, `erc-send-current-line' always considers any
5290 submitted line to be intentional."
5291 :group 'erc
5292 :version "24.4"
5293 :type '(choice number (other :tag "disabled" nil)))
5294
5295 (defun erc-send-current-line ()
5296 "Parse current line and send it to IRC."
5297 (interactive)
5298 (let ((now (float-time)))
5299 (if (or (not erc-accidental-paste-threshold-seconds)
5300 (< erc-accidental-paste-threshold-seconds
5301 (- now erc-last-input-time)))
5302 (save-restriction
5303 (widen)
5304 (if (< (point) (erc-beg-of-input-line))
5305 (erc-error "Point is not in the input area")
5306 (let ((inhibit-read-only t)
5307 (str (erc-user-input))
5308 (old-buf (current-buffer)))
5309 (if (and (not (erc-server-buffer-live-p))
5310 (not (erc-command-no-process-p str)))
5311 (erc-error "ERC: No process running")
5312 (erc-set-active-buffer (current-buffer))
5313 ;; Kill the input and the prompt
5314 (delete-region (erc-beg-of-input-line)
5315 (erc-end-of-input-line))
5316 (unwind-protect
5317 (erc-send-input str)
5318 ;; Fix the buffer if the command didn't kill it
5319 (when (buffer-live-p old-buf)
5320 (with-current-buffer old-buf
5321 (save-restriction
5322 (widen)
5323 (goto-char (point-max))
5324 (when (processp erc-server-process)
5325 (set-marker (process-mark erc-server-process) (point)))
5326 (set-marker erc-insert-marker (point))
5327 (let ((buffer-modified (buffer-modified-p)))
5328 (erc-display-prompt)
5329 (set-buffer-modified-p buffer-modified))))))
5330
5331 ;; Only when last hook has been run...
5332 (run-hook-with-args 'erc-send-completed-hook str))))
5333 (setq erc-last-input-time now))
5334 (switch-to-buffer "*ERC Accidental Paste Overflow*")
5335 (lwarn 'erc :warning
5336 "You seem to have accidentally pasted some text!"))))
5337
5338 (defun erc-user-input ()
5339 "Return the input of the user in the current buffer."
5340 (buffer-substring-no-properties
5341 erc-input-marker
5342 (erc-end-of-input-line)))
5343
5344 (defvar erc-command-regexp "^/\\([A-Za-z']+\\)\\(\\s-+.*\\|\\s-*\\)$"
5345 "Regular expression used for matching commands in ERC.")
5346
5347 (defun erc-send-input (input)
5348 "Treat INPUT as typed in by the user. It is assumed that the input
5349 and the prompt is already deleted.
5350 This returns non-nil only if we actually send anything."
5351 ;; Handle different kinds of inputs
5352 (cond
5353 ;; Ignore empty input
5354 ((if erc-send-whitespace-lines
5355 (string= input "")
5356 (string-match "\\`[ \t\r\f\n]*\\'" input))
5357 (when erc-warn-about-blank-lines
5358 (message "Blank line - ignoring...")
5359 (beep))
5360 nil)
5361 (t
5362 (let ((str input)
5363 (erc-insert-this t))
5364 (setq erc-send-this t)
5365 (run-hook-with-args 'erc-send-pre-hook input)
5366 (when erc-send-this
5367 (if (or (string-match "\n" str)
5368 (not (string-match erc-command-regexp str)))
5369 (mapc
5370 (lambda (line)
5371 (mapc
5372 (lambda (line)
5373 ;; Insert what has to be inserted for this.
5374 (erc-display-msg line)
5375 (erc-process-input-line (concat line "\n")
5376 (null erc-flood-protect) t))
5377 (or (and erc-flood-protect (erc-split-line line))
5378 (list line))))
5379 (split-string str "\n"))
5380 (erc-process-input-line (concat str "\n") t nil))
5381 t)))))
5382
5383 (defun erc-display-command (line)
5384 (when erc-insert-this
5385 (let ((insert-position (point)))
5386 (unless erc-hide-prompt
5387 (erc-display-prompt nil nil (erc-command-indicator)
5388 (and (erc-command-indicator)
5389 'erc-command-indicator-face)))
5390 (let ((beg (point)))
5391 (insert line)
5392 (erc-put-text-property beg (point)
5393 'face 'erc-command-indicator-face)
5394 (insert "\n"))
5395 (when (processp erc-server-process)
5396 (set-marker (process-mark erc-server-process) (point)))
5397 (set-marker erc-insert-marker (point))
5398 (save-excursion
5399 (save-restriction
5400 (narrow-to-region insert-position (point))
5401 (run-hooks 'erc-send-modify-hook)
5402 (run-hooks 'erc-send-post-hook))))))
5403
5404 (defun erc-display-msg (line)
5405 "Display LINE as a message of the user to the current target at the
5406 current position."
5407 (when erc-insert-this
5408 (let ((insert-position (point)))
5409 (insert (erc-format-my-nick))
5410 (let ((beg (point)))
5411 (insert line)
5412 (erc-put-text-property beg (point)
5413 'face 'erc-input-face))
5414 (insert "\n")
5415 (when (processp erc-server-process)
5416 (set-marker (process-mark erc-server-process) (point)))
5417 (set-marker erc-insert-marker (point))
5418 (save-excursion
5419 (save-restriction
5420 (narrow-to-region insert-position (point))
5421 (run-hooks 'erc-send-modify-hook)
5422 (run-hooks 'erc-send-post-hook))))))
5423
5424 (defun erc-command-symbol (command)
5425 "Return the ERC command symbol for COMMAND if it exists and is bound."
5426 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
5427 (when (fboundp cmd) cmd)))
5428
5429 (defun erc-extract-command-from-line (line)
5430 "Extract command and args from the input LINE.
5431 If no command was given, return nil. If command matches, return a
5432 list of the form: (command args) where both elements are strings."
5433 (when (string-match erc-command-regexp line)
5434 (let* ((cmd (erc-command-symbol (match-string 1 line)))
5435 ;; note: return is nil, we apply this simply for side effects
5436 (_canon-defun (while (and cmd (symbolp (symbol-function cmd)))
5437 (setq cmd (symbol-function cmd))))
5438 (cmd-fun (or cmd #'erc-cmd-default))
5439 (arg (if cmd
5440 (if (get cmd-fun 'do-not-parse-args)
5441 (format "%s" (match-string 2 line))
5442 (delete "" (split-string (erc-trim-string
5443 (match-string 2 line)) " ")))
5444 line)))
5445 (list cmd-fun arg))))
5446
5447 (defun erc-split-multiline-safe (string)
5448 "Split STRING, containing multiple lines and return them in a list.
5449 Do it only for STRING as the complete input, do not carry unfinished
5450 strings over to the next call."
5451 (let ((l ())
5452 (i0 0)
5453 (doit t))
5454 (while doit
5455 (let ((i (string-match "\r?\n" string i0))
5456 (s (substring string i0)))
5457 (cond (i (setq l (cons (substring string i0 i) l))
5458 (setq i0 (match-end 0)))
5459 ((> (length s) 0)
5460 (setq l (cons s l))(setq doit nil))
5461 (t (setq doit nil)))))
5462 (nreverse l)))
5463
5464 ;; nick handling
5465
5466 (defun erc-set-current-nick (nick)
5467 "Set the current nickname to NICK."
5468 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5469 (erc-server-buffer)
5470 (current-buffer))
5471 (setq erc-server-current-nick nick)))
5472
5473 (defun erc-current-nick ()
5474 "Return the current nickname."
5475 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5476 (erc-server-buffer)
5477 (current-buffer))
5478 erc-server-current-nick))
5479
5480 (defun erc-current-nick-p (nick)
5481 "Return non-nil if NICK is the current nickname."
5482 (erc-nick-equal-p nick (erc-current-nick)))
5483
5484 (defun erc-nick-equal-p (nick1 nick2)
5485 "Return non-nil if NICK1 and NICK2 are the same.
5486
5487 This matches strings according to the IRC protocol's case convention.
5488
5489 See also `erc-downcase'."
5490 (string= (erc-downcase nick1)
5491 (erc-downcase nick2)))
5492
5493 ;; default target handling
5494
5495 (defun erc-default-target ()
5496 "Return the current default target (as a character string) or nil if none."
5497 (let ((tgt (car erc-default-recipients)))
5498 (cond
5499 ((not tgt) nil)
5500 ((listp tgt) (cdr tgt))
5501 (t tgt))))
5502
5503 (defun erc-add-default-channel (channel)
5504 "Add CHANNEL to the default channel list."
5505 (let ((chl (downcase channel)))
5506 (setq erc-default-recipients
5507 (cons chl erc-default-recipients))))
5508
5509 (defun erc-delete-default-channel (channel &optional buffer)
5510 "Delete CHANNEL from the default channel list."
5511 (with-current-buffer (if (and buffer
5512 (bufferp buffer))
5513 buffer
5514 (current-buffer))
5515 (setq erc-default-recipients (delete (downcase channel)
5516 erc-default-recipients))))
5517
5518 (defun erc-add-query (nickname)
5519 "Add QUERY'd NICKNAME to the default channel list.
5520
5521 The previous default target of QUERY type gets removed."
5522 (let ((d1 (car erc-default-recipients))
5523 (d2 (cdr erc-default-recipients))
5524 (qt (cons 'QUERY (downcase nickname))))
5525 (setq erc-default-recipients (cons qt (if (and (listp d1)
5526 (eq (car d1) 'QUERY))
5527 d2
5528 erc-default-recipients)))))
5529
5530 (defun erc-delete-query ()
5531 "Delete the topmost target if it is a QUERY."
5532
5533 (let ((d1 (car erc-default-recipients))
5534 (d2 (cdr erc-default-recipients)))
5535 (if (and (listp d1)
5536 (eq (car d1) 'QUERY))
5537 (setq erc-default-recipients d2)
5538 (error "Current target is not a QUERY"))))
5539
5540 (defun erc-ignored-user-p (spec)
5541 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5542
5543 Takes a full SPEC of a user in the form \"nick!login@host\", and
5544 matches against all the regexp's in `erc-ignore-list'. If any
5545 match, returns that regexp."
5546 (catch 'found
5547 (dolist (ignored (erc-with-server-buffer erc-ignore-list))
5548 (if (string-match ignored spec)
5549 (throw 'found ignored)))))
5550
5551 (defun erc-ignored-reply-p (msg tgt proc)
5552 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5553 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5554
5555 Takes a message MSG to a channel and returns non-nil if the addressed
5556 user matches any regexp in `erc-ignore-reply-list'."
5557 (let ((target-nick (erc-message-target msg)))
5558 (if (not target-nick)
5559 nil
5560 (erc-with-buffer (tgt proc)
5561 (let ((user (erc-get-server-user target-nick)))
5562 (when user
5563 (erc-list-match erc-ignore-reply-list
5564 (erc-user-spec user))))))))
5565
5566 (defun erc-message-target (msg)
5567 "Return the addressed target in MSG.
5568
5569 The addressed target is the string before the first colon in MSG."
5570 (if (string-match "^\\([^: \n]*\\):" msg)
5571 (match-string 1 msg)
5572 nil))
5573
5574 (defun erc-user-spec (user)
5575 "Create a nick!user@host spec from a user struct."
5576 (let ((nick (erc-server-user-nickname user))
5577 (host (erc-server-user-host user))
5578 (login (erc-server-user-login user)))
5579 (concat (or nick "")
5580 "!"
5581 (or login "")
5582 "@"
5583 (or host ""))))
5584
5585 (defun erc-list-match (lst str)
5586 "Return non-nil if any regexp in LST matches STR."
5587 (memq nil (mapcar (lambda (regexp)
5588 (not (string-match regexp str)))
5589 lst)))
5590
5591 ;; other "toggles"
5592
5593 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5594 "Toggle automatic CTCP replies (like VERSION and PING).
5595
5596 If ARG is positive, turns CTCP replies on.
5597
5598 If ARG is non-nil and not positive, turns CTCP replies off."
5599 (interactive "P")
5600 (cond ((and (numberp arg) (> arg 0))
5601 (setq erc-disable-ctcp-replies t))
5602 (arg (setq erc-disable-ctcp-replies nil))
5603 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5604 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5605
5606 (defun erc-toggle-flood-control (&optional arg)
5607 "Toggle use of flood control on sent messages.
5608
5609 If ARG is positive, use flood control.
5610 If ARG is non-nil and not positive, do not use flood control.
5611
5612 See `erc-server-flood-margin' for an explanation of the available
5613 flood control parameters."
5614 (interactive "P")
5615 (cond ((and (numberp arg) (> arg 0))
5616 (setq erc-flood-protect t))
5617 (arg (setq erc-flood-protect nil))
5618 (t (setq erc-flood-protect (not erc-flood-protect))))
5619 (message "ERC flood control is %s"
5620 (cond (erc-flood-protect "ON")
5621 (t "OFF"))))
5622
5623 ;; Some useful channel and nick commands for fast key bindings
5624
5625 (defun erc-invite-only-mode (&optional arg)
5626 "Turn on the invite only mode (+i) for the current channel.
5627
5628 If ARG is non-nil, turn this mode off (-i).
5629
5630 This command is sent even if excess flood is detected."
5631 (interactive "P")
5632 (erc-set-active-buffer (current-buffer))
5633 (let ((tgt (erc-default-target)))
5634 (if (or (not tgt) (not (erc-channel-p tgt)))
5635 (erc-display-message nil 'error (current-buffer) 'no-target)
5636 (erc-load-irc-script-lines
5637 (list (concat "/mode " tgt (if arg " -i" " +i")))
5638 t))))
5639
5640 (defun erc-get-channel-mode-from-keypress (key)
5641 "Read a key sequence and call the corresponding channel mode function.
5642 After doing C-c C-o, type in a channel mode letter.
5643
5644 C-g means quit.
5645 RET lets you type more than one mode at a time.
5646 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5647 If \"k\" is pressed, `erc-set-channel-key' gets called.
5648 Anything else will be sent to `erc-toggle-channel-mode'."
5649 (interactive "kChannel mode (RET to set more than one): ")
5650 (when (featurep 'xemacs)
5651 (setq key (char-to-string (event-to-character (aref key 0)))))
5652 (cond ((equal key "\C-g")
5653 (keyboard-quit))
5654 ((equal key "\C-m")
5655 (erc-insert-mode-command))
5656 ((equal key "l")
5657 (call-interactively 'erc-set-channel-limit))
5658 ((equal key "k")
5659 (call-interactively 'erc-set-channel-key))
5660 (t (erc-toggle-channel-mode key))))
5661
5662 (defun erc-toggle-channel-mode (mode &optional channel)
5663 "Toggle channel MODE.
5664
5665 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5666 `erc-default-target'."
5667 (interactive "P")
5668 (erc-set-active-buffer (current-buffer))
5669 (let ((tgt (or channel (erc-default-target))))
5670 (if (or (null tgt) (null (erc-channel-p tgt)))
5671 (erc-display-message nil 'error 'active 'no-target)
5672 (let* ((active (member mode erc-channel-modes))
5673 (newstate (if active "OFF" "ON")))
5674 (erc-log (format "%s: Toggle mode %s %s" tgt mode newstate))
5675 (message "Toggle channel mode %s %s" mode newstate)
5676 (erc-server-send (format "MODE %s %s%s"
5677 tgt (if active "-" "+") mode))))))
5678
5679 (defun erc-insert-mode-command ()
5680 "Insert the line \"/mode <current target> \" at `point'."
5681 (interactive)
5682 (let ((tgt (erc-default-target)))
5683 (if tgt (insert (concat "/mode " tgt " "))
5684 (erc-display-message nil 'error (current-buffer) 'no-target))))
5685
5686 (defun erc-channel-names ()
5687 "Run \"/names #channel\" in the current channel."
5688 (interactive)
5689 (erc-set-active-buffer (current-buffer))
5690 (let ((tgt (erc-default-target)))
5691 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5692 (erc-display-message nil 'error (current-buffer) 'no-target))))
5693
5694 (defun erc-remove-text-properties-region (start end &optional object)
5695 "Clears the region (START,END) in OBJECT from all colors, etc."
5696 (interactive "r")
5697 (save-excursion
5698 (let ((inhibit-read-only t))
5699 (set-text-properties start end nil object))))
5700 (put 'erc-remove-text-properties-region 'disabled t)
5701
5702 ;; script execution and startup
5703
5704 (defun erc-find-file (file &optional path)
5705 "Search for a FILE in the filesystem.
5706 First the `default-directory' is searched for FILE, then any directories
5707 specified in the list PATH.
5708
5709 If FILE is found, return the path to it."
5710 (let ((filepath file))
5711 (if (file-readable-p filepath) filepath
5712 (while (and path
5713 (progn (setq filepath (expand-file-name file (car path)))
5714 (not (file-readable-p filepath))))
5715 (setq path (cdr path)))
5716 (if path filepath nil))))
5717
5718 (defun erc-select-startup-file ()
5719 "Select an ERC startup file.
5720 See also `erc-startup-file-list'."
5721 (catch 'found
5722 (dolist (f erc-startup-file-list)
5723 (setq f (convert-standard-filename f))
5724 (when (file-readable-p f)
5725 (throw 'found f)))))
5726
5727 (defun erc-find-script-file (file)
5728 "Search for FILE in `default-directory', and any in `erc-script-path'."
5729 (erc-find-file file erc-script-path))
5730
5731 (defun erc-load-script (file)
5732 "Load a script from FILE.
5733
5734 FILE must be the full name, it is not searched in the
5735 `erc-script-path'. If the filename ends with `.el', then load it
5736 as an Emacs Lisp program. Otherwise, treat it as a regular IRC
5737 script."
5738 (erc-log (concat "erc-load-script: " file))
5739 (cond
5740 ((string-match "\\.el\\'" file)
5741 (load file))
5742 (t
5743 (erc-load-irc-script file))))
5744
5745 (defun erc-process-script-line (line &optional args)
5746 "Process an IRC script LINE.
5747
5748 Does script-specific substitutions (script arguments, current nick,
5749 server, etc.) in LINE and returns it.
5750
5751 Substitutions are: %C and %c = current target (channel or nick),
5752 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5753 where x is any other character;
5754 $* = the entire argument string, $1 = the first argument, $2 = the second,
5755 and so on."
5756 (if (not args) (setq args ""))
5757 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5758 (percent-regexp "\\(%.\\)")
5759 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5760 (tgt (erc-default-target))
5761 (server (and (boundp 'erc-session-server) erc-session-server))
5762 (nick (erc-current-nick))
5763 (res "")
5764 (tmp nil)
5765 (arg-list nil)
5766 (arg-num 0))
5767 (if (not tgt) (setq tgt ""))
5768 (if (not server) (setq server ""))
5769 (if (not nick) (setq nick ""))
5770 ;; First, compute the argument list
5771 (setq tmp args)
5772 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5773 (setq arg-list (cons (match-string 1 tmp) arg-list))
5774 (setq tmp (match-string 2 tmp)))
5775 (setq arg-list (nreverse arg-list))
5776 (setq arg-num (length arg-list))
5777 ;; now do the substitution
5778 (setq tmp (string-match esc-regexp line))
5779 (while tmp
5780 ;;(message "beginning of while: tmp=%S" tmp)
5781 (let* ((hd (substring line 0 tmp))
5782 (esc "")
5783 (subst "")
5784 (tail (substring line tmp)))
5785 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5786 (setq esc (match-string 1 tail))
5787 (setq tail (substring tail (match-end 1))))
5788 ((string-match (concat "^" percent-regexp) tail)
5789 (setq esc (match-string 1 tail))
5790 (setq tail (substring tail (match-end 1)))))
5791 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5792 (setq res (concat res hd))
5793 (setq subst
5794 (cond ((string= esc "") "")
5795 ((string-match "^\\$\\*$" esc) args)
5796 ((string-match "^\\$\\([0-9]+\\)$" esc)
5797 (let ((n (string-to-number (match-string 1 esc))))
5798 (message "n = %S, integerp(n)=%S" n (integerp n))
5799 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5800 ((string-match "^%[Cc]$" esc) tgt)
5801 ((string-match "^%[Ss]$" esc) server)
5802 ((string-match "^%[Nn]$" esc) nick)
5803 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5804 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5805 (message "BUG IN ERC: esc=%S" esc)
5806 "")))
5807 (setq line tail)
5808 (setq tmp (string-match esc-regexp line))
5809 (setq res (concat res subst))
5810 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5811 ))
5812 (setq res (concat res line))
5813 res))
5814
5815 (defun erc-load-irc-script (file &optional force)
5816 "Load an IRC script from FILE."
5817 (erc-log (concat "erc-load-script: " file))
5818 (let ((str (with-temp-buffer
5819 (insert-file-contents file)
5820 (buffer-string))))
5821 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5822
5823 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5824 "Load IRC script LINES (a list of strings).
5825
5826 If optional NOEXPAND is non-nil, do not expand script-specific
5827 sequences, process the lines verbatim. Use this for multiline
5828 user input."
5829 (let* ((cb (current-buffer))
5830 (s "")
5831 (sp (or (erc-command-indicator) (erc-prompt)))
5832 (args (and (boundp 'erc-script-args) erc-script-args)))
5833 (if (and args (string-match "^ " args))
5834 (setq args (substring args 1)))
5835 ;; prepare the prompt string for echo
5836 (erc-put-text-property 0 (length sp)
5837 'face 'erc-command-indicator-face sp)
5838 (while lines
5839 (setq s (car lines))
5840 (erc-log (concat "erc-load-script: CMD: " s))
5841 (unless (string-match "^\\s-*$" s)
5842 (let ((line (if noexpand s (erc-process-script-line s args))))
5843 (if (and (erc-process-input-line line force)
5844 erc-script-echo)
5845 (progn
5846 (erc-put-text-property 0 (length line)
5847 'face 'erc-input-face line)
5848 (erc-display-line (concat sp line) cb)))))
5849 (setq lines (cdr lines)))))
5850
5851 ;; authentication
5852
5853 (defun erc-login ()
5854 "Perform user authentication at the IRC server."
5855 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5856 (erc-current-nick)
5857 (user-login-name)
5858 (or erc-system-name (system-name))
5859 erc-session-server
5860 erc-session-user-full-name))
5861 (if erc-session-password
5862 (erc-server-send (format "PASS %s" erc-session-password))
5863 (message "Logging in without password"))
5864 (erc-server-send (format "NICK %s" (erc-current-nick)))
5865 (erc-server-send
5866 (format "USER %s %s %s :%s"
5867 ;; hacked - S.B.
5868 (if erc-anonymous-login erc-email-userid (user-login-name))
5869 "0" "*"
5870 erc-session-user-full-name))
5871 (erc-update-mode-line))
5872
5873 ;; connection properties' heuristics
5874
5875 (defun erc-determine-parameters (&optional server port nick name)
5876 "Determine the connection and authentication parameters.
5877 Sets the buffer local variables:
5878
5879 - `erc-session-connector'
5880 - `erc-session-server'
5881 - `erc-session-port'
5882 - `erc-session-full-name'
5883 - `erc-server-current-nick'"
5884 (setq erc-session-connector erc-server-connect-function
5885 erc-session-server (erc-compute-server server)
5886 erc-session-port (or port erc-default-port)
5887 erc-session-user-full-name (erc-compute-full-name name))
5888 (erc-set-current-nick (erc-compute-nick nick)))
5889
5890 (defun erc-compute-server (&optional server)
5891 "Return an IRC server name.
5892
5893 This tries a number of increasingly more default methods until a
5894 non-nil value is found.
5895
5896 - SERVER (the argument passed to this function)
5897 - The `erc-server' option
5898 - The value of the IRCSERVER environment variable
5899 - The `erc-default-server' variable"
5900 (or server
5901 erc-server
5902 (getenv "IRCSERVER")
5903 erc-default-server))
5904
5905 (defun erc-compute-nick (&optional nick)
5906 "Return user's IRC nick.
5907
5908 This tries a number of increasingly more default methods until a
5909 non-nil value is found.
5910
5911 - NICK (the argument passed to this function)
5912 - The `erc-nick' option
5913 - The value of the IRCNICK environment variable
5914 - The result from the `user-login-name' function"
5915 (or nick
5916 (if (consp erc-nick) (car erc-nick) erc-nick)
5917 (getenv "IRCNICK")
5918 (user-login-name)))
5919
5920
5921 (defun erc-compute-full-name (&optional full-name)
5922 "Return user's full name.
5923
5924 This tries a number of increasingly more default methods until a
5925 non-nil value is found.
5926
5927 - FULL-NAME (the argument passed to this function)
5928 - The `erc-user-full-name' option
5929 - The value of the IRCNAME environment variable
5930 - The result from the `user-full-name' function"
5931 (or full-name
5932 erc-user-full-name
5933 (getenv "IRCNAME")
5934 (if erc-anonymous-login "unknown" nil)
5935 (user-full-name)))
5936
5937 (defun erc-compute-port (&optional port)
5938 "Return a port for an IRC server.
5939
5940 This tries a number of increasingly more default methods until a
5941 non-nil value is found.
5942
5943 - PORT (the argument passed to this function)
5944 - The `erc-port' option
5945 - The `erc-default-port' variable"
5946 (or port erc-port erc-default-port))
5947
5948 ;; time routines
5949
5950 (defun erc-string-to-emacs-time (string)
5951 "Convert the long number represented by STRING into an Emacs format.
5952 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
5953 (let* ((n (string-to-number (concat string ".0"))))
5954 (list (truncate (/ n 65536))
5955 (truncate (mod n 65536)))))
5956
5957 (defun erc-emacs-time-to-erc-time (time)
5958 "Convert Emacs TIME to a number of seconds since the epoch."
5959 (when time
5960 (+ (* (nth 0 time) 65536.0) (nth 1 time))))
5961 ; (round (+ (* (nth 0 tm) 65536.0) (nth 1 tm))))
5962
5963 (defun erc-current-time ()
5964 "Return the `current-time' as a number of seconds since the epoch.
5965
5966 See also `erc-emacs-time-to-erc-time'."
5967 (erc-emacs-time-to-erc-time (current-time)))
5968
5969 (defun erc-time-diff (t1 t2)
5970 "Return the time difference in seconds between T1 and T2."
5971 (abs (- t2 t1)))
5972
5973 (defun erc-time-gt (t1 t2)
5974 "Check whether T1 > T2."
5975 (> t1 t2))
5976
5977 (defun erc-sec-to-time (ns)
5978 "Convert NS to a time string HH:MM.SS."
5979 (setq ns (truncate ns))
5980 (format "%02d:%02d.%02d"
5981 (/ ns 3600)
5982 (/ (% ns 3600) 60)
5983 (% ns 60)))
5984
5985 (defun erc-seconds-to-string (seconds)
5986 "Convert a number of SECONDS into an English phrase."
5987 (let (days hours minutes format-args output)
5988 (setq days (/ seconds 86400)
5989 seconds (% seconds 86400)
5990 hours (/ seconds 3600)
5991 seconds (% seconds 3600)
5992 minutes (/ seconds 60)
5993 seconds (% seconds 60)
5994 format-args (if (> days 0)
5995 `("%d days, %d hours, %d minutes, %d seconds"
5996 ,days ,hours ,minutes ,seconds)
5997 (if (> hours 0)
5998 `("%d hours, %d minutes, %d seconds"
5999 ,hours ,minutes ,seconds)
6000 (if (> minutes 0)
6001 `("%d minutes, %d seconds" ,minutes ,seconds)
6002 `("%d seconds" ,seconds))))
6003 output (apply 'format format-args))
6004 ;; Change all "1 units" to "1 unit".
6005 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
6006 (setq output (erc-replace-match-subexpression-in-string
6007 "" output (match-string 2 output) 2 (match-beginning 2))))
6008 output))
6009
6010
6011 ;; info
6012
6013 (defconst erc-clientinfo-alist
6014 '(("ACTION" . "is used to inform about one's current activity")
6015 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
6016 ("ECHO" . "echoes its arguments back")
6017 ("FINGER" . "shows user's name, location, and idle time")
6018 ("PING" . "measures delay between peers")
6019 ("TIME" . "shows client-side time")
6020 ("USERINFO" . "shows information provided by a user")
6021 ("VERSION" . "shows client type and version"))
6022 "Alist of CTCP CLIENTINFO for ERC commands.")
6023
6024 (defun erc-client-info (s)
6025 "Return CTCP CLIENTINFO on command S.
6026 If S is nil or an empty string then return general CLIENTINFO."
6027 (if (or (not s) (string= s ""))
6028 (concat
6029 (apply #'concat
6030 (mapcar (lambda (e)
6031 (concat (car e) " "))
6032 erc-clientinfo-alist))
6033 ": use CLIENTINFO <COMMAND> to get more specific information")
6034 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
6035 (if h
6036 (concat s " " (cdr h))
6037 (concat s ": unknown command")))))
6038
6039 ;; Hook functions
6040
6041 (defun erc-directory-writable-p (dir)
6042 "Determine whether DIR is a writable directory.
6043 If it doesn't exist, create it."
6044 (unless (file-attributes dir) (make-directory dir))
6045 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
6046
6047 (defun erc-kill-query-buffers (process)
6048 "Kill all buffers of PROCESS."
6049 ;; here, we only want to match the channel buffers, to avoid
6050 ;; "selecting killed buffers" b0rkage.
6051 (erc-with-all-buffers-of-server process
6052 (lambda ()
6053 (not (erc-server-buffer-p)))
6054 (kill-buffer (current-buffer))))
6055
6056 (defun erc-nick-at-point ()
6057 "Give information about the nickname at `point'.
6058
6059 If called interactively, give a human readable message in the
6060 minibuffer. If called programmatically, return the corresponding
6061 entry of `channel-members'."
6062 (interactive)
6063 (require 'thingatpt)
6064 (let* ((word (word-at-point))
6065 (channel-data (erc-get-channel-user word))
6066 (cuser (cdr channel-data))
6067 (user (if channel-data
6068 (car channel-data)
6069 (erc-get-server-user word)))
6070 host login full-name nick voice halfop op admin owner)
6071 (when user
6072 (setq nick (erc-server-user-nickname user)
6073 host (erc-server-user-host user)
6074 login (erc-server-user-login user)
6075 full-name (erc-server-user-full-name user))
6076 (if cuser
6077 (setq voice (erc-channel-user-voice cuser)
6078 halfop (erc-channel-user-halfop cuser)
6079 op (erc-channel-user-op cuser)
6080 admin (erc-channel-user-admin cuser)
6081 owner (erc-channel-user-owner cuser))))
6082 (if (called-interactively-p 'interactive)
6083 (message "%s is %s@%s%s%s"
6084 nick login host
6085 (if full-name (format " (%s)" full-name) "")
6086 (if (or voice halfop op admin owner)
6087 (format " and is +%s%s on %s"
6088 (if voice "v" "")
6089 (if halfop "h" "")
6090 (if op "o" "")
6091 (if admin "a" "")
6092 (if owner "q" "")
6093 (erc-default-target))
6094 ""))
6095 user)))
6096
6097 (defun erc-away-time ()
6098 "Return non-nil if the current ERC process is set away.
6099
6100 In particular, the time that we were set away is returned.
6101 See `current-time' for details on the time format."
6102 (erc-with-server-buffer erc-away))
6103
6104 ;; Mode line handling
6105
6106 (defcustom erc-mode-line-format "%S %a"
6107 "A string to be formatted and shown in the mode-line in `erc-mode'.
6108
6109 The string is formatted using `format-spec' and the result is set as the value
6110 of `mode-line-buffer-identification'.
6111
6112 The following characters are replaced:
6113 %a: String indicating away status or \"\" if you are not away
6114 %l: The estimated lag time to the server
6115 %m: The modes of the channel
6116 %n: The current nick name
6117 %N: The name of the network
6118 %o: The topic of the channel
6119 %p: The session port
6120 %t: The name of the target (channel, nickname, or servername:port)
6121 %s: In the server-buffer, this gets filled with the value of
6122 `erc-server-announced-name', in a channel, the value of
6123 (erc-default-target) also get concatenated.
6124 %S: In the server-buffer, this gets filled with the value of
6125 `erc-network', in a channel, the value of (erc-default-target)
6126 also get concatenated."
6127 :group 'erc-mode-line-and-header
6128 :type 'string)
6129
6130 (defcustom erc-header-line-format "%n on %t (%m,%l) %o"
6131 "A string to be formatted and shown in the header-line in `erc-mode'.
6132 Only used starting in Emacs 21.
6133
6134 Set this to nil if you do not want the header line to be
6135 displayed.
6136
6137 See `erc-mode-line-format' for which characters are can be used."
6138 :group 'erc-mode-line-and-header
6139 :set (lambda (sym val)
6140 (set sym val)
6141 (when (fboundp 'erc-update-mode-line)
6142 (erc-update-mode-line nil)))
6143 :type '(choice (const :tag "Disabled" nil)
6144 string))
6145
6146 (defcustom erc-header-line-uses-tabbar-p nil
6147 "Use tabbar mode instead of the header line to display the header."
6148 :group 'erc-mode-line-and-header
6149 :type 'boolean)
6150
6151 (defcustom erc-header-line-uses-help-echo-p t
6152 "Show the contents of the header line in the echo area or as a tooltip
6153 when you move point into the header line."
6154 :group 'erc-mode-line-and-header
6155 :type 'boolean)
6156
6157 (defcustom erc-header-line-face-method nil
6158 "Determine what method to use when colorizing the header line text.
6159
6160 If nil, don't colorize the header text.
6161 If given a function, call it and use the resulting face name.
6162 Otherwise, use the `erc-header-line' face."
6163 :group 'erc-mode-line-and-header
6164 :type '(choice (const :tag "Don't colorize" nil)
6165 (const :tag "Use the erc-header-line face" t)
6166 (function :tag "Call a function")))
6167
6168 (defcustom erc-show-channel-key-p t
6169 "Show the channel key in the header line."
6170 :group 'erc-paranoia
6171 :type 'boolean)
6172
6173 (defcustom erc-mode-line-away-status-format
6174 "(AWAY since %a %b %d %H:%M) "
6175 "When you're away on a server, this is shown in the mode line.
6176 This should be a string with substitution variables recognized by
6177 `format-time-string'."
6178 :group 'erc-mode-line-and-header
6179 :type 'string)
6180
6181 (defun erc-shorten-server-name (server-name)
6182 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
6183 (if (stringp server-name)
6184 (with-temp-buffer
6185 (insert server-name)
6186 (let ((alist erc-common-server-suffixes))
6187 (while alist
6188 (goto-char (point-min))
6189 (if (re-search-forward (caar alist) nil t)
6190 (replace-match (cdar alist)))
6191 (setq alist (cdr alist))))
6192 (buffer-string))))
6193
6194 (defun erc-format-target ()
6195 "Return the name of the target (channel or nickname or servername:port)."
6196 (let ((target (erc-default-target)))
6197 (or target
6198 (concat (erc-shorten-server-name
6199 (or erc-server-announced-name
6200 erc-session-server))
6201 ":" (erc-port-to-string erc-session-port)))))
6202
6203 (defun erc-format-target-and/or-server ()
6204 "Return the server name or the current target and server name combined."
6205 (let ((server-name (erc-shorten-server-name
6206 (or erc-server-announced-name
6207 erc-session-server))))
6208 (cond ((erc-default-target)
6209 (concat (erc-string-no-properties (erc-default-target))
6210 "@" server-name))
6211 (server-name server-name)
6212 (t (buffer-name (current-buffer))))))
6213
6214 (defun erc-format-network ()
6215 "Return the name of the network we are currently on."
6216 (let ((network (and (fboundp 'erc-network-name) (erc-network-name))))
6217 (if (and network (symbolp network))
6218 (symbol-name network)
6219 "")))
6220
6221 (defun erc-format-target-and/or-network ()
6222 "Return the network or the current target and network combined.
6223 If the name of the network is not available, then use the
6224 shortened server name instead."
6225 (let ((network-name (or (and (fboundp 'erc-network-name) (erc-network-name))
6226 (erc-shorten-server-name
6227 (or erc-server-announced-name
6228 erc-session-server)))))
6229 (when (and network-name (symbolp network-name))
6230 (setq network-name (symbol-name network-name)))
6231 (cond ((erc-default-target)
6232 (concat (erc-string-no-properties (erc-default-target))
6233 "@" network-name))
6234 ((and network-name
6235 (not (get-buffer network-name)))
6236 (rename-buffer network-name)
6237 network-name)
6238 (t (buffer-name (current-buffer))))))
6239
6240 (defun erc-format-away-status ()
6241 "Return a formatted `erc-mode-line-away-status-format'
6242 if `erc-away' is non-nil."
6243 (let ((a (erc-away-time)))
6244 (if a
6245 (format-time-string erc-mode-line-away-status-format a)
6246 "")))
6247
6248 (defun erc-format-channel-modes ()
6249 "Return the current channel's modes."
6250 (concat (apply 'concat
6251 "+" erc-channel-modes)
6252 (cond ((and erc-channel-user-limit erc-channel-key)
6253 (if erc-show-channel-key-p
6254 (format "lk %.0f %s" erc-channel-user-limit
6255 erc-channel-key)
6256 (format "kl %.0f" erc-channel-user-limit)))
6257 (erc-channel-user-limit
6258 ;; Emacs has no bignums
6259 (format "l %.0f" erc-channel-user-limit))
6260 (erc-channel-key
6261 (if erc-show-channel-key-p
6262 (format "k %s" erc-channel-key)
6263 "k"))
6264 (t nil))))
6265
6266 (defun erc-format-lag-time ()
6267 "Return the estimated lag time to server, `erc-server-lag'."
6268 (let ((lag (erc-with-server-buffer erc-server-lag)))
6269 (cond (lag (format "lag:%.0f" lag))
6270 (t ""))))
6271
6272 ;; erc-goodies is required at end of this file.
6273 (declare-function erc-controls-strip "erc-goodies" (str))
6274
6275 (defvar tabbar--local-hlf)
6276
6277 (defun erc-update-mode-line-buffer (buffer)
6278 "Update the mode line in a single ERC buffer BUFFER."
6279 (with-current-buffer buffer
6280 (let ((spec (format-spec-make
6281 ?a (erc-format-away-status)
6282 ?l (erc-format-lag-time)
6283 ?m (erc-format-channel-modes)
6284 ?n (or (erc-current-nick) "")
6285 ?N (erc-format-network)
6286 ?o (or (erc-controls-strip erc-channel-topic) "")
6287 ?p (erc-port-to-string erc-session-port)
6288 ?s (erc-format-target-and/or-server)
6289 ?S (erc-format-target-and/or-network)
6290 ?t (erc-format-target)))
6291 (process-status (cond ((and (erc-server-process-alive)
6292 (not erc-server-connected))
6293 ":connecting")
6294 ((erc-server-process-alive)
6295 "")
6296 (t
6297 ": CLOSED")))
6298 (face (cond ((eq erc-header-line-face-method nil)
6299 nil)
6300 ((functionp erc-header-line-face-method)
6301 (funcall erc-header-line-face-method))
6302 (t
6303 'erc-header-line))))
6304 (cond ((featurep 'xemacs)
6305 (setq modeline-buffer-identification
6306 (list (format-spec erc-mode-line-format spec)))
6307 (setq modeline-process (list process-status)))
6308 (t
6309 (setq mode-line-buffer-identification
6310 (list (format-spec erc-mode-line-format spec)))
6311 (setq mode-line-process (list process-status))))
6312 (when (boundp 'header-line-format)
6313 (let ((header (if erc-header-line-format
6314 (format-spec erc-header-line-format spec)
6315 nil)))
6316 (cond (erc-header-line-uses-tabbar-p
6317 (set (make-local-variable 'tabbar--local-hlf)
6318 header-line-format)
6319 (kill-local-variable 'header-line-format))
6320 ((null header)
6321 (setq header-line-format nil))
6322 (erc-header-line-uses-help-echo-p
6323 (let ((help-echo (with-temp-buffer
6324 (insert header)
6325 (fill-region (point-min) (point-max))
6326 (buffer-string))))
6327 (setq header-line-format
6328 (erc-replace-regexp-in-string
6329 "%"
6330 "%%"
6331 (if face
6332 (erc-propertize header 'help-echo help-echo
6333 'face face)
6334 (erc-propertize header 'help-echo help-echo))))))
6335 (t (setq header-line-format
6336 (if face
6337 (erc-propertize header 'face face)
6338 header)))))))
6339 (if (featurep 'xemacs)
6340 (redraw-modeline)
6341 (force-mode-line-update))))
6342
6343 (defun erc-update-mode-line (&optional buffer)
6344 "Update the mode line in BUFFER.
6345
6346 If BUFFER is nil, update the mode line in all ERC buffers."
6347 (if (and buffer (bufferp buffer))
6348 (erc-update-mode-line-buffer buffer)
6349 (dolist (buf (erc-buffer-list))
6350 (when (buffer-live-p buf)
6351 (erc-update-mode-line-buffer buf)))))
6352
6353 ;; Miscellaneous
6354
6355 (defun erc-port-to-string (p)
6356 "Convert port P to a string.
6357 P may be an integer or a service name."
6358 (if (integerp p)
6359 (int-to-string p)
6360 p))
6361
6362 (defun erc-string-to-port (s)
6363 "Convert string S to either an integer port number or a service name."
6364 (if (numberp s)
6365 s
6366 (let ((n (string-to-number s)))
6367 (if (= n 0)
6368 s
6369 n))))
6370
6371 (defun erc-version (&optional here)
6372 "Show the version number of ERC in the minibuffer.
6373 If optional argument HERE is non-nil, insert version number at point."
6374 (interactive "P")
6375 (let ((version-string
6376 (format "ERC %s (GNU Emacs %s)" erc-version-string emacs-version)))
6377 (if here
6378 (insert version-string)
6379 (if (called-interactively-p 'interactive)
6380 (message "%s" version-string)
6381 version-string))))
6382
6383 (defun erc-modes (&optional here)
6384 "Show the active ERC modes in the minibuffer.
6385 If optional argument HERE is non-nil, insert version number at point."
6386 (interactive "P")
6387 (let ((string
6388 (mapconcat 'identity
6389 (let (modes (case-fold-search nil))
6390 (dolist (var (apropos-internal "^erc-.*mode$"))
6391 (when (and (boundp var)
6392 (symbol-value var))
6393 (setq modes (cons (symbol-name var)
6394 modes))))
6395 modes)
6396 ", ")))
6397 (if here
6398 (insert string)
6399 (if (called-interactively-p 'interactive)
6400 (message "%s" string)
6401 string))))
6402
6403 (defun erc-trim-string (s)
6404 "Trim leading and trailing spaces off S."
6405 (cond
6406 ((not (stringp s)) nil)
6407 ((string-match "^\\s-*$" s)
6408 "")
6409 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
6410 (match-string 1 s))
6411 (t
6412 s)))
6413
6414 (defun erc-arrange-session-in-multiple-windows ()
6415 "Open a window for every non-server buffer related to `erc-session-server'.
6416
6417 All windows are opened in the current frame."
6418 (interactive)
6419 (unless erc-server-process
6420 (error "No erc-server-process found in current buffer"))
6421 (let ((bufs (erc-buffer-list nil erc-server-process)))
6422 (when bufs
6423 (delete-other-windows)
6424 (switch-to-buffer (car bufs))
6425 (setq bufs (cdr bufs))
6426 (while bufs
6427 (split-window)
6428 (other-window 1)
6429 (switch-to-buffer (car bufs))
6430 (setq bufs (cdr bufs))
6431 (balance-windows)))))
6432
6433 (defun erc-popup-input-buffer ()
6434 "Provide an input buffer."
6435 (interactive)
6436 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
6437 (mode (intern
6438 (completing-read
6439 "Mode: "
6440 (mapcar (lambda (e)
6441 (list (symbol-name e)))
6442 (apropos-internal "-mode$" 'commandp))
6443 nil t))))
6444 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
6445 (funcall mode)
6446 (narrow-to-region (point) (point))
6447 (shrink-window-if-larger-than-buffer)))
6448
6449 ;;; Message catalog
6450
6451 (defun erc-make-message-variable-name (catalog entry)
6452 "Create a variable name corresponding to CATALOG's ENTRY."
6453 (intern (concat "erc-message-"
6454 (symbol-name catalog) "-" (symbol-name entry))))
6455
6456 (defun erc-define-catalog-entry (catalog entry format-spec)
6457 "Set CATALOG's ENTRY to FORMAT-SPEC."
6458 (set (erc-make-message-variable-name catalog entry)
6459 format-spec))
6460
6461 (defun erc-define-catalog (catalog entries)
6462 "Define a CATALOG according to ENTRIES."
6463 (dolist (entry entries)
6464 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
6465
6466 (erc-define-catalog
6467 'english
6468 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
6469 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
6470 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
6471 (cannot-find-file . "Cannot find file %f")
6472 (cannot-read-file . "Cannot read file %f")
6473 (connect . "Connecting to %S:%p... ")
6474 (country . "%c")
6475 (country-unknown . "%d: No such domain")
6476 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
6477 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
6478 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
6479 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
6480 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
6481 (flood-strict-mode
6482 . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
6483 (disconnected . "\n\nConnection failed! Re-establishing connection...\n")
6484 (disconnected-noreconnect
6485 . "\n\nConnection failed! Not re-establishing connection.\n")
6486 (finished . "\n\n*** ERC finished ***\n")
6487 (terminated . "\n\n*** ERC terminated: %e\n")
6488 (login . "Logging in as \'%n\'...")
6489 (nick-in-use . "%n is in use. Choose new nickname: ")
6490 (nick-too-long
6491 . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
6492 (no-default-channel . "No default channel")
6493 (no-invitation . "You've got no invitation")
6494 (no-target . "No target")
6495 (ops . "%i operator%s: %o")
6496 (ops-none . "No operators in this channel.")
6497 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
6498 (variable-not-bound . "Variable not bound!")
6499 (ACTION . "* %n %a")
6500 (CTCP-CLIENTINFO . "Client info for %n: %m")
6501 (CTCP-ECHO . "Echo %n: %m")
6502 (CTCP-FINGER . "Finger info for %n: %m")
6503 (CTCP-PING . "Ping time to %n is %t")
6504 (CTCP-TIME . "Time by %n is %m")
6505 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
6506 (CTCP-VERSION . "Version for %n is %m")
6507 (ERROR . "==> ERROR from %s: %c\n")
6508 (INVITE . "%n (%u@%h) invites you to channel %c")
6509 (JOIN . "%n (%u@%h) has joined channel %c")
6510 (JOIN-you . "You have joined channel %c")
6511 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
6512 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
6513 (KICK-by-you . "You have kicked %k off channel %c: %r")
6514 (MODE . "%n (%u@%h) has changed mode for %t to %m")
6515 (MODE-nick . "%n has changed mode for %t to %m")
6516 (NICK . "%n (%u@%h) is now known as %N")
6517 (NICK-you . "Your new nickname is %N")
6518 (PART . erc-message-english-PART)
6519 (PING . "PING from server (last: %s sec. ago)")
6520 (PONG . "PONG from %h (%i second%s)")
6521 (QUIT . "%n (%u@%h) has quit: %r")
6522 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
6523 (WALLOPS . "Wallops from %n: %m")
6524 (s004 . "%s %v %U %C")
6525 (s221 . "User modes for %n: %m")
6526 (s252 . "%i operator(s) online")
6527 (s253 . "%i unknown connection(s)")
6528 (s254 . "%i channels formed")
6529 (s275 . "%n %m")
6530 (s301 . "%n is AWAY: %r")
6531 (s303 . "Is online: %n")
6532 (s305 . "%m")
6533 (s306 . "%m")
6534 (s307 . "%n %m")
6535 (s311 . "%n is %f (%u@%h)")
6536 (s312 . "%n is/was on server %s (%c)")
6537 (s313 . "%n is an IRC operator")
6538 (s314 . "%n was %f (%u@%h)")
6539 (s317 . "%n has been idle for %i")
6540 (s317-on-since . "%n has been idle for %i, on since %t")
6541 (s319 . "%n is on channel(s): %c")
6542 (s320 . "%n is an identified user")
6543 (s321 . "Channel Users Topic")
6544 (s322 . "%c [%u] %t")
6545 (s324 . "%c modes: %m")
6546 (s328 . "%c URL: %u")
6547 (s329 . "%c was created on %t")
6548 (s330 . "%n %a %i")
6549 (s331 . "No topic is set for %c")
6550 (s332 . "Topic for %c: %T")
6551 (s333 . "%c: topic set by %n, %t")
6552 (s341 . "Inviting %n to channel %c")
6553 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
6554 (s353 . "Users on %c: %u")
6555 (s367 . "Ban for %b on %c")
6556 (s367-set-by . "Ban for %b on %c set by %s on %t")
6557 (s368 . "Banlist of %c ends.")
6558 (s379 . "%c: Forwarded to %f")
6559 (s391 . "The time at %s is %t")
6560 (s401 . "%n: No such nick/channel")
6561 (s403 . "%c: No such channel")
6562 (s404 . "%c: Cannot send to channel")
6563 (s405 . "%c: You have joined too many channels")
6564 (s406 . "%n: There was no such nickname")
6565 (s412 . "No text to send")
6566 (s421 . "%c: Unknown command")
6567 (s431 . "No nickname given")
6568 (s432 . "%n is an erroneous nickname")
6569 (s442 . "%c: You're not on that channel")
6570 (s445 . "SUMMON has been disabled")
6571 (s446 . "USERS has been disabled")
6572 (s451 . "You have not registered")
6573 (s461 . "%c: not enough parameters")
6574 (s462 . "Unauthorized command (already registered)")
6575 (s463 . "Your host isn't among the privileged")
6576 (s464 . "Password incorrect")
6577 (s465 . "You are banned from this server")
6578 (s474 . "You can't join %c because you're banned (+b)")
6579 (s475 . "You must specify the correct channel key (+k) to join %c")
6580 (s481 . "Permission Denied - You're not an IRC operator")
6581 (s482 . "You need to be a channel operator of %c to do that")
6582 (s483 . "You can't kill a server!")
6583 (s484 . "Your connection is restricted!")
6584 (s485 . "You're not the original channel operator")
6585 (s491 . "No O-lines for your host")
6586 (s501 . "Unknown MODE flag")
6587 (s502 . "You can't change modes for other users")
6588 (s671 . "%n %a")))
6589
6590 (defun erc-message-english-PART (&rest args)
6591 "Format a proper PART message.
6592
6593 This function is an example on what could be done with formatting
6594 functions."
6595 (let ((nick (cadr (memq ?n args)))
6596 (user (cadr (memq ?u args)))
6597 (host (cadr (memq ?h args)))
6598 (channel (cadr (memq ?c args)))
6599 (reason (cadr (memq ?r args))))
6600 (if (string= nick (erc-current-nick))
6601 (format "You have left channel %s" channel)
6602 (format "%s (%s@%s) has left channel %s%s"
6603 nick user host channel
6604 (if (not (string= reason ""))
6605 (format ": %s"
6606 (erc-replace-regexp-in-string "%" "%%" reason))
6607 "")))))
6608
6609
6610 (defvar erc-current-message-catalog 'english)
6611 (make-variable-buffer-local 'erc-current-message-catalog)
6612
6613 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6614 "Retrieve ENTRY from CATALOG.
6615
6616 If CATALOG is nil, `erc-current-message-catalog' is used.
6617
6618 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6619 english, catalog."
6620 (unless catalog (setq catalog erc-current-message-catalog))
6621 (let ((var (erc-make-message-variable-name catalog entry)))
6622 (if (boundp var)
6623 (symbol-value var)
6624 (when (boundp (erc-make-message-variable-name 'english entry))
6625 (symbol-value (erc-make-message-variable-name 'english entry))))))
6626
6627 (defun erc-format-message (msg &rest args)
6628 "Format MSG according to ARGS.
6629
6630 See also `format-spec'."
6631 (when (eq (logand (length args) 1) 1) ; oddp
6632 (error "Obscure usage of this function appeared"))
6633 (let ((entry (erc-retrieve-catalog-entry msg)))
6634 (when (not entry)
6635 (error "No format spec for message %s" msg))
6636 (when (functionp entry)
6637 (setq entry (apply entry args)))
6638 (format-spec entry (apply 'format-spec-make args))))
6639
6640 ;;; Various hook functions
6641
6642 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6643
6644 (defcustom erc-kill-server-hook '(erc-kill-server)
6645 "Invoked whenever a server buffer is killed via `kill-buffer'."
6646 :group 'erc-hooks
6647 :type 'hook)
6648
6649 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6650 "Invoked whenever a channel-buffer is killed via `kill-buffer'."
6651 :group 'erc-hooks
6652 :type 'hook)
6653
6654 (defcustom erc-kill-buffer-hook nil
6655 "Hook run whenever a non-server or channel buffer is killed.
6656
6657 See also `kill-buffer'."
6658 :group 'erc-hooks
6659 :type 'hook)
6660
6661 (defun erc-kill-buffer-function ()
6662 "Function to call when an ERC buffer is killed.
6663 This function should be on `kill-buffer-hook'.
6664 When the current buffer is in `erc-mode', this function will run
6665 one of the following hooks:
6666 `erc-kill-server-hook' if the server buffer was killed,
6667 `erc-kill-channel-hook' if a channel buffer was killed,
6668 or `erc-kill-buffer-hook' if any other buffer."
6669 (when (eq major-mode 'erc-mode)
6670 (erc-remove-channel-users)
6671 (cond
6672 ((eq (erc-server-buffer) (current-buffer))
6673 (run-hooks 'erc-kill-server-hook))
6674 ((erc-channel-p (erc-default-target))
6675 (run-hooks 'erc-kill-channel-hook))
6676 (t
6677 (run-hooks 'erc-kill-buffer-hook)))))
6678
6679 (defun erc-kill-server ()
6680 "Sends a QUIT command to the server when the server buffer is killed.
6681 This function should be on `erc-kill-server-hook'."
6682 (when (erc-server-process-alive)
6683 (setq erc-server-quitting t)
6684 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6685
6686 (defun erc-kill-channel ()
6687 "Sends a PART command to the server when the channel buffer is killed.
6688 This function should be on `erc-kill-channel-hook'."
6689 (when (erc-server-process-alive)
6690 (let ((tgt (erc-default-target)))
6691 (erc-server-send (format "PART %s :%s" tgt
6692 (funcall erc-part-reason nil))
6693 nil tgt))))
6694
6695 ;;; Dealing with `erc-parsed'
6696
6697 (defun erc-find-parsed-property ()
6698 "Find the next occurrence of the `erc-parsed' text property."
6699 (text-property-not-all (point-min) (point-max) 'erc-parsed nil))
6700
6701 (defun erc-restore-text-properties ()
6702 "Restore the property 'erc-parsed for the region."
6703 (let ((parsed-posn (erc-find-parsed-property)))
6704 (put-text-property
6705 (point-min) (point-max)
6706 'erc-parsed (when parsed-posn (erc-get-parsed-vector parsed-posn)))))
6707
6708 (defun erc-get-parsed-vector (point)
6709 "Return the whole parsed vector on POINT."
6710 (get-text-property point 'erc-parsed))
6711
6712 (defun erc-get-parsed-vector-nick (vect)
6713 "Return nickname in the parsed vector VECT."
6714 (let* ((untreated-nick (and vect (erc-response.sender vect)))
6715 (maybe-nick (when untreated-nick
6716 (car (split-string untreated-nick "!")))))
6717 (when (and (not (null maybe-nick))
6718 (erc-is-valid-nick-p maybe-nick))
6719 untreated-nick)))
6720
6721 (defun erc-get-parsed-vector-type (vect)
6722 "Return message type in the parsed vector VECT."
6723 (and vect
6724 (erc-response.command vect)))
6725
6726 ;; Teach url.el how to open irc:// URLs with ERC.
6727 ;; To activate, customize `url-irc-function' to `url-irc-erc'.
6728
6729 ;;;###autoload
6730 (defun erc-handle-irc-url (host port channel user password)
6731 "Use ERC to IRC on HOST:PORT in CHANNEL as USER with PASSWORD.
6732 If ERC is already connected to HOST:PORT, simply /join CHANNEL.
6733 Otherwise, connect to HOST:PORT as USER and /join CHANNEL."
6734 (let ((server-buffer
6735 (car (erc-buffer-filter
6736 (lambda ()
6737 (and (string-equal erc-session-server host)
6738 (= erc-session-port port)
6739 (erc-open-server-buffer-p)))))))
6740 (with-current-buffer (or server-buffer (current-buffer))
6741 (if (and server-buffer channel)
6742 (erc-cmd-JOIN channel)
6743 (erc-open host port (or user (erc-compute-nick)) (erc-compute-full-name)
6744 (not server-buffer) password nil channel
6745 (when server-buffer
6746 (get-buffer-process server-buffer)))))))
6747
6748 (provide 'erc)
6749
6750 ;; Deprecated. We might eventually stop requiring the goodies automatically.
6751 ;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6752 ;; avoid a recursive require error when byte-compiling the entire package.
6753 (require 'erc-goodies)
6754
6755 ;;; erc.el ends here
6756 ;;
6757 ;; Local Variables:
6758 ;; outline-regexp: ";;+"
6759 ;; indent-tabs-mode: t
6760 ;; tab-width: 8
6761 ;; End: