]> code.delx.au - gnu-emacs/blob - lisp/erc/erc.el
Merge from emacs-24; up to 2014-07-21T01:34:03Z!monnier@iro.umontreal.ca
[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 (delete "" (split-string names-string)))
4774 name op voice halfop admin owner)
4775 (let ((erc-channel-members-changed-hook nil))
4776 (dolist (item names)
4777 (let ((updatep t)
4778 (ch (aref item 0)))
4779 (setq name item op 'off voice 'off halfop 'off admin 'off owner 'off)
4780 (if (rassq ch prefix)
4781 (if (= (length item) 1)
4782 (setq updatep nil)
4783 (setq name (substring item 1))
4784 (setf (pcase ch
4785 ((pred (eq voice-ch)) voice)
4786 ((pred (eq hop-ch)) halfop)
4787 ((pred (eq op-ch)) op)
4788 ((pred (eq adm-ch)) admin)
4789 ((pred (eq own-ch)) owner)
4790 (_ (error "Unknown prefix char `%S'" ch) voice))
4791 'on)))
4792 (when updatep
4793 (puthash (erc-downcase name) t
4794 erc-channel-new-member-names)
4795 (erc-update-current-channel-member
4796 name name t voice halfop op admin owner)))))
4797 (run-hooks 'erc-channel-members-changed-hook)))
4798
4799
4800 (defcustom erc-channel-members-changed-hook nil
4801 "This hook is called every time the variable `channel-members' changes.
4802 The buffer where the change happened is current while this hook is called."
4803 :group 'erc-hooks
4804 :type 'hook)
4805
4806 (defun erc-update-user-nick (nick &optional new-nick
4807 host login full-name info)
4808 "Update the stored user information for the user with nickname NICK.
4809
4810 See also: `erc-update-user'."
4811 (erc-update-user (erc-get-server-user nick) new-nick
4812 host login full-name info))
4813
4814 (defun erc-update-user (user &optional new-nick
4815 host login full-name info)
4816 "Update user info for USER. USER must be an erc-server-user
4817 struct. Any of NEW-NICK, HOST, LOGIN, FULL-NAME, INFO which are
4818 non-nil and not equal to the existing values for USER are used to
4819 replace the stored values in USER.
4820
4821 If, and only if, a change is made,
4822 `erc-channel-members-changed-hook' is run for each channel for
4823 which USER is a member, and t is returned."
4824 (let (changed)
4825 (when user
4826 (when (and new-nick
4827 (not (equal (erc-server-user-nickname user)
4828 new-nick)))
4829 (setq changed t)
4830 (erc-change-user-nickname user new-nick))
4831 (when (and host
4832 (not (equal (erc-server-user-host user) host)))
4833 (setq changed t)
4834 (setf (erc-server-user-host user) host))
4835 (when (and login
4836 (not (equal (erc-server-user-login user) login)))
4837 (setq changed t)
4838 (setf (erc-server-user-login user) login))
4839 (when (and full-name
4840 (not (equal (erc-server-user-full-name user)
4841 full-name)))
4842 (setq changed t)
4843 (setf (erc-server-user-full-name user) full-name))
4844 (when (and info
4845 (not (equal (erc-server-user-info user) info)))
4846 (setq changed t)
4847 (setf (erc-server-user-info user) info))
4848 (if changed
4849 (dolist (buf (erc-server-user-buffers user))
4850 (if (buffer-live-p buf)
4851 (with-current-buffer buf
4852 (run-hooks 'erc-channel-members-changed-hook))))))
4853 changed))
4854
4855 (defun erc-update-current-channel-member
4856 (nick new-nick &optional add voice halfop op admin owner host login full-name info
4857 update-message-time)
4858 "Update the stored user information for the user with nickname NICK.
4859 `erc-update-user' is called to handle changes to nickname,
4860 HOST, LOGIN, FULL-NAME, and INFO. If VOICE HALFOP OP ADMIN or OWNER
4861 are non-nil, they must be equal to either `on' or `off', in which
4862 case the status of the user in the current channel is changed accordingly.
4863 If UPDATE-MESSAGE-TIME is non-nil, the last-message-time of the user
4864 in the current channel is set to (current-time).
4865
4866 If ADD is non-nil, the user will be added with the specified
4867 information if it is not already present in the user or channel
4868 lists.
4869
4870 If, and only if, changes are made, or the user is added,
4871 `erc-channel-members-updated-hook' is run, and t is returned.
4872
4873 See also: `erc-update-user' and `erc-update-channel-member'."
4874 (let* (changed user-changed
4875 (channel-data (erc-get-channel-user nick))
4876 (cuser (cdr channel-data))
4877 (user (if channel-data (car channel-data)
4878 (erc-get-server-user nick))))
4879 (if cuser
4880 (progn
4881 (erc-log (format "update-member: user = %S, cuser = %S" user cuser))
4882 (when (and voice
4883 (not (eq (erc-channel-user-voice cuser) voice)))
4884 (setq changed t)
4885 (setf (erc-channel-user-voice cuser)
4886 (cond ((eq voice 'on) t)
4887 ((eq voice 'off) nil)
4888 (t voice))))
4889 (when (and halfop
4890 (not (eq (erc-channel-user-halfop cuser) halfop)))
4891 (setq changed t)
4892 (setf (erc-channel-user-halfop cuser)
4893 (cond ((eq halfop 'on) t)
4894 ((eq halfop 'off) nil)
4895 (t halfop))))
4896 (when (and op
4897 (not (eq (erc-channel-user-op cuser) op)))
4898 (setq changed t)
4899 (setf (erc-channel-user-op cuser)
4900 (cond ((eq op 'on) t)
4901 ((eq op 'off) nil)
4902 (t op))))
4903 (when (and admin
4904 (not (eq (erc-channel-user-admin cuser) admin)))
4905 (setq changed t)
4906 (setf (erc-channel-user-admin cuser)
4907 (cond ((eq admin 'on) t)
4908 ((eq admin 'off) nil)
4909 (t admin))))
4910 (when (and owner
4911 (not (eq (erc-channel-user-owner cuser) owner)))
4912 (setq changed t)
4913 (setf (erc-channel-user-owner cuser)
4914 (cond ((eq owner 'on) t)
4915 ((eq owner 'off) nil)
4916 (t owner))))
4917 (when update-message-time
4918 (setf (erc-channel-user-last-message-time cuser) (current-time)))
4919 (setq user-changed
4920 (erc-update-user user new-nick
4921 host login full-name info)))
4922 (when add
4923 (if (null user)
4924 (progn
4925 (setq user (make-erc-server-user
4926 :nickname nick
4927 :host host
4928 :full-name full-name
4929 :login login
4930 :info info
4931 :buffers (list (current-buffer))))
4932 (erc-add-server-user nick user))
4933 (setf (erc-server-user-buffers user)
4934 (cons (current-buffer)
4935 (erc-server-user-buffers user))))
4936 (setq cuser (make-erc-channel-user
4937 :voice (cond ((eq voice 'on) t)
4938 ((eq voice 'off) nil)
4939 (t voice))
4940 :halfop (cond ((eq halfop 'on) t)
4941 ((eq halfop 'off) nil)
4942 (t halfop))
4943 :op (cond ((eq op 'on) t)
4944 ((eq op 'off) nil)
4945 (t op))
4946 :admin (cond ((eq admin 'on) t)
4947 ((eq admin 'off) nil)
4948 (t admin))
4949 :owner (cond ((eq owner 'on) t)
4950 ((eq owner 'off) nil)
4951 (t owner))
4952 :last-message-time
4953 (if update-message-time (current-time))))
4954 (puthash (erc-downcase nick) (cons user cuser)
4955 erc-channel-users)
4956 (setq changed t)))
4957 (when (and changed (null user-changed))
4958 (run-hooks 'erc-channel-members-changed-hook))
4959 (or changed user-changed add)))
4960
4961 (defun erc-update-channel-member (channel nick new-nick
4962 &optional add voice halfop op admin owner host login
4963 full-name info update-message-time)
4964 "Update user and channel information for the user with
4965 nickname NICK in channel CHANNEL.
4966
4967 See also: `erc-update-current-channel-member'."
4968 (erc-with-buffer
4969 (channel)
4970 (erc-update-current-channel-member nick new-nick add voice halfop op admin owner host
4971 login full-name info
4972 update-message-time)))
4973
4974 (defun erc-remove-current-channel-member (nick)
4975 "Remove NICK from current channel membership list.
4976 Runs `erc-channel-members-changed-hook'."
4977 (let ((channel-data (erc-get-channel-user nick)))
4978 (when channel-data
4979 (erc-remove-channel-user nick)
4980 (run-hooks 'erc-channel-members-changed-hook))))
4981
4982 (defun erc-remove-channel-member (channel nick)
4983 "Remove NICK from CHANNEL's membership list.
4984
4985 See also `erc-remove-current-channel-member'."
4986 (erc-with-buffer
4987 (channel)
4988 (erc-remove-current-channel-member nick)))
4989
4990 (defun erc-update-channel-topic (channel topic &optional modify)
4991 "Find a buffer for CHANNEL and set the TOPIC for it.
4992
4993 If optional MODIFY is 'append or 'prepend, then append or prepend the
4994 TOPIC string to the current topic."
4995 (erc-with-buffer (channel)
4996 (cond ((eq modify 'append)
4997 (setq erc-channel-topic (concat erc-channel-topic topic)))
4998 ((eq modify 'prepend)
4999 (setq erc-channel-topic (concat topic erc-channel-topic)))
5000 (t (setq erc-channel-topic topic)))
5001 (erc-update-mode-line-buffer (current-buffer))))
5002
5003 (defun erc-set-modes (tgt mode-string)
5004 "Set the modes for the TGT provided as MODE-STRING."
5005 (let* ((modes (erc-parse-modes mode-string))
5006 (add-modes (nth 0 modes))
5007 ;; list of triples: (mode-char 'on/'off argument)
5008 (arg-modes (nth 2 modes)))
5009 (cond ((erc-channel-p tgt); channel modes
5010 (let ((buf (and erc-server-process
5011 (erc-get-buffer tgt erc-server-process))))
5012 (when buf
5013 (with-current-buffer buf
5014 (setq erc-channel-modes add-modes)
5015 (setq erc-channel-user-limit nil)
5016 (setq erc-channel-key nil)
5017 (while arg-modes
5018 (let ((mode (nth 0 (car arg-modes)))
5019 (onoff (nth 1 (car arg-modes)))
5020 (arg (nth 2 (car arg-modes))))
5021 (cond ((string-match "^[Ll]" mode)
5022 (erc-update-channel-limit tgt onoff arg))
5023 ((string-match "^[Kk]" mode)
5024 (erc-update-channel-key tgt onoff arg))
5025 (t nil)))
5026 (setq arg-modes (cdr arg-modes)))
5027 (erc-update-mode-line-buffer buf)))))
5028 ;; we do not keep our nick's modes yet
5029 ;;(t (setq erc-user-modes add-modes))
5030 )
5031 ))
5032
5033 (defun erc-sort-strings (list-of-strings)
5034 "Sort LIST-OF-STRINGS in lexicographic order.
5035
5036 Side-effect free."
5037 (sort (copy-sequence list-of-strings) 'string<))
5038
5039 (defun erc-parse-modes (mode-string)
5040 "Parse MODE-STRING into a list.
5041
5042 Returns a list of three elements:
5043
5044 (ADD-MODES REMOVE-MODES ARG-MODES).
5045
5046 The add-modes and remove-modes are lists of single-character strings
5047 for modes without parameters to add and remove respectively. The
5048 arg-modes is a list of triples of the form:
5049
5050 (MODE-CHAR ON/OFF ARGUMENT)."
5051 (if (string-match "^\\s-*\\(\\S-+\\)\\(\\s-.*$\\|$\\)" mode-string)
5052 (let ((chars (mapcar 'char-to-string (match-string 1 mode-string)))
5053 ;; arguments in channel modes
5054 (args-str (match-string 2 mode-string))
5055 (args nil)
5056 (add-modes nil)
5057 (remove-modes nil)
5058 (arg-modes nil); list of triples: (mode-char 'on/'off argument)
5059 (add-p t))
5060 ;; make the argument list
5061 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" args-str)
5062 (setq args (cons (match-string 1 args-str) args))
5063 (setq args-str (match-string 2 args-str)))
5064 (setq args (nreverse args))
5065 ;; collect what modes changed, and match them with arguments
5066 (while chars
5067 (cond ((string= (car chars) "+") (setq add-p t))
5068 ((string= (car chars) "-") (setq add-p nil))
5069 ((string-match "^[qaovhbQAOVHB]" (car chars))
5070 (setq arg-modes (cons (list (car chars)
5071 (if add-p 'on 'off)
5072 (if args (car args) nil))
5073 arg-modes))
5074 (if args (setq args (cdr args))))
5075 ((string-match "^[LlKk]" (car chars))
5076 (setq arg-modes (cons (list (car chars)
5077 (if add-p 'on 'off)
5078 (if (and add-p args)
5079 (car args) nil))
5080 arg-modes))
5081 (if (and add-p args) (setq args (cdr args))))
5082 (add-p (setq add-modes (cons (car chars) add-modes)))
5083 (t (setq remove-modes (cons (car chars) remove-modes))))
5084 (setq chars (cdr chars)))
5085 (setq add-modes (nreverse add-modes))
5086 (setq remove-modes (nreverse remove-modes))
5087 (setq arg-modes (nreverse arg-modes))
5088 (list add-modes remove-modes arg-modes))
5089 nil))
5090
5091 (defun erc-update-modes (tgt mode-string &optional _nick _host _login)
5092 "Update the mode information for TGT, provided as MODE-STRING.
5093 Optional arguments: NICK, HOST and LOGIN - the attributes of the
5094 person who changed the modes."
5095 ;; FIXME: neither of nick, host, and login are used!
5096 (let* ((modes (erc-parse-modes mode-string))
5097 (add-modes (nth 0 modes))
5098 (remove-modes (nth 1 modes))
5099 ;; list of triples: (mode-char 'on/'off argument)
5100 (arg-modes (nth 2 modes)))
5101 ;; now parse the modes changes and do the updates
5102 (cond ((erc-channel-p tgt); channel modes
5103 (let ((buf (and erc-server-process
5104 (erc-get-buffer tgt erc-server-process))))
5105 (when buf
5106 ;; FIXME! This used to have an original buffer
5107 ;; variable, but it never switched back to the original
5108 ;; buffer. Is this wanted behavior?
5109 (set-buffer buf)
5110 (if (not (boundp 'erc-channel-modes))
5111 (setq erc-channel-modes nil))
5112 (while remove-modes
5113 (setq erc-channel-modes (delete (car remove-modes)
5114 erc-channel-modes)
5115 remove-modes (cdr remove-modes)))
5116 (while add-modes
5117 (setq erc-channel-modes (cons (car add-modes)
5118 erc-channel-modes)
5119 add-modes (cdr add-modes)))
5120 (setq erc-channel-modes (erc-sort-strings erc-channel-modes))
5121 (while arg-modes
5122 (let ((mode (nth 0 (car arg-modes)))
5123 (onoff (nth 1 (car arg-modes)))
5124 (arg (nth 2 (car arg-modes))))
5125 (cond ((string-match "^[Vv]" mode)
5126 (erc-update-channel-member tgt arg arg nil onoff))
5127 ((string-match "^[hH]" mode)
5128 (erc-update-channel-member tgt arg arg nil nil onoff))
5129 ((string-match "^[oO]" mode)
5130 (erc-update-channel-member tgt arg arg nil nil nil onoff))
5131 ((string-match "^[aA]" mode)
5132 (erc-update-channel-member tgt arg arg nil nil nil nil onoff))
5133 ((string-match "^[qQ]" mode)
5134 (erc-update-channel-member tgt arg arg nil nil nil nil nil onoff))
5135 ((string-match "^[Ll]" mode)
5136 (erc-update-channel-limit tgt onoff arg))
5137 ((string-match "^[Kk]" mode)
5138 (erc-update-channel-key tgt onoff arg))
5139 (t nil)); only ops are tracked now
5140 (setq arg-modes (cdr arg-modes))))
5141 (erc-update-mode-line buf))))
5142 ;; nick modes - ignored at this point
5143 (t nil))))
5144
5145 (defun erc-update-channel-limit (channel onoff n)
5146 ;; FIXME: what does ONOFF actually do? -- Lawrence 2004-01-08
5147 "Update CHANNEL's user limit to N."
5148 (if (or (not (eq onoff 'on))
5149 (and (stringp n) (string-match "^[0-9]+$" n)))
5150 (erc-with-buffer
5151 (channel)
5152 (cond ((eq onoff 'on) (setq erc-channel-user-limit (string-to-number n)))
5153 (t (setq erc-channel-user-limit nil))))))
5154
5155 (defun erc-update-channel-key (channel onoff key)
5156 "Update CHANNEL's key to KEY if ONOFF is 'on or to nil if it's 'off."
5157 (erc-with-buffer
5158 (channel)
5159 (cond ((eq onoff 'on) (setq erc-channel-key key))
5160 (t (setq erc-channel-key nil)))))
5161
5162 (defun erc-handle-user-status-change (type nlh &optional l)
5163 "Handle changes in any user's status.
5164
5165 So far, only nick change is handled.
5166
5167 Generally, the TYPE argument is a symbol describing the change type, NLH is
5168 a list containing the original nickname, login name and hostname for the user,
5169 and L is a list containing additional TYPE-specific arguments.
5170
5171 So far the following TYPE/L pairs are supported:
5172
5173 Event TYPE L
5174
5175 nickname change 'nick (NEW-NICK)"
5176 (erc-log (format "user-change: type: %S nlh: %S l: %S" type nlh l))
5177 (cond
5178 ;; nickname change
5179 ((equal type 'nick)
5180 t)
5181 (t
5182 nil)))
5183
5184 (defun erc-highlight-notice (s)
5185 "Highlight notice message S and return it.
5186 See also variable `erc-notice-highlight-type'."
5187 (cond
5188 ((eq erc-notice-highlight-type 'prefix)
5189 (erc-put-text-property 0 (length erc-notice-prefix)
5190 'face 'erc-notice-face s)
5191 s)
5192 ((eq erc-notice-highlight-type 'all)
5193 (erc-put-text-property 0 (length s) 'face 'erc-notice-face s)
5194 s)
5195 (t s)))
5196
5197 (defun erc-make-notice (message)
5198 "Notify the user of MESSAGE."
5199 (when erc-minibuffer-notice
5200 (message "%s" message))
5201 (erc-highlight-notice (concat erc-notice-prefix message)))
5202
5203 (defun erc-highlight-error (s)
5204 "Highlight error message S and return it."
5205 (erc-put-text-property 0 (length s) 'face 'erc-error-face s)
5206 s)
5207
5208 (defun erc-put-text-property (start end property value &optional object)
5209 "Set text-property for an object (usually a string).
5210 START and END define the characters covered.
5211 PROPERTY is the text-property set, usually the symbol `face'.
5212 VALUE is the value for the text-property, usually a face symbol such as
5213 the face `bold' or `erc-pal-face'.
5214 OBJECT is a string which will be modified and returned.
5215 OBJECT is modified without being copied first.
5216
5217 You can redefine or `defadvice' this function in order to add
5218 EmacsSpeak support."
5219 (put-text-property start end property value object))
5220
5221 (defun erc-list (thing)
5222 "Return THING if THING is a list, or a list with THING as its element."
5223 (if (listp thing)
5224 thing
5225 (list thing)))
5226
5227 (defun erc-parse-user (string)
5228 "Parse STRING as a user specification (nick!login@host).
5229
5230 Return a list of the three separate tokens."
5231 (cond
5232 ((string-match "^\\([^!\n]*\\)!\\([^@\n]*\\)@\\(.*\\)$" string)
5233 (list (match-string 1 string)
5234 (match-string 2 string)
5235 (match-string 3 string)))
5236 ;; Some bogus bouncers send Nick!(null), try to live with that.
5237 ((string-match "^\\([^!\n]*\\)!\\(.*\\)$" string)
5238 (list (match-string 1 string)
5239 ""
5240 (match-string 2 string)))
5241 (t
5242 (list string "" ""))))
5243
5244 (defun erc-extract-nick (string)
5245 "Return the nick corresponding to a user specification STRING.
5246
5247 See also `erc-parse-user'."
5248 (car (erc-parse-user string)))
5249
5250 (defun erc-put-text-properties (start end properties
5251 &optional object value-list)
5252 "Set text-properties for OBJECT.
5253
5254 START and END describe positions in OBJECT.
5255 If VALUE-LIST is nil, set each property in PROPERTIES to t, else set
5256 each property to the corresponding value in VALUE-LIST."
5257 (unless value-list
5258 (setq value-list (mapcar (lambda (_x) t)
5259 properties)))
5260 (while (and properties value-list)
5261 (erc-put-text-property
5262 start end (pop properties) (pop value-list) object)))
5263
5264 ;;; Input area handling:
5265
5266 (defun erc-beg-of-input-line ()
5267 "Return the value of `point' at the beginning of the input line.
5268
5269 Specifically, return the position of `erc-insert-marker'."
5270 (or (and (boundp 'erc-insert-marker)
5271 (markerp erc-insert-marker))
5272 (error "erc-insert-marker has no value, please report a bug"))
5273 (marker-position erc-insert-marker))
5274
5275 (defun erc-end-of-input-line ()
5276 "Return the value of `point' at the end of the input line."
5277 (point-max))
5278
5279 (defvar erc-last-input-time 0
5280 "Time of last call to `erc-send-current-line'.
5281 If that function has never been called, the value is 0.")
5282
5283 (defcustom erc-accidental-paste-threshold-seconds nil
5284 "Minimum time, in seconds, before sending new lines via IRC.
5285 If the value is a number, `erc-send-current-line' signals an error
5286 if its previous invocation was fewer than this many seconds ago.
5287 This is useful so that if you accidentally enter large amounts of text
5288 into the ERC buffer, that text is not sent to the IRC server.
5289
5290 If the value is nil, `erc-send-current-line' always considers any
5291 submitted line to be intentional."
5292 :group 'erc
5293 :version "24.4"
5294 :type '(choice number (other :tag "disabled" nil)))
5295
5296 (defun erc-send-current-line ()
5297 "Parse current line and send it to IRC."
5298 (interactive)
5299 (let ((now (float-time)))
5300 (if (or (not erc-accidental-paste-threshold-seconds)
5301 (< erc-accidental-paste-threshold-seconds
5302 (- now erc-last-input-time)))
5303 (save-restriction
5304 (widen)
5305 (if (< (point) (erc-beg-of-input-line))
5306 (erc-error "Point is not in the input area")
5307 (let ((inhibit-read-only t)
5308 (str (erc-user-input))
5309 (old-buf (current-buffer)))
5310 (if (and (not (erc-server-buffer-live-p))
5311 (not (erc-command-no-process-p str)))
5312 (erc-error "ERC: No process running")
5313 (erc-set-active-buffer (current-buffer))
5314 ;; Kill the input and the prompt
5315 (delete-region (erc-beg-of-input-line)
5316 (erc-end-of-input-line))
5317 (unwind-protect
5318 (erc-send-input str)
5319 ;; Fix the buffer if the command didn't kill it
5320 (when (buffer-live-p old-buf)
5321 (with-current-buffer old-buf
5322 (save-restriction
5323 (widen)
5324 (goto-char (point-max))
5325 (when (processp erc-server-process)
5326 (set-marker (process-mark erc-server-process) (point)))
5327 (set-marker erc-insert-marker (point))
5328 (let ((buffer-modified (buffer-modified-p)))
5329 (erc-display-prompt)
5330 (set-buffer-modified-p buffer-modified))))))
5331
5332 ;; Only when last hook has been run...
5333 (run-hook-with-args 'erc-send-completed-hook str))))
5334 (setq erc-last-input-time now))
5335 (switch-to-buffer "*ERC Accidental Paste Overflow*")
5336 (lwarn 'erc :warning
5337 "You seem to have accidentally pasted some text!"))))
5338
5339 (defun erc-user-input ()
5340 "Return the input of the user in the current buffer."
5341 (buffer-substring-no-properties
5342 erc-input-marker
5343 (erc-end-of-input-line)))
5344
5345 (defvar erc-command-regexp "^/\\([A-Za-z']+\\)\\(\\s-+.*\\|\\s-*\\)$"
5346 "Regular expression used for matching commands in ERC.")
5347
5348 (defun erc-send-input (input)
5349 "Treat INPUT as typed in by the user. It is assumed that the input
5350 and the prompt is already deleted.
5351 This returns non-nil only if we actually send anything."
5352 ;; Handle different kinds of inputs
5353 (cond
5354 ;; Ignore empty input
5355 ((if erc-send-whitespace-lines
5356 (string= input "")
5357 (string-match "\\`[ \t\r\f\n]*\\'" input))
5358 (when erc-warn-about-blank-lines
5359 (message "Blank line - ignoring...")
5360 (beep))
5361 nil)
5362 (t
5363 (let ((str input)
5364 (erc-insert-this t))
5365 (setq erc-send-this t)
5366 (run-hook-with-args 'erc-send-pre-hook input)
5367 (when erc-send-this
5368 (if (or (string-match "\n" str)
5369 (not (string-match erc-command-regexp str)))
5370 (mapc
5371 (lambda (line)
5372 (mapc
5373 (lambda (line)
5374 ;; Insert what has to be inserted for this.
5375 (erc-display-msg line)
5376 (erc-process-input-line (concat line "\n")
5377 (null erc-flood-protect) t))
5378 (or (and erc-flood-protect (erc-split-line line))
5379 (list line))))
5380 (split-string str "\n"))
5381 (erc-process-input-line (concat str "\n") t nil))
5382 t)))))
5383
5384 (defun erc-display-command (line)
5385 (when erc-insert-this
5386 (let ((insert-position (point)))
5387 (unless erc-hide-prompt
5388 (erc-display-prompt nil nil (erc-command-indicator)
5389 (and (erc-command-indicator)
5390 'erc-command-indicator-face)))
5391 (let ((beg (point)))
5392 (insert line)
5393 (erc-put-text-property beg (point)
5394 'face 'erc-command-indicator-face)
5395 (insert "\n"))
5396 (when (processp erc-server-process)
5397 (set-marker (process-mark erc-server-process) (point)))
5398 (set-marker erc-insert-marker (point))
5399 (save-excursion
5400 (save-restriction
5401 (narrow-to-region insert-position (point))
5402 (run-hooks 'erc-send-modify-hook)
5403 (run-hooks 'erc-send-post-hook))))))
5404
5405 (defun erc-display-msg (line)
5406 "Display LINE as a message of the user to the current target at the
5407 current position."
5408 (when erc-insert-this
5409 (let ((insert-position (point)))
5410 (insert (erc-format-my-nick))
5411 (let ((beg (point)))
5412 (insert line)
5413 (erc-put-text-property beg (point)
5414 'face 'erc-input-face))
5415 (insert "\n")
5416 (when (processp erc-server-process)
5417 (set-marker (process-mark erc-server-process) (point)))
5418 (set-marker erc-insert-marker (point))
5419 (save-excursion
5420 (save-restriction
5421 (narrow-to-region insert-position (point))
5422 (run-hooks 'erc-send-modify-hook)
5423 (run-hooks 'erc-send-post-hook))))))
5424
5425 (defun erc-command-symbol (command)
5426 "Return the ERC command symbol for COMMAND if it exists and is bound."
5427 (let ((cmd (intern-soft (format "erc-cmd-%s" (upcase command)))))
5428 (when (fboundp cmd) cmd)))
5429
5430 (defun erc-extract-command-from-line (line)
5431 "Extract command and args from the input LINE.
5432 If no command was given, return nil. If command matches, return a
5433 list of the form: (command args) where both elements are strings."
5434 (when (string-match erc-command-regexp line)
5435 (let* ((cmd (erc-command-symbol (match-string 1 line)))
5436 ;; note: return is nil, we apply this simply for side effects
5437 (_canon-defun (while (and cmd (symbolp (symbol-function cmd)))
5438 (setq cmd (symbol-function cmd))))
5439 (cmd-fun (or cmd #'erc-cmd-default))
5440 (arg (if cmd
5441 (if (get cmd-fun 'do-not-parse-args)
5442 (format "%s" (match-string 2 line))
5443 (delete "" (split-string (erc-trim-string
5444 (match-string 2 line)) " ")))
5445 line)))
5446 (list cmd-fun arg))))
5447
5448 (defun erc-split-multiline-safe (string)
5449 "Split STRING, containing multiple lines and return them in a list.
5450 Do it only for STRING as the complete input, do not carry unfinished
5451 strings over to the next call."
5452 (let ((l ())
5453 (i0 0)
5454 (doit t))
5455 (while doit
5456 (let ((i (string-match "\r?\n" string i0))
5457 (s (substring string i0)))
5458 (cond (i (setq l (cons (substring string i0 i) l))
5459 (setq i0 (match-end 0)))
5460 ((> (length s) 0)
5461 (setq l (cons s l))(setq doit nil))
5462 (t (setq doit nil)))))
5463 (nreverse l)))
5464
5465 ;; nick handling
5466
5467 (defun erc-set-current-nick (nick)
5468 "Set the current nickname to NICK."
5469 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5470 (erc-server-buffer)
5471 (current-buffer))
5472 (setq erc-server-current-nick nick)))
5473
5474 (defun erc-current-nick ()
5475 "Return the current nickname."
5476 (with-current-buffer (if (buffer-live-p (erc-server-buffer))
5477 (erc-server-buffer)
5478 (current-buffer))
5479 erc-server-current-nick))
5480
5481 (defun erc-current-nick-p (nick)
5482 "Return non-nil if NICK is the current nickname."
5483 (erc-nick-equal-p nick (erc-current-nick)))
5484
5485 (defun erc-nick-equal-p (nick1 nick2)
5486 "Return non-nil if NICK1 and NICK2 are the same.
5487
5488 This matches strings according to the IRC protocol's case convention.
5489
5490 See also `erc-downcase'."
5491 (string= (erc-downcase nick1)
5492 (erc-downcase nick2)))
5493
5494 ;; default target handling
5495
5496 (defun erc-default-target ()
5497 "Return the current default target (as a character string) or nil if none."
5498 (let ((tgt (car erc-default-recipients)))
5499 (cond
5500 ((not tgt) nil)
5501 ((listp tgt) (cdr tgt))
5502 (t tgt))))
5503
5504 (defun erc-add-default-channel (channel)
5505 "Add CHANNEL to the default channel list."
5506 (let ((chl (downcase channel)))
5507 (setq erc-default-recipients
5508 (cons chl erc-default-recipients))))
5509
5510 (defun erc-delete-default-channel (channel &optional buffer)
5511 "Delete CHANNEL from the default channel list."
5512 (with-current-buffer (if (and buffer
5513 (bufferp buffer))
5514 buffer
5515 (current-buffer))
5516 (setq erc-default-recipients (delete (downcase channel)
5517 erc-default-recipients))))
5518
5519 (defun erc-add-query (nickname)
5520 "Add QUERY'd NICKNAME to the default channel list.
5521
5522 The previous default target of QUERY type gets removed."
5523 (let ((d1 (car erc-default-recipients))
5524 (d2 (cdr erc-default-recipients))
5525 (qt (cons 'QUERY (downcase nickname))))
5526 (setq erc-default-recipients (cons qt (if (and (listp d1)
5527 (eq (car d1) 'QUERY))
5528 d2
5529 erc-default-recipients)))))
5530
5531 (defun erc-delete-query ()
5532 "Delete the topmost target if it is a QUERY."
5533
5534 (let ((d1 (car erc-default-recipients))
5535 (d2 (cdr erc-default-recipients)))
5536 (if (and (listp d1)
5537 (eq (car d1) 'QUERY))
5538 (setq erc-default-recipients d2)
5539 (error "Current target is not a QUERY"))))
5540
5541 (defun erc-ignored-user-p (spec)
5542 "Return non-nil if SPEC matches something in `erc-ignore-list'.
5543
5544 Takes a full SPEC of a user in the form \"nick!login@host\", and
5545 matches against all the regexp's in `erc-ignore-list'. If any
5546 match, returns that regexp."
5547 (catch 'found
5548 (dolist (ignored (erc-with-server-buffer erc-ignore-list))
5549 (if (string-match ignored spec)
5550 (throw 'found ignored)))))
5551
5552 (defun erc-ignored-reply-p (msg tgt proc)
5553 ;; FIXME: this docstring needs fixing -- Lawrence 2004-01-08
5554 "Return non-nil if MSG matches something in `erc-ignore-reply-list'.
5555
5556 Takes a message MSG to a channel and returns non-nil if the addressed
5557 user matches any regexp in `erc-ignore-reply-list'."
5558 (let ((target-nick (erc-message-target msg)))
5559 (if (not target-nick)
5560 nil
5561 (erc-with-buffer (tgt proc)
5562 (let ((user (erc-get-server-user target-nick)))
5563 (when user
5564 (erc-list-match erc-ignore-reply-list
5565 (erc-user-spec user))))))))
5566
5567 (defun erc-message-target (msg)
5568 "Return the addressed target in MSG.
5569
5570 The addressed target is the string before the first colon in MSG."
5571 (if (string-match "^\\([^: \n]*\\):" msg)
5572 (match-string 1 msg)
5573 nil))
5574
5575 (defun erc-user-spec (user)
5576 "Create a nick!user@host spec from a user struct."
5577 (let ((nick (erc-server-user-nickname user))
5578 (host (erc-server-user-host user))
5579 (login (erc-server-user-login user)))
5580 (concat (or nick "")
5581 "!"
5582 (or login "")
5583 "@"
5584 (or host ""))))
5585
5586 (defun erc-list-match (lst str)
5587 "Return non-nil if any regexp in LST matches STR."
5588 (memq nil (mapcar (lambda (regexp)
5589 (not (string-match regexp str)))
5590 lst)))
5591
5592 ;; other "toggles"
5593
5594 (defun erc-toggle-ctcp-autoresponse (&optional arg)
5595 "Toggle automatic CTCP replies (like VERSION and PING).
5596
5597 If ARG is positive, turns CTCP replies on.
5598
5599 If ARG is non-nil and not positive, turns CTCP replies off."
5600 (interactive "P")
5601 (cond ((and (numberp arg) (> arg 0))
5602 (setq erc-disable-ctcp-replies t))
5603 (arg (setq erc-disable-ctcp-replies nil))
5604 (t (setq erc-disable-ctcp-replies (not erc-disable-ctcp-replies))))
5605 (message "ERC CTCP replies are %s" (if erc-disable-ctcp-replies "OFF" "ON")))
5606
5607 (defun erc-toggle-flood-control (&optional arg)
5608 "Toggle use of flood control on sent messages.
5609
5610 If ARG is positive, use flood control.
5611 If ARG is non-nil and not positive, do not use flood control.
5612
5613 See `erc-server-flood-margin' for an explanation of the available
5614 flood control parameters."
5615 (interactive "P")
5616 (cond ((and (numberp arg) (> arg 0))
5617 (setq erc-flood-protect t))
5618 (arg (setq erc-flood-protect nil))
5619 (t (setq erc-flood-protect (not erc-flood-protect))))
5620 (message "ERC flood control is %s"
5621 (cond (erc-flood-protect "ON")
5622 (t "OFF"))))
5623
5624 ;; Some useful channel and nick commands for fast key bindings
5625
5626 (defun erc-invite-only-mode (&optional arg)
5627 "Turn on the invite only mode (+i) for the current channel.
5628
5629 If ARG is non-nil, turn this mode off (-i).
5630
5631 This command is sent even if excess flood is detected."
5632 (interactive "P")
5633 (erc-set-active-buffer (current-buffer))
5634 (let ((tgt (erc-default-target)))
5635 (if (or (not tgt) (not (erc-channel-p tgt)))
5636 (erc-display-message nil 'error (current-buffer) 'no-target)
5637 (erc-load-irc-script-lines
5638 (list (concat "/mode " tgt (if arg " -i" " +i")))
5639 t))))
5640
5641 (defun erc-get-channel-mode-from-keypress (key)
5642 "Read a key sequence and call the corresponding channel mode function.
5643 After doing C-c C-o, type in a channel mode letter.
5644
5645 C-g means quit.
5646 RET lets you type more than one mode at a time.
5647 If \"l\" is pressed, `erc-set-channel-limit' gets called.
5648 If \"k\" is pressed, `erc-set-channel-key' gets called.
5649 Anything else will be sent to `erc-toggle-channel-mode'."
5650 (interactive "kChannel mode (RET to set more than one): ")
5651 (when (featurep 'xemacs)
5652 (setq key (char-to-string (event-to-character (aref key 0)))))
5653 (cond ((equal key "\C-g")
5654 (keyboard-quit))
5655 ((equal key "\C-m")
5656 (erc-insert-mode-command))
5657 ((equal key "l")
5658 (call-interactively 'erc-set-channel-limit))
5659 ((equal key "k")
5660 (call-interactively 'erc-set-channel-key))
5661 (t (erc-toggle-channel-mode key))))
5662
5663 (defun erc-toggle-channel-mode (mode &optional channel)
5664 "Toggle channel MODE.
5665
5666 If CHANNEL is non-nil, toggle MODE for that channel, otherwise use
5667 `erc-default-target'."
5668 (interactive "P")
5669 (erc-set-active-buffer (current-buffer))
5670 (let ((tgt (or channel (erc-default-target))))
5671 (if (or (null tgt) (null (erc-channel-p tgt)))
5672 (erc-display-message nil 'error 'active 'no-target)
5673 (let* ((active (member mode erc-channel-modes))
5674 (newstate (if active "OFF" "ON")))
5675 (erc-log (format "%s: Toggle mode %s %s" tgt mode newstate))
5676 (message "Toggle channel mode %s %s" mode newstate)
5677 (erc-server-send (format "MODE %s %s%s"
5678 tgt (if active "-" "+") mode))))))
5679
5680 (defun erc-insert-mode-command ()
5681 "Insert the line \"/mode <current target> \" at `point'."
5682 (interactive)
5683 (let ((tgt (erc-default-target)))
5684 (if tgt (insert (concat "/mode " tgt " "))
5685 (erc-display-message nil 'error (current-buffer) 'no-target))))
5686
5687 (defun erc-channel-names ()
5688 "Run \"/names #channel\" in the current channel."
5689 (interactive)
5690 (erc-set-active-buffer (current-buffer))
5691 (let ((tgt (erc-default-target)))
5692 (if tgt (erc-load-irc-script-lines (list (concat "/names " tgt)))
5693 (erc-display-message nil 'error (current-buffer) 'no-target))))
5694
5695 (defun erc-remove-text-properties-region (start end &optional object)
5696 "Clears the region (START,END) in OBJECT from all colors, etc."
5697 (interactive "r")
5698 (save-excursion
5699 (let ((inhibit-read-only t))
5700 (set-text-properties start end nil object))))
5701 (put 'erc-remove-text-properties-region 'disabled t)
5702
5703 ;; script execution and startup
5704
5705 (defun erc-find-file (file &optional path)
5706 "Search for a FILE in the filesystem.
5707 First the `default-directory' is searched for FILE, then any directories
5708 specified in the list PATH.
5709
5710 If FILE is found, return the path to it."
5711 (let ((filepath file))
5712 (if (file-readable-p filepath) filepath
5713 (while (and path
5714 (progn (setq filepath (expand-file-name file (car path)))
5715 (not (file-readable-p filepath))))
5716 (setq path (cdr path)))
5717 (if path filepath nil))))
5718
5719 (defun erc-select-startup-file ()
5720 "Select an ERC startup file.
5721 See also `erc-startup-file-list'."
5722 (catch 'found
5723 (dolist (f erc-startup-file-list)
5724 (setq f (convert-standard-filename f))
5725 (when (file-readable-p f)
5726 (throw 'found f)))))
5727
5728 (defun erc-find-script-file (file)
5729 "Search for FILE in `default-directory', and any in `erc-script-path'."
5730 (erc-find-file file erc-script-path))
5731
5732 (defun erc-load-script (file)
5733 "Load a script from FILE.
5734
5735 FILE must be the full name, it is not searched in the
5736 `erc-script-path'. If the filename ends with `.el', then load it
5737 as an Emacs Lisp program. Otherwise, treat it as a regular IRC
5738 script."
5739 (erc-log (concat "erc-load-script: " file))
5740 (cond
5741 ((string-match "\\.el\\'" file)
5742 (load file))
5743 (t
5744 (erc-load-irc-script file))))
5745
5746 (defun erc-process-script-line (line &optional args)
5747 "Process an IRC script LINE.
5748
5749 Does script-specific substitutions (script arguments, current nick,
5750 server, etc.) in LINE and returns it.
5751
5752 Substitutions are: %C and %c = current target (channel or nick),
5753 %S %s = current server, %N %n = my current nick, and %x is x verbatim,
5754 where x is any other character;
5755 $* = the entire argument string, $1 = the first argument, $2 = the second,
5756 and so on."
5757 (if (not args) (setq args ""))
5758 (let* ((arg-esc-regexp "\\(\\$\\(\\*\\|[1-9][0-9]*\\)\\)\\([^0-9]\\|$\\)")
5759 (percent-regexp "\\(%.\\)")
5760 (esc-regexp (concat arg-esc-regexp "\\|" percent-regexp))
5761 (tgt (erc-default-target))
5762 (server (and (boundp 'erc-session-server) erc-session-server))
5763 (nick (erc-current-nick))
5764 (res "")
5765 (tmp nil)
5766 (arg-list nil)
5767 (arg-num 0))
5768 (if (not tgt) (setq tgt ""))
5769 (if (not server) (setq server ""))
5770 (if (not nick) (setq nick ""))
5771 ;; First, compute the argument list
5772 (setq tmp args)
5773 (while (string-match "^\\s-*\\(\\S-+\\)\\(\\s-+.*$\\|$\\)" tmp)
5774 (setq arg-list (cons (match-string 1 tmp) arg-list))
5775 (setq tmp (match-string 2 tmp)))
5776 (setq arg-list (nreverse arg-list))
5777 (setq arg-num (length arg-list))
5778 ;; now do the substitution
5779 (setq tmp (string-match esc-regexp line))
5780 (while tmp
5781 ;;(message "beginning of while: tmp=%S" tmp)
5782 (let* ((hd (substring line 0 tmp))
5783 (esc "")
5784 (subst "")
5785 (tail (substring line tmp)))
5786 (cond ((string-match (concat "^" arg-esc-regexp) tail)
5787 (setq esc (match-string 1 tail))
5788 (setq tail (substring tail (match-end 1))))
5789 ((string-match (concat "^" percent-regexp) tail)
5790 (setq esc (match-string 1 tail))
5791 (setq tail (substring tail (match-end 1)))))
5792 ;;(message "hd=%S, esc=%S, tail=%S, arg-num=%S" hd esc tail arg-num)
5793 (setq res (concat res hd))
5794 (setq subst
5795 (cond ((string= esc "") "")
5796 ((string-match "^\\$\\*$" esc) args)
5797 ((string-match "^\\$\\([0-9]+\\)$" esc)
5798 (let ((n (string-to-number (match-string 1 esc))))
5799 (message "n = %S, integerp(n)=%S" n (integerp n))
5800 (if (<= n arg-num) (nth (1- n) arg-list) "")))
5801 ((string-match "^%[Cc]$" esc) tgt)
5802 ((string-match "^%[Ss]$" esc) server)
5803 ((string-match "^%[Nn]$" esc) nick)
5804 ((string-match "^%\\(.\\)$" esc) (match-string 1 esc))
5805 (t (erc-log (format "BUG in erc-process-script-line: bad escape sequence: %S\n" esc))
5806 (message "BUG IN ERC: esc=%S" esc)
5807 "")))
5808 (setq line tail)
5809 (setq tmp (string-match esc-regexp line))
5810 (setq res (concat res subst))
5811 ;;(message "end of while: line=%S, res=%S, tmp=%S" line res tmp)
5812 ))
5813 (setq res (concat res line))
5814 res))
5815
5816 (defun erc-load-irc-script (file &optional force)
5817 "Load an IRC script from FILE."
5818 (erc-log (concat "erc-load-script: " file))
5819 (let ((str (with-temp-buffer
5820 (insert-file-contents file)
5821 (buffer-string))))
5822 (erc-load-irc-script-lines (erc-split-multiline-safe str) force)))
5823
5824 (defun erc-load-irc-script-lines (lines &optional force noexpand)
5825 "Load IRC script LINES (a list of strings).
5826
5827 If optional NOEXPAND is non-nil, do not expand script-specific
5828 sequences, process the lines verbatim. Use this for multiline
5829 user input."
5830 (let* ((cb (current-buffer))
5831 (s "")
5832 (sp (or (erc-command-indicator) (erc-prompt)))
5833 (args (and (boundp 'erc-script-args) erc-script-args)))
5834 (if (and args (string-match "^ " args))
5835 (setq args (substring args 1)))
5836 ;; prepare the prompt string for echo
5837 (erc-put-text-property 0 (length sp)
5838 'face 'erc-command-indicator-face sp)
5839 (while lines
5840 (setq s (car lines))
5841 (erc-log (concat "erc-load-script: CMD: " s))
5842 (unless (string-match "^\\s-*$" s)
5843 (let ((line (if noexpand s (erc-process-script-line s args))))
5844 (if (and (erc-process-input-line line force)
5845 erc-script-echo)
5846 (progn
5847 (erc-put-text-property 0 (length line)
5848 'face 'erc-input-face line)
5849 (erc-display-line (concat sp line) cb)))))
5850 (setq lines (cdr lines)))))
5851
5852 ;; authentication
5853
5854 (defun erc-login ()
5855 "Perform user authentication at the IRC server."
5856 (erc-log (format "login: nick: %s, user: %s %s %s :%s"
5857 (erc-current-nick)
5858 (user-login-name)
5859 (or erc-system-name (system-name))
5860 erc-session-server
5861 erc-session-user-full-name))
5862 (if erc-session-password
5863 (erc-server-send (format "PASS %s" erc-session-password))
5864 (message "Logging in without password"))
5865 (erc-server-send (format "NICK %s" (erc-current-nick)))
5866 (erc-server-send
5867 (format "USER %s %s %s :%s"
5868 ;; hacked - S.B.
5869 (if erc-anonymous-login erc-email-userid (user-login-name))
5870 "0" "*"
5871 erc-session-user-full-name))
5872 (erc-update-mode-line))
5873
5874 ;; connection properties' heuristics
5875
5876 (defun erc-determine-parameters (&optional server port nick name)
5877 "Determine the connection and authentication parameters.
5878 Sets the buffer local variables:
5879
5880 - `erc-session-connector'
5881 - `erc-session-server'
5882 - `erc-session-port'
5883 - `erc-session-full-name'
5884 - `erc-server-current-nick'"
5885 (setq erc-session-connector erc-server-connect-function
5886 erc-session-server (erc-compute-server server)
5887 erc-session-port (or port erc-default-port)
5888 erc-session-user-full-name (erc-compute-full-name name))
5889 (erc-set-current-nick (erc-compute-nick nick)))
5890
5891 (defun erc-compute-server (&optional server)
5892 "Return an IRC server name.
5893
5894 This tries a number of increasingly more default methods until a
5895 non-nil value is found.
5896
5897 - SERVER (the argument passed to this function)
5898 - The `erc-server' option
5899 - The value of the IRCSERVER environment variable
5900 - The `erc-default-server' variable"
5901 (or server
5902 erc-server
5903 (getenv "IRCSERVER")
5904 erc-default-server))
5905
5906 (defun erc-compute-nick (&optional nick)
5907 "Return user's IRC nick.
5908
5909 This tries a number of increasingly more default methods until a
5910 non-nil value is found.
5911
5912 - NICK (the argument passed to this function)
5913 - The `erc-nick' option
5914 - The value of the IRCNICK environment variable
5915 - The result from the `user-login-name' function"
5916 (or nick
5917 (if (consp erc-nick) (car erc-nick) erc-nick)
5918 (getenv "IRCNICK")
5919 (user-login-name)))
5920
5921
5922 (defun erc-compute-full-name (&optional full-name)
5923 "Return user's full name.
5924
5925 This tries a number of increasingly more default methods until a
5926 non-nil value is found.
5927
5928 - FULL-NAME (the argument passed to this function)
5929 - The `erc-user-full-name' option
5930 - The value of the IRCNAME environment variable
5931 - The result from the `user-full-name' function"
5932 (or full-name
5933 erc-user-full-name
5934 (getenv "IRCNAME")
5935 (if erc-anonymous-login "unknown" nil)
5936 (user-full-name)))
5937
5938 (defun erc-compute-port (&optional port)
5939 "Return a port for an IRC server.
5940
5941 This tries a number of increasingly more default methods until a
5942 non-nil value is found.
5943
5944 - PORT (the argument passed to this function)
5945 - The `erc-port' option
5946 - The `erc-default-port' variable"
5947 (or port erc-port erc-default-port))
5948
5949 ;; time routines
5950
5951 (defun erc-string-to-emacs-time (string)
5952 "Convert the long number represented by STRING into an Emacs format.
5953 Returns a list of the form (HIGH LOW), compatible with Emacs time format."
5954 (let* ((n (string-to-number (concat string ".0"))))
5955 (list (truncate (/ n 65536))
5956 (truncate (mod n 65536)))))
5957
5958 (defun erc-emacs-time-to-erc-time (time)
5959 "Convert Emacs TIME to a number of seconds since the epoch."
5960 (when time
5961 (+ (* (nth 0 time) 65536.0) (nth 1 time))))
5962 ; (round (+ (* (nth 0 tm) 65536.0) (nth 1 tm))))
5963
5964 (defun erc-current-time ()
5965 "Return the `current-time' as a number of seconds since the epoch.
5966
5967 See also `erc-emacs-time-to-erc-time'."
5968 (erc-emacs-time-to-erc-time (current-time)))
5969
5970 (defun erc-time-diff (t1 t2)
5971 "Return the time difference in seconds between T1 and T2."
5972 (abs (- t2 t1)))
5973
5974 (defun erc-time-gt (t1 t2)
5975 "Check whether T1 > T2."
5976 (> t1 t2))
5977
5978 (defun erc-sec-to-time (ns)
5979 "Convert NS to a time string HH:MM.SS."
5980 (setq ns (truncate ns))
5981 (format "%02d:%02d.%02d"
5982 (/ ns 3600)
5983 (/ (% ns 3600) 60)
5984 (% ns 60)))
5985
5986 (defun erc-seconds-to-string (seconds)
5987 "Convert a number of SECONDS into an English phrase."
5988 (let (days hours minutes format-args output)
5989 (setq days (/ seconds 86400)
5990 seconds (% seconds 86400)
5991 hours (/ seconds 3600)
5992 seconds (% seconds 3600)
5993 minutes (/ seconds 60)
5994 seconds (% seconds 60)
5995 format-args (if (> days 0)
5996 `("%d days, %d hours, %d minutes, %d seconds"
5997 ,days ,hours ,minutes ,seconds)
5998 (if (> hours 0)
5999 `("%d hours, %d minutes, %d seconds"
6000 ,hours ,minutes ,seconds)
6001 (if (> minutes 0)
6002 `("%d minutes, %d seconds" ,minutes ,seconds)
6003 `("%d seconds" ,seconds))))
6004 output (apply 'format format-args))
6005 ;; Change all "1 units" to "1 unit".
6006 (while (string-match "\\([^0-9]\\|^\\)1 \\S-+\\(s\\)" output)
6007 (setq output (erc-replace-match-subexpression-in-string
6008 "" output (match-string 2 output) 2 (match-beginning 2))))
6009 output))
6010
6011
6012 ;; info
6013
6014 (defconst erc-clientinfo-alist
6015 '(("ACTION" . "is used to inform about one's current activity")
6016 ("CLIENTINFO" . "gives help on CTCP commands supported by client")
6017 ("ECHO" . "echoes its arguments back")
6018 ("FINGER" . "shows user's name, location, and idle time")
6019 ("PING" . "measures delay between peers")
6020 ("TIME" . "shows client-side time")
6021 ("USERINFO" . "shows information provided by a user")
6022 ("VERSION" . "shows client type and version"))
6023 "Alist of CTCP CLIENTINFO for ERC commands.")
6024
6025 (defun erc-client-info (s)
6026 "Return CTCP CLIENTINFO on command S.
6027 If S is nil or an empty string then return general CLIENTINFO."
6028 (if (or (not s) (string= s ""))
6029 (concat
6030 (apply #'concat
6031 (mapcar (lambda (e)
6032 (concat (car e) " "))
6033 erc-clientinfo-alist))
6034 ": use CLIENTINFO <COMMAND> to get more specific information")
6035 (let ((h (assoc (upcase s) erc-clientinfo-alist)))
6036 (if h
6037 (concat s " " (cdr h))
6038 (concat s ": unknown command")))))
6039
6040 ;; Hook functions
6041
6042 (defun erc-directory-writable-p (dir)
6043 "Determine whether DIR is a writable directory.
6044 If it doesn't exist, create it."
6045 (unless (file-attributes dir) (make-directory dir))
6046 (or (file-accessible-directory-p dir) (error "Cannot access %s" dir)))
6047
6048 (defun erc-kill-query-buffers (process)
6049 "Kill all buffers of PROCESS."
6050 ;; here, we only want to match the channel buffers, to avoid
6051 ;; "selecting killed buffers" b0rkage.
6052 (erc-with-all-buffers-of-server process
6053 (lambda ()
6054 (not (erc-server-buffer-p)))
6055 (kill-buffer (current-buffer))))
6056
6057 (defun erc-nick-at-point ()
6058 "Give information about the nickname at `point'.
6059
6060 If called interactively, give a human readable message in the
6061 minibuffer. If called programmatically, return the corresponding
6062 entry of `channel-members'."
6063 (interactive)
6064 (require 'thingatpt)
6065 (let* ((word (word-at-point))
6066 (channel-data (erc-get-channel-user word))
6067 (cuser (cdr channel-data))
6068 (user (if channel-data
6069 (car channel-data)
6070 (erc-get-server-user word)))
6071 host login full-name nick voice halfop op admin owner)
6072 (when user
6073 (setq nick (erc-server-user-nickname user)
6074 host (erc-server-user-host user)
6075 login (erc-server-user-login user)
6076 full-name (erc-server-user-full-name user))
6077 (if cuser
6078 (setq voice (erc-channel-user-voice cuser)
6079 halfop (erc-channel-user-halfop cuser)
6080 op (erc-channel-user-op cuser)
6081 admin (erc-channel-user-admin cuser)
6082 owner (erc-channel-user-owner cuser))))
6083 (if (called-interactively-p 'interactive)
6084 (message "%s is %s@%s%s%s"
6085 nick login host
6086 (if full-name (format " (%s)" full-name) "")
6087 (if (or voice halfop op admin owner)
6088 (format " and is +%s%s%s%s%s on %s"
6089 (if voice "v" "")
6090 (if halfop "h" "")
6091 (if op "o" "")
6092 (if admin "a" "")
6093 (if owner "q" "")
6094 (erc-default-target))
6095 ""))
6096 user)))
6097
6098 (defun erc-away-time ()
6099 "Return non-nil if the current ERC process is set away.
6100
6101 In particular, the time that we were set away is returned.
6102 See `current-time' for details on the time format."
6103 (erc-with-server-buffer erc-away))
6104
6105 ;; Mode line handling
6106
6107 (defcustom erc-mode-line-format "%S %a"
6108 "A string to be formatted and shown in the mode-line in `erc-mode'.
6109
6110 The string is formatted using `format-spec' and the result is set as the value
6111 of `mode-line-buffer-identification'.
6112
6113 The following characters are replaced:
6114 %a: String indicating away status or \"\" if you are not away
6115 %l: The estimated lag time to the server
6116 %m: The modes of the channel
6117 %n: The current nick name
6118 %N: The name of the network
6119 %o: The topic of the channel
6120 %p: The session port
6121 %t: The name of the target (channel, nickname, or servername:port)
6122 %s: In the server-buffer, this gets filled with the value of
6123 `erc-server-announced-name', in a channel, the value of
6124 (erc-default-target) also get concatenated.
6125 %S: In the server-buffer, this gets filled with the value of
6126 `erc-network', in a channel, the value of (erc-default-target)
6127 also get concatenated."
6128 :group 'erc-mode-line-and-header
6129 :type 'string)
6130
6131 (defcustom erc-header-line-format "%n on %t (%m,%l) %o"
6132 "A string to be formatted and shown in the header-line in `erc-mode'.
6133 Only used starting in Emacs 21.
6134
6135 Set this to nil if you do not want the header line to be
6136 displayed.
6137
6138 See `erc-mode-line-format' for which characters are can be used."
6139 :group 'erc-mode-line-and-header
6140 :set (lambda (sym val)
6141 (set sym val)
6142 (when (fboundp 'erc-update-mode-line)
6143 (erc-update-mode-line nil)))
6144 :type '(choice (const :tag "Disabled" nil)
6145 string))
6146
6147 (defcustom erc-header-line-uses-tabbar-p nil
6148 "Use tabbar mode instead of the header line to display the header."
6149 :group 'erc-mode-line-and-header
6150 :type 'boolean)
6151
6152 (defcustom erc-header-line-uses-help-echo-p t
6153 "Show the contents of the header line in the echo area or as a tooltip
6154 when you move point into the header line."
6155 :group 'erc-mode-line-and-header
6156 :type 'boolean)
6157
6158 (defcustom erc-header-line-face-method nil
6159 "Determine what method to use when colorizing the header line text.
6160
6161 If nil, don't colorize the header text.
6162 If given a function, call it and use the resulting face name.
6163 Otherwise, use the `erc-header-line' face."
6164 :group 'erc-mode-line-and-header
6165 :type '(choice (const :tag "Don't colorize" nil)
6166 (const :tag "Use the erc-header-line face" t)
6167 (function :tag "Call a function")))
6168
6169 (defcustom erc-show-channel-key-p t
6170 "Show the channel key in the header line."
6171 :group 'erc-paranoia
6172 :type 'boolean)
6173
6174 (defcustom erc-mode-line-away-status-format
6175 "(AWAY since %a %b %d %H:%M) "
6176 "When you're away on a server, this is shown in the mode line.
6177 This should be a string with substitution variables recognized by
6178 `format-time-string'."
6179 :group 'erc-mode-line-and-header
6180 :type 'string)
6181
6182 (defun erc-shorten-server-name (server-name)
6183 "Shorten SERVER-NAME according to `erc-common-server-suffixes'."
6184 (if (stringp server-name)
6185 (with-temp-buffer
6186 (insert server-name)
6187 (let ((alist erc-common-server-suffixes))
6188 (while alist
6189 (goto-char (point-min))
6190 (if (re-search-forward (caar alist) nil t)
6191 (replace-match (cdar alist)))
6192 (setq alist (cdr alist))))
6193 (buffer-string))))
6194
6195 (defun erc-format-target ()
6196 "Return the name of the target (channel or nickname or servername:port)."
6197 (let ((target (erc-default-target)))
6198 (or target
6199 (concat (erc-shorten-server-name
6200 (or erc-server-announced-name
6201 erc-session-server))
6202 ":" (erc-port-to-string erc-session-port)))))
6203
6204 (defun erc-format-target-and/or-server ()
6205 "Return the server name or the current target and server name combined."
6206 (let ((server-name (erc-shorten-server-name
6207 (or erc-server-announced-name
6208 erc-session-server))))
6209 (cond ((erc-default-target)
6210 (concat (erc-string-no-properties (erc-default-target))
6211 "@" server-name))
6212 (server-name server-name)
6213 (t (buffer-name (current-buffer))))))
6214
6215 (defun erc-format-network ()
6216 "Return the name of the network we are currently on."
6217 (let ((network (and (fboundp 'erc-network-name) (erc-network-name))))
6218 (if (and network (symbolp network))
6219 (symbol-name network)
6220 "")))
6221
6222 (defun erc-format-target-and/or-network ()
6223 "Return the network or the current target and network combined.
6224 If the name of the network is not available, then use the
6225 shortened server name instead."
6226 (let ((network-name (or (and (fboundp 'erc-network-name) (erc-network-name))
6227 (erc-shorten-server-name
6228 (or erc-server-announced-name
6229 erc-session-server)))))
6230 (when (and network-name (symbolp network-name))
6231 (setq network-name (symbol-name network-name)))
6232 (cond ((erc-default-target)
6233 (concat (erc-string-no-properties (erc-default-target))
6234 "@" network-name))
6235 ((and network-name
6236 (not (get-buffer network-name)))
6237 (when erc-rename-buffers
6238 (rename-buffer network-name))
6239 network-name)
6240 (t (buffer-name (current-buffer))))))
6241
6242 (defun erc-format-away-status ()
6243 "Return a formatted `erc-mode-line-away-status-format'
6244 if `erc-away' is non-nil."
6245 (let ((a (erc-away-time)))
6246 (if a
6247 (format-time-string erc-mode-line-away-status-format a)
6248 "")))
6249
6250 (defun erc-format-channel-modes ()
6251 "Return the current channel's modes."
6252 (concat (apply 'concat
6253 "+" erc-channel-modes)
6254 (cond ((and erc-channel-user-limit erc-channel-key)
6255 (if erc-show-channel-key-p
6256 (format "lk %.0f %s" erc-channel-user-limit
6257 erc-channel-key)
6258 (format "kl %.0f" erc-channel-user-limit)))
6259 (erc-channel-user-limit
6260 ;; Emacs has no bignums
6261 (format "l %.0f" erc-channel-user-limit))
6262 (erc-channel-key
6263 (if erc-show-channel-key-p
6264 (format "k %s" erc-channel-key)
6265 "k"))
6266 (t nil))))
6267
6268 (defun erc-format-lag-time ()
6269 "Return the estimated lag time to server, `erc-server-lag'."
6270 (let ((lag (erc-with-server-buffer erc-server-lag)))
6271 (cond (lag (format "lag:%.0f" lag))
6272 (t ""))))
6273
6274 ;; erc-goodies is required at end of this file.
6275 (declare-function erc-controls-strip "erc-goodies" (str))
6276
6277 (defvar tabbar--local-hlf)
6278
6279 (defun erc-update-mode-line-buffer (buffer)
6280 "Update the mode line in a single ERC buffer BUFFER."
6281 (with-current-buffer buffer
6282 (let ((spec (format-spec-make
6283 ?a (erc-format-away-status)
6284 ?l (erc-format-lag-time)
6285 ?m (erc-format-channel-modes)
6286 ?n (or (erc-current-nick) "")
6287 ?N (erc-format-network)
6288 ?o (or (erc-controls-strip erc-channel-topic) "")
6289 ?p (erc-port-to-string erc-session-port)
6290 ?s (erc-format-target-and/or-server)
6291 ?S (erc-format-target-and/or-network)
6292 ?t (erc-format-target)))
6293 (process-status (cond ((and (erc-server-process-alive)
6294 (not erc-server-connected))
6295 ":connecting")
6296 ((erc-server-process-alive)
6297 "")
6298 (t
6299 ": CLOSED")))
6300 (face (cond ((eq erc-header-line-face-method nil)
6301 nil)
6302 ((functionp erc-header-line-face-method)
6303 (funcall erc-header-line-face-method))
6304 (t
6305 'erc-header-line))))
6306 (cond ((featurep 'xemacs)
6307 (setq modeline-buffer-identification
6308 (list (format-spec erc-mode-line-format spec)))
6309 (setq modeline-process (list process-status)))
6310 (t
6311 (setq mode-line-buffer-identification
6312 (list (format-spec erc-mode-line-format spec)))
6313 (setq mode-line-process (list process-status))))
6314 (when (boundp 'header-line-format)
6315 (let ((header (if erc-header-line-format
6316 (format-spec erc-header-line-format spec)
6317 nil)))
6318 (cond (erc-header-line-uses-tabbar-p
6319 (set (make-local-variable 'tabbar--local-hlf)
6320 header-line-format)
6321 (kill-local-variable 'header-line-format))
6322 ((null header)
6323 (setq header-line-format nil))
6324 (erc-header-line-uses-help-echo-p
6325 (let ((help-echo (with-temp-buffer
6326 (insert header)
6327 (fill-region (point-min) (point-max))
6328 (buffer-string))))
6329 (setq header-line-format
6330 (erc-replace-regexp-in-string
6331 "%"
6332 "%%"
6333 (if face
6334 (erc-propertize header 'help-echo help-echo
6335 'face face)
6336 (erc-propertize header 'help-echo help-echo))))))
6337 (t (setq header-line-format
6338 (if face
6339 (erc-propertize header 'face face)
6340 header)))))))
6341 (if (featurep 'xemacs)
6342 (redraw-modeline)
6343 (force-mode-line-update))))
6344
6345 (defun erc-update-mode-line (&optional buffer)
6346 "Update the mode line in BUFFER.
6347
6348 If BUFFER is nil, update the mode line in all ERC buffers."
6349 (if (and buffer (bufferp buffer))
6350 (erc-update-mode-line-buffer buffer)
6351 (dolist (buf (erc-buffer-list))
6352 (when (buffer-live-p buf)
6353 (erc-update-mode-line-buffer buf)))))
6354
6355 ;; Miscellaneous
6356
6357 (defun erc-port-to-string (p)
6358 "Convert port P to a string.
6359 P may be an integer or a service name."
6360 (if (integerp p)
6361 (int-to-string p)
6362 p))
6363
6364 (defun erc-string-to-port (s)
6365 "Convert string S to either an integer port number or a service name."
6366 (if (numberp s)
6367 s
6368 (let ((n (string-to-number s)))
6369 (if (= n 0)
6370 s
6371 n))))
6372
6373 (defun erc-version (&optional here)
6374 "Show the version number of ERC in the minibuffer.
6375 If optional argument HERE is non-nil, insert version number at point."
6376 (interactive "P")
6377 (let ((version-string
6378 (format "ERC (IRC client for Emacs %s)" emacs-version)))
6379 (if here
6380 (insert version-string)
6381 (if (called-interactively-p 'interactive)
6382 (message "%s" version-string)
6383 version-string))))
6384
6385 (defun erc-modes (&optional here)
6386 "Show the active ERC modes in the minibuffer.
6387 If optional argument HERE is non-nil, insert version number at point."
6388 (interactive "P")
6389 (let ((string
6390 (mapconcat 'identity
6391 (let (modes (case-fold-search nil))
6392 (dolist (var (apropos-internal "^erc-.*mode$"))
6393 (when (and (boundp var)
6394 (symbol-value var))
6395 (setq modes (cons (symbol-name var)
6396 modes))))
6397 modes)
6398 ", ")))
6399 (if here
6400 (insert string)
6401 (if (called-interactively-p 'interactive)
6402 (message "%s" string)
6403 string))))
6404
6405 (defun erc-trim-string (s)
6406 "Trim leading and trailing spaces off S."
6407 (cond
6408 ((not (stringp s)) nil)
6409 ((string-match "^\\s-*$" s)
6410 "")
6411 ((string-match "^\\s-*\\(.*\\S-\\)\\s-*$" s)
6412 (match-string 1 s))
6413 (t
6414 s)))
6415
6416 (defun erc-arrange-session-in-multiple-windows ()
6417 "Open a window for every non-server buffer related to `erc-session-server'.
6418
6419 All windows are opened in the current frame."
6420 (interactive)
6421 (unless erc-server-process
6422 (error "No erc-server-process found in current buffer"))
6423 (let ((bufs (erc-buffer-list nil erc-server-process)))
6424 (when bufs
6425 (delete-other-windows)
6426 (switch-to-buffer (car bufs))
6427 (setq bufs (cdr bufs))
6428 (while bufs
6429 (split-window)
6430 (other-window 1)
6431 (switch-to-buffer (car bufs))
6432 (setq bufs (cdr bufs))
6433 (balance-windows)))))
6434
6435 (defun erc-popup-input-buffer ()
6436 "Provide an input buffer."
6437 (interactive)
6438 (let ((buffer-name (generate-new-buffer-name "*ERC input*"))
6439 (mode (intern
6440 (completing-read
6441 "Mode: "
6442 (mapcar (lambda (e)
6443 (list (symbol-name e)))
6444 (apropos-internal "-mode$" 'commandp))
6445 nil t))))
6446 (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name))
6447 (funcall mode)
6448 (narrow-to-region (point) (point))
6449 (shrink-window-if-larger-than-buffer)))
6450
6451 ;;; Message catalog
6452
6453 (defun erc-make-message-variable-name (catalog entry)
6454 "Create a variable name corresponding to CATALOG's ENTRY."
6455 (intern (concat "erc-message-"
6456 (symbol-name catalog) "-" (symbol-name entry))))
6457
6458 (defun erc-define-catalog-entry (catalog entry format-spec)
6459 "Set CATALOG's ENTRY to FORMAT-SPEC."
6460 (set (erc-make-message-variable-name catalog entry)
6461 format-spec))
6462
6463 (defun erc-define-catalog (catalog entries)
6464 "Define a CATALOG according to ENTRIES."
6465 (dolist (entry entries)
6466 (erc-define-catalog-entry catalog (car entry) (cdr entry))))
6467
6468 (erc-define-catalog
6469 'english
6470 '((bad-ping-response . "Unexpected PING response from %n (time %t)")
6471 (bad-syntax . "Error occurred - incorrect usage?\n%c %u\n%d")
6472 (incorrect-args . "Incorrect arguments. Usage:\n%c %u\n%d")
6473 (cannot-find-file . "Cannot find file %f")
6474 (cannot-read-file . "Cannot read file %f")
6475 (connect . "Connecting to %S:%p... ")
6476 (country . "%c")
6477 (country-unknown . "%d: No such domain")
6478 (ctcp-empty . "Illegal empty CTCP query received from %n. Ignoring.")
6479 (ctcp-request . "==> CTCP request from %n (%u@%h): %r")
6480 (ctcp-request-to . "==> CTCP request from %n (%u@%h) to %t: %r")
6481 (ctcp-too-many . "Too many CTCP queries in single message. Ignoring")
6482 (flood-ctcp-off . "FLOOD PROTECTION: Automatic CTCP responses turned off.")
6483 (flood-strict-mode
6484 . "FLOOD PROTECTION: Switched to Strict Flood Control mode.")
6485 (disconnected . "\n\nConnection failed! Re-establishing connection...\n")
6486 (disconnected-noreconnect
6487 . "\n\nConnection failed! Not re-establishing connection.\n")
6488 (finished . "\n\n*** ERC finished ***\n")
6489 (terminated . "\n\n*** ERC terminated: %e\n")
6490 (login . "Logging in as \'%n\'...")
6491 (nick-in-use . "%n is in use. Choose new nickname: ")
6492 (nick-too-long
6493 . "WARNING: Nick length (%i) exceeds max NICKLEN(%l) defined by server")
6494 (no-default-channel . "No default channel")
6495 (no-invitation . "You've got no invitation")
6496 (no-target . "No target")
6497 (ops . "%i operator%s: %o")
6498 (ops-none . "No operators in this channel.")
6499 (undefined-ctcp . "Undefined CTCP query received. Silently ignored")
6500 (variable-not-bound . "Variable not bound!")
6501 (ACTION . "* %n %a")
6502 (CTCP-CLIENTINFO . "Client info for %n: %m")
6503 (CTCP-ECHO . "Echo %n: %m")
6504 (CTCP-FINGER . "Finger info for %n: %m")
6505 (CTCP-PING . "Ping time to %n is %t")
6506 (CTCP-TIME . "Time by %n is %m")
6507 (CTCP-UNKNOWN . "Unknown CTCP message from %n (%u@%h): %m")
6508 (CTCP-VERSION . "Version for %n is %m")
6509 (ERROR . "==> ERROR from %s: %c\n")
6510 (INVITE . "%n (%u@%h) invites you to channel %c")
6511 (JOIN . "%n (%u@%h) has joined channel %c")
6512 (JOIN-you . "You have joined channel %c")
6513 (KICK . "%n (%u@%h) has kicked %k off channel %c: %r")
6514 (KICK-you . "You have been kicked off channel %c by %n (%u@%h): %r")
6515 (KICK-by-you . "You have kicked %k off channel %c: %r")
6516 (MODE . "%n (%u@%h) has changed mode for %t to %m")
6517 (MODE-nick . "%n has changed mode for %t to %m")
6518 (NICK . "%n (%u@%h) is now known as %N")
6519 (NICK-you . "Your new nickname is %N")
6520 (PART . erc-message-english-PART)
6521 (PING . "PING from server (last: %s sec. ago)")
6522 (PONG . "PONG from %h (%i second%s)")
6523 (QUIT . "%n (%u@%h) has quit: %r")
6524 (TOPIC . "%n (%u@%h) has set the topic for %c: \"%T\"")
6525 (WALLOPS . "Wallops from %n: %m")
6526 (s004 . "%s %v %U %C")
6527 (s221 . "User modes for %n: %m")
6528 (s252 . "%i operator(s) online")
6529 (s253 . "%i unknown connection(s)")
6530 (s254 . "%i channels formed")
6531 (s275 . "%n %m")
6532 (s301 . "%n is AWAY: %r")
6533 (s303 . "Is online: %n")
6534 (s305 . "%m")
6535 (s306 . "%m")
6536 (s307 . "%n %m")
6537 (s311 . "%n is %f (%u@%h)")
6538 (s312 . "%n is/was on server %s (%c)")
6539 (s313 . "%n is an IRC operator")
6540 (s314 . "%n was %f (%u@%h)")
6541 (s317 . "%n has been idle for %i")
6542 (s317-on-since . "%n has been idle for %i, on since %t")
6543 (s319 . "%n is on channel(s): %c")
6544 (s320 . "%n is an identified user")
6545 (s321 . "Channel Users Topic")
6546 (s322 . "%c [%u] %t")
6547 (s324 . "%c modes: %m")
6548 (s328 . "%c URL: %u")
6549 (s329 . "%c was created on %t")
6550 (s330 . "%n %a %i")
6551 (s331 . "No topic is set for %c")
6552 (s332 . "Topic for %c: %T")
6553 (s333 . "%c: topic set by %n, %t")
6554 (s341 . "Inviting %n to channel %c")
6555 (s352 . "%-11c %-10n %-4a %u@%h (%f)")
6556 (s353 . "Users on %c: %u")
6557 (s367 . "Ban for %b on %c")
6558 (s367-set-by . "Ban for %b on %c set by %s on %t")
6559 (s368 . "Banlist of %c ends.")
6560 (s379 . "%c: Forwarded to %f")
6561 (s391 . "The time at %s is %t")
6562 (s401 . "%n: No such nick/channel")
6563 (s403 . "%c: No such channel")
6564 (s404 . "%c: Cannot send to channel")
6565 (s405 . "%c: You have joined too many channels")
6566 (s406 . "%n: There was no such nickname")
6567 (s412 . "No text to send")
6568 (s421 . "%c: Unknown command")
6569 (s431 . "No nickname given")
6570 (s432 . "%n is an erroneous nickname")
6571 (s442 . "%c: You're not on that channel")
6572 (s445 . "SUMMON has been disabled")
6573 (s446 . "USERS has been disabled")
6574 (s451 . "You have not registered")
6575 (s461 . "%c: not enough parameters")
6576 (s462 . "Unauthorized command (already registered)")
6577 (s463 . "Your host isn't among the privileged")
6578 (s464 . "Password incorrect")
6579 (s465 . "You are banned from this server")
6580 (s474 . "You can't join %c because you're banned (+b)")
6581 (s475 . "You must specify the correct channel key (+k) to join %c")
6582 (s481 . "Permission Denied - You're not an IRC operator")
6583 (s482 . "You need to be a channel operator of %c to do that")
6584 (s483 . "You can't kill a server!")
6585 (s484 . "Your connection is restricted!")
6586 (s485 . "You're not the original channel operator")
6587 (s491 . "No O-lines for your host")
6588 (s501 . "Unknown MODE flag")
6589 (s502 . "You can't change modes for other users")
6590 (s671 . "%n %a")))
6591
6592 (defun erc-message-english-PART (&rest args)
6593 "Format a proper PART message.
6594
6595 This function is an example on what could be done with formatting
6596 functions."
6597 (let ((nick (cadr (memq ?n args)))
6598 (user (cadr (memq ?u args)))
6599 (host (cadr (memq ?h args)))
6600 (channel (cadr (memq ?c args)))
6601 (reason (cadr (memq ?r args))))
6602 (if (string= nick (erc-current-nick))
6603 (format "You have left channel %s" channel)
6604 (format "%s (%s@%s) has left channel %s%s"
6605 nick user host channel
6606 (if (not (string= reason ""))
6607 (format ": %s"
6608 (erc-replace-regexp-in-string "%" "%%" reason))
6609 "")))))
6610
6611
6612 (defvar erc-current-message-catalog 'english)
6613 (make-variable-buffer-local 'erc-current-message-catalog)
6614
6615 (defun erc-retrieve-catalog-entry (entry &optional catalog)
6616 "Retrieve ENTRY from CATALOG.
6617
6618 If CATALOG is nil, `erc-current-message-catalog' is used.
6619
6620 If ENTRY is nil in CATALOG, it is retrieved from the fallback,
6621 english, catalog."
6622 (unless catalog (setq catalog erc-current-message-catalog))
6623 (let ((var (erc-make-message-variable-name catalog entry)))
6624 (if (boundp var)
6625 (symbol-value var)
6626 (when (boundp (erc-make-message-variable-name 'english entry))
6627 (symbol-value (erc-make-message-variable-name 'english entry))))))
6628
6629 (defun erc-format-message (msg &rest args)
6630 "Format MSG according to ARGS.
6631
6632 See also `format-spec'."
6633 (when (eq (logand (length args) 1) 1) ; oddp
6634 (error "Obscure usage of this function appeared"))
6635 (let ((entry (erc-retrieve-catalog-entry msg)))
6636 (when (not entry)
6637 (error "No format spec for message %s" msg))
6638 (when (functionp entry)
6639 (setq entry (apply entry args)))
6640 (format-spec entry (apply 'format-spec-make args))))
6641
6642 ;;; Various hook functions
6643
6644 (add-hook 'kill-buffer-hook 'erc-kill-buffer-function)
6645
6646 (defcustom erc-kill-server-hook '(erc-kill-server)
6647 "Invoked whenever a server buffer is killed via `kill-buffer'."
6648 :group 'erc-hooks
6649 :type 'hook)
6650
6651 (defcustom erc-kill-channel-hook '(erc-kill-channel)
6652 "Invoked whenever a channel-buffer is killed via `kill-buffer'."
6653 :group 'erc-hooks
6654 :type 'hook)
6655
6656 (defcustom erc-kill-buffer-hook nil
6657 "Hook run whenever a non-server or channel buffer is killed.
6658
6659 See also `kill-buffer'."
6660 :group 'erc-hooks
6661 :type 'hook)
6662
6663 (defun erc-kill-buffer-function ()
6664 "Function to call when an ERC buffer is killed.
6665 This function should be on `kill-buffer-hook'.
6666 When the current buffer is in `erc-mode', this function will run
6667 one of the following hooks:
6668 `erc-kill-server-hook' if the server buffer was killed,
6669 `erc-kill-channel-hook' if a channel buffer was killed,
6670 or `erc-kill-buffer-hook' if any other buffer."
6671 (when (eq major-mode 'erc-mode)
6672 (erc-remove-channel-users)
6673 (cond
6674 ((eq (erc-server-buffer) (current-buffer))
6675 (run-hooks 'erc-kill-server-hook))
6676 ((erc-channel-p (erc-default-target))
6677 (run-hooks 'erc-kill-channel-hook))
6678 (t
6679 (run-hooks 'erc-kill-buffer-hook)))))
6680
6681 (defun erc-kill-server ()
6682 "Sends a QUIT command to the server when the server buffer is killed.
6683 This function should be on `erc-kill-server-hook'."
6684 (when (erc-server-process-alive)
6685 (setq erc-server-quitting t)
6686 (erc-server-send (format "QUIT :%s" (funcall erc-quit-reason nil)))))
6687
6688 (defun erc-kill-channel ()
6689 "Sends a PART command to the server when the channel buffer is killed.
6690 This function should be on `erc-kill-channel-hook'."
6691 (when (erc-server-process-alive)
6692 (let ((tgt (erc-default-target)))
6693 (erc-server-send (format "PART %s :%s" tgt
6694 (funcall erc-part-reason nil))
6695 nil tgt))))
6696
6697 ;;; Dealing with `erc-parsed'
6698
6699 (defun erc-find-parsed-property ()
6700 "Find the next occurrence of the `erc-parsed' text property."
6701 (text-property-not-all (point-min) (point-max) 'erc-parsed nil))
6702
6703 (defun erc-restore-text-properties ()
6704 "Restore the property 'erc-parsed for the region."
6705 (let ((parsed-posn (erc-find-parsed-property)))
6706 (put-text-property
6707 (point-min) (point-max)
6708 'erc-parsed (when parsed-posn (erc-get-parsed-vector parsed-posn)))))
6709
6710 (defun erc-get-parsed-vector (point)
6711 "Return the whole parsed vector on POINT."
6712 (get-text-property point 'erc-parsed))
6713
6714 (defun erc-get-parsed-vector-nick (vect)
6715 "Return nickname in the parsed vector VECT."
6716 (let* ((untreated-nick (and vect (erc-response.sender vect)))
6717 (maybe-nick (when untreated-nick
6718 (car (split-string untreated-nick "!")))))
6719 (when (and (not (null maybe-nick))
6720 (erc-is-valid-nick-p maybe-nick))
6721 untreated-nick)))
6722
6723 (defun erc-get-parsed-vector-type (vect)
6724 "Return message type in the parsed vector VECT."
6725 (and vect
6726 (erc-response.command vect)))
6727
6728 ;; Teach url.el how to open irc:// URLs with ERC.
6729 ;; To activate, customize `url-irc-function' to `url-irc-erc'.
6730
6731 ;;;###autoload
6732 (defun erc-handle-irc-url (host port channel user password)
6733 "Use ERC to IRC on HOST:PORT in CHANNEL as USER with PASSWORD.
6734 If ERC is already connected to HOST:PORT, simply /join CHANNEL.
6735 Otherwise, connect to HOST:PORT as USER and /join CHANNEL."
6736 (let ((server-buffer
6737 (car (erc-buffer-filter
6738 (lambda ()
6739 (and (string-equal erc-session-server host)
6740 (= erc-session-port port)
6741 (erc-open-server-buffer-p)))))))
6742 (with-current-buffer (or server-buffer (current-buffer))
6743 (if (and server-buffer channel)
6744 (erc-cmd-JOIN channel)
6745 (erc-open host port (or user (erc-compute-nick)) (erc-compute-full-name)
6746 (not server-buffer) password nil channel
6747 (when server-buffer
6748 (get-buffer-process server-buffer)))))))
6749
6750 (provide 'erc)
6751
6752 ;; Deprecated. We might eventually stop requiring the goodies automatically.
6753 ;; IMPORTANT: This require must appear _after_ the above (provide 'erc) to
6754 ;; avoid a recursive require error when byte-compiling the entire package.
6755 (require 'erc-goodies)
6756
6757 ;;; erc.el ends here
6758 ;;
6759 ;; Local Variables:
6760 ;; outline-regexp: ";;+"
6761 ;; indent-tabs-mode: t
6762 ;; tab-width: 8
6763 ;; End: