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