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