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