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