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