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