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