*** empty log message ***
[gnus] / lisp / nnml.el
1 ;;; nnml.el --- mail spool 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 'nnmail)
34
35 (defvar nnml-directory "~/Mail/"
36   "Mail directory.")
37
38 (defvar nnml-active-file (concat nnml-directory "active")
39   "Mail active file.")
40
41 (defvar nnml-newsgroups-file (concat nnml-directory "newsgroups")
42   "Mail newsgroups description file.")
43
44 (defvar nnml-get-new-mail t
45   "If non-nil, nnml will check the incoming mail file and split the mail.")
46
47 (defvar nnml-nov-is-evil nil
48   "If non-nil, Gnus will never generate and use nov databases for mail groups.
49 Using nov databases will speed up header fetching considerably.
50 This variable shouldn't be flipped much. If you have, for some reason,
51 set this to t, and want to set it to nil again, you should always run
52 the `nnml-generate-nov-databases' command. The function will go
53 through all nnml directories and generate nov databases for them
54 all. This may very well take some time.")
55
56 \f
57
58 (defconst nnml-version "nnml 0.2"
59   "nnml version.")
60
61 (defvar nnml-current-directory nil
62   "Current news group directory.")
63
64 (defvar nnml-status-string "")
65
66 (defvar nnml-nov-buffer-alist nil)
67
68 (defvar nnml-group-alist nil)
69 (defvar nnml-active-timestamp nil)
70
71 \f
72
73 ;;; Interface functions.
74
75 (defun nnml-retrieve-headers (sequence &optional newsgroup server)
76   (save-excursion
77     (set-buffer nntp-server-buffer)
78     (erase-buffer)
79     (let ((file nil)
80           (number (length sequence))
81           (count 0)
82           beg article)
83       (nnml-possibly-change-directory newsgroup)
84       (if (nnml-retrieve-headers-with-nov sequence)
85           'nov
86         (while sequence
87           (setq article (car sequence))
88           (setq file
89                 (concat nnml-current-directory (prin1-to-string article)))
90           (if (and (file-exists-p file)
91                    (not (file-directory-p file)))
92               (progn
93                 (insert (format "221 %d Article retrieved.\n" article))
94                 (setq beg (point))
95                 (insert-file-contents file)
96                 (goto-char beg)
97                 (if (search-forward "\n\n" nil t)
98                     (forward-char -1)
99                   (goto-char (point-max))
100                   (insert "\n\n"))
101                 (insert ".\n")
102                 (delete-region (point) (point-max))))
103           (setq sequence (cdr sequence))
104           (setq count (1+ count))
105           (and (numberp nnmail-large-newsgroup)
106                (> number nnmail-large-newsgroup)
107                (zerop (% count 20))
108                gnus-verbose-backends
109                (message "nnml: Receiving headers... %d%%"
110                         (/ (* count 100) number))))
111
112         (and (numberp nnmail-large-newsgroup)
113              (> number nnmail-large-newsgroup)
114              gnus-verbose-backends
115              (message "nnml: Receiving headers... done"))
116
117         ;; Fold continuation lines.
118         (goto-char 1)
119         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
120           (replace-match " " t t))
121         'headers))))
122
123 (defun nnml-open-server (host &optional service)
124   (setq nnml-status-string "")
125   (nnheader-init-server-buffer))
126
127 (defun nnml-close-server (&optional server)
128   t)
129
130 (defun nnml-server-opened (&optional server)
131   (and nntp-server-buffer
132        (get-buffer nntp-server-buffer)))
133
134 (defun nnml-status-message (&optional server)
135   nnml-status-string)
136
137 (defun nnml-request-article (id &optional newsgroup server buffer)
138   (nnml-possibly-change-directory newsgroup)
139   (let ((file (if (stringp id)
140                   nil
141                 (concat nnml-current-directory (prin1-to-string id))))
142         (nntp-server-buffer (or buffer nntp-server-buffer)))
143     (if (and (stringp file)
144              (file-exists-p file)
145              (not (file-directory-p file)))
146         (save-excursion
147           (nnmail-find-file file)))))
148
149 (defun nnml-request-group (group &optional server dont-check)
150   (if (not (nnml-possibly-change-directory group))
151       (progn
152         (setq nnml-status-string "Invalid group (no such directory)")
153         nil)
154     (if dont-check 
155         t
156       (nnml-get-new-mail)
157       (let ((timestamp (nth 5 (file-attributes nnml-active-file))))
158         (if (or (not nnml-active-timestamp)
159                 (> (nth 0 timestamp) (nth 0 nnml-active-timestamp))
160                 (> (nth 1 timestamp) (nth 1 nnml-active-timestamp)))
161             (progn
162               (setq nnml-active-timestamp timestamp)
163               (nnml-request-list)
164               (setq nnml-group-alist (nnmail-get-active))))
165         (let ((active (nth 1 (assoc group nnml-group-alist))))
166           (save-excursion
167             (set-buffer nntp-server-buffer)
168             (erase-buffer)
169             (if (not active)
170                 ()
171               (insert (format "211 %d %d %d %s\n" 
172                               (max (1+ (- (cdr active) (car active))) 0)
173                               (car active) (cdr active) group))
174               t)))))))
175
176 (defun nnml-close-group (group &optional server)
177   t)
178
179 (defun nnml-request-list (&optional server)
180   (if server (nnml-get-new-mail))
181   (save-excursion
182     (nnmail-find-file nnml-active-file)))
183
184 (defun nnml-request-newgroups (date &optional server)
185   (nnml-request-list server))
186
187 (defun nnml-request-list-newsgroups (&optional server)
188   (save-excursion
189     (nnmail-find-file nnml-newsgroups-file)))
190
191 (defun nnml-request-post (&optional server)
192   (mail-send-and-exit nil))
193
194 (fset 'nnml-request-post-buffer 'nnmail-request-post-buffer)
195
196 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
197   (nnml-possibly-change-directory newsgroup)
198   (let* ((days (or (and nnmail-expiry-wait-function
199                         (funcall nnmail-expiry-wait-function newsgroup))
200                    nnmail-expiry-wait))
201          (active-articles 
202           (mapcar
203            (function
204             (lambda (name)
205               (string-to-int name)))
206            (directory-files nnml-current-directory nil "^[0-9]+$" t)))
207          (max-article (max active-articles))
208          article rest mod-time)
209     (while articles
210       (setq article (concat nnml-current-directory (int-to-string
211                                                       (car articles))))
212       (if (setq mod-time (nth 5 (file-attributes article)))
213           (if (and (or (not nnmail-keep-last-article)
214                        (not (= (car articles) max-article)))
215                    (or force
216                        (> (nnmail-days-between
217                            (current-time-string)
218                            (current-time-string mod-time))
219                           days)))
220               (progn
221                 (and gnus-verbose-backends (message "Deleting %s..." article))
222                 (condition-case ()
223                     (delete-file article)
224                   (file-error nil))
225                 (setq active-articles (delq (car articles) active-articles))
226                 (nnml-nov-delete-article newsgroup (car articles)))
227             (setq rest (cons (car articles) rest))))
228       (setq articles (cdr articles)))
229     (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
230       (setcar active (min active-articles))
231       (nnmail-save-active nnml-group-alist nnml-active-file))
232     (nnml-save-nov)
233     rest))
234
235 (defun nnml-request-move-article 
236   (article group server accept-form &optional last)
237   (let ((buf (get-buffer-create " *nnml move*"))
238         result)
239     (and 
240      (nnml-request-article article group server)
241      (save-excursion
242        (set-buffer buf)
243        (insert-buffer-substring nntp-server-buffer)
244        (setq result (eval accept-form))
245        (kill-buffer (current-buffer))
246        result)
247      (progn
248        (condition-case ()
249            (delete-file (concat nnml-current-directory 
250                                 (int-to-string article)))
251          (file-error nil))
252        (nnml-nov-delete-article group article)
253        (and last (nnml-save-nov))))
254     result))
255
256 (defun nnml-request-accept-article (group &optional last)
257   (let (result)
258     (if (stringp group)
259         (and 
260          (nnml-request-list)
261          (setq nnml-group-alist (nnmail-get-active))
262          ;; We trick the choosing function into believing that only one
263          ;; group is availiable.  
264          (let ((nnmail-split-methods (list (list group ""))))
265            (setq result (car (nnml-save-mail))))
266          (progn
267            (nnmail-save-active nnml-group-alist nnml-active-file)
268            (and last (nnml-save-nov))))
269       (and
270        (nnml-request-list)
271        (setq nnml-group-alist (nnmail-get-active))
272        (setq result (car (nnml-save-mail)))
273        (progn
274          (nnmail-save-active nnml-group-alist nnml-active-file)
275          (and last (nnml-save-nov)))))
276     result))
277
278 (defun nnml-request-replace-article (article group buffer)
279   (nnml-possibly-change-directory group)
280   (save-excursion
281     (set-buffer buffer)
282     (if (not (condition-case ()
283                  (progn
284                    (write-region (point-min) (point-max)
285                                  (concat nnml-current-directory 
286                                          (int-to-string article))
287                                  nil (if gnus-verbose-backends nil 'nomesg))
288                    t)
289                (error nil)))
290         ()
291       (let ((chars (nnmail-insert-lines))
292             (art (concat (int-to-string article) "\t"))
293             nov-line)
294         (setq nov-line (nnml-make-nov-line chars))
295         (save-excursion 
296           (set-buffer (nnml-open-nov group))
297           (goto-char (point-min))
298           (if (or (looking-at art)
299                   (search-forward (concat "\n" art)))
300               (progn
301                 (delete-region (progn (beginning-of-line) (point))
302                                (progn (forward-line 1) (point)))
303                 (insert (int-to-string article) nov-line)
304                 (nnml-save-nov))
305             (kill-buffer (current-buffer)))
306           t)))))
307
308
309 \f
310 ;;; Internal functions
311
312 (defun nnml-retrieve-headers-with-nov (articles)
313   (if (or gnus-nov-is-evil nnml-nov-is-evil)
314       nil
315     (let ((first (car articles))
316           (last (progn (while (cdr articles) (setq articles (cdr articles)))
317                        (car articles)))
318           (nov (concat nnml-current-directory ".nov")))
319       (if (file-exists-p nov)
320           (save-excursion
321             (set-buffer nntp-server-buffer)
322             (erase-buffer)
323             (insert-file-contents nov)
324             (goto-char 1)
325             (while (and (not (eobp)) (< first (read (current-buffer))))
326               (forward-line 1))
327             (beginning-of-line)
328             (if (not (eobp)) (delete-region 1 (point)))
329             (while (and (not (eobp)) (>= last (read (current-buffer))))
330               (forward-line 1))
331             (beginning-of-line)
332             (if (not (eobp)) (delete-region (point) (point-max)))
333             t)))))
334
335 (defun nnml-possibly-change-directory (newsgroup)
336   (if newsgroup
337       (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
338         (and (file-directory-p pathname)
339              (setq nnml-current-directory pathname)))
340     t))
341              
342 (defun nnml-create-directories ()
343   (let ((methods nnmail-split-methods)
344         dir dirs)
345     (while methods
346       (setq dir (nnmail-article-pathname (car (car methods)) nnml-directory))
347       (while (not (file-directory-p dir))
348         (setq dirs (cons dir dirs))
349         (setq dir (file-name-directory (directory-file-name dir))))
350       (while dirs
351         (if (make-directory (directory-file-name (car dirs)))
352             (error "Could not create directory %s" (car dirs)))
353         (and gnus-verbose-backends 
354              (message "Creating mail directory %s" (car dirs)))
355         (setq dirs (cdr dirs)))
356       (setq methods (cdr methods)))))
357
358 (defun nnml-save-mail ()
359   "Called narrowed to an article."
360   (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
361         chars nov-line)
362     (setq chars (nnmail-insert-lines))
363     (nnmail-insert-xref group-art)
364     (goto-char (point-min))
365     (while (looking-at "From ")
366       (replace-match "X-From-Line: ")
367       (forward-line 1))
368     ;; We save the article in all the newsgroups it belongs in.
369     (let ((ga group-art)
370           first)
371       (while ga
372         (let ((file (concat (nnmail-article-pathname 
373                              (car (car ga)) nnml-directory)
374                             (int-to-string (cdr (car ga))))))
375           (if first
376               ;; It was already saved, so we just make a hard link.
377               (add-name-to-file first file t)
378             ;; Save the article.
379             (write-region (point-min) (point-max) file nil 
380                           (if gnus-verbose-backends nil 'nomesg))
381             (setq first file)))
382         (setq ga (cdr ga))))
383     ;; Generate a nov line for this article. We generate the nov
384     ;; line after saving, because nov generation destroys the
385     ;; header. 
386     (setq nov-line (nnml-make-nov-line chars))
387     ;; Output the nov line to all nov databases that should have it.
388     (let ((ga group-art))
389       (while ga
390         (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
391         (setq ga (cdr ga))))
392     group-art))
393
394 (defun nnml-active-number (group)
395   "Compute the next article number in GROUP."
396   (let ((active (car (cdr (assoc group nnml-group-alist)))))
397     (setcdr active (1+ (cdr active)))
398     (let (file)
399       (while (file-exists-p
400               (setq file (concat (nnmail-article-pathname 
401                                   group nnml-directory)
402                                  (int-to-string (cdr active)))))
403         (setcdr active (1+ (cdr active)))))
404     (cdr active)))
405
406 (defun nnml-get-new-mail ()
407   "Read new incoming mail."
408   (let (incoming)
409     (nnml-create-directories)
410     (if (and nnml-get-new-mail nnmail-spool-file
411              (file-exists-p nnmail-spool-file)
412              (> (nth 7 (file-attributes nnmail-spool-file)) 0))
413         (progn
414           (and gnus-verbose-backends 
415                (message "nnml: Reading incoming mail..."))
416           (setq incoming 
417                 (nnmail-move-inbox nnmail-spool-file 
418                                    (concat nnml-directory "Incoming")))
419           (nnml-request-list)
420           (setq nnml-group-alist (nnmail-get-active))
421           (nnmail-split-incoming incoming 'nnml-save-mail)
422           (nnmail-save-active nnml-group-alist nnml-active-file)
423           (nnml-save-nov)
424           (run-hooks 'nnmail-read-incoming-hook)
425           ;; The following has been commented away, just to make sure
426           ;; that nobody ever loses any mail. If you feel safe that
427           ;; nnml will never do anything strange, just remove those
428           ;; two semicolons, and avoid having lots of "Incoming*"
429           ;; files. 
430 ;;         (delete-file incoming)
431           (and gnus-verbose-backends
432                (message "nnml: Reading incoming mail...done"))))))
433
434
435 (defun nnml-add-nov (group article line)
436   "Add a nov line for the GROUP base."
437   (save-excursion 
438     (set-buffer (nnml-open-nov group))
439     (goto-char (point-max))
440     (insert (int-to-string article) line)))
441
442 (defsubst nnml-header-value ()
443   (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
444
445 (defun nnml-make-nov-line (chars)
446   "Create a nov from the current headers."
447   (let ((case-fold-search t)
448         subject from date id references lines xref in-reply-to char)
449     (save-excursion
450       (save-restriction
451         (goto-char (point-min))
452         (narrow-to-region 
453          (point)
454          (1- (or (search-forward "\n\n" nil t) (point-max))))
455         ;; Fold continuation lines.
456         (goto-char (point-min))
457         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
458           (replace-match " " t t))
459         (subst-char-in-region (point-min) (point-max) ?\t ? )
460         ;; [number subject from date id references chars lines xref]
461         (save-excursion
462           (goto-char (point-min))
463           (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
464                                     nil t)
465             (beginning-of-line)
466             (setq char (downcase (following-char))) 
467             (cond
468              ((eq char ?s)
469               (setq subject (nnml-header-value)))
470              ((eq char ?f)
471               (setq from (nnml-header-value)))
472              ((eq char ?x)
473               (setq xref (nnml-header-value)))
474              ((eq char ?l)
475               (setq lines (nnml-header-value)))
476              ((eq char ?d)
477               (setq date (nnml-header-value)))
478              ((eq char ?m)
479               (setq id (setq id (nnml-header-value))))
480              ((eq char ?r)
481               (setq references (nnml-header-value)))
482              ((eq char ?i)
483               (setq in-reply-to (nnml-header-value))))
484             (forward-line 1))
485       
486           (and (not references)
487                in-reply-to
488                (string-match "<[^>]+>" in-reply-to)
489                (setq references
490                      (substring in-reply-to (match-beginning 0)
491                                 (match-end 0)))))
492         ;; [number subject from date id references chars lines xref]
493         (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
494                 (or subject "(none)")
495                 (or from "(nobody)") (or date "")
496                 (or id "") (or references "")
497                 (or chars 0) (or lines "0") (or xref ""))))))
498
499 (defun nnml-open-nov (group)
500   (or (cdr (assoc group nnml-nov-buffer-alist))
501       (let ((buffer (find-file-noselect 
502                      (concat (nnmail-article-pathname 
503                               group nnml-directory) ".nov"))))
504         (save-excursion
505           (set-buffer buffer)
506           (buffer-disable-undo (current-buffer)))
507         (setq nnml-nov-buffer-alist 
508               (cons (cons group buffer) nnml-nov-buffer-alist))
509         buffer)))
510
511 (defun nnml-save-nov ()
512   (save-excursion
513     (while nnml-nov-buffer-alist
514       (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
515           (progn
516             (set-buffer (cdr (car nnml-nov-buffer-alist)))
517             (and (buffer-modified-p)
518                  (write-region 
519                   1 (point-max) (buffer-file-name) nil 'nomesg))
520             (set-buffer-modified-p nil)
521             (kill-buffer (current-buffer))))
522       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
523
524 ;;;###autoload
525 (defun nnml-generate-nov-databases (dir)
526   "Generate nov databases in all nnml mail newsgroups."
527   (interactive 
528    (progn   
529      (setq nnml-group-alist nil)
530      (list nnml-directory)))
531   (nnml-open-server (system-name))
532   (let ((dirs (directory-files dir t nil t)))
533     (while dirs 
534       (if (and (not (string-match "/\\.\\.$" (car dirs)))
535                (not (string-match "/\\.$" (car dirs)))
536                (file-directory-p (car dirs)))
537           (nnml-generate-nov-databases (car dirs)))
538       (setq dirs (cdr dirs))))
539   (let ((files (sort
540                 (mapcar
541                  (function
542                   (lambda (name)
543                     (string-to-int name)))
544                  (directory-files dir nil "^[0-9]+$" t))
545                 (function <)))
546         (nov (concat dir "/.nov"))
547         (nov-buffer (get-buffer-create "*nov*"))
548         nov-line chars)
549     (if files
550         (setq nnml-group-alist 
551               (cons (list (nnmail-replace-chars-in-string 
552                            (substring (expand-file-name dir)
553                                       (length (expand-file-name 
554                                                nnml-directory)))
555                            ?/ ?.)
556                           (cons (car files)
557                                 (let ((f files))
558                                   (while (cdr f) (setq f (cdr f)))
559                                   (car f))))
560                     nnml-group-alist)))
561     (if files
562         (save-excursion
563           (set-buffer nntp-server-buffer)
564           (if (file-exists-p nov)
565               (delete-file nov))
566           (save-excursion
567             (set-buffer nov-buffer)
568             (buffer-disable-undo (current-buffer))
569             (erase-buffer))
570           (while files
571             (erase-buffer)
572             (insert-file-contents (concat dir "/" (int-to-string (car files))))
573             (goto-char 1)
574             (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
575                                                 (setq chars (- (point-max) 
576                                                                (point)))
577                                                 (point)))
578             (setq nov-line (nnml-make-nov-line chars))
579             (save-excursion
580               (set-buffer nov-buffer)
581               (goto-char (point-max))
582               (insert (int-to-string (car files)) nov-line))
583             (widen)
584             (setq files (cdr files)))
585           (save-excursion
586             (set-buffer nov-buffer)
587             (write-region 1 (point-max) (expand-file-name nov) nil
588                           'nomesg)
589             (kill-buffer (current-buffer)))))
590     (nnmail-save-active nnml-group-alist nnml-active-file)))
591
592 (defun nnml-nov-delete-article (group article)
593   (save-excursion
594     (set-buffer (nnml-open-nov group))
595     (goto-char 1)
596     (if (re-search-forward (concat "^" (int-to-string article) "\t"))
597         (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
598     t))
599
600 (provide 'nnml)
601
602 ;;; nnml.el ends here