*** empty log message ***
[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
37 (defvar nnmh-directory "~/Mail/"
38   "*Mail spool directory.")
39
40 (defvar nnmh-get-new-mail t
41   "*If non-nil, nnmh will check the incoming mail file and split the mail.")
42
43 (defvar nnmh-prepare-save-mail-hook nil
44   "*Hook run narrowed to an article before saving.")
45
46 (defvar nnmh-be-safe nil
47   "*If non-nil, nnmh will check all articles to make sure whether they are new or not.")
48
49 \f
50
51 (defconst nnmh-version "nnmh 0.1"
52   "nnmh version.")
53
54 (defvar nnmh-current-directory nil
55   "Current news group directory.")
56
57 (defvar nnmh-status-string "")
58 (defvar nnmh-group-alist nil)
59
60 \f
61
62 (defvar nnmh-current-server nil)
63 (defvar nnmh-server-alist nil)
64 (defvar nnmh-server-variables 
65   (list
66    (list 'nnmh-directory nnmh-directory)
67    (list 'nnmh-get-new-mail nnmh-get-new-mail)
68    '(nnmh-current-directory nil)
69    '(nnmh-status-string "")
70    '(nnmh-group-alist)))
71
72 \f
73
74 ;;; Interface functions.
75
76 (defun nnmh-retrieve-headers (sequence &optional newsgroup server)
77   (save-excursion
78     (set-buffer nntp-server-buffer)
79     (erase-buffer)
80     (let* ((file nil)
81            (number (length sequence))
82            (large (and (numberp nnmail-large-newsgroup)
83                        (> number nnmail-large-newsgroup)))
84            (count 0)
85            beg article)
86       (nnmh-possibly-change-directory newsgroup)
87       (if (stringp (car sequence))
88           'headers
89         (while sequence
90           (setq article (car sequence))
91           (setq file
92                 (concat nnmh-current-directory (int-to-string article)))
93           (if (and (file-exists-p file)
94                    (not (file-directory-p file)))
95               (progn
96                 (insert (format "221 %d Article retrieved.\n" article))
97                 (setq beg (point))
98                 (nnheader-insert-head file)
99                 (goto-char beg)
100                 (if (search-forward "\n\n" nil t)
101                     (forward-char -1)
102                   (goto-char (point-max))
103                   (insert "\n\n"))
104                 (insert ".\n")
105                 (delete-region (point) (point-max))))
106           (setq sequence (cdr sequence))
107           (setq count (1+ count))
108
109           (and large
110                (zerop (% count 20))
111                (message "nnmh: Receiving headers... %d%%"
112                         (/ (* count 100) number))))
113
114         (and large (message "nnmh: Receiving headers...done"))
115
116         ;; Fold continuation lines.
117         (goto-char (point-min))
118         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
119           (replace-match " " t t))
120         'headers))))
121
122 (defun nnmh-open-server (server &optional defs)
123   (nnheader-init-server-buffer)
124   (if (equal server nnmh-current-server)
125       t
126     (if nnmh-current-server
127         (setq nnmh-server-alist 
128               (cons (list nnmh-current-server
129                           (nnheader-save-variables nnmh-server-variables))
130                     nnmh-server-alist)))
131     (let ((state (assoc server nnmh-server-alist)))
132       (if state 
133           (progn
134             (nnheader-restore-variables (nth 1 state))
135             (setq nnmh-server-alist (delq state nnmh-server-alist)))
136         (nnheader-set-init-variables nnmh-server-variables defs)))
137     (setq nnmh-current-server server)))
138
139 (defun nnmh-close-server (&optional server)
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
161 (defun nnmh-request-group (group &optional server dont-check)
162   (and nnmh-get-new-mail (or dont-check (nnmh-get-new-mail group)))
163   (let ((pathname (nnmh-article-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
194 (defun nnmh-request-list (&optional server dir)
195   (or dir
196       (save-excursion
197         (set-buffer nntp-server-buffer)
198         (erase-buffer)
199         (setq dir nnmh-directory)))
200   (setq dir (expand-file-name dir))
201   ;; Recurse down all directories.
202   (let ((dirs (and (file-readable-p dir)
203                    (> (nth 1 (file-attributes (file-chase-links dir))) 2)
204                    (directory-files dir t nil t))))
205     (while dirs 
206       (if (and (not (string-match "/\\.\\.?$" (car dirs)))
207                (file-directory-p (car dirs))
208                (file-readable-p (car dirs)))
209           (nnmh-request-list nil (car dirs)))
210       (setq dirs (cdr dirs))))
211   ;; For each directory, generate an active file line.
212   (if (not (string= (expand-file-name nnmh-directory) dir))
213       (let ((files (mapcar
214                     (lambda (name) (string-to-int name))
215                     (directory-files dir nil "^[0-9]+$" t))))
216         (if (null files)
217             ()
218           (save-excursion
219             (set-buffer nntp-server-buffer)
220             (insert 
221              (format 
222               "%s %d %d y\n" 
223               (progn
224                 (string-match (expand-file-name nnmh-directory) dir)
225                 (nnmail-replace-chars-in-string
226                  (substring dir (match-end 0)) ?/ ?.))
227               (apply (function max) files) 
228               (apply (function min) files)))))))
229   (setq nnmh-group-alist (nnmail-get-active))
230   (and server nnmh-get-new-mail (nnmh-get-new-mail))
231   t)
232
233 (defun nnmh-request-newgroups (date &optional server)
234   (nnmh-request-list server))
235
236 (defun nnmh-request-post (&optional server)
237   (mail-send-and-exit nil))
238
239 (defalias 'nnmh-request-post-buffer 'nnmail-request-post-buffer)
240
241 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
242   (nnmh-possibly-change-directory newsgroup)
243   (let* ((days (or (and nnmail-expiry-wait-function
244                         (funcall nnmail-expiry-wait-function newsgroup))
245                    nnmail-expiry-wait))
246          article rest mod-time)
247     (if nnmail-keep-last-article
248         (progn
249           (setq articles (sort articles '>))
250           (setq rest (cons (car articles) rest))
251           (setq articles (cdr articles))))
252     (while articles
253       (setq article (concat nnmh-current-directory (int-to-string
254                                                     (car articles))))
255       (if (setq mod-time (nth 5 (file-attributes article)))
256           (if (or force
257                   (> (nnmail-days-between
258                       (current-time-string)
259                       (current-time-string mod-time))
260                      days))
261               (progn
262                 (message "Deleting %s..." article)
263                 (condition-case ()
264                     (delete-file article)
265                   (file-error nil)))
266             (setq rest (cons (car articles) rest))))
267       (setq articles (cdr articles)))
268     rest))
269
270 (defun nnmh-close-group (group &optional server)
271   t)
272
273 (defun nnmh-request-move-article 
274   (article group server accept-form &optional last)
275   (let ((buf (get-buffer-create " *nnmh move*"))
276         result)
277     (and 
278      (nnmh-request-article article group server)
279      (save-excursion
280        (set-buffer buf)
281        (insert-buffer-substring nntp-server-buffer)
282        (setq result (eval accept-form))
283        (kill-buffer (current-buffer))
284        result)
285      (condition-case ()
286          (delete-file (concat nnmh-current-directory 
287                               (int-to-string article)))
288        (file-error nil)))
289  result))
290
291 (defun nnmh-request-accept-article (group &optional last)
292   (if (stringp group)
293       (and 
294        (nnmh-request-list)
295        (setq nnmh-group-alist (nnmail-get-active))
296        ;; We trick the choosing function into believing that only one
297        ;; group is availiable.  
298        (let ((nnmail-split-methods (list (list group ""))))
299          (car (nnmh-save-mail))))
300     (and
301      (nnmh-request-list)
302      (setq nnmh-group-alist (nnmail-get-active))
303      (car (nnmh-save-mail)))))
304
305 (defun nnmh-request-replace-article (article group buffer)
306   (nnmh-possibly-change-directory group)
307   (save-excursion
308     (set-buffer buffer)
309     (nnmh-possibly-create-directory group)
310     (condition-case ()
311         (progn
312           (write-region (point-min) (point-max)
313                         (concat nnmh-current-directory (int-to-string article))
314                         nil (if gnus-verbose-backends nil 'nomesg))
315           t)
316       (error nil))))
317
318 \f
319 ;;; Internal functions.
320
321 (defun nnmh-possibly-change-directory (newsgroup)
322   (if newsgroup
323       (let ((pathname (nnmh-article-pathname newsgroup nnmh-directory)))
324         (if (file-directory-p pathname)
325             (setq nnmh-current-directory pathname)
326           (error "No such newsgroup: %s" newsgroup)))))
327
328 (defun nnmh-possibly-create-directory (group)
329   (let (dir dirs)
330     (setq dir (nnmail-article-pathname group nnmh-directory))
331     (while (not (file-directory-p dir))
332       (setq dirs (cons dir dirs))
333       (setq dir (file-name-directory (directory-file-name dir))))
334     (while dirs
335       (if (make-directory (directory-file-name (car dirs)))
336           (error "Could not create directory %s" (car dirs)))
337       (and gnus-verbose-backends 
338            (message "Creating mail directory %s" (car dirs)))
339       (setq dirs (cdr dirs)))))
340              
341 (defun nnmh-save-mail ()
342   "Called narrowed to an article."
343   (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number)))
344         chars nov-line lines hbeg hend)
345     (setq chars (nnmail-insert-lines))
346     (nnmail-insert-xref group-art)
347     (run-hooks 'nnmh-prepare-save-mail-hook)
348     (goto-char (point-min))
349     (while (looking-at "From ")
350       (replace-match "X-From-Line: ")
351       (forward-line 1))
352     ;; We save the article in all the newsgroups it belongs in.
353     (let ((ga group-art)
354           first)
355       (while ga
356         (nnmh-possibly-create-directory (car (car ga)))
357         (let ((file (concat (nnmh-article-pathname 
358                              (car (car ga)) nnmh-directory) 
359                             (int-to-string (cdr (car ga))))))
360           (if first
361               ;; It was already saved, so we just make a hard link.
362               (add-name-to-file first file t)
363             ;; Save the article.
364             (write-region (point-min) (point-max) file nil nil)
365             (setq first file)))
366         (setq ga (cdr ga))))
367     group-art))
368
369 (defun nnmh-active-number (group)
370   "Compute the next article number in GROUP."
371   (let ((active (car (cdr (assoc group nnmh-group-alist)))))
372     (setcdr active (1+ (cdr active)))
373     (let (file)
374       (while (file-exists-p
375               (setq file (concat (nnmh-article-pathname 
376                                   group nnmh-directory)
377                                  (int-to-string (cdr active)))))
378         (setcdr active (1+ (cdr active)))))
379     (cdr active)))
380
381 (defun nnmh-article-pathname (group mail-dir)
382   "Make pathname for GROUP."
383   (let ((mail-dir (file-name-as-directory (expand-file-name mail-dir))))
384     (if (file-directory-p (concat mail-dir group))
385         (concat mail-dir group "/")
386       (concat mail-dir (nnmail-replace-chars-in-string group ?. ?/) "/"))))
387
388 (defun nnmh-get-new-mail (&optional group)
389   "Read new incoming mail."
390   (let ((spools (nnmail-get-spool-files group))
391         incoming incomings)
392     (if (or (not nnmh-get-new-mail) (not nnmail-spool-file))
393         ()
394       ;; We first activate all the groups.
395       (or nnmh-group-alist
396           (nnmh-request-list))
397       ;; The we go through all the existing spool files and split the
398       ;; mail from each.
399       (while spools
400         (and
401          (file-exists-p (car spools))
402          (> (nth 7 (file-attributes (car spools))) 0)
403          (progn
404            (and gnus-verbose-backends 
405                 (message "nnmh: Reading incoming mail..."))
406            (setq incoming 
407                  (nnmail-move-inbox 
408                   (car spools) (concat nnmh-directory "Incoming")))
409            (setq incomings (cons incoming incomings))
410            (nnmail-split-incoming incoming 'nnmh-save-mail nil group)))
411         (setq spools (cdr spools)))
412       ;; If we did indeed read any incoming spools, we save all info. 
413       (if incoming 
414           (message "nnmh: Reading incoming mail...done"))
415       (while incomings
416         (and nnmail-delete-incoming
417              (file-writable-p incoming)
418              (delete-file incoming))
419         (setq incomings (cdr incomings))))))
420       
421
422 (defun nnmh-update-gnus-unreads (group)
423   ;; Go through the .nnmh-articles file and compare with the actual
424   ;; articles in this folder. The articles that are "new" will be
425   ;; marked as unread by Gnus.
426   (let* ((dir nnmh-current-directory)
427          (files (sort (mapcar (function (lambda (name) (string-to-int name)))
428                               (directory-files nnmh-current-directory 
429                                                nil "^[0-9]+$" t)) '<))
430          (nnmh-file (concat dir ".nnmh-articles"))
431          new articles)
432     ;; Load the .nnmh-articles file.
433     (if (file-exists-p nnmh-file)
434         (setq articles 
435               (let (nnmh-newsgroup-articles)
436                 (condition-case nil (load nnmh-file nil t t) (error nil))
437                 nnmh-newsgroup-articles)))
438     ;; Add all new articles to the `new' list.
439     (let ((art files))
440       (while art
441         (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
442         (setq art (cdr art))))
443     ;; Remove all deleted articles.
444     (let ((art articles))
445       (while art
446         (if (not (memq (car (car art)) files))
447             (setq articles (delq (car art) articles)))
448         (setq art (cdr art))))
449     ;; Check whether the highest-numbered articles really are the ones
450     ;; that Gnus thinks they are by looking at the time-stamps.
451     (let ((art articles))
452       (while (and art 
453                   (not (equal 
454                         (nth 5 (file-attributes 
455                                 (concat dir (int-to-string (car (car art))))))
456                         (cdr (car art)))))
457         (setq articles (delq (car art) articles))
458         (setq new (cons (car (car art)) new))
459         (setq art (cdr art))))
460     ;; Go through all the new articles and add them, and their
461     ;; time-stamps to the list.
462     (let ((n new))
463       (while n
464         (setq articles 
465               (cons (cons 
466                      (car n)
467                      (nth 5 (file-attributes 
468                              (concat dir (int-to-string (car n))))))
469                     articles))
470         (setq n (cdr n))))
471     ;; Make Gnus mark all new articles as unread.
472     (or (zerop (length new))
473         (gnus-make-articles-unread 
474          (gnus-group-prefixed-name group (list 'nnmh ""))
475          (setq new (sort new '<))))
476     ;; Sort the article list with highest numbers first.
477     (setq articles (sort articles (lambda (art1 art2) 
478                                     (> (car art1) (car art2)))))
479     ;; Finally write this list back to the .nnmh-articles file.
480     (save-excursion
481       (set-buffer (get-buffer-create "*nnmh out*"))
482       (insert ";; Gnus article active file for " group "\n\n")
483       (insert "(setq nnmh-newsgroup-articles '")
484       (insert (prin1-to-string articles) ")\n")
485       (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
486       (kill-buffer (current-buffer)))))
487
488 (provide 'nnmh)
489
490 ;;; nnmh.el ends here