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