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