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