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