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