1 ;;; nnbabyl.el --- rmail mbox access for Gnus
2 ;; Copyright (C) 1995 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; For an overview of what the interface functions do, please see the
35 (defvar nnbabyl-mbox-file (expand-file-name "~/RMAIL")
36 "The name of the rmail box file in the users home directory.")
38 (defvar nnbabyl-active-file (expand-file-name "~/.rmail-active")
39 "The name of the active file for the rmail box.")
41 (defvar nnbabyl-get-new-mail t
42 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
44 (defvar nnbabyl-prepare-save-mail-hook nil
45 "Hook run narrowed to an article before saving.")
49 (defvar nnbabyl-mail-delimiter "\^_")
51 (defconst nnbabyl-version "nnbabyl 0.1"
54 (defvar nnbabyl-mbox-buffer nil)
55 (defvar nnbabyl-current-group nil)
56 (defvar nnbabyl-status-string "")
57 (defvar nnbabyl-group-alist nil)
61 (defvar nnbabyl-current-server nil)
62 (defvar nnbabyl-server-alist nil)
63 (defvar nnbabyl-server-variables
65 (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
66 (list 'nnbabyl-active-file nnbabyl-active-file)
67 (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
68 '(nnbabyl-current-group nil)
69 '(nnbabyl-status-string "")
70 '(nnbabyl-group-alist nil)))
74 ;;; Interface functions
76 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server)
78 (set-buffer nntp-server-buffer)
81 (number (length sequence))
83 article art-string start stop)
84 (nnbabyl-possibly-change-newsgroup newsgroup)
86 (setq article (car sequence))
87 (setq art-string (nnbabyl-article-string article))
88 (set-buffer nnbabyl-mbox-buffer)
89 (if (or (search-forward art-string nil t)
90 (search-backward art-string nil t))
92 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
93 (while (and (not (looking-at ".+:"))
94 (zerop (forward-line 1))))
96 (search-forward "\n\n" nil t)
97 (setq stop (1- (point)))
98 (set-buffer nntp-server-buffer)
99 (insert "221 " (int-to-string article) " Article retrieved.\n")
100 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
101 (goto-char (point-max))
103 (setq sequence (cdr sequence))
104 (setq count (1+ count))
105 (and (numberp nnmail-large-newsgroup)
106 (> number nnmail-large-newsgroup)
108 gnus-verbose-backends
109 (message "nnbabyl: Receiving headers... %d%%"
110 (/ (* count 100) number))))
112 (and (numberp nnmail-large-newsgroup)
113 (> number nnmail-large-newsgroup)
114 gnus-verbose-backends
115 (message "nnbabyl: Receiving headers... done"))
117 ;; Fold continuation lines.
118 (goto-char (point-min))
119 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
120 (replace-match " " t t))
123 (defun nnbabyl-open-server (server &optional defs)
124 (nnheader-init-server-buffer)
125 (if (equal server nnbabyl-current-server)
127 (if nnbabyl-current-server
128 (setq nnbabyl-server-alist
129 (cons (list nnbabyl-current-server
130 (nnheader-save-variables nnbabyl-server-variables))
131 nnbabyl-server-alist)))
132 (let ((state (assoc server nnbabyl-server-alist)))
135 (nnheader-restore-variables (nth 1 state))
136 (setq nnbabyl-server-alist (delq state nnbabyl-server-alist)))
137 (nnheader-set-init-variables nnbabyl-server-variables defs)))
138 (setq nnbabyl-current-server server)))
140 (defun nnbabyl-close-server (&optional server)
143 (defun nnbabyl-server-opened (&optional server)
144 (and (equal server nnbabyl-current-server)
146 (buffer-name nntp-server-buffer)))
148 (defun nnbabyl-status-message (&optional server)
149 nnbabyl-status-string)
151 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
152 (nnbabyl-possibly-change-newsgroup newsgroup)
153 (if (stringp article)
156 (set-buffer nnbabyl-mbox-buffer)
157 (goto-char (point-min))
158 (if (search-forward (nnbabyl-article-string article) nil t)
160 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
161 (while (and (not (looking-at ".+:"))
162 (zerop (forward-line 1))))
164 (or (and (re-search-forward
165 (concat "^" nnbabyl-mail-delimiter) nil t)
167 (goto-char (point-max)))
169 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
170 (set-buffer nntp-server-buffer)
172 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
173 (goto-char (point-min))
174 (if (search-forward "\n*** EOOH ***" nil t)
176 (delete-region (progn (beginning-of-line) (point))
177 (or (search-forward "\n\n" nil t)
181 (defun nnbabyl-request-group (group &optional server dont-check)
183 (if (nnbabyl-possibly-change-newsgroup group)
186 (nnbabyl-get-new-mail group)
188 (set-buffer nntp-server-buffer)
190 (let ((active (assoc group nnbabyl-group-alist)))
191 (insert (format "211 %d %d %d %s\n"
192 (1+ (- (cdr (car (cdr active)))
193 (car (car (cdr active)))))
194 (car (car (cdr active)))
195 (cdr (car (cdr active)))
199 (defun nnbabyl-close-group (group &optional server)
202 (defun nnbabyl-request-list (&optional server)
203 (if server (nnbabyl-get-new-mail))
205 (or (nnmail-find-file nnbabyl-active-file)
207 (setq nnbabyl-group-alist (nnmail-get-active))
208 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
209 (nnmail-find-file nnbabyl-active-file)))))
211 (defun nnbabyl-request-newgroups (date &optional server)
212 (nnbabyl-request-list server))
214 (defun nnbabyl-request-list-newsgroups (&optional server)
215 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
218 (defun nnbabyl-request-post (&optional server)
219 (mail-send-and-exit nil))
221 (fset 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
223 (defun nnbabyl-request-expire-articles (articles newsgroup &optional server force)
224 (nnbabyl-possibly-change-newsgroup newsgroup)
225 (let* ((days (or (and nnmail-expiry-wait-function
226 (funcall nnmail-expiry-wait-function newsgroup))
230 (set-buffer nnbabyl-mbox-buffer)
232 (goto-char (point-min))
233 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
235 (> (nnmail-days-between
236 (current-time-string)
238 (point) (progn (end-of-line) (point))))
241 (and gnus-verbose-backends
242 (message "Deleting: %s" (car articles)))
243 (nnbabyl-delete-mail))
244 (setq rest (cons (car articles) rest))))
245 (setq articles (cdr articles)))
247 ;; Find the lowest active article in this group.
248 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
249 (goto-char (point-min))
250 (while (not (search-forward
251 (nnbabyl-article-string (car active)) nil t))
252 (setcar active (1+ (car active)))
253 (goto-char (point-min))))
254 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
257 (defun nnbabyl-request-move-article
258 (article group server accept-form &optional last)
259 (nnbabyl-possibly-change-newsgroup group)
260 (let ((buf (get-buffer-create " *nnbabyl move*"))
263 (nnbabyl-request-article article group server)
266 (insert-buffer-substring nntp-server-buffer)
267 (goto-char (point-min))
268 (if (re-search-forward
270 (save-excursion (search-forward "\n\n" nil t) (point)) t)
271 (delete-region (progn (beginning-of-line) (point))
272 (progn (forward-line 1) (point))))
273 (setq result (eval accept-form))
274 (kill-buffer (current-buffer))
277 (set-buffer nnbabyl-mbox-buffer)
278 (goto-char (point-min))
279 (if (search-forward (nnbabyl-article-string article) nil t)
280 (nnbabyl-delete-mail))
281 (and last (save-buffer))))
284 (defun nnbabyl-request-accept-article (group &optional last)
285 (let ((buf (current-buffer))
288 (nnbabyl-request-list)
289 (setq nnbabyl-group-alist (nnmail-get-active))
291 (goto-char (point-min))
292 (search-forward "\n\n" nil t)
295 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
296 (delete-region (point) (progn (forward-line 1) (point)))))
297 (let ((nnmail-split-methods
298 (if (stringp group) (list (list group ""))
299 nnmail-split-methods)))
300 (setq result (car (nnbabyl-save-mail))))
301 (set-buffer nnbabyl-mbox-buffer)
302 (goto-char (point-max))
303 (search-backward "\n\^_")
304 (goto-char (match-end 0))
309 nnbabyl-group-alist nnbabyl-active-file)))
312 (defun nnbabyl-request-replace-article (article group buffer)
313 (nnbabyl-possibly-change-newsgroup group)
315 (set-buffer nnbabyl-mbox-buffer)
316 (goto-char (point-min))
317 (if (not (search-forward (nnbabyl-article-string article) nil t))
319 (nnbabyl-delete-mail t t)
320 (insert-buffer-substring buffer)
325 ;;; Low-Level Interface
327 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
328 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
330 (defun nnbabyl-delete-mail (&optional force leave-delim)
331 ;; Delete the current X-Gnus-Newsgroup line.
334 (progn (beginning-of-line) (point))
335 (progn (forward-line 1) (point))))
336 ;; Beginning of the article.
341 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
342 (if leave-delim (progn (forward-line 1) (point))
343 (match-beginning 0)))
346 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
348 (if (and (not (bobp)) leave-delim)
349 (progn (forward-line -2) (point))
350 (match-beginning 0)))
352 (goto-char (point-min))
353 ;; Only delete the article if no other groups owns it as well.
354 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
355 (delete-region (point-min) (point-max))))))
357 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
358 (if (or (not nnbabyl-mbox-buffer)
359 (not (buffer-name nnbabyl-mbox-buffer)))
360 (save-excursion (nnbabyl-read-mbox)))
361 (or nnbabyl-group-alist
363 (nnbabyl-request-list)
364 (setq nnbabyl-group-alist (nnmail-get-active))))
366 (if (assoc newsgroup nnbabyl-group-alist)
367 (setq nnbabyl-current-group newsgroup)
368 (setq nnbabyl-status-string "No such group in file")
371 (defun nnbabyl-article-string (article)
372 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
373 (int-to-string article) " "))
375 (defun nnbabyl-insert-lines ()
376 "Insert how many lines and chars there are in the body of the mail."
379 (goto-char (point-min))
380 (if (search-forward "\n\n" nil t)
382 ;; There may be an EOOH line here...
383 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
384 (search-forward "\n\n" nil t))
385 (setq chars (- (point-max) (point)))
386 (setq lines (- (count-lines (point) (point-max)) 1))
387 ;; Move back to the end of the headers.
388 (goto-char (point-min))
389 (search-forward "\n\n" nil t)
392 (if (re-search-backward "^Lines: " nil t)
393 (delete-region (point) (progn (forward-line 1) (point)))))
394 (insert (format "Lines: %d\n" lines))
397 (defun nnbabyl-save-mail ()
398 ;; Called narrowed to an article.
399 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
400 (nnbabyl-insert-lines)
401 (nnmail-insert-xref group-art)
402 (nnbabyl-insert-newsgroup-line group-art)
403 (run-hooks 'nnbabyl-prepare-save-mail-hook)
406 (defun nnbabyl-insert-newsgroup-line (group-art)
408 (goto-char (point-min))
409 ;; If there is a C-l at the beginning of the narrowed region, this
410 ;; isn't really a "save", but rather a "scan".
411 (while (looking-at "From ")
412 (replace-match "Mail-from: From " t t)
414 (goto-char (point-min))
415 (or (looking-at "\^L")
417 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
418 (goto-char (point-max))
420 (if (search-forward "\n\n" nil t)
424 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
425 (car (car group-art)) (cdr (car group-art))
426 (current-time-string)))
427 (setq group-art (cdr group-art)))))
430 (defun nnbabyl-active-number (group)
431 ;; Find the next article number in GROUP.
432 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
433 (setcdr active (1+ (cdr active)))
436 (defun nnbabyl-read-mbox ()
437 (nnbabyl-request-list)
438 (setq nnbabyl-group-alist (nnmail-get-active))
439 (or (file-exists-p nnbabyl-mbox-file)
441 (set-buffer (setq nnbabyl-mbox-buffer
442 (create-file-buffer nnbabyl-mbox-file)))
443 (setq buffer-file-name nnbabyl-mbox-file)
444 (insert "BABYL OPTIONS:\n\n\^_")
445 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
446 (if (and nnbabyl-mbox-buffer
447 (get-buffer nnbabyl-mbox-buffer)
448 (buffer-name nnbabyl-mbox-buffer)
450 (set-buffer nnbabyl-mbox-buffer)
451 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
454 (let ((delim (concat "^" nnbabyl-mail-delimiter))
455 (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
456 (create-file-buffer nnbabyl-mbox-file)))
458 (set-buffer (setq nnbabyl-mbox-buffer buf))
459 (buffer-disable-undo (current-buffer))
461 (insert-file-contents nnbabyl-mbox-file)
462 (setq buffer-file-name nnbabyl-mbox-file)
463 (set-buffer-modified-p nil)
465 (goto-char (point-min))
466 (while (re-search-forward delim nil t)
467 (setq start (match-beginning 0))
469 (save-excursion (re-search-forward delim nil t))
471 "\nX-Gnus-Newsgroup: "
473 (setq end (or (and (re-search-forward delim nil t)
481 (narrow-to-region (1+ start) end)
482 (nnbabyl-save-mail))))))
483 (and (buffer-modified-p (current-buffer)) (save-buffer))
484 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
486 (defun nnbabyl-get-new-mail (&optional group)
487 "Read new incoming mail."
488 (let ((spools (nnmail-get-spool-files group))
491 (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
493 ;; We go through all the existing spool files and split the
497 (file-exists-p (car spools))
498 (> (nth 7 (file-attributes (car spools))) 0)
500 (and gnus-verbose-backends
501 (message "nnbabyl: Reading incoming mail..."))
504 (car spools) (concat nnbabyl-mbox-file "-Incoming")))
505 (setq incomings (cons incoming incomings))
507 (let ((in-buf (nnmail-split-incoming
508 incoming 'nnbabyl-save-mail t)))
510 (goto-char (point-min))
511 (while (search-forward "\n\^_\n" nil t)
513 (set-buffer nnbabyl-mbox-buffer)
514 (goto-char (point-max))
515 (search-backward "\n\^_" nil t)
516 (goto-char (match-end 0))
517 (insert-buffer-substring in-buf)
518 (kill-buffer in-buf)))))
519 (setq spools (cdr spools)))
520 ;; If we did indeed read any incoming spools, we save all info.
521 (and (buffer-modified-p nnbabyl-mbox-buffer)
523 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
524 (set-buffer nnbabyl-mbox-buffer)
527 ;; The following has been commented away, just to make sure
528 ;; that nobody ever loses any mail. If you feel safe that
529 ;; nnfolder will never do anything strange, just remove those
530 ;; two semicolons, and avoid having lots of "Incoming*"
532 ;; (and (file-writable-p incoming) (delete-file incoming))
533 (setq incomings (cdr incomings))))))
537 ;;; nnbabyl.el ends here