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