(rcirc-connect): Make all arguments optional, and default to global variable
[emacs.git] / lisp / net / rcirc.el
blobf2eff379d146b674680633341b035d1e35ac447e
1 ;;; rcirc.el --- default, simple IRC client.
3 ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc.
5 ;; Author: Ryan Yeske
6 ;; URL: http://www.nongnu.org/rcirc
7 ;; Keywords: comm
9 ;; This file is part of GNU Emacs.
11 ;; This file is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; This file is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Internet Relay Chat (IRC) is a form of instant communication over
29 ;; the Internet. It is mainly designed for group (many-to-many)
30 ;; communication in discussion forums called channels, but also allows
31 ;; one-to-one communication.
33 ;; Rcirc has simple defaults and clear and consistent behaviour.
34 ;; Message arrival timestamps, activity notification on the modeline,
35 ;; message filling, nick completion, and keepalive pings are all
36 ;; enabled by default, but can easily be adjusted or turned off. Each
37 ;; discussion takes place in its own buffer and there is a single
38 ;; server buffer per connection.
40 ;; Open a new irc connection with:
41 ;; M-x irc RET
43 ;;; Code:
45 (require 'ring)
46 (require 'time-date)
47 (eval-when-compile (require 'cl))
49 (defgroup rcirc nil
50 "Simple IRC client."
51 :version "22.1"
52 :prefix "rcirc-"
53 :group 'applications)
55 (defcustom rcirc-server "irc.freenode.net"
56 "The default server to connect to."
57 :type 'string
58 :group 'rcirc)
60 (defcustom rcirc-port 6667
61 "The default port to connect to."
62 :type 'integer
63 :group 'rcirc)
65 (defcustom rcirc-nick (user-login-name)
66 "Your nick."
67 :type 'string
68 :group 'rcirc)
70 (defcustom rcirc-user-name (user-login-name)
71 "Your user name sent to the server when connecting."
72 :type 'string
73 :group 'rcirc)
75 (defcustom rcirc-user-full-name (if (string= (user-full-name) "")
76 rcirc-user-name
77 (user-full-name))
78 "The full name sent to the server when connecting."
79 :type 'string
80 :group 'rcirc)
82 (defcustom rcirc-startup-channels-alist '(("^irc.freenode.net$" "#emacs"))
83 "Alist of channels to join at startup.
84 Each element looks like (SERVER-REGEXP . CHANNEL-LIST)."
85 :type '(alist :key-type string :value-type (repeat string))
86 :group 'rcirc)
88 (defcustom rcirc-fill-flag t
89 "*Non-nil means line-wrap messages printed in channel buffers."
90 :type 'boolean
91 :group 'rcirc)
93 (defcustom rcirc-fill-column nil
94 "*Column beyond which automatic line-wrapping should happen.
95 If nil, use value of `fill-column'. If 'frame-width, use the
96 maximum frame width."
97 :type '(choice (const :tag "Value of `fill-column'")
98 (const :tag "Full frame width" frame-width)
99 (integer :tag "Number of columns"))
100 :group 'rcirc)
102 (defcustom rcirc-fill-prefix nil
103 "*Text to insert before filled lines.
104 If nil, calculate the prefix dynamically to line up text
105 underneath each nick."
106 :type '(choice (const :tag "Dynamic" nil)
107 (string :tag "Prefix text"))
108 :group 'rcirc)
110 (defvar rcirc-ignore-buffer-activity-flag nil
111 "If non-nil, ignore activity in this buffer.")
112 (make-variable-buffer-local 'rcirc-ignore-buffer-activity-flag)
114 (defcustom rcirc-time-format "%H:%M "
115 "*Describes how timestamps are printed.
116 Used as the first arg to `format-time-string'."
117 :type 'string
118 :group 'rcirc)
120 (defcustom rcirc-input-ring-size 1024
121 "*Size of input history ring."
122 :type 'integer
123 :group 'rcirc)
125 (defcustom rcirc-read-only-flag t
126 "*Non-nil means make text in IRC buffers read-only."
127 :type 'boolean
128 :group 'rcirc)
130 (defcustom rcirc-buffer-maximum-lines nil
131 "*The maximum size in lines for rcirc buffers.
132 Channel buffers are truncated from the top to be no greater than this
133 number. If zero or nil, no truncating is done."
134 :type '(choice (const :tag "No truncation" nil)
135 (integer :tag "Number of lines"))
136 :group 'rcirc)
138 (defcustom rcirc-authinfo nil
139 "List of authentication passwords.
140 Each element of the list is a list with a SERVER-REGEXP string
141 and a method symbol followed by method specific arguments.
143 The valid METHOD symbols are `nickserv', `chanserv' and
144 `bitlbee'.
146 The required ARGUMENTS for each METHOD symbol are:
147 `nickserv': NICK PASSWORD
148 `chanserv': NICK CHANNEL PASSWORD
149 `bitlbee': NICK PASSWORD
151 Example:
152 ((\"freenode\" nickserv \"bob\" \"p455w0rd\")
153 (\"freenode\" chanserv \"bob\" \"#bobland\" \"passwd99\")
154 (\"bitlbee\" bitlbee \"robert\" \"sekrit\"))"
155 :type '(alist :key-type (string :tag "Server")
156 :value-type (choice (list :tag "NickServ"
157 (const nickserv)
158 (string :tag "Nick")
159 (string :tag "Password"))
160 (list :tag "ChanServ"
161 (const chanserv)
162 (string :tag "Nick")
163 (string :tag "Channel")
164 (string :tag "Password"))
165 (list :tag "BitlBee"
166 (const bitlbee)
167 (string :tag "Nick")
168 (string :tag "Password"))))
169 :group 'rcirc)
171 (defcustom rcirc-auto-authenticate-flag t
172 "*Non-nil means automatically send authentication string to server.
173 See also `rcirc-authinfo'."
174 :type 'boolean
175 :group 'rcirc)
177 (defcustom rcirc-prompt "> "
178 "Prompt string to use in IRC buffers.
180 The following replacements are made:
181 %n is your nick.
182 %s is the server.
183 %t is the buffer target, a channel or a user.
185 Setting this alone will not affect the prompt;
186 use either M-x customize or also call `rcirc-update-prompt'."
187 :type 'string
188 :set 'rcirc-set-changed
189 :initialize 'custom-initialize-default
190 :group 'rcirc)
192 (defcustom rcirc-ignore-list ()
193 "List of ignored nicks.
194 Use /ignore to list them, use /ignore NICK to add or remove a nick."
195 :type '(repeat string)
196 :group 'rcirc)
198 (defcustom rcirc-nick-abbrevs nil
199 "List of short replacements for printing nicks."
200 :type '(alist :key-type (string :tag "Nick")
201 :value-type (string :tag "Abbrev"))
202 :group 'rcirc)
204 (defvar rcirc-ignore-list-automatic ()
205 "List of ignored nicks added to `rcirc-ignore-list' because of renaming.
206 When an ignored person renames, their nick is added to both lists.
207 Nicks will be removed from the automatic list on follow-up renamings or
208 parts.")
210 (defcustom rcirc-print-hooks nil
211 "Hook run after text is printed.
212 Called with 5 arguments, PROCESS, SENDER, RESPONSE, TARGET and TEXT."
213 :type 'hook
214 :group 'rcirc)
216 (defcustom rcirc-always-use-server-buffer-flag nil
217 "Non-nil means messages without a channel target will go to the server buffer."
218 :type 'boolean
219 :group 'rcirc)
221 (defvar rcirc-prompt-start-marker nil)
222 (defvar rcirc-prompt-end-marker nil)
224 (defvar rcirc-nick-table nil)
226 (defvar rcirc-nick-syntax-table
227 (let ((table (make-syntax-table text-mode-syntax-table)))
228 (mapc (lambda (c) (modify-syntax-entry c "w" table))
229 "[]\\`_^{|}-")
230 (modify-syntax-entry ?' "_" table)
231 table)
232 "Syntax table which includes all nick characters as word constituents.")
234 ;; each process has an alist of (target . buffer) pairs
235 (defvar rcirc-buffer-alist nil)
237 (defvar rcirc-activity nil
238 "List of channels with unviewed activity.")
240 (defvar rcirc-activity-string ""
241 "String displayed in modeline representing `rcirc-activity'.")
242 (put 'rcirc-activity-string 'risky-local-variable t)
244 (defvar rcirc-process nil
245 "The server process associated with this buffer.")
247 (defvar rcirc-target nil
248 "The channel or user associated with this buffer.")
250 (defvar rcirc-urls nil
251 "List of urls seen in the current buffer.")
253 (defvar rcirc-keepalive-seconds 60
254 "Number of seconds between keepalive pings.")
256 (defconst rcirc-id-string (concat "rcirc on GNU Emacs " emacs-version))
258 (defvar rcirc-startup-channels nil)
259 ;;;###autoload
260 (defun rcirc (arg)
261 "Connect to IRC.
262 If ARG is non-nil, prompt for a server to connect to."
263 (interactive "P")
264 (if arg
265 (let* ((server (read-string "IRC Server: " rcirc-server))
266 (port (read-string "IRC Port: " (number-to-string rcirc-port)))
267 (nick (read-string "IRC Nick: " rcirc-nick))
268 (channels (split-string
269 (read-string "IRC Channels: "
270 (mapconcat 'identity
271 (rcirc-startup-channels server)
272 " "))
273 "[, ]+" t)))
274 (rcirc-connect server port nick rcirc-user-name rcirc-user-full-name
275 channels))
276 ;; make new connection using defaults unless already connected to
277 ;; the default rcirc-server
278 (let ((default-server (default-value 'rcirc-server))
279 connected)
280 (dolist (p (rcirc-process-list))
281 (when (string= default-server (process-name p))
282 (setq connected p)))
283 (if (not connected)
284 (rcirc-connect rcirc-server rcirc-port rcirc-nick
285 rcirc-user-name rcirc-user-full-name
286 (rcirc-startup-channels rcirc-server))
287 (switch-to-buffer (process-buffer connected))
288 (message "Connected to %s" rcirc-server)))))
290 ;;;###autoload
291 (defalias 'irc 'rcirc)
294 (defvar rcirc-process-output nil)
295 (defvar rcirc-topic nil)
296 (defvar rcirc-keepalive-timer nil)
297 (defvar rcirc-last-server-message-time nil)
298 (defun rcirc-connect (&optional server port nick user-name full-name startup-channels)
299 (add-hook 'window-configuration-change-hook
300 'rcirc-window-configuration-change)
302 (save-excursion
303 (message "Connecting to %s..." server)
304 (let* ((inhibit-eol-conversion)
305 (port-number (if port
306 (if (stringp port)
307 (string-to-number port)
308 port)
309 rcirc-port))
310 (server (or server rcirc-server))
311 (nick (or nick rcirc-nick))
312 (user-name (or user-name rcirc-user-name))
313 (full-name (or full-name rcirc-user-full-name))
314 (startup-channels (or startup-channels (rcirc-startup-channels server)))
315 (process (open-network-stream server nil server port-number)))
316 ;; set up process
317 (set-process-coding-system process 'raw-text 'raw-text)
318 (set-process-filter process 'rcirc-filter)
319 (switch-to-buffer (rcirc-generate-new-buffer-name process nil))
320 (set-process-buffer process (current-buffer))
321 (set-process-sentinel process 'rcirc-sentinel)
322 (rcirc-mode process nil)
323 (make-local-variable 'rcirc-buffer-alist)
324 (setq rcirc-buffer-alist nil)
325 (make-local-variable 'rcirc-nick-table)
326 (setq rcirc-nick-table (make-hash-table :test 'equal))
327 (make-local-variable 'rcirc-server)
328 (setq rcirc-server server)
329 (make-local-variable 'rcirc-nick)
330 (setq rcirc-nick nick)
331 (make-local-variable 'rcirc-process-output)
332 (setq rcirc-process-output nil)
333 (make-local-variable 'rcirc-startup-channels)
334 (setq rcirc-startup-channels startup-channels)
335 (make-local-variable 'rcirc-last-server-message-time)
336 (setq rcirc-last-server-message-time (current-time))
338 ;; identify
339 (rcirc-send-string process (concat "NICK " nick))
340 (rcirc-send-string process (concat "USER " user-name
341 " hostname servername :"
342 full-name))
344 ;; setup ping timer if necessary
345 (unless rcirc-keepalive-timer
346 (setq rcirc-keepalive-timer
347 (run-at-time 0 rcirc-keepalive-seconds 'rcirc-keepalive)))
349 (message "Connecting to %s...done" server)
351 ;; return process object
352 process)))
354 (defmacro with-rcirc-process-buffer (process &rest body)
355 (declare (indent 1) (debug t))
356 `(with-current-buffer (process-buffer ,process)
357 ,@body))
359 (defun rcirc-keepalive ()
360 "Send keep alive pings to active rcirc processes.
361 Kill processes that have not received a server message since the
362 last ping."
363 (if (rcirc-process-list)
364 (mapc (lambda (process)
365 (with-rcirc-process-buffer process
366 (if (> (cadr (time-since rcirc-last-server-message-time))
367 rcirc-keepalive-seconds)
368 (kill-process process)
369 (rcirc-send-string process (concat "PING " rcirc-server)))))
370 (rcirc-process-list))
371 (cancel-timer rcirc-keepalive-timer)
372 (setq rcirc-keepalive-timer nil)))
374 (defvar rcirc-debug-buffer " *rcirc debug*")
375 (defvar rcirc-debug-flag nil
376 "If non-nil, write information to `rcirc-debug-buffer'.")
377 (defun rcirc-debug (process text)
378 "Add an entry to the debug log including PROCESS and TEXT.
379 Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag'
380 is non-nil."
381 (when rcirc-debug-flag
382 (save-excursion
383 (save-window-excursion
384 (set-buffer (get-buffer-create rcirc-debug-buffer))
385 (goto-char (point-max))
386 (insert (concat
388 (format-time-string "%Y-%m-%dT%T ") (process-name process)
389 "] "
390 text))))))
392 (defvar rcirc-sentinel-hooks nil
393 "Hook functions called when the process sentinel is called.
394 Functions are called with PROCESS and SENTINEL arguments.")
396 (defun rcirc-sentinel (process sentinel)
397 "Called when PROCESS receives SENTINEL."
398 (let ((sentinel (replace-regexp-in-string "\n" "" sentinel)))
399 (rcirc-debug process (format "SENTINEL: %S %S\n" process sentinel))
400 (with-rcirc-process-buffer process
401 (dolist (buffer (cons nil (mapcar 'cdr rcirc-buffer-alist)))
402 (with-current-buffer (or buffer (current-buffer))
403 (rcirc-print process "rcirc.el" "ERROR" rcirc-target
404 (format "%s: %s (%S)"
405 (process-name process)
406 sentinel
407 (process-status process)) t)
408 ;; remove the prompt from buffers
409 (let ((inhibit-read-only t))
410 (delete-region rcirc-prompt-start-marker
411 rcirc-prompt-end-marker)))))
412 (run-hook-with-args 'rcirc-sentinel-hooks process sentinel)))
414 (defun rcirc-process-list ()
415 "Return a list of rcirc processes."
416 (let (ps)
417 (mapc (lambda (p)
418 (when (process-buffer p)
419 (with-rcirc-process-buffer p
420 (when (eq major-mode 'rcirc-mode)
421 (setq ps (cons p ps))))))
422 (process-list))
423 ps))
425 (defvar rcirc-receive-message-hooks nil
426 "Hook functions run when a message is received from server.
427 Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.")
428 (defun rcirc-filter (process output)
429 "Called when PROCESS receives OUTPUT."
430 (rcirc-debug process output)
431 (with-rcirc-process-buffer process
432 (setq rcirc-last-server-message-time (current-time))
433 (setq rcirc-process-output (concat rcirc-process-output output))
434 (when (= (aref rcirc-process-output
435 (1- (length rcirc-process-output))) ?\n)
436 (mapc (lambda (line)
437 (rcirc-process-server-response process line))
438 (split-string rcirc-process-output "[\n\r]" t))
439 (setq rcirc-process-output nil))))
441 (defvar rcirc-trap-errors-flag t)
442 (defun rcirc-process-server-response (process text)
443 (if rcirc-trap-errors-flag
444 (condition-case err
445 (rcirc-process-server-response-1 process text)
446 (error
447 (rcirc-print process "RCIRC" "ERROR" nil
448 (format "\"%s\" %s" text err) t)))
449 (rcirc-process-server-response-1 process text)))
451 (defun rcirc-process-server-response-1 (process text)
452 (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text)
453 (let* ((user (match-string 2 text))
454 (sender (rcirc-user-nick user))
455 (cmd (match-string 3 text))
456 (args (match-string 4 text))
457 (handler (intern-soft (concat "rcirc-handler-" cmd))))
458 (string-match "^\\([^:]*\\):?\\(.+\\)?$" args)
459 (let* ((args1 (match-string 1 args))
460 (args2 (match-string 2 args))
461 (args (delq nil (append (split-string args1 " " t)
462 (list args2)))))
463 (if (not (fboundp handler))
464 (rcirc-handler-generic process cmd sender args text)
465 (funcall handler process sender args text))
466 (run-hook-with-args 'rcirc-receive-message-hooks
467 process cmd sender args text)))
468 (message "UNHANDLED: %s" text)))
470 (defun rcirc-handler-generic (process command sender args text)
471 "Generic server response handler."
472 (rcirc-print process sender command nil
473 (mapconcat 'identity (cdr args) " ") t))
475 (defun rcirc-send-string (process string)
476 "Send PROCESS a STRING plus a newline."
477 (let ((string (concat (encode-coding-string string
478 buffer-file-coding-system)
479 "\n")))
480 (unless (eq (process-status rcirc-process) 'open)
481 (error "Network connection to %s is not open"
482 (process-name rcirc-process)))
483 (rcirc-debug process string)
484 (process-send-string process string)))
486 (defun rcirc-server (process)
487 "Return PROCESS server, given by the 001 response."
488 (with-rcirc-process-buffer process
489 rcirc-server))
491 (defun rcirc-nick (process)
492 "Return PROCESS nick."
493 (with-rcirc-process-buffer process
494 rcirc-nick))
496 (defun rcirc-abbrev-nick (nick)
497 "If NICK has an entry in `rcirc-nick-abbrevs', return its abbreviation,
498 otherwise return NICK."
499 (or (cdr (assoc nick rcirc-nick-abbrevs)) nick))
501 (defvar rcirc-max-message-length 450
502 "Messages longer than this value will be split.")
504 (defun rcirc-send-message (process target message &optional noticep)
505 "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
506 If NOTICEP is non-nil, send a notice instead of privmsg."
507 ;; max message length is 512 including CRLF
508 (let* ((response (if noticep "NOTICE" "PRIVMSG"))
509 (oversize (> (length message) rcirc-max-message-length))
510 (text (if oversize
511 (substring message 0 rcirc-max-message-length)
512 message))
513 (text (if (string= text "")
515 text))
516 (more (if oversize
517 (substring message rcirc-max-message-length))))
518 (rcirc-get-buffer-create process target)
519 (rcirc-print process (rcirc-nick process) response target text)
520 (rcirc-send-string process (concat response " " target " :" text))
521 (when more (rcirc-send-message process target more noticep))))
523 (defvar rcirc-input-ring nil)
524 (defvar rcirc-input-ring-index 0)
525 (defun rcirc-prev-input-string (arg)
526 (ring-ref rcirc-input-ring (+ rcirc-input-ring-index arg)))
528 (defun rcirc-insert-prev-input (arg)
529 (interactive "p")
530 (when (<= rcirc-prompt-end-marker (point))
531 (delete-region rcirc-prompt-end-marker (point-max))
532 (insert (rcirc-prev-input-string 0))
533 (setq rcirc-input-ring-index (1+ rcirc-input-ring-index))))
535 (defun rcirc-insert-next-input (arg)
536 (interactive "p")
537 (when (<= rcirc-prompt-end-marker (point))
538 (delete-region rcirc-prompt-end-marker (point-max))
539 (setq rcirc-input-ring-index (1- rcirc-input-ring-index))
540 (insert (rcirc-prev-input-string -1))))
542 (defvar rcirc-nick-completions nil)
543 (defvar rcirc-nick-completion-start-offset nil)
544 (defun rcirc-complete-nick ()
545 "Cycle through nick completions from list of nicks in channel."
546 (interactive)
547 (if (eq last-command 'rcirc-complete-nick)
548 (setq rcirc-nick-completions
549 (append (cdr rcirc-nick-completions)
550 (list (car rcirc-nick-completions))))
551 (setq rcirc-nick-completion-start-offset
552 (- (save-excursion
553 (if (re-search-backward " " rcirc-prompt-end-marker t)
554 (1+ (point))
555 rcirc-prompt-end-marker))
556 rcirc-prompt-end-marker))
557 (setq rcirc-nick-completions
558 (let ((completion-ignore-case t))
559 (all-completions
560 (buffer-substring
561 (+ rcirc-prompt-end-marker
562 rcirc-nick-completion-start-offset)
563 (point))
564 (mapcar (lambda (x) (cons x nil))
565 (rcirc-channel-nicks rcirc-process
566 (rcirc-buffer-target)))))))
567 (let ((completion (car rcirc-nick-completions)))
568 (when completion
569 (delete-region (+ rcirc-prompt-end-marker
570 rcirc-nick-completion-start-offset)
571 (point))
572 (insert (concat completion
573 (if (= (+ rcirc-prompt-end-marker
574 rcirc-nick-completion-start-offset)
575 rcirc-prompt-end-marker)
576 ": "))))))
578 (defun rcirc-buffer-target (&optional buffer)
579 "Return the name of target for BUFFER.
580 If buffer is nil, return the target of the current buffer."
581 (with-current-buffer (or buffer (current-buffer))
582 rcirc-target))
584 (defvar rcirc-mode-map (make-sparse-keymap)
585 "Keymap for rcirc mode.")
587 (define-key rcirc-mode-map (kbd "RET") 'rcirc-send-input)
588 (define-key rcirc-mode-map (kbd "M-p") 'rcirc-insert-prev-input)
589 (define-key rcirc-mode-map (kbd "M-n") 'rcirc-insert-next-input)
590 (define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete-nick)
591 (define-key rcirc-mode-map (kbd "C-c C-b") 'rcirc-browse-url)
592 (define-key rcirc-mode-map (kbd "C-c C-c") 'rcirc-edit-multiline)
593 (define-key rcirc-mode-map (kbd "C-c C-j") 'rcirc-cmd-join)
594 (define-key rcirc-mode-map (kbd "C-c C-k") 'rcirc-cmd-kick)
595 (define-key rcirc-mode-map (kbd "C-c C-l") 'rcirc-cmd-list)
596 (define-key rcirc-mode-map (kbd "C-c C-d") 'rcirc-cmd-mode)
597 (define-key rcirc-mode-map (kbd "C-c C-m") 'rcirc-cmd-msg)
598 (define-key rcirc-mode-map (kbd "C-c C-r") 'rcirc-cmd-nick) ; rename
599 (define-key rcirc-mode-map (kbd "C-c C-o") 'rcirc-cmd-oper)
600 (define-key rcirc-mode-map (kbd "C-c C-p") 'rcirc-cmd-part)
601 (define-key rcirc-mode-map (kbd "C-c C-q") 'rcirc-cmd-query)
602 (define-key rcirc-mode-map (kbd "C-c C-t") 'rcirc-cmd-topic)
603 (define-key rcirc-mode-map (kbd "C-c C-n") 'rcirc-cmd-names)
604 (define-key rcirc-mode-map (kbd "C-c C-w") 'rcirc-cmd-whois)
605 (define-key rcirc-mode-map (kbd "C-c C-x") 'rcirc-cmd-quit)
606 (define-key rcirc-mode-map (kbd "C-c TAB") ; C-i
607 'rcirc-toggle-ignore-buffer-activity)
608 (define-key rcirc-mode-map (kbd "C-c C-s") 'rcirc-switch-to-server-buffer)
609 (define-key rcirc-mode-map (kbd "C-c C-a") 'rcirc-jump-to-first-unread-line)
611 (defvar rcirc-browse-url-map (make-sparse-keymap)
612 "Keymap used for browsing URLs in `rcirc-mode'.")
614 (define-key rcirc-browse-url-map (kbd "RET") 'rcirc-browse-url-at-point)
615 (define-key rcirc-browse-url-map (kbd "<mouse-2>") 'rcirc-browse-url-at-mouse)
617 (defvar rcirc-short-buffer-name nil
618 "Generated abbreviation to use to indicate buffer activity.")
620 (defvar rcirc-mode-hook nil
621 "Hook run when setting up rcirc buffer.")
623 (defun rcirc-mode (process target)
624 "Major mode for IRC channel buffers.
626 \\{rcirc-mode-map}"
627 (kill-all-local-variables)
628 (use-local-map rcirc-mode-map)
629 (setq mode-name "rcirc")
630 (setq major-mode 'rcirc-mode)
632 (make-local-variable 'rcirc-input-ring)
633 (setq rcirc-input-ring (make-ring rcirc-input-ring-size))
634 (make-local-variable 'rcirc-process)
635 (setq rcirc-process process)
636 (make-local-variable 'rcirc-target)
637 (setq rcirc-target target)
638 (make-local-variable 'rcirc-topic)
639 (setq rcirc-topic nil)
641 (make-local-variable 'rcirc-short-buffer-name)
642 (setq rcirc-short-buffer-name nil)
643 (make-local-variable 'rcirc-urls)
644 (setq rcirc-urls nil)
645 (setq use-hard-newlines t)
647 ;; setup the prompt and markers
648 (make-local-variable 'rcirc-prompt-start-marker)
649 (setq rcirc-prompt-start-marker (make-marker))
650 (set-marker rcirc-prompt-start-marker (point-max))
651 (make-local-variable 'rcirc-prompt-end-marker)
652 (setq rcirc-prompt-end-marker (make-marker))
653 (set-marker rcirc-prompt-end-marker (point-max))
654 (rcirc-update-prompt)
655 (goto-char rcirc-prompt-end-marker)
656 (make-local-variable 'overlay-arrow-position)
657 (setq overlay-arrow-position (make-marker))
658 (set-marker overlay-arrow-position nil)
660 ;; add to buffer list, and update buffer abbrevs
661 (when target ; skip server buffer
662 (let ((buffer (current-buffer)))
663 (with-rcirc-process-buffer process
664 (setq rcirc-buffer-alist (cons (cons target buffer)
665 rcirc-buffer-alist))))
666 (rcirc-update-short-buffer-names))
668 (run-hooks 'rcirc-mode-hook))
670 (defun rcirc-update-prompt (&optional all)
671 "Reset the prompt string in the current buffer.
673 If ALL is non-nil, update prompts in all IRC buffers."
674 (if all
675 (mapc (lambda (process)
676 (mapc (lambda (buffer)
677 (with-current-buffer buffer
678 (rcirc-update-prompt)))
679 (with-rcirc-process-buffer process
680 (mapcar 'cdr rcirc-buffer-alist))))
681 (rcirc-process-list))
682 (let ((inhibit-read-only t)
683 (prompt (or rcirc-prompt "")))
684 (mapc (lambda (rep)
685 (setq prompt
686 (replace-regexp-in-string (car rep) (regexp-quote (cdr rep)) prompt)))
687 (list (cons "%n" (with-rcirc-process-buffer rcirc-process
688 rcirc-nick))
689 (cons "%s" (with-rcirc-process-buffer rcirc-process
690 rcirc-server))
691 (cons "%t" (or rcirc-target ""))))
692 (save-excursion
693 (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker)
694 (goto-char rcirc-prompt-start-marker)
695 (let ((start (point)))
696 (insert-before-markers prompt)
697 (set-marker rcirc-prompt-start-marker start)
698 (when (not (zerop (- rcirc-prompt-end-marker
699 rcirc-prompt-start-marker)))
700 (add-text-properties rcirc-prompt-start-marker
701 rcirc-prompt-end-marker
702 (list 'face 'rcirc-prompt
703 'read-only t 'field t
704 'front-sticky t 'rear-nonsticky t))))))))
706 (defun rcirc-set-changed (option value)
707 "Set OPTION to VALUE and do updates after a customization change."
708 (set-default option value)
709 (cond ((eq option 'rcirc-prompt)
710 (rcirc-update-prompt 'all))
712 (error "Bad option %s" option))))
714 (defun rcirc-channel-p (target)
715 "Return t if TARGET is a channel name."
716 (and target
717 (not (zerop (length target)))
718 (or (eq (aref target 0) ?#)
719 (eq (aref target 0) ?&))))
721 (defun rcirc-kill-buffer-hook ()
722 "Part the channel when killing an rcirc buffer."
723 (when (eq major-mode 'rcirc-mode)
724 (rcirc-kill-buffer-hook-1)))
725 (defun rcirc-kill-buffer-hook-1 ()
726 (let ((buffer (current-buffer)))
727 (rcirc-clear-activity buffer)
728 (when (and rcirc-process
729 (eq (process-status rcirc-process) 'open))
730 (with-rcirc-process-buffer rcirc-process
731 (setq rcirc-buffer-alist
732 (rassq-delete-all buffer rcirc-buffer-alist)))
733 (rcirc-update-short-buffer-names)
734 (if (rcirc-channel-p rcirc-target)
735 (rcirc-send-string rcirc-process
736 (concat "PART " rcirc-target
737 " :Killed buffer"))
738 (when rcirc-target
739 (rcirc-remove-nick-channel rcirc-process
740 (rcirc-nick rcirc-process)
741 rcirc-target))))))
743 (add-hook 'kill-buffer-hook 'rcirc-kill-buffer-hook)
745 (defun rcirc-generate-new-buffer-name (process target)
746 "Return a buffer name based on PROCESS and TARGET.
747 This is used for the initial name given to IRC buffers."
748 (if target
749 (concat target "@" (process-name process))
750 (concat "*" (process-name process) "*")))
752 (defun rcirc-get-buffer (process target &optional server)
753 "Return the buffer associated with the PROCESS and TARGET.
755 If optional argument SERVER is non-nil, return the server buffer
756 if there is no existing buffer for TARGET, otherwise return nil."
757 (with-rcirc-process-buffer process
758 (if (null target)
759 (current-buffer)
760 (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t))))
761 (or buffer (when server (current-buffer)))))))
763 (defun rcirc-get-buffer-create (process target)
764 "Return the buffer associated with the PROCESS and TARGET.
765 Create the buffer if it doesn't exist."
766 (let ((buffer (rcirc-get-buffer process target)))
767 (if buffer
768 (with-current-buffer buffer
769 (when (not rcirc-target)
770 (setq rcirc-target target))
771 buffer)
772 ;; create the buffer
773 (with-rcirc-process-buffer process
774 (let ((new-buffer (get-buffer-create
775 (rcirc-generate-new-buffer-name process target))))
776 (with-current-buffer new-buffer
777 (rcirc-mode process target))
778 (rcirc-put-nick-channel process (rcirc-nick process) target)
779 new-buffer)))))
781 (defun rcirc-send-input ()
782 "Send input to target associated with the current buffer."
783 (interactive)
784 (if (< (point) rcirc-prompt-end-marker)
785 ;; copy the line down to the input area
786 (progn
787 (forward-line 0)
788 (let ((start (if (eq (point) (point-min))
789 (point)
790 (if (get-text-property (1- (point)) 'hard)
791 (point)
792 (previous-single-property-change (point) 'hard))))
793 (end (next-single-property-change (1+ (point)) 'hard)))
794 (goto-char (point-max))
795 (insert (replace-regexp-in-string
796 "\n\\s-+" " "
797 (buffer-substring-no-properties start end)))))
798 ;; process input
799 (goto-char (point-max))
800 (let ((target (rcirc-buffer-target))
801 (start rcirc-prompt-end-marker))
802 (when (not (equal 0 (- (point) start)))
803 ;; delete a trailing newline
804 (when (eq (point) (point-at-bol))
805 (delete-backward-char 1))
806 (let ((input (buffer-substring-no-properties
807 rcirc-prompt-end-marker (point))))
808 (dolist (line (split-string input "\n"))
809 (rcirc-process-input-line rcirc-process target line))
810 ;; add to input-ring
811 (save-excursion
812 (ring-insert rcirc-input-ring input)
813 (setq rcirc-input-ring-index 0)))))))
815 (defun rcirc-process-input-line (process target line)
816 (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line)
817 (rcirc-process-command (match-string 1 line)
818 (match-string 2 line)
819 line)
820 (rcirc-process-message line)))
822 (defun rcirc-process-message (line)
823 (if (not rcirc-target)
824 (message "Not joined")
825 (delete-region rcirc-prompt-end-marker (point))
826 (rcirc-send-message rcirc-process rcirc-target line)))
828 (defun rcirc-process-command (command args line)
829 (if (eq (aref command 0) ?/)
830 ;; "//text" will send "/text" as a message
831 (rcirc-process-message (substring line 1))
832 (let* ((fun (intern-soft (concat "rcirc-cmd-" command))))
833 (newline)
834 (with-current-buffer (current-buffer)
835 (delete-region rcirc-prompt-end-marker (point))
836 (if (string= command "me")
837 (rcirc-print rcirc-process (rcirc-nick rcirc-process)
838 "ACTION" rcirc-target args)
839 (rcirc-print rcirc-process (rcirc-nick rcirc-process)
840 "COMMAND" rcirc-target line))
841 (set-marker rcirc-prompt-end-marker (point))
842 (if (fboundp fun)
843 (funcall fun args rcirc-process rcirc-target)
844 (rcirc-send-string rcirc-process
845 (concat command " " args)))))))
847 (defvar rcirc-parent-buffer nil)
848 (defvar rcirc-window-configuration nil)
849 (defun rcirc-edit-multiline ()
850 "Move current edit to a dedicated buffer."
851 (interactive)
852 (let ((pos (1+ (- (point) rcirc-prompt-end-marker))))
853 (goto-char (point-max))
854 (let ((text (buffer-substring rcirc-prompt-end-marker (point)))
855 (parent (buffer-name))
856 (process rcirc-process))
857 (delete-region rcirc-prompt-end-marker (point))
858 (setq rcirc-window-configuration (current-window-configuration))
859 (pop-to-buffer (concat "*multiline " parent "*"))
860 (rcirc-multiline-edit-mode)
861 (setq rcirc-parent-buffer parent)
862 (setq rcirc-process process)
863 (insert text)
864 (and (> pos 0) (goto-char pos))
865 (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent))))
867 (define-derived-mode rcirc-multiline-edit-mode
868 text-mode "rcirc multi"
869 "Major mode for multiline edits
870 \\{rcirc-multiline-edit-mode-map}"
871 (make-local-variable 'rcirc-parent-buffer)
872 (make-local-variable 'rcirc-process))
874 (define-key rcirc-multiline-edit-mode-map
875 (kbd "C-c C-c") 'rcirc-multiline-edit-submit)
876 (define-key rcirc-multiline-edit-mode-map
877 (kbd "C-x C-s") 'rcirc-multiline-edit-submit)
878 (define-key rcirc-multiline-edit-mode-map
879 (kbd "C-c C-k") 'rcirc-multiline-edit-cancel)
880 (define-key rcirc-multiline-edit-mode-map
881 (kbd "ESC ESC ESC") 'rcirc-multiline-edit-cancel)
883 (defun rcirc-multiline-edit-submit ()
884 "Send the text in buffer back to parent buffer."
885 (interactive)
886 (assert (and (eq major-mode 'rcirc-multiline-edit-mode)))
887 (assert rcirc-parent-buffer)
888 (untabify (point-min) (point-max))
889 (let ((text (buffer-substring (point-min) (point-max)))
890 (buffer (current-buffer))
891 (pos (point)))
892 (set-buffer rcirc-parent-buffer)
893 (goto-char (point-max))
894 (insert text)
895 (kill-buffer buffer)
896 (set-window-configuration rcirc-window-configuration)
897 (goto-char (+ rcirc-prompt-end-marker (1- pos)))))
899 (defun rcirc-multiline-edit-cancel ()
900 "Cancel the multiline edit."
901 (interactive)
902 (assert (and (eq major-mode 'rcirc-multiline-edit-mode)))
903 (kill-buffer (current-buffer))
904 (set-window-configuration rcirc-window-configuration))
906 (defun rcirc-any-buffer (process)
907 "Return a buffer for PROCESS, either the one selected or the process buffer."
908 (if rcirc-always-use-server-buffer-flag
909 (process-buffer process)
910 (let ((buffer (window-buffer (selected-window))))
911 (if (and buffer
912 (with-current-buffer buffer
913 (and (eq major-mode 'rcirc-mode)
914 (eq rcirc-process process))))
915 buffer
916 (process-buffer process)))))
918 (defcustom rcirc-response-formats
919 '(("PRIVMSG" . "%T<%N> %m")
920 ("NOTICE" . "%T-%N- %m")
921 ("ACTION" . "%T[%N %m]")
922 ("COMMAND" . "%T%m")
923 ("ERROR" . "%T%fw!!! %m")
924 (t . "%T%fp*** %fs%n %r %m"))
925 "An alist of formats used for printing responses.
926 The format is looked up using the response-type as a key;
927 if no match is found, the default entry (with a key of `t') is used.
929 The entry's value part should be a string, which is inserted with
930 the of the following escape sequences replaced by the described values:
932 %m The message text
933 %n The sender's nick
934 %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick')
935 %r The response-type
936 %T The timestamp (with face `rcirc-timestamp')
937 %t The target
938 %fw Following text uses the face `font-lock-warning-face'
939 %fp Following text uses the face `rcirc-server-prefix'
940 %fs Following text uses the face `rcirc-server'
941 %f[FACE] Following text uses the face FACE
942 %f- Following text uses the default face
943 %% A literal `%' character
945 :type '(alist :key-type (choice (string :tag "Type")
946 (const :tag "Default" t))
947 :value-type string)
948 :group 'rcirc)
950 (defun rcirc-format-response-string (process sender response target text)
951 "Return a nicely-formatted response string, incorporating TEXT
952 \(and perhaps other arguments). The specific formatting used
953 is found by looking up RESPONSE in `rcirc-response-formats'."
954 (let ((chunks
955 (split-string (or (cdr (assoc response rcirc-response-formats))
956 (cdr (assq t rcirc-response-formats)))
957 "%"))
958 (result "")
959 (face nil)
960 key face-key repl)
961 (when (equal (car chunks) "")
962 (pop chunks))
963 (dolist (chunk chunks)
964 (if (equal chunk "")
965 (setq key ?%)
966 (setq key (aref chunk 0))
967 (setq chunk (substring chunk 1)))
968 (setq repl
969 (cond ((eq key ?%)
970 ;; %% -- literal % character
971 "%")
972 ((or (eq key ?n) (eq key ?N))
973 ;; %n/%N -- nick
974 (let ((nick (concat (if (string= (with-rcirc-process-buffer
975 process rcirc-server)
976 sender)
978 (rcirc-abbrev-nick sender))
979 (and target (concat "," target)))))
980 (rcirc-facify nick
981 (if (eq key ?n)
982 face
983 (if (string= sender (rcirc-nick process))
984 'rcirc-my-nick
985 'rcirc-other-nick)))))
986 ((eq key ?T)
987 ;; %T -- timestamp
988 (rcirc-facify
989 (format-time-string rcirc-time-format (current-time))
990 'rcirc-timestamp))
991 ((eq key ?m)
992 ;; %m -- message text
993 ;; We add the text property `rcirc-text' to identify this
994 ;; as the body text.
995 (propertize
996 (rcirc-mangle-text process (rcirc-facify text face))
997 'rcirc-text text))
998 ((eq key ?t)
999 ;; %t -- target
1000 (rcirc-facify (or rcirc-target "") face))
1001 ((eq key ?r)
1002 ;; %r -- response
1003 (rcirc-facify response face))
1004 ((eq key ?f)
1005 ;; %f -- change face
1006 (setq face-key (aref chunk 0))
1007 (setq chunk (substring chunk 1))
1008 (cond ((eq face-key ?w)
1009 ;; %fw -- warning face
1010 (setq face 'font-lock-warning-face))
1011 ((eq face-key ?p)
1012 ;; %fp -- server-prefix face
1013 (setq face 'rcirc-server-prefix))
1014 ((eq face-key ?s)
1015 ;; %fs -- warning face
1016 (setq face 'rcirc-server))
1017 ((eq face-key ?-)
1018 ;; %fs -- warning face
1019 (setq face nil))
1020 ((and (eq face-key ?\[)
1021 (string-match "^\\([^]]*\\)[]]" chunk)
1022 (facep (match-string 1 chunk)))
1023 ;; %f[...] -- named face
1024 (setq face (intern (match-string 1 chunk)))
1025 (setq chunk (substring chunk (match-end 0)))))
1026 "")))
1027 (setq result (concat result repl (rcirc-facify chunk face))))
1028 result))
1030 (defun rcirc-target-buffer (process sender response target text)
1031 "Return a buffer to print the server response."
1032 (assert (not (bufferp target)))
1033 (with-rcirc-process-buffer process
1034 (cond ((not target)
1035 (rcirc-any-buffer process))
1036 ((not (rcirc-channel-p target))
1037 ;; message from another user
1038 (if (string= response "PRIVMSG")
1039 (rcirc-get-buffer-create process (if (string= sender rcirc-nick)
1040 target
1041 sender))
1042 (rcirc-get-buffer process target t)))
1043 ((or (rcirc-get-buffer process target)
1044 (rcirc-any-buffer process))))))
1046 (defvar rcirc-activity-type nil)
1047 (make-variable-buffer-local 'rcirc-activity-type)
1048 (defun rcirc-print (process sender response target text &optional activity)
1049 "Print TEXT in the buffer associated with TARGET.
1050 Format based on SENDER and RESPONSE. If ACTIVITY is non-nil,
1051 record activity."
1052 (unless (or (member sender rcirc-ignore-list)
1053 (member (with-syntax-table rcirc-nick-syntax-table
1054 (when (string-match "^\\([^/]\\w*\\)[:,]" text)
1055 (match-string 1 text))) rcirc-ignore-list))
1056 (let* ((buffer (rcirc-target-buffer process sender response target text))
1057 (inhibit-read-only t))
1058 (with-current-buffer buffer
1059 (let ((moving (= (point) rcirc-prompt-end-marker))
1060 (old-point (point-marker))
1061 (fill-start (marker-position rcirc-prompt-start-marker)))
1063 (unless (string= sender (rcirc-nick process))
1064 ;; only decode text from other senders, not ours
1065 (setq text (decode-coding-string (or text "")
1066 buffer-file-coding-system))
1067 ;; mark the line with overlay arrow
1068 (unless (or (marker-position overlay-arrow-position)
1069 (get-buffer-window (current-buffer)))
1070 (set-marker overlay-arrow-position
1071 (marker-position rcirc-prompt-start-marker))))
1073 ;; temporarily set the marker insertion-type because
1074 ;; insert-before-markers results in hidden text in new buffers
1075 (goto-char rcirc-prompt-start-marker)
1076 (set-marker-insertion-type rcirc-prompt-start-marker t)
1077 (set-marker-insertion-type rcirc-prompt-end-marker t)
1079 (let ((fmted-text
1080 (rcirc-format-response-string process sender response nil
1081 text)))
1083 (insert fmted-text (propertize "\n" 'hard t))
1084 (set-marker-insertion-type rcirc-prompt-start-marker nil)
1085 (set-marker-insertion-type rcirc-prompt-end-marker nil)
1087 (let ((text-start (make-marker)))
1088 (set-marker text-start
1089 (or (next-single-property-change fill-start
1090 'rcirc-text)
1091 (point-max)))
1092 ;; squeeze spaces out of text before rcirc-text
1093 (fill-region fill-start (1- text-start))
1095 ;; fill the text we just inserted, maybe
1096 (when (and rcirc-fill-flag
1097 (not (string= response "372"))) ;/motd
1098 (let ((fill-prefix
1099 (or rcirc-fill-prefix
1100 (make-string (- text-start fill-start) ?\s)))
1101 (fill-column (cond ((eq rcirc-fill-column 'frame-width)
1102 (1- (frame-width)))
1103 (rcirc-fill-column
1104 rcirc-fill-column)
1105 (t fill-column))))
1106 (fill-region fill-start rcirc-prompt-start-marker 'left t)))))
1108 ;; set inserted text to be read-only
1109 (when rcirc-read-only-flag
1110 (put-text-property rcirc-prompt-start-marker fill-start 'read-only t)
1111 (let ((inhibit-read-only t))
1112 (put-text-property rcirc-prompt-start-marker fill-start
1113 'front-sticky t)
1114 (put-text-property (1- (point)) (point) 'rear-nonsticky t)))
1116 ;; truncate buffer if it is very long
1117 (save-excursion
1118 (when (and rcirc-buffer-maximum-lines
1119 (> rcirc-buffer-maximum-lines 0)
1120 (= (forward-line (- rcirc-buffer-maximum-lines)) 0))
1121 (delete-region (point-min) (point))))
1123 ;; set the window point for buffers show in windows
1124 (walk-windows (lambda (w)
1125 (unless (eq (selected-window) w)
1126 (when (and (eq (current-buffer)
1127 (window-buffer w))
1128 (>= (window-point w)
1129 rcirc-prompt-end-marker))
1130 (set-window-point w (point-max)))))
1131 nil t)
1133 ;; restore the point
1134 (goto-char (if moving rcirc-prompt-end-marker old-point))
1136 ;; flush undo (can we do something smarter here?)
1137 (buffer-disable-undo)
1138 (buffer-enable-undo))
1140 ;; record modeline activity
1141 (when activity
1142 (let ((nick-match
1143 (string-match (concat "\\b"
1144 (regexp-quote (rcirc-nick process))
1145 "\\b")
1146 text)))
1147 (when (or (not rcirc-ignore-buffer-activity-flag)
1148 ;; always notice when our nick is mentioned, even
1149 ;; if ignoring channel activity
1150 nick-match)
1151 (rcirc-record-activity
1152 (current-buffer)
1153 (when (or nick-match (not (rcirc-channel-p rcirc-target)))
1154 'nick)))))
1156 (sit-for 0) ; displayed text before hook
1157 (run-hook-with-args 'rcirc-print-hooks
1158 process sender response target text)))))
1160 (defun rcirc-startup-channels (server)
1161 "Return the list of startup channels for SERVER."
1162 (let (channels)
1163 (dolist (i rcirc-startup-channels-alist)
1164 (if (string-match (car i) server)
1165 (setq channels (append channels (cdr i)))))
1166 channels))
1168 (defun rcirc-join-channels (process channels)
1169 "Join CHANNELS."
1170 (save-window-excursion
1171 (dolist (channel channels)
1172 (with-rcirc-process-buffer process
1173 (rcirc-cmd-join channel process)))))
1175 ;;; nick management
1176 (defun rcirc-user-nick (user)
1177 "Return the nick from USER. Remove any non-nick junk."
1178 (save-match-data
1179 (if (string-match "^[@%+]?\\([^! ]+\\)!?" (or user ""))
1180 (match-string 1 user)
1181 user)))
1183 (defun rcirc-user-non-nick (user)
1184 "Return the non-nick portion of USER."
1185 (if (string-match "^[@+]?[^! ]+!?\\(.*\\)" (or user ""))
1186 (match-string 1 user)
1187 user))
1189 (defun rcirc-nick-channels (process nick)
1190 "Return list of channels for NICK."
1191 (with-rcirc-process-buffer process
1192 (mapcar (lambda (x) (car x))
1193 (gethash nick rcirc-nick-table))))
1195 (defun rcirc-put-nick-channel (process nick channel)
1196 "Add CHANNEL to list associated with NICK."
1197 (let ((nick (rcirc-user-nick nick)))
1198 (with-rcirc-process-buffer process
1199 (let* ((chans (gethash nick rcirc-nick-table))
1200 (record (assoc-string channel chans t)))
1201 (if record
1202 (setcdr record (current-time))
1203 (puthash nick (cons (cons channel (current-time))
1204 chans)
1205 rcirc-nick-table))))))
1207 (defun rcirc-nick-remove (process nick)
1208 "Remove NICK from table."
1209 (with-rcirc-process-buffer process
1210 (remhash nick rcirc-nick-table)))
1212 (defun rcirc-remove-nick-channel (process nick channel)
1213 "Remove the CHANNEL from list associated with NICK."
1214 (with-rcirc-process-buffer process
1215 (let* ((chans (gethash nick rcirc-nick-table))
1216 (newchans
1217 ;; instead of assoc-string-delete-all:
1218 (let ((record (assoc-string channel chans t)))
1219 (when record
1220 (setcar record 'delete)
1221 (assq-delete-all 'delete chans)))))
1222 (if newchans
1223 (puthash nick newchans rcirc-nick-table)
1224 (remhash nick rcirc-nick-table)))))
1226 (defun rcirc-channel-nicks (process channel)
1227 "Return the list of nicks in CHANNEL sorted by last activity."
1228 (with-rcirc-process-buffer process
1229 (let (nicks)
1230 (maphash
1231 (lambda (k v)
1232 (let ((record (assoc-string channel v t)))
1233 (if record
1234 (setq nicks (cons (cons k (cdr record)) nicks)))))
1235 rcirc-nick-table)
1236 (mapcar (lambda (x) (car x))
1237 (sort nicks (lambda (x y) (time-less-p (cdr y) (cdr x))))))))
1239 (defun rcirc-ignore-update-automatic (nick)
1240 "Remove NICK from `rcirc-ignore-list'
1241 if NICK is also on `rcirc-ignore-list-automatic'."
1242 (when (member nick rcirc-ignore-list-automatic)
1243 (setq rcirc-ignore-list-automatic
1244 (delete nick rcirc-ignore-list-automatic)
1245 rcirc-ignore-list
1246 (delete nick rcirc-ignore-list))))
1248 ;;; activity tracking
1249 (defvar rcirc-track-minor-mode-map (make-sparse-keymap)
1250 "Keymap for rcirc track minor mode.")
1252 (define-key rcirc-track-minor-mode-map (kbd "C-c `") 'rcirc-next-active-buffer)
1253 (define-key rcirc-track-minor-mode-map (kbd "C-c C-@") 'rcirc-next-active-buffer)
1254 (define-key rcirc-track-minor-mode-map (kbd "C-c C-SPC") 'rcirc-next-active-buffer)
1256 ;;; FIXME: the code to insert `rcirc-activity-string' into
1257 ;;; `global-mode-string' isn't called when the mode is activated by
1258 ;;; customize. I don't know how to set that up.
1259 (define-minor-mode rcirc-track-minor-mode
1260 "Global minor mode for tracking activity in rcirc buffers."
1261 :init-value nil
1262 :lighter ""
1263 :keymap rcirc-track-minor-mode-map
1264 :global t
1265 :group 'rcirc
1266 (or global-mode-string (setq global-mode-string '("")))
1267 ;; toggle the mode-line channel indicator
1268 (if rcirc-track-minor-mode
1269 (and (not (memq 'rcirc-activity-string global-mode-string))
1270 (setq global-mode-string
1271 (append global-mode-string '(rcirc-activity-string))))
1272 (setq global-mode-string
1273 (delete 'rcirc-activity-string global-mode-string))))
1275 (or (assq 'rcirc-ignore-buffer-activity-flag minor-mode-alist)
1276 (setq minor-mode-alist
1277 (cons '(rcirc-ignore-buffer-activity-flag " Ignore") minor-mode-alist)))
1279 (defun rcirc-toggle-ignore-buffer-activity ()
1280 "Toggle the value of `rcirc-ignore-buffer-activity-flag'."
1281 (interactive)
1282 (setq rcirc-ignore-buffer-activity-flag
1283 (not rcirc-ignore-buffer-activity-flag))
1284 (message (if rcirc-ignore-buffer-activity-flag
1285 "Ignore activity in this buffer"
1286 "Notice activity in this buffer"))
1287 (force-mode-line-update))
1289 (defvar rcirc-switch-to-buffer-function 'switch-to-buffer
1290 "Function to use when switching buffers.
1291 Possible values are `switch-to-buffer', `pop-to-buffer', and
1292 `display-buffer'.")
1294 (defun rcirc-switch-to-server-buffer ()
1295 "Switch to the server buffer associated with current channel buffer."
1296 (interactive)
1297 (funcall rcirc-switch-to-buffer-function (process-buffer rcirc-process)))
1299 (defun rcirc-jump-to-first-unread-line ()
1300 "Move the point to the first unread line in this buffer."
1301 (interactive)
1302 (when (marker-position overlay-arrow-position)
1303 (goto-char overlay-arrow-position)))
1305 (defvar rcirc-last-non-irc-buffer nil
1306 "The buffer to switch to when there is no more activity.")
1308 (defun rcirc-next-active-buffer (arg)
1309 "Go to the ARGth rcirc buffer with activity.
1310 The function given by `rcirc-switch-to-buffer-function' is used to
1311 show the buffer."
1312 (interactive "p")
1313 (if rcirc-activity
1314 (progn
1315 (unless (eq major-mode 'rcirc-mode)
1316 (setq rcirc-last-non-irc-buffer (current-buffer)))
1317 (if (and (> arg 0)
1318 (<= arg (length rcirc-activity)))
1319 (funcall rcirc-switch-to-buffer-function
1320 (nth (1- arg) rcirc-activity))
1321 (message "Invalid arg: %d" arg)))
1322 (if (eq major-mode 'rcirc-mode)
1323 (if (not (and rcirc-last-non-irc-buffer
1324 (buffer-live-p rcirc-last-non-irc-buffer)))
1325 (message "No IRC activity. Start something.")
1326 (message "No more IRC activity. Go back to work.")
1327 (funcall rcirc-switch-to-buffer-function rcirc-last-non-irc-buffer)
1328 (setq rcirc-last-non-irc-buffer nil))
1329 (message "No IRC activity."))))
1331 (defvar rcirc-activity-hooks nil
1332 "Hook to be run when there is channel activity.
1334 Functions are called with a single argument, the buffer with the
1335 activity. Only run if the buffer is not visible and
1336 `rcirc-ignore-buffer-activity-flag' is non-nil.")
1338 (defun rcirc-record-activity (buffer type)
1339 "Record BUFFER activity with TYPE."
1340 (with-current-buffer buffer
1341 (when (not (get-buffer-window (current-buffer) t))
1342 (add-to-list 'rcirc-activity (current-buffer))
1343 (if (not rcirc-activity-type)
1344 (setq rcirc-activity-type type))
1345 (rcirc-update-activity-string)))
1346 (run-hook-with-args 'rcirc-activity-hooks buffer))
1348 (defun rcirc-clear-activity (buffer)
1349 "Clear the BUFFER activity."
1350 (setq rcirc-activity (delete buffer rcirc-activity))
1351 (with-current-buffer buffer
1352 (setq rcirc-activity-type nil)))
1354 ;; TODO: add mouse properties
1355 (defun rcirc-update-activity-string ()
1356 "Update mode-line string."
1357 (setq rcirc-activity-string
1358 (if (not rcirc-activity)
1360 (concat " ["
1361 (mapconcat
1362 (lambda (b)
1363 (let ((s (rcirc-short-buffer-name b)))
1364 (with-current-buffer b
1365 (if (not (eq rcirc-activity-type 'nick))
1367 (rcirc-facify s 'rcirc-mode-line-nick)))))
1368 rcirc-activity ",")
1369 "]"))))
1371 (defun rcirc-short-buffer-name (buffer)
1372 "Return a short name for BUFFER to use in the modeline indicator."
1373 (with-current-buffer buffer
1374 (or rcirc-short-buffer-name (buffer-name))))
1376 (defvar rcirc-current-buffer nil)
1377 (defun rcirc-window-configuration-change ()
1378 "Go through visible windows and remove buffers from activity list.
1379 Also, clear the overlay arrow if the current buffer is now hidden."
1380 (let ((current-now-hidden t))
1381 (walk-windows (lambda (w)
1382 (let ((buf (window-buffer w)))
1383 (rcirc-clear-activity buf)
1384 (when (eq buf rcirc-current-buffer)
1385 (setq current-now-hidden nil)))))
1386 (when (and rcirc-current-buffer current-now-hidden)
1387 (with-current-buffer rcirc-current-buffer
1388 (when (eq major-mode 'rcirc-mode)
1389 (marker-position overlay-arrow-position)
1390 (set-marker overlay-arrow-position nil)))))
1392 ;; remove any killed buffers from list
1393 (setq rcirc-activity
1394 (delq nil (mapcar (lambda (buf) (when (buffer-live-p buf) buf))
1395 rcirc-activity)))
1396 (rcirc-update-activity-string)
1397 (setq rcirc-current-buffer (current-buffer)))
1400 ;;; buffer name abbreviation
1401 (defun rcirc-update-short-buffer-names ()
1402 (let ((bufalist
1403 (apply 'append (mapcar (lambda (process)
1404 (with-rcirc-process-buffer process
1405 rcirc-buffer-alist))
1406 (rcirc-process-list)))))
1407 (dolist (i (rcirc-abbreviate bufalist))
1408 (with-current-buffer (cdr i)
1409 (setq rcirc-short-buffer-name (car i))))))
1411 (defun rcirc-abbreviate (pairs)
1412 (apply 'append (mapcar 'rcirc-rebuild-tree (rcirc-make-trees pairs))))
1414 (defun rcirc-rebuild-tree (tree &optional acc)
1415 (let ((ch (char-to-string (car tree))))
1416 (dolist (x (cdr tree))
1417 (if (listp x)
1418 (setq acc (append acc
1419 (mapcar (lambda (y)
1420 (cons (concat ch (car y))
1421 (cdr y)))
1422 (rcirc-rebuild-tree x))))
1423 (setq acc (cons (cons ch x) acc))))
1424 acc))
1426 (defun rcirc-make-trees (pairs)
1427 (let (alist)
1428 (mapc (lambda (pair)
1429 (if (consp pair)
1430 (let* ((str (car pair))
1431 (data (cdr pair))
1432 (char (unless (zerop (length str))
1433 (aref str 0)))
1434 (rest (unless (zerop (length str))
1435 (substring str 1)))
1436 (part (if char (assq char alist))))
1437 (if part
1438 ;; existing partition
1439 (setcdr part (cons (cons rest data) (cdr part)))
1440 ;; new partition
1441 (setq alist (cons (if char
1442 (list char (cons rest data))
1443 data)
1444 alist))))
1445 (setq alist (cons pair alist))))
1446 pairs)
1447 ;; recurse into cdrs of alist
1448 (mapc (lambda (x)
1449 (when (and (listp x) (listp (cadr x)))
1450 (setcdr x (if (> (length (cdr x)) 1)
1451 (rcirc-make-trees (cdr x))
1452 (setcdr x (list (cdadr x)))))))
1453 alist)))
1455 ;;; /commands these are called with 3 args: PROCESS, TARGET, which is
1456 ;; the current buffer/channel/user, and ARGS, which is a string
1457 ;; containing the text following the /cmd.
1459 (defmacro defun-rcirc-command (command argument docstring interactive-form
1460 &rest body)
1461 "Define a command."
1462 `(defun ,(intern (concat "rcirc-cmd-" (symbol-name command)))
1463 (,@argument &optional process target)
1464 ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values of"
1465 "\nbuffer local variables `rcirc-process' and `rcirc-target',"
1466 "\nwill be used.")
1467 ,interactive-form
1468 (let ((process (or process rcirc-process))
1469 (target (or target rcirc-target)))
1470 ,@body)))
1472 (defun-rcirc-command msg (message)
1473 "Send private MESSAGE to TARGET."
1474 (interactive "i")
1475 (if (null message)
1476 (progn
1477 (setq target (completing-read "Message nick: "
1478 (with-rcirc-process-buffer rcirc-process
1479 rcirc-nick-table)))
1480 (when (> (length target) 0)
1481 (setq message (read-string (format "Message %s: " target)))
1482 (when (> (length message) 0)
1483 (rcirc-send-message process target message))))
1484 (if (not (string-match "\\([^ ]+\\) \\(.+\\)" message))
1485 (message "Not enough args, or something.")
1486 (setq target (match-string 1 message)
1487 message (match-string 2 message))
1488 (rcirc-send-message process target message))))
1490 (defun-rcirc-command query (nick)
1491 "Open a private chat buffer to NICK."
1492 (interactive (list (completing-read "Query nick: "
1493 (with-rcirc-process-buffer rcirc-process
1494 rcirc-nick-table))))
1495 (let ((existing-buffer (rcirc-get-buffer process nick)))
1496 (switch-to-buffer (or existing-buffer
1497 (rcirc-get-buffer-create process nick)))
1498 (when (not existing-buffer)
1499 (rcirc-cmd-whois nick))))
1501 (defun-rcirc-command join (channel)
1502 "Join CHANNEL."
1503 (interactive "sJoin channel: ")
1504 (let ((buffer (rcirc-get-buffer-create process
1505 (car (split-string channel)))))
1506 (when (not (eq (selected-window) (minibuffer-window)))
1507 (funcall rcirc-switch-to-buffer-function buffer))
1508 (rcirc-send-string process (concat "JOIN " channel))))
1510 (defun-rcirc-command part (channel)
1511 "Part CHANNEL."
1512 (interactive "sPart channel: ")
1513 (let ((channel (if (> (length channel) 0) channel target)))
1514 (rcirc-send-string process (concat "PART " channel " :" rcirc-id-string))))
1516 (defun-rcirc-command quit (reason)
1517 "Send a quit message to server with REASON."
1518 (interactive "sQuit reason: ")
1519 (rcirc-send-string process (concat "QUIT :"
1520 (if (not (zerop (length reason)))
1521 reason
1522 rcirc-id-string))))
1524 (defun-rcirc-command nick (nick)
1525 "Change nick to NICK."
1526 (interactive "i")
1527 (when (null nick)
1528 (setq nick (read-string "New nick: " (rcirc-nick process))))
1529 (rcirc-send-string process (concat "NICK " nick)))
1531 (defun-rcirc-command names (channel)
1532 "Display list of names in CHANNEL or in current channel if CHANNEL is nil.
1533 If called interactively, prompt for a channel when prefix arg is supplied."
1534 (interactive "P")
1535 (if (interactive-p)
1536 (if channel
1537 (setq channel (read-string "List names in channel: " target))))
1538 (let ((channel (if (> (length channel) 0)
1539 channel
1540 target)))
1541 (rcirc-send-string process (concat "NAMES " channel))))
1543 (defun-rcirc-command topic (topic)
1544 "List TOPIC for the TARGET channel.
1545 With a prefix arg, prompt for new topic."
1546 (interactive "P")
1547 (if (and (interactive-p) topic)
1548 (setq topic (read-string "New Topic: " rcirc-topic)))
1549 (rcirc-send-string process (concat "TOPIC " target
1550 (when (> (length topic) 0)
1551 (concat " :" topic)))))
1553 (defun-rcirc-command whois (nick)
1554 "Request information from server about NICK."
1555 (interactive (list
1556 (completing-read "Whois: "
1557 (with-rcirc-process-buffer rcirc-process
1558 rcirc-nick-table))))
1559 (rcirc-send-string process (concat "WHOIS " nick)))
1561 (defun-rcirc-command mode (args)
1562 "Set mode with ARGS."
1563 (interactive (list (concat (read-string "Mode nick or channel: ")
1564 " " (read-string "Mode: "))))
1565 (rcirc-send-string process (concat "MODE " args)))
1567 (defun-rcirc-command list (channels)
1568 "Request information on CHANNELS from server."
1569 (interactive "sList Channels: ")
1570 (rcirc-send-string process (concat "LIST " channels)))
1572 (defun-rcirc-command oper (args)
1573 "Send operator command to server."
1574 (interactive "sOper args: ")
1575 (rcirc-send-string process (concat "OPER " args)))
1577 (defun-rcirc-command quote (message)
1578 "Send MESSAGE literally to server."
1579 (interactive "sServer message: ")
1580 (rcirc-send-string process message))
1582 (defun-rcirc-command kick (arg)
1583 "Kick NICK from current channel."
1584 (interactive (list
1585 (concat (completing-read "Kick nick: "
1586 (rcirc-channel-nicks rcirc-process
1587 rcirc-target))
1588 (read-from-minibuffer "Kick reason: "))))
1589 (let* ((arglist (split-string arg))
1590 (argstring (concat (car arglist) " :"
1591 (mapconcat 'identity (cdr arglist) " "))))
1592 (rcirc-send-string process (concat "KICK " target " " argstring))))
1594 (defun rcirc-cmd-ctcp (args &optional process target)
1595 (if (string-match "^\\([^ ]+\\)\\s-+\\(.+\\)$" args)
1596 (let ((target (match-string 1 args))
1597 (request (match-string 2 args)))
1598 (rcirc-send-string process
1599 (format "PRIVMSG %s \C-a%s\C-a"
1600 target (upcase request))))
1601 (rcirc-print process (rcirc-nick process) "ERROR" nil
1602 "usage: /ctcp NICK REQUEST")))
1604 (defun rcirc-cmd-me (args &optional process target)
1605 (rcirc-send-string process (format "PRIVMSG %s :\C-aACTION %s\C-a"
1606 target args)))
1608 (defun-rcirc-command ignore (nick)
1609 "Manage the ignore list.
1610 Ignore NICK, unignore NICK if already ignored, or list ignored
1611 nicks when no NICK is given. When listing ignored nicks, the
1612 ones added to the list automatically are marked with an asterisk."
1613 (interactive "sToggle ignoring of nick: ")
1614 (when (not (string= "" nick))
1615 (if (member nick rcirc-ignore-list)
1616 (setq rcirc-ignore-list (delete nick rcirc-ignore-list))
1617 (setq rcirc-ignore-list (cons nick rcirc-ignore-list))))
1618 (rcirc-print process (rcirc-nick process) "IGNORE" target
1619 (mapconcat
1620 (lambda (nick)
1621 (concat nick
1622 (if (member nick rcirc-ignore-list-automatic)
1623 "*" "")))
1624 rcirc-ignore-list " ")))
1627 (defun rcirc-message-leader (sender face)
1628 "Return a string with SENDER propertized with FACE."
1629 (rcirc-facify (concat "<" sender "> ") face))
1631 (defun rcirc-facify (string face)
1632 "Return a copy of STRING with FACE property added."
1633 (propertize (or string "") 'face face 'rear-nonsticky t))
1635 (defvar rcirc-url-regexp
1636 (rx-to-string
1637 `(and word-boundary
1638 (or "www."
1639 (and (or "http" "https" "ftp" "file" "gopher" "news" "telnet"
1640 "wais" "mailto")
1641 "://"
1642 (1+ (char "-a-zA-Z0-9_."))
1643 (optional ":" (1+ (char "0-9"))))
1644 (and (1+ (char "-a-zA-Z0-9_."))
1645 (or ".com" ".net" ".org")
1646 word-boundary))
1647 (optional
1648 (and "/"
1649 (1+ (char "-a-zA-Z0-9_=!?#$\@~`%&*+|\\/:;.,{}[]"))
1650 (char "-a-zA-Z0-9_=#$\@~`%&*+|\\/:;{}[]")))))
1651 "Regexp matching URLs. Set to nil to disable URL features in rcirc.")
1653 (defun rcirc-browse-url (&optional arg)
1654 "Prompt for URL to browse based on URLs in buffer."
1655 (interactive)
1656 (let ((completions (mapcar (lambda (x) (cons x nil)) rcirc-urls))
1657 (initial-input (car rcirc-urls))
1658 (history (cdr rcirc-urls)))
1659 (browse-url (completing-read "rcirc browse-url: "
1660 completions nil nil initial-input 'history)
1661 arg)))
1663 (defun rcirc-browse-url-at-point (point)
1664 "Send URL at point to `browse-url'."
1665 (interactive "d")
1666 (let ((beg (previous-single-property-change point 'mouse-face))
1667 (end (next-single-property-change point 'mouse-face)))
1668 (browse-url (buffer-substring-no-properties beg end))))
1670 (defun rcirc-browse-url-at-mouse (event)
1671 "Send URL at mouse click to `browse-url'."
1672 (interactive "e")
1673 (let ((position (event-end event)))
1674 (with-current-buffer (window-buffer (posn-window position))
1675 (rcirc-browse-url-at-point (posn-point position)))))
1677 (defun rcirc-map-regexp (function regexp string)
1678 "Return a copy of STRING after calling FUNCTION for each REGEXP match.
1679 FUNCTION takes 3 arguments, MATCH-START, MATCH-END, and STRING."
1680 (let ((start 0))
1681 (while (string-match regexp string start)
1682 (setq start (match-end 0))
1683 (funcall function (match-beginning 0) (match-end 0) string)))
1684 string)
1686 (defun rcirc-mangle-text (process text)
1687 "Return TEXT with properties added based on various patterns."
1688 ;; ^B
1689 (setq text
1690 (rcirc-map-regexp
1691 (lambda (start end string)
1692 (let ((orig-face (get-text-property start 'face string)))
1693 (add-text-properties
1694 start end
1695 (list 'face (if (listp orig-face)
1696 (append orig-face
1697 (list 'bold))
1698 (list orig-face 'bold))
1699 'rear-nonsticky t)
1700 string)))
1701 "\x02.*?\x02"
1702 text))
1703 ;; TODO: deal with ^_ and ^C colors sequences
1704 (while (string-match "\\(.*\\)[\x02\x01]\\(.*\\)" text)
1705 (setq text (concat (match-string 1 text)
1706 (match-string 2 text))))
1707 ;; my nick
1708 (setq text
1709 (with-syntax-table rcirc-nick-syntax-table
1710 (rcirc-map-regexp (lambda (start end string)
1711 (add-text-properties
1712 start end
1713 (list 'face 'rcirc-nick-in-message
1714 'rear-nonsticky t)
1715 string))
1716 (concat "\\b"
1717 (regexp-quote (rcirc-nick process))
1718 "\\b")
1719 text)))
1720 ;; urls
1721 (setq text
1722 (rcirc-map-regexp
1723 (lambda (start end string)
1724 (let ((orig-face (get-text-property start 'face string)))
1725 (add-text-properties start end
1726 (list 'face (if (listp orig-face)
1727 (append orig-face
1728 (list 'bold))
1729 (list orig-face 'bold))
1730 'rear-nonsticky t
1731 'mouse-face 'highlight
1732 'keymap rcirc-browse-url-map)
1733 string))
1734 (push (substring-no-properties string start end) rcirc-urls))
1735 rcirc-url-regexp
1736 text))
1737 text)
1740 ;;; handlers
1741 ;; these are called with the server PROCESS, the SENDER, which is a
1742 ;; server or a user, depending on the command, the ARGS, which is a
1743 ;; list of strings, and the TEXT, which is the original server text,
1744 ;; verbatim
1745 (defun rcirc-handler-001 (process sender args text)
1746 (rcirc-handler-generic process "001" sender args text)
1747 ;; set the real server name
1748 (with-rcirc-process-buffer process
1749 (setq rcirc-server sender)
1750 (setq rcirc-nick (car args))
1751 (rcirc-update-prompt)
1752 (when rcirc-auto-authenticate-flag (rcirc-authenticate))
1753 (rcirc-join-channels process rcirc-startup-channels)))
1755 (defun rcirc-handler-PRIVMSG (process sender args text)
1756 (let ((target (if (rcirc-channel-p (car args))
1757 (car args)
1758 sender))
1759 (message (or (cadr args) "")))
1760 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
1761 (rcirc-handler-CTCP process target sender (match-string 1 message))
1762 (rcirc-print process sender "PRIVMSG" target message t))
1763 ;; update nick timestamp
1764 (if (member target (rcirc-nick-channels process sender))
1765 (rcirc-put-nick-channel process sender target))))
1767 (defun rcirc-handler-NOTICE (process sender args text)
1768 (let ((target (car args))
1769 (message (cadr args)))
1770 (if (string-match "^\C-a\\(.*\\)\C-a$" message)
1771 (rcirc-handler-CTCP-response process target sender
1772 (match-string 1 message))
1773 (rcirc-print process sender "NOTICE"
1774 (cond ((rcirc-channel-p target)
1775 target)
1776 ;;; -ChanServ- [#gnu] Welcome...
1777 ((string-match "^\\[\\(#[^ ]+\\)\\]" message)
1778 (match-string 1 message))
1779 (sender
1780 (if (string= sender (rcirc-server process))
1781 nil ; server notice
1782 sender)))
1783 message t))))
1785 (defun rcirc-handler-WALLOPS (process sender args text)
1786 (rcirc-print process sender "WALLOPS" sender (car args) t))
1788 (defun rcirc-handler-JOIN (process sender args text)
1789 (let ((channel (car args)))
1790 (rcirc-get-buffer-create process channel)
1791 (rcirc-print process sender "JOIN" channel "")
1793 ;; print in private chat buffer if it exists
1794 (when (rcirc-get-buffer rcirc-process sender)
1795 (rcirc-print process sender "JOIN" sender channel))
1797 (rcirc-put-nick-channel process sender channel)))
1799 ;; PART and KICK are handled the same way
1800 (defun rcirc-handler-PART-or-KICK (process response channel sender nick args)
1801 (rcirc-print process sender response channel (concat channel " " args))
1803 ;; print in private chat buffer if it exists
1804 (when (rcirc-get-buffer rcirc-process nick)
1805 (rcirc-print process sender response nick (concat channel " " args)))
1807 (if (not (string= nick (rcirc-nick process)))
1808 ;; this is someone else leaving
1809 (rcirc-remove-nick-channel process nick channel)
1810 ;; this is us leaving
1811 (mapc (lambda (n)
1812 (rcirc-remove-nick-channel process n channel))
1813 (rcirc-channel-nicks process channel))
1815 ;; if the buffer is still around, make it inactive
1816 (let ((buffer (rcirc-get-buffer process channel)))
1817 (when buffer
1818 (with-current-buffer buffer
1819 (setq rcirc-target nil))))))
1821 (defun rcirc-handler-PART (process sender args text)
1822 (rcirc-ignore-update-automatic sender)
1823 (rcirc-handler-PART-or-KICK process "PART"
1824 (car args) sender sender
1825 (cadr args)))
1827 (defun rcirc-handler-KICK (process sender args text)
1828 (rcirc-handler-PART-or-KICK process "KICK" (car args) sender (cadr args)
1829 (caddr args)))
1831 (defun rcirc-handler-QUIT (process sender args text)
1832 (rcirc-ignore-update-automatic sender)
1833 (mapc (lambda (channel)
1834 (rcirc-print process sender "QUIT" channel (apply 'concat args)))
1835 (rcirc-nick-channels process sender))
1837 ;; print in private chat buffer if it exists
1838 (when (rcirc-get-buffer rcirc-process sender)
1839 (rcirc-print process sender "QUIT" sender (apply 'concat args)))
1841 (rcirc-nick-remove process sender))
1843 (defun rcirc-handler-NICK (process sender args text)
1844 (let* ((old-nick sender)
1845 (new-nick (car args))
1846 (channels (rcirc-nick-channels process old-nick)))
1847 ;; update list of ignored nicks
1848 (rcirc-ignore-update-automatic old-nick)
1849 (when (member old-nick rcirc-ignore-list)
1850 (add-to-list 'rcirc-ignore-list new-nick)
1851 (add-to-list 'rcirc-ignore-list-automatic new-nick))
1852 ;; print message to nick's channels
1853 (dolist (target channels)
1854 (rcirc-print process sender "NICK" target new-nick))
1855 ;; update private chat buffer, if it exists
1856 (let ((chat-buffer (rcirc-get-buffer process old-nick)))
1857 (when chat-buffer
1858 (with-current-buffer chat-buffer
1859 (rcirc-print process sender "NICK" old-nick new-nick)
1860 (setq rcirc-target new-nick)
1861 (rename-buffer (rcirc-generate-new-buffer-name process new-nick)))))
1862 ;; remove old nick and add new one
1863 (with-rcirc-process-buffer process
1864 (let ((v (gethash old-nick rcirc-nick-table)))
1865 (remhash old-nick rcirc-nick-table)
1866 (puthash new-nick v rcirc-nick-table))
1867 ;; if this is our nick...
1868 (when (string= old-nick rcirc-nick)
1869 (setq rcirc-nick new-nick)
1870 (rcirc-update-prompt t)
1871 ;; reauthenticate
1872 (when rcirc-auto-authenticate-flag (rcirc-authenticate))))))
1874 (defun rcirc-handler-PING (process sender args text)
1875 (rcirc-send-string process (concat "PONG " (car args))))
1877 (defun rcirc-handler-PONG (process sender args text)
1878 ;; do nothing
1881 (defun rcirc-handler-TOPIC (process sender args text)
1882 (let ((topic (cadr args)))
1883 (rcirc-print process sender "TOPIC" (car args) topic)
1884 (with-current-buffer (rcirc-get-buffer process (car args))
1885 (setq rcirc-topic topic))))
1887 (defun rcirc-handler-332 (process sender args text)
1888 "RPL_TOPIC"
1889 (let ((buffer (or (rcirc-get-buffer process (cadr args))
1890 (rcirc-get-temp-buffer-create process (cadr args)))))
1891 (with-current-buffer buffer
1892 (setq rcirc-topic (caddr args)))))
1894 (defun rcirc-handler-333 (process sender args text)
1895 "Not in rfc1459.txt"
1896 (let ((buffer (or (rcirc-get-buffer process (cadr args))
1897 (rcirc-get-temp-buffer-create process (cadr args)))))
1898 (with-current-buffer buffer
1899 (let ((setter (caddr args))
1900 (time (current-time-string
1901 (seconds-to-time
1902 (string-to-number (cadddr args))))))
1903 (rcirc-print process sender "TOPIC" (cadr args)
1904 (format "%s (%s on %s)" rcirc-topic setter time))))))
1906 (defun rcirc-handler-477 (process sender args text)
1907 "ERR_NOCHANMODES"
1908 (rcirc-print process sender "477" (cadr args) (caddr args)))
1910 (defun rcirc-handler-MODE (process sender args text)
1911 (let ((target (car args))
1912 (msg (mapconcat 'identity (cdr args) " ")))
1913 (rcirc-print process sender "MODE"
1914 (if (string= target (rcirc-nick process))
1916 target)
1917 msg)
1919 ;; print in private chat buffers if they exist
1920 (mapc (lambda (nick)
1921 (when (rcirc-get-buffer process nick)
1922 (rcirc-print process sender "MODE" nick msg)))
1923 (cddr args))))
1925 (defun rcirc-get-temp-buffer-create (process channel)
1926 "Return a buffer based on PROCESS and CHANNEL."
1927 (let ((tmpnam (concat " " (downcase channel) "TMP" (process-name process))))
1928 (get-buffer-create tmpnam)))
1930 (defun rcirc-handler-353 (process sender args text)
1931 "RPL_NAMREPLY"
1932 (let ((channel (caddr args)))
1933 (mapc (lambda (nick)
1934 (rcirc-put-nick-channel process nick channel))
1935 (split-string (cadddr args) " " t))
1936 (with-current-buffer (rcirc-get-temp-buffer-create process channel)
1937 (goto-char (point-max))
1938 (insert (car (last args)) " "))))
1940 (defun rcirc-handler-366 (process sender args text)
1941 "RPL_ENDOFNAMES"
1942 (let* ((channel (cadr args))
1943 (buffer (rcirc-get-temp-buffer-create process channel)))
1944 (with-current-buffer buffer
1945 (rcirc-print process sender "NAMES" channel
1946 (buffer-substring (point-min) (point-max))))
1947 (kill-buffer buffer)))
1949 (defun rcirc-handler-433 (process sender args text)
1950 "ERR_NICKNAMEINUSE"
1951 (rcirc-handler-generic process "433" sender args text)
1952 (let* ((new-nick (concat (cadr args) "`")))
1953 (with-rcirc-process-buffer process
1954 (rcirc-cmd-nick new-nick nil process))))
1956 (defun rcirc-authenticate ()
1957 "Send authentication to process associated with current buffer.
1958 Passwords are stored in `rcirc-authinfo' (which see)."
1959 (interactive)
1960 (with-rcirc-process-buffer rcirc-process
1961 (dolist (i rcirc-authinfo)
1962 (let ((server (car i))
1963 (nick (caddr i))
1964 (method (cadr i))
1965 (args (cdddr i)))
1966 (when (and (string-match server rcirc-server)
1967 (string-match nick rcirc-nick))
1968 (cond ((equal method 'nickserv)
1969 (rcirc-send-string
1970 rcirc-process
1971 (concat
1972 "PRIVMSG nickserv :identify "
1973 (car args))))
1974 ((equal method 'chanserv)
1975 (rcirc-send-string
1976 rcirc-process
1977 (concat
1978 "PRIVMSG chanserv :identify "
1979 (cadr args) " " (car args))))
1980 ((equal method 'bitlbee)
1981 (rcirc-send-string
1982 rcirc-process
1983 (concat "PRIVMSG &bitlbee :identify " (car args))))
1985 (message "No %S authentication method defined"
1986 method))))))))
1988 (defun rcirc-handler-INVITE (process sender args text)
1989 (rcirc-print process sender "INVITE" nil (mapconcat 'identity args " ") t))
1991 (defun rcirc-handler-ERROR (process sender args text)
1992 (rcirc-print process sender "ERROR" nil (mapconcat 'identity args " ")))
1994 (defun rcirc-handler-CTCP (process target sender text)
1995 (if (string-match "^\\([^ ]+\\) *\\(.*\\)$" text)
1996 (let* ((request (upcase (match-string 1 text)))
1997 (args (match-string 2 text))
1998 (handler (intern-soft (concat "rcirc-handler-ctcp-" request))))
1999 (if (not (fboundp handler))
2000 (rcirc-print process sender "ERROR" target
2001 (format "%s sent unsupported ctcp: %s" sender text)
2003 (funcall handler process target sender args)
2004 (if (not (string= request "ACTION"))
2005 (rcirc-print process sender "CTCP" target
2006 (format "%s" text) t))))))
2008 (defun rcirc-handler-ctcp-VERSION (process target sender args)
2009 (rcirc-send-string process
2010 (concat "NOTICE " sender
2011 " :\C-aVERSION " rcirc-id-string
2012 "\C-a")))
2014 (defun rcirc-handler-ctcp-ACTION (process target sender args)
2015 (rcirc-print process sender "ACTION" target args t))
2017 (defun rcirc-handler-ctcp-TIME (process target sender args)
2018 (rcirc-send-string process
2019 (concat "NOTICE " sender
2020 " :\C-aTIME " (current-time-string) "\C-a")))
2022 (defun rcirc-handler-CTCP-response (process target sender message)
2023 (rcirc-print process sender "CTCP" nil message t))
2025 (defgroup rcirc-faces nil
2026 "Faces for rcirc."
2027 :group 'rcirc
2028 :group 'faces)
2030 (defface rcirc-my-nick ; font-lock-function-name-face
2031 '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
2032 (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
2033 (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
2034 (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
2035 (((class color) (min-colors 8)) (:foreground "blue" :weight bold))
2036 (t (:inverse-video t :weight bold)))
2037 "The face used to highlight my messages."
2038 :group 'rcirc-faces)
2040 (defface rcirc-other-nick ; font-lock-variable-name-face
2041 '((((class grayscale) (background light))
2042 (:foreground "Gray90" :weight bold :slant italic))
2043 (((class grayscale) (background dark))
2044 (:foreground "DimGray" :weight bold :slant italic))
2045 (((class color) (min-colors 88) (background light)) (:foreground "DarkGoldenrod"))
2046 (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod"))
2047 (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
2048 (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
2049 (((class color) (min-colors 8)) (:foreground "yellow" :weight light))
2050 (t (:weight bold :slant italic)))
2051 "The face used to highlight other messages."
2052 :group 'rcirc-faces)
2054 (defface rcirc-server ; font-lock-comment-face
2055 '((((class grayscale) (background light))
2056 (:foreground "DimGray" :weight bold :slant italic))
2057 (((class grayscale) (background dark))
2058 (:foreground "LightGray" :weight bold :slant italic))
2059 (((class color) (min-colors 88) (background light))
2060 (:foreground "Firebrick"))
2061 (((class color) (min-colors 88) (background dark))
2062 (:foreground "chocolate1"))
2063 (((class color) (min-colors 16) (background light))
2064 (:foreground "red"))
2065 (((class color) (min-colors 16) (background dark))
2066 (:foreground "red1"))
2067 (((class color) (min-colors 8) (background light))
2069 (((class color) (min-colors 8) (background dark))
2071 (t (:weight bold :slant italic)))
2072 "The face used to highlight server messages."
2073 :group 'rcirc-faces)
2075 (defface rcirc-server-prefix ; font-lock-comment-delimiter-face
2076 '((default :inherit rcirc-server)
2077 (((class grayscale)))
2078 (((class color) (min-colors 16)))
2079 (((class color) (min-colors 8) (background light))
2080 :foreground "red")
2081 (((class color) (min-colors 8) (background dark))
2082 :foreground "red1"))
2083 "The face used to highlight server prefixes."
2084 :group 'rcirc-faces)
2086 (defface rcirc-timestamp
2087 '((t (:inherit default)))
2088 "The face used to highlight timestamps."
2089 :group 'rcirc-faces)
2091 (defface rcirc-nick-in-message ; font-lock-keyword-face
2092 '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold))
2093 (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold))
2094 (((class color) (min-colors 88) (background light)) (:foreground "Purple"))
2095 (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
2096 (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
2097 (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
2098 (((class color) (min-colors 8)) (:foreground "cyan" :weight bold))
2099 (t (:weight bold)))
2100 "The face used to highlight instances of nick within messages."
2101 :group 'rcirc-faces)
2103 (defface rcirc-prompt ; comint-highlight-prompt
2104 '((((min-colors 88) (background dark)) (:foreground "cyan1"))
2105 (((background dark)) (:foreground "cyan"))
2106 (t (:foreground "dark blue")))
2107 "The face used to highlight prompts."
2108 :group 'rcirc-faces)
2110 (defface rcirc-mode-line-nick
2111 '((t (:bold t)))
2112 "The face used indicate activity directed at you."
2113 :group 'rcirc-faces)
2115 ;; When using M-x flyspell-mode, only check words after the prompt
2116 (put 'rcirc-mode 'flyspell-mode-predicate 'rcirc-looking-at-input)
2117 (defun rcirc-looking-at-input ()
2118 "Returns true if point is past the input marker."
2119 (>= (point) rcirc-prompt-end-marker))
2122 (provide 'rcirc)
2124 ;; arch-tag: b471b7e8-6b5a-4399-b2c6-a3c78dfc8ffb
2125 ;;; rcirc.el ends here