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