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