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