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