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