*** 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 (and (not (search-forward
255                           (nnmbox-article-string (car active)) nil t))
256                     (<= (car active) (cdr active)))
257           (setcar active (1+ (car active)))
258           (goto-char (point-min))))
259       (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
260       rest)))
261
262 (defun nnmbox-request-move-article
263   (article group server accept-form &optional last)
264   (nnmbox-possibly-change-newsgroup group)
265   (let ((buf (get-buffer-create " *nnmbox move*"))
266         result)
267     (and 
268      (nnmbox-request-article article group server)
269      (save-excursion
270        (set-buffer buf)
271        (buffer-disable-undo (current-buffer))
272        (erase-buffer)
273        (insert-buffer-substring nntp-server-buffer)
274        (goto-char (point-min))
275        (while (re-search-forward 
276                "^X-Gnus-Newsgroup:" 
277                (save-excursion (search-forward "\n\n" nil t) (point)) t)
278          (delete-region (progn (beginning-of-line) (point))
279                         (progn (forward-line 1) (point))))
280        (setq result (eval accept-form))
281        (kill-buffer buf)
282        result)
283      (save-excursion
284        (set-buffer nnmbox-mbox-buffer)
285        (goto-char (point-min))
286        (if (search-forward (nnmbox-article-string article) nil t)
287            (nnmbox-delete-mail))
288        (and last (save-buffer))))
289     result))
290
291 (defun nnmbox-request-accept-article (group &optional last)
292   (let ((buf (current-buffer))
293         result beg)
294     (goto-char (point-min))
295     (if (looking-at "X-From-Line: ")
296         (replace-match "From ")
297       (insert "From nobody " (current-time-string) "\n"))
298     (and 
299      (nnmbox-request-list)
300      (setq nnmbox-group-alist (nnmail-get-active))
301      (progn
302        (set-buffer buf)
303        (goto-char (point-min))
304        (search-forward "\n\n" nil t)
305        (forward-line -1)
306        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
307          (delete-region (point) (progn (forward-line 1) (point))))
308        (setq result (nnmbox-save-mail (and (stringp group) group))))
309      (save-excursion
310        (set-buffer nnmbox-mbox-buffer)
311        (insert-buffer-substring buf)
312        (and last (save-buffer))
313        result)
314      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
315     (car result)))
316
317 (defun nnmbox-request-replace-article (article group buffer)
318   (nnmbox-possibly-change-newsgroup group)
319   (save-excursion
320     (set-buffer nnmbox-mbox-buffer)
321     (goto-char (point-min))
322     (if (not (search-forward (nnmbox-article-string article) nil t))
323         nil
324       (nnmbox-delete-mail t t)
325       (insert-buffer-substring buffer)
326       (save-buffer)
327       t)))
328
329 \f
330 ;;; Internal functions.
331
332 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
333 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
334 ;; delimeter line.
335 (defun nnmbox-delete-mail (&optional force leave-delim)
336   ;; Delete the current X-Gnus-Newsgroup line.
337   (or force
338       (delete-region
339        (progn (beginning-of-line) (point))
340        (progn (forward-line 1) (point))))
341   ;; Beginning of the article.
342   (save-excursion
343     (save-restriction
344       (narrow-to-region
345        (save-excursion
346          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
347          (if leave-delim (progn (forward-line 1) (point))
348            (match-beginning 0)))
349        (progn
350          (forward-line 1)
351          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
352                                      nil t)
353                   (if (and (not (bobp)) leave-delim)
354                       (progn (forward-line -2) (point))
355                     (match-beginning 0)))
356              (point-max))))
357       (goto-char (point-min))
358       ;; Only delete the article if no other groups owns it as well.
359       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
360           (delete-region (point-min) (point-max))))))
361
362 (defun nnmbox-possibly-change-newsgroup (newsgroup)
363   (if (not (get-buffer nnmbox-mbox-buffer))
364       (save-excursion
365         (set-buffer (setq nnmbox-mbox-buffer 
366                           (find-file-noselect nnmbox-mbox-file)))
367         (buffer-disable-undo (current-buffer))))
368   (if (not nnmbox-group-alist)
369       (progn
370         (nnmbox-request-list)
371         (setq nnmbox-group-alist (nnmail-get-active))))
372   (if newsgroup
373       (if (assoc newsgroup nnmbox-group-alist)
374           (setq nnmbox-current-group newsgroup))))
375
376 (defun nnmbox-article-string (article)
377   (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":" 
378           (int-to-string article) " "))
379
380 (defun nnmbox-save-mail (&optional group)
381   "Called narrowed to an article."
382   (let* ((nnmail-split-methods 
383           (if group (list (list group "")) nnmail-split-methods))
384          (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
385     (nnmail-insert-lines)
386     (nnmail-insert-xref group-art)
387     (nnmbox-insert-newsgroup-line group-art)
388     (run-hooks 'nnml-prepare-save-mail-hook)
389     group-art))
390
391 (defun nnmbox-insert-newsgroup-line (group-art)
392   (save-excursion
393     (goto-char (point-min))
394     (if (search-forward "\n\n" nil t)
395         (progn
396           (forward-char -1)
397           (while group-art
398             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
399                             (car (car group-art)) (cdr (car group-art))
400                             (current-time-string)))
401             (setq group-art (cdr group-art)))))
402     t))
403
404 (defun nnmbox-active-number (group)
405   "Find the next article number in GROUP."
406   (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
407     (setcdr active (1+ (cdr active)))
408     (cdr active)))
409
410 (defun nnmbox-read-mbox ()
411   (nnmbox-request-list)
412   (setq nnmbox-group-alist (nnmail-get-active))
413   (if (not (file-exists-p nnmbox-mbox-file))
414       (write-region 1 1 nnmbox-mbox-file t 'nomesg))
415   (if (and nnmbox-mbox-buffer
416            (get-buffer nnmbox-mbox-buffer)
417            (buffer-name nnmbox-mbox-buffer)
418            (save-excursion
419              (set-buffer nnmbox-mbox-buffer)
420              (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
421       ()
422     (save-excursion
423       (let ((delim (concat "^" rmail-unix-mail-delimiter))
424             start end)
425         (set-buffer (setq nnmbox-mbox-buffer 
426                           (find-file-noselect nnmbox-mbox-file)))
427         (buffer-disable-undo (current-buffer))
428         (goto-char (point-min))
429         (while (re-search-forward delim nil t)
430           (setq start (match-beginning 0))
431           (if (not (search-forward "\nX-Gnus-Newsgroup: " 
432                                    (save-excursion 
433                                      (setq end
434                                            (or
435                                             (and
436                                              (re-search-forward delim nil t)
437                                              (match-beginning 0))
438                                             (point-max))))
439                                    t))
440               (save-excursion
441                 (save-restriction
442                   (narrow-to-region start end)
443                   (nnmbox-save-mail))))
444           (goto-char end))))))
445
446 (defun nnmbox-get-new-mail (&optional group)
447   "Read new incoming mail."
448   (let ((spools (nnmail-get-spool-files group))
449         incoming incomings)
450     (nnmbox-read-mbox)
451     (if (or (not nnmbox-get-new-mail) (not nnmail-spool-file))
452         ()
453       ;; We go through all the existing spool files and split the
454       ;; mail from each.
455       (while spools
456         (and
457          (file-exists-p (car spools))
458          (> (nth 7 (file-attributes (car spools))) 0)
459          (progn
460            (and gnus-verbose-backends 
461                 (message "nnmbox: Reading incoming mail..."))
462            (setq incoming 
463                  (nnmail-move-inbox 
464                   (car spools) (concat nnmbox-mbox-file "-Incoming")))
465            (save-excursion
466              (let ((in-buf (nnmail-split-incoming 
467                             incoming 'nnmbox-save-mail t)))
468                (set-buffer nnmbox-mbox-buffer)
469                (goto-char (point-max))
470                (insert-buffer-substring in-buf)
471                (kill-buffer in-buf)))))
472         (setq spools (cdr spools)))
473       ;; If we did indeed read any incoming spools, we save all info. 
474       (and (buffer-modified-p nnmbox-mbox-buffer) 
475            (save-excursion
476              (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
477              (set-buffer nnmbox-mbox-buffer)
478              (save-buffer)))
479       (while incomings
480         ;; The following has been commented away, just to make sure
481         ;; that nobody ever loses any mail. If you feel safe that
482         ;; nnfolder will never do anything strange, just remove those
483         ;; two semicolons, and avoid having lots of "Incoming*"
484         ;; files. 
485         ;; (and (file-writable-p incoming) (delete-file incoming))
486         (setq incomings (cdr incomings))))))
487
488
489 (provide 'nnmbox)
490
491 ;;; nnmbox.el ends here