*** empty log message ***
[gnus] / lisp / nnbabyl.el
1 ;;; nnbabyl.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; For an overview of what the interface functions do, please see the
27 ;; Gnus sources.  
28
29 ;;; Code:
30
31 (require 'nnheader)
32 (require 'rmail)
33 (require 'nnmail)
34
35 (defvar nnbabyl-mbox-file (expand-file-name "~/RMAIL")
36   "The name of the mail box file in the users home directory.")
37
38 (defvar nnbabyl-active-file (expand-file-name "~/.rmail-active")
39   "The name of the active file for the mail box.")
40
41 (defvar nnbabyl-get-new-mail t
42   "If non-nil, nnml will check the incoming mail file and split the mail.")
43
44 \f
45
46 (defvar nnbabyl-mail-delimiter "\^_")
47
48 (defconst nnbabyl-version "nnbabyl 0.1"
49   "nnbabyl version.")
50
51 (defvar nnbabyl-current-group nil
52   "Current nnbabyl news group directory.")
53
54 (defconst nnbabyl-mbox-buffer " *nnbabyl mbox buffer*")
55
56 (defvar nnbabyl-status-string "")
57
58 (defvar nnbabyl-group-alist nil)
59
60 ;;; Interface functions
61
62 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server)
63   (save-excursion
64     (set-buffer nntp-server-buffer)
65     (erase-buffer)
66     (let ((file nil)
67           (number (length sequence))
68           (count 0)
69           beg article art-string start stop)
70       (nnbabyl-possibly-change-newsgroup newsgroup)
71       (while sequence
72         (setq article (car sequence))
73         (setq art-string (nnbabyl-article-string article))
74         (set-buffer nnbabyl-mbox-buffer)
75         (if (or (search-forward art-string nil t)
76                 (progn (goto-char 1)
77                        (search-forward art-string nil t)))
78             (progn
79               (setq start 
80                     (save-excursion
81                       (re-search-backward 
82                        (concat "^" nnbabyl-mail-delimiter) nil t)
83                       (while (and (not (looking-at ".+:"))
84                                   (zerop (forward-line 1))))
85                       (point)))
86               (search-forward "\n\n" nil t)
87               (setq stop (1- (point)))
88               (set-buffer nntp-server-buffer)
89               (insert (format "221 %d Article retrieved.\n" article))
90               (setq beg (point))
91               (insert-buffer-substring nnbabyl-mbox-buffer start stop)
92               (goto-char (point-max))
93               (insert ".\n")))
94         (setq sequence (cdr sequence))
95         (setq count (1+ count))
96         (and (numberp nnmail-large-newsgroup)
97              (> number nnmail-large-newsgroup)
98              (zerop (% count 20))
99              gnus-verbose-backends
100              (message "nnbabyl: Receiving headers... %d%%"
101                       (/ (* count 100) number))))
102
103       (and (numberp nnmail-large-newsgroup)
104            (> number nnmail-large-newsgroup)
105            gnus-verbose-backends
106            (message "nnbabyl: Receiving headers... done"))
107
108       ;; Fold continuation lines.
109       (goto-char 1)
110       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
111         (replace-match " " t t))
112       'headers)))
113
114 (defun nnbabyl-open-server (host &optional service)
115   (setq nnbabyl-status-string "")
116   (setq nnbabyl-group-alist nil)
117   (nnheader-init-server-buffer))
118
119 (defun nnbabyl-close-server (&optional server)
120   t)
121
122 (defun nnbabyl-server-opened (&optional server)
123   (and nntp-server-buffer
124        (get-buffer nntp-server-buffer)))
125
126 (defun nnbabyl-status-message (&optional server)
127   nnbabyl-status-string)
128
129 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
130   (nnbabyl-possibly-change-newsgroup newsgroup)
131   (if (stringp article)
132       nil
133     (save-excursion
134       (set-buffer nnbabyl-mbox-buffer)
135       (goto-char 1)
136       (if (search-forward (nnbabyl-article-string article) nil t)
137           (let (start stop)
138             (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
139             (while (and (not (looking-at ".+:"))
140                         (zerop (forward-line 1))))
141             (setq start (point))
142             (or (and (re-search-forward 
143                       (concat "^" nnbabyl-mail-delimiter) nil t)
144                      (forward-line -1))
145                 (goto-char (point-max)))
146             (setq stop (point))
147             (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
148               (set-buffer nntp-server-buffer)
149               (erase-buffer)
150               (insert-buffer-substring nnbabyl-mbox-buffer start stop)
151               (goto-char (point-min))
152               (if (search-forward "\n*** EOOH ***" nil t)
153                   (progn
154                     (delete-region (progn (beginning-of-line) (point))
155                                    (or (search-forward "\n\n" nil t)
156                                        (point)))))
157               t))))))
158
159 (defun nnbabyl-request-group (group &optional server dont-check)
160   (save-excursion
161     (if (nnbabyl-possibly-change-newsgroup group)
162         (if dont-check
163             t
164           (nnbabyl-get-new-mail)
165           (save-excursion
166             (set-buffer nntp-server-buffer)
167             (erase-buffer)
168             (let ((active (assoc group nnbabyl-group-alist)))
169               (insert (format "211 %d %d %d %s\n" 
170                               (1+ (- (cdr (car (cdr active)))
171                                      (car (car (cdr active)))))
172                               (car (car (cdr active)))
173                               (cdr (car (cdr active)))
174                               (car active))))
175             t)))))
176
177 (defun nnbabyl-close-group (group &optional server)
178   t)
179
180 (defun nnbabyl-request-list (&optional server)
181   (if server (nnbabyl-get-new-mail))
182   (nnmail-find-file nnbabyl-active-file))
183
184 (defun nnbabyl-request-newgroups (date &optional server)
185   (nnbabyl-request-list server))
186
187 (defun nnbabyl-request-list-newsgroups (&optional server)
188   (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
189   nil)
190
191 (defun nnbabyl-request-post (&optional server)
192   (mail-send-and-exit nil))
193
194 (fset 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
195
196 (defun nnbabyl-request-expire-articles (articles newsgroup &optional server force)
197   (nnbabyl-possibly-change-newsgroup newsgroup)
198   (let* ((days (or (and nnmail-expiry-wait-function
199                         (funcall nnmail-expiry-wait-function newsgroup))
200                    nnmail-expiry-wait))
201          article rest)
202     (save-excursion 
203       (set-buffer nnbabyl-mbox-buffer)
204       (while articles
205         (goto-char 1)
206         (if (search-forward (nnbabyl-article-string (car articles)) nil t)
207             (if (or force
208                     (> (nnmail-days-between 
209                         (current-time-string)
210                         (buffer-substring 
211                          (point) (progn (end-of-line) (point))))
212                        days))
213                 (progn
214                   (and gnus-verbose-backends
215                        (message "Deleting: %s" (car articles)))
216                   (nnbabyl-delete-mail))
217               (setq rest (cons (car articles) rest))))
218         (setq articles (cdr articles)))
219       (save-buffer)
220       ;; Find the lowest active article in this group.
221       (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
222         (goto-char (point-min))
223         (while (not (search-forward
224                      (nnbabyl-article-string (car active)) nil t))
225           (setcar (car active) (1+ (car active)))
226           (goto-char (point-min))))
227       (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
228       rest)))
229
230 (defun nnbabyl-request-move-article 
231   (article group server accept-form &optional last)
232   (nnbabyl-possibly-change-newsgroup group)
233   (let ((buf (get-buffer-create " *nnbabyl move*"))
234         result)
235     (and 
236      (nnbabyl-request-article article group server)
237      (save-excursion
238        (set-buffer buf)
239        (insert-buffer-substring nntp-server-buffer)
240        (goto-char (point-min))
241        (if (re-search-forward 
242             "^X-Gnus-Newsgroup:" 
243             (save-excursion (search-forward "\n\n" nil t) (point)) t)
244            (delete-region (progn (beginning-of-line) (point))
245                           (progn (forward-line 1) (point))))
246        (setq result (eval accept-form))
247        (kill-buffer (current-buffer))
248        result)
249      (save-excursion
250        (set-buffer nnbabyl-mbox-buffer)
251        (goto-char 1)
252        (if (search-forward (nnbabyl-article-string article) nil t)
253            (nnbabyl-delete-mail))
254        (and last (save-buffer))))
255     result))
256
257 (defun nnbabyl-request-accept-article (group &optional last)
258   (let ((buf (current-buffer))
259         result beg)
260     (and 
261      (setq nnbabyl-group-alist (nnmail-get-active))
262      (save-excursion
263        (set-buffer nnbabyl-mbox-buffer)
264        (setq beg (goto-char (point-max)))
265        (insert-buffer-substring buf)
266        (goto-char beg)
267        (if (stringp group)
268            (progn
269              (search-forward "\n\n" nil t)
270              (forward-line -1)
271              (save-excursion
272                (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
273                  (delete-region (point) (progn (forward-line 1) (point)))))
274              (setq result (nnbabyl-insert-newsgroup-line group)))
275          (setq result (nnbabyl-save-mail)))
276        (and last (save-buffer))
277        result)
278      (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file))
279     result))
280
281 (defun nnbabyl-request-replace-article (article group buffer)
282   (nnbabyl-possibly-change-newsgroup group)
283   (save-excursion
284     (set-buffer nnbabyl-mbox-buffer)
285     (goto-char 1)
286     (if (not (search-forward (nnbabyl-article-string article) nil t))
287         nil
288       (nnbabyl-delete-mail t t)
289       (insert-buffer-substring buffer)
290       (save-buffer)
291       t)))
292
293 \f
294 ;;; Low-Level Interface
295
296 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
297 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
298 ;; delimeter line.
299 (defun nnbabyl-delete-mail (&optional force leave-delim)
300   ;; Delete the current X-Gnus-Newsgroup line.
301   (or force
302       (delete-region
303        (progn (beginning-of-line) (point))
304        (progn (forward-line 1) (point))))
305   ;; Beginning of the article.
306   (save-excursion
307     (save-restriction
308       (narrow-to-region
309        (save-excursion
310          (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
311          (if leave-delim (progn (forward-line 1) (point))
312            (match-beginning 0)))
313        (progn
314          (forward-line 1)
315          (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter) 
316                                      nil t)
317                   (if (and (not (bobp)) leave-delim)
318                       (progn (forward-line -2) (point))
319                     (match-beginning 0)))
320              (point-max))))
321       (goto-char (point-min))
322       ;; Only delete the article if no other groups owns it as well.
323       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
324           (delete-region (point-min) (point-max))))))
325
326 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
327   (if (or (not nnbabyl-mbox-buffer)
328           (not (buffer-name nnbabyl-mbox-buffer)))
329       (save-excursion
330         (nnbabyl-read-mbox)))
331   (if (not nnbabyl-group-alist)
332       (setq nnbabyl-group-alist (nnmail-get-active)))
333   (if newsgroup
334       (if (assoc newsgroup nnbabyl-group-alist)
335           (setq nnbabyl-current-group newsgroup)
336         (setq nnbabyl-status-string "No such group in file")
337         nil)))
338
339 (defun nnbabyl-article-string (article)
340   (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":" 
341           (int-to-string article)))
342
343 (defun nnbabyl-save-mail ()
344   ;; Called narrowed to an article.
345   (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
346     (nnmail-insert-lines)
347     (nnmail-insert-xref group-art)
348     (nnbabyl-insert-newsgroup-line group-art)))
349
350 (defun nnbabyl-insert-newsgroup-line (group-art)
351   (save-excursion
352     (goto-char (point-min))
353     (or (looking-at "\^_")
354         (insert "\^_\^L\n0, unseen,,\n*** EOOH ***\n"))
355     (while (looking-at "From ")
356       (replace-match "Mail-from: ")
357       (forward-line 1))
358     (if (search-forward "\n\n" nil t)
359         (progn
360           (forward-char -1)
361           (while group-art
362             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
363                             (car (car group-art)) (cdr (car group-art))
364                             (current-time-string)))
365             (setq group-art (cdr group-art)))))))
366
367 (defun nnbabyl-active-number (group)
368   ;; Find the next article number in GROUP.
369   (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
370     (setcdr active (1+ (cdr active)))
371     (cdr active)))
372
373 (defun nnbabyl-read-mbox ()
374   (nnbabyl-request-list)
375   (setq nnbabyl-group-alist (nnmail-get-active))
376   (if (not (file-exists-p nnbabyl-mbox-file))
377       (write-region 1 1 nnbabyl-mbox-file t 'nomesg))
378   (if (and nnbabyl-mbox-buffer
379            (get-buffer nnbabyl-mbox-buffer)
380            (buffer-name nnbabyl-mbox-buffer)
381            (save-excursion
382              (set-buffer nnbabyl-mbox-buffer)
383              (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
384       ()
385     (save-excursion
386       (let ((delim (concat "^" nnbabyl-mail-delimiter))
387             (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
388                      (create-file-buffer nnbabyl-mbox-file)))
389             start end)
390         (set-buffer (setq nnbabyl-mbox-buffer buf))
391         (buffer-disable-undo (current-buffer))
392
393         (insert-file-contents nnbabyl-mbox-file)
394         (setq buffer-file-name nnbabyl-mbox-file)
395         (set-buffer-modified-p nil)
396
397         (goto-char (point-min))
398         (while (re-search-forward delim nil t)
399           (setq start (match-beginning 0))
400           (if (and
401                (save-excursion (re-search-forward delim nil t))
402                (not (search-forward 
403                      "\nX-Gnus-Newsgroup: " 
404                      (save-excursion 
405                        (setq end (or (and (re-search-forward delim nil t)
406                                           (match-beginning 0))
407                                      (point-max)))) t)))
408               (progn
409                 (goto-char end)
410                 (save-excursion
411                   (save-restriction
412                     (goto-char start)
413                     (narrow-to-region start end)
414                     (nnbabyl-save-mail))))))
415         (save-buffer)
416         (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
417
418 (defun nnbabyl-get-new-mail ()
419   (let (incoming)
420     (nnbabyl-read-mbox)
421     (if (and nnmail-spool-file nnbabyl-get-new-mail
422              (file-exists-p nnmail-spool-file)
423              (> (nth 7 (file-attributes nnmail-spool-file)) 0))
424         (progn
425           (and gnus-verbose-backends
426                (message "nnbabyl: Reading incoming mail..."))
427           (setq incoming 
428                 (nnmail-move-inbox nnmail-spool-file
429                                    (concat nnbabyl-mbox-file "-Incoming")))
430           (save-excursion
431             (let ((in-buf (nnmail-split-incoming 
432                            incoming 'nnbabyl-save-mail t)))
433               (set-buffer nnbabyl-mbox-buffer)
434               (goto-char (point-max))
435               (search-backward "\n\^_" nil t)
436               (insert-buffer-substring in-buf)
437               (kill-buffer in-buf)))
438           (run-hooks 'nnmail-read-incoming-hook)
439           (and gnus-verbose-backends
440                (message "nnbabyl: Reading incoming mail...done"))))
441     (and (buffer-modified-p nnbabyl-mbox-buffer) 
442          (save-excursion
443            (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
444            (set-buffer nnbabyl-mbox-buffer)
445            (save-buffer)))
446     ;; (if incoming (delete-file incoming))
447     ))
448
449 (provide 'nnbabyl)
450
451 ;;; nnbabyl.el ends here