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-retrieve-headers (sequence &optional newsgroup server)
78 (set-buffer nntp-server-buffer)
80 (let ((number (length sequence))
82 article art-string start stop)
83 (nnbabyl-possibly-change-newsgroup newsgroup)
84 (if (stringp (car sequence))
87 (setq article (car sequence))
88 (setq art-string (nnbabyl-article-string article))
89 (set-buffer nnbabyl-mbox-buffer)
90 (if (or (search-forward art-string nil t)
91 (search-backward art-string nil t))
93 (re-search-backward (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 "221 " (int-to-string article) " Article retrieved.\n")
101 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
102 (goto-char (point-max))
104 (setq sequence (cdr sequence))
105 (setq count (1+ count))
106 (and (numberp nnmail-large-newsgroup)
107 (> number nnmail-large-newsgroup)
109 gnus-verbose-backends
110 (message "nnbabyl: Receiving headers... %d%%"
111 (/ (* count 100) number))))
113 (and (numberp nnmail-large-newsgroup)
114 (> number nnmail-large-newsgroup)
115 gnus-verbose-backends
116 (message "nnbabyl: Receiving headers...done"))
118 ;; Fold continuation lines.
119 (set-buffer nntp-server-buffer)
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 nnbabyl-mbox-buffer)
150 (buffer-name nntp-server-buffer)))
152 (defun nnbabyl-status-message (&optional server)
153 nnbabyl-status-string)
155 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
156 (nnbabyl-possibly-change-newsgroup newsgroup)
157 (if (stringp article)
160 (set-buffer nnbabyl-mbox-buffer)
161 (goto-char (point-min))
162 (if (search-forward (nnbabyl-article-string article) nil t)
164 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
165 (while (and (not (looking-at ".+:"))
166 (zerop (forward-line 1))))
168 (or (and (re-search-forward
169 (concat "^" nnbabyl-mail-delimiter) nil t)
171 (goto-char (point-max)))
173 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
174 (set-buffer nntp-server-buffer)
176 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
177 (goto-char (point-min))
178 (if (search-forward "\n*** EOOH ***" nil t)
180 (delete-region (progn (beginning-of-line) (point))
181 (or (search-forward "\n\n" nil t)
185 (defun nnbabyl-request-group (group &optional server dont-check)
187 (if (nnbabyl-possibly-change-newsgroup group)
190 (nnbabyl-get-new-mail group)
192 (set-buffer nntp-server-buffer)
194 (let ((active (assoc group nnbabyl-group-alist)))
195 (insert (format "211 %d %d %d %s\n"
196 (1+ (- (cdr (car (cdr active)))
197 (car (car (cdr active)))))
198 (car (car (cdr active)))
199 (cdr (car (cdr active)))
203 (defun nnbabyl-close-group (group &optional server)
206 (defun nnbabyl-request-create-group (group &optional server)
207 (nnmail-activate 'nnbabyl)
208 (or (assoc group nnbabyl-group-alist)
210 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 0)))
211 nnbabyl-group-alist))
212 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))
215 (defun nnbabyl-request-list (&optional server)
216 (if server (nnbabyl-get-new-mail))
218 (or (nnmail-find-file nnbabyl-active-file)
220 (setq nnbabyl-group-alist (nnmail-get-active))
221 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
222 (nnmail-find-file nnbabyl-active-file)))))
224 (defun nnbabyl-request-newgroups (date &optional server)
225 (nnbabyl-request-list server))
227 (defun nnbabyl-request-list-newsgroups (&optional server)
228 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
231 (defun nnbabyl-request-post (&optional server)
232 (mail-send-and-exit nil))
234 (defalias 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
236 (defun nnbabyl-request-expire-articles
237 (articles newsgroup &optional server force)
238 (nnbabyl-possibly-change-newsgroup newsgroup)
239 (let* ((days (or (and nnmail-expiry-wait-function
240 (funcall nnmail-expiry-wait-function newsgroup))
244 (nnmail-activate 'nnbabyl)
247 (set-buffer nnbabyl-mbox-buffer)
248 (while (and articles is-old)
249 (goto-char (point-min))
250 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
253 (> (nnmail-days-between
254 (current-time-string)
256 (point) (progn (end-of-line) (point))))
259 (and gnus-verbose-backends
260 (message "Deleting article %s..." (car articles)))
261 (nnbabyl-delete-mail))
262 (setq rest (cons (car articles) rest))))
263 (setq articles (cdr articles)))
265 ;; Find the lowest active article in this group.
266 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
267 (goto-char (point-min))
268 (while (and (not (search-forward
269 (nnbabyl-article-string (car active)) nil t))
270 (<= (car active) (cdr active)))
271 (setcar active (1+ (car active)))
272 (goto-char (point-min))))
273 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
274 (nconc rest articles))))
276 (defun nnbabyl-request-move-article
277 (article group server accept-form &optional last)
278 (nnbabyl-possibly-change-newsgroup group)
279 (let ((buf (get-buffer-create " *nnbabyl move*"))
282 (nnbabyl-request-article article group server)
285 (insert-buffer-substring nntp-server-buffer)
286 (goto-char (point-min))
287 (if (re-search-forward
289 (save-excursion (search-forward "\n\n" nil t) (point)) t)
290 (delete-region (progn (beginning-of-line) (point))
291 (progn (forward-line 1) (point))))
292 (setq result (eval accept-form))
293 (kill-buffer (current-buffer))
296 (set-buffer nnbabyl-mbox-buffer)
297 (goto-char (point-min))
298 (if (search-forward (nnbabyl-article-string article) nil t)
299 (nnbabyl-delete-mail))
300 (and last (save-buffer))))
303 (defun nnbabyl-request-accept-article (group &optional last)
304 (let ((buf (current-buffer))
307 (nnmail-activate 'nnbabyl)
309 (goto-char (point-min))
310 (search-forward "\n\n" nil t)
313 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
314 (delete-region (point) (progn (forward-line 1) (point)))))
315 (let ((nnmail-split-methods
316 (if (stringp group) (list (list group ""))
317 nnmail-split-methods)))
318 (setq result (car (nnbabyl-save-mail))))
319 (set-buffer nnbabyl-mbox-buffer)
320 (goto-char (point-max))
321 (search-backward "\n\^_")
322 (goto-char (match-end 0))
327 nnbabyl-group-alist nnbabyl-active-file)))
330 (defun nnbabyl-request-replace-article (article group buffer)
331 (nnbabyl-possibly-change-newsgroup group)
333 (set-buffer nnbabyl-mbox-buffer)
334 (goto-char (point-min))
335 (if (not (search-forward (nnbabyl-article-string article) nil t))
337 (nnbabyl-delete-mail t t)
338 (insert-buffer-substring buffer)
343 ;;; Low-Level Interface
345 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
346 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
348 (defun nnbabyl-delete-mail (&optional force leave-delim)
349 ;; Delete the current X-Gnus-Newsgroup line.
352 (progn (beginning-of-line) (point))
353 (progn (forward-line 1) (point))))
354 ;; Beginning of the article.
360 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
361 (if leave-delim (progn (forward-line 1) (point))
362 (match-beginning 0)))
365 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
367 (if (and (not (bobp)) leave-delim)
368 (progn (forward-line -2) (point))
369 (match-beginning 0)))
371 (goto-char (point-min))
372 ;; Only delete the article if no other groups owns it as well.
373 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
374 (delete-region (point-min) (point-max))))))
376 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
377 (if (or (not nnbabyl-mbox-buffer)
378 (not (buffer-name nnbabyl-mbox-buffer)))
379 (save-excursion (nnbabyl-read-mbox)))
380 (or nnbabyl-group-alist
381 (nnmail-activate 'nnbabyl))
383 (if (assoc newsgroup nnbabyl-group-alist)
384 (setq nnbabyl-current-group newsgroup)
385 (setq nnbabyl-status-string "No such group in file")
388 (defun nnbabyl-article-string (article)
389 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
390 (int-to-string article) " "))
392 (defun nnbabyl-insert-lines ()
393 "Insert how many lines and chars there are in the body of the mail."
396 (goto-char (point-min))
397 (if (search-forward "\n\n" nil t)
399 ;; There may be an EOOH line here...
400 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
401 (search-forward "\n\n" nil t))
402 (setq chars (- (point-max) (point)))
403 (setq lines (- (count-lines (point) (point-max)) 1))
404 ;; Move back to the end of the headers.
405 (goto-char (point-min))
406 (search-forward "\n\n" nil t)
409 (if (re-search-backward "^Lines: " nil t)
410 (delete-region (point) (progn (forward-line 1) (point)))))
411 (insert (format "Lines: %d\n" lines))
414 (defun nnbabyl-save-mail ()
415 ;; Called narrowed to an article.
416 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
417 (nnbabyl-insert-lines)
418 (nnmail-insert-xref group-art)
419 (nnbabyl-insert-newsgroup-line group-art)
420 (run-hooks 'nnbabyl-prepare-save-mail-hook)
423 (defun nnbabyl-insert-newsgroup-line (group-art)
425 (goto-char (point-min))
426 ;; If there is a C-l at the beginning of the narrowed region, this
427 ;; isn't really a "save", but rather a "scan".
428 (while (looking-at "From ")
429 (replace-match "Mail-from: From " t t)
431 (goto-char (point-min))
432 (or (looking-at "\^L")
434 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
435 (goto-char (point-max))
437 (if (search-forward "\n\n" nil t)
441 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
442 (car (car group-art)) (cdr (car group-art))
443 (current-time-string)))
444 (setq group-art (cdr group-art)))))
447 (defun nnbabyl-active-number (group)
448 ;; Find the next article number in GROUP.
449 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
451 (setcdr active (1+ (cdr active)))
452 ;; This group is new, so we create a new entry for it.
453 ;; This might be a bit naughty... creating groups on the drop of
454 ;; a hat, but I don't know...
455 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
456 nnbabyl-group-alist)))
459 (defun nnbabyl-read-mbox ()
460 (nnmail-activate 'nnbabyl)
461 (or (file-exists-p nnbabyl-mbox-file)
463 (set-buffer (setq nnbabyl-mbox-buffer
464 (create-file-buffer nnbabyl-mbox-file)))
465 (setq buffer-file-name nnbabyl-mbox-file)
466 (insert "BABYL OPTIONS:\n\n\^_")
467 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
469 (if (and nnbabyl-mbox-buffer
470 (buffer-name nnbabyl-mbox-buffer)
472 (set-buffer nnbabyl-mbox-buffer)
473 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
476 (let ((delim (concat "^" nnbabyl-mail-delimiter))
477 (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
478 (create-file-buffer nnbabyl-mbox-file)))
480 (set-buffer (setq nnbabyl-mbox-buffer buf))
481 (buffer-disable-undo (current-buffer))
483 (insert-file-contents nnbabyl-mbox-file)
484 (setq buffer-file-name nnbabyl-mbox-file)
485 (set-buffer-modified-p nil)
487 (goto-char (point-min))
488 (while (re-search-forward delim nil t)
489 (setq start (match-beginning 0))
491 (save-excursion (re-search-forward delim nil t))
493 "\nX-Gnus-Newsgroup: "
495 (setq end (or (and (re-search-forward delim nil t)
503 (narrow-to-region (1+ start) end)
504 (nnbabyl-save-mail))))))
505 (and (buffer-modified-p (current-buffer)) (save-buffer))
506 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
508 (defun nnbabyl-remove-incoming-delims ()
509 (goto-char (point-min))
510 (while (search-forward "\^_" nil t)
511 (replace-match "?" t t)))
513 (defun nnbabyl-get-new-mail (&optional group)
514 "Read new incoming mail."
515 (let* ((spools (nnmail-get-spool-files group))
519 (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
521 ;; We go through all the existing spool files and split the
525 (file-exists-p (car spools))
526 (> (nth 7 (file-attributes (car spools))) 0)
528 (and gnus-verbose-backends
529 (message "nnbabyl: Reading incoming mail..."))
532 (car spools) (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 (and nnmail-delete-incoming
562 (file-writable-p incoming)
563 (delete-file incoming))
564 (setq incomings (cdr incomings))))))
568 ;;; nnbabyl.el ends here