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