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