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