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