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