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