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