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