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