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