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