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 beg 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 (progn (goto-char (point-min))
91 (search-forward art-string nil t)))
96 (concat "^" nnbabyl-mail-delimiter) nil t)
97 (while (and (not (looking-at ".+:"))
98 (zerop (forward-line 1))))
100 (search-forward "\n\n" nil t)
101 (setq stop (1- (point)))
102 (set-buffer nntp-server-buffer)
103 (insert (format "221 %d Article retrieved.\n" article))
105 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
106 (goto-char (point-max))
108 (setq sequence (cdr sequence))
109 (setq count (1+ count))
110 (and (numberp nnmail-large-newsgroup)
111 (> number nnmail-large-newsgroup)
113 gnus-verbose-backends
114 (message "nnbabyl: Receiving headers... %d%%"
115 (/ (* count 100) number))))
117 (and (numberp nnmail-large-newsgroup)
118 (> number nnmail-large-newsgroup)
119 gnus-verbose-backends
120 (message "nnbabyl: Receiving headers... done"))
122 ;; Fold continuation lines.
123 (goto-char (point-min))
124 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
125 (replace-match " " t t))
128 (defun nnbabyl-open-server (server &optional defs)
129 (nnheader-init-server-buffer)
130 (if (equal server nnbabyl-current-server)
132 (if nnbabyl-current-server
133 (setq nnbabyl-server-alist
134 (cons (list nnbabyl-current-server
135 (nnheader-save-variables nnbabyl-server-variables))
136 nnbabyl-server-alist)))
137 (let ((state (assoc server nnbabyl-server-alist)))
140 (nnheader-restore-variables (nth 1 state))
141 (setq nnbabyl-server-alist (delq state nnbabyl-server-alist)))
142 (nnheader-set-init-variables nnbabyl-server-variables defs)))
143 (setq nnbabyl-current-server server)))
145 (defun nnbabyl-close-server (&optional server)
148 (defun nnbabyl-server-opened (&optional server)
149 (and (equal server nnbabyl-current-server)
151 (buffer-name nntp-server-buffer)))
153 (defun nnbabyl-status-message (&optional server)
154 nnbabyl-status-string)
156 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
157 (nnbabyl-possibly-change-newsgroup newsgroup)
158 (if (stringp article)
161 (set-buffer nnbabyl-mbox-buffer)
162 (goto-char (point-min))
163 (if (search-forward (nnbabyl-article-string article) nil t)
165 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
166 (while (and (not (looking-at ".+:"))
167 (zerop (forward-line 1))))
169 (or (and (re-search-forward
170 (concat "^" nnbabyl-mail-delimiter) nil t)
172 (goto-char (point-max)))
174 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
175 (set-buffer nntp-server-buffer)
177 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
178 (goto-char (point-min))
179 (if (search-forward "\n*** EOOH ***" nil t)
181 (delete-region (progn (beginning-of-line) (point))
182 (or (search-forward "\n\n" nil t)
186 (defun nnbabyl-request-group (group &optional server dont-check)
188 (if (nnbabyl-possibly-change-newsgroup group)
191 (nnbabyl-get-new-mail group)
193 (set-buffer nntp-server-buffer)
195 (let ((active (assoc group nnbabyl-group-alist)))
196 (insert (format "211 %d %d %d %s\n"
197 (1+ (- (cdr (car (cdr active)))
198 (car (car (cdr active)))))
199 (car (car (cdr active)))
200 (cdr (car (cdr active)))
204 (defun nnbabyl-close-group (group &optional server)
207 (defun nnbabyl-request-list (&optional server)
208 (if server (nnbabyl-get-new-mail))
210 (or (nnmail-find-file nnbabyl-active-file)
212 (setq nnbabyl-group-alist (nnmail-get-active))
213 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
214 (nnmail-find-file nnbabyl-active-file)))))
216 (defun nnbabyl-request-newgroups (date &optional server)
217 (nnbabyl-request-list server))
219 (defun nnbabyl-request-list-newsgroups (&optional server)
220 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
223 (defun nnbabyl-request-post (&optional server)
224 (mail-send-and-exit nil))
226 (fset 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
228 (defun nnbabyl-request-expire-articles (articles newsgroup &optional server force)
229 (nnbabyl-possibly-change-newsgroup newsgroup)
230 (let* ((days (or (and nnmail-expiry-wait-function
231 (funcall nnmail-expiry-wait-function newsgroup))
235 (set-buffer nnbabyl-mbox-buffer)
237 (goto-char (point-min))
238 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
240 (> (nnmail-days-between
241 (current-time-string)
243 (point) (progn (end-of-line) (point))))
246 (and gnus-verbose-backends
247 (message "Deleting: %s" (car articles)))
248 (nnbabyl-delete-mail))
249 (setq rest (cons (car articles) rest))))
250 (setq articles (cdr articles)))
252 ;; Find the lowest active article in this group.
253 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
254 (goto-char (point-min))
255 (while (not (search-forward
256 (nnbabyl-article-string (car active)) nil t))
257 (setcar active (1+ (car active)))
258 (goto-char (point-min))))
259 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
262 (defun nnbabyl-request-move-article
263 (article group server accept-form &optional last)
264 (nnbabyl-possibly-change-newsgroup group)
265 (let ((buf (get-buffer-create " *nnbabyl move*"))
268 (nnbabyl-request-article article group server)
271 (insert-buffer-substring nntp-server-buffer)
272 (goto-char (point-min))
273 (if (re-search-forward
275 (save-excursion (search-forward "\n\n" nil t) (point)) t)
276 (delete-region (progn (beginning-of-line) (point))
277 (progn (forward-line 1) (point))))
278 (setq result (eval accept-form))
279 (kill-buffer (current-buffer))
282 (set-buffer nnbabyl-mbox-buffer)
283 (goto-char (point-min))
284 (if (search-forward (nnbabyl-article-string article) nil t)
285 (nnbabyl-delete-mail))
286 (and last (save-buffer))))
289 (defun nnbabyl-request-accept-article (group &optional last)
290 (let ((buf (current-buffer))
293 (nnbabyl-request-list)
294 (setq nnbabyl-group-alist (nnmail-get-active))
296 (goto-char (point-min))
297 (search-forward "\n\n" nil t)
300 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
301 (delete-region (point) (progn (forward-line 1) (point)))))
302 (let ((nnmail-split-methods
303 (if (stringp group) (list (list group ""))
304 nnmail-split-methods)))
305 (setq result (car (nnbabyl-save-mail))))
306 (set-buffer nnbabyl-mbox-buffer)
307 (goto-char (point-max))
308 (search-backward "\n\^_")
309 (goto-char (match-end 0))
314 nnbabyl-group-alist nnbabyl-active-file)))
317 (defun nnbabyl-request-replace-article (article group buffer)
318 (nnbabyl-possibly-change-newsgroup group)
320 (set-buffer nnbabyl-mbox-buffer)
321 (goto-char (point-min))
322 (if (not (search-forward (nnbabyl-article-string article) nil t))
324 (nnbabyl-delete-mail t t)
325 (insert-buffer-substring buffer)
330 ;;; Low-Level Interface
332 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
333 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
335 (defun nnbabyl-delete-mail (&optional force leave-delim)
336 ;; Delete the current X-Gnus-Newsgroup line.
339 (progn (beginning-of-line) (point))
340 (progn (forward-line 1) (point))))
341 ;; Beginning of the article.
346 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
347 (if leave-delim (progn (forward-line 1) (point))
348 (match-beginning 0)))
351 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
353 (if (and (not (bobp)) leave-delim)
354 (progn (forward-line -2) (point))
355 (match-beginning 0)))
357 (goto-char (point-min))
358 ;; Only delete the article if no other groups owns it as well.
359 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
360 (delete-region (point-min) (point-max))))))
362 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
363 (if (or (not nnbabyl-mbox-buffer)
364 (not (buffer-name nnbabyl-mbox-buffer)))
365 (save-excursion (nnbabyl-read-mbox)))
366 (or nnbabyl-group-alist
368 (nnbabyl-request-list)
369 (setq nnbabyl-group-alist (nnmail-get-active))))
371 (if (assoc newsgroup nnbabyl-group-alist)
372 (setq nnbabyl-current-group newsgroup)
373 (setq nnbabyl-status-string "No such group in file")
376 (defun nnbabyl-article-string (article)
377 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
378 (int-to-string article) " "))
380 (defun nnbabyl-insert-lines ()
381 "Insert how many lines and chars there are in the body of the mail."
384 (goto-char (point-min))
385 (if (search-forward "\n\n" nil t)
387 ;; There may be an EOOH line here...
388 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
389 (search-forward "\n\n" nil t))
390 (setq chars (- (point-max) (point)))
391 (setq lines (- (count-lines (point) (point-max)) 1))
392 ;; Move back to the end of the headers.
393 (goto-char (point-min))
394 (search-forward "\n\n" nil t)
397 (if (re-search-backward "^Lines: " nil t)
398 (delete-region (point) (progn (forward-line 1) (point)))))
399 (insert (format "Lines: %d\n" lines))
402 (defun nnbabyl-save-mail ()
403 ;; Called narrowed to an article.
404 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
405 (nnbabyl-insert-lines)
406 (nnmail-insert-xref group-art)
407 (nnbabyl-insert-newsgroup-line group-art)
408 (run-hooks 'nnbabyl-prepare-save-mail-hook)
411 (defun nnbabyl-insert-newsgroup-line (group-art)
413 (goto-char (point-min))
414 ;; If there is a C-l at the beginning of the narrowed region, this
415 ;; isn't really a "save", but rather a "scan".
416 (while (looking-at "From ")
417 (replace-match "Mail-from: From " t t)
419 (goto-char (point-min))
420 (or (looking-at "\^L")
422 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
423 (goto-char (point-max))
425 (if (search-forward "\n\n" nil t)
429 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
430 (car (car group-art)) (cdr (car group-art))
431 (current-time-string)))
432 (setq group-art (cdr group-art)))))
435 (defun nnbabyl-active-number (group)
436 ;; Find the next article number in GROUP.
437 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
438 (setcdr active (1+ (cdr active)))
441 (defun nnbabyl-read-mbox ()
442 (nnbabyl-request-list)
443 (setq nnbabyl-group-alist (nnmail-get-active))
444 (or (file-exists-p nnbabyl-mbox-file)
446 (set-buffer (setq nnbabyl-mbox-buffer
447 (create-file-buffer nnbabyl-mbox-file)))
448 (setq buffer-file-name nnbabyl-mbox-file)
449 (insert "BABYL OPTIONS:\n\n\^_")
450 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
451 (if (and nnbabyl-mbox-buffer
452 (get-buffer nnbabyl-mbox-buffer)
453 (buffer-name nnbabyl-mbox-buffer)
455 (set-buffer nnbabyl-mbox-buffer)
456 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
459 (let ((delim (concat "^" nnbabyl-mail-delimiter))
460 (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
461 (create-file-buffer nnbabyl-mbox-file)))
463 (set-buffer (setq nnbabyl-mbox-buffer buf))
464 (buffer-disable-undo (current-buffer))
466 (insert-file-contents nnbabyl-mbox-file)
467 (setq buffer-file-name nnbabyl-mbox-file)
468 (set-buffer-modified-p nil)
470 (goto-char (point-min))
471 (while (re-search-forward delim nil t)
472 (setq start (match-beginning 0))
474 (save-excursion (re-search-forward delim nil t))
476 "\nX-Gnus-Newsgroup: "
478 (setq end (or (and (re-search-forward delim nil t)
486 (narrow-to-region (1+ start) end)
487 (nnbabyl-save-mail))))))
488 (and (buffer-modified-p (current-buffer)) (save-buffer))
489 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
491 (defun nnbabyl-get-new-mail (&optional group)
492 "Read new incoming mail."
493 (let ((spools (nnmail-get-spool-files group))
496 (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
498 ;; We go through all the existing spool files and split the
502 (file-exists-p (car spools))
503 (> (nth 7 (file-attributes (car spools))) 0)
505 (and gnus-verbose-backends
506 (message "nnbabyl: Reading incoming mail..."))
509 (car spools) (concat nnbabyl-mbox-file "-Incoming")))
510 (setq incomings (cons incoming incomings))
512 (let ((in-buf (nnmail-split-incoming
513 incoming 'nnbabyl-save-mail t)))
515 (goto-char (point-min))
516 (while (search-forward "\n\^_\n" nil t)
518 (set-buffer nnbabyl-mbox-buffer)
519 (goto-char (point-max))
520 (search-backward "\n\^_" nil t)
521 (goto-char (match-end 0))
522 (insert-buffer-substring in-buf)
523 (kill-buffer in-buf)))))
524 (setq spools (cdr spools)))
525 ;; If we did indeed read any incoming spools, we save all info.
526 (and (buffer-modified-p nnbabyl-mbox-buffer)
528 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
529 (set-buffer nnbabyl-mbox-buffer)
532 ;; The following has been commented away, just to make sure
533 ;; that nobody ever loses any mail. If you feel safe that
534 ;; nnfolder will never do anything strange, just remove those
535 ;; two semicolons, and avoid having lots of "Incoming*"
537 ;; (and (file-writable-p incoming) (delete-file incoming))
538 (setq incomings (cdr incomings))))))
542 ;;; nnbabyl.el ends here