1 ;;; nnbabyl.el --- rmail mbox access for Gnus
2 ;; Copyright (C) 1995,96 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; For an overview of what the interface functions do, please see the
35 (eval-when-compile (require 'cl))
37 (defvar nnbabyl-mbox-file (expand-file-name "~/RMAIL")
38 "The name of the rmail box file in the users home directory.")
40 (defvar nnbabyl-active-file (expand-file-name "~/.rmail-active")
41 "The name of the active file for the rmail box.")
43 (defvar nnbabyl-get-new-mail t
44 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
46 (defvar nnbabyl-prepare-save-mail-hook nil
47 "Hook run narrowed to an article before saving.")
51 (defvar nnbabyl-mail-delimiter "\^_")
53 (defconst nnbabyl-version "nnbabyl 1.0"
56 (defvar nnbabyl-mbox-buffer nil)
57 (defvar nnbabyl-current-group nil)
58 (defvar nnbabyl-status-string "")
59 (defvar nnbabyl-group-alist nil)
60 (defvar nnbabyl-active-timestamp nil)
62 (defvar nnbabyl-previous-buffer-mode nil)
66 (defvar nnbabyl-current-server nil)
67 (defvar nnbabyl-server-alist nil)
68 (defvar nnbabyl-server-variables
70 (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
71 (list 'nnbabyl-active-file nnbabyl-active-file)
72 (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
73 '(nnbabyl-current-group nil)
74 '(nnbabyl-status-string "")
75 '(nnbabyl-previous-buffer-mode nil)
76 '(nnbabyl-group-alist nil)))
80 ;;; Interface functions
82 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server fetch-old)
84 (set-buffer nntp-server-buffer)
86 (let ((number (length sequence))
88 article art-string start stop)
89 (nnbabyl-possibly-change-newsgroup newsgroup)
91 (setq article (car sequence))
92 (setq art-string (nnbabyl-article-string article))
93 (set-buffer nnbabyl-mbox-buffer)
94 (if (or (search-forward art-string nil t)
95 (search-backward art-string nil t))
97 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
98 (while (and (not (looking-at ".+:"))
99 (zerop (forward-line 1))))
101 (search-forward "\n\n" nil t)
102 (setq stop (1- (point)))
103 (set-buffer nntp-server-buffer)
104 (insert "221 " (int-to-string article) " Article retrieved.\n")
105 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
106 (goto-char (point-max))
108 (setq sequence (cdr sequence))
109 (setq count (1+ count))
110 (and (numberp nnmail-large-newsgroup)
111 (> number nnmail-large-newsgroup)
113 (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
114 (/ (* count 100) number))))
116 (and (numberp nnmail-large-newsgroup)
117 (> number nnmail-large-newsgroup)
118 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
120 (set-buffer nntp-server-buffer)
121 (nnheader-fold-continuation-lines)
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)
142 ;; Restore buffer mode.
143 (when (and (nnbabyl-server-opened)
144 nnbabyl-previous-buffer-mode)
146 (set-buffer nnbabyl-mbox-buffer)
148 (car (car nnbabyl-previous-buffer-mode))
149 (cdr (car nnbabyl-previous-buffer-mode)))
150 (funcall (cdr nnbabyl-previous-buffer-mode))))
151 (setq nnbabyl-current-server nil
152 nnbabyl-mbox-buffer nil)
155 (defun nnbabyl-server-opened (&optional server)
156 (and (equal server nnbabyl-current-server)
158 (buffer-name nnbabyl-mbox-buffer)
160 (buffer-name nntp-server-buffer)))
162 (defun nnbabyl-status-message (&optional server)
163 nnbabyl-status-string)
165 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
166 (nnbabyl-possibly-change-newsgroup newsgroup)
168 (set-buffer nnbabyl-mbox-buffer)
169 (goto-char (point-min))
170 (if (search-forward (nnbabyl-article-string article) nil t)
171 (let (start stop summary-line)
172 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
173 (while (and (not (looking-at ".+:"))
174 (zerop (forward-line 1))))
176 (or (and (re-search-forward
177 (concat "^" nnbabyl-mail-delimiter) nil t)
179 (goto-char (point-max)))
181 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
182 (set-buffer nntp-server-buffer)
184 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
185 (goto-char (point-min))
186 ;; If there is an EOOH header, then we have to remove some
187 ;; duplicated headers.
188 (setq summary-line (looking-at "Summary-line:"))
189 (when (search-forward "\n*** EOOH ***" nil t)
191 ;; The headers to be deleted are located before the
193 (delete-region (point-min) (progn (forward-line 1)
196 (delete-region (progn (beginning-of-line) (point))
197 (or (search-forward "\n\n" nil t)
199 (if (numberp article)
200 (cons nnbabyl-current-group article)
201 (nnbabyl-article-group-number)))))))
203 (defun nnbabyl-request-group (group &optional server dont-check)
204 (let ((active (cadr (assoc group nnbabyl-group-alist))))
208 (nnheader-report 'nnbabyl "No such group: %s" group))
209 ((null (nnbabyl-possibly-change-newsgroup group))
210 (nnheader-report 'nnbabyl "No such group: %s" group))
212 (nnheader-report 'nnbabyl "Selected group %s" group)
215 (nnheader-report 'nnbabyl "Selected group %s" group)
216 (nnheader-insert "211 %d %d %d %s\n"
217 (1+ (- (cdr active) (car active)))
218 (car active) (cdr active) group)
221 (defun nnbabyl-request-scan (&optional group server)
227 (set-buffer nnbabyl-mbox-buffer)
229 nnbabyl-mbox-file group
232 (let ((in-buf (current-buffer)))
233 (goto-char (point-min))
234 (while (search-forward "\n\^_\n" nil t)
236 (set-buffer nnbabyl-mbox-buffer)
237 (goto-char (point-max))
238 (search-backward "\n\^_" nil t)
239 (goto-char (match-end 0))
240 (insert-buffer-substring in-buf))))))
242 (defun nnbabyl-close-group (group &optional server)
245 (defun nnbabyl-request-create-group (group &optional server)
246 (nnmail-activate 'nnbabyl)
247 (or (assoc group nnbabyl-group-alist)
249 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 0)))
250 nnbabyl-group-alist))
251 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))
254 (defun nnbabyl-request-list (&optional server)
256 (or (nnmail-find-file nnbabyl-active-file)
258 (setq nnbabyl-group-alist (nnmail-get-active))
259 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
260 (nnmail-find-file nnbabyl-active-file)))))
262 (defun nnbabyl-request-newgroups (date &optional server)
263 (nnbabyl-request-list server))
265 (defun nnbabyl-request-list-newsgroups (&optional server)
266 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
269 (defun nnbabyl-request-post (&optional server)
270 (mail-send-and-exit nil))
272 (defun nnbabyl-request-expire-articles
273 (articles newsgroup &optional server force)
274 (nnbabyl-possibly-change-newsgroup newsgroup)
277 (nnmail-activate 'nnbabyl)
280 (set-buffer nnbabyl-mbox-buffer)
281 (set-text-properties (point-min) (point-max) nil)
282 (while (and articles is-old)
283 (goto-char (point-min))
284 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
286 (nnmail-expired-article-p
289 (point) (progn (end-of-line) (point))) force))
291 (nnheader-message 5 "Deleting article %d in %s..."
292 (car articles) newsgroup)
293 (nnbabyl-delete-mail))
294 (setq rest (cons (car articles) rest))))
295 (setq articles (cdr articles)))
297 ;; Find the lowest active article in this group.
298 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
299 (goto-char (point-min))
300 (while (and (not (search-forward
301 (nnbabyl-article-string (car active)) nil t))
302 (<= (car active) (cdr active)))
303 (setcar active (1+ (car active)))
304 (goto-char (point-min))))
305 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
306 (nconc rest articles))))
308 (defun nnbabyl-request-move-article
309 (article group server accept-form &optional last)
310 (nnbabyl-possibly-change-newsgroup group)
311 (let ((buf (get-buffer-create " *nnbabyl move*"))
314 (nnbabyl-request-article article group server)
317 (insert-buffer-substring nntp-server-buffer)
318 (goto-char (point-min))
319 (if (re-search-forward
321 (save-excursion (search-forward "\n\n" nil t) (point)) t)
322 (delete-region (progn (beginning-of-line) (point))
323 (progn (forward-line 1) (point))))
324 (setq result (eval accept-form))
325 (kill-buffer (current-buffer))
328 (set-buffer nnbabyl-mbox-buffer)
329 (goto-char (point-min))
330 (if (search-forward (nnbabyl-article-string article) nil t)
331 (nnbabyl-delete-mail))
332 (and last (save-buffer))))
335 (defun nnbabyl-request-accept-article (group &optional last)
336 (let ((buf (current-buffer))
339 (nnmail-activate 'nnbabyl)
341 (goto-char (point-min))
342 (search-forward "\n\n" nil t)
345 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
346 (delete-region (point) (progn (forward-line 1) (point)))))
347 (let ((nnmail-split-methods
348 (if (stringp group) (list (list group ""))
349 nnmail-split-methods)))
350 (setq result (car (nnbabyl-save-mail))))
351 (set-buffer nnbabyl-mbox-buffer)
352 (goto-char (point-max))
353 (search-backward "\n\^_")
354 (goto-char (match-end 0))
355 (insert-buffer-substring buf)
359 nnbabyl-group-alist nnbabyl-active-file)))
362 (defun nnbabyl-request-replace-article (article group buffer)
363 (nnbabyl-possibly-change-newsgroup group)
365 (set-buffer nnbabyl-mbox-buffer)
366 (goto-char (point-min))
367 (if (not (search-forward (nnbabyl-article-string article) nil t))
369 (nnbabyl-delete-mail t t)
370 (insert-buffer-substring buffer)
374 (defun nnbabyl-request-delete-group (group &optional force server)
375 (nnbabyl-possibly-change-newsgroup group)
376 ;; Delete all articles in GROUP.
378 () ; Don't delete the articles.
380 (set-buffer nnbabyl-mbox-buffer)
381 (goto-char (point-min))
382 ;; Delete all articles in this group.
383 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
385 (while (search-forward ident nil t)
387 (nnbabyl-delete-mail))
388 (and found (save-buffer)))))
389 ;; Remove the group from all structures.
390 (setq nnbabyl-group-alist
391 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
392 nnbabyl-current-group nil)
393 ;; Save the active file.
394 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
397 (defun nnbabyl-request-rename-group (group new-name &optional server)
398 (nnbabyl-possibly-change-newsgroup group)
400 (set-buffer nnbabyl-mbox-buffer)
401 (goto-char (point-min))
402 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
403 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
405 (while (search-forward ident nil t)
406 (replace-match new-ident t t)
408 (and found (save-buffer))))
409 (let ((entry (assoc group nnbabyl-group-alist)))
410 (and entry (setcar entry new-name))
411 (setq nnbabyl-current-group nil)
412 ;; Save the new group alist.
413 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
417 ;;; Internal functions.
419 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
420 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
422 (defun nnbabyl-delete-mail (&optional force leave-delim)
423 ;; Delete the current X-Gnus-Newsgroup line.
426 (progn (beginning-of-line) (point))
427 (progn (forward-line 1) (point))))
428 ;; Beginning of the article.
434 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
435 (if leave-delim (progn (forward-line 1) (point))
436 (match-beginning 0)))
439 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
441 (if (and (not (bobp)) leave-delim)
442 (progn (forward-line -2) (point))
443 (match-beginning 0)))
445 (goto-char (point-min))
446 ;; Only delete the article if no other groups owns it as well.
447 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
448 (delete-region (point-min) (point-max))))))
450 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
451 (if (or (not nnbabyl-mbox-buffer)
452 (not (buffer-name nnbabyl-mbox-buffer)))
453 (save-excursion (nnbabyl-read-mbox)))
454 (or nnbabyl-group-alist
455 (nnmail-activate 'nnbabyl))
457 (if (assoc newsgroup nnbabyl-group-alist)
458 (setq nnbabyl-current-group newsgroup)
459 (setq nnbabyl-status-string "No such group in file")
463 (defun nnbabyl-article-string (article)
464 (if (numberp article)
465 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
466 (int-to-string article) " ")
467 (concat "\nMessage-ID: " article)))
469 (defun nnbabyl-article-group-number ()
471 (goto-char (point-min))
472 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
474 (cons (buffer-substring (match-beginning 1) (match-end 1))
476 (buffer-substring (match-beginning 2) (match-end 2)))))))
478 (defun nnbabyl-insert-lines ()
479 "Insert how many lines and chars there are in the body of the mail."
482 (goto-char (point-min))
483 (if (search-forward "\n\n" nil t)
485 ;; There may be an EOOH line here...
486 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
487 (search-forward "\n\n" nil t))
488 (setq chars (- (point-max) (point)))
489 (setq lines (- (count-lines (point) (point-max)) 1))
490 ;; Move back to the end of the headers.
491 (goto-char (point-min))
492 (search-forward "\n\n" nil t)
495 (if (re-search-backward "^Lines: " nil t)
496 (delete-region (point) (progn (forward-line 1) (point)))))
497 (insert (format "Lines: %d\n" lines))
500 (defun nnbabyl-save-mail ()
501 ;; Called narrowed to an article.
502 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
503 (nnbabyl-insert-lines)
504 (nnmail-insert-xref group-art)
505 (nnbabyl-insert-newsgroup-line group-art)
506 (run-hooks 'nnbabyl-prepare-save-mail-hook)
509 (defun nnbabyl-insert-newsgroup-line (group-art)
511 (goto-char (point-min))
512 (while (looking-at "From ")
513 (replace-match "Mail-from: From " t t)
515 ;; If there is a C-l at the beginning of the narrowed region, this
516 ;; isn't really a "save", but rather a "scan".
517 (goto-char (point-min))
518 (or (looking-at "\^L")
520 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
521 (goto-char (point-max))
523 (if (search-forward "\n\n" nil t)
527 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
528 (car (car group-art)) (cdr (car group-art))
529 (current-time-string)))
530 (setq group-art (cdr group-art)))))
533 (defun nnbabyl-active-number (group)
534 ;; Find the next article number in GROUP.
535 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
537 (setcdr active (1+ (cdr active)))
538 ;; This group is new, so we create a new entry for it.
539 ;; This might be a bit naughty... creating groups on the drop of
540 ;; a hat, but I don't know...
541 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
542 nnbabyl-group-alist)))
545 (defun nnbabyl-read-mbox ()
546 (nnmail-activate 'nnbabyl)
547 (or (file-exists-p nnbabyl-mbox-file)
549 (set-buffer (setq nnbabyl-mbox-buffer
550 (create-file-buffer nnbabyl-mbox-file)))
551 (setq buffer-file-name nnbabyl-mbox-file)
552 (insert "BABYL OPTIONS:\n\n\^_")
553 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
555 (if (and nnbabyl-mbox-buffer
556 (buffer-name nnbabyl-mbox-buffer)
558 (set-buffer nnbabyl-mbox-buffer)
559 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
562 (let ((delim (concat "^" nnbabyl-mail-delimiter))
564 (set-buffer (setq nnbabyl-mbox-buffer
565 (nnheader-find-file-noselect
566 nnbabyl-mbox-file nil 'raw)))
568 (setq nnbabyl-previous-buffer-mode
569 (cons (cons (point-min) (point-max))
572 (buffer-disable-undo (current-buffer))
574 (setq buffer-read-only nil)
577 (goto-char (point-min))
578 (re-search-forward delim nil t)
579 (setq start (match-end 0))
580 (while (re-search-forward delim nil t)
581 (setq end (match-end 0))
582 (or (search-backward "\nX-Gnus-Newsgroup: " start t)
588 (narrow-to-region start end)
590 (setq end (point-max))))))
591 (goto-char (setq start end)))
592 (and (buffer-modified-p (current-buffer)) (save-buffer))
593 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
595 (defun nnbabyl-remove-incoming-delims ()
596 (goto-char (point-min))
597 (while (search-forward "\^_" nil t)
598 (replace-match "?" t t)))
602 ;;; nnbabyl.el ends here