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-retrieqve-headers (sequence &optional newsgroup server)
78 (set-buffer nntp-server-buffer)
81 (number (length sequence))
83 article art-string start stop)
84 (nnbabyl-possibly-change-newsgroup newsgroup)
85 (if (stringp (car sequence))
88 (setq article (car sequence))
89 (setq art-string (nnbabyl-article-string article))
90 (set-buffer nnbabyl-mbox-buffer)
91 (if (or (search-forward art-string nil t)
92 (search-backward art-string nil t))
94 (re-search-backward (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 "221 " (int-to-string article) " Article retrieved.\n")
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 group)
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 (defalias '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 (car (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-insert-lines ()
378 "Insert how many lines and chars there are in the body of the mail."
381 (goto-char (point-min))
382 (if (search-forward "\n\n" nil t)
384 ;; There may be an EOOH line here...
385 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
386 (search-forward "\n\n" nil t))
387 (setq chars (- (point-max) (point)))
388 (setq lines (- (count-lines (point) (point-max)) 1))
389 ;; Move back to the end of the headers.
390 (goto-char (point-min))
391 (search-forward "\n\n" nil t)
394 (if (re-search-backward "^Lines: " nil t)
395 (delete-region (point) (progn (forward-line 1) (point)))))
396 (insert (format "Lines: %d\n" lines))
399 (defun nnbabyl-save-mail ()
400 ;; Called narrowed to an article.
401 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
402 (nnbabyl-insert-lines)
403 (nnmail-insert-xref group-art)
404 (nnbabyl-insert-newsgroup-line group-art)
405 (run-hooks 'nnbabyl-prepare-save-mail-hook)
408 (defun nnbabyl-insert-newsgroup-line (group-art)
410 (goto-char (point-min))
411 ;; If there is a C-l at the beginning of the narrowed region, this
412 ;; isn't really a "save", but rather a "scan".
413 (while (looking-at "From ")
414 (replace-match "Mail-from: From " t t)
416 (goto-char (point-min))
417 (or (looking-at "\^L")
419 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
420 (goto-char (point-max))
422 (if (search-forward "\n\n" nil t)
426 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
427 (car (car group-art)) (cdr (car group-art))
428 (current-time-string)))
429 (setq group-art (cdr group-art)))))
432 (defun nnbabyl-active-number (group)
433 ;; Find the next article number in GROUP.
434 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
435 (setcdr active (1+ (cdr active)))
438 (defun nnbabyl-read-mbox ()
439 (nnbabyl-request-list)
440 (setq nnbabyl-group-alist (nnmail-get-active))
441 (or (file-exists-p nnbabyl-mbox-file)
443 (set-buffer (setq nnbabyl-mbox-buffer
444 (create-file-buffer nnbabyl-mbox-file)))
445 (setq buffer-file-name nnbabyl-mbox-file)
446 (insert "BABYL OPTIONS:\n\n\^_")
447 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
448 (if (and nnbabyl-mbox-buffer
449 (get-buffer nnbabyl-mbox-buffer)
450 (buffer-name nnbabyl-mbox-buffer)
452 (set-buffer nnbabyl-mbox-buffer)
453 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
456 (let ((delim (concat "^" nnbabyl-mail-delimiter))
457 (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
458 (create-file-buffer nnbabyl-mbox-file)))
460 (set-buffer (setq nnbabyl-mbox-buffer buf))
461 (buffer-disable-undo (current-buffer))
463 (insert-file-contents nnbabyl-mbox-file)
464 (setq buffer-file-name nnbabyl-mbox-file)
465 (set-buffer-modified-p nil)
467 (goto-char (point-min))
468 (while (re-search-forward delim nil t)
469 (setq start (match-beginning 0))
471 (save-excursion (re-search-forward delim nil t))
473 "\nX-Gnus-Newsgroup: "
475 (setq end (or (and (re-search-forward delim nil t)
483 (narrow-to-region (1+ start) end)
484 (nnbabyl-save-mail))))))
485 (and (buffer-modified-p (current-buffer)) (save-buffer))
486 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
488 (defun nnbabyl-get-new-mail (&optional group)
489 "Read new incoming mail."
490 (let ((spools (nnmail-get-spool-files group))
493 (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
495 ;; We go through all the existing spool files and split the
499 (file-exists-p (car spools))
500 (> (nth 7 (file-attributes (car spools))) 0)
502 (and gnus-verbose-backends
503 (message "nnbabyl: Reading incoming mail..."))
506 (car spools) (concat nnbabyl-mbox-file "-Incoming")))
507 (setq incomings (cons incoming incomings))
509 (let ((in-buf (nnmail-split-incoming
510 incoming 'nnbabyl-save-mail t group)))
512 (goto-char (point-min))
513 (while (search-forward "\n\^_\n" nil t)
515 (set-buffer nnbabyl-mbox-buffer)
516 (goto-char (point-max))
517 (search-backward "\n\^_" nil t)
518 (goto-char (match-end 0))
519 (insert-buffer-substring in-buf)
520 (kill-buffer in-buf)))))
521 (setq spools (cdr spools)))
522 ;; If we did indeed read any incoming spools, we save all info.
523 (and (buffer-modified-p nnbabyl-mbox-buffer)
525 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
526 (set-buffer nnbabyl-mbox-buffer)
529 ;; The following has been commented away, just to make sure
530 ;; that nobody ever loses any mail. If you feel safe that
531 ;; nnfolder will never do anything strange, just remove those
532 ;; two semicolons, and avoid having lots of "Incoming*"
534 ;; (and (file-writable-p incoming) (delete-file incoming))
535 (setq incomings (cdr incomings))))))
539 ;;; nnbabyl.el ends here