c6e318f380b6317b3fa957bd8225fc8bf5e05b3a
[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 1.0"
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 (defvar nnmbox-active-timestamp nil)
61
62 \f
63
64 (defvar nnmbox-current-server nil)
65 (defvar nnmbox-server-alist nil)
66 (defvar nnmbox-server-variables 
67   (list
68    (list 'nnmbox-mbox-file nnmbox-mbox-file)
69    (list 'nnmbox-active-file nnmbox-active-file)
70    (list 'nnmbox-get-new-mail nnmbox-get-new-mail)
71    '(nnmbox-current-group nil)
72    '(nnmbox-status-string "")
73    '(nnmbox-group-alist nil)))
74
75 \f
76
77 ;;; Interface functions
78
79 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server)
80   (save-excursion
81     (set-buffer nntp-server-buffer)
82     (erase-buffer)
83     (let ((number (length sequence))
84           (count 0)
85           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               (insert-buffer-substring nnmbox-mbox-buffer start stop)
105               (goto-char (point-max))
106               (insert ".\n")))
107         (setq sequence (cdr sequence))
108         (setq count (1+ count))
109         (and (numberp nnmail-large-newsgroup)
110              (> number nnmail-large-newsgroup)
111              (zerop (% count 20))
112              gnus-verbose-backends
113              (message "nnmbox: Receiving headers... %d%%"
114                       (/ (* count 100) number))))
115
116       (and (numberp nnmail-large-newsgroup)
117            (> number nnmail-large-newsgroup)
118            gnus-verbose-backends
119            (message "nnmbox: Receiving headers...done"))
120
121       ;; Fold continuation lines.
122       (set-buffer nntp-server-buffer)
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        nnmbox-mbox-buffer
151        (buffer-name nnmbox-mbox-buffer)
152        nntp-server-buffer
153        (buffer-name nntp-server-buffer)))
154
155 (defun nnmbox-status-message (&optional server)
156   nnmbox-status-string)
157
158 (defun nnmbox-request-article (article &optional newsgroup server buffer)
159   (nnmbox-possibly-change-newsgroup newsgroup)
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             (if (numberp article) 
183                 (cons nnmbox-current-group article)
184               (nnmbox-article-group-number)))))))
185
186 (defun nnmbox-request-group (group &optional server dont-check)
187   (save-excursion
188     (if (nnmbox-possibly-change-newsgroup group)
189         (if dont-check
190             t
191           (save-excursion
192             (set-buffer nntp-server-buffer)
193             (erase-buffer)
194             (let ((active (assoc group nnmbox-group-alist)))
195               (insert (format "211 %d %d %d %s\n" 
196                               (1+ (- (cdr (car (cdr active)))
197                                      (car (car (cdr active)))))
198                               (car (car (cdr active)))
199                               (cdr (car (cdr active)))
200                               (car active))))
201             t)))))
202
203 (defun nnmbox-request-scan (&optional group server)
204   (nnmbox-read-mbox)
205   (nnmail-get-new-mail 
206    'nnmbox 
207    (lambda ()
208      (save-excursion
209        (set-buffer nnmbox-mbox-buffer)
210        (save-buffer)))
211    nnmbox-mbox-file group
212    (lambda ()
213      (save-excursion
214        (let ((in-buf (current-buffer)))
215          (set-buffer nnmbox-mbox-buffer)
216          (goto-char (point-max))
217          (insert-buffer-substring in-buf))))))
218
219 (defun nnmbox-close-group (group &optional server)
220   t)
221
222 (defun nnmbox-request-list (&optional server)
223   (save-excursion
224     (or (nnmail-find-file nnmbox-active-file)
225         (progn
226           (setq nnmbox-group-alist (nnmail-get-active))
227           (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
228           (nnmail-find-file nnmbox-active-file)))))
229
230 (defun nnmbox-request-newgroups (date &optional server)
231   (nnmbox-request-list server))
232
233 (defun nnmbox-request-list-newsgroups (&optional server)
234   (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
235   nil)
236
237 (defun nnmbox-request-post (&optional server)
238   (mail-send-and-exit nil))
239
240 (defalias 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
241
242 (defun nnmbox-request-expire-articles 
243   (articles newsgroup &optional server force)
244   (nnmbox-possibly-change-newsgroup newsgroup)
245   (let* ((days (or (and nnmail-expiry-wait-function
246                         (funcall nnmail-expiry-wait-function newsgroup))
247                    nnmail-expiry-wait))
248          (is-old t)
249          rest)
250     (nnmail-activate 'nnmbox)
251
252     (save-excursion 
253       (set-buffer nnmbox-mbox-buffer)
254       (while (and articles is-old)
255         (goto-char (point-min))
256         (if (search-forward (nnmbox-article-string (car articles)) nil t)
257             (if (or force
258                     (setq is-old
259                           (> (nnmail-days-between 
260                               (current-time-string)
261                               (buffer-substring 
262                                (point) (progn (end-of-line) (point))))
263                              days)))
264                 (progn
265                   (and gnus-verbose-backends
266                        (message "Deleting article %d in %s..."
267                                 (car articles) newsgroup))
268                   (nnmbox-delete-mail))
269               (setq rest (cons (car articles) rest))))
270         (setq articles (cdr articles)))
271       (save-buffer)
272       ;; Find the lowest active article in this group.
273       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
274         (goto-char (point-min))
275         (while (and (not (search-forward
276                           (nnmbox-article-string (car active)) nil t))
277                     (<= (car active) (cdr active)))
278           (setcar active (1+ (car active)))
279           (goto-char (point-min))))
280       (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
281       (nconc rest articles))))
282
283 (defun nnmbox-request-move-article
284   (article group server accept-form &optional last)
285   (nnmbox-possibly-change-newsgroup group)
286   (let ((buf (get-buffer-create " *nnmbox move*"))
287         result)
288     (and 
289      (nnmbox-request-article article group server)
290      (save-excursion
291        (set-buffer buf)
292        (buffer-disable-undo (current-buffer))
293        (erase-buffer)
294        (insert-buffer-substring nntp-server-buffer)
295        (goto-char (point-min))
296        (while (re-search-forward 
297                "^X-Gnus-Newsgroup:" 
298                (save-excursion (search-forward "\n\n" nil t) (point)) t)
299          (delete-region (progn (beginning-of-line) (point))
300                         (progn (forward-line 1) (point))))
301        (setq result (eval accept-form))
302        (kill-buffer buf)
303        result)
304      (save-excursion
305        (set-buffer nnmbox-mbox-buffer)
306        (goto-char (point-min))
307        (if (search-forward (nnmbox-article-string article) nil t)
308            (nnmbox-delete-mail))
309        (and last (save-buffer))))
310     result))
311
312 (defun nnmbox-request-accept-article (group &optional last)
313   (let ((buf (current-buffer))
314         result)
315     (goto-char (point-min))
316     (if (looking-at "X-From-Line: ")
317         (replace-match "From ")
318       (insert "From nobody " (current-time-string) "\n"))
319     (and 
320      (nnmail-activate 'nnmbox)
321      (progn
322        (set-buffer buf)
323        (goto-char (point-min))
324        (search-forward "\n\n" nil t)
325        (forward-line -1)
326        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
327          (delete-region (point) (progn (forward-line 1) (point))))
328        (setq result (nnmbox-save-mail (and (stringp group) group))))
329      (save-excursion
330        (set-buffer nnmbox-mbox-buffer)
331        (insert-buffer-substring buf)
332        (and last (save-buffer))
333        result)
334      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
335     (car result)))
336
337 (defun nnmbox-request-replace-article (article group buffer)
338   (nnmbox-possibly-change-newsgroup group)
339   (save-excursion
340     (set-buffer nnmbox-mbox-buffer)
341     (goto-char (point-min))
342     (if (not (search-forward (nnmbox-article-string article) nil t))
343         nil
344       (nnmbox-delete-mail t t)
345       (insert-buffer-substring buffer)
346       (save-buffer)
347       t)))
348
349 \f
350 ;;; Internal functions.
351
352 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
353 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
354 ;; delimeter line.
355 (defun nnmbox-delete-mail (&optional force leave-delim)
356   ;; Delete the current X-Gnus-Newsgroup line.
357   (or force
358       (delete-region
359        (progn (beginning-of-line) (point))
360        (progn (forward-line 1) (point))))
361   ;; Beginning of the article.
362   (save-excursion
363     (save-restriction
364       (narrow-to-region
365        (save-excursion
366          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
367          (if leave-delim (progn (forward-line 1) (point))
368            (match-beginning 0)))
369        (progn
370          (forward-line 1)
371          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
372                                      nil t)
373                   (if (and (not (bobp)) leave-delim)
374                       (progn (forward-line -2) (point))
375                     (match-beginning 0)))
376              (point-max))))
377       (goto-char (point-min))
378       ;; Only delete the article if no other groups owns it as well.
379       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
380           (delete-region (point-min) (point-max))))))
381
382 (defun nnmbox-possibly-change-newsgroup (newsgroup)
383   (if (or (not nnmbox-mbox-buffer)
384           (not (buffer-name nnmbox-mbox-buffer)))
385       (save-excursion
386         (set-buffer (setq nnmbox-mbox-buffer 
387                           (nnheader-find-file-noselect
388                            nnmbox-mbox-file nil 'raw)))
389         (buffer-disable-undo (current-buffer))))
390   (if (not nnmbox-group-alist)
391       (nnmail-activate 'nnmbox))
392   (if newsgroup
393       (if (assoc newsgroup nnmbox-group-alist)
394           (setq nnmbox-current-group newsgroup))))
395
396 (defun nnmbox-article-string (article)
397   (if (numberp article)
398       (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":" 
399               (int-to-string article) " ")
400     (concat "\nMessage-ID: " article)))
401
402 (defun nnmbox-article-group-number ()
403   (save-excursion
404     (goto-char (point-min))
405     (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
406                             nil t)
407          (cons (buffer-substring (match-beginning 1) (match-end 1))
408                (string-to-int
409                 (buffer-substring (match-beginning 2) (match-end 2)))))))
410
411 (defun nnmbox-save-mail (&optional group)
412   "Called narrowed to an article."
413   (let* ((nnmail-split-methods 
414           (if group (list (list group "")) nnmail-split-methods))
415          (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
416     (nnmail-insert-lines)
417     (nnmail-insert-xref group-art)
418     (nnmbox-insert-newsgroup-line group-art)
419     (run-hooks 'nnml-prepare-save-mail-hook)
420     group-art))
421
422 (defun nnmbox-insert-newsgroup-line (group-art)
423   (save-excursion
424     (goto-char (point-min))
425     (if (search-forward "\n\n" nil t)
426         (progn
427           (forward-char -1)
428           (while group-art
429             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
430                             (car (car group-art)) (cdr (car group-art))
431                             (current-time-string)))
432             (setq group-art (cdr group-art)))))
433     t))
434
435 (defun nnmbox-active-number (group)
436   ;; Find the next article number in GROUP.
437   (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
438     (if active
439         (setcdr active (1+ (cdr active)))
440       ;; This group is new, so we create a new entry for it.
441       ;; This might be a bit naughty... creating groups on the drop of
442       ;; a hat, but I don't know...
443       (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
444                                      nnmbox-group-alist)))
445     (cdr active)))
446
447 (defun nnmbox-read-mbox ()
448   (nnmail-activate 'nnmbox)
449   (if (not (file-exists-p nnmbox-mbox-file))
450       (write-region 1 1 nnmbox-mbox-file t 'nomesg))
451   (if (and nnmbox-mbox-buffer
452            (buffer-name nnmbox-mbox-buffer)
453            (save-excursion
454              (set-buffer nnmbox-mbox-buffer)
455              (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
456       ()
457     (save-excursion
458       (let ((delim (concat "^" rmail-unix-mail-delimiter))
459             start end)
460         (set-buffer (setq nnmbox-mbox-buffer 
461                           (nnheader-find-file-noselect 
462                            nnmbox-mbox-file nil 'raw)))
463         (buffer-disable-undo (current-buffer))
464         (goto-char (point-min))
465         (while (re-search-forward delim nil t)
466           (setq start (match-beginning 0))
467           (if (not (search-forward "\nX-Gnus-Newsgroup: " 
468                                    (save-excursion 
469                                      (setq end
470                                            (or
471                                             (and
472                                              (re-search-forward delim nil t)
473                                              (match-beginning 0))
474                                             (point-max))))
475                                    t))
476               (save-excursion
477                 (save-restriction
478                   (narrow-to-region start end)
479                   (nnmbox-save-mail))))
480           (goto-char end))))))
481
482 (provide 'nnmbox)
483
484 ;;; nnmbox.el ends here