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