1 ;;; nnbabyl.el --- rmail mbox access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1099, 2000, 2001
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 (defvoo nnbabyl-active-file (expand-file-name "~/.rmail-active")
50 "The name of the active file for the rmail box.")
52 (defvoo nnbabyl-get-new-mail t
53 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
56 (defvoo nnbabyl-prepare-save-mail-hook nil
57 "Hook run narrowed to an article before saving.")
61 (defvar nnbabyl-mail-delimiter "\^_")
63 (defconst nnbabyl-version "nnbabyl 1.0"
66 (defvoo nnbabyl-mbox-buffer nil)
67 (defvoo nnbabyl-current-group nil)
68 (defvoo nnbabyl-status-string "")
69 (defvoo nnbabyl-group-alist nil)
70 (defvoo nnbabyl-active-timestamp nil)
72 (defvoo nnbabyl-previous-buffer-mode nil)
75 (autoload 'gnus-set-text-properties "gnus-ems"))
79 ;;; Interface functions
81 (nnoo-define-basics nnbabyl)
83 (deffoo nnbabyl-retrieve-headers (articles &optional group server fetch-old)
85 (set-buffer nntp-server-buffer)
87 (let ((number (length articles))
89 (delim (concat "^" nnbabyl-mail-delimiter))
90 article art-string start stop)
91 (nnbabyl-possibly-change-newsgroup group server)
92 (while (setq article (pop articles))
93 (setq art-string (nnbabyl-article-string article))
94 (set-buffer nnbabyl-mbox-buffer)
96 (when (or (search-forward art-string nil t)
97 (search-backward art-string nil t))
98 (unless (re-search-backward delim nil t)
99 (goto-char (point-min)))
100 (while (and (not (looking-at ".+:"))
101 (zerop (forward-line 1))))
103 (search-forward "\n\n" nil t)
104 (setq stop (1- (point)))
105 (set-buffer nntp-server-buffer)
107 (princ article (current-buffer))
108 (insert " Article retrieved.\n")
109 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
110 (goto-char (point-max))
112 (and (numberp nnmail-large-newsgroup)
113 (> number nnmail-large-newsgroup)
114 (zerop (% (incf count) 20))
115 (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
116 (/ (* count 100) number))))
118 (and (numberp nnmail-large-newsgroup)
119 (> number nnmail-large-newsgroup)
120 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
122 (set-buffer nntp-server-buffer)
123 (nnheader-fold-continuation-lines)
126 (deffoo nnbabyl-open-server (server &optional defs)
127 (nnoo-change-server 'nnbabyl server defs)
128 (nnbabyl-create-mbox)
130 ((not (file-exists-p nnbabyl-mbox-file))
131 (nnbabyl-close-server)
132 (nnheader-report 'nnbabyl "No such file: %s" nnbabyl-mbox-file))
133 ((file-directory-p nnbabyl-mbox-file)
134 (nnbabyl-close-server)
135 (nnheader-report 'nnbabyl "Not a regular file: %s" nnbabyl-mbox-file))
137 (nnheader-report 'nnbabyl "Opened server %s using mbox %s" server
141 (deffoo nnbabyl-close-server (&optional server)
142 ;; Restore buffer mode.
143 (when (and (nnbabyl-server-opened)
144 nnbabyl-previous-buffer-mode)
146 (set-buffer nnbabyl-mbox-buffer)
148 (caar nnbabyl-previous-buffer-mode)
149 (cdar nnbabyl-previous-buffer-mode))
150 (funcall (cdr nnbabyl-previous-buffer-mode))))
151 (nnoo-close-server 'nnbabyl server)
152 (setq nnbabyl-mbox-buffer nil)
155 (deffoo nnbabyl-server-opened (&optional server)
156 (and (nnoo-current-server-p 'nnbabyl server)
158 (buffer-name nnbabyl-mbox-buffer)
160 (buffer-name nntp-server-buffer)))
162 (deffoo nnbabyl-request-article (article &optional newsgroup server buffer)
163 (nnbabyl-possibly-change-newsgroup newsgroup server)
165 (set-buffer nnbabyl-mbox-buffer)
166 (goto-char (point-min))
167 (when (search-forward (nnbabyl-article-string article) nil t)
168 (let (start stop summary-line)
169 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
170 (goto-char (point-min))
172 (while (and (not (looking-at ".+:"))
173 (zerop (forward-line 1))))
175 (or (when (re-search-forward
176 (concat "^" nnbabyl-mail-delimiter) nil t)
179 (goto-char (point-max)))
181 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
182 (set-buffer nntp-server-buffer)
184 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
185 (goto-char (point-min))
186 ;; If there is an EOOH header, then we have to remove some
187 ;; duplicated headers.
188 (setq summary-line (looking-at "Summary-line:"))
189 (when (search-forward "\n*** EOOH ***" nil t)
191 ;; The headers to be deleted are located before the
193 (delete-region (point-min) (progn (forward-line 1)
196 (delete-region (progn (beginning-of-line) (point))
197 (or (search-forward "\n\n" nil t)
199 (if (numberp article)
200 (cons nnbabyl-current-group article)
201 (nnbabyl-article-group-number)))))))
203 (deffoo nnbabyl-request-group (group &optional server dont-check)
204 (let ((active (cadr (assoc group nnbabyl-group-alist))))
208 (null (nnbabyl-possibly-change-newsgroup group server)))
209 (nnheader-report 'nnbabyl "No such group: %s" group))
211 (nnheader-report 'nnbabyl "Selected group %s" group)
212 (nnheader-insert ""))
214 (nnheader-report 'nnbabyl "Selected group %s" group)
215 (nnheader-insert "211 %d %d %d %s\n"
216 (1+ (- (cdr active) (car active)))
217 (car active) (cdr active) group))))))
219 (deffoo nnbabyl-request-scan (&optional group server)
220 (nnbabyl-possibly-change-newsgroup group server)
226 (set-buffer nnbabyl-mbox-buffer)
228 (file-name-directory nnbabyl-mbox-file)
232 (let ((in-buf (current-buffer)))
233 (goto-char (point-min))
234 (while (search-forward "\n\^_\n" nil t)
236 (set-buffer nnbabyl-mbox-buffer)
237 (goto-char (point-max))
238 (search-backward "\n\^_" nil t)
239 (goto-char (match-end 0))
240 (insert-buffer-substring in-buf)))
241 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))))
243 (deffoo nnbabyl-close-group (group &optional server)
246 (deffoo nnbabyl-request-create-group (group &optional server args)
247 (nnmail-activate 'nnbabyl)
248 (unless (assoc group nnbabyl-group-alist)
249 (push (list group (cons 1 0))
251 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
254 (deffoo nnbabyl-request-list (&optional server)
256 (nnmail-find-file nnbabyl-active-file)
257 (setq nnbabyl-group-alist (nnmail-get-active))
260 (deffoo nnbabyl-request-newgroups (date &optional server)
261 (nnbabyl-request-list server))
263 (deffoo nnbabyl-request-list-newsgroups (&optional server)
264 (nnheader-report 'nnbabyl "nnbabyl: LIST NEWSGROUPS is not implemented."))
266 (deffoo nnbabyl-request-expire-articles
267 (articles newsgroup &optional server force)
268 (nnbabyl-possibly-change-newsgroup newsgroup server)
271 (nnmail-activate 'nnbabyl)
274 (set-buffer nnbabyl-mbox-buffer)
275 (gnus-set-text-properties (point-min) (point-max) nil)
276 (while (and articles is-old)
277 (goto-char (point-min))
278 (when (search-forward (nnbabyl-article-string (car articles)) nil t)
280 (nnmail-expired-article-p
283 (point) (progn (end-of-line) (point))) force))
285 (unless (eq nnmail-expiry-target 'delete)
287 (nnbabyl-request-article (car articles)
290 (let ((nnml-current-directory nil))
291 (nnmail-expiry-target-group
292 nnmail-expiry-target newsgroup)))
293 (nnbabyl-possibly-change-newsgroup newsgroup server))
294 (nnheader-message 5 "Deleting article %d in %s..."
295 (car articles) newsgroup)
296 (nnbabyl-delete-mail))
297 (push (car articles) rest)))
298 (setq articles (cdr articles)))
300 ;; Find the lowest active article in this group.
301 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
302 (goto-char (point-min))
303 (while (and (not (search-forward
304 (nnbabyl-article-string (car active)) nil t))
305 (<= (car active) (cdr active)))
306 (setcar active (1+ (car active)))
307 (goto-char (point-min))))
308 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
309 (nconc rest articles))))
311 (deffoo nnbabyl-request-move-article
312 (article group server accept-form &optional last)
313 (let ((buf (get-buffer-create " *nnbabyl move*"))
316 (nnbabyl-request-article article group server)
319 (insert-buffer-substring nntp-server-buffer)
320 (goto-char (point-min))
321 (while (re-search-forward
323 (save-excursion (search-forward "\n\n" nil t) (point)) t)
324 (delete-region (progn (beginning-of-line) (point))
325 (progn (forward-line 1) (point))))
326 (setq result (eval accept-form))
327 (kill-buffer (current-buffer))
330 (nnbabyl-possibly-change-newsgroup group server)
331 (set-buffer nnbabyl-mbox-buffer)
332 (goto-char (point-min))
333 (if (search-forward (nnbabyl-article-string article) nil t)
334 (nnbabyl-delete-mail))
335 (and last (save-buffer))))
338 (deffoo nnbabyl-request-accept-article (group &optional server last)
339 (nnbabyl-possibly-change-newsgroup group server)
340 (nnmail-check-syntax)
341 (let ((buf (current-buffer))
344 (nnmail-activate 'nnbabyl)
346 (goto-char (point-min))
347 (search-forward "\n\n" nil t)
350 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
351 (delete-region (point) (progn (forward-line 1) (point)))))
352 (when nnmail-cache-accepted-message-ids
353 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
356 (list (cons group (nnbabyl-active-number group)))
357 (nnmail-article-group 'nnbabyl-active-number)))
358 (if (and (null result)
359 (yes-or-no-p "Moved to `junk' group; delete article? "))
361 (setq result (car (nnbabyl-save-mail result))))
362 (set-buffer nnbabyl-mbox-buffer)
363 (goto-char (point-max))
364 (search-backward "\n\^_")
365 (goto-char (match-end 0))
366 (insert-buffer-substring buf)
368 (when nnmail-cache-accepted-message-ids
369 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
371 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
374 (deffoo nnbabyl-request-replace-article (article group buffer)
375 (nnbabyl-possibly-change-newsgroup group)
377 (set-buffer nnbabyl-mbox-buffer)
378 (goto-char (point-min))
379 (if (not (search-forward (nnbabyl-article-string article) nil t))
381 (nnbabyl-delete-mail t t)
382 (insert-buffer-substring buffer)
386 (deffoo nnbabyl-request-delete-group (group &optional force server)
387 (nnbabyl-possibly-change-newsgroup group server)
388 ;; Delete all articles in GROUP.
390 () ; Don't delete the articles.
392 (set-buffer nnbabyl-mbox-buffer)
393 (goto-char (point-min))
394 ;; Delete all articles in this group.
395 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
397 (while (search-forward ident nil t)
399 (nnbabyl-delete-mail))
402 ;; Remove the group from all structures.
403 (setq nnbabyl-group-alist
404 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
405 nnbabyl-current-group nil)
406 ;; Save the active file.
407 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
410 (deffoo nnbabyl-request-rename-group (group new-name &optional server)
411 (nnbabyl-possibly-change-newsgroup group server)
413 (set-buffer nnbabyl-mbox-buffer)
414 (goto-char (point-min))
415 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
416 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
418 (while (search-forward ident nil t)
419 (replace-match new-ident t t)
423 (let ((entry (assoc group nnbabyl-group-alist)))
424 (and entry (setcar entry new-name))
425 (setq nnbabyl-current-group nil)
426 ;; Save the new group alist.
427 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
431 ;;; Internal functions.
433 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
434 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
436 (defun nnbabyl-delete-mail (&optional force leave-delim)
437 ;; Delete the current X-Gnus-Newsgroup line.
440 (progn (beginning-of-line) (point))
441 (progn (forward-line 1) (point))))
442 ;; Beginning of the article.
448 (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
449 (goto-char (point-min))
451 (if leave-delim (progn (forward-line 1) (point))
452 (match-beginning 0)))
455 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
459 (goto-char (point-min))
460 ;; Only delete the article if no other groups owns it as well.
461 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
462 (delete-region (point-min) (point-max))))))
464 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server)
466 (not (nnbabyl-server-opened server)))
467 (nnbabyl-open-server server))
468 (when (or (not nnbabyl-mbox-buffer)
469 (not (buffer-name nnbabyl-mbox-buffer)))
470 (save-excursion (nnbabyl-read-mbox)))
471 (unless nnbabyl-group-alist
472 (nnmail-activate 'nnbabyl))
474 (if (assoc newsgroup nnbabyl-group-alist)
475 (setq nnbabyl-current-group newsgroup)
476 (nnheader-report 'nnbabyl "No such group in file"))
479 (defun nnbabyl-article-string (article)
480 (if (numberp article)
481 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
482 (int-to-string article) " ")
483 (concat "\nMessage-ID: " article)))
485 (defun nnbabyl-article-group-number ()
487 (goto-char (point-min))
488 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
490 (cons (buffer-substring (match-beginning 1) (match-end 1))
492 (buffer-substring (match-beginning 2) (match-end 2)))))))
494 (defun nnbabyl-insert-lines ()
495 "Insert how many lines and chars there are in the body of the mail."
498 (goto-char (point-min))
499 (when (search-forward "\n\n" nil t)
500 ;; There may be an EOOH line here...
501 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
502 (search-forward "\n\n" nil t))
503 (setq chars (- (point-max) (point))
504 lines (max (- (count-lines (point) (point-max)) 1) 0))
505 ;; Move back to the end of the headers.
506 (goto-char (point-min))
507 (search-forward "\n\n" nil t)
510 (when (re-search-backward "^Lines: " nil t)
511 (delete-region (point) (progn (forward-line 1) (point)))))
512 (insert (format "Lines: %d\n" lines))
515 (defun nnbabyl-save-mail (group-art)
516 ;; Called narrowed to an article.
517 (nnbabyl-insert-lines)
518 (nnmail-insert-xref group-art)
519 (nnbabyl-insert-newsgroup-line group-art)
520 (run-hooks 'nnbabyl-prepare-save-mail-hook)
523 (defun nnbabyl-insert-newsgroup-line (group-art)
525 (goto-char (point-min))
526 (while (looking-at "From ")
527 (replace-match "Mail-from: From " t t)
529 ;; If there is a C-l at the beginning of the narrowed region, this
530 ;; isn't really a "save", but rather a "scan".
531 (goto-char (point-min))
532 (unless (looking-at "\^L")
534 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
535 (goto-char (point-max))
537 (when (search-forward "\n\n" nil t)
540 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
541 (caar group-art) (cdar group-art)
542 (current-time-string)))
543 (setq group-art (cdr group-art))))
546 (defun nnbabyl-active-number (group)
547 ;; Find the next article number in GROUP.
548 (let ((active (cadr (assoc group nnbabyl-group-alist))))
550 (setcdr active (1+ (cdr active)))
551 ;; This group is new, so we create a new entry for it.
552 ;; This might be a bit naughty... creating groups on the drop of
553 ;; a hat, but I don't know...
554 (push (list group (setq active (cons 1 1)))
555 nnbabyl-group-alist))
558 (defun nnbabyl-create-mbox ()
559 (unless (file-exists-p nnbabyl-mbox-file)
560 ;; Create a new, empty RMAIL mbox file.
562 (set-buffer (setq nnbabyl-mbox-buffer
563 (create-file-buffer nnbabyl-mbox-file)))
564 (setq buffer-file-name nnbabyl-mbox-file)
565 (insert "BABYL OPTIONS:\n\n\^_")
567 (point-min) (point-max) nnbabyl-mbox-file t 'nomesg))))
569 (defun nnbabyl-read-mbox ()
570 (nnmail-activate 'nnbabyl)
571 (nnbabyl-create-mbox)
573 (unless (and nnbabyl-mbox-buffer
574 (buffer-name nnbabyl-mbox-buffer)
576 (set-buffer nnbabyl-mbox-buffer)
577 (= (buffer-size) (nnheader-file-size nnbabyl-mbox-file))))
578 ;; This buffer has changed since we read it last. Possibly.
580 (let ((delim (concat "^" nnbabyl-mail-delimiter))
581 (alist nnbabyl-group-alist)
583 (set-buffer (setq nnbabyl-mbox-buffer
584 (nnheader-find-file-noselect
585 nnbabyl-mbox-file nil t)))
586 ;; Save previous buffer mode.
587 (setq nnbabyl-previous-buffer-mode
588 (cons (cons (point-min) (point-max))
591 (buffer-disable-undo)
593 (setq buffer-read-only nil)
596 ;; Go through the group alist and compare against
599 (goto-char (point-max))
600 (when (and (re-search-backward
601 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
607 (match-beginning 1) (match-end 1))))
609 (setcdr (cadar alist) number))
610 (setq alist (cdr alist)))
612 ;; We go through the mbox and make sure that each and
613 ;; every mail belongs to some group or other.
614 (goto-char (point-min))
615 (if (looking-at "\^L")
617 (re-search-forward delim nil t)
618 (setq start (match-end 0)))
619 (while (re-search-forward delim nil t)
620 (setq end (match-end 0))
621 (unless (search-backward "\nX-Gnus-Newsgroup: " start t)
625 (narrow-to-region (goto-char start) end)
627 (nnmail-article-group 'nnbabyl-active-number))
628 (setq end (point-max)))))
629 (goto-char (setq start end)))
630 (when (buffer-modified-p (current-buffer))
632 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
634 (defun nnbabyl-remove-incoming-delims ()
635 (goto-char (point-min))
636 (while (search-forward "\^_" nil t)
637 (replace-match "?" t t)))
639 (defun nnbabyl-check-mbox ()
640 "Go through the nnbabyl mbox and make sure that no article numbers are reused."
642 (let ((idents (make-vector 1000 0))
645 (when (or (not nnbabyl-mbox-buffer)
646 (not (buffer-name nnbabyl-mbox-buffer)))
648 (set-buffer nnbabyl-mbox-buffer)
649 (goto-char (point-min))
650 (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) " nil t)
651 (if (intern-soft (setq id (match-string 1)) idents)
653 (delete-region (progn (beginning-of-line) (point))
654 (progn (forward-line 1) (point)))
655 (nnheader-message 7 "Moving %s..." id)
657 (nnmail-article-group 'nnbabyl-active-number)))
659 (when (buffer-modified-p (current-buffer))
661 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
662 (nnheader-message 5 ""))))
666 ;;; nnbabyl.el ends here