966e46890c8f11fd756808154909e715e09ac88b
[elisp/gnus.git-] / lisp / nnmbox.el
1 ;;; nnmbox.el --- mail mbox access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;;      Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 ;;; Commentary:
23
24 ;; For an overview of what the interface functions do, please see the
25 ;; Gnus sources.
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30 (eval-when-compile (require 'static))
31
32 (require 'nnheader)
33 (require 'message)
34 (require 'nnmail)
35 (require 'nnoo)
36 (require 'gnus-range)
37
38 (nnoo-declare nnmbox)
39
40 (defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
41   "The name of the mail box file in the user's home directory.
42
43 This variable is a virtual server slot.  See the Gnus manual for details.")
44
45 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
46   "The name of the active file for the mail box.
47
48 This variable is a virtual server slot.  See the Gnus manual for details.")
49
50 (defvoo nnmbox-get-new-mail t
51   "If non-nil, nnmbox will check the incoming mail file and split the mail.
52
53 This variable is a virtual server slot.  See the Gnus manual for details.")
54
55 (defvoo nnmbox-prepare-save-mail-hook nil
56   "Hook run narrowed to an article before saving.
57
58 This variable is a virtual server slot.  See the Gnus manual for details.")
59
60 \f
61
62 (defconst nnmbox-version "nnmbox 1.0"
63   "nnmbox version.")
64
65 (defvoo nnmbox-current-group nil
66   "Current nnmbox news group directory.")
67
68 (defconst nnmbox-mbox-buffer nil)
69
70 (defvoo nnmbox-status-string "")
71
72 (defvoo nnmbox-group-alist nil)
73 (defvoo nnmbox-active-timestamp nil)
74
75 (defvoo nnmbox-file-coding-system 'binary)
76 (defvoo nnmbox-file-coding-system-for-write nil)
77 (defvoo nnmbox-active-file-coding-system 'binary)
78 (defvoo nnmbox-active-file-coding-system-for-write nil)
79
80 (defvar nnmbox-group-building-active-articles nil)
81 (defvar nnmbox-group-active-articles nil)
82 \f
83
84 ;;; Interface functions
85
86 (nnoo-define-basics nnmbox)
87
88 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
89   (save-excursion
90     (set-buffer nntp-server-buffer)
91     (erase-buffer)
92     (let ((number (length sequence))
93           (count 0)
94           article start stop)
95       (nnmbox-possibly-change-newsgroup newsgroup server)
96       (while sequence
97         (setq article (car sequence))
98         (set-buffer nnmbox-mbox-buffer)
99         (when (nnmbox-find-article article)
100           (setq start
101                 (save-excursion
102                   (re-search-backward
103                    (concat "^" message-unix-mail-delimiter) nil t)
104                   (point)))
105           (search-forward "\n\n" nil t)
106           (setq stop (1- (point)))
107           (set-buffer nntp-server-buffer)
108           (insert (format "221 %d Article retrieved.\n" article))
109           (insert-buffer-substring nnmbox-mbox-buffer start stop)
110           (goto-char (point-max))
111           (insert ".\n"))
112         (setq sequence (cdr sequence))
113         (setq count (1+ count))
114         (and (numberp nnmail-large-newsgroup)
115              (> number nnmail-large-newsgroup)
116              (zerop (% count 20))
117              (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
118                                (/ (* count 100) number))))
119
120       (and (numberp nnmail-large-newsgroup)
121            (> number nnmail-large-newsgroup)
122            (nnheader-message 5 "nnmbox: Receiving headers...done"))
123
124       (set-buffer nntp-server-buffer)
125       (nnheader-fold-continuation-lines)
126       'headers)))
127
128 (deffoo nnmbox-open-server (server &optional defs)
129   (nnoo-change-server 'nnmbox server defs)
130   (nnmbox-create-mbox)
131   (cond
132    ((not (file-exists-p nnmbox-mbox-file))
133     (nnmbox-close-server)
134     (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
135    ((file-directory-p nnmbox-mbox-file)
136     (nnmbox-close-server)
137     (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
138    (t
139     (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
140                      nnmbox-mbox-file)
141     t)))
142
143 (deffoo nnmbox-close-server (&optional server)
144   (when (and nnmbox-mbox-buffer
145              (buffer-name nnmbox-mbox-buffer))
146     (kill-buffer nnmbox-mbox-buffer))
147   (nnoo-close-server 'nnmbox server)
148   t)
149
150 (deffoo nnmbox-server-opened (&optional server)
151   (and (nnoo-current-server-p 'nnmbox server)
152        nnmbox-mbox-buffer
153        (buffer-name nnmbox-mbox-buffer)
154        nntp-server-buffer
155        (buffer-name nntp-server-buffer)))
156
157 (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
158   (nnmbox-possibly-change-newsgroup newsgroup server)
159   (save-excursion
160     (set-buffer nnmbox-mbox-buffer)
161     (when (nnmbox-find-article article)
162       (let (start stop)
163         (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
164         (setq start (point))
165         (forward-line 1)
166         (or (and (re-search-forward
167                   (concat "^" message-unix-mail-delimiter) nil t)
168                  (forward-line -1))
169             (goto-char (point-max)))
170         (setq stop (point))
171         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
172           (set-buffer nntp-server-buffer)
173           (erase-buffer)
174           (insert-buffer-substring nnmbox-mbox-buffer start stop)
175           (goto-char (point-min))
176           (while (looking-at "From ")
177             (delete-char 5)
178             (insert "X-From-Line: ")
179             (forward-line 1))
180           (if (numberp article)
181               (cons nnmbox-current-group article)
182             (nnmbox-article-group-number nil)))))))
183
184 (deffoo nnmbox-request-group (group &optional server dont-check)
185   (nnmbox-possibly-change-newsgroup nil server)
186   (let ((active (cadr (assoc group nnmbox-group-alist))))
187     (cond
188      ((or (null active)
189           (null (nnmbox-possibly-change-newsgroup group server)))
190       (nnheader-report 'nnmbox "No such group: %s" group))
191      (dont-check
192       (nnheader-report 'nnmbox "Selected group %s" group)
193       (nnheader-insert ""))
194      (t
195       (nnheader-report 'nnmbox "Selected group %s" group)
196       (nnheader-insert "211 %d %d %d %s\n"
197                        (1+ (- (cdr active) (car active)))
198                        (car active) (cdr active) group)))))
199
200 (static-if (boundp 'MULE)
201     (defun nnmbox-save-buffer ()
202       (let ((output-coding-system
203              (or nnmbox-file-coding-system-for-write
204                  nnmbox-file-coding-system)))
205         (save-buffer)))
206   (defun nnmbox-save-buffer ()
207     (let ((coding-system-for-write
208            (or nnmbox-file-coding-system-for-write
209                nnmbox-file-coding-system)))
210       (save-buffer)))
211   )
212
213 (defun nnmbox-save-active (group-alist active-file)
214   (let ((nnmail-active-file-coding-system
215          (or nnmbox-active-file-coding-system-for-write
216              nnmbox-active-file-coding-system)))
217     (nnmail-save-active group-alist active-file)))
218
219 (deffoo nnmbox-request-scan (&optional group server)
220   (nnmbox-possibly-change-newsgroup group server)
221   (nnmbox-read-mbox)
222   (nnmail-get-new-mail
223    'nnmbox
224    (lambda ()
225      (save-excursion
226        (set-buffer nnmbox-mbox-buffer)
227        (nnmbox-save-buffer)))
228    (file-name-directory nnmbox-mbox-file)
229    group
230    (lambda ()
231      (save-excursion
232        (let ((in-buf (current-buffer)))
233          (set-buffer nnmbox-mbox-buffer)
234          (goto-char (point-max))
235          (insert-buffer-substring in-buf)))
236      (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
237
238 (deffoo nnmbox-close-group (group &optional server)
239   t)
240
241 (deffoo nnmbox-request-create-group (group &optional server args)
242   (nnmail-activate 'nnmbox)
243   (unless (assoc group nnmbox-group-alist)
244     (push (list group (cons 1 0))
245           nnmbox-group-alist)
246     (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
247   t)
248
249 (deffoo nnmbox-request-list (&optional server)
250   (save-excursion
251     (let ((nnmail-file-coding-system
252            nnmbox-active-file-coding-system))
253       (nnmail-find-file nnmbox-active-file))
254     (setq nnmbox-group-alist (nnmail-get-active))
255     t))
256
257 (deffoo nnmbox-request-newgroups (date &optional server)
258   (nnmbox-request-list server))
259
260 (deffoo nnmbox-request-list-newsgroups (&optional server)
261   (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
262
263 (deffoo nnmbox-request-expire-articles
264     (articles newsgroup &optional server force)
265   (nnmbox-possibly-change-newsgroup newsgroup server)
266   (let* ((is-old t)
267          rest)
268     (nnmail-activate 'nnmbox)
269
270     (save-excursion
271       (set-buffer nnmbox-mbox-buffer)
272       (while (and articles is-old)
273         (when (nnmbox-find-article (car articles))
274           (if (setq is-old
275                     (nnmail-expired-article-p
276                      newsgroup
277                      (buffer-substring
278                       (point) (progn (end-of-line) (point))) force))
279               (progn
280                 (unless (eq nnmail-expiry-target 'delete)
281                   (with-temp-buffer
282                     (nnmbox-request-article (car articles)
283                                             newsgroup server
284                                             (current-buffer))
285                     (let ((nnml-current-directory nil))
286                       (nnmail-expiry-target-group
287                        nnmail-expiry-target newsgroup)))
288                   (nnmbox-possibly-change-newsgroup newsgroup server))
289                 (nnheader-message 5 "Deleting article %d in %s..."
290                                   (car articles) newsgroup)
291                 (nnmbox-delete-mail))
292             (push (car articles) rest)))
293         (setq articles (cdr articles)))
294       (nnmbox-save-buffer)
295       ;; Find the lowest active article in this group.
296       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
297         (while (and (not (nnmbox-find-article (car active)))
298                     (<= (car active) (cdr active)))
299           (setcar active (1+ (car active)))))
300       (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
301       (nconc rest articles))))
302
303 (deffoo nnmbox-request-move-article
304     (article group server accept-form &optional last)
305   (let ((buf (get-buffer-create " *nnmbox move*"))
306         result)
307     (and
308      (nnmbox-request-article article group server)
309      (save-excursion
310        (set-buffer buf)
311        (erase-buffer)
312        (insert-buffer-substring nntp-server-buffer)
313        (goto-char (point-min))
314        (while (re-search-forward
315                "^X-Gnus-Newsgroup:"
316                (save-excursion (search-forward "\n\n" nil t) (point)) t)
317          (delete-region (progn (beginning-of-line) (point))
318                         (progn (forward-line 1) (point))))
319        (setq result (eval accept-form))
320        (kill-buffer buf)
321        result)
322      (save-excursion
323        (nnmbox-possibly-change-newsgroup group server)
324        (set-buffer nnmbox-mbox-buffer)
325        (when (nnmbox-find-article article)
326          (nnmbox-delete-mail))
327        (and last (nnmbox-save-buffer))))
328     result))
329
330 (deffoo nnmbox-request-accept-article (group &optional server last)
331   (nnmbox-possibly-change-newsgroup group server)
332   (nnmail-check-syntax)
333   (let ((buf (current-buffer))
334         result)
335     (goto-char (point-min))
336     ;; The From line may have been quoted by movemail.
337     (when (looking-at (concat ">" message-unix-mail-delimiter))
338       (delete-char 1))
339     (if (looking-at "X-From-Line: ")
340         (replace-match "From ")
341       (insert "From nobody " (current-time-string) "\n"))
342     (and
343      (nnmail-activate 'nnmbox)
344      (progn
345        (set-buffer buf)
346        (goto-char (point-min))
347        (search-forward "\n\n" nil t)
348        (forward-line -1)
349        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
350          (delete-region (point) (progn (forward-line 1) (point))))
351        (when nnmail-cache-accepted-message-ids
352          (nnmail-cache-insert (nnmail-fetch-field "message-id")))
353        (setq result (if (stringp group)
354                         (list (cons group (nnmbox-active-number group)))
355                       (nnmail-article-group 'nnmbox-active-number)))
356        (if (and (null result)
357                 (yes-or-no-p "Moved to `junk' group; delete article? "))
358            (setq result 'junk)
359          (setq result (car (nnmbox-save-mail result)))))
360      (save-excursion
361        (set-buffer nnmbox-mbox-buffer)
362        (goto-char (point-max))
363        (insert-buffer-substring buf)
364        (when last
365          (when nnmail-cache-accepted-message-ids
366            (nnmail-cache-close))
367          (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
368          (nnmbox-save-buffer))))
369     result))
370
371 (deffoo nnmbox-request-replace-article (article group buffer)
372   (nnmbox-possibly-change-newsgroup group)
373   (save-excursion
374     (set-buffer nnmbox-mbox-buffer)
375     (if (not (nnmbox-find-article article))
376         nil
377       (nnmbox-delete-mail t t)
378       (insert-buffer-substring buffer)
379       (nnmbox-save-buffer)
380       t)))
381
382 (deffoo nnmbox-request-delete-group (group &optional force server)
383   (nnmbox-possibly-change-newsgroup group server)
384   ;; Delete all articles in GROUP.
385   (if (not force)
386       ()                                ; Don't delete the articles.
387     (save-excursion
388       (set-buffer nnmbox-mbox-buffer)
389       (goto-char (point-min))
390       ;; Delete all articles in this group.
391       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
392             found)
393         (while (search-forward ident nil t)
394           (setq found t)
395           (nnmbox-delete-mail))
396         (when found
397           (nnmbox-save-buffer)))))
398   ;; Remove the group from all structures.
399   (setq nnmbox-group-alist
400         (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
401         nnmbox-current-group nil)
402   ;; Save the active file.
403   (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
404   t)
405
406 (deffoo nnmbox-request-rename-group (group new-name &optional server)
407   (nnmbox-possibly-change-newsgroup group server)
408   (save-excursion
409     (set-buffer nnmbox-mbox-buffer)
410     (goto-char (point-min))
411     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
412           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
413           found)
414       (while (search-forward ident nil t)
415         (replace-match new-ident t t)
416         (setq found t))
417       (when found
418         (nnmbox-save-buffer))))
419   (let ((entry (assoc group nnmbox-group-active-articles)))
420     (when entry
421       (setcar entry new-name)))
422   (let ((entry (assoc group nnmbox-group-alist)))
423     (when entry
424       (setcar entry new-name))
425     (setq nnmbox-current-group nil)
426     ;; Save the new group alist.
427     (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
428     t))
429
430 \f
431 ;;; Internal functions.
432
433 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
434 ;; headers there are.  If LEAVE-DELIM, don't delete the Unix mbox
435 ;; delimiter line.
436 (defun nnmbox-delete-mail (&optional force leave-delim)
437   ;; Delete the current X-Gnus-Newsgroup line.
438   ;; First delete record of active article, unless the article is being
439   ;; replaced, indicated by FORCE being non-nil.
440   (if (not force)
441       (nnmbox-record-deleted-article (nnmbox-article-group-number t)))
442   (or force
443       (delete-region
444        (progn (beginning-of-line) (point))
445        (progn (forward-line 1) (point))))
446   ;; Beginning of the article.
447   (save-excursion
448     (save-restriction
449       (narrow-to-region
450        (save-excursion
451          (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
452          (if leave-delim (progn (forward-line 1) (point))
453            (match-beginning 0)))
454        (progn
455          (forward-line 1)
456          (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
457                                      nil t)
458                   (if (and (not (bobp)) leave-delim)
459                       (progn (forward-line -2) (point))
460                     (match-beginning 0)))
461              (point-max))))
462       (goto-char (point-min))
463       ;; Only delete the article if no other group owns it as well.
464       (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
465         (delete-region (point-min) (point-max))))))
466
467 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
468   (when (and server
469              (not (nnmbox-server-opened server)))
470     (nnmbox-open-server server))
471   (when (or (not nnmbox-mbox-buffer)
472             (not (buffer-name nnmbox-mbox-buffer)))
473     (nnmbox-read-mbox))
474   (when (not nnmbox-group-alist)
475     (nnmail-activate 'nnmbox))
476   (if newsgroup
477       (when (assoc newsgroup nnmbox-group-alist)
478         (setq nnmbox-current-group newsgroup))
479     t))
480
481 (defun nnmbox-article-string (article)
482   (if (numberp article)
483       (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
484               (int-to-string article) " ")
485     (concat "\nMessage-ID: " article)))
486
487 (defun nnmbox-article-group-number (this-line)
488   (save-excursion
489     (if this-line
490         (beginning-of-line)
491       (goto-char (point-min)))
492     (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
493                              nil t)
494       (cons (buffer-substring (match-beginning 1) (match-end 1))
495             (string-to-int
496              (buffer-substring (match-beginning 2) (match-end 2)))))))
497
498 (defun nnmbox-in-header-p (pos)
499   "Return non-nil if POS is in the header of an article."
500   (save-excursion
501     (goto-char pos)
502     (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
503     (search-forward "\n\n" nil t)
504     (< pos (point))))
505
506 (defun nnmbox-find-article (article)
507   "Leaves point on the relevant X-Gnus-Newsgroup line if found."
508   ;; Check that article is in the active range first, to avoid an
509   ;; expensive exhaustive search if it isn't.
510   (if (and (numberp article)
511            (not (nnmbox-is-article-active-p article)))
512       nil
513     (let ((art-string (nnmbox-article-string article))
514           (found nil))
515       ;; There is the possibility that the X-Gnus-Newsgroup line appears
516       ;; in the body of an article (for instance, if an article has been
517       ;; forwarded from someone using Gnus as their mailer), so check
518       ;; that the line is actually part of the article header.
519       (or (and (search-forward art-string nil t)
520                (nnmbox-in-header-p (point)))
521           (progn
522             (goto-char (point-min))
523             (while (and (not found)
524                         (search-forward art-string nil t))
525               (setq found (nnmbox-in-header-p (point))))
526             found)))))
527
528 (defun nnmbox-record-active-article (group-art)
529   (let* ((group (car group-art))
530          (article (cdr group-art))
531          (entry
532           (or (assoc group nnmbox-group-active-articles)
533               (progn
534                 (push (list group)
535                       nnmbox-group-active-articles)
536                 (car nnmbox-group-active-articles)))))
537     ;; add article to index, either by building complete list
538     ;; in reverse order, or as a list of ranges.
539     (if (not nnmbox-group-building-active-articles)
540         (setcdr entry (gnus-add-to-range (cdr entry) (list article)))
541       (when (memq article (cdr entry))
542         (switch-to-buffer nnmbox-mbox-buffer)
543         (error "Article %s:%d already exists!" group article))
544       (when (and (cadr entry) (< article (cadr entry)))
545         (switch-to-buffer nnmbox-mbox-buffer)
546         (error "Article %s:%d out of order" group article))
547       (setcdr entry (cons article (cdr entry))))))
548
549 (defun nnmbox-record-deleted-article (group-art)
550   (let* ((group (car group-art))
551          (article (cdr group-art))
552          (entry
553           (or (assoc group nnmbox-group-active-articles)
554               (progn
555                 (push (list group)
556                       nnmbox-group-active-articles)
557                 (car nnmbox-group-active-articles)))))
558     ;; remove article from index
559     (setcdr entry (gnus-remove-from-range (cdr entry) (list article)))))
560
561 (defun nnmbox-is-article-active-p (article)
562   (gnus-member-of-range
563    article
564    (cdr (assoc nnmbox-current-group
565                nnmbox-group-active-articles))))
566
567 (defun nnmbox-save-mail (group-art)
568   "Called narrowed to an article."
569   (let ((delim (concat "^" message-unix-mail-delimiter)))
570     (goto-char (point-min))
571     ;; This might come from somewhere else.
572     (unless (looking-at delim)
573       (insert "From nobody " (current-time-string) "\n")
574       (goto-char (point-min)))
575     ;; Quote all "From " lines in the article.
576     (forward-line 1)
577     (while (re-search-forward delim nil t)
578       (beginning-of-line)
579       (insert "> "))
580     (nnmail-insert-lines)
581     (nnmail-insert-xref group-art)
582     (nnmbox-insert-newsgroup-line group-art)
583     (let ((alist group-art))
584       (while alist
585         (nnmbox-record-active-article (car alist))
586         (setq alist (cdr alist))))
587     (run-hooks 'nnmail-prepare-save-mail-hook)
588     (run-hooks 'nnmbox-prepare-save-mail-hook)
589     group-art))
590
591 (defun nnmbox-insert-newsgroup-line (group-art)
592   (save-excursion
593     (goto-char (point-min))
594     (when (search-forward "\n\n" nil t)
595       (forward-char -1)
596       (while group-art
597         (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n"
598                         (caar group-art) (cdar group-art)
599                         (current-time-string)))
600         (setq group-art (cdr group-art))))
601     t))
602
603 (defun nnmbox-active-number (group)
604   ;; Find the next article number in GROUP.
605   (let ((active (cadr (assoc group nnmbox-group-alist))))
606     (if active
607         (setcdr active (1+ (cdr active)))
608       ;; This group is new, so we create a new entry for it.
609       ;; This might be a bit naughty... creating groups on the drop of
610       ;; a hat, but I don't know...
611       (push (list group (setq active (cons 1 1)))
612             nnmbox-group-alist))
613     (cdr active)))
614
615 (defun nnmbox-create-mbox ()
616   (when (not (file-exists-p nnmbox-mbox-file))
617     (let ((nnmail-file-coding-system
618            (or nnmbox-file-coding-system-for-write
619                nnmbox-file-coding-system))
620           (dir (file-name-directory nnmbox-mbox-file)))
621       (and dir (gnus-make-directory dir))
622       (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg))))
623
624 (defun nnmbox-read-mbox ()
625   (nnmail-activate 'nnmbox)
626   (nnmbox-create-mbox)
627   (if (and nnmbox-mbox-buffer
628            (buffer-name nnmbox-mbox-buffer)
629            (save-excursion
630              (set-buffer nnmbox-mbox-buffer)
631              (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
632       ()
633     (save-excursion
634       (let ((delim (concat "^" message-unix-mail-delimiter))
635             (alist nnmbox-group-alist)
636             (nnmbox-group-building-active-articles t)
637             start end end-header number)
638         (set-buffer (setq nnmbox-mbox-buffer
639                           (let ((nnheader-file-coding-system
640                                  nnmbox-file-coding-system))
641                             (nnheader-find-file-noselect
642                              nnmbox-mbox-file t t))))
643         (buffer-disable-undo)
644
645         ;; Go through the group alist and compare against the mbox file.
646         (while alist
647           (goto-char (point-max))
648           (when (and (re-search-backward
649                       (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
650                               (caar alist)) nil t)
651                      (> (setq number
652                               (string-to-number
653                                (buffer-substring
654                                 (match-beginning 1) (match-end 1))))
655                         (cdadar alist)))
656             (setcdr (cadar alist) number))
657           (setq alist (cdr alist)))
658
659         ;; Examine all articles for our private X-Gnus-Newsgroup
660         ;; headers.  This is done primarily as a consistency check, but
661         ;; it is convenient for building an index of the articles
662         ;; present, to avoid costly searches for missing articles
663         ;; (eg. when expiring articles).
664         (goto-char (point-min))
665         (setq nnmbox-group-active-articles nil)
666         (while (re-search-forward delim nil t)
667           (setq start (match-beginning 0))
668           (save-excursion
669             (search-forward "\n\n" nil t)
670             (setq end-header (point))
671             (setq end (or (and
672                            (re-search-forward delim nil t)
673                            (match-beginning 0))
674                           (point-max))))
675           (if (search-forward "\nX-Gnus-Newsgroup: " end-header t)
676               ;; Build a list of articles in each group, remembering
677               ;; that each article may be in more than one group.
678               (progn
679                 (nnmbox-record-active-article (nnmbox-article-group-number t))
680                 (while (search-forward "\nX-Gnus-Newsgroup: " end-header t)
681                   (nnmbox-record-active-article (nnmbox-article-group-number t))))
682             ;; The article is either new, or for some other reason
683             ;; hasn't got our private headers, so add them now.  The
684             ;; only situation I've encountered when the X-Gnus-Newsgroup
685             ;; header is missing is if the article contains a forwarded
686             ;; message which does contain that header line (earlier
687             ;; versions of Gnus didn't restrict their search to the
688             ;; headers).  In this case, there is an Xref line which
689             ;; provides the relevant information to construct the
690             ;; missing header(s).
691             (save-excursion
692               (save-restriction
693                 (narrow-to-region start end)
694                 (if (re-search-forward "\nXref: [^ ]+" end-header t)
695                     ;; generate headers from Xref:
696                     (let (alist)
697                       (while (re-search-forward " \\([^:]+\\):\\([0-9]+\\)" end-header t)
698                         (push (cons (match-string 1)
699                                     (string-to-int (match-string 2))) alist))
700                       (nnmbox-insert-newsgroup-line alist))
701                   ;; this is really a new article
702                   (nnmbox-save-mail
703                    (nnmail-article-group 'nnmbox-active-number))))))
704           (goto-char end))
705         ;; put article lists in order
706         (setq alist nnmbox-group-active-articles)
707         (while alist
708           (setcdr (car alist) (gnus-compress-sequence (nreverse (cdar alist))))
709           (setq alist (cdr alist)))))))
710
711 (provide 'nnmbox)
712
713 ;;; nnmbox.el ends here