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