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