(shr-expand-newlines): Make nested boxes work.
[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, 2011
5 ;;   Free Software Foundation, Inc.
6
7 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
9 ;; Keywords: news, mail
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; For an overview of what the interface functions do, please see the
29 ;; Gnus sources.
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (condition-case nil
35     (require 'rmail)
36   (error (nnheader-message
37       5 "Ignore rmail errors from this file, you don't have rmail")))
38 (require 'nnmail)
39 (require 'nnoo)
40 (eval-when-compile (require 'cl))
41
42 (nnoo-declare nnbabyl)
43
44 (defvoo nnbabyl-mbox-file (expand-file-name "~/RMAIL")
45   "The name of the rmail box file in the users home directory.")
46
47 (defvoo nnbabyl-active-file (expand-file-name "~/.rmail-active")
48   "The name of the active file for the rmail box.")
49
50 (defvoo nnbabyl-get-new-mail t
51   "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
52
53
54 (defvoo nnbabyl-prepare-save-mail-hook nil
55   "Hook run narrowed to an article before saving.")
56
57 \f
58
59 (defvar nnbabyl-mail-delimiter "\^_")
60
61 (defconst nnbabyl-version "nnbabyl 1.0"
62   "nnbabyl version.")
63
64 (defvoo nnbabyl-mbox-buffer nil)
65 (defvoo nnbabyl-current-group nil)
66 (defvoo nnbabyl-status-string "")
67 (defvoo nnbabyl-group-alist nil)
68 (defvoo nnbabyl-active-timestamp nil)
69
70 (defvoo nnbabyl-previous-buffer-mode nil)
71
72 \f
73
74 ;;; Interface functions
75
76 (nnoo-define-basics nnbabyl)
77
78 (deffoo nnbabyl-retrieve-headers (articles &optional group server fetch-old)
79   (with-current-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     (with-current-buffer nnbabyl-mbox-buffer
140       (narrow-to-region
141        (caar nnbabyl-previous-buffer-mode)
142        (cdar nnbabyl-previous-buffer-mode))
143       (funcall (cdr nnbabyl-previous-buffer-mode))))
144   (nnoo-close-server 'nnbabyl server)
145   (setq nnbabyl-mbox-buffer nil)
146   t)
147
148 (deffoo nnbabyl-server-opened (&optional server)
149   (and (nnoo-current-server-p 'nnbabyl server)
150        nnbabyl-mbox-buffer
151        (buffer-name nnbabyl-mbox-buffer)
152        nntp-server-buffer
153        (buffer-name nntp-server-buffer)))
154
155 (deffoo nnbabyl-request-article (article &optional newsgroup server buffer)
156   (nnbabyl-possibly-change-newsgroup newsgroup server)
157   (with-current-buffer nnbabyl-mbox-buffer
158     (goto-char (point-min))
159     (when (search-forward (nnbabyl-article-string article) nil t)
160       (let (start stop summary-line)
161         (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
162           (goto-char (point-min))
163           (end-of-line))
164         (while (and (not (looking-at ".+:"))
165                     (zerop (forward-line 1))))
166         (setq start (point))
167         (or (when (re-search-forward
168                    (concat "^" nnbabyl-mail-delimiter) nil t)
169               (beginning-of-line)
170               t)
171             (goto-char (point-max)))
172         (setq stop (point))
173         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
174           (set-buffer nntp-server-buffer)
175           (erase-buffer)
176           (insert-buffer-substring nnbabyl-mbox-buffer start stop)
177           (goto-char (point-min))
178           ;; If there is an EOOH header, then we have to remove some
179           ;; duplicated headers.
180           (setq summary-line (looking-at "Summary-line:"))
181           (when (search-forward "\n*** EOOH ***" nil t)
182             (if summary-line
183                 ;; The headers to be deleted are located before the
184                 ;; EOOH line...
185                 (delete-region (point-min) (progn (forward-line 1)
186                                                   (point)))
187               ;; ...or after.
188               (delete-region (progn (beginning-of-line) (point))
189                              (or (search-forward "\n\n" nil t)
190                                  (point)))))
191           (if (numberp article)
192               (cons nnbabyl-current-group article)
193             (nnbabyl-article-group-number)))))))
194
195 (deffoo nnbabyl-request-group (group &optional server dont-check info)
196   (let ((active (cadr (assoc group nnbabyl-group-alist))))
197     (save-excursion
198       (cond
199        ((or (null active)
200             (null (nnbabyl-possibly-change-newsgroup group server)))
201         (nnheader-report 'nnbabyl "No such group: %s" group))
202        (dont-check
203         (nnheader-report 'nnbabyl "Selected group %s" group)
204         (nnheader-insert ""))
205        (t
206         (nnheader-report 'nnbabyl "Selected group %s" group)
207         (nnheader-insert "211 %d %d %d %s\n"
208                          (1+ (- (cdr active) (car active)))
209                          (car active) (cdr active) group))))))
210
211 (deffoo nnbabyl-request-scan (&optional group server)
212   (nnbabyl-possibly-change-newsgroup group server)
213   (nnbabyl-read-mbox)
214   (nnmail-get-new-mail
215    'nnbabyl
216    (lambda ()
217      (with-current-buffer nnbabyl-mbox-buffer
218        (save-buffer)))
219    (file-name-directory nnbabyl-mbox-file)
220    group
221    (lambda ()
222      (save-excursion
223        (let ((in-buf (current-buffer)))
224          (goto-char (point-min))
225          (while (search-forward "\n\^_\n" nil t)
226            (delete-char -1))
227          (set-buffer nnbabyl-mbox-buffer)
228          (goto-char (point-max))
229          (search-backward "\n\^_" nil t)
230          (goto-char (match-end 0))
231          (insert-buffer-substring in-buf)))
232      (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))))
233
234 (deffoo nnbabyl-close-group (group &optional server)
235   t)
236
237 (deffoo nnbabyl-request-create-group (group &optional server args)
238   (nnmail-activate 'nnbabyl)
239   (unless (assoc group nnbabyl-group-alist)
240     (push (list group (cons 1 0))
241           nnbabyl-group-alist)
242     (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
243   t)
244
245 (deffoo nnbabyl-request-list (&optional server)
246   (save-excursion
247     (nnmail-find-file nnbabyl-active-file)
248     (setq nnbabyl-group-alist (nnmail-get-active))
249     t))
250
251 (deffoo nnbabyl-request-newgroups (date &optional server)
252   (nnbabyl-request-list server))
253
254 (deffoo nnbabyl-request-list-newsgroups (&optional server)
255   (nnheader-report 'nnbabyl "nnbabyl: LIST NEWSGROUPS is not implemented."))
256
257 (deffoo nnbabyl-request-expire-articles
258     (articles newsgroup &optional server force)
259   (nnbabyl-possibly-change-newsgroup newsgroup server)
260   (let* ((is-old t)
261          rest)
262     (nnmail-activate 'nnbabyl)
263
264     (with-current-buffer nnbabyl-mbox-buffer
265       (set-text-properties (point-min) (point-max) nil)
266       (while (and articles is-old)
267         (goto-char (point-min))
268         (when (search-forward (nnbabyl-article-string (car articles)) nil t)
269           (if (setq is-old
270                     (nnmail-expired-article-p
271                      newsgroup
272                      (buffer-substring
273                       (point) (progn (end-of-line) (point))) force))
274               (progn
275                 (unless (eq nnmail-expiry-target 'delete)
276                   (with-temp-buffer
277                     (nnbabyl-request-article (car articles)
278                                              newsgroup server
279                                              (current-buffer))
280                     (let ((nnml-current-directory nil))
281                       (nnmail-expiry-target-group
282                        nnmail-expiry-target newsgroup)))
283                   (nnbabyl-possibly-change-newsgroup newsgroup server))
284                 (nnheader-message 5 "Deleting article %d in %s..."
285                                   (car articles) newsgroup)
286                 (nnbabyl-delete-mail))
287             (push (car articles) rest)))
288         (setq articles (cdr articles)))
289       (save-buffer)
290       ;; Find the lowest active article in this group.
291       (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
292         (goto-char (point-min))
293         (while (and (not (search-forward
294                           (nnbabyl-article-string (car active)) nil t))
295                     (<= (car active) (cdr active)))
296           (setcar active (1+ (car active)))
297           (goto-char (point-min))))
298       (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
299       (nconc rest articles))))
300
301 (deffoo nnbabyl-request-move-article
302     (article group server accept-form &optional last move-is-internal)
303   (let ((buf (get-buffer-create " *nnbabyl move*"))
304         result)
305     (and
306      (nnbabyl-request-article article group server)
307      (with-current-buffer buf
308        (insert-buffer-substring nntp-server-buffer)
309        (goto-char (point-min))
310        (while (re-search-forward
311                "^X-Gnus-Newsgroup:"
312                (save-excursion (search-forward "\n\n" nil t) (point)) t)
313          (delete-region (point-at-bol) (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 (point-at-bol) (progn (forward-line 1) (point))))
431   ;; Beginning of the article.
432   (save-excursion
433     (save-restriction
434       (widen)
435       (narrow-to-region
436        (save-excursion
437          (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
438            (goto-char (point-min))
439            (end-of-line))
440          (if leave-delim (progn (forward-line 1) (point))
441            (match-beginning 0)))
442        (progn
443          (forward-line 1)
444          (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
445                                      nil t)
446                   (match-beginning 0))
447              (point-max))))
448       (goto-char (point-min))
449       ;; Only delete the article if no other groups owns it as well.
450       (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
451         (delete-region (point-min) (point-max))))))
452
453 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server)
454   (when (and server
455              (not (nnbabyl-server-opened server)))
456     (nnbabyl-open-server server))
457   (when (or (not nnbabyl-mbox-buffer)
458             (not (buffer-name nnbabyl-mbox-buffer)))
459     (save-excursion (nnbabyl-read-mbox)))
460   (unless nnbabyl-group-alist
461     (nnmail-activate 'nnbabyl))
462   (if newsgroup
463       (if (assoc newsgroup nnbabyl-group-alist)
464           (setq nnbabyl-current-group newsgroup)
465         (nnheader-report 'nnbabyl "No such group in file"))
466     t))
467
468 (defun nnbabyl-article-string (article)
469   (if (numberp article)
470       (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
471               (int-to-string article) " ")
472     (concat "\nMessage-ID: " article)))
473
474 (defun nnbabyl-article-group-number ()
475   (save-excursion
476     (goto-char (point-min))
477     (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
478                              nil t)
479       (cons (buffer-substring (match-beginning 1) (match-end 1))
480             (string-to-number
481              (buffer-substring (match-beginning 2) (match-end 2)))))))
482
483 (defun nnbabyl-insert-lines ()
484   "Insert how many lines and chars there are in the body of the mail."
485   (let (lines chars)
486     (save-excursion
487       (goto-char (point-min))
488       (when (search-forward "\n\n" nil t)
489         ;; There may be an EOOH line here...
490         (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
491           (search-forward "\n\n" nil t))
492         (setq chars (- (point-max) (point))
493               lines (max (- (count-lines (point) (point-max)) 1) 0))
494         ;; Move back to the end of the headers.
495         (goto-char (point-min))
496         (search-forward "\n\n" nil t)
497         (forward-char -1)
498         (save-excursion
499           (when (re-search-backward "^Lines: " nil t)
500             (delete-region (point) (progn (forward-line 1) (point)))))
501         (insert (format "Lines: %d\n" lines))
502         chars))))
503
504 (defun nnbabyl-save-mail (group-art)
505   ;; Called narrowed to an article.
506   (nnbabyl-insert-lines)
507   (nnmail-insert-xref group-art)
508   (nnbabyl-insert-newsgroup-line group-art)
509   (run-hooks 'nnbabyl-prepare-save-mail-hook)
510   group-art)
511
512 (defun nnbabyl-insert-newsgroup-line (group-art)
513   (save-excursion
514     (goto-char (point-min))
515     (while (looking-at "From ")
516       (replace-match "Mail-from: From " t t)
517       (forward-line 1))
518     ;; If there is a C-l at the beginning of the narrowed region, this
519     ;; isn't really a "save", but rather a "scan".
520     (goto-char (point-min))
521     (unless (looking-at "\^L")
522       (save-excursion
523         (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
524         (goto-char (point-max))
525         (insert "\^_\n")))
526     (when (search-forward "\n\n" nil t)
527       (forward-char -1)
528       (while group-art
529         (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n"
530                         (caar group-art) (cdar group-art)
531                         (current-time-string)))
532         (setq group-art (cdr group-art))))
533     t))
534
535 (defun nnbabyl-active-number (group)
536   ;; Find the next article number in GROUP.
537   (let ((active (cadr (assoc group nnbabyl-group-alist))))
538     (if active
539         (setcdr active (1+ (cdr active)))
540       ;; This group is new, so we create a new entry for it.
541       ;; This might be a bit naughty... creating groups on the drop of
542       ;; a hat, but I don't know...
543       (push (list group (setq active (cons 1 1)))
544             nnbabyl-group-alist))
545     (cdr active)))
546
547 (defun nnbabyl-create-mbox ()
548   (unless (file-exists-p nnbabyl-mbox-file)
549     ;; Create a new, empty RMAIL mbox file.
550     (with-current-buffer (setq nnbabyl-mbox-buffer
551                                (create-file-buffer nnbabyl-mbox-file))
552       (setq buffer-file-name nnbabyl-mbox-file)
553       (insert "BABYL OPTIONS:\n\n\^_")
554       (nnmail-write-region
555        (point-min) (point-max) nnbabyl-mbox-file t 'nomesg))))
556
557 (defun nnbabyl-read-mbox ()
558   (nnmail-activate 'nnbabyl)
559   (nnbabyl-create-mbox)
560
561   (unless (and nnbabyl-mbox-buffer
562                (buffer-name nnbabyl-mbox-buffer)
563                (with-current-buffer nnbabyl-mbox-buffer
564                  (= (buffer-size) (nnheader-file-size nnbabyl-mbox-file))))
565     ;; This buffer has changed since we read it last.  Possibly.
566     (save-excursion
567       (let ((delim (concat "^" nnbabyl-mail-delimiter))
568             (alist nnbabyl-group-alist)
569             start end number)
570         (set-buffer (setq nnbabyl-mbox-buffer
571                           (nnheader-find-file-noselect
572                            nnbabyl-mbox-file nil t)))
573         ;; Save previous buffer mode.
574         (setq nnbabyl-previous-buffer-mode
575               (cons (cons (point-min) (point-max))
576                     major-mode))
577
578         (buffer-disable-undo)
579         (widen)
580         (setq buffer-read-only nil)
581         (fundamental-mode)
582
583         ;; Go through the group alist and compare against
584         ;; the rmail file.
585         (while alist
586           (goto-char (point-max))
587           (when (and (re-search-backward
588                       (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
589                               (caar alist))
590                       nil t)
591                      (> (setq number
592                               (string-to-number
593                                (buffer-substring
594                                 (match-beginning 1) (match-end 1))))
595                         (cdadar alist)))
596             (setcdr (cadar alist) number))
597           (setq alist (cdr alist)))
598
599         ;; We go through the mbox and make sure that each and
600         ;; every mail belongs to some group or other.
601         (goto-char (point-min))
602         (if (looking-at "\^L")
603             (setq start (point))
604           (re-search-forward delim nil t)
605           (setq start (match-end 0)))
606         (while (re-search-forward delim nil t)
607           (setq end (match-end 0))
608           (unless (search-backward "\nX-Gnus-Newsgroup: " start t)
609             (goto-char end)
610             (save-excursion
611               (save-restriction
612                 (narrow-to-region (goto-char start) end)
613                 (nnbabyl-save-mail
614                  (nnmail-article-group 'nnbabyl-active-number))
615                 (setq end (point-max)))))
616           (goto-char (setq start end)))
617         (when (buffer-modified-p (current-buffer))
618           (save-buffer))
619         (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
620
621 (defun nnbabyl-remove-incoming-delims ()
622   (goto-char (point-min))
623   (while (search-forward "\^_" nil t)
624     (replace-match "?" t t)))
625
626 (defun nnbabyl-check-mbox ()
627   "Go through the nnbabyl mbox and make sure that no article numbers are reused."
628   (interactive)
629   (let ((idents (make-vector 1000 0))
630         id)
631     (save-excursion
632       (when (or (not nnbabyl-mbox-buffer)
633                 (not (buffer-name nnbabyl-mbox-buffer)))
634         (nnbabyl-read-mbox))
635       (set-buffer nnbabyl-mbox-buffer)
636       (goto-char (point-min))
637       (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) "  nil t)
638         (if (intern-soft (setq id (match-string 1)) idents)
639             (progn
640               (delete-region (point-at-bol) (progn (forward-line 1) (point)))
641               (nnheader-message 7 "Moving %s..." id)
642               (nnbabyl-save-mail
643                (nnmail-article-group 'nnbabyl-active-number)))
644           (intern id idents)))
645       (when (buffer-modified-p (current-buffer))
646         (save-buffer))
647       (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
648       (nnheader-message 5 ""))))
649
650 (provide 'nnbabyl)
651
652 ;;; nnbabyl.el ends here