Merge branch 'master' of https://git.gnus.org/gnus
[gnus] / lisp / nnbabyl.el
1 ;;; nnbabyl.el --- rmail mbox access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1099, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
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   (error (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
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 \f
72
73 ;;; Interface functions
74
75 (nnoo-define-basics nnbabyl)
76
77 (deffoo nnbabyl-retrieve-headers (articles &optional group server fetch-old)
78   (with-current-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     (with-current-buffer nnbabyl-mbox-buffer
139       (narrow-to-region
140        (caar nnbabyl-previous-buffer-mode)
141        (cdar nnbabyl-previous-buffer-mode))
142       (funcall (cdr nnbabyl-previous-buffer-mode))))
143   (nnoo-close-server 'nnbabyl server)
144   (setq nnbabyl-mbox-buffer nil)
145   t)
146
147 (deffoo nnbabyl-server-opened (&optional server)
148   (and (nnoo-current-server-p 'nnbabyl server)
149        nnbabyl-mbox-buffer
150        (buffer-name nnbabyl-mbox-buffer)
151        nntp-server-buffer
152        (buffer-name nntp-server-buffer)))
153
154 (deffoo nnbabyl-request-article (article &optional newsgroup server buffer)
155   (nnbabyl-possibly-change-newsgroup newsgroup server)
156   (with-current-buffer nnbabyl-mbox-buffer
157     (goto-char (point-min))
158     (when (search-forward (nnbabyl-article-string article) nil t)
159       (let (start stop summary-line)
160         (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
161           (goto-char (point-min))
162           (end-of-line))
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 info)
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      (with-current-buffer nnbabyl-mbox-buffer
217        (save-buffer)))
218    (file-name-directory nnbabyl-mbox-file)
219    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     (with-current-buffer nnbabyl-mbox-buffer
264       (set-text-properties (point-min) (point-max) nil)
265       (while (and articles is-old)
266         (goto-char (point-min))
267         (when (search-forward (nnbabyl-article-string (car articles)) nil t)
268           (if (setq is-old
269                     (nnmail-expired-article-p
270                      newsgroup
271                      (buffer-substring
272                       (point) (progn (end-of-line) (point))) force))
273               (progn
274                 (unless (eq nnmail-expiry-target 'delete)
275                   (with-temp-buffer
276                     (nnbabyl-request-article (car articles)
277                                              newsgroup server
278                                              (current-buffer))
279                     (let ((nnml-current-directory nil))
280                       (nnmail-expiry-target-group
281                        nnmail-expiry-target newsgroup)))
282                   (nnbabyl-possibly-change-newsgroup newsgroup server))
283                 (nnheader-message 5 "Deleting article %d in %s..."
284                                   (car articles) newsgroup)
285                 (nnbabyl-delete-mail))
286             (push (car articles) rest)))
287         (setq articles (cdr articles)))
288       (save-buffer)
289       ;; Find the lowest active article in this group.
290       (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
291         (goto-char (point-min))
292         (while (and (not (search-forward
293                           (nnbabyl-article-string (car active)) nil t))
294                     (<= (car active) (cdr active)))
295           (setcar active (1+ (car active)))
296           (goto-char (point-min))))
297       (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
298       (nconc rest articles))))
299
300 (deffoo nnbabyl-request-move-article
301     (article group server accept-form &optional last move-is-internal)
302   (let ((buf (get-buffer-create " *nnbabyl move*"))
303         result)
304     (and
305      (nnbabyl-request-article article group server)
306      (with-current-buffer buf
307        (insert-buffer-substring nntp-server-buffer)
308        (goto-char (point-min))
309        (while (re-search-forward
310                "^X-Gnus-Newsgroup:"
311                (save-excursion (search-forward "\n\n" nil t) (point)) t)
312          (delete-region (progn (beginning-of-line) (point))
313                         (progn (forward-line 1) (point))))
314        (setq result (eval accept-form))
315        (kill-buffer (current-buffer))
316        result)
317      (save-excursion
318        (nnbabyl-possibly-change-newsgroup group server)
319        (set-buffer nnbabyl-mbox-buffer)
320        (goto-char (point-min))
321        (if (search-forward (nnbabyl-article-string article) nil t)
322            (nnbabyl-delete-mail))
323        (and last (save-buffer))))
324     result))
325
326 (deffoo nnbabyl-request-accept-article (group &optional server last)
327   (nnbabyl-possibly-change-newsgroup group server)
328   (nnmail-check-syntax)
329   (let ((buf (current-buffer))
330         result beg)
331     (and
332      (nnmail-activate 'nnbabyl)
333      (save-excursion
334        (goto-char (point-min))
335        (search-forward "\n\n" nil t)
336        (forward-line -1)
337        (save-excursion
338          (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
339            (delete-region (point) (progn (forward-line 1) (point)))))
340        (when nnmail-cache-accepted-message-ids
341          (nnmail-cache-insert (nnmail-fetch-field "message-id")
342                               group
343                               (nnmail-fetch-field "subject")
344                               (nnmail-fetch-field "from")))
345        (setq result
346              (if (stringp group)
347                  (list (cons group (nnbabyl-active-number group)))
348                (nnmail-article-group 'nnbabyl-active-number)))
349        (if (and (null result)
350                 (yes-or-no-p "Moved to `junk' group; delete article? "))
351            (setq result 'junk)
352          (setq result (car (nnbabyl-save-mail result))))
353        (set-buffer nnbabyl-mbox-buffer)
354        (goto-char (point-max))
355        (search-backward "\n\^_")
356        (goto-char (match-end 0))
357        (insert-buffer-substring buf)
358        (when last
359          (when nnmail-cache-accepted-message-ids
360            (nnmail-cache-insert (nnmail-fetch-field "message-id")
361                                 group
362                                 (nnmail-fetch-field "subject")
363                                 (nnmail-fetch-field "from")))
364          (save-buffer)
365          (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
366        result))))
367
368 (deffoo nnbabyl-request-replace-article (article group buffer)
369   (nnbabyl-possibly-change-newsgroup group)
370   (with-current-buffer nnbabyl-mbox-buffer
371     (goto-char (point-min))
372     (if (not (search-forward (nnbabyl-article-string article) nil t))
373         nil
374       (nnbabyl-delete-mail t t)
375       (insert-buffer-substring buffer)
376       (save-buffer)
377       t)))
378
379 (deffoo nnbabyl-request-delete-group (group &optional force server)
380   (nnbabyl-possibly-change-newsgroup group server)
381   ;; Delete all articles in GROUP.
382   (if (not force)
383       ()                                ; Don't delete the articles.
384     (with-current-buffer nnbabyl-mbox-buffer
385       (goto-char (point-min))
386       ;; Delete all articles in this group.
387       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
388             found)
389         (while (search-forward ident nil t)
390           (setq found t)
391           (nnbabyl-delete-mail))
392         (when found
393           (save-buffer)))))
394   ;; Remove the group from all structures.
395   (setq nnbabyl-group-alist
396         (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
397         nnbabyl-current-group nil)
398   ;; Save the active file.
399   (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
400   t)
401
402 (deffoo nnbabyl-request-rename-group (group new-name &optional server)
403   (nnbabyl-possibly-change-newsgroup group server)
404   (with-current-buffer nnbabyl-mbox-buffer
405     (goto-char (point-min))
406     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
407           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
408           found)
409       (while (search-forward ident nil t)
410         (replace-match new-ident t t)
411         (setq found t))
412       (when found
413         (save-buffer))))
414   (let ((entry (assoc group nnbabyl-group-alist)))
415     (and entry (setcar entry new-name))
416     (setq nnbabyl-current-group nil)
417     ;; Save the new group alist.
418     (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
419     t))
420
421 \f
422 ;;; Internal functions.
423
424 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
425 ;; headers there are.  If LEAVE-DELIM, don't delete the Unix mbox
426 ;; delimiter line.
427 (defun nnbabyl-delete-mail (&optional force leave-delim)
428   ;; Delete the current X-Gnus-Newsgroup line.
429   (unless force
430     (delete-region
431      (progn (beginning-of-line) (point))
432      (progn (forward-line 1) (point))))
433   ;; Beginning of the article.
434   (save-excursion
435     (save-restriction
436       (widen)
437       (narrow-to-region
438        (save-excursion
439          (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
440            (goto-char (point-min))
441            (end-of-line))
442          (if leave-delim (progn (forward-line 1) (point))
443            (match-beginning 0)))
444        (progn
445          (forward-line 1)
446          (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
447                                      nil t)
448                   (match-beginning 0))
449              (point-max))))
450       (goto-char (point-min))
451       ;; Only delete the article if no other groups owns it as well.
452       (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
453         (delete-region (point-min) (point-max))))))
454
455 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server)
456   (when (and server
457              (not (nnbabyl-server-opened server)))
458     (nnbabyl-open-server server))
459   (when (or (not nnbabyl-mbox-buffer)
460             (not (buffer-name nnbabyl-mbox-buffer)))
461     (save-excursion (nnbabyl-read-mbox)))
462   (unless nnbabyl-group-alist
463     (nnmail-activate 'nnbabyl))
464   (if newsgroup
465       (if (assoc newsgroup nnbabyl-group-alist)
466           (setq nnbabyl-current-group newsgroup)
467         (nnheader-report 'nnbabyl "No such group in file"))
468     t))
469
470 (defun nnbabyl-article-string (article)
471   (if (numberp article)
472       (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
473               (int-to-string article) " ")
474     (concat "\nMessage-ID: " article)))
475
476 (defun nnbabyl-article-group-number ()
477   (save-excursion
478     (goto-char (point-min))
479     (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
480                              nil t)
481       (cons (buffer-substring (match-beginning 1) (match-end 1))
482             (string-to-number
483              (buffer-substring (match-beginning 2) (match-end 2)))))))
484
485 (defun nnbabyl-insert-lines ()
486   "Insert how many lines and chars there are in the body of the mail."
487   (let (lines chars)
488     (save-excursion
489       (goto-char (point-min))
490       (when (search-forward "\n\n" nil t)
491         ;; There may be an EOOH line here...
492         (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
493           (search-forward "\n\n" nil t))
494         (setq chars (- (point-max) (point))
495               lines (max (- (count-lines (point) (point-max)) 1) 0))
496         ;; Move back to the end of the headers.
497         (goto-char (point-min))
498         (search-forward "\n\n" nil t)
499         (forward-char -1)
500         (save-excursion
501           (when (re-search-backward "^Lines: " nil t)
502             (delete-region (point) (progn (forward-line 1) (point)))))
503         (insert (format "Lines: %d\n" lines))
504         chars))))
505
506 (defun nnbabyl-save-mail (group-art)
507   ;; Called narrowed to an article.
508   (nnbabyl-insert-lines)
509   (nnmail-insert-xref group-art)
510   (nnbabyl-insert-newsgroup-line group-art)
511   (run-hooks 'nnbabyl-prepare-save-mail-hook)
512   group-art)
513
514 (defun nnbabyl-insert-newsgroup-line (group-art)
515   (save-excursion
516     (goto-char (point-min))
517     (while (looking-at "From ")
518       (replace-match "Mail-from: From " t t)
519       (forward-line 1))
520     ;; If there is a C-l at the beginning of the narrowed region, this
521     ;; isn't really a "save", but rather a "scan".
522     (goto-char (point-min))
523     (unless (looking-at "\^L")
524       (save-excursion
525         (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
526         (goto-char (point-max))
527         (insert "\^_\n")))
528     (when (search-forward "\n\n" nil t)
529       (forward-char -1)
530       (while group-art
531         (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n"
532                         (caar group-art) (cdar group-art)
533                         (current-time-string)))
534         (setq group-art (cdr group-art))))
535     t))
536
537 (defun nnbabyl-active-number (group)
538   ;; Find the next article number in GROUP.
539   (let ((active (cadr (assoc group nnbabyl-group-alist))))
540     (if active
541         (setcdr active (1+ (cdr active)))
542       ;; This group is new, so we create a new entry for it.
543       ;; This might be a bit naughty... creating groups on the drop of
544       ;; a hat, but I don't know...
545       (push (list group (setq active (cons 1 1)))
546             nnbabyl-group-alist))
547     (cdr active)))
548
549 (defun nnbabyl-create-mbox ()
550   (unless (file-exists-p nnbabyl-mbox-file)
551     ;; Create a new, empty RMAIL mbox file.
552     (with-current-buffer (setq nnbabyl-mbox-buffer
553                                (create-file-buffer nnbabyl-mbox-file))
554       (setq buffer-file-name nnbabyl-mbox-file)
555       (insert "BABYL OPTIONS:\n\n\^_")
556       (nnmail-write-region
557        (point-min) (point-max) nnbabyl-mbox-file t 'nomesg))))
558
559 (defun nnbabyl-read-mbox ()
560   (nnmail-activate 'nnbabyl)
561   (nnbabyl-create-mbox)
562
563   (unless (and nnbabyl-mbox-buffer
564                (buffer-name nnbabyl-mbox-buffer)
565                (with-current-buffer nnbabyl-mbox-buffer
566                  (= (buffer-size) (nnheader-file-size nnbabyl-mbox-file))))
567     ;; This buffer has changed since we read it last.  Possibly.
568     (save-excursion
569       (let ((delim (concat "^" nnbabyl-mail-delimiter))
570             (alist nnbabyl-group-alist)
571             start end number)
572         (set-buffer (setq nnbabyl-mbox-buffer
573                           (nnheader-find-file-noselect
574                            nnbabyl-mbox-file nil t)))
575         ;; Save previous buffer mode.
576         (setq nnbabyl-previous-buffer-mode
577               (cons (cons (point-min) (point-max))
578                     major-mode))
579
580         (buffer-disable-undo)
581         (widen)
582         (setq buffer-read-only nil)
583         (fundamental-mode)
584
585         ;; Go through the group alist and compare against
586         ;; the rmail file.
587         (while alist
588           (goto-char (point-max))
589           (when (and (re-search-backward
590                       (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
591                               (caar alist))
592                       nil t)
593                      (> (setq number
594                               (string-to-number
595                                (buffer-substring
596                                 (match-beginning 1) (match-end 1))))
597                         (cdadar alist)))
598             (setcdr (cadar alist) number))
599           (setq alist (cdr alist)))
600
601         ;; We go through the mbox and make sure that each and
602         ;; every mail belongs to some group or other.
603         (goto-char (point-min))
604         (if (looking-at "\^L")
605             (setq start (point))
606           (re-search-forward delim nil t)
607           (setq start (match-end 0)))
608         (while (re-search-forward delim nil t)
609           (setq end (match-end 0))
610           (unless (search-backward "\nX-Gnus-Newsgroup: " start t)
611             (goto-char end)
612             (save-excursion
613               (save-restriction
614                 (narrow-to-region (goto-char start) end)
615                 (nnbabyl-save-mail
616                  (nnmail-article-group 'nnbabyl-active-number))
617                 (setq end (point-max)))))
618           (goto-char (setq start end)))
619         (when (buffer-modified-p (current-buffer))
620           (save-buffer))
621         (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
622
623 (defun nnbabyl-remove-incoming-delims ()
624   (goto-char (point-min))
625   (while (search-forward "\^_" nil t)
626     (replace-match "?" t t)))
627
628 (defun nnbabyl-check-mbox ()
629   "Go through the nnbabyl mbox and make sure that no article numbers are reused."
630   (interactive)
631   (let ((idents (make-vector 1000 0))
632         id)
633     (save-excursion
634       (when (or (not nnbabyl-mbox-buffer)
635                 (not (buffer-name nnbabyl-mbox-buffer)))
636         (nnbabyl-read-mbox))
637       (set-buffer nnbabyl-mbox-buffer)
638       (goto-char (point-min))
639       (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) "  nil t)
640         (if (intern-soft (setq id (match-string 1)) idents)
641             (progn
642               (delete-region (progn (beginning-of-line) (point))
643                              (progn (forward-line 1) (point)))
644               (nnheader-message 7 "Moving %s..." id)
645               (nnbabyl-save-mail
646                (nnmail-article-group 'nnbabyl-active-number)))
647           (intern id idents)))
648       (when (buffer-modified-p (current-buffer))
649         (save-buffer))
650       (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
651       (nnheader-message 5 ""))))
652
653 (provide 'nnbabyl)
654
655 ;;; nnbabyl.el ends here