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