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