Synch to No Gnus 200601130715.
[elisp/gnus.git-] / lisp / nnmbox.el
1 ;;; nnmbox.el --- mail mbox access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005 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., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, 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 (defvar 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 (defun nnmbox-save-buffer ()
193   (let ((coding-system-for-write (or nnmbox-file-coding-system-for-write
194                                      nnmbox-file-coding-system)))
195     (save-buffer)))
196
197 (defun nnmbox-save-active (group-alist active-file)
198   (let ((nnmail-active-file-coding-system
199          (or nnmbox-active-file-coding-system-for-write
200              nnmbox-active-file-coding-system)))
201     (nnmail-save-active group-alist active-file)))
202
203 (deffoo nnmbox-request-scan (&optional group server)
204   (nnmbox-possibly-change-newsgroup group server)
205   (nnmbox-read-mbox)
206   (nnmail-get-new-mail
207    'nnmbox
208    (lambda ()
209      (save-excursion
210        (set-buffer nnmbox-mbox-buffer)
211        (nnmbox-save-buffer)))
212    (file-name-directory nnmbox-mbox-file)
213    group
214    (lambda ()
215      (save-excursion
216        (let ((in-buf (current-buffer)))
217          (set-buffer nnmbox-mbox-buffer)
218          (goto-char (point-max))
219          (insert-buffer-substring in-buf)))
220      (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
221
222 (deffoo nnmbox-close-group (group &optional server)
223   t)
224
225 (deffoo nnmbox-request-create-group (group &optional server args)
226   (nnmail-activate 'nnmbox)
227   (unless (assoc group nnmbox-group-alist)
228     (push (list group (cons 1 0))
229           nnmbox-group-alist)
230     (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
231   t)
232
233 (deffoo nnmbox-request-list (&optional server)
234   (save-excursion
235     (let ((nnmail-file-coding-system
236            nnmbox-active-file-coding-system))
237       (nnmail-find-file nnmbox-active-file))
238     (setq nnmbox-group-alist (nnmail-get-active))
239     t))
240
241 (deffoo nnmbox-request-newgroups (date &optional server)
242   (nnmbox-request-list server))
243
244 (deffoo nnmbox-request-list-newsgroups (&optional server)
245   (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
246
247 (deffoo nnmbox-request-expire-articles
248     (articles newsgroup &optional server force)
249   (nnmbox-possibly-change-newsgroup newsgroup server)
250   (let* ((is-old t)
251          rest)
252     (nnmail-activate 'nnmbox)
253
254     (save-excursion
255       (set-buffer nnmbox-mbox-buffer)
256       (while (and articles is-old)
257         (when (nnmbox-find-article (car articles))
258           (if (setq is-old
259                     (nnmail-expired-article-p
260                      newsgroup
261                      (buffer-substring
262                       (point) (progn (end-of-line) (point))) force))
263               (progn
264                 (unless (eq nnmail-expiry-target 'delete)
265                   (with-temp-buffer
266                     (nnmbox-request-article (car articles)
267                                             newsgroup server
268                                             (current-buffer))
269                     (let ((nnml-current-directory nil))
270                       (nnmail-expiry-target-group
271                        nnmail-expiry-target newsgroup)))
272                   (nnmbox-possibly-change-newsgroup newsgroup server))
273                 (nnheader-message 5 "Deleting article %d in %s..."
274                                   (car articles) newsgroup)
275                 (nnmbox-delete-mail))
276             (push (car articles) rest)))
277         (setq articles (cdr articles)))
278       (nnmbox-save-buffer)
279       ;; Find the lowest active article in this group.
280       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
281         (while (and (not (nnmbox-find-article (car active)))
282                     (<= (car active) (cdr active)))
283           (setcar active (1+ (car active)))))
284       (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
285       (nconc rest articles))))
286
287 (deffoo nnmbox-request-move-article
288     (article group server accept-form &optional last move-is-internal)
289   (let ((buf (get-buffer-create " *nnmbox move*"))
290         result)
291     (and
292      (nnmbox-request-article article group server)
293      (save-excursion
294        (set-buffer buf)
295        (erase-buffer)
296        (insert-buffer-substring nntp-server-buffer)
297        (goto-char (point-min))
298        (while (re-search-forward
299                "^X-Gnus-Newsgroup:"
300                (save-excursion (search-forward "\n\n" nil t) (point)) t)
301          (gnus-delete-line))
302        (setq result (eval accept-form))
303        (kill-buffer buf)
304        result)
305      (save-excursion
306        (nnmbox-possibly-change-newsgroup group server)
307        (set-buffer nnmbox-mbox-buffer)
308        (when (nnmbox-find-article article)
309          (nnmbox-delete-mail))
310        (and last (nnmbox-save-buffer))))
311     result))
312
313 (deffoo nnmbox-request-accept-article (group &optional server last)
314   (nnmbox-possibly-change-newsgroup group server)
315   (nnmail-check-syntax)
316   (let ((buf (current-buffer))
317         result)
318     (goto-char (point-min))
319     ;; The From line may have been quoted by movemail.
320     (when (looking-at (concat ">" message-unix-mail-delimiter))
321       (delete-char 1))
322     (if (looking-at "X-From-Line: ")
323         (replace-match "From ")
324       (insert "From nobody " (current-time-string) "\n"))
325     (and
326      (nnmail-activate 'nnmbox)
327      (progn
328        (set-buffer buf)
329        (goto-char (point-min))
330        (search-forward "\n\n" nil t)
331        (forward-line -1)
332        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
333          (delete-region (point) (progn (forward-line 1) (point))))
334        (when nnmail-cache-accepted-message-ids
335          (nnmail-cache-insert (nnmail-fetch-field "message-id") 
336                               group
337                               (nnmail-fetch-field "subject")
338                               (nnmail-fetch-field "from")))
339        (setq result (if (stringp group)
340                         (list (cons group (nnmbox-active-number group)))
341                       (nnmail-article-group 'nnmbox-active-number)))
342        (if (and (null result)
343                 (yes-or-no-p "Moved to `junk' group; delete article? "))
344            (setq result 'junk)
345          (setq result (car (nnmbox-save-mail result)))))
346      (save-excursion
347        (set-buffer nnmbox-mbox-buffer)
348        (goto-char (point-max))
349        (insert-buffer-substring buf)
350        (when last
351          (when nnmail-cache-accepted-message-ids
352            (nnmail-cache-close))
353          (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
354          (nnmbox-save-buffer))))
355     result))
356
357 (deffoo nnmbox-request-replace-article (article group buffer)
358   (nnmbox-possibly-change-newsgroup group)
359   (save-excursion
360     (set-buffer nnmbox-mbox-buffer)
361     (if (not (nnmbox-find-article article))
362         nil
363       (nnmbox-delete-mail t t)
364       (insert-buffer-substring buffer)
365       (nnmbox-save-buffer)
366       t)))
367
368 (deffoo nnmbox-request-delete-group (group &optional force server)
369   (nnmbox-possibly-change-newsgroup group server)
370   ;; Delete all articles in GROUP.
371   (if (not force)
372       ()                                ; Don't delete the articles.
373     (save-excursion
374       (set-buffer nnmbox-mbox-buffer)
375       (goto-char (point-min))
376       ;; Delete all articles in this group.
377       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
378             found)
379         (while (search-forward ident nil t)
380           (setq found t)
381           (nnmbox-delete-mail))
382         (when found
383           (nnmbox-save-buffer)))))
384   ;; Remove the group from all structures.
385   (setq nnmbox-group-alist
386         (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
387         nnmbox-current-group nil)
388   ;; Save the active file.
389   (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
390   t)
391
392 (deffoo nnmbox-request-rename-group (group new-name &optional server)
393   (nnmbox-possibly-change-newsgroup group server)
394   (save-excursion
395     (set-buffer nnmbox-mbox-buffer)
396     (goto-char (point-min))
397     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
398           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
399           found)
400       (while (search-forward ident nil t)
401         (replace-match new-ident t t)
402         (setq found t))
403       (when found
404         (nnmbox-save-buffer))))
405   (let ((entry (assoc group nnmbox-group-active-articles)))
406     (when entry
407       (setcar entry new-name)))
408   (let ((entry (assoc group nnmbox-group-alist)))
409     (when entry
410       (setcar entry new-name))
411     (setq nnmbox-current-group nil)
412     ;; Save the new group alist.
413     (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
414     t))
415
416 \f
417 ;;; Internal functions.
418
419 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
420 ;; headers there are.  If LEAVE-DELIM, don't delete the Unix mbox
421 ;; delimiter line.
422 (defun nnmbox-delete-mail (&optional force leave-delim)
423   ;; Delete the current X-Gnus-Newsgroup line.
424   ;; First delete record of active article, unless the article is being
425   ;; replaced, indicated by FORCE being non-nil.
426   (if (not force)
427       (nnmbox-record-deleted-article (nnmbox-article-group-number t)))
428   (or force
429       (gnus-delete-line))
430   ;; Beginning of the article.
431   (save-excursion
432     (save-restriction
433       (narrow-to-region
434        (save-excursion
435          (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
436          (if leave-delim (progn (forward-line 1) (point))
437            (match-beginning 0)))
438        (progn
439          (forward-line 1)
440          (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
441                                      nil t)
442                   (if (and (not (bobp)) leave-delim)
443                       (progn (forward-line -2) (point))
444                     (match-beginning 0)))
445              (point-max))))
446       (goto-char (point-min))
447       ;; Only delete the article if no other group owns it as well.
448       (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
449         (delete-region (point-min) (point-max))))))
450
451 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
452   (when (and server
453              (not (nnmbox-server-opened server)))
454     (nnmbox-open-server server))
455   (when (or (not nnmbox-mbox-buffer)
456             (not (buffer-name nnmbox-mbox-buffer)))
457     (nnmbox-read-mbox))
458   (when (not nnmbox-group-alist)
459     (nnmail-activate 'nnmbox))
460   (if newsgroup
461       (when (assoc newsgroup nnmbox-group-alist)
462         (setq nnmbox-current-group newsgroup))
463     t))
464
465 (defun nnmbox-article-string (article)
466   (if (numberp article)
467       (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
468               (int-to-string article) " ")
469     (concat "\nMessage-ID: " article)))
470
471 (defun nnmbox-article-group-number (this-line)
472   (save-excursion
473     (if this-line
474         (beginning-of-line)
475       (goto-char (point-min)))
476     (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
477                              nil t)
478       (cons (buffer-substring (match-beginning 1) (match-end 1))
479             (string-to-number
480              (buffer-substring (match-beginning 2) (match-end 2)))))))
481
482 (defun nnmbox-in-header-p (pos)
483   "Return non-nil if POS is in the header of an article."
484   (save-excursion
485     (goto-char pos)
486     (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
487     (search-forward "\n\n" nil t)
488     (< pos (point))))
489
490 (defun nnmbox-find-article (article)
491   "Leaves point on the relevant X-Gnus-Newsgroup line if found."
492   ;; Check that article is in the active range first, to avoid an
493   ;; expensive exhaustive search if it isn't.
494   (if (and (numberp article)
495            (not (nnmbox-is-article-active-p article)))
496       nil
497     (let ((art-string (nnmbox-article-string article))
498           (found nil))
499       ;; There is the possibility that the X-Gnus-Newsgroup line appears
500       ;; in the body of an article (for instance, if an article has been
501       ;; forwarded from someone using Gnus as their mailer), so check
502       ;; that the line is actually part of the article header.
503       (or (and (search-forward art-string nil t)
504                (nnmbox-in-header-p (point)))
505           (progn
506             (goto-char (point-min))
507             (while (and (not found)
508                         (search-forward art-string nil t))
509               (setq found (nnmbox-in-header-p (point))))
510             found)))))
511
512 (defun nnmbox-record-active-article (group-art)
513   (let* ((group (car group-art))
514          (article (cdr group-art))
515          (entry
516           (or (assoc group nnmbox-group-active-articles)
517               (progn
518                 (push (list group)
519                       nnmbox-group-active-articles)
520                 (car nnmbox-group-active-articles)))))
521     ;; add article to index, either by building complete list
522     ;; in reverse order, or as a list of ranges.
523     (if (not nnmbox-group-building-active-articles)
524         (setcdr entry (gnus-add-to-range (cdr entry) (list article)))
525       (when (memq article (cdr entry))
526         (switch-to-buffer nnmbox-mbox-buffer)
527         (error "Article %s:%d already exists!" group article))
528       (when (and (cadr entry) (< article (cadr entry)))
529         (switch-to-buffer nnmbox-mbox-buffer)
530         (error "Article %s:%d out of order" group article))
531       (setcdr entry (cons article (cdr entry))))))
532
533 (defun nnmbox-record-deleted-article (group-art)
534   (let* ((group (car group-art))
535          (article (cdr group-art))
536          (entry
537           (or (assoc group nnmbox-group-active-articles)
538               (progn
539                 (push (list group)
540                       nnmbox-group-active-articles)
541                 (car nnmbox-group-active-articles)))))
542     ;; remove article from index
543     (setcdr entry (gnus-remove-from-range (cdr entry) (list article)))))
544
545 (defun nnmbox-is-article-active-p (article)
546   (gnus-member-of-range
547    article
548    (cdr (assoc nnmbox-current-group
549                nnmbox-group-active-articles))))
550
551 (defun nnmbox-save-mail (group-art)
552   "Called narrowed to an article."
553   (let ((delim (concat "^" message-unix-mail-delimiter)))
554     (goto-char (point-min))
555     ;; This might come from somewhere else.
556     (unless (looking-at delim)
557       (insert "From nobody " (current-time-string) "\n")
558       (goto-char (point-min)))
559     ;; Quote all "From " lines in the article.
560     (forward-line 1)
561     (while (re-search-forward delim nil t)
562       (beginning-of-line)
563       (insert "> "))
564     (nnmail-insert-lines)
565     (nnmail-insert-xref group-art)
566     (nnmbox-insert-newsgroup-line group-art)
567     (let ((alist group-art))
568       (while alist
569         (nnmbox-record-active-article (car alist))
570         (setq alist (cdr alist))))
571     (run-hooks 'nnmail-prepare-save-mail-hook)
572     (run-hooks 'nnmbox-prepare-save-mail-hook)
573     group-art))
574
575 (defun nnmbox-insert-newsgroup-line (group-art)
576   (save-excursion
577     (goto-char (point-min))
578     (when (search-forward "\n\n" nil t)
579       (forward-char -1)
580       (while group-art
581         (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n"
582                         (caar group-art) (cdar group-art)
583                         (current-time-string)))
584         (setq group-art (cdr group-art))))
585     t))
586
587 (defun nnmbox-active-number (group)
588   ;; Find the next article number in GROUP.
589   (let ((active (cadr (assoc group nnmbox-group-alist))))
590     (if active
591         (setcdr active (1+ (cdr active)))
592       ;; This group is new, so we create a new entry for it.
593       ;; This might be a bit naughty... creating groups on the drop of
594       ;; a hat, but I don't know...
595       (push (list group (setq active (cons 1 1)))
596             nnmbox-group-alist))
597     (cdr active)))
598
599 (defun nnmbox-create-mbox ()
600   (when (not (file-exists-p nnmbox-mbox-file))
601     (let ((nnmail-file-coding-system
602            (or nnmbox-file-coding-system-for-write
603                nnmbox-file-coding-system))
604           (dir (file-name-directory nnmbox-mbox-file)))
605       (and dir (gnus-make-directory dir))
606       (nnmail-write-region (point-min) (point-min)
607                            nnmbox-mbox-file t 'nomesg))))
608
609 (defun nnmbox-read-mbox ()
610   (nnmail-activate 'nnmbox)
611   (nnmbox-create-mbox)
612   (if (and nnmbox-mbox-buffer
613            (buffer-name nnmbox-mbox-buffer)
614            (save-excursion
615              (set-buffer nnmbox-mbox-buffer)
616              (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
617       ()
618     (save-excursion
619       (let ((delim (concat "^" message-unix-mail-delimiter))
620             (alist nnmbox-group-alist)
621             (nnmbox-group-building-active-articles t)
622             start end end-header number)
623         (set-buffer (setq nnmbox-mbox-buffer
624                           (let ((nnheader-file-coding-system
625                                  nnmbox-file-coding-system))
626                             (nnheader-find-file-noselect
627                              nnmbox-mbox-file t t))))
628         (buffer-disable-undo)
629
630         ;; Go through the group alist and compare against the mbox file.
631         (while alist
632           (goto-char (point-max))
633           (when (and (re-search-backward
634                       (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
635                               (caar alist)) nil t)
636                      (> (setq number
637                               (string-to-number
638                                (buffer-substring
639                                 (match-beginning 1) (match-end 1))))
640                         (cdadar alist)))
641             (setcdr (cadar alist) number))
642           (setq alist (cdr alist)))
643
644         ;; Examine all articles for our private X-Gnus-Newsgroup
645         ;; headers.  This is done primarily as a consistency check, but
646         ;; it is convenient for building an index of the articles
647         ;; present, to avoid costly searches for missing articles
648         ;; (eg. when expiring articles).
649         (goto-char (point-min))
650         (setq nnmbox-group-active-articles nil)
651         (while (re-search-forward delim nil t)
652           (setq start (match-beginning 0))
653           (save-excursion
654             (search-forward "\n\n" nil t)
655             (setq end-header (point))
656             (setq end (or (and
657                            (re-search-forward delim nil t)
658                            (match-beginning 0))
659                           (point-max))))
660           (if (search-forward "\nX-Gnus-Newsgroup: " end-header t)
661               ;; Build a list of articles in each group, remembering
662               ;; that each article may be in more than one group.
663               (progn
664                 (nnmbox-record-active-article (nnmbox-article-group-number t))
665                 (while (search-forward "\nX-Gnus-Newsgroup: " end-header t)
666                   (nnmbox-record-active-article (nnmbox-article-group-number t))))
667             ;; The article is either new, or for some other reason
668             ;; hasn't got our private headers, so add them now.  The
669             ;; only situation I've encountered when the X-Gnus-Newsgroup
670             ;; header is missing is if the article contains a forwarded
671             ;; message which does contain that header line (earlier
672             ;; versions of Gnus didn't restrict their search to the
673             ;; headers).  In this case, there is an Xref line which
674             ;; provides the relevant information to construct the
675             ;; missing header(s).
676             (save-excursion
677               (save-restriction
678                 (narrow-to-region start end)
679                 (if (re-search-forward "\nXref: [^ ]+" end-header t)
680                     ;; generate headers from Xref:
681                     (let (alist)
682                       (while (re-search-forward " \\([^:]+\\):\\([0-9]+\\)" end-header t)
683                         (push (cons (match-string 1)
684                                     (string-to-number (match-string 2))) alist))
685                       (nnmbox-insert-newsgroup-line alist))
686                   ;; this is really a new article
687                   (nnmbox-save-mail
688                    (nnmail-article-group 'nnmbox-active-number))))))
689           (goto-char end))
690         ;; put article lists in order
691         (setq alist nnmbox-group-active-articles)
692         (while alist
693           (setcdr (car alist) (gnus-compress-sequence (nreverse (cdar alist))))
694           (setq alist (cdr alist)))))))
695
696 (provide 'nnmbox)
697
698 ;;; nnmbox.el ends here