*** empty log message ***
[gnus] / lisp / nnmbox.el
1 ;;; nnmbox.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995,96,97 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 'message)
34 (require 'nnmail)
35 (require 'nnoo)
36 (eval-when-compile (require 'cl))
37
38 (nnoo-declare nnmbox)
39
40 (defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
41   "The name of the mail box file in the user's home directory.")
42
43 (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
44   "The name of the active file for the mail box.")
45
46 (defvoo nnmbox-get-new-mail t
47   "If non-nil, nnmbox will check the incoming mail file and split the mail.")
48
49 (defvoo nnmbox-prepare-save-mail-hook nil
50   "Hook run narrowed to an article before saving.")
51
52 \f
53
54 (defconst nnmbox-version "nnmbox 1.0"
55   "nnmbox version.")
56
57 (defvoo nnmbox-current-group nil
58   "Current nnmbox news group directory.")
59
60 (defconst nnmbox-mbox-buffer nil)
61
62 (defvoo nnmbox-status-string "")
63
64 (defvoo nnmbox-group-alist nil)
65 (defvoo nnmbox-active-timestamp nil)
66
67 \f
68
69 ;;; Interface functions
70
71 (nnoo-define-basics nnmbox)
72
73 (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
74   (save-excursion
75     (set-buffer nntp-server-buffer)
76     (erase-buffer)
77     (let ((number (length sequence))
78           (count 0)
79           article art-string start stop)
80       (nnmbox-possibly-change-newsgroup newsgroup server)
81       (while sequence
82         (setq article (car sequence))
83         (setq art-string (nnmbox-article-string article))
84         (set-buffer nnmbox-mbox-buffer)
85         (when (or (search-forward art-string nil t)
86                   (progn (goto-char (point-min))
87                          (search-forward art-string nil t)))
88           (setq start 
89                 (save-excursion
90                   (re-search-backward 
91                    (concat "^" message-unix-mail-delimiter) nil t)
92                   (point)))
93           (search-forward "\n\n" nil t)
94           (setq stop (1- (point)))
95           (set-buffer nntp-server-buffer)
96           (insert (format "221 %d Article retrieved.\n" article))
97           (insert-buffer-substring nnmbox-mbox-buffer start stop)
98           (goto-char (point-max))
99           (insert ".\n"))
100         (setq sequence (cdr sequence))
101         (setq count (1+ count))
102         (and (numberp nnmail-large-newsgroup)
103              (> number nnmail-large-newsgroup)
104              (zerop (% count 20))
105              (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
106                                (/ (* count 100) number))))
107
108       (and (numberp nnmail-large-newsgroup)
109            (> number nnmail-large-newsgroup)
110            (nnheader-message 5 "nnmbox: Receiving headers...done"))
111
112       (set-buffer nntp-server-buffer)
113       (nnheader-fold-continuation-lines)
114       'headers)))
115
116 (deffoo nnmbox-open-server (server &optional defs)
117   (nnoo-change-server 'nnmbox server defs)
118   (nnmbox-create-mbox)
119   (cond 
120    ((not (file-exists-p nnmbox-mbox-file))
121     (nnmbox-close-server)
122     (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
123    ((file-directory-p nnmbox-mbox-file)
124     (nnmbox-close-server)
125     (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
126    (t
127     (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
128                      nnmbox-mbox-file)
129     t)))
130
131 (deffoo nnmbox-close-server (&optional server)
132   (when (and nnmbox-mbox-buffer
133              (buffer-name nnmbox-mbox-buffer))
134     (kill-buffer nnmbox-mbox-buffer))
135   (nnoo-close-server 'nnmbox server)
136   t)
137
138 (deffoo nnmbox-server-opened (&optional server)
139   (and (nnoo-current-server-p 'nnmbox server)
140        nnmbox-mbox-buffer
141        (buffer-name nnmbox-mbox-buffer)
142        nntp-server-buffer
143        (buffer-name nntp-server-buffer)))
144
145 (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
146   (nnmbox-possibly-change-newsgroup newsgroup server)
147   (save-excursion
148     (set-buffer nnmbox-mbox-buffer)
149     (goto-char (point-min))
150     (when (search-forward (nnmbox-article-string article) nil t)
151       (let (start stop)
152         (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
153         (setq start (point))
154         (forward-line 1)
155         (or (and (re-search-forward 
156                   (concat "^" message-unix-mail-delimiter) nil t)
157                  (forward-line -1))
158             (goto-char (point-max)))
159         (setq stop (point))
160         (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
161           (set-buffer nntp-server-buffer)
162           (erase-buffer)
163           (insert-buffer-substring nnmbox-mbox-buffer start stop)
164           (goto-char (point-min))
165           (while (looking-at "From ")
166             (delete-char 5)
167             (insert "X-From-Line: ")
168             (forward-line 1))
169           (if (numberp article)
170               (cons nnmbox-current-group article)
171             (nnmbox-article-group-number)))))))
172
173 (deffoo nnmbox-request-group (group &optional server dont-check)
174   (let ((active (cadr (assoc group nnmbox-group-alist))))
175     (cond 
176      ((or (null active)
177           (null (nnmbox-possibly-change-newsgroup group server)))
178       (nnheader-report 'nnmbox "No such group: %s" group))
179      (dont-check
180       (nnheader-report 'nnmbox "Selected group %s" group)
181       (nnheader-insert ""))
182      (t
183       (nnheader-report 'nnmbox "Selected group %s" group)
184       (nnheader-insert "211 %d %d %d %s\n" 
185                        (1+ (- (cdr active) (car active)))
186                        (car active) (cdr active) group)))))
187
188 (deffoo nnmbox-request-scan (&optional group server)
189   (nnmbox-possibly-change-newsgroup group server)
190   (nnmbox-read-mbox)
191   (nnmail-get-new-mail 
192    'nnmbox 
193    (lambda ()
194      (save-excursion
195        (set-buffer nnmbox-mbox-buffer)
196        (save-buffer)))
197    nnmbox-mbox-file group
198    (lambda ()
199      (save-excursion
200        (let ((in-buf (current-buffer)))
201          (set-buffer nnmbox-mbox-buffer)
202          (goto-char (point-max))
203          (insert-buffer-substring in-buf)))
204      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))))
205
206 (deffoo nnmbox-close-group (group &optional server)
207   t)
208
209 (deffoo nnmbox-request-list (&optional server)
210   (save-excursion
211     (nnmail-find-file nnmbox-active-file)
212     (setq nnmbox-group-alist (nnmail-get-active))
213     t))
214
215 (deffoo nnmbox-request-newgroups (date &optional server)
216   (nnmbox-request-list server))
217
218 (deffoo nnmbox-request-list-newsgroups (&optional server)
219   (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
220
221 (deffoo nnmbox-request-expire-articles 
222   (articles newsgroup &optional server force)
223   (nnmbox-possibly-change-newsgroup newsgroup server)
224   (let* ((is-old t)
225          rest)
226     (nnmail-activate 'nnmbox)
227
228     (save-excursion 
229       (set-buffer nnmbox-mbox-buffer)
230       (while (and articles is-old)
231         (goto-char (point-min))
232         (when (search-forward (nnmbox-article-string (car articles)) nil t)
233           (if (setq is-old
234                     (nnmail-expired-article-p
235                      newsgroup
236                      (buffer-substring 
237                       (point) (progn (end-of-line) (point))) force))
238               (progn
239                 (nnheader-message 5 "Deleting article %d in %s..."
240                                   (car articles) newsgroup)
241                 (nnmbox-delete-mail))
242             (push (car articles) rest)))
243         (setq articles (cdr articles)))
244       (save-buffer)
245       ;; Find the lowest active article in this group.
246       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
247         (goto-char (point-min))
248         (while (and (not (search-forward
249                           (nnmbox-article-string (car active)) nil t))
250                     (<= (car active) (cdr active)))
251           (setcar active (1+ (car active)))
252           (goto-char (point-min))))
253       (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
254       (nconc rest articles))))
255
256 (deffoo nnmbox-request-move-article
257   (article group server accept-form &optional last)
258   (let ((buf (get-buffer-create " *nnmbox move*"))
259         result)
260     (and 
261      (nnmbox-request-article article group server)
262      (save-excursion
263        (set-buffer buf)
264        (buffer-disable-undo (current-buffer))
265        (erase-buffer)
266        (insert-buffer-substring nntp-server-buffer)
267        (goto-char (point-min))
268        (while (re-search-forward 
269                "^X-Gnus-Newsgroup:" 
270                (save-excursion (search-forward "\n\n" nil t) (point)) t)
271          (delete-region (progn (beginning-of-line) (point))
272                         (progn (forward-line 1) (point))))
273        (setq result (eval accept-form))
274        (kill-buffer buf)
275        result)
276      (save-excursion
277        (nnmbox-possibly-change-newsgroup group server)
278        (set-buffer nnmbox-mbox-buffer)
279        (goto-char (point-min))
280        (when (search-forward (nnmbox-article-string article) nil t)
281          (nnmbox-delete-mail))
282        (and last (save-buffer))))
283     result))
284
285 (deffoo nnmbox-request-accept-article (group &optional server last)
286   (nnmbox-possibly-change-newsgroup group server)
287   (nnmail-check-syntax)
288   (let ((buf (current-buffer))
289         result)
290     (goto-char (point-min))
291     ;; The From line may have been quoted by movemail.
292     (when (looking-at (concat ">" message-unix-mail-delimiter))
293       (delete-char 1))
294     (if (looking-at "X-From-Line: ")
295         (replace-match "From ")
296       (insert "From nobody " (current-time-string) "\n"))
297     (and 
298      (nnmail-activate 'nnmbox)
299      (progn
300        (set-buffer buf)
301        (goto-char (point-min))
302        (search-forward "\n\n" nil t)
303        (forward-line -1)
304        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
305          (delete-region (point) (progn (forward-line 1) (point))))
306        (setq result (nnmbox-save-mail
307                      (if (stringp group)
308                          (list (cons group (nnmbox-active-number group)))
309                        (nnmail-article-group 'nnmbox-active-number)))))
310      (save-excursion
311        (set-buffer nnmbox-mbox-buffer)
312        (goto-char (point-max))
313        (insert-buffer-substring buf)
314        (and last (save-buffer))
315        result)
316      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
317     (car result)))
318
319 (deffoo nnmbox-request-replace-article (article group buffer)
320   (nnmbox-possibly-change-newsgroup group)
321   (save-excursion
322     (set-buffer nnmbox-mbox-buffer)
323     (goto-char (point-min))
324     (if (not (search-forward (nnmbox-article-string article) nil t))
325         nil
326       (nnmbox-delete-mail t t)
327       (insert-buffer-substring buffer)
328       (save-buffer)
329       t)))
330
331 (deffoo nnmbox-request-delete-group (group &optional force server)
332   (nnmbox-possibly-change-newsgroup group server)
333   ;; Delete all articles in GROUP.
334   (if (not force)
335       ()                                ; Don't delete the articles.
336     (save-excursion
337       (set-buffer nnmbox-mbox-buffer)
338       (goto-char (point-min))
339       ;; Delete all articles in this group.
340       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
341             found)
342         (while (search-forward ident nil t)
343           (setq found t)
344           (nnmbox-delete-mail))
345         (when found
346           (save-buffer)))))
347   ;; Remove the group from all structures.
348   (setq nnmbox-group-alist 
349         (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
350         nnmbox-current-group nil)
351   ;; Save the active file.
352   (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
353   t)
354
355 (deffoo nnmbox-request-rename-group (group new-name &optional server)
356   (nnmbox-possibly-change-newsgroup group server)
357   (save-excursion
358     (set-buffer nnmbox-mbox-buffer)
359     (goto-char (point-min))
360     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
361           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
362           found)
363       (while (search-forward ident nil t)
364         (replace-match new-ident t t)
365         (setq found t))
366       (when found
367         (save-buffer))))
368   (let ((entry (assoc group nnmbox-group-alist)))
369     (when entry
370       (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 "^" message-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 "^" message-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       (when (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 &optional server)
410   (when (and server 
411              (not (nnmbox-server-opened server)))
412     (nnmbox-open-server server))
413   (when (or (not nnmbox-mbox-buffer)
414             (not (buffer-name nnmbox-mbox-buffer)))
415     (save-excursion
416       (set-buffer (setq nnmbox-mbox-buffer 
417                         (nnheader-find-file-noselect
418                          nnmbox-mbox-file nil 'raw)))
419       (buffer-disable-undo (current-buffer))))
420   (when (not nnmbox-group-alist)
421     (nnmail-activate 'nnmbox))
422   (if newsgroup
423       (when (assoc newsgroup nnmbox-group-alist)
424         (setq nnmbox-current-group newsgroup))
425     t))
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     (when (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 (group-art)
443   "Called narrowed to an article."
444   (let ((delim (concat "^" message-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 'nnmail-prepare-save-mail-hook)
459     (run-hooks 'nnmbox-prepare-save-mail-hook)
460     group-art))
461
462 (defun nnmbox-insert-newsgroup-line (group-art)
463   (save-excursion
464     (goto-char (point-min))
465     (when (search-forward "\n\n" nil t)
466       (forward-char -1)
467       (while group-art
468         (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
469                         (caar group-art) (cdar 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 (cadr (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       (push (list group (setq active (cons 1 1)))
483             nnmbox-group-alist))
484     (cdr active)))
485
486 (defun nnmbox-create-mbox ()
487   (when (not (file-exists-p nnmbox-mbox-file))
488     (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg)))
489
490 (defun nnmbox-read-mbox ()
491   (nnmail-activate 'nnmbox)
492   (nnmbox-create-mbox)
493   (if (and nnmbox-mbox-buffer
494            (buffer-name nnmbox-mbox-buffer)
495            (save-excursion
496              (set-buffer nnmbox-mbox-buffer)
497              (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
498       ()
499     (save-excursion
500       (let ((delim (concat "^" message-unix-mail-delimiter))
501             (alist nnmbox-group-alist)
502             start end number)
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
508         ;; Go through the group alist and compare against
509         ;; the mbox file.
510         (while alist
511           (goto-char (point-max))
512           (when (and (re-search-backward
513                       (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
514                               (caar alist)) nil t)
515                      (>= (setq number
516                                (string-to-number 
517                                 (buffer-substring
518                                  (match-beginning 1) (match-end 1))))
519                          (cdadar alist)))
520             (setcdr (cadar alist) (1+ number)))
521           (setq alist (cdr alist)))
522         
523         (goto-char (point-min))
524         (while (re-search-forward delim nil t)
525           (setq start (match-beginning 0))
526           (when (not (search-forward "\nX-Gnus-Newsgroup: " 
527                                      (save-excursion 
528                                        (setq end
529                                              (or
530                                               (and
531                                                (re-search-forward delim nil t)
532                                                (match-beginning 0))
533                                               (point-max))))
534                                      t))
535             (save-excursion
536               (save-restriction
537                 (narrow-to-region start end)
538                 (nnmbox-save-mail 
539                  (nnmail-article-group 'nnmbox-active-number)))))
540           (goto-char end))))))
541
542 (provide 'nnmbox)
543
544 ;;; nnmbox.el ends here