*** empty log message ***
[gnus] / lisp / nnbabyl.el
1 ;;; nnbabyl.el --- mail 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 \f
45
46 (defvar nnbabyl-mail-delimiter "\^_")
47
48 (defconst nnbabyl-version "nnbabyl 0.1"
49   "nnbabyl version.")
50
51 (defvar nnbabyl-mbox-buffer nil)
52 (defvar nnbabyl-current-group nil)
53 (defvar nnbabyl-status-string "")
54 (defvar nnbabyl-group-alist nil)
55
56 \f
57
58 (defvar nnbabyl-current-server nil)
59 (defvar nnbabyl-server-alist nil)
60 (defvar nnbabyl-server-variables 
61   (list
62    (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
63    (list 'nnbabyl-active-file nnbabyl-active-file)
64    (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
65    '(nnbabyl-current-group nil)
66    '(nnbabyl-status-string "")
67    '(nnbabyl-group-alist nil)))
68
69 \f
70
71 ;;; Interface functions
72
73 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server)
74   (save-excursion
75     (set-buffer nntp-server-buffer)
76     (erase-buffer)
77     (let ((file nil)
78           (number (length sequence))
79           (count 0)
80           beg article art-string start stop)
81       (nnbabyl-possibly-change-newsgroup newsgroup)
82       (while sequence
83         (setq article (car sequence))
84         (setq art-string (nnbabyl-article-string article))
85         (set-buffer nnbabyl-mbox-buffer)
86         (if (or (search-forward art-string nil t)
87                 (progn (goto-char (point-min))
88                        (search-forward art-string nil t)))
89             (progn
90               (setq start 
91                     (save-excursion
92                       (re-search-backward 
93                        (concat "^" nnbabyl-mail-delimiter) nil t)
94                       (while (and (not (looking-at ".+:"))
95                                   (zerop (forward-line 1))))
96                       (point)))
97               (search-forward "\n\n" nil t)
98               (setq stop (1- (point)))
99               (set-buffer nntp-server-buffer)
100               (insert (format "221 %d Article retrieved.\n" article))
101               (setq beg (point))
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)
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 (fset '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 (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-save-mail ()
378   ;; Called narrowed to an article.
379   (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
380     (nnmail-insert-lines)
381     (nnmail-insert-xref group-art)
382     (nnbabyl-insert-newsgroup-line group-art)))
383
384 (defun nnbabyl-insert-newsgroup-line (group-art)
385   (save-excursion
386     (goto-char (point-min))
387     ;; If there is a C-l at the beginning of the narrowed region, this
388     ;; isn't really a "save", but rather a "scan".
389     (or (looking-at "\^L")
390         (save-excursion
391           (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
392           (goto-char (point-max))
393           (insert "\^_\n")))
394     (while (looking-at "From ")
395       (replace-match "Mail-from: " t t)
396       (forward-line 1))
397     (if (search-forward "\n\n" nil t)
398         (progn
399           (forward-char -1)
400           (while group-art
401             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
402                             (car (car group-art)) (cdr (car group-art))
403                             (current-time-string)))
404             (setq group-art (cdr group-art)))))
405     t))
406
407 (defun nnbabyl-active-number (group)
408   ;; Find the next article number in GROUP.
409   (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
410     (setcdr active (1+ (cdr active)))
411     (cdr active)))
412
413 (defun nnbabyl-read-mbox ()
414   (nnbabyl-request-list)
415   (setq nnbabyl-group-alist (nnmail-get-active))
416   (or (file-exists-p nnbabyl-mbox-file)
417       (save-excursion
418         (set-buffer (setq nnbabyl-mbox-buffer
419                           (create-file-buffer nnbabyl-mbox-file)))
420         (setq buffer-file-name nnbabyl-mbox-file)
421         (insert "BABYL OPTIONS:\n\n\^_")
422         (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
423   (if (and nnbabyl-mbox-buffer
424            (get-buffer nnbabyl-mbox-buffer)
425            (buffer-name nnbabyl-mbox-buffer)
426            (save-excursion
427              (set-buffer nnbabyl-mbox-buffer)
428              (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
429       ()
430     (save-excursion
431       (let ((delim (concat "^" nnbabyl-mail-delimiter))
432             (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
433                      (create-file-buffer nnbabyl-mbox-file)))
434             start end)
435         (set-buffer (setq nnbabyl-mbox-buffer buf))
436         (buffer-disable-undo (current-buffer))
437
438         (insert-file-contents nnbabyl-mbox-file)
439         (setq buffer-file-name nnbabyl-mbox-file)
440         (set-buffer-modified-p nil)
441
442         (goto-char (point-min))
443         (while (re-search-forward delim nil t)
444           (setq start (match-beginning 0))
445           (if (and
446                (save-excursion (re-search-forward delim nil t))
447                (not (search-forward 
448                      "\nX-Gnus-Newsgroup: " 
449                      (save-excursion 
450                        (setq end (or (and (re-search-forward delim nil t)
451                                           (match-beginning 0))
452                                      (point-max)))) t)))
453               (progn
454                 (goto-char end)
455                 (save-excursion
456                   (save-restriction
457                     (goto-char start)
458                     (narrow-to-region (1+ start) end)
459                     (nnbabyl-save-mail))))))
460         (and (buffer-modified-p (current-buffer)) (save-buffer))
461         (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
462
463 (defun nnbabyl-get-new-mail ()
464   "Read new incoming mail."
465   (let ((spools (if (listp nnmail-spool-file) nnmail-spool-file
466                   (list nnmail-spool-file)))
467         incoming incomings)
468     (nnbabyl-read-mbox)
469     (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
470         ()
471       ;; We go through all the existing spool files and split the
472       ;; mail from each.
473       (while spools
474         (and
475          (file-exists-p (car spools))
476          (> (nth 7 (file-attributes (car spools))) 0)
477          (progn
478            (and gnus-verbose-backends 
479                 (message "nnbabyl: Reading incoming mail..."))
480            (setq incoming 
481                  (nnmail-move-inbox 
482                   (car spools) (concat nnbabyl-mbox-file "-Incoming")))
483            (setq incomings (cons incoming incomings))
484            (save-excursion
485              (let ((in-buf (nnmail-split-incoming 
486                             incoming 'nnbabyl-save-mail t)))
487                (set-buffer in-buf)
488                (goto-char (point-min))
489                (while (search-forward "\n\^_\n" nil t)
490                  (delete-char -1))
491                (set-buffer nnbabyl-mbox-buffer)
492                (goto-char (point-max))
493                (search-backward "\n\^_" nil t)
494                (goto-char (match-end 0))
495                (insert-buffer-substring in-buf)
496                (kill-buffer in-buf)))))
497         (setq spools (cdr spools)))
498       ;; If we did indeed read any incoming spools, we save all info. 
499       (and (buffer-modified-p nnbabyl-mbox-buffer) 
500            (save-excursion
501              (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
502              (set-buffer nnbabyl-mbox-buffer)
503              (save-buffer)))
504       (while incomings
505         ;; The following has been commented away, just to make sure
506         ;; that nobody ever loses any mail. If you feel safe that
507         ;; nnfolder will never do anything strange, just remove those
508         ;; two semicolons, and avoid having lots of "Incoming*"
509         ;; files. 
510         ;; (and (file-writable-p incoming) (delete-file incoming))
511         (setq incomings (cdr incomings))))))
512
513 (provide 'nnbabyl)
514
515 ;;; nnbabyl.el ends here