* gnus-spec.el (gnus-correct-length): New function.
[gnus] / lisp / nnmh.el
1 ;;; nnmh.el --- mhspool access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;;      Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
30 ;; For an overview of what the interface functions do, please see the
31 ;; Gnus sources.
32
33 ;;; Code:
34
35 (require 'nnheader)
36 (require 'nnmail)
37 (require 'gnus-start)
38 (require 'nnoo)
39 (eval-when-compile (require 'cl))
40
41 (nnoo-declare nnmh)
42
43 (defvoo nnmh-directory message-directory
44   "*Mail spool directory.
45
46 This variable is a virtual server slot.  See the Gnus manual for details.")
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 This variable is a virtual server slot.  See the Gnus manual for details.")
52
53 (defvoo nnmh-prepare-save-mail-hook nil
54   "*Hook run narrowed to an article before saving.
55
56 This variable is a virtual server slot.  See the Gnus manual for details.")
57
58 (defvoo nnmh-be-safe nil
59   "*If non-nil, nnmh will check all articles to make sure whether they are new or not.
60 Go through the .nnmh-articles file and compare with the actual
61 articles in this folder.  The articles that are \"new\" will be marked
62 as unread by Gnus.
63
64 This variable is a virtual server slot.  See the Gnus manual for details.")
65
66 \f
67
68 (defconst nnmh-version "nnmh 1.0"
69   "nnmh version.")
70
71 (defvoo nnmh-current-directory nil
72   "Current news group directory.")
73
74 (defvoo nnmh-status-string "")
75 (defvoo nnmh-group-alist nil)
76 ;; Don't even think about setting this variable.  It does not exist.
77 ;; Forget about it.  Uh-huh.  Nope.  Nobody here.  It's only bound
78 ;; dynamically by certain functions in nndraft.
79 (defvar nnmh-allow-delete-final nil)
80
81 \f
82
83 ;;; Interface functions.
84
85 (nnoo-define-basics nnmh)
86
87 (deffoo nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
88   (save-excursion
89     (set-buffer nntp-server-buffer)
90     (erase-buffer)
91     (let* ((file nil)
92            (number (length articles))
93            (large (and (numberp nnmail-large-newsgroup)
94                        (> number nnmail-large-newsgroup)))
95            (count 0)
96            (file-name-coding-system nnmail-pathname-coding-system)
97            beg article)
98       (nnmh-possibly-change-directory newsgroup server)
99       ;; We don't support fetching by Message-ID.
100       (if (stringp (car articles))
101           'headers
102         (while articles
103           (when (and (file-exists-p
104                       (setq file (concat (file-name-as-directory
105                                           nnmh-current-directory)
106                                          (int-to-string
107                                           (setq article (pop articles))))))
108                      (not (file-directory-p file)))
109             (insert (format "221 %d Article retrieved.\n" article))
110             (setq beg (point))
111             (nnheader-insert-head file)
112             (goto-char beg)
113             (if (search-forward "\n\n" nil t)
114                 (forward-char -1)
115               (goto-char (point-max))
116               (insert "\n\n"))
117             (insert ".\n")
118             (delete-region (point) (point-max)))
119           (setq count (1+ count))
120
121           (and large
122                (zerop (% count 20))
123                (nnheader-message 5 "nnmh: Receiving headers... %d%%"
124                                  (/ (* count 100) number))))
125
126         (when large
127           (nnheader-message 5 "nnmh: Receiving headers...done"))
128
129         (nnheader-fold-continuation-lines)
130         'headers))))
131
132 (deffoo nnmh-open-server (server &optional defs)
133   (nnoo-change-server 'nnmh server defs)
134   (when (not (file-exists-p nnmh-directory))
135     (condition-case ()
136         (make-directory nnmh-directory t)
137       (error t)))
138   (cond
139    ((not (file-exists-p nnmh-directory))
140     (nnmh-close-server)
141     (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
142    ((not (file-directory-p (file-truename nnmh-directory)))
143     (nnmh-close-server)
144     (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
145    (t
146     (nnheader-report 'nnmh "Opened server %s using directory %s"
147                      server nnmh-directory)
148     t)))
149
150 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
151   (nnmh-possibly-change-directory newsgroup server)
152   (let ((file (if (stringp id)
153                   nil
154                 (concat nnmh-current-directory (int-to-string id))))
155         (file-name-coding-system nnmail-pathname-coding-system)
156         (nntp-server-buffer (or buffer nntp-server-buffer)))
157     (and (stringp file)
158          (file-exists-p file)
159          (not (file-directory-p file))
160          (save-excursion (nnmail-find-file file))
161          (string-to-int (file-name-nondirectory file)))))
162
163 (deffoo nnmh-request-group (group &optional server dont-check)
164   (nnheader-init-server-buffer)
165   (nnmh-possibly-change-directory group server)
166   (let ((pathname (nnmail-group-pathname group nnmh-directory))
167         (file-name-coding-system nnmail-pathname-coding-system)
168         dir)
169     (cond
170      ((not (file-directory-p pathname))
171       (nnheader-report
172        'nnmh "Can't select group (no such directory): %s" group))
173      (t
174       (setq nnmh-current-directory pathname)
175       (and nnmh-get-new-mail
176            nnmh-be-safe
177            (nnmh-update-gnus-unreads group))
178       (cond
179        (dont-check
180         (nnheader-report 'nnmh "Selected group %s" group)
181         t)
182        (t
183         ;; Re-scan the directory if it's on a foreign system.
184         (nnheader-re-read-dir pathname)
185         (setq dir
186               (sort
187                (mapcar (lambda (name) (string-to-int name))
188                        (directory-files pathname nil "^[0-9]+$" t))
189                '<))
190         (cond
191          (dir
192           (setq nnmh-group-alist
193                 (delq (assoc group nnmh-group-alist) nnmh-group-alist))
194           (push (list group (cons (car dir) (car (last dir))))
195                 nnmh-group-alist)
196           (nnheader-report 'nnmh "Selected group %s" group)
197           (nnheader-insert
198            "211 %d %d %d %s\n" (length dir) (car dir)
199            (car (last dir)) group))
200          (t
201           (nnheader-report 'nnmh "Empty group %s" group)
202           (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
203
204 (deffoo nnmh-request-scan (&optional group server)
205   (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
206
207 (deffoo nnmh-request-list (&optional server dir)
208   (nnheader-insert "")
209   (nnmh-possibly-change-directory nil server)
210   (let ((file-name-coding-system nnmail-pathname-coding-system)
211         (nnmh-toplev
212          (file-truename (or dir (file-name-as-directory nnmh-directory)))))
213     (nnmh-request-list-1 nnmh-toplev))
214   (setq nnmh-group-alist (nnmail-get-active))
215   t)
216
217 (defvar nnmh-toplev)
218 (defun nnmh-request-list-1 (dir)
219   (setq dir (expand-file-name dir))
220   ;; Recurse down all directories.
221   (let ((dirs (and (file-readable-p dir)
222                    (> (nth 1 (file-attributes (file-chase-links dir))) 2)
223                    (nnheader-directory-files dir t nil t)))
224         rdir)
225     ;; Recurse down directories.
226     (while (setq rdir (pop dirs))
227       (when (and (file-directory-p rdir)
228                  (file-readable-p rdir)
229                  (not (equal (file-truename rdir)
230                              (file-truename dir))))
231         (nnmh-request-list-1 rdir))))
232   ;; For each directory, generate an active file line.
233   (unless (string= (expand-file-name nnmh-toplev) dir)
234     (let ((files (mapcar
235                   (lambda (name) (string-to-int name))
236                   (directory-files dir nil "^[0-9]+$" t))))
237       (when files
238         (save-excursion
239           (set-buffer nntp-server-buffer)
240           (goto-char (point-max))
241           (insert
242            (format
243             "%s %d %d y\n"
244             (progn
245               (string-match
246                (regexp-quote
247                 (file-truename (file-name-as-directory
248                                 (expand-file-name nnmh-toplev))))
249                dir)
250               (mm-string-as-multibyte
251                (mm-encode-coding-string
252                 (nnheader-replace-chars-in-string
253                  (substring dir (match-end 0))
254                  ?/ ?.)
255                 nnmail-pathname-coding-system)))
256             (apply 'max files)
257             (apply 'min files)))))))
258   t)
259
260 (deffoo nnmh-request-newgroups (date &optional server)
261   (nnmh-request-list server))
262
263 (deffoo nnmh-request-expire-articles (articles newsgroup
264                                                &optional server force)
265   (nnmh-possibly-change-directory newsgroup server)
266   (let* ((is-old t)
267          article rest mod-time)
268     (nnheader-init-server-buffer)
269
270     (while (and articles is-old)
271       (setq article (concat nnmh-current-directory
272                             (int-to-string (car articles))))
273       (when (setq mod-time (nth 5 (file-attributes article)))
274         (if (and (nnmh-deletable-article-p newsgroup (car articles))
275                  (setq is-old
276                        (nnmail-expired-article-p newsgroup mod-time force)))
277             (progn
278               ;; Allow a special target group. -- jcn
279               (unless (eq nnmail-expiry-target 'delete)
280                 (with-temp-buffer
281                   (nnmh-request-article (car articles)
282                                         newsgroup server (current-buffer))
283                   (nnmail-expiry-target-group
284                    nnmail-expiry-target newsgroup)))
285               (nnheader-message 5 "Deleting article %s in %s..."
286                                 article newsgroup)
287               (condition-case ()
288                   (funcall nnmail-delete-file-function article)
289                 (file-error
290                  (nnheader-message 1 "Couldn't delete article %s in %s"
291                                    article newsgroup)
292                  (push (car articles) rest))))
293           (push (car articles) rest)))
294       (setq articles (cdr articles)))
295     (nnheader-message 5 "")
296     (nconc rest articles)))
297
298 (deffoo nnmh-close-group (group &optional server)
299   t)
300
301 (deffoo nnmh-request-move-article
302     (article group server accept-form &optional last)
303   (let ((buf (get-buffer-create " *nnmh move*"))
304         result)
305     (and
306      (nnmh-deletable-article-p group article)
307      (nnmh-request-article article group server)
308      (save-excursion
309        (set-buffer buf)
310        (erase-buffer)
311        (insert-buffer-substring nntp-server-buffer)
312        (setq result (eval accept-form))
313        (kill-buffer (current-buffer))
314        result)
315      (progn
316        (nnmh-possibly-change-directory group server)
317        (condition-case ()
318            (funcall nnmail-delete-file-function
319                     (concat nnmh-current-directory (int-to-string article)))
320          (file-error nil))))
321     result))
322
323 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
324   (nnmh-possibly-change-directory group server)
325   (nnmail-check-syntax)
326   (when nnmail-cache-accepted-message-ids
327     (nnmail-cache-insert (nnmail-fetch-field "message-id")))
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         (error "No such newsgroup: %s" newsgroup)))))
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