*** 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   (unless dir
183     (nnheader-insert "")
184     (setq dir (file-truename (file-name-as-directory nnmh-directory))))
185   (setq dir (expand-file-name dir))
186   ;; Recurse down all directories.
187   (let ((dirs (and (file-readable-p dir)
188                    (> (nth 1 (file-attributes (file-chase-links dir))) 2)
189                    (directory-files dir t nil t)))
190         dir)
191     ;; Recurse down directories.
192     (while (setq dir (pop dirs))
193       (when (and (not (string-match "/\\.\\.?$" dir))
194                  (file-directory-p dir)
195                  (file-readable-p dir))
196         (nnmh-request-list nil dir))))
197   ;; For each directory, generate an active file line.
198   (unless (string= (expand-file-name nnmh-directory) dir)
199     (let ((files (mapcar
200                   (lambda (name) (string-to-int name))
201                   (directory-files dir nil "^[0-9]+$" t))))
202       (when files
203         (save-excursion
204           (set-buffer nntp-server-buffer)
205           (goto-char (point-max))
206           (insert 
207            (format 
208             "%s %d %d y\n" 
209             (progn
210               (string-match 
211                (file-truename (file-name-as-directory 
212                                (expand-file-name nnmh-directory))) dir)
213               (nnheader-replace-chars-in-string
214                (substring dir (match-end 0)) ?/ ?.))
215             (apply (function max) files) 
216             (apply (function min) files)))))))
217   (setq nnmh-group-alist (nnmail-get-active))
218   t)
219
220 (deffoo nnmh-request-newgroups (date &optional server)
221   (nnmh-request-list server))
222
223 (deffoo nnmh-request-expire-articles (articles newsgroup &optional server force)
224   (nnmh-possibly-change-directory newsgroup server)
225   (let* ((active-articles 
226           (mapcar
227            (function
228             (lambda (name)
229               (string-to-int name)))
230            (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
231          (is-old t)
232          article rest mod-time)
233     (nnmail-activate 'nnmh)
234
235     (while (and articles is-old)
236       (setq article (concat nnmh-current-directory 
237                             (int-to-string (car articles))))
238       (if (setq mod-time (nth 5 (file-attributes article)))
239           (if (and (nnmh-deletable-article-p newsgroup (car articles))
240                    (setq is-old
241                          (nnmail-expired-article-p newsgroup mod-time force)))
242               (progn
243                 (nnheader-message 5 "Deleting article %s in %s..." 
244                                   article newsgroup)
245                 (condition-case ()
246                     (funcall nnmail-delete-file-function article)
247                   (file-error
248                    (setq rest (cons (car articles) rest)))))
249             (setq rest (cons (car articles) rest))))
250       (setq articles (cdr articles)))
251     (message "")
252     (nconc rest articles)))
253
254 (deffoo nnmh-close-group (group &optional server)
255   t)
256
257 (deffoo nnmh-request-move-article 
258   (article group server accept-form &optional last)
259   (let ((buf (get-buffer-create " *nnmh move*"))
260         result)
261     (and 
262      (nnmh-deletable-article-p group article)
263      (nnmh-request-article article group server)
264      (save-excursion
265        (set-buffer buf)
266        (insert-buffer-substring nntp-server-buffer)
267        (setq result (eval accept-form))
268        (kill-buffer (current-buffer))
269        result)
270      (condition-case ()
271          (funcall nnmail-delete-file-function
272                   (concat nnmh-current-directory (int-to-string article)))
273        (file-error nil)))
274     result))
275
276 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
277   (nnmh-possibly-change-directory group server)
278   (if (stringp group)
279       (and 
280        (nnmail-activate 'nnmh)
281        ;; We trick the choosing function into believing that only one
282        ;; group is available.  
283        (let ((nnmail-split-methods (list (list group ""))))
284          (car (nnmh-save-mail noinsert))))
285     (and
286      (nnmail-activate 'nnmh)
287      (car (nnmh-save-mail noinsert)))))
288
289 (deffoo nnmh-request-replace-article (article group buffer)
290   (nnmh-possibly-change-directory group)
291   (save-excursion
292     (set-buffer buffer)
293     (nnmh-possibly-create-directory group)
294     (condition-case ()
295         (progn
296           (write-region (point-min) (point-max)
297                         (concat nnmh-current-directory (int-to-string article))
298                         nil (if (nnheader-be-verbose 5) nil 'nomesg))
299           t)
300       (error nil))))
301
302 (deffoo nnmh-request-create-group (group &optional server) 
303   (nnmail-activate 'nnmh)
304   (or (assoc group nnmh-group-alist)
305       (let (active)
306         (setq nnmh-group-alist (cons (list group (setq active (cons 1 0)))
307                                      nnmh-group-alist))
308         (nnmh-possibly-create-directory group)
309         (nnmh-possibly-change-directory group server)
310         (let ((articles (mapcar
311                          (lambda (file)
312                            (string-to-int file))
313                          (directory-files 
314                           nnmh-current-directory nil "^[0-9]+$"))))
315           (and articles
316                (progn
317                  (setcar active (apply 'min articles))
318                  (setcdr active (apply 'max articles)))))))
319   t)
320
321 (deffoo nnmh-request-delete-group (group &optional force server)
322   (nnmh-possibly-change-directory group server)
323   ;; Delete all articles in GROUP.
324   (if (not force)
325       ()                                ; Don't delete the articles.
326     (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
327       (while articles 
328         (and (file-writable-p (car articles))
329              (progn
330                (nnheader-message 5 "Deleting article %s in %s..."
331                                  (car articles) group)
332                (funcall nnmail-delete-file-function (car articles))))
333         (setq articles (cdr articles))))
334     ;; Try to delete the directory itself.
335     (condition-case ()
336         (delete-directory nnmh-current-directory)
337       (error nil)))
338   ;; Remove the group from all structures.
339   (setq nnmh-group-alist 
340         (delq (assoc group nnmh-group-alist) nnmh-group-alist)
341         nnmh-current-directory nil)
342   t)
343
344 (deffoo nnmh-request-rename-group (group new-name &optional server)
345   (nnmh-possibly-change-directory group server)
346   ;; Rename directory.
347   (and (file-writable-p nnmh-current-directory)
348        (condition-case ()
349            (progn
350              (rename-file 
351               (directory-file-name nnmh-current-directory)
352               (directory-file-name 
353                (nnmail-group-pathname new-name nnmh-directory)))
354              t)
355          (error nil))
356        ;; That went ok, so we change the internal structures.
357        (let ((entry (assoc group nnmh-group-alist)))
358          (and entry (setcar entry new-name))
359          (setq nnmh-current-directory nil)
360          t)))
361
362 \f
363 ;;; Internal functions.
364
365 (defun nnmh-possibly-change-directory (newsgroup &optional server)
366   (when (and server 
367              (not (nnmh-server-opened server)))
368     (nnmh-open-server server))
369   (if newsgroup
370       (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
371         (if (file-directory-p pathname)
372             (setq nnmh-current-directory pathname)
373           (error "No such newsgroup: %s" newsgroup)))))
374
375 (defun nnmh-possibly-create-directory (group)
376   (let (dir dirs)
377     (setq dir (nnmail-group-pathname group nnmh-directory))
378     (while (not (file-directory-p dir))
379       (setq dirs (cons dir dirs))
380       (setq dir (file-name-directory (directory-file-name dir))))
381     (while dirs
382       (if (make-directory (directory-file-name (car dirs)))
383           (error "Could not create directory %s" (car dirs)))
384       (nnheader-message 5 "Creating mail directory %s" (car dirs))
385       (setq dirs (cdr dirs)))))
386              
387 (defun nnmh-save-mail (&optional noinsert)
388   "Called narrowed to an article."
389   (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
390     (unless noinsert
391       (nnmail-insert-lines)
392       (nnmail-insert-xref group-art))
393     (run-hooks 'nnmh-prepare-save-mail-hook)
394     (goto-char (point-min))
395     (while (looking-at "From ")
396       (replace-match "X-From-Line: ")
397       (forward-line 1))
398     ;; We save the article in all the newsgroups it belongs in.
399     (let ((ga group-art)
400           first)
401       (while ga
402         (nnmh-possibly-create-directory (caar ga))
403         (let ((file (concat (nnmail-group-pathname 
404                              (caar ga) nnmh-directory) 
405                             (int-to-string (cdar ga)))))
406           (if first
407               ;; It was already saved, so we just make a hard link.
408               (funcall nnmail-crosspost-link-function first file t)
409             ;; Save the article.
410             (write-region (point-min) (point-max) file nil nil)
411             (setq first file)))
412         (setq ga (cdr ga))))
413     group-art))
414
415 (defun nnmh-active-number (group)
416   "Compute the next article number in GROUP."
417   (let ((active (cadr (assoc group nnmh-group-alist))))
418     ;; The group wasn't known to nnmh, so we just create an active
419     ;; entry for it.   
420     (or active
421         (progn
422           (setq active (cons 1 0))
423           (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
424     (setcdr active (1+ (cdr active)))
425     (while (file-exists-p
426             (concat (nnmail-group-pathname group nnmh-directory)
427                     (int-to-string (cdr active))))
428       (setcdr active (1+ (cdr active))))
429     (cdr active)))
430
431 (defun nnmh-update-gnus-unreads (group)
432   ;; Go through the .nnmh-articles file and compare with the actual
433   ;; articles in this folder. The articles that are "new" will be
434   ;; marked as unread by Gnus.
435   (let* ((dir nnmh-current-directory)
436          (files (sort (mapcar (function (lambda (name) (string-to-int name)))
437                               (directory-files nnmh-current-directory 
438                                                nil "^[0-9]+$" t)) '<))
439          (nnmh-file (concat dir ".nnmh-articles"))
440          new articles)
441     ;; Load the .nnmh-articles file.
442     (if (file-exists-p nnmh-file)
443         (setq articles 
444               (let (nnmh-newsgroup-articles)
445                 (condition-case nil (load nnmh-file nil t t) (error nil))
446                 nnmh-newsgroup-articles)))
447     ;; Add all new articles to the `new' list.
448     (let ((art files))
449       (while art
450         (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
451         (setq art (cdr art))))
452     ;; Remove all deleted articles.
453     (let ((art articles))
454       (while art
455         (if (not (memq (caar art) files))
456             (setq articles (delq (car art) articles)))
457         (setq art (cdr art))))
458     ;; Check whether the highest-numbered articles really are the ones
459     ;; that Gnus thinks they are by looking at the time-stamps.
460     (let ((art articles))
461       (while (and art 
462                   (not (equal 
463                         (nth 5 (file-attributes 
464                                 (concat dir (int-to-string (caar art)))))
465                         (cdar art))))
466         (setq articles (delq (car art) articles))
467         (setq new (cons (caar art) new))
468         (setq art (cdr art))))
469     ;; Go through all the new articles and add them, and their
470     ;; time-stamps to the list.
471     (let ((n new))
472       (while n
473         (setq articles 
474               (cons (cons 
475                      (car n)
476                      (nth 5 (file-attributes 
477                              (concat dir (int-to-string (car n))))))
478                     articles))
479         (setq n (cdr n))))
480     ;; Make Gnus mark all new articles as unread.
481     (or (zerop (length new))
482         (gnus-make-articles-unread 
483          (gnus-group-prefixed-name group (list 'nnmh ""))
484          (setq new (sort new '<))))
485     ;; Sort the article list with highest numbers first.
486     (setq articles (sort articles (lambda (art1 art2) 
487                                     (> (car art1) (car art2)))))
488     ;; Finally write this list back to the .nnmh-articles file.
489     (save-excursion
490       (set-buffer (get-buffer-create "*nnmh out*"))
491       (insert ";; Gnus article active file for " group "\n\n")
492       (insert "(setq nnmh-newsgroup-articles '")
493       (insert (prin1-to-string articles) ")\n")
494       (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
495       (kill-buffer (current-buffer)))))
496
497 (defun nnmh-deletable-article-p (group article)
498   "Say whether ARTICLE in GROUP can be deleted."
499   (let ((path (concat nnmh-current-directory (int-to-string article))))
500     (and (file-writable-p path)
501          (or (not nnmail-keep-last-article)
502              (not (eq (cdr (nth 1 (assoc group nnmh-group-alist))) 
503                       article))))))
504
505 (provide 'nnmh)
506
507 ;;; nnmh.el ends here