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