*** 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   (equal server nnmbox-current-server))
147
148 (defun nnmbox-status-message (&optional server)
149   nnmbox-status-string)
150
151 (defun nnmbox-request-article (article &optional newsgroup server buffer)
152   (nnmbox-possibly-change-newsgroup newsgroup)
153   (if (stringp article)
154       nil
155     (save-excursion
156       (set-buffer nnmbox-mbox-buffer)
157       (goto-char 1)
158       (if (search-forward (nnmbox-article-string article) nil t)
159           (let (start stop)
160             (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
161             (setq start (point))
162             (forward-line 1)
163             (or (and (re-search-forward 
164                       (concat "^" rmail-unix-mail-delimiter) nil t)
165                      (forward-line -1))
166                 (goto-char (point-max)))
167             (setq stop (point))
168             (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
169               (set-buffer nntp-server-buffer)
170               (erase-buffer)
171               (insert-buffer-substring nnmbox-mbox-buffer start stop)
172               (goto-char (point-min))
173               (while (looking-at "From ")
174                 (delete-char 5)
175                 (insert "X-From-Line: ")
176                 (forward-line 1))
177               t))))))
178
179 (defun nnmbox-request-group (group &optional server dont-check)
180   (save-excursion
181     (if (nnmbox-possibly-change-newsgroup group)
182         (if dont-check
183             t
184           (nnmbox-get-new-mail)
185           (save-excursion
186             (set-buffer nntp-server-buffer)
187             (erase-buffer)
188             (let ((active (assoc group nnmbox-group-alist)))
189               (insert (format "211 %d %d %d %s\n" 
190                               (1+ (- (cdr (car (cdr active)))
191                                      (car (car (cdr active)))))
192                               (car (car (cdr active)))
193                               (cdr (car (cdr active)))
194                               (car active))))
195             t)))))
196
197 (defun nnmbox-close-group (group &optional server)
198   t)
199
200 (defun nnmbox-request-list (&optional server)
201   (if server (nnmbox-get-new-mail))
202   (or (nnmail-find-file nnmbox-active-file)
203       (progn
204         (setq nnmbox-group-alist (nnmail-get-active))
205         (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
206         (nnmail-find-file nnmbox-active-file))))
207
208 (defun nnmbox-request-newgroups (date &optional server)
209   (nnmbox-request-list server))
210
211 (defun nnmbox-request-list-newsgroups (&optional server)
212   (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
213   nil)
214
215 (defun nnmbox-request-post (&optional server)
216   (mail-send-and-exit nil))
217
218 (fset 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
219
220 (defun nnmbox-request-expire-articles 
221   (articles newsgroup &optional server force)
222   (nnmbox-possibly-change-newsgroup newsgroup)
223   (let* ((days (or (and nnmail-expiry-wait-function
224                         (funcall nnmail-expiry-wait-function newsgroup))
225                    nnmail-expiry-wait))
226          article rest)
227     (save-excursion 
228       (set-buffer nnmbox-mbox-buffer)
229       (while articles
230         (goto-char 1)
231         (if (search-forward (nnmbox-article-string (car articles)) nil t)
232             (if (or force
233                     (> (nnmail-days-between 
234                         (current-time-string)
235                         (buffer-substring 
236                          (point) (progn (end-of-line) (point))))
237                        days))
238                 (progn
239                   (and gnus-verbose-backends
240                        (message "Deleting: %s" (car articles)))
241                   (nnmbox-delete-mail))
242               (setq rest (cons (car articles) rest))))
243         (setq articles (cdr articles)))
244       (save-buffer)
245       ;; Find the lowest active article in this group.
246       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
247         (goto-char (point-min))
248         (while (not (search-forward
249                      (nnmbox-article-string (car active)) nil t))
250           (setcar (car active) (1+ (car active)))
251           (goto-char (point-min))))
252       (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
253       rest)))
254
255 (defun nnmbox-request-move-article
256   (article group server accept-form &optional last)
257   (nnmbox-possibly-change-newsgroup group)
258   (let ((buf (get-buffer-create " *nnmbox move*"))
259         result)
260     (and 
261      (nnmbox-request-article article group server)
262      (save-excursion
263        (set-buffer buf)
264        (buffer-disable-undo (current-buffer))
265        (erase-buffer)
266        (insert-buffer-substring nntp-server-buffer)
267        (goto-char (point-min))
268        (while (re-search-forward 
269                "^X-Gnus-Newsgroup:" 
270                (save-excursion (search-forward "\n\n" nil t) (point)) t)
271          (delete-region (progn (beginning-of-line) (point))
272                         (progn (forward-line 1) (point))))
273        (setq result (eval accept-form))
274        (kill-buffer buf)
275        result)
276      (save-excursion
277        (set-buffer nnmbox-mbox-buffer)
278        (goto-char 1)
279        (if (search-forward (nnmbox-article-string article) nil t)
280            (nnmbox-delete-mail))
281        (and last (save-buffer))))
282     result))
283
284 (defun nnmbox-request-accept-article (group &optional last)
285   (let ((buf (current-buffer))
286         result beg)
287     (goto-char (point-min))
288     (if (looking-at "X-From-Line: ")
289         (replace-match "From ")
290       (insert "From nobody " (current-time-string) "\n"))
291     (and 
292      (nnmbox-request-list)
293      (setq nnmbox-group-alist (nnmail-get-active))
294      (progn
295        (set-buffer buf)
296        (goto-char (point-min))
297        (search-forward "\n\n" nil t)
298        (forward-line -1)
299        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
300          (delete-region (point) (progn (forward-line 1) (point))))
301        (setq result (nnmbox-save-mail (and (stringp group) group))))
302      (save-excursion
303        (set-buffer nnmbox-mbox-buffer)
304        (insert-buffer-substring buf)
305        (and last (save-buffer))
306        result)
307      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
308     (car result)))
309
310 (defun nnmbox-request-replace-article (article group buffer)
311   (nnmbox-possibly-change-newsgroup group)
312   (save-excursion
313     (set-buffer nnmbox-mbox-buffer)
314     (goto-char 1)
315     (if (not (search-forward (nnmbox-article-string article) nil t))
316         nil
317       (nnmbox-delete-mail t t)
318       (insert-buffer-substring buffer)
319       (save-buffer)
320       t)))
321
322 \f
323 ;;; Internal functions.
324
325 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
326 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
327 ;; delimeter line.
328 (defun nnmbox-delete-mail (&optional force leave-delim)
329   ;; Delete the current X-Gnus-Newsgroup line.
330   (or force
331       (delete-region
332        (progn (beginning-of-line) (point))
333        (progn (forward-line 1) (point))))
334   ;; Beginning of the article.
335   (save-excursion
336     (save-restriction
337       (narrow-to-region
338        (save-excursion
339          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
340          (if leave-delim (progn (forward-line 1) (point))
341            (match-beginning 0)))
342        (progn
343          (forward-line 1)
344          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
345                                      nil t)
346                   (if (and (not (bobp)) leave-delim)
347                       (progn (forward-line -2) (point))
348                     (match-beginning 0)))
349              (point-max))))
350       (goto-char (point-min))
351       ;; Only delete the article if no other groups owns it as well.
352       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
353           (delete-region (point-min) (point-max))))))
354
355 (defun nnmbox-possibly-change-newsgroup (newsgroup)
356   (if (not (get-buffer nnmbox-mbox-buffer))
357       (save-excursion
358         (set-buffer (setq nnmbox-mbox-buffer 
359                           (find-file-noselect nnmbox-mbox-file)))
360         (buffer-disable-undo (current-buffer))))
361   (if (not nnmbox-group-alist)
362       (progn
363         (nnmbox-request-list)
364         (setq nnmbox-group-alist (nnmail-get-active))))
365   (if newsgroup
366       (if (assoc newsgroup nnmbox-group-alist)
367           (setq nnmbox-current-group newsgroup))))
368
369 (defun nnmbox-article-string (article)
370   (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":" 
371           (int-to-string article) " "))
372
373 (defun nnmbox-save-mail (&optional group)
374   "Called narrowed to an article."
375   (let* ((nnmail-split-methods 
376           (if group (list (list group "")) nnmail-split-methods))
377          (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
378     (nnmail-insert-lines)
379     (nnmail-insert-xref group-art)
380     (nnmbox-insert-newsgroup-line group-art)
381     group-art))
382
383 (defun nnmbox-insert-newsgroup-line (group-art)
384   (save-excursion
385     (goto-char (point-min))
386     (if (search-forward "\n\n" nil t)
387         (progn
388           (forward-char -1)
389           (while group-art
390             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
391                             (car (car group-art)) (cdr (car group-art))
392                             (current-time-string)))
393             (setq group-art (cdr group-art)))))
394     t))
395
396 (defun nnmbox-active-number (group)
397   "Find the next article number in GROUP."
398   (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
399     (setcdr active (1+ (cdr active)))
400     (cdr active)))
401
402 (defun nnmbox-read-mbox ()
403   (nnmbox-request-list)
404   (setq nnmbox-group-alist (nnmail-get-active))
405   (if (not (file-exists-p nnmbox-mbox-file))
406       (write-region 1 1 nnmbox-mbox-file t 'nomesg))
407   (if (and nnmbox-mbox-buffer
408            (get-buffer nnmbox-mbox-buffer)
409            (buffer-name nnmbox-mbox-buffer)
410            (save-excursion
411              (set-buffer nnmbox-mbox-buffer)
412              (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
413       ()
414     (save-excursion
415       (let ((delim (concat "^" rmail-unix-mail-delimiter))
416             start end)
417         (set-buffer (setq nnmbox-mbox-buffer 
418                           (find-file-noselect nnmbox-mbox-file)))
419         (buffer-disable-undo (current-buffer))
420         (goto-char (point-min))
421         (while (re-search-forward delim nil t)
422           (setq start (match-beginning 0))
423           (if (not (search-forward "\nX-Gnus-Newsgroup: " 
424                                    (save-excursion 
425                                      (setq end
426                                            (or
427                                             (and
428                                              (re-search-forward delim nil t)
429                                              (match-beginning 0))
430                                             (point-max))))
431                                    t))
432               (save-excursion
433                 (save-restriction
434                   (narrow-to-region start end)
435                   (nnmbox-save-mail))))
436           (goto-char end))))))
437
438 (defun nnmbox-get-new-mail ()
439   (let (incoming)
440     (nnmbox-read-mbox)
441     (if (and nnmail-spool-file nnmbox-get-new-mail
442              (file-exists-p nnmail-spool-file)
443              (> (nth 7 (file-attributes nnmail-spool-file)) 0))
444         (progn
445           (and gnus-verbose-backends
446                (message "nnmbox: Reading incoming mail..."))
447           (setq incoming 
448                 (nnmail-move-inbox nnmail-spool-file
449                                    (concat nnmbox-mbox-file "-Incoming")))
450           (save-excursion
451             (let ((in-buf (nnmail-split-incoming 
452                            incoming 'nnmbox-save-mail t)))
453               (set-buffer nnmbox-mbox-buffer)
454               (goto-char (point-max))
455               (insert-buffer-substring in-buf)
456               (kill-buffer in-buf)))
457           (run-hooks 'nnmail-read-incoming-hook)
458           (and gnus-verbose-backends
459                (message "nnmbox: Reading incoming mail...done"))))
460     (and (buffer-modified-p nnmbox-mbox-buffer) 
461          (save-excursion
462            (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
463            (set-buffer nnmbox-mbox-buffer)
464            (save-buffer)))
465 ;    (if incoming
466 ;       (delete-file incoming))
467     ))
468
469 (provide 'nnmbox)
470
471 ;;; nnmbox.el ends here