1 ;;; nnbabyl.el --- rmail mbox access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1099, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
10 ;; This file is part of GNU Emacs.
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)
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.
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.
29 ;; For an overview of what the interface functions do, please see the
34 (eval-when-compile (require 'cl))
40 5 "Ignore rmail errors from this file, you don't have rmail")))
44 (nnoo-declare nnbabyl)
46 (defvoo nnbabyl-mbox-file (expand-file-name "~/RMAIL")
47 "The name of the rmail box file in the users home directory.
49 This variable is a virtual server slot. See the Gnus manual for details.")
51 (defvoo nnbabyl-active-file (expand-file-name "~/.rmail-active")
52 "The name of the active file for the rmail box.
54 This variable is a virtual server slot. See the Gnus manual for details.")
56 (defvoo nnbabyl-get-new-mail t
57 "If non-nil, nnbabyl will check the incoming mail file and split the mail.
59 This variable is a virtual server slot. See the Gnus manual for details.")
62 (defvoo nnbabyl-prepare-save-mail-hook nil
63 "Hook run narrowed to an article before saving.
65 This variable is a virtual server slot. See the Gnus manual for details.")
69 (defvar nnbabyl-mail-delimiter "\^_")
71 (defconst nnbabyl-version "nnbabyl 1.0"
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)
80 (defvoo nnbabyl-previous-buffer-mode nil)
83 (autoload 'gnus-set-text-properties "gnus-ems"))
87 ;;; Interface functions
89 (nnoo-define-basics nnbabyl)
91 (deffoo nnbabyl-retrieve-headers (articles &optional group server fetch-old)
93 (set-buffer nntp-server-buffer)
95 (let ((number (length articles))
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)
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))))
111 (search-forward "\n\n" nil t)
112 (setq stop (1- (point)))
113 (set-buffer nntp-server-buffer)
115 (princ article (current-buffer))
116 (insert " Article retrieved.\n")
117 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
118 (goto-char (point-max))
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))))
126 (and (numberp nnmail-large-newsgroup)
127 (> number nnmail-large-newsgroup)
128 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
130 (set-buffer nntp-server-buffer)
131 (nnheader-fold-continuation-lines)
134 (deffoo nnbabyl-open-server (server &optional defs)
135 (nnoo-change-server 'nnbabyl server defs)
136 (nnbabyl-create-mbox)
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))
145 (nnheader-report 'nnbabyl "Opened server %s using mbox %s" server
149 (deffoo nnbabyl-close-server (&optional server)
150 ;; Restore buffer mode.
151 (when (and (nnbabyl-server-opened)
152 nnbabyl-previous-buffer-mode)
154 (set-buffer nnbabyl-mbox-buffer)
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)
163 (deffoo nnbabyl-server-opened (&optional server)
164 (and (nnoo-current-server-p 'nnbabyl server)
166 (buffer-name nnbabyl-mbox-buffer)
168 (buffer-name nntp-server-buffer)))
170 (deffoo nnbabyl-request-article (article &optional newsgroup server buffer)
171 (nnbabyl-possibly-change-newsgroup newsgroup server)
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))
180 (while (and (not (looking-at ".+:"))
181 (zerop (forward-line 1))))
183 (or (when (re-search-forward
184 (concat "^" nnbabyl-mail-delimiter) nil t)
187 (goto-char (point-max)))
189 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
190 (set-buffer nntp-server-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)
199 ;; The headers to be deleted are located before the
201 (delete-region (point-min) (progn (forward-line 1)
204 (delete-region (progn (beginning-of-line) (point))
205 (or (search-forward "\n\n" nil t)
207 (if (numberp article)
208 (cons nnbabyl-current-group article)
209 (nnbabyl-article-group-number)))))))
211 (deffoo nnbabyl-request-group (group &optional server dont-check)
212 (let ((active (cadr (assoc group nnbabyl-group-alist))))
216 (null (nnbabyl-possibly-change-newsgroup group server)))
217 (nnheader-report 'nnbabyl "No such group: %s" group))
219 (nnheader-report 'nnbabyl "Selected group %s" group)
220 (nnheader-insert ""))
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))))))
227 (deffoo nnbabyl-request-scan (&optional group server)
228 (nnbabyl-possibly-change-newsgroup group server)
234 (set-buffer nnbabyl-mbox-buffer)
236 (file-name-directory nnbabyl-mbox-file)
240 (let ((in-buf (current-buffer)))
241 (goto-char (point-min))
242 (while (search-forward "\n\^_\n" nil t)
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))))
251 (deffoo nnbabyl-close-group (group &optional server)
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))
259 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
262 (deffoo nnbabyl-request-list (&optional server)
264 (nnmail-find-file nnbabyl-active-file)
265 (setq nnbabyl-group-alist (nnmail-get-active))
268 (deffoo nnbabyl-request-newgroups (date &optional server)
269 (nnbabyl-request-list server))
271 (deffoo nnbabyl-request-list-newsgroups (&optional server)
272 (nnheader-report 'nnbabyl "nnbabyl: LIST NEWSGROUPS is not implemented."))
274 (deffoo nnbabyl-request-expire-articles
275 (articles newsgroup &optional server force)
276 (nnbabyl-possibly-change-newsgroup newsgroup server)
279 (nnmail-activate 'nnbabyl)
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)
288 (nnmail-expired-article-p
291 (point) (progn (end-of-line) (point))) force))
293 (unless (eq nnmail-expiry-target 'delete)
295 (nnbabyl-request-article (car articles)
298 (let ((nnml-current-directory nil))
299 (nnmail-expiry-target-group
300 nnmail-expiry-target newsgroup))))
301 (nnheader-message 5 "Deleting article %d in %s..."
302 (car articles) newsgroup)
303 (nnbabyl-delete-mail))
304 (push (car articles) rest)))
305 (setq articles (cdr articles)))
307 ;; Find the lowest active article in this group.
308 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
309 (goto-char (point-min))
310 (while (and (not (search-forward
311 (nnbabyl-article-string (car active)) nil t))
312 (<= (car active) (cdr active)))
313 (setcar active (1+ (car active)))
314 (goto-char (point-min))))
315 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
316 (nconc rest articles))))
318 (deffoo nnbabyl-request-move-article
319 (article group server accept-form &optional last)
320 (let ((buf (get-buffer-create " *nnbabyl move*"))
323 (nnbabyl-request-article article group server)
326 (insert-buffer-substring nntp-server-buffer)
327 (goto-char (point-min))
328 (while (re-search-forward
330 (save-excursion (search-forward "\n\n" nil t) (point)) t)
331 (delete-region (progn (beginning-of-line) (point))
332 (progn (forward-line 1) (point))))
333 (setq result (eval accept-form))
334 (kill-buffer (current-buffer))
337 (nnbabyl-possibly-change-newsgroup group server)
338 (set-buffer nnbabyl-mbox-buffer)
339 (goto-char (point-min))
340 (if (search-forward (nnbabyl-article-string article) nil t)
341 (nnbabyl-delete-mail))
342 (and last (save-buffer))))
345 (deffoo nnbabyl-request-accept-article (group &optional server last)
346 (nnbabyl-possibly-change-newsgroup group server)
347 (nnmail-check-syntax)
348 (let ((buf (current-buffer))
351 (nnmail-activate 'nnbabyl)
353 (goto-char (point-min))
354 (search-forward "\n\n" nil t)
357 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
358 (delete-region (point) (progn (forward-line 1) (point)))))
359 (when nnmail-cache-accepted-message-ids
360 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
363 (list (cons group (nnbabyl-active-number group)))
364 (nnmail-article-group 'nnbabyl-active-number)))
365 (if (and (null result)
366 (yes-or-no-p "Moved to `junk' group; delete article? "))
368 (setq result (car (nnbabyl-save-mail result))))
369 (set-buffer nnbabyl-mbox-buffer)
370 (goto-char (point-max))
371 (search-backward "\n\^_")
372 (goto-char (match-end 0))
373 (insert-buffer-substring buf)
375 (when nnmail-cache-accepted-message-ids
376 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
378 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
381 (deffoo nnbabyl-request-replace-article (article group buffer)
382 (nnbabyl-possibly-change-newsgroup group)
384 (set-buffer nnbabyl-mbox-buffer)
385 (goto-char (point-min))
386 (if (not (search-forward (nnbabyl-article-string article) nil t))
388 (nnbabyl-delete-mail t t)
389 (insert-buffer-substring buffer)
393 (deffoo nnbabyl-request-delete-group (group &optional force server)
394 (nnbabyl-possibly-change-newsgroup group server)
395 ;; Delete all articles in GROUP.
397 () ; Don't delete the articles.
399 (set-buffer nnbabyl-mbox-buffer)
400 (goto-char (point-min))
401 ;; Delete all articles in this group.
402 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
404 (while (search-forward ident nil t)
406 (nnbabyl-delete-mail))
409 ;; Remove the group from all structures.
410 (setq nnbabyl-group-alist
411 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
412 nnbabyl-current-group nil)
413 ;; Save the active file.
414 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
417 (deffoo nnbabyl-request-rename-group (group new-name &optional server)
418 (nnbabyl-possibly-change-newsgroup group server)
420 (set-buffer nnbabyl-mbox-buffer)
421 (goto-char (point-min))
422 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
423 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
425 (while (search-forward ident nil t)
426 (replace-match new-ident t t)
430 (let ((entry (assoc group nnbabyl-group-alist)))
431 (and entry (setcar entry new-name))
432 (setq nnbabyl-current-group nil)
433 ;; Save the new group alist.
434 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
438 ;;; Internal functions.
440 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
441 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
443 (defun nnbabyl-delete-mail (&optional force leave-delim)
444 ;; Delete the current X-Gnus-Newsgroup line.
447 (progn (beginning-of-line) (point))
448 (progn (forward-line 1) (point))))
449 ;; Beginning of the article.
455 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
456 (goto-char (point-min))
458 (if leave-delim (progn (forward-line 1) (point))
459 (match-beginning 0)))
462 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
466 (goto-char (point-min))
467 ;; Only delete the article if no other groups owns it as well.
468 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
469 (delete-region (point-min) (point-max))))))
471 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server)
473 (not (nnbabyl-server-opened server)))
474 (nnbabyl-open-server server))
475 (when (or (not nnbabyl-mbox-buffer)
476 (not (buffer-name nnbabyl-mbox-buffer)))
477 (save-excursion (nnbabyl-read-mbox)))
478 (unless nnbabyl-group-alist
479 (nnmail-activate 'nnbabyl))
481 (if (assoc newsgroup nnbabyl-group-alist)
482 (setq nnbabyl-current-group newsgroup)
483 (nnheader-report 'nnbabyl "No such group in file"))
486 (defun nnbabyl-article-string (article)
487 (if (numberp article)
488 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
489 (int-to-string article) " ")
490 (concat "\nMessage-ID: " article)))
492 (defun nnbabyl-article-group-number ()
494 (goto-char (point-min))
495 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
497 (cons (buffer-substring (match-beginning 1) (match-end 1))
499 (buffer-substring (match-beginning 2) (match-end 2)))))))
501 (defun nnbabyl-insert-lines ()
502 "Insert how many lines and chars there are in the body of the mail."
505 (goto-char (point-min))
506 (when (search-forward "\n\n" nil t)
507 ;; There may be an EOOH line here...
508 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
509 (search-forward "\n\n" nil t))
510 (setq chars (- (point-max) (point))
511 lines (max (- (count-lines (point) (point-max)) 1) 0))
512 ;; Move back to the end of the headers.
513 (goto-char (point-min))
514 (search-forward "\n\n" nil t)
517 (when (re-search-backward "^Lines: " nil t)
518 (delete-region (point) (progn (forward-line 1) (point)))))
519 (insert (format "Lines: %d\n" lines))
522 (defun nnbabyl-save-mail (group-art)
523 ;; Called narrowed to an article.
524 (nnbabyl-insert-lines)
525 (nnmail-insert-xref group-art)
526 (nnbabyl-insert-newsgroup-line group-art)
527 (run-hooks 'nnbabyl-prepare-save-mail-hook)
530 (defun nnbabyl-insert-newsgroup-line (group-art)
532 (goto-char (point-min))
533 (while (looking-at "From ")
534 (replace-match "Mail-from: From " t t)
536 ;; If there is a C-l at the beginning of the narrowed region, this
537 ;; isn't really a "save", but rather a "scan".
538 (goto-char (point-min))
539 (unless (looking-at "\^L")
541 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
542 (goto-char (point-max))
544 (when (search-forward "\n\n" nil t)
547 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
548 (caar group-art) (cdar group-art)
549 (current-time-string)))
550 (setq group-art (cdr group-art))))
553 (defun nnbabyl-active-number (group)
554 ;; Find the next article number in GROUP.
555 (let ((active (cadr (assoc group nnbabyl-group-alist))))
557 (setcdr active (1+ (cdr active)))
558 ;; This group is new, so we create a new entry for it.
559 ;; This might be a bit naughty... creating groups on the drop of
560 ;; a hat, but I don't know...
561 (push (list group (setq active (cons 1 1)))
562 nnbabyl-group-alist))
565 (defun nnbabyl-create-mbox ()
566 (unless (file-exists-p nnbabyl-mbox-file)
567 ;; Create a new, empty RMAIL mbox file.
569 (set-buffer (setq nnbabyl-mbox-buffer
570 (create-file-buffer nnbabyl-mbox-file)))
571 (setq buffer-file-name nnbabyl-mbox-file)
572 (insert "BABYL OPTIONS:\n\n\^_")
574 (point-min) (point-max) nnbabyl-mbox-file t 'nomesg))))
576 (defun nnbabyl-read-mbox ()
577 (nnmail-activate 'nnbabyl)
578 (nnbabyl-create-mbox)
580 (unless (and nnbabyl-mbox-buffer
581 (buffer-name nnbabyl-mbox-buffer)
583 (set-buffer nnbabyl-mbox-buffer)
584 (= (buffer-size) (nnheader-file-size nnbabyl-mbox-file))))
585 ;; This buffer has changed since we read it last. Possibly.
587 (let ((delim (concat "^" nnbabyl-mail-delimiter))
588 (alist nnbabyl-group-alist)
590 (set-buffer (setq nnbabyl-mbox-buffer
591 (nnheader-find-file-noselect
592 nnbabyl-mbox-file nil t)))
593 ;; Save previous buffer mode.
594 (setq nnbabyl-previous-buffer-mode
595 (cons (cons (point-min) (point-max))
598 (buffer-disable-undo)
600 (setq buffer-read-only nil)
603 ;; Go through the group alist and compare against
606 (goto-char (point-max))
607 (when (and (re-search-backward
608 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
614 (match-beginning 1) (match-end 1))))
616 (setcdr (cadar alist) number))
617 (setq alist (cdr alist)))
619 ;; We go through the mbox and make sure that each and
620 ;; every mail belongs to some group or other.
621 (goto-char (point-min))
622 (if (looking-at "\^L")
624 (re-search-forward delim nil t)
625 (setq start (match-end 0)))
626 (while (re-search-forward delim nil t)
627 (setq end (match-end 0))
628 (unless (search-backward "\nX-Gnus-Newsgroup: " start t)
632 (narrow-to-region (goto-char start) end)
634 (nnmail-article-group 'nnbabyl-active-number))
635 (setq end (point-max)))))
636 (goto-char (setq start end)))
637 (when (buffer-modified-p (current-buffer))
639 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
641 (defun nnbabyl-remove-incoming-delims ()
642 (goto-char (point-min))
643 (while (search-forward "\^_" nil t)
644 (replace-match "?" t t)))
646 (defun nnbabyl-check-mbox ()
647 "Go through the nnbabyl mbox and make sure that no article numbers are reused."
649 (let ((idents (make-vector 1000 0))
652 (when (or (not nnbabyl-mbox-buffer)
653 (not (buffer-name nnbabyl-mbox-buffer)))
655 (set-buffer nnbabyl-mbox-buffer)
656 (goto-char (point-min))
657 (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) " nil t)
658 (if (intern-soft (setq id (match-string 1)) idents)
660 (delete-region (progn (beginning-of-line) (point))
661 (progn (forward-line 1) (point)))
662 (nnheader-message 7 "Moving %s..." id)
664 (nnmail-article-group 'nnbabyl-active-number)))
666 (when (buffer-modified-p (current-buffer))
668 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
669 (nnheader-message 5 ""))))
673 ;;; nnbabyl.el ends here