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)
86 (setq article (car sequence))
87 (setq art-string (nnbabyl-article-string article))
88 (set-buffer nnbabyl-mbox-buffer)
89 (if (or (search-forward art-string nil t)
90 (search-backward art-string nil t))
92 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
93 (while (and (not (looking-at ".+:"))
94 (zerop (forward-line 1))))
96 (search-forward "\n\n" nil t)
97 (setq stop (1- (point)))
98 (set-buffer nntp-server-buffer)
99 (insert "221 " (int-to-string article) " Article retrieved.\n")
100 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
101 (goto-char (point-max))
103 (setq sequence (cdr sequence))
104 (setq count (1+ count))
105 (and (numberp nnmail-large-newsgroup)
106 (> number nnmail-large-newsgroup)
108 gnus-verbose-backends
109 (message "nnbabyl: Receiving headers... %d%%"
110 (/ (* count 100) number))))
112 (and (numberp nnmail-large-newsgroup)
113 (> number nnmail-large-newsgroup)
114 gnus-verbose-backends
115 (message "nnbabyl: Receiving headers...done"))
117 ;; Fold continuation lines.
118 (set-buffer nntp-server-buffer)
119 (goto-char (point-min))
120 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
121 (replace-match " " t t))
124 (defun nnbabyl-open-server (server &optional defs)
125 (nnheader-init-server-buffer)
126 (if (equal server nnbabyl-current-server)
128 (if nnbabyl-current-server
129 (setq nnbabyl-server-alist
130 (cons (list nnbabyl-current-server
131 (nnheader-save-variables nnbabyl-server-variables))
132 nnbabyl-server-alist)))
133 (let ((state (assoc server nnbabyl-server-alist)))
136 (nnheader-restore-variables (nth 1 state))
137 (setq nnbabyl-server-alist (delq state nnbabyl-server-alist)))
138 (nnheader-set-init-variables nnbabyl-server-variables defs)))
139 (setq nnbabyl-current-server server)))
141 (defun nnbabyl-close-server (&optional server)
144 (defun nnbabyl-server-opened (&optional server)
145 (and (equal server nnbabyl-current-server)
147 (buffer-name nnbabyl-mbox-buffer)
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)
157 (set-buffer nnbabyl-mbox-buffer)
158 (goto-char (point-min))
159 (if (search-forward (nnbabyl-article-string article) nil t)
161 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
162 (while (and (not (looking-at ".+:"))
163 (zerop (forward-line 1))))
165 (or (and (re-search-forward
166 (concat "^" nnbabyl-mail-delimiter) nil t)
168 (goto-char (point-max)))
170 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
171 (set-buffer nntp-server-buffer)
173 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
174 (goto-char (point-min))
175 (if (search-forward "\n*** EOOH ***" nil t)
177 (delete-region (progn (beginning-of-line) (point))
178 (or (search-forward "\n\n" nil t)
180 (if (numberp article)
181 (cons nnbabyl-current-group article)
182 (nnbabyl-article-group-number)))))))
184 (defun nnbabyl-request-group (group &optional server dont-check)
186 (if (nnbabyl-possibly-change-newsgroup 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-request-scan (&optional group server)
207 (set-buffer nnbabyl-mbox-buffer)
209 nnbabyl-mbox-file group
212 (let ((in-buf (current-buffer)))
213 (goto-char (point-min))
214 (while (search-forward "\n\^_\n" nil t)
216 (set-buffer nnbabyl-mbox-buffer)
217 (goto-char (point-max))
218 (search-backward "\n\^_" nil t)
219 (goto-char (match-end 0))
220 (insert-buffer-substring in-buf))))))
222 (defun nnbabyl-close-group (group &optional server)
225 (defun nnbabyl-request-create-group (group &optional server)
226 (nnmail-activate 'nnbabyl)
227 (or (assoc group nnbabyl-group-alist)
229 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 0)))
230 nnbabyl-group-alist))
231 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))
234 (defun nnbabyl-request-list (&optional server)
236 (or (nnmail-find-file nnbabyl-active-file)
238 (setq nnbabyl-group-alist (nnmail-get-active))
239 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
240 (nnmail-find-file nnbabyl-active-file)))))
242 (defun nnbabyl-request-newgroups (date &optional server)
243 (nnbabyl-request-list server))
245 (defun nnbabyl-request-list-newsgroups (&optional server)
246 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
249 (defun nnbabyl-request-post (&optional server)
250 (mail-send-and-exit nil))
252 (defalias 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
254 (defun nnbabyl-request-expire-articles
255 (articles newsgroup &optional server force)
256 (nnbabyl-possibly-change-newsgroup newsgroup)
257 (let* ((days (or (and nnmail-expiry-wait-function
258 (funcall nnmail-expiry-wait-function newsgroup))
262 (nnmail-activate 'nnbabyl)
265 (set-buffer nnbabyl-mbox-buffer)
266 (set-text-properties (point-min) (point-max) nil)
267 (while (and articles is-old)
268 (goto-char (point-min))
269 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
272 (> (nnmail-days-between
273 (current-time-string)
275 (point) (progn (end-of-line) (point))))
278 (and gnus-verbose-backends
279 (message "Deleting article %d in %s..."
280 (car articles) newsgroup))
281 (nnbabyl-delete-mail))
282 (setq rest (cons (car articles) rest))))
283 (setq articles (cdr articles)))
285 ;; Find the lowest active article in this group.
286 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
287 (goto-char (point-min))
288 (while (and (not (search-forward
289 (nnbabyl-article-string (car active)) nil t))
290 (<= (car active) (cdr active)))
291 (setcar active (1+ (car active)))
292 (goto-char (point-min))))
293 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
294 (nconc rest articles))))
296 (defun nnbabyl-request-move-article
297 (article group server accept-form &optional last)
298 (nnbabyl-possibly-change-newsgroup group)
299 (let ((buf (get-buffer-create " *nnbabyl move*"))
302 (nnbabyl-request-article article group server)
305 (insert-buffer-substring nntp-server-buffer)
306 (goto-char (point-min))
307 (if (re-search-forward
309 (save-excursion (search-forward "\n\n" nil t) (point)) t)
310 (delete-region (progn (beginning-of-line) (point))
311 (progn (forward-line 1) (point))))
312 (setq result (eval accept-form))
313 (kill-buffer (current-buffer))
316 (set-buffer nnbabyl-mbox-buffer)
317 (goto-char (point-min))
318 (if (search-forward (nnbabyl-article-string article) nil t)
319 (nnbabyl-delete-mail))
320 (and last (save-buffer))))
323 (defun nnbabyl-request-accept-article (group &optional last)
324 (let ((buf (current-buffer))
327 (nnmail-activate 'nnbabyl)
329 (goto-char (point-min))
330 (search-forward "\n\n" nil t)
333 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
334 (delete-region (point) (progn (forward-line 1) (point)))))
335 (let ((nnmail-split-methods
336 (if (stringp group) (list (list group ""))
337 nnmail-split-methods)))
338 (setq result (car (nnbabyl-save-mail))))
339 (set-buffer nnbabyl-mbox-buffer)
340 (goto-char (point-max))
341 (search-backward "\n\^_")
342 (goto-char (match-end 0))
347 nnbabyl-group-alist nnbabyl-active-file)))
350 (defun nnbabyl-request-replace-article (article group buffer)
351 (nnbabyl-possibly-change-newsgroup group)
353 (set-buffer nnbabyl-mbox-buffer)
354 (goto-char (point-min))
355 (if (not (search-forward (nnbabyl-article-string article) nil t))
357 (nnbabyl-delete-mail t t)
358 (insert-buffer-substring buffer)
363 ;;; Low-Level Interface
365 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
366 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
368 (defun nnbabyl-delete-mail (&optional force leave-delim)
369 ;; Delete the current X-Gnus-Newsgroup line.
372 (progn (beginning-of-line) (point))
373 (progn (forward-line 1) (point))))
374 ;; Beginning of the article.
380 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
381 (if leave-delim (progn (forward-line 1) (point))
382 (match-beginning 0)))
385 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
387 (if (and (not (bobp)) leave-delim)
388 (progn (forward-line -2) (point))
389 (match-beginning 0)))
391 (goto-char (point-min))
392 ;; Only delete the article if no other groups owns it as well.
393 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
394 (delete-region (point-min) (point-max))))))
396 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
397 (if (or (not nnbabyl-mbox-buffer)
398 (not (buffer-name nnbabyl-mbox-buffer)))
399 (save-excursion (nnbabyl-read-mbox)))
400 (or nnbabyl-group-alist
401 (nnmail-activate 'nnbabyl))
403 (if (assoc newsgroup nnbabyl-group-alist)
404 (setq nnbabyl-current-group newsgroup)
405 (setq nnbabyl-status-string "No such group in file")
408 (defun nnbabyl-article-string (article)
409 (if (numberp article)
410 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
411 (int-to-string article) " ")
412 (concat "\nMessage-ID: " article)))
414 (defun nnbabyl-article-group-number ()
416 (goto-char (point-min))
417 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
419 (cons (buffer-substring (match-beginning 1) (match-end 1))
421 (buffer-substring (match-beginning 2) (match-end 2)))))))
423 (defun nnbabyl-insert-lines ()
424 "Insert how many lines and chars there are in the body of the mail."
427 (goto-char (point-min))
428 (if (search-forward "\n\n" nil t)
430 ;; There may be an EOOH line here...
431 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
432 (search-forward "\n\n" nil t))
433 (setq chars (- (point-max) (point)))
434 (setq lines (- (count-lines (point) (point-max)) 1))
435 ;; Move back to the end of the headers.
436 (goto-char (point-min))
437 (search-forward "\n\n" nil t)
440 (if (re-search-backward "^Lines: " nil t)
441 (delete-region (point) (progn (forward-line 1) (point)))))
442 (insert (format "Lines: %d\n" lines))
445 (defun nnbabyl-save-mail ()
446 ;; Called narrowed to an article.
447 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
448 (nnbabyl-insert-lines)
449 (nnmail-insert-xref group-art)
450 (nnbabyl-insert-newsgroup-line group-art)
451 (run-hooks 'nnbabyl-prepare-save-mail-hook)
454 (defun nnbabyl-insert-newsgroup-line (group-art)
456 (goto-char (point-min))
457 (while (looking-at "From ")
458 (replace-match "Mail-from: From " t t)
460 ;; If there is a C-l at the beginning of the narrowed region, this
461 ;; isn't really a "save", but rather a "scan".
462 (goto-char (point-min))
463 (or (looking-at "\^L")
465 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
466 (goto-char (point-max))
468 (if (search-forward "\n\n" nil t)
472 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
473 (car (car group-art)) (cdr (car group-art))
474 (current-time-string)))
475 (setq group-art (cdr group-art)))))
478 (defun nnbabyl-active-number (group)
479 ;; Find the next article number in GROUP.
480 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
482 (setcdr active (1+ (cdr active)))
483 ;; This group is new, so we create a new entry for it.
484 ;; This might be a bit naughty... creating groups on the drop of
485 ;; a hat, but I don't know...
486 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
487 nnbabyl-group-alist)))
490 (defun nnbabyl-read-mbox ()
491 (nnmail-activate 'nnbabyl)
492 (or (file-exists-p nnbabyl-mbox-file)
494 (set-buffer (setq nnbabyl-mbox-buffer
495 (create-file-buffer nnbabyl-mbox-file)))
496 (setq buffer-file-name nnbabyl-mbox-file)
497 (insert "BABYL OPTIONS:\n\n\^_")
498 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
500 (if (and nnbabyl-mbox-buffer
501 (buffer-name nnbabyl-mbox-buffer)
503 (set-buffer nnbabyl-mbox-buffer)
504 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
507 (let ((delim (concat "^" nnbabyl-mail-delimiter))
509 (set-buffer (setq nnbabyl-mbox-buffer
510 (nnheader-find-file-noselect
511 nnbabyl-mbox-file nil 'raw)))
512 (buffer-disable-undo (current-buffer))
514 (setq buffer-read-only nil)
517 (goto-char (point-min))
518 (re-search-forward delim nil t)
519 (setq start (match-end 0))
520 (while (re-search-forward delim nil t)
521 (setq end (match-end 0))
522 (or (search-backward "\nX-Gnus-Newsgroup: " start t)
528 (narrow-to-region start end)
530 (setq end (point-max))))))
531 (goto-char (setq start end)))
532 (and (buffer-modified-p (current-buffer)) (save-buffer))
533 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
535 (defun nnbabyl-remove-incoming-delims ()
536 (goto-char (point-min))
537 (while (search-forward "\^_" nil t)
538 (replace-match "?" t t)))
542 ;;; nnbabyl.el ends here