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