*** 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
238   (articles newsgroup &optional server force)
239   (nnbabyl-possibly-change-newsgroup newsgroup)
240   (let* ((days (or (and nnmail-expiry-wait-function
241                         (funcall nnmail-expiry-wait-function newsgroup))
242                    nnmail-expiry-wait))
243          (is-old t)
244          rest)
245     (save-excursion 
246       (set-buffer nnbabyl-mbox-buffer)
247       (while articles
248         (goto-char (point-min))
249         (if (search-forward (nnbabyl-article-string (car articles)) nil t)
250             (if (or force
251                     (setq is-old
252                           (> (nnmail-days-between 
253                               (current-time-string)
254                               (buffer-substring 
255                                (point) (progn (end-of-line) (point))))
256                              days)))
257                 (progn
258                   (and gnus-verbose-backends
259                        (message "Deleting: %s" (car articles)))
260                   (nnbabyl-delete-mail))
261               (setq rest (cons (car articles) rest))))
262         (setq articles (cdr articles)))
263       (save-buffer)
264       ;; Find the lowest active article in this group.
265       (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
266         (goto-char (point-min))
267         (while (and (not (search-forward
268                           (nnbabyl-article-string (car active)) nil t))
269                     (<= (car active) (cdr active)))
270           (setcar active (1+ (car active)))
271           (goto-char (point-min))))
272       (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
273       (nconc rest articles))))
274
275 (defun nnbabyl-request-move-article 
276   (article group server accept-form &optional last)
277   (nnbabyl-possibly-change-newsgroup group)
278   (let ((buf (get-buffer-create " *nnbabyl move*"))
279         result)
280     (and 
281      (nnbabyl-request-article article group server)
282      (save-excursion
283        (set-buffer buf)
284        (insert-buffer-substring nntp-server-buffer)
285        (goto-char (point-min))
286        (if (re-search-forward 
287             "^X-Gnus-Newsgroup:" 
288             (save-excursion (search-forward "\n\n" nil t) (point)) t)
289            (delete-region (progn (beginning-of-line) (point))
290                           (progn (forward-line 1) (point))))
291        (setq result (eval accept-form))
292        (kill-buffer (current-buffer))
293        result)
294      (save-excursion
295        (set-buffer nnbabyl-mbox-buffer)
296        (goto-char (point-min))
297        (if (search-forward (nnbabyl-article-string article) nil t)
298            (nnbabyl-delete-mail))
299        (and last (save-buffer))))
300     result))
301
302 (defun nnbabyl-request-accept-article (group &optional last)
303   (let ((buf (current-buffer))
304         result beg)
305     (and 
306      (nnbabyl-request-list)
307      (setq nnbabyl-group-alist (nnmail-get-active))
308      (save-excursion
309        (goto-char (point-min))
310        (search-forward "\n\n" nil t)
311        (forward-line -1)
312        (save-excursion
313          (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
314            (delete-region (point) (progn (forward-line 1) (point)))))
315        (let ((nnmail-split-methods
316               (if (stringp group) (list (list group "")) 
317                 nnmail-split-methods)))
318          (setq result (car (nnbabyl-save-mail))))
319        (set-buffer nnbabyl-mbox-buffer)
320        (goto-char (point-max))
321        (search-backward "\n\^_")
322        (goto-char (match-end 0))
323        (insert-buffer buf)
324        (and last (progn 
325                    (save-buffer)
326                    (nnmail-save-active
327                     nnbabyl-group-alist nnbabyl-active-file)))
328        result))))
329
330 (defun nnbabyl-request-replace-article (article group buffer)
331   (nnbabyl-possibly-change-newsgroup group)
332   (save-excursion
333     (set-buffer nnbabyl-mbox-buffer)
334     (goto-char (point-min))
335     (if (not (search-forward (nnbabyl-article-string article) nil t))
336         nil
337       (nnbabyl-delete-mail t t)
338       (insert-buffer-substring buffer)
339       (save-buffer)
340       t)))
341
342 \f
343 ;;; Low-Level Interface
344
345 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
346 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
347 ;; delimeter line.
348 (defun nnbabyl-delete-mail (&optional force leave-delim)
349   ;; Delete the current X-Gnus-Newsgroup line.
350   (or force
351       (delete-region
352        (progn (beginning-of-line) (point))
353        (progn (forward-line 1) (point))))
354   ;; Beginning of the article.
355   (save-excursion
356     (save-restriction
357       (widen)
358       (narrow-to-region
359        (save-excursion
360          (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
361          (if leave-delim (progn (forward-line 1) (point))
362            (match-beginning 0)))
363        (progn
364          (forward-line 1)
365          (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter) 
366                                      nil t)
367                   (if (and (not (bobp)) leave-delim)
368                       (progn (forward-line -2) (point))
369                     (match-beginning 0)))
370              (point-max))))
371       (goto-char (point-min))
372       ;; Only delete the article if no other groups owns it as well.
373       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
374           (delete-region (point-min) (point-max))))))
375
376 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
377   (if (or (not nnbabyl-mbox-buffer)
378           (not (buffer-name nnbabyl-mbox-buffer)))
379       (save-excursion (nnbabyl-read-mbox)))
380   (or nnbabyl-group-alist
381       (progn
382         (nnbabyl-request-list)
383         (setq nnbabyl-group-alist (nnmail-get-active))))
384   (if newsgroup
385       (if (assoc newsgroup nnbabyl-group-alist)
386           (setq nnbabyl-current-group newsgroup)
387         (setq nnbabyl-status-string "No such group in file")
388         nil)))
389
390 (defun nnbabyl-article-string (article)
391   (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":" 
392           (int-to-string article) " "))
393
394 (defun nnbabyl-insert-lines ()
395   "Insert how many lines and chars there are in the body of the mail."
396   (let (lines chars)
397     (save-excursion
398       (goto-char (point-min))
399       (if (search-forward "\n\n" nil t) 
400           (progn
401             ;; There may be an EOOH line here...
402             (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
403                 (search-forward "\n\n" nil t))
404             (setq chars (- (point-max) (point)))
405             (setq lines (- (count-lines (point) (point-max)) 1))
406             ;; Move back to the end of the headers. 
407             (goto-char (point-min))
408             (search-forward "\n\n" nil t)
409             (forward-char -1)
410             (save-excursion
411               (if (re-search-backward "^Lines: " nil t)
412                   (delete-region (point) (progn (forward-line 1) (point)))))
413             (insert (format "Lines: %d\n" lines))
414             chars)))))
415
416 (defun nnbabyl-save-mail ()
417   ;; Called narrowed to an article.
418   (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
419     (nnbabyl-insert-lines)
420     (nnmail-insert-xref group-art)
421     (nnbabyl-insert-newsgroup-line group-art)
422     (run-hooks 'nnbabyl-prepare-save-mail-hook)
423     group-art))
424
425 (defun nnbabyl-insert-newsgroup-line (group-art)
426   (save-excursion
427     (goto-char (point-min))
428     ;; If there is a C-l at the beginning of the narrowed region, this
429     ;; isn't really a "save", but rather a "scan".
430     (while (looking-at "From ")
431       (replace-match "Mail-from: From " t t)
432       (forward-line 1))
433     (goto-char (point-min))
434     (or (looking-at "\^L")
435         (save-excursion
436           (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
437           (goto-char (point-max))
438           (insert "\^_\n")))
439     (if (search-forward "\n\n" nil t)
440         (progn
441           (forward-char -1)
442           (while group-art
443             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
444                             (car (car group-art)) (cdr (car group-art))
445                             (current-time-string)))
446             (setq group-art (cdr group-art)))))
447     t))
448
449 (defun nnbabyl-active-number (group)
450   ;; Find the next article number in GROUP.
451   (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
452     (setcdr active (1+ (cdr active)))
453     (cdr active)))
454
455 (defun nnbabyl-read-mbox ()
456   (nnbabyl-request-list)
457   (setq nnbabyl-group-alist (nnmail-get-active))
458   (or (file-exists-p nnbabyl-mbox-file)
459       (save-excursion
460         (set-buffer (setq nnbabyl-mbox-buffer
461                           (create-file-buffer nnbabyl-mbox-file)))
462         (setq buffer-file-name nnbabyl-mbox-file)
463         (insert "BABYL OPTIONS:\n\n\^_")
464         (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
465
466   (if (and nnbabyl-mbox-buffer
467            (buffer-name nnbabyl-mbox-buffer)
468            (save-excursion
469              (set-buffer nnbabyl-mbox-buffer)
470              (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
471       ()
472     (save-excursion
473       (let ((delim (concat "^" nnbabyl-mail-delimiter))
474             (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
475                      (create-file-buffer nnbabyl-mbox-file)))
476             start end)
477         (set-buffer (setq nnbabyl-mbox-buffer buf))
478         (buffer-disable-undo (current-buffer))
479
480         (insert-file-contents nnbabyl-mbox-file)
481         (setq buffer-file-name nnbabyl-mbox-file)
482         (set-buffer-modified-p nil)
483
484         (goto-char (point-min))
485         (while (re-search-forward delim nil t)
486           (setq start (match-beginning 0))
487           (if (and
488                (save-excursion (re-search-forward delim nil t))
489                (not (search-forward 
490                      "\nX-Gnus-Newsgroup: " 
491                      (save-excursion 
492                        (setq end (or (and (re-search-forward delim nil t)
493                                           (match-beginning 0))
494                                      (point-max)))) t)))
495               (progn
496                 (goto-char end)
497                 (save-excursion
498                   (save-restriction
499                     (goto-char start)
500                     (narrow-to-region (1+ start) end)
501                     (nnbabyl-save-mail))))))
502         (and (buffer-modified-p (current-buffer)) (save-buffer))
503         (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
504
505 (defun nnbabyl-remove-incoming-delims ()
506   (goto-char (point-min))
507   (while (search-forward "\^_" nil t)
508     (replace-match "?" t t)))
509
510 (defun nnbabyl-get-new-mail (&optional group)
511   "Read new incoming mail."
512   (let* ((spools (nnmail-get-spool-files group))
513          (group-in group)
514          incoming incomings)
515     (nnbabyl-read-mbox)
516     (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
517         ()
518       ;; We go through all the existing spool files and split the
519       ;; mail from each.
520       (while spools
521         (and
522          (file-exists-p (car spools))
523          (> (nth 7 (file-attributes (car spools))) 0)
524          (progn
525            (and gnus-verbose-backends 
526                 (message "nnbabyl: Reading incoming mail..."))
527            (setq incoming 
528                  (nnmail-move-inbox 
529                   (car spools) (concat nnbabyl-mbox-file "-Incoming")))
530            (setq incomings (cons incoming incomings))
531            (save-excursion
532              (setq group (nnmail-get-split-group (car spools) group-in))
533              (let* ((nnmail-prepare-incoming-hook
534                      (cons 'nnbabyl-remove-incoming-delims
535                            nnmail-prepare-incoming-hook))
536                     in-buf)
537                (setq in-buf (nnmail-split-incoming 
538                              incoming 'nnbabyl-save-mail t group))
539                (set-buffer in-buf)
540                (goto-char (point-min))
541                (while (search-forward "\n\^_\n" nil t)
542                  (delete-char -1))
543                (set-buffer nnbabyl-mbox-buffer)
544                (goto-char (point-max))
545                (search-backward "\n\^_" nil t)
546                (goto-char (match-end 0))
547                (insert-buffer-substring in-buf)
548                (kill-buffer in-buf)))))
549         (setq spools (cdr spools)))
550       ;; If we did indeed read any incoming spools, we save all info. 
551       (and (buffer-modified-p nnbabyl-mbox-buffer) 
552            (save-excursion
553              (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
554              (set-buffer nnbabyl-mbox-buffer)
555              (save-buffer)))
556       (if incomings (run-hooks 'nnmail-read-incoming-hook))
557       (while incomings
558         (and nnmail-delete-incoming
559              (file-writable-p incoming) 
560              (delete-file incoming))
561         (setq incomings (cdr incomings))))))
562
563 (provide 'nnbabyl)
564
565 ;;; nnbabyl.el ends here