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