*** empty log message ***
[gnus] / lisp / nnmbox.el
1 ;;; nnmbox.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 nnmbox-mbox-file (expand-file-name "~/mbox")
36   "The name of the mail box file in the users home directory.")
37
38 (defvar nnmbox-active-file (expand-file-name "~/.mbox-active")
39   "The name of the active file for the mail box.")
40
41 (defvar nnmbox-get-new-mail t
42   "If non-nil, nnml will check the incoming mail file and split the mail.")
43
44 \f
45
46 (defconst nnmbox-version "nnmbox 0.1"
47   "nnmbox version.")
48
49 (defvar nnmbox-current-group nil
50   "Current nnmbox news group directory.")
51
52 (defconst nnmbox-mbox-buffer " *nnmbox mbox buffer*")
53
54 (defvar nnmbox-status-string "")
55
56 (defvar nnmbox-group-alist nil)
57
58 \f
59
60 (defvar nnmbox-current-server nil)
61 (defvar nnmbox-server-alist nil)
62 (defvar nnmbox-server-variables 
63   (list
64    (list 'nnmbox-mbox-file nnmbox-mbox-file)
65    (list 'nnmbox-active-file nnmbox-active-file)
66    (list 'nnmbox-get-new-mail nnmbox-get-new-mail)
67    '(nnmbox-current-group nil)
68    '(nnmbox-status-string "")
69    '(nnmbox-group-alist nil)))
70
71 \f
72
73 ;;; Interface functions
74
75 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server)
76   (save-excursion
77     (set-buffer nntp-server-buffer)
78     (erase-buffer)
79     (let ((file nil)
80           (number (length sequence))
81           (count 0)
82           beg article art-string start stop)
83       (nnmbox-possibly-change-newsgroup newsgroup)
84       (while sequence
85         (setq article (car sequence))
86         (setq art-string (nnmbox-article-string article))
87         (set-buffer nnmbox-mbox-buffer)
88         (if (or (search-forward art-string nil t)
89                 (progn (goto-char 1)
90                        (search-forward art-string nil t)))
91             (progn
92               (setq start 
93                     (save-excursion
94                       (re-search-backward 
95                        (concat "^" rmail-unix-mail-delimiter) nil t)
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 nnmbox-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 "nnmbox: 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 "nnmbox: Receiving headers... done"))
118
119       ;; Fold continuation lines.
120       (goto-char 1)
121       (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
122         (replace-match " " t t))
123       'headers)))
124
125 (defun nnmbox-open-server (server &optional defs)
126   (nnheader-init-server-buffer)
127   (if (equal server nnmbox-current-server)
128       t
129     (if nnmbox-current-server
130         (setq nnmbox-server-alist 
131               (cons (list nnmbox-current-server
132                           (nnheader-save-variables nnmbox-server-variables))
133                     nnmbox-server-alist)))
134     (let ((state (assoc server nnmbox-server-alist)))
135       (if state 
136           (progn
137             (nnheader-restore-variables (nth 1 state))
138             (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
139         (nnheader-set-init-variables nnmbox-server-variables defs)))
140     (setq nnmbox-current-server server)))
141
142 (defun nnmbox-close-server (&optional server)
143   t)
144
145 (defun nnmbox-server-opened (&optional server)
146   (and (equal server nnmbox-current-server)
147        nntp-server-buffer
148        (buffer-name nntp-server-buffer)))
149
150 (defun nnmbox-status-message (&optional server)
151   nnmbox-status-string)
152
153 (defun nnmbox-request-article (article &optional newsgroup server buffer)
154   (nnmbox-possibly-change-newsgroup newsgroup)
155   (if (stringp article)
156       nil
157     (save-excursion
158       (set-buffer nnmbox-mbox-buffer)
159       (goto-char 1)
160       (if (search-forward (nnmbox-article-string article) nil t)
161           (let (start stop)
162             (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
163             (setq start (point))
164             (forward-line 1)
165             (or (and (re-search-forward 
166                       (concat "^" rmail-unix-mail-delimiter) nil t)
167                      (forward-line -1))
168                 (goto-char (point-max)))
169             (setq stop (point))
170             (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
171               (set-buffer nntp-server-buffer)
172               (erase-buffer)
173               (insert-buffer-substring nnmbox-mbox-buffer start stop)
174               (goto-char (point-min))
175               (while (looking-at "From ")
176                 (delete-char 5)
177                 (insert "X-From-Line: ")
178                 (forward-line 1))
179               t))))))
180
181 (defun nnmbox-request-group (group &optional server dont-check)
182   (save-excursion
183     (if (nnmbox-possibly-change-newsgroup group)
184         (if dont-check
185             t
186           (nnmbox-get-new-mail)
187           (save-excursion
188             (set-buffer nntp-server-buffer)
189             (erase-buffer)
190             (let ((active (assoc group nnmbox-group-alist)))
191               (insert (format "211 %d %d %d %s\n" 
192                               (1+ (- (cdr (car (cdr active)))
193                                      (car (car (cdr active)))))
194                               (car (car (cdr active)))
195                               (cdr (car (cdr active)))
196                               (car active))))
197             t)))))
198
199 (defun nnmbox-close-group (group &optional server)
200   t)
201
202 (defun nnmbox-request-list (&optional server)
203   (if server (nnmbox-get-new-mail))
204   (or (nnmail-find-file nnmbox-active-file)
205       (progn
206         (setq nnmbox-group-alist (nnmail-get-active))
207         (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
208         (nnmail-find-file nnmbox-active-file))))
209
210 (defun nnmbox-request-newgroups (date &optional server)
211   (nnmbox-request-list server))
212
213 (defun nnmbox-request-list-newsgroups (&optional server)
214   (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
215   nil)
216
217 (defun nnmbox-request-post (&optional server)
218   (mail-send-and-exit nil))
219
220 (fset 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
221
222 (defun nnmbox-request-expire-articles 
223   (articles newsgroup &optional server force)
224   (nnmbox-possibly-change-newsgroup newsgroup)
225   (let* ((days (or (and nnmail-expiry-wait-function
226                         (funcall nnmail-expiry-wait-function newsgroup))
227                    nnmail-expiry-wait))
228          article rest)
229     (save-excursion 
230       (set-buffer nnmbox-mbox-buffer)
231       (while articles
232         (goto-char 1)
233         (if (search-forward (nnmbox-article-string (car articles)) nil t)
234             (if (or force
235                     (> (nnmail-days-between 
236                         (current-time-string)
237                         (buffer-substring 
238                          (point) (progn (end-of-line) (point))))
239                        days))
240                 (progn
241                   (and gnus-verbose-backends
242                        (message "Deleting: %s" (car articles)))
243                   (nnmbox-delete-mail))
244               (setq rest (cons (car articles) rest))))
245         (setq articles (cdr articles)))
246       (save-buffer)
247       ;; Find the lowest active article in this group.
248       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
249         (goto-char (point-min))
250         (while (not (search-forward
251                      (nnmbox-article-string (car active)) nil t))
252           (setcar active (1+ (car active)))
253           (goto-char (point-min))))
254       (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
255       rest)))
256
257 (defun nnmbox-request-move-article
258   (article group server accept-form &optional last)
259   (nnmbox-possibly-change-newsgroup group)
260   (let ((buf (get-buffer-create " *nnmbox move*"))
261         result)
262     (and 
263      (nnmbox-request-article article group server)
264      (save-excursion
265        (set-buffer buf)
266        (buffer-disable-undo (current-buffer))
267        (erase-buffer)
268        (insert-buffer-substring nntp-server-buffer)
269        (goto-char (point-min))
270        (while (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 buf)
277        result)
278      (save-excursion
279        (set-buffer nnmbox-mbox-buffer)
280        (goto-char 1)
281        (if (search-forward (nnmbox-article-string article) nil t)
282            (nnmbox-delete-mail))
283        (and last (save-buffer))))
284     result))
285
286 (defun nnmbox-request-accept-article (group &optional last)
287   (let ((buf (current-buffer))
288         result beg)
289     (goto-char (point-min))
290     (if (looking-at "X-From-Line: ")
291         (replace-match "From ")
292       (insert "From nobody " (current-time-string) "\n"))
293     (and 
294      (nnmbox-request-list)
295      (setq nnmbox-group-alist (nnmail-get-active))
296      (progn
297        (set-buffer buf)
298        (goto-char (point-min))
299        (search-forward "\n\n" nil t)
300        (forward-line -1)
301        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
302          (delete-region (point) (progn (forward-line 1) (point))))
303        (setq result (nnmbox-save-mail (and (stringp group) group))))
304      (save-excursion
305        (set-buffer nnmbox-mbox-buffer)
306        (insert-buffer-substring buf)
307        (and last (save-buffer))
308        result)
309      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
310     (car result)))
311
312 (defun nnmbox-request-replace-article (article group buffer)
313   (nnmbox-possibly-change-newsgroup group)
314   (save-excursion
315     (set-buffer nnmbox-mbox-buffer)
316     (goto-char 1)
317     (if (not (search-forward (nnmbox-article-string article) nil t))
318         nil
319       (nnmbox-delete-mail t t)
320       (insert-buffer-substring buffer)
321       (save-buffer)
322       t)))
323
324 \f
325 ;;; Internal functions.
326
327 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
328 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
329 ;; delimeter line.
330 (defun nnmbox-delete-mail (&optional force leave-delim)
331   ;; Delete the current X-Gnus-Newsgroup line.
332   (or force
333       (delete-region
334        (progn (beginning-of-line) (point))
335        (progn (forward-line 1) (point))))
336   ;; Beginning of the article.
337   (save-excursion
338     (save-restriction
339       (narrow-to-region
340        (save-excursion
341          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
342          (if leave-delim (progn (forward-line 1) (point))
343            (match-beginning 0)))
344        (progn
345          (forward-line 1)
346          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
347                                      nil t)
348                   (if (and (not (bobp)) leave-delim)
349                       (progn (forward-line -2) (point))
350                     (match-beginning 0)))
351              (point-max))))
352       (goto-char (point-min))
353       ;; Only delete the article if no other groups owns it as well.
354       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
355           (delete-region (point-min) (point-max))))))
356
357 (defun nnmbox-possibly-change-newsgroup (newsgroup)
358   (if (not (get-buffer nnmbox-mbox-buffer))
359       (save-excursion
360         (set-buffer (setq nnmbox-mbox-buffer 
361                           (find-file-noselect nnmbox-mbox-file)))
362         (buffer-disable-undo (current-buffer))))
363   (if (not nnmbox-group-alist)
364       (progn
365         (nnmbox-request-list)
366         (setq nnmbox-group-alist (nnmail-get-active))))
367   (if newsgroup
368       (if (assoc newsgroup nnmbox-group-alist)
369           (setq nnmbox-current-group newsgroup))))
370
371 (defun nnmbox-article-string (article)
372   (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":" 
373           (int-to-string article) " "))
374
375 (defun nnmbox-save-mail (&optional group)
376   "Called narrowed to an article."
377   (let* ((nnmail-split-methods 
378           (if group (list (list group "")) nnmail-split-methods))
379          (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
380     (nnmail-insert-lines)
381     (nnmail-insert-xref group-art)
382     (nnmbox-insert-newsgroup-line group-art)
383     group-art))
384
385 (defun nnmbox-insert-newsgroup-line (group-art)
386   (save-excursion
387     (goto-char (point-min))
388     (if (search-forward "\n\n" nil t)
389         (progn
390           (forward-char -1)
391           (while group-art
392             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
393                             (car (car group-art)) (cdr (car group-art))
394                             (current-time-string)))
395             (setq group-art (cdr group-art)))))
396     t))
397
398 (defun nnmbox-active-number (group)
399   "Find the next article number in GROUP."
400   (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
401     (setcdr active (1+ (cdr active)))
402     (cdr active)))
403
404 (defun nnmbox-read-mbox ()
405   (nnmbox-request-list)
406   (setq nnmbox-group-alist (nnmail-get-active))
407   (if (not (file-exists-p nnmbox-mbox-file))
408       (write-region 1 1 nnmbox-mbox-file t 'nomesg))
409   (if (and nnmbox-mbox-buffer
410            (get-buffer nnmbox-mbox-buffer)
411            (buffer-name nnmbox-mbox-buffer)
412            (save-excursion
413              (set-buffer nnmbox-mbox-buffer)
414              (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
415       ()
416     (save-excursion
417       (let ((delim (concat "^" rmail-unix-mail-delimiter))
418             start end)
419         (set-buffer (setq nnmbox-mbox-buffer 
420                           (find-file-noselect nnmbox-mbox-file)))
421         (buffer-disable-undo (current-buffer))
422         (goto-char (point-min))
423         (while (re-search-forward delim nil t)
424           (setq start (match-beginning 0))
425           (if (not (search-forward "\nX-Gnus-Newsgroup: " 
426                                    (save-excursion 
427                                      (setq end
428                                            (or
429                                             (and
430                                              (re-search-forward delim nil t)
431                                              (match-beginning 0))
432                                             (point-max))))
433                                    t))
434               (save-excursion
435                 (save-restriction
436                   (narrow-to-region start end)
437                   (nnmbox-save-mail))))
438           (goto-char end))))))
439
440 (defun nnmbox-get-new-mail ()
441   (let (incoming)
442     (nnmbox-read-mbox)
443     (if (and nnmail-spool-file nnmbox-get-new-mail
444              (file-exists-p nnmail-spool-file)
445              (> (nth 7 (file-attributes nnmail-spool-file)) 0))
446         (progn
447           (and gnus-verbose-backends
448                (message "nnmbox: Reading incoming mail..."))
449           (setq incoming 
450                 (nnmail-move-inbox nnmail-spool-file
451                                    (concat nnmbox-mbox-file "-Incoming")))
452           (save-excursion
453             (let ((in-buf (nnmail-split-incoming 
454                            incoming 'nnmbox-save-mail t)))
455               (set-buffer nnmbox-mbox-buffer)
456               (goto-char (point-max))
457               (insert-buffer-substring in-buf)
458               (kill-buffer in-buf)))
459           (run-hooks 'nnmail-read-incoming-hook)
460           (and gnus-verbose-backends
461                (message "nnmbox: Reading incoming mail...done"))))
462     (and (buffer-modified-p nnmbox-mbox-buffer) 
463          (save-excursion
464            (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
465            (set-buffer nnmbox-mbox-buffer)
466            (save-buffer)))
467 ;    (if incoming
468 ;       (delete-file incoming))
469     ))
470
471 (provide 'nnmbox)
472
473 ;;; nnmbox.el ends here