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