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