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