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 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.")
46 (defvar nnbabyl-mail-delimiter "\^_")
48 (defconst nnbabyl-version "nnbabyl 0.1"
51 (defvar nnbabyl-mbox-buffer nil)
52 (defvar nnbabyl-current-group nil)
53 (defvar nnbabyl-status-string "")
54 (defvar nnbabyl-group-alist nil)
58 (defvar nnbabyl-current-server nil)
59 (defvar nnbabyl-server-alist nil)
60 (defvar nnbabyl-server-variables
62 (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
63 (list 'nnbabyl-active-file nnbabyl-active-file)
64 (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
65 '(nnbabyl-current-group nil)
66 '(nnbabyl-status-string "")
67 '(nnbabyl-group-alist nil)))
71 ;;; Interface functions
73 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server)
75 (set-buffer nntp-server-buffer)
78 (number (length sequence))
80 beg article art-string start stop)
81 (nnbabyl-possibly-change-newsgroup newsgroup)
83 (setq article (car sequence))
84 (setq art-string (nnbabyl-article-string article))
85 (set-buffer nnbabyl-mbox-buffer)
86 (if (or (search-forward art-string nil t)
87 (progn (goto-char (point-min))
88 (search-forward art-string nil t)))
93 (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 (format "221 %d Article retrieved.\n" article))
102 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
103 (goto-char (point-max))
105 (setq sequence (cdr sequence))
106 (setq count (1+ count))
107 (and (numberp nnmail-large-newsgroup)
108 (> number nnmail-large-newsgroup)
110 gnus-verbose-backends
111 (message "nnbabyl: Receiving headers... %d%%"
112 (/ (* count 100) number))))
114 (and (numberp nnmail-large-newsgroup)
115 (> number nnmail-large-newsgroup)
116 gnus-verbose-backends
117 (message "nnbabyl: Receiving headers... done"))
119 ;; Fold continuation lines.
120 (goto-char (point-min))
121 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
122 (replace-match " " t t))
125 (defun nnbabyl-open-server (server &optional defs)
126 (nnheader-init-server-buffer)
127 (if (equal server nnbabyl-current-server)
129 (if nnbabyl-current-server
130 (setq nnbabyl-server-alist
131 (cons (list nnbabyl-current-server
132 (nnheader-save-variables nnbabyl-server-variables))
133 nnbabyl-server-alist)))
134 (let ((state (assoc server nnbabyl-server-alist)))
137 (nnheader-restore-variables (nth 1 state))
138 (setq nnbabyl-server-alist (delq state nnbabyl-server-alist)))
139 (nnheader-set-init-variables nnbabyl-server-variables defs)))
140 (setq nnbabyl-current-server server)))
142 (defun nnbabyl-close-server (&optional server)
145 (defun nnbabyl-server-opened (&optional server)
146 (and (equal server nnbabyl-current-server)
148 (buffer-name nntp-server-buffer)))
150 (defun nnbabyl-status-message (&optional server)
151 nnbabyl-status-string)
153 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
154 (nnbabyl-possibly-change-newsgroup newsgroup)
155 (if (stringp article)
158 (set-buffer nnbabyl-mbox-buffer)
159 (goto-char (point-min))
160 (if (search-forward (nnbabyl-article-string article) nil t)
162 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
163 (while (and (not (looking-at ".+:"))
164 (zerop (forward-line 1))))
166 (or (and (re-search-forward
167 (concat "^" nnbabyl-mail-delimiter) nil t)
169 (goto-char (point-max)))
171 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
172 (set-buffer nntp-server-buffer)
174 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
175 (goto-char (point-min))
176 (if (search-forward "\n*** EOOH ***" nil t)
178 (delete-region (progn (beginning-of-line) (point))
179 (or (search-forward "\n\n" nil t)
183 (defun nnbabyl-request-group (group &optional server dont-check)
185 (if (nnbabyl-possibly-change-newsgroup group)
188 (nnbabyl-get-new-mail)
190 (set-buffer nntp-server-buffer)
192 (let ((active (assoc group nnbabyl-group-alist)))
193 (insert (format "211 %d %d %d %s\n"
194 (1+ (- (cdr (car (cdr active)))
195 (car (car (cdr active)))))
196 (car (car (cdr active)))
197 (cdr (car (cdr active)))
201 (defun nnbabyl-close-group (group &optional server)
204 (defun nnbabyl-request-list (&optional server)
205 (if server (nnbabyl-get-new-mail))
207 (or (nnmail-find-file nnbabyl-active-file)
209 (setq nnbabyl-group-alist (nnmail-get-active))
210 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
211 (nnmail-find-file nnbabyl-active-file)))))
213 (defun nnbabyl-request-newgroups (date &optional server)
214 (nnbabyl-request-list server))
216 (defun nnbabyl-request-list-newsgroups (&optional server)
217 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
220 (defun nnbabyl-request-post (&optional server)
221 (mail-send-and-exit nil))
223 (fset 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
225 (defun nnbabyl-request-expire-articles (articles newsgroup &optional server force)
226 (nnbabyl-possibly-change-newsgroup newsgroup)
227 (let* ((days (or (and nnmail-expiry-wait-function
228 (funcall nnmail-expiry-wait-function newsgroup))
232 (set-buffer nnbabyl-mbox-buffer)
234 (goto-char (point-min))
235 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
237 (> (nnmail-days-between
238 (current-time-string)
240 (point) (progn (end-of-line) (point))))
243 (and gnus-verbose-backends
244 (message "Deleting: %s" (car articles)))
245 (nnbabyl-delete-mail))
246 (setq rest (cons (car articles) rest))))
247 (setq articles (cdr articles)))
249 ;; Find the lowest active article in this group.
250 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
251 (goto-char (point-min))
252 (while (not (search-forward
253 (nnbabyl-article-string (car active)) nil t))
254 (setcar active (1+ (car active)))
255 (goto-char (point-min))))
256 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
259 (defun nnbabyl-request-move-article
260 (article group server accept-form &optional last)
261 (nnbabyl-possibly-change-newsgroup group)
262 (let ((buf (get-buffer-create " *nnbabyl move*"))
265 (nnbabyl-request-article article group server)
268 (insert-buffer-substring nntp-server-buffer)
269 (goto-char (point-min))
270 (if (re-search-forward
272 (save-excursion (search-forward "\n\n" nil t) (point)) t)
273 (delete-region (progn (beginning-of-line) (point))
274 (progn (forward-line 1) (point))))
275 (setq result (eval accept-form))
276 (kill-buffer (current-buffer))
279 (set-buffer nnbabyl-mbox-buffer)
280 (goto-char (point-min))
281 (if (search-forward (nnbabyl-article-string article) nil t)
282 (nnbabyl-delete-mail))
283 (and last (save-buffer))))
286 (defun nnbabyl-request-accept-article (group &optional last)
287 (let ((buf (current-buffer))
290 (nnbabyl-request-list)
291 (setq nnbabyl-group-alist (nnmail-get-active))
293 (goto-char (point-min))
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 (let ((nnmail-split-methods
300 (if (stringp group) (list (list group ""))
301 nnmail-split-methods)))
302 (setq result (nnbabyl-save-mail)))
303 (set-buffer nnbabyl-mbox-buffer)
304 (goto-char (point-max))
305 (search-backward "\n\^_")
306 (goto-char (match-end 0))
311 nnbabyl-group-alist nnbabyl-active-file)))
314 (defun nnbabyl-request-replace-article (article group buffer)
315 (nnbabyl-possibly-change-newsgroup group)
317 (set-buffer nnbabyl-mbox-buffer)
318 (goto-char (point-min))
319 (if (not (search-forward (nnbabyl-article-string article) nil t))
321 (nnbabyl-delete-mail t t)
322 (insert-buffer-substring buffer)
327 ;;; Low-Level Interface
329 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
330 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
332 (defun nnbabyl-delete-mail (&optional force leave-delim)
333 ;; Delete the current X-Gnus-Newsgroup line.
336 (progn (beginning-of-line) (point))
337 (progn (forward-line 1) (point))))
338 ;; Beginning of the article.
343 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
344 (if leave-delim (progn (forward-line 1) (point))
345 (match-beginning 0)))
348 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
350 (if (and (not (bobp)) leave-delim)
351 (progn (forward-line -2) (point))
352 (match-beginning 0)))
354 (goto-char (point-min))
355 ;; Only delete the article if no other groups owns it as well.
356 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
357 (delete-region (point-min) (point-max))))))
359 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
360 (if (or (not nnbabyl-mbox-buffer)
361 (not (buffer-name nnbabyl-mbox-buffer)))
362 (save-excursion (nnbabyl-read-mbox)))
363 (or nnbabyl-group-alist
365 (nnbabyl-request-list)
366 (setq nnbabyl-group-alist (nnmail-get-active))))
368 (if (assoc newsgroup nnbabyl-group-alist)
369 (setq nnbabyl-current-group newsgroup)
370 (setq nnbabyl-status-string "No such group in file")
373 (defun nnbabyl-article-string (article)
374 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
375 (int-to-string article)))
377 (defun nnbabyl-save-mail ()
378 ;; Called narrowed to an article.
379 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
380 (nnmail-insert-lines)
381 (nnmail-insert-xref group-art)
382 (nnbabyl-insert-newsgroup-line group-art)))
384 (defun nnbabyl-insert-newsgroup-line (group-art)
386 (goto-char (point-min))
387 ;; If there is a C-l at the beginning of the narrowed region, this
388 ;; isn't really a "save", but rather a "scan".
389 (or (looking-at "\^L")
391 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
392 (goto-char (point-max))
394 (while (looking-at "From ")
395 (replace-match "Mail-from: " t t)
397 (if (search-forward "\n\n" nil t)
401 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
402 (car (car group-art)) (cdr (car group-art))
403 (current-time-string)))
404 (setq group-art (cdr group-art)))))
407 (defun nnbabyl-active-number (group)
408 ;; Find the next article number in GROUP.
409 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
410 (setcdr active (1+ (cdr active)))
413 (defun nnbabyl-read-mbox ()
414 (nnbabyl-request-list)
415 (setq nnbabyl-group-alist (nnmail-get-active))
416 (or (file-exists-p nnbabyl-mbox-file)
418 (set-buffer (setq nnbabyl-mbox-buffer
419 (create-file-buffer nnbabyl-mbox-file)))
420 (setq buffer-file-name nnbabyl-mbox-file)
421 (insert "BABYL OPTIONS:\n\n\^_")
422 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
423 (if (and nnbabyl-mbox-buffer
424 (get-buffer nnbabyl-mbox-buffer)
425 (buffer-name nnbabyl-mbox-buffer)
427 (set-buffer nnbabyl-mbox-buffer)
428 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
431 (let ((delim (concat "^" nnbabyl-mail-delimiter))
432 (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
433 (create-file-buffer nnbabyl-mbox-file)))
435 (set-buffer (setq nnbabyl-mbox-buffer buf))
436 (buffer-disable-undo (current-buffer))
438 (insert-file-contents nnbabyl-mbox-file)
439 (setq buffer-file-name nnbabyl-mbox-file)
440 (set-buffer-modified-p nil)
442 (goto-char (point-min))
443 (while (re-search-forward delim nil t)
444 (setq start (match-beginning 0))
446 (save-excursion (re-search-forward delim nil t))
448 "\nX-Gnus-Newsgroup: "
450 (setq end (or (and (re-search-forward delim nil t)
458 (narrow-to-region (1+ start) end)
459 (nnbabyl-save-mail))))))
460 (and (buffer-modified-p (current-buffer)) (save-buffer))
461 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
463 (defun nnbabyl-get-new-mail ()
464 "Read new incoming mail."
465 (let ((spools (if (listp nnmail-spool-file) nnmail-spool-file
466 (list nnmail-spool-file)))
469 (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
471 ;; We go through all the existing spool files and split the
475 (file-exists-p (car spools))
476 (> (nth 7 (file-attributes (car spools))) 0)
478 (and gnus-verbose-backends
479 (message "nnbabyl: Reading incoming mail..."))
482 (car spools) (concat nnbabyl-mbox-file "-Incoming")))
483 (setq incomings (cons incoming incomings))
485 (let ((in-buf (nnmail-split-incoming
486 incoming 'nnbabyl-save-mail t)))
488 (goto-char (point-min))
489 (while (search-forward "\n\^_\n" nil t)
491 (set-buffer nnbabyl-mbox-buffer)
492 (goto-char (point-max))
493 (search-backward "\n\^_" nil t)
494 (goto-char (match-end 0))
495 (insert-buffer-substring in-buf)
496 (kill-buffer in-buf)))))
497 (setq spools (cdr spools)))
498 ;; If we did indeed read any incoming spools, we save all info.
499 (and (buffer-modified-p nnbabyl-mbox-buffer)
501 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
502 (set-buffer nnbabyl-mbox-buffer)
505 ;; The following has been commented away, just to make sure
506 ;; that nobody ever loses any mail. If you feel safe that
507 ;; nnfolder will never do anything strange, just remove those
508 ;; two semicolons, and avoid having lots of "Incoming*"
510 ;; (and (file-writable-p incoming) (delete-file incoming))
511 (setq incomings (cdr incomings))))))
515 ;;; nnbabyl.el ends here