*** empty log message ***
[gnus] / lisp / nnml.el
1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995,96,97 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
28 ;; For an overview of what the interface functions do, please see the
29 ;; Gnus sources.  
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'nnmail)
35 (require 'nnoo)
36 (require 'cl)
37
38 (nnoo-declare nnml)
39
40 (defvoo nnml-directory message-directory
41   "Mail spool directory.")
42
43 (defvoo nnml-active-file 
44   (concat (file-name-as-directory nnml-directory) "active")
45   "Mail active file.")
46
47 (defvoo nnml-newsgroups-file 
48   (concat (file-name-as-directory nnml-directory) "newsgroups")
49   "Mail newsgroups description file.")
50
51 (defvoo nnml-get-new-mail t
52   "If non-nil, nnml will check the incoming mail file and split the mail.")
53
54 (defvoo nnml-nov-is-evil nil
55   "If non-nil, Gnus will never generate and use nov databases for mail groups.
56 Using nov databases will speed up header fetching considerably.
57 This variable shouldn't be flipped much.  If you have, for some reason,
58 set this to t, and want to set it to nil again, you should always run
59 the `nnml-generate-nov-databases' command.  The function will go
60 through all nnml directories and generate nov databases for them
61 all.  This may very well take some time.")
62
63 (defvoo nnml-prepare-save-mail-hook nil
64   "Hook run narrowed to an article before saving.")
65
66 (defvoo nnml-inhibit-expiry nil
67   "If non-nil, inhibit expiry.")
68
69
70 \f
71
72 (defconst nnml-version "nnml 1.0"
73   "nnml version.")
74
75 (defvoo nnml-nov-file-name ".overview")
76
77 (defvoo nnml-current-directory nil)
78 (defvoo nnml-current-group nil)
79 (defvoo nnml-status-string "")
80 (defvoo nnml-nov-buffer-alist nil)
81 (defvoo nnml-group-alist nil)
82 (defvoo nnml-active-timestamp nil)
83 (defvoo nnml-article-file-alist nil)
84
85 (defvoo nnml-generate-active-function 'nnml-generate-active-info)
86
87 \f
88
89 ;;; Interface functions.
90
91 (nnoo-define-basics nnml)
92
93 (deffoo nnml-retrieve-headers (sequence &optional newsgroup server fetch-old)
94   (save-excursion
95     (set-buffer nntp-server-buffer)
96     (erase-buffer)
97     (let ((file nil)
98           (number (length sequence))
99           (count 0)
100           beg article)
101       (if (stringp (car sequence))
102           'headers
103         (nnml-possibly-change-directory newsgroup server)
104         (unless nnml-article-file-alist
105           (setq nnml-article-file-alist
106                 (nnheader-article-to-file-alist nnml-current-directory)))
107         (if (nnml-retrieve-headers-with-nov sequence fetch-old)
108             'nov
109           (while sequence
110             (setq article (car sequence))
111             (setq file 
112                   (concat nnml-current-directory 
113                           (or (cdr (assq article nnml-article-file-alist))
114                               "")))
115             (when (and (file-exists-p file)
116                        (not (file-directory-p file)))
117               (insert (format "221 %d Article retrieved.\n" article))
118               (setq beg (point))
119               (nnheader-insert-head file)
120               (goto-char beg)
121               (if (search-forward "\n\n" nil t)
122                   (forward-char -1)
123                 (goto-char (point-max))
124                 (insert "\n\n"))
125               (insert ".\n")
126               (delete-region (point) (point-max)))
127             (setq sequence (cdr sequence))
128             (setq count (1+ count))
129             (and (numberp nnmail-large-newsgroup)
130                  (> number nnmail-large-newsgroup)
131                  (zerop (% count 20))
132                  (nnheader-message 6 "nnml: Receiving headers... %d%%"
133                                    (/ (* count 100) number))))
134
135           (and (numberp nnmail-large-newsgroup)
136                (> number nnmail-large-newsgroup)
137                (nnheader-message 6 "nnml: Receiving headers...done"))
138
139           (nnheader-fold-continuation-lines)
140           'headers)))))
141
142 (deffoo nnml-open-server (server &optional defs)
143   (nnoo-change-server 'nnml server defs)
144   (when (not (file-exists-p nnml-directory))
145     (condition-case ()
146         (make-directory nnml-directory t)
147       (error)))
148   (cond 
149    ((not (file-exists-p nnml-directory))
150     (nnml-close-server)
151     (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
152    ((not (file-directory-p (file-truename nnml-directory)))
153     (nnml-close-server)
154     (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
155    (t
156     (nnheader-report 'nnml "Opened server %s using directory %s"
157                      server nnml-directory)
158     t)))
159
160 (defun nnml-request-regenerate (server)
161   (nnml-possibly-change-directory nil server)
162   (nnml-generate-nov-databases))
163
164 (deffoo nnml-request-article (id &optional newsgroup server buffer)
165   (nnml-possibly-change-directory newsgroup server)
166   (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
167          path gpath group-num)
168     (if (stringp id)
169         (when (and (setq group-num (nnml-find-group-number id))
170                    (cdr
171                     (assq (cdr group-num)
172                           (nnheader-article-to-file-alist
173                            (setq gpath
174                                  (nnmail-group-pathname
175                                   (car group-num)
176                                   nnml-directory))))))
177           (setq path (concat gpath (int-to-string (cdr group-num)))))
178       (setq path (nnml-article-to-file id)))
179     (cond 
180      ((not path)
181       (nnheader-report 'nnml "No such article: %s" id))
182      ((not (file-exists-p path))
183       (nnheader-report 'nnml "No such file: %s" path))
184      ((file-directory-p path)
185       (nnheader-report 'nnml "File is a directory: %s" path))
186      ((not (save-excursion (nnmail-find-file path)))
187       (nnheader-report 'nnml "Couldn't read file: %s" path))
188      (t
189       (nnheader-report 'nnml "Article %s retrieved" id)
190       ;; We return the article number.
191       (cons newsgroup (string-to-int (file-name-nondirectory path)))))))
192
193 (deffoo nnml-request-group (group &optional server dont-check)
194   (cond 
195    ((not (nnml-possibly-change-directory group server))
196     (nnheader-report 'nnml "Invalid group (no such directory)"))
197    ((not (file-exists-p nnml-current-directory))
198     (nnheader-report 'nnml "Directory %s does not exist"
199                      nnml-current-directory))
200    ((not (file-directory-p nnml-current-directory))
201     (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
202    (dont-check 
203     (nnheader-report 'nnml "Group %s selected" group)
204     t)
205    (t
206     (nnheader-re-read-dir nnml-current-directory)
207     (nnmail-activate 'nnml)
208     (let ((active (nth 1 (assoc group nnml-group-alist))))
209       (if (not active)
210           (nnheader-report 'nnml "No such group: %s" group)
211         (nnheader-report 'nnml "Selected group %s" group)
212         (nnheader-insert "211 %d %d %d %s\n" 
213                          (max (1+ (- (cdr active) (car active))) 0)
214                          (car active) (cdr active) group))))))
215
216 (deffoo nnml-request-scan (&optional group server)
217   (setq nnml-article-file-alist nil)
218   (nnml-possibly-change-directory group server)
219   (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
220
221 (deffoo nnml-close-group (group &optional server)
222   (setq nnml-article-file-alist nil)
223   t)
224
225 (deffoo nnml-request-create-group (group &optional server args)
226   (nnmail-activate 'nnml)
227   (unless (assoc group nnml-group-alist)
228     (let (active)
229       (push (list group (setq active (cons 1 0)))
230             nnml-group-alist)
231       (nnml-possibly-create-directory group)
232       (nnml-possibly-change-directory group server)
233       (let ((articles (nnheader-directory-articles nnml-current-directory)))
234         (when articles
235           (setcar active (apply 'min articles))
236           (setcdr active (apply 'max articles))))
237       (nnmail-save-active nnml-group-alist nnml-active-file)))
238   t)
239
240 (deffoo nnml-request-list (&optional server)
241   (save-excursion
242     (nnmail-find-file nnml-active-file)
243     (setq nnml-group-alist (nnmail-get-active))
244     t))
245
246 (deffoo nnml-request-newgroups (date &optional server)
247   (nnml-request-list server))
248
249 (deffoo nnml-request-list-newsgroups (&optional server)
250   (save-excursion
251     (nnmail-find-file nnml-newsgroups-file)))
252
253 (deffoo nnml-request-expire-articles (articles newsgroup
254                                                &optional server force)
255   (nnml-possibly-change-directory newsgroup server)
256   (let* ((active-articles 
257           (nnheader-directory-articles nnml-current-directory))
258          (is-old t)
259          article rest mod-time number)
260     (nnmail-activate 'nnml)
261
262     (unless nnml-article-file-alist
263       (setq nnml-article-file-alist
264             (nnheader-article-to-file-alist nnml-current-directory)))
265
266     (while (and articles is-old)
267       (when (setq article
268                   (assq (setq number (pop articles)) 
269                         nnml-article-to-file-alist))
270         (setq article (concat nnml-current-directory (cdr article)))
271         (when (setq mod-time (nth 5 (file-attributes article)))
272           (if (and (nnml-deletable-article-p newsgroup number)
273                    (setq is-old 
274                          (nnmail-expired-article-p newsgroup mod-time force
275                                                    nnml-inhibit-expiry)))
276               (progn
277                 (nnheader-message 5 "Deleting article %s in %s..."
278                                   article newsgroup)
279                 (condition-case ()
280                     (funcall nnmail-delete-file-function article)
281                   (file-error
282                    (push number rest)))
283                 (setq active-articles (delq number active-articles))
284                 (nnml-nov-delete-article newsgroup number))
285             (push number rest)))))
286     (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
287       (when active
288         (setcar active (or (and active-articles
289                                 (apply 'min active-articles))
290                            (1+ (cdr active)))))
291       (nnmail-save-active nnml-group-alist nnml-active-file))
292     (nnml-save-nov)
293     (message "")
294     (nconc rest articles)))
295
296 (deffoo nnml-request-move-article 
297   (article group server accept-form &optional last)
298   (let ((buf (get-buffer-create " *nnml move*"))
299         result)
300     (nnml-possibly-change-directory group server)
301     (unless nnml-article-file-alist
302       (setq nnml-article-file-alist
303             (nnheader-article-to-file-alist nnml-current-directory)))
304     (and 
305      (nnml-deletable-article-p group article)
306      (nnml-request-article article group server)
307      (save-excursion
308        (set-buffer buf)
309        (insert-buffer-substring nntp-server-buffer)
310        (setq result (eval accept-form))
311        (kill-buffer (current-buffer))
312        result)
313      (progn
314        (nnml-possibly-change-directory group server)
315        (condition-case ()
316            (funcall nnmail-delete-file-function
317                     (concat nnml-current-directory 
318                             (int-to-string article)))
319          (file-error nil))
320        (nnml-nov-delete-article group article)
321        (and last (nnml-save-nov))))
322     result))
323
324 (deffoo nnml-request-accept-article (group &optional server last)
325   (nnml-possibly-change-directory group server)
326   (nnmail-check-syntax)
327   (let (result)
328     (if (stringp group)
329         (and 
330          (nnmail-activate 'nnml)
331          (setq result (car (nnml-save-mail 
332                             (list (cons group (nnml-active-number group))))))
333          (progn
334            (nnmail-save-active nnml-group-alist nnml-active-file)
335            (and last (nnml-save-nov))))
336       (and
337        (nnmail-activate 'nnml)
338        (setq result (car (nnml-save-mail
339                           (nnmail-article-group 'nnml-active-number))))
340        (progn
341          (nnmail-save-active nnml-group-alist nnml-active-file)
342          (and last (nnml-save-nov)))))
343     result))
344
345 (deffoo nnml-request-replace-article (article group buffer)
346   (nnml-possibly-change-directory group)
347   (save-excursion
348     (set-buffer buffer)
349     (nnml-possibly-create-directory group)
350     (let ((chars (nnmail-insert-lines))
351           (art (concat (int-to-string article) "\t"))
352           headers)
353       (when (condition-case ()
354                 (progn
355                   (nnmail-write-region 
356                    (point-min) (point-max)
357                    (concat nnml-current-directory
358                            (int-to-string article))
359                    nil (if (nnheader-be-verbose 5) nil 'nomesg))
360                   t)
361               (error nil))
362         (setq headers (nnml-parse-head chars article))
363         ;; Replace the NOV line in the NOV file.
364         (save-excursion 
365           (set-buffer (nnml-open-nov group))
366           (goto-char (point-min))
367           (if (or (looking-at art)
368                   (search-forward (concat "\n" art) nil t))
369               ;; Delete the old NOV line.
370               (delete-region (progn (beginning-of-line) (point))
371                              (progn (forward-line 1) (point)))
372             ;; The line isn't here, so we have to find out where
373             ;; we should insert it.  (This situation should never
374             ;; occur, but one likes to make sure...)
375             (while (and (looking-at "[0-9]+\t")
376                         (< (string-to-int 
377                             (buffer-substring 
378                              (match-beginning 0) (match-end 0)))
379                            article)
380                         (zerop (forward-line 1)))))
381           (beginning-of-line)
382           (nnheader-insert-nov headers)
383           (nnml-save-nov)
384           t)))))
385
386 (deffoo nnml-request-delete-group (group &optional force server)
387   (nnml-possibly-change-directory group server)
388   (when force
389     ;; Delete all articles in GROUP.
390     (let ((articles 
391            (directory-files 
392             nnml-current-directory t
393             (concat nnheader-numerical-short-files
394                     "\\|" (regexp-quote nnml-nov-file-name) "$")))
395           article)
396       (while articles 
397         (setq article (pop articles))
398         (when (file-writable-p article)
399           (nnheader-message 5 "Deleting article %s in %s..." article group)
400           (funcall nnmail-delete-file-function article))))
401     ;; Try to delete the directory itself.
402     (condition-case ()
403         (delete-directory nnml-current-directory)
404       (error nil)))
405   ;; Remove the group from all structures.
406   (setq nnml-group-alist 
407         (delq (assoc group nnml-group-alist) nnml-group-alist)
408         nnml-current-group nil
409         nnml-current-directory nil)
410   ;; Save the active file.
411   (nnmail-save-active nnml-group-alist nnml-active-file)
412   t)
413
414 (deffoo nnml-request-rename-group (group new-name &optional server)
415   (nnml-possibly-change-directory group server)
416   (let ((new-dir (nnmail-group-pathname new-name nnml-directory))
417         (old-dir (nnmail-group-pathname group nnml-directory)))
418     (when (condition-case ()
419               (progn
420                 (make-directory new-dir t)
421                 t)
422             (error nil))
423       ;; We move the articles file by file instead of renaming
424       ;; the directory -- there may be subgroups in this group.
425       ;; One might be more clever, I guess.
426       (let ((files (nnheader-article-to-file-alist old-dir)))
427         (while files
428           (rename-file 
429            (concat old-dir (cdar files))
430            (concat new-dir (cdar files)))
431           (pop files)))
432       ;; Move .overview file.
433       (let ((overview (concat old-dir nnml-nov-file-name)))
434         (when (file-exists-p overview)
435           (rename-file overview (concat new-dir nnml-nov-file-name))))
436       (when (<= (length (directory-files old-dir)) 2)
437         (condition-case ()
438             (delete-directory old-dir)
439           (error nil)))
440       ;; That went ok, so we change the internal structures.
441       (let ((entry (assoc group nnml-group-alist)))
442         (when entry
443           (setcar entry new-name))
444         (setq nnml-current-directory nil
445               nnml-current-group nil)
446         ;; Save the new group alist.
447         (nnmail-save-active nnml-group-alist nnml-active-file)
448         t))))
449
450 (deffoo nnml-set-status (article name value &optional group server)
451   (nnml-possibly-change-directory group server)
452   (let ((file (nnml-article-to-file article)))
453     (cond
454      ((not (file-exists-p file))
455       (nnheader-report 'nnml "File %s does not exist" file))
456      (t
457       (nnheader-temp-write file
458         (nnheader-insert-file-contents file)
459         (nnmail-replace-status name value))
460       t))))
461
462 \f
463 ;;; Internal functions.
464
465 (defun nnml-article-to-file (article)
466   (unless nnml-article-file-alist
467     (setq nnml-article-file-alist
468           (nnheader-article-to-file-alist nnml-current-directory)))
469   (let (file)
470     (when (setq file (cdr (assq article nnml-article-file-alist)))
471       (concat nnml-current-directory file))))
472
473 (defun nnml-deletable-article-p (group article)
474   "Say whether ARTICLE in GROUP can be deleted."
475   (let (file path)
476     (when (setq file (cdr (assq article nnml-article-file-alist)))
477       (setq path (concat nnml-current-directory file))
478       (when (file-writable-p path)
479         (or (not nnmail-keep-last-article)
480             (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
481                      article)))))))
482
483 ;; Find an article number in the current group given the Message-ID. 
484 (defun nnml-find-group-number (id)
485   (save-excursion
486     (set-buffer (get-buffer-create " *nnml id*"))
487     (buffer-disable-undo (current-buffer))
488     (let ((alist nnml-group-alist)
489           number)
490       ;; We want to look through all .overview files, but we want to
491       ;; start with the one in the current directory.  It seems most
492       ;; likely that the article we are looking for is in that group. 
493       (if (setq number (nnml-find-id nnml-current-group id))
494           (cons nnml-current-group number)
495         ;; It wasn't there, so we look through the other groups as well.
496         (while (and (not number)
497                     alist)
498           (or (string= (caar alist) nnml-current-group)
499               (setq number (nnml-find-id (caar alist) id)))
500           (or number
501               (setq alist (cdr alist))))
502         (and number
503              (cons (caar alist) number))))))
504
505 (defun nnml-find-id (group id)
506   (erase-buffer)
507   (let ((nov (concat (nnmail-group-pathname group nnml-directory)
508                      nnml-nov-file-name))
509         number found)
510     (when (file-exists-p nov)
511       (nnheader-insert-file-contents nov)
512       (while (and (not found)
513                   (search-forward id nil t)) ; We find the ID.
514         ;; And the id is in the fourth field.
515         (if (not (and (search-backward "\t" nil t 4)
516                       (not (search-backward"\t" (gnus-point-at-bol) t))))
517             (forward-line 1)
518           (beginning-of-line)
519           (setq found t)
520           ;; We return the article number.
521           (setq number
522                 (condition-case ()
523                     (read (current-buffer))
524                   (error nil)))))
525       number)))
526
527 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
528   (if (or gnus-nov-is-evil nnml-nov-is-evil)
529       nil
530     (let ((nov (concat nnml-current-directory nnml-nov-file-name)))
531       (when (file-exists-p nov)
532         (save-excursion
533           (set-buffer nntp-server-buffer)
534           (erase-buffer)
535           (nnheader-insert-file-contents nov)
536           (if (and fetch-old
537                    (not (numberp fetch-old)))
538               t                         ; Don't remove anything.
539             (nnheader-nov-delete-outside-range
540              (if fetch-old (max 1 (- (car articles) fetch-old))
541                (car articles))
542              (car (last articles)))
543             t))))))
544
545 (defun nnml-possibly-change-directory (group &optional server)
546   (when (and server
547              (not (nnml-server-opened server)))
548     (nnml-open-server server))
549   (when group
550     (let ((pathname (nnmail-group-pathname group nnml-directory)))
551       (when (not (equal pathname nnml-current-directory))
552         (setq nnml-current-directory pathname
553               nnml-current-group group
554               nnml-article-file-alist nil))))
555   t)
556
557 (defun nnml-possibly-create-directory (group)
558   (let (dir dirs)
559     (setq dir (nnmail-group-pathname group nnml-directory))
560     (while (not (file-directory-p dir))
561       (push dir dirs)
562       (setq dir (file-name-directory (directory-file-name dir))))
563     (while dirs
564       (make-directory (directory-file-name (car dirs)))
565       (nnheader-message 5 "Creating mail directory %s" (car dirs))
566       (setq dirs (cdr dirs)))))
567              
568 (defun nnml-save-mail (group-art)
569   "Called narrowed to an article."
570   (let (chars headers)
571     (setq chars (nnmail-insert-lines))
572     (nnmail-insert-xref group-art)
573     (run-hooks 'nnmail-prepare-save-mail-hook)
574     (run-hooks 'nnml-prepare-save-mail-hook)
575     (goto-char (point-min))
576     (while (looking-at "From ")
577       (replace-match "X-From-Line: ")
578       (forward-line 1))
579     ;; We save the article in all the newsgroups it belongs in.
580     (let ((ga group-art)
581           first)
582       (while ga
583         (nnml-possibly-create-directory (caar ga))
584         (let ((file (concat (nnmail-group-pathname 
585                              (caar ga) nnml-directory)
586                             (int-to-string (cdar ga)))))
587           (if first
588               ;; It was already saved, so we just make a hard link.
589               (funcall nnmail-crosspost-link-function first file t)
590             ;; Save the article.
591             (nnmail-write-region (point-min) (point-max) file nil 
592                                  (if (nnheader-be-verbose 5) nil 'nomesg))
593             (setq first file)))
594         (setq ga (cdr ga))))
595     ;; Generate a nov line for this article.  We generate the nov
596     ;; line after saving, because nov generation destroys the
597     ;; header. 
598     (setq headers (nnml-parse-head chars))
599     ;; Output the nov line to all nov databases that should have it.
600     (let ((ga group-art))
601       (while ga
602         (nnml-add-nov (caar ga) (cdar ga) headers)
603         (setq ga (cdr ga))))
604     group-art))
605
606 (defun nnml-active-number (group)
607   "Compute the next article number in GROUP."
608   (let ((active (cadr (assoc group nnml-group-alist))))
609     ;; The group wasn't known to nnml, so we just create an active
610     ;; entry for it.   
611     (unless active
612       ;; Perhaps the active file was corrupt?  See whether
613       ;; there are any articles in this group.
614       (nnml-possibly-create-directory group)
615       (nnml-possibly-change-directory group)
616       (unless nnml-article-file-alist
617         (setq nnml-article-file-alist
618               (sort
619                (nnheader-article-to-file-alist nnml-current-directory)
620                (lambda (a1 a2) (< (car a1) (car a2))))))
621       (setq active
622             (if nnml-article-file-alist
623                 (cons (caar nnml-article-file-alist)
624                       (caar (last nnml-article-file-alist)))
625               (cons 1 0)))
626       (push (list group active) nnml-group-alist))
627     (setcdr active (1+ (cdr active)))
628     (while (file-exists-p
629             (concat (nnmail-group-pathname group nnml-directory)
630                     (int-to-string (cdr active))))
631       (setcdr active (1+ (cdr active))))
632     (cdr active)))
633
634 (defun nnml-add-nov (group article headers)
635   "Add a nov line for the GROUP base."
636   (save-excursion 
637     (set-buffer (nnml-open-nov group))
638     (goto-char (point-max))
639     (mail-header-set-number headers article)
640     (nnheader-insert-nov headers)))
641
642 (defsubst nnml-header-value ()
643   (buffer-substring (match-end 0) (progn (end-of-line) (point))))
644
645 (defun nnml-parse-head (chars &optional number)
646   "Parse the head of the current buffer."
647   (save-excursion
648     (save-restriction
649       (goto-char (point-min))
650       (narrow-to-region 
651        (point)
652        (1- (or (search-forward "\n\n" nil t) (point-max))))
653       ;; Fold continuation lines.
654       (goto-char (point-min))
655       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
656         (replace-match " " t t))
657       ;; Remove any tabs; they are too confusing.
658       (subst-char-in-region (point-min) (point-max) ?\t ? )
659       (let ((headers (nnheader-parse-head t)))
660         (mail-header-set-chars headers chars)
661         (mail-header-set-number headers number)
662         headers))))
663
664 (defun nnml-open-nov (group)
665   (or (cdr (assoc group nnml-nov-buffer-alist))
666       (let ((buffer (nnheader-find-file-noselect 
667                      (concat (nnmail-group-pathname group nnml-directory)
668                              nnml-nov-file-name))))
669         (save-excursion
670           (set-buffer buffer)
671           (buffer-disable-undo (current-buffer)))
672         (push (cons group buffer) nnml-nov-buffer-alist)
673         buffer)))
674
675 (defun nnml-save-nov ()
676   (save-excursion
677     (while nnml-nov-buffer-alist
678       (when (buffer-name (cdar nnml-nov-buffer-alist))
679         (set-buffer (cdar nnml-nov-buffer-alist))
680         (when (buffer-modified-p)
681           (nnmail-write-region 1 (point-max) (buffer-file-name) nil 'nomesg))
682         (set-buffer-modified-p nil)
683         (kill-buffer (current-buffer)))
684       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
685
686 ;;;###autoload
687 (defun nnml-generate-nov-databases ()
688   "Generate nov databases in all nnml directories."
689   (interactive)
690   ;; Read the active file to make sure we don't re-use articles 
691   ;; numbers in empty groups.
692   (nnmail-activate 'nnml)
693   (nnml-open-server (or (nnoo-current-server 'nnml) ""))
694   (setq nnml-directory (expand-file-name nnml-directory))
695   ;; Recurse down the directories.
696   (nnml-generate-nov-databases-1 nnml-directory)
697   ;; Save the active file.
698   (nnmail-save-active nnml-group-alist nnml-active-file))
699
700 (defun nnml-generate-nov-databases-1 (dir &optional seen)
701   (setq dir (file-name-as-directory dir))
702   ;; Only scan this sub-tree if we haven't been here yet.
703   (unless (member (file-truename dir) seen)
704     (push (file-truename dir) seen)
705     ;; We descend recursively
706     (let ((dirs (directory-files dir t nil t))
707           dir)
708       (while (setq dir (pop dirs))
709         (when (and (not (member (file-name-nondirectory dir) '("." "..")))
710                    (file-directory-p dir))
711           (nnml-generate-nov-databases-1 dir seen))))
712     ;; Do this directory.
713     (let ((files (sort
714                   (mapcar
715                    (lambda (name) (string-to-int name))
716                    (directory-files dir nil "^[0-9]+$" t))
717                   '<)))
718       (when files
719         (funcall nnml-generate-active-function dir)
720         ;; Generate the nov file.
721         (nnml-generate-nov-file dir files)))))
722
723 (defvar files)
724 (defun nnml-generate-active-info (dir)
725   ;; Update the active info for this group.
726   (let ((group (nnheader-file-to-group 
727                 (directory-file-name dir) nnml-directory)))
728     (setq nnml-group-alist
729           (delq (assoc group nnml-group-alist) nnml-group-alist))
730     (push (list group
731                 (cons (car files)
732                       (let ((f files))
733                         (while (cdr f) (setq f (cdr f)))
734                         (car f))))
735           nnml-group-alist)))
736
737 (defun nnml-generate-nov-file (dir files)
738   (let* ((dir (file-name-as-directory dir))
739          (nov (concat dir nnml-nov-file-name))
740          (nov-buffer (get-buffer-create " *nov*"))
741          chars file headers)
742     (save-excursion
743       ;; Init the nov buffer.
744       (set-buffer nov-buffer)
745       (buffer-disable-undo (current-buffer))
746       (erase-buffer)
747       (set-buffer nntp-server-buffer)
748       ;; Delete the old NOV file.
749       (when (file-exists-p nov)
750         (funcall nnmail-delete-file-function nov))
751       (while files
752         (unless (file-directory-p 
753                  (setq file (concat dir (int-to-string (car files)))))
754           (erase-buffer)
755           (nnheader-insert-file-contents file)
756           (narrow-to-region 
757            (goto-char (point-min))
758            (progn
759              (search-forward "\n\n" nil t)
760              (setq chars (- (point-max) (point)))
761              (max 1 (1- (point)))))
762           (when (and (not (= 0 chars))  ; none of them empty files...
763                      (not (= (point-min) (point-max))))
764             (goto-char (point-min))
765             (setq headers (nnml-parse-head chars (car files)))
766             (save-excursion
767               (set-buffer nov-buffer)
768               (goto-char (point-max))
769               (nnheader-insert-nov headers)))
770           (widen))
771         (setq files (cdr files)))
772       (save-excursion
773         (set-buffer nov-buffer)
774         (nnmail-write-region 1 (point-max) nov nil 'nomesg)
775         (kill-buffer (current-buffer))))))
776
777 (defun nnml-nov-delete-article (group article)
778   (save-excursion
779     (set-buffer (nnml-open-nov group))
780     (goto-char (point-min))
781     (when (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
782       (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
783     t))
784
785 (provide 'nnml)
786
787 ;;; nnml.el ends here