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