*** 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 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 fetch-old)
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   (setq nnmbox-current-server nil)
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   (save-excursion
162     (set-buffer nnmbox-mbox-buffer)
163     (goto-char (point-min))
164     (if (search-forward (nnmbox-article-string article) nil t)
165         (let (start stop)
166           (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
167           (setq start (point))
168           (forward-line 1)
169           (or (and (re-search-forward 
170                     (concat "^" rmail-unix-mail-delimiter) nil t)
171                    (forward-line -1))
172               (goto-char (point-max)))
173           (setq stop (point))
174           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
175             (set-buffer nntp-server-buffer)
176             (erase-buffer)
177             (insert-buffer-substring nnmbox-mbox-buffer start stop)
178             (goto-char (point-min))
179             (while (looking-at "From ")
180               (delete-char 5)
181               (insert "X-From-Line: ")
182               (forward-line 1))
183             (if (numberp article) 
184                 (cons nnmbox-current-group article)
185               (nnmbox-article-group-number)))))))
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           (save-excursion
193             (set-buffer nntp-server-buffer)
194             (erase-buffer)
195             (let ((active (assoc group nnmbox-group-alist)))
196               (insert (format "211 %d %d %d %s\n" 
197                               (1+ (- (cdr (car (cdr active)))
198                                      (car (car (cdr active)))))
199                               (car (car (cdr active)))
200                               (cdr (car (cdr active)))
201                               (car active))))
202             t)))))
203
204 (defun nnmbox-request-scan (&optional group server)
205   (nnmbox-read-mbox)
206   (nnmail-get-new-mail 
207    'nnmbox 
208    (lambda ()
209      (save-excursion
210        (set-buffer nnmbox-mbox-buffer)
211        (save-buffer)))
212    nnmbox-mbox-file group
213    (lambda ()
214      (save-excursion
215        (let ((in-buf (current-buffer)))
216          (set-buffer nnmbox-mbox-buffer)
217          (goto-char (point-max))
218          (insert-buffer-substring in-buf))))))
219
220 (defun nnmbox-close-group (group &optional server)
221   t)
222
223 (defun nnmbox-request-list (&optional server)
224   (save-excursion
225     (or (nnmail-find-file nnmbox-active-file)
226         (progn
227           (setq nnmbox-group-alist (nnmail-get-active))
228           (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
229           (nnmail-find-file nnmbox-active-file)))))
230
231 (defun nnmbox-request-newgroups (date &optional server)
232   (nnmbox-request-list server))
233
234 (defun nnmbox-request-list-newsgroups (&optional server)
235   (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
236   nil)
237
238 (defun nnmbox-request-post (&optional server)
239   (mail-send-and-exit nil))
240
241 (defun nnmbox-request-expire-articles 
242   (articles newsgroup &optional server force)
243   (nnmbox-possibly-change-newsgroup newsgroup)
244   (let* ((days (or (and nnmail-expiry-wait-function
245                         (funcall nnmail-expiry-wait-function newsgroup))
246                    nnmail-expiry-wait))
247          (is-old t)
248          rest)
249     (nnmail-activate 'nnmbox)
250
251     (save-excursion 
252       (set-buffer nnmbox-mbox-buffer)
253       (while (and articles is-old)
254         (goto-char (point-min))
255         (if (search-forward (nnmbox-article-string (car articles)) nil t)
256             (if (or force
257                     (setq is-old
258                           (> (nnmail-days-between 
259                               (current-time-string)
260                               (buffer-substring 
261                                (point) (progn (end-of-line) (point))))
262                              days)))
263                 (progn
264                   (and gnus-verbose-backends
265                        (message "Deleting article %d in %s..."
266                                 (car articles) newsgroup))
267                   (nnmbox-delete-mail))
268               (setq rest (cons (car articles) rest))))
269         (setq articles (cdr articles)))
270       (save-buffer)
271       ;; Find the lowest active article in this group.
272       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
273         (goto-char (point-min))
274         (while (and (not (search-forward
275                           (nnmbox-article-string (car active)) nil t))
276                     (<= (car active) (cdr active)))
277           (setcar active (1+ (car active)))
278           (goto-char (point-min))))
279       (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
280       (nconc rest articles))))
281
282 (defun nnmbox-request-move-article
283   (article group server accept-form &optional last)
284   (nnmbox-possibly-change-newsgroup group)
285   (let ((buf (get-buffer-create " *nnmbox move*"))
286         result)
287     (and 
288      (nnmbox-request-article article group server)
289      (save-excursion
290        (set-buffer buf)
291        (buffer-disable-undo (current-buffer))
292        (erase-buffer)
293        (insert-buffer-substring nntp-server-buffer)
294        (goto-char (point-min))
295        (while (re-search-forward 
296                "^X-Gnus-Newsgroup:" 
297                (save-excursion (search-forward "\n\n" nil t) (point)) t)
298          (delete-region (progn (beginning-of-line) (point))
299                         (progn (forward-line 1) (point))))
300        (setq result (eval accept-form))
301        (kill-buffer buf)
302        result)
303      (save-excursion
304        (set-buffer nnmbox-mbox-buffer)
305        (goto-char (point-min))
306        (if (search-forward (nnmbox-article-string article) nil t)
307            (nnmbox-delete-mail))
308        (and last (save-buffer))))
309     result))
310
311 (defun nnmbox-request-accept-article (group &optional last)
312   (let ((buf (current-buffer))
313         result)
314     (goto-char (point-min))
315     (if (looking-at "X-From-Line: ")
316         (replace-match "From ")
317       (insert "From nobody " (current-time-string) "\n"))
318     (and 
319      (nnmail-activate 'nnmbox)
320      (progn
321        (set-buffer buf)
322        (goto-char (point-min))
323        (search-forward "\n\n" nil t)
324        (forward-line -1)
325        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
326          (delete-region (point) (progn (forward-line 1) (point))))
327        (setq result (nnmbox-save-mail (and (stringp group) group))))
328      (save-excursion
329        (set-buffer nnmbox-mbox-buffer)
330        (insert-buffer-substring buf)
331        (and last (save-buffer))
332        result)
333      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
334     (car result)))
335
336 (defun nnmbox-request-replace-article (article group buffer)
337   (nnmbox-possibly-change-newsgroup group)
338   (save-excursion
339     (set-buffer nnmbox-mbox-buffer)
340     (goto-char (point-min))
341     (if (not (search-forward (nnmbox-article-string article) nil t))
342         nil
343       (nnmbox-delete-mail t t)
344       (insert-buffer-substring buffer)
345       (save-buffer)
346       t)))
347
348 (defun nnmbox-request-delete-group (group &optional force server)
349   (nnmbox-possibly-change-newsgroup group)
350   ;; Delete all articles in GROUP.
351   (if (not force)
352       ()                                ; Don't delete the articles.
353     (save-excursion
354       (set-buffer nnmbox-mbox-buffer)
355       (goto-char (point-min))
356       ;; Delete all articles in this group.
357       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
358             found)
359         (while (search-forward ident nil t)
360           (setq found t)
361           (nnmbox-delete-mail))
362         (and found (save-buffer)))))
363   ;; Remove the group from all structures.
364   (setq nnmbox-group-alist 
365         (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
366         nnmbox-current-group nil)
367   ;; Save the active file.
368   (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
369   t)
370
371 (defun nnmbox-request-rename-group (group new-name &optional server)
372   (nnmbox-possibly-change-newsgroup group)
373   (save-excursion
374     (set-buffer nnmbox-mbox-buffer)
375     (goto-char (point-min))
376     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
377           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
378           found)
379       (while (search-forward ident nil t)
380         (replace-match new-ident t t)
381         (setq found t))
382       (and found (save-buffer))))
383   (let ((entry (assoc group nnmbox-group-alist)))
384     (and entry (setcar entry new-name))
385     (setq nnmbox-current-group nil)
386     ;; Save the new group alist.
387     (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
388     t))
389
390 \f
391 ;;; Internal functions.
392
393 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
394 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
395 ;; delimeter line.
396 (defun nnmbox-delete-mail (&optional force leave-delim)
397   ;; Delete the current X-Gnus-Newsgroup line.
398   (or force
399       (delete-region
400        (progn (beginning-of-line) (point))
401        (progn (forward-line 1) (point))))
402   ;; Beginning of the article.
403   (save-excursion
404     (save-restriction
405       (narrow-to-region
406        (save-excursion
407          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
408          (if leave-delim (progn (forward-line 1) (point))
409            (match-beginning 0)))
410        (progn
411          (forward-line 1)
412          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
413                                      nil t)
414                   (if (and (not (bobp)) leave-delim)
415                       (progn (forward-line -2) (point))
416                     (match-beginning 0)))
417              (point-max))))
418       (goto-char (point-min))
419       ;; Only delete the article if no other groups owns it as well.
420       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
421           (delete-region (point-min) (point-max))))))
422
423 (defun nnmbox-possibly-change-newsgroup (newsgroup)
424   (if (or (not nnmbox-mbox-buffer)
425           (not (buffer-name nnmbox-mbox-buffer)))
426       (save-excursion
427         (set-buffer (setq nnmbox-mbox-buffer 
428                           (nnheader-find-file-noselect
429                            nnmbox-mbox-file nil 'raw)))
430         (buffer-disable-undo (current-buffer))))
431   (if (not nnmbox-group-alist)
432       (nnmail-activate 'nnmbox))
433   (if newsgroup
434       (if (assoc newsgroup nnmbox-group-alist)
435           (setq nnmbox-current-group newsgroup))))
436
437 (defun nnmbox-article-string (article)
438   (if (numberp article)
439       (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":" 
440               (int-to-string article) " ")
441     (concat "\nMessage-ID: " article)))
442
443 (defun nnmbox-article-group-number ()
444   (save-excursion
445     (goto-char (point-min))
446     (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
447                             nil t)
448          (cons (buffer-substring (match-beginning 1) (match-end 1))
449                (string-to-int
450                 (buffer-substring (match-beginning 2) (match-end 2)))))))
451
452 (defun nnmbox-save-mail (&optional group)
453   "Called narrowed to an article."
454   (let* ((nnmail-split-methods 
455           (if group (list (list group "")) nnmail-split-methods))
456          (group-art (nreverse (nnmail-article-group 'nnmbox-active-number)))
457          (delim (concat "^" rmail-unix-mail-delimiter)))
458     (goto-char (point-min))
459     ;; This might come from somewhere else.
460     (unless (looking-at delim)
461       (insert "From nobody " (current-time-string) "\n")
462       (goto-char (point-min)))
463     ;; Quote all "From " lines in the article.
464     (forward-line 1)
465     (while (re-search-forward delim nil t)
466       (beginning-of-line)
467       (insert "> "))
468     (nnmail-insert-lines)
469     (nnmail-insert-xref group-art)
470     (nnmbox-insert-newsgroup-line group-art)
471     (run-hooks 'nnmbox-prepare-save-mail-hook)
472     group-art))
473
474 (defun nnmbox-insert-newsgroup-line (group-art)
475   (save-excursion
476     (goto-char (point-min))
477     (if (search-forward "\n\n" nil t)
478         (progn
479           (forward-char -1)
480           (while group-art
481             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
482                             (car (car group-art)) (cdr (car group-art))
483                             (current-time-string)))
484             (setq group-art (cdr group-art)))))
485     t))
486
487 (defun nnmbox-active-number (group)
488   ;; Find the next article number in GROUP.
489   (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
490     (if active
491         (setcdr active (1+ (cdr active)))
492       ;; This group is new, so we create a new entry for it.
493       ;; This might be a bit naughty... creating groups on the drop of
494       ;; a hat, but I don't know...
495       (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
496                                      nnmbox-group-alist)))
497     (cdr active)))
498
499 (defun nnmbox-read-mbox ()
500   (nnmail-activate 'nnmbox)
501   (if (not (file-exists-p nnmbox-mbox-file))
502       (write-region 1 1 nnmbox-mbox-file t 'nomesg))
503   (if (and nnmbox-mbox-buffer
504            (buffer-name nnmbox-mbox-buffer)
505            (save-excursion
506              (set-buffer nnmbox-mbox-buffer)
507              (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
508       ()
509     (save-excursion
510       (let ((delim (concat "^" rmail-unix-mail-delimiter))
511             start end)
512         (set-buffer (setq nnmbox-mbox-buffer 
513                           (nnheader-find-file-noselect 
514                            nnmbox-mbox-file nil 'raw)))
515         (buffer-disable-undo (current-buffer))
516         (goto-char (point-min))
517         (while (re-search-forward delim nil t)
518           (setq start (match-beginning 0))
519           (if (not (search-forward "\nX-Gnus-Newsgroup: " 
520                                    (save-excursion 
521                                      (setq end
522                                            (or
523                                             (and
524                                              (re-search-forward delim nil t)
525                                              (match-beginning 0))
526                                             (point-max))))
527                                    t))
528               (save-excursion
529                 (save-restriction
530                   (narrow-to-region start end)
531                   (nnmbox-save-mail))))
532           (goto-char end))))))
533
534 (provide 'nnmbox)
535
536 ;;; nnmbox.el ends here