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