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