19034
|
1 ;;; mspools.el --- Show mail spools waiting to be read
|
17905
|
2
|
|
3 ;; Copyright (C) 1997 Stephen Eglen
|
|
4
|
18929
|
5 ;; Author: Stephen Eglen <stephen@cns.ed.ac.uk>
|
|
6 ;; Maintainer: Stephen Eglen <stephen@cns.ed.ac.uk>
|
17905
|
7 ;; Created: 22 Jan 1997
|
18929
|
8 ;; Keywords: mail
|
17905
|
9
|
|
10
|
|
11 ;; This program 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 2, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; This program 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; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; If you use a mail filter (e.g. procmail, filter) to put mail messages in
|
|
29 ;; folders, this file will let you see which folders have mail waiting
|
|
30 ;; to be read in them. It assumes that new mail for the file `folder'
|
|
31 ;; is written by the filter to a file called `folder.spool'. (If the
|
|
32 ;; file writes directly to `folder' you may lose mail if new mail
|
|
33 ;; arrives whilst you are reading the folder in emacs, hence the use
|
|
34 ;; of a spool file.) For example, the following procmail recipe puts
|
|
35 ;; any mail with `emacs' in the subject line into the spool file
|
|
36 ;; `apple.spool', ready to go into the folder `emacs'.
|
|
37 ;:0:
|
|
38 ;* ^Subject.*emacs
|
|
39 ;emacs.spool
|
|
40
|
|
41 ;; It also assumes that all of your spool files and mail folders live
|
|
42 ;; in the directory pointed to by `mspools-folder-directory', so you must
|
|
43 ;; set this (see Installation).
|
|
44
|
|
45 ;; When you run `mspools-show', it creates a *spools* buffer containing
|
|
46 ;; all of the spools in the folder directory that are waiting to be
|
|
47 ;; read. On each line is the spool name and its size in bytes. Move
|
|
48 ;; to the line of the folder that you would like to read, and then
|
|
49 ;; press return or space. The mailer (VM or RMAIL) should then read
|
|
50 ;; that folder and get the new mail for you. When you return to the
|
|
51 ;; *spools* buffer, you will either see "*" to indicate that the spool
|
|
52 ;; has been read, or the remaining unread spools, depending on the
|
|
53 ;; value of `mspools-update'.
|
|
54
|
|
55 ;; This file should work with both VM and RMAIL. See the variable
|
|
56 ;; `mspools-using-vm' for details.
|
|
57
|
|
58
|
|
59 ;;; Installation
|
|
60
|
|
61 ;; Basic
|
|
62 ;(autoload 'mspools-show "mspools" "Show outstanding mail spools." t)
|
|
63 ; Point to directory where spool files and folders are:
|
|
64 ; (setq mspools-folder-directory "~/MAIL/")
|
|
65
|
|
66 ;; Extras
|
|
67 ; possibly bind it to a key:
|
|
68 ;(global-set-key '[S-f1] 'mspools-show)
|
|
69 ;(setq mspools-update t)
|
|
70
|
|
71 ;; Interface with the mail filter
|
|
72 ; We assume that the mail filter drops new mail into the spool
|
|
73 ; `folder.spool'. If your spool files are something like folder.xyz
|
|
74 ; for inbox `folder', then do
|
|
75 ; (setq spool-suffix "xyz")
|
|
76 ; If you use other conventions for your spool files, this code will
|
|
77 ; need rewriting.
|
|
78
|
|
79 ;;; Warning for VM users
|
|
80 ;; Dont use if you are not sure what you are doing! The value of
|
|
81 ;; vm-spool-files is altered, so you may not be able to read incoming
|
|
82 ;; mail with VM if this is incorrectly set.
|
|
83
|
|
84 ;; Useful settings for VM
|
|
85 ;vm-auto-get-new-mail should be t (default t)
|
|
86
|
|
87 ;;; Acknowledgements
|
|
88 ;; The code for setting up vm-spool-files came from
|
|
89 ;;http://www-users.informatik.rwth-aachen.de/~berg/archive/procmail/0047.html
|
|
90 ;; Thanks to jond@mitre.org (Jonathan Doughty)
|
|
91
|
|
92 ;;; TODO
|
|
93
|
|
94 ;; What if users have mail spools in more than one directory? Extend
|
|
95 ;; mspools-folder-directory to be a list of files?
|
|
96
|
|
97 ;; I was going to add mouse support so that you could click on a line
|
|
98 ;; to visit the buffer. Tell me if you want it, and I can put the
|
|
99 ;; code in (I dont use the mouse much, so I havent bothered with it so
|
|
100 ;; far).
|
|
101
|
|
102
|
|
103 ;; Rather than showing size in bytes, could we see the number of msgs
|
|
104 ;; waiting? (Could be more time demanding / system dependent).
|
|
105 ;; Perl script counts the number of /^From / occurences.
|
|
106 ;; ?
|
|
107 ;; Include date
|
|
108 ;; (substring (current-time-string (nth 4 (file-attributes "~/INBOX"))) 4 19)
|
|
109 ;; Maybe just call a perl script to do all the hard work, and
|
|
110 ;; visualise the results in the buffer.
|
|
111
|
|
112 ;; Shrink wrap the buffer to remove excess white-space?
|
|
113
|
|
114
|
|
115 ;;; User Variables
|
|
116
|
|
117
|
|
118 (defvar mspools-update nil
|
|
119 "*Non-nil means update *spools* buffer after visiting any folder.")
|
|
120
|
|
121 (defvar mspools-suffix "spool"
|
|
122 "*Extension used for spool files (not including full stop).")
|
|
123
|
|
124 ;;; Internal Variables
|
|
125
|
|
126 (defvar mspools-vm-system-mail (getenv "MAIL")
|
|
127 "Main mailbox used. Only used by VM.")
|
|
128
|
|
129 (defvar mspools-vm-system-mail-crash
|
|
130 (concat mspools-vm-system-mail ".crash")
|
|
131 "Crash box for main mailbox. See also `mspools-vm-system-mail'.
|
|
132 Only used by VM." )
|
|
133
|
|
134
|
|
135 (defvar mspools-files nil
|
|
136 "List of entries (SPOOL . SIZE) giving spool name and file size.")
|
|
137
|
|
138 (defvar mspools-files-len nil
|
|
139 "Length of `mspools-files' list.")
|
|
140
|
|
141 (defvar mspools-buffer "*spools*"
|
|
142 "Name of buffer for displaying spool info.")
|
|
143
|
|
144 (defvar mspools-mode-map nil
|
|
145 "Keymap for the *spools* buffer.")
|
|
146
|
|
147 (defvar mspools-folder-directory
|
|
148 (if (boundp 'vm-folder-directory)
|
|
149 vm-folder-directory
|
|
150 nil)
|
|
151 "Directory where mail folders are kept. Defaults to
|
|
152 `vm-folder-directory' if bound else nil. Make sure it has a trailing /
|
|
153 at the end. ")
|
|
154
|
|
155
|
|
156 (defvar mspools-using-vm
|
|
157 (fboundp 'vm)
|
|
158 "*Non-nil if VM is used as mail reader, otherwise RMAIL is used.")
|
|
159
|
|
160
|
|
161 ;;; Code
|
|
162
|
|
163 ;;; VM Specific code
|
|
164 (if mspools-using-vm
|
|
165 (require 'vm-vars))
|
|
166
|
|
167 (defun mspools-set-vm-spool-files ()
|
|
168 "Set value of `vm-spool-files'. Only needed for VM."
|
|
169 (setq
|
|
170 vm-spool-files
|
|
171 (append
|
|
172 (list
|
|
173 ;; Main mailbox
|
|
174 (list vm-primary-inbox
|
|
175 mspools-vm-system-mail; your mailbox
|
|
176 mspools-vm-system-mail-crash ; crash for mailbox
|
|
177 ))
|
|
178
|
|
179 ;; Mailing list inboxes
|
|
180 (mapcar '(lambda (s)
|
|
181 "make the appropriate entry for vm-spool-files"
|
|
182 (list
|
|
183 (concat vm-folder-directory s)
|
|
184 (concat vm-folder-directory s "." mspools-suffix)
|
|
185 (concat vm-folder-directory s ".crash")))
|
|
186 ;; So I create a vm-spool-files entry for each of those mail drops
|
|
187 (mapcar 'file-name-sans-extension
|
|
188 (directory-files vm-folder-directory nil
|
|
189 (format "^[^.]+\\.%s" mspools-suffix)))
|
|
190 ))
|
|
191 ))
|
|
192
|
|
193
|
|
194
|
|
195 ;;; MSPOOLS-SHOW -- the main function
|
|
196 (defun mspools-show ( &optional noshow)
|
|
197 "Show the list of non-empty spool files in the *spools* buffer.
|
|
198 Buffer is not displayed if SHOW is non-nil."
|
|
199 (interactive)
|
|
200 (if (get-buffer mspools-buffer)
|
|
201 ;; buffer exists
|
|
202 (progn
|
|
203 (set-buffer mspools-buffer)
|
|
204 (setq buffer-read-only nil)
|
|
205 (delete-region (point-min) (point-max)))
|
|
206 ;; else buff. doesnt exist so create it
|
|
207 (get-buffer-create mspools-buffer))
|
|
208
|
|
209 ;; generate the list of spool files
|
|
210 (if mspools-using-vm
|
|
211 (mspools-set-vm-spool-files))
|
|
212
|
|
213 (mspools-get-spool-files)
|
|
214 (if (not noshow) (pop-to-buffer mspools-buffer))
|
|
215
|
|
216 (setq buffer-read-only t)
|
|
217 (mspools-mode)
|
|
218 )
|
|
219
|
|
220
|
|
221
|
|
222
|
|
223 (defun mspools-visit-spool ()
|
|
224 "Visit the folder on the current line of the *spools* buffer."
|
|
225 (interactive)
|
|
226 (let ( spool-name folder-name)
|
|
227 (setq spool-name (mspools-get-spool-name))
|
|
228 (setq folder-name (mspools-get-folder-from-spool spool-name))
|
|
229
|
|
230 ;; put in a little "*" to indicate spool file has been read.
|
|
231 (if (not mspools-update)
|
|
232 (save-excursion
|
|
233 (setq buffer-read-only nil)
|
|
234 (beginning-of-line)
|
|
235 (insert "*")
|
|
236 (delete-char 1)
|
|
237 (setq buffer-read-only t)
|
|
238 ))
|
|
239
|
|
240
|
|
241 (message "folder %s spool %s" folder-name spool-name)
|
|
242 (if (eq (count-lines (point-min)
|
|
243 (save-excursion
|
|
244 (end-of-line)
|
|
245 (point)))
|
|
246 mspools-files-len)
|
|
247 (next-line (- 1 mspools-files-len)) ;back to top of list
|
|
248 ;; else just on to next line
|
|
249 (next-line 1))
|
|
250
|
|
251 ;; Choose whether to use VM or RMAIL for reading folder.
|
|
252 (if mspools-using-vm
|
|
253 (vm-visit-folder (concat mspools-folder-directory folder-name))
|
|
254 ;; else using RMAIL
|
|
255 (rmail (concat mspools-folder-directory folder-name))
|
|
256 (setq rmail-inbox-list
|
|
257 (list (concat mspools-folder-directory spool-name)))
|
|
258 (rmail-get-new-mail))
|
|
259
|
|
260
|
|
261 (if mspools-update
|
|
262 ;; generate new list of spools.
|
|
263 (save-excursion
|
|
264 (mspools-show-again 'noshow)))
|
|
265 ))
|
|
266
|
|
267
|
|
268
|
|
269
|
|
270 (defun mspools-get-folder-from-spool (name)
|
|
271 "Return folder name corresponding to the spool file NAME."
|
|
272 ;; Simply strip of the extension.
|
|
273 (file-name-sans-extension name))
|
|
274
|
|
275 ;; Alternative version if you have more complicated mapping of spool name
|
|
276 ;; to file name.
|
|
277 ;(defun get-folder-from-spool-safe (name)
|
|
278 ; "Return the folder name corresponding to the spool file NAME."
|
|
279 ; (if (string-match "^\\(.*\\)\.spool$" name)
|
|
280 ; (substring name (match-beginning 1) (match-end 1))
|
|
281 ; (error "Could not extract folder name from spool name %s" name)))
|
|
282
|
|
283 ; test
|
|
284 ;(mspools-get-folder-from-spool "happy.spool")
|
|
285 ;(mspools-get-folder-from-spool "happy.sp")
|
|
286
|
|
287
|
|
288
|
|
289 (defun mspools-get-spool-name ()
|
|
290 "Return the name of the spool on the current line."
|
|
291 (let ((line-num (1- (count-lines (point-min)
|
|
292 (save-excursion
|
|
293 (end-of-line)
|
|
294 (point))
|
|
295 ))))
|
|
296 (car (nth line-num mspools-files))))
|
|
297
|
|
298 ;;; Keymap
|
|
299
|
|
300 (if mspools-mode-map
|
|
301 ()
|
|
302 (setq mspools-mode-map (make-sparse-keymap))
|
|
303
|
|
304 (define-key mspools-mode-map "\C-c\C-c" 'mspools-visit-spool)
|
|
305 (define-key mspools-mode-map "\C-m" 'mspools-visit-spool)
|
|
306 (define-key mspools-mode-map " " 'mspools-visit-spool)
|
|
307 (define-key mspools-mode-map "?" 'mspools-help)
|
|
308 (define-key mspools-mode-map "q" 'mspools-quit)
|
|
309 (define-key mspools-mode-map "g" 'revert-buffer))
|
|
310
|
|
311
|
|
312 ;;; Spools mode functions
|
|
313
|
|
314 (defun mspools-revert-buffer (ignore noconfirm)
|
|
315 "Re-run mspools-show to revert the *spools* buffer."
|
|
316 (mspools-show 'noshow))
|
|
317
|
|
318 (defun mspools-show-again (&optional noshow)
|
|
319 "Update the *spools* buffer. This is useful if mspools-update is
|
|
320 nil."
|
|
321 (interactive)
|
|
322 (mspools-show noshow))
|
|
323
|
|
324 (defun mspools-help ()
|
|
325 "Show help for `mspools-mode'."
|
|
326 (interactive)
|
|
327 (describe-function 'mspools-mode))
|
|
328
|
|
329 (defun mspools-quit ()
|
|
330 "Quit the *spools* buffer."
|
|
331 (interactive)
|
|
332 (kill-buffer mspools-buffer))
|
|
333
|
|
334
|
|
335 (defun mspools-mode ()
|
|
336 "Major mode for output from mspools-show.
|
|
337 \\<mspools-mode-map>Move point to one of the items in this buffer, then use
|
|
338 \\[mspools-visit-spool] to go to the spool that the current line refers to.
|
|
339 \\[mspools-show-again] to regenerate the list of spools.
|
|
340 \\{mspools-mode-map}"
|
|
341 (kill-all-local-variables)
|
|
342 (make-local-variable 'revert-buffer-function)
|
|
343 (setq revert-buffer-function 'mspools-revert-buffer)
|
|
344 (use-local-map mspools-mode-map)
|
|
345 (setq major-mode 'mspools-mode)
|
|
346 (setq mode-name "MSpools")
|
|
347 )
|
|
348
|
|
349
|
|
350 (defun mspools-get-spool-files ()
|
|
351 "Find the list of spool files and display them in *spools* buffer."
|
|
352 (let (folders head spool len beg end any)
|
|
353 (setq folders (directory-files mspools-folder-directory nil
|
|
354 (format "^[^.]+\\.%s" mspools-suffix)))
|
|
355
|
|
356
|
|
357 (setq folders (mapcar 'mspools-size-folder folders))
|
|
358 (setq folders (delq nil folders))
|
|
359 (setq mspools-files folders)
|
|
360 (setq mspools-files-len (length mspools-files))
|
|
361 (set-buffer mspools-buffer)
|
|
362 (while folders
|
|
363 (setq any t)
|
|
364 (setq head (car folders))
|
|
365 (setq spool (car head))
|
|
366 (setq len (cdr head))
|
|
367 (setq folders (cdr folders))
|
|
368 (setq beg (point))
|
|
369 (insert (format " %10d %s" len spool))
|
|
370 (setq end (point))
|
|
371 (insert "\n")
|
|
372 ;;(put-text-property beg end 'mouse-face 'highlight)
|
|
373 )
|
|
374 (if any
|
|
375 (delete-char -1)) ;delete last RET
|
|
376 (goto-char (point-min))
|
|
377 ))
|
|
378
|
|
379
|
|
380
|
|
381 (defun mspools-size-folder (spool)
|
|
382 "Return (SPOOL . SIZE ) iff SIZE of spool file is non-zero."
|
|
383 ;; 7th file attribute is the size of the file in bytes.
|
|
384 (let ((size (nth 7
|
|
385 (file-attributes (concat mspools-folder-directory spool)))))
|
|
386 ;; todo (if (and (not (null size)) (> size 0))
|
|
387 (if (> size 0)
|
|
388 (cons spool size)
|
|
389 ;; else SPOOL is empty
|
|
390 nil)))
|
|
391
|
|
392 (provide 'mspools)
|
|
393 ;;; MSPOOLS.EL ends here
|