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