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