*** 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 1.0"
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       (setq nnmh-status-string "No such group")
194       nil)))
195
196 (defun nnmh-request-list (&optional server dir)
197   (or dir
198       (save-excursion
199         (set-buffer nntp-server-buffer)
200         (erase-buffer)
201         (setq dir (file-name-as-directory nnmh-directory))))
202   (setq dir (expand-file-name dir))
203   ;; Recurse down all directories.
204   (let ((dirs (and (file-readable-p dir)
205                    (> (nth 1 (file-attributes (file-chase-links dir))) 2)
206                    (directory-files dir t nil t))))
207     (while dirs 
208       (if (and (not (string-match "/\\.\\.?$" (car dirs)))
209                (file-directory-p (car dirs))
210                (file-readable-p (car dirs)))
211           (nnmh-request-list nil (car dirs)))
212       (setq dirs (cdr dirs))))
213   ;; For each directory, generate an active file line.
214   (if (not (string= (expand-file-name nnmh-directory) dir))
215       (let ((files (mapcar
216                     (lambda (name) (string-to-int name))
217                     (directory-files dir nil "^[0-9]+$" t))))
218         (if (null files)
219             ()
220           (save-excursion
221             (set-buffer nntp-server-buffer)
222             (goto-char (point-max))
223             (insert 
224              (format 
225               "%s %d %d y\n" 
226               (progn
227                 (string-match (file-name-as-directory 
228                                (expand-file-name nnmh-directory)) dir)
229                 (nnmail-replace-chars-in-string
230                  (substring dir (match-end 0)) ?/ ?.))
231               (apply (function max) files) 
232               (apply (function min) files)))))))
233   (setq nnmh-group-alist (nnmail-get-active))
234   (and server nnmh-get-new-mail (nnmh-get-new-mail))
235   t)
236
237 (defun nnmh-request-newgroups (date &optional server)
238   (nnmh-request-list server))
239
240 (defun nnmh-request-post (&optional server)
241   (mail-send-and-exit nil))
242
243 (defalias 'nnmh-request-post-buffer 'nnmail-request-post-buffer)
244
245 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
246   (nnmh-possibly-change-directory newsgroup)
247   (let* ((days (or (and nnmail-expiry-wait-function
248                         (funcall nnmail-expiry-wait-function newsgroup))
249                    nnmail-expiry-wait))
250          (active-articles 
251           (mapcar
252            (function
253             (lambda (name)
254               (string-to-int name)))
255            (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
256          (max-article (and active-articles (apply 'max active-articles)))
257          (is-old t)
258          article rest mod-time)
259     (nnmail-activate 'nnmh)
260
261     (while (and articles is-old)
262       (setq article (concat nnmh-current-directory 
263                             (int-to-string (car articles))))
264       (if (setq mod-time (nth 5 (file-attributes article)))
265           (if (and (or (not nnmail-keep-last-article)
266                        (not max-article)
267                        (not (= (car articles) max-article)))
268                    (not (equal mod-time '(0 0)))
269                    (or force
270                        (setq is-old
271                              (> (nnmail-days-between
272                                  (current-time-string)
273                                  (current-time-string mod-time))
274                                 days))))
275               (progn
276                 (and gnus-verbose-backends 
277                      (message "Deleting article %s..." article))
278                 (condition-case ()
279                     (delete-file article)
280                   (file-error
281                    (setq rest (cons (car articles) rest)))))
282             (setq rest (cons (car articles) rest))))
283       (setq articles (cdr articles)))
284     (message "")
285     (nconc rest articles)))
286
287 (defun nnmh-close-group (group &optional server)
288   t)
289
290 (defun nnmh-request-move-article 
291   (article group server accept-form &optional last)
292   (let ((buf (get-buffer-create " *nnmh move*"))
293         result)
294     (and 
295      (nnmh-request-article article group server)
296      (save-excursion
297        (set-buffer buf)
298        (insert-buffer-substring nntp-server-buffer)
299        (setq result (eval accept-form))
300        (kill-buffer (current-buffer))
301        result)
302      (condition-case ()
303          (delete-file (concat nnmh-current-directory 
304                               (int-to-string article)))
305        (file-error nil)))
306     result))
307
308 (defun nnmh-request-accept-article (group &optional last)
309   (if (stringp group)
310       (and 
311        (nnmail-activate 'nnmh)
312        ;; We trick the choosing function into believing that only one
313        ;; group is availiable.  
314        (let ((nnmail-split-methods (list (list group ""))))
315          (car (nnmh-save-mail))))
316     (and
317      (nnmail-activate 'nnmh)
318      (car (nnmh-save-mail)))))
319
320 (defun nnmh-request-replace-article (article group buffer)
321   (nnmh-possibly-change-directory group)
322   (save-excursion
323     (set-buffer buffer)
324     (nnmh-possibly-create-directory group)
325     (condition-case ()
326         (progn
327           (write-region (point-min) (point-max)
328                         (concat nnmh-current-directory (int-to-string article))
329                         nil (if gnus-verbose-backends nil 'nomesg))
330           t)
331       (error nil))))
332
333 \f
334 ;;; Internal functions.
335
336 (defun nnmh-possibly-change-directory (newsgroup)
337   (if newsgroup
338       (let ((pathname (nnmh-article-pathname newsgroup nnmh-directory)))
339         (if (file-directory-p pathname)
340             (setq nnmh-current-directory pathname)
341           (error "No such newsgroup: %s" newsgroup)))))
342
343 (defun nnmh-possibly-create-directory (group)
344   (let (dir dirs)
345     (setq dir (nnmh-article-pathname group nnmh-directory))
346     (while (not (file-directory-p dir))
347       (setq dirs (cons dir dirs))
348       (setq dir (file-name-directory (directory-file-name dir))))
349     (while dirs
350       (if (make-directory (directory-file-name (car dirs)))
351           (error "Could not create directory %s" (car dirs)))
352       (and gnus-verbose-backends 
353            (message "Creating mail directory %s" (car dirs)))
354       (setq dirs (cdr dirs)))))
355              
356 (defun nnmh-save-mail ()
357   "Called narrowed to an article."
358   (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
359     (nnmail-insert-lines)
360     (nnmail-insert-xref group-art)
361     (run-hooks 'nnmh-prepare-save-mail-hook)
362     (goto-char (point-min))
363     (while (looking-at "From ")
364       (replace-match "X-From-Line: ")
365       (forward-line 1))
366     ;; We save the article in all the newsgroups it belongs in.
367     (let ((ga group-art)
368           first)
369       (while ga
370         (nnmh-possibly-create-directory (car (car ga)))
371         (let ((file (concat (nnmh-article-pathname 
372                              (car (car ga)) nnmh-directory) 
373                             (int-to-string (cdr (car ga))))))
374           (if first
375               ;; It was already saved, so we just make a hard link.
376               (add-name-to-file first file t)
377             ;; Save the article.
378             (write-region (point-min) (point-max) file nil nil)
379             (setq first file)))
380         (setq ga (cdr ga))))
381     group-art))
382
383 (defun nnmh-active-number (group)
384   "Compute the next article number in GROUP."
385   (let ((active (car (cdr (assoc group nnmh-group-alist)))))
386     ;; The group wasn't known to nnmh, so we just create an active
387     ;; entry for it.   
388     (or active
389         (progn
390           (setq active (cons 1 0))
391           (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
392     (setcdr active (1+ (cdr active)))
393     (while (file-exists-p
394             (concat (nnmh-article-pathname group nnmh-directory)
395                     (int-to-string (cdr active))))
396       (setcdr active (1+ (cdr active))))
397     (cdr active)))
398
399 (defun nnmh-article-pathname (group mail-dir)
400   "Make pathname for GROUP."
401   (let ((mail-dir (file-name-as-directory (expand-file-name mail-dir))))
402     (if (file-directory-p (concat mail-dir group))
403         (concat mail-dir group "/")
404       (concat mail-dir (nnmail-replace-chars-in-string group ?. ?/) "/"))))
405
406 (defun nnmh-get-new-mail (&optional group)
407   "Read new incoming mail."
408   (let* ((spools (nnmail-get-spool-files group))
409          (group-in group)
410          incoming incomings)
411     (if (or (not nnmh-get-new-mail) (not nnmail-spool-file))
412         ()
413       ;; We first activate all the groups.
414       (or nnmh-group-alist
415           (nnmh-request-list))
416       ;; The we go through all the existing spool files and split the
417       ;; mail from each.
418       (while spools
419         (and
420          (file-exists-p (car spools))
421          (> (nth 7 (file-attributes (car spools))) 0)
422          (progn
423            (and gnus-verbose-backends 
424                 (message "nnmh: Reading incoming mail..."))
425            (if (not (setq incoming 
426                           (nnmail-move-inbox 
427                            (car spools) 
428                            (concat (file-name-as-directory nnmh-directory)
429                                    "Incoming"))))
430                ()
431              (setq incomings (cons incoming incomings))
432              (setq group (nnmail-get-split-group (car spools) group-in))
433              (nnmail-split-incoming incoming 'nnmh-save-mail nil group))))
434         (setq spools (cdr spools)))
435       ;; If we did indeed read any incoming spools, we save all info. 
436       (if incoming 
437           (message "nnmh: Reading incoming mail...done"))
438       (while incomings
439         (setq incoming (car incomings))
440         (and nnmail-delete-incoming
441              (file-exists-p incoming)
442              (file-writable-p incoming)
443              (delete-file incoming))
444         (setq incomings (cdr incomings))))))
445       
446
447 (defun nnmh-update-gnus-unreads (group)
448   ;; Go through the .nnmh-articles file and compare with the actual
449   ;; articles in this folder. The articles that are "new" will be
450   ;; marked as unread by Gnus.
451   (let* ((dir nnmh-current-directory)
452          (files (sort (mapcar (function (lambda (name) (string-to-int name)))
453                               (directory-files nnmh-current-directory 
454                                                nil "^[0-9]+$" t)) '<))
455          (nnmh-file (concat dir ".nnmh-articles"))
456          new articles)
457     ;; Load the .nnmh-articles file.
458     (if (file-exists-p nnmh-file)
459         (setq articles 
460               (let (nnmh-newsgroup-articles)
461                 (condition-case nil (load nnmh-file nil t t) (error nil))
462                 nnmh-newsgroup-articles)))
463     ;; Add all new articles to the `new' list.
464     (let ((art files))
465       (while art
466         (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
467         (setq art (cdr art))))
468     ;; Remove all deleted articles.
469     (let ((art articles))
470       (while art
471         (if (not (memq (car (car art)) files))
472             (setq articles (delq (car art) articles)))
473         (setq art (cdr art))))
474     ;; Check whether the highest-numbered articles really are the ones
475     ;; that Gnus thinks they are by looking at the time-stamps.
476     (let ((art articles))
477       (while (and art 
478                   (not (equal 
479                         (nth 5 (file-attributes 
480                                 (concat dir (int-to-string (car (car art))))))
481                         (cdr (car art)))))
482         (setq articles (delq (car art) articles))
483         (setq new (cons (car (car art)) new))
484         (setq art (cdr art))))
485     ;; Go through all the new articles and add them, and their
486     ;; time-stamps to the list.
487     (let ((n new))
488       (while n
489         (setq articles 
490               (cons (cons 
491                      (car n)
492                      (nth 5 (file-attributes 
493                              (concat dir (int-to-string (car n))))))
494                     articles))
495         (setq n (cdr n))))
496     ;; Make Gnus mark all new articles as unread.
497     (or (zerop (length new))
498         (gnus-make-articles-unread 
499          (gnus-group-prefixed-name group (list 'nnmh ""))
500          (setq new (sort new '<))))
501     ;; Sort the article list with highest numbers first.
502     (setq articles (sort articles (lambda (art1 art2) 
503                                     (> (car art1) (car art2)))))
504     ;; Finally write this list back to the .nnmh-articles file.
505     (save-excursion
506       (set-buffer (get-buffer-create "*nnmh out*"))
507       (insert ";; Gnus article active file for " group "\n\n")
508       (insert "(setq nnmh-newsgroup-articles '")
509       (insert (prin1-to-string articles) ")\n")
510       (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
511       (kill-buffer (current-buffer)))))
512
513 (provide 'nnmh)
514
515 ;;; nnmh.el ends here