*** empty log message ***
[gnus] / lisp / nnbabyl.el
1 ;;; nnbabyl.el --- rmail mbox access for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; For an overview of what the interface functions do, please see the
27 ;; Gnus sources.  
28
29 ;;; Code:
30
31 (require 'nnheader)
32 (require 'rmail)
33 (require 'nnmail)
34
35 (defvar nnbabyl-mbox-file (expand-file-name "~/RMAIL")
36   "The name of the rmail box file in the users home directory.")
37
38 (defvar nnbabyl-active-file (expand-file-name "~/.rmail-active")
39   "The name of the active file for the rmail box.")
40
41 (defvar nnbabyl-get-new-mail t
42   "If non-nil, nnbabyl will check the incoming mail file and split the mail.")
43
44 (defvar nnbabyl-prepare-save-mail-hook nil
45   "Hook run narrowed to an article before saving.")
46
47 \f
48
49 (defvar nnbabyl-mail-delimiter "\^_")
50
51 (defconst nnbabyl-version "nnbabyl 0.1"
52   "nnbabyl version.")
53
54 (defvar nnbabyl-mbox-buffer nil)
55 (defvar nnbabyl-current-group nil)
56 (defvar nnbabyl-status-string "")
57 (defvar nnbabyl-group-alist nil)
58
59 \f
60
61 (defvar nnbabyl-current-server nil)
62 (defvar nnbabyl-server-alist nil)
63 (defvar nnbabyl-server-variables 
64   (list
65    (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
66    (list 'nnbabyl-active-file nnbabyl-active-file)
67    (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
68    '(nnbabyl-current-group nil)
69    '(nnbabyl-status-string "")
70    '(nnbabyl-group-alist nil)))
71
72 \f
73
74 ;;; Interface functions
75
76 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server)
77   (save-excursion
78     (set-buffer nntp-server-buffer)
79     (erase-buffer)
80     (let ((number (length sequence))
81           (count 0)
82           article art-string start stop)
83       (nnbabyl-possibly-change-newsgroup newsgroup)
84       (if (stringp (car sequence))
85           'headers
86         (while sequence
87           (setq article (car sequence))
88           (setq art-string (nnbabyl-article-string article))
89           (set-buffer nnbabyl-mbox-buffer)
90           (if (or (search-forward art-string nil t)
91                   (search-backward art-string nil t))
92               (progn
93                 (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
94                 (while (and (not (looking-at ".+:"))
95                             (zerop (forward-line 1))))
96                 (setq start (point))
97                 (search-forward "\n\n" nil t)
98                 (setq stop (1- (point)))
99                 (set-buffer nntp-server-buffer)
100                 (insert "221 " (int-to-string article) " Article retrieved.\n")
101                 (insert-buffer-substring nnbabyl-mbox-buffer start stop)
102                 (goto-char (point-max))
103                 (insert ".\n")))
104           (setq sequence (cdr sequence))
105           (setq count (1+ count))
106           (and (numberp nnmail-large-newsgroup)
107                (> number nnmail-large-newsgroup)
108                (zerop (% count 20))
109                gnus-verbose-backends
110                (message "nnbabyl: Receiving headers... %d%%"
111                         (/ (* count 100) number))))
112
113         (and (numberp nnmail-large-newsgroup)
114              (> number nnmail-large-newsgroup)
115              gnus-verbose-backends
116              (message "nnbabyl: Receiving headers...done"))
117
118         ;; Fold continuation lines.
119         (set-buffer nntp-server-buffer)
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        nnbabyl-mbox-buffer
148        (buffer-name nnbabyl-mbox-buffer)
149        nntp-server-buffer
150        (buffer-name nntp-server-buffer)))
151
152 (defun nnbabyl-status-message (&optional server)
153   nnbabyl-status-string)
154
155 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
156   (nnbabyl-possibly-change-newsgroup newsgroup)
157   (if (stringp article)
158       nil
159     (save-excursion
160       (set-buffer nnbabyl-mbox-buffer)
161       (goto-char (point-min))
162       (if (search-forward (nnbabyl-article-string article) nil t)
163           (let (start stop)
164             (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
165             (while (and (not (looking-at ".+:"))
166                         (zerop (forward-line 1))))
167             (setq start (point))
168             (or (and (re-search-forward 
169                       (concat "^" nnbabyl-mail-delimiter) nil t)
170                      (forward-line -1))
171                 (goto-char (point-max)))
172             (setq stop (point))
173             (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
174               (set-buffer nntp-server-buffer)
175               (erase-buffer)
176               (insert-buffer-substring nnbabyl-mbox-buffer start stop)
177               (goto-char (point-min))
178               (if (search-forward "\n*** EOOH ***" nil t)
179                   (progn
180                     (delete-region (progn (beginning-of-line) (point))
181                                    (or (search-forward "\n\n" nil t)
182                                        (point)))))
183               t))))))
184
185 (defun nnbabyl-request-group (group &optional server dont-check)
186   (save-excursion
187     (if (nnbabyl-possibly-change-newsgroup group)
188         (if dont-check
189             t
190           (nnbabyl-get-new-mail group)
191           (save-excursion
192             (set-buffer nntp-server-buffer)
193             (erase-buffer)
194             (let ((active (assoc group nnbabyl-group-alist)))
195               (insert (format "211 %d %d %d %s\n" 
196                               (1+ (- (cdr (car (cdr active)))
197                                      (car (car (cdr active)))))
198                               (car (car (cdr active)))
199                               (cdr (car (cdr active)))
200                               (car active))))
201             t)))))
202
203 (defun nnbabyl-close-group (group &optional server)
204   t)
205
206 (defun nnbabyl-request-create-group (group &optional server) 
207   (nnbabyl-request-list)
208   (setq nnbabyl-group-alist (nnmail-get-active))
209   (or (assoc group nnbabyl-group-alist)
210       (let (active)
211         (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 0)))
212                                         nnbabyl-group-alist))
213         (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))
214   t)
215
216 (defun nnbabyl-request-list (&optional server)
217   (if server (nnbabyl-get-new-mail))
218   (save-excursion
219     (or (nnmail-find-file nnbabyl-active-file)
220         (progn
221           (setq nnbabyl-group-alist (nnmail-get-active))
222           (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
223           (nnmail-find-file nnbabyl-active-file)))))
224
225 (defun nnbabyl-request-newgroups (date &optional server)
226   (nnbabyl-request-list server))
227
228 (defun nnbabyl-request-list-newsgroups (&optional server)
229   (setq nnbabyl-status-string "nnbabyl: LIST NEWSGROUPS is not implemented.")
230   nil)
231
232 (defun nnbabyl-request-post (&optional server)
233   (mail-send-and-exit nil))
234
235 (defalias 'nnbabyl-request-post-buffer 'nnmail-request-post-buffer)
236
237 (defun nnbabyl-request-expire-articles (articles newsgroup &optional server force)
238   (nnbabyl-possibly-change-newsgroup newsgroup)
239   (let* ((days (or (and nnmail-expiry-wait-function
240                         (funcall nnmail-expiry-wait-function newsgroup))
241                    nnmail-expiry-wait))
242          rest)
243     (save-excursion 
244       (set-buffer nnbabyl-mbox-buffer)
245       (while articles
246         (goto-char (point-min))
247         (if (search-forward (nnbabyl-article-string (car articles)) nil t)
248             (if (or force
249                     (> (nnmail-days-between 
250                         (current-time-string)
251                         (buffer-substring 
252                          (point) (progn (end-of-line) (point))))
253                        days))
254                 (progn
255                   (and gnus-verbose-backends
256                        (message "Deleting: %s" (car articles)))
257                   (nnbabyl-delete-mail))
258               (setq rest (cons (car articles) rest))))
259         (setq articles (cdr articles)))
260       (save-buffer)
261       ;; Find the lowest active article in this group.
262       (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
263         (goto-char (point-min))
264         (while (and (not (search-forward
265                           (nnbabyl-article-string (car active)) nil t))
266                     (<= (car active) (cdr active)))
267           (setcar active (1+ (car active)))
268           (goto-char (point-min))))
269       (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
270       rest)))
271
272 (defun nnbabyl-request-move-article 
273   (article group server accept-form &optional last)
274   (nnbabyl-possibly-change-newsgroup group)
275   (let ((buf (get-buffer-create " *nnbabyl move*"))
276         result)
277     (and 
278      (nnbabyl-request-article article group server)
279      (save-excursion
280        (set-buffer buf)
281        (insert-buffer-substring nntp-server-buffer)
282        (goto-char (point-min))
283        (if (re-search-forward 
284             "^X-Gnus-Newsgroup:" 
285             (save-excursion (search-forward "\n\n" nil t) (point)) t)
286            (delete-region (progn (beginning-of-line) (point))
287                           (progn (forward-line 1) (point))))
288        (setq result (eval accept-form))
289        (kill-buffer (current-buffer))
290        result)
291      (save-excursion
292        (set-buffer nnbabyl-mbox-buffer)
293        (goto-char (point-min))
294        (if (search-forward (nnbabyl-article-string article) nil t)
295            (nnbabyl-delete-mail))
296        (and last (save-buffer))))
297     result))
298
299 (defun nnbabyl-request-accept-article (group &optional last)
300   (let ((buf (current-buffer))
301         result beg)
302     (and 
303      (nnbabyl-request-list)
304      (setq nnbabyl-group-alist (nnmail-get-active))
305      (save-excursion
306        (goto-char (point-min))
307        (search-forward "\n\n" nil t)
308        (forward-line -1)
309        (save-excursion
310          (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
311            (delete-region (point) (progn (forward-line 1) (point)))))
312        (let ((nnmail-split-methods
313               (if (stringp group) (list (list group "")) 
314                 nnmail-split-methods)))
315          (setq result (car (nnbabyl-save-mail))))
316        (set-buffer nnbabyl-mbox-buffer)
317        (goto-char (point-max))
318        (search-backward "\n\^_")
319        (goto-char (match-end 0))
320        (insert-buffer buf)
321        (and last (progn 
322                    (save-buffer)
323                    (nnmail-save-active
324                     nnbabyl-group-alist nnbabyl-active-file)))
325        result))))
326
327 (defun nnbabyl-request-replace-article (article group buffer)
328   (nnbabyl-possibly-change-newsgroup group)
329   (save-excursion
330     (set-buffer nnbabyl-mbox-buffer)
331     (goto-char (point-min))
332     (if (not (search-forward (nnbabyl-article-string article) nil t))
333         nil
334       (nnbabyl-delete-mail t t)
335       (insert-buffer-substring buffer)
336       (save-buffer)
337       t)))
338
339 \f
340 ;;; Low-Level Interface
341
342 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
343 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
344 ;; delimeter line.
345 (defun nnbabyl-delete-mail (&optional force leave-delim)
346   ;; Delete the current X-Gnus-Newsgroup line.
347   (or force
348       (delete-region
349        (progn (beginning-of-line) (point))
350        (progn (forward-line 1) (point))))
351   ;; Beginning of the article.
352   (save-excursion
353     (save-restriction
354       (widen)
355       (narrow-to-region
356        (save-excursion
357          (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
358          (if leave-delim (progn (forward-line 1) (point))
359            (match-beginning 0)))
360        (progn
361          (forward-line 1)
362          (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter) 
363                                      nil t)
364                   (if (and (not (bobp)) leave-delim)
365                       (progn (forward-line -2) (point))
366                     (match-beginning 0)))
367              (point-max))))
368       (goto-char (point-min))
369       ;; Only delete the article if no other groups owns it as well.
370       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
371           (delete-region (point-min) (point-max))))))
372
373 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
374   (if (or (not nnbabyl-mbox-buffer)
375           (not (buffer-name nnbabyl-mbox-buffer)))
376       (save-excursion (nnbabyl-read-mbox)))
377   (or nnbabyl-group-alist
378       (progn
379         (nnbabyl-request-list)
380         (setq nnbabyl-group-alist (nnmail-get-active))))
381   (if newsgroup
382       (if (assoc newsgroup nnbabyl-group-alist)
383           (setq nnbabyl-current-group newsgroup)
384         (setq nnbabyl-status-string "No such group in file")
385         nil)))
386
387 (defun nnbabyl-article-string (article)
388   (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":" 
389           (int-to-string article) " "))
390
391 (defun nnbabyl-insert-lines ()
392   "Insert how many lines and chars there are in the body of the mail."
393   (let (lines chars)
394     (save-excursion
395       (goto-char (point-min))
396       (if (search-forward "\n\n" nil t) 
397           (progn
398             ;; There may be an EOOH line here...
399             (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
400                 (search-forward "\n\n" nil t))
401             (setq chars (- (point-max) (point)))
402             (setq lines (- (count-lines (point) (point-max)) 1))
403             ;; Move back to the end of the headers. 
404             (goto-char (point-min))
405             (search-forward "\n\n" nil t)
406             (forward-char -1)
407             (save-excursion
408               (if (re-search-backward "^Lines: " nil t)
409                   (delete-region (point) (progn (forward-line 1) (point)))))
410             (insert (format "Lines: %d\n" lines))
411             chars)))))
412
413 (defun nnbabyl-save-mail ()
414   ;; Called narrowed to an article.
415   (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
416     (nnbabyl-insert-lines)
417     (nnmail-insert-xref group-art)
418     (nnbabyl-insert-newsgroup-line group-art)
419     (run-hooks 'nnbabyl-prepare-save-mail-hook)
420     group-art))
421
422 (defun nnbabyl-insert-newsgroup-line (group-art)
423   (save-excursion
424     (goto-char (point-min))
425     ;; If there is a C-l at the beginning of the narrowed region, this
426     ;; isn't really a "save", but rather a "scan".
427     (while (looking-at "From ")
428       (replace-match "Mail-from: From " t t)
429       (forward-line 1))
430     (goto-char (point-min))
431     (or (looking-at "\^L")
432         (save-excursion
433           (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
434           (goto-char (point-max))
435           (insert "\^_\n")))
436     (if (search-forward "\n\n" nil t)
437         (progn
438           (forward-char -1)
439           (while group-art
440             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
441                             (car (car group-art)) (cdr (car group-art))
442                             (current-time-string)))
443             (setq group-art (cdr group-art)))))
444     t))
445
446 (defun nnbabyl-active-number (group)
447   ;; Find the next article number in GROUP.
448   (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
449     (setcdr active (1+ (cdr active)))
450     (cdr active)))
451
452 (defun nnbabyl-read-mbox ()
453   (nnbabyl-request-list)
454   (setq nnbabyl-group-alist (nnmail-get-active))
455   (or (file-exists-p nnbabyl-mbox-file)
456       (save-excursion
457         (set-buffer (setq nnbabyl-mbox-buffer
458                           (create-file-buffer nnbabyl-mbox-file)))
459         (setq buffer-file-name nnbabyl-mbox-file)
460         (insert "BABYL OPTIONS:\n\n\^_")
461         (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
462
463   (if (and nnbabyl-mbox-buffer
464            (buffer-name nnbabyl-mbox-buffer)
465            (save-excursion
466              (set-buffer nnbabyl-mbox-buffer)
467              (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
468       ()
469     (save-excursion
470       (let ((delim (concat "^" nnbabyl-mail-delimiter))
471             (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
472                      (create-file-buffer nnbabyl-mbox-file)))
473             start end)
474         (set-buffer (setq nnbabyl-mbox-buffer buf))
475         (buffer-disable-undo (current-buffer))
476
477         (insert-file-contents nnbabyl-mbox-file)
478         (setq buffer-file-name nnbabyl-mbox-file)
479         (set-buffer-modified-p nil)
480
481         (goto-char (point-min))
482         (while (re-search-forward delim nil t)
483           (setq start (match-beginning 0))
484           (if (and
485                (save-excursion (re-search-forward delim nil t))
486                (not (search-forward 
487                      "\nX-Gnus-Newsgroup: " 
488                      (save-excursion 
489                        (setq end (or (and (re-search-forward delim nil t)
490                                           (match-beginning 0))
491                                      (point-max)))) t)))
492               (progn
493                 (goto-char end)
494                 (save-excursion
495                   (save-restriction
496                     (goto-char start)
497                     (narrow-to-region (1+ start) end)
498                     (nnbabyl-save-mail))))))
499         (and (buffer-modified-p (current-buffer)) (save-buffer))
500         (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
501
502 (defun nnbabyl-remove-incoming-delims ()
503   (goto-char (point-min))
504   (while (search-forward "\^_" nil t)
505     (replace-match "?" t t)))
506
507 (defun nnbabyl-get-new-mail (&optional group)
508   "Read new incoming mail."
509   (let* ((spools (nnmail-get-spool-files group))
510          (group-in group)
511          incoming incomings)
512     (nnbabyl-read-mbox)
513     (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
514         ()
515       ;; We go through all the existing spool files and split the
516       ;; mail from each.
517       (while spools
518         (and
519          (file-exists-p (car spools))
520          (> (nth 7 (file-attributes (car spools))) 0)
521          (progn
522            (and gnus-verbose-backends 
523                 (message "nnbabyl: Reading incoming mail..."))
524            (setq incoming 
525                  (nnmail-move-inbox 
526                   (car spools) (concat nnbabyl-mbox-file "-Incoming")))
527            (setq incomings (cons incoming incomings))
528            (save-excursion
529              (setq group (nnmail-get-split-group (car spools) group-in))
530              (let* ((nnmail-prepare-incoming-hook
531                      (cons 'nnbabyl-remove-incoming-delims
532                            nnmail-prepare-incoming-hook))
533                     in-buf)
534                (setq in-buf (nnmail-split-incoming 
535                              incoming 'nnbabyl-save-mail t group))
536                (set-buffer in-buf)
537                (goto-char (point-min))
538                (while (search-forward "\n\^_\n" nil t)
539                  (delete-char -1))
540                (set-buffer nnbabyl-mbox-buffer)
541                (goto-char (point-max))
542                (search-backward "\n\^_" nil t)
543                (goto-char (match-end 0))
544                (insert-buffer-substring in-buf)
545                (kill-buffer in-buf)))))
546         (setq spools (cdr spools)))
547       ;; If we did indeed read any incoming spools, we save all info. 
548       (and (buffer-modified-p nnbabyl-mbox-buffer) 
549            (save-excursion
550              (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
551              (set-buffer nnbabyl-mbox-buffer)
552              (save-buffer)))
553       (if incomings (run-hooks 'nnmail-read-incoming-hook))
554       (while incomings
555         (and nnmail-delete-incoming
556              (file-writable-p incoming) 
557              (delete-file incoming))
558         (setq incomings (cdr incomings))))))
559
560 (provide 'nnbabyl)
561
562 ;;; nnbabyl.el ends here