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