* net/rcirc.el (rcirc-sort-nicknames): Change default.
[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
a91dedc4
SM
284;; Does this really merit a config variable?
285(defcustom rcirc-sort-nicknames t
c62bf05a
DD
286 "If non-nil, sorts nickname listings."
287 :type 'boolean
288 :group 'rcirc)
289
db58efbf
EZ
290(defcustom rcirc-always-use-server-buffer-flag nil
291 "Non-nil means messages without a channel target will go to the server buffer."
292 :type 'boolean
293 :group 'rcirc)
294
02f47e86 295(defcustom rcirc-decode-coding-system 'utf-8
a2524d26
EZ
296 "Coding system used to decode incoming irc messages."
297 :type 'coding-system
298 :group 'rcirc)
299
300(defcustom rcirc-encode-coding-system 'utf-8
301 "Coding system used to encode outgoing irc messages."
302 :type 'coding-system
303 :group 'rcirc)
304
305(defcustom rcirc-coding-system-alist nil
f8db61b2 306 "Alist to decide a coding system to use for a channel I/O operation.
a2524d26
EZ
307The format is ((PATTERN . VAL) ...).
308PATTERN is either a string or a cons of strings.
309If PATTERN is a string, it is used to match a target.
310If PATTERN is a cons of strings, the car part is used to match a
311target, and the cdr part is used to match a server.
312VAL is either a coding system or a cons of coding systems.
313If VAL is a coding system, it is used for both decoding and encoding
314messages.
315If VAL is a cons of coding systems, the car part is used for decoding,
316and the cdr part is used for encoding."
317 :type '(alist :key-type (choice (string :tag "Channel Regexp")
318 (cons (string :tag "Channel Regexp")
319 (string :tag "Server Regexp")))
320 :value-type (choice coding-system
321 (cons (coding-system :tag "Decode")
322 (coding-system :tag "Encode"))))
323 :group 'rcirc)
324
325(defcustom rcirc-multiline-major-mode 'fundamental-mode
326 "Major-mode function to use in multiline edit buffers."
327 :type 'function
328 :group 'rcirc)
329
330(defvar rcirc-nick nil)
331
bd43c990
RS
332(defvar rcirc-prompt-start-marker nil)
333(defvar rcirc-prompt-end-marker nil)
334
335(defvar rcirc-nick-table nil)
336
a0a5c583
GM
337(defvar rcirc-recent-quit-alist nil
338 "Alist of nicks that have recently quit or parted the channel.")
339
2c8abe90
AS
340(defvar rcirc-nick-syntax-table
341 (let ((table (make-syntax-table text-mode-syntax-table)))
342 (mapc (lambda (c) (modify-syntax-entry c "w" table))
343 "[]\\`_^{|}-")
344 (modify-syntax-entry ?' "_" table)
345 table)
346 "Syntax table which includes all nick characters as word constituents.")
347
adf794e4
EZ
348;; each process has an alist of (target . buffer) pairs
349(defvar rcirc-buffer-alist nil)
350
bd43c990 351(defvar rcirc-activity nil
a2524d26 352 "List of buffers with unviewed activity.")
bd43c990
RS
353
354(defvar rcirc-activity-string ""
355 "String displayed in modeline representing `rcirc-activity'.")
356(put 'rcirc-activity-string 'risky-local-variable t)
357
a2524d26
EZ
358(defvar rcirc-server-buffer nil
359 "The server buffer associated with this channel buffer.")
bd43c990
RS
360
361(defvar rcirc-target nil
362 "The channel or user associated with this buffer.")
363
bd43c990
RS
364(defvar rcirc-urls nil
365 "List of urls seen in the current buffer.")
7faa3f8c 366(put 'rcirc-urls 'permanent-local t)
bd43c990 367
2e875089 368(defvar rcirc-timeout-seconds 600
8216fbaf 369 "Kill connection after this many seconds if there is no activity.")
bd43c990 370
adf794e4 371(defconst rcirc-id-string (concat "rcirc on GNU Emacs " emacs-version))
bd43c990 372\f
bd43c990 373(defvar rcirc-startup-channels nil)
195eca78 374
c0db3477
CY
375(defvar rcirc-server-name-history nil
376 "History variable for \\[rcirc] call.")
377
378(defvar rcirc-server-port-history nil
379 "History variable for \\[rcirc] call.")
380
381(defvar rcirc-nick-name-history nil
382 "History variable for \\[rcirc] call.")
383
d26781af
RY
384(defvar rcirc-user-name-history nil
385 "History variable for \\[rcirc] call.")
386
bd43c990 387;;;###autoload
db58efbf 388(defun rcirc (arg)
0ffab1eb 389 "Connect to all servers in `rcirc-server-alist'.
195eca78
SM
390
391Do not connect to a server if it is already connected.
392
393If ARG is non-nil, instead prompt for connection parameters."
db58efbf
EZ
394 (interactive "P")
395 (if arg
0ffab1eb
TTN
396 (let* ((server (completing-read "IRC Server: "
397 rcirc-server-alist
195eca78 398 nil nil
c0db3477
CY
399 (caar rcirc-server-alist)
400 'rcirc-server-name-history))
0ffab1eb
TTN
401 (server-plist (cdr (assoc-string server rcirc-server-alist)))
402 (port (read-string "IRC Port: "
195eca78 403 (number-to-string
d8937064 404 (or (plist-get server-plist :port)
c0db3477
CY
405 rcirc-default-port))
406 'rcirc-server-port-history))
195eca78 407 (nick (read-string "IRC Nick: "
d8937064 408 (or (plist-get server-plist :nick)
c0db3477
CY
409 rcirc-default-nick)
410 'rcirc-nick-name-history))
d26781af
RY
411 (user-name (read-string "IRC Username: "
412 (or (plist-get server-plist :user-name)
413 rcirc-default-user-name)
414 'rcirc-user-name-history))
415 (pass (read-passwd "IRC Password: " nil
416 (plist-get server-plist :pass)))
db58efbf
EZ
417 (channels (split-string
418 (read-string "IRC Channels: "
0ffab1eb 419 (mapconcat 'identity
195eca78 420 (plist-get server-plist
d8937064 421 :channels)
195eca78 422 " "))
db58efbf 423 "[, ]+" t)))
d26781af 424 (rcirc-connect server port nick user-name pass
0ffab1eb 425 rcirc-default-full-name
db58efbf 426 channels))
0ffab1eb 427 ;; connect to servers in `rcirc-server-alist'
195eca78 428 (let (connected-servers)
0ffab1eb 429 (dolist (c rcirc-server-alist)
195eca78 430 (let ((server (car c))
0ffab1eb
TTN
431 (nick (or (plist-get (cdr c) :nick) rcirc-default-nick))
432 (port (or (plist-get (cdr c) :port) rcirc-default-port))
433 (user-name (or (plist-get (cdr c) :user-name)
195eca78 434 rcirc-default-user-name))
d26781af 435 (pass (plist-get (cdr c) :pass))
0ffab1eb
TTN
436 (full-name (or (plist-get (cdr c) :full-name)
437 rcirc-default-full-name))
438 (channels (plist-get (cdr c) :channels)))
195eca78
SM
439 (when server
440 (let (connected)
441 (dolist (p (rcirc-process-list))
442 (when (string= server (process-name p))
443 (setq connected p)))
444 (if (not connected)
445 (condition-case e
d26781af 446 (rcirc-connect server port nick user-name pass
195eca78 447 full-name channels)
0ffab1eb 448 (quit (message "Quit connecting to %s" server)))
195eca78
SM
449 (with-current-buffer (process-buffer connected)
450 (setq connected-servers
451 (cons (process-contact (get-buffer-process
452 (current-buffer)) :host)
453 connected-servers))))))))
454 (when connected-servers
455 (message "Already connected to %s"
a0a5c583
GM
456 (if (cdr connected-servers)
457 (concat (mapconcat 'identity (butlast connected-servers) ", ")
458 ", and "
459 (car (last connected-servers)))
460 (car connected-servers)))))))
195eca78 461
bd43c990
RS
462;;;###autoload
463(defalias 'irc 'rcirc)
464
465\f
466(defvar rcirc-process-output nil)
bd43c990
RS
467(defvar rcirc-topic nil)
468(defvar rcirc-keepalive-timer nil)
ad8121fe 469(defvar rcirc-last-server-message-time nil)
8216fbaf
EZ
470(defvar rcirc-server nil) ; server provided by server
471(defvar rcirc-server-name nil) ; server name given by 001 response
472(defvar rcirc-timeout-timer nil)
473(defvar rcirc-user-disconnect nil)
474(defvar rcirc-connecting nil)
475(defvar rcirc-process nil)
8d214091
RF
476
477;;;###autoload
d26781af
RY
478(defun rcirc-connect (server &optional port nick user-name pass
479 full-name startup-channels)
bd43c990
RS
480 (save-excursion
481 (message "Connecting to %s..." server)
482 (let* ((inhibit-eol-conversion)
2fbed782
EZ
483 (port-number (if port
484 (if (stringp port)
485 (string-to-number port)
486 port)
a2524d26 487 rcirc-default-port))
a2524d26
EZ
488 (nick (or nick rcirc-default-nick))
489 (user-name (or user-name rcirc-default-user-name))
0ffab1eb 490 (full-name (or full-name rcirc-default-full-name))
a2524d26 491 (startup-channels startup-channels)
8216fbaf 492 (process (make-network-process :name server :host server :service port-number)))
bd43c990
RS
493 ;; set up process
494 (set-process-coding-system process 'raw-text 'raw-text)
adf794e4 495 (switch-to-buffer (rcirc-generate-new-buffer-name process nil))
bd43c990 496 (set-process-buffer process (current-buffer))
bd43c990 497 (rcirc-mode process nil)
a2524d26
EZ
498 (set-process-sentinel process 'rcirc-sentinel)
499 (set-process-filter process 'rcirc-filter)
8216fbaf
EZ
500 (make-local-variable 'rcirc-process)
501 (setq rcirc-process process)
a2524d26
EZ
502 (make-local-variable 'rcirc-server)
503 (setq rcirc-server server)
8216fbaf
EZ
504 (make-local-variable 'rcirc-server-name)
505 (setq rcirc-server-name server) ; update when we get 001 response
adf794e4
EZ
506 (make-local-variable 'rcirc-buffer-alist)
507 (setq rcirc-buffer-alist nil)
bd43c990
RS
508 (make-local-variable 'rcirc-nick-table)
509 (setq rcirc-nick-table (make-hash-table :test 'equal))
bd43c990
RS
510 (make-local-variable 'rcirc-nick)
511 (setq rcirc-nick nick)
512 (make-local-variable 'rcirc-process-output)
513 (setq rcirc-process-output nil)
bd43c990
RS
514 (make-local-variable 'rcirc-startup-channels)
515 (setq rcirc-startup-channels startup-channels)
ad8121fe
EZ
516 (make-local-variable 'rcirc-last-server-message-time)
517 (setq rcirc-last-server-message-time (current-time))
8216fbaf
EZ
518 (make-local-variable 'rcirc-timeout-timer)
519 (setq rcirc-timeout-timer nil)
520 (make-local-variable 'rcirc-user-disconnect)
521 (setq rcirc-user-disconnect nil)
522 (make-local-variable 'rcirc-connecting)
523 (setq rcirc-connecting t)
bd43c990 524
195eca78
SM
525 (add-hook 'auto-save-hook 'rcirc-log-write)
526
bd43c990 527 ;; identify
d26781af
RY
528 (when pass
529 (rcirc-send-string process (concat "PASS " pass)))
bd43c990
RS
530 (rcirc-send-string process (concat "NICK " nick))
531 (rcirc-send-string process (concat "USER " user-name
d26781af 532 " 0 * :" full-name))
bd43c990
RS
533
534 ;; setup ping timer if necessary
8216fbaf
EZ
535 (unless rcirc-keepalive-timer
536 (setq rcirc-keepalive-timer
537 (run-at-time 0 (/ rcirc-timeout-seconds 2) 'rcirc-keepalive)))
bd43c990
RS
538
539 (message "Connecting to %s...done" server)
540
541 ;; return process object
542 process)))
543
adf794e4
EZ
544(defmacro with-rcirc-process-buffer (process &rest body)
545 (declare (indent 1) (debug t))
546 `(with-current-buffer (process-buffer ,process)
547 ,@body))
548
a2524d26
EZ
549(defmacro with-rcirc-server-buffer (&rest body)
550 (declare (indent 0) (debug t))
551 `(with-current-buffer rcirc-server-buffer
552 ,@body))
553
bd43c990 554(defun rcirc-keepalive ()
ad8121fe
EZ
555 "Send keep alive pings to active rcirc processes.
556Kill processes that have not received a server message since the
557last ping."
bd43c990
RS
558 (if (rcirc-process-list)
559 (mapc (lambda (process)
8216fbaf
EZ
560 (with-rcirc-process-buffer process
561 (when (not rcirc-connecting)
0ffab1eb 562 (rcirc-send-string process
195eca78
SM
563 (format "PRIVMSG %s :\C-aKEEPALIVE %f\C-a"
564 rcirc-nick
a68366f4
GM
565 (if (featurep 'xemacs)
566 (time-to-seconds
567 (current-time))
568 (float-time)))))))
bd43c990 569 (rcirc-process-list))
8216fbaf 570 ;; no processes, clean up timer
bd43c990
RS
571 (cancel-timer rcirc-keepalive-timer)
572 (setq rcirc-keepalive-timer nil)))
573
195eca78
SM
574(defun rcirc-handler-ctcp-KEEPALIVE (process target sender message)
575 (with-rcirc-process-buffer process
a68366f4
GM
576 (setq header-line-format (format "%f" (- (if (featurep 'xemacs)
577 (time-to-seconds
578 (current-time))
579 (float-time))
195eca78
SM
580 (string-to-number message))))))
581
adf794e4
EZ
582(defvar rcirc-debug-buffer " *rcirc debug*")
583(defvar rcirc-debug-flag nil
584 "If non-nil, write information to `rcirc-debug-buffer'.")
585(defun rcirc-debug (process text)
bd43c990 586 "Add an entry to the debug log including PROCESS and TEXT.
2e398771 587Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag'
adf794e4
EZ
588is non-nil."
589 (when rcirc-debug-flag
9a529312 590 (with-current-buffer (get-buffer-create rcirc-debug-buffer)
195eca78
SM
591 (goto-char (point-max))
592 (insert (concat
593 "["
594 (format-time-string "%Y-%m-%dT%T ") (process-name process)
595 "] "
596 text)))))
adf794e4 597
bd43c990
RS
598(defvar rcirc-sentinel-hooks nil
599 "Hook functions called when the process sentinel is called.
600Functions are called with PROCESS and SENTINEL arguments.")
601
602(defun rcirc-sentinel (process sentinel)
603 "Called when PROCESS receives SENTINEL."
604 (let ((sentinel (replace-regexp-in-string "\n" "" sentinel)))
adf794e4
EZ
605 (rcirc-debug process (format "SENTINEL: %S %S\n" process sentinel))
606 (with-rcirc-process-buffer process
607 (dolist (buffer (cons nil (mapcar 'cdr rcirc-buffer-alist)))
adf794e4 608 (with-current-buffer (or buffer (current-buffer))
db58efbf
EZ
609 (rcirc-print process "rcirc.el" "ERROR" rcirc-target
610 (format "%s: %s (%S)"
611 (process-name process)
612 sentinel
8216fbaf 613 (process-status process)) (not rcirc-target))
195eca78 614 (rcirc-disconnect-buffer)))
8216fbaf 615 (run-hook-with-args 'rcirc-sentinel-hooks process sentinel))))
bd43c990 616
195eca78
SM
617(defun rcirc-disconnect-buffer (&optional buffer)
618 (with-current-buffer (or buffer (current-buffer))
619 ;; set rcirc-target to nil for each channel so cleanup
620 ;; doesnt happen when we reconnect
621 (setq rcirc-target nil)
0ffab1eb 622 (setq mode-line-process ":disconnected")))
195eca78 623
bd43c990
RS
624(defun rcirc-process-list ()
625 "Return a list of rcirc processes."
626 (let (ps)
627 (mapc (lambda (p)
18aa2c90 628 (when (buffer-live-p (process-buffer p))
adf794e4 629 (with-rcirc-process-buffer p
bd43c990
RS
630 (when (eq major-mode 'rcirc-mode)
631 (setq ps (cons p ps))))))
632 (process-list))
633 ps))
634
635(defvar rcirc-receive-message-hooks nil
2e398771
JB
636 "Hook functions run when a message is received from server.
637Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.")
bd43c990
RS
638(defun rcirc-filter (process output)
639 "Called when PROCESS receives OUTPUT."
adf794e4 640 (rcirc-debug process output)
8216fbaf 641 (rcirc-reschedule-timeout process)
adf794e4 642 (with-rcirc-process-buffer process
ad8121fe 643 (setq rcirc-last-server-message-time (current-time))
bd43c990
RS
644 (setq rcirc-process-output (concat rcirc-process-output output))
645 (when (= (aref rcirc-process-output
646 (1- (length rcirc-process-output))) ?\n)
647 (mapc (lambda (line)
648 (rcirc-process-server-response process line))
adf794e4 649 (split-string rcirc-process-output "[\n\r]" t))
bd43c990
RS
650 (setq rcirc-process-output nil))))
651
8216fbaf
EZ
652(defun rcirc-reschedule-timeout (process)
653 (with-rcirc-process-buffer process
654 (when (not rcirc-connecting)
655 (with-rcirc-process-buffer process
656 (when rcirc-timeout-timer (cancel-timer rcirc-timeout-timer))
657 (setq rcirc-timeout-timer (run-at-time rcirc-timeout-seconds nil
658 'rcirc-delete-process
659 process))))))
660
661(defun rcirc-delete-process (process)
8216fbaf
EZ
662 (delete-process process))
663
adf794e4 664(defvar rcirc-trap-errors-flag t)
bd43c990 665(defun rcirc-process-server-response (process text)
adf794e4 666 (if rcirc-trap-errors-flag
bd43c990
RS
667 (condition-case err
668 (rcirc-process-server-response-1 process text)
669 (error
670 (rcirc-print process "RCIRC" "ERROR" nil
adf794e4 671 (format "\"%s\" %s" text err) t)))
bd43c990
RS
672 (rcirc-process-server-response-1 process text)))
673
674(defun rcirc-process-server-response-1 (process text)
675 (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text)
db58efbf
EZ
676 (let* ((user (match-string 2 text))
677 (sender (rcirc-user-nick user))
bd43c990
RS
678 (cmd (match-string 3 text))
679 (args (match-string 4 text))
680 (handler (intern-soft (concat "rcirc-handler-" cmd))))
681 (string-match "^\\([^:]*\\):?\\(.+\\)?$" args)
682 (let* ((args1 (match-string 1 args))
683 (args2 (match-string 2 args))
adf794e4
EZ
684 (args (delq nil (append (split-string args1 " " t)
685 (list args2)))))
bd43c990
RS
686 (if (not (fboundp handler))
687 (rcirc-handler-generic process cmd sender args text)
688 (funcall handler process sender args text))
689 (run-hook-with-args 'rcirc-receive-message-hooks
690 process cmd sender args text)))
691 (message "UNHANDLED: %s" text)))
692
f8db61b2
EZ
693(defvar rcirc-responses-no-activity '("305" "306")
694 "Responses that don't trigger activity in the mode-line indicator.")
695
696(defun rcirc-handler-generic (process response sender args text)
bd43c990 697 "Generic server response handler."
f8db61b2
EZ
698 (rcirc-print process sender response nil
699 (mapconcat 'identity (cdr args) " ")
700 (not (member response rcirc-responses-no-activity))))
bd43c990
RS
701
702(defun rcirc-send-string (process string)
703 "Send PROCESS a STRING plus a newline."
a2524d26 704 (let ((string (concat (encode-coding-string string rcirc-encode-coding-system)
bd43c990 705 "\n")))
a2524d26 706 (unless (eq (process-status process) 'open)
53f831f3 707 (error "Network connection to %s is not open"
a2524d26 708 (process-name process)))
adf794e4 709 (rcirc-debug process string)
bd43c990
RS
710 (process-send-string process string)))
711
a2524d26
EZ
712(defun rcirc-buffer-process (&optional buffer)
713 "Return the process associated with channel BUFFER.
714With no argument or nil as argument, use the current buffer."
8216fbaf
EZ
715 (or (get-buffer-process (if buffer
716 (with-current-buffer buffer
717 rcirc-server-buffer)
718 rcirc-server-buffer))
719 rcirc-process))
a2524d26
EZ
720
721(defun rcirc-server-name (process)
722 "Return PROCESS server name, given by the 001 response."
adf794e4 723 (with-rcirc-process-buffer process
195eca78
SM
724 (or rcirc-server-name
725 (warn "server name for process %S unknown" process))))
bd43c990
RS
726
727(defun rcirc-nick (process)
728 "Return PROCESS nick."
92c4adc1 729 (with-rcirc-process-buffer process
a2524d26
EZ
730 (or rcirc-nick rcirc-default-nick)))
731
732(defun rcirc-buffer-nick (&optional buffer)
733 "Return the nick associated with BUFFER.
734With no argument or nil as argument, use the current buffer."
735 (with-current-buffer (or buffer (current-buffer))
736 (with-current-buffer rcirc-server-buffer
737 (or rcirc-nick rcirc-default-nick))))
bd43c990 738
02f47e86 739(defvar rcirc-max-message-length 420
bd43c990
RS
740 "Messages longer than this value will be split.")
741
195eca78 742(defun rcirc-send-message (process target message &optional noticep silent)
bd43c990 743 "Send TARGET associated with PROCESS a privmsg with text MESSAGE.
195eca78
SM
744If NOTICEP is non-nil, send a notice instead of privmsg.
745If SILENT is non-nil, do not print the message in any irc buffer."
bd43c990
RS
746 ;; max message length is 512 including CRLF
747 (let* ((response (if noticep "NOTICE" "PRIVMSG"))
748 (oversize (> (length message) rcirc-max-message-length))
749 (text (if oversize
750 (substring message 0 rcirc-max-message-length)
751 message))
752 (text (if (string= text "")
753 " "
754 text))
755 (more (if oversize
756 (substring message rcirc-max-message-length))))
db58efbf 757 (rcirc-get-buffer-create process target)
bd43c990 758 (rcirc-send-string process (concat response " " target " :" text))
195eca78
SM
759 (unless silent
760 (rcirc-print process (rcirc-nick process) response target text))
db58efbf 761 (when more (rcirc-send-message process target more noticep))))
bd43c990
RS
762
763(defvar rcirc-input-ring nil)
764(defvar rcirc-input-ring-index 0)
765(defun rcirc-prev-input-string (arg)
766 (ring-ref rcirc-input-ring (+ rcirc-input-ring-index arg)))
767
768(defun rcirc-insert-prev-input (arg)
769 (interactive "p")
770 (when (<= rcirc-prompt-end-marker (point))
771 (delete-region rcirc-prompt-end-marker (point-max))
772 (insert (rcirc-prev-input-string 0))
773 (setq rcirc-input-ring-index (1+ rcirc-input-ring-index))))
774
775(defun rcirc-insert-next-input (arg)
776 (interactive "p")
777 (when (<= rcirc-prompt-end-marker (point))
778 (delete-region rcirc-prompt-end-marker (point-max))
779 (setq rcirc-input-ring-index (1- rcirc-input-ring-index))
780 (insert (rcirc-prev-input-string -1))))
781
782(defvar rcirc-nick-completions nil)
783(defvar rcirc-nick-completion-start-offset nil)
7faa3f8c 784
bd43c990
RS
785(defun rcirc-complete-nick ()
786 "Cycle through nick completions from list of nicks in channel."
787 (interactive)
7faa3f8c 788 (if (eq last-command this-command)
bd43c990
RS
789 (setq rcirc-nick-completions
790 (append (cdr rcirc-nick-completions)
791 (list (car rcirc-nick-completions))))
792 (setq rcirc-nick-completion-start-offset
793 (- (save-excursion
794 (if (re-search-backward " " rcirc-prompt-end-marker t)
795 (1+ (point))
796 rcirc-prompt-end-marker))
797 rcirc-prompt-end-marker))
798 (setq rcirc-nick-completions
799 (let ((completion-ignore-case t))
adf794e4
EZ
800 (all-completions
801 (buffer-substring
bd43c990
RS
802 (+ rcirc-prompt-end-marker
803 rcirc-nick-completion-start-offset)
804 (point))
805 (mapcar (lambda (x) (cons x nil))
a2524d26
EZ
806 (rcirc-channel-nicks (rcirc-buffer-process)
807 rcirc-target))))))
bd43c990
RS
808 (let ((completion (car rcirc-nick-completions)))
809 (when completion
adf794e4 810 (delete-region (+ rcirc-prompt-end-marker
7faa3f8c
MB
811 rcirc-nick-completion-start-offset)
812 (point))
bd43c990 813 (insert (concat completion
adf794e4 814 (if (= (+ rcirc-prompt-end-marker
bd43c990
RS
815 rcirc-nick-completion-start-offset)
816 rcirc-prompt-end-marker)
817 ": "))))))
818
a2524d26
EZ
819(defun set-rcirc-decode-coding-system (coding-system)
820 "Set the decode coding system used in this channel."
821 (interactive "zCoding system for incoming messages: ")
822 (setq rcirc-decode-coding-system coding-system))
823
824(defun set-rcirc-encode-coding-system (coding-system)
825 "Set the encode coding system used in this channel."
826 (interactive "zCoding system for outgoing messages: ")
827 (setq rcirc-encode-coding-system coding-system))
bd43c990
RS
828
829(defvar rcirc-mode-map (make-sparse-keymap)
830 "Keymap for rcirc mode.")
831
832(define-key rcirc-mode-map (kbd "RET") 'rcirc-send-input)
833(define-key rcirc-mode-map (kbd "M-p") 'rcirc-insert-prev-input)
834(define-key rcirc-mode-map (kbd "M-n") 'rcirc-insert-next-input)
835(define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete-nick)
836(define-key rcirc-mode-map (kbd "C-c C-b") 'rcirc-browse-url)
837(define-key rcirc-mode-map (kbd "C-c C-c") 'rcirc-edit-multiline)
838(define-key rcirc-mode-map (kbd "C-c C-j") 'rcirc-cmd-join)
839(define-key rcirc-mode-map (kbd "C-c C-k") 'rcirc-cmd-kick)
a2524d26 840(define-key rcirc-mode-map (kbd "C-c C-l") 'rcirc-toggle-low-priority)
bd43c990
RS
841(define-key rcirc-mode-map (kbd "C-c C-d") 'rcirc-cmd-mode)
842(define-key rcirc-mode-map (kbd "C-c C-m") 'rcirc-cmd-msg)
843(define-key rcirc-mode-map (kbd "C-c C-r") 'rcirc-cmd-nick) ; rename
195eca78 844(define-key rcirc-mode-map (kbd "C-c C-o") 'rcirc-omit-mode)
a0a5c583 845(define-key rcirc-mode-map (kbd "M-o") 'rcirc-omit-mode)
bd43c990
RS
846(define-key rcirc-mode-map (kbd "C-c C-p") 'rcirc-cmd-part)
847(define-key rcirc-mode-map (kbd "C-c C-q") 'rcirc-cmd-query)
848(define-key rcirc-mode-map (kbd "C-c C-t") 'rcirc-cmd-topic)
849(define-key rcirc-mode-map (kbd "C-c C-n") 'rcirc-cmd-names)
850(define-key rcirc-mode-map (kbd "C-c C-w") 'rcirc-cmd-whois)
851(define-key rcirc-mode-map (kbd "C-c C-x") 'rcirc-cmd-quit)
852(define-key rcirc-mode-map (kbd "C-c TAB") ; C-i
adf794e4 853 'rcirc-toggle-ignore-buffer-activity)
bd43c990
RS
854(define-key rcirc-mode-map (kbd "C-c C-s") 'rcirc-switch-to-server-buffer)
855(define-key rcirc-mode-map (kbd "C-c C-a") 'rcirc-jump-to-first-unread-line)
856
adf794e4 857(defvar rcirc-browse-url-map (make-sparse-keymap)
2e398771 858 "Keymap used for browsing URLs in `rcirc-mode'.")
adf794e4
EZ
859
860(define-key rcirc-browse-url-map (kbd "RET") 'rcirc-browse-url-at-point)
861(define-key rcirc-browse-url-map (kbd "<mouse-2>") 'rcirc-browse-url-at-mouse)
f2ec0e5e 862(define-key rcirc-browse-url-map [follow-link] 'mouse-face)
adf794e4
EZ
863
864(defvar rcirc-short-buffer-name nil
865 "Generated abbreviation to use to indicate buffer activity.")
866
bd43c990
RS
867(defvar rcirc-mode-hook nil
868 "Hook run when setting up rcirc buffer.")
869
a2524d26
EZ
870(defvar rcirc-last-post-time nil)
871
195eca78
SM
872(defvar rcirc-log-alist nil
873 "Alist of lines to log to disk when `rcirc-log-flag' is non-nil.
874Each element looks like (FILENAME . TEXT).")
875
a0a5c583
GM
876(defvar rcirc-current-line 0
877 "The current number of responses printed in this channel.
878This number is independent of the number of lines in the buffer.")
879
bd43c990 880(defun rcirc-mode (process target)
2e398771 881 "Major mode for IRC channel buffers.
bd43c990
RS
882
883\\{rcirc-mode-map}"
884 (kill-all-local-variables)
885 (use-local-map rcirc-mode-map)
886 (setq mode-name "rcirc")
887 (setq major-mode 'rcirc-mode)
195eca78 888 (setq mode-line-process nil)
bd43c990
RS
889
890 (make-local-variable 'rcirc-input-ring)
891 (setq rcirc-input-ring (make-ring rcirc-input-ring-size))
a2524d26
EZ
892 (make-local-variable 'rcirc-server-buffer)
893 (setq rcirc-server-buffer (process-buffer process))
bd43c990
RS
894 (make-local-variable 'rcirc-target)
895 (setq rcirc-target target)
ad8121fe
EZ
896 (make-local-variable 'rcirc-topic)
897 (setq rcirc-topic nil)
a2524d26
EZ
898 (make-local-variable 'rcirc-last-post-time)
899 (setq rcirc-last-post-time (current-time))
195eca78
SM
900 (make-local-variable 'fill-paragraph-function)
901 (setq fill-paragraph-function 'rcirc-fill-paragraph)
a0a5c583
GM
902 (make-local-variable 'rcirc-recent-quit-alist)
903 (setq rcirc-recent-quit-alist nil)
904 (make-local-variable 'rcirc-current-line)
905 (setq rcirc-current-line 0)
adf794e4
EZ
906
907 (make-local-variable 'rcirc-short-buffer-name)
908 (setq rcirc-short-buffer-name nil)
bd43c990 909 (make-local-variable 'rcirc-urls)
bd43c990 910 (setq use-hard-newlines t)
bd43c990 911
a0a5c583
GM
912 ;; setup for omitting responses
913 (setq buffer-invisibility-spec '())
914 (setq buffer-display-table (make-display-table))
915 (set-display-table-slot buffer-display-table 4
916 (let ((glyph (make-glyph-code
917 ?. 'font-lock-keyword-face)))
918 (make-vector 3 glyph)))
919
a2524d26
EZ
920 (make-local-variable 'rcirc-decode-coding-system)
921 (make-local-variable 'rcirc-encode-coding-system)
922 (dolist (i rcirc-coding-system-alist)
923 (let ((chan (if (consp (car i)) (caar i) (car i)))
924 (serv (if (consp (car i)) (cdar i) "")))
925 (when (and (string-match chan (or target ""))
926 (string-match serv (rcirc-server-name process)))
aac5d1fd
EZ
927 (setq rcirc-decode-coding-system (if (consp (cdr i)) (cadr i) (cdr i))
928 rcirc-encode-coding-system (if (consp (cdr i)) (cddr i) (cdr i))))))
a2524d26 929
bd43c990
RS
930 ;; setup the prompt and markers
931 (make-local-variable 'rcirc-prompt-start-marker)
932 (setq rcirc-prompt-start-marker (make-marker))
933 (set-marker rcirc-prompt-start-marker (point-max))
934 (make-local-variable 'rcirc-prompt-end-marker)
935 (setq rcirc-prompt-end-marker (make-marker))
936 (set-marker rcirc-prompt-end-marker (point-max))
937 (rcirc-update-prompt)
938 (goto-char rcirc-prompt-end-marker)
939 (make-local-variable 'overlay-arrow-position)
940 (setq overlay-arrow-position (make-marker))
941 (set-marker overlay-arrow-position nil)
942
a2524d26
EZ
943 ;; if the user changes the major mode or kills the buffer, there is
944 ;; cleanup work to do
f8db61b2
EZ
945 (add-hook 'change-major-mode-hook 'rcirc-change-major-mode-hook nil t)
946 (add-hook 'kill-buffer-hook 'rcirc-kill-buffer-hook nil t)
a2524d26 947
adf794e4
EZ
948 ;; add to buffer list, and update buffer abbrevs
949 (when target ; skip server buffer
950 (let ((buffer (current-buffer)))
951 (with-rcirc-process-buffer process
952 (setq rcirc-buffer-alist (cons (cons target buffer)
953 rcirc-buffer-alist))))
954 (rcirc-update-short-buffer-names))
955
bd43c990
RS
956 (run-hooks 'rcirc-mode-hook))
957
adf794e4
EZ
958(defun rcirc-update-prompt (&optional all)
959 "Reset the prompt string in the current buffer.
c18a54de 960
adf794e4
EZ
961If ALL is non-nil, update prompts in all IRC buffers."
962 (if all
963 (mapc (lambda (process)
964 (mapc (lambda (buffer)
965 (with-current-buffer buffer
966 (rcirc-update-prompt)))
967 (with-rcirc-process-buffer process
968 (mapcar 'cdr rcirc-buffer-alist))))
969 (rcirc-process-list))
970 (let ((inhibit-read-only t)
971 (prompt (or rcirc-prompt "")))
972 (mapc (lambda (rep)
973 (setq prompt
a2524d26
EZ
974 (replace-regexp-in-string (car rep) (cdr rep) prompt)))
975 (list (cons "%n" (rcirc-buffer-nick))
8216fbaf 976 (cons "%s" (with-rcirc-server-buffer rcirc-server-name))
adf794e4
EZ
977 (cons "%t" (or rcirc-target ""))))
978 (save-excursion
979 (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker)
980 (goto-char rcirc-prompt-start-marker)
981 (let ((start (point)))
982 (insert-before-markers prompt)
983 (set-marker rcirc-prompt-start-marker start)
984 (when (not (zerop (- rcirc-prompt-end-marker
985 rcirc-prompt-start-marker)))
986 (add-text-properties rcirc-prompt-start-marker
987 rcirc-prompt-end-marker
988 (list 'face 'rcirc-prompt
989 'read-only t 'field t
990 'front-sticky t 'rear-nonsticky t))))))))
991
992(defun rcirc-set-changed (option value)
993 "Set OPTION to VALUE and do updates after a customization change."
994 (set-default option value)
995 (cond ((eq option 'rcirc-prompt)
996 (rcirc-update-prompt 'all))
997 (t
998 (error "Bad option %s" option))))
bd43c990
RS
999
1000(defun rcirc-channel-p (target)
1001 "Return t if TARGET is a channel name."
1002 (and target
1003 (not (zerop (length target)))
1004 (or (eq (aref target 0) ?#)
1005 (eq (aref target 0) ?&))))
1006
1007(defun rcirc-kill-buffer-hook ()
1008 "Part the channel when killing an rcirc buffer."
1009 (when (eq major-mode 'rcirc-mode)
a2524d26
EZ
1010 (rcirc-clean-up-buffer "Killed buffer")))
1011
1012(defun rcirc-change-major-mode-hook ()
1013 "Part the channel when changing the major-mode."
1014 (rcirc-clean-up-buffer "Changed major mode"))
1015
1016(defun rcirc-clean-up-buffer (reason)
adf794e4
EZ
1017 (let ((buffer (current-buffer)))
1018 (rcirc-clear-activity buffer)
a2524d26
EZ
1019 (when (and (rcirc-buffer-process)
1020 (eq (process-status (rcirc-buffer-process)) 'open))
1021 (with-rcirc-server-buffer
1022 (setq rcirc-buffer-alist
1023 (rassq-delete-all buffer rcirc-buffer-alist)))
adf794e4 1024 (rcirc-update-short-buffer-names)
bd43c990 1025 (if (rcirc-channel-p rcirc-target)
a2524d26
EZ
1026 (rcirc-send-string (rcirc-buffer-process)
1027 (concat "PART " rcirc-target " :" reason))
adf794e4 1028 (when rcirc-target
a2524d26
EZ
1029 (rcirc-remove-nick-channel (rcirc-buffer-process)
1030 (rcirc-buffer-nick)
195eca78
SM
1031 rcirc-target))))
1032 (setq rcirc-target nil)))
adf794e4 1033
adf794e4
EZ
1034(defun rcirc-generate-new-buffer-name (process target)
1035 "Return a buffer name based on PROCESS and TARGET.
2e398771 1036This is used for the initial name given to IRC buffers."
195eca78
SM
1037 (substring-no-properties
1038 (if target
1039 (concat target "@" (process-name process))
1040 (concat "*" (process-name process) "*"))))
bd43c990 1041
adf794e4 1042(defun rcirc-get-buffer (process target &optional server)
bd43c990 1043 "Return the buffer associated with the PROCESS and TARGET.
adf794e4 1044
adf794e4
EZ
1045If optional argument SERVER is non-nil, return the server buffer
1046if there is no existing buffer for TARGET, otherwise return nil."
1047 (with-rcirc-process-buffer process
1048 (if (null target)
1049 (current-buffer)
1050 (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t))))
1051 (or buffer (when server (current-buffer)))))))
bd43c990
RS
1052
1053(defun rcirc-get-buffer-create (process target)
adf794e4
EZ
1054 "Return the buffer associated with the PROCESS and TARGET.
1055Create the buffer if it doesn't exist."
1056 (let ((buffer (rcirc-get-buffer process target)))
a2524d26 1057 (if (and buffer (buffer-live-p buffer))
2fbed782 1058 (with-current-buffer buffer
db58efbf 1059 (when (not rcirc-target)
2fbed782 1060 (setq rcirc-target target))
db58efbf 1061 buffer)
195eca78
SM
1062 ;; create the buffer
1063 (with-rcirc-process-buffer process
1064 (let ((new-buffer (get-buffer-create
1065 (rcirc-generate-new-buffer-name process target))))
1066 (with-current-buffer new-buffer
a0a5c583
GM
1067 (rcirc-mode process target)
1068 (rcirc-put-nick-channel process (rcirc-nick process) target
1069 rcirc-current-line))
195eca78 1070 new-buffer)))))
bd43c990
RS
1071
1072(defun rcirc-send-input ()
1073 "Send input to target associated with the current buffer."
1074 (interactive)
53f831f3
AS
1075 (if (< (point) rcirc-prompt-end-marker)
1076 ;; copy the line down to the input area
1077 (progn
1078 (forward-line 0)
1079 (let ((start (if (eq (point) (point-min))
1080 (point)
1081 (if (get-text-property (1- (point)) 'hard)
1082 (point)
1083 (previous-single-property-change (point) 'hard))))
1084 (end (next-single-property-change (1+ (point)) 'hard)))
1085 (goto-char (point-max))
1086 (insert (replace-regexp-in-string
1087 "\n\\s-+" " "
1088 (buffer-substring-no-properties start end)))))
1089 ;; process input
1090 (goto-char (point-max))
a2524d26
EZ
1091 (when (not (equal 0 (- (point) rcirc-prompt-end-marker)))
1092 ;; delete a trailing newline
d355a0b7
SM
1093 (when (bolp)
1094 (delete-char -1))
a2524d26
EZ
1095 (let ((input (buffer-substring-no-properties
1096 rcirc-prompt-end-marker (point))))
1097 (dolist (line (split-string input "\n"))
1098 (rcirc-process-input-line line))
1099 ;; add to input-ring
1100 (save-excursion
1101 (ring-insert rcirc-input-ring input)
1102 (setq rcirc-input-ring-index 0))))))
1103
195eca78
SM
1104(defun rcirc-fill-paragraph (&optional arg)
1105 (interactive "p")
1106 (when (> (point) rcirc-prompt-end-marker)
1107 (save-restriction
1108 (narrow-to-region rcirc-prompt-end-marker (point-max))
1109 (let ((fill-column rcirc-max-message-length))
1110 (fill-region (point-min) (point-max))))))
1111
a2524d26 1112(defun rcirc-process-input-line (line)
db58efbf
EZ
1113 (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line)
1114 (rcirc-process-command (match-string 1 line)
1115 (match-string 2 line)
1116 line)
1117 (rcirc-process-message line)))
1118
1119(defun rcirc-process-message (line)
1120 (if (not rcirc-target)
a2524d26 1121 (message "Not joined (no target)")
db58efbf 1122 (delete-region rcirc-prompt-end-marker (point))
a2524d26
EZ
1123 (rcirc-send-message (rcirc-buffer-process) rcirc-target line)
1124 (setq rcirc-last-post-time (current-time))))
db58efbf
EZ
1125
1126(defun rcirc-process-command (command args line)
1127 (if (eq (aref command 0) ?/)
1128 ;; "//text" will send "/text" as a message
1129 (rcirc-process-message (substring line 1))
a2524d26
EZ
1130 (let ((fun (intern-soft (concat "rcirc-cmd-" command)))
1131 (process (rcirc-buffer-process)))
db58efbf
EZ
1132 (newline)
1133 (with-current-buffer (current-buffer)
1134 (delete-region rcirc-prompt-end-marker (point))
1135 (if (string= command "me")
a2524d26 1136 (rcirc-print process (rcirc-buffer-nick)
db58efbf 1137 "ACTION" rcirc-target args)
a2524d26 1138 (rcirc-print process (rcirc-buffer-nick)
db58efbf
EZ
1139 "COMMAND" rcirc-target line))
1140 (set-marker rcirc-prompt-end-marker (point))
1141 (if (fboundp fun)
a2524d26
EZ
1142 (funcall fun args process rcirc-target)
1143 (rcirc-send-string process
f8db61b2 1144 (concat command " :" args)))))))
db58efbf 1145
bd43c990
RS
1146(defvar rcirc-parent-buffer nil)
1147(defvar rcirc-window-configuration nil)
1148(defun rcirc-edit-multiline ()
1149 "Move current edit to a dedicated buffer."
1150 (interactive)
1151 (let ((pos (1+ (- (point) rcirc-prompt-end-marker))))
1152 (goto-char (point-max))
a0a5c583
GM
1153 (let ((text (buffer-substring-no-properties rcirc-prompt-end-marker
1154 (point)))
a2524d26 1155 (parent (buffer-name)))
bd43c990
RS
1156 (delete-region rcirc-prompt-end-marker (point))
1157 (setq rcirc-window-configuration (current-window-configuration))
1158 (pop-to-buffer (concat "*multiline " parent "*"))
a2524d26
EZ
1159 (funcall rcirc-multiline-major-mode)
1160 (rcirc-multiline-minor-mode 1)
bd43c990 1161 (setq rcirc-parent-buffer parent)
bd43c990 1162 (insert text)
db58efbf
EZ
1163 (and (> pos 0) (goto-char pos))
1164 (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent))))
bd43c990 1165
a2524d26
EZ
1166(defvar rcirc-multiline-minor-mode-map (make-sparse-keymap)
1167 "Keymap for multiline mode in rcirc.")
92c4adc1 1168(define-key rcirc-multiline-minor-mode-map
a2524d26
EZ
1169 (kbd "C-c C-c") 'rcirc-multiline-minor-submit)
1170(define-key rcirc-multiline-minor-mode-map
1171 (kbd "C-x C-s") 'rcirc-multiline-minor-submit)
1172(define-key rcirc-multiline-minor-mode-map
1173 (kbd "C-c C-k") 'rcirc-multiline-minor-cancel)
1174(define-key rcirc-multiline-minor-mode-map
1175 (kbd "ESC ESC ESC") 'rcirc-multiline-minor-cancel)
1176
1177(define-minor-mode rcirc-multiline-minor-mode
1178 "Minor mode for editing multiple lines in rcirc."
1179 :init-value nil
1180 :lighter " rcirc-mline"
1181 :keymap rcirc-multiline-minor-mode-map
1182 :global nil
1183 :group 'rcirc
bd43c990 1184 (make-local-variable 'rcirc-parent-buffer)
02f47e86
MB
1185 (put 'rcirc-parent-buffer 'permanent-local t)
1186 (setq fill-column rcirc-max-message-length))
a2524d26
EZ
1187
1188(defun rcirc-multiline-minor-submit ()
bd43c990
RS
1189 "Send the text in buffer back to parent buffer."
1190 (interactive)
adf794e4 1191 (untabify (point-min) (point-max))
bd43c990
RS
1192 (let ((text (buffer-substring (point-min) (point-max)))
1193 (buffer (current-buffer))
1194 (pos (point)))
1195 (set-buffer rcirc-parent-buffer)
1196 (goto-char (point-max))
1197 (insert text)
bd43c990 1198 (kill-buffer buffer)
adf794e4
EZ
1199 (set-window-configuration rcirc-window-configuration)
1200 (goto-char (+ rcirc-prompt-end-marker (1- pos)))))
bd43c990 1201
a2524d26 1202(defun rcirc-multiline-minor-cancel ()
bd43c990
RS
1203 "Cancel the multiline edit."
1204 (interactive)
bd43c990
RS
1205 (kill-buffer (current-buffer))
1206 (set-window-configuration rcirc-window-configuration))
1207
2fbed782 1208(defun rcirc-any-buffer (process)
adf794e4 1209 "Return a buffer for PROCESS, either the one selected or the process buffer."
2fbed782
EZ
1210 (if rcirc-always-use-server-buffer-flag
1211 (process-buffer process)
1212 (let ((buffer (window-buffer (selected-window))))
1213 (if (and buffer
1214 (with-current-buffer buffer
1215 (and (eq major-mode 'rcirc-mode)
a2524d26 1216 (eq (rcirc-buffer-process) process))))
2fbed782
EZ
1217 buffer
1218 (process-buffer process)))))
bd43c990 1219
324e4da7 1220(defcustom rcirc-response-formats
195eca78
SM
1221 '(("PRIVMSG" . "<%N> %m")
1222 ("NOTICE" . "-%N- %m")
1223 ("ACTION" . "[%N %m]")
1224 ("COMMAND" . "%m")
1225 ("ERROR" . "%fw!!! %m")
1226 (t . "%fp*** %fs%n %r %m"))
324e4da7
MB
1227 "An alist of formats used for printing responses.
1228The format is looked up using the response-type as a key;
1229if no match is found, the default entry (with a key of `t') is used.
1230
1231The entry's value part should be a string, which is inserted with
1232the of the following escape sequences replaced by the described values:
1233
1234 %m The message text
2fbed782
EZ
1235 %n The sender's nick
1236 %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick')
324e4da7 1237 %r The response-type
324e4da7
MB
1238 %t The target
1239 %fw Following text uses the face `font-lock-warning-face'
1240 %fp Following text uses the face `rcirc-server-prefix'
1241 %fs Following text uses the face `rcirc-server'
1242 %f[FACE] Following text uses the face FACE
3715419e 1243 %f- Following text uses the default face
a2524d26 1244 %% A literal `%' character"
324e4da7
MB
1245 :type '(alist :key-type (choice (string :tag "Type")
1246 (const :tag "Default" t))
1247 :value-type string)
1248 :group 'rcirc)
1249
0ffab1eb 1250(defcustom rcirc-omit-responses
a0a5c583 1251 '("JOIN" "PART" "QUIT" "NICK")
195eca78
SM
1252 "Responses which will be hidden when `rcirc-omit-mode' is enabled."
1253 :type '(repeat string)
1254 :group 'rcirc)
1255
bd43c990 1256(defun rcirc-format-response-string (process sender response target text)
324e4da7
MB
1257 "Return a nicely-formatted response string, incorporating TEXT
1258\(and perhaps other arguments). The specific formatting used
1259is found by looking up RESPONSE in `rcirc-response-formats'."
195eca78
SM
1260 (with-temp-buffer
1261 (insert (or (cdr (assoc response rcirc-response-formats))
1262 (cdr (assq t rcirc-response-formats))))
1263 (goto-char (point-min))
1264 (let ((start (point-min))
1265 (sender (if (or (not sender)
1266 (string= (rcirc-server-name process) sender))
1267 ""
1268 sender))
1269 face)
1270 (while (re-search-forward "%\\(\\(f\\(.\\)\\)\\|\\(.\\)\\)" nil t)
1271 (rcirc-add-face start (match-beginning 0) face)
1272 (setq start (match-beginning 0))
1273 (replace-match
1274 (case (aref (match-string 1) 0)
1275 (?f (setq face
1276 (case (string-to-char (match-string 3))
1277 (?w 'font-lock-warning-face)
1278 (?p 'rcirc-server-prefix)
1279 (?s 'rcirc-server)
1280 (t nil)))
1281 "")
1282 (?n sender)
1283 (?N (let ((my-nick (rcirc-nick process)))
1284 (save-match-data
1285 (with-syntax-table rcirc-nick-syntax-table
1286 (rcirc-facify sender
1287 (cond ((string= sender my-nick)
1288 'rcirc-my-nick)
1289 ((and rcirc-bright-nicks
0ffab1eb 1290 (string-match
195eca78
SM
1291 (regexp-opt rcirc-bright-nicks
1292 'words)
1293 sender))
1294 'rcirc-bright-nick)
1295 ((and rcirc-dim-nicks
1296 (string-match
1297 (regexp-opt rcirc-dim-nicks
1298 'words)
1299 sender))
1300 'rcirc-dim-nick)
1301 (t
1302 'rcirc-other-nick)))))))
1303 (?m (propertize text 'rcirc-text text))
1304 (?r response)
1305 (?t (or target ""))
1306 (t (concat "UNKNOWN CODE:" (match-string 0))))
1307 t t nil 0)
1308 (rcirc-add-face (match-beginning 0) (match-end 0) face))
1309 (rcirc-add-face start (match-beginning 0) face))
1310 (buffer-substring (point-min) (point-max))))
bd43c990 1311
db58efbf
EZ
1312(defun rcirc-target-buffer (process sender response target text)
1313 "Return a buffer to print the server response."
1314 (assert (not (bufferp target)))
1315 (with-rcirc-process-buffer process
1316 (cond ((not target)
2fbed782 1317 (rcirc-any-buffer process))
db58efbf
EZ
1318 ((not (rcirc-channel-p target))
1319 ;; message from another user
195eca78
SM
1320 (if (or (string= response "PRIVMSG")
1321 (string= response "ACTION"))
db58efbf
EZ
1322 (rcirc-get-buffer-create process (if (string= sender rcirc-nick)
1323 target
1324 sender))
1325 (rcirc-get-buffer process target t)))
1326 ((or (rcirc-get-buffer process target)
2fbed782 1327 (rcirc-any-buffer process))))))
db58efbf 1328
f8db61b2
EZ
1329(defvar rcirc-activity-types nil)
1330(make-variable-buffer-local 'rcirc-activity-types)
a2524d26
EZ
1331(defvar rcirc-last-sender nil)
1332(make-variable-buffer-local 'rcirc-last-sender)
7faa3f8c 1333
195eca78
SM
1334(defcustom rcirc-log-directory "~/.emacs.d/rcirc-log"
1335 "Directory to keep IRC logfiles."
1336 :type 'directory
1337 :group 'rcirc)
1338
1339(defcustom rcirc-log-flag nil
1340 "Non-nil means log IRC activity to disk.
1341Logfiles are kept in `rcirc-log-directory'."
1342 :type 'boolean
1343 :group 'rcirc)
1344
a0a5c583
GM
1345(defcustom rcirc-omit-threshold 100
1346 "Number of lines since last activity from a nick before `rcirc-omit-responses' are omitted."
1347 :type 'integer
1348 :group 'rcirc)
1349
683b7dc6 1350(defun rcirc-last-quit-line (process nick target)
a0a5c583
GM
1351 "Return the line number where NICK left TARGET.
1352Returns nil if the information is not recorded."
683b7dc6 1353 (let ((chanbuf (rcirc-get-buffer process target)))
a0a5c583
GM
1354 (when chanbuf
1355 (cdr (assoc-string nick (with-current-buffer chanbuf
1356 rcirc-recent-quit-alist))))))
1357
683b7dc6 1358(defun rcirc-last-line (process nick target)
a0a5c583 1359 "Return the line from the last activity from NICK in TARGET."
683b7dc6 1360 (let* ((chanbuf (rcirc-get-buffer process target))
a0a5c583
GM
1361 (line (or (cdr (assoc-string target
1362 (gethash nick (with-rcirc-server-buffer
1363 rcirc-nick-table)) t))
683b7dc6 1364 (rcirc-last-quit-line process nick target))))
a0a5c583
GM
1365 (if line
1366 line
1367 ;;(message "line is nil for %s in %s" nick target)
1368 nil)))
1369
683b7dc6 1370(defun rcirc-elapsed-lines (process nick target)
a0a5c583 1371 "Return the number of lines since activity from NICK in TARGET."
683b7dc6 1372 (let ((last-activity-line (rcirc-last-line process nick target)))
a0a5c583
GM
1373 (when (and last-activity-line
1374 (> last-activity-line 0))
1375 (- rcirc-current-line last-activity-line))))
1376
729f1525
DN
1377(defvar rcirc-markup-text-functions
1378 '(rcirc-markup-attributes
1379 rcirc-markup-my-nick
1380 rcirc-markup-urls
1381 rcirc-markup-keywords
683b7dc6 1382 rcirc-markup-bright-nicks)
729f1525
DN
1383
1384 "List of functions used to manipulate text before it is printed.
1385
683b7dc6
GM
1386Each function takes two arguments, SENDER, and RESPONSE. The
1387buffer is narrowed with the text to be printed and the point is
1388at the beginning of the `rcirc-text' propertized text.")
729f1525 1389
bd43c990
RS
1390(defun rcirc-print (process sender response target text &optional activity)
1391 "Print TEXT in the buffer associated with TARGET.
1392Format based on SENDER and RESPONSE. If ACTIVITY is non-nil,
1393record activity."
a2524d26 1394 (or text (setq text ""))
0ffab1eb
TTN
1395 (unless (and (or (member sender rcirc-ignore-list)
1396 (member (with-syntax-table rcirc-nick-syntax-table
1397 (when (string-match "^\\([^/]\\w*\\)[:,]" text)
1398 (match-string 1 text)))
1399 rcirc-ignore-list))
a0a5c583
GM
1400 ;; do not ignore if we sent the message
1401 (not (string= sender (rcirc-nick process))))
db58efbf 1402 (let* ((buffer (rcirc-target-buffer process sender response target text))
2c8abe90
AS
1403 (inhibit-read-only t))
1404 (with-current-buffer buffer
1405 (let ((moving (= (point) rcirc-prompt-end-marker))
1406 (old-point (point-marker))
1407 (fill-start (marker-position rcirc-prompt-start-marker)))
1408
1409 (unless (string= sender (rcirc-nick process))
1410 ;; only decode text from other senders, not ours
a2524d26 1411 (setq text (decode-coding-string text rcirc-decode-coding-system))
2c8abe90
AS
1412 ;; mark the line with overlay arrow
1413 (unless (or (marker-position overlay-arrow-position)
195eca78
SM
1414 (get-buffer-window (current-buffer))
1415 (member response rcirc-omit-responses))
2c8abe90
AS
1416 (set-marker overlay-arrow-position
1417 (marker-position rcirc-prompt-start-marker))))
1418
1419 ;; temporarily set the marker insertion-type because
1420 ;; insert-before-markers results in hidden text in new buffers
1421 (goto-char rcirc-prompt-start-marker)
1422 (set-marker-insertion-type rcirc-prompt-start-marker t)
1423 (set-marker-insertion-type rcirc-prompt-end-marker t)
324e4da7 1424
195eca78 1425 (let ((start (point)))
0ffab1eb 1426 (insert (rcirc-format-response-string process sender response nil
195eca78
SM
1427 text)
1428 (propertize "\n" 'hard t))
1429
1430 ;; squeeze spaces out of text before rcirc-text
0ffab1eb 1431 (fill-region fill-start
195eca78
SM
1432 (1- (or (next-single-property-change fill-start
1433 'rcirc-text)
1434 rcirc-prompt-end-marker)))
1435
1436 ;; run markup functions
1437 (save-excursion
1438 (save-restriction
1439 (narrow-to-region start rcirc-prompt-start-marker)
1440 (goto-char (or (next-single-property-change start 'rcirc-text)
1441 (point)))
1442 (when (rcirc-buffer-process)
1443 (save-excursion (rcirc-markup-timestamp sender response))
1444 (dolist (fn rcirc-markup-text-functions)
1445 (save-excursion (funcall fn sender response)))
a0a5c583
GM
1446 (when rcirc-fill-flag
1447 (save-excursion (rcirc-markup-fill sender response))))
195eca78
SM
1448
1449 (when rcirc-read-only-flag
1450 (add-text-properties (point-min) (point-max)
1451 '(read-only t front-sticky t))))
1452 ;; make text omittable
683b7dc6
GM
1453 (let ((last-activity-lines (rcirc-elapsed-lines process sender target)))
1454 (if (and (not (string= (rcirc-nick process) sender))
1455 (member response rcirc-omit-responses)
1456 (or (not last-activity-lines)
1457 (< rcirc-omit-threshold last-activity-lines)))
1458 (put-text-property (1- start) (1- rcirc-prompt-start-marker)
1459 'invisible 'rcirc-omit)
1460 ;; otherwise increment the line count
1461 (setq rcirc-current-line (1+ rcirc-current-line))))))
195eca78
SM
1462
1463 (set-marker-insertion-type rcirc-prompt-start-marker nil)
1464 (set-marker-insertion-type rcirc-prompt-end-marker nil)
2c8abe90
AS
1465
1466 ;; truncate buffer if it is very long
1467 (save-excursion
1468 (when (and rcirc-buffer-maximum-lines
1469 (> rcirc-buffer-maximum-lines 0)
1470 (= (forward-line (- rcirc-buffer-maximum-lines)) 0))
1471 (delete-region (point-min) (point))))
1472
1473 ;; set the window point for buffers show in windows
1474 (walk-windows (lambda (w)
d40ac716
CY
1475 (when (and (not (eq (selected-window) w))
1476 (eq (current-buffer)
1477 (window-buffer w))
1478 (>= (window-point w)
1479 rcirc-prompt-end-marker))
195eca78 1480 (set-window-point w (point-max))))
2c8abe90
AS
1481 nil t)
1482
1483 ;; restore the point
1484 (goto-char (if moving rcirc-prompt-end-marker old-point))
1485
195eca78 1486 ;; keep window on bottom line if it was already there
d40ac716
CY
1487 (when rcirc-scroll-show-maximum-output
1488 (walk-windows (lambda (w)
1489 (when (eq (window-buffer w) (current-buffer))
1490 (with-current-buffer (window-buffer w)
1491 (when (eq major-mode 'rcirc-mode)
1492 (with-selected-window w
195eca78
SM
1493 (when (<= (- (window-height)
1494 (count-screen-lines (window-point)
1495 (window-start))
d40ac716
CY
1496 1)
1497 0)
1498 (recenter -1)))))))
195eca78 1499 nil t))
d40ac716 1500
2c8abe90
AS
1501 ;; flush undo (can we do something smarter here?)
1502 (buffer-disable-undo)
1503 (buffer-enable-undo))
1504
1505 ;; record modeline activity
f8db61b2
EZ
1506 (when (and activity
1507 (not rcirc-ignore-buffer-activity-flag)
1508 (not (and rcirc-dim-nicks sender
195eca78
SM
1509 (string-match (regexp-opt rcirc-dim-nicks) sender)
1510 (rcirc-channel-p target))))
f8db61b2
EZ
1511 (rcirc-record-activity (current-buffer)
1512 (when (not (rcirc-channel-p rcirc-target))
1513 'nick)))
2c8abe90 1514
195eca78
SM
1515 (when rcirc-log-flag
1516 (rcirc-log process sender response target text))
1517
2c8abe90
AS
1518 (sit-for 0) ; displayed text before hook
1519 (run-hook-with-args 'rcirc-print-hooks
1520 process sender response target text)))))
bd43c990 1521
aacde24f
MB
1522(defcustom rcirc-log-filename-function 'rcirc-generate-new-buffer-name
1523 "A function to generate the filename used by rcirc's logging facility.
1524
1525It is called with two arguments, PROCESS and TARGET (see
1526`rcirc-generate-new-buffer-name' for their meaning), and should
1527return the filename, or nil if no logging is desired for this
1528session.
1529
1530If the returned filename is absolute (`file-name-absolute-p'
1531returns true), then it is used as-is, otherwise the resulting
1532file is put into `rcirc-log-directory'."
1533 :group 'rcirc
1534 :type 'function)
1535
195eca78
SM
1536(defun rcirc-log (process sender response target text)
1537 "Record line in `rcirc-log', to be later written to disk."
aacde24f
MB
1538 (let ((filename (funcall rcirc-log-filename-function process target)))
1539 (unless (null filename)
1540 (let ((cell (assoc-string filename rcirc-log-alist))
1541 (line (concat (format-time-string rcirc-time-format)
1542 (substring-no-properties
1543 (rcirc-format-response-string process sender
1544 response target text))
1545 "\n")))
1546 (if cell
1547 (setcdr cell (concat (cdr cell) line))
1548 (setq rcirc-log-alist
1549 (cons (cons filename line) rcirc-log-alist)))))))
195eca78
SM
1550
1551(defun rcirc-log-write ()
1552 "Flush `rcirc-log-alist' data to disk.
1553
aacde24f
MB
1554Log data is written to `rcirc-log-directory', except for
1555log-files with absolute names (see `rcirc-log-filename-function')."
195eca78 1556 (dolist (cell rcirc-log-alist)
aacde24f
MB
1557 (let ((filename (expand-file-name (car cell) rcirc-log-directory))
1558 (coding-system-for-write 'utf-8))
1559 (make-directory (file-name-directory filename) t)
1560 (with-temp-buffer
1561 (insert (cdr cell))
1562 (write-region (point-min) (point-max) filename t 'quiet))))
195eca78 1563 (setq rcirc-log-alist nil))
bd43c990 1564
d7a0fd6f
GM
1565(defun rcirc-view-log-file ()
1566 "View logfile corresponding to the current buffer."
1567 (interactive)
1568 (find-file-other-window
1569 (expand-file-name (funcall rcirc-log-filename-function
1570 (rcirc-buffer-process) rcirc-target)
1571 rcirc-log-directory)))
1572
bd43c990
RS
1573(defun rcirc-join-channels (process channels)
1574 "Join CHANNELS."
1575 (save-window-excursion
db58efbf
EZ
1576 (dolist (channel channels)
1577 (with-rcirc-process-buffer process
1578 (rcirc-cmd-join channel process)))))
bd43c990
RS
1579\f
1580;;; nick management
8216fbaf 1581(defvar rcirc-nick-prefix-chars "~&@%+")
bd43c990
RS
1582(defun rcirc-user-nick (user)
1583 "Return the nick from USER. Remove any non-nick junk."
db58efbf 1584 (save-match-data
8216fbaf
EZ
1585 (if (string-match (concat "^[" rcirc-nick-prefix-chars
1586 "]?\\([^! ]+\\)!?") (or user ""))
db58efbf
EZ
1587 (match-string 1 user)
1588 user)))
bd43c990 1589
bd43c990
RS
1590(defun rcirc-nick-channels (process nick)
1591 "Return list of channels for NICK."
db58efbf
EZ
1592 (with-rcirc-process-buffer process
1593 (mapcar (lambda (x) (car x))
1594 (gethash nick rcirc-nick-table))))
bd43c990 1595
a0a5c583
GM
1596(defun rcirc-put-nick-channel (process nick channel &optional line)
1597 "Add CHANNEL to list associated with NICK.
1598Update the associated linestamp if LINE is non-nil.
1599
1600If the record doesn't exist, and LINE is nil, set the linestamp
1601to zero."
2fbed782
EZ
1602 (let ((nick (rcirc-user-nick nick)))
1603 (with-rcirc-process-buffer process
1604 (let* ((chans (gethash nick rcirc-nick-table))
1605 (record (assoc-string channel chans t)))
1606 (if record
a0a5c583
GM
1607 (when line (setcdr record line))
1608 (puthash nick (cons (cons channel (or line 0))
2fbed782
EZ
1609 chans)
1610 rcirc-nick-table))))))
bd43c990
RS
1611
1612(defun rcirc-nick-remove (process nick)
1613 "Remove NICK from table."
adf794e4 1614 (with-rcirc-process-buffer process
bd43c990
RS
1615 (remhash nick rcirc-nick-table)))
1616
1617(defun rcirc-remove-nick-channel (process nick channel)
1618 "Remove the CHANNEL from list associated with NICK."
adf794e4 1619 (with-rcirc-process-buffer process
db58efbf 1620 (let* ((chans (gethash nick rcirc-nick-table))
adf794e4
EZ
1621 (newchans
1622 ;; instead of assoc-string-delete-all:
1623 (let ((record (assoc-string channel chans t)))
1624 (when record
1625 (setcar record 'delete)
1626 (assq-delete-all 'delete chans)))))
bd43c990
RS
1627 (if newchans
1628 (puthash nick newchans rcirc-nick-table)
1629 (remhash nick rcirc-nick-table)))))
1630
a2524d26
EZ
1631(defun rcirc-channel-nicks (process target)
1632 "Return the list of nicks associated with TARGET sorted by last activity."
1633 (when target
1634 (if (rcirc-channel-p target)
1635 (with-rcirc-process-buffer process
1636 (let (nicks)
1637 (maphash
1638 (lambda (k v)
1639 (let ((record (assoc-string target v t)))
1640 (if record
1641 (setq nicks (cons (cons k (cdr record)) nicks)))))
1642 rcirc-nick-table)
1643 (mapcar (lambda (x) (car x))
a0a5c583
GM
1644 (sort nicks (lambda (x y)
1645 (let ((lx (or (cdr x) 0))
1646 (ly (or (cdr y) 0)))
1647 (< ly lx)))))))
a2524d26 1648 (list target))))
2c8abe90
AS
1649
1650(defun rcirc-ignore-update-automatic (nick)
2e398771
JB
1651 "Remove NICK from `rcirc-ignore-list'
1652if NICK is also on `rcirc-ignore-list-automatic'."
2c8abe90
AS
1653 (when (member nick rcirc-ignore-list-automatic)
1654 (setq rcirc-ignore-list-automatic
1655 (delete nick rcirc-ignore-list-automatic)
1656 rcirc-ignore-list
1657 (delete nick rcirc-ignore-list))))
bd43c990 1658\f
c62bf05a
DD
1659(defun rcirc-nickname< (s1 s2)
1660 "Compares two IRC nicknames. Operator nicknames (@) are
1661considered less than voiced nicknames (+). Any other nicknames
1662are greater than voiced nicknames.
1663
1664Returns t if S1 is less than S2, otherwise nil.
1665
1666The comparison is case-insensitive."
1667 (setq s1 (downcase s1)
1668 s2 (downcase s2))
1669 (let* ((s1-op (eq ?@ (string-to-char s1)))
1670 (s2-op (eq ?@ (string-to-char s2))))
1671 (if s1-op
1672 (if s2-op
1673 (string< (substring s1 1) (substring s2 1))
1674 t)
1675 (if s2-op
1676 nil
1677 (string< s1 s2)))))
1678
1679(defun rcirc-sort-nicknames-join (input sep)
1680 "Takes a string of nicknames and returns the string with the
1681nicknames sorted.
1682
1683INPUT is a string containing nicknames separated by SEP.
1684
1685This function is non-destructive, sorting a copy of the input."
a91dedc4
SM
1686 (let* ((parts (split-string input sep t))
1687 (sorted (sort parts 'rcirc-nickname<)))
1688 (mapconcat 'identity sorted sep)))
c62bf05a 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