7ad97b05d21e26b300372ec59c8da76ec150c155
[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 (defun nnmbox-request-expire-articles 
241   (articles newsgroup &optional server force)
242   (nnmbox-possibly-change-newsgroup newsgroup)
243   (let* ((days (or (and nnmail-expiry-wait-function
244                         (funcall nnmail-expiry-wait-function newsgroup))
245                    nnmail-expiry-wait))
246          (is-old t)
247          rest)
248     (nnmail-activate 'nnmbox)
249
250     (save-excursion 
251       (set-buffer nnmbox-mbox-buffer)
252       (while (and articles is-old)
253         (goto-char (point-min))
254         (if (search-forward (nnmbox-article-string (car articles)) nil t)
255             (if (or force
256                     (setq is-old
257                           (> (nnmail-days-between 
258                               (current-time-string)
259                               (buffer-substring 
260                                (point) (progn (end-of-line) (point))))
261                              days)))
262                 (progn
263                   (and gnus-verbose-backends
264                        (message "Deleting article %d in %s..."
265                                 (car articles) newsgroup))
266                   (nnmbox-delete-mail))
267               (setq rest (cons (car articles) rest))))
268         (setq articles (cdr articles)))
269       (save-buffer)
270       ;; Find the lowest active article in this group.
271       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
272         (goto-char (point-min))
273         (while (and (not (search-forward
274                           (nnmbox-article-string (car active)) nil t))
275                     (<= (car active) (cdr active)))
276           (setcar active (1+ (car active)))
277           (goto-char (point-min))))
278       (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
279       (nconc rest articles))))
280
281 (defun nnmbox-request-move-article
282   (article group server accept-form &optional last)
283   (nnmbox-possibly-change-newsgroup group)
284   (let ((buf (get-buffer-create " *nnmbox move*"))
285         result)
286     (and 
287      (nnmbox-request-article article group server)
288      (save-excursion
289        (set-buffer buf)
290        (buffer-disable-undo (current-buffer))
291        (erase-buffer)
292        (insert-buffer-substring nntp-server-buffer)
293        (goto-char (point-min))
294        (while (re-search-forward 
295                "^X-Gnus-Newsgroup:" 
296                (save-excursion (search-forward "\n\n" nil t) (point)) t)
297          (delete-region (progn (beginning-of-line) (point))
298                         (progn (forward-line 1) (point))))
299        (setq result (eval accept-form))
300        (kill-buffer buf)
301        result)
302      (save-excursion
303        (set-buffer nnmbox-mbox-buffer)
304        (goto-char (point-min))
305        (if (search-forward (nnmbox-article-string article) nil t)
306            (nnmbox-delete-mail))
307        (and last (save-buffer))))
308     result))
309
310 (defun nnmbox-request-accept-article (group &optional last)
311   (let ((buf (current-buffer))
312         result)
313     (goto-char (point-min))
314     (if (looking-at "X-From-Line: ")
315         (replace-match "From ")
316       (insert "From nobody " (current-time-string) "\n"))
317     (and 
318      (nnmail-activate 'nnmbox)
319      (progn
320        (set-buffer buf)
321        (goto-char (point-min))
322        (search-forward "\n\n" nil t)
323        (forward-line -1)
324        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
325          (delete-region (point) (progn (forward-line 1) (point))))
326        (setq result (nnmbox-save-mail (and (stringp group) group))))
327      (save-excursion
328        (set-buffer nnmbox-mbox-buffer)
329        (insert-buffer-substring buf)
330        (and last (save-buffer))
331        result)
332      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
333     (car result)))
334
335 (defun nnmbox-request-replace-article (article group buffer)
336   (nnmbox-possibly-change-newsgroup group)
337   (save-excursion
338     (set-buffer nnmbox-mbox-buffer)
339     (goto-char (point-min))
340     (if (not (search-forward (nnmbox-article-string article) nil t))
341         nil
342       (nnmbox-delete-mail t t)
343       (insert-buffer-substring buffer)
344       (save-buffer)
345       t)))
346
347 \f
348 ;;; Internal functions.
349
350 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
351 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
352 ;; delimeter line.
353 (defun nnmbox-delete-mail (&optional force leave-delim)
354   ;; Delete the current X-Gnus-Newsgroup line.
355   (or force
356       (delete-region
357        (progn (beginning-of-line) (point))
358        (progn (forward-line 1) (point))))
359   ;; Beginning of the article.
360   (save-excursion
361     (save-restriction
362       (narrow-to-region
363        (save-excursion
364          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
365          (if leave-delim (progn (forward-line 1) (point))
366            (match-beginning 0)))
367        (progn
368          (forward-line 1)
369          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
370                                      nil t)
371                   (if (and (not (bobp)) leave-delim)
372                       (progn (forward-line -2) (point))
373                     (match-beginning 0)))
374              (point-max))))
375       (goto-char (point-min))
376       ;; Only delete the article if no other groups owns it as well.
377       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
378           (delete-region (point-min) (point-max))))))
379
380 (defun nnmbox-possibly-change-newsgroup (newsgroup)
381   (if (or (not nnmbox-mbox-buffer)
382           (not (buffer-name nnmbox-mbox-buffer)))
383       (save-excursion
384         (set-buffer (setq nnmbox-mbox-buffer 
385                           (nnheader-find-file-noselect
386                            nnmbox-mbox-file nil 'raw)))
387         (buffer-disable-undo (current-buffer))))
388   (if (not nnmbox-group-alist)
389       (nnmail-activate 'nnmbox))
390   (if newsgroup
391       (if (assoc newsgroup nnmbox-group-alist)
392           (setq nnmbox-current-group newsgroup))))
393
394 (defun nnmbox-article-string (article)
395   (if (numberp article)
396       (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":" 
397               (int-to-string article) " ")
398     (concat "\nMessage-ID: " article)))
399
400 (defun nnmbox-article-group-number ()
401   (save-excursion
402     (goto-char (point-min))
403     (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
404                             nil t)
405          (cons (buffer-substring (match-beginning 1) (match-end 1))
406                (string-to-int
407                 (buffer-substring (match-beginning 2) (match-end 2)))))))
408
409 (defun nnmbox-save-mail (&optional group)
410   "Called narrowed to an article."
411   (let* ((nnmail-split-methods 
412           (if group (list (list group "")) nnmail-split-methods))
413          (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
414     (nnmail-insert-lines)
415     (nnmail-insert-xref group-art)
416     (nnmbox-insert-newsgroup-line group-art)
417     (run-hooks 'nnml-prepare-save-mail-hook)
418     group-art))
419
420 (defun nnmbox-insert-newsgroup-line (group-art)
421   (save-excursion
422     (goto-char (point-min))
423     (if (search-forward "\n\n" nil t)
424         (progn
425           (forward-char -1)
426           (while group-art
427             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
428                             (car (car group-art)) (cdr (car group-art))
429                             (current-time-string)))
430             (setq group-art (cdr group-art)))))
431     t))
432
433 (defun nnmbox-active-number (group)
434   ;; Find the next article number in GROUP.
435   (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
436     (if active
437         (setcdr active (1+ (cdr active)))
438       ;; This group is new, so we create a new entry for it.
439       ;; This might be a bit naughty... creating groups on the drop of
440       ;; a hat, but I don't know...
441       (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
442                                      nnmbox-group-alist)))
443     (cdr active)))
444
445 (defun nnmbox-read-mbox ()
446   (nnmail-activate 'nnmbox)
447   (if (not (file-exists-p nnmbox-mbox-file))
448       (write-region 1 1 nnmbox-mbox-file t 'nomesg))
449   (if (and nnmbox-mbox-buffer
450            (buffer-name nnmbox-mbox-buffer)
451            (save-excursion
452              (set-buffer nnmbox-mbox-buffer)
453              (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
454       ()
455     (save-excursion
456       (let ((delim (concat "^" rmail-unix-mail-delimiter))
457             start end)
458         (set-buffer (setq nnmbox-mbox-buffer 
459                           (nnheader-find-file-noselect 
460                            nnmbox-mbox-file nil 'raw)))
461         (buffer-disable-undo (current-buffer))
462         (goto-char (point-min))
463         (while (re-search-forward delim nil t)
464           (setq start (match-beginning 0))
465           (if (not (search-forward "\nX-Gnus-Newsgroup: " 
466                                    (save-excursion 
467                                      (setq end
468                                            (or
469                                             (and
470                                              (re-search-forward delim nil t)
471                                              (match-beginning 0))
472                                             (point-max))))
473                                    t))
474               (save-excursion
475                 (save-restriction
476                   (narrow-to-region start end)
477                   (nnmbox-save-mail))))
478           (goto-char end))))))
479
480 (provide 'nnmbox)
481
482 ;;; nnmbox.el ends here