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