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