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