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