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)
80 (let ((number (length sequence))
82 article art-string start stop)
83 (nnbabyl-possibly-change-newsgroup newsgroup)
84 (if (stringp (car sequence))
87 (setq article (car sequence))
88 (setq art-string (nnbabyl-article-string article))
89 (set-buffer nnbabyl-mbox-buffer)
90 (if (or (search-forward art-string nil t)
91 (search-backward art-string nil t))
93 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
94 (while (and (not (looking-at ".+:"))
95 (zerop (forward-line 1))))
97 (search-forward "\n\n" nil t)
98 (setq stop (1- (point)))
99 (set-buffer nntp-server-buffer)
100 (insert "221 " (int-to-string article) " Article retrieved.\n")
101 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
102 (goto-char (point-max))
104 (setq sequence (cdr sequence))
105 (setq count (1+ count))
106 (and (numberp nnmail-large-newsgroup)
107 (> number nnmail-large-newsgroup)
109 gnus-verbose-backends
110 (message "nnbabyl: Receiving headers... %d%%"
111 (/ (* count 100) number))))
113 (and (numberp nnmail-large-newsgroup)
114 (> number nnmail-large-newsgroup)
115 gnus-verbose-backends
116 (message "nnbabyl: Receiving headers...done"))
118 ;; Fold continuation lines.
119 (goto-char (point-min))
120 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
121 (replace-match " " t t))
124 (defun nnbabyl-open-server (server &optional defs)
125 (nnheader-init-server-buffer)
126 (if (equal server nnbabyl-current-server)
128 (if nnbabyl-current-server
129 (setq nnbabyl-server-alist
130 (cons (list nnbabyl-current-server
131 (nnheader-save-variables nnbabyl-server-variables))
132 nnbabyl-server-alist)))
133 (let ((state (assoc server nnbabyl-server-alist)))
136 (nnheader-restore-variables (nth 1 state))
137 (setq nnbabyl-server-alist (delq state nnbabyl-server-alist)))
138 (nnheader-set-init-variables nnbabyl-server-variables defs)))
139 (setq nnbabyl-current-server server)))
141 (defun nnbabyl-close-server (&optional server)
144 (defun nnbabyl-server-opened (&optional server)
145 (and (equal server nnbabyl-current-server)
147 (buffer-name nnbabyl-mbox-buffer)
149 (buffer-name nntp-server-buffer)))
151 (defun nnbabyl-status-message (&optional server)
152 nnbabyl-status-string)
154 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
155 (nnbabyl-possibly-change-newsgroup newsgroup)
156 (if (stringp article)
159 (set-buffer nnbabyl-mbox-buffer)
160 (goto-char (point-min))
161 (if (search-forward (nnbabyl-article-string article) nil t)
163 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
164 (while (and (not (looking-at ".+:"))
165 (zerop (forward-line 1))))
167 (or (and (re-search-forward
168 (concat "^" nnbabyl-mail-delimiter) nil t)
170 (goto-char (point-max)))
172 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
173 (set-buffer nntp-server-buffer)
175 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
176 (goto-char (point-min))
177 (if (search-forward "\n*** EOOH ***" nil t)
179 (delete-region (progn (beginning-of-line) (point))
180 (or (search-forward "\n\n" nil t)
184 (defun nnbabyl-request-group (group &optional server dont-check)
186 (if (nnbabyl-possibly-change-newsgroup group)
189 (nnbabyl-get-new-mail group)
191 (set-buffer nntp-server-buffer)
193 (let ((active (assoc group nnbabyl-group-alist)))
194 (insert (format "211 %d %d %d %s\n"
195 (1+ (- (cdr (car (cdr active)))
196 (car (car (cdr active)))))
197 (car (car (cdr active)))
198 (cdr (car (cdr active)))
202 (defun nnbabyl-close-group (group &optional server)
205 (defun nnbabyl-request-list (&optional server)
206 (if server (nnbabyl-get-new-mail))
208 (or (nnmail-find-file nnbabyl-active-file)
210 (setq nnbabyl-group-alist (nnmail-get-active))
211 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
212 (nnmail-find-file nnbabyl-active-file)))))
214 (defun nnbabyl-request-newgroups (date &optional server)
215 (nnbabyl-request-list server))
217 (defun nnbabyl-request-list-newsgroups (&optional server)
218 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
221 (defun nnbabyl-request-post (&optional server)
222 (mail-send-and-exit nil))
224 (defalias 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
226 (defun nnbabyl-request-expire-articles (articles newsgroup &optional server force)
227 (nnbabyl-possibly-change-newsgroup newsgroup)
228 (let* ((days (or (and nnmail-expiry-wait-function
229 (funcall nnmail-expiry-wait-function newsgroup))
233 (set-buffer nnbabyl-mbox-buffer)
235 (goto-char (point-min))
236 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
238 (> (nnmail-days-between
239 (current-time-string)
241 (point) (progn (end-of-line) (point))))
244 (and gnus-verbose-backends
245 (message "Deleting: %s" (car articles)))
246 (nnbabyl-delete-mail))
247 (setq rest (cons (car articles) rest))))
248 (setq articles (cdr articles)))
250 ;; Find the lowest active article in this group.
251 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
252 (goto-char (point-min))
253 (while (and (not (search-forward
254 (nnbabyl-article-string (car active)) nil t))
255 (<= (car active) (cdr active)))
256 (setcar active (1+ (car active)))
257 (goto-char (point-min))))
258 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
261 (defun nnbabyl-request-move-article
262 (article group server accept-form &optional last)
263 (nnbabyl-possibly-change-newsgroup group)
264 (let ((buf (get-buffer-create " *nnbabyl move*"))
267 (nnbabyl-request-article article group server)
270 (insert-buffer-substring nntp-server-buffer)
271 (goto-char (point-min))
272 (if (re-search-forward
274 (save-excursion (search-forward "\n\n" nil t) (point)) t)
275 (delete-region (progn (beginning-of-line) (point))
276 (progn (forward-line 1) (point))))
277 (setq result (eval accept-form))
278 (kill-buffer (current-buffer))
281 (set-buffer nnbabyl-mbox-buffer)
282 (goto-char (point-min))
283 (if (search-forward (nnbabyl-article-string article) nil t)
284 (nnbabyl-delete-mail))
285 (and last (save-buffer))))
288 (defun nnbabyl-request-accept-article (group &optional last)
289 (let ((buf (current-buffer))
292 (nnbabyl-request-list)
293 (setq nnbabyl-group-alist (nnmail-get-active))
295 (goto-char (point-min))
296 (search-forward "\n\n" nil t)
299 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
300 (delete-region (point) (progn (forward-line 1) (point)))))
301 (let ((nnmail-split-methods
302 (if (stringp group) (list (list group ""))
303 nnmail-split-methods)))
304 (setq result (car (nnbabyl-save-mail))))
305 (set-buffer nnbabyl-mbox-buffer)
306 (goto-char (point-max))
307 (search-backward "\n\^_")
308 (goto-char (match-end 0))
313 nnbabyl-group-alist nnbabyl-active-file)))
316 (defun nnbabyl-request-replace-article (article group buffer)
317 (nnbabyl-possibly-change-newsgroup group)
319 (set-buffer nnbabyl-mbox-buffer)
320 (goto-char (point-min))
321 (if (not (search-forward (nnbabyl-article-string article) nil t))
323 (nnbabyl-delete-mail t t)
324 (insert-buffer-substring buffer)
329 ;;; Low-Level Interface
331 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
332 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
334 (defun nnbabyl-delete-mail (&optional force leave-delim)
335 ;; Delete the current X-Gnus-Newsgroup line.
338 (progn (beginning-of-line) (point))
339 (progn (forward-line 1) (point))))
340 ;; 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)))
452 (if (and 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-remove-incoming-delims ()
492 (goto-char (point-min))
493 (while (search-forward "\^_" nil t)
494 (replace-match "?" t t)))
496 (defun nnbabyl-get-new-mail (&optional group)
497 "Read new incoming mail."
498 (let* ((spools (nnmail-get-spool-files group))
502 (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
504 ;; We go through all the existing spool files and split the
508 (file-exists-p (car spools))
509 (> (nth 7 (file-attributes (car spools))) 0)
511 (and gnus-verbose-backends
512 (message "nnbabyl: Reading incoming mail..."))
515 (car spools) (concat nnbabyl-mbox-file "-Incoming")))
516 (setq incomings (cons incoming incomings))
518 (setq group (nnmail-get-split-group (car spools) group-in))
519 (let* ((nnmail-prepare-incoming-hook
520 (cons 'nnbabyl-remove-incoming-delims
521 nnmail-prepare-incoming-hook))
523 (setq in-buf (nnmail-split-incoming
524 incoming 'nnbabyl-save-mail t group))
526 (goto-char (point-min))
527 (while (search-forward "\n\^_\n" nil t)
529 (set-buffer nnbabyl-mbox-buffer)
530 (goto-char (point-max))
531 (search-backward "\n\^_" nil t)
532 (goto-char (match-end 0))
533 (insert-buffer-substring in-buf)
534 (kill-buffer in-buf)))))
535 (setq spools (cdr spools)))
536 ;; If we did indeed read any incoming spools, we save all info.
537 (and (buffer-modified-p nnbabyl-mbox-buffer)
539 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
540 (set-buffer nnbabyl-mbox-buffer)
542 (if incomings (run-hooks 'nnmail-read-incoming-hook))
544 (and nnmail-delete-incoming
545 (file-writable-p incoming)
546 (delete-file incoming))
547 (setq incomings (cdr incomings))))))
551 ;;; nnbabyl.el ends here