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