1 ;;; nnbabyl.el --- mail 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 mail 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 mail box.")
41 (defvar nnbabyl-get-new-mail t
42 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
46 (defvar nnbabyl-mail-delimiter "\^_")
48 (defconst nnbabyl-version "nnbabyl 0.1"
51 (defconst nnbabyl-mbox-buffer " *nnbabyl mbox buffer*")
53 (defvar nnbabyl-current-group nil)
54 (defvar nnbabyl-status-string "")
55 (defvar nnbabyl-group-alist nil)
59 (defvar nnbabyl-current-server nil)
60 (defvar nnbabyl-server-alist nil)
61 (defvar nnbabyl-server-variables
63 (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
64 (list 'nnbabyl-active-file nnbabyl-active-file)
65 (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
66 '(nnbabyl-current-group nil)
67 '(nnbabyl-status-string "")
68 '(nnbabyl-group-alist nil)))
72 ;;; Interface functions
74 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server)
76 (set-buffer nntp-server-buffer)
79 (number (length sequence))
81 beg article art-string start stop)
82 (nnbabyl-possibly-change-newsgroup newsgroup)
84 (setq article (car sequence))
85 (setq art-string (nnbabyl-article-string article))
86 (set-buffer nnbabyl-mbox-buffer)
87 (if (or (search-forward art-string nil t)
89 (search-forward art-string nil t)))
94 (concat "^" nnbabyl-mail-delimiter) nil t)
95 (while (and (not (looking-at ".+:"))
96 (zerop (forward-line 1))))
98 (search-forward "\n\n" nil t)
99 (setq stop (1- (point)))
100 (set-buffer nntp-server-buffer)
101 (insert (format "221 %d Article retrieved.\n" article))
103 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
104 (goto-char (point-max))
106 (setq sequence (cdr sequence))
107 (setq count (1+ count))
108 (and (numberp nnmail-large-newsgroup)
109 (> number nnmail-large-newsgroup)
111 gnus-verbose-backends
112 (message "nnbabyl: Receiving headers... %d%%"
113 (/ (* count 100) number))))
115 (and (numberp nnmail-large-newsgroup)
116 (> number nnmail-large-newsgroup)
117 gnus-verbose-backends
118 (message "nnbabyl: Receiving headers... done"))
120 ;; Fold continuation lines.
122 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
123 (replace-match " " t t))
126 (defun nnbabyl-open-server (server &optional defs)
127 (nnheader-init-server-buffer)
128 (if (equal server nnbabyl-current-server)
130 (if nnbabyl-current-server
131 (setq nnbabyl-server-alist
132 (cons (list nnbabyl-current-server
133 (nnheader-save-variables nnbabyl-server-variables))
134 nnbabyl-server-alist)))
135 (let ((state (assoc server nnbabyl-server-alist)))
138 (nnheader-restore-variables (nth 1 state))
139 (setq nnbabyl-server-alist (delq state nnbabyl-server-alist)))
140 (nnheader-set-init-variables nnbabyl-server-variables defs)))
141 (setq nnbabyl-current-server server)))
143 (defun nnbabyl-close-server (&optional server)
146 (defun nnbabyl-server-opened (&optional server)
147 (and (equal server nnbabyl-current-server)
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)
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)
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))
207 (nnmail-find-file nnbabyl-active-file))
209 (defun nnbabyl-request-newgroups (date &optional server)
210 (nnbabyl-request-list server))
212 (defun nnbabyl-request-list-newsgroups (&optional server)
213 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
216 (defun nnbabyl-request-post (&optional server)
217 (mail-send-and-exit nil))
219 (fset 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
221 (defun nnbabyl-request-expire-articles (articles newsgroup &optional server force)
222 (nnbabyl-possibly-change-newsgroup newsgroup)
223 (let* ((days (or (and nnmail-expiry-wait-function
224 (funcall nnmail-expiry-wait-function newsgroup))
228 (set-buffer nnbabyl-mbox-buffer)
231 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
233 (> (nnmail-days-between
234 (current-time-string)
236 (point) (progn (end-of-line) (point))))
239 (and gnus-verbose-backends
240 (message "Deleting: %s" (car articles)))
241 (nnbabyl-delete-mail))
242 (setq rest (cons (car articles) rest))))
243 (setq articles (cdr articles)))
245 ;; Find the lowest active article in this group.
246 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
247 (goto-char (point-min))
248 (while (not (search-forward
249 (nnbabyl-article-string (car active)) nil t))
250 (setcar (car active) (1+ (car active)))
251 (goto-char (point-min))))
252 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
255 (defun nnbabyl-request-move-article
256 (article group server accept-form &optional last)
257 (nnbabyl-possibly-change-newsgroup group)
258 (let ((buf (get-buffer-create " *nnbabyl move*"))
261 (nnbabyl-request-article article group server)
264 (insert-buffer-substring nntp-server-buffer)
265 (goto-char (point-min))
266 (if (re-search-forward
268 (save-excursion (search-forward "\n\n" nil t) (point)) t)
269 (delete-region (progn (beginning-of-line) (point))
270 (progn (forward-line 1) (point))))
271 (setq result (eval accept-form))
272 (kill-buffer (current-buffer))
275 (set-buffer nnbabyl-mbox-buffer)
277 (if (search-forward (nnbabyl-article-string article) nil t)
278 (nnbabyl-delete-mail))
279 (and last (save-buffer))))
282 (defun nnbabyl-request-accept-article (group &optional last)
283 (let ((buf (current-buffer))
286 (setq nnbabyl-group-alist (nnmail-get-active))
288 (set-buffer nnbabyl-mbox-buffer)
289 (setq beg (goto-char (point-max)))
290 (insert-buffer-substring buf)
294 (search-forward "\n\n" nil t)
297 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
298 (delete-region (point) (progn (forward-line 1) (point)))))
299 (setq result (nnbabyl-insert-newsgroup-line group)))
300 (setq result (nnbabyl-save-mail)))
301 (and last (save-buffer))
303 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
306 (defun nnbabyl-request-replace-article (article group buffer)
307 (nnbabyl-possibly-change-newsgroup group)
309 (set-buffer nnbabyl-mbox-buffer)
311 (if (not (search-forward (nnbabyl-article-string article) nil t))
313 (nnbabyl-delete-mail t t)
314 (insert-buffer-substring buffer)
319 ;;; Low-Level Interface
321 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
322 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
324 (defun nnbabyl-delete-mail (&optional force leave-delim)
325 ;; Delete the current X-Gnus-Newsgroup line.
328 (progn (beginning-of-line) (point))
329 (progn (forward-line 1) (point))))
330 ;; Beginning of the article.
335 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
336 (if leave-delim (progn (forward-line 1) (point))
337 (match-beginning 0)))
340 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
342 (if (and (not (bobp)) leave-delim)
343 (progn (forward-line -2) (point))
344 (match-beginning 0)))
346 (goto-char (point-min))
347 ;; Only delete the article if no other groups owns it as well.
348 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
349 (delete-region (point-min) (point-max))))))
351 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
352 (if (or (not nnbabyl-mbox-buffer)
353 (not (buffer-name nnbabyl-mbox-buffer)))
355 (nnbabyl-read-mbox)))
356 (if (not nnbabyl-group-alist)
357 (setq nnbabyl-group-alist (nnmail-get-active)))
359 (if (assoc newsgroup nnbabyl-group-alist)
360 (setq nnbabyl-current-group newsgroup)
361 (setq nnbabyl-status-string "No such group in file")
364 (defun nnbabyl-article-string (article)
365 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
366 (int-to-string article)))
368 (defun nnbabyl-save-mail ()
369 ;; Called narrowed to an article.
370 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
371 (nnmail-insert-lines)
372 (nnmail-insert-xref group-art)
373 (nnbabyl-insert-newsgroup-line group-art)))
375 (defun nnbabyl-insert-newsgroup-line (group-art)
377 (goto-char (point-min))
378 (or (looking-at "\^_")
379 (insert "\^_\^L\n0, unseen,,\n*** EOOH ***\n"))
380 (while (looking-at "From ")
381 (replace-match "Mail-from: ")
383 (if (search-forward "\n\n" nil t)
387 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
388 (car (car group-art)) (cdr (car group-art))
389 (current-time-string)))
390 (setq group-art (cdr group-art)))))))
392 (defun nnbabyl-active-number (group)
393 ;; Find the next article number in GROUP.
394 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
395 (setcdr active (1+ (cdr active)))
398 (defun nnbabyl-read-mbox ()
399 (nnbabyl-request-list)
400 (setq nnbabyl-group-alist (nnmail-get-active))
401 (if (not (file-exists-p nnbabyl-mbox-file))
402 (write-region 1 1 nnbabyl-mbox-file t 'nomesg))
403 (if (and nnbabyl-mbox-buffer
404 (get-buffer nnbabyl-mbox-buffer)
405 (buffer-name nnbabyl-mbox-buffer)
407 (set-buffer nnbabyl-mbox-buffer)
408 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
411 (let ((delim (concat "^" nnbabyl-mail-delimiter))
412 (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
413 (create-file-buffer nnbabyl-mbox-file)))
415 (set-buffer (setq nnbabyl-mbox-buffer buf))
416 (buffer-disable-undo (current-buffer))
418 (insert-file-contents nnbabyl-mbox-file)
419 (setq buffer-file-name nnbabyl-mbox-file)
420 (set-buffer-modified-p nil)
422 (goto-char (point-min))
423 (while (re-search-forward delim nil t)
424 (setq start (match-beginning 0))
426 (save-excursion (re-search-forward delim nil t))
428 "\nX-Gnus-Newsgroup: "
430 (setq end (or (and (re-search-forward delim nil t)
438 (narrow-to-region start end)
439 (nnbabyl-save-mail))))))
441 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
443 (defun nnbabyl-get-new-mail ()
446 (if (and nnmail-spool-file nnbabyl-get-new-mail
447 (file-exists-p nnmail-spool-file)
448 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
450 (and gnus-verbose-backends
451 (message "nnbabyl: Reading incoming mail..."))
453 (nnmail-move-inbox nnmail-spool-file
454 (concat nnbabyl-mbox-file "-Incoming")))
456 (let ((in-buf (nnmail-split-incoming
457 incoming 'nnbabyl-save-mail t)))
458 (set-buffer nnbabyl-mbox-buffer)
459 (goto-char (point-max))
460 (search-backward "\n\^_" nil t)
461 (insert-buffer-substring in-buf)
462 (kill-buffer in-buf)))
463 (run-hooks 'nnmail-read-incoming-hook)
464 (and gnus-verbose-backends
465 (message "nnbabyl: Reading incoming mail...done"))))
466 (and (buffer-modified-p nnbabyl-mbox-buffer)
468 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
469 (set-buffer nnbabyl-mbox-buffer)
471 ;; (if incoming (delete-file incoming))
476 ;;; nnbabyl.el ends here