1 ;;; nnbabyl.el --- rmail mbox access for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; For an overview of what the interface functions do, please see the
35 (t (nnheader-message 5 "Ignore rmail errors from this file, you don't have rmail")))
38 (eval-when-compile (require 'cl
))
40 (nnoo-declare nnbabyl
)
42 (defvoo nnbabyl-mbox-file
(expand-file-name "~/RMAIL")
43 "The name of the rmail box file in the users home directory.")
45 (defvoo nnbabyl-active-file
(expand-file-name "~/.rmail-active")
46 "The name of the active file for the rmail box.")
48 (defvoo nnbabyl-get-new-mail t
49 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
51 (defvoo nnbabyl-prepare-save-mail-hook nil
52 "Hook run narrowed to an article before saving.")
56 (defvar nnbabyl-mail-delimiter
"\^_")
58 (defconst nnbabyl-version
"nnbabyl 1.0"
61 (defvoo nnbabyl-mbox-buffer nil
)
62 (defvoo nnbabyl-current-group nil
)
63 (defvoo nnbabyl-status-string
"")
64 (defvoo nnbabyl-group-alist nil
)
65 (defvoo nnbabyl-active-timestamp nil
)
67 (defvoo nnbabyl-previous-buffer-mode nil
)
70 (autoload 'gnus-set-text-properties
"gnus-ems"))
74 ;;; Interface functions
76 (nnoo-define-basics nnbabyl
)
78 (deffoo nnbabyl-retrieve-headers
(articles &optional group server fetch-old
)
80 (set-buffer nntp-server-buffer
)
82 (let ((number (length articles
))
84 (delim (concat "^" nnbabyl-mail-delimiter
))
85 article art-string start stop
)
86 (nnbabyl-possibly-change-newsgroup group server
)
87 (while (setq article
(pop articles
))
88 (setq art-string
(nnbabyl-article-string article
))
89 (set-buffer nnbabyl-mbox-buffer
)
91 (when (or (search-forward art-string nil t
)
92 (search-backward art-string nil t
))
93 (unless (re-search-backward delim nil t
)
94 (goto-char (point-min)))
95 (while (and (not (looking-at ".+:"))
96 (zerop (forward-line 1))))
98 (search-forward "\n\n" nil t
)
99 (setq stop
(1- (point)))
100 (set-buffer nntp-server-buffer
)
102 (princ article
(current-buffer))
103 (insert " Article retrieved.\n")
104 (insert-buffer-substring nnbabyl-mbox-buffer start stop
)
105 (goto-char (point-max))
107 (and (numberp nnmail-large-newsgroup
)
108 (> number nnmail-large-newsgroup
)
109 (zerop (%
(incf count
) 20))
110 (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
111 (/ (* count
100) number
))))
113 (and (numberp nnmail-large-newsgroup
)
114 (> number nnmail-large-newsgroup
)
115 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
117 (set-buffer nntp-server-buffer
)
118 (nnheader-fold-continuation-lines)
121 (deffoo nnbabyl-open-server
(server &optional defs
)
122 (nnoo-change-server 'nnbabyl server defs
)
123 (nnbabyl-create-mbox)
125 ((not (file-exists-p nnbabyl-mbox-file
))
126 (nnbabyl-close-server)
127 (nnheader-report 'nnbabyl
"No such file: %s" nnbabyl-mbox-file
))
128 ((file-directory-p nnbabyl-mbox-file
)
129 (nnbabyl-close-server)
130 (nnheader-report 'nnbabyl
"Not a regular file: %s" nnbabyl-mbox-file
))
132 (nnheader-report 'nnbabyl
"Opened server %s using mbox %s" server
136 (deffoo nnbabyl-close-server
(&optional server
)
137 ;; Restore buffer mode.
138 (when (and (nnbabyl-server-opened)
139 nnbabyl-previous-buffer-mode
)
141 (set-buffer nnbabyl-mbox-buffer
)
143 (caar nnbabyl-previous-buffer-mode
)
144 (cdar nnbabyl-previous-buffer-mode
))
145 (funcall (cdr nnbabyl-previous-buffer-mode
))))
146 (nnoo-close-server 'nnbabyl server
)
147 (setq nnbabyl-mbox-buffer nil
)
150 (deffoo nnbabyl-server-opened
(&optional server
)
151 (and (nnoo-current-server-p 'nnbabyl server
)
153 (buffer-name nnbabyl-mbox-buffer
)
155 (buffer-name nntp-server-buffer
)))
157 (deffoo nnbabyl-request-article
(article &optional newsgroup server buffer
)
158 (nnbabyl-possibly-change-newsgroup newsgroup server
)
160 (set-buffer nnbabyl-mbox-buffer
)
161 (goto-char (point-min))
162 (when (search-forward (nnbabyl-article-string article
) nil t
)
163 (let (start stop summary-line
)
164 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter
) nil t
)
165 (goto-char (point-min))
167 (while (and (not (looking-at ".+:"))
168 (zerop (forward-line 1))))
170 (or (when (re-search-forward
171 (concat "^" nnbabyl-mail-delimiter
) nil t
)
174 (goto-char (point-max)))
176 (let ((nntp-server-buffer (or buffer nntp-server-buffer
)))
177 (set-buffer nntp-server-buffer
)
179 (insert-buffer-substring nnbabyl-mbox-buffer start stop
)
180 (goto-char (point-min))
181 ;; If there is an EOOH header, then we have to remove some
182 ;; duplicated headers.
183 (setq summary-line
(looking-at "Summary-line:"))
184 (when (search-forward "\n*** EOOH ***" nil t
)
186 ;; The headers to be deleted are located before the
188 (delete-region (point-min) (progn (forward-line 1)
191 (delete-region (progn (beginning-of-line) (point))
192 (or (search-forward "\n\n" nil t
)
194 (if (numberp article
)
195 (cons nnbabyl-current-group article
)
196 (nnbabyl-article-group-number)))))))
198 (deffoo nnbabyl-request-group
(group &optional server dont-check
)
199 (let ((active (cadr (assoc group nnbabyl-group-alist
))))
203 (null (nnbabyl-possibly-change-newsgroup group server
)))
204 (nnheader-report 'nnbabyl
"No such group: %s" group
))
206 (nnheader-report 'nnbabyl
"Selected group %s" group
)
207 (nnheader-insert ""))
209 (nnheader-report 'nnbabyl
"Selected group %s" group
)
210 (nnheader-insert "211 %d %d %d %s\n"
211 (1+ (- (cdr active
) (car active
)))
212 (car active
) (cdr active
) group
))))))
214 (deffoo nnbabyl-request-scan
(&optional group server
)
215 (nnbabyl-possibly-change-newsgroup group server
)
221 (set-buffer nnbabyl-mbox-buffer
)
223 (file-name-directory nnbabyl-mbox-file
)
227 (let ((in-buf (current-buffer)))
228 (goto-char (point-min))
229 (while (search-forward "\n\^_\n" nil t
)
231 (set-buffer nnbabyl-mbox-buffer
)
232 (goto-char (point-max))
233 (search-backward "\n\^_" nil t
)
234 (goto-char (match-end 0))
235 (insert-buffer-substring in-buf
)))
236 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
))))
238 (deffoo nnbabyl-close-group
(group &optional server
)
241 (deffoo nnbabyl-request-create-group
(group &optional server args
)
242 (nnmail-activate 'nnbabyl
)
243 (unless (assoc group nnbabyl-group-alist
)
244 (push (list group
(cons 1 0))
246 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
))
249 (deffoo nnbabyl-request-list
(&optional server
)
251 (nnmail-find-file nnbabyl-active-file
)
252 (setq nnbabyl-group-alist
(nnmail-get-active))
255 (deffoo nnbabyl-request-newgroups
(date &optional server
)
256 (nnbabyl-request-list server
))
258 (deffoo nnbabyl-request-list-newsgroups
(&optional server
)
259 (nnheader-report 'nnbabyl
"nnbabyl: LIST NEWSGROUPS is not implemented."))
261 (deffoo nnbabyl-request-expire-articles
262 (articles newsgroup
&optional server force
)
263 (nnbabyl-possibly-change-newsgroup newsgroup server
)
266 (nnmail-activate 'nnbabyl
)
269 (set-buffer nnbabyl-mbox-buffer
)
270 (gnus-set-text-properties (point-min) (point-max) nil
)
271 (while (and articles is-old
)
272 (goto-char (point-min))
273 (when (search-forward (nnbabyl-article-string (car articles
)) nil t
)
275 (nnmail-expired-article-p
278 (point) (progn (end-of-line) (point))) force
))
280 (nnheader-message 5 "Deleting article %d in %s..."
281 (car articles
) newsgroup
)
282 (nnbabyl-delete-mail))
283 (push (car articles
) rest
)))
284 (setq articles
(cdr articles
)))
286 ;; Find the lowest active article in this group.
287 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist
))))
288 (goto-char (point-min))
289 (while (and (not (search-forward
290 (nnbabyl-article-string (car active
)) nil t
))
291 (<= (car active
) (cdr active
)))
292 (setcar active
(1+ (car active
)))
293 (goto-char (point-min))))
294 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
)
295 (nconc rest articles
))))
297 (deffoo nnbabyl-request-move-article
298 (article group server accept-form
&optional last
)
299 (let ((buf (get-buffer-create " *nnbabyl move*"))
302 (nnbabyl-request-article article group server
)
305 (insert-buffer-substring nntp-server-buffer
)
306 (goto-char (point-min))
307 (while (re-search-forward
309 (save-excursion (search-forward "\n\n" nil t
) (point)) t
)
310 (delete-region (progn (beginning-of-line) (point))
311 (progn (forward-line 1) (point))))
312 (setq result
(eval accept-form
))
313 (kill-buffer (current-buffer))
316 (nnbabyl-possibly-change-newsgroup group server
)
317 (set-buffer nnbabyl-mbox-buffer
)
318 (goto-char (point-min))
319 (if (search-forward (nnbabyl-article-string article
) nil t
)
320 (nnbabyl-delete-mail))
321 (and last
(save-buffer))))
324 (deffoo nnbabyl-request-accept-article
(group &optional server last
)
325 (nnbabyl-possibly-change-newsgroup group server
)
326 (nnmail-check-syntax)
327 (let ((buf (current-buffer))
330 (nnmail-activate 'nnbabyl
)
332 (goto-char (point-min))
333 (search-forward "\n\n" nil t
)
336 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t
)
337 (delete-region (point) (progn (forward-line 1) (point)))))
338 (when nnmail-cache-accepted-message-ids
339 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
342 (list (cons group
(nnbabyl-active-number group
)))
343 (nnmail-article-group 'nnbabyl-active-number
)))
344 (if (and (null result
)
345 (yes-or-no-p "Moved to `junk' group; delete article? "))
347 (setq result
(car (nnbabyl-save-mail result
))))
348 (set-buffer nnbabyl-mbox-buffer
)
349 (goto-char (point-max))
350 (search-backward "\n\^_")
351 (goto-char (match-end 0))
352 (insert-buffer-substring buf
)
354 (when nnmail-cache-accepted-message-ids
355 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
357 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
))
360 (deffoo nnbabyl-request-replace-article
(article group buffer
)
361 (nnbabyl-possibly-change-newsgroup group
)
363 (set-buffer nnbabyl-mbox-buffer
)
364 (goto-char (point-min))
365 (if (not (search-forward (nnbabyl-article-string article
) nil t
))
367 (nnbabyl-delete-mail t t
)
368 (insert-buffer-substring buffer
)
372 (deffoo nnbabyl-request-delete-group
(group &optional force server
)
373 (nnbabyl-possibly-change-newsgroup group server
)
374 ;; Delete all articles in GROUP.
376 () ; Don't delete the articles.
378 (set-buffer nnbabyl-mbox-buffer
)
379 (goto-char (point-min))
380 ;; Delete all articles in this group.
381 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group
":"))
383 (while (search-forward ident nil t
)
385 (nnbabyl-delete-mail))
388 ;; Remove the group from all structures.
389 (setq nnbabyl-group-alist
390 (delq (assoc group nnbabyl-group-alist
) nnbabyl-group-alist
)
391 nnbabyl-current-group nil
)
392 ;; Save the active file.
393 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
)
396 (deffoo nnbabyl-request-rename-group
(group new-name
&optional server
)
397 (nnbabyl-possibly-change-newsgroup group server
)
399 (set-buffer nnbabyl-mbox-buffer
)
400 (goto-char (point-min))
401 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group
":"))
402 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name
":"))
404 (while (search-forward ident nil t
)
405 (replace-match new-ident t t
)
409 (let ((entry (assoc group nnbabyl-group-alist
)))
410 (and entry
(setcar entry new-name
))
411 (setq nnbabyl-current-group nil
)
412 ;; Save the new group alist.
413 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
)
417 ;;; Internal functions.
419 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
420 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
422 (defun nnbabyl-delete-mail (&optional force leave-delim
)
423 ;; Delete the current X-Gnus-Newsgroup line.
426 (progn (beginning-of-line) (point))
427 (progn (forward-line 1) (point))))
428 ;; Beginning of the article.
434 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter
) nil t
)
435 (goto-char (point-min))
437 (if leave-delim
(progn (forward-line 1) (point))
438 (match-beginning 0)))
441 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter
)
445 (goto-char (point-min))
446 ;; Only delete the article if no other groups owns it as well.
447 (when (or force
(not (re-search-forward "^X-Gnus-Newsgroup: " nil t
)))
448 (delete-region (point-min) (point-max))))))
450 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server
)
452 (not (nnbabyl-server-opened server
)))
453 (nnbabyl-open-server server
))
454 (when (or (not nnbabyl-mbox-buffer
)
455 (not (buffer-name nnbabyl-mbox-buffer
)))
456 (save-excursion (nnbabyl-read-mbox)))
457 (unless nnbabyl-group-alist
458 (nnmail-activate 'nnbabyl
))
460 (if (assoc newsgroup nnbabyl-group-alist
)
461 (setq nnbabyl-current-group newsgroup
)
462 (nnheader-report 'nnbabyl
"No such group in file"))
465 (defun nnbabyl-article-string (article)
466 (if (numberp article
)
467 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group
":"
468 (int-to-string article
) " ")
469 (concat "\nMessage-ID: " article
)))
471 (defun nnbabyl-article-group-number ()
473 (goto-char (point-min))
474 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
476 (cons (buffer-substring (match-beginning 1) (match-end 1))
478 (buffer-substring (match-beginning 2) (match-end 2)))))))
480 (defun nnbabyl-insert-lines ()
481 "Insert how many lines and chars there are in the body of the mail."
484 (goto-char (point-min))
485 (when (search-forward "\n\n" nil t
)
486 ;; There may be an EOOH line here...
487 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
488 (search-forward "\n\n" nil t
))
489 (setq chars
(- (point-max) (point))
490 lines
(max (- (count-lines (point) (point-max)) 1) 0))
491 ;; Move back to the end of the headers.
492 (goto-char (point-min))
493 (search-forward "\n\n" nil t
)
496 (when (re-search-backward "^Lines: " nil t
)
497 (delete-region (point) (progn (forward-line 1) (point)))))
498 (insert (format "Lines: %d\n" lines
))
501 (defun nnbabyl-save-mail (group-art)
502 ;; Called narrowed to an article.
503 (nnbabyl-insert-lines)
504 (nnmail-insert-xref group-art
)
505 (nnbabyl-insert-newsgroup-line group-art
)
506 (run-hooks 'nnbabyl-prepare-save-mail-hook
)
509 (defun nnbabyl-insert-newsgroup-line (group-art)
511 (goto-char (point-min))
512 (while (looking-at "From ")
513 (replace-match "Mail-from: From " t t
)
515 ;; If there is a C-l at the beginning of the narrowed region, this
516 ;; isn't really a "save", but rather a "scan".
517 (goto-char (point-min))
518 (unless (looking-at "\^L")
520 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
521 (goto-char (point-max))
523 (when (search-forward "\n\n" nil t
)
526 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
527 (caar group-art
) (cdar group-art
)
528 (current-time-string)))
529 (setq group-art
(cdr group-art
))))
532 (defun nnbabyl-active-number (group)
533 ;; Find the next article number in GROUP.
534 (let ((active (cadr (assoc group nnbabyl-group-alist
))))
536 (setcdr active
(1+ (cdr active
)))
537 ;; This group is new, so we create a new entry for it.
538 ;; This might be a bit naughty... creating groups on the drop of
539 ;; a hat, but I don't know...
540 (push (list group
(setq active
(cons 1 1)))
541 nnbabyl-group-alist
))
544 (defun nnbabyl-create-mbox ()
545 (unless (file-exists-p nnbabyl-mbox-file
)
546 ;; Create a new, empty RMAIL mbox file.
548 (set-buffer (setq nnbabyl-mbox-buffer
549 (create-file-buffer nnbabyl-mbox-file
)))
550 (setq buffer-file-name nnbabyl-mbox-file
)
551 (insert "BABYL OPTIONS:\n\n\^_")
553 (point-min) (point-max) nnbabyl-mbox-file t
'nomesg
))))
555 (defun nnbabyl-read-mbox ()
556 (nnmail-activate 'nnbabyl
)
557 (nnbabyl-create-mbox)
559 (unless (and nnbabyl-mbox-buffer
560 (buffer-name nnbabyl-mbox-buffer
)
562 (set-buffer nnbabyl-mbox-buffer
)
563 (= (buffer-size) (nnheader-file-size nnbabyl-mbox-file
))))
564 ;; This buffer has changed since we read it last. Possibly.
566 (let ((delim (concat "^" nnbabyl-mail-delimiter
))
567 (alist nnbabyl-group-alist
)
569 (set-buffer (setq nnbabyl-mbox-buffer
570 (nnheader-find-file-noselect
571 nnbabyl-mbox-file nil
'raw
)))
572 ;; Save previous buffer mode.
573 (setq nnbabyl-previous-buffer-mode
574 (cons (cons (point-min) (point-max))
577 (buffer-disable-undo (current-buffer))
579 (setq buffer-read-only nil
)
582 ;; Go through the group alist and compare against
585 (goto-char (point-max))
586 (when (and (re-search-backward
587 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
593 (match-beginning 1) (match-end 1))))
595 (setcdr (cadar alist
) number
))
596 (setq alist
(cdr alist
)))
598 ;; We go through the mbox and make sure that each and
599 ;; every mail belongs to some group or other.
600 (goto-char (point-min))
601 (if (looking-at "\^L")
603 (re-search-forward delim nil t
)
604 (setq start
(match-end 0)))
605 (while (re-search-forward delim nil t
)
606 (setq end
(match-end 0))
607 (unless (search-backward "\nX-Gnus-Newsgroup: " start t
)
611 (narrow-to-region (goto-char start
) end
)
613 (nnmail-article-group 'nnbabyl-active-number
))
614 (setq end
(point-max)))))
615 (goto-char (setq start end
)))
616 (when (buffer-modified-p (current-buffer))
618 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
)))))
620 (defun nnbabyl-remove-incoming-delims ()
621 (goto-char (point-min))
622 (while (search-forward "\^_" nil t
)
623 (replace-match "?" t t
)))
625 (defun nnbabyl-check-mbox ()
626 "Go through the nnbabyl mbox and make sure that no article numbers are reused."
628 (let ((idents (make-vector 1000 0))
631 (when (or (not nnbabyl-mbox-buffer
)
632 (not (buffer-name nnbabyl-mbox-buffer
)))
634 (set-buffer nnbabyl-mbox-buffer
)
635 (goto-char (point-min))
636 (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) " nil t
)
637 (if (intern-soft (setq id
(match-string 1)) idents
)
639 (delete-region (progn (beginning-of-line) (point))
640 (progn (forward-line 1) (point)))
641 (nnheader-message 7 "Moving %s..." id
)
643 (nnmail-article-group 'nnbabyl-active-number
)))
645 (when (buffer-modified-p (current-buffer))
647 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file
)
648 (nnheader-message 5 ""))))
652 ;;; nnbabyl.el ends here