*** 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   (setq nnml-status-string "")
124   (nnheader-init-server-buffer))
125
126 (defun nnml-close-server (&optional server)
127   "Close news server."
128   t)
129
130 (defun nnml-server-opened (&optional server)
131   "Return server process status, T or NIL.
132 If the stream is opened, return T, otherwise return NIL."
133   (and nntp-server-buffer
134        (get-buffer nntp-server-buffer)))
135
136 (defun nnml-status-message ()
137   "Return server status response as string."
138   nnml-status-string)
139
140 (defun nnml-request-article (id &optional newsgroup server buffer)
141   "Select article by message ID (or number)."
142   (nnml-possibly-change-directory newsgroup)
143   (let ((file (if (stringp id)
144                   nil
145                 (concat nnml-current-directory (prin1-to-string id))))
146         (nntp-server-buffer (or buffer nntp-server-buffer)))
147     (if (and (stringp file)
148              (file-exists-p file)
149              (not (file-directory-p file)))
150         (save-excursion
151           (nnmail-find-file file)))))
152
153 (defun nnml-request-group (group &optional server dont-check)
154   "Select news GROUP."
155   (nnml-possibly-change-directory group)
156   (or dont-check (nnml-get-new-mail))
157   (or nnml-group-alist 
158       (progn
159         (nnml-request-list)
160         (setq nnml-group-alist (nnmail-get-active))))
161   (let ((active (nth 1 (assoc group nnml-group-alist))))
162     (save-excursion
163       (set-buffer nntp-server-buffer)
164       (erase-buffer)
165       (if (not active)
166           ()
167         (insert (format "211 %d %d %d %s\n" 
168                         (max (1+ (- (cdr active) (car active))) 0)
169                         (car active) (cdr active) group))
170         t))))
171
172 (defun nnml-close-group (group &optional server)
173   t)
174
175 (defun nnml-request-list (&optional server)
176   "List active newsgoups."
177   (if server (nnml-get-new-mail))
178   (save-excursion
179     (nnmail-find-file nnml-active-file)))
180
181 (defun nnml-request-newgroups (date &optional server)
182   "List groups created after DATE."
183   (nnml-request-list server))
184
185 (defun nnml-request-list-newsgroups (&optional server)
186   "List newsgroups (defined in NNTP2)."
187   (save-excursion
188     (nnmail-find-file nnml-newsgroups-file)))
189
190 (defun nnml-request-post (&optional server)
191   "Post a new news in current buffer."
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   "Expire all articles in the ARTICLES list in group GROUP.
198 The list of unexpired articles will be returned (ie. all articles that
199 were too fresh to be expired).
200 If FORCE is non-nil, ARTICLES will be deleted whether they are old or not."
201   (nnml-possibly-change-directory newsgroup)
202   (let* ((days (or (and nnmail-expiry-wait-function
203                         (funcall nnmail-expiry-wait-function newsgroup))
204                    nnmail-expiry-wait))
205          article rest mod-time)
206     (while articles
207       (setq article (concat nnml-current-directory (int-to-string
208                                                       (car articles))))
209       (if (setq mod-time (nth 5 (file-attributes article)))
210           (if (or force
211                   (> (nnmail-days-between
212                       (current-time-string)
213                       (current-time-string mod-time))
214                      days))
215               (progn
216                 (and gnus-verbose-backends (message "Deleting %s..." article))
217                 (condition-case ()
218                     (delete-file article)
219                   (file-error nil))
220                 (nnml-nov-delete-article newsgroup (car articles)))
221             (setq rest (cons (car articles) rest))))
222       (setq articles (cdr articles)))
223     (nnml-save-nov)
224     rest))
225
226 (defun nnml-request-move-article (article group server accept-form)
227   (let ((buf (get-buffer-create " *nnml move*"))
228         result)
229     (and 
230      (nnml-request-article article group server)
231      (save-excursion
232        (set-buffer buf)
233        (insert-buffer-substring nntp-server-buffer)
234        (setq result (eval accept-form))
235        (kill-buffer (current-buffer))
236        result)
237      (progn
238        (condition-case ()
239            (delete-file (concat nnml-current-directory 
240                                 (int-to-string article)))
241          (file-error nil))
242        (nnml-nov-delete-article group article)
243        (nnml-save-nov)))
244     result))
245
246 (defun nnml-request-accept-article (group)
247   (let (result)
248     (if (stringp group)
249         (and 
250          (nnml-request-list)
251          (setq nnml-group-alist (nnmail-get-active))
252          ;; We trick the choosing function into believing that only one
253          ;; group is availiable.  
254          (let ((nnmail-split-methods (list (list group ""))))
255            (setq result (car (nnml-save-mail))))
256          (progn
257            (nnmail-save-active nnml-group-alist nnml-active-file)
258            (nnml-save-nov)))
259       (and
260        (nnml-request-list)
261        (setq nnml-group-alist (nnmail-get-active))
262        (setq result (car (nnml-save-mail)))
263        (progn
264          (nnmail-save-active nnml-group-alist nnml-active-file)
265          (nnml-save-nov))))
266     result))
267
268 (defun nnml-request-replace-article (article group buffer)
269   (nnml-possibly-change-directory group)
270   (save-excursion
271     (set-buffer buffer)
272     (if (not (condition-case ()
273                  (progn
274                    (write-region (point-min) (point-max)
275                                  (concat nnml-current-directory 
276                                          (int-to-string article))
277                                  nil (if gnus-verbose-backends nil 'nomesg))
278                    t)
279                (error nil)))
280         ()
281       (let ((chars (nnmail-insert-lines))
282             (art (concat (int-to-string article) "\t"))
283             nov-line)
284         (setq nov-line (nnml-make-nov-line chars))
285         (save-excursion 
286           (set-buffer (nnml-open-nov group))
287           (goto-char (point-min))
288           (if (or (looking-at art)
289                   (search-forward (concat "\n" art)))
290               (progn
291                 (delete-region (progn (beginning-of-line) (point))
292                                (progn (forward-line 1) (point)))
293                 (insert (int-to-string article) nov-line)
294                 (nnml-save-nov))
295             (kill-buffer (current-buffer)))
296           t)))))
297
298
299 \f
300 ;;; Internal functions
301
302 (defun nnml-retrieve-headers-with-nov (articles)
303   (if (or gnus-nov-is-evil nnml-nov-is-evil)
304       nil
305     (let ((first (car articles))
306           (last (progn (while (cdr articles) (setq articles (cdr articles)))
307                        (car articles)))
308           (nov (concat nnml-current-directory ".nov")))
309       (if (file-exists-p nov)
310           (save-excursion
311             (set-buffer nntp-server-buffer)
312             (erase-buffer)
313             (insert-file-contents nov)
314             (goto-char 1)
315             (while (and (not (eobp)) (< first (read (current-buffer))))
316               (forward-line 1))
317             (beginning-of-line)
318             (if (not (eobp)) (delete-region 1 (point)))
319             (while (and (not (eobp)) (>= last (read (current-buffer))))
320               (forward-line 1))
321             (beginning-of-line)
322             (if (not (eobp)) (delete-region (point) (point-max)))
323             t)))))
324
325 (defun nnml-possibly-change-directory (newsgroup)
326   (if newsgroup
327       (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
328         (if (file-directory-p pathname)
329             (setq nnml-current-directory pathname)
330           (error "No such newsgroup: %s" newsgroup)))))
331
332 (defun nnml-create-directories ()
333   (let ((methods nnmail-split-methods)
334         dir dirs)
335     (while methods
336       (setq dir (nnmail-article-pathname (car (car methods)) nnml-directory))
337       (while (not (file-directory-p dir))
338         (setq dirs (cons dir dirs))
339         (setq dir (file-name-directory (directory-file-name dir))))
340       (while dirs
341         (if (make-directory (directory-file-name (car dirs)))
342             (error "Could not create directory %s" (car dirs)))
343         (and gnus-verbose-backends 
344              (message "Creating mail directory %s" (car dirs)))
345         (setq dirs (cdr dirs)))
346       (setq methods (cdr methods)))))
347
348 (defun nnml-save-mail ()
349   "Called narrowed to an article."
350   (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
351         chars nov-line)
352     (setq chars (nnmail-insert-lines))
353     (nnmail-insert-xref group-art)
354     (goto-char (point-min))
355     (while (looking-at "From ")
356       (replace-match "X-From-Line: ")
357       (forward-line 1))
358     ;; We save the article in all the newsgroups it belongs in.
359     (let ((ga group-art)
360           first)
361       (while ga
362         (let ((file (concat (nnmail-article-pathname 
363                              (car (car ga)) nnml-directory)
364                             (int-to-string (cdr (car ga))))))
365           (if first
366               ;; It was already saved, so we just make a hard link.
367               (add-name-to-file first file t)
368             ;; Save the article.
369             (write-region (point-min) (point-max) file nil 
370                           (if gnus-verbose-backends nil 'nomesg))
371             (setq first file)))
372         (setq ga (cdr ga))))
373     ;; Generate a nov line for this article. We generate the nov
374     ;; line after saving, because nov generation destroys the
375     ;; header. 
376     (setq nov-line (nnml-make-nov-line chars))
377     ;; Output the nov line to all nov databases that should have it.
378     (let ((ga group-art))
379       (while ga
380         (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
381         (setq ga (cdr ga))))
382     group-art))
383
384 (defun nnml-active-number (group)
385   "Compute the next article number in GROUP."
386   (let ((active (car (cdr (assoc group nnml-group-alist)))))
387     (setcdr active (1+ (cdr active)))
388     (let (file)
389       (while (file-exists-p
390               (setq file (concat (nnmail-article-pathname 
391                                   group nnml-directory)
392                                  (int-to-string (cdr active)))))
393         (setcdr active (1+ (cdr active)))))
394     (cdr active)))
395
396 (defun nnml-get-new-mail ()
397   "Read new incoming mail."
398   (let (incoming)
399     (nnml-create-directories)
400     (if (and nnml-get-new-mail nnmail-spool-file
401              (file-exists-p nnmail-spool-file)
402              (> (nth 7 (file-attributes nnmail-spool-file)) 0))
403         (progn
404           (and gnus-verbose-backends 
405                (message "nnml: Reading incoming mail..."))
406           (setq incoming 
407                 (nnmail-move-inbox nnmail-spool-file 
408                                    (concat nnml-directory "Incoming")))
409           (nnml-request-list)
410           (setq nnml-group-alist (nnmail-get-active))
411           (nnmail-split-incoming incoming 'nnml-save-mail)
412           (nnmail-save-active nnml-group-alist nnml-active-file)
413           (nnml-save-nov)
414           (run-hooks 'nnmail-read-incoming-hook)
415           ;; The following has been commented away, just to make sure
416           ;; that nobody ever loses any mail. If you feel safe that
417           ;; nnml will never do anything strange, just remove those
418           ;; two semicolons, and avoid having lots of "Incoming*"
419           ;; files. 
420 ;;         (delete-file incoming)
421           (and gnus-verbose-backends
422                (message "nnml: Reading incoming mail...done"))))))
423
424
425 (defun nnml-add-nov (group article line)
426   "Add a nov line for the GROUP base."
427   (save-excursion 
428     (set-buffer (nnml-open-nov group))
429     (goto-char (point-max))
430     (insert (int-to-string article) line)))
431
432 (defsubst nnml-header-value ()
433   (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
434
435 (defun nnml-make-nov-line (chars)
436   "Create a nov from the current headers."
437   (let ((case-fold-search t)
438         subject from date id references lines xref in-reply-to char)
439     (save-excursion
440       (save-restriction
441         (goto-char (point-min))
442         (narrow-to-region 
443          (point)
444          (1- (or (search-forward "\n\n" nil t) (point-max))))
445         ;; Fold continuation lines.
446         (goto-char (point-min))
447         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
448           (replace-match " " t t))
449         (subst-char-in-region (point-min) (point-max) ?\t ? )
450         ;; [number subject from date id references chars lines xref]
451         (save-excursion
452           (goto-char (point-min))
453           (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
454                                     nil t)
455             (beginning-of-line)
456             (setq char (downcase (following-char))) 
457             (cond
458              ((eq char ?s)
459               (setq subject (nnml-header-value)))
460              ((eq char ?f)
461               (setq from (nnml-header-value)))
462              ((eq char ?x)
463               (setq xref (nnml-header-value)))
464              ((eq char ?l)
465               (setq lines (nnml-header-value)))
466              ((eq char ?d)
467               (setq date (nnml-header-value)))
468              ((eq char ?m)
469               (setq id (setq id (nnml-header-value))))
470              ((eq char ?r)
471               (setq references (nnml-header-value)))
472              ((eq char ?i)
473               (setq in-reply-to (nnml-header-value))))
474             (forward-line 1))
475       
476           (and (not references)
477                in-reply-to
478                (string-match "<[^>]+>" in-reply-to)
479                (setq references
480                      (substring in-reply-to (match-beginning 0)
481                                 (match-end 0)))))
482         ;; [number subject from date id references chars lines xref]
483         (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
484                 (or subject "(none)")
485                 (or from "(nobody)") (or date "")
486                 (or id "") (or references "")
487                 (or chars 0) (or lines "0") (or xref ""))))))
488
489 (defun nnml-open-nov (group)
490   (or (cdr (assoc group nnml-nov-buffer-alist))
491       (let ((buffer (find-file-noselect 
492                      (concat (nnmail-article-pathname 
493                               group nnml-directory) ".nov"))))
494         (save-excursion
495           (set-buffer buffer)
496           (buffer-disable-undo (current-buffer)))
497         (setq nnml-nov-buffer-alist 
498               (cons (cons group buffer) nnml-nov-buffer-alist))
499         buffer)))
500
501 (defun nnml-save-nov ()
502   (save-excursion
503     (while nnml-nov-buffer-alist
504       (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
505           (progn
506             (set-buffer (cdr (car nnml-nov-buffer-alist)))
507             (write-region 1 (point-max) (buffer-file-name) nil 'nomesg)
508             (set-buffer-modified-p nil)
509             (kill-buffer (current-buffer))))
510       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
511
512 (defun nnml-generate-nov-databases (dir)
513   "Generate nov databases in all nnml mail newsgroups."
514   (interactive 
515    (progn   
516      (setq nnml-group-alist nil)
517      (list nnml-directory)))
518   (nnml-open-server (system-name))
519   (let ((dirs (directory-files dir t nil t)))
520     (while dirs 
521       (if (and (not (string-match "/\\.\\.$" (car dirs)))
522                (not (string-match "/\\.$" (car dirs)))
523                (file-directory-p (car dirs)))
524           (nnml-generate-nov-databases (car dirs)))
525       (setq dirs (cdr dirs))))
526   (let ((files (sort
527                 (mapcar
528                  (function
529                   (lambda (name)
530                     (string-to-int name)))
531                  (directory-files dir nil "^[0-9]+$" t))
532                 (function <)))
533         (nov (concat dir "/.nov"))
534         (nov-buffer (get-buffer-create "*nov*"))
535         nov-line chars)
536     (if files
537         (setq nnml-group-alist 
538               (cons (list (nnmail-replace-chars-in-string 
539                            (substring (expand-file-name dir)
540                                       (length (expand-file-name 
541                                                nnml-directory)))
542                            ?/ ?.)
543                           (cons (car files)
544                                 (let ((f files))
545                                   (while (cdr f) (setq f (cdr f)))
546                                   (car f))))
547                     nnml-group-alist)))
548     (if files
549         (save-excursion
550           (set-buffer nntp-server-buffer)
551           (if (file-exists-p nov)
552               (delete-file nov))
553           (save-excursion
554             (set-buffer nov-buffer)
555             (buffer-disable-undo (current-buffer))
556             (erase-buffer))
557           (while files
558             (erase-buffer)
559             (insert-file-contents (concat dir "/" (int-to-string (car files))))
560             (goto-char 1)
561             (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
562                                                 (setq chars (- (point-max) 
563                                                                (point)))
564                                                 (point)))
565             (setq nov-line (nnml-make-nov-line chars))
566             (save-excursion
567               (set-buffer nov-buffer)
568               (goto-char (point-max))
569               (insert (int-to-string (car files)) nov-line))
570             (widen)
571             (setq files (cdr files)))
572           (save-excursion
573             (set-buffer nov-buffer)
574             (write-region 1 (point-max) (expand-file-name nov) nil
575                           'nomesg)
576             (kill-buffer (current-buffer)))))
577     (nnmail-save-active nnml-group-alist nnml-active-file)))
578
579 (defun nnml-nov-delete-article (group article)
580   (save-excursion
581     (set-buffer (nnml-open-nov group))
582     (goto-char 1)
583     (if (re-search-forward (concat "^" (int-to-string article) "\t"))
584         (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
585     t))
586
587 (provide 'nnml)
588
589 ;;; nnml.el ends here