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