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