*** 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 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   (nnmh-open-server-internal host service))
107
108 (defun nnmh-close-server (&optional server)
109   "Close news server."
110   (nnmh-close-server-internal))
111
112 (fset 'nnmh-request-quit (symbol-function 'nnmh-close-server))
113
114 (defun nnmh-server-opened (&optional server)
115   "Return server process status, T or NIL.
116 If the stream is opened, return T, otherwise return NIL."
117   (and nntp-server-buffer
118        (get-buffer nntp-server-buffer)))
119
120 (defun nnmh-status-message ()
121   "Return server status response as string."
122   nnmh-status-string)
123
124 (defun nnmh-request-article (id &optional newsgroup server buffer)
125   "Select article by message ID (or number)."
126   (nnmh-possibly-change-directory newsgroup)
127   (let ((file (if (stringp id)
128                   nil
129                 (concat nnmh-current-directory (prin1-to-string id))))
130         (nntp-server-buffer (or buffer nntp-server-buffer)))
131     (if (and (stringp file)
132              (file-exists-p file)
133              (not (file-directory-p file)))
134         (save-excursion
135           (nnmail-find-file file)))))
136
137 (defun nnmh-request-group (group &optional server dont-check)
138   "Select news GROUP."
139   (and nnmh-get-new-mail (or dont-check (nnmh-get-new-mail)))
140   (let ((pathname (nnmail-article-pathname group nnmh-directory))
141         dir)
142     (if (file-directory-p pathname)
143         (progn
144           (setq nnmh-current-directory pathname)
145           (and nnmh-get-new-mail (nnmh-update-gnus-unreads group))
146           (or dont-check
147               (progn
148                 (setq dir 
149                       (sort
150                        (mapcar
151                         (function
152                          (lambda (name)
153                            (string-to-int name)))
154                         (directory-files pathname nil "^[0-9]+$" t))
155                        '<))
156                 (save-excursion
157                   (set-buffer nntp-server-buffer)
158                   (erase-buffer)
159                   (if dir
160                       (insert (format "211 %d %d %d %s\n" (length dir) 
161                                       (car dir)
162                                       (progn (while (cdr dir)
163                                                (setq dir (cdr dir)))
164                                              (car dir))
165                                       group))
166                     (insert (format "211 0 1 0 %s\n" group))))))
167           t))))
168
169 (defun nnmh-request-list (&optional server dir)
170   "Get list of active articles in all newsgroups."
171   (and server nnmh-get-new-mail (nnmh-get-new-mail))
172   (or dir
173       (save-excursion
174         (set-buffer nntp-server-buffer)
175         (erase-buffer)
176         (setq dir nnmh-directory)))
177   (setq dir (expand-file-name dir))
178   ;; Recurse down all directories.
179   (let ((dirs (directory-files dir t nil t)))
180     (while dirs 
181       (if (and (not (string-match "/\\.\\.$" (car dirs)))
182                (not (string-match "/\\.$" (car dirs)))
183                (file-directory-p (car dirs)))
184           (nnmh-request-list server (car dirs)))
185       (setq dirs (cdr dirs))))
186   ;; For each directory, generate an active file line.
187   (if (not (string= (expand-file-name nnmh-directory) dir))
188       (let ((files (mapcar
189                     (lambda (name) (string-to-int name))
190                     (directory-files dir nil "^[0-9]+$" t))))
191         (save-excursion
192           (set-buffer nntp-server-buffer)
193           (insert 
194            (format 
195             "%s %d %d y\n" 
196             (progn
197               (string-match (expand-file-name nnmh-directory) dir)
198               (nnmail-replace-chars-in-string
199                (substring dir (match-end 0)) ?/ ?.))
200             (if files (apply (function max) files) 0)
201             (if files (apply (function min) files) 0))))))
202   t)
203
204 (defun nnmh-request-newgroups (date &optional server)
205   "List groups created after DATE."
206   (nnmh-request-list server))
207
208 (defun nnmh-request-post (&optional server)
209   "Post a new news in current buffer."
210   (mail-send-and-exit nil))
211
212 (fset 'nnmh-request-post-buffer 'nnmail-request-post-buffer)
213
214 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
215   "Expire all articles in the ARTICLES list in group GROUP.
216 The list of unexpired articles will be returned (ie. all articles that
217 were too fresh to be expired).
218 If FORCE is non-nil, ARTICLES will be deleted whether they are old or not."
219   (nnmh-possibly-change-directory newsgroup)
220   (let* ((days (or (and nnmail-expiry-wait-function
221                         (funcall nnmail-expiry-wait-function newsgroup))
222                    nnmail-expiry-wait))
223          article rest mod-time)
224     (while articles
225       (setq article (concat nnmh-current-directory (int-to-string
226                                                     (car articles))))
227       (if (setq mod-time (nth 5 (file-attributes article)))
228           (if (or force
229                   (> (nnmail-days-between
230                       (current-time-string)
231                       (current-time-string mod-time))
232                      days))
233               (progn
234                 (message "Deleting %s..." article)
235                 (condition-case ()
236                     (delete-file article)
237                   (file-error nil)))
238             (setq rest (cons (car articles) rest))))
239       (setq articles (cdr articles)))
240     rest))
241
242 (defun nnmh-close-group (group &optional server)
243   t)
244
245 (defun nnmh-request-move-article (article group server accept-form)
246   (let ((buf (get-buffer-create " *nnmh move*"))
247         result)
248     (and 
249      (nnmh-request-article article group server)
250      (save-excursion
251        (set-buffer buf)
252        (insert-buffer-substring nntp-server-buffer)
253        (setq result (eval accept-form))
254        (kill-buffer (current-buffer))
255        result)
256      (condition-case ()
257          (delete-file (concat nnmh-current-directory 
258                               (int-to-string article)))
259        (file-error nil)))
260  result))
261
262 (defun nnmh-request-accept-article (group)
263   (if (stringp group)
264       (and 
265        (nnmh-request-list)
266        (setq nnmh-group-alist (nnmail-get-active))
267        ;; We trick the choosing function into believing that only one
268        ;; group is availiable.  
269        (let ((nnmail-split-methods '(group "")))
270          (cons group (nnmh-save-mail))))
271     (and
272      (nnmh-request-list)
273      (setq nnmh-group-alist (nnmail-get-active))
274      (nnmh-save-mail))))
275
276 \f
277 ;;; Low-Level Interface
278
279 (defun nnmh-open-server-internal (host &optional service)
280   "Open connection to news server on HOST by SERVICE."
281   (save-excursion
282     ;; Initialize communication buffer.
283     (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
284     (set-buffer nntp-server-buffer)
285     (buffer-disable-undo (current-buffer))
286     (erase-buffer)
287     (kill-all-local-variables)
288     (setq case-fold-search t)           ;Should ignore case.
289     t))
290
291 (defun nnmh-close-server-internal ()
292   "Close connection to news server."
293   nil)
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