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