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