035027f32a9926f6752a5d4eaaf3e6f7753d0942
[gnus] / lisp / nnbabyl.el
1 ;;; nnbabyl.el --- rmail mbox access for Gnus
2 ;; Copyright (C) 1995,96 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; For an overview of what the interface functions do, please see the
28 ;; Gnus sources.  
29
30 ;;; Code:
31
32 (require 'nnheader)
33 (require 'rmail)
34 (require 'nnmail)
35
36 (defvar nnbabyl-mbox-file (expand-file-name "~/RMAIL")
37   "The name of the rmail box file in the users home directory.")
38
39 (defvar nnbabyl-active-file (expand-file-name "~/.rmail-active")
40   "The name of the active file for the rmail box.")
41
42 (defvar nnbabyl-get-new-mail t
43   "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
44
45 (defvar nnbabyl-prepare-save-mail-hook nil
46   "Hook run narrowed to an article before saving.")
47
48 \f
49
50 (defvar nnbabyl-mail-delimiter "\^_")
51
52 (defconst nnbabyl-version "nnbabyl 1.0"
53   "nnbabyl version.")
54
55 (defvar nnbabyl-mbox-buffer nil)
56 (defvar nnbabyl-current-group nil)
57 (defvar nnbabyl-status-string "")
58 (defvar nnbabyl-group-alist nil)
59 (defvar nnbabyl-active-timestamp nil)
60
61 (defvar nnbabyl-previous-buffer-mode nil)
62
63 \f
64
65 (defvar nnbabyl-current-server nil)
66 (defvar nnbabyl-server-alist nil)
67 (defvar nnbabyl-server-variables 
68   (list
69    (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
70    (list 'nnbabyl-active-file nnbabyl-active-file)
71    (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
72    '(nnbabyl-current-group nil)
73    '(nnbabyl-status-string "")
74    '(nnbabyl-previous-buffer-mode nil)
75    '(nnbabyl-group-alist nil)))
76
77 \f
78
79 ;;; Interface functions
80
81 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server fetch-old)
82   (save-excursion
83     (set-buffer nntp-server-buffer)
84     (erase-buffer)
85     (let ((number (length sequence))
86           (count 0)
87           article art-string start stop)
88       (nnbabyl-possibly-change-newsgroup newsgroup)
89       (while sequence
90         (setq article (car sequence))
91         (setq art-string (nnbabyl-article-string article))
92         (set-buffer nnbabyl-mbox-buffer)
93         (if (or (search-forward art-string nil t)
94                 (search-backward art-string nil t))
95             (progn
96               (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
97               (while (and (not (looking-at ".+:"))
98                           (zerop (forward-line 1))))
99               (setq start (point))
100               (search-forward "\n\n" nil t)
101               (setq stop (1- (point)))
102               (set-buffer nntp-server-buffer)
103               (insert "221 " (int-to-string article) " Article retrieved.\n")
104               (insert-buffer-substring nnbabyl-mbox-buffer start stop)
105               (goto-char (point-max))
106               (insert ".\n")))
107         (setq sequence (cdr sequence))
108         (setq count (1+ count))
109         (and (numberp nnmail-large-newsgroup)
110              (> number nnmail-large-newsgroup)
111              (zerop (% count 20))
112              (nnheader-message 5 "nnbabyl: Receiving headers... %d%%"
113                                (/ (* count 100) number))))
114
115       (and (numberp nnmail-large-newsgroup)
116            (> number nnmail-large-newsgroup)
117            (nnheader-message 5 "nnbabyl: Receiving headers...done"))
118
119       (set-buffer nntp-server-buffer)
120       (nnheader-fold-continuation-lines)
121       'headers)))
122
123 (defun nnbabyl-open-server (server &optional defs)
124   (nnheader-init-server-buffer)
125   (if (equal server nnbabyl-current-server)
126       t
127     (if nnbabyl-current-server
128         (setq nnbabyl-server-alist 
129               (cons (list nnbabyl-current-server
130                           (nnheader-save-variables nnbabyl-server-variables))
131                     nnbabyl-server-alist)))
132     (let ((state (assoc server nnbabyl-server-alist)))
133       (if state 
134           (progn
135             (nnheader-restore-variables (nth 1 state))
136             (setq nnbabyl-server-alist (delq state nnbabyl-server-alist)))
137         (nnheader-set-init-variables nnbabyl-server-variables defs)))
138     (setq nnbabyl-current-server server)))
139
140 (defun nnbabyl-close-server (&optional server)
141   ;; Restore buffer mode.
142   (when (and (nnbabyl-server-opened)
143              nnbabyl-previous-buffer-mode)
144     (save-excursion
145       (set-buffer nnbabyl-mbox-buffer)
146       (narrow-to-region
147        (car (car nnbabyl-previous-buffer-mode))
148        (cdr (car nnbabyl-previous-buffer-mode)))
149       (funcall (cdr nnbabyl-previous-buffer-mode))))
150   (setq nnbabyl-current-server nil
151         nnbabyl-mbox-buffer nil)
152   t)
153
154 (defun nnbabyl-server-opened (&optional server)
155   (and (equal server nnbabyl-current-server)
156        nnbabyl-mbox-buffer
157        (buffer-name nnbabyl-mbox-buffer)
158        nntp-server-buffer
159        (buffer-name nntp-server-buffer)))
160
161 (defun nnbabyl-status-message (&optional server)
162   nnbabyl-status-string)
163
164 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
165   (nnbabyl-possibly-change-newsgroup newsgroup)
166   (save-excursion
167     (set-buffer nnbabyl-mbox-buffer)
168     (goto-char (point-min))
169     (if (search-forward (nnbabyl-article-string article) nil t)
170         (let (start stop summary-line)
171           (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
172           (while (and (not (looking-at ".+:"))
173                       (zerop (forward-line 1))))
174           (setq start (point))
175           (or (and (re-search-forward 
176                     (concat "^" nnbabyl-mail-delimiter) nil t)
177                    (forward-line -1))
178               (goto-char (point-max)))
179           (setq stop (point))
180           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
181             (set-buffer nntp-server-buffer)
182             (erase-buffer)
183             (insert-buffer-substring nnbabyl-mbox-buffer start stop)
184             (goto-char (point-min))
185             ;; If there is an EOOH header, then we have to remove some
186             ;; duplicated headers. 
187             (setq summary-line (looking-at "Summary-line:"))
188             (when (search-forward "\n*** EOOH ***" nil t)
189               (if summary-line
190                   ;; The headers to be deleted are located before the
191                   ;; EOOH line...
192                   (delete-region (point-min) (progn (forward-line 1)
193                   (point)))
194                 ;; ...or after.
195                 (delete-region (progn (beginning-of-line) (point))
196                                (or (search-forward "\n\n" nil t)
197                                    (point)))))
198             (if (numberp article) 
199                 (cons nnbabyl-current-group article)
200               (nnbabyl-article-group-number)))))))
201
202 (defun nnbabyl-request-group (group &optional server dont-check)
203   (let ((active (cadr (assoc group nnbabyl-group-alist))))
204     (save-excursion
205       (cond 
206        ((null active)
207         (nnheader-report 'nnbabyl "No such group: %s" group))
208        ((null (nnbabyl-possibly-change-newsgroup group))
209         (nnheader-report 'nnbabyl "No such group: %s" group))
210        (dont-check
211         (nnheader-report 'nnbabyl "Selected group %s" group)
212         t)
213        ((> (car active) (cdr active))
214         (nnheader-report 'nnbabyl "Empty group %s" group))
215        (t
216         (nnheader-report 'nnbabyl "Selected group %s" group)
217         (nnheader-insert "211 %d %d %d %s\n" 
218                          (1+ (- (cdr active) (car active)))
219                          (car active) (cdr active) group)
220         t)))))
221
222 (defun nnbabyl-request-scan (&optional group server)
223   (nnbabyl-read-mbox)
224   (nnmail-get-new-mail 
225    'nnbabyl 
226    (lambda ()
227      (save-excursion
228        (set-buffer nnbabyl-mbox-buffer)
229        (save-buffer)))
230    nnbabyl-mbox-file group
231    (lambda ()
232      (save-excursion
233        (let ((in-buf (current-buffer)))
234          (goto-char (point-min))
235          (while (search-forward "\n\^_\n" nil t)
236            (delete-char -1))
237          (set-buffer nnbabyl-mbox-buffer)
238          (goto-char (point-max))
239          (search-backward "\n\^_" nil t)
240          (goto-char (match-end 0))
241          (insert-buffer-substring in-buf))))))
242
243 (defun nnbabyl-close-group (group &optional server)
244   t)
245
246 (defun nnbabyl-request-create-group (group &optional server) 
247   (nnmail-activate 'nnbabyl)
248   (or (assoc group nnbabyl-group-alist)
249       (let (active)
250         (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 0)))
251                                         nnbabyl-group-alist))
252         (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))
253   t)
254
255 (defun nnbabyl-request-list (&optional server)
256   (save-excursion
257     (or (nnmail-find-file nnbabyl-active-file)
258         (progn
259           (setq nnbabyl-group-alist (nnmail-get-active))
260           (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
261           (nnmail-find-file nnbabyl-active-file)))))
262
263 (defun nnbabyl-request-newgroups (date &optional server)
264   (nnbabyl-request-list server))
265
266 (defun nnbabyl-request-list-newsgroups (&optional server)
267   (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
268   nil)
269
270 (defun nnbabyl-request-post (&optional server)
271   (mail-send-and-exit nil))
272
273 (defun nnbabyl-request-expire-articles
274   (articles newsgroup &optional server force)
275   (nnbabyl-possibly-change-newsgroup newsgroup)
276   (let* ((is-old t)
277          rest)
278     (nnmail-activate 'nnbabyl)
279
280     (save-excursion 
281       (set-buffer nnbabyl-mbox-buffer)
282       (set-text-properties (point-min) (point-max) nil)
283       (while (and articles is-old)
284         (goto-char (point-min))
285         (if (search-forward (nnbabyl-article-string (car articles)) nil t)
286             (if (setq is-old
287                       (nnmail-expired-article-p
288                        newsgroup
289                        (buffer-substring 
290                         (point) (progn (end-of-line) (point))) force))
291                 (progn
292                   (nnheader-message 5 "Deleting article %d in %s..." 
293                                     (car articles) newsgroup)
294                   (nnbabyl-delete-mail))
295               (setq rest (cons (car articles) rest))))
296         (setq articles (cdr articles)))
297       (save-buffer)
298       ;; Find the lowest active article in this group.
299       (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
300         (goto-char (point-min))
301         (while (and (not (search-forward
302                           (nnbabyl-article-string (car active)) nil t))
303                     (<= (car active) (cdr active)))
304           (setcar active (1+ (car active)))
305           (goto-char (point-min))))
306       (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
307       (nconc rest articles))))
308
309 (defun nnbabyl-request-move-article 
310   (article group server accept-form &optional last)
311   (nnbabyl-possibly-change-newsgroup group)
312   (let ((buf (get-buffer-create " *nnbabyl move*"))
313         result)
314     (and 
315      (nnbabyl-request-article article group server)
316      (save-excursion
317        (set-buffer buf)
318        (insert-buffer-substring nntp-server-buffer)
319        (goto-char (point-min))
320        (if (re-search-forward 
321             "^X-Gnus-Newsgroup:" 
322             (save-excursion (search-forward "\n\n" nil t) (point)) t)
323            (delete-region (progn (beginning-of-line) (point))
324                           (progn (forward-line 1) (point))))
325        (setq result (eval accept-form))
326        (kill-buffer (current-buffer))
327        result)
328      (save-excursion
329        (set-buffer nnbabyl-mbox-buffer)
330        (goto-char (point-min))
331        (if (search-forward (nnbabyl-article-string article) nil t)
332            (nnbabyl-delete-mail))
333        (and last (save-buffer))))
334     result))
335
336 (defun nnbabyl-request-accept-article (group &optional last)
337   (let ((buf (current-buffer))
338         result beg)
339     (and 
340      (nnmail-activate 'nnbabyl)
341      (save-excursion
342        (goto-char (point-min))
343        (search-forward "\n\n" nil t)
344        (forward-line -1)
345        (save-excursion
346          (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
347            (delete-region (point) (progn (forward-line 1) (point)))))
348        (let ((nnmail-split-methods
349               (if (stringp group) (list (list group "")) 
350                 nnmail-split-methods)))
351          (setq result (car (nnbabyl-save-mail))))
352        (set-buffer nnbabyl-mbox-buffer)
353        (goto-char (point-max))
354        (search-backward "\n\^_")
355        (goto-char (match-end 0))
356        (insert-buffer-substring buf)
357        (and last (progn 
358                    (save-buffer)
359                    (nnmail-save-active
360                     nnbabyl-group-alist nnbabyl-active-file)))
361        result))))
362
363 (defun nnbabyl-request-replace-article (article group buffer)
364   (nnbabyl-possibly-change-newsgroup group)
365   (save-excursion
366     (set-buffer nnbabyl-mbox-buffer)
367     (goto-char (point-min))
368     (if (not (search-forward (nnbabyl-article-string article) nil t))
369         nil
370       (nnbabyl-delete-mail t t)
371       (insert-buffer-substring buffer)
372       (save-buffer)
373       t)))
374
375 (defun nnbabyl-request-delete-group (group &optional force server)
376   (nnbabyl-possibly-change-newsgroup group)
377   ;; Delete all articles in GROUP.
378   (if (not force)
379       ()                                ; Don't delete the articles.
380     (save-excursion
381       (set-buffer nnbabyl-mbox-buffer)
382       (goto-char (point-min))
383       ;; Delete all articles in this group.
384       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
385             found)
386         (while (search-forward ident nil t)
387           (setq found t)
388           (nnbabyl-delete-mail))
389         (and found (save-buffer)))))
390   ;; Remove the group from all structures.
391   (setq nnbabyl-group-alist 
392         (delq (assoc group nnbabyl-group-alist) nnbabyl-group-alist)
393         nnbabyl-current-group nil)
394   ;; Save the active file.
395   (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
396   t)
397
398 (defun nnbabyl-request-rename-group (group new-name &optional server)
399   (nnbabyl-possibly-change-newsgroup group)
400   (save-excursion
401     (set-buffer nnbabyl-mbox-buffer)
402     (goto-char (point-min))
403     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":"))
404           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
405           found)
406       (while (search-forward ident nil t)
407         (replace-match new-ident t t)
408         (setq found t))
409       (and found (save-buffer))))
410   (let ((entry (assoc group nnbabyl-group-alist)))
411     (and entry (setcar entry new-name))
412     (setq nnbabyl-current-group nil)
413     ;; Save the new group alist.
414     (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
415     t))
416
417 \f
418 ;;; Internal functions.
419
420 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
421 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
422 ;; delimiter line.
423 (defun nnbabyl-delete-mail (&optional force leave-delim)
424   ;; Delete the current X-Gnus-Newsgroup line.
425   (or force
426       (delete-region
427        (progn (beginning-of-line) (point))
428        (progn (forward-line 1) (point))))
429   ;; Beginning of the article.
430   (save-excursion
431     (save-restriction
432       (widen)
433       (narrow-to-region
434        (save-excursion
435          (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
436          (if leave-delim (progn (forward-line 1) (point))
437            (match-beginning 0)))
438        (progn
439          (forward-line 1)
440          (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter) 
441                                      nil t)
442                   (if (and (not (bobp)) leave-delim)
443                       (progn (forward-line -2) (point))
444                     (match-beginning 0)))
445              (point-max))))
446       (goto-char (point-min))
447       ;; Only delete the article if no other groups owns it as well.
448       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
449           (delete-region (point-min) (point-max))))))
450
451 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
452   (if (or (not nnbabyl-mbox-buffer)
453           (not (buffer-name nnbabyl-mbox-buffer)))
454       (save-excursion (nnbabyl-read-mbox)))
455   (or nnbabyl-group-alist
456       (nnmail-activate 'nnbabyl))
457   (if newsgroup
458       (if (assoc newsgroup nnbabyl-group-alist)
459           (setq nnbabyl-current-group newsgroup)
460         (setq nnbabyl-status-string "No such group in file")
461         nil)
462     t))
463
464 (defun nnbabyl-article-string (article)
465   (if (numberp article)
466       (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":" 
467               (int-to-string article) " ")
468     (concat "\nMessage-ID: " article)))
469
470 (defun nnbabyl-article-group-number ()
471   (save-excursion
472     (goto-char (point-min))
473     (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
474                             nil t)
475          (cons (buffer-substring (match-beginning 1) (match-end 1))
476                (string-to-int
477                 (buffer-substring (match-beginning 2) (match-end 2)))))))
478
479 (defun nnbabyl-insert-lines ()
480   "Insert how many lines and chars there are in the body of the mail."
481   (let (lines chars)
482     (save-excursion
483       (goto-char (point-min))
484       (if (search-forward "\n\n" nil t) 
485           (progn
486             ;; There may be an EOOH line here...
487             (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
488                 (search-forward "\n\n" nil t))
489             (setq chars (- (point-max) (point)))
490             (setq lines (- (count-lines (point) (point-max)) 1))
491             ;; Move back to the end of the headers. 
492             (goto-char (point-min))
493             (search-forward "\n\n" nil t)
494             (forward-char -1)
495             (save-excursion
496               (if (re-search-backward "^Lines: " nil t)
497                   (delete-region (point) (progn (forward-line 1) (point)))))
498             (insert (format "Lines: %d\n" lines))
499             chars)))))
500
501 (defun nnbabyl-save-mail ()
502   ;; Called narrowed to an article.
503   (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
504     (nnbabyl-insert-lines)
505     (nnmail-insert-xref group-art)
506     (nnbabyl-insert-newsgroup-line group-art)
507     (run-hooks 'nnbabyl-prepare-save-mail-hook)
508     group-art))
509
510 (defun nnbabyl-insert-newsgroup-line (group-art)
511   (save-excursion
512     (goto-char (point-min))
513     (while (looking-at "From ")
514       (replace-match "Mail-from: From " t t)
515       (forward-line 1))
516     ;; If there is a C-l at the beginning of the narrowed region, this
517     ;; isn't really a "save", but rather a "scan".
518     (goto-char (point-min))
519     (or (looking-at "\^L")
520         (save-excursion
521           (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
522           (goto-char (point-max))
523           (insert "\^_\n")))
524     (if (search-forward "\n\n" nil t)
525         (progn
526           (forward-char -1)
527           (while group-art
528             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
529                             (car (car group-art)) (cdr (car group-art))
530                             (current-time-string)))
531             (setq group-art (cdr group-art)))))
532     t))
533
534 (defun nnbabyl-active-number (group)
535   ;; Find the next article number in GROUP.
536   (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
537     (if active
538         (setcdr active (1+ (cdr active)))
539       ;; This group is new, so we create a new entry for it.
540       ;; This might be a bit naughty... creating groups on the drop of
541       ;; a hat, but I don't know...
542       (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
543                                       nnbabyl-group-alist)))
544     (cdr active)))
545
546 (defun nnbabyl-read-mbox ()
547   (nnmail-activate 'nnbabyl)
548   (or (file-exists-p nnbabyl-mbox-file)
549       (save-excursion
550         (set-buffer (setq nnbabyl-mbox-buffer
551                           (create-file-buffer nnbabyl-mbox-file)))
552         (setq buffer-file-name nnbabyl-mbox-file)
553         (insert "BABYL OPTIONS:\n\n\^_")
554         (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
555
556   (if (and nnbabyl-mbox-buffer
557            (buffer-name nnbabyl-mbox-buffer)
558            (save-excursion
559              (set-buffer nnbabyl-mbox-buffer)
560              (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
561       ()
562     (save-excursion
563       (let ((delim (concat "^" nnbabyl-mail-delimiter))
564             start end)
565         (set-buffer (setq nnbabyl-mbox-buffer 
566                           (nnheader-find-file-noselect 
567                            nnbabyl-mbox-file nil 'raw)))
568         ;; Save buffer mode.
569         (setq nnbabyl-previous-buffer-mode 
570               (cons (cons (point-min) (point-max))
571                     major-mode))
572
573         (buffer-disable-undo (current-buffer))
574         (widen)
575         (setq buffer-read-only nil)
576         (fundamental-mode)
577         
578         (goto-char (point-min))
579         (re-search-forward delim nil t)
580         (setq start (match-end 0))
581         (while (re-search-forward delim nil t)
582           (setq end (match-end 0))
583           (or (search-backward "\nX-Gnus-Newsgroup: " start t)
584               (progn
585                 (goto-char end)
586                 (save-excursion
587                   (save-restriction
588                     (goto-char start)
589                     (narrow-to-region start end)
590                     (nnbabyl-save-mail)
591                     (setq end (point-max))))))
592           (goto-char (setq start end)))
593         (and (buffer-modified-p (current-buffer)) (save-buffer))
594         (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
595
596 (defun nnbabyl-remove-incoming-delims ()
597   (goto-char (point-min))
598   (while (search-forward "\^_" nil t)
599     (replace-match "?" t t)))
600
601 (provide 'nnbabyl)
602
603 ;;; nnbabyl.el ends here