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