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