*** empty log message ***
[gnus] / lisp / nnmbox.el
1 ;;; nnmbox.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995,96 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; For an overview of what the interface functions do, please see the
28 ;; Gnus sources.  
29
30 ;;; Code:
31
32 (require 'nnheader)
33 (require 'rmail)
34 (require 'nnmail)
35 (eval-when-compile (require 'cl))
36
37 (defvar nnmbox-mbox-file (expand-file-name "~/mbox")
38   "The name of the mail box file in the user's home directory.")
39
40 (defvar nnmbox-active-file (expand-file-name "~/.mbox-active")
41   "The name of the active file for the mail box.")
42
43 (defvar nnmbox-get-new-mail t
44   "If non-nil, nnmbox will check the incoming mail file and split the mail.")
45
46 (defvar nnmbox-prepare-save-mail-hook nil
47   "Hook run narrowed to an article before saving.")
48
49 \f
50
51 (defconst nnmbox-version "nnmbox 1.0"
52   "nnmbox version.")
53
54 (defvar nnmbox-current-group nil
55   "Current nnmbox news group directory.")
56
57 (defconst nnmbox-mbox-buffer nil)
58
59 (defvar nnmbox-status-string "")
60
61 (defvar nnmbox-group-alist nil)
62 (defvar nnmbox-active-timestamp nil)
63
64 \f
65
66 (defvar nnmbox-current-server nil)
67 (defvar nnmbox-server-alist nil)
68 (defvar nnmbox-server-variables 
69   `((nnmbox-mbox-file ,nnmbox-mbox-file)
70     (nnmbox-active-file ,nnmbox-active-file)
71     (nnmbox-get-new-mail ,nnmbox-get-new-mail)
72     (nnmbox-current-group nil)
73     (nnmbox-status-string "")
74     (nnmbox-group-alist nil)))
75
76 \f
77
78 ;;; Interface functions
79
80 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
81   (save-excursion
82     (set-buffer nntp-server-buffer)
83     (erase-buffer)
84     (let ((number (length sequence))
85           (count 0)
86           article art-string start stop)
87       (nnmbox-possibly-change-newsgroup newsgroup)
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              (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
114                                (/ (* count 100) number))))
115
116       (and (numberp nnmail-large-newsgroup)
117            (> number nnmail-large-newsgroup)
118            (nnheader-message 5 "nnmbox: Receiving headers...done"))
119
120       (set-buffer nntp-server-buffer)
121       (nnheader-fold-continuation-lines)
122       'headers)))
123
124 (defun nnmbox-open-server (server &optional defs)
125   (nnheader-change-server 'nnmbox server defs)
126   (cond 
127    ((not (file-exists-p nnmbox-mbox-file))
128     (nnmbox-close-server)
129     (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
130    ((file-directory-p nnmbox-mbox-file)
131     (nnmbox-close-server)
132     (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
133    (t
134     (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
135                      nnmbox-mbox-file)
136     t)))
137
138 (defun nnmbox-close-server (&optional server)
139   (when (and nnmbox-mbox-buffer
140              (buffer-name nnmbox-mbox-buffer))
141     (kill-buffer nnmbox-mbox-buffer))
142   (setq nnmbox-current-server nil)
143   t)
144
145 (defun nnmbox-server-opened (&optional server)
146   (and (equal server nnmbox-current-server)
147        nnmbox-mbox-buffer
148        (buffer-name nnmbox-mbox-buffer)
149        nntp-server-buffer
150        (buffer-name nntp-server-buffer)))
151
152 (defun nnmbox-status-message (&optional server)
153   nnmbox-status-string)
154
155 (defun nnmbox-request-article (article &optional newsgroup server buffer)
156   (nnmbox-possibly-change-newsgroup newsgroup)
157   (save-excursion
158     (set-buffer nnmbox-mbox-buffer)
159     (goto-char (point-min))
160     (if (search-forward (nnmbox-article-string article) nil t)
161         (let (start stop)
162           (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
163           (setq start (point))
164           (forward-line 1)
165           (or (and (re-search-forward 
166                     (concat "^" rmail-unix-mail-delimiter) nil t)
167                    (forward-line -1))
168               (goto-char (point-max)))
169           (setq stop (point))
170           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
171             (set-buffer nntp-server-buffer)
172             (erase-buffer)
173             (insert-buffer-substring nnmbox-mbox-buffer start stop)
174             (goto-char (point-min))
175             (while (looking-at "From ")
176               (delete-char 5)
177               (insert "X-From-Line: ")
178               (forward-line 1))
179             (if (numberp article) 
180                 (cons nnmbox-current-group article)
181               (nnmbox-article-group-number)))))))
182
183 (defun nnmbox-request-group (group &optional server dont-check)
184   (let ((active (cadr (assoc group nnmbox-group-alist))))
185     (cond 
186      ((null active)
187       (nnheader-report 'nnmbox "No such group: %s" group))
188      ((null (nnmbox-possibly-change-newsgroup group))
189       (nnheader-report 'nnmbox "No such group: %s" group))
190      (dont-check
191       (nnheader-report 'nnmbox "Selected group %s" group)
192       (nnheader-insert ""))
193      (t
194       (nnheader-report 'nnmbox "Selected group %s" group)
195       (nnheader-insert "211 %d %d %d %s\n" 
196                        (1+ (- (cdr active) (car active)))
197                        (car active) (cdr active) group)
198       t))))
199
200 (defun nnmbox-request-scan (&optional group server)
201   (nnmbox-read-mbox)
202   (nnmail-get-new-mail 
203    'nnmbox 
204    (lambda ()
205      (save-excursion
206        (set-buffer nnmbox-mbox-buffer)
207        (save-buffer)))
208    nnmbox-mbox-file group
209    (lambda ()
210      (save-excursion
211        (let ((in-buf (current-buffer)))
212          (set-buffer nnmbox-mbox-buffer)
213          (goto-char (point-max))
214          (insert-buffer-substring in-buf))))))
215
216 (defun nnmbox-close-group (group &optional server)
217   t)
218
219 (defun nnmbox-request-list (&optional server)
220   (save-excursion
221     (or (nnmail-find-file nnmbox-active-file)
222         (progn
223           (setq nnmbox-group-alist (nnmail-get-active))
224           (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
225           (nnmail-find-file nnmbox-active-file)))))
226
227 (defun nnmbox-request-newgroups (date &optional server)
228   (nnmbox-request-list server))
229
230 (defun nnmbox-request-list-newsgroups (&optional server)
231   (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
232
233 (defun nnmbox-request-expire-articles 
234   (articles newsgroup &optional server force)
235   (nnmbox-possibly-change-newsgroup newsgroup)
236   (let* ((is-old t)
237          rest)
238     (nnmail-activate 'nnmbox)
239
240     (save-excursion 
241       (set-buffer nnmbox-mbox-buffer)
242       (while (and articles is-old)
243         (goto-char (point-min))
244         (if (search-forward (nnmbox-article-string (car articles)) nil t)
245             (if (setq is-old
246                       (nnmail-expired-article-p
247                        newsgroup
248                        (buffer-substring 
249                         (point) (progn (end-of-line) (point))) force))
250                 (progn
251                   (nnheader-message 5 "Deleting article %d in %s..."
252                                     (car articles) newsgroup)
253                   (nnmbox-delete-mail))
254               (setq rest (cons (car articles) rest))))
255         (setq articles (cdr articles)))
256       (save-buffer)
257       ;; Find the lowest active article in this group.
258       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
259         (goto-char (point-min))
260         (while (and (not (search-forward
261                           (nnmbox-article-string (car active)) nil t))
262                     (<= (car active) (cdr active)))
263           (setcar active (1+ (car active)))
264           (goto-char (point-min))))
265       (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
266       (nconc rest articles))))
267
268 (defun nnmbox-request-move-article
269   (article group server accept-form &optional last)
270   (nnmbox-possibly-change-newsgroup group)
271   (let ((buf (get-buffer-create " *nnmbox move*"))
272         result)
273     (and 
274      (nnmbox-request-article article group server)
275      (save-excursion
276        (set-buffer buf)
277        (buffer-disable-undo (current-buffer))
278        (erase-buffer)
279        (insert-buffer-substring nntp-server-buffer)
280        (goto-char (point-min))
281        (while (re-search-forward 
282                "^X-Gnus-Newsgroup:" 
283                (save-excursion (search-forward "\n\n" nil t) (point)) t)
284          (delete-region (progn (beginning-of-line) (point))
285                         (progn (forward-line 1) (point))))
286        (setq result (eval accept-form))
287        (kill-buffer buf)
288        result)
289      (save-excursion
290        (set-buffer nnmbox-mbox-buffer)
291        (goto-char (point-min))
292        (if (search-forward (nnmbox-article-string article) nil t)
293            (nnmbox-delete-mail))
294        (and last (save-buffer))))
295     result))
296
297 (defun nnmbox-request-accept-article (group &optional last)
298   (let ((buf (current-buffer))
299         result)
300     (goto-char (point-min))
301     (if (looking-at "X-From-Line: ")
302         (replace-match "From ")
303       (insert "From nobody " (current-time-string) "\n"))
304     (and 
305      (nnmail-activate 'nnmbox)
306      (progn
307        (set-buffer buf)
308        (goto-char (point-min))
309        (search-forward "\n\n" nil t)
310        (forward-line -1)
311        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
312          (delete-region (point) (progn (forward-line 1) (point))))
313        (setq result (nnmbox-save-mail (and (stringp group) group))))
314      (save-excursion
315        (set-buffer nnmbox-mbox-buffer)
316        (insert-buffer-substring buf)
317        (and last (save-buffer))
318        result)
319      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
320     (car result)))
321
322 (defun nnmbox-request-replace-article (article group buffer)
323   (nnmbox-possibly-change-newsgroup group)
324   (save-excursion
325     (set-buffer nnmbox-mbox-buffer)
326     (goto-char (point-min))
327     (if (not (search-forward (nnmbox-article-string article) nil t))
328         nil
329       (nnmbox-delete-mail t t)
330       (insert-buffer-substring buffer)
331       (save-buffer)
332       t)))
333
334 (defun nnmbox-request-delete-group (group &optional force server)
335   (nnmbox-possibly-change-newsgroup group)
336   ;; Delete all articles in GROUP.
337   (if (not force)
338       ()                                ; Don't delete the articles.
339     (save-excursion
340       (set-buffer nnmbox-mbox-buffer)
341       (goto-char (point-min))
342       ;; Delete all articles in this group.
343       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
344             found)
345         (while (search-forward ident nil t)
346           (setq found t)
347           (nnmbox-delete-mail))
348         (and found (save-buffer)))))
349   ;; Remove the group from all structures.
350   (setq nnmbox-group-alist 
351         (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
352         nnmbox-current-group nil)
353   ;; Save the active file.
354   (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
355   t)
356
357 (defun nnmbox-request-rename-group (group new-name &optional server)
358   (nnmbox-possibly-change-newsgroup group)
359   (save-excursion
360     (set-buffer nnmbox-mbox-buffer)
361     (goto-char (point-min))
362     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
363           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
364           found)
365       (while (search-forward ident nil t)
366         (replace-match new-ident t t)
367         (setq found t))
368       (and found (save-buffer))))
369   (let ((entry (assoc group nnmbox-group-alist)))
370     (and entry (setcar entry new-name))
371     (setq nnmbox-current-group nil)
372     ;; Save the new group alist.
373     (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
374     t))
375
376 \f
377 ;;; Internal functions.
378
379 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
380 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
381 ;; delimiter line.
382 (defun nnmbox-delete-mail (&optional force leave-delim)
383   ;; Delete the current X-Gnus-Newsgroup line.
384   (or force
385       (delete-region
386        (progn (beginning-of-line) (point))
387        (progn (forward-line 1) (point))))
388   ;; Beginning of the article.
389   (save-excursion
390     (save-restriction
391       (narrow-to-region
392        (save-excursion
393          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
394          (if leave-delim (progn (forward-line 1) (point))
395            (match-beginning 0)))
396        (progn
397          (forward-line 1)
398          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
399                                      nil t)
400                   (if (and (not (bobp)) leave-delim)
401                       (progn (forward-line -2) (point))
402                     (match-beginning 0)))
403              (point-max))))
404       (goto-char (point-min))
405       ;; Only delete the article if no other groups owns it as well.
406       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
407           (delete-region (point-min) (point-max))))))
408
409 (defun nnmbox-possibly-change-newsgroup (newsgroup)
410   (if (or (not nnmbox-mbox-buffer)
411           (not (buffer-name nnmbox-mbox-buffer)))
412       (save-excursion
413         (set-buffer (setq nnmbox-mbox-buffer 
414                           (nnheader-find-file-noselect
415                            nnmbox-mbox-file nil 'raw)))
416         (buffer-disable-undo (current-buffer))))
417   (if (not nnmbox-group-alist)
418       (nnmail-activate 'nnmbox))
419   (if newsgroup
420       (if (assoc newsgroup nnmbox-group-alist)
421           (setq nnmbox-current-group newsgroup))
422     t))
423
424 (defun nnmbox-article-string (article)
425   (if (numberp article)
426       (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":" 
427               (int-to-string article) " ")
428     (concat "\nMessage-ID: " article)))
429
430 (defun nnmbox-article-group-number ()
431   (save-excursion
432     (goto-char (point-min))
433     (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
434                             nil t)
435          (cons (buffer-substring (match-beginning 1) (match-end 1))
436                (string-to-int
437                 (buffer-substring (match-beginning 2) (match-end 2)))))))
438
439 (defun nnmbox-save-mail (&optional group)
440   "Called narrowed to an article."
441   (let* ((nnmail-split-methods 
442           (if group (list (list group "")) nnmail-split-methods))
443          (group-art (nreverse (nnmail-article-group 'nnmbox-active-number)))
444          (delim (concat "^" rmail-unix-mail-delimiter)))
445     (goto-char (point-min))
446     ;; This might come from somewhere else.
447     (unless (looking-at delim)
448       (insert "From nobody " (current-time-string) "\n")
449       (goto-char (point-min)))
450     ;; Quote all "From " lines in the article.
451     (forward-line 1)
452     (while (re-search-forward delim nil t)
453       (beginning-of-line)
454       (insert "> "))
455     (nnmail-insert-lines)
456     (nnmail-insert-xref group-art)
457     (nnmbox-insert-newsgroup-line group-art)
458     (run-hooks 'nnmbox-prepare-save-mail-hook)
459     group-art))
460
461 (defun nnmbox-insert-newsgroup-line (group-art)
462   (save-excursion
463     (goto-char (point-min))
464     (if (search-forward "\n\n" nil t)
465         (progn
466           (forward-char -1)
467           (while group-art
468             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
469                             (car (car group-art)) (cdr (car group-art))
470                             (current-time-string)))
471             (setq group-art (cdr group-art)))))
472     t))
473
474 (defun nnmbox-active-number (group)
475   ;; Find the next article number in GROUP.
476   (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
477     (if active
478         (setcdr active (1+ (cdr active)))
479       ;; This group is new, so we create a new entry for it.
480       ;; This might be a bit naughty... creating groups on the drop of
481       ;; a hat, but I don't know...
482       (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
483                                      nnmbox-group-alist)))
484     (cdr active)))
485
486 (defun nnmbox-read-mbox ()
487   (nnmail-activate 'nnmbox)
488   (if (not (file-exists-p nnmbox-mbox-file))
489       (write-region 1 1 nnmbox-mbox-file t 'nomesg))
490   (if (and nnmbox-mbox-buffer
491            (buffer-name nnmbox-mbox-buffer)
492            (save-excursion
493              (set-buffer nnmbox-mbox-buffer)
494              (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
495       ()
496     (save-excursion
497       (let ((delim (concat "^" rmail-unix-mail-delimiter))
498             start end)
499         (set-buffer (setq nnmbox-mbox-buffer 
500                           (nnheader-find-file-noselect
501                            nnmbox-mbox-file nil 'raw)))
502         (buffer-disable-undo (current-buffer))
503         (goto-char (point-min))
504         (while (re-search-forward delim nil t)
505           (setq start (match-beginning 0))
506           (if (not (search-forward "\nX-Gnus-Newsgroup: " 
507                                    (save-excursion 
508                                      (setq end
509                                            (or
510                                             (and
511                                              (re-search-forward delim nil t)
512                                              (match-beginning 0))
513                                             (point-max))))
514                                    t))
515               (save-excursion
516                 (save-restriction
517                   (narrow-to-region start end)
518                   (nnmbox-save-mail))))
519           (goto-char end))))))
520
521 (provide 'nnmbox)
522
523 ;;; nnmbox.el ends here