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 1.0"
54 (defvar nnbabyl-mbox-buffer nil)
55 (defvar nnbabyl-current-group nil)
56 (defvar nnbabyl-status-string "")
57 (defvar nnbabyl-group-alist nil)
58 (defvar nnbabyl-active-timestamp nil)
62 (defvar nnbabyl-current-server nil)
63 (defvar nnbabyl-server-alist nil)
64 (defvar nnbabyl-server-variables
66 (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
67 (list 'nnbabyl-active-file nnbabyl-active-file)
68 (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
69 '(nnbabyl-current-group nil)
70 '(nnbabyl-status-string "")
71 '(nnbabyl-group-alist nil)))
75 ;;; Interface functions
77 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server)
79 (set-buffer nntp-server-buffer)
81 (let ((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 (set-buffer nntp-server-buffer)
121 (goto-char (point-min))
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 nnbabyl-mbox-buffer)
151 (buffer-name nntp-server-buffer)))
153 (defun nnbabyl-status-message (&optional server)
154 nnbabyl-status-string)
156 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
157 (nnbabyl-possibly-change-newsgroup newsgroup)
158 (if (stringp article)
161 (set-buffer nnbabyl-mbox-buffer)
162 (goto-char (point-min))
163 (if (search-forward (nnbabyl-article-string article) nil t)
165 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
166 (while (and (not (looking-at ".+:"))
167 (zerop (forward-line 1))))
169 (or (and (re-search-forward
170 (concat "^" nnbabyl-mail-delimiter) nil t)
172 (goto-char (point-max)))
174 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
175 (set-buffer nntp-server-buffer)
177 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
178 (goto-char (point-min))
179 (if (search-forward "\n*** EOOH ***" nil t)
181 (delete-region (progn (beginning-of-line) (point))
182 (or (search-forward "\n\n" nil t)
186 (defun nnbabyl-request-group (group &optional server dont-check)
188 (if (nnbabyl-possibly-change-newsgroup group)
191 (nnbabyl-get-new-mail group)
193 (set-buffer nntp-server-buffer)
195 (let ((active (assoc group nnbabyl-group-alist)))
196 (insert (format "211 %d %d %d %s\n"
197 (1+ (- (cdr (car (cdr active)))
198 (car (car (cdr active)))))
199 (car (car (cdr active)))
200 (cdr (car (cdr active)))
204 (defun nnbabyl-close-group (group &optional server)
207 (defun nnbabyl-request-create-group (group &optional server)
208 (nnmail-activate 'nnbabyl)
209 (or (assoc group nnbabyl-group-alist)
211 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 0)))
212 nnbabyl-group-alist))
213 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))
216 (defun nnbabyl-request-list (&optional server)
217 (if server (nnbabyl-get-new-mail))
219 (or (nnmail-find-file nnbabyl-active-file)
221 (setq nnbabyl-group-alist (nnmail-get-active))
222 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
223 (nnmail-find-file nnbabyl-active-file)))))
225 (defun nnbabyl-request-newgroups (date &optional server)
226 (nnbabyl-request-list server))
228 (defun nnbabyl-request-list-newsgroups (&optional server)
229 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
232 (defun nnbabyl-request-post (&optional server)
233 (mail-send-and-exit nil))
235 (defalias 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
237 (defun nnbabyl-request-expire-articles
238 (articles newsgroup &optional server force)
239 (nnbabyl-possibly-change-newsgroup newsgroup)
240 (let* ((days (or (and nnmail-expiry-wait-function
241 (funcall nnmail-expiry-wait-function newsgroup))
245 (nnmail-activate 'nnbabyl)
248 (set-buffer nnbabyl-mbox-buffer)
249 (set-text-properties (point-min) (point-max) nil)
250 (while (and articles is-old)
251 (goto-char (point-min))
252 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
255 (> (nnmail-days-between
256 (current-time-string)
258 (point) (progn (end-of-line) (point))))
261 (and gnus-verbose-backends
262 (message "Deleting article %s..." (car articles)))
263 (nnbabyl-delete-mail))
264 (setq rest (cons (car articles) rest))))
265 (setq articles (cdr articles)))
267 ;; Find the lowest active article in this group.
268 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
269 (goto-char (point-min))
270 (while (and (not (search-forward
271 (nnbabyl-article-string (car active)) nil t))
272 (<= (car active) (cdr active)))
273 (setcar active (1+ (car active)))
274 (goto-char (point-min))))
275 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
276 (nconc rest articles))))
278 (defun nnbabyl-request-move-article
279 (article group server accept-form &optional last)
280 (nnbabyl-possibly-change-newsgroup group)
281 (let ((buf (get-buffer-create " *nnbabyl move*"))
284 (nnbabyl-request-article article group server)
287 (insert-buffer-substring nntp-server-buffer)
288 (goto-char (point-min))
289 (if (re-search-forward
291 (save-excursion (search-forward "\n\n" nil t) (point)) t)
292 (delete-region (progn (beginning-of-line) (point))
293 (progn (forward-line 1) (point))))
294 (setq result (eval accept-form))
295 (kill-buffer (current-buffer))
298 (set-buffer nnbabyl-mbox-buffer)
299 (goto-char (point-min))
300 (if (search-forward (nnbabyl-article-string article) nil t)
301 (nnbabyl-delete-mail))
302 (and last (save-buffer))))
305 (defun nnbabyl-request-accept-article (group &optional last)
306 (let ((buf (current-buffer))
309 (nnmail-activate 'nnbabyl)
311 (goto-char (point-min))
312 (search-forward "\n\n" nil t)
315 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
316 (delete-region (point) (progn (forward-line 1) (point)))))
317 (let ((nnmail-split-methods
318 (if (stringp group) (list (list group ""))
319 nnmail-split-methods)))
320 (setq result (car (nnbabyl-save-mail))))
321 (set-buffer nnbabyl-mbox-buffer)
322 (goto-char (point-max))
323 (search-backward "\n\^_")
324 (goto-char (match-end 0))
329 nnbabyl-group-alist nnbabyl-active-file)))
332 (defun nnbabyl-request-replace-article (article group buffer)
333 (nnbabyl-possibly-change-newsgroup group)
335 (set-buffer nnbabyl-mbox-buffer)
336 (goto-char (point-min))
337 (if (not (search-forward (nnbabyl-article-string article) nil t))
339 (nnbabyl-delete-mail t t)
340 (insert-buffer-substring buffer)
345 ;;; Low-Level Interface
347 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
348 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
350 (defun nnbabyl-delete-mail (&optional force leave-delim)
351 ;; Delete the current X-Gnus-Newsgroup line.
354 (progn (beginning-of-line) (point))
355 (progn (forward-line 1) (point))))
356 ;; Beginning of the article.
362 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
363 (if leave-delim (progn (forward-line 1) (point))
364 (match-beginning 0)))
367 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
369 (if (and (not (bobp)) leave-delim)
370 (progn (forward-line -2) (point))
371 (match-beginning 0)))
373 (goto-char (point-min))
374 ;; Only delete the article if no other groups owns it as well.
375 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
376 (delete-region (point-min) (point-max))))))
378 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
379 (if (or (not nnbabyl-mbox-buffer)
380 (not (buffer-name nnbabyl-mbox-buffer)))
381 (save-excursion (nnbabyl-read-mbox)))
382 (or nnbabyl-group-alist
383 (nnmail-activate 'nnbabyl))
385 (if (assoc newsgroup nnbabyl-group-alist)
386 (setq nnbabyl-current-group newsgroup)
387 (setq nnbabyl-status-string "No such group in file")
390 (defun nnbabyl-article-string (article)
391 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
392 (int-to-string article) " "))
394 (defun nnbabyl-insert-lines ()
395 "Insert how many lines and chars there are in the body of the mail."
398 (goto-char (point-min))
399 (if (search-forward "\n\n" nil t)
401 ;; There may be an EOOH line here...
402 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
403 (search-forward "\n\n" nil t))
404 (setq chars (- (point-max) (point)))
405 (setq lines (- (count-lines (point) (point-max)) 1))
406 ;; Move back to the end of the headers.
407 (goto-char (point-min))
408 (search-forward "\n\n" nil t)
411 (if (re-search-backward "^Lines: " nil t)
412 (delete-region (point) (progn (forward-line 1) (point)))))
413 (insert (format "Lines: %d\n" lines))
416 (defun nnbabyl-save-mail ()
417 ;; Called narrowed to an article.
418 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
419 (nnbabyl-insert-lines)
420 (nnmail-insert-xref group-art)
421 (nnbabyl-insert-newsgroup-line group-art)
422 (run-hooks 'nnbabyl-prepare-save-mail-hook)
425 (defun nnbabyl-insert-newsgroup-line (group-art)
427 (goto-char (point-min))
428 (while (looking-at "From ")
429 (replace-match "Mail-from: From " t t)
431 ;; If there is a C-l at the beginning of the narrowed region, this
432 ;; isn't really a "save", but rather a "scan".
433 (goto-char (point-min))
434 (or (looking-at "\^L")
436 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
437 (goto-char (point-max))
439 (if (search-forward "\n\n" nil t)
443 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
444 (car (car group-art)) (cdr (car group-art))
445 (current-time-string)))
446 (setq group-art (cdr group-art)))))
449 (defun nnbabyl-active-number (group)
450 ;; Find the next article number in GROUP.
451 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
453 (setcdr active (1+ (cdr active)))
454 ;; This group is new, so we create a new entry for it.
455 ;; This might be a bit naughty... creating groups on the drop of
456 ;; a hat, but I don't know...
457 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
458 nnbabyl-group-alist)))
461 (defun nnbabyl-read-mbox ()
462 (nnmail-activate 'nnbabyl)
463 (or (file-exists-p nnbabyl-mbox-file)
465 (set-buffer (setq nnbabyl-mbox-buffer
466 (create-file-buffer nnbabyl-mbox-file)))
467 (setq buffer-file-name nnbabyl-mbox-file)
468 (insert "BABYL OPTIONS:\n\n\^_")
469 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
471 (if (and nnbabyl-mbox-buffer
472 (buffer-name nnbabyl-mbox-buffer)
474 (set-buffer nnbabyl-mbox-buffer)
475 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
478 (let ((delim (concat "^" nnbabyl-mail-delimiter))
480 (set-buffer (setq nnbabyl-mbox-buffer
481 (nnheader-find-file-noselect
482 nnbabyl-mbox-file nil 'raw)))
483 (buffer-disable-undo (current-buffer))
485 (setq buffer-read-only nil)
488 (goto-char (point-min))
489 (re-search-forward delim nil t)
490 (setq start (match-end 0))
491 (while (re-search-forward delim nil t)
492 (setq end (match-end 0))
493 (or (search-backward "\nX-Gnus-Newsgroup: " start t)
499 (narrow-to-region start end)
501 (setq end (point-max))))))
502 (goto-char (setq start end)))
503 (and (buffer-modified-p (current-buffer)) (save-buffer))
504 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
506 (defun nnbabyl-remove-incoming-delims ()
507 (goto-char (point-min))
508 (while (search-forward "\^_" nil t)
509 (replace-match "?" t t)))
511 (defun nnbabyl-get-new-mail (&optional group)
512 "Read new incoming mail."
513 (let* ((spools (nnmail-get-spool-files group))
517 (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
519 ;; We go through all the existing spool files and split the
523 (file-exists-p (car spools))
524 (> (nth 7 (file-attributes (car spools))) 0)
526 (and gnus-verbose-backends
527 (message "nnbabyl: Reading incoming mail..."))
528 (if (not (setq incoming
531 (concat nnbabyl-mbox-file "-Incoming"))))
533 (setq incomings (cons incoming incomings))
535 (setq group (nnmail-get-split-group (car spools) group-in))
536 (let* ((nnmail-prepare-incoming-hook
537 (cons 'nnbabyl-remove-incoming-delims
538 nnmail-prepare-incoming-hook))
540 (setq in-buf (nnmail-split-incoming
541 incoming 'nnbabyl-save-mail t group))
543 (goto-char (point-min))
544 (while (search-forward "\n\^_\n" nil t)
546 (set-buffer nnbabyl-mbox-buffer)
547 (goto-char (point-max))
548 (search-backward "\n\^_" nil t)
549 (goto-char (match-end 0))
550 (insert-buffer-substring in-buf)
551 (kill-buffer in-buf))))))
552 (setq spools (cdr spools)))
553 ;; If we did indeed read any incoming spools, we save all info.
554 (and (buffer-modified-p nnbabyl-mbox-buffer)
556 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
557 (set-buffer nnbabyl-mbox-buffer)
559 (if incomings (run-hooks 'nnmail-read-incoming-hook))
561 (setq incoming (car incomings))
562 (and nnmail-delete-incoming
563 (file-exists-p incoming)
564 (file-writable-p incoming)
565 (delete-file incoming))
566 (setq incomings (cdr incomings))))))
570 ;;; nnbabyl.el ends here