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