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