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