*** empty log message ***
[gnus] / lisp / nnmh.el
1 ;;; nnmh.el --- mail spool access for Gnus (mhspool)
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
28 ;;; Code:
29
30 (require 'nnheader)
31 (require 'rmail)
32 (require 'nnmail)
33 (require 'gnus)
34
35 (defvar nnmh-directory "~/Mail/"
36   "Mail directory.")
37
38 (defvar nnmh-get-new-mail t
39   "If non-nil, nnmh will check the incoming mail file and split the mail.")
40
41 \f
42
43 (defconst nnmh-version "nnmh 0.1"
44   "nnmh version.")
45
46 (defvar nnmh-current-directory nil
47   "Current news group directory.")
48
49 (defvar nnmh-status-string "")
50
51 (defvar nnmh-group-alist nil)
52
53 \f
54
55 ;;; Interface functions.
56
57 (defun nnmh-retrieve-headers (sequence &optional newsgroup server)
58   "Retrieve the headers for the articles in SEQUENCE.
59 Newsgroup must be selected before calling this function."
60   (save-excursion
61     (set-buffer nntp-server-buffer)
62     (erase-buffer)
63     (let ((file nil)
64           (number (length sequence))
65           (count 0)
66           beg article)
67       (nnmh-possibly-change-directory newsgroup)
68       (while sequence
69         (setq article (car sequence))
70         (setq file
71               (concat nnmh-current-directory (prin1-to-string article)))
72         (if (and (file-exists-p file)
73                  (not (file-directory-p file)))
74             (progn
75               (insert (format "221 %d Article retrieved.\n" article))
76               (setq beg (point))
77               (insert-file-contents file)
78               (goto-char beg)
79               (if (search-forward "\n\n" nil t)
80                   (forward-char -1)
81                 (goto-char (point-max))
82                 (insert "\n\n"))
83               (insert ".\n")
84               (delete-region (point) (point-max))))
85         (setq sequence (cdr sequence))
86         (setq count (1+ count))
87         (and (numberp nnmail-large-newsgroup)
88              (> number nnmail-large-newsgroup)
89              (zerop (% count 20))
90              (message "nnmh: Receiving headers... %d%%"
91                       (/ (* count 100) number))))
92
93       (and (numberp nnmail-large-newsgroup)
94            (> number nnmail-large-newsgroup)
95            (message "nnmh: Receiving headers... done"))
96
97       ;; Fold continuation lines.
98       (goto-char 1)
99       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
100         (replace-match " " t t))
101       'headers)))
102
103 (defun nnmh-open-server (host &optional service)
104   "Open nnmh mail backend."
105   (setq nnmh-status-string "")
106   (nnheader-init-server-buffer))
107
108 (defun nnmh-close-server (&optional server)
109   "Close news server."
110   t)
111
112 (defun nnmh-server-opened (&optional server)
113   "Return server process status, T or NIL.
114 If the stream is opened, return T, otherwise return NIL."
115   (and nntp-server-buffer
116        (get-buffer nntp-server-buffer)))
117
118 (defun nnmh-status-message (&optional server)
119   "Return server status response as string."
120   nnmh-status-string)
121
122 (defun nnmh-request-article (id &optional newsgroup server buffer)
123   "Select article by message ID (or number)."
124   (nnmh-possibly-change-directory newsgroup)
125   (let ((file (if (stringp id)
126                   nil
127                 (concat nnmh-current-directory (prin1-to-string id))))
128         (nntp-server-buffer (or buffer nntp-server-buffer)))
129     (if (and (stringp file)
130              (file-exists-p file)
131              (not (file-directory-p file)))
132         (save-excursion
133           (nnmail-find-file file)))))
134
135 (defun nnmh-request-group (group &optional server dont-check)
136   "Select news GROUP."
137   (and nnmh-get-new-mail (or dont-check (nnmh-get-new-mail)))
138   (let ((pathname (nnmail-article-pathname group nnmh-directory))
139         dir)
140     (if (file-directory-p pathname)
141         (progn
142           (setq nnmh-current-directory pathname)
143           (and nnmh-get-new-mail (nnmh-update-gnus-unreads group))
144           (or dont-check
145               (progn
146                 (setq dir 
147                       (sort
148                        (mapcar
149                         (function
150                          (lambda (name)
151                            (string-to-int name)))
152                         (directory-files pathname nil "^[0-9]+$" t))
153                        '<))
154                 (save-excursion
155                   (set-buffer nntp-server-buffer)
156                   (erase-buffer)
157                   (if dir
158                       (insert (format "211 %d %d %d %s\n" (length dir) 
159                                       (car dir)
160                                       (progn (while (cdr dir)
161                                                (setq dir (cdr dir)))
162                                              (car dir))
163                                       group))
164                     (insert (format "211 0 1 0 %s\n" group))))))
165           t))))
166
167 (defun nnmh-request-list (&optional server dir)
168   "Get list of active articles in all newsgroups."
169   (and server nnmh-get-new-mail (nnmh-get-new-mail))
170   (or dir
171       (save-excursion
172         (set-buffer nntp-server-buffer)
173         (erase-buffer)
174         (setq dir nnmh-directory)))
175   (setq dir (expand-file-name dir))
176   ;; Recurse down all directories.
177   (let ((dirs (directory-files dir t nil t)))
178     (while dirs 
179       (if (and (not (string-match "/\\.\\.$" (car dirs)))
180                (not (string-match "/\\.$" (car dirs)))
181                (file-directory-p (car dirs)))
182           (nnmh-request-list server (car dirs)))
183       (setq dirs (cdr dirs))))
184   ;; For each directory, generate an active file line.
185   (if (not (string= (expand-file-name nnmh-directory) dir))
186       (let ((files (mapcar
187                     (lambda (name) (string-to-int name))
188                     (directory-files dir nil "^[0-9]+$" t))))
189         (save-excursion
190           (set-buffer nntp-server-buffer)
191           (insert 
192            (format 
193             "%s %d %d y\n" 
194             (progn
195               (string-match (expand-file-name nnmh-directory) dir)
196               (nnmail-replace-chars-in-string
197                (substring dir (match-end 0)) ?/ ?.))
198             (if files (apply (function max) files) 0)
199             (if files (apply (function min) files) 0))))))
200   t)
201
202 (defun nnmh-request-newgroups (date &optional server)
203   "List groups created after DATE."
204   (nnmh-request-list server))
205
206 (defun nnmh-request-post (&optional server)
207   "Post a new news in current buffer."
208   (mail-send-and-exit nil))
209
210 (fset 'nnmh-request-post-buffer 'nnmail-request-post-buffer)
211
212 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
213   "Expire all articles in the ARTICLES list in group GROUP.
214 The list of unexpired articles will be returned (ie. all articles that
215 were too fresh to be expired).
216 If FORCE is non-nil, ARTICLES will be deleted whether they are old or not."
217   (nnmh-possibly-change-directory newsgroup)
218   (let* ((days (or (and nnmail-expiry-wait-function
219                         (funcall nnmail-expiry-wait-function newsgroup))
220                    nnmail-expiry-wait))
221          article rest mod-time)
222     (if nnmail-keep-last-article
223         (progn
224           (setq articles (sort articles '>))
225           (setq rest (cons (car articles) rest))
226           (setq articles (cdr articles))))
227     (while articles
228       (setq article (concat nnmh-current-directory (int-to-string
229                                                     (car articles))))
230       (if (setq mod-time (nth 5 (file-attributes article)))
231           (if (or force
232                   (> (nnmail-days-between
233                       (current-time-string)
234                       (current-time-string mod-time))
235                      days))
236               (progn
237                 (message "Deleting %s..." article)
238                 (condition-case ()
239                     (delete-file article)
240                   (file-error nil)))
241             (setq rest (cons (car articles) rest))))
242       (setq articles (cdr articles)))
243     rest))
244
245 (defun nnmh-close-group (group &optional server)
246   t)
247
248 (defun nnmh-request-move-article 
249   (article group server accept-form &optional last)
250   (let ((buf (get-buffer-create " *nnmh move*"))
251         result)
252     (and 
253      (nnmh-request-article article group server)
254      (save-excursion
255        (set-buffer buf)
256        (insert-buffer-substring nntp-server-buffer)
257        (setq result (eval accept-form))
258        (kill-buffer (current-buffer))
259        result)
260      (condition-case ()
261          (delete-file (concat nnmh-current-directory 
262                               (int-to-string article)))
263        (file-error nil)))
264  result))
265
266 (defun nnmh-request-accept-article (group &optional last)
267   (if (stringp group)
268       (and 
269        (nnmh-request-list)
270        (setq nnmh-group-alist (nnmail-get-active))
271        ;; We trick the choosing function into believing that only one
272        ;; group is availiable.  
273        (let ((nnmail-split-methods '(group "")))
274          (cons group (nnmh-save-mail))))
275     (and
276      (nnmh-request-list)
277      (setq nnmh-group-alist (nnmail-get-active))
278      (nnmh-save-mail))))
279
280 (defun nnmh-request-replace-article (article group buffer)
281   (nnmh-possibly-change-directory group)
282   (save-excursion
283     (set-buffer buffer)
284     (condition-case ()
285         (progn
286           (write-region (point-min) (point-max)
287                         (concat nnmh-current-directory (int-to-string article))
288                         nil (if gnus-verbose-backends nil 'nomesg))
289           t)
290       (error nil))))
291
292 \f
293 ;;; Internal functions.
294
295 (defun nnmh-possibly-change-directory (newsgroup)
296   (if newsgroup
297       (let ((pathname (nnmail-article-pathname newsgroup nnmh-directory)))
298         (if (file-directory-p pathname)
299             (setq nnmh-current-directory pathname)
300           (error "No such newsgroup: %s" newsgroup)))))
301
302 (defun nnmh-create-directories ()
303   (let ((methods nnmail-split-methods)
304         dir dirs)
305     (while methods
306       (setq dir (nnmail-article-pathname (car (car methods)) nnmh-directory))
307       (while (not (file-directory-p dir))
308         (setq dirs (cons dir dirs))
309         (setq dir (file-name-directory (directory-file-name dir))))
310       (while dirs
311         (if (make-directory (directory-file-name (car dirs)))
312             (error "Could not create directory %s" (car dirs)))
313         (message "Creating mail directory %s" (car dirs))
314         (setq dirs (cdr dirs)))
315       (setq methods (cdr methods)))))
316
317 (defun nnmh-save-mail ()
318   "Called narrowed to an article."
319   (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number)))
320         chars nov-line lines hbeg hend)
321     (setq chars (nnmail-insert-lines))
322     (nnmail-insert-xref group-art)
323     (goto-char (point-min))
324     (while (looking-at "From ")
325       (replace-match "X-From-Line: ")
326       (forward-line 1))
327     ;; We save the article in all the newsgroups it belongs in.
328     (let ((ga group-art)
329           first)
330       (while ga
331         (let ((file (concat (nnmail-article-pathname 
332                              (car (car ga)) nnmh-directory) 
333                             (int-to-string (cdr (car ga))))))
334           (if first
335               ;; It was already saved, so we just make a hard link.
336               (add-name-to-file first file t)
337             ;; Save the article.
338             (write-region (point-min) (point-max) file nil nil)
339             (setq first file)))
340         (setq ga (cdr ga))))
341     group-art))
342
343 (defun nnmh-active-number (group)
344   "Compute the next article number in GROUP."
345   (let ((active (car (cdr (assoc group nnmh-group-alist)))))
346     (setcdr active (1+ (cdr active)))
347     (let (file)
348       (while (file-exists-p
349               (setq file (concat (nnmail-article-pathname 
350                                   group nnmh-directory)
351                                  (int-to-string (cdr active)))))
352         (setcdr active (1+ (cdr active)))))
353     (cdr active)))
354
355 (defun nnmh-get-new-mail ()
356   "Read new incoming mail."
357   (let (incoming)
358     (nnmh-create-directories)
359     (if (and nnmh-get-new-mail nnmail-spool-file
360              (file-exists-p nnmail-spool-file)
361              (> (nth 7 (file-attributes nnmail-spool-file)) 0))
362         (progn
363           (message "nnmh: Reading incoming mail...")
364           (setq incoming 
365                 (nnmail-move-inbox nnmail-spool-file
366                                    (concat nnmh-directory "Incoming")))
367           (nnmh-request-list)
368           (setq nnmh-group-alist (nnmail-get-active))
369           (nnmail-split-incoming incoming 'nnmh-save-mail)
370           (run-hooks 'nnmail-read-incoming-hook)
371 ;;         (delete-file incoming)
372           (message "nnmh: Reading incoming mail...done")))))
373
374 (defun nnmh-update-gnus-unreads (group)
375   ;; Go through the .nnmh-articles file and compare with the actual
376   ;; articles in this folder. The articles that are "new" will be
377   ;; marked as unread by Gnus.
378   (let* ((dir nnmh-current-directory)
379          (files (sort (mapcar (function (lambda (name) (string-to-int name)))
380                               (directory-files nnmh-current-directory 
381                                                nil "^[0-9]+$" t)) '<))
382          (nnmh-file (concat dir ".nnmh-articles"))
383          new articles)
384     ;; Load the .nnmh-articles file.
385     (if (file-exists-p nnmh-file)
386         (setq articles 
387               (let (nnmh-newsgroup-articles)
388                 (condition-case nil (load nnmh-file nil t t) (error nil))
389                 nnmh-newsgroup-articles)))
390     ;; Add all new articles to the `new' list.
391     (let ((art files))
392       (while art
393         (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
394         (setq art (cdr art))))
395     ;; Remove all deleted articles.
396     (let ((art articles))
397       (while art
398         (if (not (memq (car (car art)) files))
399             (setq articles (delq (car art) articles)))
400         (setq art (cdr art))))
401     ;; Check whether the highest-numbered articles really are the ones
402     ;; that Gnus thinks they are by looking at the time-stamps.
403     (let ((art articles))
404       (while (and art 
405                   (not (equal 
406                         (nth 5 (file-attributes 
407                                 (concat dir (int-to-string (car (car art))))))
408                         (cdr (car art)))))
409         (setq articles (delq (car art) articles))
410         (setq new (cons (car (car art)) new))
411         (setq art (cdr art))))
412     ;; Go through all the new articles and add them, and their
413     ;; time-stamps to the list.
414     (let ((n new))
415       (while n
416         (setq articles 
417               (cons (cons 
418                      (car n)
419                      (nth 5 (file-attributes 
420                              (concat dir (int-to-string (car n))))))
421                     articles))
422         (setq n (cdr n))))
423     ;; Make Gnus mark all new articles as unread.
424     (or (zerop (length new))
425         (gnus-make-articles-unread 
426          (gnus-group-prefixed-name group (list 'nnmh ""))
427          (setq new (sort new '<))))
428     ;; Sort the article list with highest numbers first.
429     (setq articles (sort articles (lambda (art1 art2) 
430                                     (> (car art1) (car art2)))))
431     ;; Finally write this list back to the .nnmh-articles file.
432     (save-excursion
433       (set-buffer (get-buffer-create "*nnmh out*"))
434       (insert ";; Gnus article active file for " group "\n\n")
435       (insert "(setq nnmh-newsgroup-articles '")
436       (insert (prin1-to-string articles) ")\n")
437       (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
438       (kill-buffer (current-buffer)))))
439
440 (provide 'nnmh)
441
442 ;;; nnmh.el ends here