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