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