Fix last fix.
[bpt/emacs.git] / lisp / net / rcirc.el
1 ;;; rcirc.el --- default, simple IRC client.
2
3 ;; Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4
5 ;; Author: Ryan Yeske
6 ;; URL: http://www.nongnu.org/rcirc
7 ;; Keywords: comm
8
9 ;; This file is part of GNU Emacs.
10
11 ;; This file is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; Internet Relay Chat (IRC) is a form of instant communication over
29 ;; the Internet. It is mainly designed for group (many-to-many)
30 ;; communication in discussion forums called channels, but also allows
31 ;; one-to-one communication.
32
33 ;; Rcirc has simple defaults and clear and consistent behaviour.
34 ;; Message arrival timestamps, activity notification on the modeline,
35 ;; message filling, nick completion, and keepalive pings are all
36 ;; enabled by default, but can easily be adjusted or turned off. Each
37 ;; discussion takes place in its own buffer and there is a single
38 ;; server buffer per connection.
39
40 ;; Open a new irc connection with:
41 ;; M-x irc RET
42
43 ;;; Todo:
44
45 ;;; Code:
46
47 (require 'ring)
48 (require 'time-date)
49 (eval-when-compile (require 'cl))
50
51 (defgroup rcirc nil
52 "Simple IRC client."
53 :version "22.1"
54 :prefix "rcirc-"
55 :link '(custom-manual "(rcirc)")
56 :group 'applications)
57
58 (defcustom rcirc-default-server "irc.freenode.net"
59 "The default server to connect to."
60 :type 'string
61 :group 'rcirc)
62
63 (defcustom rcirc-default-port 6667
64 "The default port to connect to."
65 :type 'integer
66 :group 'rcirc)
67
68 (defcustom rcirc-default-nick (user-login-name)
69 "Your nick."
70 :type 'string
71 :group 'rcirc)
72
73 (defcustom rcirc-default-user-name (user-login-name)
74 "Your user name sent to the server when connecting."
75 :type 'string
76 :group 'rcirc)
77
78 (defcustom rcirc-default-user-full-name (if (string= (user-full-name) "")
79 rcirc-default-user-name
80 (user-full-name))
81 "The full name sent to the server when connecting."
82 :type 'string
83 :group 'rcirc)
84
85 (defcustom rcirc-startup-channels-alist '(("^irc.freenode.net$" "#rcirc"))
86 "Alist of channels to join at startup.
87 Each element looks like (SERVER-REGEXP . CHANNEL-LIST)."
88 :type '(alist :key-type string :value-type (repeat string))
89 :group 'rcirc)
90
91 (defcustom rcirc-fill-flag t
92 "*Non-nil means line-wrap messages printed in channel buffers."
93 :type 'boolean
94 :group 'rcirc)
95
96 (defcustom rcirc-fill-column nil
97 "*Column beyond which automatic line-wrapping should happen.
98 If nil, use value of `fill-column'. If 'frame-width, use the
99 maximum frame width."
100 :type '(choice (const :tag "Value of `fill-column'")
101 (const :tag "Full frame width" frame-width)
102 (integer :tag "Number of columns"))
103 :group 'rcirc)
104
105 (defcustom rcirc-fill-prefix nil
106 "*Text to insert before filled lines.
107 If nil, calculate the prefix dynamically to line up text
108 underneath each nick."
109 :type '(choice (const :tag "Dynamic" nil)
110 (string :tag "Prefix text"))
111 :group 'rcirc)
112
113 (defvar rcirc-ignore-buffer-activity-flag nil
114 "If non-nil, ignore activity in this buffer.")
115 (make-variable-buffer-local 'rcirc-ignore-buffer-activity-flag)
116
117 (defvar rcirc-low-priority-flag nil
118 "If non-nil, activity in this buffer is considered low priority.")
119 (make-variable-buffer-local 'rcirc-low-priority-flag)
120
121 (defcustom rcirc-time-format "%H:%M "
122 "*Describes how timestamps are printed.
123 Used as the first arg to `format-time-string'."
124 :type 'string
125 :group 'rcirc)
126
127 (defcustom rcirc-input-ring-size 1024
128 "*Size of input history ring."
129 :type 'integer
130 :group 'rcirc)
131
132 (defcustom rcirc-read-only-flag t
133 "*Non-nil means make text in IRC buffers read-only."
134 :type 'boolean
135 :group 'rcirc)
136
137 (defcustom rcirc-buffer-maximum-lines nil
138 "*The maximum size in lines for rcirc buffers.
139 Channel buffers are truncated from the top to be no greater than this
140 number. If zero or nil, no truncating is done."
141 :type '(choice (const :tag "No truncation" nil)
142 (integer :tag "Number of lines"))
143 :group 'rcirc)
144
145 (defcustom rcirc-scroll-show-maximum-output t
146 "*If non-nil, scroll buffer to keep the point at the bottom of
147 the window."
148 :type 'boolean
149 :group 'rcirc)
150
151 (defcustom rcirc-authinfo nil
152 "List of authentication passwords.
153 Each element of the list is a list with a SERVER-REGEXP string
154 and a method symbol followed by method specific arguments.
155
156 The valid METHOD symbols are `nickserv', `chanserv' and
157 `bitlbee'.
158
159 The required ARGUMENTS for each METHOD symbol are:
160 `nickserv': NICK PASSWORD
161 `chanserv': NICK CHANNEL PASSWORD
162 `bitlbee': NICK PASSWORD
163
164 Example:
165 ((\"freenode\" nickserv \"bob\" \"p455w0rd\")
166 (\"freenode\" chanserv \"bob\" \"#bobland\" \"passwd99\")
167 (\"bitlbee\" bitlbee \"robert\" \"sekrit\"))"
168 :type '(alist :key-type (string :tag "Server")
169 :value-type (choice (list :tag "NickServ"
170 (const nickserv)
171 (string :tag "Nick")
172 (string :tag "Password"))
173 (list :tag "ChanServ"
174 (const chanserv)
175 (string :tag "Nick")
176 (string :tag "Channel")
177 (string :tag "Password"))
178 (list :tag "BitlBee"
179 (const bitlbee)
180 (string :tag "Nick")
181 (string :tag "Password"))))
182 :group 'rcirc)
183
184 (defcustom rcirc-auto-authenticate-flag t
185 "*Non-nil means automatically send authentication string to server.
186 See also `rcirc-authinfo'."
187 :type 'boolean
188 :group 'rcirc)
189
190 (defcustom rcirc-prompt "> "
191 "Prompt string to use in IRC buffers.
192
193 The following replacements are made:
194 %n is your nick.
195 %s is the server.
196 %t is the buffer target, a channel or a user.
197
198 Setting this alone will not affect the prompt;
199 use either M-x customize or also call `rcirc-update-prompt'."
200 :type 'string
201 :set 'rcirc-set-changed
202 :initialize 'custom-initialize-default
203 :group 'rcirc)
204
205 (defcustom rcirc-keywords nil
206 "List of keywords to highlight in message text."
207 :type '(repeat string)
208 :group 'rcirc)
209
210 (defcustom rcirc-ignore-list ()
211 "List of ignored nicks.
212 Use /ignore to list them, use /ignore NICK to add or remove a nick."
213 :type '(repeat string)
214 :group 'rcirc)
215
216 (defvar rcirc-ignore-list-automatic ()
217 "List of ignored nicks added to `rcirc-ignore-list' because of renaming.
218 When an ignored person renames, their nick is added to both lists.
219 Nicks will be removed from the automatic list on follow-up renamings or
220 parts.")
221
222 (defcustom rcirc-bright-nicks nil
223 "List of nicks to be emphasized.
224 See `rcirc-bright-nick' face."
225 :type '(repeat string)
226 :group 'rcirc)
227
228 (defcustom rcirc-dim-nicks nil
229 "List of nicks to be deemphasized.
230 See `rcirc-dim-nick' face."
231 :type '(repeat string)
232 :group 'rcirc)
233
234 (defcustom rcirc-print-hooks nil
235 "Hook run after text is printed.
236 Called with 5 arguments, PROCESS, SENDER, RESPONSE, TARGET and TEXT."
237 :type 'hook
238 :group 'rcirc)
239
240 (defcustom rcirc-always-use-server-buffer-flag nil
241 "Non-nil means messages without a channel target will go to the server buffer."
242 :type 'boolean
243 :group 'rcirc)
244
245 (defcustom rcirc-decode-coding-system 'utf-8
246 "Coding system used to decode incoming irc messages."
247 :type 'coding-system
248 :group 'rcirc)
249
250 (defcustom rcirc-encode-coding-system 'utf-8
251 "Coding system used to encode outgoing irc messages."
252 :type 'coding-system
253 :group 'rcirc)
254
255 (defcustom rcirc-coding-system-alist nil
256 "Alist to decide a coding system to use for a channel I/O operation.
257 The format is ((PATTERN . VAL) ...).
258 PATTERN is either a string or a cons of strings.
259 If PATTERN is a string, it is used to match a target.
260 If PATTERN is a cons of strings, the car part is used to match a
261 target, and the cdr part is used to match a server.
262 VAL is either a coding system or a cons of coding systems.
263 If VAL is a coding system, it is used for both decoding and encoding
264 messages.
265 If VAL is a cons of coding systems, the car part is used for decoding,
266 and the cdr part is used for encoding."
267 :type '(alist :key-type (choice (string :tag "Channel Regexp")
268 (cons (string :tag "Channel Regexp")
269 (string :tag "Server Regexp")))
270 :value-type (choice coding-system
271 (cons (coding-system :tag "Decode")
272 (coding-system :tag "Encode"))))
273 :group 'rcirc)
274
275 (defcustom rcirc-multiline-major-mode 'fundamental-mode
276 "Major-mode function to use in multiline edit buffers."
277 :type 'function
278 :group 'rcirc)
279
280 (defvar rcirc-nick nil)
281
282 (defvar rcirc-prompt-start-marker nil)
283 (defvar rcirc-prompt-end-marker nil)
284
285 (defvar rcirc-nick-table nil)
286
287 (defvar rcirc-nick-syntax-table
288 (let ((table (make-syntax-table text-mode-syntax-table)))
289 (mapc (lambda (c) (modify-syntax-entry c "w" table))
290 "[]\\`_^{|}-")
291 (modify-syntax-entry ?' "_" table)
292 table)
293 "Syntax table which includes all nick characters as word constituents.")
294
295 ;; each process has an alist of (target . buffer) pairs
296 (defvar rcirc-buffer-alist nil)
297
298 (defvar rcirc-activity nil
299 "List of buffers with unviewed activity.")
300
301 (defvar rcirc-activity-string ""
302 "String displayed in modeline representing `rcirc-activity'.")
303 (put 'rcirc-activity-string 'risky-local-variable t)
304
305 (defvar rcirc-server-buffer nil
306 "The server buffer associated with this channel buffer.")
307
308 (defvar rcirc-target nil
309 "The channel or user associated with this buffer.")
310
311 (defvar rcirc-urls nil
312 "List of urls seen in the current buffer.")
313 (put 'rcirc-urls 'permanent-local t)
314
315 (defvar rcirc-timeout-seconds 60
316 "Kill connection after this many seconds if there is no activity.")
317
318 (defconst rcirc-id-string (concat "rcirc on GNU Emacs " emacs-version))
319 \f
320 (defvar rcirc-startup-channels nil)
321 ;;;###autoload
322 (defun rcirc (arg)
323 "Connect to IRC.
324 If ARG is non-nil, prompt for a server to connect to."
325 (interactive "P")
326 (if arg
327 (let* ((server (read-string "IRC Server: " rcirc-default-server))
328 (port (read-string "IRC Port: " (number-to-string rcirc-default-port)))
329 (nick (read-string "IRC Nick: " rcirc-default-nick))
330 (channels (split-string
331 (read-string "IRC Channels: "
332 (mapconcat 'identity (rcirc-startup-channels server) " "))
333 "[, ]+" t)))
334 (rcirc-connect server port nick rcirc-default-user-name rcirc-default-user-full-name
335 channels))
336 ;; make new connection using defaults unless already connected to
337 ;; the default rcirc-server
338 (let (connected)
339 (dolist (p (rcirc-process-list))
340 (when (string= rcirc-default-server (process-name p))
341 (setq connected p)))
342 (if (not connected)
343 (rcirc-connect rcirc-default-server rcirc-default-port
344 rcirc-default-nick rcirc-default-user-name
345 rcirc-default-user-full-name
346 (rcirc-startup-channels rcirc-default-server))
347 (switch-to-buffer (process-buffer connected))
348 (message "Connected to %s"
349 (process-contact (get-buffer-process (current-buffer))
350 :host))))))
351 ;;;###autoload
352 (defalias 'irc 'rcirc)
353
354 \f
355 (defvar rcirc-process-output nil)
356 (defvar rcirc-topic nil)
357 (defvar rcirc-keepalive-timer nil)
358 (defvar rcirc-last-server-message-time nil)
359 (defvar rcirc-server nil) ; server provided by server
360 (defvar rcirc-server-name nil) ; server name given by 001 response
361 (defvar rcirc-timeout-timer nil)
362 (defvar rcirc-user-disconnect nil)
363 (defvar rcirc-connecting nil)
364 (defvar rcirc-process nil)
365
366 ;;;###autoload
367 (defun rcirc-connect (&optional server port nick user-name full-name startup-channels)
368 (save-excursion
369 (message "Connecting to %s..." server)
370 (let* ((inhibit-eol-conversion)
371 (port-number (if port
372 (if (stringp port)
373 (string-to-number port)
374 port)
375 rcirc-default-port))
376 (server (or server rcirc-default-server))
377 (nick (or nick rcirc-default-nick))
378 (user-name (or user-name rcirc-default-user-name))
379 (full-name (or full-name rcirc-default-user-full-name))
380 (startup-channels startup-channels)
381 (process (make-network-process :name server :host server :service port-number)))
382 ;; set up process
383 (set-process-coding-system process 'raw-text 'raw-text)
384 (switch-to-buffer (rcirc-generate-new-buffer-name process nil))
385 (set-process-buffer process (current-buffer))
386 (rcirc-mode process nil)
387 (set-process-sentinel process 'rcirc-sentinel)
388 (set-process-filter process 'rcirc-filter)
389 (make-local-variable 'rcirc-process)
390 (setq rcirc-process process)
391 (make-local-variable 'rcirc-server)
392 (setq rcirc-server server)
393 (make-local-variable 'rcirc-server-name)
394 (setq rcirc-server-name server) ; update when we get 001 response
395 (make-local-variable 'rcirc-buffer-alist)
396 (setq rcirc-buffer-alist nil)
397 (make-local-variable 'rcirc-nick-table)
398 (setq rcirc-nick-table (make-hash-table :test 'equal))
399 (make-local-variable 'rcirc-nick)
400 (setq rcirc-nick nick)
401 (make-local-variable 'rcirc-process-output)
402 (setq rcirc-process-output nil)
403 (make-local-variable 'rcirc-startup-channels)
404 (setq rcirc-startup-channels startup-channels)
405 (make-local-variable 'rcirc-last-server-message-time)
406 (setq rcirc-last-server-message-time (current-time))
407 (make-local-variable 'rcirc-timeout-timer)
408 (setq rcirc-timeout-timer nil)
409 (make-local-variable 'rcirc-user-disconnect)
410 (setq rcirc-user-disconnect nil)
411 (make-local-variable 'rcirc-connecting)
412 (setq rcirc-connecting t)
413
414 ;; identify
415 (rcirc-send-string process (concat "NICK " nick))
416 (rcirc-send-string process (concat "USER " user-name
417 " hostname servername :"
418 full-name))
419
420 ;; setup ping timer if necessary
421 (unless rcirc-keepalive-timer
422 (setq rcirc-keepalive-timer
423 (run-at-time 0 (/ rcirc-timeout-seconds 2) 'rcirc-keepalive)))
424
425 (message "Connecting to %s...done" server)
426
427 ;; return process object
428 process)))
429
430 (defmacro with-rcirc-process-buffer (process &rest body)
431 (declare (indent 1) (debug t))
432 `(with-current-buffer (process-buffer ,process)
433 ,@body))
434
435 (defmacro with-rcirc-server-buffer (&rest body)
436 (declare (indent 0) (debug t))
437 `(with-current-buffer rcirc-server-buffer
438 ,@body))
439
440 (defun rcirc-keepalive ()
441 "Send keep alive pings to active rcirc processes.
442 Kill processes that have not received a server message since the
443 last ping."
444 (if (rcirc-process-list)
445 (mapc (lambda (process)
446 (with-rcirc-process-buffer process
447 (when (not rcirc-connecting)
448 (rcirc-send-string process (concat "PING " (rcirc-server-name process))))))
449 (rcirc-process-list))
450 ;; no processes, clean up timer
451 (cancel-timer rcirc-keepalive-timer)
452 (setq rcirc-keepalive-timer nil)))
453
454 (defvar rcirc-debug-buffer " *rcirc debug*")
455 (defvar rcirc-debug-flag nil
456 "If non-nil, write information to `rcirc-debug-buffer'.")
457 (defun rcirc-debug (process text)
458 "Add an entry to the debug log including PROCESS and TEXT.
459 Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag'
460 is non-nil."
461 (when rcirc-debug-flag
462 (save-excursion
463 (save-window-excursion
464 (set-buffer (get-buffer-create rcirc-debug-buffer))
465 (goto-char (point-max))
466 (insert (concat
467 "["
468 (format-time-string "%Y-%m-%dT%T ") (process-name process)
469 "] "
470 text))))))
471
472 (defvar rcirc-sentinel-hooks nil
473 "Hook functions called when the process sentinel is called.
474 Functions are called with PROCESS and SENTINEL arguments.")
475
476 (defun rcirc-sentinel (process sentinel)
477 "Called when PROCESS receives SENTINEL."
478 (let ((sentinel (replace-regexp-in-string "\n" "" sentinel)))
479 (rcirc-debug process (format "SENTINEL: %S %S\n" process sentinel))
480 (with-rcirc-process-buffer process
481 (dolist (buffer (cons nil (mapcar 'cdr rcirc-buffer-alist)))
482 (with-current-buffer (or buffer (current-buffer))
483 (rcirc-print process "rcirc.el" "ERROR" rcirc-target
484 (format "%s: %s (%S)"
485 (process-name process)
486 sentinel
487 (process-status process)) (not rcirc-target))
488 ;; remove the prompt from buffers
489 (let ((inhibit-read-only t))
490 (delete-region rcirc-prompt-start-marker
491 rcirc-prompt-end-marker))))
492 (run-hook-with-args 'rcirc-sentinel-hooks process sentinel))))
493
494 (defun rcirc-process-list ()
495 "Return a list of rcirc processes."
496 (let (ps)
497 (mapc (lambda (p)
498 (when (buffer-live-p (process-buffer p))
499 (with-rcirc-process-buffer p
500 (when (eq major-mode 'rcirc-mode)
501 (setq ps (cons p ps))))))
502 (process-list))
503 ps))
504
505 (defvar rcirc-receive-message-hooks nil
506 "Hook functions run when a message is received from server.
507 Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.")
508 (defun rcirc-filter (process output)
509 "Called when PROCESS receives OUTPUT."
510 (rcirc-debug process output)
511 (rcirc-reschedule-timeout process)
512 (with-rcirc-process-buffer process
513 (setq rcirc-last-server-message-time (current-time))
514 (setq rcirc-process-output (concat rcirc-process-output output))
515 (when (= (aref rcirc-process-output
516 (1- (length rcirc-process-output))) ?\n)
517 (mapc (lambda (line)
518 (rcirc-process-server-response process line))
519 (split-string rcirc-process-output "[\n\r]" t))
520 (setq rcirc-process-output nil))))
521
522 (defun rcirc-reschedule-timeout (process)
523 (with-rcirc-process-buffer process
524 (when (not rcirc-connecting)
525 (with-rcirc-process-buffer process
526 (when rcirc-timeout-timer (cancel-timer rcirc-timeout-timer))
527 (setq rcirc-timeout-timer (run-at-time rcirc-timeout-seconds nil
528 'rcirc-delete-process
529 process))))))
530
531 (defun rcirc-delete-process (process)
532 (message "delete process %S" process)
533 (delete-process process))
534
535 (defvar rcirc-trap-errors-flag t)
536 (defun rcirc-process-server-response (process text)
537 (if rcirc-trap-errors-flag
538 (condition-case err
539 (rcirc-process-server-response-1 process text)
540 (error
541 (rcirc-print process "RCIRC" "ERROR" nil
542 (format "\"%s\" %s" text err) t)))
543 (rcirc-process-server-response-1 process text)))
544
545 (defun rcirc-process-server-response-1 (process text)
546 (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text)
547 (let* ((user (match-string 2 text))
548 (sender (rcirc-user-nick user))
549 (cmd (match-string 3 text))
550 (args (match-string 4 text))
551 (handler (intern-soft (concat "rcirc-handler-" cmd))))
552 (string-match "^\\([^:]*\\):?\\(.+\\)?$" args)
553 (let* ((args1 (match-string 1 args))
554 (args2 (match-string 2 args))
555 (args (delq nil (append (split-string args1 " " t)
556 (list args2)))))
557 (if (not (fboundp handler))
558 (rcirc-handler-generic process cmd sender args text)
559 (funcall handler process sender args text))
560 (run-hook-with-args 'rcirc-receive-message-hooks
561 process cmd sender args text)))
562 (message "UNHANDLED: %s" text)))
563
564 (defvar rcirc-responses-no-activity '("305" "306")
565 "Responses that don't trigger activity in the mode-line indicator.")
566
567 (defun rcirc-handler-generic (process response sender args text)
568 "Generic server response handler."
569 (rcirc-print process sender response nil
570 (mapconcat 'identity (cdr args) " ")
571 (not (member response rcirc-responses-no-activity))))
572
573 (defun rcirc-send-string (process string)
574 "Send PROCESS a STRING plus a newline."
575 (let ((string (concat (encode-coding-string string rcirc-encode-coding-system)
576 "\n")))
577 (unless (eq (process-status process) 'open)
578 (error "Network connection to %s is not open"
579 (process-name process)))
580 (rcirc-debug process string)
581 (process-send-string process string)))
582
583 (defun rcirc-buffer-process (&optional buffer)
584 "Return the process associated with channel BUFFER.
585 With no argument or nil as argument, use the current buffer."
586 (or (get-buffer-process (if buffer
587 (with-current-buffer buffer
588 rcirc-server-buffer)
589 rcirc-server-buffer))
590 rcirc-process))
591
592 (defun rcirc-server-name (process)
593 "Return PROCESS server name, given by the 001 response."
594 (with-rcirc-process-buffer process
595 (or rcirc-server-name rcirc-default-server)))
596
597 (defun rcirc-nick (process)
598 "Return PROCESS nick."
599 (with-rcirc-process-buffer process
600 (or rcirc-nick rcirc-default-nick)))
601
602 (defun rcirc-buffer-nick (&optional buffer)
603 "Return the nick associated with BUFFER.
604 With no argument or nil as argument, use the current buffer."
605 (with-current-buffer (or buffer (current-buffer))
606 (with-current-buffer rcirc-server-buffer
607 (or rcirc-nick rcirc-default-nick))))
608
609 (defvar rcirc-max-message-length 420
610 "Messages longer than this value will be split.")
611
612 (defun rcirc-send-message (process target message &optional noticep)
613 "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
614 If NOTICEP is non-nil, send a notice instead of privmsg."
615 ;; max message length is 512 including CRLF
616 (let* ((response (if noticep "NOTICE" "PRIVMSG"))
617 (oversize (> (length message) rcirc-max-message-length))
618 (text (if oversize
619 (substring message 0 rcirc-max-message-length)
620 message))
621 (text (if (string= text "")
622 " "
623 text))
624 (more (if oversize
625 (substring message rcirc-max-message-length))))
626 (rcirc-get-buffer-create process target)
627 (rcirc-print process (rcirc-nick process) response target text)
628 (rcirc-send-string process (concat response " " target " :" text))
629 (when more (rcirc-send-message process target more noticep))))
630
631 (defvar rcirc-input-ring nil)
632 (defvar rcirc-input-ring-index 0)
633 (defun rcirc-prev-input-string (arg)
634 (ring-ref rcirc-input-ring (+ rcirc-input-ring-index arg)))
635
636 (defun rcirc-insert-prev-input (arg)
637 (interactive "p")
638 (when (<= rcirc-prompt-end-marker (point))
639 (delete-region rcirc-prompt-end-marker (point-max))
640 (insert (rcirc-prev-input-string 0))
641 (setq rcirc-input-ring-index (1+ rcirc-input-ring-index))))
642
643 (defun rcirc-insert-next-input (arg)
644 (interactive "p")
645 (when (<= rcirc-prompt-end-marker (point))
646 (delete-region rcirc-prompt-end-marker (point-max))
647 (setq rcirc-input-ring-index (1- rcirc-input-ring-index))
648 (insert (rcirc-prev-input-string -1))))
649
650 (defvar rcirc-nick-completions nil)
651 (defvar rcirc-nick-completion-start-offset nil)
652
653 (defun rcirc-complete-nick ()
654 "Cycle through nick completions from list of nicks in channel."
655 (interactive)
656 (if (eq last-command this-command)
657 (setq rcirc-nick-completions
658 (append (cdr rcirc-nick-completions)
659 (list (car rcirc-nick-completions))))
660 (setq rcirc-nick-completion-start-offset
661 (- (save-excursion
662 (if (re-search-backward " " rcirc-prompt-end-marker t)
663 (1+ (point))
664 rcirc-prompt-end-marker))
665 rcirc-prompt-end-marker))
666 (setq rcirc-nick-completions
667 (let ((completion-ignore-case t))
668 (all-completions
669 (buffer-substring
670 (+ rcirc-prompt-end-marker
671 rcirc-nick-completion-start-offset)
672 (point))
673 (mapcar (lambda (x) (cons x nil))
674 (rcirc-channel-nicks (rcirc-buffer-process)
675 rcirc-target))))))
676 (let ((completion (car rcirc-nick-completions)))
677 (when completion
678 (rcirc-put-nick-channel (rcirc-buffer-process) completion rcirc-target)
679 (delete-region (+ rcirc-prompt-end-marker
680 rcirc-nick-completion-start-offset)
681 (point))
682 (insert (concat completion
683 (if (= (+ rcirc-prompt-end-marker
684 rcirc-nick-completion-start-offset)
685 rcirc-prompt-end-marker)
686 ": "))))))
687
688 (defun set-rcirc-decode-coding-system (coding-system)
689 "Set the decode coding system used in this channel."
690 (interactive "zCoding system for incoming messages: ")
691 (setq rcirc-decode-coding-system coding-system))
692
693 (defun set-rcirc-encode-coding-system (coding-system)
694 "Set the encode coding system used in this channel."
695 (interactive "zCoding system for outgoing messages: ")
696 (setq rcirc-encode-coding-system coding-system))
697
698 (defvar rcirc-mode-map (make-sparse-keymap)
699 "Keymap for rcirc mode.")
700
701 (define-key rcirc-mode-map (kbd "RET") 'rcirc-send-input)
702 (define-key rcirc-mode-map (kbd "M-p") 'rcirc-insert-prev-input)
703 (define-key rcirc-mode-map (kbd "M-n") 'rcirc-insert-next-input)
704 (define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete-nick)
705 (define-key rcirc-mode-map (kbd "C-c C-b") 'rcirc-browse-url)
706 (define-key rcirc-mode-map (kbd "C-c C-c") 'rcirc-edit-multiline)
707 (define-key rcirc-mode-map (kbd "C-c C-j") 'rcirc-cmd-join)
708 (define-key rcirc-mode-map (kbd "C-c C-k") 'rcirc-cmd-kick)
709 (define-key rcirc-mode-map (kbd "C-c C-l") 'rcirc-toggle-low-priority)
710 (define-key rcirc-mode-map (kbd "C-c C-d") 'rcirc-cmd-mode)
711 (define-key rcirc-mode-map (kbd "C-c C-m") 'rcirc-cmd-msg)
712 (define-key rcirc-mode-map (kbd "C-c C-r") 'rcirc-cmd-nick) ; rename
713 (define-key rcirc-mode-map (kbd "C-c C-o") 'rcirc-cmd-oper)
714 (define-key rcirc-mode-map (kbd "C-c C-p") 'rcirc-cmd-part)
715 (define-key rcirc-mode-map (kbd "C-c C-q") 'rcirc-cmd-query)
716 (define-key rcirc-mode-map (kbd "C-c C-t") 'rcirc-cmd-topic)
717 (define-key rcirc-mode-map (kbd "C-c C-n") 'rcirc-cmd-names)
718 (define-key rcirc-mode-map (kbd "C-c C-w") 'rcirc-cmd-whois)
719 (define-key rcirc-mode-map (kbd "C-c C-x") 'rcirc-cmd-quit)
720 (define-key rcirc-mode-map (kbd "C-c TAB") ; C-i
721 'rcirc-toggle-ignore-buffer-activity)
722 (define-key rcirc-mode-map (kbd "C-c C-s") 'rcirc-switch-to-server-buffer)
723 (define-key rcirc-mode-map (kbd "C-c C-a") 'rcirc-jump-to-first-unread-line)
724
725 (defvar rcirc-browse-url-map (make-sparse-keymap)
726 "Keymap used for browsing URLs in `rcirc-mode'.")
727
728 (define-key rcirc-browse-url-map (kbd "RET") 'rcirc-browse-url-at-point)
729 (define-key rcirc-browse-url-map (kbd "<mouse-2>") 'rcirc-browse-url-at-mouse)
730
731 (defvar rcirc-short-buffer-name nil
732 "Generated abbreviation to use to indicate buffer activity.")
733
734 (defvar rcirc-mode-hook nil
735 "Hook run when setting up rcirc buffer.")
736
737 (defvar rcirc-last-post-time nil)
738
739 (defun rcirc-mode (process target)
740 "Major mode for IRC channel buffers.
741
742 \\{rcirc-mode-map}"
743 (kill-all-local-variables)
744 (use-local-map rcirc-mode-map)
745 (setq mode-name "rcirc")
746 (setq major-mode 'rcirc-mode)
747
748 (make-local-variable 'rcirc-input-ring)
749 (setq rcirc-input-ring (make-ring rcirc-input-ring-size))
750 (make-local-variable 'rcirc-server-buffer)
751 (setq rcirc-server-buffer (process-buffer process))
752 (make-local-variable 'rcirc-target)
753 (setq rcirc-target target)
754 (make-local-variable 'rcirc-topic)
755 (setq rcirc-topic nil)
756 (make-local-variable 'rcirc-last-post-time)
757 (setq rcirc-last-post-time (current-time))
758
759 (make-local-variable 'rcirc-short-buffer-name)
760 (setq rcirc-short-buffer-name nil)
761 (make-local-variable 'rcirc-urls)
762 (setq use-hard-newlines t)
763
764 (make-local-variable 'rcirc-decode-coding-system)
765 (make-local-variable 'rcirc-encode-coding-system)
766 (dolist (i rcirc-coding-system-alist)
767 (let ((chan (if (consp (car i)) (caar i) (car i)))
768 (serv (if (consp (car i)) (cdar i) "")))
769 (when (and (string-match chan (or target ""))
770 (string-match serv (rcirc-server-name process)))
771 (setq rcirc-decode-coding-system (if (consp (cdr i)) (cadr i) (cdr i))
772 rcirc-encode-coding-system (if (consp (cdr i)) (cddr i) (cdr i))))))
773
774 ;; setup the prompt and markers
775 (make-local-variable 'rcirc-prompt-start-marker)
776 (setq rcirc-prompt-start-marker (make-marker))
777 (set-marker rcirc-prompt-start-marker (point-max))
778 (make-local-variable 'rcirc-prompt-end-marker)
779 (setq rcirc-prompt-end-marker (make-marker))
780 (set-marker rcirc-prompt-end-marker (point-max))
781 (rcirc-update-prompt)
782 (goto-char rcirc-prompt-end-marker)
783 (make-local-variable 'overlay-arrow-position)
784 (setq overlay-arrow-position (make-marker))
785 (set-marker overlay-arrow-position nil)
786
787 ;; if the user changes the major mode or kills the buffer, there is
788 ;; cleanup work to do
789 (add-hook 'change-major-mode-hook 'rcirc-change-major-mode-hook nil t)
790 (add-hook 'kill-buffer-hook 'rcirc-kill-buffer-hook nil t)
791
792 ;; add to buffer list, and update buffer abbrevs
793 (when target ; skip server buffer
794 (let ((buffer (current-buffer)))
795 (with-rcirc-process-buffer process
796 (setq rcirc-buffer-alist (cons (cons target buffer)
797 rcirc-buffer-alist))))
798 (rcirc-update-short-buffer-names))
799
800 (run-hooks 'rcirc-mode-hook))
801
802 (defun rcirc-update-prompt (&optional all)
803 "Reset the prompt string in the current buffer.
804
805 If ALL is non-nil, update prompts in all IRC buffers."
806 (if all
807 (mapc (lambda (process)
808 (mapc (lambda (buffer)
809 (with-current-buffer buffer
810 (rcirc-update-prompt)))
811 (with-rcirc-process-buffer process
812 (mapcar 'cdr rcirc-buffer-alist))))
813 (rcirc-process-list))
814 (let ((inhibit-read-only t)
815 (prompt (or rcirc-prompt "")))
816 (mapc (lambda (rep)
817 (setq prompt
818 (replace-regexp-in-string (car rep) (cdr rep) prompt)))
819 (list (cons "%n" (rcirc-buffer-nick))
820 (cons "%s" (with-rcirc-server-buffer rcirc-server-name))
821 (cons "%t" (or rcirc-target ""))))
822 (save-excursion
823 (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker)
824 (goto-char rcirc-prompt-start-marker)
825 (let ((start (point)))
826 (insert-before-markers prompt)
827 (set-marker rcirc-prompt-start-marker start)
828 (when (not (zerop (- rcirc-prompt-end-marker
829 rcirc-prompt-start-marker)))
830 (add-text-properties rcirc-prompt-start-marker
831 rcirc-prompt-end-marker
832 (list 'face 'rcirc-prompt
833 'read-only t 'field t
834 'front-sticky t 'rear-nonsticky t))))))))
835
836 (defun rcirc-set-changed (option value)
837 "Set OPTION to VALUE and do updates after a customization change."
838 (set-default option value)
839 (cond ((eq option 'rcirc-prompt)
840 (rcirc-update-prompt 'all))
841 (t
842 (error "Bad option %s" option))))
843
844 (defun rcirc-channel-p (target)
845 "Return t if TARGET is a channel name."
846 (and target
847 (not (zerop (length target)))
848 (or (eq (aref target 0) ?#)
849 (eq (aref target 0) ?&))))
850
851 (defun rcirc-kill-buffer-hook ()
852 "Part the channel when killing an rcirc buffer."
853 (when (eq major-mode 'rcirc-mode)
854 (rcirc-clean-up-buffer "Killed buffer")))
855
856 (defun rcirc-change-major-mode-hook ()
857 "Part the channel when changing the major-mode."
858 (rcirc-clean-up-buffer "Changed major mode"))
859
860 (defun rcirc-clean-up-buffer (reason)
861 (let ((buffer (current-buffer)))
862 (rcirc-clear-activity buffer)
863 (when (and (rcirc-buffer-process)
864 (eq (process-status (rcirc-buffer-process)) 'open))
865 (with-rcirc-server-buffer
866 (setq rcirc-buffer-alist
867 (rassq-delete-all buffer rcirc-buffer-alist)))
868 (rcirc-update-short-buffer-names)
869 (if (rcirc-channel-p rcirc-target)
870 (rcirc-send-string (rcirc-buffer-process)
871 (concat "PART " rcirc-target " :" reason))
872 (when rcirc-target
873 (rcirc-remove-nick-channel (rcirc-buffer-process)
874 (rcirc-buffer-nick)
875 rcirc-target))))))
876
877 (defun rcirc-generate-new-buffer-name (process target)
878 "Return a buffer name based on PROCESS and TARGET.
879 This is used for the initial name given to IRC buffers."
880 (if target
881 (concat target "@" (process-name process))
882 (concat "*" (process-name process) "*")))
883
884 (defun rcirc-get-buffer (process target &optional server)
885 "Return the buffer associated with the PROCESS and TARGET.
886
887 If optional argument SERVER is non-nil, return the server buffer
888 if there is no existing buffer for TARGET, otherwise return nil."
889 (with-rcirc-process-buffer process
890 (if (null target)
891 (current-buffer)
892 (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t))))
893 (or buffer (when server (current-buffer)))))))
894
895 (defun rcirc-get-buffer-create (process target)
896 "Return the buffer associated with the PROCESS and TARGET.
897 Create the buffer if it doesn't exist."
898 (let ((buffer (rcirc-get-buffer process target)))
899 (if (and buffer (buffer-live-p buffer))
900 (with-current-buffer buffer
901 (when (not rcirc-target)
902 (setq rcirc-target target))
903 buffer)
904 ;; create the buffer
905 (with-rcirc-process-buffer process
906 (let ((new-buffer (get-buffer-create
907 (rcirc-generate-new-buffer-name process target))))
908 (with-current-buffer new-buffer
909 (rcirc-mode process target))
910 (rcirc-put-nick-channel process (rcirc-nick process) target)
911 new-buffer)))))
912
913 (defun rcirc-send-input ()
914 "Send input to target associated with the current buffer."
915 (interactive)
916 (if (< (point) rcirc-prompt-end-marker)
917 ;; copy the line down to the input area
918 (progn
919 (forward-line 0)
920 (let ((start (if (eq (point) (point-min))
921 (point)
922 (if (get-text-property (1- (point)) 'hard)
923 (point)
924 (previous-single-property-change (point) 'hard))))
925 (end (next-single-property-change (1+ (point)) 'hard)))
926 (goto-char (point-max))
927 (insert (replace-regexp-in-string
928 "\n\\s-+" " "
929 (buffer-substring-no-properties start end)))))
930 ;; process input
931 (goto-char (point-max))
932 (when (not (equal 0 (- (point) rcirc-prompt-end-marker)))
933 ;; delete a trailing newline
934 (when (eq (point) (point-at-bol))
935 (delete-backward-char 1))
936 (let ((input (buffer-substring-no-properties
937 rcirc-prompt-end-marker (point))))
938 (dolist (line (split-string input "\n"))
939 (rcirc-process-input-line line))
940 ;; add to input-ring
941 (save-excursion
942 (ring-insert rcirc-input-ring input)
943 (setq rcirc-input-ring-index 0))))))
944
945 (defun rcirc-process-input-line (line)
946 (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line)
947 (rcirc-process-command (match-string 1 line)
948 (match-string 2 line)
949 line)
950 (rcirc-process-message line)))
951
952 (defun rcirc-process-message (line)
953 (if (not rcirc-target)
954 (message "Not joined (no target)")
955 (delete-region rcirc-prompt-end-marker (point))
956 (rcirc-send-message (rcirc-buffer-process) rcirc-target line)
957 (setq rcirc-last-post-time (current-time))))
958
959 (defun rcirc-process-command (command args line)
960 (if (eq (aref command 0) ?/)
961 ;; "//text" will send "/text" as a message
962 (rcirc-process-message (substring line 1))
963 (let ((fun (intern-soft (concat "rcirc-cmd-" command)))
964 (process (rcirc-buffer-process)))
965 (newline)
966 (with-current-buffer (current-buffer)
967 (delete-region rcirc-prompt-end-marker (point))
968 (if (string= command "me")
969 (rcirc-print process (rcirc-buffer-nick)
970 "ACTION" rcirc-target args)
971 (rcirc-print process (rcirc-buffer-nick)
972 "COMMAND" rcirc-target line))
973 (set-marker rcirc-prompt-end-marker (point))
974 (if (fboundp fun)
975 (funcall fun args process rcirc-target)
976 (rcirc-send-string process
977 (concat command " :" args)))))))
978
979 (defvar rcirc-parent-buffer nil)
980 (defvar rcirc-window-configuration nil)
981 (defun rcirc-edit-multiline ()
982 "Move current edit to a dedicated buffer."
983 (interactive)
984 (let ((pos (1+ (- (point) rcirc-prompt-end-marker))))
985 (goto-char (point-max))
986 (let ((text (buffer-substring rcirc-prompt-end-marker (point)))
987 (parent (buffer-name)))
988 (delete-region rcirc-prompt-end-marker (point))
989 (setq rcirc-window-configuration (current-window-configuration))
990 (pop-to-buffer (concat "*multiline " parent "*"))
991 (funcall rcirc-multiline-major-mode)
992 (rcirc-multiline-minor-mode 1)
993 (setq rcirc-parent-buffer parent)
994 (insert text)
995 (and (> pos 0) (goto-char pos))
996 (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent))))
997
998 (defvar rcirc-multiline-minor-mode-map (make-sparse-keymap)
999 "Keymap for multiline mode in rcirc.")
1000 (define-key rcirc-multiline-minor-mode-map
1001 (kbd "C-c C-c") 'rcirc-multiline-minor-submit)
1002 (define-key rcirc-multiline-minor-mode-map
1003 (kbd "C-x C-s") 'rcirc-multiline-minor-submit)
1004 (define-key rcirc-multiline-minor-mode-map
1005 (kbd "C-c C-k") 'rcirc-multiline-minor-cancel)
1006 (define-key rcirc-multiline-minor-mode-map
1007 (kbd "ESC ESC ESC") 'rcirc-multiline-minor-cancel)
1008
1009 (define-minor-mode rcirc-multiline-minor-mode
1010 "Minor mode for editing multiple lines in rcirc."
1011 :init-value nil
1012 :lighter " rcirc-mline"
1013 :keymap rcirc-multiline-minor-mode-map
1014 :global nil
1015 :group 'rcirc
1016 (make-local-variable 'rcirc-parent-buffer)
1017 (put 'rcirc-parent-buffer 'permanent-local t)
1018 (setq fill-column rcirc-max-message-length))
1019
1020 (defun rcirc-multiline-minor-submit ()
1021 "Send the text in buffer back to parent buffer."
1022 (interactive)
1023 (assert rcirc-parent-buffer)
1024 (untabify (point-min) (point-max))
1025 (let ((text (buffer-substring (point-min) (point-max)))
1026 (buffer (current-buffer))
1027 (pos (point)))
1028 (set-buffer rcirc-parent-buffer)
1029 (goto-char (point-max))
1030 (insert text)
1031 (kill-buffer buffer)
1032 (set-window-configuration rcirc-window-configuration)
1033 (goto-char (+ rcirc-prompt-end-marker (1- pos)))))
1034
1035 (defun rcirc-multiline-minor-cancel ()
1036 "Cancel the multiline edit."
1037 (interactive)
1038 (kill-buffer (current-buffer))
1039 (set-window-configuration rcirc-window-configuration))
1040
1041 (defun rcirc-any-buffer (process)
1042 "Return a buffer for PROCESS, either the one selected or the process buffer."
1043 (if rcirc-always-use-server-buffer-flag
1044 (process-buffer process)
1045 (let ((buffer (window-buffer (selected-window))))
1046 (if (and buffer
1047 (with-current-buffer buffer
1048 (and (eq major-mode 'rcirc-mode)
1049 (eq (rcirc-buffer-process) process))))
1050 buffer
1051 (process-buffer process)))))
1052
1053 (defcustom rcirc-response-formats
1054 '(("PRIVMSG" . "%T<%N> %m")
1055 ("NOTICE" . "%T-%N- %m")
1056 ("ACTION" . "%T[%N %m]")
1057 ("COMMAND" . "%T%m")
1058 ("ERROR" . "%T%fw!!! %m")
1059 (t . "%T%fp*** %fs%n %r %m"))
1060 "An alist of formats used for printing responses.
1061 The format is looked up using the response-type as a key;
1062 if no match is found, the default entry (with a key of `t') is used.
1063
1064 The entry's value part should be a string, which is inserted with
1065 the of the following escape sequences replaced by the described values:
1066
1067 %m The message text
1068 %n The sender's nick
1069 %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick')
1070 %r The response-type
1071 %T The timestamp (with face `rcirc-timestamp')
1072 %t The target
1073 %fw Following text uses the face `font-lock-warning-face'
1074 %fp Following text uses the face `rcirc-server-prefix'
1075 %fs Following text uses the face `rcirc-server'
1076 %f[FACE] Following text uses the face FACE
1077 %f- Following text uses the default face
1078 %% A literal `%' character"
1079 :type '(alist :key-type (choice (string :tag "Type")
1080 (const :tag "Default" t))
1081 :value-type string)
1082 :group 'rcirc)
1083
1084 (defun rcirc-format-response-string (process sender response target text)
1085 "Return a nicely-formatted response string, incorporating TEXT
1086 \(and perhaps other arguments). The specific formatting used
1087 is found by looking up RESPONSE in `rcirc-response-formats'."
1088 (let ((chunks
1089 (split-string (or (cdr (assoc response rcirc-response-formats))
1090 (cdr (assq t rcirc-response-formats)))
1091 "%"))
1092 (sender (or sender ""))
1093 (result "")
1094 (face nil)
1095 key face-key repl)
1096 (when (equal (car chunks) "")
1097 (pop chunks))
1098 (dolist (chunk chunks)
1099 (if (equal chunk "")
1100 (setq key ?%)
1101 (setq key (aref chunk 0))
1102 (setq chunk (substring chunk 1)))
1103 (setq repl
1104 (cond ((eq key ?%)
1105 ;; %% -- literal % character
1106 "%")
1107 ((or (eq key ?n) (eq key ?N))
1108 ;; %n/%N -- nick
1109 (let ((nick (concat (if (string= (rcirc-server-name process)
1110 sender)
1111 ""
1112 sender)
1113 (and target (concat "," target)))))
1114 (rcirc-facify nick
1115 (if (eq key ?n)
1116 face
1117 (cond ((string= sender (rcirc-nick process))
1118 'rcirc-my-nick)
1119 ((and rcirc-bright-nicks
1120 (string-match
1121 (regexp-opt rcirc-bright-nicks)
1122 sender))
1123 'rcirc-bright-nick)
1124 ((and rcirc-dim-nicks
1125 (string-match
1126 (regexp-opt rcirc-dim-nicks)
1127 sender))
1128 'rcirc-dim-nick)
1129 (t
1130 'rcirc-other-nick))))))
1131 ((eq key ?T)
1132 ;; %T -- timestamp
1133 (rcirc-facify
1134 (format-time-string rcirc-time-format (current-time))
1135 'rcirc-timestamp))
1136 ((eq key ?m)
1137 ;; %m -- message text
1138 (rcirc-markup-text process sender response (rcirc-facify text face)))
1139 ((eq key ?t)
1140 ;; %t -- target
1141 (rcirc-facify (or rcirc-target "") face))
1142 ((eq key ?r)
1143 ;; %r -- response
1144 (rcirc-facify response face))
1145 ((eq key ?f)
1146 ;; %f -- change face
1147 (setq face-key (aref chunk 0))
1148 (setq chunk (substring chunk 1))
1149 (cond ((eq face-key ?w)
1150 ;; %fw -- warning face
1151 (setq face 'font-lock-warning-face))
1152 ((eq face-key ?p)
1153 ;; %fp -- server-prefix face
1154 (setq face 'rcirc-server-prefix))
1155 ((eq face-key ?s)
1156 ;; %fs -- warning face
1157 (setq face 'rcirc-server))
1158 ((eq face-key ?-)
1159 ;; %fs -- warning face
1160 (setq face nil))
1161 ((and (eq face-key ?\[)
1162 (string-match "^\\([^]]*\\)[]]" chunk)
1163 (facep (match-string 1 chunk)))
1164 ;; %f[...] -- named face
1165 (setq face (intern (match-string 1 chunk)))
1166 (setq chunk (substring chunk (match-end 0)))))
1167 "")))
1168 (setq result (concat result repl (rcirc-facify chunk face))))
1169 result))
1170
1171 (defun rcirc-target-buffer (process sender response target text)
1172 "Return a buffer to print the server response."
1173 (assert (not (bufferp target)))
1174 (with-rcirc-process-buffer process
1175 (cond ((not target)
1176 (rcirc-any-buffer process))
1177 ((not (rcirc-channel-p target))
1178 ;; message from another user
1179 (if (string= response "PRIVMSG")
1180 (rcirc-get-buffer-create process (if (string= sender rcirc-nick)
1181 target
1182 sender))
1183 (rcirc-get-buffer process target t)))
1184 ((or (rcirc-get-buffer process target)
1185 (rcirc-any-buffer process))))))
1186
1187 (defvar rcirc-activity-types nil)
1188 (make-variable-buffer-local 'rcirc-activity-types)
1189 (defvar rcirc-last-sender nil)
1190 (make-variable-buffer-local 'rcirc-last-sender)
1191
1192 (defun rcirc-print (process sender response target text &optional activity)
1193 "Print TEXT in the buffer associated with TARGET.
1194 Format based on SENDER and RESPONSE. If ACTIVITY is non-nil,
1195 record activity."
1196 (or text (setq text ""))
1197 (unless (or (member sender rcirc-ignore-list)
1198 (member (with-syntax-table rcirc-nick-syntax-table
1199 (when (string-match "^\\([^/]\\w*\\)[:,]" text)
1200 (match-string 1 text)))
1201 rcirc-ignore-list))
1202 (let* ((buffer (rcirc-target-buffer process sender response target text))
1203 (inhibit-read-only t))
1204 (with-current-buffer buffer
1205 (let ((moving (= (point) rcirc-prompt-end-marker))
1206 (old-point (point-marker))
1207 (fill-start (marker-position rcirc-prompt-start-marker)))
1208
1209 (unless (string= sender (rcirc-nick process))
1210 ;; only decode text from other senders, not ours
1211 (setq text (decode-coding-string text rcirc-decode-coding-system))
1212 ;; mark the line with overlay arrow
1213 (unless (or (marker-position overlay-arrow-position)
1214 (get-buffer-window (current-buffer)))
1215 (set-marker overlay-arrow-position
1216 (marker-position rcirc-prompt-start-marker))))
1217
1218 ;; temporarily set the marker insertion-type because
1219 ;; insert-before-markers results in hidden text in new buffers
1220 (goto-char rcirc-prompt-start-marker)
1221 (set-marker-insertion-type rcirc-prompt-start-marker t)
1222 (set-marker-insertion-type rcirc-prompt-end-marker t)
1223
1224 (let ((fmted-text
1225 (rcirc-format-response-string process sender response nil
1226 text)))
1227
1228 (insert fmted-text (propertize "\n" 'hard t))
1229 (set-marker-insertion-type rcirc-prompt-start-marker nil)
1230 (set-marker-insertion-type rcirc-prompt-end-marker nil)
1231
1232 (let ((text-start (make-marker)))
1233 (set-marker text-start
1234 (or (next-single-property-change fill-start
1235 'rcirc-text)
1236 rcirc-prompt-end-marker))
1237 ;; squeeze spaces out of text before rcirc-text
1238 (fill-region fill-start (1- text-start))
1239
1240 ;; fill the text we just inserted, maybe
1241 (when (and rcirc-fill-flag
1242 (not (string= response "372"))) ;/motd
1243 (let ((fill-prefix
1244 (or rcirc-fill-prefix
1245 (make-string (- text-start fill-start) ?\s)))
1246 (fill-column (cond ((eq rcirc-fill-column 'frame-width)
1247 (1- (frame-width)))
1248 (rcirc-fill-column
1249 rcirc-fill-column)
1250 (t fill-column))))
1251 (fill-region fill-start rcirc-prompt-start-marker 'left t)))))
1252
1253 ;; set inserted text to be read-only
1254 (when rcirc-read-only-flag
1255 (put-text-property rcirc-prompt-start-marker fill-start 'read-only t)
1256 (let ((inhibit-read-only t))
1257 (put-text-property rcirc-prompt-start-marker fill-start
1258 'front-sticky t)
1259 (put-text-property (1- (point)) (point) 'rear-nonsticky t)))
1260
1261 ;; truncate buffer if it is very long
1262 (save-excursion
1263 (when (and rcirc-buffer-maximum-lines
1264 (> rcirc-buffer-maximum-lines 0)
1265 (= (forward-line (- rcirc-buffer-maximum-lines)) 0))
1266 (delete-region (point-min) (point))))
1267
1268 ;; set the window point for buffers show in windows
1269 (walk-windows (lambda (w)
1270 (when (and (not (eq (selected-window) w))
1271 (eq (current-buffer)
1272 (window-buffer w))
1273 (>= (window-point w)
1274 rcirc-prompt-end-marker))
1275 (set-window-point w (point-max))))
1276 nil t)
1277
1278 ;; restore the point
1279 (goto-char (if moving rcirc-prompt-end-marker old-point))
1280
1281 ;; keep window on bottom line if it was already there
1282 (when rcirc-scroll-show-maximum-output
1283 (walk-windows (lambda (w)
1284 (when (eq (window-buffer w) (current-buffer))
1285 (with-current-buffer (window-buffer w)
1286 (when (eq major-mode 'rcirc-mode)
1287 (with-selected-window w
1288 (when (<= (- (window-height)
1289 (count-screen-lines
1290 (window-point)
1291 (window-start))
1292 1)
1293 0)
1294 (recenter -1)))))))
1295 nil t))
1296
1297 ;; flush undo (can we do something smarter here?)
1298 (buffer-disable-undo)
1299 (buffer-enable-undo))
1300
1301 ;; record modeline activity
1302 (when (and activity
1303 (not rcirc-ignore-buffer-activity-flag)
1304 (not (and rcirc-dim-nicks sender
1305 (string-match (regexp-opt rcirc-dim-nicks) sender))))
1306 (rcirc-record-activity (current-buffer)
1307 (when (not (rcirc-channel-p rcirc-target))
1308 'nick)))
1309
1310 (sit-for 0) ; displayed text before hook
1311 (run-hook-with-args 'rcirc-print-hooks
1312 process sender response target text)))))
1313
1314 (defun rcirc-startup-channels (server)
1315 "Return the list of startup channels for SERVER."
1316 (let (channels)
1317 (dolist (i rcirc-startup-channels-alist)
1318 (if (string-match (car i) server)
1319 (setq channels (append channels (cdr i)))))
1320 channels))
1321
1322 (defun rcirc-join-channels (process channels)
1323 "Join CHANNELS."
1324 (save-window-excursion
1325 (dolist (channel channels)
1326 (with-rcirc-process-buffer process
1327 (rcirc-cmd-join channel process)))))
1328 \f
1329 ;;; nick management
1330 (defvar rcirc-nick-prefix-chars "~&@%+")
1331 (defun rcirc-user-nick (user)
1332 "Return the nick from USER. Remove any non-nick junk."
1333 (save-match-data
1334 (if (string-match (concat "^[" rcirc-nick-prefix-chars
1335 "]?\\([^! ]+\\)!?") (or user ""))
1336 (match-string 1 user)
1337 user)))
1338
1339 (defun rcirc-nick-channels (process nick)
1340 "Return list of channels for NICK."
1341 (with-rcirc-process-buffer process
1342 (mapcar (lambda (x) (car x))
1343 (gethash nick rcirc-nick-table))))
1344
1345 (defun rcirc-put-nick-channel (process nick channel)
1346 "Add CHANNEL to list associated with NICK."
1347 (let ((nick (rcirc-user-nick nick)))
1348 (with-rcirc-process-buffer process
1349 (let* ((chans (gethash nick rcirc-nick-table))
1350 (record (assoc-string channel chans t)))
1351 (if record
1352 (setcdr record (current-time))
1353 (puthash nick (cons (cons channel (current-time))
1354 chans)
1355 rcirc-nick-table))))))
1356
1357 (defun rcirc-nick-remove (process nick)
1358 "Remove NICK from table."
1359 (with-rcirc-process-buffer process
1360 (remhash nick rcirc-nick-table)))
1361
1362 (defun rcirc-remove-nick-channel (process nick channel)
1363 "Remove the CHANNEL from list associated with NICK."
1364 (with-rcirc-process-buffer process
1365 (let* ((chans (gethash nick rcirc-nick-table))
1366 (newchans
1367 ;; instead of assoc-string-delete-all:
1368 (let ((record (assoc-string channel chans t)))
1369 (when record
1370 (setcar record 'delete)
1371 (assq-delete-all 'delete chans)))))
1372 (if newchans
1373 (puthash nick newchans rcirc-nick-table)
1374 (remhash nick rcirc-nick-table)))))
1375
1376 (defun rcirc-channel-nicks (process target)
1377 "Return the list of nicks associated with TARGET sorted by last activity."
1378 (when target
1379 (if (rcirc-channel-p target)
1380 (with-rcirc-process-buffer process
1381 (let (nicks)
1382 (maphash
1383 (lambda (k v)
1384 (let ((record (assoc-string target v t)))
1385 (if record
1386 (setq nicks (cons (cons k (cdr record)) nicks)))))
1387 rcirc-nick-table)
1388 (mapcar (lambda (x) (car x))
1389 (sort nicks (lambda (x y) (time-less-p (cdr y) (cdr x)))))))
1390 (list target))))
1391
1392 (defun rcirc-ignore-update-automatic (nick)
1393 "Remove NICK from `rcirc-ignore-list'
1394 if NICK is also on `rcirc-ignore-list-automatic'."
1395 (when (member nick rcirc-ignore-list-automatic)
1396 (setq rcirc-ignore-list-automatic
1397 (delete nick rcirc-ignore-list-automatic)
1398 rcirc-ignore-list
1399 (delete nick rcirc-ignore-list))))
1400 \f
1401 ;;; activity tracking
1402 (defvar rcirc-track-minor-mode-map (make-sparse-keymap)
1403 "Keymap for rcirc track minor mode.")
1404
1405 (define-key rcirc-track-minor-mode-map (kbd "C-c `") 'rcirc-next-active-buffer)
1406 (define-key rcirc-track-minor-mode-map (kbd "C-c C-@") 'rcirc-next-active-buffer)
1407 (define-key rcirc-track-minor-mode-map (kbd "C-c C-SPC") 'rcirc-next-active-buffer)
1408
1409 ;;;###autoload
1410 (define-minor-mode rcirc-track-minor-mode
1411 "Global minor mode for tracking activity in rcirc buffers."
1412 :init-value nil
1413 :lighter ""
1414 :keymap rcirc-track-minor-mode-map
1415 :global t
1416 :group 'rcirc
1417 (or global-mode-string (setq global-mode-string '("")))
1418 ;; toggle the mode-line channel indicator
1419 (if rcirc-track-minor-mode
1420 (progn
1421 (and (not (memq 'rcirc-activity-string global-mode-string))
1422 (setq global-mode-string
1423 (append global-mode-string '(rcirc-activity-string))))
1424 (add-hook 'window-configuration-change-hook
1425 'rcirc-window-configuration-change))
1426 (setq global-mode-string
1427 (delete 'rcirc-activity-string global-mode-string))
1428 (remove-hook 'window-configuration-change-hook
1429 'rcirc-window-configuration-change)))
1430
1431 (or (assq 'rcirc-ignore-buffer-activity-flag minor-mode-alist)
1432 (setq minor-mode-alist
1433 (cons '(rcirc-ignore-buffer-activity-flag " Ignore") minor-mode-alist)))
1434 (or (assq 'rcirc-low-priority-flag minor-mode-alist)
1435 (setq minor-mode-alist
1436 (cons '(rcirc-low-priority-flag " LowPri") minor-mode-alist)))
1437
1438 (defun rcirc-toggle-ignore-buffer-activity ()
1439 "Toggle the value of `rcirc-ignore-buffer-activity-flag'."
1440 (interactive)
1441 (setq rcirc-ignore-buffer-activity-flag
1442 (not rcirc-ignore-buffer-activity-flag))
1443 (message (if rcirc-ignore-buffer-activity-flag
1444 "Ignore activity in this buffer"
1445 "Notice activity in this buffer"))
1446 (force-mode-line-update))
1447
1448 (defun rcirc-toggle-low-priority ()
1449 "Toggle the value of `rcirc-low-priority-flag'."
1450 (interactive)
1451 (setq rcirc-low-priority-flag
1452 (not rcirc-low-priority-flag))
1453 (message (if rcirc-low-priority-flag
1454 "Activity in this buffer is low priority"
1455 "Activity in this buffer is normal priority"))
1456 (force-mode-line-update))
1457
1458 (defvar rcirc-switch-to-buffer-function 'switch-to-buffer
1459 "Function to use when switching buffers.
1460 Possible values are `switch-to-buffer', `pop-to-buffer', and
1461 `display-buffer'.")
1462
1463 (defun rcirc-switch-to-server-buffer ()
1464 "Switch to the server buffer associated with current channel buffer."
1465 (interactive)
1466 (funcall rcirc-switch-to-buffer-function rcirc-server-buffer))
1467
1468 (defun rcirc-jump-to-first-unread-line ()
1469 "Move the point to the first unread line in this buffer."
1470 (interactive)
1471 (when (marker-position overlay-arrow-position)
1472 (goto-char overlay-arrow-position)))
1473
1474 (defvar rcirc-last-non-irc-buffer nil
1475 "The buffer to switch to when there is no more activity.")
1476
1477 (defun rcirc-next-active-buffer (arg)
1478 "Go to the next rcirc buffer with activity.
1479 With prefix ARG, go to the next low priority buffer with activity.
1480 The function given by `rcirc-switch-to-buffer-function' is used to
1481 show the buffer."
1482 (interactive "P")
1483 (let* ((pair (rcirc-split-activity rcirc-activity))
1484 (lopri (car pair))
1485 (hipri (cdr pair)))
1486 (if (or (and (not arg) hipri)
1487 (and arg lopri))
1488 (progn
1489 (unless (eq major-mode 'rcirc-mode)
1490 (setq rcirc-last-non-irc-buffer (current-buffer)))
1491 (funcall rcirc-switch-to-buffer-function
1492 (car (if arg lopri hipri))))
1493 (if (eq major-mode 'rcirc-mode)
1494 (if (not (and rcirc-last-non-irc-buffer
1495 (buffer-live-p rcirc-last-non-irc-buffer)))
1496 (message "No IRC activity. Start something.")
1497 (message "No more IRC activity. Go back to work.")
1498 (funcall rcirc-switch-to-buffer-function rcirc-last-non-irc-buffer)
1499 (setq rcirc-last-non-irc-buffer nil))
1500 (message (concat
1501 "No IRC activity."
1502 (when lopri
1503 (concat
1504 " Type C-u "
1505 (key-description (this-command-keys))
1506 " for low priority activity."))))))))
1507
1508 (defvar rcirc-activity-hooks nil
1509 "Hook to be run when there is channel activity.
1510
1511 Functions are called with a single argument, the buffer with the
1512 activity. Only run if the buffer is not visible and
1513 `rcirc-ignore-buffer-activity-flag' is non-nil.")
1514
1515 (defun rcirc-record-activity (buffer &optional type)
1516 "Record BUFFER activity with TYPE."
1517 (with-current-buffer buffer
1518 (when (not (get-buffer-window (current-buffer) t))
1519 (setq rcirc-activity
1520 (sort (add-to-list 'rcirc-activity (current-buffer))
1521 (lambda (b1 b2)
1522 (let ((t1 (with-current-buffer b1 rcirc-last-post-time))
1523 (t2 (with-current-buffer b2 rcirc-last-post-time)))
1524 (time-less-p t2 t1)))))
1525 (pushnew type rcirc-activity-types)
1526 (rcirc-update-activity-string)))
1527 (run-hook-with-args 'rcirc-activity-hooks buffer))
1528
1529 (defun rcirc-clear-activity (buffer)
1530 "Clear the BUFFER activity."
1531 (setq rcirc-activity (delete buffer rcirc-activity))
1532 (with-current-buffer buffer
1533 (setq rcirc-activity-types nil)))
1534
1535 (defun rcirc-split-activity (activity)
1536 "Return a cons cell with ACTIVITY split into (lopri . hipri)."
1537 (let (lopri hipri)
1538 (dolist (buf rcirc-activity)
1539 (with-current-buffer buf
1540 (if (and rcirc-low-priority-flag
1541 (not (member 'nick rcirc-activity-types)))
1542 (add-to-list 'lopri buf t)
1543 (add-to-list 'hipri buf t))))
1544 (cons lopri hipri)))
1545
1546 ;; TODO: add mouse properties
1547 (defun rcirc-update-activity-string ()
1548 "Update mode-line string."
1549 (let* ((pair (rcirc-split-activity rcirc-activity))
1550 (lopri (car pair))
1551 (hipri (cdr pair)))
1552 (setq rcirc-activity-string
1553 (cond ((or hipri lopri)
1554 (concat "-"
1555 (and hipri "[")
1556 (rcirc-activity-string hipri)
1557 (and hipri lopri ",")
1558 (and lopri
1559 (concat "("
1560 (rcirc-activity-string lopri)
1561 ")"))
1562 (and hipri "]")
1563 "-"))
1564 ((not (null (rcirc-process-list)))
1565 "-[]-")
1566 (t "")))))
1567
1568 (defun rcirc-activity-string (buffers)
1569 (mapconcat (lambda (b)
1570 (let ((s (substring-no-properties (rcirc-short-buffer-name b))))
1571 (with-current-buffer b
1572 (dolist (type rcirc-activity-types)
1573 (rcirc-add-face 0 (length s)
1574 (case type
1575 (nick 'rcirc-track-nick)
1576 (keyword 'rcirc-track-keyword))
1577 s)))
1578 s))
1579 buffers ","))
1580
1581 (defun rcirc-short-buffer-name (buffer)
1582 "Return a short name for BUFFER to use in the modeline indicator."
1583 (with-current-buffer buffer
1584 (or rcirc-short-buffer-name (buffer-name))))
1585
1586 (defvar rcirc-current-buffer nil)
1587 (defun rcirc-window-configuration-change ()
1588 "Go through visible windows and remove buffers from activity list.
1589 Also, clear the overlay arrow if the current buffer is now hidden."
1590 (let ((current-now-hidden t))
1591 (walk-windows (lambda (w)
1592 (let ((buf (window-buffer w)))
1593 (with-current-buffer buf
1594 (when (eq major-mode 'rcirc-mode)
1595 (rcirc-clear-activity buf)))
1596 (when (eq buf rcirc-current-buffer)
1597 (setq current-now-hidden nil)))))
1598 ;; add overlay arrow if the buffer isn't displayed
1599 (when (and current-now-hidden
1600 rcirc-current-buffer
1601 (buffer-live-p rcirc-current-buffer))
1602 (with-current-buffer rcirc-current-buffer
1603 (when (and (eq major-mode 'rcirc-mode)
1604 (marker-position overlay-arrow-position))
1605 (set-marker overlay-arrow-position nil)))))
1606
1607 ;; remove any killed buffers from list
1608 (setq rcirc-activity
1609 (delq nil (mapcar (lambda (buf) (when (buffer-live-p buf) buf))
1610 rcirc-activity)))
1611 (rcirc-update-activity-string)
1612 (setq rcirc-current-buffer (current-buffer)))
1613
1614 \f
1615 ;;; buffer name abbreviation
1616 (defun rcirc-update-short-buffer-names ()
1617 (let ((bufalist
1618 (apply 'append (mapcar (lambda (process)
1619 (with-rcirc-process-buffer process
1620 rcirc-buffer-alist))
1621 (rcirc-process-list)))))
1622 (dolist (i (rcirc-abbreviate bufalist))
1623 (when (buffer-live-p (cdr i))
1624 (with-current-buffer (cdr i)
1625 (setq rcirc-short-buffer-name (car i)))))))
1626
1627 (defun rcirc-abbreviate (pairs)
1628 (apply 'append (mapcar 'rcirc-rebuild-tree (rcirc-make-trees pairs))))
1629
1630 (defun rcirc-rebuild-tree (tree &optional acc)
1631 (let ((ch (char-to-string (car tree))))
1632 (dolist (x (cdr tree))
1633 (if (listp x)
1634 (setq acc (append acc
1635 (mapcar (lambda (y)
1636 (cons (concat ch (car y))
1637 (cdr y)))
1638 (rcirc-rebuild-tree x))))
1639 (setq acc (cons (cons ch x) acc))))
1640 acc))
1641
1642 (defun rcirc-make-trees (pairs)
1643 (let (alist)
1644 (mapc (lambda (pair)
1645 (if (consp pair)
1646 (let* ((str (car pair))
1647 (data (cdr pair))
1648 (char (unless (zerop (length str))
1649 (aref str 0)))
1650 (rest (unless (zerop (length str))
1651 (substring str 1)))
1652 (part (if char (assq char alist))))
1653 (if part
1654 ;; existing partition
1655 (setcdr part (cons (cons rest data) (cdr part)))
1656 ;; new partition
1657 (setq alist (cons (if char
1658 (list char (cons rest data))
1659 data)
1660 alist))))
1661 (setq alist (cons pair alist))))
1662 pairs)
1663 ;; recurse into cdrs of alist
1664 (mapc (lambda (x)
1665 (when (and (listp x) (listp (cadr x)))
1666 (setcdr x (if (> (length (cdr x)) 1)
1667 (rcirc-make-trees (cdr x))
1668 (setcdr x (list (cdadr x)))))))
1669 alist)))
1670 \f
1671 ;;; /commands these are called with 3 args: PROCESS, TARGET, which is
1672 ;; the current buffer/channel/user, and ARGS, which is a string
1673 ;; containing the text following the /cmd.
1674
1675 (defmacro defun-rcirc-command (command argument docstring interactive-form
1676 &rest body)
1677 "Define a command."
1678 `(defun ,(intern (concat "rcirc-cmd-" (symbol-name command)))
1679 (,@argument &optional process target)
1680 ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values given"
1681 "\nby `rcirc-buffer-process' and `rcirc-target' will be used.")
1682 ,interactive-form
1683 (let ((process (or process (rcirc-buffer-process)))
1684 (target (or target rcirc-target)))
1685 ,@body)))
1686
1687 (defun-rcirc-command msg (message)
1688 "Send private MESSAGE to TARGET."
1689 (interactive "i")
1690 (if (null message)
1691 (progn
1692 (setq target (completing-read "Message nick: "
1693 (with-rcirc-server-buffer
1694 rcirc-nick-table)))
1695 (when (> (length target) 0)
1696 (setq message (read-string (format "Message %s: " target)))
1697 (when (> (length message) 0)
1698 (rcirc-send-message process target message))))
1699 (if (not (string-match "\\([^ ]+\\) \\(.+\\)" message))
1700 (message "Not enough args, or something.")
1701 (setq target (match-string 1 message)
1702 message (match-string 2 message))
1703 (rcirc-send-message process target message))))
1704
1705 (defun-rcirc-command query (nick)
1706 "Open a private chat buffer to NICK."
1707 (interactive (list (completing-read "Query nick: "
1708 (with-rcirc-server-buffer rcirc-nick-table))))
1709 (let ((existing-buffer (rcirc-get-buffer process nick)))
1710 (switch-to-buffer (or existing-buffer
1711 (rcirc-get-buffer-create process nick)))
1712 (when (not existing-buffer)
1713 (rcirc-cmd-whois nick))))
1714
1715 (defun-rcirc-command join (channel)
1716 "Join CHANNEL."
1717 (interactive "sJoin channel: ")
1718 (let ((buffer (rcirc-get-buffer-create process
1719 (car (split-string channel)))))
1720 (rcirc-send-string process (concat "JOIN " channel))
1721 (when (not (eq (selected-window) (minibuffer-window)))
1722 (funcall rcirc-switch-to-buffer-function buffer))))
1723
1724 (defun-rcirc-command part (channel)
1725 "Part CHANNEL."
1726 (interactive "sPart channel: ")
1727 (let ((channel (if (> (length channel) 0) channel target)))
1728 (rcirc-send-string process (concat "PART " channel " :" rcirc-id-string))))
1729
1730 (defun-rcirc-command quit (reason)
1731 "Send a quit message to server with REASON."
1732 (interactive "sQuit reason: ")
1733 (rcirc-send-string process (concat "QUIT :"
1734 (if (not (zerop (length reason)))
1735 reason
1736 rcirc-id-string))))
1737
1738 (defun-rcirc-command nick (nick)
1739 "Change nick to NICK."
1740 (interactive "i")
1741 (when (null nick)
1742 (setq nick (read-string "New nick: " (rcirc-nick process))))
1743 (rcirc-send-string process (concat "NICK " nick)))
1744
1745 (defun-rcirc-command names (channel)
1746 "Display list of names in CHANNEL or in current channel if CHANNEL is nil.
1747 If called interactively, prompt for a channel when prefix arg is supplied."
1748 (interactive "P")
1749 (if (interactive-p)
1750 (if channel
1751 (setq channel (read-string "List names in channel: " target))))
1752 (let ((channel (if (> (length channel) 0)
1753 channel
1754 target)))
1755 (rcirc-send-string process (concat "NAMES " channel))))
1756
1757 (defun-rcirc-command topic (topic)
1758 "List TOPIC for the TARGET channel.
1759 With a prefix arg, prompt for new topic."
1760 (interactive "P")
1761 (if (and (interactive-p) topic)
1762 (setq topic (read-string "New Topic: " rcirc-topic)))
1763 (rcirc-send-string process (concat "TOPIC " target
1764 (when (> (length topic) 0)
1765 (concat " :" topic)))))
1766
1767 (defun-rcirc-command whois (nick)
1768 "Request information from server about NICK."
1769 (interactive (list
1770 (completing-read "Whois: "
1771 (with-rcirc-server-buffer rcirc-nick-table))))
1772 (rcirc-send-string process (concat "WHOIS " nick)))
1773
1774 (defun-rcirc-command mode (args)
1775 "Set mode with ARGS."
1776 (interactive (list (concat (read-string "Mode nick or channel: ")
1777 " " (read-string "Mode: "))))
1778 (rcirc-send-string process (concat "MODE " args)))
1779
1780 (defun-rcirc-command list (channels)
1781 "Request information on CHANNELS from server."
1782 (interactive "sList Channels: ")
1783 (rcirc-send-string process (concat "LIST " channels)))
1784
1785 (defun-rcirc-command oper (args)
1786 "Send operator command to server."
1787 (interactive "sOper args: ")
1788 (rcirc-send-string process (concat "OPER " args)))
1789
1790 (defun-rcirc-command quote (message)
1791 "Send MESSAGE literally to server."
1792 (interactive "sServer message: ")
1793 (rcirc-send-string process message))
1794
1795 (defun-rcirc-command kick (arg)
1796 "Kick NICK from current channel."
1797 (interactive (list
1798 (concat (completing-read "Kick nick: "
1799 (rcirc-channel-nicks
1800 (rcirc-buffer-process)
1801 rcirc-target))
1802 (read-from-minibuffer "Kick reason: "))))
1803 (let* ((arglist (split-string arg))
1804 (argstring (concat (car arglist) " :"
1805 (mapconcat 'identity (cdr arglist) " "))))
1806 (rcirc-send-string process (concat "KICK " target " " argstring))))
1807
1808 (defun rcirc-cmd-ctcp (args &optional process target)
1809 (if (string-match "^\\([^ ]+\\)\\s-+\\(.+\\)$" args)
1810 (let ((target (match-string 1 args))
1811 (request (match-string 2 args)))
1812 (rcirc-send-string process
1813 (format "PRIVMSG %s \C-a%s\C-a"
1814 target (upcase request))))
1815 (rcirc-print process (rcirc-nick process) "ERROR" nil
1816 "usage: /ctcp NICK REQUEST")))
1817
1818 (defun rcirc-cmd-me (args &optional process target)
1819 (rcirc-send-string process (format "PRIVMSG %s :\C-aACTION %s\C-a"
1820 target args)))
1821
1822 (defun rcirc-add-or-remove (set &optional elt)
1823 (if (and elt (not (string= "" elt)))
1824 (if (member-ignore-case elt set)
1825 (delete elt set)
1826 (cons elt set))
1827 set))
1828
1829 (defun-rcirc-command ignore (nick)
1830 "Manage the ignore list.
1831 Ignore NICK, unignore NICK if already ignored, or list ignored
1832 nicks when no NICK is given. When listing ignored nicks, the
1833 ones added to the list automatically are marked with an asterisk."
1834 (interactive "sToggle ignoring of nick: ")
1835 (setq rcirc-ignore-list (rcirc-add-or-remove rcirc-ignore-list nick))
1836 (rcirc-print process nil "IGNORE" target
1837 (mapconcat
1838 (lambda (nick)
1839 (concat nick
1840 (if (member nick rcirc-ignore-list-automatic)
1841 "*" "")))
1842 rcirc-ignore-list " ")))
1843
1844 (defun-rcirc-command bright (nick)
1845 "Manage the bright nick list."
1846 (interactive "sToggle emphasis of nick: ")
1847 (setq rcirc-bright-nicks (rcirc-add-or-remove rcirc-bright-nicks nick))
1848 (rcirc-print process nil "BRIGHT" target
1849 (mapconcat 'identity rcirc-bright-nicks " ")))
1850
1851 (defun-rcirc-command dim (nick)
1852 "Manage the dim nick list."
1853 (interactive "sToggle deemphasis of nick: ")
1854 (setq rcirc-dim-nicks (rcirc-add-or-remove rcirc-dim-nicks nick))
1855 (rcirc-print process nil "DIM" target
1856 (mapconcat 'identity rcirc-dim-nicks " ")))
1857
1858 (defun-rcirc-command keyword (keyword)
1859 "Manage the keyword list.
1860 Mark KEYWORD, unmark KEYWORD if already marked, or list marked
1861 keywords when no KEYWORD is given."
1862 (interactive "sToggle highlighting of keyword: ")
1863 (setq rcirc-keywords (rcirc-add-or-remove rcirc-keywords keyword))
1864 (rcirc-print process nil "KEYWORD" target
1865 (mapconcat 'identity rcirc-keywords " ")))
1866
1867 \f
1868 (defun rcirc-add-face (start end name &optional object)
1869 "Add face NAME to the face text property of the text from START to END."
1870 (when name
1871 (let ((pos start)
1872 next prop)
1873 (while (< pos end)
1874 (setq prop (get-text-property pos 'face object)
1875 next (next-single-property-change pos 'face object end))
1876 (unless (member name (get-text-property pos 'face object))
1877 (add-text-properties pos next (list 'face (cons name prop)) object))
1878 (setq pos next)))))
1879
1880 (defun rcirc-facify (string face)
1881 "Return a copy of STRING with FACE property added."
1882 (let ((string (or string "")))
1883 (rcirc-add-face 0 (length string) face string)
1884 string))
1885
1886 (defvar rcirc-url-regexp
1887 (rx-to-string
1888 `(and word-boundary
1889 (or (and
1890 (or (and (or "http" "https" "ftp" "file" "gopher" "news"
1891 "telnet" "wais" "mailto")
1892 "://")
1893 "www.")
1894 (1+ (char "-a-zA-Z0-9_."))
1895 (1+ (char "-a-zA-Z0-9_"))
1896 (optional ":" (1+ (char "0-9"))))
1897 (and (1+ (char "-a-zA-Z0-9_."))
1898 (or ".com" ".net" ".org")
1899 word-boundary))
1900 (optional
1901 (and "/"
1902 (1+ (char "-a-zA-Z0-9_=!?#$\@~`%&*+|\\/:;.,{}[]()"))
1903 (char "-a-zA-Z0-9_=#$\@~`%&*+|\\/:;{}[]()")))))
1904 "Regexp matching URLs. Set to nil to disable URL features in rcirc.")
1905
1906 (defun rcirc-browse-url (&optional arg)
1907 "Prompt for URL to browse based on URLs in buffer."
1908 (interactive "P")
1909 (let ((completions (mapcar (lambda (x) (cons x nil)) rcirc-urls))
1910 (initial-input (car rcirc-urls))
1911 (history (cdr rcirc-urls)))
1912 (browse-url (completing-read "rcirc browse-url: "
1913 completions nil nil initial-input 'history)
1914 arg)))
1915
1916 (defun rcirc-browse-url-at-point (point)
1917 "Send URL at point to `browse-url'."
1918 (interactive "d")
1919 (let ((beg (previous-single-property-change (1+ point) 'mouse-face))
1920 (end (next-single-property-change point 'mouse-face)))
1921 (browse-url (buffer-substring-no-properties beg end))))
1922
1923 (defun rcirc-browse-url-at-mouse (event)
1924 "Send URL at mouse click to `browse-url'."
1925 (interactive "e")
1926 (let ((position (event-end event)))
1927 (with-current-buffer (window-buffer (posn-window position))
1928 (rcirc-browse-url-at-point (posn-point position)))))
1929
1930 \f
1931 (defvar rcirc-markup-text-functions
1932 '(rcirc-markup-body-text
1933 rcirc-markup-attributes
1934 rcirc-markup-my-nick
1935 rcirc-markup-urls
1936 rcirc-markup-keywords
1937 rcirc-markup-bright-nicks)
1938 "List of functions used to manipulate text before it is printed.
1939
1940 Each function takes three arguments, PROCESS, SENDER, RESPONSE
1941 and CHANNEL-BUFFER. The current buffer is temporary buffer that
1942 contains the text to manipulate. Each function works on the text
1943 in this buffer.")
1944
1945 (defun rcirc-markup-text (process sender response text)
1946 "Return TEXT with properties added based on various patterns."
1947 (let ((channel-buffer (current-buffer)))
1948 (with-temp-buffer
1949 (insert text)
1950 (goto-char (point-min))
1951 (dolist (fn rcirc-markup-text-functions)
1952 (save-excursion
1953 (funcall fn process sender response channel-buffer)))
1954 (buffer-substring (point-min) (point-max)))))
1955
1956 (defun rcirc-markup-body-text (process sender response channel-buffer)
1957 ;; We add the text property `rcirc-text' to identify this as the
1958 ;; body text.
1959 (add-text-properties (point-min) (point-max)
1960 (list 'rcirc-text (buffer-substring-no-properties
1961 (point-min) (point-max)))))
1962
1963 (defun rcirc-markup-attributes (process sender response channel-buffer)
1964 (while (re-search-forward "\\([\C-b\C-_\C-v]\\).*?\\(\\1\\|\C-o\\)" nil t)
1965 (rcirc-add-face (match-beginning 0) (match-end 0)
1966 (case (char-after (match-beginning 1))
1967 (?\C-b 'bold)
1968 (?\C-v 'italic)
1969 (?\C-_ 'underline)))
1970 ;; keep the ^O since it could terminate other attributes
1971 (when (not (eq ?\C-o (char-before (match-end 2))))
1972 (delete-region (match-beginning 2) (match-end 2)))
1973 (delete-region (match-beginning 1) (match-end 1))
1974 (goto-char (1+ (match-beginning 1))))
1975 ;; remove the ^O characters now
1976 (while (re-search-forward "\C-o+" nil t)
1977 (delete-region (match-beginning 0) (match-end 0))))
1978
1979 (defun rcirc-markup-my-nick (process sender response channel-buffer)
1980 (with-syntax-table rcirc-nick-syntax-table
1981 (while (re-search-forward (concat "\\b"
1982 (regexp-quote (rcirc-nick process))
1983 "\\b")
1984 nil t)
1985 (rcirc-add-face (match-beginning 0) (match-end 0)
1986 'rcirc-nick-in-message)
1987 (when (string= response "PRIVMSG")
1988 (rcirc-add-face (point-min) (point-max) 'rcirc-nick-in-message-full-line)
1989 (rcirc-record-activity channel-buffer 'nick)))))
1990
1991 (defun rcirc-markup-urls (process sender response channel-buffer)
1992 (while (re-search-forward rcirc-url-regexp nil t)
1993 (let ((start (match-beginning 0))
1994 (end (match-end 0)))
1995 (rcirc-add-face start end 'rcirc-url)
1996 (add-text-properties start end (list 'mouse-face 'highlight
1997 'keymap rcirc-browse-url-map))
1998 ;; record the url
1999 (let ((url (buffer-substring-no-properties start end)))
2000 (with-current-buffer channel-buffer
2001 (push url rcirc-urls))))))
2002
2003 (defun rcirc-markup-keywords (process sender response channel-buffer)
2004 (let* ((target (with-current-buffer channel-buffer (or rcirc-target "")))
2005 (keywords (delq nil (mapcar (lambda (keyword)
2006 (when (not (string-match keyword target))
2007 keyword))
2008 rcirc-keywords))))
2009 (when keywords
2010 (while (re-search-forward (regexp-opt keywords 'words) nil t)
2011 (rcirc-add-face (match-beginning 0) (match-end 0) 'rcirc-keyword)
2012 (when (and (string= response "PRIVMSG")
2013 (not (string= sender (rcirc-nick process))))
2014 (rcirc-record-activity channel-buffer 'keyword))))))
2015
2016 (defun rcirc-markup-bright-nicks (process sender response channel-buffer)
2017 (when (and rcirc-bright-nicks
2018 (string= response "NAMES"))
2019 (with-syntax-table rcirc-nick-syntax-table
2020 (while (re-search-forward (regexp-opt rcirc-bright-nicks 'words) nil t)
2021 (rcirc-add-face (match-beginning 0) (match-end 0)
2022 'rcirc-bright-nick)))))
2023 \f
2024 ;;; handlers
2025 ;; these are called with the server PROCESS, the SENDER, which is a
2026 ;; server or a user, depending on the command, the ARGS, which is a
2027 ;; list of strings, and the TEXT, which is the original server text,
2028 ;; verbatim
2029 (defun rcirc-handler-001 (process sender args text)
2030 (rcirc-handler-generic process "001" sender args text)
2031 ;; set the real server name
2032 (with-rcirc-process-buffer process
2033 (setq rcirc-connecting nil)
2034 (rcirc-reschedule-timeout process)
2035 (setq rcirc-server-name sender)
2036 (setq rcirc-nick (car args))
2037 (rcirc-update-prompt)
2038 (when rcirc-auto-authenticate-flag (rcirc-authenticate))
2039 (rcirc-join-channels process rcirc-startup-channels)))
2040
2041 (defun rcirc-handler-PRIVMSG (process sender args text)
2042 (let ((target (if (rcirc-channel-p (car args))
2043 (car args)
2044 sender))
2045 (message (or (cadr args) "")))
2046 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
2047 (rcirc-handler-CTCP process target sender (match-string 1 message))
2048 (rcirc-print process sender "PRIVMSG" target message t))
2049 ;; update nick timestamp
2050 (if (member target (rcirc-nick-channels process sender))
2051 (rcirc-put-nick-channel process sender target))))
2052
2053 (defun rcirc-handler-NOTICE (process sender args text)
2054 (let ((target (car args))
2055 (message (cadr args)))
2056 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
2057 (rcirc-handler-CTCP-response process target sender
2058 (match-string 1 message))
2059 (rcirc-print process sender "NOTICE"
2060 (cond ((rcirc-channel-p target)
2061 target)
2062 ;;; -ChanServ- [#gnu] Welcome...
2063 ((string-match "\\[\\(#[^\] ]+\\)\\]" message)
2064 (match-string 1 message))
2065 (sender
2066 (if (string= sender (rcirc-server-name process))
2067 nil ; server notice
2068 sender)))
2069 message t))))
2070
2071 (defun rcirc-handler-WALLOPS (process sender args text)
2072 (rcirc-print process sender "WALLOPS" sender (car args) t))
2073
2074 (defun rcirc-handler-JOIN (process sender args text)
2075 (let ((channel (car args)))
2076 (rcirc-get-buffer-create process channel)
2077 (rcirc-print process sender "JOIN" channel "")
2078
2079 ;; print in private chat buffer if it exists
2080 (when (rcirc-get-buffer (rcirc-buffer-process) sender)
2081 (rcirc-print process sender "JOIN" sender channel))
2082
2083 (rcirc-put-nick-channel process sender channel)))
2084
2085 ;; PART and KICK are handled the same way
2086 (defun rcirc-handler-PART-or-KICK (process response channel sender nick args)
2087 (rcirc-ignore-update-automatic nick)
2088 (if (not (string= nick (rcirc-nick process)))
2089 ;; this is someone else leaving
2090 (rcirc-remove-nick-channel process nick channel)
2091 ;; this is us leaving
2092 (mapc (lambda (n)
2093 (rcirc-remove-nick-channel process n channel))
2094 (rcirc-channel-nicks process channel))
2095
2096 ;; if the buffer is still around, make it inactive
2097 (let ((buffer (rcirc-get-buffer process channel)))
2098 (when buffer
2099 (with-current-buffer buffer
2100 (setq rcirc-target nil))))))
2101
2102 (defun rcirc-handler-PART (process sender args text)
2103 (let* ((channel (car args))
2104 (reason (cadr args))
2105 (message (concat channel " " reason)))
2106 (rcirc-print process sender "PART" channel message)
2107 ;; print in private chat buffer if it exists
2108 (when (rcirc-get-buffer (rcirc-buffer-process) sender)
2109 (rcirc-print process sender "PART" sender message))
2110
2111 (rcirc-handler-PART-or-KICK process "PART" channel sender sender reason)))
2112
2113 (defun rcirc-handler-KICK (process sender args text)
2114 (let* ((channel (car args))
2115 (nick (cadr args))
2116 (reason (caddr args))
2117 (message (concat nick " " channel " " reason)))
2118 (rcirc-print process sender "KICK" channel message t)
2119 ;; print in private chat buffer if it exists
2120 (when (rcirc-get-buffer (rcirc-buffer-process) nick)
2121 (rcirc-print process sender "KICK" nick message))
2122
2123 (rcirc-handler-PART-or-KICK process "KICK" channel sender nick reason)))
2124
2125 (defun rcirc-handler-QUIT (process sender args text)
2126 (rcirc-ignore-update-automatic sender)
2127 (mapc (lambda (channel)
2128 (rcirc-print process sender "QUIT" channel (apply 'concat args)))
2129 (rcirc-nick-channels process sender))
2130
2131 ;; print in private chat buffer if it exists
2132 (when (rcirc-get-buffer (rcirc-buffer-process) sender)
2133 (rcirc-print process sender "QUIT" sender (apply 'concat args)))
2134
2135 (rcirc-nick-remove process sender))
2136
2137 (defun rcirc-handler-NICK (process sender args text)
2138 (let* ((old-nick sender)
2139 (new-nick (car args))
2140 (channels (rcirc-nick-channels process old-nick)))
2141 ;; update list of ignored nicks
2142 (rcirc-ignore-update-automatic old-nick)
2143 (when (member old-nick rcirc-ignore-list)
2144 (add-to-list 'rcirc-ignore-list new-nick)
2145 (add-to-list 'rcirc-ignore-list-automatic new-nick))
2146 ;; print message to nick's channels
2147 (dolist (target channels)
2148 (rcirc-print process sender "NICK" target new-nick))
2149 ;; update private chat buffer, if it exists
2150 (let ((chat-buffer (rcirc-get-buffer process old-nick)))
2151 (when chat-buffer
2152 (with-current-buffer chat-buffer
2153 (rcirc-print process sender "NICK" old-nick new-nick)
2154 (setq rcirc-target new-nick)
2155 (rename-buffer (rcirc-generate-new-buffer-name process new-nick)))))
2156 ;; remove old nick and add new one
2157 (with-rcirc-process-buffer process
2158 (let ((v (gethash old-nick rcirc-nick-table)))
2159 (remhash old-nick rcirc-nick-table)
2160 (puthash new-nick v rcirc-nick-table))
2161 ;; if this is our nick...
2162 (when (string= old-nick rcirc-nick)
2163 (setq rcirc-nick new-nick)
2164 (rcirc-update-prompt t)
2165 ;; reauthenticate
2166 (when rcirc-auto-authenticate-flag (rcirc-authenticate))))))
2167
2168 (defun rcirc-handler-PING (process sender args text)
2169 (rcirc-send-string process (concat "PONG " (car args))))
2170
2171 (defun rcirc-handler-PONG (process sender args text)
2172 ;; do nothing
2173 )
2174
2175 (defun rcirc-handler-TOPIC (process sender args text)
2176 (let ((topic (cadr args)))
2177 (rcirc-print process sender "TOPIC" (car args) topic)
2178 (with-current-buffer (rcirc-get-buffer process (car args))
2179 (setq rcirc-topic topic))))
2180
2181 (defvar rcirc-nick-away-alist nil)
2182 (defun rcirc-handler-301 (process sender args text)
2183 "RPL_AWAY"
2184 (let* ((nick (cadr args))
2185 (rec (assoc-string nick rcirc-nick-away-alist))
2186 (away-message (caddr args)))
2187 (when (or (not rec)
2188 (not (string= (cdr rec) away-message)))
2189 ;; away message has changed
2190 (rcirc-handler-generic process "AWAY" nick (cdr args) text)
2191 (if rec
2192 (setcdr rec away-message)
2193 (setq rcirc-nick-away-alist (cons (cons nick away-message)
2194 rcirc-nick-away-alist))))))
2195
2196 (defun rcirc-handler-332 (process sender args text)
2197 "RPL_TOPIC"
2198 (let ((buffer (or (rcirc-get-buffer process (cadr args))
2199 (rcirc-get-temp-buffer-create process (cadr args)))))
2200 (with-current-buffer buffer
2201 (setq rcirc-topic (caddr args)))))
2202
2203 (defun rcirc-handler-333 (process sender args text)
2204 "Not in rfc1459.txt"
2205 (let ((buffer (or (rcirc-get-buffer process (cadr args))
2206 (rcirc-get-temp-buffer-create process (cadr args)))))
2207 (with-current-buffer buffer
2208 (let ((setter (caddr args))
2209 (time (current-time-string
2210 (seconds-to-time
2211 (string-to-number (cadddr args))))))
2212 (rcirc-print process sender "TOPIC" (cadr args)
2213 (format "%s (%s on %s)" rcirc-topic setter time))))))
2214
2215 (defun rcirc-handler-477 (process sender args text)
2216 "ERR_NOCHANMODES"
2217 (rcirc-print process sender "477" (cadr args) (caddr args)))
2218
2219 (defun rcirc-handler-MODE (process sender args text)
2220 (let ((target (car args))
2221 (msg (mapconcat 'identity (cdr args) " ")))
2222 (rcirc-print process sender "MODE"
2223 (if (string= target (rcirc-nick process))
2224 nil
2225 target)
2226 msg)
2227
2228 ;; print in private chat buffers if they exist
2229 (mapc (lambda (nick)
2230 (when (rcirc-get-buffer process nick)
2231 (rcirc-print process sender "MODE" nick msg)))
2232 (cddr args))))
2233
2234 (defun rcirc-get-temp-buffer-create (process channel)
2235 "Return a buffer based on PROCESS and CHANNEL."
2236 (let ((tmpnam (concat " " (downcase channel) "TMP" (process-name process))))
2237 (get-buffer-create tmpnam)))
2238
2239 (defun rcirc-handler-353 (process sender args text)
2240 "RPL_NAMREPLY"
2241 (let ((channel (caddr args)))
2242 (mapc (lambda (nick)
2243 (rcirc-put-nick-channel process nick channel))
2244 (split-string (cadddr args) " " t))
2245 (with-current-buffer (rcirc-get-temp-buffer-create process channel)
2246 (goto-char (point-max))
2247 (insert (car (last args)) " "))))
2248
2249 (defun rcirc-handler-366 (process sender args text)
2250 "RPL_ENDOFNAMES"
2251 (let* ((channel (cadr args))
2252 (buffer (rcirc-get-temp-buffer-create process channel)))
2253 (with-current-buffer buffer
2254 (rcirc-print process sender "NAMES" channel
2255 (buffer-substring (point-min) (point-max))))
2256 (kill-buffer buffer)))
2257
2258 (defun rcirc-handler-433 (process sender args text)
2259 "ERR_NICKNAMEINUSE"
2260 (rcirc-handler-generic process "433" sender args text)
2261 (let* ((new-nick (concat (cadr args) "`")))
2262 (with-rcirc-process-buffer process
2263 (rcirc-cmd-nick new-nick nil process))))
2264
2265 (defun rcirc-authenticate ()
2266 "Send authentication to process associated with current buffer.
2267 Passwords are stored in `rcirc-authinfo' (which see)."
2268 (interactive)
2269 (with-rcirc-server-buffer
2270 (dolist (i rcirc-authinfo)
2271 (let ((process (rcirc-buffer-process))
2272 (server (car i))
2273 (nick (caddr i))
2274 (method (cadr i))
2275 (args (cdddr i)))
2276 (when (and (string-match server rcirc-server)
2277 (string-match nick rcirc-nick))
2278 (cond ((equal method 'nickserv)
2279 (rcirc-send-string
2280 process
2281 (concat
2282 "PRIVMSG nickserv :identify "
2283 (car args))))
2284 ((equal method 'chanserv)
2285 (rcirc-send-string
2286 process
2287 (concat
2288 "PRIVMSG chanserv :identify "
2289 (cadr args) " " (car args))))
2290 ((equal method 'bitlbee)
2291 (rcirc-send-string
2292 process
2293 (concat "PRIVMSG &bitlbee :identify " (car args))))
2294 (t
2295 (message "No %S authentication method defined"
2296 method))))))))
2297
2298 (defun rcirc-handler-INVITE (process sender args text)
2299 (rcirc-print process sender "INVITE" nil (mapconcat 'identity args " ") t))
2300
2301 (defun rcirc-handler-ERROR (process sender args text)
2302 (rcirc-print process sender "ERROR" nil (mapconcat 'identity args " ")))
2303
2304 (defun rcirc-handler-CTCP (process target sender text)
2305 (if (string-match "^\\([^ ]+\\) *\\(.*\\)$" text)
2306 (let* ((request (upcase (match-string 1 text)))
2307 (args (match-string 2 text))
2308 (handler (intern-soft (concat "rcirc-handler-ctcp-" request))))
2309 (if (not (fboundp handler))
2310 (rcirc-print process sender "ERROR" target
2311 (format "%s sent unsupported ctcp: %s" sender text)
2312 t)
2313 (funcall handler process target sender args)
2314 (if (not (string= request "ACTION"))
2315 (rcirc-print process sender "CTCP" target
2316 (format "%s" text) t))))))
2317
2318 (defun rcirc-handler-ctcp-VERSION (process target sender args)
2319 (rcirc-send-string process
2320 (concat "NOTICE " sender
2321 " :\C-aVERSION " rcirc-id-string
2322 "\C-a")))
2323
2324 (defun rcirc-handler-ctcp-ACTION (process target sender args)
2325 (rcirc-print process sender "ACTION" target args t))
2326
2327 (defun rcirc-handler-ctcp-TIME (process target sender args)
2328 (rcirc-send-string process
2329 (concat "NOTICE " sender
2330 " :\C-aTIME " (current-time-string) "\C-a")))
2331
2332 (defun rcirc-handler-CTCP-response (process target sender message)
2333 (rcirc-print process sender "CTCP" nil message t))
2334 \f
2335 (defgroup rcirc-faces nil
2336 "Faces for rcirc."
2337 :group 'rcirc
2338 :group 'faces)
2339
2340 (defface rcirc-my-nick ; font-lock-function-name-face
2341 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
2342 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
2343 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
2344 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
2345 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
2346 (t (:inverse-video t :weight bold)))
2347 "The face used to highlight my messages."
2348 :group 'rcirc-faces)
2349
2350 (defface rcirc-other-nick ; font-lock-variable-name-face
2351 '((((class grayscale) (background light))
2352 (:foreground "Gray90" :weight bold :slant italic))
2353 (((class grayscale) (background dark))
2354 (:foreground "DimGray" :weight bold :slant italic))
2355 (((class color) (min-colors 88) (background light)) (:foreground "DarkGoldenrod"))
2356 (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod"))
2357 (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
2358 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
2359 (((class color) (min-colors 8)) (:foreground "yellow" :weight light))
2360 (t (:weight bold :slant italic)))
2361 "The face used to highlight other messages."
2362 :group 'rcirc-faces)
2363
2364 (defface rcirc-bright-nick
2365 '((((class grayscale) (background light))
2366 (:foreground "LightGray" :weight bold :underline t))
2367 (((class grayscale) (background dark))
2368 (:foreground "Gray50" :weight bold :underline t))
2369 (((class color) (min-colors 88) (background light)) (:foreground "CadetBlue"))
2370 (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine"))
2371 (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
2372 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
2373 (((class color) (min-colors 8)) (:foreground "magenta"))
2374 (t (:weight bold :underline t)))
2375 "Face used for nicks matched by `rcirc-bright-nicks'."
2376 :group 'rcirc-faces)
2377
2378 (defface rcirc-dim-nick
2379 '((t :inherit default))
2380 "Face used for nicks in `rcirc-dim-nicks'."
2381 :group 'rcirc-faces)
2382
2383 (defface rcirc-server ; font-lock-comment-face
2384 '((((class grayscale) (background light))
2385 (:foreground "DimGray" :weight bold :slant italic))
2386 (((class grayscale) (background dark))
2387 (:foreground "LightGray" :weight bold :slant italic))
2388 (((class color) (min-colors 88) (background light))
2389 (:foreground "Firebrick"))
2390 (((class color) (min-colors 88) (background dark))
2391 (:foreground "chocolate1"))
2392 (((class color) (min-colors 16) (background light))
2393 (:foreground "red"))
2394 (((class color) (min-colors 16) (background dark))
2395 (:foreground "red1"))
2396 (((class color) (min-colors 8) (background light))
2397 )
2398 (((class color) (min-colors 8) (background dark))
2399 )
2400 (t (:weight bold :slant italic)))
2401 "The face used to highlight server messages."
2402 :group 'rcirc-faces)
2403
2404 (defface rcirc-server-prefix ; font-lock-comment-delimiter-face
2405 '((default :inherit rcirc-server)
2406 (((class grayscale)))
2407 (((class color) (min-colors 16)))
2408 (((class color) (min-colors 8) (background light))
2409 :foreground "red")
2410 (((class color) (min-colors 8) (background dark))
2411 :foreground "red1"))
2412 "The face used to highlight server prefixes."
2413 :group 'rcirc-faces)
2414
2415 (defface rcirc-timestamp
2416 '((t (:inherit default)))
2417 "The face used to highlight timestamps."
2418 :group 'rcirc-faces)
2419
2420 (defface rcirc-nick-in-message ; font-lock-keyword-face
2421 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
2422 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
2423 (((class color) (min-colors 88) (background light)) (:foreground "Purple"))
2424 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
2425 (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
2426 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
2427 (((class color) (min-colors 8)) (:foreground "cyan" :weight bold))
2428 (t (:weight bold)))
2429 "The face used to highlight instances of your nick within messages."
2430 :group 'rcirc-faces)
2431
2432 (defface rcirc-nick-in-message-full-line
2433 '((t (:bold t)))
2434 "The face used emphasize the entire message when your nick is mentioned."
2435 :group 'rcirc-faces)
2436
2437 (defface rcirc-prompt ; comint-highlight-prompt
2438 '((((min-colors 88) (background dark)) (:foreground "cyan1"))
2439 (((background dark)) (:foreground "cyan"))
2440 (t (:foreground "dark blue")))
2441 "The face used to highlight prompts."
2442 :group 'rcirc-faces)
2443
2444 (defface rcirc-track-nick
2445 '((((type tty)) (:inherit default))
2446 (t (:inverse-video t)))
2447 "The face used in the mode-line when your nick is mentioned."
2448 :group 'rcirc-faces)
2449
2450 (defface rcirc-track-keyword
2451 '((t (:bold t )))
2452 "The face used in the mode-line when keywords are mentioned."
2453 :group 'rcirc-faces)
2454
2455 (defface rcirc-url
2456 '((t (:bold t)))
2457 "The face used to highlight urls."
2458 :group 'rcirc-faces)
2459
2460 (defface rcirc-keyword
2461 '((t (:inherit highlight)))
2462 "The face used to highlight keywords."
2463 :group 'rcirc-faces)
2464
2465 \f
2466 ;; When using M-x flyspell-mode, only check words after the prompt
2467 (put 'rcirc-mode 'flyspell-mode-predicate 'rcirc-looking-at-input)
2468 (defun rcirc-looking-at-input ()
2469 "Returns true if point is past the input marker."
2470 (>= (point) rcirc-prompt-end-marker))
2471 \f
2472
2473 (provide 'rcirc)
2474
2475 ;; arch-tag: b471b7e8-6b5a-4399-b2c6-a3c78dfc8ffb
2476 ;;; rcirc.el ends here