*** 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)
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)
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)
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 last noinsert)
298   (if (stringp group)
299       (and 
300        (nnmail-activate 'nnmh)
301        ;; We trick the choosing function into believing that only one
302        ;; group is available.  
303        (let ((nnmail-split-methods (list (list group ""))))
304          (car (nnmh-save-mail noinsert))))
305     (and
306      (nnmail-activate 'nnmh)
307      (car (nnmh-save-mail noinsert)))))
308
309 (defun nnmh-request-replace-article (article group buffer)
310   (nnmh-possibly-change-directory group)
311   (save-excursion
312     (set-buffer buffer)
313     (nnmh-possibly-create-directory group)
314     (condition-case ()
315         (progn
316           (write-region (point-min) (point-max)
317                         (concat nnmh-current-directory (int-to-string article))
318                         nil (if (nnheader-be-verbose 5) nil 'nomesg))
319           t)
320       (error nil))))
321
322 (defun nnmh-request-create-group (group &optional server) 
323   (nnmail-activate 'nnmh)
324   (or (assoc group nnmh-group-alist)
325       (let (active)
326         (setq nnmh-group-alist (cons (list group (setq active (cons 1 0)))
327                                      nnmh-group-alist))
328         (nnmh-possibly-create-directory group)
329         (nnmh-possibly-change-directory group)
330         (let ((articles (mapcar
331                          (lambda (file)
332                            (string-to-int file))
333                          (directory-files 
334                           nnmh-current-directory nil "^[0-9]+$"))))
335           (and articles
336                (progn
337                  (setcar active (apply 'min articles))
338                  (setcdr active (apply 'max articles)))))))
339   t)
340
341 (defun nnmh-request-delete-group (group &optional force server)
342   (nnmh-possibly-change-directory group)
343   ;; Delete all articles in GROUP.
344   (if (not force)
345       ()                                ; Don't delete the articles.
346     (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
347       (while articles 
348         (and (file-writable-p (car articles))
349              (progn
350                (nnheader-message 5 "Deleting article %s in %s..."
351                                  (car articles) group)
352                (funcall nnmail-delete-file-function (car articles))))
353         (setq articles (cdr articles))))
354     ;; Try to delete the directory itself.
355     (condition-case ()
356         (delete-directory nnmh-current-directory)
357       (error nil)))
358   ;; Remove the group from all structures.
359   (setq nnmh-group-alist 
360         (delq (assoc group nnmh-group-alist) nnmh-group-alist)
361         nnmh-current-directory nil)
362   t)
363
364 (defun nnmh-request-rename-group (group new-name &optional server)
365   (nnmh-possibly-change-directory group)
366   ;; Rename directory.
367   (and (file-writable-p nnmh-current-directory)
368        (condition-case ()
369            (progn
370              (rename-file 
371               (directory-file-name nnmh-current-directory)
372               (directory-file-name 
373                (nnmail-group-pathname new-name nnmh-directory)))
374              t)
375          (error nil))
376        ;; That went ok, so we change the internal structures.
377        (let ((entry (assoc group nnmh-group-alist)))
378          (and entry (setcar entry new-name))
379          (setq nnmh-current-directory nil)
380          t)))
381
382 \f
383 ;;; Internal functions.
384
385 (defun nnmh-possibly-change-directory (newsgroup)
386   (if newsgroup
387       (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
388         (if (file-directory-p pathname)
389             (setq nnmh-current-directory pathname)
390           (error "No such newsgroup: %s" newsgroup)))))
391
392 (defun nnmh-possibly-create-directory (group)
393   (let (dir dirs)
394     (setq dir (nnmail-group-pathname group nnmh-directory))
395     (while (not (file-directory-p dir))
396       (setq dirs (cons dir dirs))
397       (setq dir (file-name-directory (directory-file-name dir))))
398     (while dirs
399       (if (make-directory (directory-file-name (car dirs)))
400           (error "Could not create directory %s" (car dirs)))
401       (nnheader-message 5 "Creating mail directory %s" (car dirs))
402       (setq dirs (cdr dirs)))))
403              
404 (defun nnmh-save-mail (&optional noinsert)
405   "Called narrowed to an article."
406   (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
407     (unless noinsert
408       (nnmail-insert-lines)
409       (nnmail-insert-xref group-art))
410     (run-hooks 'nnmh-prepare-save-mail-hook)
411     (goto-char (point-min))
412     (while (looking-at "From ")
413       (replace-match "X-From-Line: ")
414       (forward-line 1))
415     ;; We save the article in all the newsgroups it belongs in.
416     (let ((ga group-art)
417           first)
418       (while ga
419         (nnmh-possibly-create-directory (caar ga))
420         (let ((file (concat (nnmail-group-pathname 
421                              (caar ga) nnmh-directory) 
422                             (int-to-string (cdar ga)))))
423           (if first
424               ;; It was already saved, so we just make a hard link.
425               (funcall nnmail-crosspost-link-function first file t)
426             ;; Save the article.
427             (write-region (point-min) (point-max) file nil nil)
428             (setq first file)))
429         (setq ga (cdr ga))))
430     group-art))
431
432 (defun nnmh-active-number (group)
433   "Compute the next article number in GROUP."
434   (let ((active (cadr (assoc group nnmh-group-alist))))
435     ;; The group wasn't known to nnmh, so we just create an active
436     ;; entry for it.   
437     (or active
438         (progn
439           (setq active (cons 1 0))
440           (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
441     (setcdr active (1+ (cdr active)))
442     (while (file-exists-p
443             (concat (nnmail-group-pathname group nnmh-directory)
444                     (int-to-string (cdr active))))
445       (setcdr active (1+ (cdr active))))
446     (cdr active)))
447
448 (defun nnmh-update-gnus-unreads (group)
449   ;; Go through the .nnmh-articles file and compare with the actual
450   ;; articles in this folder. The articles that are "new" will be
451   ;; marked as unread by Gnus.
452   (let* ((dir nnmh-current-directory)
453          (files (sort (mapcar (function (lambda (name) (string-to-int name)))
454                               (directory-files nnmh-current-directory 
455                                                nil "^[0-9]+$" t)) '<))
456          (nnmh-file (concat dir ".nnmh-articles"))
457          new articles)
458     ;; Load the .nnmh-articles file.
459     (if (file-exists-p nnmh-file)
460         (setq articles 
461               (let (nnmh-newsgroup-articles)
462                 (condition-case nil (load nnmh-file nil t t) (error nil))
463                 nnmh-newsgroup-articles)))
464     ;; Add all new articles to the `new' list.
465     (let ((art files))
466       (while art
467         (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
468         (setq art (cdr art))))
469     ;; Remove all deleted articles.
470     (let ((art articles))
471       (while art
472         (if (not (memq (caar art) files))
473             (setq articles (delq (car art) articles)))
474         (setq art (cdr art))))
475     ;; Check whether the highest-numbered articles really are the ones
476     ;; that Gnus thinks they are by looking at the time-stamps.
477     (let ((art articles))
478       (while (and art 
479                   (not (equal 
480                         (nth 5 (file-attributes 
481                                 (concat dir (int-to-string (caar art)))))
482                         (cdar art))))
483         (setq articles (delq (car art) articles))
484         (setq new (cons (caar art) new))
485         (setq art (cdr art))))
486     ;; Go through all the new articles and add them, and their
487     ;; time-stamps to the list.
488     (let ((n new))
489       (while n
490         (setq articles 
491               (cons (cons 
492                      (car n)
493                      (nth 5 (file-attributes 
494                              (concat dir (int-to-string (car n))))))
495                     articles))
496         (setq n (cdr n))))
497     ;; Make Gnus mark all new articles as unread.
498     (or (zerop (length new))
499         (gnus-make-articles-unread 
500          (gnus-group-prefixed-name group (list 'nnmh ""))
501          (setq new (sort new '<))))
502     ;; Sort the article list with highest numbers first.
503     (setq articles (sort articles (lambda (art1 art2) 
504                                     (> (car art1) (car art2)))))
505     ;; Finally write this list back to the .nnmh-articles file.
506     (save-excursion
507       (set-buffer (get-buffer-create "*nnmh out*"))
508       (insert ";; Gnus article active file for " group "\n\n")
509       (insert "(setq nnmh-newsgroup-articles '")
510       (insert (prin1-to-string articles) ")\n")
511       (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
512       (kill-buffer (current-buffer)))))
513
514 (defun nnmh-deletable-article-p (group article)
515   "Say whether ARTICLE in GROUP can be deleted."
516   (let ((path (concat nnmh-current-directory (int-to-string article))))
517     (and (file-writable-p path)
518          (or (not nnmail-keep-last-article)
519              (not (eq (cdr (nth 1 (assoc group nnmh-group-alist))) 
520                       article))))))
521
522 (provide 'nnmh)
523
524 ;;; nnmh.el ends here