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