*** 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 (defvar nnbabyl-active-timestamp nil)
59
60 \f
61
62 (defvar nnbabyl-current-server nil)
63 (defvar nnbabyl-server-alist nil)
64 (defvar nnbabyl-server-variables 
65   (list
66    (list 'nnbabyl-mbox-file nnbabyl-mbox-file)
67    (list 'nnbabyl-active-file nnbabyl-active-file)
68    (list 'nnbabyl-get-new-mail nnbabyl-get-new-mail)
69    '(nnbabyl-current-group nil)
70    '(nnbabyl-status-string "")
71    '(nnbabyl-group-alist nil)))
72
73 \f
74
75 ;;; Interface functions
76
77 (defun nnbabyl-retrieve-headers (sequence &optional newsgroup server)
78   (save-excursion
79     (set-buffer nntp-server-buffer)
80     (erase-buffer)
81     (let ((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         (set-buffer nntp-server-buffer)
121         (goto-char (point-min))
122         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
123           (replace-match " " t t))
124         'headers))))
125
126 (defun nnbabyl-open-server (server &optional defs)
127   (nnheader-init-server-buffer)
128   (if (equal server nnbabyl-current-server)
129       t
130     (if nnbabyl-current-server
131         (setq nnbabyl-server-alist 
132               (cons (list nnbabyl-current-server
133                           (nnheader-save-variables nnbabyl-server-variables))
134                     nnbabyl-server-alist)))
135     (let ((state (assoc server nnbabyl-server-alist)))
136       (if state 
137           (progn
138             (nnheader-restore-variables (nth 1 state))
139             (setq nnbabyl-server-alist (delq state nnbabyl-server-alist)))
140         (nnheader-set-init-variables nnbabyl-server-variables defs)))
141     (setq nnbabyl-current-server server)))
142
143 (defun nnbabyl-close-server (&optional server)
144   t)
145
146 (defun nnbabyl-server-opened (&optional server)
147   (and (equal server nnbabyl-current-server)
148        nnbabyl-mbox-buffer
149        (buffer-name nnbabyl-mbox-buffer)
150        nntp-server-buffer
151        (buffer-name nntp-server-buffer)))
152
153 (defun nnbabyl-status-message (&optional server)
154   nnbabyl-status-string)
155
156 (defun nnbabyl-request-article (article &optional newsgroup server buffer)
157   (nnbabyl-possibly-change-newsgroup newsgroup)
158   (if (stringp article)
159       nil
160     (save-excursion
161       (set-buffer nnbabyl-mbox-buffer)
162       (goto-char (point-min))
163       (if (search-forward (nnbabyl-article-string article) nil t)
164           (let (start stop)
165             (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
166             (while (and (not (looking-at ".+:"))
167                         (zerop (forward-line 1))))
168             (setq start (point))
169             (or (and (re-search-forward 
170                       (concat "^" nnbabyl-mail-delimiter) nil t)
171                      (forward-line -1))
172                 (goto-char (point-max)))
173             (setq stop (point))
174             (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
175               (set-buffer nntp-server-buffer)
176               (erase-buffer)
177               (insert-buffer-substring nnbabyl-mbox-buffer start stop)
178               (goto-char (point-min))
179               (if (search-forward "\n*** EOOH ***" nil t)
180                   (progn
181                     (delete-region (progn (beginning-of-line) (point))
182                                    (or (search-forward "\n\n" nil t)
183                                        (point)))))
184               t))))))
185
186 (defun nnbabyl-request-group (group &optional server dont-check)
187   (save-excursion
188     (if (nnbabyl-possibly-change-newsgroup group)
189         (if dont-check
190             t
191           (nnbabyl-get-new-mail group)
192           (save-excursion
193             (set-buffer nntp-server-buffer)
194             (erase-buffer)
195             (let ((active (assoc group nnbabyl-group-alist)))
196               (insert (format "211 %d %d %d %s\n" 
197                               (1+ (- (cdr (car (cdr active)))
198                                      (car (car (cdr active)))))
199                               (car (car (cdr active)))
200                               (cdr (car (cdr active)))
201                               (car active))))
202             t)))))
203
204 (defun nnbabyl-close-group (group &optional server)
205   t)
206
207 (defun nnbabyl-request-create-group (group &optional server) 
208   (nnmail-activate 'nnbabyl)
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     (nnmail-activate 'nnbabyl)
246
247     (save-excursion 
248       (set-buffer nnbabyl-mbox-buffer)
249       (while (and articles is-old)
250         (goto-char (point-min))
251         (if (search-forward (nnbabyl-article-string (car articles)) nil t)
252             (if (or force
253                     (setq is-old
254                           (> (nnmail-days-between 
255                               (current-time-string)
256                               (buffer-substring 
257                                (point) (progn (end-of-line) (point))))
258                              days)))
259                 (progn
260                   (and gnus-verbose-backends
261                        (message "Deleting article %s..." (car articles)))
262                   (nnbabyl-delete-mail))
263               (setq rest (cons (car articles) rest))))
264         (setq articles (cdr articles)))
265       (save-buffer)
266       ;; Find the lowest active article in this group.
267       (let ((active (nth 1 (assoc newsgroup nnbabyl-group-alist))))
268         (goto-char (point-min))
269         (while (and (not (search-forward
270                           (nnbabyl-article-string (car active)) nil t))
271                     (<= (car active) (cdr active)))
272           (setcar active (1+ (car active)))
273           (goto-char (point-min))))
274       (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
275       (nconc rest articles))))
276
277 (defun nnbabyl-request-move-article 
278   (article group server accept-form &optional last)
279   (nnbabyl-possibly-change-newsgroup group)
280   (let ((buf (get-buffer-create " *nnbabyl move*"))
281         result)
282     (and 
283      (nnbabyl-request-article article group server)
284      (save-excursion
285        (set-buffer buf)
286        (insert-buffer-substring nntp-server-buffer)
287        (goto-char (point-min))
288        (if (re-search-forward 
289             "^X-Gnus-Newsgroup:" 
290             (save-excursion (search-forward "\n\n" nil t) (point)) t)
291            (delete-region (progn (beginning-of-line) (point))
292                           (progn (forward-line 1) (point))))
293        (setq result (eval accept-form))
294        (kill-buffer (current-buffer))
295        result)
296      (save-excursion
297        (set-buffer nnbabyl-mbox-buffer)
298        (goto-char (point-min))
299        (if (search-forward (nnbabyl-article-string article) nil t)
300            (nnbabyl-delete-mail))
301        (and last (save-buffer))))
302     result))
303
304 (defun nnbabyl-request-accept-article (group &optional last)
305   (let ((buf (current-buffer))
306         result beg)
307     (and 
308      (nnmail-activate 'nnbabyl)
309      (save-excursion
310        (goto-char (point-min))
311        (search-forward "\n\n" nil t)
312        (forward-line -1)
313        (save-excursion
314          (while (re-search-backward "^X-Gnus-Newsgroup: " beg t)
315            (delete-region (point) (progn (forward-line 1) (point)))))
316        (let ((nnmail-split-methods
317               (if (stringp group) (list (list group "")) 
318                 nnmail-split-methods)))
319          (setq result (car (nnbabyl-save-mail))))
320        (set-buffer nnbabyl-mbox-buffer)
321        (goto-char (point-max))
322        (search-backward "\n\^_")
323        (goto-char (match-end 0))
324        (insert-buffer buf)
325        (and last (progn 
326                    (save-buffer)
327                    (nnmail-save-active
328                     nnbabyl-group-alist nnbabyl-active-file)))
329        result))))
330
331 (defun nnbabyl-request-replace-article (article group buffer)
332   (nnbabyl-possibly-change-newsgroup group)
333   (save-excursion
334     (set-buffer nnbabyl-mbox-buffer)
335     (goto-char (point-min))
336     (if (not (search-forward (nnbabyl-article-string article) nil t))
337         nil
338       (nnbabyl-delete-mail t t)
339       (insert-buffer-substring buffer)
340       (save-buffer)
341       t)))
342
343 \f
344 ;;; Low-Level Interface
345
346 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
347 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
348 ;; delimeter line.
349 (defun nnbabyl-delete-mail (&optional force leave-delim)
350   ;; Delete the current X-Gnus-Newsgroup line.
351   (or force
352       (delete-region
353        (progn (beginning-of-line) (point))
354        (progn (forward-line 1) (point))))
355   ;; Beginning of the article.
356   (save-excursion
357     (save-restriction
358       (widen)
359       (narrow-to-region
360        (save-excursion
361          (re-search-backward (concat "^" nnbabyl-mail-delimiter) nil t)
362          (if leave-delim (progn (forward-line 1) (point))
363            (match-beginning 0)))
364        (progn
365          (forward-line 1)
366          (or (and (re-search-forward (concat "^" nnbabyl-mail-delimiter) 
367                                      nil t)
368                   (if (and (not (bobp)) leave-delim)
369                       (progn (forward-line -2) (point))
370                     (match-beginning 0)))
371              (point-max))))
372       (goto-char (point-min))
373       ;; Only delete the article if no other groups owns it as well.
374       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
375           (delete-region (point-min) (point-max))))))
376
377 (defun nnbabyl-possibly-change-newsgroup (newsgroup)
378   (if (or (not nnbabyl-mbox-buffer)
379           (not (buffer-name nnbabyl-mbox-buffer)))
380       (save-excursion (nnbabyl-read-mbox)))
381   (or nnbabyl-group-alist
382       (nnmail-activate 'nnbabyl))
383   (if newsgroup
384       (if (assoc newsgroup nnbabyl-group-alist)
385           (setq nnbabyl-current-group newsgroup)
386         (setq nnbabyl-status-string "No such group in file")
387         nil)))
388
389 (defun nnbabyl-article-string (article)
390   (concat "\nX-Gnus-Newsgroup: " nnbabyl-current-group ":" 
391           (int-to-string article) " "))
392
393 (defun nnbabyl-insert-lines ()
394   "Insert how many lines and chars there are in the body of the mail."
395   (let (lines chars)
396     (save-excursion
397       (goto-char (point-min))
398       (if (search-forward "\n\n" nil t) 
399           (progn
400             ;; There may be an EOOH line here...
401             (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*")
402                 (search-forward "\n\n" nil t))
403             (setq chars (- (point-max) (point)))
404             (setq lines (- (count-lines (point) (point-max)) 1))
405             ;; Move back to the end of the headers. 
406             (goto-char (point-min))
407             (search-forward "\n\n" nil t)
408             (forward-char -1)
409             (save-excursion
410               (if (re-search-backward "^Lines: " nil t)
411                   (delete-region (point) (progn (forward-line 1) (point)))))
412             (insert (format "Lines: %d\n" lines))
413             chars)))))
414
415 (defun nnbabyl-save-mail ()
416   ;; Called narrowed to an article.
417   (let ((group-art (nreverse (nnmail-article-group 'nnbabyl-active-number))))
418     (nnbabyl-insert-lines)
419     (nnmail-insert-xref group-art)
420     (nnbabyl-insert-newsgroup-line group-art)
421     (run-hooks 'nnbabyl-prepare-save-mail-hook)
422     group-art))
423
424 (defun nnbabyl-insert-newsgroup-line (group-art)
425   (save-excursion
426     (goto-char (point-min))
427     ;; If there is a C-l at the beginning of the narrowed region, this
428     ;; isn't really a "save", but rather a "scan".
429     (while (looking-at "From ")
430       (replace-match "Mail-from: From " t t)
431       (forward-line 1))
432     (goto-char (point-min))
433     (or (looking-at "\^L")
434         (save-excursion
435           (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
436           (goto-char (point-max))
437           (insert "\^_\n")))
438     (if (search-forward "\n\n" nil t)
439         (progn
440           (forward-char -1)
441           (while group-art
442             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
443                             (car (car group-art)) (cdr (car group-art))
444                             (current-time-string)))
445             (setq group-art (cdr group-art)))))
446     t))
447
448 (defun nnbabyl-active-number (group)
449   ;; Find the next article number in GROUP.
450   (let ((active (car (cdr (assoc group nnbabyl-group-alist)))))
451     (if active
452         (setcdr active (1+ (cdr active)))
453       ;; This group is new, so we create a new entry for it.
454       ;; This might be a bit naughty... creating groups on the drop of
455       ;; a hat, but I don't know...
456       (setq nnbabyl-group-alist (cons (list group (setq active (cons 1 1)))
457                                       nnbabyl-group-alist)))
458     (cdr active)))
459
460 (defun nnbabyl-read-mbox ()
461   (nnmail-activate 'nnbabyl)
462   (or (file-exists-p nnbabyl-mbox-file)
463       (save-excursion
464         (set-buffer (setq nnbabyl-mbox-buffer
465                           (create-file-buffer nnbabyl-mbox-file)))
466         (setq buffer-file-name nnbabyl-mbox-file)
467         (insert "BABYL OPTIONS:\n\n\^_")
468         (write-region (point-min) (point-max) nnbabyl-mbox-file t 'nomesg)))
469
470   (if (and nnbabyl-mbox-buffer
471            (buffer-name nnbabyl-mbox-buffer)
472            (save-excursion
473              (set-buffer nnbabyl-mbox-buffer)
474              (= (buffer-size) (nth 7 (file-attributes nnbabyl-mbox-file)))))
475       ()
476     (save-excursion
477       (let ((delim (concat "^" nnbabyl-mail-delimiter))
478             (buf (or (get-buffer (file-name-nondirectory nnbabyl-mbox-file))
479                      (create-file-buffer nnbabyl-mbox-file)))
480             start end)
481         (set-buffer (setq nnbabyl-mbox-buffer buf))
482         (buffer-disable-undo (current-buffer))
483
484         (insert-file-contents nnbabyl-mbox-file)
485         (setq buffer-file-name nnbabyl-mbox-file)
486         (set-buffer-modified-p nil)
487
488         (goto-char (point-min))
489         (while (re-search-forward delim nil t)
490           (setq start (match-beginning 0))
491           (if (and
492                (save-excursion (re-search-forward delim nil t))
493                (not (search-forward 
494                      "\nX-Gnus-Newsgroup: " 
495                      (save-excursion 
496                        (setq end (or (and (re-search-forward delim nil t)
497                                           (match-beginning 0))
498                                      (point-max)))) t)))
499               (progn
500                 (goto-char end)
501                 (save-excursion
502                   (save-restriction
503                     (goto-char start)
504                     (narrow-to-region (1+ start) end)
505                     (nnbabyl-save-mail))))))
506         (and (buffer-modified-p (current-buffer)) (save-buffer))
507         (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)))))
508
509 (defun nnbabyl-remove-incoming-delims ()
510   (goto-char (point-min))
511   (while (search-forward "\^_" nil t)
512     (replace-match "?" t t)))
513
514 (defun nnbabyl-get-new-mail (&optional group)
515   "Read new incoming mail."
516   (let* ((spools (nnmail-get-spool-files group))
517          (group-in group)
518          incoming incomings)
519     (nnbabyl-read-mbox)
520     (if (or (not nnbabyl-get-new-mail) (not nnmail-spool-file))
521         ()
522       ;; We go through all the existing spool files and split the
523       ;; mail from each.
524       (while spools
525         (and
526          (file-exists-p (car spools))
527          (> (nth 7 (file-attributes (car spools))) 0)
528          (progn
529            (and gnus-verbose-backends 
530                 (message "nnbabyl: Reading incoming mail..."))
531            (setq incoming 
532                  (nnmail-move-inbox 
533                   (car spools) (concat nnbabyl-mbox-file "-Incoming")))
534            (setq incomings (cons incoming incomings))
535            (save-excursion
536              (setq group (nnmail-get-split-group (car spools) group-in))
537              (let* ((nnmail-prepare-incoming-hook
538                      (cons 'nnbabyl-remove-incoming-delims
539                            nnmail-prepare-incoming-hook))
540                     in-buf)
541                (setq in-buf (nnmail-split-incoming 
542                              incoming 'nnbabyl-save-mail t group))
543                (set-buffer in-buf)
544                (goto-char (point-min))
545                (while (search-forward "\n\^_\n" nil t)
546                  (delete-char -1))
547                (set-buffer nnbabyl-mbox-buffer)
548                (goto-char (point-max))
549                (search-backward "\n\^_" nil t)
550                (goto-char (match-end 0))
551                (insert-buffer-substring in-buf)
552                (kill-buffer in-buf)))))
553         (setq spools (cdr spools)))
554       ;; If we did indeed read any incoming spools, we save all info. 
555       (and (buffer-modified-p nnbabyl-mbox-buffer) 
556            (save-excursion
557              (nnmail-save-active nnbabyl-group-alist nnbabyl-active-file)
558              (set-buffer nnbabyl-mbox-buffer)
559              (save-buffer)))
560       (if incomings (run-hooks 'nnmail-read-incoming-hook))
561       (while incomings
562         (setq incoming (car incomings))
563         (and nnmail-delete-incoming
564              (file-exists-p incoming) 
565              (file-writable-p incoming) 
566              (delete-file incoming))
567         (setq incomings (cdr incomings))))))
568
569 (provide 'nnbabyl)
570
571 ;;; nnbabyl.el ends here