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