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