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