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