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