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