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)
65 (autoload 'gnus-set-text-properties "gnus-ems"))
69 (defvar nnbabyl-current-server nil)
70 (defvar nnbabyl-server-alist nil)
71 (defvar nnbabyl-server-variables
72 `((nnbabyl-mbox-file ,nnbabyl-mbox-file)
73 (nnbabyl-active-file ,nnbabyl-active-file)
74 (nnbabyl-get-new-mail ,nnbabyl-get-new-mail)
75 (nnbabyl-current-group nil)
76 (nnbabyl-status-string "")
77 (nnbabyl-previous-buffer-mode nil)
78 (nnbabyl-group-alist nil)))
82 ;;; Interface functions
84 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server fetch-old)
86 (set-buffer nntp-server-buffer)
88 (let ((number (length sequence))
90 article art-string start stop)
91 (nnbabyl-possibly-change-newsgroup newsgroup server)
93 (setq article (car sequence))
94 (setq art-string (nnbabyl-article-string article))
95 (set-buffer nnbabyl-mbox-buffer)
96 (if (or (search-forward art-string nil t)
97 (search-backward art-string nil t))
99 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
100 (while (and (not (looking-at ".+:"))
101 (zerop (forward-line 1))))
103 (search-forward "\n\n" nil t)
104 (setq stop (1- (point)))
105 (set-buffer nntp-server-buffer)
106 (insert "221 " (int-to-string article) " Article retrieved.\n")
107 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
108 (goto-char (point-max))
110 (setq sequence (cdr sequence))
111 (setq count (1+ count))
112 (and (numberp nnmail-large-newsgroup)
113 (> number nnmail-large-newsgroup)
115 (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
116 (/ (* count 100) number))))
118 (and (numberp nnmail-large-newsgroup)
119 (> number nnmail-large-newsgroup)
120 (nnheader-message 5 "nnbabyl: Receiving headers...done"))
122 (set-buffer nntp-server-buffer)
123 (nnheader-fold-continuation-lines)
126 (defun nnbabyl-open-server (server &optional defs)
127 (nnheader-change-server 'nnbabyl server defs)
129 ((not (file-exists-p nnbabyl-mbox-file))
130 (nnbabyl-close-server)
131 (nnheader-report 'nnbabyl "No such file: %s" nnbabyl-mbox-file))
132 ((file-directory-p nnbabyl-mbox-file)
133 (nnbabyl-close-server)
134 (nnheader-report 'nnbabyl "Not a regular file: %s" nnbabyl-mbox-file))
136 (nnheader-report 'nnbabyl "Opened server %s using mbox %s" 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 (caar nnbabyl-previous-buffer-mode)
148 (cdar 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 server)
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 server))
209 (nnheader-report 'nnbabyl "No such group: %s" group))
211 (nnheader-report 'nnbabyl "Selected group %s" group)
214 (nnheader-report 'nnbabyl "Selected group %s" group)
215 (nnheader-insert "211 %d %d %d %s\n"
216 (1+ (- (cdr active) (car active)))
217 (car active) (cdr active) group)
220 (defun nnbabyl-request-scan (&optional group server)
226 (set-buffer nnbabyl-mbox-buffer)
228 nnbabyl-mbox-file group
231 (let ((in-buf (current-buffer)))
232 (goto-char (point-min))
233 (while (search-forward "\n\^_\n" nil t)
235 (set-buffer nnbabyl-mbox-buffer)
236 (goto-char (point-max))
237 (search-backward "\n\^_" nil t)
238 (goto-char (match-end 0))
239 (insert-buffer-substring in-buf))))))
241 (defun nnbabyl-close-group (group &optional server)
244 (defun nnbabyl-request-create-group (group &optional server)
245 (nnmail-activate 'nnbabyl)
246 (unless (assoc group nnbabyl-group-alist)
247 (setq nnbabyl-group-alist (cons (list group (cons 1 0))
248 nnbabyl-group-alist))
249 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
252 (defun nnbabyl-request-list (&optional server)
254 (or (nnmail-find-file nnbabyl-active-file)
256 (setq nnbabyl-group-alist (nnmail-get-active))
257 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
258 (nnmail-find-file nnbabyl-active-file)))))
260 (defun nnbabyl-request-newgroups (date &optional server)
261 (nnbabyl-request-list server))
263 (defun nnbabyl-request-list-newsgroups (&optional server)
264 (nnheader-report 'nnbabyl "nnbabyl: LIST NEWSGROUPS is not implemented."))
266 (defun nnbabyl-request-expire-articles
267 (articles newsgroup &optional server force)
268 (nnbabyl-possibly-change-newsgroup newsgroup server)
271 (nnmail-activate 'nnbabyl)
274 (set-buffer nnbabyl-mbox-buffer)
275 (gnus-set-text-properties (point-min) (point-max) nil)
276 (while (and articles is-old)
277 (goto-char (point-min))
278 (if (search-forward (nnbabyl-article-string (car articles)) nil t)
280 (nnmail-expired-article-p
283 (point) (progn (end-of-line) (point))) force))
285 (nnheader-message 5 "Deleting article %d in %s..."
286 (car articles) newsgroup)
287 (nnbabyl-delete-mail))
288 (setq rest (cons (car articles) rest))))
289 (setq articles (cdr articles)))
291 ;; Find the lowest active article in this group.
292 (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
293 (goto-char (point-min))
294 (while (and (not (search-forward
295 (nnbabyl-article-string (car active)) nil t))
296 (<= (car active) (cdr active)))
297 (setcar active (1+ (car active)))
298 (goto-char (point-min))))
299 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
300 (nconc rest articles))))
302 (defun nnbabyl-request-move-article
303 (article group server accept-form &optional last)
304 (nnbabyl-possibly-change-newsgroup group server)
305 (let ((buf (get-buffer-create " *nnbabyl move*"))
308 (nnbabyl-request-article article group server)
311 (insert-buffer-substring nntp-server-buffer)
312 (goto-char (point-min))
313 (if (re-search-forward
315 (save-excursion (search-forward "\n\n" nil t) (point)) t)
316 (delete-region (progn (beginning-of-line) (point))
317 (progn (forward-line 1) (point))))
318 (setq result (eval accept-form))
319 (kill-buffer (current-buffer))
322 (set-buffer nnbabyl-mbox-buffer)
323 (goto-char (point-min))
324 (if (search-forward (nnbabyl-article-string article) nil t)
325 (nnbabyl-delete-mail))
326 (and last (save-buffer))))
329 (defun nnbabyl-request-accept-article (group &optional server last)
330 (nnbabyl-possibly-change-newsgroup group server)
331 (let ((buf (current-buffer))
334 (nnmail-activate 'nnbabyl)
336 (goto-char (point-min))
337 (search-forward "\n\n" nil t)
340 (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
341 (delete-region (point) (progn (forward-line 1) (point)))))
342 (let ((nnmail-split-methods
343 (if (stringp group) (list (list group ""))
344 nnmail-split-methods)))
345 (setq result (car (nnbabyl-save-mail))))
346 (set-buffer nnbabyl-mbox-buffer)
347 (goto-char (point-max))
348 (search-backward "\n\^_")
349 (goto-char (match-end 0))
350 (insert-buffer-substring buf)
354 nnbabyl-group-alist nnbabyl-active-file)))
357 (defun nnbabyl-request-replace-article (article group buffer)
358 (nnbabyl-possibly-change-newsgroup group)
360 (set-buffer nnbabyl-mbox-buffer)
361 (goto-char (point-min))
362 (if (not (search-forward (nnbabyl-article-string article) nil t))
364 (nnbabyl-delete-mail t t)
365 (insert-buffer-substring buffer)
369 (defun nnbabyl-request-delete-group (group &optional force server)
370 (nnbabyl-possibly-change-newsgroup group server)
371 ;; Delete all articles in GROUP.
373 () ; Don't delete the articles.
375 (set-buffer nnbabyl-mbox-buffer)
376 (goto-char (point-min))
377 ;; Delete all articles in this group.
378 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
380 (while (search-forward ident nil t)
382 (nnbabyl-delete-mail))
383 (and found (save-buffer)))))
384 ;; Remove the group from all structures.
385 (setq nnbabyl-group-alist
386 (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
387 nnbabyl-current-group nil)
388 ;; Save the active file.
389 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
392 (defun nnbabyl-request-rename-group (group new-name &optional server)
393 (nnbabyl-possibly-change-newsgroup group server)
395 (set-buffer nnbabyl-mbox-buffer)
396 (goto-char (point-min))
397 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
398 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
400 (while (search-forward ident nil t)
401 (replace-match new-ident t t)
403 (and found (save-buffer))))
404 (let ((entry (assoc group nnbabyl-group-alist)))
405 (and entry (setcar entry new-name))
406 (setq nnbabyl-current-group nil)
407 ;; Save the new group alist.
408 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
412 ;;; Internal functions.
414 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
415 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
417 (defun nnbabyl-delete-mail (&optional force leave-delim)
418 ;; Delete the current X-Gnus-Newsgroup line.
421 (progn (beginning-of-line) (point))
422 (progn (forward-line 1) (point))))
423 ;; Beginning of the article.
429 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
430 (if leave-delim (progn (forward-line 1) (point))
431 (match-beginning 0)))
434 (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter)
436 (if (and (not (bobp)) leave-delim)
437 (progn (forward-line -2) (point))
438 (match-beginning 0)))
440 (goto-char (point-min))
441 ;; Only delete the article if no other groups owns it as well.
442 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
443 (delete-region (point-min) (point-max))))))
445 (defun nnbabyl-possibly-change-newsgroup (newsgroup &optional server)
447 (not (nnbabyl-server-opened server)))
448 (nnbabyl-open-server server))
449 (if (or (not nnbabyl-mbox-buffer)
450 (not (buffer-name nnbabyl-mbox-buffer)))
451 (save-excursion (nnbabyl-read-mbox)))
452 (or nnbabyl-group-alist
453 (nnmail-activate 'nnbabyl))
455 (if (assoc newsgroup nnbabyl-group-alist)
456 (setq nnbabyl-current-group newsgroup)
457 (nnheader-report 'nnbabyl "No such group in file"))
460 (defun nnbabyl-article-string (article)
461 (if (numberp article)
462 (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"
463 (int-to-string article) " ")
464 (concat "\nMessage-ID: " article)))
466 (defun nnbabyl-article-group-number ()
468 (goto-char (point-min))
469 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
471 (cons (buffer-substring (match-beginning 1) (match-end 1))
473 (buffer-substring (match-beginning 2) (match-end 2)))))))
475 (defun nnbabyl-insert-lines ()
476 "Insert how many lines and chars there are in the body of the mail."
479 (goto-char (point-min))
480 (if (search-forward "\n\n" nil t)
482 ;; There may be an EOOH line here...
483 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
484 (search-forward "\n\n" nil t))
485 (setq chars (- (point-max) (point)))
486 (setq lines (- (count-lines (point) (point-max)) 1))
487 ;; Move back to the end of the headers.
488 (goto-char (point-min))
489 (search-forward "\n\n" nil t)
492 (if (re-search-backward "^Lines: " nil t)
493 (delete-region (point) (progn (forward-line 1) (point)))))
494 (insert (format "Lines: %d\n" lines))
497 (defun nnbabyl-save-mail ()
498 ;; Called narrowed to an article.
499 (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
500 (nnbabyl-insert-lines)
501 (nnmail-insert-xref group-art)
502 (nnbabyl-insert-newsgroup-line group-art)
503 (run-hooks 'nnbabyl-prepare-save-mail-hook)
506 (defun nnbabyl-insert-newsgroup-line (group-art)
508 (goto-char (point-min))
509 (while (looking-at "From ")
510 (replace-match "Mail-from: From " t t)
512 ;; If there is a C-l at the beginning of the narrowed region, this
513 ;; isn't really a "save", but rather a "scan".
514 (goto-char (point-min))
515 (or (looking-at "\^L")
517 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
518 (goto-char (point-max))
520 (if (search-forward "\n\n" nil t)
524 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
525 (caar group-art) (cdar group-art)
526 (current-time-string)))
527 (setq group-art (cdr group-art)))))
530 (defun nnbabyl-active-number (group)
531 ;; Find the next article number in GROUP.
532 (let ((active (cadr (assoc group nnbabyl-group-alist))))
534 (setcdr active (1+ (cdr active)))
535 ;; This group is new, so we create a new entry for it.
536 ;; This might be a bit naughty... creating groups on the drop of
537 ;; a hat, but I don't know...
538 (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
539 nnbabyl-group-alist)))
542 (defun nnbabyl-read-mbox ()
543 (nnmail-activate 'nnbabyl)
544 (or (file-exists-p nnbabyl-mbox-file)
546 (set-buffer (setq nnbabyl-mbox-buffer
547 (create-file-buffer nnbabyl-mbox-file)))
548 (setq buffer-file-name nnbabyl-mbox-file)
549 (insert "BABYL OPTIONS:\n\n\^_")
550 (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
552 (if (and nnbabyl-mbox-buffer
553 (buffer-name nnbabyl-mbox-buffer)
555 (set-buffer nnbabyl-mbox-buffer)
556 (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
559 (let ((delim (concat "^" nnbabyl-mail-delimiter))
561 (set-buffer (setq nnbabyl-mbox-buffer
562 (nnheader-find-file-noselect
563 nnbabyl-mbox-file nil 'raw)))
565 (setq nnbabyl-previous-buffer-mode
566 (cons (cons (point-min) (point-max))
569 (buffer-disable-undo (current-buffer))
571 (setq buffer-read-only nil)
574 (goto-char (point-min))
575 (re-search-forward delim nil t)
576 (setq start (match-end 0))
577 (while (re-search-forward delim nil t)
578 (setq end (match-end 0))
579 (or (search-backward "\nX-Gnus-Newsgroup: " start t)
585 (narrow-to-region start end)
587 (setq end (point-max))))))
588 (goto-char (setq start end)))
589 (and (buffer-modified-p (current-buffer)) (save-buffer))
590 (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
592 (defun nnbabyl-remove-incoming-delims ()
593 (goto-char (point-min))
594 (while (search-forward "\^_" nil t)
595 (replace-match "?" t t)))
599 ;;; nnbabyl.el ends here