2001-10-29 Simon Josefsson <jas@extundo.com>
[gnus] / lisp / nnbabyl.el
1 ;;; nnbabyl.el --- rmail mbox access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1099, 2000
4 ;;      Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; For an overview of what the interface functions do, please see the
30 ;; Gnus sources.
31
32 ;;; Code:
33
34 (require 'nnheader)
35 (condition-case nil
36     (require 'rmail)
37   (t (nnheader-message
38       5 "Ignore rmail errors from this file, you don't have rmail")))
39 (require 'nnmail)
40 (require 'nnoo)
41 (eval-when-compile (require 'cl))
42
43 (nnoo-declare nnbabyl)
44
45 (defvoo nnbabyl-mbox-file (expand-file-name "~/RMAIL")
46   "The name of the rmail box file in the users home directory.
47
48 This variable is a virtual server slot.  See the Gnus manual for details.")
49
50 (defvoo nnbabyl-active-file (expand-file-name "~/.rmail-active")
51   "The name of the active file for the rmail box.
52
53 This variable is a virtual server slot.  See the Gnus manual for details.")
54
55 (defvoo nnbabyl-get-new-mail t
56   "If non-nil, nnbabyl will check the incoming mail file and split the mail.
57
58 This variable is a virtual server slot.  See the Gnus manual for details.")
59
60
61 (defvoo nnbabyl-prepare-save-mail-hook nil
62   "Hook run narrowed to an article before saving.
63
64 This variable is a virtual server slot.  See the Gnus manual for details.")
65
66 \f
67
68 (defvar nnbabyl-mail-delimiter "\^_")
69
70 (defconst nnbabyl-version "nnbabyl 1.0"
71   "nnbabyl version.")
72
73 (defvoo nnbabyl-mbox-buffer nil)
74 (defvoo nnbabyl-current-group nil)
75 (defvoo nnbabyl-status-string "")
76 (defvoo nnbabyl-group-alist nil)
77 (defvoo nnbabyl-active-timestamp nil)
78
79 (defvoo nnbabyl-previous-buffer-mode nil)
80
81 (eval-and-compile
82   (autoload 'gnus-set-text-properties "gnus-ems"))
83
84 \f
85
86 ;;; Interface functions
87
88 (nnoo-define-basics nnbabyl)
89
90 (deffoo nnbabyl-retrieve-headers (articles &optional group server fetch-old)
91   (save-excursion
92     (set-buffer nntp-server-buffer)
93     (erase-buffer)
94     (let ((number (length articles))
95           (count 0)
96           (delim (concat "^" nnbabyl-mail-delimiter))
97           article art-string start stop)
98       (nnbabyl-possibly-change-newsgroup group server)
99       (while (setq article (pop articles))
100         (setq art-string (nnbabyl-article-string article))
101         (set-buffer nnbabyl-mbox-buffer)
102         (end-of-line)
103         (when (or (search-forward art-string nil t)
104                   (search-backward art-string nil t))
105           (unless (re-search-backward delim nil t)
106             (goto-char (point-min)))
107           (while (and (not (looking-at ".+:"))
108                       (zerop (forward-line 1))))
109           (setq start (point))
110           (search-forward "\n\n" nil t)
111           (setq stop (1- (point)))
112           (set-buffer nntp-server-buffer)
113           (insert "221 ")
114           (princ article (current-buffer))
115           (insert " Article retrieved.\n")
116           (insert-buffer-substring nnbabyl-mbox-buffer start stop)
117           (goto-char (point-max))
118           (insert ".\n"))
119         (and (numberp nnmail-large-newsgroup)
120              (> number nnmail-large-newsgroup)
121              (zerop (% (incf count) 20))
122              (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
123                                (/ (* count 100) number))))
124
125       (and (numberp nnmail-large-newsgroup)
126            (> number nnmail-large-newsgroup)
127            (nnheader-message 5 "nnbabyl: Receiving headers...done"))
128
129       (set-buffer nntp-server-buffer)
130       (nnheader-fold-continuation-lines)
131       'headers)))
132
133 (deffoo nnbabyl-open-server (server &optional defs)
134   (nnoo-change-server 'nnbabyl server defs)
135   (nnbabyl-create-mbox)
136   (cond
137    ((not (file-exists-p nnbabyl-mbox-file))
138     (nnbabyl-close-server)
139     (nnheader-report 'nnbabyl "No such file: %s" nnbabyl-mbox-file))
140    ((file-directory-p nnbabyl-mbox-file)
141     (nnbabyl-close-server)
142     (nnheader-report 'nnbabyl "Not a regular file: %s" nnbabyl-mbox-file))
143    (t
144     (nnheader-report 'nnbabyl "Opened server %s using mbox %s" server
145                      nnbabyl-mbox-file)
146     t)))
147
148 (deffoo nnbabyl-close-server (&optional server)
149   ;; Restore buffer mode.
150   (when (and (nnbabyl-server-opened)
151              nnbabyl-previous-buffer-mode)
152     (save-excursion
153       (set-buffer nnbabyl-mbox-buffer)
154       (narrow-to-region
155        (caar nnbabyl-previous-buffer-mode)
156        (cdar nnbabyl-previous-buffer-mode))
157       (funcall (cdr nnbabyl-previous-buffer-mode))))
158   (nnoo-close-server 'nnbabyl server)
159   (setq nnbabyl-mbox-buffer nil)
160   t)
161
162 (deffoo nnbabyl-server-opened (&optional server)
163   (and (nnoo-current-server-p 'nnbabyl server)
164        nnbabyl-mbox-buffer
165        (buffer-name nnbabyl-mbox-buffer)
166        nntp-server-buffer
167        (buffer-name nntp-server-buffer)))
168
169 (deffoo nnbabyl-request-article (article &optional newsgroup server buffer)
170   (nnbabyl-possibly-change-newsgroup newsgroup server)
171   (save-excursion
172     (set-buffer nnbabyl-mbox-buffer)
173     (goto-char (point-min))
174     (when (search-forward (nnbabyl-article-string article) nil t)
175       (let (start stop summary-line)
176         (unless (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
177           (goto-char (point-min))
178           (end-of-line))
179         (while (and (not (looking-at ".+:"))
180                     (zerop (forward-line 1))))
181         (setq start (point))
182         (or (when (re-search-forward
183                    (concat "^" nnbabyl-mail-delimiter) nil t)
184               (beginning-of-line)
185               t)
186             (goto-char (point-max)))
187         (setq stop (point))
188         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
189           (set-buffer nntp-server-buffer)
190           (erase-buffer)
191           (insert-buffer-substring nnbabyl-mbox-buffer start stop)
192           (goto-char (point-min))
193           ;; If there is an EOOH header, then we have to remove some
194           ;; duplicated headers.
195           (setq summary-line (looking-at "Summary-line:"))
196           (when (search-forward "\n*** EOOH ***" nil t)
197             (if summary-line
198                 ;; The headers to be deleted are located before the
199                 ;; EOOH line...
200                 (delete-region (point-min) (progn (forward-line 1)
201                                                   (point)))
202               ;; ...or after.
203               (delete-region (progn (beginning-of-line) (point))
204                              (or (search-forward "\n\n" nil t)
205                                  (point)))))
206           (if (numberp article)
207               (cons nnbabyl-current-group article)
208             (nnbabyl-article-group-number)))))))