Add 2011 to FSF/AIST copyright years.
[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, 2010, 2011 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 (with-current-buffer (or buffer (process-buffer erc-server-process))
2362 (let ((insert-position (or (marker-position erc-insert-marker)
2363 (point-max))))
2364 (let ((string string) ;; FIXME! Can this be removed?
2365 (buffer-undo-list t)
2366 (inhibit-read-only t))
2367 (unless (string-match "\n$" string)
2368 (setq string (concat string "\n"))
2369 (when (erc-string-invisible-p string)
2370 (erc-put-text-properties 0 (length string)
2371 '(invisible intangible) string)))
2372 (erc-log (concat "erc-display-line: " string
2373 (format "(%S)" string) " in buffer "
2374 (format "%s" buffer)))
2375 (setq erc-insert-this t)
2376 (run-hook-with-args 'erc-insert-pre-hook string)
2377 (if (null erc-insert-this)
2378 ;; Leave erc-insert-this set to t as much as possible. Fran
2379 ;; Litterio <franl> has seen erc-insert-this set to nil while
2380 ;; erc-send-pre-hook is running, which should never happen. This
2381 ;; may cure it.
2382 (setq erc-insert-this t)
2383 (save-excursion ;; to restore point in the new buffer
2384 (save-restriction
2385 (widen)
2386 (goto-char insert-position)
2387 (insert-before-markers string)
2388 ;; run insertion hook, with point at restored location
2389 (save-restriction
2390 (narrow-to-region insert-position (point))
2391 (run-hooks 'erc-insert-modify-hook)
2392 (run-hooks 'erc-insert-post-hook)
2393 (when erc-remove-parsed-property
2394 (remove-text-properties (point-min) (point-max)
2395 '(erc-parsed nil))))))))
2396 (erc-update-undo-list (- (or (marker-position erc-insert-marker)
2397 (point-max))
2398 insert-position))))))
2399
2400 (defun erc-update-undo-list (shift)
2401 ;; Translate buffer positions in buffer-undo-list by SHIFT.
2402 (unless (or (zerop shift) (atom buffer-undo-list))
2403 (let ((list buffer-undo-list) elt)
2404 (while list
2405 (setq elt (car list))
2406 (cond ((integerp elt) ; POSITION
2407 (incf (car list) shift))
2408 ((or (atom elt) ; nil, EXTENT
2409 ;; (eq t (car elt)) ; (t HIGH . LOW)
2410 (markerp (car elt))) ; (MARKER . DISTANCE)
2411 nil)
2412 ((integerp (car elt)) ; (BEGIN . END)
2413 (incf (car elt) shift)
2414 (incf (cdr elt) shift))
2415 ((stringp (car elt)) ; (TEXT . POSITION)
2416 (incf (cdr elt) (* (if (natnump (cdr elt)) 1 -1) shift)))
2417 ((null (car elt)) ; (nil PROPERTY VALUE BEG . END)
2418 (let ((cons (nthcdr 3 elt)))
2419 (incf (car cons) shift)
2420 (incf (cdr cons) shift)))
2421 ((and (featurep 'xemacs)
2422 (extentp (car elt))) ; (EXTENT START END)
2423 (incf (nth 1 elt) shift)
2424 (incf (nth 2 elt) shift)))
2425 (setq list (cdr list))))))
2426
2427 (defvar erc-valid-nick-regexp "[]a-zA-Z^[;\\`_{}|][]^[;\\`_{}|a-zA-Z0-9-]*"
2428 "Regexp which matches all valid characters in a IRC nickname.")
2429
2430 (defun erc-is-valid-nick-p (nick)
2431 "Check if NICK is a valid IRC nickname."
2432 (string-match (concat "^" erc-valid-nick-regexp "$") nick))
2433
2434 (defun erc-display-line (string &optional buffer)
2435 "Display STRING in the ERC BUFFER.
2436 All screen output must be done through this function. If BUFFER is nil
2437 or omitted, the default ERC buffer for the `erc-session-server' is used.
2438 The BUFFER can be an actual buffer, a list of buffers, 'all or 'active.
2439 If BUFFER = 'all, the string is displayed in all the ERC buffers for the
2440 current session. 'active means the current active buffer
2441 \(`erc-active-buffer'). If the buffer can't be resolved, the current
2442 buffer is used. `erc-display-line-1' is used to display STRING.
2443
2444 If STRING is nil, the function does nothing."
2445 (let ((inhibit-point-motion-hooks t)
2446 new-bufs)
2447 (dolist (buf (cond
2448 ((bufferp buffer) (list buffer))
2449 ((listp buffer) buffer)
2450 ((processp buffer) (list (process-buffer buffer)))
2451 ((eq 'all buffer)
2452 ;; Hmm, or all of the same session server?
2453 (erc-buffer-list nil erc-server-process))
2454 ((and (eq 'active buffer) (erc-active-buffer))
2455 (list (erc-active-buffer)))
2456 ((erc-server-buffer-live-p)
2457 (list (process-buffer erc-server-process)))
2458 (t (list (current-buffer)))))
2459 (when (buffer-live-p buf)
2460 (erc-display-line-1 string buf)
2461 (add-to-list 'new-bufs buf)))
2462 (when (null new-bufs)
2463 (if (erc-server-buffer-live-p)
2464 (erc-display-line-1 string (process-buffer erc-server-process))
2465 (erc-display-line-1 string (current-buffer))))))
2466
2467 (defun erc-display-message-highlight (type string)
2468 "Highlight STRING according to TYPE, where erc-TYPE-face is an ERC face.
2469
2470 See also `erc-make-notice'."
2471 (cond ((eq type 'notice)
2472 (erc-make-notice string))
2473 (t
2474 (erc-put-text-property
2475 0 (length string)
2476 'face (or (intern-soft
2477 (concat "erc-" (symbol-name type) "-face"))
2478 "erc-default-face")
2479 string)
2480 string)))
2481
2482 (defun erc-display-message (parsed type buffer msg &rest args)
2483 "Display MSG in BUFFER.
2484
2485 ARGS, PARSED, and TYPE are used to format MSG sensibly.
2486
2487 See also `erc-format-message' and `erc-display-line'."
2488 (let ((string (if (symbolp msg)
2489 (apply 'erc-format-message msg args)
2490 msg)))
2491 (setq string
2492 (cond
2493 ((null type)
2494 string)
2495 ((listp type)
2496 (mapc (lambda (type)
2497 (setq string
2498 (erc-display-message-highlight type string)))
2499 type)
2500 string)
2501 ((symbolp type)
2502 (erc-display-message-highlight type string))))
2503
2504 (if (not (erc-response-p parsed))
2505 (erc-display-line string buffer)
2506 (unless (member (erc-response.command parsed) erc-hide-list)
2507 (erc-put-text-property 0 (length string) 'erc-parsed parsed string)
2508 (erc-put-text-property 0 (length string) 'rear-sticky t string)
2509 (erc-display-line string buffer)))))
2510
2511 (defun erc-message-type-member (position list)
2512 "Return non-nil if the erc-parsed text-property at POSITION is in LIST.
2513
2514 This function relies on the erc-parsed text-property being
2515 present."
2516 (let ((prop-val (erc-get-parsed-vector position)))
2517 (and prop-val (member (erc-response.command prop-val) list))))
2518
2519 (defvar erc-send-input-line-function 'erc-send-input-line)
2520 (make-variable-buffer-local 'erc-send-input-line-function)
2521
2522 (defun erc-send-input-line (target line &optional force)
2523 "Send LINE to TARGET.
2524
2525 See also `erc-server-send'."
2526 (setq line (format "PRIVMSG %s :%s"
2527 target
2528 ;; If the line is empty, we still want to
2529 ;; send it - i.e. an empty pasted line.
2530 (if (string= line "\n")
2531 " \n"
2532 line)))
2533 (erc-server-send line force target))
2534
2535 (defun erc-get-arglist (fun)
2536 "Return the argument list of a function without the parens."
2537 (let ((arglist (format "%S" (erc-function-arglist fun))))
2538 (if (string-match "^(\\(.*\\))$" arglist)
2539 (match-string 1 arglist)
2540 arglist)))
2541
2542 (defun erc-command-no-process-p (str)
2543 "Return non-nil if STR is an ERC command that can be run when the process
2544 is not alive, nil otherwise."
2545 (let ((fun (erc-extract-command-from-line str)))
2546 (and fun
2547 (symbolp (car fun))
2548 (get (car fun) 'process-not-needed))))
2549
2550 (defun erc-command-name (cmd)
2551 "For CMD being the function name of a ERC command, something like
2552 erc-cmd-FOO, this returns a string /FOO."
2553 (let ((command-name (symbol-name cmd)))
2554 (if (string-match "^erc-cmd-\\(.*\\)$" command-name)
2555 (concat "/" (match-string 1 command-name))
2556 command-name)))
2557
2558 (defun erc-process-input-line (line &optional force no-command)
2559 "Translate LINE to an RFC1459 command and send it based.
2560 Returns non-nil if the command is actually sent to the server, and nil
2561 otherwise.
2562
2563 If the command in the LINE is not bound as a function `erc-cmd-<COMMAND>',
2564 it is passed to `erc-cmd-default'. If LINE is not a command (i.e. doesn't
2565 start with /<COMMAND>) then it is sent as a message.
2566
2567 An optional FORCE argument forces sending the line when flood
2568 protection is in effect. The optional NO-COMMAND argument prohibits
2569 this function from interpreting the line as a command."
2570 (let ((command-list (erc-extract-command-from-line line)))
2571 (if (and command-list
2572 (not no-command))
2573 (let* ((cmd (nth 0 command-list))
2574 (args (nth 1 command-list)))
2575 (condition-case nil
2576 (if (listp args)
2577 (apply cmd args)
2578 (funcall cmd args))
2579 (wrong-number-of-arguments
2580 (erc-display-message nil 'error (current-buffer) 'incorrect-args
2581 ?c (erc-command-name cmd)
2582 ?u (or (erc-get-arglist cmd)
2583 "")
2584 ?d (format "%s\n"
2585 (or (documentation cmd) "")))
2586 nil)))
2587 (let ((r (erc-default-target)))
2588 (if r
2589 (funcall erc-send-input-line-function r line force)
2590 (erc-display-message nil 'error (current-buffer) 'no-target)
2591 nil)))))
2592
2593 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2594 ;; Input commands handlers
2595 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2596
2597 (defun erc-cmd-AMSG (line)
2598 "Send LINE to all channels of the current server that you are on."
2599 (interactive "sSend to all channels you're on: ")
2600 (setq line (erc-trim-string line))
2601 (erc-with-all-buffers-of-server nil
2602 (lambda ()
2603 (erc-channel-p (erc-default-target)))
2604 (erc-send-message line)))
2605 (put 'erc-cmd-AMSG 'do-not-parse-args t)
2606
2607 (defun erc-cmd-SAY (line)
2608 "Send LINE to the current query or channel as a message, not a command.
2609
2610 Use this when you want to send a message with a leading '/'. Note
2611 that since multi-line messages are never a command, you don't
2612 need this when pasting multiple lines of text."
2613 (if (string-match "^\\s-*$" line)
2614 nil
2615 (string-match "^ ?\\(.*\\)" line)
2616 (erc-process-input-line (match-string 1 line) nil t)))
2617 (put 'erc-cmd-SAY 'do-not-parse-args t)
2618
2619 (defun erc-cmd-SET (line)
2620 "Set the variable named by the first word in LINE to some VALUE.
2621 VALUE is computed by evaluating the rest of LINE in Lisp."
2622 (cond
2623 ((string-match "^\\s-*\\(\\S-+\\)\\s-+\\(.*\\)$" line)
2624 (let ((var (read (concat "erc-" (match-string 1 line))))
2625 (val (read (match-string 2 line))))
2626 (if (boundp var)
2627 (progn
2628 (set var (eval val))
2629 (erc-display-message
2630 nil nil 'active (format "Set %S to %S" var val))
2631 t)
2632 (setq var (read (match-string 1 line)))
2633 (if (boundp var)
2634 (progn
2635 (set var (eval val))
2636 (erc-display-message
2637 nil nil 'active (format "Set %S to %S" var val))
2638 t)
2639 (erc-display-message nil 'error 'active 'variable-not-bound)
2640 nil))))
2641 ((string-match "^\\s-*$" line)
2642 (erc-display-line
2643 (concat "Available user variables:\n"
2644 (apply
2645 'concat
2646 (mapcar
2647 (lambda (var)
2648 (let ((val (symbol-value var)))
2649 (concat (format "%S:" var)
2650 (if (consp val)
2651 (concat "\n" (pp-to-string val))
2652 (format " %S\n" val)))))
2653 (apropos-internal "^erc-" 'user-variable-p))))
2654 (current-buffer)) t)
2655 (t nil)))
2656 (defalias 'erc-cmd-VAR 'erc-cmd-SET)
2657 (defalias 'erc-cmd-VARIABLE 'erc-cmd-SET)
2658 (put 'erc-cmd-SET 'do-not-parse-args t)
2659 (put 'erc-cmd-SET 'process-not-needed t)
2660
2661 (defun erc-cmd-default (line)
2662 "Fallback command.
2663
2664 Commands for which no erc-cmd-xxx exists, are tunnelled through
2665 this function. LINE is sent to the server verbatim, and
2666 therefore has to contain the command itself as well."
2667 (erc-log (format "cmd: DEFAULT: %s" line))
2668 (erc-server-send (substring line 1))
2669 t)
2670
2671 (defun erc-cmd-IGNORE (&optional user)
2672 "Ignore USER. This should be a regexp matching nick!user@host.
2673 If no USER argument is specified, list the contents of `erc-ignore-list'."
2674 (if user
2675 (let ((quoted (regexp-quote user)))
2676 (when (and (not (string= user quoted))
2677 (y-or-n-p (format "Use regexp-quoted form (%s) instead? "
2678 quoted)))
2679 (setq user quoted))
2680 (erc-display-line
2681 (erc-make-notice (format "Now ignoring %s" user))
2682 'active)
2683 (erc-with-server-buffer (add-to-list 'erc-ignore-list user)))
2684 (if (null (erc-with-server-buffer erc-ignore-list))
2685 (erc-display-line (erc-make-notice "Ignore list is empty") 'active)
2686 (erc-display-line (erc-make-notice "Ignore list:") 'active)
2687 (mapc #'(lambda (item)
2688 (erc-display-line (erc-make-notice item)
2689 'active))
2690 (erc-with-server-buffer erc-ignore-list))))
2691 t)
2692
2693 (defun erc-cmd-UNIGNORE (user)
2694 "Remove the user specified in USER from the ignore list."
2695 (let ((ignored-nick (car (erc-with-server-buffer
2696 (erc-member-ignore-case (regexp-quote user)
2697 erc-ignore-list)))))
2698 (unless ignored-nick
2699 (if (setq ignored-nick (erc-ignored-user-p user))
2700 (unless (y-or-n-p (format "Remove this regexp (%s)? "
2701 ignored-nick))
2702 (setq ignored-nick nil))
2703 (erc-display-line
2704 (erc-make-notice (format "%s is not currently ignored!" user))
2705 'active)))
2706 (when ignored-nick
2707 (erc-display-line
2708 (erc-make-notice (format "No longer ignoring %s" user))
2709 'active)
2710 (erc-with-server-buffer
2711 (setq erc-ignore-list (delete ignored-nick erc-ignore-list)))))
2712 t)
2713
2714 (defun erc-cmd-CLEAR ()
2715 "Clear the window content."
2716 (recenter 0)
2717 t)
2718 (put 'erc-cmd-CLEAR 'process-not-needed t)
2719
2720 (defun erc-cmd-OPS ()
2721 "Show the ops in the current channel."
2722 (interactive)
2723 (let ((ops nil))
2724 (if erc-channel-users
2725 (maphash (lambda (nick user-data)
2726 (let ((cuser (cdr user-data)))
2727 (if (and cuser
2728 (erc-channel-user-op cuser))
2729 (setq ops (cons (erc-server-user-nickname
2730 (car user-data))
2731 ops)))))
2732 erc-channel-users))
2733 (setq ops (sort ops 'string-lessp))
2734 (if ops
2735 (erc-display-message
2736 nil 'notice (current-buffer) 'ops
2737 ?i (length ops) ?s (if (> (length ops) 1) "s" "")
2738 ?o (mapconcat 'identity ops " "))
2739 (erc-display-message nil 'notice (current-buffer) 'ops-none)))
2740 t)
2741
2742 (defun erc-cmd-COUNTRY (tld)
2743 "Display the country associated with the top level domain TLD."
2744 (require 'mail-extr)
2745 (let ((co (ignore-errors (what-domain tld))))
2746 (if co
2747 (erc-display-message
2748 nil 'notice 'active 'country ?c co ?d tld)
2749 (erc-display-message
2750 nil 'notice 'active 'country-unknown ?d tld))
2751 t))
2752 (put 'erc-cmd-COUNTRY 'process-not-needed t)
2753
2754 (defun erc-cmd-AWAY (line)
2755 "Mark the user as being away, the reason being indicated by LINE.
2756 If no reason is given, unset away status."
2757 (when (string-match "^\\s-*\\(.*\\)$" line)
2758 (let ((reason (match-string 1 line)))
2759 (erc-log (format "cmd: AWAY: %s" reason))
2760 (erc-server-send
2761 (if (string= reason "")
2762 "AWAY"
2763 (concat "AWAY :" reason))))
2764 t))
2765 (put 'erc-cmd-AWAY 'do-not-parse-args t)
2766
2767 (defun erc-cmd-GAWAY (line)
2768 "Mark the user as being away everywhere, the reason being indicated by LINE."
2769 ;; on all server buffers.
2770 (erc-with-all-buffers-of-server nil
2771 #'erc-open-server-buffer-p
2772 (erc-cmd-AWAY line)))
2773 (put 'erc-cmd-GAWAY 'do-not-parse-args t)
2774
2775 (defun erc-cmd-CTCP (nick cmd &rest args)
2776 "Send a Client To Client Protocol message to NICK.
2777
2778 CMD is the CTCP command, possible values being ECHO, FINGER, CLIENTINFO, TIME,
2779 VERSION and so on. It is called with ARGS."
2780 (let ((str (concat cmd
2781 (when args
2782 (concat " " (mapconcat #'identity args " "))))))
2783 (erc-log (format "cmd: CTCP [%s]: [%s]" nick str))
2784 (erc-send-ctcp-message nick str)
2785 t))
2786
2787 (defun erc-cmd-HELP (&optional func)
2788 "Popup help information.
2789
2790 If FUNC contains a valid function or variable, help about that
2791 will be displayed. If FUNC is empty, display an apropos about
2792 ERC commands. Otherwise, do `apropos' in the ERC namespace
2793 \(\"erc-.*LINE\"\).
2794
2795 Examples:
2796 To find out about erc and bbdb, do
2797 /help bbdb.*
2798
2799 For help about the WHOIS command, do:
2800 /help whois
2801
2802 For a list of user commands (/join /part, ...):
2803 /help."
2804 (if func
2805 (let* ((sym (or (let ((sym (intern-soft
2806 (concat "erc-cmd-" (upcase func)))))
2807 (if (and sym (or (boundp sym) (fboundp sym)))
2808 sym
2809 nil))
2810 (let ((sym (intern-soft func)))
2811 (if (and sym (or (boundp sym) (fboundp sym)))
2812 sym
2813 nil))
2814 (let ((sym (intern-soft (concat "erc-" func))))
2815 (if (and sym (or (boundp sym) (fboundp sym)))
2816 sym
2817 nil)))))
2818 (if sym
2819 (cond
2820 ((boundp sym) (describe-variable sym))
2821 ((fboundp sym) (describe-function sym))
2822 (t nil))
2823 (apropos-command (concat "erc-.*" func) nil
2824 (lambda (x)
2825 (or (commandp x)
2826 (get x 'custom-type))))
2827 t))
2828 (apropos "erc-cmd-")
2829 (message "Type C-h m to get additional information about keybindings.")
2830 t))
2831
2832 (defalias 'erc-cmd-H 'erc-cmd-HELP)
2833 (put 'erc-cmd-HELP 'process-not-needed t)
2834
2835 (defun erc-cmd-JOIN (channel &optional key)
2836 "Join the channel given in CHANNEL, optionally with KEY.
2837 If CHANNEL is specified as \"-invite\", join the channel to which you
2838 were most recently invited. See also `invitation'."
2839 (let (chnl)
2840 (if (string= (upcase channel) "-INVITE")
2841 (if erc-invitation
2842 (setq chnl erc-invitation)
2843 (erc-display-message nil 'error (current-buffer) 'no-invitation))
2844 (setq chnl (erc-ensure-channel-name channel)))
2845 (when chnl
2846 ;; Prevent double joining of same channel on same server.
2847 (let ((joined-channels
2848 (mapcar #'(lambda (chanbuf)
2849 (with-current-buffer chanbuf (erc-default-target)))
2850 (erc-channel-list erc-server-process))))
2851 (if (erc-member-ignore-case chnl joined-channels)
2852 (switch-to-buffer (car (erc-member-ignore-case chnl
2853 joined-channels)))
2854 (erc-log (format "cmd: JOIN: %s" chnl))
2855 (if (and chnl key)
2856 (erc-server-send (format "JOIN %s %s" chnl key))
2857 (erc-server-send (format "JOIN %s" chnl)))))))
2858 t)
2859
2860 (defalias 'erc-cmd-CHANNEL 'erc-cmd-JOIN)
2861 (defalias 'erc-cmd-J 'erc-cmd-JOIN)
2862
2863 (defvar erc-channel-new-member-names nil
2864 "If non-nil, a names list is currently being received.
2865
2866 If non-nil, this variable is a hash-table that associates
2867 received nicks with t.")
2868 (make-variable-buffer-local 'erc-channel-new-member-names)
2869
2870 (defun erc-cmd-NAMES (&optional channel)
2871 "Display the users in CHANNEL.
2872 If CHANNEL is not specified, display the users in the current channel.
2873 This function clears the channel name list first, then sends the
2874 command."
2875 (let ((tgt (or (and (erc-channel-p channel) channel)
2876 (erc-default-target))))
2877 (if (and tgt (erc-channel-p tgt))
2878 (progn
2879 (erc-log (format "cmd: DEFAULT: NAMES %s" tgt))
2880 (erc-with-buffer
2881 (tgt)
2882 (erc-channel-begin-receiving-names))
2883 (erc-server-send (concat "NAMES " tgt)))
2884 (erc-display-message nil 'error (current-buffer) 'no-default-channel)))
2885 t)
2886 (defalias 'erc-cmd-N 'erc-cmd-NAMES)
2887
2888 (defun erc-cmd-KICK (target &optional reason-or-nick &rest reasonwords)
2889 "Kick the user indicated in LINE from the current channel.
2890 LINE has the format: \"#CHANNEL NICK REASON\" or \"NICK REASON\"."
2891 (let ((reasonstring (mapconcat 'identity reasonwords " ")))
2892 (if (string= "" reasonstring)
2893 (setq reasonstring (format "Kicked by %s" (erc-current-nick))))
2894 (if (erc-channel-p target)
2895 (let ((nick reason-or-nick))
2896 (erc-log (format "cmd: KICK: %s/%s: %s" nick target reasonstring))
2897 (erc-server-send (format "KICK %s %s :%s" target nick reasonstring)
2898 nil target)
2899 t)
2900 (when target
2901 (let ((ch (erc-default-target)))
2902 (setq reasonstring (concat
2903 (if reason-or-nick (concat reason-or-nick " "))
2904 reasonstring))
2905 (if ch
2906 (progn
2907 (erc-log
2908 (format "cmd: KICK: %s/%s: %s" target ch reasonstring))
2909 (erc-server-send
2910 (format "KICK %s %s :%s" ch target reasonstring) nil ch))
2911 (erc-display-message nil 'error (current-buffer)
2912 'no-default-channel))
2913 t)))))
2914
2915 (defvar erc-script-args nil)
2916
2917 (defun erc-cmd-LOAD (line)
2918 "Load the script provided in the LINE.
2919 If LINE continues beyond the file name, the rest of
2920 it is put in a (local) variable `erc-script-args',
2921 which can be used in Emacs Lisp scripts.
2922
2923 The optional FORCE argument is ignored here - you can't force loading
2924 a script after exceeding the flood threshold."
2925 (cond
2926 ((string-match "^\\s-*\\(\\S-+\\)\\(.*\\)$" line)
2927 (let* ((file-to-find (match-string 1 line))
2928 (erc-script-args (match-string 2 line))
2929 (file (erc-find-file file-to-find erc-script-path)))
2930 (erc-log (format "cmd: LOAD: %s" file-to-find))
2931 (cond
2932 ((not file)
2933 (erc-display-message nil 'error (current-buffer)
2934 'cannot-find-file ?f file-to-find))
2935 ((not (file-readable-p file))
2936 (erc-display-message nil 'error (current-buffer)
2937 'cannot-read-file ?f file))
2938 (t
2939 (message "Loading \'%s\'..." file)
2940 (erc-load-script file)
2941 (message "Loading \'%s\'...done" file))))
2942 t)
2943 (t nil)))
2944
2945 (defun erc-cmd-WHOIS (user &optional server)
2946 "Display whois information for USER.
2947
2948 If SERVER is non-nil, use that, rather than the current server."
2949 ;; FIXME: is the above docstring correct? -- Lawrence 2004-01-08
2950 (let ((send (if server
2951 (format "WHOIS %s %s" user server)
2952 (format "WHOIS %s" user))))
2953 (erc-log (format "cmd: %s" send))
2954 (erc-server-send send)
2955 t))
2956 (defalias 'erc-cmd-WI 'erc-cmd-WHOIS)
2957
2958 (defun erc-cmd-WHOAMI ()
2959 "Display whois information about yourself."
2960 (erc-cmd-WHOIS (erc-current-nick))
2961 t)
2962
2963 (defun erc-cmd-IDLE (nick)
2964 "Show the length of time NICK has been idle."
2965 (let ((origbuf (current-buffer))
2966 symlist)
2967 (erc-with-server-buffer
2968 (add-to-list 'symlist
2969 (cons (erc-once-with-server-event
2970 311 `(string= ,nick
2971 (second
2972 (erc-response.command-args parsed))))
2973 'erc-server-311-functions))
2974 (add-to-list 'symlist
2975 (cons (erc-once-with-server-event
2976 312 `(string= ,nick
2977 (second
2978 (erc-response.command-args parsed))))
2979 'erc-server-312-functions))
2980 (add-to-list 'symlist
2981 (cons (erc-once-with-server-event
2982 318 `(string= ,nick
2983 (second
2984 (erc-response.command-args parsed))))
2985 'erc-server-318-functions))
2986 (add-to-list 'symlist
2987 (cons (erc-once-with-server-event
2988 319 `(string= ,nick
2989 (second
2990 (erc-response.command-args parsed))))
2991 'erc-server-319-functions))
2992 (add-to-list 'symlist
2993 (cons (erc-once-with-server-event
2994 320 `(string= ,nick
2995 (second
2996 (erc-response.command-args parsed))))
2997 'erc-server-320-functions))
2998 (add-to-list 'symlist
2999 (cons (erc-once-with-server-event
3000 330 `(string= ,nick
3001 (second
3002 (erc-response.command-args parsed))))
3003 'erc-server-330-functions))
3004 (add-to-list 'symlist
3005 (cons (erc-once-with-server-event
3006 317
3007 `(let ((idleseconds
3008 (string-to-number
3009 (third
3010 (erc-response.command-args parsed)))))
3011 (erc-display-line
3012 (erc-make-notice
3013 (format "%s has been idle for %s."
3014 (erc-string-no-properties ,nick)
3015 (erc-seconds-to-string idleseconds)))
3016 ,origbuf))
3017 t)
3018 'erc-server-317-functions))
3019
3020 ;; Send the WHOIS command.
3021 (erc-cmd-WHOIS nick)
3022
3023 ;; Remove the uninterned symbols from the server hooks that did not run.
3024 (run-at-time 20 nil `(lambda ()
3025 (with-current-buffer ,(current-buffer)
3026 (dolist (sym ',symlist)
3027 (let ((hooksym (cdr sym))
3028 (funcsym (car sym)))
3029 (remove-hook hooksym funcsym t))))))))
3030 t)
3031
3032 (defun erc-cmd-DESCRIBE (line)
3033 "Pose some action to a certain user.
3034 LINE has the format \"USER ACTION\"."
3035 (cond
3036 ((string-match
3037 "^\\s-*\\(\\S-+\\)\\s-\\(.*\\)$" line)
3038 (let ((dst (match-string 1 line))
3039 (s (match-string 2 line)))
3040 (erc-log (format "cmd: DESCRIBE: [%s] %s" dst s))
3041 (erc-send-action dst s))
3042 t)
3043 (t nil)))
3044 (put 'erc-cmd-DESCRIBE 'do-not-parse-args t)
3045
3046 (defun erc-cmd-ME (line)
3047 "Send LINE as an action."
3048 (cond
3049 ((string-match "^\\s-\\(.*\\)$" line)
3050 (let ((s (match-string 1 line)))
3051 (erc-log (format "cmd: ME: %s" s))
3052 (erc-send-action (erc-default-target) s))
3053 t)
3054 (t nil)))
3055 (put 'erc-cmd-ME 'do-not-parse-args t)
3056
3057 (defun erc-cmd-ME\'S (line)
3058 "Do a /ME command, but add the string \" 's\" to the beginning."
3059 (erc-cmd-ME (concat " 's" line)))
3060 (put 'erc-cmd-ME\'S 'do-not-parse-args t)
3061
3062 (defun erc-cmd-LASTLOG (line)
3063 "Show all lines in the current buffer matching the regexp LINE.
3064
3065 If a match spreads across multiple lines, all those lines are shown.
3066
3067 The lines are shown in a buffer named `*Occur*'.
3068 It serves as a menu to find any of the occurrences in this buffer.
3069 \\[describe-mode] in that buffer will explain how.
3070
3071 If LINE contains upper case characters (excluding those preceded by `\'),
3072 the matching is case-sensitive."
3073 (occur line)
3074 t)
3075 (put 'erc-cmd-LASTLOG 'do-not-parse-args t)
3076 (put 'erc-cmd-LASTLOG 'process-not-needed t)
3077
3078 (defun erc-send-message (line &optional force)
3079 "Send LINE to the current channel or user and display it.
3080
3081 See also `erc-message' and `erc-display-line'."
3082 (erc-message "PRIVMSG" (concat (erc-default-target) " " line) force)
3083 (erc-display-line
3084 (concat (erc-format-my-nick) line)
3085 (current-buffer))
3086 ;; FIXME - treat multiline, run hooks, or remove me?
3087 t)
3088
3089 (defun erc-cmd-MODE (line)
3090 "Change or display the mode value of a channel or user.
3091 The first word specifies the target. The rest is the mode string
3092 to send.
3093
3094 If only one word is given, display the mode of that target.
3095
3096 A list of valid mode strings for Freenode may be found at
3097 URL `http://freenode.net/using_the_network.shtml'."
3098 (cond
3099 ((string-match "^\\s-\\(.*\\)$" line)
3100 (let ((s (match-string 1 line)))
3101 (erc-log (format "cmd: MODE: %s" s))
3102 (erc-server-send (concat "MODE " line)))
3103 t)
3104 (t nil)))
3105 (put 'erc-cmd-MODE 'do-not-parse-args t)
3106
3107 (defun erc-cmd-NOTICE (channel-or-user &rest message)
3108 "Send a notice to the channel or user given as the first word.
3109 The rest is the message to send."
3110 (erc-message "NOTICE" (concat channel-or-user " "
3111 (mapconcat #'identity message " "))))
3112
3113 (defun erc-cmd-MSG (line)
3114 "Send a message to the channel or user given as the first word in LINE.
3115
3116 The rest of LINE is the message to send."
3117 (erc-message "PRIVMSG" line))
3118
3119 (defalias 'erc-cmd-M 'erc-cmd-MSG)
3120 (put 'erc-cmd-MSG 'do-not-parse-args t)
3121
3122 (defun erc-cmd-SQUERY (line)
3123 "Send a Service Query to the service given as the first word in LINE.
3124
3125 The rest of LINE is the message to send."
3126 (erc-message "SQUERY" line))
3127
3128 (defun erc-cmd-NICK (nick)
3129 "Change current nickname to NICK."
3130 (erc-log (format "cmd: NICK: %s (erc-bad-nick: %S)" nick erc-bad-nick))
3131 (let ((nicklen (cdr (assoc "NICKLEN" (erc-with-server-buffer
3132 erc-server-parameters)))))
3133 (and nicklen (> (length nick) (string-to-number nicklen))
3134 (erc-display-message
3135 nil 'notice 'active 'nick-too-long
3136 ?i (length nick) ?l nicklen)))
3137 (erc-server-send (format "NICK %s" nick))
3138 (cond (erc-bad-nick
3139 (erc-set-current-nick nick)
3140 (erc-update-mode-line)
3141 (setq erc-bad-nick nil)))
3142 t)
3143
3144 (defun erc-cmd-PART (line)
3145 "When LINE is an empty string, leave the current channel.
3146 Otherwise leave the channel indicated by LINE."
3147 (cond
3148 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-?\\(.*\\)$" line)
3149 (let* ((ch (match-string 1 line))
3150 (msg (match-string 2 line))
3151 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3152 (erc-log (format "cmd: PART: %s: %s" ch reason))
3153 (erc-server-send (if (string= reason "")
3154 (format "PART %s" ch)
3155 (format "PART %s :%s" ch reason))
3156 nil ch))
3157 t)
3158 ((string-match "^\\s-*\\(.*\\)$" line)
3159 (let* ((ch (erc-default-target))
3160 (msg (match-string 1 line))
3161 (reason (funcall erc-part-reason (if (equal msg "") nil msg))))
3162 (if (and ch (erc-channel-p ch))
3163 (progn
3164 (erc-log (format "cmd: PART: %s: %s" ch reason))
3165 (erc-server-send (if (string= reason "")
3166 (format "PART %s" ch)
3167 (format "PART %s :%s" ch reason))
3168 nil ch))
3169 (erc-display-message nil 'error (current-buffer) 'no-target)))
3170 t)
3171 (t nil)))
3172 (put 'erc-cmd-PART 'do-not-parse-args t)
3173
3174 (defalias 'erc-cmd-LEAVE 'erc-cmd-PART)
3175
3176 (defun erc-cmd-PING (recipient)
3177 "Ping RECIPIENT."
3178 (let ((time (format "%f" (erc-current-time))))
3179 (erc-log (format "cmd: PING: %s" time))
3180 (erc-cmd-CTCP recipient "PING" time)))
3181
3182 (defun erc-cmd-QUOTE (line)
3183 "Send LINE directly to the server.
3184 All the text given as argument is sent to the sever as unmodified,
3185 just as you provided it. Use this command with care!"
3186 (cond
3187 ((string-match "^ ?\\(.+\\)$" line)
3188 (erc-server-send (match-string 1 line)))
3189 (t nil)))
3190 (put 'erc-cmd-QUOTE 'do-not-parse-args t)
3191
3192 (defcustom erc-query-display 'window
3193 "Indicates how to display query buffers when using the /QUERY
3194 command to talk to someone.
3195
3196 The default behavior is to display the message in a new window
3197 and bring it to the front. See the documentation for
3198 `erc-join-buffer' for a description of the available choices.
3199
3200 See also `erc-auto-query' to decide how private messages from
3201 other people should be displayed."
3202 :group 'erc-query
3203 :type '(choice (const :tag "Split window and select" window)
3204 (const :tag "Split window, don't select" window-noselect)
3205 (const :tag "New frame" frame)
3206 (const :tag "Bury in new buffer" bury)
3207 (const :tag "Use current buffer" buffer)
3208 (const :tag "Use current buffer" t)))
3209
3210 (defun erc-cmd-QUERY (&optional user)
3211 "Open a query with USER.
3212 The type of query window/frame/etc will depend on the value of
3213 `erc-query-display'.
3214
3215 If USER is omitted, close the current query buffer if one exists
3216 - except this is broken now ;-)"
3217 (interactive
3218 (list (read-from-minibuffer "Start a query with: " nil)))
3219 (let ((session-buffer (erc-server-buffer))
3220 (erc-join-buffer erc-query-display))
3221 (if user
3222 (erc-query user session-buffer)
3223 ;; currently broken, evil hack to display help anyway
3224 ;(erc-delete-query))))
3225 (signal 'wrong-number-of-arguments ""))))
3226 (defalias 'erc-cmd-Q 'erc-cmd-QUERY)
3227
3228 (defun erc-quit-reason-normal (&optional s)
3229 "Normal quit message.
3230
3231 If S is non-nil, it will be used as the quit reason."
3232 (or s
3233 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3234 erc-version-string) ; erc-official-location)
3235 ))
3236
3237 (defun erc-quit-reason-zippy (&optional s)
3238 "Zippy quit message.
3239
3240 If S is non-nil, it will be used as the quit reason."
3241 (or s
3242 (erc-replace-regexp-in-string "\n" "" (yow))))
3243
3244 (defun erc-quit-reason-various (s)
3245 "Choose a quit reason based on S (a string)."
3246 (when (featurep 'xemacs) (require 'poe))
3247 (let ((res (car (assoc-default (or s "")
3248 erc-quit-reason-various-alist 'string-match))))
3249 (cond
3250 ((functionp res) (funcall res))
3251 ((stringp res) res)
3252 (s s)
3253 (t (erc-quit-reason-normal)))))
3254
3255 (defun erc-part-reason-normal (&optional s)
3256 "Normal part message.
3257
3258 If S is non-nil, it will be used as the quit reason."
3259 (or s
3260 (format "\C-bERC\C-b %s (IRC client for Emacs)"; - \C-b%s\C-b"
3261 erc-version-string) ; erc-official-location)
3262 ))
3263
3264 (defun erc-part-reason-zippy (&optional s)
3265 "Zippy part message.
3266
3267 If S is non-nil, it will be used as the quit reason."
3268 (or s
3269 (erc-replace-regexp-in-string "\n" "" (yow))))
3270
3271 (defun erc-part-reason-various (s)
3272 "Choose a part reason based on S (a string)."
3273 (when (featurep 'xemacs) (require 'poe))
3274 (let ((res (car (assoc-default (or s "")
3275 erc-part-reason-various-alist 'string-match))))
3276 (cond
3277 ((functionp res) (funcall res))
3278 ((stringp res) res)
3279 (s s)
3280 (t (erc-part-reason-normal)))))
3281
3282 (defun erc-cmd-QUIT (reason)
3283 "Disconnect from the current server.
3284 If REASON is omitted, display a default quit message, otherwise display
3285 the message given by REASON."
3286 (unless reason
3287 (setq reason ""))
3288 (cond
3289 ((string-match "^\\s-*\\(.*\\)$" reason)
3290 (let* ((s (match-string 1 reason))
3291 (buffer (erc-server-buffer))
3292 (reason (funcall erc-quit-reason (if (equal s "") nil s)))
3293 server-proc)
3294 (with-current-buffer (if (and buffer
3295 (bufferp buffer))
3296 buffer
3297 (current-buffer))
3298 (erc-log (format "cmd: QUIT: %s" reason))
3299 (setq erc-server-quitting t)
3300 (erc-set-active-buffer (erc-server-buffer))
3301 (setq server-proc erc-server-process)
3302 (erc-server-send (format "QUIT :%s" reason)))
3303 (run-hook-with-args 'erc-quit-hook server-proc)
3304 (when erc-kill-queries-on-quit
3305 (erc-kill-query-buffers server-proc))
3306 ;; if the process has not been killed within 4 seconds, kill it
3307 (run-at-time 4 nil
3308 (lambda (proc)
3309 (when (and (processp proc)
3310 (memq (process-status proc) '(run open)))
3311 (delete-process proc)))
3312 server-proc))
3313 t)
3314 (t nil)))
3315
3316 (defalias 'erc-cmd-BYE 'erc-cmd-QUIT)
3317 (defalias 'erc-cmd-EXIT 'erc-cmd-QUIT)
3318 (defalias 'erc-cmd-SIGNOFF 'erc-cmd-QUIT)
3319 (put 'erc-cmd-QUIT 'do-not-parse-args t)
3320 (put 'erc-cmd-QUIT 'process-not-needed t)
3321
3322 (defun erc-cmd-GQUIT (reason)
3323 "Disconnect from all servers at once with the same quit REASON."
3324 (erc-with-all-buffers-of-server nil #'erc-open-server-buffer-p
3325 (erc-cmd-QUIT reason))
3326 (when erc-kill-queries-on-quit
3327 ;; if the query buffers have not been killed within 4 seconds,
3328 ;; kill them
3329 (run-at-time
3330 4 nil
3331 (lambda ()
3332 (dolist (buffer (erc-buffer-list (lambda (buf)
3333 (not (erc-server-buffer-p buf)))))
3334 (kill-buffer buffer)))))
3335 t)
3336
3337 (defalias 'erc-cmd-GQ 'erc-cmd-GQUIT)
3338 (put 'erc-cmd-GQUIT 'do-not-parse-args t)
3339 (put 'erc-cmd-GQUIT 'process-not-needed t)
3340
3341 (defun erc-cmd-RECONNECT ()
3342 "Try to reconnect to the current IRC server."
3343 (let ((buffer (erc-server-buffer))
3344 (process nil))
3345 (unless (buffer-live-p buffer)
3346 (setq buffer (current-buffer)))
3347 (with-current-buffer buffer
3348 (setq erc-server-quitting nil)
3349 (setq erc-server-reconnecting t)
3350 (setq erc-server-reconnect-count 0)
3351 (setq process (get-buffer-process (erc-server-buffer)))
3352 (if process
3353 (delete-process process)
3354 (erc-server-reconnect))
3355 (setq erc-server-reconnecting nil)))
3356 t)
3357 (put 'erc-cmd-RECONNECT 'process-not-needed t)
3358
3359 (defun erc-cmd-SERVER (server)
3360 "Connect to SERVER, leaving existing connection intact."
3361 (erc-log (format "cmd: SERVER: %s" server))
3362 (condition-case nil
3363 (erc :server server :nick (erc-current-nick))
3364 (error
3365 (erc-error "Cannot find host %s." server)))
3366 t)
3367 (put 'erc-cmd-SERVER 'process-not-needed t)
3368
3369 (defvar motif-version-string)
3370 (defvar gtk-version-string)
3371
3372 (defun erc-cmd-SV ()
3373 "Say the current ERC and Emacs version into channel."
3374 (erc-send-message (format "I'm using ERC %s with %s %s (%s%s) of %s."
3375 erc-version-string
3376 (if (featurep 'xemacs) "XEmacs" "GNU Emacs")
3377 emacs-version
3378 system-configuration
3379 (concat
3380 (cond ((featurep 'motif)
3381 (concat ", " (substring
3382 motif-version-string 4)))
3383 ((featurep 'gtk)
3384 (concat ", GTK+ Version "
3385 gtk-version-string))
3386 ((featurep 'x-toolkit) ", X toolkit")
3387 (t ""))
3388 (if (and (boundp 'x-toolkit-scroll-bars)
3389 (memq x-toolkit-scroll-bars
3390 '(xaw xaw3d)))
3391 (format ", %s scroll bars"
3392 (capitalize (symbol-name
3393 x-toolkit-scroll-bars)))
3394 "")
3395 (if (featurep 'multi-tty) ", multi-tty" ""))
3396 erc-emacs-build-time))
3397 t)
3398
3399 (defun erc-cmd-SM ()
3400 "Say the current ERC modes into channel."
3401 (erc-send-message (format "I'm using the following modules: %s!"
3402 (erc-modes)))
3403 t)
3404
3405 (defun erc-cmd-DEOP (&rest people)
3406 "Remove the operator setting from user(s) given in PEOPLE."
3407 (when (> (length people) 0)
3408 (erc-server-send (concat "MODE " (erc-default-target)
3409 " -"
3410 (make-string (length people) ?o)
3411 " "
3412 (mapconcat 'identity people " ")))
3413 t))
3414
3415 (defun erc-cmd-OP (&rest people)
3416 "Add the operator setting to users(s) given in PEOPLE."
3417 (when (> (length people) 0)
3418 (erc-server-send (concat "MODE " (erc-default-target)
3419 " +"
3420 (make-string (length people) ?o)
3421 " "
3422 (mapconcat 'identity people " ")))
3423 t))
3424
3425 (defun erc-cmd-TIME (&optional line)
3426 "Request the current time and date from the current server."
3427 (cond
3428 ((and line (string-match "^\\s-*\\(.*\\)$" line))
3429 (let ((args (match-string 1 line)))
3430 (erc-log (format "cmd: TIME: %s" args))
3431 (erc-server-send (concat "TIME " args)))
3432 t)
3433 (t (erc-server-send "TIME"))))
3434 (defalias 'erc-cmd-DATE 'erc-cmd-TIME)
3435
3436 (defun erc-cmd-TOPIC (topic)
3437 "Set or request the topic for a channel.
3438 LINE has the format: \"#CHANNEL TOPIC\", \"#CHANNEL\", \"TOPIC\"
3439 or the empty string.
3440
3441 If no #CHANNEL is given, the default channel is used. If TOPIC is
3442 given, the channel topic is modified, otherwise the current topic will
3443 be displayed."
3444 (cond
3445 ;; /topic #channel TOPIC
3446 ((string-match "^\\s-*\\([&#+!]\\S-+\\)\\s-\\(.*\\)$" topic)
3447 (let ((ch (match-string 1 topic))
3448 (topic (match-string 2 topic)))
3449 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3450 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3451 t)
3452 ;; /topic #channel
3453 ((string-match "^\\s-*\\([&#+!]\\S-+\\)" topic)
3454 (let ((ch (match-string 1 topic)))
3455 (erc-server-send (format "TOPIC %s" ch) nil ch)
3456 t))
3457 ;; /topic
3458 ((string-match "^\\s-*$" topic)
3459 (let ((ch (erc-default-target)))
3460 (erc-server-send (format "TOPIC %s" ch) nil ch)
3461 t))
3462 ;; /topic TOPIC
3463 ((string-match "^\\s-*\\(.*\\)$" topic)
3464 (let ((ch (erc-default-target))
3465 (topic (match-string 1 topic)))
3466 (if (and ch (erc-channel-p ch))
3467 (progn
3468 (erc-log (format "cmd: TOPIC [%s]: %s" ch topic))
3469 (erc-server-send (format "TOPIC %s :%s" ch topic) nil ch))
3470 (erc-display-message nil 'error (current-buffer) 'no-target)))
3471 t)
3472 (t nil)))
3473 (defalias 'erc-cmd-T 'erc-cmd-TOPIC)
3474 (put 'erc-cmd-TOPIC 'do-not-parse-args t)
3475
3476 (defun erc-cmd-APPENDTOPIC (topic)
3477 "Append TOPIC to the current channel topic, separated by a space."
3478 (let ((oldtopic erc-channel-topic))
3479 ;; display help when given no arguments
3480 (when (string-match "^\\s-*$" topic)
3481 (signal 'wrong-number-of-arguments nil))
3482 ;; strip trailing ^O
3483 (when (string-match "\\(.*\\)\C-o" oldtopic)
3484 (erc-cmd-TOPIC (concat (match-string 1 oldtopic) topic)))))
3485 (defalias 'erc-cmd-AT 'erc-cmd-APPENDTOPIC)
3486 (put 'erc-cmd-APPENDTOPIC 'do-not-parse-args t)
3487
3488 (defun erc-cmd-CLEARTOPIC (&optional channel)
3489 "Clear the topic for a CHANNEL.
3490 If CHANNEL is not specified, clear the topic for the default channel."
3491 (interactive "sClear topic of channel (RET is current channel): ")
3492 (let ((chnl (or (and (erc-channel-p channel) channel) (erc-default-target))))
3493 (when chnl
3494 (erc-server-send (format "TOPIC %s :" chnl))
3495 t)))
3496
3497 ;;; Banlists
3498
3499 (defvar erc-channel-banlist nil
3500 "A list of bans seen for the current channel.
3501
3502 Each ban is an alist of the form:
3503 (WHOSET . MASK)
3504
3505 The property `received-from-server' indicates whether
3506 or not the ban list has been requested from the server.")
3507 (make-variable-buffer-local 'erc-channel-banlist)
3508 (put 'erc-channel-banlist 'received-from-server nil)
3509
3510 (defun erc-cmd-BANLIST ()
3511 "Pretty-print the contents of `erc-channel-banlist'.
3512
3513 The ban list is fetched from the server if necessary."
3514 (let ((chnl (erc-default-target))
3515 (chnl-name (buffer-name)))
3516
3517 (cond
3518 ((not (erc-channel-p chnl))
3519 (erc-display-line (erc-make-notice "You're not on a channel\n")
3520 'active))
3521
3522 ((not (get 'erc-channel-banlist 'received-from-server))
3523 (let ((old-367-hook erc-server-367-functions))
3524 (setq erc-server-367-functions 'erc-banlist-store
3525 erc-channel-banlist nil)
3526 ;; fetch the ban list then callback
3527 (erc-with-server-buffer
3528 (erc-once-with-server-event
3529 368
3530 `(with-current-buffer ,chnl-name
3531 (put 'erc-channel-banlist 'received-from-server t)
3532 (setq erc-server-367-functions ',old-367-hook)
3533 (erc-cmd-BANLIST)
3534 t))
3535 (erc-server-send (format "MODE %s b" chnl)))))
3536
3537 ((null erc-channel-banlist)
3538 (erc-display-line (erc-make-notice
3539 (format "No bans for channel: %s\n" chnl))
3540 'active)
3541 (put 'erc-channel-banlist 'received-from-server nil))
3542
3543 (t
3544 (let* ((erc-fill-column (or (and (boundp 'erc-fill-column)
3545 erc-fill-column)
3546 (and (boundp 'fill-column)
3547 fill-column)
3548 (1- (window-width))))
3549 (separator (make-string erc-fill-column ?=))
3550 (fmt (concat
3551 "%-" (number-to-string (/ erc-fill-column 2)) "s"
3552 "%" (number-to-string (/ erc-fill-column 2)) "s")))
3553
3554 (erc-display-line
3555 (erc-make-notice (format "Ban list for channel: %s\n"
3556 (erc-default-target)))
3557 'active)
3558
3559 (erc-display-line separator 'active)
3560 (erc-display-line (format fmt "Ban Mask" "Banned By") 'active)
3561 (erc-display-line separator 'active)
3562
3563 (mapc
3564 (lambda (x)
3565 (erc-display-line
3566 (format fmt
3567 (truncate-string-to-width (cdr x) (/ erc-fill-column 2))
3568 (if (car x)
3569 (truncate-string-to-width (car x) (/ erc-fill-column 2))
3570 ""))
3571 'active))
3572 erc-channel-banlist)
3573
3574 (erc-display-line (erc-make-notice "End of Ban list")
3575 'active)
3576 (put 'erc-channel-banlist 'received-from-server nil)))))
3577 t)
3578
3579 (defalias 'erc-cmd-BL 'erc-cmd-BANLIST)
3580
3581 (defun erc-cmd-MASSUNBAN ()
3582 "Mass Unban.
3583
3584 Unban all currently banned users in the current channel."
3585 (let ((chnl (erc-default-target)))
3586 (cond
3587
3588 ((not (erc-channel-p chnl))
3589 (erc-display-line
3590 (erc-make-notice "You're not on a channel\n")
3591 'active))
3592
3593 ((not (get 'erc-channel-banlist 'received-from-server))
3594 (let ((old-367-hook erc-server-367-functions))
3595 (setq erc-server-367-functions 'erc-banlist-store)
3596 ;; fetch the ban list then callback
3597 (erc-with-server-buffer
3598 (erc-once-with-server-event
3599 368
3600 `(with-current-buffer ,chnl
3601 (put 'erc-channel-banlist 'received-from-server t)
3602 (setq erc-server-367-functions ,old-367-hook)
3603 (erc-cmd-MASSUNBAN)
3604 t))
3605 (erc-server-send (format "MODE %s b" chnl)))))
3606
3607 (t (let ((bans (mapcar 'cdr erc-channel-banlist)))
3608 (when bans
3609 ;; Glob the bans into groups of three, and carry out the unban.
3610 ;; eg. /mode #foo -bbb a*!*@* b*!*@* c*!*@*
3611 (mapc
3612 (lambda (x)
3613 (erc-server-send
3614 (format "MODE %s -%s %s" (erc-default-target)
3615 (make-string (length x) ?b)
3616 (mapconcat 'identity x " "))))
3617 (erc-group-list bans 3))))
3618 t))))
3619
3620 (defalias 'erc-cmd-MUB 'erc-cmd-MASSUNBAN)
3621
3622 ;;;; End of IRC commands
3623
3624 (defun erc-ensure-channel-name (channel)
3625 "Return CHANNEL if it is a valid channel name.
3626 Eventually add a # in front of it, if that turns it into a valid channel name."
3627 (if (erc-channel-p channel)
3628 channel
3629 (concat "#" channel)))
3630
3631 (defun erc-grab-region (start end)
3632 "Copy the region between START and END in a recreatable format.
3633
3634 Converts all the IRC text properties in each line of the region
3635 into control codes and writes them to a separate buffer. The
3636 resulting text may be used directly as a script to generate this
3637 text again."
3638 (interactive "r")
3639 (erc-set-active-buffer (current-buffer))
3640 (save-excursion
3641 (let* ((cb (current-buffer))
3642 (buf (generate-new-buffer erc-grab-buffer-name))
3643 (region (buffer-substring start end))
3644 (lines (erc-split-multiline-safe region)))
3645 (set-buffer buf)
3646 (dolist (line lines)
3647 (insert (concat line "\n")))
3648 (set-buffer cb)
3649 (switch-to-buffer-other-window buf)))
3650 (message "erc-grab-region doesn't grab colors etc. anymore. If you use this, please tell the maintainers.")
3651 (ding))
3652
3653 (defun erc-display-prompt (&optional buffer pos prompt face)
3654 "Display PROMPT in BUFFER at position POS.
3655 Display an ERC prompt in BUFFER.
3656
3657 If PROMPT is nil, one is constructed with the function `erc-prompt'.
3658 If BUFFER is nil, the `current-buffer' is used.
3659 If POS is nil, PROMPT will be displayed at `point'.
3660 If FACE is non-nil, it will be used to propertize the prompt. If it is nil,
3661 `erc-prompt-face' will be used."
3662 (let* ((prompt (or prompt (erc-prompt)))
3663 (l (length prompt))
3664 (ob (current-buffer)))
3665 ;; We cannot use save-excursion because we move point, therefore
3666 ;; we resort to the ol' ob trick to restore this.
3667 (when (and buffer (bufferp buffer))
3668 (set-buffer buffer))
3669
3670 ;; now save excursion again to store where point and mark are
3671 ;; in the current buffer
3672 (save-excursion
3673 (setq pos (or pos (point)))
3674 (goto-char pos)
3675 (when (> l 0)
3676 ;; Do not extend the text properties when typing at the end
3677 ;; of the prompt, but stuff typed in front of the prompt
3678 ;; shall remain part of the prompt.
3679 (setq prompt (erc-propertize prompt
3680 'start-open t ; XEmacs
3681 'rear-nonsticky t ; Emacs
3682 'erc-prompt t
3683 'front-sticky t
3684 'read-only t))
3685 (erc-put-text-property 0 (1- (length prompt))
3686 'face (or face 'erc-prompt-face)
3687 prompt)
3688 (insert prompt))
3689 ;; Set the input marker
3690 (set-marker erc-input-marker (point)))
3691
3692 ;; Now we are back at the old position. If the prompt was
3693 ;; inserted here or before us, advance point by the length of
3694 ;; the prompt.
3695 (when (or (not pos) (<= (point) pos))
3696 (forward-char l))
3697 ;; Clear the undo buffer now, so the user can undo his stuff,
3698 ;; but not the stuff we did. Sneaky!
3699 (setq buffer-undo-list nil)
3700 (set-buffer ob)))
3701
3702 ;; interactive operations
3703
3704 (defun erc-input-message ()
3705 "Read input from the minibuffer."
3706 (interactive)
3707 (let ((minibuffer-allow-text-properties t)
3708 (read-map minibuffer-local-map))
3709 (insert (read-from-minibuffer "Message: "
3710 (string (if (featurep 'xemacs)
3711 last-command-char
3712 last-command-event)) read-map))
3713 (erc-send-current-line)))
3714
3715 (defvar erc-action-history-list ()
3716 "History list for interactive action input.")
3717
3718 (defun erc-input-action ()
3719 "Interactively input a user action and send it to IRC."
3720 (interactive "")
3721 (erc-set-active-buffer (current-buffer))
3722 (let ((action (read-from-minibuffer
3723 "Action: " nil nil nil 'erc-action-history-list)))
3724 (if (not (string-match "^\\s-*$" action))
3725 (erc-send-action (erc-default-target) action))))
3726
3727 (defun erc-join-channel (channel &optional key)
3728 "Join CHANNEL.
3729
3730 If `point' is at the beginning of a channel name, use that as default."
3731 (interactive
3732 (list
3733 (let ((chnl (if (looking-at "\\([&#+!][^ \n]+\\)") (match-string 1) ""))
3734 (table (when (erc-server-buffer-live-p)
3735 (set-buffer (process-buffer erc-server-process))
3736 erc-channel-list)))
3737 (completing-read "Join channel: " table nil nil nil nil chnl))
3738 (when (or current-prefix-arg erc-prompt-for-channel-key)
3739 (read-from-minibuffer "Channel key (RET for none): " nil))))
3740 (erc-cmd-JOIN channel (when (>= (length key) 1) key)))
3741
3742 (defun erc-part-from-channel (reason)
3743 "Part from the current channel and prompt for a REASON."
3744 (interactive
3745 (list
3746 (if (and (boundp 'reason) (stringp reason) (not (string= reason "")))
3747 reason
3748 (read-from-minibuffer (concat "Reason for leaving " (erc-default-target)
3749 ": ")))))
3750 (erc-cmd-PART (concat (erc-default-target)" " reason)))
3751
3752 (defun erc-set-topic (topic)
3753 "Prompt for a TOPIC for the current channel."
3754 (interactive
3755 (list
3756 (read-from-minibuffer
3757 (concat "Set topic of " (erc-default-target) ": ")
3758 (when erc-channel-topic
3759 (let ((ss (split-string erc-channel-topic "\C-o")))
3760 (cons (apply 'concat (if (cdr ss) (butlast ss) ss))
3761 0))))))
3762 (let ((topic-list (split-string topic "\C-o"))) ; strip off the topic setter
3763 (erc-cmd-TOPIC (concat (erc-default-target) " " (car topic-list)))))
3764
3765 (defun erc-set-channel-limit (&optional limit)
3766 "Set a LIMIT for the current channel. Remove limit if nil.
3767 Prompt for one if called interactively."
3768 (interactive (list (read-from-minibuffer
3769 (format "Limit for %s (RET to remove limit): "
3770 (erc-default-target)))))
3771 (let ((tgt (erc-default-target)))
3772 (if (and limit (>= (length limit) 1))
3773 (erc-server-send (format "MODE %s +l %s" tgt limit))
3774 (erc-server-send (format "MODE %s -l" tgt)))))
3775
3776 (defun erc-set-channel-key (&optional key)
3777 "Set a KEY for the current channel. Remove key if nil.
3778 Prompt for one if called interactively."
3779 (interactive (list (read-from-minibuffer
3780 (format "Key for %s (RET to remove key): "
3781 (erc-default-target)))))
3782 (let ((tgt (erc-default-target)))
3783 (if (and key (>= (length key) 1))
3784 (erc-server-send (format "MODE %s +k %s" tgt key))
3785 (erc-server-send (format "MODE %s -k" tgt)))))
3786
3787 (defun erc-quit-server (reason)
3788 "Disconnect from current server after prompting for REASON.
3789 `erc-quit-reason' works with this just like with `erc-cmd-QUIT'."
3790 (interactive (list (read-from-minibuffer
3791 (format "Reason for quitting %s: "
3792 (or erc-server-announced-name
3793 erc-session-server)))))
3794 (erc-cmd-QUIT reason))
3795
3796 ;; Movement of point
3797
3798 (defun erc-bol ()
3799 "Move `point' to the beginning of the current line.
3800
3801 This places `point' just after the prompt, or at the beginning of the line."
3802 (interactive)
3803 (forward-line 0)
3804 (when (get-text-property (point) 'erc-prompt)
3805 (goto-char erc-input-marker))
3806 (point))
3807
3808 (defun erc-kill-input ()
3809 "Kill current input line using `erc-bol' followed by `kill-line'."
3810 (interactive)
3811 (when (and (erc-bol)
3812 (/= (point) (point-max))) ;; Prevent a (ding) and an error when
3813 ;; there's nothing to kill
3814 (if (boundp 'erc-input-ring-index)
3815 (setq erc-input-ring-index nil))
3816 (kill-line)))
3817
3818 (defun erc-complete-word ()
3819 "Complete the word before point.
3820
3821 This function uses `erc-complete-functions'."
3822 (interactive)
3823 (unless (run-hook-with-args-until-success 'erc-complete-functions)
3824 (beep)))
3825
3826 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3827 ;;
3828 ;; IRC SERVER INPUT HANDLING
3829 ;;
3830 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3831
3832 ;;;; New Input parsing
3833
3834 ; Stolen from ZenIRC. I just wanna test this code, so here is
3835 ; experiment area.
3836
3837 (defcustom erc-default-server-hook '(erc-debug-missing-hooks
3838 erc-default-server-handler)
3839 "*Default for server messages which aren't covered by `erc-server-hooks'."
3840 :group 'erc-server-hooks
3841 :type 'hook)
3842
3843 (defun erc-default-server-handler (proc parsed)
3844 "Default server handler.
3845
3846 Displays PROC and PARSED appropriately using `erc-display-message'."
3847 (erc-display-message
3848 parsed 'notice proc
3849 (mapconcat
3850 'identity
3851 (let (res)
3852 (mapc #'(lambda (x)
3853 (if (stringp x)
3854 (setq res (append res (list x)))))
3855 parsed)
3856 res)
3857 " ")))
3858
3859 (defvar erc-server-vectors
3860 '(["msgtype" "sender" "to" "arg1" "arg2" "arg3" "..."])
3861 "List of received server messages which ERC does not specifically handle.
3862 See `erc-debug-missing-hooks'.")
3863 ;(make-variable-buffer-local 'erc-server-vectors)
3864
3865 (defun erc-debug-missing-hooks (proc parsed)
3866 "Add PARSED server message ERC does not yet handle to `erc-server-vectors'.
3867 These vectors can be helpful when adding new server message handlers to ERC.
3868 See `erc-default-server-hook'."
3869 (nconc erc-server-vectors (list parsed))
3870 nil)
3871
3872 (defun erc-query (target server)
3873 "Open a query buffer on TARGET, using SERVER.
3874 To change how this query window is displayed, use `let' to bind
3875 `erc-join-buffer' before calling this."
3876 (unless (and server
3877 (buffer-live-p server)
3878 (set-buffer server))
3879 (error "Couldn't switch to server buffer"))
3880 (let ((buf (erc-open erc-session-server
3881 erc-session-port
3882 (erc-current-nick)
3883 erc-session-user-full-name
3884 nil
3885 nil
3886 (list target)
3887 target
3888 erc-server-process)))
3889 (unless buf
3890 (error "Couldn't open query window"))
3891 (erc-update-mode-line)
3892 buf))
3893
3894 (defcustom erc-auto-query 'window-noselect
3895 "If non-nil, create a query buffer each time you receive a private message.
3896 If the buffer doesn't already exist, it is created.
3897
3898 This can be set to a symbol, to control how the new query window
3899 should appear. The default behavior is to display the buffer in
3900 a new window, but not to select it. See the documentation for
3901 `erc-join-buffer' for a description of the available choices."
3902 :group 'erc-query
3903 :type '(choice (const :tag "Don't create query window" nil)
3904 (const :tag "Split window and select" window)
3905 (const :tag "Split window, don't select" window-noselect)
3906 (const :tag "New frame" frame)
3907 (const :tag "Bury in new buffer" bury)
3908 (const :tag "Use current buffer" buffer)
3909 (const :tag "Use current buffer" t)))
3910
3911 (defcustom erc-query-on-unjoined-chan-privmsg t
3912 "If non-nil create query buffer on receiving any PRIVMSG at all.
3913 This includes PRIVMSGs directed to channels. If you are using an IRC
3914 bouncer, such as dircproxy, to keep a log of channels when you are
3915 disconnected, you should set this option to t."
3916 :group 'erc-query
3917 :type 'boolean)
3918
3919 (defcustom erc-format-query-as-channel-p t
3920 "If non-nil, format text from others in a query buffer like in a channel,
3921 otherwise format like a private message."
3922 :group 'erc-query
3923 :type 'boolean)
3924
3925 (defcustom erc-minibuffer-notice nil
3926 "If non-nil, print ERC notices for the user in the minibuffer.
3927 Only happens when the session buffer isn't visible."
3928 :group 'erc-display
3929 :type 'boolean)
3930
3931 (defcustom erc-minibuffer-ignored nil
3932 "If non-nil, print a message in the minibuffer if we ignored something."
3933 :group 'erc-ignore
3934 :type 'boolean)
3935
3936 (defun erc-wash-quit-reason (reason nick login host)
3937 "Remove duplicate text from quit REASON.
3938 Specifically in relation to NICK (user@host) information. Returns REASON
3939 unmodified if nothing can be removed.
3940 E.g. \"Read error to Nick [user@some.host]: 110\" would be shortened to
3941 \"Read error: 110\". The same applies for \"Ping Timeout\"."
3942 (setq nick (regexp-quote nick)
3943 login (regexp-quote login)
3944 host (regexp-quote host))
3945 (or (when (string-match (concat "^\\(Read error\\) to "
3946 nick "\\[" host "\\]: "
3947 "\\(.+\\)$") reason)
3948 (concat (match-string 1 reason) ": " (match-string 2 reason)))
3949 (when (string-match (concat "^\\(Ping timeout\\) for "
3950 nick "\\[" host "\\]$") reason)
3951 (match-string 1 reason))
3952 reason))
3953
3954 (defun erc-nickname-in-use (nick reason)
3955 "If NICK is unavailable, tell the user the REASON.
3956
3957 See also `erc-display-error-notice'."
3958 (if (or (not erc-try-new-nick-p)
3959 ;; how many default-nicks are left + one more try...
3960 (eq erc-nick-change-attempt-count
3961 (if (consp erc-nick)
3962 (+ (length erc-nick) 1)
3963 1)))
3964 (erc-display-error-notice
3965 nil
3966 (format "Nickname %s is %s, try another." nick reason))
3967 (setq erc-nick-change-attempt-count (+ erc-nick-change-attempt-count 1))
3968 (let ((newnick (nth 1 erc-default-nicks))
3969 (nicklen (cdr (assoc "NICKLEN"
3970 (erc-with-server-buffer
3971 erc-server-parameters)))))
3972 (setq erc-bad-nick t)
3973 ;; try to use a different nick
3974 (if erc-default-nicks
3975 (setq erc-default-nicks (cdr erc-default-nicks)))
3976 (if (not newnick)
3977 (setq newnick (concat (truncate-string-to-width
3978 nick
3979 (if (and erc-server-connected nicklen)
3980 (- (string-to-number nicklen)
3981 (length erc-nick-uniquifier))
3982 ;; rfc2812 max nick length = 9
3983 ;; we must assume this is the
3984 ;; server's setting if we haven't
3985 ;; established a connection yet
3986 (- 9 (length erc-nick-uniquifier))))
3987 erc-nick-uniquifier)))
3988 (erc-cmd-NICK newnick)
3989 (erc-display-error-notice
3990 nil
3991 (format "Nickname %s is %s, trying %s"
3992 nick reason newnick)))))
3993
3994 ;;; Server messages
3995
3996 (defgroup erc-server-hooks nil
3997 "Server event callbacks.
3998 Every server event - like numeric replies - has its own hook.
3999 Those hooks are all called using `run-hook-with-args-until-success'.
4000 They receive as first argument the process object from where the event
4001 originated from,
4002 and as second argument the event parsed as a vector."
4003 :group 'erc-hooks)
4004
4005 (defun erc-display-server-message (proc parsed)
4006 "Display the message sent by the server as a notice."
4007 (erc-display-message
4008 parsed 'notice 'active (erc-response.contents parsed)))
4009
4010 (defun erc-auto-query (proc parsed)
4011 ;; FIXME: This needs more documentation, unless it's not a user function --
4012 ;; Lawrence 2004-01-08
4013 "Put this on `erc-server-PRIVMSG-functions'."
4014 (when erc-auto-query
4015 (let* ((nick (car (erc-parse-user (erc-response.sender parsed))))
4016 (target (car (erc-response.command-args parsed)))
4017 (msg (erc-response.contents parsed))
4018 (query (if (not erc-query-on-unjoined-chan-privmsg)
4019 nick
4020 (if (erc-current-nick-p target)
4021 nick
4022 target))))
4023 (and (not (erc-ignored-user-p (erc-response.sender parsed)))
4024 (or erc-query-on-unjoined-chan-privmsg
4025 (string= target (erc-current-nick)))
4026 (not (erc-get-buffer query proc))
4027 (not (erc-is-message-ctcp-and-not-action-p msg))
4028 (let ((erc-query-display erc-auto-query))
4029 (erc-cmd-QUERY query))
4030 nil))))
4031
4032 (defun erc-is-message-ctcp-p (message)
4033 "Check if MESSAGE is a CTCP message or not."
4034 (string-match "^\C-a\\([^\C-a]*\\)\C-a?$" message))
4035
4036 (defun erc-is-message-ctcp-and-not-action-p (message)
4037 "Check if MESSAGE is a CTCP message or not."
4038 (and (erc-is-message-ctcp-p message)
4039 (not (string-match "^\C-a\\ACTION.*\C-a$" message))))
4040
4041 (defun erc-format-privmessage (nick msg privp msgp)
4042 "Format a PRIVMSG in an insertible fashion."
4043 (let* ((mark-s (if msgp (if privp "*" "<") "-"))
4044 (mark-e (if msgp (if privp "*" ">") "-"))
4045 (str (format "%s%s%s %s" mark-s nick mark-e msg))
4046 (nick-face (if privp 'erc-nick-msg-face 'erc-nick-default-face))
4047 (msg-face (if privp 'erc-direct-msg-face 'erc-default-face)))
4048 ;; add text properties to text before the nick, the nick and after the nick
4049 (erc-put-text-property 0 (length mark-s) 'face msg-face str)
4050 (erc-put-text-property (length mark-s) (+ (length mark-s) (length nick))
4051 'face nick-face str)
4052 (erc-put-text-property (+ (length mark-s) (length nick)) (length str)
4053 'face msg-face str)
4054 str))
4055
4056 (defcustom erc-format-nick-function 'erc-format-nick
4057 "*Function to format a nickname for message display."
4058 :group 'erc-display
4059 :type 'function)
4060
4061 (defun erc-format-nick (&optional user channel-data)
4062 "Return the nickname of USER.
4063 See also `erc-format-nick-function'."
4064 (when user (erc-server-user-nickname user)))
4065
4066 (defun erc-format-@nick (&optional user channel-data)
4067 "Format the nickname of USER showing if USER is an operator or has voice.
4068 Operators have \"@\" and users with voice have \"+\" as a prefix.
4069 Use CHANNEL-DATA to determine op and voice status.
4070 See also `erc-format-nick-function'."
4071 (when user
4072 (let ((op (and channel-data (erc-channel-user-op channel-data) "@"))
4073 (voice (and channel-data (erc-channel-user-voice channel-data) "+")))
4074 (concat voice op (erc-server-user-nickname user)))))
4075
4076 (defun erc-format-my-nick ()
4077 "Return the beginning of this user's message, correctly propertized."
4078 (if erc-show-my-nick
4079 (let ((open "<")
4080 (close "> ")
4081 (nick (erc-current-nick)))
4082 (concat
4083 (erc-propertize open 'face 'erc-default-face)
4084 (erc-propertize nick 'face 'erc-my-nick-face)
4085 (erc-propertize close 'face 'erc-default-face)))
4086 (let ((prefix "> "))
4087 (erc-propertize prefix 'face 'erc-default-face))))
4088
4089 (defun erc-echo-notice-in-default-buffer (s parsed buffer sender)
4090 "Echos a private notice in the default buffer, namely the
4091 target buffer specified by BUFFER, or there is no target buffer,
4092 the server buffer. This function is designed to be added to
4093 either `erc-echo-notice-hook' or `erc-echo-notice-always-hook',
4094 and always returns t."
4095 (erc-display-message parsed nil buffer s)
4096 t)
4097
4098 (defun erc-echo-notice-in-target-buffer (s parsed buffer sender)
4099 "Echos a private notice in BUFFER, if BUFFER is non-nil. This
4100 function is designed to be added to either `erc-echo-notice-hook'
4101 or `erc-echo-notice-always-hook', and returns non-nil if BUFFER
4102 is non-nil."
4103 (if buffer
4104 (progn (erc-display-message parsed nil buffer s) t)
4105 nil))
4106
4107 (defun erc-echo-notice-in-minibuffer (s parsed buffer sender)
4108 "Echos a private notice in the minibuffer. This function is
4109 designed to be added to either `erc-echo-notice-hook' or
4110 `erc-echo-notice-always-hook', and always returns t."
4111 (message "%s" (concat "NOTICE: " s))
4112 t)
4113
4114 (defun erc-echo-notice-in-server-buffer (s parsed buffer sender)
4115 "Echos a private notice in the server buffer. This function is
4116 designed to be added to either `erc-echo-notice-hook' or
4117 `erc-echo-notice-always-hook', and always returns t."
4118 (erc-display-message parsed nil nil s)
4119 t)
4120
4121 (defun erc-echo-notice-in-active-non-server-buffer (s parsed buffer sender)
4122 "Echos a private notice in the active buffer if the active
4123 buffer is not the server buffer. This function is designed to be
4124 added to either `erc-echo-notice-hook' or
4125 `erc-echo-notice-always-hook', and returns non-nil if the active
4126 buffer is not the server buffer."
4127 (if (not (eq (erc-server-buffer) (erc-active-buffer)))
4128 (progn (erc-display-message parsed nil 'active s) t)
4129 nil))
4130
4131 (defun erc-echo-notice-in-active-buffer (s parsed buffer sender)
4132 "Echos a private notice in the active buffer. This function is
4133 designed to be added to either `erc-echo-notice-hook' or
4134 `erc-echo-notice-always-hook', and always returns t."
4135 (erc-display-message parsed nil 'active s)
4136 t)
4137
4138 (defun erc-echo-notice-in-user-buffers (s parsed buffer sender)
4139 "Echos a private notice in all of the buffers for which SENDER
4140 is a member. This function is designed to be added to either
4141 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4142 returns non-nil if there is at least one buffer for which the
4143 sender is a member.
4144
4145 See also: `erc-echo-notice-in-first-user-buffer',
4146 `erc-buffer-list-with-nick'."
4147 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4148 (if buffers
4149 (progn (erc-display-message parsed nil buffers s) t)
4150 nil)))
4151
4152 (defun erc-echo-notice-in-user-and-target-buffers (s parsed buffer sender)
4153 "Echos a private notice in BUFFER and in all of the buffers for
4154 which SENDER is a member. This function is designed to be added
4155 to either `erc-echo-notice-hook' or
4156 `erc-echo-notice-always-hook', and returns non-nil if there is
4157 at least one buffer for which the sender is a member or the
4158 default target.
4159
4160 See also: `erc-echo-notice-in-user-buffers',
4161 `erc-buffer-list-with-nick'."
4162 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4163 (add-to-list 'buffers buffer)
4164 (if buffers
4165 (progn (erc-display-message parsed nil buffers s) t)
4166 nil)))
4167
4168 (defun erc-echo-notice-in-first-user-buffer (s parsed buffer sender)
4169 "Echos a private notice in one of the buffers for which SENDER
4170 is a member. This function is designed to be added to either
4171 `erc-echo-notice-hook' or `erc-echo-notice-always-hook', and
4172 returns non-nil if there is at least one buffer for which the
4173 sender is a member.
4174
4175 See also: `erc-echo-notice-in-user-buffers',
4176 `erc-buffer-list-with-nick'."
4177 (let ((buffers (erc-buffer-list-with-nick sender erc-server-process)))
4178 (if buffers
4179 (progn (erc-display-message parsed nil (car buffers) s) t)
4180 nil)))
4181
4182 ;;; Ban manipulation
4183
4184 (defun erc-banlist-store (proc parsed)
4185 "Record ban entries for a channel."
4186 (multiple-value-bind (channel mask whoset)
4187 (values-list (cdr (erc-response.command-args parsed)))
4188 ;; Determine to which buffer the message corresponds
4189 (let ((buffer (erc-get-buffer channel proc)))
4190 (with-current-buffer buffer
4191 (unless (member (cons whoset mask) erc-channel-banlist)
4192 (setq erc-channel-banlist (cons (cons whoset mask)
4193 erc-channel-banlist))))))
4194 nil)
4195
4196 (defun erc-banlist-finished (proc parsed)
4197 "Record that we have received the banlist."
4198 (let* ((channel (second (erc-response.command-args parsed)))
4199 (buffer (erc-get-buffer channel proc)))
4200 (with-current-buffer buffer
4201 (put 'erc-channel-banlist 'received-from-server t)))
4202 t) ; suppress the 'end of banlist' message
4203
4204 (defun erc-banlist-update (proc parsed)
4205 "Check MODE commands for bans and update the banlist appropriately."
4206 ;; FIXME: Possibly incorrect. -- Lawrence 2004-05-11
4207 (let* ((tgt (first (erc-response.command-args parsed)))
4208 (mode (erc-response.contents parsed))
4209 (whoset (erc-response.sender parsed))
4210 (buffer (erc-get-buffer tgt proc)))
4211 (when buffer
4212 (with-current-buffer buffer
4213 (cond ((not (get 'erc-channel-banlist 'received-from-server)) nil)
4214 ((string-match "^\\([+-]\\)b" mode)
4215 ;; This is a ban
4216 (cond
4217 ((string-match "^-" mode)
4218 ;; Remove the unbanned masks from the ban list
4219 (setq erc-channel-banlist
4220 (erc-delete-if
4221 #'(lambda (y)
4222 (member (upcase (cdr y))
4223 (mapcar #'upcase
4224 (cdr (split-string mode)))))
4225 erc-channel-banlist)))
4226 ((string-match "^+" mode)
4227 ;; Add the banned mask(s) to the ban list
4228 (mapc
4229 (lambda (mask)
4230 (unless (member (cons whoset mask) erc-channel-banlist)
4231 (setq erc-channel-banlist
4232 (cons (cons whoset mask) erc-channel-banlist))))
4233 (cdr (split-string mode))))))))))
4234 nil)
4235
4236 ;; used for the banlist cmds
4237 (defun erc-group-list (list n)
4238 "Group LIST into sublists of length N."
4239 (cond ((null list) nil)
4240 ((null (nthcdr n list)) (list list))
4241 (t (cons (erc-subseq list 0 n) (erc-group-list (nthcdr n list) n)))))
4242
4243
4244 ;;; MOTD numreplies
4245
4246 (defun erc-handle-login ()
4247 "Handle the logging in process of connection."
4248 (unless erc-logged-in
4249 (setq erc-logged-in t)
4250 (message "Logging in as \'%s\'... done" (erc-current-nick))
4251 ;; execute a startup script
4252 (let ((f (erc-select-startup-file)))
4253 (when f
4254 (erc-load-script f)))))
4255
4256 (defun erc-connection-established (proc parsed)
4257 "Run just after connection.
4258
4259 Set user modes and run `erc-after-connect' hook."
4260 (with-current-buffer (process-buffer proc)
4261 (unless erc-server-connected ; only once per session
4262 (let ((server (or erc-server-announced-name
4263 (erc-response.sender parsed)))
4264 (nick (car (erc-response.command-args parsed)))
4265 (buffer (process-buffer proc)))
4266 (setq erc-server-connected t)
4267 (erc-update-mode-line)
4268 (erc-set-initial-user-mode nick buffer)
4269 (erc-server-setup-periodical-ping buffer)
4270 (run-hook-with-args 'erc-after-connect server nick)))))
4271
4272 (defun erc-set-initial-user-mode (nick buffer)
4273 "If `erc-user-mode' is non-nil for NICK, set the user modes.
4274 The server buffer is given by BUFFER."
4275 (with-current-buffer buffer
4276 (when erc-user-mode
4277 (let ((mode (if (functionp erc-user-mode)
4278 (funcall erc-user-mode)
4279 erc-user-mode)))
4280 (when (stringp mode)
4281 (erc-log (format "changing mode for %s to %s" nick mode))
4282 (erc-server-send (format "MODE %s %s" nick mode)))))))
4283
4284 (defun erc-display-error-notice (parsed string)
4285 "Display STRING as an error notice.
4286
4287 See also `erc-display-message'."
4288 (erc-display-message
4289 parsed '(notice error) 'active string))
4290
4291 (defun erc-process-ctcp-query (proc parsed nick login host)
4292 ;; FIXME: This needs a proper docstring -- Lawrence 2004-01-08
4293 "Process a CTCP query."
4294 (let ((queries (delete "" (split-string (erc-response.contents parsed)
4295 "\C-a"))))
4296 (if (> (length queries) 4)
4297 (erc-display-message
4298 parsed (list 'notice 'error) proc 'ctcp-too-many)
4299 (if (= 0 (length queries))
4300 (erc-display-message
4301 parsed (list 'notice 'error) proc
4302 'ctcp-empty ?n nick)
4303 (while queries
4304 (let* ((type (upcase (car (split-string (car queries)))))
4305 (hook (intern-soft (concat "erc-ctcp-query-" type "-hook"))))
4306 (if (and hook (boundp hook))
4307 (if (string-equal type "ACTION")
4308 (run-hook-with-args-until-success
4309 hook proc parsed nick login host
4310 (car (erc-response.command-args parsed))
4311 (car queries))
4312 (when erc-paranoid
4313 (if (erc-current-nick-p
4314 (car (erc-response.command-args parsed)))
4315 (erc-display-message
4316 parsed 'error 'active 'ctcp-request
4317 ?n nick ?u login ?h host ?r (car queries))
4318 (erc-display-message
4319 parsed 'error 'active 'ctcp-request-to
4320 ?n nick ?u login ?h host ?r (car queries)
4321 ?t (car (erc-response.command-args parsed)))))
4322 (run-hook-with-args-until-success
4323 hook proc nick login host
4324 (car (erc-response.command-args parsed))
4325 (car queries)))
4326 (erc-display-message
4327 parsed (list 'notice 'error) proc
4328 'undefined-ctcp)))
4329 (setq queries (cdr queries)))))))
4330
4331 (defvar erc-ctcp-query-ACTION-hook '(erc-ctcp-query-ACTION))
4332
4333 (defun erc-ctcp-query-ACTION (proc parsed nick login host to msg)
4334 "Respond to a CTCP ACTION query."
4335 (when (string-match "^ACTION\\s-\\(.*\\)\\s-*$" msg)
4336 (let ((s (match-string 1 msg))
4337 (buf (or (erc-get-buffer to proc)
4338 (erc-get-buffer nick proc)
4339 (process-buffer proc))))
4340 (erc-display-message
4341 parsed 'action buf
4342 'ACTION ?n nick ?u login ?h host ?a s))))
4343
4344 (defvar erc-ctcp-query-CLIENTINFO-hook '(erc-ctcp-query-CLIENTINFO))
4345
4346 (defun erc-ctcp-query-CLIENTINFO (proc nick login host to msg)
4347 "Respond to a CTCP CLIENTINFO query."
4348 (when (string-match "^CLIENTINFO\\(\\s-*\\|\\s-+.*\\)$" msg)
4349 (let ((s (erc-client-info (erc-trim-string (match-string 1 msg)))))
4350 (unless erc-disable-ctcp-replies
4351 (erc-send-ctcp-notice nick (format "CLIENTINFO %s" s)))))
4352 nil)
4353
4354 (defvar erc-ctcp-query-ECHO-hook '(erc-ctcp-query-ECHO))
4355 (defun erc-ctcp-query-ECHO (proc nick login host to msg)
4356 "Respond to a CTCP ECHO query."
4357 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4358 (let ((s (match-string 1 msg)))
4359 (unless erc-disable-ctcp-replies
4360 (erc-send-ctcp-notice nick (format "ECHO %s" s)))))
4361 nil)
4362
4363 (defvar erc-ctcp-query-FINGER-hook '(erc-ctcp-query-FINGER))
4364 (defun erc-ctcp-query-FINGER (proc nick login host to msg)
4365 "Respond to a CTCP FINGER query."
4366 (unless erc-disable-ctcp-replies
4367 (let ((s (if erc-anonymous-login
4368 (format "FINGER I'm %s." (erc-current-nick))
4369 (format "FINGER %s (%s@%s)."
4370 (user-full-name)
4371 (user-login-name)
4372 (system-name))))
4373 (ns (erc-time-diff erc-server-last-sent-time (erc-current-time))))
4374 (when (> ns 0)
4375 (setq s (concat s " Idle for " (erc-sec-to-time ns))))
4376 (erc-send-ctcp-notice nick s)))
4377 nil)
4378
4379 (defvar erc-ctcp-query-PING-hook '(erc-ctcp-query-PING))
4380 (defun erc-ctcp-query-PING (proc nick login host to msg)
4381 "Respond to a CTCP PING query."
4382 (when (string-match "^PING\\s-+\\(.*\\)" msg)
4383 (unless erc-disable-ctcp-replies
4384 (let ((arg (match-string 1 msg)))
4385 (erc-send-ctcp-notice nick (format "PING %s" arg)))))
4386 nil)
4387
4388 (defvar erc-ctcp-query-TIME-hook '(erc-ctcp-query-TIME))
4389 (defun erc-ctcp-query-TIME (proc nick login host to msg)
4390 "Respond to a CTCP TIME query."
4391 (unless erc-disable-ctcp-replies
4392 (erc-send-ctcp-notice nick (format "TIME %s" (current-time-string))))
4393 nil)
4394
4395 (defvar erc-ctcp-query-USERINFO-hook '(erc-ctcp-query-USERINFO))
4396 (defun erc-ctcp-query-USERINFO (proc nick login host to msg)
4397 "Respond to a CTCP USERINFO query."
4398 (unless erc-disable-ctcp-replies
4399 (erc-send-ctcp-notice nick (format "USERINFO %s" erc-user-information)))
4400 nil)
4401
4402 (defvar erc-ctcp-query-VERSION-hook '(erc-ctcp-query-VERSION))
4403 (defun erc-ctcp-query-VERSION (proc nick login host to msg)
4404 "Respond to a CTCP VERSION query."
4405 (unless erc-disable-ctcp-replies
4406 (erc-send-ctcp-notice
4407 nick (format
4408 "VERSION \C-bERC\C-b %s - an IRC client for emacs (\C-b%s\C-b)"
4409 erc-version-string
4410 erc-official-location)))
4411 nil)
4412
4413 (defun erc-process-ctcp-reply (proc parsed nick login host msg)
4414 "Process MSG as a CTCP reply."
4415 (let* ((type (car (split-string msg)))
4416 (hook (intern (concat "erc-ctcp-reply-" type "-hook"))))
4417 (if (boundp hook)
4418 (run-hook-with-args-until-success
4419 hook proc nick login host
4420 (car (erc-response.command-args parsed)) msg)
4421 (erc-display-message
4422 parsed 'notice 'active
4423 'CTCP-UNKNOWN ?n nick ?u login ?h host ?m msg))))
4424
4425 (defvar erc-ctcp-reply-ECHO-hook '(erc-ctcp-reply-ECHO))
4426 (defun erc-ctcp-reply-ECHO (proc nick login host to msg)
4427 "Handle a CTCP ECHO reply."
4428 (when (string-match "^ECHO\\s-+\\(.*\\)\\s-*$" msg)
4429 (let ((message (match-string 1 msg)))
4430 (erc-display-message
4431 nil '(notice action) 'active
4432 'CTCP-ECHO ?n nick ?m message)))
4433 nil)
4434
4435 (defvar erc-ctcp-reply-CLIENTINFO-hook '(erc-ctcp-reply-CLIENTINFO))
4436 (defun erc-ctcp-reply-CLIENTINFO (proc nick login host to msg)
4437 "Handle a CTCP CLIENTINFO reply."
4438 (when (string-match "^CLIENTINFO\\s-+\\(.*\\)\\s-*$" msg)
4439 (let ((message (match-string 1 msg)))
4440 (erc-display-message
4441 nil 'notice 'active
4442 'CTCP-CLIENTINFO ?n nick ?m message)))
4443 nil)
4444
4445 (defvar erc-ctcp-reply-FINGER-hook '(erc-ctcp-reply-FINGER))
4446 (defun erc-ctcp-reply-FINGER (proc nick login host to msg)
4447 "Handle a CTCP FINGER reply."
4448 (when (string-match "^FINGER\\s-+\\(.*\\)\\s-*$" msg)
4449 (let ((message (match-string 1 msg)))
4450 (erc-display-message
4451 nil 'notice 'active
4452 'CTCP-FINGER ?n nick ?m message)))
4453 nil)
4454
4455 (defvar erc-ctcp-reply-PING-hook '(erc-ctcp-reply-PING))
4456 (defun erc-ctcp-reply-PING (proc nick login host to msg)
4457 "Handle a CTCP PING reply."
4458 (if (not (string-match "^PING\\s-+\\([0-9.]+\\)" msg))
4459 nil
4460 (let ((time (match-string 1 msg)))
4461 (condition-case nil
4462 (let ((delta (erc-time-diff (string-to-number time)
4463 (erc-current-time))))
4464 (erc-display-message
4465 nil 'notice 'active
4466 'CTCP-PING ?n nick
4467 ?t (erc-sec-to-time delta)))
4468 (range-error
4469 (erc-display-message
4470 nil 'error 'active
4471 'bad-ping-response ?n nick ?t time))))))
4472
4473 (defvar erc-ctcp-reply-TIME-hook '(erc-ctcp-reply-TIME))
4474 (defun erc-ctcp-reply-TIME (proc nick login host to msg)
4475 "Handle a CTCP TIME reply."
4476 (when (string-match "^TIME\\s-+\\(.*\\)\\s-*$" msg)
4477 (let ((message (match-string 1 msg)))
4478 (erc-display-message
4479 nil 'notice 'active
4480 'CTCP-TIME ?n nick ?m message)))
4481 nil)
4482
4483 (defvar erc-ctcp-reply-VERSION-hook '(erc-ctcp-reply-VERSION))
4484 (defun erc-ctcp-reply-VERSION (proc nick login host to msg)
4485 "Handle a CTCP VERSION reply."
4486 (when (string-match "^VERSION\\s-+\\(.*\\)\\s-*$" msg)
4487 (let ((message (match-string 1 msg)))
4488 (erc-display-message
4489 nil 'notice 'active
4490 'CTCP-VERSION ?n nick ?m message)))
4491 nil)
4492
4493 (defun erc-process-away (proc away-p)
4494 "Toggle the away status of the user depending on the value of AWAY-P.
4495
4496 If nil, set the user as away.
4497 If non-nil, return from being away."
4498 (let ((sessionbuf (process-buffer proc)))
4499 (when sessionbuf
4500 (with-current-buffer sessionbuf
4501 (when erc-away-nickname
4502 (erc-log (format "erc-process-away: away-nick: %s, away-p: %s"
4503 erc-away-nickname away-p))
4504 (erc-cmd-NICK (if away-p
4505 erc-away-nickname
4506 erc-nick)))
4507 (cond
4508 (away-p
4509 (setq erc-away (current-time)))
4510 (t
4511 (let ((away-time erc-away))
4512 ;; away must be set to NIL BEFORE sending anything to prevent
4513 ;; an infinite recursion
4514 (setq erc-away nil)
4515 (with-current-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-connector'
5658 - `erc-session-server'
5659 - `erc-session-port'
5660 - `erc-session-full-name'
5661 - `erc-server-current-nick'"
5662 (setq erc-session-connector erc-server-connect-function
5663 erc-session-server (erc-compute-server server)
5664 erc-session-port (or port erc-default-port)
5665 erc-session-user-full-name (erc-compute-full-name name))
5666 (erc-set-current-nick (erc-compute-nick nick)))
5667
5668 (defun erc-compute-server (&optional server)
5669 "Return an IRC server name.
5670
5671 This tries a number of increasingly more default methods until a
5672 non-nil value is found.
5673
5674 - SERVER (the argument passed to this function)
5675 - The `erc-server' option
5676 - The value of the IRCSERVER environment variable
5677 - The `erc-default-server' variable"
5678 (or server
5679 erc-server
5680 (getenv "IRCSERVER")
5681 erc-default-server))
5682
5683 (defun erc-compute-nick (&optional nick)
5684 "Return user's IRC nick.
5685
5686 This tries a number of increasingly more default methods until a
5687 non-nil value is found.
5688
5689 - NICK (the argument passed to this function)
5690 - The `erc-nick' option
5691 - The value of the IRCNICK environment variable
5692 - The result from the `user-login-name' function"
5693 (or nick
5694 (if (consp erc-nick) (car erc-nick) erc-nick)
5695 (getenv "IRCNICK")
5696 (user-login-name)))
5697
5698
5699 (defun erc-compute-full-name (&optional full-name)
5700 "Return user's full name.
5701
5702 This tries a number of increasingly more default methods until a
5703 non-nil value is found.
5704
5705 - FULL-NAME (the argument passed to this function)
5706 - The `erc-user-full-name' option
5707 - The value of the IRCNAME environment variable
5708 - The result from the `user-full-name' function"
5709 (or full-name
5710 erc-user-full-name
5711 (getenv "IRCNAME")
5712 (if erc-anonymous-login "unknown" nil)
5713 (user-full-name)))
5714
5715 (defun erc-compute-port (&optional port)
5716 "Return a port for an IRC server.
5717
5718 This tries a number of increasingly more default methods until a
5719 non-nil value is found.
5720
5721 - PORT (the argument passed to this function)
5722 - The `erc-port' option
5723 - The `erc-default-port' variable"
5724 (or port erc-port erc-default-port))
5725
5726 ;; time routines
5727
5728 (defun erc-string-to-emacs-time (string)
5729 "Convert the long number represented by STRING into an Emacs format.
5730 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
5731 (let* ((n (string-to-number (concat string ".0"))))
5732 (list (truncate (/ n 65536))
5733 (truncate (mod n 65536)))))
5734
5735 (defun erc-emacs-time-to-erc-time (time)
5736 "Convert Emacs TIME to a number of seconds since the epoch."
5737 (when time
5738 (+ (* (nth 0 time) 65536.0) (nth 1 time))))
5739 ; (round (+ (* (nth 0 tm) 65536.0) (nth 1 tm))))
5740
5741 (defun erc-current-time ()
5742 "Return the `current-time' as a number of seconds since the epoch.
5743
5744 See also `erc-emacs-time-to-erc-time'."
5745 (erc-emacs-time-to-erc-time (current-time)))
5746
5747 (defun erc-time-diff (t1 t2)
5748 "Return the time difference in seconds between T1 and T2."
5749 (abs (- t2 t1)))
5750
5751 (defun erc-time-gt (t1 t2)
5752 "Check whether T1 > T2."
5753 (> t1 t2))
5754
5755 (defun erc-sec-to-time (ns)
5756 "Convert NS to a time string HH:MM.SS."
5757 (setq ns (truncate ns))
5758 (format "%02d:%02d.%02d"
5759 (/ ns 3600)
5760 (/ (% ns 3600) 60)
5761 (% ns 60)))
5762
5763 (defun erc-seconds-to-string (seconds)
5764 "Convert a number of SECONDS into an English phrase."
5765 (let (days hours minutes format-args output)
5766 (setq days (/ seconds 86400)
5767 seconds (% seconds 86400)
5768 hours (/ seconds 3600)
5769 seconds (% seconds 3600)
5770 minutes (/ seconds 60)
5771 seconds (% seconds 60)
5772 format-args (if (> days 0)
5773 `("%d days, %d hours, %d minutes, %d seconds"
5774 ,days ,hours ,minutes ,seconds)
5775 (if (> hours 0)
5776 `("%d hours, %d minutes, %d seconds"
5777 ,hours ,minutes ,seconds)
5778 (if (> minutes 0)
5779 `("%d minutes, %d seconds" ,minutes ,seconds)
5780 `("%d seconds" ,seconds))))
5781 output (apply 'format format-args))
5782 ;; Change all "1 units" to "1 unit".
5783 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
5784 (setq output (erc-replace-match-subexpression-in-string
5785 "" output (match-string 2 output) 2 (match-beginning 2))))
5786 output))
5787
5788
5789 ;; info
5790
5791 (defconst erc-clientinfo-alist
5792 '(("ACTION" . "is used to inform about one's current activity")
5793 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
5794 ("ECHO" . "echoes its arguments back")
5795 ("FINGER" . "shows user's name, location, and idle time")
5796 ("PING" . "measures delay between peers")
5797 ("TIME" . "shows client-side time")
5798 ("USERINFO" . "shows information provided by a user")
5799 ("VERSION" . "shows client type and version"))
5800 "Alist of CTCP CLIENTINFO for ERC commands.")
5801
5802 (defun erc-client-info (s)
5803 "Return CTCP CLIENTINFO on command S.
5804 If S is nil or an empty string then return general CLIENTINFO."
5805 (if (or (not s) (string= s ""))
5806 (concat
5807 (apply #'concat
5808 (mapcar (lambda (e)
5809 (concat (car e) " "))
5810 erc-clientinfo-alist))
5811 ": use CLIENTINFO <COMMAND> to get more specific information")
5812 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
5813 (if h
5814 (concat s " " (cdr h))
5815 (concat s ": unknown command")))))
5816
5817 ;; Hook functions
5818
5819 (defun erc-directory-writable-p (dir)
5820 "Determine whether DIR is a writable directory.
5821 If it doesn't exist, create it."
5822 (unless (file-attributes dir) (make-directory dir))
5823 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
5824
5825 (defun erc-kill-query-buffers (process)
5826 "Kill all buffers of PROCESS."
5827 ;; here, we only want to match the channel buffers, to avoid
5828 ;; "selecting killed buffers" b0rkage.
5829 (erc-with-all-buffers-of-server process
5830 (lambda ()
5831 (not (erc-server-buffer-p)))
5832 (kill-buffer (current-buffer))))
5833
5834 (defun erc-nick-at-point ()
5835 "Give information about the nickname at `point'.
5836
5837 If called interactively, give a human readable message in the
5838 minibuffer. If called programatically, return the corresponding
5839 entry of `channel-members'."
5840 (interactive)
5841 (require 'thingatpt)
5842 (let* ((word (word-at-point))
5843 (channel-data (erc-get-channel-user word))
5844 (cuser (cdr channel-data))
5845 (user (if channel-data
5846 (car channel-data)
5847 (erc-get-server-user word)))
5848 host login full-name info nick op voice)
5849 (when user
5850 (setq nick (erc-server-user-nickname user)
5851 host (erc-server-user-host user)
5852 login (erc-server-user-login user)
5853 full-name (erc-server-user-full-name user)
5854 info (erc-server-user-info user))
5855 (if cuser
5856 (setq op (erc-channel-user-op cuser)
5857 voice (erc-channel-user-voice cuser)))
5858 (if (interactive-p)
5859 (message "%s is %s@%s%s%s"
5860 nick login host
5861 (if full-name (format " (%s)" full-name) "")
5862 (if (or op voice)
5863 (format " and is +%s%s on %s"
5864 (if op "o" "")
5865 (if voice "v" "")
5866 (erc-default-target))
5867 ""))
5868 user))))
5869
5870 (defun erc-away-time ()
5871 "Return non-nil if the current ERC process is set away.
5872
5873 In particular, the time that we were set away is returned.
5874 See `current-time' for details on the time format."
5875 (erc-with-server-buffer erc-away))
5876
5877 ;; Mode line handling
5878
5879 (defcustom erc-mode-line-format "%S %a"
5880 "A string to be formatted and shown in the mode-line in `erc-mode'.
5881
5882 The string is formatted using `format-spec' and the result is set as the value
5883 of `mode-line-buffer-identification'.
5884
5885 The following characters are replaced:
5886 %a: String indicating away status or \"\" if you are not away
5887 %l: The estimated lag time to the server
5888 %m: The modes of the channel
5889 %n: The current nick name
5890 %N: The name of the network
5891 %o: The topic of the channel
5892 %p: The session port
5893 %t: The name of the target (channel, nickname, or servername:port)
5894 %s: In the server-buffer, this gets filled with the value of
5895 `erc-server-announced-name', in a channel, the value of
5896 (erc-default-target) also get concatenated.
5897 %S: In the server-buffer, this gets filled with the value of
5898 `erc-network', in a channel, the value of (erc-default-target)
5899 also get concatenated."
5900 :group 'erc-mode-line-and-header
5901 :type 'string)
5902
5903 (defcustom erc-header-line-format "%n on %t (%m,%l) %o"
5904 "A string to be formatted and shown in the header-line in `erc-mode'.
5905 Only used starting in Emacs 21.
5906
5907 Set this to nil if you do not want the header line to be
5908 displayed.
5909
5910 See `erc-mode-line-format' for which characters are can be used."
5911 :group 'erc-mode-line-and-header
5912 :set (lambda (sym val)
5913 (set sym val)
5914 (when (fboundp 'erc-update-mode-line)
5915 (erc-update-mode-line nil)))
5916 :type '(choice (const :tag "Disabled" nil)
5917 string))
5918
5919 (defcustom erc-header-line-uses-tabbar-p nil
5920 "Use tabbar mode instead of the header line to display the header."
5921 :group 'erc-mode-line-and-header
5922 :type 'boolean)
5923
5924 (defcustom erc-header-line-uses-help-echo-p t
5925 "Show the contents of the header line in the echo area or as a tooltip
5926 when you move point into the header line."
5927 :group 'erc-mode-line-and-header
5928 :type 'boolean)
5929
5930 (defcustom erc-header-line-face-method nil
5931 "Determine what method to use when colorizing the header line text.
5932
5933 If nil, don't colorize the header text.
5934 If given a function, call it and use the resulting face name.
5935 Otherwise, use the `erc-header-line' face."
5936 :group 'erc-mode-line-and-header
5937 :type '(choice (const :tag "Don't colorize" nil)
5938 (const :tag "Use the erc-header-line face" t)
5939 (function :tag "Call a function")))
5940
5941 (defcustom erc-show-channel-key-p t
5942 "Show the channel key in the header line."
5943 :group 'erc-paranoia
5944 :type 'boolean)
5945
5946 (defcustom erc-common-server-suffixes
5947 '(("openprojects.net$" . "OPN")
5948 ("freenode.net$" . "freenode")
5949 ("oftc.net$" . "OFTC"))
5950 "Alist of common server name suffixes.
5951 This variable is used in mode-line display to save screen
5952 real estate. Set it to nil if you want to avoid changing
5953 displayed hostnames."
5954 :group 'erc-mode-line-and-header
5955 :type 'alist)
5956
5957 (defcustom erc-mode-line-away-status-format
5958 "(AWAY since %a %b %d %H:%M) "
5959 "When you're away on a server, this is shown in the mode line.
5960 This should be a string with substitution variables recognized by
5961 `format-time-string'."
5962 :group 'erc-mode-line-and-header
5963 :type 'string)
5964
5965 (defun erc-shorten-server-name (server-name)
5966 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
5967 (if (stringp server-name)
5968 (with-temp-buffer
5969 (insert server-name)
5970 (let ((alist erc-common-server-suffixes))
5971 (while alist
5972 (goto-char (point-min))
5973 (if (re-search-forward (caar alist) nil t)
5974 (replace-match (cdar alist)))
5975 (setq alist (cdr alist))))
5976 (buffer-string))))
5977
5978 (defun erc-format-target ()
5979 "Return the name of the target (channel or nickname or servername:port)."
5980 (let ((target (erc-default-target)))
5981 (or target
5982 (concat (erc-shorten-server-name
5983 (or erc-server-announced-name
5984 erc-session-server))
5985 ":" (erc-port-to-string erc-session-port)))))
5986
5987 (defun erc-format-target-and/or-server ()
5988 "Return the server name or the current target and server name combined."
5989 (let ((server-name (erc-shorten-server-name
5990 (or erc-server-announced-name
5991 erc-session-server))))
5992 (cond ((erc-default-target)
5993 (concat (erc-string-no-properties (erc-default-target))
5994 "@" server-name))
5995 (server-name server-name)
5996 (t (buffer-name (current-buffer))))))
5997
5998 (defun erc-format-network ()
5999 "Return the name of the network we are currently on."
6000 (let ((network (and (fboundp 'erc-network-name) (erc-network-name))))
6001 (if (and network (symbolp network))
6002 (symbol-name network)
6003 "")))
6004
6005 (defun erc-format-target-and/or-network ()
6006 "Return the network or the current target and network combined.
6007 If the name of the network is not available, then use the
6008 shortened server name instead."
6009 (let ((network-name (or (and (fboundp 'erc-network-name) (erc-network-name))
6010 (erc-shorten-server-name
6011 (or erc-server-announced-name
6012 erc-session-server)))))
6013 (when (and network-name (symbolp network-name))
6014 (setq network-name (symbol-name network-name)))
6015 (cond ((erc-default-target)
6016 (concat (erc-string-no-properties (erc-default-target))
6017 "@" network-name))
6018 (network-name network-name)
6019 (t (buffer-name (current-buffer))))))
6020
6021 (defun erc-format-away-status ()
6022 "Return a formatted `erc-mode-line-away-status-format'
6023 if `erc-away' is non-nil."
6024 (let ((a (erc-away-time)))
6025 (if a
6026 (format-time-string erc-mode-line-away-status-format a)
6027 "")))
6028
6029 (defun erc-format-channel-modes ()
6030 "Return the current channel's modes."
6031 (concat (apply 'concat
6032 "+" erc-channel-modes)
6033 (cond ((and erc-channel-user-limit erc-channel-key)
6034 (if erc-show-channel-key-p
6035 (format "lk %.0f %s" erc-channel-user-limit
6036 erc-channel-key)
6037 (format "kl %.0f" erc-channel-user-limit)))
6038 (erc-channel-user-limit
6039 ;; Emacs has no bignums
6040 (format "l %.0f" erc-channel-user-limit))
6041 (erc-channel-key
6042 (if erc-show-channel-key-p
6043 (format "k %s" erc-channel-key)
6044 "k"))
6045 (t nil))))
6046
6047 (defun erc-format-lag-time ()
6048 "Return the estimated lag time to server, `erc-server-lag'."
6049 (let ((lag (erc-with-server-buffer erc-server-lag)))
6050 (cond (lag (format "lag:%.0f" lag))
6051 (t ""))))
6052
6053 ;; erc-goodies is required at end of this file.
6054 (declare-function erc-controls-strip "erc-goodies" (str))
6055
6056 (defvar tabbar--local-hlf)
6057
6058 (defun erc-update-mode-line-buffer (buffer)
6059 "Update the mode line in a single ERC buffer BUFFER."
6060 (with-current-buffer buffer
6061 (let ((spec (format-spec-make
6062 ?a (erc-format-away-status)
6063 ?l (erc-format-lag-time)
6064 ?m (erc-format-channel-modes)
6065 ?n (or (erc-current-nick) "")
6066 ?N (erc-format-network)
6067 ?o (erc-controls-strip erc-channel-topic)
6068 ?p (erc-port-to-string erc-session-port)
6069 ?s (erc-format-target-and/or-server)
6070 ?S (erc-format-target-and/or-network)
6071 ?t (erc-format-target)))
6072 (process-status (cond ((and (erc-server-process-alive)
6073 (not erc-server-connected))
6074 ":connecting")
6075 ((erc-server-process-alive)
6076 "")
6077 (t
6078 ": CLOSED")))
6079 (face (cond ((eq erc-header-line-face-method nil)
6080 nil)
6081 ((functionp erc-header-line-face-method)
6082 (funcall erc-header-line-face-method))
6083 (t
6084 'erc-header-line))))
6085 (cond ((featurep 'xemacs)
6086 (setq modeline-buffer-identification
6087 (list (format-spec erc-mode-line-format spec)))
6088 (setq modeline-process (list process-status)))
6089 (t
6090 (setq mode-line-buffer-identification
6091 (list (format-spec erc-mode-line-format spec)))
6092 (setq mode-line-process (list process-status))))
6093 (when (boundp 'header-line-format)
6094 (let ((header (if erc-header-line-format
6095 (format-spec erc-header-line-format spec)
6096 nil)))
6097 (cond (erc-header-line-uses-tabbar-p
6098 (set (make-local-variable 'tabbar--local-hlf)
6099 header-line-format)
6100 (kill-local-variable 'header-line-format))
6101 ((null header)
6102 (setq header-line-format nil))
6103 (erc-header-line-uses-help-echo-p
6104 (let ((help-echo (with-temp-buffer
6105 (insert header)
6106 (fill-region (point-min) (point-max))
6107 (buffer-string))))
6108 (setq header-line-format
6109 (erc-replace-regexp-in-string
6110 "%"
6111 "%%"
6112 (if face
6113 (erc-propertize header 'help-echo help-echo
6114 'face face)
6115 (erc-propertize header 'help-echo help-echo))))))
6116 (t (setq header-line-format
6117 (if face
6118 (erc-propertize header 'face face)
6119 header)))))))
6120 (if (featurep 'xemacs)
6121 (redraw-modeline)
6122 (force-mode-line-update))))
6123
6124 (defun erc-update-mode-line (&optional buffer)
6125 "Update the mode line in BUFFER.
6126
6127 If BUFFER is nil, update the mode line in all ERC buffers."
6128 (if (and buffer (bufferp buffer))
6129 (erc-update-mode-line-buffer buffer)
6130 (dolist (buf (erc-buffer-list))
6131 (when (buffer-live-p buf)
6132 (erc-update-mode-line-buffer buf)))))
6133
6134 ;; Miscellaneous
6135
6136 (defun erc-port-to-string (p)
6137 "Convert port P to a string.
6138 P may be an integer or a service name."
6139 (if (integerp p)
6140 (int-to-string p)
6141 p))
6142
6143 (defun erc-string-to-port (s)
6144 "Convert string S to either an integer port number or a service name."
6145 (if (numberp s)
6146 s
6147 (let ((n (string-to-number s)))
6148 (if (= n 0)
6149 s
6150 n))))
6151
6152 (defun erc-version (&optional here)
6153 "Show the version number of ERC in the minibuffer.
6154 If optional argument HERE is non-nil, insert version number at point."
6155 (interactive "P")
6156 (let ((version-string
6157 (format "ERC %s (GNU Emacs %s)" erc-version-string emacs-version)))
6158 (if here
6159 (insert version-string)
6160 (if (interactive-p)
6161 (message "%s" version-string)
6162 version-string))))
6163
6164 (defun erc-modes (&optional here)
6165 "Show the active ERC modes in the minibuffer.
6166 If optional argument HERE is non-nil, insert version number at point."
6167 (interactive "P")
6168 (let ((string
6169 (mapconcat 'identity
6170 (let (modes (case-fold-search nil))
6171 (dolist (var (apropos-internal "^erc-.*mode$"))
6172 (when (and (boundp var)
6173 (symbol-value var))
6174 (setq modes (cons (symbol-name var)
6175 modes))))
6176 modes)
6177 ", ")))
6178 (if here
6179 (insert string)
6180 (if (interactive-p)
6181 (message "%s" string)
6182 string))))
6183
6184 (defun erc-trim-string (s)
6185 "Trim leading and trailing spaces off S."
6186 (cond
6187 ((not (stringp s)) nil)
6188 ((string-match "^\\s-*$" s)
6189 "")
6190 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
6191 (match-string 1 s))
6192 (t
6193 s)))
6194
6195 (defun erc-arrange-session-in-multiple-windows ()
6196 "Open a window for every non-server buffer related to `erc-session-server'.
6197
6198 All windows are opened in the current frame."
6199 (interactive)
6200 (unless erc-server-process
6201 (error "No erc-server-process found in current buffer"))
6202 (let ((bufs (erc-buffer-list nil erc-server-process)))
6203 (when bufs
6204 (delete-other-windows)
6205 (switch-to-buffer (car bufs))
6206 (setq bufs (cdr bufs))
6207 (while bufs
6208 (split-window)
6209 (other-window 1)
6210 (switch-to-buffer (car bufs))
6211 (setq bufs (cdr bufs))
6212 (balance-windows)))))
6213
6214 (defun erc-popup-input-buffer ()
6215 "Provide an input buffer."
6216 (interactive)
6217 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
6218 (mode (intern
6219 (completing-read
6220 "Mode: "
6221 (mapcar (lambda (e)
6222 (list (symbol-name e)))
6223 (apropos-internal "-mode$" 'commandp))
6224 nil t))))
6225 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
6226 (funcall mode)
6227 (narrow-to-region (point) (point))
6228 (shrink-window-if-larger-than-buffer)))
6229
6230 ;;; Message catalog
6231
6232 (defun erc-make-message-variable-name (catalog entry)
6233 "Create a variable name corresponding to CATALOG's ENTRY."
6234 (intern (concat "erc-message-"
6235 (symbol-name catalog) "-" (symbol-name entry))))
6236
6237 (defun erc-define-catalog-entry (catalog entry format-spec)
6238 "Set CATALOG's ENTRY to FORMAT-SPEC."
6239 (set (erc-make-message-variable-name catalog entry)
6240 format-spec))
6241
6242 (defun erc-define-catalog (catalog entries)
6243 "Define a CATALOG according to ENTRIES."
6244 (dolist (entry entries)
6245 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
6246
6247 (erc-define-catalog
6248 'english
6249 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
6250 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
6251 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
6252 (cannot-find-file . "Cannot find file %f")
6253 (cannot-read-file . "Cannot read file %f")
6254 (connect . "Connecting to %S:%p... ")
6255 (country . "%c")
6256 (country-unknown . "%d: No such domain")
6257 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
6258 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
6259 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
6260 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
6261 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
6262 (flood-strict-mode
6263 . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
6264 (disconnected . "\n\nConnection failed! Re-establishing connection...\n")
6265 (disconnected-noreconnect
6266 . "\n\nConnection failed! Not re-establishing connection.\n")
6267 (finished . "\n\n*** ERC finished ***\n")
6268 (terminated . "\n\n*** ERC terminated: %e\n")
6269 (login . "Logging in as \'%n\'...")
6270 (nick-in-use . "%n is in use. Choose new nickname: ")
6271 (nick-too-long
6272 . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
6273 (no-default-channel . "No default channel")
6274 (no-invitation . "You've got no invitation")
6275 (no-target . "No target")
6276 (ops . "%i operator%s: %o")
6277 (ops-none . "No operators in this channel.")
6278 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
6279 (variable-not-bound . "Variable not bound!")
6280 (ACTION . "* %n %a")
6281 (CTCP-CLIENTINFO . "Client info for %n: %m")
6282 (CTCP-ECHO . "Echo %n: %m")
6283 (CTCP-FINGER . "Finger info for %n: %m")
6284 (CTCP-PING . "Ping time to %n is %t")
6285 (CTCP-TIME . "Time by %n is %m")
6286 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
6287 (CTCP-VERSION . "Version for %n is %m")
6288 (ERROR . "==> ERROR from %s: %c\n")
6289 (INVITE . "%n (%u@%h) invites you to channel %c")
6290 (JOIN . "%n (%u@%h) has joined channel %c")
6291 (JOIN-you . "You have joined channel %c")
6292 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
6293 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
6294 (KICK-by-you . "You have kicked %k off channel %c: %r")
6295 (MODE . "%n (%u@%h) has changed mode for %t to %m")
6296 (MODE-nick . "%n has changed mode for %t to %m")
6297 (NICK . "%n (%u@%h) is now known as %N")
6298 (NICK-you . "Your new nickname is %N")
6299 (PART . erc-message-english-PART)
6300 (PING . "PING from server (last: %s sec. ago)")
6301 (PONG . "PONG from %h (%i second%s)")
6302 (QUIT . "%n (%u@%h) has quit: %r")
6303 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
6304 (WALLOPS . "Wallops from %n: %m")
6305 (s004 . "%s %v %U %C")
6306 (s221 . "User modes for %n: %m")
6307 (s252 . "%i operator(s) online")
6308 (s253 . "%i unknown connection(s)")
6309 (s254 . "%i channels formed")
6310 (s275 . "%n %m")
6311 (s301 . "%n is AWAY: %r")
6312 (s303 . "Is online: %n")
6313 (s305 . "%m")
6314 (s306 . "%m")
6315 (s307 . "%n %m")
6316 (s311 . "%n is %f (%u@%h)")
6317 (s312 . "%n is/was on server %s (%c)")
6318 (s313 . "%n is an IRC operator")
6319 (s314 . "%n was %f (%u@%h)")
6320 (s317 . "%n has been idle for %i")
6321 (s317-on-since . "%n has been idle for %i, on since %t")
6322 (s319 . "%n is on channel(s): %c")
6323 (s320 . "%n is an identified user")
6324 (s321 . "Channel Users Topic")
6325 (s322 . "%c [%u] %t")
6326 (s324 . "%c modes: %m")
6327 (s328 . "%c URL: %u")
6328 (s329 . "%c was created on %t")
6329 (s330 . "%n %a %i")
6330 (s331 . "No topic is set for %c")
6331 (s332 . "Topic for %c: %T")
6332 (s333 . "%c: topic set by %n, %t")
6333 (s341 . "Inviting %n to channel %c")
6334 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
6335 (s353 . "Users on %c: %u")
6336 (s367 . "Ban for %b on %c")
6337 (s367-set-by . "Ban for %b on %c set by %s on %t")
6338 (s368 . "Banlist of %c ends.")
6339 (s379 . "%c: Forwarded to %f")
6340 (s391 . "The time at %s is %t")
6341 (s401 . "%n: No such nick/channel")
6342 (s403 . "%c: No such channel")
6343 (s404 . "%c: Cannot send to channel")
6344 (s405 . "%c: You have joined too many channels")
6345 (s406 . "%n: There was no such nickname")
6346 (s412 . "No text to send")
6347 (s421 . "%c: Unknown command")
6348 (s431 . "No nickname given")
6349 (s432 . "%n is an erroneous nickname")
6350 (s442 . "%c: You're not on that channel")
6351 (s445 . "SUMMON has been disabled")
6352 (s446 . "USERS has been disabled")
6353 (s451 . "You have not registered")
6354 (s461 . "%c: not enough parameters")
6355 (s462 . "Unauthorized command (already registered)")
6356 (s463 . "Your host isn't among the privileged")
6357 (s464 . "Password incorrect")
6358 (s465 . "You are banned from this server")
6359 (s474 . "You can't join %c because you're banned (+b)")
6360 (s475 . "You must specify the correct channel key (+k) to join %c")
6361 (s481 . "Permission Denied - You're not an IRC operator")
6362 (s482 . "You need to be a channel operator of %c to do that")
6363 (s483 . "You can't kill a server!")
6364 (s484 . "Your connection is restricted!")
6365 (s485 . "You're not the original channel operator")
6366 (s491 . "No O-lines for your host")
6367 (s501 . "Unknown MODE flag")
6368 (s502 . "You can't change modes for other users")))
6369
6370 (defun erc-message-english-PART (&rest args)
6371 "Format a proper PART message.
6372
6373 This function is an example on what could be done with formatting
6374 functions."
6375 (let ((nick (cadr (memq ?n args)))
6376 (user (cadr (memq ?u args)))
6377 (host (cadr (memq ?h args)))
6378 (channel (cadr (memq ?c args)))
6379 (reason (cadr (memq ?r args))))
6380 (if (string= nick (erc-current-nick))
6381 (format "You have left channel %s" channel)
6382 (format "%s (%s@%s) has left channel %s%s"
6383 nick user host channel
6384 (if (not (string= reason ""))
6385 (format ": %s"
6386 (erc-replace-regexp-in-string "%" "%%" reason))
6387 "")))))
6388
6389
6390 (defvar erc-current-message-catalog 'english)
6391 (make-variable-buffer-local 'erc-current-message-catalog)
6392
6393 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6394 "Retrieve ENTRY from CATALOG.
6395
6396 If CATALOG is nil, `erc-current-message-catalog' is used.
6397
6398 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6399 english, catalog."
6400 (unless catalog (setq catalog erc-current-message-catalog))
6401 (let ((var (erc-make-message-variable-name catalog entry)))
6402 (if (boundp var)
6403 (symbol-value var)
6404 (when (boundp (erc-make-message-variable-name 'english entry))
6405 (symbol-value (erc-make-message-variable-name 'english entry))))))
6406
6407 (defun erc-format-message (msg &rest args)
6408 "Format MSG according to ARGS.
6409
6410 See also `format-spec'."
6411 (when (eq (logand (length args) 1) 1) ; oddp
6412 (error "Obscure usage of this function appeared"))
6413 (let ((entry (erc-retrieve-catalog-entry msg)))
6414 (when (not entry)
6415 (error "No format spec for message %s" msg))
6416 (when (functionp entry)
6417 (setq entry (apply entry args)))
6418 (format-spec entry (apply 'format-spec-make args))))
6419
6420 ;;; Various hook functions
6421
6422 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6423
6424 (defcustom erc-kill-server-hook '(erc-kill-server)
6425 "*Invoked whenever a server-buffer is killed via `kill-buffer'."
6426 :group 'erc-hooks
6427 :type 'hook)
6428
6429 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6430 "*Invoked whenever a channel-buffer is killed via `kill-buffer'."
6431 :group 'erc-hooks
6432 :type 'hook)
6433
6434 (defcustom erc-kill-buffer-hook nil
6435 "*Hook run whenever a non-server or channel buffer is killed.
6436
6437 See also `kill-buffer'."
6438 :group 'erc-hooks
6439 :type 'hook)
6440
6441 (defun erc-kill-buffer-function ()
6442 "Function to call when an ERC buffer is killed.
6443 This function should be on `kill-buffer-hook'.
6444 When the current buffer is in `erc-mode', this function will run
6445 one of the following hooks:
6446 `erc-kill-server-hook' if the server buffer was killed,
6447 `erc-kill-channel-hook' if a channel buffer was killed,
6448 or `erc-kill-buffer-hook' if any other buffer."
6449 (when (eq major-mode 'erc-mode)
6450 (erc-remove-channel-users)
6451 (cond
6452 ((eq (erc-server-buffer) (current-buffer))
6453 (run-hooks 'erc-kill-server-hook))
6454 ((erc-channel-p (erc-default-target))
6455 (run-hooks 'erc-kill-channel-hook))
6456 (t
6457 (run-hooks 'erc-kill-buffer-hook)))))
6458
6459 (defun erc-kill-server ()
6460 "Sends a QUIT command to the server when the server buffer is killed.
6461 This function should be on `erc-kill-server-hook'."
6462 (when (erc-server-process-alive)
6463 (setq erc-server-quitting t)
6464 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6465
6466 (defun erc-kill-channel ()
6467 "Sends a PART command to the server when the channel buffer is killed.
6468 This function should be on `erc-kill-channel-hook'."
6469 (when (erc-server-process-alive)
6470 (let ((tgt (erc-default-target)))
6471 (erc-server-send (format "PART %s :%s" tgt
6472 (funcall erc-part-reason nil))
6473 nil tgt))))
6474
6475 ;;; Dealing with `erc-parsed'
6476
6477 (defun erc-find-parsed-property ()
6478 "Find the next occurrence of the `erc-parsed' text property."
6479 (text-property-not-all (point-min) (point-max) 'erc-parsed nil))
6480
6481 (defun erc-restore-text-properties ()
6482 "Restore the property 'erc-parsed for the region."
6483 (let ((parsed-posn (erc-find-parsed-property)))
6484 (put-text-property
6485 (point-min) (point-max)
6486 'erc-parsed (when parsed-posn (erc-get-parsed-vector parsed-posn)))))
6487
6488 (defun erc-get-parsed-vector (point)
6489 "Return the whole parsed vector on POINT."
6490 (get-text-property point 'erc-parsed))
6491
6492 (defun erc-get-parsed-vector-nick (vect)
6493 "Return nickname in the parsed vector VECT."
6494 (let* ((untreated-nick (and vect (erc-response.sender vect)))
6495 (maybe-nick (when untreated-nick
6496 (car (split-string untreated-nick "!")))))
6497 (when (and (not (null maybe-nick))
6498 (erc-is-valid-nick-p maybe-nick))
6499 untreated-nick)))
6500
6501 (defun erc-get-parsed-vector-type (vect)
6502 "Return message type in the parsed vector VECT."
6503 (and vect
6504 (erc-response.command vect)))
6505
6506 ;; Teach url.el how to open irc:// URLs with ERC.
6507 ;; To activate, customize `url-irc-function' to `url-irc-erc'.
6508
6509 ;;;###autoload
6510 (defun erc-handle-irc-url (host port channel user password)
6511 "Use ERC to IRC on HOST:PORT in CHANNEL as USER with PASSWORD.
6512 If ERC is already connected to HOST:PORT, simply /join CHANNEL.
6513 Otherwise, connect to HOST:PORT as USER and /join CHANNEL."
6514 (let ((server-buffer
6515 (car (erc-buffer-filter
6516 (lambda ()
6517 (and (string-equal erc-session-server host)
6518 (= erc-session-port port)
6519 (erc-open-server-buffer-p)))))))
6520 (with-current-buffer (or server-buffer (current-buffer))
6521 (if (and server-buffer channel)
6522 (erc-cmd-JOIN channel)
6523 (erc-open host port (or user (erc-compute-nick)) (erc-compute-full-name)
6524 (not server-buffer) password nil channel
6525 (when server-buffer
6526 (get-buffer-process server-buffer)))))))
6527
6528 (provide 'erc)
6529
6530 ;;; Deprecated. We might eventually stop requiring the goodies automatically.
6531 ;;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6532 ;;; avoid a recursive require error when byte-compiling the entire package.
6533 (require 'erc-goodies)
6534
6535 ;;; erc.el ends here
6536 ;;
6537 ;; Local Variables:
6538 ;; outline-regexp: ";;+"
6539 ;; indent-tabs-mode: t
6540 ;; tab-width: 8
6541 ;; End:
6542
6543 ;; arch-tag: d19587f6-627e-48c1-8d86-58595fa3eca3