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