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