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