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