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