1b660f903c679423b5a308572b1d10f552d05c27
[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               (nnheader-replace-chars-in-string
275                (decode-coding-string (substring dir (match-end 0))
276                                      nnmail-pathname-coding-system)
277                ?/ ?.))
278             (apply 'max files)
279             (apply 'min files)))))))
280   t)
281
282 (deffoo nnmh-request-newgroups (date &optional server)
283   (nnmh-request-list server))
284
285 (deffoo nnmh-request-expire-articles (articles newsgroup
286                                                &optional server force)
287   (nnmh-possibly-change-directory newsgroup server)
288   (let* ((is-old t)
289          article rest mod-time)
290     (nnheader-init-server-buffer)
291
292     (while (and articles is-old)
293       (setq article (concat nnmh-current-directory
294                             (int-to-string (car articles))))
295       (when (setq mod-time (nth 5 (file-attributes article)))
296         (if (and (nnmh-deletable-article-p newsgroup (car articles))
297                  (setq is-old
298                        (nnmail-expired-article-p newsgroup mod-time force)))
299             (progn
300               ;; Allow a special target group. -- jcn
301               (unless (eq nnmail-expiry-target 'delete)
302                 (with-temp-buffer
303                   (nnmh-request-article (car articles)
304                                         newsgroup server (current-buffer))
305                   (nnmail-expiry-target-group
306                    nnmail-expiry-target newsgroup)))
307               (nnheader-message 5 "Deleting article %s in %s..."
308                                 article newsgroup)
309               (condition-case ()
310                   (funcall nnmail-delete-file-function article)
311                 (file-error
312                  (nnheader-message 1 "Couldn't delete article %s in %s"
313                                    article newsgroup)
314                  (push (car articles) rest))))
315           (push (car articles) rest)))
316       (setq articles (cdr articles)))
317     (nnheader-message 5 "")
318     (nconc rest articles)))
319
320 (deffoo nnmh-close-group (group &optional server)
321   t)
322
323 (deffoo nnmh-request-move-article
324     (article group server accept-form &optional last)
325   (let ((buf (get-buffer-create " *nnmh move*"))
326         result)
327     (and
328      (nnmh-deletable-article-p group article)
329      (nnmh-request-article article group server)
330      (save-excursion
331        (set-buffer buf)
332        (erase-buffer)
333        (insert-buffer-substring nntp-server-buffer)
334        (setq result (eval accept-form))
335        (kill-buffer (current-buffer))
336        result)
337      (progn
338        (nnmh-possibly-change-directory group server)
339        (condition-case ()
340            (funcall nnmail-delete-file-function
341                     (concat nnmh-current-directory (int-to-string article)))
342          (file-error nil))))
343     result))
344
345 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
346   (nnmh-possibly-change-directory group server)
347   (if (and (not (equal group "queue"))
348            (not (equal group "draft")))
349       (nnmail-check-syntax))
350   (when nnmail-cache-accepted-message-ids
351     (nnmail-cache-insert (nnmail-fetch-field "message-id")))
352   (nnheader-init-server-buffer)
353   (prog1
354       (if (stringp group)
355           (if noinsert
356               (nnmh-active-number group)
357             (car (nnmh-save-mail
358                   (list (cons group (nnmh-active-number group)))
359                   noinsert)))
360         (let ((res (nnmail-article-group 'nnmh-active-number)))
361           (if (and (null res)
362                    (yes-or-no-p "Moved to `junk' group; delete article? "))
363               'junk
364             (car (nnmh-save-mail res noinsert)))))
365     (when (and last nnmail-cache-accepted-message-ids)
366       (nnmail-cache-close))))
367
368 (deffoo nnmh-request-replace-article (article group buffer)
369   (nnmh-possibly-change-directory group)
370   (save-excursion
371     (set-buffer buffer)
372     (nnmh-possibly-create-directory group)
373     (ignore-errors
374       (nnmail-write-region
375        (point-min) (point-max)
376        (concat nnmh-current-directory (int-to-string article))
377        nil (if (nnheader-be-verbose 5) nil 'nomesg))
378       t)))
379
380 (deffoo nnmh-request-create-group (group &optional server args)
381   (nnheader-init-server-buffer)
382   (unless (assoc group nnmh-group-alist)
383     (let (active)
384       (push (list group (setq active (cons 1 0)))
385             nnmh-group-alist)
386       (nnmh-possibly-create-directory group)
387       (nnmh-possibly-change-directory group server)
388       (let ((articles (mapcar
389                        (lambda (file)
390                          (string-to-int file))
391                        (directory-files
392                         nnmh-current-directory nil "^[0-9]+$"))))
393         (when articles
394           (setcar active (apply 'min articles))
395           (setcdr active (apply 'max articles))))))
396   t)
397
398 (deffoo nnmh-request-delete-group (group &optional force server)
399   (nnmh-possibly-change-directory group server)
400   ;; Delete all articles in GROUP.
401   (if (not force)
402       ()                                ; Don't delete the articles.
403     (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
404       (while articles
405         (when (file-writable-p (car articles))
406           (nnheader-message 5 "Deleting article %s in %s..."
407                             (car articles) group)
408           (funcall nnmail-delete-file-function (car articles)))
409         (setq articles (cdr articles))))
410     ;; Try to delete the directory itself.
411     (ignore-errors
412       (delete-directory nnmh-current-directory)))
413   ;; Remove the group from all structures.
414   (setq nnmh-group-alist
415         (delq (assoc group nnmh-group-alist) nnmh-group-alist)
416         nnmh-current-directory nil)
417   t)
418
419 (deffoo nnmh-request-rename-group (group new-name &optional server)
420   (nnmh-possibly-change-directory group server)
421   (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
422         (old-dir (nnmail-group-pathname group nnmh-directory)))
423     (when (ignore-errors
424             (make-directory new-dir t)
425             t)
426       ;; We move the articles file by file instead of renaming
427       ;; the directory -- there may be subgroups in this group.
428       ;; One might be more clever, I guess.
429       (let ((files (nnheader-article-to-file-alist old-dir)))
430         (while files
431           (rename-file
432            (concat old-dir (cdar files))
433            (concat new-dir (cdar files)))
434           (pop files)))
435       (when (<= (length (directory-files old-dir)) 2)
436         (ignore-errors
437           (delete-directory old-dir)))
438       ;; That went ok, so we change the internal structures.
439       (let ((entry (assoc group nnmh-group-alist)))
440         (when entry
441           (setcar entry new-name))
442         (setq nnmh-current-directory nil)
443         t))))
444
445 (nnoo-define-skeleton nnmh)
446
447 \f
448 ;;; Internal functions.
449
450 (defun nnmh-possibly-change-directory (newsgroup &optional server)
451   (when (and server
452              (not (nnmh-server-opened server)))
453     (nnmh-open-server server))
454   (when newsgroup
455     (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
456           (file-name-coding-system nnmail-pathname-coding-system)
457           (pathname-coding-system nnmail-pathname-coding-system))
458       (if (file-directory-p pathname)
459           (setq nnmh-current-directory pathname)
460         (error "No such newsgroup: %s" newsgroup)))))
461
462 (defun nnmh-possibly-create-directory (group)
463   (let (dir dirs)
464     (setq dir (nnmail-group-pathname group nnmh-directory))
465     (while (not (file-directory-p dir))
466       (push dir dirs)
467       (setq dir (file-name-directory (directory-file-name dir))))
468     (while dirs
469       (when (make-directory (directory-file-name (car dirs)))
470         (error "Could not create directory %s" (car dirs)))
471       (nnheader-message 5 "Creating mail directory %s" (car dirs))
472       (setq dirs (cdr dirs)))))
473
474 (defun nnmh-save-mail (group-art &optional noinsert)
475   "Called narrowed to an article."
476   (unless noinsert
477     (nnmail-insert-lines)
478     (nnmail-insert-xref group-art))
479   (run-hooks 'nnmail-prepare-save-mail-hook)
480   (run-hooks 'nnmh-prepare-save-mail-hook)
481   (goto-char (point-min))
482   (while (looking-at "From ")
483     (replace-match "X-From-Line: ")
484     (forward-line 1))
485   ;; We save the article in all the newsgroups it belongs in.
486   (let ((ga group-art)
487         first)
488     (while ga
489       (nnmh-possibly-create-directory (caar ga))
490       (let ((file (concat (nnmail-group-pathname
491                            (caar ga) nnmh-directory)
492                           (int-to-string (cdar ga)))))
493         (if first
494             ;; It was already saved, so we just make a hard link.
495             (funcall nnmail-crosspost-link-function first file t)
496           ;; Save the article.
497           (nnmail-write-region (point-min) (point-max) file nil nil)
498           (setq first file)))
499       (setq ga (cdr ga))))
500   group-art)
501
502 (defun nnmh-active-number (group)
503   "Compute the next article number in GROUP."
504   (let ((active (cadr (assoc group nnmh-group-alist)))
505         (dir (nnmail-group-pathname group nnmh-directory))
506         (file-name-coding-system nnmail-pathname-coding-system)
507         (pathname-coding-system nnmail-pathname-coding-system)
508         file)
509     (unless active
510       ;; The group wasn't known to nnmh, so we just create an active
511       ;; entry for it.
512       (setq active (cons 1 0))
513       (push (list group active) nnmh-group-alist)
514       (unless (file-exists-p dir)
515         (gnus-make-directory dir))
516       ;; Find the highest number in the group.
517       (let ((files (sort
518                     (mapcar
519                      (lambda (f)
520                        (string-to-int f))
521                      (directory-files dir nil "^[0-9]+$"))
522                     '>)))
523         (when files
524           (setcdr active (car files)))))
525     (setcdr active (1+ (cdr active)))
526     (while (or
527             ;; See whether the file exists...
528             (file-exists-p
529              (setq file (concat (nnmail-group-pathname group nnmh-directory)
530                                 (int-to-string (cdr active)))))
531             ;; ... or there is a buffer that will make that file exist
532             ;; in the future.
533             (get-file-buffer file))
534       ;; Skip past that file.
535       (setcdr active (1+ (cdr active))))
536     (cdr active)))
537
538 (defun nnmh-update-gnus-unreads (group)
539   ;; Go through the .nnmh-articles file and compare with the actual
540   ;; articles in this folder.  The articles that are "new" will be
541   ;; marked as unread by Gnus.
542   (let* ((dir nnmh-current-directory)
543          (files (sort (mapcar (function (lambda (name) (string-to-int name)))
544                               (directory-files nnmh-current-directory
545                                                nil "^[0-9]+$" t))
546                       '<))
547          (nnmh-file (concat dir ".nnmh-articles"))
548          new articles)
549     ;; Load the .nnmh-articles file.
550     (when (file-exists-p nnmh-file)
551       (setq articles
552             (let (nnmh-newsgroup-articles)
553               (ignore-errors (load nnmh-file nil t t))
554               nnmh-newsgroup-articles)))
555     ;; Add all new articles to the `new' list.
556     (let ((art files))
557       (while art
558         (unless (assq (car art) articles)
559           (push (car art) new))
560         (setq art (cdr art))))
561     ;; Remove all deleted articles.
562     (let ((art articles))
563       (while art
564         (unless (memq (caar art) files)
565           (setq articles (delq (car art) articles)))
566         (setq art (cdr art))))
567     ;; Check whether the articles really are the ones that Gnus thinks
568     ;; they are by looking at the time-stamps.
569     (let ((arts articles)
570           art)
571       (while (setq art (pop arts))
572         (when (not (equal
573                     (nth 5 (file-attributes
574                             (concat dir (int-to-string (car art)))))
575                     (cdr art)))
576           (setq articles (delq art articles))
577           (push (car art) new))))
578     ;; Go through all the new articles and add them, and their
579     ;; time-stamps, to the list.
580     (setq articles
581           (nconc articles
582                  (mapcar
583                   (lambda (art)
584                     (cons art
585                           (nth 5 (file-attributes
586                                   (concat dir (int-to-string art))))))
587                   new)))
588     ;; Make Gnus mark all new articles as unread.
589     (when new
590       (gnus-make-articles-unread
591        (gnus-group-prefixed-name group (list 'nnmh ""))
592        (setq new (sort new '<))))
593     ;; Sort the article list with highest numbers first.
594     (setq articles (sort articles (lambda (art1 art2)
595                                     (> (car art1) (car art2)))))
596     ;; Finally write this list back to the .nnmh-articles file.
597     (with-temp-file nnmh-file
598       (insert ";; Gnus article active file for " group "\n\n")
599       (insert "(setq nnmh-newsgroup-articles '")
600       (gnus-prin1 articles)
601       (insert ")\n"))))
602
603 (defun nnmh-deletable-article-p (group article)
604   "Say whether ARTICLE in GROUP can be deleted."
605   (let ((path (concat nnmh-current-directory (int-to-string article))))
606     ;; Writable.
607     (and (file-writable-p path)
608          (or
609           ;; We can never delete the last article in the group.
610           (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
611                    article))
612           ;; Well, we can.
613           nnmh-allow-delete-final))))
614
615 (provide 'nnmh)
616
617 ;;; nnmh.el ends here