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