70b2f3cd1a4c5f82c65a1d01490fcf306bec49d8
[gnus] / lisp / nnmh.el
1 ;;; nnmh.el --- mhspool access for Gnus
2 ;; Copyright (C) 1995 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     (if (file-directory-p pathname)
166         (progn
167           (setq nnmh-current-directory pathname)
168           (and nnmh-get-new-mail 
169                nnmh-be-safe
170                (nnmh-update-gnus-unreads group))
171           (or dont-check
172               (progn
173                 (setq dir 
174                       (sort
175                        (mapcar
176                         (function
177                          (lambda (name)
178                            (string-to-int name)))
179                         (directory-files pathname nil "^[0-9]+$" t))
180                        '<))
181                 (save-excursion
182                   (set-buffer nntp-server-buffer)
183                   (erase-buffer)
184                   (if dir
185                       (insert (format "211 %d %d %d %s\n" (length dir) 
186                                       (car dir)
187                                       (progn (while (cdr dir)
188                                                (setq dir (cdr dir)))
189                                              (car dir))
190                                       group))
191                     (insert (format "211 0 1 0 %s\n" group))))))
192           t)
193       (setq nnmh-status-string "No such group")
194       nil)))
195
196 (defun nnmh-request-scan (&optional group server)
197   (nnmail-get-new-mail 'nnmh nil nnmh-directory group))      
198
199 (defun nnmh-request-list (&optional server dir)
200   (or dir
201       (save-excursion
202         (set-buffer nntp-server-buffer)
203         (erase-buffer)
204         (setq dir (file-truename (file-name-as-directory nnmh-directory)))))
205   (setq dir (expand-file-name dir))
206   ;; Recurse down all directories.
207   (let ((dirs (and (file-readable-p dir)
208                    (> (nth 1 (file-attributes (file-chase-links dir))) 2)
209                    (directory-files dir t nil t))))
210     (while dirs 
211       (if (and (not (string-match "/\\.\\.?$" (car dirs)))
212                (file-directory-p (car dirs))
213                (file-readable-p (car dirs)))
214           (nnmh-request-list nil (car dirs)))
215       (setq dirs (cdr dirs))))
216   ;; For each directory, generate an active file line.
217   (if (not (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         (if (null files)
222             ()
223           (save-excursion
224             (set-buffer nntp-server-buffer)
225             (goto-char (point-max))
226             (insert 
227              (format 
228               "%s %d %d y\n" 
229               (progn
230                 (string-match 
231                  (file-truename (file-name-as-directory 
232                                  (expand-file-name nnmh-directory))) dir)
233                 (nnmail-replace-chars-in-string
234                  (substring dir (match-end 0)) ?/ ?.))
235               (apply (function max) files) 
236               (apply (function min) files)))))))
237   (setq nnmh-group-alist (nnmail-get-active))
238   t)
239
240 (defun nnmh-request-newgroups (date &optional server)
241   (nnmh-request-list server))
242
243 (defun nnmh-request-post (&optional server)
244   (mail-send-and-exit nil))
245
246 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
247   (nnmh-possibly-change-directory newsgroup)
248   (let* ((active-articles 
249           (mapcar
250            (function
251             (lambda (name)
252               (string-to-int name)))
253            (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
254          (max-article (and active-articles (apply 'max active-articles)))
255          (is-old t)
256          article rest mod-time)
257     (nnmail-activate 'nnmh)
258
259     (while (and articles is-old)
260       (setq article (concat nnmh-current-directory 
261                             (int-to-string (car articles))))
262       (if (setq mod-time (nth 5 (file-attributes article)))
263           (if (and (nnmh-deletable-article-p newsgroup (car articles))
264                    (setq is-old
265                          (nnmail-expired-article-p newsgroup mod-time force)))
266               (progn
267                 (and gnus-verbose-backends 
268                      (message "Deleting article %s in %s..." 
269                               article newsgroup))
270                 (condition-case ()
271                     (funcall nnmail-delete-file-function article)
272                   (file-error
273                    (setq rest (cons (car articles) rest)))))
274             (setq rest (cons (car articles) rest))))
275       (setq articles (cdr articles)))
276     (message "")
277     (nconc rest articles)))
278
279 (defun nnmh-close-group (group &optional server)
280   t)
281
282 (defun nnmh-request-move-article 
283   (article group server accept-form &optional last)
284   (let ((buf (get-buffer-create " *nnmh move*"))
285         result)
286     (and 
287      (nnmh-deletable-article-p group article)
288      (nnmh-request-article article group server)
289      (save-excursion
290        (set-buffer buf)
291        (insert-buffer-substring nntp-server-buffer)
292        (setq result (eval accept-form))
293        (kill-buffer (current-buffer))
294        result)
295      (condition-case ()
296          (funcall nnmail-delete-file-function
297                   (concat nnmh-current-directory (int-to-string article)))
298        (file-error nil)))
299     result))
300
301 (defun nnmh-request-accept-article (group &optional last noinsert)
302   (if (stringp group)
303       (and 
304        (nnmail-activate 'nnmh)
305        ;; We trick the choosing function into believing that only one
306        ;; group is availiable.  
307        (let ((nnmail-split-methods (list (list group ""))))
308          (car (nnmh-save-mail noinsert))))
309     (and
310      (nnmail-activate 'nnmh)
311      (car (nnmh-save-mail noinsert)))))
312
313 (defun nnmh-request-replace-article (article group buffer)
314   (nnmh-possibly-change-directory group)
315   (save-excursion
316     (set-buffer buffer)
317     (nnmh-possibly-create-directory group)
318     (condition-case ()
319         (progn
320           (write-region (point-min) (point-max)
321                         (concat nnmh-current-directory (int-to-string article))
322                         nil (if gnus-verbose-backends nil 'nomesg))
323           t)
324       (error nil))))
325
326 (defun nnmh-request-create-group (group &optional server) 
327   (nnmail-activate 'nnmh)
328   (or (assoc group nnmh-group-alist)
329       (let (active)
330         (setq nnmh-group-alist (cons (list group (setq active (cons 1 0)))
331                                      nnmh-group-alist))
332         (nnmh-possibly-create-directory group)
333         (nnmh-possibly-change-directory group)
334         (let ((articles (mapcar
335                          (lambda (file)
336                            (string-to-int file))
337                          (directory-files 
338                           nnmh-current-directory nil "^[0-9]+$"))))
339           (and articles
340                (progn
341                  (setcar active (apply 'min articles))
342                  (setcdr active (apply 'max articles)))))))
343   t)
344
345 (defun nnmh-request-delete-group (group &optional force server)
346   (nnmh-possibly-change-directory group)
347   ;; Delete all articles in GROUP.
348   (if (not force)
349       ()                                ; Don't delete the articles.
350     (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
351       (while articles 
352         (and (file-writable-p (car articles))
353              (progn
354                (and gnus-verbose-backends
355                     (message (message "Deleting article %s in %s..."
356                                       (car articles) group)))
357                (funcall nnmail-delete-file-function (car articles))))
358         (setq articles (cdr articles))))
359     ;; Try to delete the directory itself.
360     (condition-case ()
361         (delete-directory nnmh-current-directory)
362       (error nil)))
363   ;; Remove the group from all structures.
364   (setq nnmh-group-alist 
365         (delq (assoc group nnmh-group-alist) nnmh-group-alist)
366         nnmh-current-directory nil)
367   t)
368
369 (defun nnmh-request-rename-group (group new-name &optional server)
370   (nnmh-possibly-change-directory group)
371   ;; Rename directory.
372   (and (file-writable-p nnmh-current-directory)
373        (condition-case ()
374            (progn
375              (rename-file 
376               (directory-file-name nnmh-current-directory)
377               (directory-file-name 
378                (nnmail-group-pathname new-name nnmh-directory)))
379              t)
380          (error nil))
381        ;; That went ok, so we change the internal structures.
382        (let ((entry (assoc group nnmh-group-alist)))
383          (and entry (setcar entry new-name))
384          (setq nnmh-current-directory nil)
385          t)))
386
387 \f
388 ;;; Internal functions.
389
390 (defun nnmh-possibly-change-directory (newsgroup)
391   (if newsgroup
392       (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
393         (if (file-directory-p pathname)
394             (setq nnmh-current-directory pathname)
395           (error "No such newsgroup: %s" newsgroup)))))
396
397 (defun nnmh-possibly-create-directory (group)
398   (let (dir dirs)
399     (setq dir (nnmail-group-pathname group nnmh-directory))
400     (while (not (file-directory-p dir))
401       (setq dirs (cons dir dirs))
402       (setq dir (file-name-directory (directory-file-name dir))))
403     (while dirs
404       (if (make-directory (directory-file-name (car dirs)))
405           (error "Could not create directory %s" (car dirs)))
406       (and gnus-verbose-backends 
407            (message "Creating mail directory %s" (car dirs)))
408       (setq dirs (cdr dirs)))))
409              
410 (defun nnmh-save-mail (&optional noinsert)
411   "Called narrowed to an article."
412   (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
413     (unless noinsert
414       (nnmail-insert-lines)
415       (nnmail-insert-xref group-art))
416     (run-hooks 'nnmh-prepare-save-mail-hook)
417     (goto-char (point-min))
418     (while (looking-at "From ")
419       (replace-match "X-From-Line: ")
420       (forward-line 1))
421     ;; We save the article in all the newsgroups it belongs in.
422     (let ((ga group-art)
423           first)
424       (while ga
425         (nnmh-possibly-create-directory (car (car ga)))
426         (let ((file (concat (nnmail-group-pathname 
427                              (car (car ga)) nnmh-directory) 
428                             (int-to-string (cdr (car ga))))))
429           (if first
430               ;; It was already saved, so we just make a hard link.
431               (add-name-to-file first file t)
432             ;; Save the article.
433             (write-region (point-min) (point-max) file nil nil)
434             (setq first file)))
435         (setq ga (cdr ga))))
436     group-art))
437
438 (defun nnmh-active-number (group)
439   "Compute the next article number in GROUP."
440   (let ((active (car (cdr (assoc group nnmh-group-alist)))))
441     ;; The group wasn't known to nnmh, so we just create an active
442     ;; entry for it.   
443     (or active
444         (progn
445           (setq active (cons 1 0))
446           (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
447     (setcdr active (1+ (cdr active)))
448     (while (file-exists-p
449             (concat (nnmail-group-pathname group nnmh-directory)
450                     (int-to-string (cdr active))))
451       (setcdr active (1+ (cdr active))))
452     (cdr active)))
453
454 (defun nnmh-update-gnus-unreads (group)
455   ;; Go through the .nnmh-articles file and compare with the actual
456   ;; articles in this folder. The articles that are "new" will be
457   ;; marked as unread by Gnus.
458   (let* ((dir nnmh-current-directory)
459          (files (sort (mapcar (function (lambda (name) (string-to-int name)))
460                               (directory-files nnmh-current-directory 
461                                                nil "^[0-9]+$" t)) '<))
462          (nnmh-file (concat dir ".nnmh-articles"))
463          new articles)
464     ;; Load the .nnmh-articles file.
465     (if (file-exists-p nnmh-file)
466         (setq articles 
467               (let (nnmh-newsgroup-articles)
468                 (condition-case nil (load nnmh-file nil t t) (error nil))
469                 nnmh-newsgroup-articles)))
470     ;; Add all new articles to the `new' list.
471     (let ((art files))
472       (while art
473         (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
474         (setq art (cdr art))))
475     ;; Remove all deleted articles.
476     (let ((art articles))
477       (while art
478         (if (not (memq (car (car art)) files))
479             (setq articles (delq (car art) articles)))
480         (setq art (cdr art))))
481     ;; Check whether the highest-numbered articles really are the ones
482     ;; that Gnus thinks they are by looking at the time-stamps.
483     (let ((art articles))
484       (while (and art 
485                   (not (equal 
486                         (nth 5 (file-attributes 
487                                 (concat dir (int-to-string (car (car art))))))
488                         (cdr (car art)))))
489         (setq articles (delq (car art) articles))
490         (setq new (cons (car (car art)) new))
491         (setq art (cdr art))))
492     ;; Go through all the new articles and add them, and their
493     ;; time-stamps to the list.
494     (let ((n new))
495       (while n
496         (setq articles 
497               (cons (cons 
498                      (car n)
499                      (nth 5 (file-attributes 
500                              (concat dir (int-to-string (car n))))))
501                     articles))
502         (setq n (cdr n))))
503     ;; Make Gnus mark all new articles as unread.
504     (or (zerop (length new))
505         (gnus-make-articles-unread 
506          (gnus-group-prefixed-name group (list 'nnmh ""))
507          (setq new (sort new '<))))
508     ;; Sort the article list with highest numbers first.
509     (setq articles (sort articles (lambda (art1 art2) 
510                                     (> (car art1) (car art2)))))
511     ;; Finally write this list back to the .nnmh-articles file.
512     (save-excursion
513       (set-buffer (get-buffer-create "*nnmh out*"))
514       (insert ";; Gnus article active file for " group "\n\n")
515       (insert "(setq nnmh-newsgroup-articles '")
516       (insert (prin1-to-string articles) ")\n")
517       (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
518       (kill-buffer (current-buffer)))))
519
520 (defun nnmh-deletable-article-p (group article)
521   "Say whether ARTICLE in GROUP can be deleted."
522   (or (not nnmail-keep-last-article)
523       (not (eq (cdr (nth 1 (assoc group nnmh-group-alist))) article))))
524
525 (provide 'nnmh)
526
527 ;;; nnmh.el ends here