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