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
36 (defvar nnbabyl-mbox-file (expand-file-name "~/RMAIL")
37 "The name of the rmail box file in the users home directory.")
39 (defvar nnbabyl-active-file (expand-file-name "~/.rmail-active")
40 "The name of the active file for the rmail box.")
42 (defvar nnbabyl-get-new-mail t
43 "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
45 (defvar nnbabyl-prepare-save-mail-hook nil
46 "Hook run narrowed to an article before saving.")
50 (defvar nnbabyl-mail-delimiter "\^_")
52 (defconst nnbabyl-version "nnbabyl 1.0"
55 (defvar nnbabyl-mbox-buffer nil)
56 (defvar nnbabyl-current-group nil)
57 (defvar nnbabyl-status-string "")
58 (defvar nnbabyl-group-alist nil)
59 (defvar nnbabyl-active-timestamp nil)
61 (defvar nnbabyl-previous-buffer-mode nil)
65 (defvar nnbabyl-current-server nil)
66 (defvar nnbabyl-server-alist nil)
67 (defvar nnbabyl-server-variables
69 (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
70 (list 'nnbabyl-active-file nnbabyl-active-file)
71 (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
72 '(nnbabyl-current-group nil)
73 '(nnbabyl-status-string "")
74 '(nnbabyl-previous-buffer-mode nil)
75 '(nnbabyl-group-alist nil)))
79 ;;; Interface functions
81 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server fetch-old)
83 (set-buffer nntp-server-buffer)
85 (let ((number (length sequence))
87 article art-string start stop)
88 (nnbabyl-possibly-change-newsgroup newsgroup)
90 (setq article (car sequence))
91 (setq art-string (nnbabyl-article-string article))
92 (set-buffer nnbabyl-mbox-buffer)
93 (if (or (search-forward art-string nil t)
94 (search-backward art-string nil t))
96 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
97 (while (and (not (looking-at ".+:"))
98 (zerop (forward-line 1))))
100 (search-forward "\n\n" nil t)
101 (setq stop (1- (point)))
102 (set-buffer nntp-server-buffer)
103 (insert "221 " (int-to-string article) " Article retrieved.\n")
104 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
105 (goto-char (point-max))
107 (setq sequence (cdr sequence))
108 (setq count (1+ count))
109 (and (numberp nnmail-large-newsgroup)
110 (> number nnmail-large-newsgroup)
112 (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
113 (/ (* count 100) number))))
115 (and (numberp nnmail-large-newsgroup)
116 (> number nnmail-large-newsgroup)
117 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
119 (set-buffer nntp-server-buffer)
120 (nnheader-fold-continuation-lines)
123 (defun nnbabyl-open-server (server &optional defs)
124 (nnheader-init-server-buffer)
125 (if (equal server nnbabyl-current-server)
127 (if nnbabyl-current-server
128 (setq nnbabyl-server-alist
129 (cons (list nnbabyl-current-server
130 (nnheader-save-variables nnbabyl-server-variables))
131 nnbabyl-server-alist)))
132 (let ((state (assoc server nnbabyl-server-alist)))
135 (nnheader-restore-variables (nth 1 state))
136 (setq nnbabyl-server-alist (delq state nnbabyl-server-alist)))
137 (nnheader-set-init-variables nnbabyl-server-variables defs)))
138 (setq nnbabyl-current-server server)))
140 (defun nnbabyl-close-server (&optional server)
141 ;; Restore buffer mode.
142 (when (and (nnbabyl-server-opened)
143 nnbabyl-previous-buffer-mode)
145 (set-buffer nnbabyl-mbox-buffer)
147 (car (car nnbabyl-previous-buffer-mode))
148 (cdr (car nnbabyl-previous-buffer-mode)))
149 (funcall (cdr nnbabyl-previous-buffer-mode))))
150 (setq nnbabyl-current-server nil
151 nnbabyl-mbox-buffer nil)
154 (defun nnbabyl-server-opened (&optional server)
155 (and (equal server nnbabyl-current-server)
157 (buffer-name nnbabyl-mbox-buffer)
159 (buffer-name nntp-server-buffer)))
161 (defun nnbabyl-status-message (&optional server)
162 nnbabyl-status-string)
164 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
165 (nnbabyl-possibly-change-newsgroup newsgroup)
167 (set-buffer nnbabyl-mbox-buffer)
168 (goto-char (point-min))
169 (if (search-forward (nnbabyl-article-string article) nil t)
170 (let (start stop summary-line)
171 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
172 (while (and (not (looking-at ".+:"))
173 (zerop (forward-line 1))))
175 (or (and (re-search-forward
176 (concat "^" nnbabyl-mail-delimiter) nil t)
178 (goto-char (point-max)))
180 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
181 (set-buffer nntp-server-buffer)
183 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
184 (goto-char (point-min))
185 ;; If there is an EOOH header, then we have to remove some
186 ;; duplicated headers.
187 (setq summary-line (looking-at "Summary-line:"))
188 (when (search-forward "\n*** EOOH ***" nil t)
190 ;; The headers to be deleted are located before the
192 (delete-region (point-min) (progn (forward-line 1)
195 (delete-region (progn (beginning-of-line) (point))
196 (or (search-forward "\n\n" nil t)
198 (if (numberp article)
199 (cons nnbabyl-current-group article)
200 (nnbabyl-article-group-number)))))))
202 (defun nnbabyl-request-group (group &optional server dont-check)
203 (let ((active (cadr (assoc group nnbabyl-group-alist))))
207 (nnheader-report 'nnbabyl "No such group: %s" group))
208 ((null (nnbabyl-possibly-change-newsgroup group))
209 (nnheader-report 'nnbabyl "No such group: %s" group))
211 (nnheader-report 'nnbabyl "Selected group %s" group)
213 ((> (car active) (cdr active))
214 (nnheader-report 'nnbabyl "Empty group %s" group))
216 (nnheader-report 'nnbabyl "Selected group %s" group)
217 (nnheader-insert "211 %d %d %d %s\n"
218 (1+ (- (cdr active) (car active)))
219 (car active) (cdr active) group)
222 (defun nnbabyl-request-scan (&optional group server)
228 (set-buffer nnbabyl-mbox-buffer)
230 nnbabyl-mbox-file group
233 (let ((in-buf (current-buffer)))
234 (goto-char (point-min))
235 (while (search-forward "\n\^_\n" nil t)
237 (set-buffer nnbabyl-mbox-buffer)
238 (goto-char (point-max))
239 (search-backward "\n\^_" nil t)
240 (goto-char (match-end 0))
241 (insert-buffer-substring in-buf))))))
243 (defun nnbabyl-close-group (group &optional server)
246 (defun nnbabyl-request-create-group (group &optional server)
247 (nnmail-activate 'nnbabyl)
248 (or (assoc group nnbabyl-group-alist)
250 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 0)))
251 nnbabyl-group-alist))
252 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))
255 (defun nnbabyl-request-list (&optional server)
257 (or (nnmail-find-file nnbabyl-active-file)
259 (setq nnbabyl-group-alist (nnmail-get-active))
260 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
261 (nnmail-find-file nnbabyl-active-file)))))
263 (defun nnbabyl-request-newgroups (date &optional server)
264 (nnbabyl-request-list server))
266 (defun nnbabyl-request-list-newsgroups (&optional server)
267 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
270 (defun nnbabyl-request-post (&optional server)
271 (mail-send-and-exit nil))
273 (defun nnbabyl-request-expire-articles
274 (articles newsgroup &optional server force)
275 (nnbabyl-possibly-change-newsgroup newsgroup)
278 (nnmail-activate 'nnbabyl)
281 (set-buffer nnbabyl-mbox-buffer)
282 (set-text-properties (point-min) (point-max) nil)
283 (while (and articles is-old)
284 (goto-char (point-min))
285 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
287 (nnmail-expired-article-p
290 (point) (progn (end-of-line) (point))) force))
292 (nnheader-message 5 "Deleting article %d in %s..."
293 (car articles) newsgroup)
294 (nnbabyl-delete-mail))
295 (setq rest (cons (car articles) rest))))
296 (setq articles (cdr articles)))
298 ;; Find the lowest active article in this group.
299 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
300 (goto-char (point-min))
301 (while (and (not (search-forward
302 (nnbabyl-article-string (car active)) nil t))
303 (<= (car active) (cdr active)))
304 (setcar active (1+ (car active)))
305 (goto-char (point-min))))
306 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
307 (nconc rest articles))))
309 (defun nnbabyl-request-move-article
310 (article group server accept-form &optional last)
311 (nnbabyl-possibly-change-newsgroup group)
312 (let ((buf (get-buffer-create " *nnbabyl move*"))
315 (nnbabyl-request-article article group server)
318 (insert-buffer-substring nntp-server-buffer)
319 (goto-char (point-min))
320 (if (re-search-forward
322 (save-excursion (search-forward "\n\n" nil t) (point)) t)
323 (delete-region (progn (beginning-of-line) (point))
324 (progn (forward-line 1) (point))))
325 (setq result (eval accept-form))
326 (kill-buffer (current-buffer))
329 (set-buffer nnbabyl-mbox-buffer)
330 (goto-char (point-min))
331 (if (search-forward (nnbabyl-article-string article) nil t)
332 (nnbabyl-delete-mail))
333 (and last (save-buffer))))
336 (defun nnbabyl-request-accept-article (group &optional last)
337 (let ((buf (current-buffer))
340 (nnmail-activate 'nnbabyl)
342 (goto-char (point-min))
343 (search-forward "\n\n" nil t)
346 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
347 (delete-region (point) (progn (forward-line 1) (point)))))
348 (let ((nnmail-split-methods
349 (if (stringp group) (list (list group ""))
350 nnmail-split-methods)))
351 (setq result (car (nnbabyl-save-mail))))
352 (set-buffer nnbabyl-mbox-buffer)
353 (goto-char (point-max))
354 (search-backward "\n\^_")
355 (goto-char (match-end 0))
356 (insert-buffer-substring buf)
360 nnbabyl-group-alist nnbabyl-active-file)))
363 (defun nnbabyl-request-replace-article (article group buffer)
364 (nnbabyl-possibly-change-newsgroup group)
366 (set-buffer nnbabyl-mbox-buffer)
367 (goto-char (point-min))
368 (if (not (search-forward (nnbabyl-article-string article) nil t))
370 (nnbabyl-delete-mail t t)
371 (insert-buffer-substring buffer)
375 (defun nnbabyl-request-delete-group (group &optional force server)
376 (nnbabyl-possibly-change-newsgroup group)
377 ;; Delete all articles in GROUP.
379 () ; Don't delete the articles.
381 (set-buffer nnbabyl-mbox-buffer)
382 (goto-char (point-min))
383 ;; Delete all articles in this group.
384 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
386 (while (search-forward ident nil t)
388 (nnbabyl-delete-mail))
389 (and found (save-buffer)))))
390 ;; Remove the group from all structures.
391 (setq nnbabyl-group-alist
392 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
393 nnbabyl-current-group nil)
394 ;; Save the active file.
395 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
398 (defun nnbabyl-request-rename-group (group new-name &optional server)
399 (nnbabyl-possibly-change-newsgroup group)
401 (set-buffer nnbabyl-mbox-buffer)
402 (goto-char (point-min))
403 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
404 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
406 (while (search-forward ident nil t)
407 (replace-match new-ident t t)
409 (and found (save-buffer))))
410 (let ((entry (assoc group nnbabyl-group-alist)))
411 (and entry (setcar entry new-name))
412 (setq nnbabyl-current-group nil)
413 ;; Save the new group alist.
414 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
418 ;;; Internal functions.
420 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
421 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
423 (defun nnbabyl-delete-mail (&optional force leave-delim)
424 ;; Delete the current X-Gnus-Newsgroup line.
427 (progn (beginning-of-line) (point))
428 (progn (forward-line 1) (point))))
429 ;; Beginning of the article.
435 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
436 (if leave-delim (progn (forward-line 1) (point))
437 (match-beginning 0)))
440 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
442 (if (and (not (bobp)) leave-delim)
443 (progn (forward-line -2) (point))
444 (match-beginning 0)))
446 (goto-char (point-min))
447 ;; Only delete the article if no other groups owns it as well.
448 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
449 (delete-region (point-min) (point-max))))))
451 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
452 (if (or (not nnbabyl-mbox-buffer)
453 (not (buffer-name nnbabyl-mbox-buffer)))
454 (save-excursion (nnbabyl-read-mbox)))
455 (or nnbabyl-group-alist
456 (nnmail-activate 'nnbabyl))
458 (if (assoc newsgroup nnbabyl-group-alist)
459 (setq nnbabyl-current-group newsgroup)
460 (setq nnbabyl-status-string "No such group in file")
464 (defun nnbabyl-article-string (article)
465 (if (numberp article)
466 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
467 (int-to-string article) " ")
468 (concat "\nMessage-ID: " article)))
470 (defun nnbabyl-article-group-number ()
472 (goto-char (point-min))
473 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
475 (cons (buffer-substring (match-beginning 1) (match-end 1))
477 (buffer-substring (match-beginning 2) (match-end 2)))))))
479 (defun nnbabyl-insert-lines ()
480 "Insert how many lines and chars there are in the body of the mail."
483 (goto-char (point-min))
484 (if (search-forward "\n\n" nil t)
486 ;; There may be an EOOH line here...
487 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
488 (search-forward "\n\n" nil t))
489 (setq chars (- (point-max) (point)))
490 (setq lines (- (count-lines (point) (point-max)) 1))
491 ;; Move back to the end of the headers.
492 (goto-char (point-min))
493 (search-forward "\n\n" nil t)
496 (if (re-search-backward "^Lines: " nil t)
497 (delete-region (point) (progn (forward-line 1) (point)))))
498 (insert (format "Lines: %d\n" lines))
501 (defun nnbabyl-save-mail ()
502 ;; Called narrowed to an article.
503 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
504 (nnbabyl-insert-lines)
505 (nnmail-insert-xref group-art)
506 (nnbabyl-insert-newsgroup-line group-art)
507 (run-hooks 'nnbabyl-prepare-save-mail-hook)
510 (defun nnbabyl-insert-newsgroup-line (group-art)
512 (goto-char (point-min))
513 (while (looking-at "From ")
514 (replace-match "Mail-from: From " t t)
516 ;; If there is a C-l at the beginning of the narrowed region, this
517 ;; isn't really a "save", but rather a "scan".
518 (goto-char (point-min))
519 (or (looking-at "\^L")
521 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
522 (goto-char (point-max))
524 (if (search-forward "\n\n" nil t)
528 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
529 (car (car group-art)) (cdr (car group-art))
530 (current-time-string)))
531 (setq group-art (cdr group-art)))))
534 (defun nnbabyl-active-number (group)
535 ;; Find the next article number in GROUP.
536 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
538 (setcdr active (1+ (cdr active)))
539 ;; This group is new, so we create a new entry for it.
540 ;; This might be a bit naughty... creating groups on the drop of
541 ;; a hat, but I don't know...
542 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
543 nnbabyl-group-alist)))
546 (defun nnbabyl-read-mbox ()
547 (nnmail-activate 'nnbabyl)
548 (or (file-exists-p nnbabyl-mbox-file)
550 (set-buffer (setq nnbabyl-mbox-buffer
551 (create-file-buffer nnbabyl-mbox-file)))
552 (setq buffer-file-name nnbabyl-mbox-file)
553 (insert "BABYL OPTIONS:\n\n\^_")
554 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
556 (if (and nnbabyl-mbox-buffer
557 (buffer-name nnbabyl-mbox-buffer)
559 (set-buffer nnbabyl-mbox-buffer)
560 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
563 (let ((delim (concat "^" nnbabyl-mail-delimiter))
565 (set-buffer (setq nnbabyl-mbox-buffer
566 (nnheader-find-file-noselect
567 nnbabyl-mbox-file nil 'raw)))
569 (setq nnbabyl-previous-buffer-mode
570 (cons (cons (point-min) (point-max))
573 (buffer-disable-undo (current-buffer))
575 (setq buffer-read-only nil)
578 (goto-char (point-min))
579 (re-search-forward delim nil t)
580 (setq start (match-end 0))
581 (while (re-search-forward delim nil t)
582 (setq end (match-end 0))
583 (or (search-backward "\nX-Gnus-Newsgroup: " start t)
589 (narrow-to-region start end)
591 (setq end (point-max))))))
592 (goto-char (setq start end)))
593 (and (buffer-modified-p (current-buffer)) (save-buffer))
594 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
596 (defun nnbabyl-remove-incoming-delims ()
597 (goto-char (point-min))
598 (while (search-forward "\^_" nil t)
599 (replace-match "?" t t)))
603 ;;; nnbabyl.el ends here