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