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