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, nnml will check the incoming mail file and split the mail.")
46 (defvar nnbabyl-mail-delimiter "\^_")
48 (defconst nnbabyl-version "nnbabyl 0.1"
51 (defvar nnbabyl-current-group nil
52 "Current nnbabyl news group directory.")
54 (defconst nnbabyl-mbox-buffer " *nnbabyl mbox buffer*")
56 (defvar nnbabyl-status-string "")
58 (defvar nnbabyl-group-alist nil)
60 ;;; Interface functions
62 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server)
64 (set-buffer nntp-server-buffer)
67 (number (length sequence))
69 beg article art-string start stop)
70 (nnbabyl-possibly-change-newsgroup newsgroup)
72 (setq article (car sequence))
73 (setq art-string (nnbabyl-article-string article))
74 (set-buffer nnbabyl-mbox-buffer)
75 (if (or (search-forward art-string nil t)
77 (search-forward art-string nil t)))
82 (concat "^" nnbabyl-mail-delimiter) nil t)
83 (while (and (not (looking-at ".+:"))
84 (zerop (forward-line 1))))
86 (search-forward "\n\n" nil t)
87 (setq stop (1- (point)))
88 (set-buffer nntp-server-buffer)
89 (insert (format "221 %d Article retrieved.\n" article))
91 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
92 (goto-char (point-max))
94 (setq sequence (cdr sequence))
95 (setq count (1+ count))
96 (and (numberp nnmail-large-newsgroup)
97 (> number nnmail-large-newsgroup)
100 (message "nnbabyl: Receiving headers... %d%%"
101 (/ (* count 100) number))))
103 (and (numberp nnmail-large-newsgroup)
104 (> number nnmail-large-newsgroup)
105 gnus-verbose-backends
106 (message "nnbabyl: Receiving headers... done"))
108 ;; Fold continuation lines.
110 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
111 (replace-match " " t t))
114 (defun nnbabyl-open-server (host &optional service)
115 (setq nnbabyl-status-string "")
116 (setq nnbabyl-group-alist nil)
117 (nnheader-init-server-buffer))
119 (defun nnbabyl-close-server (&optional server)
122 (defun nnbabyl-server-opened (&optional server)
123 (and nntp-server-buffer
124 (get-buffer nntp-server-buffer)))
126 (defun nnbabyl-status-message (&optional server)
127 nnbabyl-status-string)
129 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
130 (nnbabyl-possibly-change-newsgroup newsgroup)
131 (if (stringp article)
134 (set-buffer nnbabyl-mbox-buffer)
136 (if (search-forward (nnbabyl-article-string article) nil t)
138 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
139 (while (and (not (looking-at ".+:"))
140 (zerop (forward-line 1))))
142 (or (and (re-search-forward
143 (concat "^" nnbabyl-mail-delimiter) nil t)
145 (goto-char (point-max)))
147 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
148 (set-buffer nntp-server-buffer)
150 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
151 (goto-char (point-min))
152 (if (search-forward "\n*** EOOH ***" nil t)
154 (delete-region (progn (beginning-of-line) (point))
155 (or (search-forward "\n\n" nil t)
159 (defun nnbabyl-request-group (group &optional server dont-check)
161 (if (nnbabyl-possibly-change-newsgroup group)
164 (nnbabyl-get-new-mail)
166 (set-buffer nntp-server-buffer)
168 (let ((active (assoc group nnbabyl-group-alist)))
169 (insert (format "211 %d %d %d %s\n"
170 (1+ (- (cdr (car (cdr active)))
171 (car (car (cdr active)))))
172 (car (car (cdr active)))
173 (cdr (car (cdr active)))
177 (defun nnbabyl-close-group (group &optional server)
180 (defun nnbabyl-request-list (&optional server)
181 (if server (nnbabyl-get-new-mail))
182 (nnmail-find-file nnbabyl-active-file))
184 (defun nnbabyl-request-newgroups (date &optional server)
185 (nnbabyl-request-list server))
187 (defun nnbabyl-request-list-newsgroups (&optional server)
188 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
191 (defun nnbabyl-request-post (&optional server)
192 (mail-send-and-exit nil))
194 (fset 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
196 (defun nnbabyl-request-expire-articles (articles newsgroup &optional server force)
197 (nnbabyl-possibly-change-newsgroup newsgroup)
198 (let* ((days (or (and nnmail-expiry-wait-function
199 (funcall nnmail-expiry-wait-function newsgroup))
203 (set-buffer nnbabyl-mbox-buffer)
206 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
208 (> (nnmail-days-between
209 (current-time-string)
211 (point) (progn (end-of-line) (point))))
214 (and gnus-verbose-backends
215 (message "Deleting: %s" (car articles)))
216 (nnbabyl-delete-mail))
217 (setq rest (cons (car articles) rest))))
218 (setq articles (cdr articles)))
220 ;; Find the lowest active article in this group.
221 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
222 (goto-char (point-min))
223 (while (not (search-forward
224 (nnbabyl-article-string (car active)) nil t))
225 (setcar (car active) (1+ (car active)))
226 (goto-char (point-min))))
227 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
230 (defun nnbabyl-request-move-article
231 (article group server accept-form &optional last)
232 (nnbabyl-possibly-change-newsgroup group)
233 (let ((buf (get-buffer-create " *nnbabyl move*"))
236 (nnbabyl-request-article article group server)
239 (insert-buffer-substring nntp-server-buffer)
240 (goto-char (point-min))
241 (if (re-search-forward
243 (save-excursion (search-forward "\n\n" nil t) (point)) t)
244 (delete-region (progn (beginning-of-line) (point))
245 (progn (forward-line 1) (point))))
246 (setq result (eval accept-form))
247 (kill-buffer (current-buffer))
250 (set-buffer nnbabyl-mbox-buffer)
252 (if (search-forward (nnbabyl-article-string article) nil t)
253 (nnbabyl-delete-mail))
254 (and last (save-buffer))))
257 (defun nnbabyl-request-accept-article (group &optional last)
258 (let ((buf (current-buffer))
261 (setq nnbabyl-group-alist (nnmail-get-active))
263 (set-buffer nnbabyl-mbox-buffer)
264 (setq beg (goto-char (point-max)))
265 (insert-buffer-substring buf)
269 (search-forward "\n\n" nil t)
272 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
273 (delete-region (point) (progn (forward-line 1) (point)))))
274 (setq result (nnbabyl-insert-newsgroup-line group)))
275 (setq result (nnbabyl-save-mail)))
276 (and last (save-buffer))
278 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
281 (defun nnbabyl-request-replace-article (article group buffer)
282 (nnbabyl-possibly-change-newsgroup group)
284 (set-buffer nnbabyl-mbox-buffer)
286 (if (not (search-forward (nnbabyl-article-string article) nil t))
288 (nnbabyl-delete-mail t t)
289 (insert-buffer-substring buffer)
294 ;;; Low-Level Interface
296 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
297 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
299 (defun nnbabyl-delete-mail (&optional force leave-delim)
300 ;; Delete the current X-Gnus-Newsgroup line.
303 (progn (beginning-of-line) (point))
304 (progn (forward-line 1) (point))))
305 ;; Beginning of the article.
310 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
311 (if leave-delim (progn (forward-line 1) (point))
312 (match-beginning 0)))
315 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
317 (if (and (not (bobp)) leave-delim)
318 (progn (forward-line -2) (point))
319 (match-beginning 0)))
321 (goto-char (point-min))
322 ;; Only delete the article if no other groups owns it as well.
323 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
324 (delete-region (point-min) (point-max))))))
326 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
327 (if (or (not nnbabyl-mbox-buffer)
328 (not (buffer-name nnbabyl-mbox-buffer)))
330 (nnbabyl-read-mbox)))
331 (if (not nnbabyl-group-alist)
332 (setq nnbabyl-group-alist (nnmail-get-active)))
334 (if (assoc newsgroup nnbabyl-group-alist)
335 (setq nnbabyl-current-group newsgroup)
336 (setq nnbabyl-status-string "No such group in file")
339 (defun nnbabyl-article-string (article)
340 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
341 (int-to-string article)))
343 (defun nnbabyl-save-mail ()
344 ;; Called narrowed to an article.
345 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
346 (nnmail-insert-lines)
347 (nnmail-insert-xref group-art)
348 (nnbabyl-insert-newsgroup-line group-art)))
350 (defun nnbabyl-insert-newsgroup-line (group-art)
352 (goto-char (point-min))
353 (or (looking-at "\^_")
354 (insert "\^_\^L\n0, unseen,,\n*** EOOH ***\n"))
355 (while (looking-at "From ")
356 (replace-match "Mail-from: ")
358 (if (search-forward "\n\n" nil t)
362 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
363 (car (car group-art)) (cdr (car group-art))
364 (current-time-string)))
365 (setq group-art (cdr group-art)))))))
367 (defun nnbabyl-active-number (group)
368 ;; Find the next article number in GROUP.
369 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
370 (setcdr active (1+ (cdr active)))
373 (defun nnbabyl-read-mbox ()
374 (nnbabyl-request-list)
375 (setq nnbabyl-group-alist (nnmail-get-active))
376 (if (not (file-exists-p nnbabyl-mbox-file))
377 (write-region 1 1 nnbabyl-mbox-file t 'nomesg))
378 (if (and nnbabyl-mbox-buffer
379 (get-buffer nnbabyl-mbox-buffer)
380 (buffer-name nnbabyl-mbox-buffer)
382 (set-buffer nnbabyl-mbox-buffer)
383 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
386 (let ((delim (concat "^" nnbabyl-mail-delimiter))
387 (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
388 (create-file-buffer nnbabyl-mbox-file)))
390 (set-buffer (setq nnbabyl-mbox-buffer buf))
391 (buffer-disable-undo (current-buffer))
393 (insert-file-contents nnbabyl-mbox-file)
394 (setq buffer-file-name nnbabyl-mbox-file)
395 (set-buffer-modified-p nil)
397 (goto-char (point-min))
398 (while (re-search-forward delim nil t)
399 (setq start (match-beginning 0))
401 (save-excursion (re-search-forward delim nil t))
403 "\nX-Gnus-Newsgroup: "
405 (setq end (or (and (re-search-forward delim nil t)
413 (narrow-to-region start end)
414 (nnbabyl-save-mail))))))
416 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
418 (defun nnbabyl-get-new-mail ()
421 (if (and nnmail-spool-file nnbabyl-get-new-mail
422 (file-exists-p nnmail-spool-file)
423 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
425 (and gnus-verbose-backends
426 (message "nnbabyl: Reading incoming mail..."))
428 (nnmail-move-inbox nnmail-spool-file
429 (concat nnbabyl-mbox-file "-Incoming")))
431 (let ((in-buf (nnmail-split-incoming
432 incoming 'nnbabyl-save-mail t)))
433 (set-buffer nnbabyl-mbox-buffer)
434 (goto-char (point-max))
435 (search-backward "\n\^_" nil t)
436 (insert-buffer-substring in-buf)
437 (kill-buffer in-buf)))
438 (run-hooks 'nnmail-read-incoming-hook)
439 (and gnus-verbose-backends
440 (message "nnbabyl: Reading incoming mail...done"))))
441 (and (buffer-modified-p nnbabyl-mbox-buffer)
443 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
444 (set-buffer nnbabyl-mbox-buffer)
446 ;; (if incoming (delete-file incoming))
451 ;;; nnbabyl.el ends here