home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / msgs-check.el < prev    next >
Encoding:
Text File  |  1991-03-30  |  7.2 KB  |  191 lines

  1. ; Path: utkcs2!emory!samsung!usc!rutgers!mcnc!xanth!talos!kjones
  2. ; >From: elves@magic-tree.keebler.com (Those Keebler Elves)
  3. ; Newsgroups: comp.emacs
  4. ; Subject: support for Berkeley msgs(1) under GNU Emacs
  5. ; Date: 25 Jul 90 18:42:28 GMT
  6. ; The following file of Lisp code provides just enough support for
  7. ; the Berkeley msgs(1) bulletin system so that you can use existing
  8. ; mail tools under Emacs to read them.  Installation instructions
  9. ; are in the comments at the top of the file.
  10. ; Once the package is installed, "M-x msgs-check", or (msgs-check)
  11. ; msgs(1) system, and display an indicator in the mode line if any
  12. ; such messages are found.
  13. ; "M-x msgs-gobble-messages", or (setq msgs-auto-gobble-messages t)
  14. ; found in the msgs(1) system to the folder specified by the
  15. ; variable msgs-folder (default ~/MSGS).  ~/.msgsrc is updated to
  16. ; mark these messages as read.
  17. ; The spool checker is driver by an interval timer, so my timer
  18. ; package must be installed for msgs-check to work.  The rest of
  19. ; the package can live without timers.  You can get the timer
  20. ; package from the OSU Emacs-Lisp archives or directly from me.
  21. ; kyle jones   <kjones@talos.pm.com>   ...!uunet!talos!kjones
  22. ; ----------------------------------------------------------------------
  23. ;;; Check for and/or gather new messages in the msgs(1) bulletin system.
  24. ;;; Copyright (C) 1990 Kyle E. Jones
  25. ;;;
  26. ;;; This program is free software; you can redistribute it and/or modify
  27. ;;; it under the terms of the GNU General Public License as published by
  28. ;;; the Free Software Foundation; either version 1, or (at your option)
  29. ;;; any later version.
  30. ;;;
  31. ;;; This program is distributed in the hope that it will be useful,
  32. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34. ;;; GNU General Public License for more details.
  35. ;;;
  36. ;;; A copy of the GNU General Public License can be obtained from this
  37. ;;; program's author (send electronic mail to kyle@cs.odu.edu) or from
  38. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  39. ;;; 02139, USA.
  40. ;;;
  41. ;;; Send bug reports to kyle@cs.odu.edu.
  42.  
  43. ;; Save this package in a file named msgschk.el in a directory
  44. ;; that Emacs will search for Lisp libraries.  Byte-compile it.
  45. ;;
  46. ;; This package is autoloadable.  Use
  47. ;;    (autoload 'msgs-check "msgschk" nil t)
  48. ;;    (autoload 'msgs-gobble-messages "msgschk" nil t)
  49. ;; in your .emacs file.
  50. ;;
  51. ;; Once the package is installed, "M-x msgs-check", or
  52. ;; (msgs-check) from the .emacs will cause Emacs to check for new
  53. ;; messages in the msgs(1) system, and display an indicator in
  54. ;; the mode line if any such messages are found.
  55. ;;
  56. ;; "M-x msgs-gobble-messages", or (setq msgs-auto-gobble-messages t)
  57. ;; in the .emacs will cause emacs to append any new messages found
  58. ;; to the folder specified by the variable msgs-folder (default
  59. ;; ~/MSGS).  ~/.msgsrc is updated to mark these messages as read.
  60.  
  61. (require 'timer)
  62.  
  63. (provide 'msgs-check)
  64.  
  65. (defvar msgs-directory "/usr/msgs"
  66.   "*Directory where msgs(1) messages are spooled.")
  67.  
  68. (defvar msgs-rc "~/.msgsrc"
  69.   "*File that keeps track of what msgs(1) message you've read.")
  70.  
  71. (defvar msgs-check-interval 300
  72.   "*Number of seconds between checks for new messages.")
  73.  
  74. (defvar msgs-folder "~/MSGS"
  75.   "*Folder where message gathered from the msgs(1) system are stored.")
  76.  
  77. (defvar msgs-auto-gobble-messages nil
  78.   "*Non-nil value causes the msgs checker to automatically append any new
  79. messages posted to the msgs(1) bulletin board into the folder specified
  80. by the variable msgs-folder.  Your .msgsrc is updated to indicate
  81. that these new messages have been read.")
  82.  
  83. (defvar msgs-unread-messages-string nil)
  84.  
  85. (defun msgs-check ()
  86.   "Periodically check for new messages (see msgs(1)).
  87. If there are unread message, an indicator will apear in the mode
  88. line.  The variable msgs-check-interval controls how often the
  89. spool is checked for new messages."
  90.   (interactive)
  91.   ;; sanity check msgs-check-interval
  92.   (if (< msgs-check-interval 5)
  93.       (setq msgs-check-interval 5))
  94.   ;; install the display string
  95.   (or global-mode-string
  96.       (setq global-mode-string '("")))
  97.   (or (memq 'msgs-unread-messages-string global-mode-string)
  98.       (setq global-mode-string
  99.         (append global-mode-string '(msgs-unread-messages-string))))
  100.   ;; if the timer already exists, destroy it.
  101.   (and (get-timer "msgs-check") (delete-timer "msgs-check"))
  102.   ;; schedule the check function to be run immediately,
  103.   ;; then every msgs-check-interval seconds thereafter.
  104.   (start-timer "msgs-check" 'msgs-check-function 1 msgs-check-interval))
  105.  
  106. (defun msgs-check-function ()
  107.   (setq msgs-unread-messages-string nil)
  108.   (cond ((msgs-unread-messages-p)
  109.      (setq msgs-unread-messages-string " [new msgs] ")
  110.      (and msgs-auto-gobble-messages msgs-folder (msgs-gobble-messages)))
  111.     ((and msgs-folder (file-exists-p msgs-folder)
  112.           (not (zerop (nth 7 (file-attributes msgs-folder)))))
  113.      (setq msgs-unread-messages-string " [msgs] ")))
  114.   ;; Force mode line updates
  115.   (save-excursion (set-buffer (other-buffer)))
  116.   (set-buffer-modified-p (buffer-modified-p))
  117.   (sit-for 0))
  118.  
  119. (defun msgs-unread-messages-p ()
  120.   (let (last-msg-read min-msg max-msg)
  121.     (save-excursion
  122.       (set-buffer (get-buffer-create " *msgs check*"))
  123.       (erase-buffer)
  124.       (condition-case ()
  125.       (insert-file-contents msgs-rc)
  126.     (error (insert "0\n")))
  127.       (goto-char (point-max))
  128.       (condition-case ()
  129.       (insert-file-contents (concat msgs-directory "/bounds"))
  130.     (error (insert "0 0\n")))
  131.       (goto-char (point-min))
  132.       (setq last-msg-read (1- (msgs-read-number))
  133.         min-msg (msgs-read-number)
  134.         max-msg (msgs-read-number)))
  135.     (< last-msg-read max-msg)))
  136.  
  137. (defun msgs-gobble-messages ()
  138.   "Append any new messsages from msgs(1) to the file specifed by msgs-folder.
  139. This file should be considered a spool area.  If you're using VM or RMAIL
  140. to read mail under Emacs, it is advised that you not visit this file directly,
  141. Rather, you should set the appropriate mailer variables so that
  142. you mail reader will look for messages in this file in addition
  143. to your normal system mailbox."
  144.   (interactive)
  145.   (let (last-msg-read min-msg max-msg)
  146.     (save-excursion
  147.       (set-buffer (get-buffer-create " *msgs check*"))
  148.       (erase-buffer)
  149.       (condition-case ()
  150.       (insert-file-contents msgs-rc)
  151.     (error (insert "0\n")))
  152.       (goto-char (point-max))
  153.       (condition-case ()
  154.       (insert-file-contents (concat msgs-directory "/bounds"))
  155.     (error (insert "0 0\n")))
  156.       (goto-char (point-min))
  157.       (setq last-msg-read (1- (msgs-read-number))
  158.         min-msg (msgs-read-number)
  159.         max-msg (msgs-read-number))
  160.       (if (< last-msg-read max-msg)
  161.       (progn
  162.         (erase-buffer)
  163.         (goto-char (point-max))
  164.         (while (<= last-msg-read max-msg)
  165.           (setq last-msg-read (1+ last-msg-read))
  166.           (condition-case ()
  167.           (progn
  168.             (insert-file-contents
  169.              (concat msgs-directory "/" (int-to-string last-msg-read)))
  170.             (goto-char (point-max))
  171.             (insert "\n"))
  172.         (file-error nil)))
  173.         (write-region (point-min) (point-max) msgs-folder t 0)
  174.         (let (start)
  175.           (setq start (point))
  176.           (insert (int-to-string last-msg-read) "\n")
  177.           (write-region start (point) msgs-rc nil 0)))))))
  178.  
  179. (defun msgs-read-number ()
  180.   (condition-case ()
  181.       (read (current-buffer))
  182.     (error 0)))
  183.  
  184.  
  185.