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