*** empty log message ***
[gnus] / lisp / nnmh.el
1 ;;; nnmh.el --- mhspool access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
28 ;; For an overview of what the interface functions do, please see the
29 ;; Gnus sources.  
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'rmail)
35 (require 'nnmail)
36 (require 'gnus)
37 (require 'nnoo)
38 (eval-and-compile (require 'cl))
39
40 (nnoo-declare nnmh)
41
42 (defvoo nnmh-directory "~/Mail/"
43   "*Mail spool directory.")
44
45 (defvoo nnmh-get-new-mail t
46   "*If non-nil, nnmh will check the incoming mail file and split the mail.")
47
48 (defvoo nnmh-prepare-save-mail-hook nil
49   "*Hook run narrowed to an article before saving.")
50
51 (defvoo nnmh-be-safe nil
52   "*If non-nil, nnmh will check all articles to make sure whether they are new or not.")
53
54 \f
55
56 (defconst nnmh-version "nnmh 1.0"
57   "nnmh version.")
58
59 (defvoo nnmh-current-directory nil
60   "Current news group directory.")
61
62 (defvoo nnmh-status-string "")
63 (defvoo nnmh-group-alist nil)
64
65 \f
66
67 ;;; Interface functions.
68
69 (nnoo-define-basics nnmh)
70
71 (deffoo nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
72   (save-excursion
73     (set-buffer nntp-server-buffer)
74     (erase-buffer)
75     (let* ((file nil)
76            (number (length articles))
77            (large (and (numberp nnmail-large-newsgroup)
78                        (> number nnmail-large-newsgroup)))
79            (count 0)
80            beg article)
81       (nnmh-possibly-change-directory newsgroup server)
82       ;; We don't support fetching by Message-ID.
83       (if (stringp (car articles))
84           'headers
85         (while articles
86           (when (and (file-exists-p 
87                       (setq file (concat (file-name-as-directory 
88                                           nnmh-current-directory)
89                                          (int-to-string
90                                           (setq article (pop articles))))))
91
92                      (not (file-directory-p file)))
93             (insert (format "221 %d Article retrieved.\n" article))
94             (setq beg (point))
95             (nnheader-insert-head file)
96             (goto-char beg)
97             (if (search-forward "\n\n" nil t)
98                 (forward-char -1)
99               (goto-char (point-max))
100               (insert "\n\n"))
101             (insert ".\n")
102             (delete-region (point) (point-max)))
103           (setq count (1+ count))
104
105           (and large
106                (zerop (% count 20))
107                (message "nnmh: Receiving headers... %d%%"
108                         (/ (* count 100) number))))
109
110         (and large (message "nnmh: Receiving headers...done"))
111
112         (nnheader-fold-continuation-lines)
113         'headers))))
114
115 (deffoo nnmh-open-server (server &optional defs)
116   (nnoo-change-server 'nnmh server defs)
117   (when (not (file-exists-p nnmh-directory))
118     (condition-case ()
119         (make-directory nnmh-directory t)
120       (error t)))
121   (cond 
122    ((not (file-exists-p nnmh-directory))
123     (nnmh-close-server)
124     (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
125    ((not (file-directory-p (file-truename nnmh-directory)))
126     (nnmh-close-server)
127     (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
128    (t
129     (nnheader-report 'nnmh "Opened server %s using directory %s"
130                      server nnmh-directory)
131     t)))
132
133 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
134   (nnmh-possibly-change-directory newsgroup server)
135   (let ((file (if (stringp id)
136                   nil
137                 (concat nnmh-current-directory (int-to-string id))))
138         (nntp-server-buffer (or buffer nntp-server-buffer)))
139     (and (stringp file)
140          (file-exists-p file)
141          (not (file-directory-p file))
142          (save-excursion (nnmail-find-file file))
143          (string-to-int (file-name-nondirectory file)))))
144
145 (deffoo nnmh-request-group (group &optional server dont-check)
146   (let ((pathname (nnmail-group-pathname group nnmh-directory))
147         dir)
148     (cond 
149      ((not (file-directory-p pathname))
150       (nnheader-report 
151        'nnmh "Can't select group (no such directory): %s" group))
152      (t
153       (setq nnmh-current-directory pathname)
154       (and nnmh-get-new-mail 
155            nnmh-be-safe
156            (nnmh-update-gnus-unreads group))
157       (cond
158        (dont-check
159         (nnheader-report 'nnmh "Selected group %s" group)
160         t)
161        (t
162         (setq dir 
163               (sort
164                (mapcar (lambda (name) (string-to-int name))
165                        (directory-files pathname nil "^[0-9]+$" t))
166                '<))
167           (cond 
168            (dir
169             (nnheader-report 'nnmh "Selected group %s" group)
170             (nnheader-insert
171              "211 %d %d %d %s\n" (length dir) (car dir)
172              (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
173              group))
174            (t
175             (nnheader-report 'nnmh "Empty group %s" group)
176             (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
177
178 (deffoo nnmh-request-scan (&optional group server)
179   (nnmail-get-new-mail 'nnmh nil nnmh-directory group))      
180
181 (deffoo nnmh-request-list (&optional server dir)
182   (nnheader-insert "")
183   (let ((nnmh-toplev
184          (or dir (file-truename (file-name-as-directory nnmh-directory)))))
185     (nnmh-request-list-1 nnmh-toplev))
186   (setq nnmh-group-alist (nnmail-get-active))
187   t)
188
189 (defvar nnmh-toplev)
190 (defun nnmh-request-list-1 (dir)
191   (setq dir (expand-file-name dir))
192   ;; Recurse down all directories.
193   (let ((dirs (and (file-readable-p dir)
194                    (> (nth 1 (file-attributes (file-chase-links dir))) 2)
195                    (directory-files dir t nil t)))
196         dir)
197     ;; Recurse down directories.
198     (while (setq dir (pop dirs))
199       (when (and (not (string-match "/\\.\\.?$" dir))
200                  (file-directory-p dir)
201                  (file-readable-p dir))
202         (nnmh-request-list-1 dir))))
203   ;; For each directory, generate an active file line.
204   (unless (string= (expand-file-name nnmh-toplev) dir)
205     (let ((files (mapcar
206                   (lambda (name) (string-to-int name))
207                   (directory-files dir nil "^[0-9]+$" t))))
208       (when files
209         (save-excursion
210           (set-buffer nntp-server-buffer)
211           (goto-char (point-max))
212           (insert 
213            (format 
214             "%s %d %d y\n" 
215             (progn
216               (string-match 
217                (file-truename (file-name-as-directory 
218                                (expand-file-name nnmh-toplev))) dir)
219               (nnheader-replace-chars-in-string
220                (substring dir (match-end 0)) ?/ ?.))
221             (apply (function max) files) 
222             (apply (function min) files)))))))
223   t)
224
225 (deffoo nnmh-request-newgroups (date &optional server)
226   (nnmh-request-list server))
227
228 (deffoo nnmh-request-expire-articles (articles newsgroup &optional server force)
229   (nnmh-possibly-change-directory newsgroup server)
230   (let* ((active-articles 
231           (mapcar
232            (function
233             (lambda (name)
234               (string-to-int name)))
235            (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
236          (is-old t)
237          article rest mod-time)
238     (nnmail-activate 'nnmh)
239
240     (while (and articles is-old)
241       (setq article (concat nnmh-current-directory 
242                             (int-to-string (car articles))))
243       (if (setq mod-time (nth 5 (file-attributes article)))
244           (if (and (nnmh-deletable-article-p newsgroup (car articles))
245                    (setq is-old
246                          (nnmail-expired-article-p newsgroup mod-time force)))
247               (progn
248                 (nnheader-message 5 "Deleting article %s in %s..." 
249                                   article newsgroup)
250                 (condition-case ()
251                     (funcall nnmail-delete-file-function article)
252                   (file-error
253                    (setq rest (cons (car articles) rest)))))
254             (setq rest (cons (car articles) rest))))
255       (setq articles (cdr articles)))
256     (message "")
257     (nconc rest articles)))
258
259 (deffoo nnmh-close-group (group &optional server)
260   t)
261
262 (deffoo nnmh-request-move-article 
263   (article group server accept-form &optional last)
264   (let ((buf (get-buffer-create " *nnmh move*"))
265         result)
266     (and 
267      (nnmh-deletable-article-p group article)
268      (nnmh-request-article article group server)
269      (save-excursion
270        (set-buffer buf)
271        (insert-buffer-substring nntp-server-buffer)
272        (setq result (eval accept-form))
273        (kill-buffer (current-buffer))
274        result)
275      (condition-case ()
276          (funcall nnmail-delete-file-function
277                   (concat nnmh-current-directory (int-to-string article)))
278        (file-error nil)))
279     result))
280
281 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
282   (nnmh-possibly-change-directory group server)
283   (if (stringp group)
284       (and 
285        (nnmail-activate 'nnmh)
286        ;; We trick the choosing function into believing that only one
287        ;; group is available.  
288        (let ((nnmail-split-methods (list (list group ""))))
289          (car (nnmh-save-mail noinsert))))
290     (and
291      (nnmail-activate 'nnmh)
292      (car (nnmh-save-mail noinsert)))))
293
294 (deffoo nnmh-request-replace-article (article group buffer)
295   (nnmh-possibly-change-directory group)
296   (save-excursion
297     (set-buffer buffer)
298     (nnmh-possibly-create-directory group)
299     (condition-case ()
300         (progn
301           (write-region (point-min) (point-max)
302                         (concat nnmh-current-directory (int-to-string article))
303                         nil (if (nnheader-be-verbose 5) nil 'nomesg))
304           t)
305       (error nil))))
306
307 (deffoo nnmh-request-create-group (group &optional server) 
308   (nnmail-activate 'nnmh)
309   (or (assoc group nnmh-group-alist)
310       (let (active)
311         (setq nnmh-group-alist (cons (list group (setq active (cons 1 0)))
312                                      nnmh-group-alist))
313         (nnmh-possibly-create-directory group)
314         (nnmh-possibly-change-directory group server)
315         (let ((articles (mapcar
316                          (lambda (file)
317                            (string-to-int file))
318                          (directory-files 
319                           nnmh-current-directory nil "^[0-9]+$"))))
320           (and articles
321                (progn
322                  (setcar active (apply 'min articles))
323                  (setcdr active (apply 'max articles)))))))
324   t)
325
326 (deffoo nnmh-request-delete-group (group &optional force server)
327   (nnmh-possibly-change-directory group server)
328   ;; Delete all articles in GROUP.
329   (if (not force)
330       ()                                ; Don't delete the articles.
331     (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
332       (while articles 
333         (and (file-writable-p (car articles))
334              (progn
335                (nnheader-message 5 "Deleting article %s in %s..."
336                                  (car articles) group)
337                (funcall nnmail-delete-file-function (car articles))))
338         (setq articles (cdr articles))))
339     ;; Try to delete the directory itself.
340     (condition-case ()
341         (delete-directory nnmh-current-directory)
342       (error nil)))
343   ;; Remove the group from all structures.
344   (setq nnmh-group-alist 
345         (delq (assoc group nnmh-group-alist) nnmh-group-alist)
346         nnmh-current-directory nil)
347   t)
348
349 (deffoo nnmh-request-rename-group (group new-name &optional server)
350   (nnmh-possibly-change-directory group server)
351   ;; Rename directory.
352   (and (file-writable-p nnmh-current-directory)
353        (condition-case ()
354            (progn
355              (rename-file 
356               (directory-file-name nnmh-current-directory)
357               (directory-file-name 
358                (nnmail-group-pathname new-name nnmh-directory)))
359              t)
360          (error nil))
361        ;; That went ok, so we change the internal structures.
362        (let ((entry (assoc group nnmh-group-alist)))
363          (and entry (setcar entry new-name))
364          (setq nnmh-current-directory nil)
365          t)))
366
367 \f
368 ;;; Internal functions.
369
370 (defun nnmh-possibly-change-directory (newsgroup &optional server)
371   (when (and server 
372              (not (nnmh-server-opened server)))
373     (nnmh-open-server server))
374   (if newsgroup
375       (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
376         (if (file-directory-p pathname)
377             (setq nnmh-current-directory pathname)
378           (error "No such newsgroup: %s" newsgroup)))))
379
380 (defun nnmh-possibly-create-directory (group)
381   (let (dir dirs)
382     (setq dir (nnmail-group-pathname group nnmh-directory))
383     (while (not (file-directory-p dir))
384       (setq dirs (cons dir dirs))
385       (setq dir (file-name-directory (directory-file-name dir))))
386     (while dirs
387       (if (make-directory (directory-file-name (car dirs)))
388           (error "Could not create directory %s" (car dirs)))
389       (nnheader-message 5 "Creating mail directory %s" (car dirs))
390       (setq dirs (cdr dirs)))))
391              
392 (defun nnmh-save-mail (&optional noinsert)
393   "Called narrowed to an article."
394   (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
395     (unless noinsert
396       (nnmail-insert-lines)
397       (nnmail-insert-xref group-art))
398     (run-hooks 'nnmh-prepare-save-mail-hook)
399     (goto-char (point-min))
400     (while (looking-at "From ")
401       (replace-match "X-From-Line: ")
402       (forward-line 1))
403     ;; We save the article in all the newsgroups it belongs in.
404     (let ((ga group-art)
405           first)
406       (while ga
407         (nnmh-possibly-create-directory (caar ga))
408         (let ((file (concat (nnmail-group-pathname 
409                              (caar ga) nnmh-directory) 
410                             (int-to-string (cdar ga)))))
411           (if first
412               ;; It was already saved, so we just make a hard link.
413               (funcall nnmail-crosspost-link-function first file t)
414             ;; Save the article.
415             (write-region (point-min) (point-max) file nil nil)
416             (setq first file)))
417         (setq ga (cdr ga))))
418     group-art))
419
420 (defun nnmh-active-number (group)
421   "Compute the next article number in GROUP."
422   (let ((active (cadr (assoc group nnmh-group-alist))))
423     ;; The group wasn't known to nnmh, so we just create an active
424     ;; entry for it.   
425     (or active
426         (progn
427           (setq active (cons 1 0))
428           (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
429     (setcdr active (1+ (cdr active)))
430     (while (file-exists-p
431             (concat (nnmail-group-pathname group nnmh-directory)
432                     (int-to-string (cdr active))))
433       (setcdr active (1+ (cdr active))))
434     (cdr active)))
435
436 (defun nnmh-update-gnus-unreads (group)
437   ;; Go through the .nnmh-articles file and compare with the actual
438   ;; articles in this folder. The articles that are "new" will be
439   ;; marked as unread by Gnus.
440   (let* ((dir nnmh-current-directory)
441          (files (sort (mapcar (function (lambda (name) (string-to-int name)))
442                               (directory-files nnmh-current-directory 
443                                                nil "^[0-9]+$" t)) '<))
444          (nnmh-file (concat dir ".nnmh-articles"))
445          new articles)
446     ;; Load the .nnmh-articles file.
447     (if (file-exists-p nnmh-file)
448         (setq articles 
449               (let (nnmh-newsgroup-articles)
450                 (condition-case nil (load nnmh-file nil t t) (error nil))
451                 nnmh-newsgroup-articles)))
452     ;; Add all new articles to the `new' list.
453     (let ((art files))
454       (while art
455         (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
456         (setq art (cdr art))))
457     ;; Remove all deleted articles.
458     (let ((art articles))
459       (while art
460         (if (not (memq (caar art) files))
461             (setq articles (delq (car art) articles)))
462         (setq art (cdr art))))
463     ;; Check whether the highest-numbered articles really are the ones
464     ;; that Gnus thinks they are by looking at the time-stamps.
465     (let ((art articles))
466       (while (and art 
467                   (not (equal 
468                         (nth 5 (file-attributes 
469                                 (concat dir (int-to-string (caar art)))))
470                         (cdar art))))
471         (setq articles (delq (car art) articles))
472         (setq new (cons (caar art) new))
473         (setq art (cdr art))))
474     ;; Go through all the new articles and add them, and their
475     ;; time-stamps to the list.
476     (let ((n new))
477       (while n
478         (setq articles 
479               (cons (cons 
480                      (car n)
481                      (nth 5 (file-attributes 
482                              (concat dir (int-to-string (car n))))))
483                     articles))
484         (setq n (cdr n))))
485     ;; Make Gnus mark all new articles as unread.
486     (or (zerop (length new))
487         (gnus-make-articles-unread 
488          (gnus-group-prefixed-name group (list 'nnmh ""))
489          (setq new (sort new '<))))
490     ;; Sort the article list with highest numbers first.
491     (setq articles (sort articles (lambda (art1 art2) 
492                                     (> (car art1) (car art2)))))
493     ;; Finally write this list back to the .nnmh-articles file.
494     (save-excursion
495       (set-buffer (get-buffer-create "*nnmh out*"))
496       (insert ";; Gnus article active file for " group "\n\n")
497       (insert "(setq nnmh-newsgroup-articles '")
498       (insert (prin1-to-string articles) ")\n")
499       (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
500       (kill-buffer (current-buffer)))))
501
502 (defun nnmh-deletable-article-p (group article)
503   "Say whether ARTICLE in GROUP can be deleted."
504   (let ((path (concat nnmh-current-directory (int-to-string article))))
505     (and (file-writable-p path)
506          (or (not nnmail-keep-last-article)
507              (not (eq (cdr (nth 1 (assoc group nnmh-group-alist))) 
508                       article))))))
509
510 (provide 'nnmh)
511
512 ;;; nnmh.el ends here