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