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