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