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