1 ;;; nnbabyl.el --- rmail mbox access for Gnus
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
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 (eval-when-compile (require 'cl))
38 (nnoo-declare nnbabyl)
40 (defvoo nnbabyl-mbox-file (expand-file-name "~/RMAIL")
41 "The name of the rmail box file in the users home directory.")
43 (defvoo nnbabyl-active-file (expand-file-name "~/.rmail-active")
44 "The name of the active file for the rmail box.")
46 (defvoo nnbabyl-get-new-mail t
47 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
49 (defvoo nnbabyl-prepare-save-mail-hook nil
50 "Hook run narrowed to an article before saving.")
54 (defvar nnbabyl-mail-delimiter "\^_")
56 (defconst nnbabyl-version "nnbabyl 1.0"
59 (defvoo nnbabyl-mbox-buffer nil)
60 (defvoo nnbabyl-current-group nil)
61 (defvoo nnbabyl-status-string "")
62 (defvoo nnbabyl-group-alist nil)
63 (defvoo nnbabyl-active-timestamp nil)
65 (defvoo nnbabyl-previous-buffer-mode nil)
68 (autoload 'gnus-set-text-properties "gnus-ems"))
72 ;;; Interface functions
74 (nnoo-define-basics nnbabyl)
76 (deffoo nnbabyl-retrieve-headers (articles &optional group server fetch-old)
78 (set-buffer nntp-server-buffer)
80 (let ((number (length articles))
82 (delim (concat "^" nnbabyl-mail-delimiter))
83 article art-string start stop)
84 (nnbabyl-possibly-change-newsgroup group server)
85 (while (setq article (pop articles))
86 (setq art-string (nnbabyl-article-string article))
87 (set-buffer nnbabyl-mbox-buffer)
89 (when (or (search-forward art-string nil t)
90 (search-backward art-string nil t))
91 (re-search-backward delim nil t)
92 (while (and (not (looking-at ".+:"))
93 (zerop (forward-line 1))))
95 (search-forward "\n\n" nil t)
96 (setq stop (1- (point)))
97 (set-buffer nntp-server-buffer)
99 (princ article (current-buffer))
100 (insert " Article retrieved.\n")
101 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
102 (goto-char (point-max))
104 (and (numberp nnmail-large-newsgroup)
105 (> number nnmail-large-newsgroup)
106 (zerop (% (incf count) 20))
107 (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
108 (/ (* count 100) number))))
110 (and (numberp nnmail-large-newsgroup)
111 (> number nnmail-large-newsgroup)
112 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
114 (set-buffer nntp-server-buffer)
115 (nnheader-fold-continuation-lines)
118 (deffoo nnbabyl-open-server (server &optional defs)
119 (nnoo-change-server 'nnbabyl server defs)
120 (nnbabyl-create-mbox)
122 ((not (file-exists-p nnbabyl-mbox-file))
123 (nnbabyl-close-server)
124 (nnheader-report 'nnbabyl "No such file: %s" nnbabyl-mbox-file))
125 ((file-directory-p nnbabyl-mbox-file)
126 (nnbabyl-close-server)
127 (nnheader-report 'nnbabyl "Not a regular file: %s" nnbabyl-mbox-file))
129 (nnheader-report 'nnbabyl "Opened server %s using mbox %s" server
133 (deffoo nnbabyl-close-server (&optional server)
134 ;; Restore buffer mode.
135 (when (and (nnbabyl-server-opened)
136 nnbabyl-previous-buffer-mode)
138 (set-buffer nnbabyl-mbox-buffer)
140 (caar nnbabyl-previous-buffer-mode)
141 (cdar nnbabyl-previous-buffer-mode))
142 (funcall (cdr nnbabyl-previous-buffer-mode))))
143 (nnoo-close-server 'nnbabyl server)
144 (setq nnbabyl-mbox-buffer nil)
147 (deffoo nnbabyl-server-opened (&optional server)
148 (and (nnoo-current-server-p 'nnbabyl server)
150 (buffer-name nnbabyl-mbox-buffer)
152 (buffer-name nntp-server-buffer)))
154 (deffoo nnbabyl-request-article (article &optional newsgroup server buffer)
155 (nnbabyl-possibly-change-newsgroup newsgroup server)
157 (set-buffer nnbabyl-mbox-buffer)
158 (goto-char (point-min))
159 (when (search-forward (nnbabyl-article-string article) nil t)
160 (let (start stop summary-line)
161 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
162 (while (and (not (looking-at ".+:"))
163 (zerop (forward-line 1))))
165 (or (when (re-search-forward
166 (concat "^" nnbabyl-mail-delimiter) nil t)
169 (goto-char (point-max)))
171 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
172 (set-buffer nntp-server-buffer)
174 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
175 (goto-char (point-min))
176 ;; If there is an EOOH header, then we have to remove some
177 ;; duplicated headers.
178 (setq summary-line (looking-at "Summary-line:"))
179 (when (search-forward "\n*** EOOH ***" nil t)
181 ;; The headers to be deleted are located before the
183 (delete-region (point-min) (progn (forward-line 1)
186 (delete-region (progn (beginning-of-line) (point))
187 (or (search-forward "\n\n" nil t)
189 (if (numberp article)
190 (cons nnbabyl-current-group article)
191 (nnbabyl-article-group-number)))))))
193 (deffoo nnbabyl-request-group (group &optional server dont-check)
194 (let ((active (cadr (assoc group nnbabyl-group-alist))))
198 (null (nnbabyl-possibly-change-newsgroup group server)))
199 (nnheader-report 'nnbabyl "No such group: %s" group))
201 (nnheader-report 'nnbabyl "Selected group %s" group)
202 (nnheader-insert ""))
204 (nnheader-report 'nnbabyl "Selected group %s" group)
205 (nnheader-insert "211 %d %d %d %s\n"
206 (1+ (- (cdr active) (car active)))
207 (car active) (cdr active) group))))))
209 (deffoo nnbabyl-request-scan (&optional group server)
210 (nnbabyl-possibly-change-newsgroup group server)
216 (set-buffer nnbabyl-mbox-buffer)
218 nnbabyl-mbox-file group
221 (let ((in-buf (current-buffer)))
222 (goto-char (point-min))
223 (while (search-forward "\n\^_\n" nil t)
225 (set-buffer nnbabyl-mbox-buffer)
226 (goto-char (point-max))
227 (search-backward "\n\^_" nil t)
228 (goto-char (match-end 0))
229 (insert-buffer-substring in-buf)))
230 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))))
232 (deffoo nnbabyl-close-group (group &optional server)
235 (deffoo nnbabyl-request-create-group (group &optional server args)
236 (nnmail-activate 'nnbabyl)
237 (unless (assoc group nnbabyl-group-alist)
238 (push (list group (cons 1 0))
240 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
243 (deffoo nnbabyl-request-list (&optional server)
245 (nnmail-find-file nnbabyl-active-file)
246 (setq nnbabyl-group-alist (nnmail-get-active))
249 (deffoo nnbabyl-request-newgroups (date &optional server)
250 (nnbabyl-request-list server))
252 (deffoo nnbabyl-request-list-newsgroups (&optional server)
253 (nnheader-report 'nnbabyl "nnbabyl: LIST NEWSGROUPS is not implemented."))
255 (deffoo nnbabyl-request-expire-articles
256 (articles newsgroup &optional server force)
257 (nnbabyl-possibly-change-newsgroup newsgroup server)
260 (nnmail-activate 'nnbabyl)
263 (set-buffer nnbabyl-mbox-buffer)
264 (gnus-set-text-properties (point-min) (point-max) nil)
265 (while (and articles is-old)
266 (goto-char (point-min))
267 (when (search-forward (nnbabyl-article-string (car articles)) nil t)
269 (nnmail-expired-article-p
272 (point) (progn (end-of-line) (point))) force))
274 (nnheader-message 5 "Deleting article %d in %s..."
275 (car articles) newsgroup)
276 (nnbabyl-delete-mail))
277 (push (car articles) rest)))
278 (setq articles (cdr articles)))
280 ;; Find the lowest active article in this group.
281 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
282 (goto-char (point-min))
283 (while (and (not (search-forward
284 (nnbabyl-article-string (car active)) nil t))
285 (<= (car active) (cdr active)))
286 (setcar active (1+ (car active)))
287 (goto-char (point-min))))
288 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
289 (nconc rest articles))))
291 (deffoo nnbabyl-request-move-article
292 (article group server accept-form &optional last)
293 (let ((buf (get-buffer-create " *nnbabyl move*"))
296 (nnbabyl-request-article article group server)
299 (insert-buffer-substring nntp-server-buffer)
300 (goto-char (point-min))
301 (while (re-search-forward
303 (save-excursion (search-forward "\n\n" nil t) (point)) t)
304 (delete-region (progn (beginning-of-line) (point))
305 (progn (forward-line 1) (point))))
306 (setq result (eval accept-form))
307 (kill-buffer (current-buffer))
310 (nnbabyl-possibly-change-newsgroup group server)
311 (set-buffer nnbabyl-mbox-buffer)
312 (goto-char (point-min))
313 (if (search-forward (nnbabyl-article-string article) nil t)
314 (nnbabyl-delete-mail))
315 (and last (save-buffer))))
318 (deffoo nnbabyl-request-accept-article (group &optional server last)
319 (nnbabyl-possibly-change-newsgroup group server)
320 (nnmail-check-syntax)
321 (let ((buf (current-buffer))
324 (nnmail-activate 'nnbabyl)
326 (goto-char (point-min))
327 (search-forward "\n\n" nil t)
330 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
331 (delete-region (point) (progn (forward-line 1) (point)))))
332 (setq result (car (nnbabyl-save-mail
334 (list (cons group (nnbabyl-active-number group)))
335 (nnmail-article-group 'nnbabyl-active-number)))))
336 (set-buffer nnbabyl-mbox-buffer)
337 (goto-char (point-max))
338 (search-backward "\n\^_")
339 (goto-char (match-end 0))
340 (insert-buffer-substring buf)
343 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
346 (deffoo nnbabyl-request-replace-article (article group buffer)
347 (nnbabyl-possibly-change-newsgroup group)
349 (set-buffer nnbabyl-mbox-buffer)
350 (goto-char (point-min))
351 (if (not (search-forward (nnbabyl-article-string article) nil t))
353 (nnbabyl-delete-mail t t)
354 (insert-buffer-substring buffer)
358 (deffoo nnbabyl-request-delete-group (group &optional force server)
359 (nnbabyl-possibly-change-newsgroup group server)
360 ;; Delete all articles in GROUP.
362 () ; Don't delete the articles.
364 (set-buffer nnbabyl-mbox-buffer)
365 (goto-char (point-min))
366 ;; Delete all articles in this group.
367 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
369 (while (search-forward ident nil t)
371 (nnbabyl-delete-mail))
374 ;; Remove the group from all structures.
375 (setq nnbabyl-group-alist
376 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
377 nnbabyl-current-group nil)
378 ;; Save the active file.
379 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
382 (deffoo nnbabyl-request-rename-group (group new-name &optional server)
383 (nnbabyl-possibly-change-newsgroup group server)
385 (set-buffer nnbabyl-mbox-buffer)
386 (goto-char (point-min))
387 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
388 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
390 (while (search-forward ident nil t)
391 (replace-match new-ident t t)
395 (let ((entry (assoc group nnbabyl-group-alist)))
396 (and entry (setcar entry new-name))
397 (setq nnbabyl-current-group nil)
398 ;; Save the new group alist.
399 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
403 ;;; Internal functions.
405 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
406 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
408 (defun nnbabyl-delete-mail (&optional force leave-delim)
409 ;; Delete the current X-Gnus-Newsgroup line.
412 (progn (beginning-of-line) (point))
413 (progn (forward-line 1) (point))))
414 ;; Beginning of the article.
420 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
421 (if leave-delim (progn (forward-line 1) (point))
422 (match-beginning 0)))
425 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
429 (goto-char (point-min))
430 ;; Only delete the article if no other groups owns it as well.
431 (when (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
432 (delete-region (point-min) (point-max))))))
434 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server)
436 (not (nnbabyl-server-opened server)))
437 (nnbabyl-open-server server))
438 (when (or (not nnbabyl-mbox-buffer)
439 (not (buffer-name nnbabyl-mbox-buffer)))
440 (save-excursion (nnbabyl-read-mbox)))
441 (unless nnbabyl-group-alist
442 (nnmail-activate 'nnbabyl))
444 (if (assoc newsgroup nnbabyl-group-alist)
445 (setq nnbabyl-current-group newsgroup)
446 (nnheader-report 'nnbabyl "No such group in file"))
449 (defun nnbabyl-article-string (article)
450 (if (numberp article)
451 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
452 (int-to-string article) " ")
453 (concat "\nMessage-ID: " article)))
455 (defun nnbabyl-article-group-number ()
457 (goto-char (point-min))
458 (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
460 (cons (buffer-substring (match-beginning 1) (match-end 1))
462 (buffer-substring (match-beginning 2) (match-end 2)))))))
464 (defun nnbabyl-insert-lines ()
465 "Insert how many lines and chars there are in the body of the mail."
468 (goto-char (point-min))
469 (when (search-forward "\n\n" nil t)
470 ;; There may be an EOOH line here...
471 (when (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
472 (search-forward "\n\n" nil t))
473 (setq chars (- (point-max) (point))
474 lines (max (- (count-lines (point) (point-max)) 1) 0))
475 ;; Move back to the end of the headers.
476 (goto-char (point-min))
477 (search-forward "\n\n" nil t)
480 (when (re-search-backward "^Lines: " nil t)
481 (delete-region (point) (progn (forward-line 1) (point)))))
482 (insert (format "Lines: %d\n" lines))
485 (defun nnbabyl-save-mail (group-art)
486 ;; Called narrowed to an article.
487 (nnbabyl-insert-lines)
488 (nnmail-insert-xref group-art)
489 (nnbabyl-insert-newsgroup-line group-art)
490 (run-hooks 'nnbabyl-prepare-save-mail-hook)
493 (defun nnbabyl-insert-newsgroup-line (group-art)
495 (goto-char (point-min))
496 (while (looking-at "From ")
497 (replace-match "Mail-from: From " t t)
499 ;; If there is a C-l at the beginning of the narrowed region, this
500 ;; isn't really a "save", but rather a "scan".
501 (goto-char (point-min))
502 (unless (looking-at "\^L")
504 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
505 (goto-char (point-max))
507 (when (search-forward "\n\n" nil t)
510 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
511 (caar group-art) (cdar group-art)
512 (current-time-string)))
513 (setq group-art (cdr group-art))))
516 (defun nnbabyl-active-number (group)
517 ;; Find the next article number in GROUP.
518 (let ((active (cadr (assoc group nnbabyl-group-alist))))
520 (setcdr active (1+ (cdr active)))
521 ;; This group is new, so we create a new entry for it.
522 ;; This might be a bit naughty... creating groups on the drop of
523 ;; a hat, but I don't know...
524 (push (list group (setq active (cons 1 1)))
525 nnbabyl-group-alist))
528 (defun nnbabyl-create-mbox ()
529 (unless (file-exists-p nnbabyl-mbox-file)
530 ;; Create a new, empty RMAIL mbox file.
532 (set-buffer (setq nnbabyl-mbox-buffer
533 (create-file-buffer nnbabyl-mbox-file)))
534 (setq buffer-file-name nnbabyl-mbox-file)
535 (insert "BABYL OPTIONS:\n\n\^_")
537 (point-min) (point-max) nnbabyl-mbox-file t 'nomesg))))
539 (defun nnbabyl-read-mbox ()
540 (nnmail-activate 'nnbabyl)
541 (nnbabyl-create-mbox)
543 (if (and nnbabyl-mbox-buffer
544 (buffer-name nnbabyl-mbox-buffer)
546 (set-buffer nnbabyl-mbox-buffer)
547 (= (buffer-size) (nnheader-file-size nnbabyl-mbox-file))))
548 () ; This buffer hasn't changed since we read it last. Possibly.
550 (let ((delim (concat "^" nnbabyl-mail-delimiter))
551 (alist nnbabyl-group-alist)
553 (set-buffer (setq nnbabyl-mbox-buffer
554 (nnheader-find-file-noselect
555 nnbabyl-mbox-file nil 'raw)))
556 ;; Save previous buffer mode.
557 (setq nnbabyl-previous-buffer-mode
558 (cons (cons (point-min) (point-max))
561 (buffer-disable-undo (current-buffer))
563 (setq buffer-read-only nil)
566 ;; Go through the group alist and compare against
569 (goto-char (point-max))
570 (when (and (re-search-backward
571 (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
577 (match-beginning 1) (match-end 1))))
579 (setcdr (cadar alist) (1+ number)))
580 (setq alist (cdr alist)))
582 ;; We go through the mbox and make sure that each and
583 ;; every mail belongs to some group or other.
584 (goto-char (point-min))
585 (re-search-forward delim nil t)
586 (setq start (match-end 0))
587 (while (re-search-forward delim nil t)
588 (setq end (match-end 0))
589 (unless (search-backward "\nX-Gnus-Newsgroup: " start t)
593 (narrow-to-region (goto-char start) end)
595 (nnmail-article-group 'nnbabyl-active-number))
596 (setq end (point-max)))))
597 (goto-char (setq start end)))
598 (when (buffer-modified-p (current-buffer))
600 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
602 (defun nnbabyl-remove-incoming-delims ()
603 (goto-char (point-min))
604 (while (search-forward "\^_" nil t)
605 (replace-match "?" t t)))
607 (defun nnbabyl-check-mbox ()
608 "Go through the nnbabyl mbox and make sure that no article numbers are reused."
610 (let ((idents (make-vector 1000 0))
613 (when (or (not nnbabyl-mbox-buffer)
614 (not (buffer-name nnbabyl-mbox-buffer)))
616 (set-buffer nnbabyl-mbox-buffer)
617 (goto-char (point-min))
618 (while (re-search-forward "^X-Gnus-Newsgroup: \\([^ ]+\\) " nil t)
619 (if (intern-soft (setq id (match-string 1)) idents)
621 (delete-region (progn (beginning-of-line) (point))
622 (progn (forward-line 1) (point)))
623 (nnheader-message 7 "Moving %s..." id)
625 (nnmail-article-group 'nnbabyl-active-number)))
627 (when (buffer-modified-p (current-buffer))
629 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
634 ;;; nnbabyl.el ends here