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 (assoc group nnbabyl-group-alist)))
206 ((null (nnbabyl-possibly-change-newsgroup group))
207 (nnheader-report 'nnbabyl "No such group: %s" group))
209 (nnheader-report 'nnbabyl "Selected group %s" group)
211 ((> (car active) (cdr active))
212 (nnheader-report 'nnbabyl "Empty group %s" group))
214 (nnheader-report 'nnbabyl "Selected group %s" group)
216 (set-buffer nntp-server-buffer)
218 (insert (format "211 %d %d %d %s\n"
219 (1+ (- (cdr (car (cdr active)))
220 (car (car (cdr active)))))
221 (car (car (cdr active)))
222 (cdr (car (cdr active)))
226 (defun nnbabyl-request-scan (&optional group server)
232 (set-buffer nnbabyl-mbox-buffer)
234 nnbabyl-mbox-file group
237 (let ((in-buf (current-buffer)))
238 (goto-char (point-min))
239 (while (search-forward "\n\^_\n" nil t)
241 (set-buffer nnbabyl-mbox-buffer)
242 (goto-char (point-max))
243 (search-backward "\n\^_" nil t)
244 (goto-char (match-end 0))
245 (insert-buffer-substring in-buf))))))
247 (defun nnbabyl-close-group (group &optional server)
250 (defun nnbabyl-request-create-group (group &optional server)
251 (nnmail-activate 'nnbabyl)
252 (or (assoc group nnbabyl-group-alist)
254 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 0)))
255 nnbabyl-group-alist))
256 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))
259 (defun nnbabyl-request-list (&optional server)
261 (or (nnmail-find-file nnbabyl-active-file)
263 (setq nnbabyl-group-alist (nnmail-get-active))
264 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
265 (nnmail-find-file nnbabyl-active-file)))))
267 (defun nnbabyl-request-newgroups (date &optional server)
268 (nnbabyl-request-list server))
270 (defun nnbabyl-request-list-newsgroups (&optional server)
271 (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
274 (defun nnbabyl-request-post (&optional server)
275 (mail-send-and-exit nil))
277 (defun nnbabyl-request-expire-articles
278 (articles newsgroup &optional server force)
279 (nnbabyl-possibly-change-newsgroup newsgroup)
282 (nnmail-activate 'nnbabyl)
285 (set-buffer nnbabyl-mbox-buffer)
286 (set-text-properties (point-min) (point-max) nil)
287 (while (and articles is-old)
288 (goto-char (point-min))
289 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
291 (nnmail-expired-article-p
294 (point) (progn (end-of-line) (point))) force))
296 (nnheader-message 5 "Deleting article %d in %s..."
297 (car articles) newsgroup)
298 (nnbabyl-delete-mail))
299 (setq rest (cons (car articles) rest))))
300 (setq articles (cdr articles)))
302 ;; Find the lowest active article in this group.
303 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
304 (goto-char (point-min))
305 (while (and (not (search-forward
306 (nnbabyl-article-string (car active)) nil t))
307 (<= (car active) (cdr active)))
308 (setcar active (1+ (car active)))
309 (goto-char (point-min))))
310 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
311 (nconc rest articles))))
313 (defun nnbabyl-request-move-article
314 (article group server accept-form &optional last)
315 (nnbabyl-possibly-change-newsgroup group)
316 (let ((buf (get-buffer-create " *nnbabyl move*"))
319 (nnbabyl-request-article article group server)
322 (insert-buffer-substring nntp-server-buffer)
323 (goto-char (point-min))
324 (if (re-search-forward
326 (save-excursion (search-forward "\n\n" nil t) (point)) t)
327 (delete-region (progn (beginning-of-line) (point))
328 (progn (forward-line 1) (point))))
329 (setq result (eval accept-form))
330 (kill-buffer (current-buffer))
333 (set-buffer nnbabyl-mbox-buffer)
334 (goto-char (point-min))
335 (if (search-forward (nnbabyl-article-string article) nil t)
336 (nnbabyl-delete-mail))
337 (and last (save-buffer))))
340 (defun nnbabyl-request-accept-article (group &optional last)
341 (let ((buf (current-buffer))
344 (nnmail-activate 'nnbabyl)
346 (goto-char (point-min))
347 (search-forward "\n\n" nil t)
350 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
351 (delete-region (point) (progn (forward-line 1) (point)))))
352 (let ((nnmail-split-methods
353 (if (stringp group) (list (list group ""))
354 nnmail-split-methods)))
355 (setq result (car (nnbabyl-save-mail))))
356 (set-buffer nnbabyl-mbox-buffer)
357 (goto-char (point-max))
358 (search-backward "\n\^_")
359 (goto-char (match-end 0))
360 (insert-buffer-substring buf)
364 nnbabyl-group-alist nnbabyl-active-file)))
367 (defun nnbabyl-request-replace-article (article group buffer)
368 (nnbabyl-possibly-change-newsgroup group)
370 (set-buffer nnbabyl-mbox-buffer)
371 (goto-char (point-min))
372 (if (not (search-forward (nnbabyl-article-string article) nil t))
374 (nnbabyl-delete-mail t t)
375 (insert-buffer-substring buffer)
379 (defun nnbabyl-request-delete-group (group &optional force server)
380 (nnbabyl-possibly-change-newsgroup group)
381 ;; Delete all articles in GROUP.
383 () ; Don't delete the articles.
385 (set-buffer nnbabyl-mbox-buffer)
386 (goto-char (point-min))
387 ;; Delete all articles in this group.
388 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
390 (while (search-forward ident nil t)
392 (nnbabyl-delete-mail))
393 (and found (save-buffer)))))
394 ;; Remove the group from all structures.
395 (setq nnbabyl-group-alist
396 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
397 nnbabyl-current-group nil)
398 ;; Save the active file.
399 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
402 (defun nnbabyl-request-rename-group (group new-name &optional server)
403 (nnbabyl-possibly-change-newsgroup group)
405 (set-buffer nnbabyl-mbox-buffer)
406 (goto-char (point-min))
407 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
408 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
410 (while (search-forward ident nil t)
411 (replace-match new-ident t t)
413 (and found (save-buffer))))
414 (let ((entry (assoc group nnbabyl-group-alist)))
415 (and entry (setcar entry new-name))
416 (setq nnbabyl-current-group nil)
417 ;; Save the new group alist.
418 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
422 ;;; Internal functions.
424 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
425 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
427 (defun nnbabyl-delete-mail (&optional force leave-delim)
428 ;; Delete the current X-Gnus-Newsgroup line.
431 (progn (beginning-of-line) (point))
432 (progn (forward-line 1) (point))))
433 ;; Beginning of the article.
439 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
440 (if leave-delim (progn (forward-line 1) (point))
441 (match-beginning 0)))
444 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
446 (if (and (not (bobp)) leave-delim)
447 (progn (forward-line -2) (point))
448 (match-beginning 0)))
450 (goto-char (point-min))
451 ;; Only delete the article if no other groups owns it as well.
452 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
453 (delete-region (point-min) (point-max))))))
455 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
456 (if (or (not nnbabyl-mbox-buffer)
457 (not (buffer-name nnbabyl-mbox-buffer)))
458 (save-excursion (nnbabyl-read-mbox)))
459 (or nnbabyl-group-alist
460 (nnmail-activate 'nnbabyl))
462 (if (assoc newsgroup nnbabyl-group-alist)
463 (setq nnbabyl-current-group newsgroup)
464 (setq nnbabyl-status-string "No such group in file")
467 (defun nnbabyl-article-string (article)
468 (if (numberp article)
469 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
470 (int-to-string article) " ")
471 (concat "\nMessage-ID: " article)))
473 (defun nnbabyl-article-group-number ()
475 (goto-char (point-min))
476 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
478 (cons (buffer-substring (match-beginning 1) (match-end 1))
480 (buffer-substring (match-beginning 2) (match-end 2)))))))
482 (defun nnbabyl-insert-lines ()
483 "Insert how many lines and chars there are in the body of the mail."
486 (goto-char (point-min))
487 (if (search-forward "\n\n" nil t)
489 ;; There may be an EOOH line here...
490 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
491 (search-forward "\n\n" nil t))
492 (setq chars (- (point-max) (point)))
493 (setq lines (- (count-lines (point) (point-max)) 1))
494 ;; Move back to the end of the headers.
495 (goto-char (point-min))
496 (search-forward "\n\n" nil t)
499 (if (re-search-backward "^Lines: " nil t)
500 (delete-region (point) (progn (forward-line 1) (point)))))
501 (insert (format "Lines: %d\n" lines))
504 (defun nnbabyl-save-mail ()
505 ;; Called narrowed to an article.
506 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
507 (nnbabyl-insert-lines)
508 (nnmail-insert-xref group-art)
509 (nnbabyl-insert-newsgroup-line group-art)
510 (run-hooks 'nnbabyl-prepare-save-mail-hook)
513 (defun nnbabyl-insert-newsgroup-line (group-art)
515 (goto-char (point-min))
516 (while (looking-at "From ")
517 (replace-match "Mail-from: From " t t)
519 ;; If there is a C-l at the beginning of the narrowed region, this
520 ;; isn't really a "save", but rather a "scan".
521 (goto-char (point-min))
522 (or (looking-at "\^L")
524 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
525 (goto-char (point-max))
527 (if (search-forward "\n\n" nil t)
531 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
532 (car (car group-art)) (cdr (car group-art))
533 (current-time-string)))
534 (setq group-art (cdr group-art)))))
537 (defun nnbabyl-active-number (group)
538 ;; Find the next article number in GROUP.
539 (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
541 (setcdr active (1+ (cdr active)))
542 ;; This group is new, so we create a new entry for it.
543 ;; This might be a bit naughty... creating groups on the drop of
544 ;; a hat, but I don't know...
545 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
546 nnbabyl-group-alist)))
549 (defun nnbabyl-read-mbox ()
550 (nnmail-activate 'nnbabyl)
551 (or (file-exists-p nnbabyl-mbox-file)
553 (set-buffer (setq nnbabyl-mbox-buffer
554 (create-file-buffer nnbabyl-mbox-file)))
555 (setq buffer-file-name nnbabyl-mbox-file)
556 (insert "BABYL OPTIONS:\n\n\^_")
557 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
559 (if (and nnbabyl-mbox-buffer
560 (buffer-name nnbabyl-mbox-buffer)
562 (set-buffer nnbabyl-mbox-buffer)
563 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
566 (let ((delim (concat "^" nnbabyl-mail-delimiter))
568 (set-buffer (setq nnbabyl-mbox-buffer
569 (nnheader-find-file-noselect
570 nnbabyl-mbox-file nil 'raw)))
572 (setq nnbabyl-previous-buffer-mode
573 (cons (cons (point-min) (point-max))
576 (buffer-disable-undo (current-buffer))
578 (setq buffer-read-only nil)
581 (goto-char (point-min))
582 (re-search-forward delim nil t)
583 (setq start (match-end 0))
584 (while (re-search-forward delim nil t)
585 (setq end (match-end 0))
586 (or (search-backward "\nX-Gnus-Newsgroup: " start t)
592 (narrow-to-region start end)
594 (setq end (point-max))))))
595 (goto-char (setq start end)))
596 (and (buffer-modified-p (current-buffer)) (save-buffer))
597 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
599 (defun nnbabyl-remove-incoming-delims ()
600 (goto-char (point-min))
601 (while (search-forward "\^_" nil t)
602 (replace-match "?" t t)))
606 ;;; nnbabyl.el ends here