1 ;;; nnbabyl.el --- rmail mbox access for Gnus
2 ;; Copyright (C) 1995,96,97,98,99 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
36 5 "Ignore rmail errors from this file, you don't have rmail")))
39 (eval-when-compile (require 'cl))
41 (nnoo-declare nnbabyl)
43 (defvoo nnbabyl-mbox-file (expand-file-name "~/RMAIL")
44 "The name of the rmail box file in the users home directory.")
46 (defvoo nnbabyl-active-file (expand-file-name "~/.rmail-active")
47 "The name of the active file for the rmail box.")
49 (defvoo nnbabyl-get-new-mail t
50 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
52 (defvoo nnbabyl-prepare-save-mail-hook nil
53 "Hook run narrowed to an article before saving.")
57 (defvar nnbabyl-mail-delimiter "\^_")
59 (defconst nnbabyl-version "nnbabyl 1.0"
62 (defvoo nnbabyl-mbox-buffer nil)
63 (defvoo nnbabyl-current-group nil)
64 (defvoo nnbabyl-status-string "")
65 (defvoo nnbabyl-group-alist nil)
66 (defvoo nnbabyl-active-timestamp nil)
68 (defvoo nnbabyl-previous-buffer-mode nil)
71 (autoload 'gnus-set-text-properties "gnus-ems"))
75 ;;; Interface functions
77 (nnoo-define-basics nnbabyl)
79 (deffoo nnbabyl-retrieve-headers (articles &optional group server fetch-old)
81 (set-buffer nntp-server-buffer)
83 (let ((number (length articles))
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)
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))))
99 (search-forward "\n\n" nil t)
100 (setq stop (1- (point)))
101 (set-buffer nntp-server-buffer)
103 (princ article (current-buffer))
104 (insert " Article retrieved.\n")
105 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
106 (goto-char (point-max))
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))))
114 (and (numberp nnmail-large-newsgroup)
115 (> number nnmail-large-newsgroup)
116 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
118 (set-buffer nntp-server-buffer)
119 (nnheader-fold-continuation-lines)
122 (deffoo nnbabyl-open-server (server &optional defs)
123 (nnoo-change-server 'nnbabyl server defs)
124 (nnbabyl-create-mbox)
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))
133 (nnheader-report 'nnbabyl "Opened server %s using mbox %s" server
137 (deffoo nnbabyl-close-server (&optional server)
138 ;; Restore buffer mode.
139 (when (and (nnbabyl-server-opened)
140 nnbabyl-previous-buffer-mode)
142 (set-buffer nnbabyl-mbox-buffer)
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)
151 (deffoo nnbabyl-server-opened (&optional server)
152 (and (nnoo-current-server-p 'nnbabyl server)
154 (buffer-name nnbabyl-mbox-buffer)
156 (buffer-name nntp-server-buffer)))
158 (deffoo nnbabyl-request-article (article &optional newsgroup server buffer)
159 (nnbabyl-possibly-change-newsgroup newsgroup server)
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))
168 (while (and (not (looking-at ".+:"))
169 (zerop (forward-line 1))))
171 (or (when (re-search-forward
172 (concat "^" nnbabyl-mail-delimiter) nil t)
175 (goto-char (point-max)))
177 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
178 (set-buffer nntp-server-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)
187 ;; The headers to be deleted are located before the
189 (delete-region (point-min) (progn (forward-line 1)
192 (delete-region (progn (beginning-of-line) (point))
193 (or (search-forward "\n\n" nil t)
195 (if (numberp article)
196 (cons nnbabyl-current-group article)
197 (nnbabyl-article-group-number)))))))
199 (deffoo nnbabyl-request-group (group &optional server dont-check)
200 (let ((active (cadr (assoc group nnbabyl-group-alist))))
204 (null (nnbabyl-possibly-change-newsgroup group server)))
205 (nnheader-report 'nnbabyl "No such group: %s" group))
207 (nnheader-report 'nnbabyl "Selected group %s" group)
208 (nnheader-insert ""))
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))))))
215 (deffoo nnbabyl-request-scan (&optional group server)
216 (nnbabyl-possibly-change-newsgroup group server)
222 (set-buffer nnbabyl-mbox-buffer)
224 (file-name-directory nnbabyl-mbox-file)
228 (let ((in-buf (current-buffer)))
229 (goto-char (point-min))
230 (while (search-forward "\n\^_\n" nil t)
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))))
239 (deffoo nnbabyl-close-group (group &optional server)
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))
247 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
250 (deffoo nnbabyl-request-list (&optional server)
252 (nnmail-find-file nnbabyl-active-file)
253 (setq nnbabyl-group-alist (nnmail-get-active))
256 (deffoo nnbabyl-request-newgroups (date &optional server)
257 (nnbabyl-request-list server))
259 (deffoo nnbabyl-request-list-newsgroups (&optional server)
260 (nnheader-report 'nnbabyl "nnbabyl: LIST NEWSGROUPS is not implemented."))
262 (deffoo nnbabyl-request-expire-articles
263 (articles newsgroup &optional server force)
264 (nnbabyl-possibly-change-newsgroup newsgroup server)
267 (nnmail-activate 'nnbabyl)
270 (set-buffer nnbabyl-mbox-buffer)
271 (gnus-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)
276 (nnmail-expired-article-p
279 (point) (progn (end-of-line) (point))) force))
281 (nnheader-message 5 "Deleting article %d in %s..."
282 (car articles) newsgroup)
283 (nnbabyl-delete-mail))
284 (push (car articles) rest)))
285 (setq articles (cdr articles)))
287 ;; Find the lowest active article in this group.
288 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
289 (goto-char (point-min))
290 (while (and (not (search-forward
291 (nnbabyl-article-string (car active)) nil t))
292 (<= (car active) (cdr active)))
293 (setcar active (1+ (car active)))
294 (goto-char (point-min))))
295 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
296 (nconc rest articles))))
298 (deffoo nnbabyl-request-move-article
299 (article group server accept-form &optional last)
300 (let ((buf (get-buffer-create " *nnbabyl move*"))
303 (nnbabyl-request-article article group server)
306 (insert-buffer-substring nntp-server-buffer)
307 (goto-char (point-min))
308 (while (re-search-forward
310 (save-excursion (search-forward "\n\n" nil t) (point)) t)
311 (delete-region (progn (beginning-of-line) (point))
312 (progn (forward-line 1) (point))))
313 (setq result (eval accept-form))
314 (kill-buffer (current-buffer))
317 (nnbabyl-possibly-change-newsgroup group server)
318 (set-buffer nnbabyl-mbox-buffer)
319 (goto-char (point-min))
320 (if (search-forward (nnbabyl-article-string article) nil t)
321 (nnbabyl-delete-mail))
322 (and last (save-buffer))))
325 (deffoo nnbabyl-request-accept-article (group &optional server last)
326 (nnbabyl-possibly-change-newsgroup group server)
327 (nnmail-check-syntax)
328 (let ((buf (current-buffer))
331 (nnmail-activate 'nnbabyl)
333 (goto-char (point-min))
334 (search-forward "\n\n" nil t)
337 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
338 (delete-region (point) (progn (forward-line 1) (point)))))
339 (when nnmail-cache-accepted-message-ids
340 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
343 (list (cons group (nnbabyl-active-number group)))
344 (nnmail-article-group 'nnbabyl-active-number)))
345 (if (and (null result)
346 (yes-or-no-p "Moved to `junk' group; delete article? "))
348 (setq result (car (nnbabyl-save-mail result))))
349 (set-buffer nnbabyl-mbox-buffer)
350 (goto-char (point-max))
351 (search-backward "\n\^_")
352 (goto-char (match-end 0))
353 (insert-buffer-substring buf)
355 (when nnmail-cache-accepted-message-ids
356 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
358 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
361 (deffoo nnbabyl-request-replace-article (article group buffer)
362 (nnbabyl-possibly-change-newsgroup group)
364 (set-buffer nnbabyl-mbox-buffer)
365 (goto-char (point-min))
366 (if (not (search-forward (nnbabyl-article-string article) nil t))
368 (nnbabyl-delete-mail t t)
369 (insert-buffer-substring buffer)
373 (deffoo nnbabyl-request-delete-group (group &optional force server)
374 (nnbabyl-possibly-change-newsgroup group server)
375 ;; Delete all articles in GROUP.
377 () ; Don't delete the articles.
379 (set-buffer nnbabyl-mbox-buffer)
380 (goto-char (point-min))
381 ;; Delete all articles in this group.
382 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
384 (while (search-forward ident nil t)
386 (nnbabyl-delete-mail))
389 ;; Remove the group from all structures.
390 (setq nnbabyl-group-alist
391 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
392 nnbabyl-current-group nil)
393 ;; Save the active file.
394 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
397 (deffoo nnbabyl-request-rename-group (group new-name &optional server)
398 (nnbabyl-possibly-change-newsgroup group server)
400 (set-buffer nnbabyl-mbox-buffer)
401 (goto-char (point-min))
402 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
403 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
405 (while (search-forward ident nil t)
406 (replace-match new-ident t t)
410 (let ((entry (assoc group nnbabyl-group-alist)))
411 (and entry (setcar entry new-name))
412 (setq nnbabyl-current-group nil)
413 ;; Save the new group alist.
414 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
418 ;;; Internal functions.
420 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
421 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
423 (defun nnbabyl-delete-mail (&optional force leave-delim)
424 ;; Delete the current X-Gnus-Newsgroup line.
427 (progn (beginning-of-line) (point))
428 (progn (forward-line 1) (point))))
429 ;; Beginning of the article.
435 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
436 (goto-char (point-min))
438 (if leave-delim (progn (forward-line 1) (point))
439 (match-beginning 0)))
442 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
446 (goto-char (point-min))
447 ;; Only delete the article if no other groups owns it as well.
448 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
449 (delete-region (point-min) (point-max))))))
451 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server)
453 (not (nnbabyl-server-opened server)))
454 (nnbabyl-open-server server))
455 (when (or (not nnbabyl-mbox-buffer)
456 (not (buffer-name nnbabyl-mbox-buffer)))
457 (save-excursion (nnbabyl-read-mbox)))
458 (unless nnbabyl-group-alist
459 (nnmail-activate 'nnbabyl))
461 (if (assoc newsgroup nnbabyl-group-alist)
462 (setq nnbabyl-current-group newsgroup)
463 (nnheader-report 'nnbabyl "No such group in file"))
466 (defun nnbabyl-article-string (article)
467 (if (numberp article)
468 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
469 (int-to-string article) " ")
470 (concat "\nMessage-ID: " article)))
472 (defun nnbabyl-article-group-number ()
474 (goto-char (point-min))
475 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
477 (cons (buffer-substring (match-beginning 1) (match-end 1))
479 (buffer-substring (match-beginning 2) (match-end 2)))))))
481 (defun nnbabyl-insert-lines ()
482 "Insert how many lines and chars there are in the body of the mail."
485 (goto-char (point-min))
486 (when (search-forward "\n\n" nil t)
487 ;; There may be an EOOH line here...
488 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
489 (search-forward "\n\n" nil t))
490 (setq chars (- (point-max) (point))
491 lines (max (- (count-lines (point) (point-max)) 1) 0))
492 ;; Move back to the end of the headers.
493 (goto-char (point-min))
494 (search-forward "\n\n" nil t)
497 (when (re-search-backward "^Lines: " nil t)
498 (delete-region (point) (progn (forward-line 1) (point)))))
499 (insert (format "Lines: %d\n" lines))
502 (defun nnbabyl-save-mail (group-art)
503 ;; Called narrowed to an article.
504 (nnbabyl-insert-lines)
505 (nnmail-insert-xref group-art)
506 (nnbabyl-insert-newsgroup-line group-art)
507 (run-hooks 'nnbabyl-prepare-save-mail-hook)
510 (defun nnbabyl-insert-newsgroup-line (group-art)
512 (goto-char (point-min))
513 (while (looking-at "From ")
514 (replace-match "Mail-from: From " t t)
516 ;; If there is a C-l at the beginning of the narrowed region, this
517 ;; isn't really a "save", but rather a "scan".
518 (goto-char (point-min))
519 (unless (looking-at "\^L")
521 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
522 (goto-char (point-max))
524 (when (search-forward "\n\n" nil t)
527 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
528 (caar group-art) (cdar group-art)
529 (current-time-string)))
530 (setq group-art (cdr group-art))))
533 (defun nnbabyl-active-number (group)
534 ;; Find the next article number in GROUP.
535 (let ((active (cadr (assoc group nnbabyl-group-alist))))
537 (setcdr active (1+ (cdr active)))
538 ;; This group is new, so we create a new entry for it.
539 ;; This might be a bit naughty... creating groups on the drop of
540 ;; a hat, but I don't know...
541 (push (list group (setq active (cons 1 1)))
542 nnbabyl-group-alist))
545 (defun nnbabyl-create-mbox ()
546 (unless (file-exists-p nnbabyl-mbox-file)
547 ;; Create a new, empty RMAIL mbox file.
549 (set-buffer (setq nnbabyl-mbox-buffer
550 (create-file-buffer nnbabyl-mbox-file)))
551 (setq buffer-file-name nnbabyl-mbox-file)
552 (insert "BABYL OPTIONS:\n\n\^_")
554 (point-min) (point-max) nnbabyl-mbox-file t 'nomesg))))
556 (defun nnbabyl-read-mbox ()
557 (nnmail-activate 'nnbabyl)
558 (nnbabyl-create-mbox)
560 (unless (and nnbabyl-mbox-buffer
561 (buffer-name nnbabyl-mbox-buffer)
563 (set-buffer nnbabyl-mbox-buffer)
564 (= (buffer-size) (nnheader-file-size nnbabyl-mbox-file))))
565 ;; This buffer has changed since we read it last. Possibly.
567 (let ((delim (concat "^" nnbabyl-mail-delimiter))
568 (alist nnbabyl-group-alist)
570 (set-buffer (setq nnbabyl-mbox-buffer
571 (nnheader-find-file-noselect
572 nnbabyl-mbox-file nil t)))
573 ;; Save previous buffer mode.
574 (setq nnbabyl-previous-buffer-mode
575 (cons (cons (point-min) (point-max))
578 (buffer-disable-undo)
580 (setq buffer-read-only nil)
583 ;; Go through the group alist and compare against
586 (goto-char (point-max))
587 (when (and (re-search-backward
588 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
594 (match-beginning 1) (match-end 1))))
596 (setcdr (cadar alist) number))
597 (setq alist (cdr alist)))
599 ;; We go through the mbox and make sure that each and
600 ;; every mail belongs to some group or other.
601 (goto-char (point-min))
602 (if (looking-at "\^L")
604 (re-search-forward delim nil t)
605 (setq start (match-end 0)))
606 (while (re-search-forward delim nil t)
607 (setq end (match-end 0))
608 (unless (search-backward "\nX-Gnus-Newsgroup: " start t)
612 (narrow-to-region (goto-char start) end)
614 (nnmail-article-group 'nnbabyl-active-number))
615 (setq end (point-max)))))
616 (goto-char (setq start end)))
617 (when (buffer-modified-p (current-buffer))
619 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
621 (defun nnbabyl-remove-incoming-delims ()
622 (goto-char (point-min))
623 (while (search-forward "\^_" nil t)
624 (replace-match "?" t t)))
626 (defun nnbabyl-check-mbox ()
627 "Go through the nnbabyl mbox and make sure that no article numbers are reused."
629 (let ((idents (make-vector 1000 0))
632 (when (or (not nnbabyl-mbox-buffer)
633 (not (buffer-name nnbabyl-mbox-buffer)))
635 (set-buffer nnbabyl-mbox-buffer)
636 (goto-char (point-min))
637 (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) " nil t)
638 (if (intern-soft (setq id (match-string 1)) idents)
640 (delete-region (progn (beginning-of-line) (point))
641 (progn (forward-line 1) (point)))
642 (nnheader-message 7 "Moving %s..." id)
644 (nnmail-article-group 'nnbabyl-active-number)))
646 (when (buffer-modified-p (current-buffer))
648 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
649 (nnheader-message 5 ""))))
653 ;;; nnbabyl.el ends here