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