Search for host/port combinations in auth-source on OS X
[emacs.git] / lisp / net / rcirc.el
blobf2c8c5d50c5f76b0a665413009a047ea2dd24409
1 ;;; rcirc.el --- default, simple IRC client -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2005-2016 Free Software Foundation, Inc.
5 ;; Author: Ryan Yeske <rcyeske@gmail.com>
6 ;; Maintainers: Ryan Yeske <rcyeske@gmail.com>,
7 ;; Leo Liu <sdl.web@gmail.com>
8 ;; Keywords: comm
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Internet Relay Chat (IRC) is a form of instant communication over
28 ;; the Internet. It is mainly designed for group (many-to-many)
29 ;; communication in discussion forums called channels, but also allows
30 ;; one-to-one communication.
32 ;; Rcirc has simple defaults and clear and consistent behavior.
33 ;; Message arrival timestamps, activity notification on the mode line,
34 ;; message filling, nick completion, and keepalive pings are all
35 ;; enabled by default, but can easily be adjusted or turned off. Each
36 ;; discussion takes place in its own buffer and there is a single
37 ;; server buffer per connection.
39 ;; Open a new irc connection with:
40 ;; M-x irc RET
42 ;;; Todo:
44 ;;; Code:
46 (require 'cl-lib)
47 (require 'ring)
48 (require 'time-date)
50 (defgroup rcirc nil
51 "Simple IRC client."
52 :version "22.1"
53 :prefix "rcirc-"
54 :link '(custom-manual "(rcirc)")
55 :group 'applications)
57 (defcustom rcirc-server-alist
58 '(("irc.freenode.net" :channels ("#rcirc")
59 ;; Don't use the TLS port by default, in case gnutls is not available.
60 ;; :port 7000 :encryption tls
61 ))
62 "An alist of IRC connections to establish when running `rcirc'.
63 Each element looks like (SERVER-NAME PARAMETERS).
65 SERVER-NAME is a string describing the server to connect
66 to.
68 The optional PARAMETERS come in pairs PARAMETER VALUE.
70 The following parameters are recognized:
72 `:nick'
74 VALUE must be a string. If absent, `rcirc-default-nick' is used
75 for this connection.
77 `:port'
79 VALUE must be a number or string. If absent,
80 `rcirc-default-port' is used.
82 `:user-name'
84 VALUE must be a string. If absent, `rcirc-default-user-name' is
85 used.
87 `:password'
89 VALUE must be a string. If absent, no PASS command will be sent
90 to the server.
92 `:full-name'
94 VALUE must be a string. If absent, `rcirc-default-full-name' is
95 used.
97 `:channels'
99 VALUE must be a list of strings describing which channels to join
100 when connecting to this server. If absent, no channels will be
101 connected to automatically.
103 `:encryption'
105 VALUE must be `plain' (the default) for unencrypted connections, or `tls'
106 for connections using SSL/TLS.
108 `:server-alias'
110 VALUE must be a string that will be used instead of the server name for
111 display purposes. If absent, the real server name will be displayed instead."
112 :type '(alist :key-type string
113 :value-type (plist :options
114 ((:nick string)
115 (:port integer)
116 (:user-name string)
117 (:password string)
118 (:full-name string)
119 (:channels (repeat string))
120 (:encryption (choice (const tls)
121 (const plain)))
122 (:server-alias string))))
123 :group 'rcirc)
125 (defcustom rcirc-default-port 6667
126 "The default port to connect to."
127 :type 'integer
128 :group 'rcirc)
130 (defcustom rcirc-default-nick (user-login-name)
131 "Your nick."
132 :type 'string
133 :group 'rcirc)
135 (defcustom rcirc-default-user-name "user"
136 "Your user name sent to the server when connecting."
137 :version "24.1" ; changed default
138 :type 'string
139 :group 'rcirc)
141 (defcustom rcirc-default-full-name "unknown"
142 "The full name sent to the server when connecting."
143 :version "24.1" ; changed default
144 :type 'string
145 :group 'rcirc)
147 (defcustom rcirc-fill-flag t
148 "Non-nil means line-wrap messages printed in channel buffers."
149 :type 'boolean
150 :group 'rcirc)
152 (defcustom rcirc-fill-column nil
153 "Column beyond which automatic line-wrapping should happen.
154 If nil, use value of `fill-column'.
155 If a function (e.g., `frame-text-width' or `window-text-width'),
156 call it to compute the number of columns."
157 :risky t ; can get funcalled
158 :type '(choice (const :tag "Value of `fill-column'" nil)
159 (integer :tag "Number of columns")
160 (function :tag "Function returning the number of columns"))
161 :group 'rcirc)
163 (defcustom rcirc-fill-prefix nil
164 "Text to insert before filled lines.
165 If nil, calculate the prefix dynamically to line up text
166 underneath each nick."
167 :type '(choice (const :tag "Dynamic" nil)
168 (string :tag "Prefix text"))
169 :group 'rcirc)
171 (defvar rcirc-ignore-buffer-activity-flag nil
172 "If non-nil, ignore activity in this buffer.")
173 (make-variable-buffer-local 'rcirc-ignore-buffer-activity-flag)
175 (defvar rcirc-low-priority-flag nil
176 "If non-nil, activity in this buffer is considered low priority.")
177 (make-variable-buffer-local 'rcirc-low-priority-flag)
179 (defvar rcirc-omit-mode nil
180 "Non-nil if Rcirc-Omit mode is enabled.
181 Use the command `rcirc-omit-mode' to change this variable.")
182 (make-variable-buffer-local 'rcirc-omit-mode)
184 (defcustom rcirc-time-format "%H:%M "
185 "Describes how timestamps are printed.
186 Used as the first arg to `format-time-string'."
187 :type 'string
188 :group 'rcirc)
190 (defcustom rcirc-input-ring-size 1024
191 "Size of input history ring."
192 :type 'integer
193 :group 'rcirc)
195 (defcustom rcirc-read-only-flag t
196 "Non-nil means make text in IRC buffers read-only."
197 :type 'boolean
198 :group 'rcirc)
200 (defcustom rcirc-buffer-maximum-lines nil
201 "The maximum size in lines for rcirc buffers.
202 Channel buffers are truncated from the top to be no greater than this
203 number. If zero or nil, no truncating is done."
204 :type '(choice (const :tag "No truncation" nil)
205 (integer :tag "Number of lines"))
206 :group 'rcirc)
208 (defcustom rcirc-scroll-show-maximum-output t
209 "If non-nil, scroll buffer to keep the point at the bottom of
210 the window."
211 :type 'boolean
212 :group 'rcirc)
214 (defcustom rcirc-authinfo nil
215 "List of authentication passwords.
216 Each element of the list is a list with a SERVER-REGEXP string
217 and a method symbol followed by method specific arguments.
219 The valid METHOD symbols are `nickserv', `chanserv' and
220 `bitlbee'.
222 The ARGUMENTS for each METHOD symbol are:
223 `nickserv': NICK PASSWORD [NICKSERV-NICK]
224 `chanserv': NICK CHANNEL PASSWORD
225 `bitlbee': NICK PASSWORD
226 `quakenet': ACCOUNT PASSWORD
228 Examples:
229 ((\"freenode\" nickserv \"bob\" \"p455w0rd\")
230 (\"freenode\" chanserv \"bob\" \"#bobland\" \"passwd99\")
231 (\"bitlbee\" bitlbee \"robert\" \"sekrit\")
232 (\"dal.net\" nickserv \"bob\" \"sekrit\" \"NickServ@services.dal.net\")
233 (\"quakenet.org\" quakenet \"bobby\" \"sekrit\"))"
234 :type '(alist :key-type (string :tag "Server")
235 :value-type (choice (list :tag "NickServ"
236 (const nickserv)
237 (string :tag "Nick")
238 (string :tag "Password"))
239 (list :tag "ChanServ"
240 (const chanserv)
241 (string :tag "Nick")
242 (string :tag "Channel")
243 (string :tag "Password"))
244 (list :tag "BitlBee"
245 (const bitlbee)
246 (string :tag "Nick")
247 (string :tag "Password"))
248 (list :tag "QuakeNet"
249 (const quakenet)
250 (string :tag "Account")
251 (string :tag "Password"))))
252 :group 'rcirc)
254 (defcustom rcirc-auto-authenticate-flag t
255 "Non-nil means automatically send authentication string to server.
256 See also `rcirc-authinfo'."
257 :type 'boolean
258 :group 'rcirc)
260 (defcustom rcirc-authenticate-before-join t
261 "Non-nil means authenticate to services before joining channels.
262 Currently only works with NickServ on some networks."
263 :version "24.1"
264 :type 'boolean
265 :group 'rcirc)
267 (defcustom rcirc-prompt "> "
268 "Prompt string to use in IRC buffers.
270 The following replacements are made:
271 %n is your nick.
272 %s is the server.
273 %t is the buffer target, a channel or a user.
275 Setting this alone will not affect the prompt;
276 use either M-x customize or also call `rcirc-update-prompt'."
277 :type 'string
278 :set 'rcirc-set-changed
279 :initialize 'custom-initialize-default
280 :group 'rcirc)
282 (defcustom rcirc-keywords nil
283 "List of keywords to highlight in message text."
284 :type '(repeat string)
285 :group 'rcirc)
287 (defcustom rcirc-ignore-list ()
288 "List of ignored nicks.
289 Use /ignore to list them, use /ignore NICK to add or remove a nick."
290 :type '(repeat string)
291 :group 'rcirc)
293 (defvar rcirc-ignore-list-automatic ()
294 "List of ignored nicks added to `rcirc-ignore-list' because of renaming.
295 When an ignored person renames, their nick is added to both lists.
296 Nicks will be removed from the automatic list on follow-up renamings or
297 parts.")
299 (defcustom rcirc-bright-nicks nil
300 "List of nicks to be emphasized.
301 See `rcirc-bright-nick' face."
302 :type '(repeat string)
303 :group 'rcirc)
305 (defcustom rcirc-dim-nicks nil
306 "List of nicks to be deemphasized.
307 See `rcirc-dim-nick' face."
308 :type '(repeat string)
309 :group 'rcirc)
311 (define-obsolete-variable-alias 'rcirc-print-hooks
312 'rcirc-print-functions "24.3")
313 (defcustom rcirc-print-functions nil
314 "Hook run after text is printed.
315 Called with 5 arguments, PROCESS, SENDER, RESPONSE, TARGET and TEXT."
316 :type 'hook
317 :group 'rcirc)
319 (defvar rcirc-authenticated-hook nil
320 "Hook run after successfully authenticated.")
322 (defcustom rcirc-always-use-server-buffer-flag nil
323 "Non-nil means messages without a channel target will go to the server buffer."
324 :type 'boolean
325 :group 'rcirc)
327 (defcustom rcirc-decode-coding-system 'utf-8
328 "Coding system used to decode incoming irc messages.
329 Set to `undecided' if you want the encoding of the incoming
330 messages autodetected."
331 :type 'coding-system
332 :group 'rcirc)
334 (defcustom rcirc-encode-coding-system 'utf-8
335 "Coding system used to encode outgoing irc messages."
336 :type 'coding-system
337 :group 'rcirc)
339 (defcustom rcirc-coding-system-alist nil
340 "Alist to decide a coding system to use for a channel I/O operation.
341 The format is ((PATTERN . VAL) ...).
342 PATTERN is either a string or a cons of strings.
343 If PATTERN is a string, it is used to match a target.
344 If PATTERN is a cons of strings, the car part is used to match a
345 target, and the cdr part is used to match a server.
346 VAL is either a coding system or a cons of coding systems.
347 If VAL is a coding system, it is used for both decoding and encoding
348 messages.
349 If VAL is a cons of coding systems, the car part is used for decoding,
350 and the cdr part is used for encoding."
351 :type '(alist :key-type (choice (string :tag "Channel Regexp")
352 (cons (string :tag "Channel Regexp")
353 (string :tag "Server Regexp")))
354 :value-type (choice coding-system
355 (cons (coding-system :tag "Decode")
356 (coding-system :tag "Encode"))))
357 :group 'rcirc)
359 (defcustom rcirc-multiline-major-mode 'fundamental-mode
360 "Major-mode function to use in multiline edit buffers."
361 :type 'function
362 :group 'rcirc)
364 (defcustom rcirc-nick-completion-format "%s: "
365 "Format string to use in nick completions.
367 The format string is only used when completing at the beginning
368 of a line. The string is passed as the first argument to
369 `format' with the nickname as the second argument."
370 :version "24.1"
371 :type 'string
372 :group 'rcirc)
374 (defcustom rcirc-kill-channel-buffers nil
375 "When non-nil, kill channel buffers when the server buffer is killed.
376 Only the channel buffers associated with the server in question
377 will be killed."
378 :version "24.3"
379 :type 'boolean
380 :group 'rcirc)
382 (defvar rcirc-nick nil)
384 (defvar rcirc-prompt-start-marker nil)
385 (defvar rcirc-prompt-end-marker nil)
387 (defvar rcirc-nick-table nil)
389 (defvar rcirc-recent-quit-alist nil
390 "Alist of nicks that have recently quit or parted the channel.")
392 (defvar rcirc-nick-syntax-table
393 (let ((table (make-syntax-table text-mode-syntax-table)))
394 (mapc (lambda (c) (modify-syntax-entry c "w" table))
395 "[]\\`_^{|}-")
396 (modify-syntax-entry ?' "_" table)
397 table)
398 "Syntax table which includes all nick characters as word constituents.")
400 ;; each process has an alist of (target . buffer) pairs
401 (defvar rcirc-buffer-alist nil)
403 (defvar rcirc-activity nil
404 "List of buffers with unviewed activity.")
406 (defvar rcirc-activity-string ""
407 "String displayed in mode line representing `rcirc-activity'.")
408 (put 'rcirc-activity-string 'risky-local-variable t)
410 (defvar rcirc-server-buffer nil
411 "The server buffer associated with this channel buffer.")
413 (defvar rcirc-target nil
414 "The channel or user associated with this buffer.")
416 (defvar rcirc-urls nil
417 "List of URLs seen in the current buffer and their start positions.")
418 (put 'rcirc-urls 'permanent-local t)
420 (defvar rcirc-timeout-seconds 600
421 "Kill connection after this many seconds if there is no activity.")
423 (defconst rcirc-id-string (concat "rcirc on GNU Emacs " emacs-version))
424 \f
425 (defvar rcirc-startup-channels nil)
427 (defvar rcirc-server-name-history nil
428 "History variable for \\[rcirc] call.")
430 (defvar rcirc-server-port-history nil
431 "History variable for \\[rcirc] call.")
433 (defvar rcirc-nick-name-history nil
434 "History variable for \\[rcirc] call.")
436 (defvar rcirc-user-name-history nil
437 "History variable for \\[rcirc] call.")
439 ;;;###autoload
440 (defun rcirc (arg)
441 "Connect to all servers in `rcirc-server-alist'.
443 Do not connect to a server if it is already connected.
445 If ARG is non-nil, instead prompt for connection parameters."
446 (interactive "P")
447 (if arg
448 (let* ((server (completing-read "IRC Server: "
449 rcirc-server-alist
450 nil nil
451 (caar rcirc-server-alist)
452 'rcirc-server-name-history))
453 (server-plist (cdr (assoc-string server rcirc-server-alist)))
454 (port (read-string "IRC Port: "
455 (number-to-string
456 (or (plist-get server-plist :port)
457 rcirc-default-port))
458 'rcirc-server-port-history))
459 (nick (read-string "IRC Nick: "
460 (or (plist-get server-plist :nick)
461 rcirc-default-nick)
462 'rcirc-nick-name-history))
463 (user-name (read-string "IRC Username: "
464 (or (plist-get server-plist :user-name)
465 rcirc-default-user-name)
466 'rcirc-user-name-history))
467 (password (read-passwd "IRC Password: " nil
468 (plist-get server-plist :password)))
469 (channels (split-string
470 (read-string "IRC Channels: "
471 (mapconcat 'identity
472 (plist-get server-plist
473 :channels)
474 " "))
475 "[, ]+" t))
476 (encryption (rcirc-prompt-for-encryption server-plist)))
477 (rcirc-connect server port nick user-name
478 rcirc-default-full-name
479 channels password encryption))
480 ;; connect to servers in `rcirc-server-alist'
481 (let (connected-servers)
482 (dolist (c rcirc-server-alist)
483 (let ((server (car c))
484 (nick (or (plist-get (cdr c) :nick) rcirc-default-nick))
485 (port (or (plist-get (cdr c) :port) rcirc-default-port))
486 (user-name (or (plist-get (cdr c) :user-name)
487 rcirc-default-user-name))
488 (full-name (or (plist-get (cdr c) :full-name)
489 rcirc-default-full-name))
490 (channels (plist-get (cdr c) :channels))
491 (password (plist-get (cdr c) :password))
492 (encryption (plist-get (cdr c) :encryption))
493 (server-alias (plist-get (cdr c) :server-alias))
494 contact)
495 (when server
496 (let (connected)
497 (dolist (p (rcirc-process-list))
498 (when (string= (or server-alias server) (process-name p))
499 (setq connected p)))
500 (if (not connected)
501 (condition-case nil
502 (rcirc-connect server port nick user-name
503 full-name channels password encryption
504 server-alias)
505 (quit (message "Quit connecting to %s"
506 (or server-alias server))))
507 (with-current-buffer (process-buffer connected)
508 (setq contact (process-contact
509 (get-buffer-process (current-buffer)) :name))
510 (setq connected-servers
511 (cons (if (stringp contact)
512 contact (or server-alias server))
513 connected-servers))))))))
514 (when connected-servers
515 (message "Already connected to %s"
516 (if (cdr connected-servers)
517 (concat (mapconcat 'identity (butlast connected-servers) ", ")
518 ", and "
519 (car (last connected-servers)))
520 (car connected-servers)))))))
522 ;;;###autoload
523 (defalias 'irc 'rcirc)
525 \f
526 (defvar rcirc-process-output nil)
527 (defvar rcirc-topic nil)
528 (defvar rcirc-keepalive-timer nil)
529 (defvar rcirc-last-server-message-time nil)
530 (defvar rcirc-server nil) ; server provided by server
531 (defvar rcirc-server-name nil) ; server name given by 001 response
532 (defvar rcirc-timeout-timer nil)
533 (defvar rcirc-user-authenticated nil)
534 (defvar rcirc-user-disconnect nil)
535 (defvar rcirc-connecting nil)
536 (defvar rcirc-connection-info nil)
537 (defvar rcirc-process nil)
539 ;;;###autoload
540 (defun rcirc-connect (server &optional port nick user-name
541 full-name startup-channels password encryption
542 server-alias)
543 (save-excursion
544 (message "Connecting to %s..." (or server-alias server))
545 (let* ((inhibit-eol-conversion)
546 (port-number (if port
547 (if (stringp port)
548 (string-to-number port)
549 port)
550 rcirc-default-port))
551 (nick (or nick rcirc-default-nick))
552 (user-name (or user-name rcirc-default-user-name))
553 (full-name (or full-name rcirc-default-full-name))
554 (startup-channels startup-channels)
555 (process (open-network-stream
556 (or server-alias server) nil server port-number
557 :type (or encryption 'plain))))
558 ;; set up process
559 (set-process-coding-system process 'raw-text 'raw-text)
560 (switch-to-buffer (rcirc-generate-new-buffer-name process nil))
561 (set-process-buffer process (current-buffer))
562 (rcirc-mode process nil)
563 (set-process-sentinel process 'rcirc-sentinel)
564 (set-process-filter process 'rcirc-filter)
566 (setq-local rcirc-connection-info
567 (list server port nick user-name full-name startup-channels
568 password encryption))
569 (setq-local rcirc-process process)
570 (setq-local rcirc-server server)
571 (setq-local rcirc-server-name
572 (or server-alias server)) ; Update when we get 001 response.
573 (setq-local rcirc-buffer-alist nil)
574 (setq-local rcirc-nick-table (make-hash-table :test 'equal))
575 (setq-local rcirc-nick nick)
576 (setq-local rcirc-process-output nil)
577 (setq-local rcirc-startup-channels startup-channels)
578 (setq-local rcirc-last-server-message-time (current-time))
580 (setq-local rcirc-timeout-timer nil)
581 (setq-local rcirc-user-disconnect nil)
582 (setq-local rcirc-user-authenticated nil)
583 (setq-local rcirc-connecting t)
585 (add-hook 'auto-save-hook 'rcirc-log-write)
587 ;; identify
588 (unless (zerop (length password))
589 (rcirc-send-string process (concat "PASS " password)))
590 (rcirc-send-string process (concat "NICK " nick))
591 (rcirc-send-string process (concat "USER " user-name
592 " 0 * :" full-name))
594 ;; setup ping timer if necessary
595 (unless rcirc-keepalive-timer
596 (setq rcirc-keepalive-timer
597 (run-at-time 0 (/ rcirc-timeout-seconds 2) 'rcirc-keepalive)))
599 (message "Connecting to %s...done" (or server-alias server))
601 ;; return process object
602 process)))
604 (defmacro with-rcirc-process-buffer (process &rest body)
605 (declare (indent 1) (debug t))
606 `(with-current-buffer (process-buffer ,process)
607 ,@body))
609 (defmacro with-rcirc-server-buffer (&rest body)
610 (declare (indent 0) (debug t))
611 `(with-current-buffer rcirc-server-buffer
612 ,@body))
614 (defalias 'rcirc-float-time
615 (if (featurep 'xemacs)
616 'time-to-seconds
617 'float-time))
619 (defun rcirc-prompt-for-encryption (server-plist)
620 "Prompt the user for the encryption method to use.
621 SERVER-PLIST is the property list for the server."
622 (let ((msg "Encryption (default %s): ")
623 (choices '("plain" "tls"))
624 (default (or (plist-get server-plist :encryption)
625 'plain)))
626 (intern
627 (completing-read (format msg default)
628 choices nil t nil nil (symbol-name default)))))
630 (defun rcirc-keepalive ()
631 "Send keep alive pings to active rcirc processes.
632 Kill processes that have not received a server message since the
633 last ping."
634 (if (rcirc-process-list)
635 (mapc (lambda (process)
636 (with-rcirc-process-buffer process
637 (when (not rcirc-connecting)
638 (rcirc-send-ctcp process
639 rcirc-nick
640 (format "KEEPALIVE %f"
641 (rcirc-float-time))))))
642 (rcirc-process-list))
643 ;; no processes, clean up timer
644 (when (timerp rcirc-keepalive-timer)
645 (cancel-timer rcirc-keepalive-timer))
646 (setq rcirc-keepalive-timer nil)))
648 (defun rcirc-handler-ctcp-KEEPALIVE (process _target _sender message)
649 (with-rcirc-process-buffer process
650 (setq header-line-format (format "%f" (- (rcirc-float-time)
651 (string-to-number message))))))
653 (defvar rcirc-debug-buffer "*rcirc debug*")
654 (defvar rcirc-debug-flag nil
655 "If non-nil, write information to `rcirc-debug-buffer'.")
656 (defun rcirc-debug (process text)
657 "Add an entry to the debug log including PROCESS and TEXT.
658 Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag'
659 is non-nil."
660 (when rcirc-debug-flag
661 (with-current-buffer (get-buffer-create rcirc-debug-buffer)
662 (goto-char (point-max))
663 (insert (concat
664 "["
665 (format-time-string "%Y-%m-%dT%T ") (process-name process)
666 "] "
667 text)))))
669 (define-obsolete-variable-alias 'rcirc-sentinel-hooks
670 'rcirc-sentinel-functions "24.3")
671 (defvar rcirc-sentinel-functions nil
672 "Hook functions called when the process sentinel is called.
673 Functions are called with PROCESS and SENTINEL arguments.")
675 (defcustom rcirc-reconnect-delay 0
676 "The minimum interval in seconds between reconnect attempts.
677 When 0, do not auto-reconnect."
678 :version "25.1"
679 :type 'integer
680 :group 'rcirc)
682 (defvar rcirc-last-connect-time nil
683 "The last time the buffer was connected.")
685 (defun rcirc-sentinel (process sentinel)
686 "Called when PROCESS receives SENTINEL."
687 (let ((sentinel (replace-regexp-in-string "\n" "" sentinel)))
688 (rcirc-debug process (format "SENTINEL: %S %S\n" process sentinel))
689 (with-rcirc-process-buffer process
690 (dolist (buffer (cons nil (mapcar 'cdr rcirc-buffer-alist)))
691 (with-current-buffer (or buffer (current-buffer))
692 (rcirc-print process "rcirc.el" "ERROR" rcirc-target
693 (format "%s: %s (%S)"
694 (process-name process)
695 sentinel
696 (process-status process))
697 (not rcirc-target))
698 (rcirc-disconnect-buffer)))
699 (when (and (string= sentinel "deleted")
700 (< 0 rcirc-reconnect-delay))
701 (let ((now (current-time)))
702 (when (or (null rcirc-last-connect-time)
703 (< rcirc-reconnect-delay
704 (float-time (time-subtract now rcirc-last-connect-time))))
705 (setq rcirc-last-connect-time now)
706 (rcirc-cmd-reconnect nil))))
707 (run-hook-with-args 'rcirc-sentinel-functions process sentinel))))
709 (defun rcirc-disconnect-buffer (&optional buffer)
710 (with-current-buffer (or buffer (current-buffer))
711 ;; set rcirc-target to nil for each channel so cleanup
712 ;; doesn't happen when we reconnect
713 (setq rcirc-target nil)
714 (setq mode-line-process ":disconnected")))
716 (defun rcirc-process-list ()
717 "Return a list of rcirc processes."
718 (let (ps)
719 (mapc (lambda (p)
720 (when (buffer-live-p (process-buffer p))
721 (with-rcirc-process-buffer p
722 (when (eq major-mode 'rcirc-mode)
723 (setq ps (cons p ps))))))
724 (process-list))
725 ps))
727 (define-obsolete-variable-alias 'rcirc-receive-message-hooks
728 'rcirc-receive-message-functions "24.3")
729 (defvar rcirc-receive-message-functions nil
730 "Hook functions run when a message is received from server.
731 Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.")
732 (defun rcirc-filter (process output)
733 "Called when PROCESS receives OUTPUT."
734 (rcirc-debug process output)
735 (rcirc-reschedule-timeout process)
736 (with-rcirc-process-buffer process
737 (setq rcirc-last-server-message-time (current-time))
738 (setq rcirc-process-output (concat rcirc-process-output output))
739 (when (= (aref rcirc-process-output
740 (1- (length rcirc-process-output))) ?\n)
741 (mapc (lambda (line)
742 (rcirc-process-server-response process line))
743 (split-string rcirc-process-output "[\n\r]" t))
744 (setq rcirc-process-output nil))))
746 (defun rcirc-reschedule-timeout (process)
747 (with-rcirc-process-buffer process
748 (when (not rcirc-connecting)
749 (with-rcirc-process-buffer process
750 (when rcirc-timeout-timer (cancel-timer rcirc-timeout-timer))
751 (setq rcirc-timeout-timer (run-at-time rcirc-timeout-seconds nil
752 'rcirc-delete-process
753 process))))))
755 (defun rcirc-delete-process (process)
756 (delete-process process))
758 (defvar rcirc-trap-errors-flag t)
759 (defun rcirc-process-server-response (process text)
760 (if rcirc-trap-errors-flag
761 (condition-case err
762 (rcirc-process-server-response-1 process text)
763 (error
764 (rcirc-print process "RCIRC" "ERROR" nil
765 (format "\"%s\" %s" text err) t)))
766 (rcirc-process-server-response-1 process text)))
768 (defun rcirc-process-server-response-1 (process text)
769 (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text)
770 (let* ((user (match-string 2 text))
771 (sender (rcirc-user-nick user))
772 (cmd (match-string 3 text))
773 (args (match-string 4 text))
774 (handler (intern-soft (concat "rcirc-handler-" cmd))))
775 (string-match "^\\([^:]*\\):?\\(.+\\)?$" args)
776 (let* ((args1 (match-string 1 args))
777 (args2 (match-string 2 args))
778 (args (delq nil (append (split-string args1 " " t)
779 (list args2)))))
780 (if (not (fboundp handler))
781 (rcirc-handler-generic process cmd sender args text)
782 (funcall handler process sender args text))
783 (run-hook-with-args 'rcirc-receive-message-functions
784 process cmd sender args text)))
785 (message "UNHANDLED: %s" text)))
787 (defvar rcirc-responses-no-activity '("305" "306")
788 "Responses that don't trigger activity in the mode-line indicator.")
790 (defun rcirc-handler-generic (process response sender args _text)
791 "Generic server response handler."
792 (rcirc-print process sender response nil
793 (mapconcat 'identity (cdr args) " ")
794 (not (member response rcirc-responses-no-activity))))
796 (defun rcirc--connection-open-p (process)
797 (memq (process-status process) '(run open)))
799 (defun rcirc-send-string (process string)
800 "Send PROCESS a STRING plus a newline."
801 (let ((string (concat (encode-coding-string string rcirc-encode-coding-system)
802 "\n")))
803 (unless (rcirc--connection-open-p process)
804 (error "Network connection to %s is not open"
805 (process-name process)))
806 (rcirc-debug process string)
807 (process-send-string process string)))
809 (defun rcirc-send-privmsg (process target string)
810 (rcirc-send-string process (format "PRIVMSG %s :%s" target string)))
812 (defun rcirc-send-ctcp (process target request &optional args)
813 (let ((args (if args (concat " " args) "")))
814 (rcirc-send-privmsg process target
815 (format "\C-a%s%s\C-a" request args))))
817 (defun rcirc-buffer-process (&optional buffer)
818 "Return the process associated with channel BUFFER.
819 With no argument or nil as argument, use the current buffer."
820 (let ((buffer (or buffer (and (buffer-live-p rcirc-server-buffer)
821 rcirc-server-buffer))))
822 (if buffer
823 (with-current-buffer buffer rcirc-process)
824 rcirc-process)))
826 (defun rcirc-server-name (process)
827 "Return PROCESS server name, given by the 001 response."
828 (with-rcirc-process-buffer process
829 (or rcirc-server-name
830 (warn "server name for process %S unknown" process))))
832 (defun rcirc-nick (process)
833 "Return PROCESS nick."
834 (with-rcirc-process-buffer process
835 (or rcirc-nick rcirc-default-nick)))
837 (defun rcirc-buffer-nick (&optional buffer)
838 "Return the nick associated with BUFFER.
839 With no argument or nil as argument, use the current buffer."
840 (with-current-buffer (or buffer (current-buffer))
841 (with-current-buffer rcirc-server-buffer
842 (or rcirc-nick rcirc-default-nick))))
844 (defvar rcirc-max-message-length 420
845 "Messages longer than this value will be split.")
847 (defun rcirc-split-message (message)
848 "Split MESSAGE into chunks within `rcirc-max-message-length'."
849 ;; `rcirc-encode-coding-system' can have buffer-local value.
850 (let ((encoding rcirc-encode-coding-system))
851 (with-temp-buffer
852 (insert message)
853 (goto-char (point-min))
854 (let (result)
855 (while (not (eobp))
856 (goto-char (or (byte-to-position rcirc-max-message-length)
857 (point-max)))
858 ;; max message length is 512 including CRLF
859 (while (and (not (bobp))
860 (> (length (encode-coding-region
861 (point-min) (point) encoding t))
862 rcirc-max-message-length))
863 (forward-char -1))
864 (push (delete-and-extract-region (point-min) (point)) result))
865 (nreverse result)))))
867 (defun rcirc-send-message (process target message &optional noticep silent)
868 "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
869 If NOTICEP is non-nil, send a notice instead of privmsg.
870 If SILENT is non-nil, do not print the message in any irc buffer."
871 (let ((response (if noticep "NOTICE" "PRIVMSG")))
872 (rcirc-get-buffer-create process target)
873 (dolist (msg (rcirc-split-message message))
874 (rcirc-send-string process (concat response " " target " :" msg))
875 (unless silent
876 (rcirc-print process (rcirc-nick process) response target msg)))))
878 (defvar rcirc-input-ring nil)
879 (defvar rcirc-input-ring-index 0)
881 (defun rcirc-prev-input-string (arg)
882 (ring-ref rcirc-input-ring (+ rcirc-input-ring-index arg)))
884 (defun rcirc-insert-prev-input ()
885 (interactive)
886 (when (<= rcirc-prompt-end-marker (point))
887 (delete-region rcirc-prompt-end-marker (point-max))
888 (insert (rcirc-prev-input-string 0))
889 (setq rcirc-input-ring-index (1+ rcirc-input-ring-index))))
891 (defun rcirc-insert-next-input ()
892 (interactive)
893 (when (<= rcirc-prompt-end-marker (point))
894 (delete-region rcirc-prompt-end-marker (point-max))
895 (setq rcirc-input-ring-index (1- rcirc-input-ring-index))
896 (insert (rcirc-prev-input-string -1))))
898 (defvar rcirc-server-commands
899 '("/admin" "/away" "/connect" "/die" "/error" "/info"
900 "/invite" "/ison" "/join" "/kick" "/kill" "/links"
901 "/list" "/lusers" "/mode" "/motd" "/names" "/nick"
902 "/notice" "/oper" "/part" "/pass" "/ping" "/pong"
903 "/privmsg" "/quit" "/rehash" "/restart" "/service" "/servlist"
904 "/server" "/squery" "/squit" "/stats" "/summon" "/time"
905 "/topic" "/trace" "/user" "/userhost" "/users" "/version"
906 "/wallops" "/who" "/whois" "/whowas")
907 "A list of user commands by IRC server.
908 The value defaults to RFCs 1459 and 2812.")
910 ;; /me and /ctcp are not defined by `defun-rcirc-command'.
911 (defvar rcirc-client-commands '("/me" "/ctcp")
912 "A list of user commands defined by IRC client rcirc.
913 The list is updated automatically by `defun-rcirc-command'.")
915 (defun rcirc-completion-at-point ()
916 "Function used for `completion-at-point-functions' in `rcirc-mode'."
917 (and (rcirc-looking-at-input)
918 (let* ((beg (save-excursion
919 ;; On some networks it is common to message or
920 ;; mention someone using @nick instead of just
921 ;; nick.
922 (if (re-search-backward "[[:space:]@]" rcirc-prompt-end-marker t)
923 (1+ (point))
924 rcirc-prompt-end-marker)))
925 (table (if (and (= beg rcirc-prompt-end-marker)
926 (eq (char-after beg) ?/))
927 (delete-dups
928 (nconc (sort (copy-sequence rcirc-client-commands)
929 'string-lessp)
930 (sort (copy-sequence rcirc-server-commands)
931 'string-lessp)))
932 (rcirc-channel-nicks (rcirc-buffer-process)
933 rcirc-target))))
934 (list beg (point) table))))
936 (defvar rcirc-completions nil)
937 (defvar rcirc-completion-start nil)
939 (defun rcirc-complete ()
940 "Cycle through completions from list of nicks in channel or IRC commands.
941 IRC command completion is performed only if `/' is the first input char."
942 (interactive)
943 (unless (rcirc-looking-at-input)
944 (error "Point not located after rcirc prompt"))
945 (if (eq last-command this-command)
946 (setq rcirc-completions
947 (append (cdr rcirc-completions) (list (car rcirc-completions))))
948 (let ((completion-ignore-case t)
949 (table (rcirc-completion-at-point)))
950 (setq rcirc-completion-start (car table))
951 (setq rcirc-completions
952 (and rcirc-completion-start
953 (all-completions (buffer-substring rcirc-completion-start
954 (cadr table))
955 (nth 2 table))))))
956 (let ((completion (car rcirc-completions)))
957 (when completion
958 (delete-region rcirc-completion-start (point))
959 (insert
960 (cond
961 ((= (aref completion 0) ?/) (concat completion " "))
962 ((= rcirc-completion-start rcirc-prompt-end-marker)
963 (format rcirc-nick-completion-format completion))
964 (t completion))))))
966 (defun set-rcirc-decode-coding-system (coding-system)
967 "Set the decode coding system used in this channel."
968 (interactive "zCoding system for incoming messages: ")
969 (setq-local rcirc-decode-coding-system coding-system))
971 (defun set-rcirc-encode-coding-system (coding-system)
972 "Set the encode coding system used in this channel."
973 (interactive "zCoding system for outgoing messages: ")
974 (setq-local rcirc-encode-coding-system coding-system))
976 (defvar rcirc-mode-map
977 (let ((map (make-sparse-keymap)))
978 (define-key map (kbd "RET") 'rcirc-send-input)
979 (define-key map (kbd "M-p") 'rcirc-insert-prev-input)
980 (define-key map (kbd "M-n") 'rcirc-insert-next-input)
981 (define-key map (kbd "TAB") 'rcirc-complete)
982 (define-key map (kbd "C-c C-b") 'rcirc-browse-url)
983 (define-key map (kbd "C-c C-c") 'rcirc-edit-multiline)
984 (define-key map (kbd "C-c C-j") 'rcirc-cmd-join)
985 (define-key map (kbd "C-c C-k") 'rcirc-cmd-kick)
986 (define-key map (kbd "C-c C-l") 'rcirc-toggle-low-priority)
987 (define-key map (kbd "C-c C-d") 'rcirc-cmd-mode)
988 (define-key map (kbd "C-c C-m") 'rcirc-cmd-msg)
989 (define-key map (kbd "C-c C-r") 'rcirc-cmd-nick) ; rename
990 (define-key map (kbd "C-c C-o") 'rcirc-omit-mode)
991 (define-key map (kbd "C-c C-p") 'rcirc-cmd-part)
992 (define-key map (kbd "C-c C-q") 'rcirc-cmd-query)
993 (define-key map (kbd "C-c C-t") 'rcirc-cmd-topic)
994 (define-key map (kbd "C-c C-n") 'rcirc-cmd-names)
995 (define-key map (kbd "C-c C-w") 'rcirc-cmd-whois)
996 (define-key map (kbd "C-c C-x") 'rcirc-cmd-quit)
997 (define-key map (kbd "C-c TAB") ; C-i
998 'rcirc-toggle-ignore-buffer-activity)
999 (define-key map (kbd "C-c C-s") 'rcirc-switch-to-server-buffer)
1000 (define-key map (kbd "C-c C-a") 'rcirc-jump-to-first-unread-line)
1001 map)
1002 "Keymap for rcirc mode.")
1004 (defvar rcirc-short-buffer-name nil
1005 "Generated abbreviation to use to indicate buffer activity.")
1007 (defvar rcirc-mode-hook nil
1008 "Hook run when setting up rcirc buffer.")
1010 (defvar rcirc-last-post-time nil)
1012 (defvar rcirc-log-alist nil
1013 "Alist of lines to log to disk when `rcirc-log-flag' is non-nil.
1014 Each element looks like (FILENAME . TEXT).")
1016 (defvar rcirc-current-line 0
1017 "The current number of responses printed in this channel.
1018 This number is independent of the number of lines in the buffer.")
1020 (defun rcirc-mode (process target)
1021 ;; FIXME: Use define-derived-mode.
1022 "Major mode for IRC channel buffers.
1024 \\{rcirc-mode-map}"
1025 (kill-all-local-variables)
1026 (use-local-map rcirc-mode-map)
1027 (setq mode-name "rcirc")
1028 (setq major-mode 'rcirc-mode)
1029 (setq mode-line-process nil)
1031 (setq-local rcirc-input-ring
1032 ;; If rcirc-input-ring is already a ring with desired
1033 ;; size do not re-initialize.
1034 (if (and (ring-p rcirc-input-ring)
1035 (= (ring-size rcirc-input-ring)
1036 rcirc-input-ring-size))
1037 rcirc-input-ring
1038 (make-ring rcirc-input-ring-size)))
1039 (setq-local rcirc-server-buffer (process-buffer process))
1040 (setq-local rcirc-target target)
1041 (setq-local rcirc-topic nil)
1042 (setq-local rcirc-last-post-time (current-time))
1043 (setq-local fill-paragraph-function 'rcirc-fill-paragraph)
1044 (setq-local rcirc-recent-quit-alist nil)
1045 (setq-local rcirc-current-line 0)
1046 (setq-local rcirc-last-connect-time (current-time))
1048 (use-hard-newlines t)
1049 (setq-local rcirc-short-buffer-name nil)
1050 (setq-local rcirc-urls nil)
1052 ;; setup for omitting responses
1053 (setq buffer-invisibility-spec '())
1054 (setq buffer-display-table (make-display-table))
1055 (set-display-table-slot buffer-display-table 4
1056 (let ((glyph (make-glyph-code
1057 ?. 'font-lock-keyword-face)))
1058 (make-vector 3 glyph)))
1060 (dolist (i rcirc-coding-system-alist)
1061 (let ((chan (if (consp (car i)) (caar i) (car i)))
1062 (serv (if (consp (car i)) (cdar i) "")))
1063 (when (and (string-match chan (or target ""))
1064 (string-match serv (rcirc-server-name process)))
1065 (setq-local rcirc-decode-coding-system
1066 (if (consp (cdr i)) (cadr i) (cdr i)))
1067 (setq-local rcirc-encode-coding-system
1068 (if (consp (cdr i)) (cddr i) (cdr i))))))
1070 ;; setup the prompt and markers
1071 (setq-local rcirc-prompt-start-marker (point-max-marker))
1072 (setq-local rcirc-prompt-end-marker (point-max-marker))
1073 (rcirc-update-prompt)
1074 (goto-char rcirc-prompt-end-marker)
1076 (setq-local overlay-arrow-position (make-marker))
1078 ;; if the user changes the major mode or kills the buffer, there is
1079 ;; cleanup work to do
1080 (add-hook 'change-major-mode-hook 'rcirc-change-major-mode-hook nil t)
1081 (add-hook 'kill-buffer-hook 'rcirc-kill-buffer-hook nil t)
1083 ;; add to buffer list, and update buffer abbrevs
1084 (when target ; skip server buffer
1085 (let ((buffer (current-buffer)))
1086 (with-rcirc-process-buffer process
1087 (setq rcirc-buffer-alist (cons (cons target buffer)
1088 rcirc-buffer-alist))))
1089 (rcirc-update-short-buffer-names))
1091 (add-hook 'completion-at-point-functions
1092 'rcirc-completion-at-point nil 'local)
1094 (run-mode-hooks 'rcirc-mode-hook))
1096 (defun rcirc-update-prompt (&optional all)
1097 "Reset the prompt string in the current buffer.
1099 If ALL is non-nil, update prompts in all IRC buffers."
1100 (if all
1101 (mapc (lambda (process)
1102 (mapc (lambda (buffer)
1103 (with-current-buffer buffer
1104 (rcirc-update-prompt)))
1105 (with-rcirc-process-buffer process
1106 (mapcar 'cdr rcirc-buffer-alist))))
1107 (rcirc-process-list))
1108 (let ((inhibit-read-only t)
1109 (prompt (or rcirc-prompt "")))
1110 (mapc (lambda (rep)
1111 (setq prompt
1112 (replace-regexp-in-string (car rep) (cdr rep) prompt)))
1113 (list (cons "%n" (rcirc-buffer-nick))
1114 (cons "%s" (with-rcirc-server-buffer rcirc-server-name))
1115 (cons "%t" (or rcirc-target ""))))
1116 (save-excursion
1117 (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker)
1118 (goto-char rcirc-prompt-start-marker)
1119 (let ((start (point)))
1120 (insert-before-markers prompt)
1121 (set-marker rcirc-prompt-start-marker start)
1122 (when (not (zerop (- rcirc-prompt-end-marker
1123 rcirc-prompt-start-marker)))
1124 (add-text-properties rcirc-prompt-start-marker
1125 rcirc-prompt-end-marker
1126 (list 'face 'rcirc-prompt
1127 'read-only t 'field t
1128 'front-sticky t 'rear-nonsticky t))))))))
1130 (defun rcirc-set-changed (option value)
1131 "Set OPTION to VALUE and do updates after a customization change."
1132 (set-default option value)
1133 (cond ((eq option 'rcirc-prompt)
1134 (rcirc-update-prompt 'all))
1135 (t
1136 (error "Bad option %s" option))))
1138 (defun rcirc-channel-p (target)
1139 "Return t if TARGET is a channel name."
1140 (and target
1141 (not (zerop (length target)))
1142 (or (eq (aref target 0) ?#)
1143 (eq (aref target 0) ?&))))
1145 (defcustom rcirc-log-directory "~/.emacs.d/rcirc-log"
1146 "Directory to keep IRC logfiles."
1147 :type 'directory
1148 :group 'rcirc)
1150 (defcustom rcirc-log-flag nil
1151 "Non-nil means log IRC activity to disk.
1152 Logfiles are kept in `rcirc-log-directory'."
1153 :type 'boolean
1154 :group 'rcirc)
1156 (defun rcirc-kill-buffer-hook ()
1157 "Part the channel when killing an rcirc buffer.
1159 If `rcirc-kill-channel-buffers' is non-nil and the killed buffer
1160 is a server buffer, kills all of the channel buffers associated
1161 with it."
1162 (when (eq major-mode 'rcirc-mode)
1163 (when (and rcirc-log-flag
1164 rcirc-log-directory)
1165 (rcirc-log-write))
1166 (rcirc-clean-up-buffer "Killed buffer")
1167 (when (and rcirc-buffer-alist ;; it's a server buffer
1168 rcirc-kill-channel-buffers)
1169 (dolist (channel rcirc-buffer-alist)
1170 (kill-buffer (cdr channel))))))
1172 (defun rcirc-change-major-mode-hook ()
1173 "Part the channel when changing the major-mode."
1174 (rcirc-clean-up-buffer "Changed major mode"))
1176 (defun rcirc-clean-up-buffer (reason)
1177 (let ((buffer (current-buffer)))
1178 (rcirc-clear-activity buffer)
1179 (when (and (rcirc-buffer-process)
1180 (rcirc--connection-open-p (rcirc-buffer-process)))
1181 (with-rcirc-server-buffer
1182 (setq rcirc-buffer-alist
1183 (rassq-delete-all buffer rcirc-buffer-alist)))
1184 (rcirc-update-short-buffer-names)
1185 (if (rcirc-channel-p rcirc-target)
1186 (rcirc-send-string (rcirc-buffer-process)
1187 (concat "PART " rcirc-target " :" reason))
1188 (when rcirc-target
1189 (rcirc-remove-nick-channel (rcirc-buffer-process)
1190 (rcirc-buffer-nick)
1191 rcirc-target))))
1192 (setq rcirc-target nil)))
1194 (defun rcirc-generate-new-buffer-name (process target)
1195 "Return a buffer name based on PROCESS and TARGET.
1196 This is used for the initial name given to IRC buffers."
1197 (substring-no-properties
1198 (if target
1199 (concat target "@" (process-name process))
1200 (concat "*" (process-name process) "*"))))
1202 (defun rcirc-get-buffer (process target &optional server)
1203 "Return the buffer associated with the PROCESS and TARGET.
1205 If optional argument SERVER is non-nil, return the server buffer
1206 if there is no existing buffer for TARGET, otherwise return nil."
1207 (with-rcirc-process-buffer process
1208 (if (null target)
1209 (current-buffer)
1210 (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t))))
1211 (or buffer (when server (current-buffer)))))))
1213 (defun rcirc-get-buffer-create (process target)
1214 "Return the buffer associated with the PROCESS and TARGET.
1215 Create the buffer if it doesn't exist."
1216 (let ((buffer (rcirc-get-buffer process target)))
1217 (if (and buffer (buffer-live-p buffer))
1218 (with-current-buffer buffer
1219 (when (not rcirc-target)
1220 (setq rcirc-target target))
1221 buffer)
1222 ;; create the buffer
1223 (with-rcirc-process-buffer process
1224 (let ((new-buffer (get-buffer-create
1225 (rcirc-generate-new-buffer-name process target))))
1226 (with-current-buffer new-buffer
1227 (rcirc-mode process target)
1228 (rcirc-put-nick-channel process (rcirc-nick process) target
1229 rcirc-current-line))
1230 new-buffer)))))
1232 (defun rcirc-send-input ()
1233 "Send input to target associated with the current buffer."
1234 (interactive)
1235 (if (< (point) rcirc-prompt-end-marker)
1236 ;; copy the line down to the input area
1237 (progn
1238 (forward-line 0)
1239 (let ((start (if (eq (point) (point-min))
1240 (point)
1241 (if (get-text-property (1- (point)) 'hard)
1242 (point)
1243 (previous-single-property-change (point) 'hard))))
1244 (end (next-single-property-change (1+ (point)) 'hard)))
1245 (goto-char (point-max))
1246 (insert (replace-regexp-in-string
1247 "\n\\s-+" " "
1248 (buffer-substring-no-properties start end)))))
1249 ;; process input
1250 (goto-char (point-max))
1251 (when (not (equal 0 (- (point) rcirc-prompt-end-marker)))
1252 ;; delete a trailing newline
1253 (when (eq (point) (point-at-bol))
1254 (delete-char -1))
1255 (let ((input (buffer-substring-no-properties
1256 rcirc-prompt-end-marker (point))))
1257 (dolist (line (split-string input "\n"))
1258 (rcirc-process-input-line line))
1259 ;; add to input-ring
1260 (save-excursion
1261 (ring-insert rcirc-input-ring input)
1262 (setq rcirc-input-ring-index 0))))))
1264 (defun rcirc-fill-paragraph (&optional justify)
1265 (interactive "P")
1266 (when (> (point) rcirc-prompt-end-marker)
1267 (save-restriction
1268 (narrow-to-region rcirc-prompt-end-marker (point-max))
1269 (let ((fill-column rcirc-max-message-length))
1270 (fill-region (point-min) (point-max) justify)))))
1272 (defun rcirc-process-input-line (line)
1273 (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line)
1274 (rcirc-process-command (match-string 1 line)
1275 (match-string 2 line)
1276 line)
1277 (rcirc-process-message line)))
1279 (defun rcirc-process-message (line)
1280 (if (not rcirc-target)
1281 (message "Not joined (no target)")
1282 (delete-region rcirc-prompt-end-marker (point))
1283 (rcirc-send-message (rcirc-buffer-process) rcirc-target line)
1284 (setq rcirc-last-post-time (current-time))))
1286 (defun rcirc-process-command (command args line)
1287 (if (eq (aref command 0) ?/)
1288 ;; "//text" will send "/text" as a message
1289 (rcirc-process-message (substring line 1))
1290 (let ((fun (intern-soft (concat "rcirc-cmd-" command)))
1291 (process (rcirc-buffer-process)))
1292 (newline)
1293 (with-current-buffer (current-buffer)
1294 (delete-region rcirc-prompt-end-marker (point))
1295 (if (string= command "me")
1296 (rcirc-print process (rcirc-buffer-nick)
1297 "ACTION" rcirc-target args)
1298 (rcirc-print process (rcirc-buffer-nick)
1299 "COMMAND" rcirc-target line))
1300 (set-marker rcirc-prompt-end-marker (point))
1301 (if (fboundp fun)
1302 (funcall fun args process rcirc-target)
1303 (rcirc-send-string process
1304 (concat command " :" args)))))))
1306 (defvar rcirc-parent-buffer nil)
1307 (make-variable-buffer-local 'rcirc-parent-buffer)
1308 (put 'rcirc-parent-buffer 'permanent-local t)
1309 (defvar rcirc-window-configuration nil)
1310 (defun rcirc-edit-multiline ()
1311 "Move current edit to a dedicated buffer."
1312 (interactive)
1313 (let ((pos (1+ (- (point) rcirc-prompt-end-marker))))
1314 (goto-char (point-max))
1315 (let ((text (buffer-substring-no-properties rcirc-prompt-end-marker
1316 (point)))
1317 (parent (buffer-name)))
1318 (delete-region rcirc-prompt-end-marker (point))
1319 (setq rcirc-window-configuration (current-window-configuration))
1320 (pop-to-buffer (concat "*multiline " parent "*"))
1321 (funcall rcirc-multiline-major-mode)
1322 (rcirc-multiline-minor-mode 1)
1323 (setq rcirc-parent-buffer parent)
1324 (insert text)
1325 (and (> pos 0) (goto-char pos))
1326 (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent))))
1328 (defvar rcirc-multiline-minor-mode-map
1329 (let ((map (make-sparse-keymap)))
1330 (define-key map (kbd "C-c C-c") 'rcirc-multiline-minor-submit)
1331 (define-key map (kbd "C-x C-s") 'rcirc-multiline-minor-submit)
1332 (define-key map (kbd "C-c C-k") 'rcirc-multiline-minor-cancel)
1333 (define-key map (kbd "ESC ESC ESC") 'rcirc-multiline-minor-cancel)
1334 map)
1335 "Keymap for multiline mode in rcirc.")
1337 (define-minor-mode rcirc-multiline-minor-mode
1338 "Minor mode for editing multiple lines in rcirc.
1339 With a prefix argument ARG, enable the mode if ARG is positive,
1340 and disable it otherwise. If called from Lisp, enable the mode
1341 if ARG is omitted or nil."
1342 :init-value nil
1343 :lighter " rcirc-mline"
1344 :keymap rcirc-multiline-minor-mode-map
1345 :global nil
1346 :group 'rcirc
1347 (setq fill-column rcirc-max-message-length))
1349 (defun rcirc-multiline-minor-submit ()
1350 "Send the text in buffer back to parent buffer."
1351 (interactive)
1352 (untabify (point-min) (point-max))
1353 (let ((text (buffer-substring (point-min) (point-max)))
1354 (buffer (current-buffer))
1355 (pos (point)))
1356 (set-buffer rcirc-parent-buffer)
1357 (goto-char (point-max))
1358 (insert text)
1359 (kill-buffer buffer)
1360 (set-window-configuration rcirc-window-configuration)
1361 (goto-char (+ rcirc-prompt-end-marker (1- pos)))))
1363 (defun rcirc-multiline-minor-cancel ()
1364 "Cancel the multiline edit."
1365 (interactive)
1366 (kill-buffer (current-buffer))
1367 (set-window-configuration rcirc-window-configuration))
1369 (defun rcirc-any-buffer (process)
1370 "Return a buffer for PROCESS, either the one selected or the process buffer."
1371 (if rcirc-always-use-server-buffer-flag
1372 (process-buffer process)
1373 (let ((buffer (window-buffer)))
1374 (if (and buffer
1375 (with-current-buffer buffer
1376 (and (eq major-mode 'rcirc-mode)
1377 (eq (rcirc-buffer-process) process))))
1378 buffer
1379 (process-buffer process)))))
1381 (defcustom rcirc-response-formats
1382 '(("PRIVMSG" . "<%N> %m")
1383 ("NOTICE" . "-%N- %m")
1384 ("ACTION" . "[%N %m]")
1385 ("COMMAND" . "%m")
1386 ("ERROR" . "%fw!!! %m")
1387 (t . "%fp*** %fs%n %r %m"))
1388 "An alist of formats used for printing responses.
1389 The format is looked up using the response-type as a key;
1390 if no match is found, the default entry (with a key of t) is used.
1392 The entry's value part should be a string, which is inserted with
1393 the of the following escape sequences replaced by the described values:
1395 %m The message text
1396 %n The sender's nick
1397 %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick')
1398 %r The response-type
1399 %t The target
1400 %fw Following text uses the face `font-lock-warning-face'
1401 %fp Following text uses the face `rcirc-server-prefix'
1402 %fs Following text uses the face `rcirc-server'
1403 %f[FACE] Following text uses the face FACE
1404 %f- Following text uses the default face
1405 %% A literal `%' character"
1406 :type '(alist :key-type (choice (string :tag "Type")
1407 (const :tag "Default" t))
1408 :value-type string)
1409 :group 'rcirc)
1411 (defcustom rcirc-omit-responses
1412 '("JOIN" "PART" "QUIT" "NICK")
1413 "Responses which will be hidden when `rcirc-omit-mode' is enabled."
1414 :type '(repeat string)
1415 :group 'rcirc)
1417 (defun rcirc-format-response-string (process sender response target text)
1418 "Return a nicely-formatted response string, incorporating TEXT
1419 \(and perhaps other arguments). The specific formatting used
1420 is found by looking up RESPONSE in `rcirc-response-formats'."
1421 (with-temp-buffer
1422 (insert (or (cdr (assoc response rcirc-response-formats))
1423 (cdr (assq t rcirc-response-formats))))
1424 (goto-char (point-min))
1425 (let ((start (point-min))
1426 (sender (if (or (not sender)
1427 (string= (rcirc-server-name process) sender))
1428 ""
1429 sender))
1430 face)
1431 (while (re-search-forward "%\\(\\(f\\(.\\)\\)\\|\\(.\\)\\)" nil t)
1432 (rcirc-add-face start (match-beginning 0) face)
1433 (setq start (match-beginning 0))
1434 (replace-match
1435 (cl-case (aref (match-string 1) 0)
1436 (?f (setq face
1437 (cl-case (string-to-char (match-string 3))
1438 (?w 'font-lock-warning-face)
1439 (?p 'rcirc-server-prefix)
1440 (?s 'rcirc-server)
1441 (t nil)))
1442 "")
1443 (?n sender)
1444 (?N (let ((my-nick (rcirc-nick process)))
1445 (save-match-data
1446 (with-syntax-table rcirc-nick-syntax-table
1447 (rcirc-facify sender
1448 (cond ((string= sender my-nick)
1449 'rcirc-my-nick)
1450 ((and rcirc-bright-nicks
1451 (string-match
1452 (regexp-opt rcirc-bright-nicks
1453 'words)
1454 sender))
1455 'rcirc-bright-nick)
1456 ((and rcirc-dim-nicks
1457 (string-match
1458 (regexp-opt rcirc-dim-nicks
1459 'words)
1460 sender))
1461 'rcirc-dim-nick)
1462 (t
1463 'rcirc-other-nick)))))))
1464 (?m (propertize text 'rcirc-text text))
1465 (?r response)
1466 (?t (or target ""))
1467 (t (concat "UNKNOWN CODE:" (match-string 0))))
1468 t t nil 0)
1469 (rcirc-add-face (match-beginning 0) (match-end 0) face))
1470 (rcirc-add-face start (match-beginning 0) face))
1471 (buffer-substring (point-min) (point-max))))
1473 (defun rcirc-target-buffer (process sender response target _text)
1474 "Return a buffer to print the server response."
1475 (cl-assert (not (bufferp target)))
1476 (with-rcirc-process-buffer process
1477 (cond ((not target)
1478 (rcirc-any-buffer process))
1479 ((not (rcirc-channel-p target))
1480 ;; message from another user
1481 (if (or (string= response "PRIVMSG")
1482 (string= response "ACTION"))
1483 (rcirc-get-buffer-create process (if (string= sender rcirc-nick)
1484 target
1485 sender))
1486 (rcirc-get-buffer process target t)))
1487 ((or (rcirc-get-buffer process target)
1488 (rcirc-any-buffer process))))))
1490 (defvar rcirc-activity-types nil)
1491 (make-variable-buffer-local 'rcirc-activity-types)
1492 (defvar rcirc-last-sender nil)
1493 (make-variable-buffer-local 'rcirc-last-sender)
1495 (defcustom rcirc-omit-threshold 100
1496 "Number of lines since last activity from a nick before `rcirc-omit-responses' are omitted."
1497 :type 'integer
1498 :group 'rcirc)
1500 (defcustom rcirc-log-process-buffers nil
1501 "Non-nil if rcirc process buffers should be logged to disk."
1502 :group 'rcirc
1503 :type 'boolean
1504 :version "24.1")
1506 (defun rcirc-last-quit-line (process nick target)
1507 "Return the line number where NICK left TARGET.
1508 Returns nil if the information is not recorded."
1509 (let ((chanbuf (rcirc-get-buffer process target)))
1510 (when chanbuf
1511 (cdr (assoc-string nick (with-current-buffer chanbuf
1512 rcirc-recent-quit-alist))))))
1514 (defun rcirc-last-line (process nick target)
1515 "Return the line from the last activity from NICK in TARGET."
1516 (let ((line (or (cdr (assoc-string target
1517 (gethash nick (with-rcirc-server-buffer
1518 rcirc-nick-table)) t))
1519 (rcirc-last-quit-line process nick target))))
1520 (if line
1521 line
1522 ;;(message "line is nil for %s in %s" nick target)
1523 nil)))
1525 (defun rcirc-elapsed-lines (process nick target)
1526 "Return the number of lines since activity from NICK in TARGET."
1527 (let ((last-activity-line (rcirc-last-line process nick target)))
1528 (when (and last-activity-line
1529 (> last-activity-line 0))
1530 (- rcirc-current-line last-activity-line))))
1532 (defvar rcirc-markup-text-functions
1533 '(rcirc-markup-attributes
1534 rcirc-markup-my-nick
1535 rcirc-markup-urls
1536 rcirc-markup-keywords
1537 rcirc-markup-bright-nicks)
1539 "List of functions used to manipulate text before it is printed.
1541 Each function takes two arguments, SENDER, and RESPONSE. The
1542 buffer is narrowed with the text to be printed and the point is
1543 at the beginning of the `rcirc-text' propertized text.")
1545 (defun rcirc-print (process sender response target text &optional activity)
1546 "Print TEXT in the buffer associated with TARGET.
1547 Format based on SENDER and RESPONSE. If ACTIVITY is non-nil,
1548 record activity."
1549 (or text (setq text ""))
1550 (unless (and (or (member sender rcirc-ignore-list)
1551 (member (with-syntax-table rcirc-nick-syntax-table
1552 (when (string-match "^\\([^/]\\w*\\)[:,]" text)
1553 (match-string 1 text)))
1554 rcirc-ignore-list))
1555 ;; do not ignore if we sent the message
1556 (not (string= sender (rcirc-nick process))))
1557 (let* ((buffer (rcirc-target-buffer process sender response target text))
1558 (inhibit-read-only t))
1559 (with-current-buffer buffer
1560 (let ((moving (= (point) rcirc-prompt-end-marker))
1561 (old-point (point-marker))
1562 (fill-start (marker-position rcirc-prompt-start-marker)))
1564 (setq text (decode-coding-string text rcirc-decode-coding-system))
1565 (unless (string= sender (rcirc-nick process))
1566 ;; mark the line with overlay arrow
1567 (unless (or (marker-position overlay-arrow-position)
1568 (get-buffer-window (current-buffer))
1569 (member response rcirc-omit-responses))
1570 (set-marker overlay-arrow-position
1571 (marker-position rcirc-prompt-start-marker))))
1573 ;; temporarily set the marker insertion-type because
1574 ;; insert-before-markers results in hidden text in new buffers
1575 (goto-char rcirc-prompt-start-marker)
1576 (set-marker-insertion-type rcirc-prompt-start-marker t)
1577 (set-marker-insertion-type rcirc-prompt-end-marker t)
1579 (let ((start (point)))
1580 (insert (rcirc-format-response-string process sender response nil
1581 text)
1582 (propertize "\n" 'hard t))
1584 ;; squeeze spaces out of text before rcirc-text
1585 (fill-region fill-start
1586 (1- (or (next-single-property-change fill-start
1587 'rcirc-text)
1588 rcirc-prompt-end-marker)))
1590 ;; run markup functions
1591 (save-excursion
1592 (save-restriction
1593 (narrow-to-region start rcirc-prompt-start-marker)
1594 (goto-char (or (next-single-property-change start 'rcirc-text)
1595 (point)))
1596 (when (rcirc-buffer-process)
1597 (save-excursion (rcirc-markup-timestamp sender response))
1598 (dolist (fn rcirc-markup-text-functions)
1599 (save-excursion (funcall fn sender response)))
1600 (when rcirc-fill-flag
1601 (save-excursion (rcirc-markup-fill sender response))))
1603 (when rcirc-read-only-flag
1604 (add-text-properties (point-min) (point-max)
1605 '(read-only t front-sticky t))))
1606 ;; make text omittable
1607 (let ((last-activity-lines (rcirc-elapsed-lines process sender target)))
1608 (if (and (not (string= (rcirc-nick process) sender))
1609 (member response rcirc-omit-responses)
1610 (or (not last-activity-lines)
1611 (< rcirc-omit-threshold last-activity-lines)))
1612 (put-text-property (1- start) (1- rcirc-prompt-start-marker)
1613 'invisible 'rcirc-omit)
1614 ;; otherwise increment the line count
1615 (setq rcirc-current-line (1+ rcirc-current-line))))))
1617 (set-marker-insertion-type rcirc-prompt-start-marker nil)
1618 (set-marker-insertion-type rcirc-prompt-end-marker nil)
1620 ;; truncate buffer if it is very long
1621 (save-excursion
1622 (when (and rcirc-buffer-maximum-lines
1623 (> rcirc-buffer-maximum-lines 0)
1624 (= (forward-line (- rcirc-buffer-maximum-lines)) 0))
1625 (delete-region (point-min) (point))))
1627 ;; set the window point for buffers show in windows
1628 (walk-windows (lambda (w)
1629 (when (and (not (eq (selected-window) w))
1630 (eq (current-buffer)
1631 (window-buffer w))
1632 (>= (window-point w)
1633 rcirc-prompt-end-marker))
1634 (set-window-point w (point-max))))
1635 nil t)
1637 ;; restore the point
1638 (goto-char (if moving rcirc-prompt-end-marker old-point))
1640 ;; keep window on bottom line if it was already there
1641 (when rcirc-scroll-show-maximum-output
1642 (let ((window (get-buffer-window)))
1643 (when window
1644 (with-selected-window window
1645 (when (eq major-mode 'rcirc-mode)
1646 (when (<= (- (window-height)
1647 (count-screen-lines (window-point)
1648 (window-start))
1649 1)
1650 0)
1651 (recenter -1)))))))
1653 ;; flush undo (can we do something smarter here?)
1654 (buffer-disable-undo)
1655 (buffer-enable-undo))
1657 ;; record mode line activity
1658 (when (and activity
1659 (not rcirc-ignore-buffer-activity-flag)
1660 (not (and rcirc-dim-nicks sender
1661 (string-match (regexp-opt rcirc-dim-nicks) sender)
1662 (rcirc-channel-p target))))
1663 (rcirc-record-activity (current-buffer)
1664 (when (not (rcirc-channel-p rcirc-target))
1665 'nick)))
1667 (when (and rcirc-log-flag
1668 (or target
1669 rcirc-log-process-buffers))
1670 (rcirc-log process sender response target text))
1672 (sit-for 0) ; displayed text before hook
1673 (run-hook-with-args 'rcirc-print-functions
1674 process sender response target text)))))
1676 (defun rcirc-generate-log-filename (process target)
1677 (if target
1678 (rcirc-generate-new-buffer-name process target)
1679 (process-name process)))
1681 (defcustom rcirc-log-filename-function 'rcirc-generate-log-filename
1682 "A function to generate the filename used by rcirc's logging facility.
1684 It is called with two arguments, PROCESS and TARGET (see
1685 `rcirc-generate-new-buffer-name' for their meaning), and should
1686 return the filename, or nil if no logging is desired for this
1687 session.
1689 If the returned filename is absolute (`file-name-absolute-p'
1690 returns t), then it is used as-is, otherwise the resulting file
1691 is put into `rcirc-log-directory'.
1693 The filename is then cleaned using `convert-standard-filename' to
1694 guarantee valid filenames for the current OS."
1695 :group 'rcirc
1696 :type 'function)
1698 (defun rcirc-log (process sender response target text)
1699 "Record line in `rcirc-log', to be later written to disk."
1700 (let ((filename (funcall rcirc-log-filename-function process target)))
1701 (unless (null filename)
1702 (let ((cell (assoc-string filename rcirc-log-alist))
1703 (line (concat (format-time-string rcirc-time-format)
1704 (substring-no-properties
1705 (rcirc-format-response-string process sender
1706 response target text))
1707 "\n")))
1708 (if cell
1709 (setcdr cell (concat (cdr cell) line))
1710 (setq rcirc-log-alist
1711 (cons (cons filename line) rcirc-log-alist)))))))
1713 (defun rcirc-log-write ()
1714 "Flush `rcirc-log-alist' data to disk.
1716 Log data is written to `rcirc-log-directory', except for
1717 log-files with absolute names (see `rcirc-log-filename-function')."
1718 (dolist (cell rcirc-log-alist)
1719 (let ((filename (convert-standard-filename
1720 (expand-file-name (car cell)
1721 rcirc-log-directory)))
1722 (coding-system-for-write 'utf-8))
1723 (make-directory (file-name-directory filename) t)
1724 (with-temp-buffer
1725 (insert (cdr cell))
1726 (write-region (point-min) (point-max) filename t 'quiet))))
1727 (setq rcirc-log-alist nil))
1729 (defun rcirc-view-log-file ()
1730 "View logfile corresponding to the current buffer."
1731 (interactive)
1732 (find-file-other-window
1733 (expand-file-name (funcall rcirc-log-filename-function
1734 (rcirc-buffer-process) rcirc-target)
1735 rcirc-log-directory)))
1737 (defun rcirc-join-channels (process channels)
1738 "Join CHANNELS."
1739 (save-window-excursion
1740 (dolist (channel channels)
1741 (with-rcirc-process-buffer process
1742 (rcirc-cmd-join channel process)))))
1743 \f
1744 ;;; nick management
1745 (defvar rcirc-nick-prefix-chars "~&@%+")
1746 (defun rcirc-user-nick (user)
1747 "Return the nick from USER. Remove any non-nick junk."
1748 (save-match-data
1749 (if (string-match (concat "^[" rcirc-nick-prefix-chars
1750 "]?\\([^! ]+\\)!?") (or user ""))
1751 (match-string 1 user)
1752 user)))
1754 (defun rcirc-nick-channels (process nick)
1755 "Return list of channels for NICK."
1756 (with-rcirc-process-buffer process
1757 (mapcar (lambda (x) (car x))
1758 (gethash nick rcirc-nick-table))))
1760 (defun rcirc-put-nick-channel (process nick channel &optional line)
1761 "Add CHANNEL to list associated with NICK.
1762 Update the associated linestamp if LINE is non-nil.
1764 If the record doesn't exist, and LINE is nil, set the linestamp
1765 to zero."
1766 (let ((nick (rcirc-user-nick nick)))
1767 (with-rcirc-process-buffer process
1768 (let* ((chans (gethash nick rcirc-nick-table))
1769 (record (assoc-string channel chans t)))
1770 (if record
1771 (when line (setcdr record line))
1772 (puthash nick (cons (cons channel (or line 0))
1773 chans)
1774 rcirc-nick-table))))))
1776 (defun rcirc-nick-remove (process nick)
1777 "Remove NICK from table."
1778 (with-rcirc-process-buffer process
1779 (remhash nick rcirc-nick-table)))
1781 (defun rcirc-remove-nick-channel (process nick channel)
1782 "Remove the CHANNEL from list associated with NICK."
1783 (with-rcirc-process-buffer process
1784 (let* ((chans (gethash nick rcirc-nick-table))
1785 (newchans
1786 ;; instead of assoc-string-delete-all:
1787 (let ((record (assoc-string channel chans t)))
1788 (when record
1789 (setcar record 'delete)
1790 (assq-delete-all 'delete chans)))))
1791 (if newchans
1792 (puthash nick newchans rcirc-nick-table)
1793 (remhash nick rcirc-nick-table)))))
1795 (defun rcirc-channel-nicks (process target)
1796 "Return the list of nicks associated with TARGET sorted by last activity."
1797 (when target
1798 (if (rcirc-channel-p target)
1799 (with-rcirc-process-buffer process
1800 (let (nicks)
1801 (maphash
1802 (lambda (k v)
1803 (let ((record (assoc-string target v t)))
1804 (if record
1805 (setq nicks (cons (cons k (cdr record)) nicks)))))
1806 rcirc-nick-table)
1807 (mapcar (lambda (x) (car x))
1808 (sort nicks (lambda (x y)
1809 (let ((lx (or (cdr x) 0))
1810 (ly (or (cdr y) 0)))
1811 (< ly lx)))))))
1812 (list target))))
1814 (defun rcirc-ignore-update-automatic (nick)
1815 "Remove NICK from `rcirc-ignore-list'
1816 if NICK is also on `rcirc-ignore-list-automatic'."
1817 (when (member nick rcirc-ignore-list-automatic)
1818 (setq rcirc-ignore-list-automatic
1819 (delete nick rcirc-ignore-list-automatic)
1820 rcirc-ignore-list
1821 (delete nick rcirc-ignore-list))))
1822 \f
1823 (defun rcirc-nickname< (s1 s2)
1824 "Return t if IRC nickname S1 is less than S2, and nil otherwise.
1825 Operator nicknames (@) are considered less than voiced
1826 nicknames (+). Any other nicknames are greater than voiced
1827 nicknames. The comparison is case-insensitive."
1828 (setq s1 (downcase s1)
1829 s2 (downcase s2))
1830 (let* ((s1-op (eq ?@ (string-to-char s1)))
1831 (s2-op (eq ?@ (string-to-char s2))))
1832 (if s1-op
1833 (if s2-op
1834 (string< (substring s1 1) (substring s2 1))
1835 t)
1836 (if s2-op
1837 nil
1838 (string< s1 s2)))))
1840 (defun rcirc-sort-nicknames-join (input sep)
1841 "Return a string of sorted nicknames.
1842 INPUT is a string containing nicknames separated by SEP.
1843 This function does not alter the INPUT string."
1844 (let* ((parts (split-string input sep t))
1845 (sorted (sort parts 'rcirc-nickname<)))
1846 (mapconcat 'identity sorted sep)))
1847 \f
1848 ;;; activity tracking
1849 (defvar rcirc-track-minor-mode-map
1850 (let ((map (make-sparse-keymap)))
1851 (define-key map (kbd "C-c C-@") 'rcirc-next-active-buffer)
1852 (define-key map (kbd "C-c C-SPC") 'rcirc-next-active-buffer)
1853 map)
1854 "Keymap for rcirc track minor mode.")
1856 ;;;###autoload
1857 (define-minor-mode rcirc-track-minor-mode
1858 "Global minor mode for tracking activity in rcirc buffers.
1859 With a prefix argument ARG, enable the mode if ARG is positive,
1860 and disable it otherwise. If called from Lisp, enable the mode
1861 if ARG is omitted or nil."
1862 :init-value nil
1863 :lighter ""
1864 :keymap rcirc-track-minor-mode-map
1865 :global t
1866 :group 'rcirc
1867 (or global-mode-string (setq global-mode-string '("")))
1868 ;; toggle the mode-line channel indicator
1869 (if rcirc-track-minor-mode
1870 (progn
1871 (and (not (memq 'rcirc-activity-string global-mode-string))
1872 (setq global-mode-string
1873 (append global-mode-string '(rcirc-activity-string))))
1874 (add-hook 'window-configuration-change-hook
1875 'rcirc-window-configuration-change))
1876 (setq global-mode-string
1877 (delete 'rcirc-activity-string global-mode-string))
1878 (remove-hook 'window-configuration-change-hook
1879 'rcirc-window-configuration-change)))
1881 (or (assq 'rcirc-ignore-buffer-activity-flag minor-mode-alist)
1882 (setq minor-mode-alist
1883 (cons '(rcirc-ignore-buffer-activity-flag " Ignore") minor-mode-alist)))
1884 (or (assq 'rcirc-low-priority-flag minor-mode-alist)
1885 (setq minor-mode-alist
1886 (cons '(rcirc-low-priority-flag " LowPri") minor-mode-alist)))
1887 (or (assq 'rcirc-omit-mode minor-mode-alist)
1888 (setq minor-mode-alist
1889 (cons '(rcirc-omit-mode " Omit") minor-mode-alist)))
1891 (defun rcirc-toggle-ignore-buffer-activity ()
1892 "Toggle the value of `rcirc-ignore-buffer-activity-flag'."
1893 (interactive)
1894 (setq rcirc-ignore-buffer-activity-flag
1895 (not rcirc-ignore-buffer-activity-flag))
1896 (message (if rcirc-ignore-buffer-activity-flag
1897 "Ignore activity in this buffer"
1898 "Notice activity in this buffer"))
1899 (force-mode-line-update))
1901 (defun rcirc-toggle-low-priority ()
1902 "Toggle the value of `rcirc-low-priority-flag'."
1903 (interactive)
1904 (setq rcirc-low-priority-flag
1905 (not rcirc-low-priority-flag))
1906 (message (if rcirc-low-priority-flag
1907 "Activity in this buffer is low priority"
1908 "Activity in this buffer is normal priority"))
1909 (force-mode-line-update))
1911 (defun rcirc-omit-mode ()
1912 "Toggle the Rcirc-Omit mode.
1913 If enabled, \"uninteresting\" lines are not shown.
1914 Uninteresting lines are those whose responses are listed in
1915 `rcirc-omit-responses'."
1916 (interactive)
1917 (setq rcirc-omit-mode (not rcirc-omit-mode))
1918 (if rcirc-omit-mode
1919 (progn
1920 (add-to-invisibility-spec '(rcirc-omit . nil))
1921 (message "Rcirc-Omit mode enabled"))
1922 (remove-from-invisibility-spec '(rcirc-omit . nil))
1923 (message "Rcirc-Omit mode disabled"))
1924 (dolist (window (get-buffer-window-list (current-buffer)))
1925 (with-selected-window window
1926 (recenter (when (> (point) rcirc-prompt-start-marker) -1)))))
1928 (defun rcirc-switch-to-server-buffer ()
1929 "Switch to the server buffer associated with current channel buffer."
1930 (interactive)
1931 (unless (buffer-live-p rcirc-server-buffer)
1932 (error "No such buffer"))
1933 (switch-to-buffer rcirc-server-buffer))
1935 (defun rcirc-jump-to-first-unread-line ()
1936 "Move the point to the first unread line in this buffer."
1937 (interactive)
1938 (if (marker-position overlay-arrow-position)
1939 (goto-char overlay-arrow-position)
1940 (message "No unread messages")))
1942 (defun rcirc-bury-buffers ()
1943 "Bury all RCIRC buffers."
1944 (interactive)
1945 (dolist (buf (buffer-list))
1946 (when (eq 'rcirc-mode (with-current-buffer buf major-mode))
1947 (bury-buffer buf) ; buffers not shown
1948 (quit-windows-on buf)))) ; buffers shown in a window
1950 (defun rcirc-next-active-buffer (arg)
1951 "Switch to the next rcirc buffer with activity.
1952 With prefix ARG, go to the next low priority buffer with activity."
1953 (interactive "P")
1954 (let* ((pair (rcirc-split-activity rcirc-activity))
1955 (lopri (car pair))
1956 (hipri (cdr pair)))
1957 (if (or (and (not arg) hipri)
1958 (and arg lopri))
1959 (progn
1960 (switch-to-buffer (car (if arg lopri hipri)))
1961 (when (> (point) rcirc-prompt-start-marker)
1962 (recenter -1)))
1963 (rcirc-bury-buffers)
1964 (message "No IRC activity.%s"
1965 (if lopri
1966 (concat
1967 " Type C-u " (key-description (this-command-keys))
1968 " for low priority activity.")
1969 "")))))
1971 (define-obsolete-variable-alias 'rcirc-activity-hooks
1972 'rcirc-activity-functions "24.3")
1973 (defvar rcirc-activity-functions nil
1974 "Hook to be run when there is channel activity.
1976 Functions are called with a single argument, the buffer with the
1977 activity. Only run if the buffer is not visible and
1978 `rcirc-ignore-buffer-activity-flag' is non-nil.")
1980 (defun rcirc-record-activity (buffer &optional type)
1981 "Record BUFFER activity with TYPE."
1982 (with-current-buffer buffer
1983 (let ((old-activity rcirc-activity)
1984 (old-types rcirc-activity-types))
1985 (when (not (get-buffer-window (current-buffer) t))
1986 (setq rcirc-activity
1987 (sort (if (memq (current-buffer) rcirc-activity) rcirc-activity
1988 (cons (current-buffer) rcirc-activity))
1989 (lambda (b1 b2)
1990 (let ((t1 (with-current-buffer b1 rcirc-last-post-time))
1991 (t2 (with-current-buffer b2 rcirc-last-post-time)))
1992 (time-less-p t2 t1)))))
1993 (cl-pushnew type rcirc-activity-types)
1994 (unless (and (equal rcirc-activity old-activity)
1995 (member type old-types))
1996 (rcirc-update-activity-string)))))
1997 (run-hook-with-args 'rcirc-activity-functions buffer))
1999 (defun rcirc-clear-activity (buffer)
2000 "Clear the BUFFER activity."
2001 (setq rcirc-activity (remove buffer rcirc-activity))
2002 (with-current-buffer buffer
2003 (setq rcirc-activity-types nil)))
2005 (defun rcirc-clear-unread (buffer)
2006 "Erase the last read message arrow from BUFFER."
2007 (when (buffer-live-p buffer)
2008 (with-current-buffer buffer
2009 (set-marker overlay-arrow-position nil))))
2011 (defun rcirc-split-activity (activity)
2012 "Return a cons cell with ACTIVITY split into (lopri . hipri)."
2013 (let (lopri hipri)
2014 (dolist (buf activity)
2015 (with-current-buffer buf
2016 (if (and rcirc-low-priority-flag
2017 (not (member 'nick rcirc-activity-types)))
2018 (push buf lopri)
2019 (push buf hipri))))
2020 (cons (nreverse lopri) (nreverse hipri))))
2022 (defvar rcirc-update-activity-string-hook nil
2023 "Hook run whenever the activity string is updated.")
2025 ;; TODO: add mouse properties
2026 (defun rcirc-update-activity-string ()
2027 "Update mode-line string."
2028 (let* ((pair (rcirc-split-activity rcirc-activity))
2029 (lopri (car pair))
2030 (hipri (cdr pair)))
2031 (setq rcirc-activity-string
2032 (cond ((or hipri lopri)
2033 (concat (and hipri "[")
2034 (rcirc-activity-string hipri)
2035 (and hipri lopri ",")
2036 (and lopri
2037 (concat "("
2038 (rcirc-activity-string lopri)
2039 ")"))
2040 (and hipri "]")))
2041 ((not (null (rcirc-process-list)))
2042 "[]")
2043 (t "[]")))
2044 (run-hooks 'rcirc-update-activity-string-hook)))
2046 (defun rcirc-activity-string (buffers)
2047 (mapconcat (lambda (b)
2048 (let ((s (substring-no-properties (rcirc-short-buffer-name b))))
2049 (with-current-buffer b
2050 (dolist (type rcirc-activity-types)
2051 (rcirc-add-face 0 (length s)
2052 (cl-case type
2053 (nick 'rcirc-track-nick)
2054 (keyword 'rcirc-track-keyword))
2055 s)))
2056 s))
2057 buffers ","))
2059 (defun rcirc-short-buffer-name (buffer)
2060 "Return a short name for BUFFER to use in the mode line indicator."
2061 (with-current-buffer buffer
2062 (or rcirc-short-buffer-name (buffer-name))))
2064 (defun rcirc-visible-buffers ()
2065 "Return a list of the visible buffers that are in rcirc-mode."
2066 (let (acc)
2067 (walk-windows (lambda (w)
2068 (with-current-buffer (window-buffer w)
2069 (when (eq major-mode 'rcirc-mode)
2070 (push (current-buffer) acc)))))
2071 acc))
2073 (defvar rcirc-visible-buffers nil)
2074 (defun rcirc-window-configuration-change ()
2075 (unless (minibuffer-window-active-p (minibuffer-window))
2076 ;; delay this until command has finished to make sure window is
2077 ;; actually visible before clearing activity
2078 (add-hook 'post-command-hook 'rcirc-window-configuration-change-1)))
2080 (defun rcirc-window-configuration-change-1 ()
2081 ;; clear activity and overlay arrows
2082 (let* ((old-activity rcirc-activity)
2083 (hidden-buffers rcirc-visible-buffers))
2085 (setq rcirc-visible-buffers (rcirc-visible-buffers))
2087 (dolist (vbuf rcirc-visible-buffers)
2088 (setq hidden-buffers (delq vbuf hidden-buffers))
2089 ;; clear activity for all visible buffers
2090 (rcirc-clear-activity vbuf))
2092 ;; clear unread arrow from recently hidden buffers
2093 (dolist (hbuf hidden-buffers)
2094 (rcirc-clear-unread hbuf))
2096 ;; remove any killed buffers from list
2097 (setq rcirc-activity
2098 (delq nil (mapcar (lambda (buf) (when (buffer-live-p buf) buf))
2099 rcirc-activity)))
2100 ;; update the mode-line string
2101 (unless (equal old-activity rcirc-activity)
2102 (rcirc-update-activity-string)))
2104 (remove-hook 'post-command-hook 'rcirc-window-configuration-change-1))
2106 \f
2107 ;;; buffer name abbreviation
2108 (defun rcirc-update-short-buffer-names ()
2109 (let ((bufalist
2110 (apply 'append (mapcar (lambda (process)
2111 (with-rcirc-process-buffer process
2112 rcirc-buffer-alist))
2113 (rcirc-process-list)))))
2114 (dolist (i (rcirc-abbreviate bufalist))
2115 (when (buffer-live-p (cdr i))
2116 (with-current-buffer (cdr i)
2117 (setq rcirc-short-buffer-name (car i)))))))
2119 (defun rcirc-abbreviate (pairs)
2120 (apply 'append (mapcar 'rcirc-rebuild-tree (rcirc-make-trees pairs))))
2122 (defun rcirc-rebuild-tree (tree &optional acc)
2123 (let ((ch (char-to-string (car tree))))
2124 (dolist (x (cdr tree))
2125 (if (listp x)
2126 (setq acc (append acc
2127 (mapcar (lambda (y)
2128 (cons (concat ch (car y))
2129 (cdr y)))
2130 (rcirc-rebuild-tree x))))
2131 (setq acc (cons (cons ch x) acc))))
2132 acc))
2134 (defun rcirc-make-trees (pairs)
2135 (let (alist)
2136 (mapc (lambda (pair)
2137 (if (consp pair)
2138 (let* ((str (car pair))
2139 (data (cdr pair))
2140 (char (unless (zerop (length str))
2141 (aref str 0)))
2142 (rest (unless (zerop (length str))
2143 (substring str 1)))
2144 (part (if char (assq char alist))))
2145 (if part
2146 ;; existing partition
2147 (setcdr part (cons (cons rest data) (cdr part)))
2148 ;; new partition
2149 (setq alist (cons (if char
2150 (list char (cons rest data))
2151 data)
2152 alist))))
2153 (setq alist (cons pair alist))))
2154 pairs)
2155 ;; recurse into cdrs of alist
2156 (mapc (lambda (x)
2157 (when (and (listp x) (listp (cadr x)))
2158 (setcdr x (if (> (length (cdr x)) 1)
2159 (rcirc-make-trees (cdr x))
2160 (setcdr x (list (cl-cdadr x)))))))
2161 alist)))
2162 \f
2163 ;;; /commands these are called with 3 args: PROCESS, TARGET, which is
2164 ;; the current buffer/channel/user, and ARGS, which is a string
2165 ;; containing the text following the /cmd.
2167 (defmacro defun-rcirc-command (command argument docstring interactive-form
2168 &rest body)
2169 "Define a command."
2170 `(progn
2171 (add-to-list 'rcirc-client-commands ,(concat "/" (symbol-name command)))
2172 (defun ,(intern (concat "rcirc-cmd-" (symbol-name command)))
2173 (,@argument &optional process target)
2174 ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values given"
2175 "\nby `rcirc-buffer-process' and `rcirc-target' will be used.")
2176 ,interactive-form
2177 (let ((process (or process (rcirc-buffer-process)))
2178 (target (or target rcirc-target)))
2179 (ignore target) ; mark `target' variable as ignorable
2180 ,@body))))
2182 (defun-rcirc-command msg (message)
2183 "Send private MESSAGE to TARGET."
2184 (interactive "i")
2185 (if (null message)
2186 (progn
2187 (setq target (completing-read "Message nick: "
2188 (with-rcirc-server-buffer
2189 rcirc-nick-table)))
2190 (when (> (length target) 0)
2191 (setq message (read-string (format "Message %s: " target)))
2192 (when (> (length message) 0)
2193 (rcirc-send-message process target message))))
2194 (if (not (string-match "\\([^ ]+\\) \\(.+\\)" message))
2195 (message "Not enough args, or something.")
2196 (setq target (match-string 1 message)
2197 message (match-string 2 message))
2198 (rcirc-send-message process target message))))
2200 (defun-rcirc-command query (nick)
2201 "Open a private chat buffer to NICK."
2202 (interactive (list (completing-read "Query nick: "
2203 (with-rcirc-server-buffer rcirc-nick-table))))
2204 (let ((existing-buffer (rcirc-get-buffer process nick)))
2205 (switch-to-buffer (or existing-buffer
2206 (rcirc-get-buffer-create process nick)))
2207 (when (not existing-buffer)
2208 (rcirc-cmd-whois nick))))
2210 (defun-rcirc-command join (channels)
2211 "Join CHANNELS.
2212 CHANNELS is a comma- or space-separated string of channel names."
2213 (interactive "sJoin channels: ")
2214 (let* ((split-channels (split-string channels "[ ,]" t))
2215 (buffers (mapcar (lambda (ch)
2216 (rcirc-get-buffer-create process ch))
2217 split-channels))
2218 (channels (mapconcat 'identity split-channels ",")))
2219 (rcirc-send-string process (concat "JOIN " channels))
2220 (when (not (eq (selected-window) (minibuffer-window)))
2221 (dolist (b buffers) ;; order the new channel buffers in the buffer list
2222 (switch-to-buffer b)))))
2224 (defun-rcirc-command invite (nick-channel)
2225 "Invite NICK to CHANNEL."
2226 (interactive (list
2227 (concat
2228 (completing-read "Invite nick: "
2229 (with-rcirc-server-buffer rcirc-nick-table))
2230 " "
2231 (read-string "Channel: "))))
2232 (rcirc-send-string process (concat "INVITE " nick-channel)))
2234 ;; TODO: /part #channel reason, or consider removing #channel altogether
2235 (defun-rcirc-command part (channel)
2236 "Part CHANNEL."
2237 (interactive "sPart channel: ")
2238 (let ((channel (if (> (length channel) 0) channel target)))
2239 (rcirc-send-string process (concat "PART " channel " :" rcirc-id-string))))
2241 (defun-rcirc-command quit (reason)
2242 "Send a quit message to server with REASON."
2243 (interactive "sQuit reason: ")
2244 (rcirc-send-string process (concat "QUIT :"
2245 (if (not (zerop (length reason)))
2246 reason
2247 rcirc-id-string))))
2249 (defun-rcirc-command reconnect (_)
2250 "Reconnect to current server."
2251 (interactive "i")
2252 (with-rcirc-server-buffer
2253 (cond
2254 (rcirc-connecting (message "Already connecting"))
2255 ((process-live-p process) (message "Server process is alive"))
2256 (t (let ((conn-info rcirc-connection-info))
2257 (setf (nth 5 conn-info)
2258 (cl-remove-if-not #'rcirc-channel-p
2259 (mapcar #'car rcirc-buffer-alist)))
2260 (apply #'rcirc-connect conn-info))))))
2262 (defun-rcirc-command nick (nick)
2263 "Change nick to NICK."
2264 (interactive "i")
2265 (when (null nick)
2266 (setq nick (read-string "New nick: " (rcirc-nick process))))
2267 (rcirc-send-string process (concat "NICK " nick)))
2269 (defun-rcirc-command names (channel)
2270 "Display list of names in CHANNEL or in current channel if CHANNEL is nil.
2271 If called interactively, prompt for a channel when prefix arg is supplied."
2272 (interactive "P")
2273 (if (called-interactively-p 'interactive)
2274 (if channel
2275 (setq channel (read-string "List names in channel: " target))))
2276 (let ((channel (if (> (length channel) 0)
2277 channel
2278 target)))
2279 (rcirc-send-string process (concat "NAMES " channel))))
2281 (defun-rcirc-command topic (topic)
2282 "List TOPIC for the TARGET channel.
2283 With a prefix arg, prompt for new topic."
2284 (interactive "P")
2285 (if (and (called-interactively-p 'interactive) topic)
2286 (setq topic (read-string "New Topic: " rcirc-topic)))
2287 (rcirc-send-string process (concat "TOPIC " target
2288 (when (> (length topic) 0)
2289 (concat " :" topic)))))
2291 (defun-rcirc-command whois (nick)
2292 "Request information from server about NICK."
2293 (interactive (list
2294 (completing-read "Whois: "
2295 (with-rcirc-server-buffer rcirc-nick-table))))
2296 (rcirc-send-string process (concat "WHOIS " nick)))
2298 (defun-rcirc-command mode (args)
2299 "Set mode with ARGS."
2300 (interactive (list (concat (read-string "Mode nick or channel: ")
2301 " " (read-string "Mode: "))))
2302 (rcirc-send-string process (concat "MODE " args)))
2304 (defun-rcirc-command list (channels)
2305 "Request information on CHANNELS from server."
2306 (interactive "sList Channels: ")
2307 (rcirc-send-string process (concat "LIST " channels)))
2309 (defun-rcirc-command oper (args)
2310 "Send operator command to server."
2311 (interactive "sOper args: ")
2312 (rcirc-send-string process (concat "OPER " args)))
2314 (defun-rcirc-command quote (message)
2315 "Send MESSAGE literally to server."
2316 (interactive "sServer message: ")
2317 (rcirc-send-string process message))
2319 (defun-rcirc-command kick (arg)
2320 "Kick NICK from current channel."
2321 (interactive (list
2322 (concat (completing-read "Kick nick: "
2323 (rcirc-channel-nicks
2324 (rcirc-buffer-process)
2325 rcirc-target))
2326 (read-from-minibuffer "Kick reason: "))))
2327 (let* ((arglist (split-string arg))
2328 (argstring (concat (car arglist) " :"
2329 (mapconcat 'identity (cdr arglist) " "))))
2330 (rcirc-send-string process (concat "KICK " target " " argstring))))
2332 (defun rcirc-cmd-ctcp (args &optional process _target)
2333 (if (string-match "^\\([^ ]+\\)\\s-+\\(.+\\)$" args)
2334 (let* ((target (match-string 1 args))
2335 (request (upcase (match-string 2 args)))
2336 (function (intern-soft (concat "rcirc-ctcp-sender-" request))))
2337 (if (fboundp function) ;; use special function if available
2338 (funcall function process target request)
2339 (rcirc-send-ctcp process target request)))
2340 (rcirc-print process (rcirc-nick process) "ERROR" nil
2341 "usage: /ctcp NICK REQUEST")))
2343 (defun rcirc-ctcp-sender-PING (process target _request)
2344 "Send a CTCP PING message to TARGET."
2345 (let ((timestamp (format "%.0f" (rcirc-float-time))))
2346 (rcirc-send-ctcp process target "PING" timestamp)))
2348 (defun rcirc-cmd-me (args &optional process target)
2349 (rcirc-send-ctcp process target "ACTION" args))
2351 (defun rcirc-add-or-remove (set &rest elements)
2352 (dolist (elt elements)
2353 (if (and elt (not (string= "" elt)))
2354 (setq set (if (member-ignore-case elt set)
2355 (delete elt set)
2356 (cons elt set)))))
2357 set)
2359 (defun-rcirc-command ignore (nick)
2360 "Manage the ignore list.
2361 Ignore NICK, unignore NICK if already ignored, or list ignored
2362 nicks when no NICK is given. When listing ignored nicks, the
2363 ones added to the list automatically are marked with an asterisk."
2364 (interactive "sToggle ignoring of nick: ")
2365 (setq rcirc-ignore-list
2366 (apply #'rcirc-add-or-remove rcirc-ignore-list
2367 (split-string nick nil t)))
2368 (rcirc-print process nil "IGNORE" target
2369 (mapconcat
2370 (lambda (nick)
2371 (concat nick
2372 (if (member nick rcirc-ignore-list-automatic)
2373 "*" "")))
2374 rcirc-ignore-list " ")))
2376 (defun-rcirc-command bright (nick)
2377 "Manage the bright nick list."
2378 (interactive "sToggle emphasis of nick: ")
2379 (setq rcirc-bright-nicks
2380 (apply #'rcirc-add-or-remove rcirc-bright-nicks
2381 (split-string nick nil t)))
2382 (rcirc-print process nil "BRIGHT" target
2383 (mapconcat 'identity rcirc-bright-nicks " ")))
2385 (defun-rcirc-command dim (nick)
2386 "Manage the dim nick list."
2387 (interactive "sToggle deemphasis of nick: ")
2388 (setq rcirc-dim-nicks
2389 (apply #'rcirc-add-or-remove rcirc-dim-nicks
2390 (split-string nick nil t)))
2391 (rcirc-print process nil "DIM" target
2392 (mapconcat 'identity rcirc-dim-nicks " ")))
2394 (defun-rcirc-command keyword (keyword)
2395 "Manage the keyword list.
2396 Mark KEYWORD, unmark KEYWORD if already marked, or list marked
2397 keywords when no KEYWORD is given."
2398 (interactive "sToggle highlighting of keyword: ")
2399 (setq rcirc-keywords
2400 (apply #'rcirc-add-or-remove rcirc-keywords
2401 (split-string keyword nil t)))
2402 (rcirc-print process nil "KEYWORD" target
2403 (mapconcat 'identity rcirc-keywords " ")))
2405 \f
2406 (defun rcirc-add-face (start end name &optional object)
2407 "Add face NAME to the face text property of the text from START to END."
2408 (when name
2409 (let ((pos start)
2410 next prop)
2411 (while (< pos end)
2412 (setq prop (get-text-property pos 'font-lock-face object)
2413 next (next-single-property-change pos 'font-lock-face object end))
2414 (unless (member name (get-text-property pos 'font-lock-face object))
2415 (add-text-properties pos next
2416 (list 'font-lock-face (cons name prop)) object))
2417 (setq pos next)))))
2419 (defun rcirc-facify (string face)
2420 "Return a copy of STRING with FACE property added."
2421 (let ((string (or string "")))
2422 (rcirc-add-face 0 (length string) face string)
2423 string))
2425 (defvar rcirc-url-regexp
2426 (concat
2427 "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
2428 "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
2429 "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
2430 (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
2431 (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
2432 (punct "!?:;.,"))
2433 (concat
2434 "\\(?:"
2435 ;; Match paired parentheses, e.g. in Wikipedia URLs:
2436 "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]"
2437 "\\|"
2438 "[" chars punct "]+" "[" chars "]"
2439 "\\)"))
2440 (concat ;; XEmacs 21.4 doesn't support POSIX.
2441 "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
2442 "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
2443 "\\)")
2444 "Regexp matching URLs. Set to nil to disable URL features in rcirc.")
2446 ;; cf cl-remove-if-not
2447 (defun rcirc-condition-filter (condp lst)
2448 "Remove all items not satisfying condition CONDP in list LST.
2449 CONDP is a function that takes a list element as argument and returns
2450 non-nil if that element should be included. Returns a new list."
2451 (delq nil (mapcar (lambda (x) (and (funcall condp x) x)) lst)))
2453 (defun rcirc-browse-url (&optional arg)
2454 "Prompt for URL to browse based on URLs in buffer before point.
2456 If ARG is given, opens the URL in a new browser window."
2457 (interactive "P")
2458 (let* ((point (point))
2459 (filtered (rcirc-condition-filter
2460 (lambda (x) (>= point (cdr x)))
2461 rcirc-urls))
2462 (completions (mapcar (lambda (x) (car x)) filtered))
2463 (defaults (mapcar (lambda (x) (car x)) filtered)))
2464 (browse-url (completing-read "Rcirc browse-url: "
2465 completions nil nil (car defaults) nil defaults)
2466 arg)))
2467 \f
2468 (defun rcirc-markup-timestamp (_sender _response)
2469 (goto-char (point-min))
2470 (insert (rcirc-facify (format-time-string rcirc-time-format)
2471 'rcirc-timestamp)))
2473 (defun rcirc-markup-attributes (_sender _response)
2474 (while (re-search-forward "\\([\C-b\C-_\C-v]\\).*?\\(\\1\\|\C-o\\)" nil t)
2475 (rcirc-add-face (match-beginning 0) (match-end 0)
2476 (cl-case (char-after (match-beginning 1))
2477 (?\C-b 'bold)
2478 (?\C-v 'italic)
2479 (?\C-_ 'underline)))
2480 ;; keep the ^O since it could terminate other attributes
2481 (when (not (eq ?\C-o (char-before (match-end 2))))
2482 (delete-region (match-beginning 2) (match-end 2)))
2483 (delete-region (match-beginning 1) (match-end 1))
2484 (goto-char (match-beginning 1)))
2485 ;; remove the ^O characters now
2486 (goto-char (point-min))
2487 (while (re-search-forward "\C-o+" nil t)
2488 (delete-region (match-beginning 0) (match-end 0))))
2490 (defun rcirc-markup-my-nick (_sender response)
2491 (with-syntax-table rcirc-nick-syntax-table
2492 (while (re-search-forward (concat "\\b"
2493 (regexp-quote (rcirc-nick
2494 (rcirc-buffer-process)))
2495 "\\b")
2496 nil t)
2497 (rcirc-add-face (match-beginning 0) (match-end 0)
2498 'rcirc-nick-in-message)
2499 (when (string= response "PRIVMSG")
2500 (rcirc-add-face (point-min) (point-max)
2501 'rcirc-nick-in-message-full-line)
2502 (rcirc-record-activity (current-buffer) 'nick)))))
2504 (defun rcirc-markup-urls (_sender _response)
2505 (while (and rcirc-url-regexp ;; nil means disable URL catching
2506 (re-search-forward rcirc-url-regexp nil t))
2507 (let* ((start (match-beginning 0))
2508 (end (match-end 0))
2509 (url (match-string-no-properties 0))
2510 (link-text (buffer-substring-no-properties start end)))
2511 (make-button start end
2512 'face 'rcirc-url
2513 'follow-link t
2514 'rcirc-url url
2515 'action (lambda (button)
2516 (browse-url (button-get button 'rcirc-url))))
2517 ;; record the url if it is not already the latest stored url
2518 (when (not (string= link-text (caar rcirc-urls)))
2519 (push (cons link-text start) rcirc-urls)))))
2521 (defun rcirc-markup-keywords (sender response)
2522 (when (and (string= response "PRIVMSG")
2523 (not (string= sender (rcirc-nick (rcirc-buffer-process)))))
2524 (let* ((target (or rcirc-target ""))
2525 (keywords (delq nil (mapcar (lambda (keyword)
2526 (when (not (string-match keyword
2527 target))
2528 keyword))
2529 rcirc-keywords))))
2530 (when keywords
2531 (while (re-search-forward (regexp-opt keywords 'words) nil t)
2532 (rcirc-add-face (match-beginning 0) (match-end 0) 'rcirc-keyword)
2533 (rcirc-record-activity (current-buffer) 'keyword))))))
2535 (defun rcirc-markup-bright-nicks (_sender response)
2536 (when (and rcirc-bright-nicks
2537 (string= response "NAMES"))
2538 (with-syntax-table rcirc-nick-syntax-table
2539 (while (re-search-forward (regexp-opt rcirc-bright-nicks 'words) nil t)
2540 (rcirc-add-face (match-beginning 0) (match-end 0)
2541 'rcirc-bright-nick)))))
2543 (defun rcirc-markup-fill (_sender response)
2544 (when (not (string= response "372")) ; /motd
2545 (let ((fill-prefix
2546 (or rcirc-fill-prefix
2547 (make-string (- (point) (line-beginning-position)) ?\s)))
2548 (fill-column (- (cond ((null rcirc-fill-column) fill-column)
2549 ((functionp rcirc-fill-column)
2550 (funcall rcirc-fill-column))
2551 (t rcirc-fill-column))
2552 ;; make sure ... doesn't cause line wrapping
2553 3)))
2554 (fill-region (point) (point-max) nil t))))
2555 \f
2556 ;;; handlers
2557 ;; these are called with the server PROCESS, the SENDER, which is a
2558 ;; server or a user, depending on the command, the ARGS, which is a
2559 ;; list of strings, and the TEXT, which is the original server text,
2560 ;; verbatim
2561 (defun rcirc-handler-001 (process sender args text)
2562 (rcirc-handler-generic process "001" sender args text)
2563 (with-rcirc-process-buffer process
2564 (setq rcirc-connecting nil)
2565 (rcirc-reschedule-timeout process)
2566 (setq rcirc-server-name sender)
2567 (setq rcirc-nick (car args))
2568 (rcirc-update-prompt)
2569 (if rcirc-auto-authenticate-flag
2570 (if (and rcirc-authenticate-before-join
2571 ;; We have to ensure that there's an authentication
2572 ;; entry for that server. Else,
2573 ;; rcirc-authenticated-hook won't be triggered, and
2574 ;; autojoin won't happen at all.
2575 (let (auth-required)
2576 (dolist (s rcirc-authinfo auth-required)
2577 (when (string-match (car s) rcirc-server-name)
2578 (setq auth-required t)))))
2579 (progn
2580 (add-hook 'rcirc-authenticated-hook 'rcirc-join-channels-post-auth t t)
2581 (rcirc-authenticate))
2582 (rcirc-authenticate)
2583 (rcirc-join-channels process rcirc-startup-channels))
2584 (rcirc-join-channels process rcirc-startup-channels))))
2586 (defun rcirc-join-channels-post-auth (process)
2587 "Join `rcirc-startup-channels' after authenticating."
2588 (with-rcirc-process-buffer process
2589 (rcirc-join-channels process rcirc-startup-channels)))
2591 (defun rcirc-handler-PRIVMSG (process sender args text)
2592 (rcirc-check-auth-status process sender args text)
2593 (let ((target (if (rcirc-channel-p (car args))
2594 (car args)
2595 sender))
2596 (message (or (cadr args) "")))
2597 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
2598 (rcirc-handler-CTCP process target sender (match-string 1 message))
2599 (rcirc-print process sender "PRIVMSG" target message t))
2600 ;; update nick linestamp
2601 (with-current-buffer (rcirc-get-buffer process target t)
2602 (rcirc-put-nick-channel process sender target rcirc-current-line))))
2604 (defun rcirc-handler-NOTICE (process sender args text)
2605 (rcirc-check-auth-status process sender args text)
2606 (let ((target (car args))
2607 (message (cadr args)))
2608 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
2609 (rcirc-handler-CTCP-response process target sender
2610 (match-string 1 message))
2611 (rcirc-print process sender "NOTICE"
2612 (cond ((rcirc-channel-p target)
2613 target)
2614 ;;; -ChanServ- [#gnu] Welcome...
2615 ((string-match "\\[\\(#[^] ]+\\)\\]" message)
2616 (match-string 1 message))
2617 (sender
2618 (if (string= sender (rcirc-server-name process))
2619 nil ; server notice
2620 sender)))
2621 message t))))
2623 (defun rcirc-check-auth-status (process sender args _text)
2624 "Check if the user just authenticated.
2625 If authenticated, runs `rcirc-authenticated-hook' with PROCESS as
2626 the only argument."
2627 (with-rcirc-process-buffer process
2628 (when (and (not rcirc-user-authenticated)
2629 rcirc-authenticate-before-join
2630 rcirc-auto-authenticate-flag)
2631 (let ((target (car args))
2632 (message (cadr args)))
2633 (when (or
2634 (and ;; nickserv
2635 (string= sender "NickServ")
2636 (string= target rcirc-nick)
2637 (member message
2638 (list
2639 (format "You are now identified for \C-b%s\C-b." rcirc-nick)
2640 (format "You are successfully identified as \C-b%s\C-b." rcirc-nick)
2641 "Password accepted - you are now recognized."
2642 )))
2643 (and ;; quakenet
2644 (string= sender "Q")
2645 (string= target rcirc-nick)
2646 (string-match "\\`You are now logged in as .+\\.\\'" message)))
2647 (setq rcirc-user-authenticated t)
2648 (run-hook-with-args 'rcirc-authenticated-hook process)
2649 (remove-hook 'rcirc-authenticated-hook 'rcirc-join-channels-post-auth t))))))
2651 (defun rcirc-handler-WALLOPS (process sender args _text)
2652 (rcirc-print process sender "WALLOPS" sender (car args) t))
2654 (defun rcirc-handler-JOIN (process sender args _text)
2655 (let ((channel (car args)))
2656 (with-current-buffer (rcirc-get-buffer-create process channel)
2657 ;; when recently rejoining, restore the linestamp
2658 (rcirc-put-nick-channel process sender channel
2659 (let ((last-activity-lines
2660 (rcirc-elapsed-lines process sender channel)))
2661 (when (and last-activity-lines
2662 (< last-activity-lines rcirc-omit-threshold))
2663 (rcirc-last-line process sender channel))))
2664 ;; reset mode-line-process in case joining a channel with an
2665 ;; already open buffer (after getting kicked e.g.)
2666 (setq mode-line-process nil))
2668 (rcirc-print process sender "JOIN" channel "")
2670 ;; print in private chat buffer if it exists
2671 (when (rcirc-get-buffer (rcirc-buffer-process) sender)
2672 (rcirc-print process sender "JOIN" sender channel))))
2674 ;; PART and KICK are handled the same way
2675 (defun rcirc-handler-PART-or-KICK (process _response channel _sender nick _args)
2676 (rcirc-ignore-update-automatic nick)
2677 (if (not (string= nick (rcirc-nick process)))
2678 ;; this is someone else leaving
2679 (progn
2680 (rcirc-maybe-remember-nick-quit process nick channel)
2681 (rcirc-remove-nick-channel process nick channel))
2682 ;; this is us leaving
2683 (mapc (lambda (n)
2684 (rcirc-remove-nick-channel process n channel))
2685 (rcirc-channel-nicks process channel))
2687 ;; if the buffer is still around, make it inactive
2688 (let ((buffer (rcirc-get-buffer process channel)))
2689 (when buffer
2690 (rcirc-disconnect-buffer buffer)))))
2692 (defun rcirc-handler-PART (process sender args _text)
2693 (let* ((channel (car args))
2694 (reason (cadr args))
2695 (message (concat channel " " reason)))
2696 (rcirc-print process sender "PART" channel message)
2697 ;; print in private chat buffer if it exists
2698 (when (rcirc-get-buffer (rcirc-buffer-process) sender)
2699 (rcirc-print process sender "PART" sender message))
2701 (rcirc-handler-PART-or-KICK process "PART" channel sender sender reason)))
2703 (defun rcirc-handler-KICK (process sender args _text)
2704 (let* ((channel (car args))
2705 (nick (cadr args))
2706 (reason (nth 2 args))
2707 (message (concat nick " " channel " " reason)))
2708 (rcirc-print process sender "KICK" channel message t)
2709 ;; print in private chat buffer if it exists
2710 (when (rcirc-get-buffer (rcirc-buffer-process) nick)
2711 (rcirc-print process sender "KICK" nick message))
2713 (rcirc-handler-PART-or-KICK process "KICK" channel sender nick reason)))
2715 (defun rcirc-maybe-remember-nick-quit (process nick channel)
2716 "Remember NICK as leaving CHANNEL if they recently spoke."
2717 (let ((elapsed-lines (rcirc-elapsed-lines process nick channel)))
2718 (when (and elapsed-lines
2719 (< elapsed-lines rcirc-omit-threshold))
2720 (let ((buffer (rcirc-get-buffer process channel)))
2721 (when buffer
2722 (with-current-buffer buffer
2723 (let ((record (assoc-string nick rcirc-recent-quit-alist t))
2724 (line (rcirc-last-line process nick channel)))
2725 (if record
2726 (setcdr record line)
2727 (setq rcirc-recent-quit-alist
2728 (cons (cons nick line)
2729 rcirc-recent-quit-alist))))))))))
2731 (defun rcirc-handler-QUIT (process sender args _text)
2732 (rcirc-ignore-update-automatic sender)
2733 (mapc (lambda (channel)
2734 ;; broadcast quit message each channel
2735 (rcirc-print process sender "QUIT" channel (apply 'concat args))
2736 ;; record nick in quit table if they recently spoke
2737 (rcirc-maybe-remember-nick-quit process sender channel))
2738 (rcirc-nick-channels process sender))
2739 (rcirc-nick-remove process sender))
2741 (defun rcirc-handler-NICK (process sender args _text)
2742 (let* ((old-nick sender)
2743 (new-nick (car args))
2744 (channels (rcirc-nick-channels process old-nick)))
2745 ;; update list of ignored nicks
2746 (rcirc-ignore-update-automatic old-nick)
2747 (when (member old-nick rcirc-ignore-list)
2748 (add-to-list 'rcirc-ignore-list new-nick)
2749 (add-to-list 'rcirc-ignore-list-automatic new-nick))
2750 ;; print message to nick's channels
2751 (dolist (target channels)
2752 (rcirc-print process sender "NICK" target new-nick))
2753 ;; update private chat buffer, if it exists
2754 (let ((chat-buffer (rcirc-get-buffer process old-nick)))
2755 (when chat-buffer
2756 (with-current-buffer chat-buffer
2757 (rcirc-print process sender "NICK" old-nick new-nick)
2758 (setq rcirc-target new-nick)
2759 (rename-buffer (rcirc-generate-new-buffer-name process new-nick)))))
2760 ;; remove old nick and add new one
2761 (with-rcirc-process-buffer process
2762 (let ((v (gethash old-nick rcirc-nick-table)))
2763 (remhash old-nick rcirc-nick-table)
2764 (puthash new-nick v rcirc-nick-table))
2765 ;; if this is our nick...
2766 (when (string= old-nick rcirc-nick)
2767 (setq rcirc-nick new-nick)
2768 (rcirc-update-prompt t)
2769 ;; reauthenticate
2770 (when rcirc-auto-authenticate-flag (rcirc-authenticate))))))
2772 (defun rcirc-handler-PING (process _sender args _text)
2773 (rcirc-send-string process (concat "PONG :" (car args))))
2775 (defun rcirc-handler-PONG (_process _sender _args _text)
2776 ;; do nothing
2779 (defun rcirc-handler-TOPIC (process sender args _text)
2780 (let ((topic (cadr args)))
2781 (rcirc-print process sender "TOPIC" (car args) topic)
2782 (with-current-buffer (rcirc-get-buffer process (car args))
2783 (setq rcirc-topic topic))))
2785 (defvar rcirc-nick-away-alist nil)
2786 (defun rcirc-handler-301 (process _sender args text)
2787 "RPL_AWAY"
2788 (let* ((nick (cadr args))
2789 (rec (assoc-string nick rcirc-nick-away-alist))
2790 (away-message (nth 2 args)))
2791 (when (or (not rec)
2792 (not (string= (cdr rec) away-message)))
2793 ;; away message has changed
2794 (rcirc-handler-generic process "AWAY" nick (cdr args) text)
2795 (if rec
2796 (setcdr rec away-message)
2797 (setq rcirc-nick-away-alist (cons (cons nick away-message)
2798 rcirc-nick-away-alist))))))
2800 (defun rcirc-handler-317 (process sender args _text)
2801 "RPL_WHOISIDLE"
2802 (let* ((nick (nth 1 args))
2803 (idle-secs (string-to-number (nth 2 args)))
2804 (idle-string
2805 (if (< idle-secs most-positive-fixnum)
2806 (format-seconds "%yy %dd %hh %mm %z%ss" idle-secs)
2807 "a very long time"))
2808 (signon-time (seconds-to-time (string-to-number (nth 3 args))))
2809 (signon-string (format-time-string "%c" signon-time))
2810 (message (format "%s idle for %s, signed on %s"
2811 nick idle-string signon-string)))
2812 (rcirc-print process sender "317" nil message t)))
2814 (defun rcirc-handler-332 (process _sender args _text)
2815 "RPL_TOPIC"
2816 (let ((buffer (or (rcirc-get-buffer process (cadr args))
2817 (rcirc-get-temp-buffer-create process (cadr args)))))
2818 (with-current-buffer buffer
2819 (setq rcirc-topic (nth 2 args)))))
2821 (defun rcirc-handler-333 (process sender args _text)
2822 "333 says who set the topic and when.
2823 Not in rfc1459.txt"
2824 (let ((buffer (or (rcirc-get-buffer process (cadr args))
2825 (rcirc-get-temp-buffer-create process (cadr args)))))
2826 (with-current-buffer buffer
2827 (let ((setter (nth 2 args))
2828 (time (current-time-string
2829 (seconds-to-time
2830 (string-to-number (cl-cadddr args))))))
2831 (rcirc-print process sender "TOPIC" (cadr args)
2832 (format "%s (%s on %s)" rcirc-topic setter time))))))
2834 (defun rcirc-handler-477 (process sender args _text)
2835 "ERR_NOCHANMODES"
2836 (rcirc-print process sender "477" (cadr args) (nth 2 args)))
2838 (defun rcirc-handler-MODE (process sender args _text)
2839 (let ((target (car args))
2840 (msg (mapconcat 'identity (cdr args) " ")))
2841 (rcirc-print process sender "MODE"
2842 (if (string= target (rcirc-nick process))
2843 nil
2844 target)
2845 msg)
2847 ;; print in private chat buffers if they exist
2848 (mapc (lambda (nick)
2849 (when (rcirc-get-buffer process nick)
2850 (rcirc-print process sender "MODE" nick msg)))
2851 (cddr args))))
2853 (defun rcirc-get-temp-buffer-create (process channel)
2854 "Return a buffer based on PROCESS and CHANNEL."
2855 (let ((tmpnam (concat " " (downcase channel) "TMP" (process-name process))))
2856 (get-buffer-create tmpnam)))
2858 (defun rcirc-handler-353 (process _sender args _text)
2859 "RPL_NAMREPLY"
2860 (let ((channel (nth 2 args))
2861 (names (or (nth 3 args) "")))
2862 (mapc (lambda (nick)
2863 (rcirc-put-nick-channel process nick channel))
2864 (split-string names " " t))
2865 ;; create a temporary buffer to insert the names into
2866 ;; rcirc-handler-366 (RPL_ENDOFNAMES) will handle it
2867 (with-current-buffer (rcirc-get-temp-buffer-create process channel)
2868 (goto-char (point-max))
2869 (insert (car (last args)) " "))))
2871 (defun rcirc-handler-366 (process sender args _text)
2872 "RPL_ENDOFNAMES"
2873 (let* ((channel (cadr args))
2874 (buffer (rcirc-get-temp-buffer-create process channel)))
2875 (with-current-buffer buffer
2876 (rcirc-print process sender "NAMES" channel
2877 (let ((content (buffer-substring (point-min) (point-max))))
2878 (rcirc-sort-nicknames-join content " "))))
2879 (kill-buffer buffer)))
2881 (defun rcirc-handler-433 (process sender args text)
2882 "ERR_NICKNAMEINUSE"
2883 (rcirc-handler-generic process "433" sender args text)
2884 (let* ((new-nick (concat (cadr args) "`")))
2885 (with-rcirc-process-buffer process
2886 (rcirc-cmd-nick new-nick nil process))))
2888 (defun rcirc-authenticate ()
2889 "Send authentication to process associated with current buffer.
2890 Passwords are stored in `rcirc-authinfo' (which see)."
2891 (interactive)
2892 (with-rcirc-server-buffer
2893 (dolist (i rcirc-authinfo)
2894 (let ((process (rcirc-buffer-process))
2895 (server (car i))
2896 (nick (nth 2 i))
2897 (method (cadr i))
2898 (args (cl-cdddr i)))
2899 (when (and (string-match server rcirc-server))
2900 (if (and (memq method '(nickserv chanserv bitlbee))
2901 (string-match nick rcirc-nick))
2902 ;; the following methods rely on the user's nickname.
2903 (cl-case method
2904 (nickserv
2905 (rcirc-send-privmsg
2906 process
2907 (or (cadr args) "NickServ")
2908 (concat "IDENTIFY " (car args))))
2909 (chanserv
2910 (rcirc-send-privmsg
2911 process
2912 "ChanServ"
2913 (format "IDENTIFY %s %s" (car args) (cadr args))))
2914 (bitlbee
2915 (rcirc-send-privmsg
2916 process
2917 "&bitlbee"
2918 (concat "IDENTIFY " (car args)))))
2919 ;; quakenet authentication doesn't rely on the user's nickname.
2920 ;; the variable `nick' here represents the Q account name.
2921 (when (eq method 'quakenet)
2922 (rcirc-send-privmsg
2923 process
2924 "Q@CServe.quakenet.org"
2925 (format "AUTH %s %s" nick (car args))))))))))
2927 (defun rcirc-handler-INVITE (process sender args _text)
2928 (rcirc-print process sender "INVITE" nil (mapconcat 'identity args " ") t))
2930 (defun rcirc-handler-ERROR (process sender args _text)
2931 (rcirc-print process sender "ERROR" nil (mapconcat 'identity args " ")))
2933 (defun rcirc-handler-CTCP (process target sender text)
2934 (if (string-match "^\\([^ ]+\\) *\\(.*\\)$" text)
2935 (let* ((request (upcase (match-string 1 text)))
2936 (args (match-string 2 text))
2937 (handler (intern-soft (concat "rcirc-handler-ctcp-" request))))
2938 (if (not (fboundp handler))
2939 (rcirc-print process sender "ERROR" target
2940 (format "%s sent unsupported ctcp: %s" sender text)
2941 t)
2942 (funcall handler process target sender args)
2943 (unless (or (string= request "ACTION")
2944 (string= request "KEEPALIVE"))
2945 (rcirc-print process sender "CTCP" target
2946 (format "%s" text) t))))))
2948 (defun rcirc-handler-ctcp-VERSION (process _target sender _args)
2949 (rcirc-send-string process
2950 (concat "NOTICE " sender
2951 " :\C-aVERSION " rcirc-id-string
2952 "\C-a")))
2954 (defun rcirc-handler-ctcp-ACTION (process target sender args)
2955 (rcirc-print process sender "ACTION" target args t))
2957 (defun rcirc-handler-ctcp-TIME (process _target sender _args)
2958 (rcirc-send-string process
2959 (concat "NOTICE " sender
2960 " :\C-aTIME " (current-time-string) "\C-a")))
2962 (defun rcirc-handler-CTCP-response (process _target sender message)
2963 (rcirc-print process sender "CTCP" nil message t))
2964 \f
2965 (defgroup rcirc-faces nil
2966 "Faces for rcirc."
2967 :group 'rcirc
2968 :group 'faces)
2970 (defface rcirc-my-nick ; font-lock-function-name-face
2971 '((((class color) (min-colors 88) (background light)) :foreground "Blue1")
2972 (((class color) (min-colors 88) (background dark)) :foreground "LightSkyBlue")
2973 (((class color) (min-colors 16) (background light)) :foreground "Blue")
2974 (((class color) (min-colors 16) (background dark)) :foreground "LightSkyBlue")
2975 (((class color) (min-colors 8)) :foreground "blue" :weight bold)
2976 (t :inverse-video t :weight bold))
2977 "Rcirc face for my messages."
2978 :group 'rcirc-faces)
2980 (defface rcirc-other-nick ; font-lock-variable-name-face
2981 '((((class grayscale) (background light))
2982 :foreground "Gray90" :weight bold :slant italic)
2983 (((class grayscale) (background dark))
2984 :foreground "DimGray" :weight bold :slant italic)
2985 (((class color) (min-colors 88) (background light)) :foreground "DarkGoldenrod")
2986 (((class color) (min-colors 88) (background dark)) :foreground "LightGoldenrod")
2987 (((class color) (min-colors 16) (background light)) :foreground "DarkGoldenrod")
2988 (((class color) (min-colors 16) (background dark)) :foreground "LightGoldenrod")
2989 (((class color) (min-colors 8)) :foreground "yellow" :weight light)
2990 (t :weight bold :slant italic))
2991 "Rcirc face for other users' messages."
2992 :group 'rcirc-faces)
2994 (defface rcirc-bright-nick
2995 '((((class grayscale) (background light))
2996 :foreground "LightGray" :weight bold :underline t)
2997 (((class grayscale) (background dark))
2998 :foreground "Gray50" :weight bold :underline t)
2999 (((class color) (min-colors 88) (background light)) :foreground "CadetBlue")
3000 (((class color) (min-colors 88) (background dark)) :foreground "Aquamarine")
3001 (((class color) (min-colors 16) (background light)) :foreground "CadetBlue")
3002 (((class color) (min-colors 16) (background dark)) :foreground "Aquamarine")
3003 (((class color) (min-colors 8)) :foreground "magenta")
3004 (t :weight bold :underline t))
3005 "Rcirc face for nicks matched by `rcirc-bright-nicks'."
3006 :group 'rcirc-faces)
3008 (defface rcirc-dim-nick
3009 '((t :inherit default))
3010 "Rcirc face for nicks in `rcirc-dim-nicks'."
3011 :group 'rcirc-faces)
3013 (defface rcirc-server ; font-lock-comment-face
3014 '((((class grayscale) (background light))
3015 :foreground "DimGray" :weight bold :slant italic)
3016 (((class grayscale) (background dark))
3017 :foreground "LightGray" :weight bold :slant italic)
3018 (((class color) (min-colors 88) (background light))
3019 :foreground "Firebrick")
3020 (((class color) (min-colors 88) (background dark))
3021 :foreground "chocolate1")
3022 (((class color) (min-colors 16) (background light))
3023 :foreground "red")
3024 (((class color) (min-colors 16) (background dark))
3025 :foreground "red1")
3026 (((class color) (min-colors 8) (background light)))
3027 (((class color) (min-colors 8) (background dark)))
3028 (t :weight bold :slant italic))
3029 "Rcirc face for server messages."
3030 :group 'rcirc-faces)
3032 (defface rcirc-server-prefix ; font-lock-comment-delimiter-face
3033 '((default :inherit rcirc-server)
3034 (((class grayscale)))
3035 (((class color) (min-colors 16)))
3036 (((class color) (min-colors 8) (background light))
3037 :foreground "red")
3038 (((class color) (min-colors 8) (background dark))
3039 :foreground "red1"))
3040 "Rcirc face for server prefixes."
3041 :group 'rcirc-faces)
3043 (defface rcirc-timestamp
3044 '((t :inherit default))
3045 "Rcirc face for timestamps."
3046 :group 'rcirc-faces)
3048 (defface rcirc-nick-in-message ; font-lock-keyword-face
3049 '((((class grayscale) (background light)) :foreground "LightGray" :weight bold)
3050 (((class grayscale) (background dark)) :foreground "DimGray" :weight bold)
3051 (((class color) (min-colors 88) (background light)) :foreground "Purple")
3052 (((class color) (min-colors 88) (background dark)) :foreground "Cyan1")
3053 (((class color) (min-colors 16) (background light)) :foreground "Purple")
3054 (((class color) (min-colors 16) (background dark)) :foreground "Cyan")
3055 (((class color) (min-colors 8)) :foreground "cyan" :weight bold)
3056 (t :weight bold))
3057 "Rcirc face for instances of your nick within messages."
3058 :group 'rcirc-faces)
3060 (defface rcirc-nick-in-message-full-line '((t :weight bold))
3061 "Rcirc face for emphasizing the entire message when your nick is mentioned."
3062 :group 'rcirc-faces)
3064 (defface rcirc-prompt ; comint-highlight-prompt
3065 '((((min-colors 88) (background dark)) :foreground "cyan1")
3066 (((background dark)) :foreground "cyan")
3067 (t :foreground "dark blue"))
3068 "Rcirc face for prompts."
3069 :group 'rcirc-faces)
3071 (defface rcirc-track-nick
3072 '((((type tty)) :inherit default)
3073 (t :inverse-video t))
3074 "Rcirc face used in the mode-line when your nick is mentioned."
3075 :group 'rcirc-faces)
3077 (defface rcirc-track-keyword '((t :weight bold))
3078 "Rcirc face used in the mode-line when keywords are mentioned."
3079 :group 'rcirc-faces)
3081 (defface rcirc-url '((t :weight bold))
3082 "Rcirc face used to highlight urls."
3083 :group 'rcirc-faces)
3085 (defface rcirc-keyword '((t :inherit highlight))
3086 "Rcirc face used to highlight keywords."
3087 :group 'rcirc-faces)
3089 \f
3090 ;; When using M-x flyspell-mode, only check words after the prompt
3091 (put 'rcirc-mode 'flyspell-mode-predicate 'rcirc-looking-at-input)
3092 (defun rcirc-looking-at-input ()
3093 "Returns true if point is past the input marker."
3094 (>= (point) rcirc-prompt-end-marker))
3095 \f
3097 (provide 'rcirc)
3099 ;;; rcirc.el ends here