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 (defun nnbabyl-request-expire-articles
253 (articles newsgroup &optional server force)
254 (nnbabyl-possibly-change-newsgroup newsgroup)
255 (let* ((days (or (and nnmail-expiry-wait-function
256 (funcall nnmail-expiry-wait-function newsgroup))
260 (nnmail-activate 'nnbabyl)
263 (set-buffer nnbabyl-mbox-buffer)
264 (set-text-properties (point-min) (point-max) nil)
265 (while (and articles is-old)
266 (goto-char (point-min))
267 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
270 (> (nnmail-days-between
271 (current-time-string)
273 (point) (progn (end-of-line) (point))))
276 (and gnus-verbose-backends
277 (message "Deleting article %d in %s..."
278 (car articles) newsgroup))
279 (nnbabyl-delete-mail))
280 (setq rest (cons (car articles) rest))))
281 (setq articles (cdr articles)))
283 ;; Find the lowest active article in this group.
284 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
285 (goto-char (point-min))
286 (while (and (not (search-forward
287 (nnbabyl-article-string (car active)) nil t))
288 (<= (car active) (cdr active)))
289 (setcar active (1+ (car active)))
290 (goto-char (point-min))))
291 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
292 (nconc rest articles))))
294 (defun nnbabyl-request-move-article
295 (article group server accept-form &optional last)
296 (nnbabyl-possibly-change-newsgroup group)
297 (let ((buf (get-buffer-create " *nnbabyl move*"))
300 (nnbabyl-request-article article group server)
303 (insert-buffer-substring nntp-server-buffer)
304 (goto-char (point-min))
305 (if (re-search-forward
307 (save-excursion (search-forward "\n\n" nil t) (point)) t)
308 (delete-region (progn (beginning-of-line) (point))
309 (progn (forward-line 1) (point))))
310 (setq result (eval accept-form))
311 (kill-buffer (current-buffer))
314 (set-buffer nnbabyl-mbox-buffer)
315 (goto-char (point-min))
316 (if (search-forward (nnbabyl-article-string article) nil t)
317 (nnbabyl-delete-mail))
318 (and last (save-buffer))))
321 (defun nnbabyl-request-accept-article (group &optional last)
322 (let ((buf (current-buffer))
325 (nnmail-activate 'nnbabyl)
327 (goto-char (point-min))
328 (search-forward "\n\n" nil t)
331 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
332 (delete-region (point) (progn (forward-line 1) (point)))))
333 (let ((nnmail-split-methods
334 (if (stringp group) (list (list group ""))
335 nnmail-split-methods)))
336 (setq result (car (nnbabyl-save-mail))))
337 (set-buffer nnbabyl-mbox-buffer)
338 (goto-char (point-max))
339 (search-backward "\n\^_")
340 (goto-char (match-end 0))
345 nnbabyl-group-alist nnbabyl-active-file)))
348 (defun nnbabyl-request-replace-article (article group buffer)
349 (nnbabyl-possibly-change-newsgroup group)
351 (set-buffer nnbabyl-mbox-buffer)
352 (goto-char (point-min))
353 (if (not (search-forward (nnbabyl-article-string article) nil t))
355 (nnbabyl-delete-mail t t)
356 (insert-buffer-substring buffer)
361 ;;; Low-Level Interface
363 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
364 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
366 (defun nnbabyl-delete-mail (&optional force leave-delim)
367 ;; Delete the current X-Gnus-Newsgroup line.
370 (progn (beginning-of-line) (point))
371 (progn (forward-line 1) (point))))
372 ;; Beginning of the article.
378 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
379 (if leave-delim (progn (forward-line 1) (point))
380 (match-beginning 0)))
383 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
385 (if (and (not (bobp)) leave-delim)
386 (progn (forward-line -2) (point))
387 (match-beginning 0)))
389 (goto-char (point-min))
390 ;; Only delete the article if no other groups owns it as well.
391 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
392 (delete-region (point-min) (point-max))))))
394 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
395 (if (or (not nnbabyl-mbox-buffer)
396 (not (buffer-name nnbabyl-mbox-buffer)))
397 (save-excursion (nnbabyl-read-mbox)))
398 (or nnbabyl-group-alist
399 (nnmail-activate 'nnbabyl))
401 (if (assoc newsgroup nnbabyl-group-alist)
402 (setq nnbabyl-current-group newsgroup)
403 (setq nnbabyl-status-string "No such group in file")
406 (defun nnbabyl-article-string (article)
407 (if (numberp article)
408 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
409 (int-to-string article) " ")
410 (concat "\nMessage-ID: " article)))
412 (defun nnbabyl-article-group-number ()
414 (goto-char (point-min))
415 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
417 (cons (buffer-substring (match-beginning 1) (match-end 1))
419 (buffer-substring (match-beginning 2) (match-end 2)))))))
421 (defun nnbabyl-insert-lines ()
422 "Insert how many lines and chars there are in the body of the mail."
425 (goto-char (point-min))
426 (if (search-forward "\n\n" nil t)
428 ;; There may be an EOOH line here...
429 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
430 (search-forward "\n\n" nil t))
431 (setq chars (- (point-max) (point)))
432 (setq lines (- (count-lines (point) (point-max)) 1))
433 ;; Move back to the end of the headers.
434 (goto-char (point-min))
435 (search-forward "\n\n" nil t)
438 (if (re-search-backward "^Lines: " nil t)
439 (delete-region (point) (progn (forward-line 1) (point)))))
440 (insert (format "Lines: %d\n" lines))
443 (defun nnbabyl-save-mail ()
444 ;; Called narrowed to an article.
445 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
446 (nnbabyl-insert-lines)
447 (nnmail-insert-xref group-art)
448 (nnbabyl-insert-newsgroup-line group-art)
449 (run-hooks 'nnbabyl-prepare-save-mail-hook)
452 (defun nnbabyl-insert-newsgroup-line (group-art)
454 (goto-char (point-min))
455 (while (looking-at "From ")
456 (replace-match "Mail-from: From " t t)
458 ;; If there is a C-l at the beginning of the narrowed region, this
459 ;; isn't really a "save", but rather a "scan".
460 (goto-char (point-min))
461 (or (looking-at "\^L")
463 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
464 (goto-char (point-max))
466 (if (search-forward "\n\n" nil t)
470 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
471 (car (car group-art)) (cdr (car group-art))
472 (current-time-string)))
473 (setq group-art (cdr group-art)))))
476 (defun nnbabyl-active-number (group)
477 ;; Find the next article number in GROUP.
478 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
480 (setcdr active (1+ (cdr active)))
481 ;; This group is new, so we create a new entry for it.
482 ;; This might be a bit naughty... creating groups on the drop of
483 ;; a hat, but I don't know...
484 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
485 nnbabyl-group-alist)))
488 (defun nnbabyl-read-mbox ()
489 (nnmail-activate 'nnbabyl)
490 (or (file-exists-p nnbabyl-mbox-file)
492 (set-buffer (setq nnbabyl-mbox-buffer
493 (create-file-buffer nnbabyl-mbox-file)))
494 (setq buffer-file-name nnbabyl-mbox-file)
495 (insert "BABYL OPTIONS:\n\n\^_")
496 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
498 (if (and nnbabyl-mbox-buffer
499 (buffer-name nnbabyl-mbox-buffer)
501 (set-buffer nnbabyl-mbox-buffer)
502 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
505 (let ((delim (concat "^" nnbabyl-mail-delimiter))
507 (set-buffer (setq nnbabyl-mbox-buffer
508 (nnheader-find-file-noselect
509 nnbabyl-mbox-file nil 'raw)))
510 (buffer-disable-undo (current-buffer))
512 (setq buffer-read-only nil)
515 (goto-char (point-min))
516 (re-search-forward delim nil t)
517 (setq start (match-end 0))
518 (while (re-search-forward delim nil t)
519 (setq end (match-end 0))
520 (or (search-backward "\nX-Gnus-Newsgroup: " start t)
526 (narrow-to-region start end)
528 (setq end (point-max))))))
529 (goto-char (setq start end)))
530 (and (buffer-modified-p (current-buffer)) (save-buffer))
531 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
533 (defun nnbabyl-remove-incoming-delims ()
534 (goto-char (point-min))
535 (while (search-forward "\^_" nil t)
536 (replace-match "?" t t)))
540 ;;; nnbabyl.el ends here