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