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