1 ;;; rcirc.el --- default, simple IRC client.
3 ;; Copyright (C) 2005-2011 Free Software Foundation, Inc.
5 ;; Author: Ryan Yeske <rcyeske@gmail.com>
6 ;; Maintainers: Ryan Yeske <rcyeske@gmail.com>,
7 ;; Deniz Dogan <deniz.a.m.dogan@gmail.com>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Internet Relay Chat (IRC) is a form of instant communication over
28 ;; the Internet. It is mainly designed for group (many-to-many)
29 ;; communication in discussion forums called channels, but also allows
30 ;; one-to-one communication.
32 ;; Rcirc has simple defaults and clear and consistent behavior.
33 ;; Message arrival timestamps, activity notification on the modeline,
34 ;; message filling, nick completion, and keepalive pings are all
35 ;; enabled by default, but can easily be adjusted or turned off. Each
36 ;; discussion takes place in its own buffer and there is a single
37 ;; server buffer per connection.
39 ;; Open a new irc connection with:
48 (eval-when-compile (require 'cl))
54 :link '(custom-manual "(rcirc)")
57 (defcustom rcirc-server-alist
58 '(("irc.freenode.net" :channels ("#rcirc")))
59 "An alist of IRC connections to establish when running `rcirc'.
60 Each element looks like (SERVER-NAME PARAMETERS).
62 SERVER-NAME is a string describing the server to connect
65 The optional PARAMETERS come in pairs PARAMETER VALUE.
67 The following parameters are recognized:
71 VALUE must be a string. If absent, `rcirc-default-nick' is used
76 VALUE must be a number or string. If absent,
77 `rcirc-default-port' is used.
81 VALUE must be a string. If absent, `rcirc-default-user-name' is
86 VALUE must be a string. If absent, no PASS command will be sent
91 VALUE must be a string. If absent, `rcirc-default-full-name' is
96 VALUE must be a list of strings describing which channels to join
97 when connecting to this server. If absent, no channels will be
98 connected to automatically."
99 :type '(alist :key-type string
100 :value-type (plist :options ((:nick string)
105 (:channels (repeat string)))))
108 (defcustom rcirc-default-port 6667
109 "The default port to connect to."
113 (defcustom rcirc-default-nick (user-login-name)
118 (defcustom rcirc-default-user-name "user"
119 "Your user name sent to the server when connecting."
120 :version "24.1" ; changed default
124 (defcustom rcirc-default-full-name "unknown"
125 "The full name sent to the server when connecting."
126 :version "24.1" ; changed default
130 (defcustom rcirc-fill-flag t
131 "*Non-nil means line-wrap messages printed in channel buffers."
135 (defcustom rcirc-fill-column nil
136 "*Column beyond which automatic line-wrapping should happen.
137 If nil, use value of `fill-column'. If 'frame-width, use the
138 maximum frame width."
139 :type '(choice (const :tag "Value of `fill-column'")
140 (const :tag "Full frame width" frame-width)
141 (integer :tag "Number of columns"))
144 (defcustom rcirc-fill-prefix nil
145 "*Text to insert before filled lines.
146 If nil, calculate the prefix dynamically to line up text
147 underneath each nick."
148 :type '(choice (const :tag "Dynamic" nil)
149 (string :tag "Prefix text"))
152 (defvar rcirc-ignore-buffer-activity-flag nil
153 "If non-nil, ignore activity in this buffer.")
154 (make-variable-buffer-local 'rcirc-ignore-buffer-activity-flag)
156 (defvar rcirc-low-priority-flag nil
157 "If non-nil, activity in this buffer is considered low priority.")
158 (make-variable-buffer-local 'rcirc-low-priority-flag)
160 (defvar rcirc-omit-mode nil
161 "Non-nil if Rcirc-Omit mode is enabled.
162 Use the command `rcirc-omit-mode' to change this variable.")
163 (make-variable-buffer-local 'rcirc-omit-mode)
165 (defcustom rcirc-time-format "%H:%M "
166 "*Describes how timestamps are printed.
167 Used as the first arg to `format-time-string'."
171 (defcustom rcirc-input-ring-size 1024
172 "*Size of input history ring."
176 (defcustom rcirc-read-only-flag t
177 "*Non-nil means make text in IRC buffers read-only."
181 (defcustom rcirc-buffer-maximum-lines nil
182 "*The maximum size in lines for rcirc buffers.
183 Channel buffers are truncated from the top to be no greater than this
184 number. If zero or nil, no truncating is done."
185 :type '(choice (const :tag "No truncation" nil)
186 (integer :tag "Number of lines"))
189 (defcustom rcirc-scroll-show-maximum-output t
190 "*If non-nil, scroll buffer to keep the point at the bottom of
195 (defcustom rcirc-authinfo nil
196 "List of authentication passwords.
197 Each element of the list is a list with a SERVER-REGEXP string
198 and a method symbol followed by method specific arguments.
200 The valid METHOD symbols are `nickserv', `chanserv' and
203 The ARGUMENTS for each METHOD symbol are:
204 `nickserv': NICK PASSWORD [NICKSERV-NICK]
205 `chanserv': NICK CHANNEL PASSWORD
206 `bitlbee': NICK PASSWORD
209 ((\"freenode\" nickserv \"bob\" \"p455w0rd\")
210 (\"freenode\" chanserv \"bob\" \"#bobland\" \"passwd99\")
211 (\"bitlbee\" bitlbee \"robert\" \"sekrit\")
212 (\"dal.net\" nickserv \"bob\" \"sekrit\" \"NickServ@services.dal.net\"))"
213 :type '(alist :key-type (string :tag "Server")
214 :value-type (choice (list :tag "NickServ"
217 (string :tag "Password"))
218 (list :tag "ChanServ"
221 (string :tag "Channel")
222 (string :tag "Password"))
226 (string :tag "Password"))))
229 (defcustom rcirc-auto-authenticate-flag t
230 "*Non-nil means automatically send authentication string to server.
231 See also `rcirc-authinfo'."
235 (defcustom rcirc-prompt "> "
236 "Prompt string to use in IRC buffers.
238 The following replacements are made:
241 %t is the buffer target, a channel or a user.
243 Setting this alone will not affect the prompt;
244 use either M-x customize or also call `rcirc-update-prompt'."
246 :set 'rcirc-set-changed
247 :initialize 'custom-initialize-default
250 (defcustom rcirc-keywords nil
251 "List of keywords to highlight in message text."
252 :type '(repeat string)
255 (defcustom rcirc-ignore-list ()
256 "List of ignored nicks.
257 Use /ignore to list them, use /ignore NICK to add or remove a nick."
258 :type '(repeat string)
261 (defvar rcirc-ignore-list-automatic ()
262 "List of ignored nicks added to `rcirc-ignore-list' because of renaming.
263 When an ignored person renames, their nick is added to both lists.
264 Nicks will be removed from the automatic list on follow-up renamings or
267 (defcustom rcirc-bright-nicks nil
268 "List of nicks to be emphasized.
269 See `rcirc-bright-nick' face."
270 :type '(repeat string)
273 (defcustom rcirc-dim-nicks nil
274 "List of nicks to be deemphasized.
275 See `rcirc-dim-nick' face."
276 :type '(repeat string)
279 (defcustom rcirc-print-hooks nil
280 "Hook run after text is printed.
281 Called with 5 arguments, PROCESS, SENDER, RESPONSE, TARGET and TEXT."
285 (defcustom rcirc-always-use-server-buffer-flag nil
286 "Non-nil means messages without a channel target will go to the server buffer."
290 (defcustom rcirc-decode-coding-system 'utf-8
291 "Coding system used to decode incoming irc messages."
295 (defcustom rcirc-encode-coding-system 'utf-8
296 "Coding system used to encode outgoing irc messages."
300 (defcustom rcirc-coding-system-alist nil
301 "Alist to decide a coding system to use for a channel I/O operation.
302 The format is ((PATTERN . VAL) ...).
303 PATTERN is either a string or a cons of strings.
304 If PATTERN is a string, it is used to match a target.
305 If PATTERN is a cons of strings, the car part is used to match a
306 target, and the cdr part is used to match a server.
307 VAL is either a coding system or a cons of coding systems.
308 If VAL is a coding system, it is used for both decoding and encoding
310 If VAL is a cons of coding systems, the car part is used for decoding,
311 and the cdr part is used for encoding."
312 :type '(alist :key-type (choice (string :tag "Channel Regexp")
313 (cons (string :tag "Channel Regexp")
314 (string :tag "Server Regexp")))
315 :value-type (choice coding-system
316 (cons (coding-system :tag "Decode")
317 (coding-system :tag "Encode"))))
320 (defcustom rcirc-multiline-major-mode 'fundamental-mode
321 "Major-mode function to use in multiline edit buffers."
325 (defcustom rcirc-nick-completion-format "%s: "
326 "Format string to use in nick completions.
328 The format string is only used when completing at the beginning
329 of a line. The string is passed as the first argument to
330 `format' with the nickname as the second argument."
335 (defvar rcirc-nick nil)
337 (defvar rcirc-prompt-start-marker nil)
338 (defvar rcirc-prompt-end-marker nil)
340 (defvar rcirc-nick-table nil)
342 (defvar rcirc-recent-quit-alist nil
343 "Alist of nicks that have recently quit or parted the channel.")
345 (defvar rcirc-nick-syntax-table
346 (let ((table (make-syntax-table text-mode-syntax-table)))
347 (mapc (lambda (c) (modify-syntax-entry c "w" table))
349 (modify-syntax-entry ?' "_" table)
351 "Syntax table which includes all nick characters as word constituents.")
353 ;; each process has an alist of (target . buffer) pairs
354 (defvar rcirc-buffer-alist nil)
356 (defvar rcirc-activity nil
357 "List of buffers with unviewed activity.")
359 (defvar rcirc-activity-string ""
360 "String displayed in modeline representing `rcirc-activity'.")
361 (put 'rcirc-activity-string 'risky-local-variable t)
363 (defvar rcirc-server-buffer nil
364 "The server buffer associated with this channel buffer.")
366 (defvar rcirc-target nil
367 "The channel or user associated with this buffer.")
369 (defvar rcirc-urls nil
370 "List of urls seen in the current buffer.")
371 (put 'rcirc-urls 'permanent-local t)
373 (defvar rcirc-timeout-seconds 600
374 "Kill connection after this many seconds if there is no activity.")
376 (defconst rcirc-id-string (concat "rcirc on GNU Emacs " emacs-version))
378 (defvar rcirc-startup-channels nil)
380 (defvar rcirc-server-name-history nil
381 "History variable for \\[rcirc] call.")
383 (defvar rcirc-server-port-history nil
384 "History variable for \\[rcirc] call.")
386 (defvar rcirc-nick-name-history nil
387 "History variable for \\[rcirc] call.")
389 (defvar rcirc-user-name-history nil
390 "History variable for \\[rcirc] call.")
394 "Connect to all servers in `rcirc-server-alist'.
396 Do not connect to a server if it is already connected.
398 If ARG is non-nil, instead prompt for connection parameters."
401 (let* ((server (completing-read "IRC Server: "
404 (caar rcirc-server-alist)
405 'rcirc-server-name-history))
406 (server-plist (cdr (assoc-string server rcirc-server-alist)))
407 (port (read-string "IRC Port: "
409 (or (plist-get server-plist :port)
411 'rcirc-server-port-history))
412 (nick (read-string "IRC Nick: "
413 (or (plist-get server-plist :nick)
415 'rcirc-nick-name-history))
416 (user-name (read-string "IRC Username: "
417 (or (plist-get server-plist :user-name)
418 rcirc-default-user-name)
419 'rcirc-user-name-history))
420 (password (read-passwd "IRC Password: " nil
421 (plist-get server-plist :password)))
422 (channels (split-string
423 (read-string "IRC Channels: "
425 (plist-get server-plist
429 (rcirc-connect server port nick user-name
430 rcirc-default-full-name
432 ;; connect to servers in `rcirc-server-alist'
433 (let (connected-servers)
434 (dolist (c rcirc-server-alist)
435 (let ((server (car c))
436 (nick (or (plist-get (cdr c) :nick) rcirc-default-nick))
437 (port (or (plist-get (cdr c) :port) rcirc-default-port))
438 (user-name (or (plist-get (cdr c) :user-name)
439 rcirc-default-user-name))
440 (full-name (or (plist-get (cdr c) :full-name)
441 rcirc-default-full-name))
442 (channels (plist-get (cdr c) :channels))
443 (password (plist-get (cdr c) :password)))
446 (dolist (p (rcirc-process-list))
447 (when (string= server (process-name p))
451 (rcirc-connect server port nick user-name
452 full-name channels password)
453 (quit (message "Quit connecting to %s" server)))
454 (with-current-buffer (process-buffer connected)
455 (setq connected-servers
456 (cons (process-contact (get-buffer-process
457 (current-buffer)) :host)
458 connected-servers))))))))
459 (when connected-servers
460 (message "Already connected to %s"
461 (if (cdr connected-servers)
462 (concat (mapconcat 'identity (butlast connected-servers) ", ")
464 (car (last connected-servers)))
465 (car connected-servers)))))))
468 (defalias 'irc 'rcirc)
471 (defvar rcirc-process-output nil)
472 (defvar rcirc-topic nil)
473 (defvar rcirc-keepalive-timer nil)
474 (defvar rcirc-last-server-message-time nil)
475 (defvar rcirc-server nil) ; server provided by server
476 (defvar rcirc-server-name nil) ; server name given by 001 response
477 (defvar rcirc-timeout-timer nil)
478 (defvar rcirc-user-disconnect nil)
479 (defvar rcirc-connecting nil)
480 (defvar rcirc-process nil)
483 (defun rcirc-connect (server &optional port nick user-name
484 full-name startup-channels password)
486 (message "Connecting to %s..." server)
487 (let* ((inhibit-eol-conversion)
488 (port-number (if port
490 (string-to-number port)
493 (nick (or nick rcirc-default-nick))
494 (user-name (or user-name rcirc-default-user-name))
495 (full-name (or full-name rcirc-default-full-name))
496 (startup-channels startup-channels)
497 (process (make-network-process :name server :host server :service port-number)))
499 (set-process-coding-system process 'raw-text 'raw-text)
500 (switch-to-buffer (rcirc-generate-new-buffer-name process nil))
501 (set-process-buffer process (current-buffer))
502 (rcirc-mode process nil)
503 (set-process-sentinel process 'rcirc-sentinel)
504 (set-process-filter process 'rcirc-filter)
505 (make-local-variable 'rcirc-process)
506 (setq rcirc-process process)
507 (make-local-variable 'rcirc-server)
508 (setq rcirc-server server)
509 (make-local-variable 'rcirc-server-name)
510 (setq rcirc-server-name server) ; update when we get 001 response
511 (make-local-variable 'rcirc-buffer-alist)
512 (setq rcirc-buffer-alist nil)
513 (make-local-variable 'rcirc-nick-table)
514 (setq rcirc-nick-table (make-hash-table :test 'equal))
515 (make-local-variable 'rcirc-nick)
516 (setq rcirc-nick nick)
517 (make-local-variable 'rcirc-process-output)
518 (setq rcirc-process-output nil)
519 (make-local-variable 'rcirc-startup-channels)
520 (setq rcirc-startup-channels startup-channels)
521 (make-local-variable 'rcirc-last-server-message-time)
522 (setq rcirc-last-server-message-time (current-time))
523 (make-local-variable 'rcirc-timeout-timer)
524 (setq rcirc-timeout-timer nil)
525 (make-local-variable 'rcirc-user-disconnect)
526 (setq rcirc-user-disconnect nil)
527 (make-local-variable 'rcirc-connecting)
528 (setq rcirc-connecting t)
530 (add-hook 'auto-save-hook 'rcirc-log-write)
534 (rcirc-send-string process (concat "PASS " password)))
535 (rcirc-send-string process (concat "NICK " nick))
536 (rcirc-send-string process (concat "USER " user-name
539 ;; setup ping timer if necessary
540 (unless rcirc-keepalive-timer
541 (setq rcirc-keepalive-timer
542 (run-at-time 0 (/ rcirc-timeout-seconds 2) 'rcirc-keepalive)))
544 (message "Connecting to %s...done" server)
546 ;; return process object
549 (defmacro with-rcirc-process-buffer (process &rest body)
550 (declare (indent 1) (debug t))
551 `(with-current-buffer (process-buffer ,process)
554 (defmacro with-rcirc-server-buffer (&rest body)
555 (declare (indent 0) (debug t))
556 `(with-current-buffer rcirc-server-buffer
559 (defun rcirc-keepalive ()
560 "Send keep alive pings to active rcirc processes.
561 Kill processes that have not received a server message since the
563 (if (rcirc-process-list)
564 (mapc (lambda (process)
565 (with-rcirc-process-buffer process
566 (when (not rcirc-connecting)
567 (rcirc-send-string process
568 (format "PRIVMSG %s :\C-aKEEPALIVE %f\C-a"
570 (if (featurep 'xemacs)
574 (rcirc-process-list))
575 ;; no processes, clean up timer
576 (cancel-timer rcirc-keepalive-timer)
577 (setq rcirc-keepalive-timer nil)))
579 (defun rcirc-handler-ctcp-KEEPALIVE (process target sender message)
580 (with-rcirc-process-buffer process
581 (setq header-line-format (format "%f" (- (if (featurep 'xemacs)
585 (string-to-number message))))))
587 (defvar rcirc-debug-buffer " *rcirc debug*")
588 (defvar rcirc-debug-flag nil
589 "If non-nil, write information to `rcirc-debug-buffer'.")
590 (defun rcirc-debug (process text)
591 "Add an entry to the debug log including PROCESS and TEXT.
592 Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag'
594 (when rcirc-debug-flag
595 (with-current-buffer (get-buffer-create rcirc-debug-buffer)
596 (goto-char (point-max))
599 (format-time-string "%Y-%m-%dT%T ") (process-name process)
603 (defvar rcirc-sentinel-hooks nil
604 "Hook functions called when the process sentinel is called.
605 Functions are called with PROCESS and SENTINEL arguments.")
607 (defun rcirc-sentinel (process sentinel)
608 "Called when PROCESS receives SENTINEL."
609 (let ((sentinel (replace-regexp-in-string "\n" "" sentinel)))
610 (rcirc-debug process (format "SENTINEL: %S %S\n" process sentinel))
611 (with-rcirc-process-buffer process
612 (dolist (buffer (cons nil (mapcar 'cdr rcirc-buffer-alist)))
613 (with-current-buffer (or buffer (current-buffer))
614 (rcirc-print process "rcirc.el" "ERROR" rcirc-target
615 (format "%s: %s (%S)"
616 (process-name process)
618 (process-status process)) (not rcirc-target))
619 (rcirc-disconnect-buffer)))
620 (run-hook-with-args 'rcirc-sentinel-hooks process sentinel))))
622 (defun rcirc-disconnect-buffer (&optional buffer)
623 (with-current-buffer (or buffer (current-buffer))
624 ;; set rcirc-target to nil for each channel so cleanup
625 ;; doesnt happen when we reconnect
626 (setq rcirc-target nil)
627 (setq mode-line-process ":disconnected")))
629 (defun rcirc-process-list ()
630 "Return a list of rcirc processes."
633 (when (buffer-live-p (process-buffer p))
634 (with-rcirc-process-buffer p
635 (when (eq major-mode 'rcirc-mode)
636 (setq ps (cons p ps))))))
640 (defvar rcirc-receive-message-hooks nil
641 "Hook functions run when a message is received from server.
642 Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.")
643 (defun rcirc-filter (process output)
644 "Called when PROCESS receives OUTPUT."
645 (rcirc-debug process output)
646 (rcirc-reschedule-timeout process)
647 (with-rcirc-process-buffer process
648 (setq rcirc-last-server-message-time (current-time))
649 (setq rcirc-process-output (concat rcirc-process-output output))
650 (when (= (aref rcirc-process-output
651 (1- (length rcirc-process-output))) ?\n)
653 (rcirc-process-server-response process line))
654 (split-string rcirc-process-output "[\n\r]" t))
655 (setq rcirc-process-output nil))))
657 (defun rcirc-reschedule-timeout (process)
658 (with-rcirc-process-buffer process
659 (when (not rcirc-connecting)
660 (with-rcirc-process-buffer process
661 (when rcirc-timeout-timer (cancel-timer rcirc-timeout-timer))
662 (setq rcirc-timeout-timer (run-at-time rcirc-timeout-seconds nil
663 'rcirc-delete-process
666 (defun rcirc-delete-process (process)
667 (delete-process process))
669 (defvar rcirc-trap-errors-flag t)
670 (defun rcirc-process-server-response (process text)
671 (if rcirc-trap-errors-flag
673 (rcirc-process-server-response-1 process text)
675 (rcirc-print process "RCIRC" "ERROR" nil
676 (format "\"%s\" %s" text err) t)))
677 (rcirc-process-server-response-1 process text)))
679 (defun rcirc-process-server-response-1 (process text)
680 (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text)
681 (let* ((user (match-string 2 text))
682 (sender (rcirc-user-nick user))
683 (cmd (match-string 3 text))
684 (args (match-string 4 text))
685 (handler (intern-soft (concat "rcirc-handler-" cmd))))
686 (string-match "^\\([^:]*\\):?\\(.+\\)?$" args)
687 (let* ((args1 (match-string 1 args))
688 (args2 (match-string 2 args))
689 (args (delq nil (append (split-string args1 " " t)
691 (if (not (fboundp handler))
692 (rcirc-handler-generic process cmd sender args text)
693 (funcall handler process sender args text))
694 (run-hook-with-args 'rcirc-receive-message-hooks
695 process cmd sender args text)))
696 (message "UNHANDLED: %s" text)))
698 (defvar rcirc-responses-no-activity '("305" "306")
699 "Responses that don't trigger activity in the mode-line indicator.")
701 (defun rcirc-handler-generic (process response sender args text)
702 "Generic server response handler."
703 (rcirc-print process sender response nil
704 (mapconcat 'identity (cdr args) " ")
705 (not (member response rcirc-responses-no-activity))))
707 (defun rcirc-send-string (process string)
708 "Send PROCESS a STRING plus a newline."
709 (let ((string (concat (encode-coding-string string rcirc-encode-coding-system)
711 (unless (eq (process-status process) 'open)
712 (error "Network connection to %s is not open"
713 (process-name process)))
714 (rcirc-debug process string)
715 (process-send-string process string)))
717 (defun rcirc-buffer-process (&optional buffer)
718 "Return the process associated with channel BUFFER.
719 With no argument or nil as argument, use the current buffer."
720 (or (get-buffer-process (if buffer
721 (with-current-buffer buffer
723 rcirc-server-buffer))
726 (defun rcirc-server-name (process)
727 "Return PROCESS server name, given by the 001 response."
728 (with-rcirc-process-buffer process
729 (or rcirc-server-name
730 (warn "server name for process %S unknown" process))))
732 (defun rcirc-nick (process)
733 "Return PROCESS nick."
734 (with-rcirc-process-buffer process
735 (or rcirc-nick rcirc-default-nick)))
737 (defun rcirc-buffer-nick (&optional buffer)
738 "Return the nick associated with BUFFER.
739 With no argument or nil as argument, use the current buffer."
740 (with-current-buffer (or buffer (current-buffer))
741 (with-current-buffer rcirc-server-buffer
742 (or rcirc-nick rcirc-default-nick))))
744 (defvar rcirc-max-message-length 420
745 "Messages longer than this value will be split.")
747 (defun rcirc-send-message (process target message &optional noticep silent)
748 "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
749 If NOTICEP is non-nil, send a notice instead of privmsg.
750 If SILENT is non-nil, do not print the message in any irc buffer."
751 ;; max message length is 512 including CRLF
752 (let* ((response (if noticep "NOTICE" "PRIVMSG"))
753 (oversize (> (length message) rcirc-max-message-length))
755 (substring message 0 rcirc-max-message-length)
757 (text (if (string= text "")
761 (substring message rcirc-max-message-length))))
762 (rcirc-get-buffer-create process target)
763 (rcirc-send-string process (concat response " " target " :" text))
765 (rcirc-print process (rcirc-nick process) response target text))
766 (when more (rcirc-send-message process target more noticep))))
768 (defvar rcirc-input-ring nil)
769 (defvar rcirc-input-ring-index 0)
770 (defun rcirc-prev-input-string (arg)
771 (ring-ref rcirc-input-ring (+ rcirc-input-ring-index arg)))
773 (defun rcirc-insert-prev-input (arg)
775 (when (<= rcirc-prompt-end-marker (point))
776 (delete-region rcirc-prompt-end-marker (point-max))
777 (insert (rcirc-prev-input-string 0))
778 (setq rcirc-input-ring-index (1+ rcirc-input-ring-index))))
780 (defun rcirc-insert-next-input (arg)
782 (when (<= rcirc-prompt-end-marker (point))
783 (delete-region rcirc-prompt-end-marker (point-max))
784 (setq rcirc-input-ring-index (1- rcirc-input-ring-index))
785 (insert (rcirc-prev-input-string -1))))
787 (defvar rcirc-server-commands
788 '("/admin" "/away" "/connect" "/die" "/error" "/info"
789 "/invite" "/ison" "/join" "/kick" "/kill" "/links"
790 "/list" "/lusers" "/mode" "/motd" "/names" "/nick"
791 "/notice" "/oper" "/part" "/pass" "/ping" "/pong"
792 "/privmsg" "/quit" "/rehash" "/restart" "/service" "/servlist"
793 "/server" "/squery" "/squit" "/stats" "/summon" "/time"
794 "/topic" "/trace" "/user" "/userhost" "/users" "/version"
795 "/wallops" "/who" "/whois" "/whowas")
796 "A list of user commands by IRC server.
797 The value defaults to RFCs 1459 and 2812.")
799 ;; /me and /ctcp are not defined by `defun-rcirc-command'.
800 (defvar rcirc-client-commands '("/me" "/ctcp")
801 "A list of user commands defined by IRC client rcirc.
802 The list is updated automatically by `defun-rcirc-command'.")
804 (defun rcirc-completion-at-point ()
805 "Function used for `completion-at-point-functions' in `rcirc-mode'."
806 (let* ((beg (save-excursion
807 (if (re-search-backward " " rcirc-prompt-end-marker t)
809 rcirc-prompt-end-marker)))
810 (table (if (and (= beg rcirc-prompt-end-marker)
811 (eq (char-after beg) ?/))
814 (sort (copy-sequence rcirc-client-commands) 'string-lessp)
815 (sort (copy-sequence rcirc-server-commands) 'string-lessp)))
816 (rcirc-channel-nicks (rcirc-buffer-process) rcirc-target))))
817 (list beg (point) table)))
819 (defvar rcirc-completions nil)
820 (defvar rcirc-completion-start nil)
822 (defun rcirc-complete ()
823 "Cycle through completions from list of nicks in channel or IRC commands.
824 IRC command completion is performed only if '/' is the first input char."
826 (if (eq last-command this-command)
827 (setq rcirc-completions
828 (append (cdr rcirc-completions) (list (car rcirc-completions))))
829 (let ((completion-ignore-case t)
830 (table (rcirc-completion-at-point)))
831 (setq rcirc-completion-start (car table))
832 (setq rcirc-completions
833 (all-completions (buffer-substring rcirc-completion-start
836 (let ((completion (car rcirc-completions)))
838 (delete-region rcirc-completion-start (point))
841 ((= (aref completion 0) ?/) (concat completion " "))
842 ((= rcirc-completion-start rcirc-prompt-end-marker)
843 (format rcirc-nick-completion-format completion))
846 (defun set-rcirc-decode-coding-system (coding-system)
847 "Set the decode coding system used in this channel."
848 (interactive "zCoding system for incoming messages: ")
849 (setq rcirc-decode-coding-system coding-system))
851 (defun set-rcirc-encode-coding-system (coding-system)
852 "Set the encode coding system used in this channel."
853 (interactive "zCoding system for outgoing messages: ")
854 (setq rcirc-encode-coding-system coding-system))
856 (defvar rcirc-mode-map (make-sparse-keymap)
857 "Keymap for rcirc mode.")
859 (define-key rcirc-mode-map (kbd "RET") 'rcirc-send-input)
860 (define-key rcirc-mode-map (kbd "M-p") 'rcirc-insert-prev-input)
861 (define-key rcirc-mode-map (kbd "M-n") 'rcirc-insert-next-input)
862 (define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete)
863 (define-key rcirc-mode-map (kbd "C-c C-b") 'rcirc-browse-url)
864 (define-key rcirc-mode-map (kbd "C-c C-c") 'rcirc-edit-multiline)
865 (define-key rcirc-mode-map (kbd "C-c C-j") 'rcirc-cmd-join)
866 (define-key rcirc-mode-map (kbd "C-c C-k") 'rcirc-cmd-kick)
867 (define-key rcirc-mode-map (kbd "C-c C-l") 'rcirc-toggle-low-priority)
868 (define-key rcirc-mode-map (kbd "C-c C-d") 'rcirc-cmd-mode)
869 (define-key rcirc-mode-map (kbd "C-c C-m") 'rcirc-cmd-msg)
870 (define-key rcirc-mode-map (kbd "C-c C-r") 'rcirc-cmd-nick) ; rename
871 (define-key rcirc-mode-map (kbd "C-c C-o") 'rcirc-omit-mode)
872 (define-key rcirc-mode-map (kbd "M-o") 'rcirc-omit-mode)
873 (define-key rcirc-mode-map (kbd "C-c C-p") 'rcirc-cmd-part)
874 (define-key rcirc-mode-map (kbd "C-c C-q") 'rcirc-cmd-query)
875 (define-key rcirc-mode-map (kbd "C-c C-t") 'rcirc-cmd-topic)
876 (define-key rcirc-mode-map (kbd "C-c C-n") 'rcirc-cmd-names)
877 (define-key rcirc-mode-map (kbd "C-c C-w") 'rcirc-cmd-whois)
878 (define-key rcirc-mode-map (kbd "C-c C-x") 'rcirc-cmd-quit)
879 (define-key rcirc-mode-map (kbd "C-c TAB") ; C-i
880 'rcirc-toggle-ignore-buffer-activity)
881 (define-key rcirc-mode-map (kbd "C-c C-s") 'rcirc-switch-to-server-buffer)
882 (define-key rcirc-mode-map (kbd "C-c C-a") 'rcirc-jump-to-first-unread-line)
884 (defvar rcirc-browse-url-map (make-sparse-keymap)
885 "Keymap used for browsing URLs in `rcirc-mode'.")
887 (define-key rcirc-browse-url-map (kbd "RET") 'rcirc-browse-url-at-point)
888 (define-key rcirc-browse-url-map (kbd "<mouse-2>") 'rcirc-browse-url-at-mouse)
889 (define-key rcirc-browse-url-map [follow-link] 'mouse-face)
891 (defvar rcirc-short-buffer-name nil
892 "Generated abbreviation to use to indicate buffer activity.")
894 (defvar rcirc-mode-hook nil
895 "Hook run when setting up rcirc buffer.")
897 (defvar rcirc-last-post-time nil)
899 (defvar rcirc-log-alist nil
900 "Alist of lines to log to disk when `rcirc-log-flag' is non-nil.
901 Each element looks like (FILENAME . TEXT).")
903 (defvar rcirc-current-line 0
904 "The current number of responses printed in this channel.
905 This number is independent of the number of lines in the buffer.")
907 (defun rcirc-mode (process target)
908 ;; FIXME: Use define-derived-mode.
909 "Major mode for IRC channel buffers.
912 (kill-all-local-variables)
913 (use-local-map rcirc-mode-map)
914 (setq mode-name "rcirc")
915 (setq major-mode 'rcirc-mode)
916 (setq mode-line-process nil)
918 (make-local-variable 'rcirc-input-ring)
919 (setq rcirc-input-ring (make-ring rcirc-input-ring-size))
920 (make-local-variable 'rcirc-server-buffer)
921 (setq rcirc-server-buffer (process-buffer process))
922 (make-local-variable 'rcirc-target)
923 (setq rcirc-target target)
924 (make-local-variable 'rcirc-topic)
925 (setq rcirc-topic nil)
926 (make-local-variable 'rcirc-last-post-time)
927 (setq rcirc-last-post-time (current-time))
928 (make-local-variable 'fill-paragraph-function)
929 (setq fill-paragraph-function 'rcirc-fill-paragraph)
930 (make-local-variable 'rcirc-recent-quit-alist)
931 (setq rcirc-recent-quit-alist nil)
932 (make-local-variable 'rcirc-current-line)
933 (setq rcirc-current-line 0)
935 (make-local-variable 'rcirc-short-buffer-name)
936 (setq rcirc-short-buffer-name nil)
937 (make-local-variable 'rcirc-urls)
938 (setq use-hard-newlines t)
940 ;; setup for omitting responses
941 (setq buffer-invisibility-spec '())
942 (setq buffer-display-table (make-display-table))
943 (set-display-table-slot buffer-display-table 4
944 (let ((glyph (make-glyph-code
945 ?. 'font-lock-keyword-face)))
946 (make-vector 3 glyph)))
948 (make-local-variable 'rcirc-decode-coding-system)
949 (make-local-variable 'rcirc-encode-coding-system)
950 (dolist (i rcirc-coding-system-alist)
951 (let ((chan (if (consp (car i)) (caar i) (car i)))
952 (serv (if (consp (car i)) (cdar i) "")))
953 (when (and (string-match chan (or target ""))
954 (string-match serv (rcirc-server-name process)))
955 (setq rcirc-decode-coding-system (if (consp (cdr i)) (cadr i) (cdr i))
956 rcirc-encode-coding-system (if (consp (cdr i)) (cddr i) (cdr i))))))
958 ;; setup the prompt and markers
959 (make-local-variable 'rcirc-prompt-start-marker)
960 (setq rcirc-prompt-start-marker (make-marker))
961 (set-marker rcirc-prompt-start-marker (point-max))
962 (make-local-variable 'rcirc-prompt-end-marker)
963 (setq rcirc-prompt-end-marker (make-marker))
964 (set-marker rcirc-prompt-end-marker (point-max))
965 (rcirc-update-prompt)
966 (goto-char rcirc-prompt-end-marker)
967 (make-local-variable 'overlay-arrow-position)
968 (setq overlay-arrow-position (make-marker))
969 (set-marker overlay-arrow-position nil)
971 ;; if the user changes the major mode or kills the buffer, there is
972 ;; cleanup work to do
973 (add-hook 'change-major-mode-hook 'rcirc-change-major-mode-hook nil t)
974 (add-hook 'kill-buffer-hook 'rcirc-kill-buffer-hook nil t)
976 ;; add to buffer list, and update buffer abbrevs
977 (when target ; skip server buffer
978 (let ((buffer (current-buffer)))
979 (with-rcirc-process-buffer process
980 (setq rcirc-buffer-alist (cons (cons target buffer)
981 rcirc-buffer-alist))))
982 (rcirc-update-short-buffer-names))
984 (add-hook 'completion-at-point-functions
985 'rcirc-completion-at-point nil 'local)
987 (run-mode-hooks 'rcirc-mode-hook))
989 (defun rcirc-update-prompt (&optional all)
990 "Reset the prompt string in the current buffer.
992 If ALL is non-nil, update prompts in all IRC buffers."
994 (mapc (lambda (process)
995 (mapc (lambda (buffer)
996 (with-current-buffer buffer
997 (rcirc-update-prompt)))
998 (with-rcirc-process-buffer process
999 (mapcar 'cdr rcirc-buffer-alist))))
1000 (rcirc-process-list))
1001 (let ((inhibit-read-only t)
1002 (prompt (or rcirc-prompt "")))
1005 (replace-regexp-in-string (car rep) (cdr rep) prompt)))
1006 (list (cons "%n" (rcirc-buffer-nick))
1007 (cons "%s" (with-rcirc-server-buffer rcirc-server-name))
1008 (cons "%t" (or rcirc-target ""))))
1010 (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker)
1011 (goto-char rcirc-prompt-start-marker)
1012 (let ((start (point)))
1013 (insert-before-markers prompt)
1014 (set-marker rcirc-prompt-start-marker start)
1015 (when (not (zerop (- rcirc-prompt-end-marker
1016 rcirc-prompt-start-marker)))
1017 (add-text-properties rcirc-prompt-start-marker
1018 rcirc-prompt-end-marker
1019 (list 'face 'rcirc-prompt
1020 'read-only t 'field t
1021 'front-sticky t 'rear-nonsticky t))))))))
1023 (defun rcirc-set-changed (option value)
1024 "Set OPTION to VALUE and do updates after a customization change."
1025 (set-default option value)
1026 (cond ((eq option 'rcirc-prompt)
1027 (rcirc-update-prompt 'all))
1029 (error "Bad option %s" option))))
1031 (defun rcirc-channel-p (target)
1032 "Return t if TARGET is a channel name."
1034 (not (zerop (length target)))
1035 (or (eq (aref target 0) ?#)
1036 (eq (aref target 0) ?&))))
1038 (defcustom rcirc-log-directory "~/.emacs.d/rcirc-log"
1039 "Directory to keep IRC logfiles."
1043 (defcustom rcirc-log-flag nil
1044 "Non-nil means log IRC activity to disk.
1045 Logfiles are kept in `rcirc-log-directory'."
1049 (defun rcirc-kill-buffer-hook ()
1050 "Part the channel when killing an rcirc buffer."
1051 (when (eq major-mode 'rcirc-mode)
1052 (when (and rcirc-log-flag
1053 rcirc-log-directory)
1055 (rcirc-clean-up-buffer "Killed buffer")))
1057 (defun rcirc-change-major-mode-hook ()
1058 "Part the channel when changing the major-mode."
1059 (rcirc-clean-up-buffer "Changed major mode"))
1061 (defun rcirc-clean-up-buffer (reason)
1062 (let ((buffer (current-buffer)))
1063 (rcirc-clear-activity buffer)
1064 (when (and (rcirc-buffer-process)
1065 (eq (process-status (rcirc-buffer-process)) 'open))
1066 (with-rcirc-server-buffer
1067 (setq rcirc-buffer-alist
1068 (rassq-delete-all buffer rcirc-buffer-alist)))
1069 (rcirc-update-short-buffer-names)
1070 (if (rcirc-channel-p rcirc-target)
1071 (rcirc-send-string (rcirc-buffer-process)
1072 (concat "PART " rcirc-target " :" reason))
1074 (rcirc-remove-nick-channel (rcirc-buffer-process)
1077 (setq rcirc-target nil)))
1079 (defun rcirc-generate-new-buffer-name (process target)
1080 "Return a buffer name based on PROCESS and TARGET.
1081 This is used for the initial name given to IRC buffers."
1082 (substring-no-properties
1084 (concat target "@" (process-name process))
1085 (concat "*" (process-name process) "*"))))
1087 (defun rcirc-get-buffer (process target &optional server)
1088 "Return the buffer associated with the PROCESS and TARGET.
1090 If optional argument SERVER is non-nil, return the server buffer
1091 if there is no existing buffer for TARGET, otherwise return nil."
1092 (with-rcirc-process-buffer process
1095 (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t))))
1096 (or buffer (when server (current-buffer)))))))
1098 (defun rcirc-get-buffer-create (process target)
1099 "Return the buffer associated with the PROCESS and TARGET.
1100 Create the buffer if it doesn't exist."
1101 (let ((buffer (rcirc-get-buffer process target)))
1102 (if (and buffer (buffer-live-p buffer))
1103 (with-current-buffer buffer
1104 (when (not rcirc-target)
1105 (setq rcirc-target target))
1107 ;; create the buffer
1108 (with-rcirc-process-buffer process
1109 (let ((new-buffer (get-buffer-create
1110 (rcirc-generate-new-buffer-name process target))))
1111 (with-current-buffer new-buffer
1112 (rcirc-mode process target)
1113 (rcirc-put-nick-channel process (rcirc-nick process) target
1114 rcirc-current-line))
1117 (defun rcirc-send-input ()
1118 "Send input to target associated with the current buffer."
1120 (if (< (point) rcirc-prompt-end-marker)
1121 ;; copy the line down to the input area
1124 (let ((start (if (eq (point) (point-min))
1126 (if (get-text-property (1- (point)) 'hard)
1128 (previous-single-property-change (point) 'hard))))
1129 (end (next-single-property-change (1+ (point)) 'hard)))
1130 (goto-char (point-max))
1131 (insert (replace-regexp-in-string
1133 (buffer-substring-no-properties start end)))))
1135 (goto-char (point-max))
1136 (when (not (equal 0 (- (point) rcirc-prompt-end-marker)))
1137 ;; delete a trailing newline
1138 (when (eq (point) (point-at-bol))
1140 (let ((input (buffer-substring-no-properties
1141 rcirc-prompt-end-marker (point))))
1142 (dolist (line (split-string input "\n"))
1143 (rcirc-process-input-line line))
1144 ;; add to input-ring
1146 (ring-insert rcirc-input-ring input)
1147 (setq rcirc-input-ring-index 0))))))
1149 (defun rcirc-fill-paragraph (&optional arg)
1151 (when (> (point) rcirc-prompt-end-marker)
1153 (narrow-to-region rcirc-prompt-end-marker (point-max))
1154 (let ((fill-column rcirc-max-message-length))
1155 (fill-region (point-min) (point-max))))))
1157 (defun rcirc-process-input-line (line)
1158 (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line)
1159 (rcirc-process-command (match-string 1 line)
1160 (match-string 2 line)
1162 (rcirc-process-message line)))
1164 (defun rcirc-process-message (line)
1165 (if (not rcirc-target)
1166 (message "Not joined (no target)")
1167 (delete-region rcirc-prompt-end-marker (point))
1168 (rcirc-send-message (rcirc-buffer-process) rcirc-target line)
1169 (setq rcirc-last-post-time (current-time))))
1171 (defun rcirc-process-command (command args line)
1172 (if (eq (aref command 0) ?/)
1173 ;; "//text" will send "/text" as a message
1174 (rcirc-process-message (substring line 1))
1175 (let ((fun (intern-soft (concat "rcirc-cmd-" command)))
1176 (process (rcirc-buffer-process)))
1178 (with-current-buffer (current-buffer)
1179 (delete-region rcirc-prompt-end-marker (point))
1180 (if (string= command "me")
1181 (rcirc-print process (rcirc-buffer-nick)
1182 "ACTION" rcirc-target args)
1183 (rcirc-print process (rcirc-buffer-nick)
1184 "COMMAND" rcirc-target line))
1185 (set-marker rcirc-prompt-end-marker (point))
1187 (funcall fun args process rcirc-target)
1188 (rcirc-send-string process
1189 (concat command " :" args)))))))
1191 (defvar rcirc-parent-buffer nil)
1192 (defvar rcirc-window-configuration nil)
1193 (defun rcirc-edit-multiline ()
1194 "Move current edit to a dedicated buffer."
1196 (let ((pos (1+ (- (point) rcirc-prompt-end-marker))))
1197 (goto-char (point-max))
1198 (let ((text (buffer-substring-no-properties rcirc-prompt-end-marker
1200 (parent (buffer-name)))
1201 (delete-region rcirc-prompt-end-marker (point))
1202 (setq rcirc-window-configuration (current-window-configuration))
1203 (pop-to-buffer (concat "*multiline " parent "*"))
1204 (funcall rcirc-multiline-major-mode)
1205 (rcirc-multiline-minor-mode 1)
1206 (setq rcirc-parent-buffer parent)
1208 (and (> pos 0) (goto-char pos))
1209 (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent))))
1211 (defvar rcirc-multiline-minor-mode-map (make-sparse-keymap)
1212 "Keymap for multiline mode in rcirc.")
1213 (define-key rcirc-multiline-minor-mode-map
1214 (kbd "C-c C-c") 'rcirc-multiline-minor-submit)
1215 (define-key rcirc-multiline-minor-mode-map
1216 (kbd "C-x C-s") 'rcirc-multiline-minor-submit)
1217 (define-key rcirc-multiline-minor-mode-map
1218 (kbd "C-c C-k") 'rcirc-multiline-minor-cancel)
1219 (define-key rcirc-multiline-minor-mode-map
1220 (kbd "ESC ESC ESC") 'rcirc-multiline-minor-cancel)
1222 (define-minor-mode rcirc-multiline-minor-mode
1223 "Minor mode for editing multiple lines in rcirc."
1225 :lighter " rcirc-mline"
1226 :keymap rcirc-multiline-minor-mode-map
1229 (make-local-variable 'rcirc-parent-buffer)
1230 (put 'rcirc-parent-buffer 'permanent-local t)
1231 (setq fill-column rcirc-max-message-length))
1233 (defun rcirc-multiline-minor-submit ()
1234 "Send the text in buffer back to parent buffer."
1236 (untabify (point-min) (point-max))
1237 (let ((text (buffer-substring (point-min) (point-max)))
1238 (buffer (current-buffer))
1240 (set-buffer rcirc-parent-buffer)
1241 (goto-char (point-max))
1243 (kill-buffer buffer)
1244 (set-window-configuration rcirc-window-configuration)
1245 (goto-char (+ rcirc-prompt-end-marker (1- pos)))))
1247 (defun rcirc-multiline-minor-cancel ()
1248 "Cancel the multiline edit."
1250 (kill-buffer (current-buffer))
1251 (set-window-configuration rcirc-window-configuration))
1253 (defun rcirc-any-buffer (process)
1254 "Return a buffer for PROCESS, either the one selected or the process buffer."
1255 (if rcirc-always-use-server-buffer-flag
1256 (process-buffer process)
1257 (let ((buffer (window-buffer (selected-window))))
1259 (with-current-buffer buffer
1260 (and (eq major-mode 'rcirc-mode)
1261 (eq (rcirc-buffer-process) process))))
1263 (process-buffer process)))))
1265 (defcustom rcirc-response-formats
1266 '(("PRIVMSG" . "<%N> %m")
1267 ("NOTICE" . "-%N- %m")
1268 ("ACTION" . "[%N %m]")
1270 ("ERROR" . "%fw!!! %m")
1271 (t . "%fp*** %fs%n %r %m"))
1272 "An alist of formats used for printing responses.
1273 The format is looked up using the response-type as a key;
1274 if no match is found, the default entry (with a key of `t') is used.
1276 The entry's value part should be a string, which is inserted with
1277 the of the following escape sequences replaced by the described values:
1280 %n The sender's nick
1281 %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick')
1282 %r The response-type
1284 %fw Following text uses the face `font-lock-warning-face'
1285 %fp Following text uses the face `rcirc-server-prefix'
1286 %fs Following text uses the face `rcirc-server'
1287 %f[FACE] Following text uses the face FACE
1288 %f- Following text uses the default face
1289 %% A literal `%' character"
1290 :type '(alist :key-type (choice (string :tag "Type")
1291 (const :tag "Default" t))
1295 (defcustom rcirc-omit-responses
1296 '("JOIN" "PART" "QUIT" "NICK")
1297 "Responses which will be hidden when `rcirc-omit-mode' is enabled."
1298 :type '(repeat string)
1301 (defun rcirc-format-response-string (process sender response target text)
1302 "Return a nicely-formatted response string, incorporating TEXT
1303 \(and perhaps other arguments). The specific formatting used
1304 is found by looking up RESPONSE in `rcirc-response-formats'."
1306 (insert (or (cdr (assoc response rcirc-response-formats))
1307 (cdr (assq t rcirc-response-formats))))
1308 (goto-char (point-min))
1309 (let ((start (point-min))
1310 (sender (if (or (not sender)
1311 (string= (rcirc-server-name process) sender))
1315 (while (re-search-forward "%\\(\\(f\\(.\\)\\)\\|\\(.\\)\\)" nil t)
1316 (rcirc-add-face start (match-beginning 0) face)
1317 (setq start (match-beginning 0))
1319 (case (aref (match-string 1) 0)
1321 (case (string-to-char (match-string 3))
1322 (?w 'font-lock-warning-face)
1323 (?p 'rcirc-server-prefix)
1328 (?N (let ((my-nick (rcirc-nick process)))
1330 (with-syntax-table rcirc-nick-syntax-table
1331 (rcirc-facify sender
1332 (cond ((string= sender my-nick)
1334 ((and rcirc-bright-nicks
1336 (regexp-opt rcirc-bright-nicks
1340 ((and rcirc-dim-nicks
1342 (regexp-opt rcirc-dim-nicks
1347 'rcirc-other-nick)))))))
1348 (?m (propertize text 'rcirc-text text))
1351 (t (concat "UNKNOWN CODE:" (match-string 0))))
1353 (rcirc-add-face (match-beginning 0) (match-end 0) face))
1354 (rcirc-add-face start (match-beginning 0) face))
1355 (buffer-substring (point-min) (point-max))))
1357 (defun rcirc-target-buffer (process sender response target text)
1358 "Return a buffer to print the server response."
1359 (assert (not (bufferp target)))
1360 (with-rcirc-process-buffer process
1362 (rcirc-any-buffer process))
1363 ((not (rcirc-channel-p target))
1364 ;; message from another user
1365 (if (or (string= response "PRIVMSG")
1366 (string= response "ACTION"))
1367 (rcirc-get-buffer-create process (if (string= sender rcirc-nick)
1370 (rcirc-get-buffer process target t)))
1371 ((or (rcirc-get-buffer process target)
1372 (rcirc-any-buffer process))))))
1374 (defvar rcirc-activity-types nil)
1375 (make-variable-buffer-local 'rcirc-activity-types)
1376 (defvar rcirc-last-sender nil)
1377 (make-variable-buffer-local 'rcirc-last-sender)
1379 (defcustom rcirc-omit-threshold 100
1380 "Number of lines since last activity from a nick before `rcirc-omit-responses' are omitted."
1384 (defcustom rcirc-log-process-buffers nil
1385 "Non-nil if rcirc process buffers should be logged to disk."
1390 (defun rcirc-last-quit-line (process nick target)
1391 "Return the line number where NICK left TARGET.
1392 Returns nil if the information is not recorded."
1393 (let ((chanbuf (rcirc-get-buffer process target)))
1395 (cdr (assoc-string nick (with-current-buffer chanbuf
1396 rcirc-recent-quit-alist))))))
1398 (defun rcirc-last-line (process nick target)
1399 "Return the line from the last activity from NICK in TARGET."
1400 (let* ((chanbuf (rcirc-get-buffer process target))
1401 (line (or (cdr (assoc-string target
1402 (gethash nick (with-rcirc-server-buffer
1403 rcirc-nick-table)) t))
1404 (rcirc-last-quit-line process nick target))))
1407 ;;(message "line is nil for %s in %s" nick target)
1410 (defun rcirc-elapsed-lines (process nick target)
1411 "Return the number of lines since activity from NICK in TARGET."
1412 (let ((last-activity-line (rcirc-last-line process nick target)))
1413 (when (and last-activity-line
1414 (> last-activity-line 0))
1415 (- rcirc-current-line last-activity-line))))
1417 (defvar rcirc-markup-text-functions
1418 '(rcirc-markup-attributes
1419 rcirc-markup-my-nick
1421 rcirc-markup-keywords
1422 rcirc-markup-bright-nicks)
1424 "List of functions used to manipulate text before it is printed.
1426 Each function takes two arguments, SENDER, and RESPONSE. The
1427 buffer is narrowed with the text to be printed and the point is
1428 at the beginning of the `rcirc-text' propertized text.")
1430 (defun rcirc-print (process sender response target text &optional activity)
1431 "Print TEXT in the buffer associated with TARGET.
1432 Format based on SENDER and RESPONSE. If ACTIVITY is non-nil,
1434 (or text (setq text ""))
1435 (unless (and (or (member sender rcirc-ignore-list)
1436 (member (with-syntax-table rcirc-nick-syntax-table
1437 (when (string-match "^\\([^/]\\w*\\)[:,]" text)
1438 (match-string 1 text)))
1440 ;; do not ignore if we sent the message
1441 (not (string= sender (rcirc-nick process))))
1442 (let* ((buffer (rcirc-target-buffer process sender response target text))
1443 (inhibit-read-only t))
1444 (with-current-buffer buffer
1445 (let ((moving (= (point) rcirc-prompt-end-marker))
1446 (old-point (point-marker))
1447 (fill-start (marker-position rcirc-prompt-start-marker)))
1449 (unless (string= sender (rcirc-nick process))
1450 ;; only decode text from other senders, not ours
1451 (setq text (decode-coding-string text rcirc-decode-coding-system))
1452 ;; mark the line with overlay arrow
1453 (unless (or (marker-position overlay-arrow-position)
1454 (get-buffer-window (current-buffer))
1455 (member response rcirc-omit-responses))
1456 (set-marker overlay-arrow-position
1457 (marker-position rcirc-prompt-start-marker))))
1459 ;; temporarily set the marker insertion-type because
1460 ;; insert-before-markers results in hidden text in new buffers
1461 (goto-char rcirc-prompt-start-marker)
1462 (set-marker-insertion-type rcirc-prompt-start-marker t)
1463 (set-marker-insertion-type rcirc-prompt-end-marker t)
1465 (let ((start (point)))
1466 (insert (rcirc-format-response-string process sender response nil
1468 (propertize "\n" 'hard t))
1470 ;; squeeze spaces out of text before rcirc-text
1471 (fill-region fill-start
1472 (1- (or (next-single-property-change fill-start
1474 rcirc-prompt-end-marker)))
1476 ;; run markup functions
1479 (narrow-to-region start rcirc-prompt-start-marker)
1480 (goto-char (or (next-single-property-change start 'rcirc-text)
1482 (when (rcirc-buffer-process)
1483 (save-excursion (rcirc-markup-timestamp sender response))
1484 (dolist (fn rcirc-markup-text-functions)
1485 (save-excursion (funcall fn sender response)))
1486 (when rcirc-fill-flag
1487 (save-excursion (rcirc-markup-fill sender response))))
1489 (when rcirc-read-only-flag
1490 (add-text-properties (point-min) (point-max)
1491 '(read-only t front-sticky t))))
1492 ;; make text omittable
1493 (let ((last-activity-lines (rcirc-elapsed-lines process sender target)))
1494 (if (and (not (string= (rcirc-nick process) sender))
1495 (member response rcirc-omit-responses)
1496 (or (not last-activity-lines)
1497 (< rcirc-omit-threshold last-activity-lines)))
1498 (put-text-property (1- start) (1- rcirc-prompt-start-marker)
1499 'invisible 'rcirc-omit)
1500 ;; otherwise increment the line count
1501 (setq rcirc-current-line (1+ rcirc-current-line))))))
1503 (set-marker-insertion-type rcirc-prompt-start-marker nil)
1504 (set-marker-insertion-type rcirc-prompt-end-marker nil)
1506 ;; truncate buffer if it is very long
1508 (when (and rcirc-buffer-maximum-lines
1509 (> rcirc-buffer-maximum-lines 0)
1510 (= (forward-line (- rcirc-buffer-maximum-lines)) 0))
1511 (delete-region (point-min) (point))))
1513 ;; set the window point for buffers show in windows
1514 (walk-windows (lambda (w)
1515 (when (and (not (eq (selected-window) w))
1516 (eq (current-buffer)
1518 (>= (window-point w)
1519 rcirc-prompt-end-marker))
1520 (set-window-point w (point-max))))
1523 ;; restore the point
1524 (goto-char (if moving rcirc-prompt-end-marker old-point))
1526 ;; keep window on bottom line if it was already there
1527 (when rcirc-scroll-show-maximum-output
1528 (walk-windows (lambda (w)
1529 (when (eq (window-buffer w) (current-buffer))
1530 (with-current-buffer (window-buffer w)
1531 (when (eq major-mode 'rcirc-mode)
1532 (with-selected-window w
1533 (when (<= (- (window-height)
1534 (count-screen-lines (window-point)
1541 ;; flush undo (can we do something smarter here?)
1542 (buffer-disable-undo)
1543 (buffer-enable-undo))
1545 ;; record modeline activity
1547 (not rcirc-ignore-buffer-activity-flag)
1548 (not (and rcirc-dim-nicks sender
1549 (string-match (regexp-opt rcirc-dim-nicks) sender)
1550 (rcirc-channel-p target))))
1551 (rcirc-record-activity (current-buffer)
1552 (when (not (rcirc-channel-p rcirc-target))
1555 (when (and rcirc-log-flag
1557 rcirc-log-process-buffers))
1558 (rcirc-log process sender response target text))
1560 (sit-for 0) ; displayed text before hook
1561 (run-hook-with-args 'rcirc-print-hooks
1562 process sender response target text)))))
1564 (defun rcirc-generate-log-filename (process target)
1566 (rcirc-generate-new-buffer-name process target)
1567 (process-name process)))
1569 (defcustom rcirc-log-filename-function 'rcirc-generate-log-filename
1570 "A function to generate the filename used by rcirc's logging facility.
1572 It is called with two arguments, PROCESS and TARGET (see
1573 `rcirc-generate-new-buffer-name' for their meaning), and should
1574 return the filename, or nil if no logging is desired for this
1577 If the returned filename is absolute (`file-name-absolute-p'
1578 returns t), then it is used as-is, otherwise the resulting file
1579 is put into `rcirc-log-directory'.
1581 The filename is then cleaned using `convert-standard-filename' to
1582 guarantee valid filenames for the current OS."
1586 (defun rcirc-log (process sender response target text)
1587 "Record line in `rcirc-log', to be later written to disk."
1588 (let ((filename (funcall rcirc-log-filename-function process target)))
1589 (unless (null filename)
1590 (let ((cell (assoc-string filename rcirc-log-alist))
1591 (line (concat (format-time-string rcirc-time-format)
1592 (substring-no-properties
1593 (rcirc-format-response-string process sender
1594 response target text))
1597 (setcdr cell (concat (cdr cell) line))
1598 (setq rcirc-log-alist
1599 (cons (cons filename line) rcirc-log-alist)))))))
1601 (defun rcirc-log-write ()
1602 "Flush `rcirc-log-alist' data to disk.
1604 Log data is written to `rcirc-log-directory', except for
1605 log-files with absolute names (see `rcirc-log-filename-function')."
1606 (dolist (cell rcirc-log-alist)
1607 (let ((filename (convert-standard-filename
1608 (expand-file-name (car cell)
1609 rcirc-log-directory)))
1610 (coding-system-for-write 'utf-8))
1611 (make-directory (file-name-directory filename) t)
1614 (write-region (point-min) (point-max) filename t 'quiet))))
1615 (setq rcirc-log-alist nil))
1617 (defun rcirc-view-log-file ()
1618 "View logfile corresponding to the current buffer."
1620 (find-file-other-window
1621 (expand-file-name (funcall rcirc-log-filename-function
1622 (rcirc-buffer-process) rcirc-target)
1623 rcirc-log-directory)))
1625 (defun rcirc-join-channels (process channels)
1627 (save-window-excursion
1628 (dolist (channel channels)
1629 (with-rcirc-process-buffer process
1630 (rcirc-cmd-join channel process)))))
1633 (defvar rcirc-nick-prefix-chars "~&@%+")
1634 (defun rcirc-user-nick (user)
1635 "Return the nick from USER. Remove any non-nick junk."
1637 (if (string-match (concat "^[" rcirc-nick-prefix-chars
1638 "]?\\([^! ]+\\)!?") (or user ""))
1639 (match-string 1 user)
1642 (defun rcirc-nick-channels (process nick)
1643 "Return list of channels for NICK."
1644 (with-rcirc-process-buffer process
1645 (mapcar (lambda (x) (car x))
1646 (gethash nick rcirc-nick-table))))
1648 (defun rcirc-put-nick-channel (process nick channel &optional line)
1649 "Add CHANNEL to list associated with NICK.
1650 Update the associated linestamp if LINE is non-nil.
1652 If the record doesn't exist, and LINE is nil, set the linestamp
1654 (let ((nick (rcirc-user-nick nick)))
1655 (with-rcirc-process-buffer process
1656 (let* ((chans (gethash nick rcirc-nick-table))
1657 (record (assoc-string channel chans t)))
1659 (when line (setcdr record line))
1660 (puthash nick (cons (cons channel (or line 0))
1662 rcirc-nick-table))))))
1664 (defun rcirc-nick-remove (process nick)
1665 "Remove NICK from table."
1666 (with-rcirc-process-buffer process
1667 (remhash nick rcirc-nick-table)))
1669 (defun rcirc-remove-nick-channel (process nick channel)
1670 "Remove the CHANNEL from list associated with NICK."
1671 (with-rcirc-process-buffer process
1672 (let* ((chans (gethash nick rcirc-nick-table))
1674 ;; instead of assoc-string-delete-all:
1675 (let ((record (assoc-string channel chans t)))
1677 (setcar record 'delete)
1678 (assq-delete-all 'delete chans)))))
1680 (puthash nick newchans rcirc-nick-table)
1681 (remhash nick rcirc-nick-table)))))
1683 (defun rcirc-channel-nicks (process target)
1684 "Return the list of nicks associated with TARGET sorted by last activity."
1686 (if (rcirc-channel-p target)
1687 (with-rcirc-process-buffer process
1691 (let ((record (assoc-string target v t)))
1693 (setq nicks (cons (cons k (cdr record)) nicks)))))
1695 (mapcar (lambda (x) (car x))
1696 (sort nicks (lambda (x y)
1697 (let ((lx (or (cdr x) 0))
1698 (ly (or (cdr y) 0)))
1702 (defun rcirc-ignore-update-automatic (nick)
1703 "Remove NICK from `rcirc-ignore-list'
1704 if NICK is also on `rcirc-ignore-list-automatic'."
1705 (when (member nick rcirc-ignore-list-automatic)
1706 (setq rcirc-ignore-list-automatic
1707 (delete nick rcirc-ignore-list-automatic)
1709 (delete nick rcirc-ignore-list))))
1711 (defun rcirc-nickname< (s1 s2)
1712 "Return t if IRC nickname S1 is less than S2, and nil otherwise.
1713 Operator nicknames (@) are considered less than voiced
1714 nicknames (+). Any other nicknames are greater than voiced
1715 nicknames. The comparison is case-insensitive."
1716 (setq s1 (downcase s1)
1718 (let* ((s1-op (eq ?@ (string-to-char s1)))
1719 (s2-op (eq ?@ (string-to-char s2))))
1722 (string< (substring s1 1) (substring s2 1))
1728 (defun rcirc-sort-nicknames-join (input sep)
1729 "Return a string of sorted nicknames.
1730 INPUT is a string containing nicknames separated by SEP.
1731 This function does not alter the INPUT string."
1732 (let* ((parts (split-string input sep t))
1733 (sorted (sort parts 'rcirc-nickname<)))
1734 (mapconcat 'identity sorted sep)))
1736 ;;; activity tracking
1737 (defvar rcirc-track-minor-mode-map (make-sparse-keymap)
1738 "Keymap for rcirc track minor mode.")
1740 (define-key rcirc-track-minor-mode-map (kbd "C-c C-@") 'rcirc-next-active-buffer)
1741 (define-key rcirc-track-minor-mode-map (kbd "C-c C-SPC") 'rcirc-next-active-buffer)
1744 (define-minor-mode rcirc-track-minor-mode
1745 "Global minor mode for tracking activity in rcirc buffers."
1748 :keymap rcirc-track-minor-mode-map
1751 (or global-mode-string (setq global-mode-string '("")))
1752 ;; toggle the mode-line channel indicator
1753 (if rcirc-track-minor-mode
1755 (and (not (memq 'rcirc-activity-string global-mode-string))
1756 (setq global-mode-string
1757 (append global-mode-string '(rcirc-activity-string))))
1758 (add-hook 'window-configuration-change-hook
1759 'rcirc-window-configuration-change))
1760 (setq global-mode-string
1761 (delete 'rcirc-activity-string global-mode-string))
1762 (remove-hook 'window-configuration-change-hook
1763 'rcirc-window-configuration-change)))
1765 (or (assq 'rcirc-ignore-buffer-activity-flag minor-mode-alist)
1766 (setq minor-mode-alist
1767 (cons '(rcirc-ignore-buffer-activity-flag " Ignore") minor-mode-alist)))
1768 (or (assq 'rcirc-low-priority-flag minor-mode-alist)
1769 (setq minor-mode-alist
1770 (cons '(rcirc-low-priority-flag " LowPri") minor-mode-alist)))
1771 (or (assq 'rcirc-omit-mode minor-mode-alist)
1772 (setq minor-mode-alist
1773 (cons '(rcirc-omit-mode " Omit") minor-mode-alist)))
1775 (defun rcirc-toggle-ignore-buffer-activity ()
1776 "Toggle the value of `rcirc-ignore-buffer-activity-flag'."
1778 (setq rcirc-ignore-buffer-activity-flag
1779 (not rcirc-ignore-buffer-activity-flag))
1780 (message (if rcirc-ignore-buffer-activity-flag
1781 "Ignore activity in this buffer"
1782 "Notice activity in this buffer"))
1783 (force-mode-line-update))
1785 (defun rcirc-toggle-low-priority ()
1786 "Toggle the value of `rcirc-low-priority-flag'."
1788 (setq rcirc-low-priority-flag
1789 (not rcirc-low-priority-flag))
1790 (message (if rcirc-low-priority-flag
1791 "Activity in this buffer is low priority"
1792 "Activity in this buffer is normal priority"))
1793 (force-mode-line-update))
1795 (defun rcirc-omit-mode ()
1796 "Toggle the Rcirc-Omit mode.
1797 If enabled, \"uninteresting\" lines are not shown.
1798 Uninteresting lines are those whose responses are listed in
1799 `rcirc-omit-responses'."
1801 (setq rcirc-omit-mode (not rcirc-omit-mode))
1804 (add-to-invisibility-spec '(rcirc-omit . nil))
1805 (message "Rcirc-Omit mode enabled"))
1806 (remove-from-invisibility-spec '(rcirc-omit . nil))
1807 (message "Rcirc-Omit mode disabled"))
1808 (recenter (when (> (point) rcirc-prompt-start-marker) -1)))
1810 (defun rcirc-switch-to-server-buffer ()
1811 "Switch to the server buffer associated with current channel buffer."
1813 (switch-to-buffer rcirc-server-buffer))
1815 (defun rcirc-jump-to-first-unread-line ()
1816 "Move the point to the first unread line in this buffer."
1818 (if (marker-position overlay-arrow-position)
1819 (goto-char overlay-arrow-position)
1820 (message "No unread messages")))
1822 (defun rcirc-non-irc-buffer ()
1823 (let ((buflist (buffer-list))
1825 (while (and buflist (not buffer))
1826 (with-current-buffer (car buflist)
1827 (unless (or (eq major-mode 'rcirc-mode)
1828 (= ?\s (aref (buffer-name) 0)) ; internal buffers
1829 (get-buffer-window (current-buffer)))
1830 (setq buffer (current-buffer))))
1831 (setq buflist (cdr buflist)))
1834 (defun rcirc-next-active-buffer (arg)
1835 "Switch to the next rcirc buffer with activity.
1836 With prefix ARG, go to the next low priority buffer with activity."
1838 (let* ((pair (rcirc-split-activity rcirc-activity))
1841 (if (or (and (not arg) hipri)
1844 (switch-to-buffer (car (if arg lopri hipri)))
1845 (when (> (point) rcirc-prompt-start-marker)
1847 (if (eq major-mode 'rcirc-mode)
1848 (switch-to-buffer (rcirc-non-irc-buffer))
1849 (message "%s" (concat
1854 (key-description (this-command-keys))
1855 " for low priority activity."))))))))
1857 (defvar rcirc-activity-hooks nil
1858 "Hook to be run when there is channel activity.
1860 Functions are called with a single argument, the buffer with the
1861 activity. Only run if the buffer is not visible and
1862 `rcirc-ignore-buffer-activity-flag' is non-nil.")
1864 (defun rcirc-record-activity (buffer &optional type)
1865 "Record BUFFER activity with TYPE."
1866 (with-current-buffer buffer
1867 (let ((old-activity rcirc-activity)
1868 (old-types rcirc-activity-types))
1869 (when (not (get-buffer-window (current-buffer) t))
1870 (setq rcirc-activity
1871 (sort (add-to-list 'rcirc-activity (current-buffer))
1873 (let ((t1 (with-current-buffer b1 rcirc-last-post-time))
1874 (t2 (with-current-buffer b2 rcirc-last-post-time)))
1875 (time-less-p t2 t1)))))
1876 (pushnew type rcirc-activity-types)
1877 (unless (and (equal rcirc-activity old-activity)
1878 (member type old-types))
1879 (rcirc-update-activity-string)))))
1880 (run-hook-with-args 'rcirc-activity-hooks buffer))
1882 (defun rcirc-clear-activity (buffer)
1883 "Clear the BUFFER activity."
1884 (setq rcirc-activity (remove buffer rcirc-activity))
1885 (with-current-buffer buffer
1886 (setq rcirc-activity-types nil)))
1888 (defun rcirc-clear-unread (buffer)
1889 "Erase the last read message arrow from BUFFER."
1890 (when (buffer-live-p buffer)
1891 (with-current-buffer buffer
1892 (set-marker overlay-arrow-position nil))))
1894 (defun rcirc-split-activity (activity)
1895 "Return a cons cell with ACTIVITY split into (lopri . hipri)."
1897 (dolist (buf rcirc-activity)
1898 (with-current-buffer buf
1899 (if (and rcirc-low-priority-flag
1900 (not (member 'nick rcirc-activity-types)))
1901 (add-to-list 'lopri buf t)
1902 (add-to-list 'hipri buf t))))
1903 (cons lopri hipri)))
1905 (defvar rcirc-update-activity-string-hook nil
1906 "Hook run whenever the activity string is updated.")
1908 ;; TODO: add mouse properties
1909 (defun rcirc-update-activity-string ()
1910 "Update mode-line string."
1911 (let* ((pair (rcirc-split-activity rcirc-activity))
1914 (setq rcirc-activity-string
1915 (cond ((or hipri lopri)
1916 (concat (and hipri "[")
1917 (rcirc-activity-string hipri)
1918 (and hipri lopri ",")
1921 (rcirc-activity-string lopri)
1924 ((not (null (rcirc-process-list)))
1927 (run-hooks 'rcirc-update-activity-string-hook)))
1929 (defun rcirc-activity-string (buffers)
1930 (mapconcat (lambda (b)
1931 (let ((s (substring-no-properties (rcirc-short-buffer-name b))))
1932 (with-current-buffer b
1933 (dolist (type rcirc-activity-types)
1934 (rcirc-add-face 0 (length s)
1936 (nick 'rcirc-track-nick)
1937 (keyword 'rcirc-track-keyword))
1942 (defun rcirc-short-buffer-name (buffer)
1943 "Return a short name for BUFFER to use in the modeline indicator."
1944 (with-current-buffer buffer
1945 (or rcirc-short-buffer-name (buffer-name))))
1947 (defun rcirc-visible-buffers ()
1948 "Return a list of the visible buffers that are in rcirc-mode."
1950 (walk-windows (lambda (w)
1951 (with-current-buffer (window-buffer w)
1952 (when (eq major-mode 'rcirc-mode)
1953 (push (current-buffer) acc)))))
1956 (defvar rcirc-visible-buffers nil)
1957 (defun rcirc-window-configuration-change ()
1958 (unless (minibuffer-window-active-p (minibuffer-window))
1959 ;; delay this until command has finished to make sure window is
1960 ;; actually visible before clearing activity
1961 (add-hook 'post-command-hook 'rcirc-window-configuration-change-1)))
1963 (defun rcirc-window-configuration-change-1 ()
1964 ;; clear activity and overlay arrows
1965 (let* ((old-activity rcirc-activity)
1966 (hidden-buffers rcirc-visible-buffers))
1968 (setq rcirc-visible-buffers (rcirc-visible-buffers))
1970 (dolist (vbuf rcirc-visible-buffers)
1971 (setq hidden-buffers (delq vbuf hidden-buffers))
1972 ;; clear activity for all visible buffers
1973 (rcirc-clear-activity vbuf))
1975 ;; clear unread arrow from recently hidden buffers
1976 (dolist (hbuf hidden-buffers)
1977 (rcirc-clear-unread hbuf))
1979 ;; remove any killed buffers from list
1980 (setq rcirc-activity
1981 (delq nil (mapcar (lambda (buf) (when (buffer-live-p buf) buf))
1983 ;; update the mode-line string
1984 (unless (equal old-activity rcirc-activity)
1985 (rcirc-update-activity-string)))
1987 (remove-hook 'post-command-hook 'rcirc-window-configuration-change-1))
1990 ;;; buffer name abbreviation
1991 (defun rcirc-update-short-buffer-names ()
1993 (apply 'append (mapcar (lambda (process)
1994 (with-rcirc-process-buffer process
1995 rcirc-buffer-alist))
1996 (rcirc-process-list)))))
1997 (dolist (i (rcirc-abbreviate bufalist))
1998 (when (buffer-live-p (cdr i))
1999 (with-current-buffer (cdr i)
2000 (setq rcirc-short-buffer-name (car i)))))))
2002 (defun rcirc-abbreviate (pairs)
2003 (apply 'append (mapcar 'rcirc-rebuild-tree (rcirc-make-trees pairs))))
2005 (defun rcirc-rebuild-tree (tree &optional acc)
2006 (let ((ch (char-to-string (car tree))))
2007 (dolist (x (cdr tree))
2009 (setq acc (append acc
2011 (cons (concat ch (car y))
2013 (rcirc-rebuild-tree x))))
2014 (setq acc (cons (cons ch x) acc))))
2017 (defun rcirc-make-trees (pairs)
2019 (mapc (lambda (pair)
2021 (let* ((str (car pair))
2023 (char (unless (zerop (length str))
2025 (rest (unless (zerop (length str))
2027 (part (if char (assq char alist))))
2029 ;; existing partition
2030 (setcdr part (cons (cons rest data) (cdr part)))
2032 (setq alist (cons (if char
2033 (list char (cons rest data))
2036 (setq alist (cons pair alist))))
2038 ;; recurse into cdrs of alist
2040 (when (and (listp x) (listp (cadr x)))
2041 (setcdr x (if (> (length (cdr x)) 1)
2042 (rcirc-make-trees (cdr x))
2043 (setcdr x (list (cdadr x)))))))
2046 ;;; /commands these are called with 3 args: PROCESS, TARGET, which is
2047 ;; the current buffer/channel/user, and ARGS, which is a string
2048 ;; containing the text following the /cmd.
2050 (defmacro defun-rcirc-command (command argument docstring interactive-form
2054 (add-to-list 'rcirc-client-commands ,(concat "/" (symbol-name command)))
2055 (defun ,(intern (concat "rcirc-cmd-" (symbol-name command)))
2056 (,@argument &optional process target)
2057 ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values given"
2058 "\nby `rcirc-buffer-process' and `rcirc-target' will be used.")
2060 (let ((process (or process (rcirc-buffer-process)))
2061 (target (or target rcirc-target)))
2064 (defun-rcirc-command msg (message)
2065 "Send private MESSAGE to TARGET."
2069 (setq target (completing-read "Message nick: "
2070 (with-rcirc-server-buffer
2072 (when (> (length target) 0)
2073 (setq message (read-string (format "Message %s: " target)))
2074 (when (> (length message) 0)
2075 (rcirc-send-message process target message))))
2076 (if (not (string-match "\\([^ ]+\\) \\(.+\\)" message))
2077 (message "Not enough args, or something.")
2078 (setq target (match-string 1 message)
2079 message (match-string 2 message))
2080 (rcirc-send-message process target message))))
2082 (defun-rcirc-command query (nick)
2083 "Open a private chat buffer to NICK."
2084 (interactive (list (completing-read "Query nick: "
2085 (with-rcirc-server-buffer rcirc-nick-table))))
2086 (let ((existing-buffer (rcirc-get-buffer process nick)))
2087 (switch-to-buffer (or existing-buffer
2088 (rcirc-get-buffer-create process nick)))
2089 (when (not existing-buffer)
2090 (rcirc-cmd-whois nick))))
2092 (defun-rcirc-command join (channel)
2094 (interactive "sJoin channel: ")
2095 (let ((buffer (rcirc-get-buffer-create process
2096 (car (split-string channel)))))
2097 (rcirc-send-string process (concat "JOIN " channel))
2098 (when (not (eq (selected-window) (minibuffer-window)))
2099 (switch-to-buffer buffer))))
2101 ;; TODO: /part #channel reason, or consider removing #channel altogether
2102 (defun-rcirc-command part (channel)
2104 (interactive "sPart channel: ")
2105 (let ((channel (if (> (length channel) 0) channel target)))
2106 (rcirc-send-string process (concat "PART " channel " :" rcirc-id-string))))
2108 (defun-rcirc-command quit (reason)
2109 "Send a quit message to server with REASON."
2110 (interactive "sQuit reason: ")
2111 (rcirc-send-string process (concat "QUIT :"
2112 (if (not (zerop (length reason)))
2116 (defun-rcirc-command nick (nick)
2117 "Change nick to NICK."
2120 (setq nick (read-string "New nick: " (rcirc-nick process))))
2121 (rcirc-send-string process (concat "NICK " nick)))
2123 (defun-rcirc-command names (channel)
2124 "Display list of names in CHANNEL or in current channel if CHANNEL is nil.
2125 If called interactively, prompt for a channel when prefix arg is supplied."
2127 (if (called-interactively-p 'interactive)
2129 (setq channel (read-string "List names in channel: " target))))
2130 (let ((channel (if (> (length channel) 0)
2133 (rcirc-send-string process (concat "NAMES " channel))))
2135 (defun-rcirc-command topic (topic)
2136 "List TOPIC for the TARGET channel.
2137 With a prefix arg, prompt for new topic."
2139 (if (and (called-interactively-p 'interactive) topic)
2140 (setq topic (read-string "New Topic: " rcirc-topic)))
2141 (rcirc-send-string process (concat "TOPIC " target
2142 (when (> (length topic) 0)
2143 (concat " :" topic)))))
2145 (defun-rcirc-command whois (nick)
2146 "Request information from server about NICK."
2148 (completing-read "Whois: "
2149 (with-rcirc-server-buffer rcirc-nick-table))))
2150 (rcirc-send-string process (concat "WHOIS " nick)))
2152 (defun-rcirc-command mode (args)
2153 "Set mode with ARGS."
2154 (interactive (list (concat (read-string "Mode nick or channel: ")
2155 " " (read-string "Mode: "))))
2156 (rcirc-send-string process (concat "MODE " args)))
2158 (defun-rcirc-command list (channels)
2159 "Request information on CHANNELS from server."
2160 (interactive "sList Channels: ")
2161 (rcirc-send-string process (concat "LIST " channels)))
2163 (defun-rcirc-command oper (args)
2164 "Send operator command to server."
2165 (interactive "sOper args: ")
2166 (rcirc-send-string process (concat "OPER " args)))
2168 (defun-rcirc-command quote (message)
2169 "Send MESSAGE literally to server."
2170 (interactive "sServer message: ")
2171 (rcirc-send-string process message))
2173 (defun-rcirc-command kick (arg)
2174 "Kick NICK from current channel."
2176 (concat (completing-read "Kick nick: "
2177 (rcirc-channel-nicks
2178 (rcirc-buffer-process)
2180 (read-from-minibuffer "Kick reason: "))))
2181 (let* ((arglist (split-string arg))
2182 (argstring (concat (car arglist) " :"
2183 (mapconcat 'identity (cdr arglist) " "))))
2184 (rcirc-send-string process (concat "KICK " target " " argstring))))
2186 (defun rcirc-cmd-ctcp (args &optional process target)
2187 (if (string-match "^\\([^ ]+\\)\\s-+\\(.+\\)$" args)
2188 (let* ((target (match-string 1 args))
2189 (request (upcase (match-string 2 args)))
2190 (function (intern-soft (concat "rcirc-ctcp-sender-" request))))
2191 (if (fboundp function) ;; use special function if available
2192 (funcall function process target request)
2193 (rcirc-send-string process
2194 (format "PRIVMSG %s :\C-a%s\C-a"
2196 (rcirc-print process (rcirc-nick process) "ERROR" nil
2197 "usage: /ctcp NICK REQUEST")))
2199 (defun rcirc-ctcp-sender-PING (process target request)
2200 "Send a CTCP PING message to TARGET."
2201 (let ((timestamp (format "%.0f" (float-time))))
2202 (rcirc-send-string process
2203 (format "PRIVMSG %s :\C-aPING %s\C-a" target timestamp))))
2205 (defun rcirc-cmd-me (args &optional process target)
2206 (rcirc-send-string process (format "PRIVMSG %s :\C-aACTION %s\C-a"
2209 (defun rcirc-add-or-remove (set &rest elements)
2210 (dolist (elt elements)
2211 (if (and elt (not (string= "" elt)))
2212 (setq set (if (member-ignore-case elt set)
2217 (defun-rcirc-command ignore (nick)
2218 "Manage the ignore list.
2219 Ignore NICK, unignore NICK if already ignored, or list ignored
2220 nicks when no NICK is given. When listing ignored nicks, the
2221 ones added to the list automatically are marked with an asterisk."
2222 (interactive "sToggle ignoring of nick: ")
2223 (setq rcirc-ignore-list
2224 (apply #'rcirc-add-or-remove rcirc-ignore-list
2225 (split-string nick nil t)))
2226 (rcirc-print process nil "IGNORE" target
2230 (if (member nick rcirc-ignore-list-automatic)
2232 rcirc-ignore-list " ")))
2234 (defun-rcirc-command bright (nick)
2235 "Manage the bright nick list."
2236 (interactive "sToggle emphasis of nick: ")
2237 (setq rcirc-bright-nicks
2238 (apply #'rcirc-add-or-remove rcirc-bright-nicks
2239 (split-string nick nil t)))
2240 (rcirc-print process nil "BRIGHT" target
2241 (mapconcat 'identity rcirc-bright-nicks " ")))
2243 (defun-rcirc-command dim (nick)
2244 "Manage the dim nick list."
2245 (interactive "sToggle deemphasis of nick: ")
2246 (setq rcirc-dim-nicks
2247 (apply #'rcirc-add-or-remove rcirc-dim-nicks
2248 (split-string nick nil t)))
2249 (rcirc-print process nil "DIM" target
2250 (mapconcat 'identity rcirc-dim-nicks " ")))
2252 (defun-rcirc-command keyword (keyword)
2253 "Manage the keyword list.
2254 Mark KEYWORD, unmark KEYWORD if already marked, or list marked
2255 keywords when no KEYWORD is given."
2256 (interactive "sToggle highlighting of keyword: ")
2257 (setq rcirc-keywords
2258 (apply #'rcirc-add-or-remove rcirc-keywords
2259 (split-string keyword nil t)))
2260 (rcirc-print process nil "KEYWORD" target
2261 (mapconcat 'identity rcirc-keywords " ")))
2264 (defun rcirc-add-face (start end name &optional object)
2265 "Add face NAME to the face text property of the text from START to END."
2270 (setq prop (get-text-property pos 'face object)
2271 next (next-single-property-change pos 'face object end))
2272 (unless (member name (get-text-property pos 'face object))
2273 (add-text-properties pos next (list 'face (cons name prop)) object))
2276 (defun rcirc-facify (string face)
2277 "Return a copy of STRING with FACE property added."
2278 (let ((string (or string "")))
2279 (rcirc-add-face 0 (length string) face string)
2282 (defvar rcirc-url-regexp
2284 "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
2285 "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
2286 "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
2287 (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
2288 (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
2292 ;; Match paired parentheses, e.g. in Wikipedia URLs:
2293 "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]"
2295 "[" chars punct "]+" "[" chars "]"
2297 (concat ;; XEmacs 21.4 doesn't support POSIX.
2298 "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
2299 "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
2301 "Regexp matching URLs. Set to nil to disable URL features in rcirc.")
2303 (defun rcirc-browse-url (&optional arg)
2304 "Prompt for URL to browse based on URLs in buffer."
2306 (let ((completions (mapcar (lambda (x) (cons x nil)) rcirc-urls))
2307 (initial-input (car rcirc-urls))
2308 (history (cdr rcirc-urls)))
2309 (browse-url (completing-read "rcirc browse-url: "
2310 completions nil nil initial-input 'history)
2313 (defun rcirc-browse-url-at-point (point)
2314 "Send URL at point to `browse-url'."
2316 (let ((beg (previous-single-property-change (1+ point) 'mouse-face))
2317 (end (next-single-property-change point 'mouse-face)))
2318 (browse-url (buffer-substring-no-properties beg end))))
2320 (defun rcirc-browse-url-at-mouse (event)
2321 "Send URL at mouse click to `browse-url'."
2323 (let ((position (event-end event)))
2324 (with-current-buffer (window-buffer (posn-window position))
2325 (rcirc-browse-url-at-point (posn-point position)))))
2328 (defun rcirc-markup-timestamp (sender response)
2329 (goto-char (point-min))
2330 (insert (rcirc-facify (format-time-string rcirc-time-format)
2333 (defun rcirc-markup-attributes (sender response)
2334 (while (re-search-forward "\\([\C-b\C-_\C-v]\\).*?\\(\\1\\|\C-o\\)" nil t)
2335 (rcirc-add-face (match-beginning 0) (match-end 0)
2336 (case (char-after (match-beginning 1))
2339 (?\C-_ 'underline)))
2340 ;; keep the ^O since it could terminate other attributes
2341 (when (not (eq ?\C-o (char-before (match-end 2))))
2342 (delete-region (match-beginning 2) (match-end 2)))
2343 (delete-region (match-beginning 1) (match-end 1))
2344 (goto-char (match-beginning 1)))
2345 ;; remove the ^O characters now
2346 (while (re-search-forward "\C-o+" nil t)
2347 (delete-region (match-beginning 0) (match-end 0))))
2349 (defun rcirc-markup-my-nick (sender response)
2350 (with-syntax-table rcirc-nick-syntax-table
2351 (while (re-search-forward (concat "\\b"
2352 (regexp-quote (rcirc-nick
2353 (rcirc-buffer-process)))
2356 (rcirc-add-face (match-beginning 0) (match-end 0)
2357 'rcirc-nick-in-message)
2358 (when (string= response "PRIVMSG")
2359 (rcirc-add-face (point-min) (point-max)
2360 'rcirc-nick-in-message-full-line)
2361 (rcirc-record-activity (current-buffer) 'nick)))))
2363 (defun rcirc-markup-urls (sender response)
2364 (while (re-search-forward rcirc-url-regexp nil t)
2365 (let ((start (match-beginning 0))
2366 (end (match-end 0)))
2367 (rcirc-add-face start end 'rcirc-url)
2368 (add-text-properties start end (list 'mouse-face 'highlight
2369 'keymap rcirc-browse-url-map))
2371 (push (buffer-substring-no-properties start end) rcirc-urls))))
2373 (defun rcirc-markup-keywords (sender response)
2374 (when (and (string= response "PRIVMSG")
2375 (not (string= sender (rcirc-nick (rcirc-buffer-process)))))
2376 (let* ((target (or rcirc-target ""))
2377 (keywords (delq nil (mapcar (lambda (keyword)
2378 (when (not (string-match keyword
2383 (while (re-search-forward (regexp-opt keywords 'words) nil t)
2384 (rcirc-add-face (match-beginning 0) (match-end 0) 'rcirc-keyword)
2385 (rcirc-record-activity (current-buffer) 'keyword))))))
2387 (defun rcirc-markup-bright-nicks (sender response)
2388 (when (and rcirc-bright-nicks
2389 (string= response "NAMES"))
2390 (with-syntax-table rcirc-nick-syntax-table
2391 (while (re-search-forward (regexp-opt rcirc-bright-nicks 'words) nil t)
2392 (rcirc-add-face (match-beginning 0) (match-end 0)
2393 'rcirc-bright-nick)))))
2395 (defun rcirc-markup-fill (sender response)
2396 (when (not (string= response "372")) ; /motd
2398 (or rcirc-fill-prefix
2399 (make-string (- (point) (line-beginning-position)) ?\s)))
2400 (fill-column (- (cond ((eq rcirc-fill-column 'frame-width)
2405 ;; make sure ... doesn't cause line wrapping
2407 (fill-region (point) (point-max) nil t))))
2410 ;; these are called with the server PROCESS, the SENDER, which is a
2411 ;; server or a user, depending on the command, the ARGS, which is a
2412 ;; list of strings, and the TEXT, which is the original server text,
2414 (defun rcirc-handler-001 (process sender args text)
2415 (rcirc-handler-generic process "001" sender args text)
2416 (with-rcirc-process-buffer process
2417 (setq rcirc-connecting nil)
2418 (rcirc-reschedule-timeout process)
2419 (setq rcirc-server-name sender)
2420 (setq rcirc-nick (car args))
2421 (rcirc-update-prompt)
2422 (when rcirc-auto-authenticate-flag (rcirc-authenticate))
2423 (rcirc-join-channels process rcirc-startup-channels)))
2425 (defun rcirc-handler-PRIVMSG (process sender args text)
2426 (let ((target (if (rcirc-channel-p (car args))
2429 (message (or (cadr args) "")))
2430 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
2431 (rcirc-handler-CTCP process target sender (match-string 1 message))
2432 (rcirc-print process sender "PRIVMSG" target message t))
2433 ;; update nick linestamp
2434 (with-current-buffer (rcirc-get-buffer process target t)
2435 (rcirc-put-nick-channel process sender target rcirc-current-line))))
2437 (defun rcirc-handler-NOTICE (process sender args text)
2438 (let ((target (car args))
2439 (message (cadr args)))
2440 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
2441 (rcirc-handler-CTCP-response process target sender
2442 (match-string 1 message))
2443 (rcirc-print process sender "NOTICE"
2444 (cond ((rcirc-channel-p target)
2446 ;;; -ChanServ- [#gnu] Welcome...
2447 ((string-match "\\[\\(#[^\] ]+\\)\\]" message)
2448 (match-string 1 message))
2450 (if (string= sender (rcirc-server-name process))
2455 (defun rcirc-handler-WALLOPS (process sender args text)
2456 (rcirc-print process sender "WALLOPS" sender (car args) t))
2458 (defun rcirc-handler-JOIN (process sender args text)
2459 (let ((channel (car args)))
2460 (with-current-buffer (rcirc-get-buffer-create process channel)
2461 ;; when recently rejoining, restore the linestamp
2462 (rcirc-put-nick-channel process sender channel
2463 (let ((last-activity-lines
2464 (rcirc-elapsed-lines process sender channel)))
2465 (when (and last-activity-lines
2466 (< last-activity-lines rcirc-omit-threshold))
2467 (rcirc-last-line process sender channel))))
2468 ;; reset mode-line-process in case joining a channel with an
2469 ;; already open buffer (after getting kicked e.g.)
2470 (setq mode-line-process nil))
2472 (rcirc-print process sender "JOIN" channel "")
2474 ;; print in private chat buffer if it exists
2475 (when (rcirc-get-buffer (rcirc-buffer-process) sender)
2476 (rcirc-print process sender "JOIN" sender channel))))
2478 ;; PART and KICK are handled the same way
2479 (defun rcirc-handler-PART-or-KICK (process response channel sender nick args)
2480 (rcirc-ignore-update-automatic nick)
2481 (if (not (string= nick (rcirc-nick process)))
2482 ;; this is someone else leaving
2484 (rcirc-maybe-remember-nick-quit process nick channel)
2485 (rcirc-remove-nick-channel process nick channel))
2486 ;; this is us leaving
2488 (rcirc-remove-nick-channel process n channel))
2489 (rcirc-channel-nicks process channel))
2491 ;; if the buffer is still around, make it inactive
2492 (let ((buffer (rcirc-get-buffer process channel)))
2494 (rcirc-disconnect-buffer buffer)))))
2496 (defun rcirc-handler-PART (process sender args text)
2497 (let* ((channel (car args))
2498 (reason (cadr args))
2499 (message (concat channel " " reason)))
2500 (rcirc-print process sender "PART" channel message)
2501 ;; print in private chat buffer if it exists
2502 (when (rcirc-get-buffer (rcirc-buffer-process) sender)
2503 (rcirc-print process sender "PART" sender message))
2505 (rcirc-handler-PART-or-KICK process "PART" channel sender sender reason)))
2507 (defun rcirc-handler-KICK (process sender args text)
2508 (let* ((channel (car args))
2510 (reason (caddr args))
2511 (message (concat nick " " channel " " reason)))
2512 (rcirc-print process sender "KICK" channel message t)
2513 ;; print in private chat buffer if it exists
2514 (when (rcirc-get-buffer (rcirc-buffer-process) nick)
2515 (rcirc-print process sender "KICK" nick message))
2517 (rcirc-handler-PART-or-KICK process "KICK" channel sender nick reason)))
2519 (defun rcirc-maybe-remember-nick-quit (process nick channel)
2520 "Remember NICK as leaving CHANNEL if they recently spoke."
2521 (let ((elapsed-lines (rcirc-elapsed-lines process nick channel)))
2522 (when (and elapsed-lines
2523 (< elapsed-lines rcirc-omit-threshold))
2524 (let ((buffer (rcirc-get-buffer process channel)))
2526 (with-current-buffer buffer
2527 (let ((record (assoc-string nick rcirc-recent-quit-alist t))
2528 (line (rcirc-last-line process nick channel)))
2530 (setcdr record line)
2531 (setq rcirc-recent-quit-alist
2532 (cons (cons nick line)
2533 rcirc-recent-quit-alist))))))))))
2535 (defun rcirc-handler-QUIT (process sender args text)
2536 (rcirc-ignore-update-automatic sender)
2537 (mapc (lambda (channel)
2538 ;; broadcast quit message each channel
2539 (rcirc-print process sender "QUIT" channel (apply 'concat args))
2540 ;; record nick in quit table if they recently spoke
2541 (rcirc-maybe-remember-nick-quit process sender channel))
2542 (rcirc-nick-channels process sender))
2543 (rcirc-nick-remove process sender))
2545 (defun rcirc-handler-NICK (process sender args text)
2546 (let* ((old-nick sender)
2547 (new-nick (car args))
2548 (channels (rcirc-nick-channels process old-nick)))
2549 ;; update list of ignored nicks
2550 (rcirc-ignore-update-automatic old-nick)
2551 (when (member old-nick rcirc-ignore-list)
2552 (add-to-list 'rcirc-ignore-list new-nick)
2553 (add-to-list 'rcirc-ignore-list-automatic new-nick))
2554 ;; print message to nick's channels
2555 (dolist (target channels)
2556 (rcirc-print process sender "NICK" target new-nick))
2557 ;; update private chat buffer, if it exists
2558 (let ((chat-buffer (rcirc-get-buffer process old-nick)))
2560 (with-current-buffer chat-buffer
2561 (rcirc-print process sender "NICK" old-nick new-nick)
2562 (setq rcirc-target new-nick)
2563 (rename-buffer (rcirc-generate-new-buffer-name process new-nick)))))
2564 ;; remove old nick and add new one
2565 (with-rcirc-process-buffer process
2566 (let ((v (gethash old-nick rcirc-nick-table)))
2567 (remhash old-nick rcirc-nick-table)
2568 (puthash new-nick v rcirc-nick-table))
2569 ;; if this is our nick...
2570 (when (string= old-nick rcirc-nick)
2571 (setq rcirc-nick new-nick)
2572 (rcirc-update-prompt t)
2574 (when rcirc-auto-authenticate-flag (rcirc-authenticate))))))
2576 (defun rcirc-handler-PING (process sender args text)
2577 (rcirc-send-string process (concat "PONG :" (car args))))
2579 (defun rcirc-handler-PONG (process sender args text)
2583 (defun rcirc-handler-TOPIC (process sender args text)
2584 (let ((topic (cadr args)))
2585 (rcirc-print process sender "TOPIC" (car args) topic)
2586 (with-current-buffer (rcirc-get-buffer process (car args))
2587 (setq rcirc-topic topic))))
2589 (defvar rcirc-nick-away-alist nil)
2590 (defun rcirc-handler-301 (process sender args text)
2592 (let* ((nick (cadr args))
2593 (rec (assoc-string nick rcirc-nick-away-alist))
2594 (away-message (caddr args)))
2596 (not (string= (cdr rec) away-message)))
2597 ;; away message has changed
2598 (rcirc-handler-generic process "AWAY" nick (cdr args) text)
2600 (setcdr rec away-message)
2601 (setq rcirc-nick-away-alist (cons (cons nick away-message)
2602 rcirc-nick-away-alist))))))
2604 (defun rcirc-handler-317 (process sender args text)
2606 (let* ((nick (nth 1 args))
2607 (idle-secs (string-to-number (nth 2 args)))
2609 (if (< idle-secs most-positive-fixnum)
2610 (format-seconds "%yy %dd %hh %mm %z%ss" idle-secs)
2611 "a very long time"))
2612 (signon-time (seconds-to-time (string-to-number (nth 3 args))))
2613 (signon-string (format-time-string "%c" signon-time))
2614 (message (format "%s idle for %s, signed on %s"
2615 nick idle-string signon-string)))
2616 (rcirc-print process sender "317" nil message t)))
2618 (defun rcirc-handler-332 (process sender args text)
2620 (let ((buffer (or (rcirc-get-buffer process (cadr args))
2621 (rcirc-get-temp-buffer-create process (cadr args)))))
2622 (with-current-buffer buffer
2623 (setq rcirc-topic (caddr args)))))
2625 (defun rcirc-handler-333 (process sender args text)
2626 "Not in rfc1459.txt"
2627 (let ((buffer (or (rcirc-get-buffer process (cadr args))
2628 (rcirc-get-temp-buffer-create process (cadr args)))))
2629 (with-current-buffer buffer
2630 (let ((setter (caddr args))
2631 (time (current-time-string
2633 (string-to-number (cadddr args))))))
2634 (rcirc-print process sender "TOPIC" (cadr args)
2635 (format "%s (%s on %s)" rcirc-topic setter time))))))
2637 (defun rcirc-handler-477 (process sender args text)
2639 (rcirc-print process sender "477" (cadr args) (caddr args)))
2641 (defun rcirc-handler-MODE (process sender args text)
2642 (let ((target (car args))
2643 (msg (mapconcat 'identity (cdr args) " ")))
2644 (rcirc-print process sender "MODE"
2645 (if (string= target (rcirc-nick process))
2650 ;; print in private chat buffers if they exist
2651 (mapc (lambda (nick)
2652 (when (rcirc-get-buffer process nick)
2653 (rcirc-print process sender "MODE" nick msg)))
2656 (defun rcirc-get-temp-buffer-create (process channel)
2657 "Return a buffer based on PROCESS and CHANNEL."
2658 (let ((tmpnam (concat " " (downcase channel) "TMP" (process-name process))))
2659 (get-buffer-create tmpnam)))
2661 (defun rcirc-handler-353 (process sender args text)
2663 (let ((channel (caddr args)))
2664 (mapc (lambda (nick)
2665 (rcirc-put-nick-channel process nick channel))
2666 (split-string (cadddr args) " " t))
2667 (with-current-buffer (rcirc-get-temp-buffer-create process channel)
2668 (goto-char (point-max))
2669 (insert (car (last args)) " "))))
2671 (defun rcirc-handler-366 (process sender args text)
2673 (let* ((channel (cadr args))
2674 (buffer (rcirc-get-temp-buffer-create process channel)))
2675 (with-current-buffer buffer
2676 (rcirc-print process sender "NAMES" channel
2677 (let ((content (buffer-substring (point-min) (point-max))))
2678 (rcirc-sort-nicknames-join content " "))))
2679 (kill-buffer buffer)))
2681 (defun rcirc-handler-433 (process sender args text)
2683 (rcirc-handler-generic process "433" sender args text)
2684 (let* ((new-nick (concat (cadr args) "`")))
2685 (with-rcirc-process-buffer process
2686 (rcirc-cmd-nick new-nick nil process))))
2688 (defun rcirc-authenticate ()
2689 "Send authentication to process associated with current buffer.
2690 Passwords are stored in `rcirc-authinfo' (which see)."
2692 (with-rcirc-server-buffer
2693 (dolist (i rcirc-authinfo)
2694 (let ((process (rcirc-buffer-process))
2699 (when (and (string-match server rcirc-server)
2700 (string-match nick rcirc-nick))
2701 (cond ((equal method 'nickserv)
2704 (concat "PRIVMSG " (or (cadr args) "nickserv")
2705 " :identify " (car args))))
2706 ((equal method 'chanserv)
2710 "PRIVMSG chanserv :identify "
2711 (car args) " " (cadr args))))
2712 ((equal method 'bitlbee)
2715 (concat "PRIVMSG &bitlbee :identify " (car args))))
2717 (message "No %S authentication method defined"
2720 (defun rcirc-handler-INVITE (process sender args text)
2721 (rcirc-print process sender "INVITE" nil (mapconcat 'identity args " ") t))
2723 (defun rcirc-handler-ERROR (process sender args text)
2724 (rcirc-print process sender "ERROR" nil (mapconcat 'identity args " ")))
2726 (defun rcirc-handler-CTCP (process target sender text)
2727 (if (string-match "^\\([^ ]+\\) *\\(.*\\)$" text)
2728 (let* ((request (upcase (match-string 1 text)))
2729 (args (match-string 2 text))
2730 (handler (intern-soft (concat "rcirc-handler-ctcp-" request))))
2731 (if (not (fboundp handler))
2732 (rcirc-print process sender "ERROR" target
2733 (format "%s sent unsupported ctcp: %s" sender text)
2735 (funcall handler process target sender args)
2736 (unless (or (string= request "ACTION")
2737 (string= request "KEEPALIVE"))
2738 (rcirc-print process sender "CTCP" target
2739 (format "%s" text) t))))))
2741 (defun rcirc-handler-ctcp-VERSION (process target sender args)
2742 (rcirc-send-string process
2743 (concat "NOTICE " sender
2744 " :\C-aVERSION " rcirc-id-string
2747 (defun rcirc-handler-ctcp-ACTION (process target sender args)
2748 (rcirc-print process sender "ACTION" target args t))
2750 (defun rcirc-handler-ctcp-TIME (process target sender args)
2751 (rcirc-send-string process
2752 (concat "NOTICE " sender
2753 " :\C-aTIME " (current-time-string) "\C-a")))
2755 (defun rcirc-handler-CTCP-response (process target sender message)
2756 (rcirc-print process sender "CTCP" nil message t))
2758 (defgroup rcirc-faces nil
2763 (defface rcirc-my-nick ; font-lock-function-name-face
2764 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
2765 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
2766 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
2767 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
2768 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
2769 (t (:inverse-video t :weight bold)))
2770 "The face used to highlight my messages."
2771 :group 'rcirc-faces)
2773 (defface rcirc-other-nick ; font-lock-variable-name-face
2774 '((((class grayscale) (background light))
2775 (:foreground "Gray90" :weight bold :slant italic))
2776 (((class grayscale) (background dark))
2777 (:foreground "DimGray" :weight bold :slant italic))
2778 (((class color) (min-colors 88) (background light)) (:foreground "DarkGoldenrod"))
2779 (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod"))
2780 (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
2781 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
2782 (((class color) (min-colors 8)) (:foreground "yellow" :weight light))
2783 (t (:weight bold :slant italic)))
2784 "The face used to highlight other messages."
2785 :group 'rcirc-faces)
2787 (defface rcirc-bright-nick
2788 '((((class grayscale) (background light))
2789 (:foreground "LightGray" :weight bold :underline t))
2790 (((class grayscale) (background dark))
2791 (:foreground "Gray50" :weight bold :underline t))
2792 (((class color) (min-colors 88) (background light)) (:foreground "CadetBlue"))
2793 (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine"))
2794 (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
2795 (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
2796 (((class color) (min-colors 8)) (:foreground "magenta"))
2797 (t (:weight bold :underline t)))
2798 "Face used for nicks matched by `rcirc-bright-nicks'."
2799 :group 'rcirc-faces)
2801 (defface rcirc-dim-nick
2802 '((t :inherit default))
2803 "Face used for nicks in `rcirc-dim-nicks'."
2804 :group 'rcirc-faces)
2806 (defface rcirc-server ; font-lock-comment-face
2807 '((((class grayscale) (background light))
2808 (:foreground "DimGray" :weight bold :slant italic))
2809 (((class grayscale) (background dark))
2810 (:foreground "LightGray" :weight bold :slant italic))
2811 (((class color) (min-colors 88) (background light))
2812 (:foreground "Firebrick"))
2813 (((class color) (min-colors 88) (background dark))
2814 (:foreground "chocolate1"))
2815 (((class color) (min-colors 16) (background light))
2816 (:foreground "red"))
2817 (((class color) (min-colors 16) (background dark))
2818 (:foreground "red1"))
2819 (((class color) (min-colors 8) (background light))
2821 (((class color) (min-colors 8) (background dark))
2823 (t (:weight bold :slant italic)))
2824 "The face used to highlight server messages."
2825 :group 'rcirc-faces)
2827 (defface rcirc-server-prefix ; font-lock-comment-delimiter-face
2828 '((default :inherit rcirc-server)
2829 (((class grayscale)))
2830 (((class color) (min-colors 16)))
2831 (((class color) (min-colors 8) (background light))
2833 (((class color) (min-colors 8) (background dark))
2834 :foreground "red1"))
2835 "The face used to highlight server prefixes."
2836 :group 'rcirc-faces)
2838 (defface rcirc-timestamp
2839 '((t (:inherit default)))
2840 "The face used to highlight timestamps."
2841 :group 'rcirc-faces)
2843 (defface rcirc-nick-in-message ; font-lock-keyword-face
2844 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
2845 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
2846 (((class color) (min-colors 88) (background light)) (:foreground "Purple"))
2847 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
2848 (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
2849 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
2850 (((class color) (min-colors 8)) (:foreground "cyan" :weight bold))
2852 "The face used to highlight instances of your nick within messages."
2853 :group 'rcirc-faces)
2855 (defface rcirc-nick-in-message-full-line
2857 "The face used emphasize the entire message when your nick is mentioned."
2858 :group 'rcirc-faces)
2860 (defface rcirc-prompt ; comint-highlight-prompt
2861 '((((min-colors 88) (background dark)) (:foreground "cyan1"))
2862 (((background dark)) (:foreground "cyan"))
2863 (t (:foreground "dark blue")))
2864 "The face used to highlight prompts."
2865 :group 'rcirc-faces)
2867 (defface rcirc-track-nick
2868 '((((type tty)) (:inherit default))
2869 (t (:inverse-video t)))
2870 "The face used in the mode-line when your nick is mentioned."
2871 :group 'rcirc-faces)
2873 (defface rcirc-track-keyword
2875 "The face used in the mode-line when keywords are mentioned."
2876 :group 'rcirc-faces)
2880 "The face used to highlight urls."
2881 :group 'rcirc-faces)
2883 (defface rcirc-keyword
2884 '((t (:inherit highlight)))
2885 "The face used to highlight keywords."
2886 :group 'rcirc-faces)
2889 ;; When using M-x flyspell-mode, only check words after the prompt
2890 (put 'rcirc-mode 'flyspell-mode-predicate 'rcirc-looking-at-input)
2891 (defun rcirc-looking-at-input ()
2892 "Returns true if point is past the input marker."
2893 (>= (point) rcirc-prompt-end-marker))
2898 ;;; rcirc.el ends here