1 ;;; mchat.el --- Multicast Chatting package for XEmacs
3 ;; Copyright (C) 1997-2000 Didier Verna.
5 ;; Author: Didier Verna <didier@xemacs.org>
6 ;; Maintainer: Didier Verna <didier@xemacs.org>
7 ;; Created: Mon Feb 15 18:45:02 1999 under XEmacs 21.2 (beta 9)
8 ;; Last Revision: Fri Aug 4 13:47:56 2000
9 ;; Keywords: comm processes
11 ;; This file is part of XEmacs.
13 ;; XEmacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2 of the License, or
16 ;; (at your option) any later version.
18 ;; XEmacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program; if not, write to the Free Software
25 ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 ;; Contents management by FCM version 0.1.
32 ;; MChat is a package allowing a conversation to take place between a
33 ;; potentially infinite number of people across the Internet. It uses the
34 ;; Multicast support that I've added to XEmacs in the early 21.0 days. At that
35 ;; time a rudimentary version of MChat was written, merely to illustrate the
36 ;; feature, and almost nobody knew it, appart from the other developpers. This
37 ;; version is much improved and completely backward incompatible ;-).
39 ;; #### NOTE: before using this package, you most likely have to customize the
40 ;; `mchat-nsl-method' first. It specifies a program like nsl or nslookup in
41 ;; order to retrieve the IP number of your machine.
43 ;; The main entry point to the package is the `mchat' function which allows
44 ;; you to open a multicast group based on the MChat protocol. There are a
45 ;; number a variables that you can customize in the 'mchat custom group. Their
46 ;; doc-string is rather self-explanatory I think. You might also want to look
47 ;; at the bindings (C-h b) in MChat buffers, or at the menubar entries to get
48 ;; an idea of what you can do.
50 ;; #### WARNING: the current version (but there's also a limitation in the
51 ;; internals of XEmacs's processes) doesn't let you send messages longer than
52 ;; 500 octets or so, including the protocol header. I plan to change this in
53 ;; the future (see the todo list).
58 ;; The original idea of MChat is from Philippe Dax <dax@infres.enst.fr>. Akim
59 ;; Demaille <akim@epita.fr> was *THE* beta tester for the 2.0 release. Many
60 ;; good ideas come from him.
65 ;; - implement fragmentation support. When we have a proper packaging scheme
66 ;; for providing both lisp and C modules, I'll probably rewrite the
67 ;; multicast support as a module because the current process abstraction is
68 ;; not very well suited to it. In UDP, you'd like to have access to more of
69 ;; the datagrams components, and use proper non-connected mode routines to
70 ;; send/receive to/from the network.
71 ;; - maybe implement dynamic heart-bit
80 ;; User Configurable stuff ==================================================
83 "Multicast Chatting package.")
85 (defcustom mchat-user-tag (user-full-name)
86 "*The tag to use to identify your messages."
90 (defcustom mchat-registered-groups nil
91 "*Alist of registered mchat groups. Each element looks like
92 (NAME ADDRESS) where NAME is a string used to identify the group, and
93 ADDRESS is the multicast address. NAME must be unique. Please refer to
94 `open-multicast-group' for the syntax of ADDRESS."
96 :type '(repeat (group (string :tag " Name")
97 (string :tag "Address"))))
99 (defcustom mchat-message-cache-size 32
100 "*Number of sent messages to remember for each MChat group."
104 (defcustom mchat-circulate-messages nil
105 "*If non nil, consider the message caches as circular lists, allowing
106 to jump directly from one end to the other."
110 (defcustom mchat-ring-sound t
111 "*Sound to use when somebody rings an MChat group (see `sound-alist').
112 Otherwise, t means just beep and nil means don't ever produce any sound."
116 (defcustom mchat-flash-on-message 'needed
117 "*When you receive a message in an MChat group, the selected frame can
118 be flashed according to the value of this variable:
119 - if t, always flash when a message arrives. As a special exception, this
120 doesn't flash when you are in one of the two MChat buffers corresponding
122 - if 'needed, flash only when the MChat buffer is not visible,
123 - if nil, never flash.
125 NOTE: currently, it's not always possible to figure out exactly whether
126 the MChat buffer is visible (for instance if its frame is mapped, but
127 obscured by some other window)."
129 :type '(choice (const :tag "always" t)
130 (const :tag "needed" needed)
131 (const :tag "never" nil)))
133 (defcustom mchat-major-mode-string "MChat"
134 "*String to use in the modeline when the MChat major mode is active."
138 (defcustom mchat-major-mode-hooks nil
139 "*Hooks to run after setting up the MChat major mode."
143 (defcustom mchat-minor-mode-string " mchat"
144 "*String to use in the modeline when the MChat minor mode is active."
148 (defcustom mchat-minor-mode-hooks nil
149 "*Hooks to run after setting up the MChat minor mode."
153 (defcustom mchat-before-send-hooks nil
154 "*Hooks to run before sending a message. The hooks are executed in the
155 MChat message buffer."
159 (defcustom mchat-treat-message-hooks nil
160 "*Hooks to run after a message is received. The hooks will be called with
161 two arguments, the beginning and end position of the message in the MChat
162 group buffer. The group buffer will be current when the hooks are executed."
166 (defcustom mchat-message-buffer-major-mode 'text
167 "*Major mode to use for MChat message buffers (less the -mode suffix)."
171 (defcustom mchat-loaded-hooks nil
172 "*Hooks run when mchat is loaded."
176 (defcustom mchat-window-percent 75
177 "*Percentage of frame occupied by the Group buffer. The rest will go to the
182 (defcustom mchat-nsl-method
185 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
187 "*Method to use in order to retrieve one IP address from your machine. It
188 looks like (PROGRAM ARGS REGEXP START) where:
189 - PROGRAM is the program to be called (a string)
190 - ARGS is a lisp expression returning a string of arguments you might want to
192 - REGEXP is the regexp to find the IP address in the output of PROGRAM,
193 - START is the indice of the sub-match returning the first component of the
196 :type '(list (string :tag " Program")
197 (sexp :tag " Arguments")
198 (string :tag " Regexp")
199 (integer :tag "First match")))
201 (defface mchat-comment-face '((t (:italic t)))
202 "*Face used when inserting text that is not a message in the MChat group
206 (defface mchat-tag-face '((t (:bold t)))
207 "*Face used for identifying the sender of a message."
211 ;; Private variables ========================================================
213 ;; $Format: "(defconst mchat-prcs-major-version \"$ProjectMajorVersion$\")"$
214 (defconst mchat-prcs-major-version "version-2-1")
215 ;; $Format: "(defconst mchat-prcs-minor-version \"$ProjectMinorVersion$\")"$
216 (defconst mchat-prcs-minor-version "1")
217 (defconst mchat-version
218 (let ((level mchat-prcs-minor-version)
220 (string-match "\\(branch\\|version\\)-\\([0-9]+\\)-\\([0-9]+\\)"
221 mchat-prcs-major-version)
222 (setq major (match-string 2 mchat-prcs-major-version)
223 minor (match-string 3 mchat-prcs-major-version)
224 status (match-string 1 mchat-prcs-major-version))
225 (cond ((string= status "version")
226 (setq level (int-to-string (1- (string-to-int level))))
227 (if (string-equal level "0")
228 (concat major "." minor)
229 (concat major "." minor "." level)))
230 ((string= status "branch")
231 (concat major "." minor "-b" level)))
234 ;; #### NOTE: the group informations stored in each entry are constant stuff.
235 ;; Things likely to change (like members) are stored in buffer-local
237 (defvar mchat-groups nil
238 ;; List of all currently active mchat groups.
239 ;; Each element of the list is of the form: (NAME ADDRESS (KEY . VALUE) ...)
240 ;; NAME is the name use to identify the group,
241 ;; ADDRESS is the multicast address for the group,
242 ;; KEY can currently be one of the following:
243 ;; buffer: VALUE is the message edition buffer for this group,
244 ;; proc: VALUE is the process id associated with this group,
245 ;; zombie-check: VALUE is the timeout id corresponding to the zonbie check.
248 ;; Used to retrieve back the group desc from mchat-groups.
249 (make-variable-buffer-local
250 (defvar mchat-group-name nil
251 ;; Becomes automatically buffer local when set. Contains the name of the
252 ;; MChat group the buffer is associated with.
255 ;; See the note on top of `mchat-groups'
256 (make-variable-buffer-local
257 (defvar mchat-group-members nil
258 ;; Becomes automatically buffer local when set. This is a list of group
259 ;; members. Each element looks like: (ID (KEY . VALUE) ...)
260 ;; ID is the unique identifier for each member.
261 ;; KEY can be one the following:
262 ;; tag: VALUE is the tag to used to identify messages from this guy,
263 ;; time-stamp: VALUE is the last time I received a message from this guy.
264 ;; messges: VALUE is a list of buffered messages to be processed *after*
265 ;; we get information on the guy (especially the tag).
268 (make-variable-buffer-local
269 (defvar mchat-heart-bit-timeout nil
270 ;; Becomes automatically buffer local when set. The heart-bit timeout id.
273 (make-variable-buffer-local
274 (defvar mchat-message-cache nil
275 ;; Becomes automatically buffer local when set. The cache of messages
279 (make-variable-buffer-local
280 (defvar mchat-message-cache-indice nil
281 ;; Becomes automatically buffer local when set. The cache indice of the
282 ;; message currently displayed in the mchat message buffer.
285 (make-variable-buffer-local
286 (defvar mchat-unsent-message nil
287 ;; Becomes automatically buffer local when set. Remembers a message being
288 ;; edited we're moving in the messages history
291 (defconst mchat-submenu
293 :included mchat-group-name
296 [ "send it" mchat-message t ]
297 [ "discard it" mchat-discard-message t ]
298 [ "pop next" mchat-next-message t ]
299 [ "pop previous" mchat-previous-message t ]
300 [ "pop first" mchat-first-message t ]
301 [ "pop last" mchat-last-message t ]
304 [ "erase buffer" mchat-erase-buffer t ]
305 [ "ring bell" mchat-ring t ]
306 [ "show who's here" mchat-who t ]
307 [ "show info" mchat-info t ]
308 ;;[ "listening is " mchat-suspend t "on" ]
309 [ "quit" mchat-quit t ]
312 [ "register group" mchat-register t ]
313 [ "unregister group" mchat-unregister t ]
314 [ "switch group" mchat-switch-to-group t ]
315 [ "bury group" mchat-bury t ]
318 [ "MChat version" mchat-version t ]
320 ;; MChat-menu definition.
324 ;; MChat protocol management routines =======================================
326 ;; Here's a description of the MChat protocol (version 2.0) in pseudo-BNF:
328 ;; MESSAGE := PROTO_VERSION USER_ID LENGTH DATA
329 ;; PROTO_VERSION := PROTO_MAJOR (1 octet) PROTO_MINOR (1 octet)
330 ;; (see `mchat-protocol-*-version')
331 ;; USER_ID := USER_IP USER_PID
332 ;; LENGTH := length of the data segment (2 octets)
333 ;; DATA := ATOM ATOM_DATA
334 ;; USER_IP := one IP address of the machine we're running on (4 octets)
335 ;; USER_PID := PID of the process running an MChat client (8 octets)
337 ;; ATOM <ATOM_DATA>: meaning (see `mchat-protocol-atom-*')
338 ;; 'join <user tag>: join the group
339 ;; 'quit <nothing>: quit the group
340 ;; 'whois <user id>: ask <user id> for info
341 ;; 'iam <user tag>: send info on me
342 ;; 'ring <nothing>: ring the group
343 ;; 'msg <text>: send a message to the group
344 ;; 'heart-bit <nothing>: just show people that we exist
346 ;; The user id serves for identifying uniquely each member of the group. Some
347 ;; of them could use the same tag however.
349 ;; The main use of the LENGTH field is because of possible messages
350 ;; concatenation. It's then important to separate each message from the next
353 ;; - 'join and 'quit are sent once.
354 ;; - 'iam should be sent if somebody sent a 'whois request about me.
355 ;; - 'whois should be sent when we receive something (except 'join and 'iam)
356 ;; about a member we don't know.
357 ;; 'heart-bit should be sent only if we were idle for a long time.
360 ;; Each one must be representable on 8 bits, but I doubt I'll ever reach 255.
361 ;; $Format: "(defconst mchat-protocol-major-version $ProtocolMajorVersion$)"$
362 (defconst mchat-protocol-major-version 2)
363 ;; $Format: "(defconst mchat-protocol-minor-version $ProtocolMinorVersion$)"$
364 (defconst mchat-protocol-minor-version 0)
366 (defconst mchat-protocol-version
368 (aset str 0 (int-to-char mchat-protocol-major-version))
369 (aset str 1 (int-to-char mchat-protocol-minor-version))
372 (defconst mchat-heart-bit-delay 60
373 ;; Maximum inactivity period.
376 (defconst mchat-zombie-latency 5
377 ;; If I didn't receive any message (heart-bit included) from a member after
378 ;; this number of times the heart-bit, the guy must have been killed.
381 (defvar mchat-user-id nil
382 ;; #### FIXME: is it necessary to use 64bits for pids ?
383 ;; 12 octets header for all mchat messages. Octets list with explanation:
384 ;; 0- 3 -> IP address
385 ;; 4-12 -> emacs pid on 64 bits
386 ;; The pair IP-addres/emacs pid makes a unique identifier for everybody.
389 (defun mchat-user-id ()
390 ;; Build and return the user id, or nil.
392 (let ((str (make-string 12 0))
395 (n (cadddr mchat-nsl-method)))
397 (call-process (car mchat-nsl-method)
399 nil (eval (cadr mchat-nsl-method)))
400 (goto-char (point-min))
401 (re-search-forward (caddr mchat-nsl-method))
404 (int-to-char (string-to-int (match-string (+ i n)))))
409 (aset str i (int-to-char n))
410 (setq pid (/ (- pid n) 256))
416 ;; MChat protocol atoms
417 (defconst mchat-protocol-atom-join (int-to-char 0))
418 (defconst mchat-protocol-atom-quit (int-to-char 1))
419 (defconst mchat-protocol-atom-whois (int-to-char 10))
420 (defconst mchat-protocol-atom-iam (int-to-char 11))
421 (defconst mchat-protocol-atom-ring (int-to-char 50))
422 (defconst mchat-protocol-atom-msg (int-to-char 100))
423 (defconst mchat-protocol-atom-heart-bit (int-to-char 255))
425 ;; Message sending routines ------------------------------------------------
427 ;; #### NOTE: should implement message fragmentation
428 (defun mchat-send (proc data)
429 ;; send DATA to the process PROC, in the MChat protocol format. Disable the
430 ;; heart bit before sending, and restores it afterwards.
431 (let ((data-len "\0\0")
434 (with-current-buffer (process-buffer proc)
435 (disable-timeout mchat-heart-bit-timeout)
436 ;; compute and add the length of the message
438 (aset data-len 1 (int-to-char n))
439 (setq n (/ (- len n) 256))
441 (process-send-string proc (concat mchat-protocol-version
445 (setq mchat-heart-bit-timeout
446 (add-timeout mchat-heart-bit-delay 'mchat-heart-bit proc
447 mchat-heart-bit-delay)))
450 (defun mchat-send-join (proc)
451 ;; Sends a `join' message
452 (mchat-send proc (concat (char-to-string mchat-protocol-atom-join)
455 (defun mchat-send-quit (proc)
456 ;; Sends a `quit' message
457 (mchat-send proc (char-to-string mchat-protocol-atom-quit)))
459 (defun mchat-send-whois (proc data)
460 ;; Sends a `whois' message
461 (mchat-send proc (concat (char-to-string mchat-protocol-atom-whois)
464 (defun mchat-send-iam (proc)
465 ;; Sends a `iam' message
466 (mchat-send proc (concat (char-to-string mchat-protocol-atom-iam)
469 (defun mchat-send-ring (proc)
470 ;; Sends a `ring' message
471 (mchat-send proc (char-to-string mchat-protocol-atom-ring)))
473 (defun mchat-send-msg (proc data)
474 ;; Sends a real text message
475 (mchat-send proc (concat (char-to-string mchat-protocol-atom-msg)
478 (defun mchat-send-heart-bit (proc)
479 ;; sends the heart-bit message
480 (mchat-send proc (char-to-string mchat-protocol-atom-heart-bit)))
483 ;; Message receiving routines ----------------------------------------------
486 (defun mchat-current-time ()
487 ;; Return the current time in seconds
488 (let ((tm (current-time)))
489 (+ (* (car tm) 65536.0)
491 (/ (or (caddr tm) 0) 1000000.0))
494 (defmacro mchat-with-face (face &rest body)
495 ;; Execute BODY and put the result in face FACE
496 `(let ((start (point)))
498 (set-extent-face (make-extent start (point)) ,face)
500 (put 'mchat-with-face 'lisp-indent-function 1)
502 (defmacro mchat-insertion (&rest body)
503 ;; Execute BODY in current buffer as if it was a process output, and put it
505 ;; #### WARNING: BODY must use `insert-before-markers', not `insert'.
507 (let ((proc (get-buffer-process (current-buffer)))
509 (goto-char (process-mark proc))
511 (set-marker (process-mark proc) (point)))
513 (put 'mchat-insertion 'lisp-indent-function 0)
515 ;; #### NOTE: For some reason, I've noticed that I'm likely to receive several
516 ;; messages twice. I couldn't figure out why in the multicast API. Maybe a
517 ;; problem of loopback of some sort ? Just ignore doublets anyway.
518 (defun mchat-treat-join (grp id data) ;; DATA is the user tag
519 (let* ((proc (cdr (assoc 'proc grp)))
520 (buf (process-buffer proc))
522 (with-current-buffer buf
523 (setq who (assoc id mchat-group-members))
525 ;; update the members list
528 (cons 'time-stamp (mchat-current-time))))
529 (push who mchat-group-members)
531 (mchat-with-face 'mchat-comment-face
532 (insert-before-markers (concat data " joins.\n"))))
536 (defun mchat-treat-quit (grp id)
537 (let ((buf (process-buffer (cdr (assoc 'proc grp))))
539 (with-current-buffer buf
540 (setq who (assoc id mchat-group-members))
541 (when who ;; otherwise, I didn't know the guy anyway
543 (mchat-with-face 'mchat-comment-face
544 (insert-before-markers
545 (concat (or (cdr (assoc 'tag who)) "<unknown>") " quits.\n"))))
547 (setq mchat-group-members (remassoc id mchat-group-members))
551 ;; #### NOTE: those two functions should probably use the CL package to
552 ;; perform substitutions, but I'm not sure it's worth it. Maybe if one day I
553 ;; have more things to update in the members structure, I could write a more
555 (defun mchat-update-time-stamp (id)
556 ;; Update the timestamp for user ID in current MChat group buffer
557 (let ((who (assoc id mchat-group-members)))
558 (setq mchat-group-members (remassoc id mchat-group-members))
559 (setq who (remassoc 'time-stamp who))
560 (setq who (append who (list (cons 'time-stamp (mchat-current-time)))))
561 (push who mchat-group-members)
564 (defun mchat-bufferize-message (id data)
565 ;; Add a message to the unprocessed list
566 (let* ((who (assoc id mchat-group-members))
567 (messages (assoc 'messages who)))
568 (setq mchat-group-members (remassoc id mchat-group-members))
569 (setq who (remassoc 'messages who))
571 (setq messages (append messages (list data)))
573 (setq messages (list 'messages data)))
574 (setq who (append who messages))
575 (push who mchat-group-members)
578 (defun mchat-treat-whois (grp id data) ;; DATA is the user id to check
579 (let* ((proc (cdr (assoc 'proc grp)))
580 (buf (process-buffer proc))
583 (when (string-equal mchat-user-id data)
584 (mchat-send-iam proc))
586 (with-current-buffer buf
587 (setq who (assoc id mchat-group-members))
588 (cond ((not who) ;; yet unknown
589 ;; update the members list
590 (setq who (list id (cons 'time-stamp (mchat-current-time))))
591 (push who mchat-group-members)
592 (mchat-send-whois proc id))
593 ((not (assoc 'tag who)) ;; half-known
594 (mchat-update-time-stamp id)
595 (mchat-send-whois proc id))
596 (t ;; completely known
597 (mchat-update-time-stamp id))))
600 (defun mchat-treat-iam (grp id data) ;; DATA is the user tag
601 (let ((buf (process-buffer (cdr (assoc 'proc grp))))
603 (with-current-buffer buf
604 (setq who (assoc id mchat-group-members))
605 (cond ((not who) ;; yet unknown
608 (cons 'time-stamp (mchat-current-time))))
609 (push who mchat-group-members)
611 (mchat-with-face 'mchat-comment-face
612 (insert-before-markers (concat data " is here.\n")))))
613 ((not (assoc 'tag who)) ;; half-known
614 (let ((messages (cdr (assoc 'messages who))))
615 (setq who (append who (list (cons 'tag data))))
617 (setq who (remassoc 'messages who)))
618 (setq mchat-group-members (remassoc id mchat-group-members))
619 (push who mchat-group-members)
620 (mchat-update-time-stamp id)
622 (mchat-with-face 'mchat-comment-face
623 (insert-before-markers (concat data " is here.\n"))))
626 (while (setq msg (pop messages))
627 (mchat-treat-data grp id msg))))))
628 (t ;; completely known
629 ;; #### FIXME: should check if tag is still the same
630 (mchat-update-time-stamp id))))
634 ;; like 'ding but check mchat-ring-sound
635 (cond ((eq mchat-ring-sound t) ;; just the bell
637 (mchat-ring-sound ;; a real sound. Try it or use the bell
638 (if (and (or (featurep 'native-sound)
639 (featurep 'nas-sound))
640 (device-sound-enabled-p))
641 (ding t mchat-ring-sound)
645 ;; Handles my own messages
646 (defun mchat-treat-ring (grp id)
647 (let* ((proc (cdr (assoc 'proc grp)))
648 (buf (process-buffer proc))
650 (if (string-equal id mchat-user-id)
651 (with-current-buffer buf
653 (mchat-with-face 'mchat-comment-face
654 (insert-before-markers "I ring.\n")))
657 (with-current-buffer buf
658 (setq who (assoc id mchat-group-members))
659 (cond ((not who) ;; unknown
661 (cons 'time-stamp (mchat-current-time))
664 mchat-protocol-atom-ring))
666 (push who mchat-group-members)
667 (mchat-send-whois proc id))
668 ((not (assoc 'tag who)) ;; half known
669 (mchat-bufferize-message id (char-to-string
670 mchat-protocol-atom-ring))
671 (mchat-update-time-stamp id)
672 (mchat-send-whois proc id))
673 (t ;;completely known
674 (mchat-update-time-stamp id)
676 (mchat-with-face 'mchat-comment-face
677 (insert-before-markers
678 (concat (cdr (assoc 'tag who)) " rings.\n"))))
682 ;; Handles my own messages
683 (defun mchat-treat-msg (grp id data) ;; DATA is a normal message
684 (let* ((proc (cdr (assoc 'proc grp)))
685 (buf (process-buffer proc))
687 (if (string-equal id mchat-user-id)
688 (with-current-buffer buf
690 (mchat-with-face 'mchat-tag-face
691 (insert-before-markers "I say:\n"))
694 (insert-before-markers (concat data "\n"))
696 (run-hook-with-args 'mchat-treat-message-hooks
699 (with-current-buffer buf
700 (setq who (assoc id mchat-group-members))
701 (cond ((not who) ;; unknown
703 (cons 'time-stamp (mchat-current-time))
705 (concat (char-to-string
706 mchat-protocol-atom-msg)
709 (push who mchat-group-members)
710 (mchat-send-whois proc id))
711 ((not (assoc 'tag who)) ;; half known
712 (mchat-bufferize-message
714 (concat (char-to-string mchat-protocol-atom-msg)
716 (mchat-update-time-stamp id)
717 (mchat-send-whois proc id))
718 (t ;; completely known
721 (mchat-with-face 'mchat-tag-face
722 (insert-before-markers
723 (concat (cdr (assoc 'tag who)) " says:\n")))
726 (insert-before-markers (concat data "\n"))
728 (run-hook-with-args 'mchat-treat-message-hooks
730 ;; #### WARNING: do this outside buf !!
731 ;; Now possibly flash if the buffer is not visible (but not if
734 (cond ((and (eq mchat-flash-on-message t)
735 (not (eq (current-buffer) buf))
736 (not (eq (current-buffer)
737 (cdr (assoc 'buffer grp)))))
738 (let ((visible-bell t)) (ding t t)))
739 ((and (eq mchat-flash-on-message 'needed)
740 (not (get-buffer-window buf 'visible)))
741 (let ((visible-bell t)) (ding t t))))))
744 (defun mchat-treat-heart-bit (grp id)
745 (let* ((proc (cdr (assoc 'proc grp)))
746 (buf (process-buffer proc))
748 (with-current-buffer buf
749 (setq who (assoc id mchat-group-members))
750 (cond ((not who) ;; unknown
751 (setq who (list id (cons 'time-stamp (mchat-current-time))))
752 (push who mchat-group-members)
753 (mchat-send-whois proc id))
754 ((not (assoc 'tag who)) ;; half known
755 (mchat-update-time-stamp id)
756 (mchat-send-whois proc id))
757 (t ;; completely known
758 (mchat-update-time-stamp id))))
761 (defun mchat-treat-data (grp id data)
762 ;; treat an MChat protocol message for group GRP. Branch to the proper
764 (cond ((eq (aref data 0) mchat-protocol-atom-join) ;; join
765 (unless (string-equal id mchat-user-id)
766 (mchat-treat-join grp id (substring data 1))))
767 ((eq (aref data 0) mchat-protocol-atom-quit) ;; quit
768 (unless (string-equal id mchat-user-id)
769 (mchat-treat-quit grp id)))
770 ((eq (aref data 0) mchat-protocol-atom-whois) ;; whois
771 (unless (string-equal id mchat-user-id)
772 (mchat-treat-whois grp id (substring data 1))))
773 ((eq (aref data 0) mchat-protocol-atom-iam) ;; iam
774 (unless (string-equal id mchat-user-id)
775 (mchat-treat-iam grp id (substring data 1))))
776 ;; Handle my own messages
777 ((eq (aref data 0) mchat-protocol-atom-ring) ;; ring
778 (mchat-treat-ring grp id))
779 ;; Handle my own messages
780 ((eq (aref data 0) mchat-protocol-atom-msg) ;; msg
781 (mchat-treat-msg grp id (substring data 1)))
782 ((eq (aref data 0) mchat-protocol-atom-heart-bit) ;; heart bit
783 (unless (string-equal id mchat-user-id)
784 (mchat-treat-heart-bit grp id)))
787 ;; #### FIXME: I think we can loose UDP packets, but we can assume that if one
788 ;; is received, it's not corrupted, right ?
789 ;; Also, I think I've encountered cases where packets were concatenated, so I
790 ;; handle this by adding the length of each DATA segment, and calling again
791 ;; the process filter myself if the message is too long.
792 (defun mchat-process-filter (proc str)
793 ;; Filter for all received mchat messages
794 (let ((grp (catch 'found
795 (let ((grps mchat-groups) grp)
796 (while (setq grp (pop grps))
797 (if (equal proc (cdr (assoc 'proc grp)))
798 (throw 'found grp))))))
799 (major (char-to-int (aref str 0)))
800 (minor (char-to-int (aref str 1)))
802 ;; Check that we understand this protocol version:
803 (if (> (+ (* 256 major) minor)
804 (+ (* 256 mchat-protocol-major-version)
805 mchat-protocol-minor-version))
806 (with-current-buffer (process-buffer proc)
808 (mchat-with-face 'mchat-comment-face
809 (insert-before-markers
810 "Received a message with MChat protocol version "
811 (int-to-string major) "." (int-to-string minor) " !\n")
812 (insert-before-markers
813 "Please check if you have an outdated package.\n")
814 (insert-before-markers
815 "(also, note that this might just be a corrupted message)\n")
817 ;; else, this protocol version is understood, decode the message.
818 ;; #### NOTE: the following stuff could change for newer protocol
819 ;; versions. Just there's only one now (2.0).
820 (setq id (substring str 2 14)
821 len (+ (* 256 (char-to-int (aref str 14)))
822 (char-to-int (aref str 15)))
823 data (substring str 16 (+ 16 len))
824 next (and (> (length str) (+ 16 len))
825 (substring str (+ 16 len))))
826 (mchat-treat-data grp id data)
827 (and next (mchat-process-filter proc next)))
831 ;; MChat groups display routines ============================================
833 ;; Each frame in which an MChat group is displayed gets a
834 ;; 'mchat-window-configurations property. This property is a list of
835 ;; window configurations current before each call to `mchat-switch-to-group'.
837 (defun mchat-switch-to-group (grp)
838 "Switch to the MChat group GRP in the current frame. This function remembers
839 the previous window configuration, that you will be able to get back using
842 (list (assoc (completing-read "Group: " mchat-groups nil t) mchat-groups)))
843 ;; Remember the window configuration
844 (set-frame-property (selected-frame) 'mchat-window-configurations
845 (cons (current-window-configuration)
846 (frame-property (selected-frame)
847 'mchat-window-configurations)))
848 ;; Setup the frame for this group
849 (delete-other-windows)
850 (switch-to-buffer (process-buffer (cdr (assoc 'proc grp))))
851 (split-window nil (/ (* (window-height) mchat-window-percent) 100))
853 (switch-to-buffer (cdr (assoc 'buffer grp)))
857 "Bury the MChat group displayed in the current frame. This function
858 actually restores the window configuration immediately preceding the last call
859 to `mchat-switch-to-group' in this frame.
861 If this frame didn't display an MChat group before (properly speaking, that is
862 if `mchat-switch-to-group' was not called in this frame before), this function
865 (let ((wcs (frame-property (selected-frame) 'mchat-window-configurations)))
867 (set-frame-property (selected-frame)
868 'mchat-window-configurations (cdr wcs))
869 ;; #### NOTE: `set-window-configuration' doesn't seem to produce any
870 ;; errors when the window configuration can't be restored (like, if one
871 ;; buffer is killed, or something).
872 (set-window-configuration (car wcs)))
876 ;; MChat group destruction routines =========================================
878 (defun mchat-kill-group (grp)
879 ;; Destroy the group and clean up.
880 ;; XEmacs handles deleting the processes associated with buffers being
881 ;; killed, but I prefer to do it myself, in priority.
882 (let* ((proc (cdr (assoc 'proc grp)))
883 (group-buffer (process-buffer proc))
884 (message-buffer (cdr (assoc 'buffer grp)))
885 (zombie-check (cdr (assoc 'zombie-check grp))))
886 ;; #### WARNING: this is the first thing to do, because mchat-send reputs
887 ;; the timeout after sending the message !!
888 (mchat-send-quit proc)
889 ;; Don't call the hooks !!
890 (with-current-buffer group-buffer
891 (disable-timeout mchat-heart-bit-timeout)
892 (remove-hook 'kill-buffer-hook 'mchat-kill-buffer-hook t))
893 (with-current-buffer message-buffer
894 (remove-hook 'kill-buffer-hook 'mchat-kill-buffer-hook t))
895 (disable-timeout zombie-check)
896 (delete-process proc)
897 (kill-buffer group-buffer)
898 (kill-buffer message-buffer)
899 (setq mchat-groups (remassoc (car grp) mchat-groups))
903 ;; MChat modes routines =====================================================
905 ;; #### NOTE: for the moment, all of the following functions are supposed to
906 ;; be run only from an MChat buffer. Maybe we could make some of them query
907 ;; the group otherwise, but there again, I don't find this particularly
910 (defsubst mchat-interactive ()
911 (let* ((grp (assoc mchat-group-name mchat-groups)))
914 (error "No MChat group associated with this buffer."))
917 (defun mchat-quit (grp)
918 "Quit the MChat group GRP."
919 (interactive (mchat-interactive))
921 (mchat-kill-group grp))
923 (defun mchat-ring (grp)
924 "Ring the bell on the MChat group GRP. If people are not looking at the
925 buffer, at least they can hear you... Hmm maybe I should have called this
926 function `mchat-annoy-user'... See also `mchat-flash-on-message'."
927 (interactive (mchat-interactive))
928 (mchat-send-ring (cdr (assoc 'proc grp))))
930 (defsubst mchat-maybe-remember-message ()
931 ;; Remember the message being edited, *even* if it's still empty. Indeed, we
932 ;; want to get it back after moving in the cache.
933 (when (or (buffer-modified-p) (eq 0 (length (buffer-substring))))
934 (setq mchat-unsent-message (buffer-substring))))
936 (defun mchat-next-message (grp &optional n)
937 "Restore the next Nth message (previous if N is negative) in the
938 message buffer of group GRP. The variable `mchat-circulate-messages' controls
939 the behavior when N is out of bounds. If N is not given, it is 1 by default.
941 When called interactively, use the prefix to change N."
944 (list (if current-prefix-arg
945 (prefix-numeric-value current-prefix-arg)
948 (with-current-buffer (cdr (assoc 'buffer grp))
949 (mchat-maybe-remember-message)
950 ;; When moving in the cache the unset message must be virtually part of
951 ;; the cache in order to get it back.
952 (let* ((cache (append mchat-message-cache (list mchat-unsent-message)))
954 (indice (or mchat-message-cache-indice (1- len))))
955 ;; find the new indice
956 (setq n (+ indice n))
957 (cond (mchat-circulate-messages
965 (setq n (1- len))))))
968 (insert (nth n cache))
969 (setq mchat-message-cache-indice n)
970 (unless (and (eq n (1- len)) (> (length (buffer-substring)) 0))
971 (set-buffer-modified-p nil))
975 (defun mchat-previous-message (grp)
976 "Restore the previous message in the message buffer of group GRP. See also
977 the variable `mchat-circulate-messages'"
978 (interactive (mchat-interactive))
979 (mchat-next-message grp -1))
981 (defsubst mchat-pop-cached-message (n)
982 ;; Pops the message number n from the cache.
984 (insert (nth n mchat-message-cache))
985 (setq mchat-message-cache-indice n)
986 (set-buffer-modified-p nil))
988 (defun mchat-first-message (grp)
989 "Restore the oldest cached message in the message buffer of group GRP."
990 (interactive (mchat-interactive))
991 (with-current-buffer (cdr (assoc 'buffer grp))
992 (when mchat-message-cache
993 (mchat-maybe-remember-message)
994 (mchat-pop-cached-message 0))
997 ;; #### NOTE: contrary to `mchat-next-message', this function doesn't consider
998 ;; the message currently edited as part of the cache. However, it is
999 ;; remembered and can be accessed with one `M-n'.
1000 (defun mchat-last-message (grp)
1001 "Restore the youngest cached message in the message buffer of group GRP."
1002 (interactive (mchat-interactive))
1003 (with-current-buffer (cdr (assoc 'buffer grp))
1004 (when mchat-message-cache
1005 (mchat-maybe-remember-message)
1006 (mchat-pop-cached-message (1- (length mchat-message-cache))))
1009 (defun mchat-message (grp)
1010 "Send the contents of the message buffer to the MChat group GRP."
1011 (interactive (mchat-interactive))
1012 (with-current-buffer (cdr (assoc 'buffer grp))
1013 ;; the hooks might change something, so run them here.
1014 (run-hooks 'mchat-before-send-hooks)
1015 (let ((str (buffer-substring)))
1016 (cond ((or (not str) (= 0 (length str)))
1017 (error "The message buffer is empty."))
1018 ((> (length str) 450)
1019 (error "Message too long. Please send it in shorter pieces."))
1021 (when (buffer-modified-p) ;; a new message
1022 ;; make room for it in the cache ('and should be enough)
1023 (while (>= (length mchat-message-cache)
1024 mchat-message-cache-size)
1025 (pop mchat-message-cache))
1027 (setq mchat-message-cache
1028 (append mchat-message-cache (list str)))
1029 (setq mchat-message-cache-indice nil)
1030 (setq mchat-unsent-message nil)
1032 (set-buffer-modified-p nil))
1034 (mchat-send-msg (cdr (assoc 'proc grp)) str)
1038 (defun mchat-info (grp)
1039 "Display information on the current MChat group."
1040 (interactive (mchat-interactive))
1041 (message "\"%s\" on %s" (car grp) (cadr grp)))
1043 (defun mchat-who (grp)
1044 "Display the list of known members of the current group."
1045 (interactive (mchat-interactive))
1046 (with-current-buffer (process-buffer (cdr (assoc 'proc grp)))
1048 (mchat-with-face 'mchat-comment-face
1049 (let ((members mchat-group-members)
1052 (insert-before-markers
1053 "You're the only one, Max. Getting bored?\n")
1055 (insert-before-markers
1056 (concat "People currently known on `" (car grp) "' are:\n"))
1057 (while (setq member (pop members))
1058 (insert-before-markers
1059 (concat "- "(cdr (assoc 'tag member)) ",\n")))
1060 (delete-backward-char 2)
1061 (insert-before-markers ".\n")))
1065 (defun mchat-erase-buffer (grp)
1066 "Erase the current MChat group buffer."
1067 (interactive (mchat-interactive)) ;; this is just a protection
1068 (with-current-buffer (process-buffer (cdr (assoc 'proc grp)))
1069 (let ((buffer-read-only nil))
1073 (defun mchat-discard-message (grp)
1074 "Discard the contents of the current MChat message buffer."
1075 (interactive (mchat-interactive))
1076 (with-current-buffer (cdr (assoc 'buffer grp))
1079 (defun mchat-register (grp)
1080 "Register the current group (add it to `mchat-registered-groups')."
1081 (interactive (mchat-interactive))
1082 (if (assoc (car grp) mchat-registered-groups)
1083 (error "This group is already defined.")
1085 (setq mchat-registered-groups
1086 (append `((,(car grp) ,(cadr grp))) mchat-registered-groups))
1087 (when (y-or-n-p "Group registered. Keep it for future sessions ? ")
1088 ;; #### FIXME: should I use Custom also to set the variable, or only if
1089 ;; we want to save the value ?
1090 (custom-set-variables
1091 `(mchat-registered-groups (quote ,mchat-registered-groups) t))
1095 (defun mchat-unregister (grp)
1096 "Unregister the current group (remove it to `mchat-registered-groups')."
1097 (interactive (mchat-interactive))
1098 (setq mchat-registered-groups (remassoc (car grp) mchat-registered-groups))
1099 (when (y-or-n-p "Group unregistered. Forget it for future sessions ? ")
1100 ;; #### FIXME: should I use Custom also to set the variable, or only if
1101 ;; we want to save the value ?
1102 (custom-set-variables
1103 `(mchat-registered-groups (quote ,mchat-registered-groups) t))
1107 (defun mchat-version ()
1108 "Show the current version of MChat."
1110 (message "MChat version %s" mchat-version))
1113 ;; MChat major mode (for the group buffers) =================================
1115 ;; #### NOTE: I could let more bindings here (the message ones in particular),
1116 ;; but since people are not supposed to live in the group buffer, I restrict
1117 ;; the commands available on purpose.
1119 (defvar mchat-major-mode-map
1120 (let ((m (make-sparse-keymap 'mchat-major-mode-map)))
1122 (define-key m "e" 'mchat-erase-buffer)
1123 (define-key m "b" 'mchat-ring)
1124 (define-key m "w" 'mchat-who)
1125 (define-key m "i" 'mchat-info)
1126 ;;; (define-key m "s" 'mchat-suspend)
1127 (define-key m "q" 'mchat-quit)
1129 (define-key m "r" 'mchat-register)
1130 (define-key m "u" 'mchat-unregister)
1131 (define-key m "o" 'mchat-switch-to-group)
1132 (define-key m "x" 'mchat-bury)
1134 (define-key m "v" 'mchat-version)
1136 ;; MChat major mode keymap
1139 (defun mchat-major-mode ()
1140 "Turns on the MChat major mode. Used for MChat group buffers. You're not
1141 supposed to use this. To join an MChat group, use `mchat' instead."
1142 (kill-all-local-variables)
1143 (setq buffer-read-only t)
1144 (use-local-map mchat-major-mode-map)
1145 (setq major-mode 'mchat-major-mode)
1146 (setq mode-name mchat-major-mode-string)
1147 (when (featurep 'menubar)
1148 (setq mode-popup-menu mchat-submenu))
1149 (run-hooks 'mchat-major-mode-hooks))
1152 ;; MChat minor mode (for the message buffers) ===============================
1154 (defvar mchat-minor-mode-map
1155 (let ((m (make-sparse-keymap 'mchat-minor-mode-map)))
1157 (define-key m [(control c) (control c)] 'mchat-message)
1158 (define-key m [(control c) (control d)] 'mchat-discard-message)
1159 (define-key m [(meta n)] 'mchat-next-message)
1160 (define-key m [(meta p)] 'mchat-previous-message)
1161 (define-key m [(meta P)] 'mchat-first-message)
1162 (define-key m [(meta N)] 'mchat-last-message)
1164 (define-key m [(control c) (control e)] 'mchat-erase-buffer)
1165 (define-key m [(control c) (control b)] 'mchat-ring)
1166 (define-key m [(control c) (control w)] 'mchat-who)
1167 (define-key m [(control c) (control i)] 'mchat-info)
1168 ;;; (define-key m "s" 'mchat-suspend)
1169 (define-key m [(control c) (control q)] 'mchat-quit)
1171 (define-key m [(control c) (control r)] 'mchat-register)
1172 (define-key m [(control c) (control u)] 'mchat-unregister)
1173 (define-key m [(control c) (control o)] 'mchat-switch-to-group)
1174 (define-key m [(control c) (control x)] 'mchat-bury)
1176 (define-key m [(control c) (control v)] 'mchat-version)
1178 ;; MChat minor mode keymap
1181 (make-variable-buffer-local
1182 (defvar mchat-minor-mode nil))
1184 (defun mchat-minor-mode (arg)
1185 "Toggles the MChat minor mode. Used for MChat message buffers. You're not
1186 supposed to use this. To join an MChat group, use `mchat' instead."
1188 (setq mchat-minor-mode
1189 (if (null arg) (not mchat-minor-mode)
1190 (> (prefix-numeric-value arg) 0)))
1191 (when (featurep 'menubar)
1192 ;; I know, I know, this should be the *major* mode menu, not mine.
1193 (setq mode-popup-menu mchat-submenu))
1194 (run-hooks 'mchat-minor-mode-hooks))
1197 'mchat-minor-mode mchat-minor-mode-string mchat-minor-mode-map)
1200 ;; MChat group creation routines ============================================
1202 ;; It's OK to call mchat-kill-group, because it's OK to re-kill killed
1204 (defun mchat-kill-buffer-hook ()
1205 (mchat-kill-group (assoc mchat-group-name mchat-groups)))
1207 (defun mchat-heart-bit (proc)
1208 ;; Timeout to resend a `heart-bit' message
1209 (mchat-send-heart-bit proc))
1211 (defun mchat-zombie-check (buffer)
1212 (with-current-buffer buffer
1213 (let ((members mchat-group-members)
1214 (now (mchat-current-time))
1216 (while (setq member (pop members))
1217 (when (> now (+ (cdr (assoc 'time-stamp member))
1218 (* mchat-heart-bit-delay mchat-zombie-latency)))
1219 ;; this guy must be dead
1220 (setq mchat-group-members
1221 (remassoc (car member) mchat-group-members))
1223 (mchat-with-face 'mchat-comment-face
1224 (insert-before-markers
1225 (concat (cdr (assoc 'tag member)) " seems to have died.\n"))))
1229 (defun mchat-create-group (name address)
1230 ;; Ensure the user id is created, create the MChat group NAME at address
1231 ;; ADDRESS, update mchat-groups, and return the group description.
1232 ;; #### NOTE: previously, the user id was created at load time which was
1233 ;; VERY BAD, because the user would not have the opportunity to correct a
1234 ;; bogus nsl-method.
1235 (unless mchat-user-id
1236 (or (setq mchat-user-id (mchat-user-id))
1237 (error "Can't build user id. Please check your mchat-nsl-method.")
1239 (let* ((bufname (concat "MChat (" name ")"))
1240 (proc (open-multicast-group bufname bufname address))
1241 (buf (get-buffer-create (concat "MChat message (" name ")")))
1246 ;; Group buffer setup
1247 (with-current-buffer bufname
1249 (setq mchat-group-name name)
1250 ;; #### NOTE: it's a bit silly, but sending the 'join message will
1251 ;; delete this timeout and reput it after :-/
1252 (setq mchat-heart-bit-timeout
1253 (add-timeout mchat-heart-bit-delay 'mchat-heart-bit proc
1254 mchat-heart-bit-delay))
1255 ;; I'm not sure this is really needed all the time, but in case the
1256 ;; menubar is not the default one, make sure we have the submenu.
1257 (and (featurep 'menubar) (add-submenu nil mchat-submenu))
1258 (make-local-hook 'kill-buffer-hook)
1259 (add-hook 'kill-buffer-hook 'mchat-kill-buffer-hook nil t))
1260 ;; Message buffer setup
1261 (with-current-buffer buf
1262 (apply (intern (concat (symbol-name mchat-message-buffer-major-mode)
1264 nil) ;; I'm not sure why this is needed.
1265 (setq mchat-group-name name)
1266 (mchat-minor-mode 1)
1267 (make-local-hook 'kill-buffer-hook)
1268 (add-hook 'kill-buffer-hook 'mchat-kill-buffer-hook nil t))
1270 (set-process-filter proc 'mchat-process-filter)
1271 (set-marker (process-mark proc) (point) (process-buffer proc))
1273 (append desc (list (cons 'zombie-check
1274 (add-timeout (* mchat-zombie-latency
1275 mchat-heart-bit-delay)
1277 (process-buffer proc)
1278 (* mchat-zombie-latency
1279 mchat-heart-bit-delay))))))
1281 (mchat-send-join proc)
1282 (setq mchat-groups (cons desc mchat-groups))
1285 ;; #### FIXME: remove this function. It seems to be used only once.
1286 (defun mchat-known-groups ()
1287 ;; Returns an alist of elements of the form (NAME ADDRESS) for ALL groups
1288 ;; known to MChat. This includes those in `mchat-registered-groups', and
1289 ;; those that are currently known but not registered. Since group names must
1290 ;; be unique, it's quicker to match the names only.
1291 (let ((grps mchat-groups) grp res)
1292 (while (setq grp (pop grps))
1293 (if (not (assoc (car grp) mchat-registered-groups))
1294 (setq res (cons (list (car grp) (cadr grp)) res))))
1295 (append res mchat-registered-groups)
1298 (defsubst mchat-all-interactive ()
1299 ;; We don't allow the same name for different groups, so if a known group
1300 ;; name is given, don't ask for the address. The completion occurs for both
1301 ;; predefined groups and groups that exist but are not registered.
1302 (let* ((grps (mchat-known-groups))
1303 (grpname (completing-read "Name: " grps))
1304 (grpaddr (or (cadr (assoc grpname grps))
1305 (read-string "Address: "))))
1306 (list grpname grpaddr)
1309 (defmacro mchat-sanity-check ()
1310 ;; For non interactive calls, check the validity of the given address, or
1311 ;; find it. Really, I'm too good to you, guys.
1312 `(unless (interactive-p)
1313 (let ((grp (assoc name mchat-registered-groups)))
1316 (setq address (cadr grp))
1317 (or (equal address (cadr grp))
1318 (error "Address doesn't not match group name!")))
1320 (error "No address given for unregistered group!"))))))
1322 (defmacro mchat-maybe-create-and-switch-to-group (&rest body)
1323 ;; Maybe create the group, execute body, and setup the frame
1324 `(let ((grp (assoc name mchat-groups)))
1326 (setq grp (mchat-create-group name address)))
1328 (mchat-switch-to-group grp)))
1331 (defun mchat (name &optional address)
1332 "Join MChat multicast group NAME at address ADDRESS. When called
1333 interactively, you will be prompted for the group name, and if the group is
1334 not registered, for the corresponding multicast address. Please refer to
1335 `mchat-predefined-groups' for a list of known groups, and
1336 `open-multicast-group' for the syntax of ADDRESS."
1337 (interactive (mchat-all-interactive))
1338 (mchat-sanity-check)
1339 (mchat-maybe-create-and-switch-to-group))
1342 (defun mchat-other-frame (name &optional address)
1343 "Like `mchat', but pop up a new frame."
1344 (interactive (mchat-all-interactive))
1345 (mchat-sanity-check)
1346 (mchat-maybe-create-and-switch-to-group
1347 (select-frame (make-frame))))
1351 (run-hooks 'mchat-loaded-hooks)
1353 ;;; mchat.el ends here