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