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