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