*** 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 '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         (if (or (search-forward art-string nil t)
86                 (progn (goto-char (point-min))
87                        (search-forward art-string nil t)))
88             (progn
89               (setq start 
90                     (save-excursion
91                       (re-search-backward 
92                        (concat "^" message-unix-mail-delimiter) nil t)
93                       (point)))
94               (search-forward "\n\n" nil t)
95               (setq stop (1- (point)))
96               (set-buffer nntp-server-buffer)
97               (insert (format "221 %d Article retrieved.\n" article))
98               (insert-buffer-substring nnmbox-mbox-buffer start stop)
99               (goto-char (point-max))
100               (insert ".\n")))
101         (setq sequence (cdr sequence))
102         (setq count (1+ count))
103         (and (numberp nnmail-large-newsgroup)
104              (> number nnmail-large-newsgroup)
105              (zerop (% count 20))
106              (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
107                                (/ (* count 100) number))))
108
109       (and (numberp nnmail-large-newsgroup)
110            (> number nnmail-large-newsgroup)
111            (nnheader-message 5 "nnmbox: Receiving headers...done"))
112
113       (set-buffer nntp-server-buffer)
114       (nnheader-fold-continuation-lines)
115       'headers)))
116
117 (deffoo nnmbox-open-server (server &optional defs)
118   (nnoo-change-server 'nnmbox server defs)
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     (if (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
214 (deffoo nnmbox-request-newgroups (date &optional server)
215   (nnmbox-request-list server))
216
217 (deffoo nnmbox-request-list-newsgroups (&optional server)
218   (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
219
220 (deffoo nnmbox-request-expire-articles 
221   (articles newsgroup &optional server force)
222   (nnmbox-possibly-change-newsgroup newsgroup server)
223   (let* ((is-old t)
224          rest)
225     (nnmail-activate 'nnmbox)
226
227     (save-excursion 
228       (set-buffer nnmbox-mbox-buffer)
229       (while (and articles is-old)
230         (goto-char (point-min))
231         (if (search-forward (nnmbox-article-string (car articles)) nil t)
232             (if (setq is-old
233                       (nnmail-expired-article-p
234                        newsgroup
235                        (buffer-substring 
236                         (point) (progn (end-of-line) (point))) force))
237                 (progn
238                   (nnheader-message 5 "Deleting article %d in %s..."
239                                     (car articles) newsgroup)
240                   (nnmbox-delete-mail))
241               (setq rest (cons (car articles) rest))))
242         (setq articles (cdr articles)))
243       (save-buffer)
244       ;; Find the lowest active article in this group.
245       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
246         (goto-char (point-min))
247         (while (and (not (search-forward
248                           (nnmbox-article-string (car active)) nil t))
249                     (<= (car active) (cdr active)))
250           (setcar active (1+ (car active)))
251           (goto-char (point-min))))
252       (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
253       (nconc rest articles))))
254
255 (deffoo nnmbox-request-move-article
256   (article group server accept-form &optional last)
257   (let ((buf (get-buffer-create " *nnmbox move*"))
258         result)
259     (and 
260      (nnmbox-request-article article group server)
261      (save-excursion
262        (set-buffer buf)
263        (buffer-disable-undo (current-buffer))
264        (erase-buffer)
265        (insert-buffer-substring nntp-server-buffer)
266        (goto-char (point-min))
267        (while (re-search-forward 
268                "^X-Gnus-Newsgroup:" 
269                (save-excursion (search-forward "\n\n" nil t) (point)) t)
270          (delete-region (progn (beginning-of-line) (point))
271                         (progn (forward-line 1) (point))))
272        (setq result (eval accept-form))
273        (kill-buffer buf)
274        result)
275      (save-excursion
276        (nnmbox-possibly-change-newsgroup group server)
277        (set-buffer nnmbox-mbox-buffer)
278        (goto-char (point-min))
279        (if (search-forward (nnmbox-article-string article) nil t)
280            (nnmbox-delete-mail))
281        (and last (save-buffer))))
282     result))
283
284 (deffoo nnmbox-request-accept-article (group &optional server last)
285   (nnmbox-possibly-change-newsgroup group server)
286   (nnmail-check-syntax)
287   (let ((buf (current-buffer))
288         result)
289     (goto-char (point-min))
290     ;; The From line may have been quoted by movemail.
291     (when (looking-at (concat ">" message-unix-mail-delimiter))
292       (delete-char 1))
293     (if (looking-at "X-From-Line: ")
294         (replace-match "From ")
295       (insert "From nobody " (current-time-string) "\n"))
296     (and 
297      (nnmail-activate 'nnmbox)
298      (progn
299        (set-buffer buf)
300        (goto-char (point-min))
301        (search-forward "\n\n" nil t)
302        (forward-line -1)
303        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
304          (delete-region (point) (progn (forward-line 1) (point))))
305        (setq result (nnmbox-save-mail
306                      (if (stringp group)
307                          (list (cons group (nnmbox-active-number group)))
308                        (nnmail-article-group 'nnmbox-active-number)))))
309      (save-excursion
310        (set-buffer nnmbox-mbox-buffer)
311        (goto-char (point-max))
312        (insert-buffer-substring buf)
313        (and last (save-buffer))
314        result)
315      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
316     (car result)))
317
318 (deffoo nnmbox-request-replace-article (article group buffer)
319   (nnmbox-possibly-change-newsgroup group)
320   (save-excursion
321     (set-buffer nnmbox-mbox-buffer)
322     (goto-char (point-min))
323     (if (not (search-forward (nnmbox-article-string article) nil t))
324         nil
325       (nnmbox-delete-mail t t)
326       (insert-buffer-substring buffer)
327       (save-buffer)
328       t)))
329
330 (deffoo nnmbox-request-delete-group (group &optional force server)
331   (nnmbox-possibly-change-newsgroup group server)
332   ;; Delete all articles in GROUP.
333   (if (not force)
334       ()                                ; Don't delete the articles.
335     (save-excursion
336       (set-buffer nnmbox-mbox-buffer)
337       (goto-char (point-min))
338       ;; Delete all articles in this group.
339       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
340             found)
341         (while (search-forward ident nil t)
342           (setq found t)
343           (nnmbox-delete-mail))
344         (and found (save-buffer)))))
345   ;; Remove the group from all structures.
346   (setq nnmbox-group-alist 
347         (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
348         nnmbox-current-group nil)
349   ;; Save the active file.
350   (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
351   t)
352
353 (deffoo nnmbox-request-rename-group (group new-name &optional server)
354   (nnmbox-possibly-change-newsgroup group server)
355   (save-excursion
356     (set-buffer nnmbox-mbox-buffer)
357     (goto-char (point-min))
358     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
359           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
360           found)
361       (while (search-forward ident nil t)
362         (replace-match new-ident t t)
363         (setq found t))
364       (and found (save-buffer))))
365   (let ((entry (assoc group nnmbox-group-alist)))
366     (and entry (setcar entry new-name))
367     (setq nnmbox-current-group nil)
368     ;; Save the new group alist.
369     (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
370     t))
371
372 \f
373 ;;; Internal functions.
374
375 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
376 ;; headers there are.  If LEAVE-DELIM, don't delete the Unix mbox
377 ;; delimiter line.
378 (defun nnmbox-delete-mail (&optional force leave-delim)
379   ;; Delete the current X-Gnus-Newsgroup line.
380   (or force
381       (delete-region
382        (progn (beginning-of-line) (point))
383        (progn (forward-line 1) (point))))
384   ;; Beginning of the article.
385   (save-excursion
386     (save-restriction
387       (narrow-to-region
388        (save-excursion
389          (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
390          (if leave-delim (progn (forward-line 1) (point))
391            (match-beginning 0)))
392        (progn
393          (forward-line 1)
394          (or (and (re-search-forward (concat "^" message-unix-mail-delimiter) 
395                                      nil t)
396                   (if (and (not (bobp)) leave-delim)
397                       (progn (forward-line -2) (point))
398                     (match-beginning 0)))
399              (point-max))))
400       (goto-char (point-min))
401       ;; Only delete the article if no other groups owns it as well.
402       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
403           (delete-region (point-min) (point-max))))))
404
405 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
406   (when (and server 
407              (not (nnmbox-server-opened server)))
408     (nnmbox-open-server server))
409   (if (or (not nnmbox-mbox-buffer)
410           (not (buffer-name nnmbox-mbox-buffer)))
411       (save-excursion
412         (set-buffer (setq nnmbox-mbox-buffer 
413                           (nnheader-find-file-noselect
414                            nnmbox-mbox-file nil 'raw)))
415         (buffer-disable-undo (current-buffer))))
416   (if (not nnmbox-group-alist)
417       (nnmail-activate 'nnmbox))
418   (if newsgroup
419       (if (assoc newsgroup nnmbox-group-alist)
420           (setq nnmbox-current-group newsgroup))
421     t))
422
423 (defun nnmbox-article-string (article)
424   (if (numberp article)
425       (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":" 
426               (int-to-string article) " ")
427     (concat "\nMessage-ID: " article)))
428
429 (defun nnmbox-article-group-number ()
430   (save-excursion
431     (goto-char (point-min))
432     (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
433                             nil t)
434          (cons (buffer-substring (match-beginning 1) (match-end 1))
435                (string-to-int
436                 (buffer-substring (match-beginning 2) (match-end 2)))))))
437
438 (defun nnmbox-save-mail (group-art)
439   "Called narrowed to an article."
440   (let ((delim (concat "^" message-unix-mail-delimiter)))
441     (goto-char (point-min))
442     ;; This might come from somewhere else.
443     (unless (looking-at delim)
444       (insert "From nobody " (current-time-string) "\n")
445       (goto-char (point-min)))
446     ;; Quote all "From " lines in the article.
447     (forward-line 1)
448     (while (re-search-forward delim nil t)
449       (beginning-of-line)
450       (insert "> "))
451     (nnmail-insert-lines)
452     (nnmail-insert-xref group-art)
453     (nnmbox-insert-newsgroup-line group-art)
454     (run-hooks 'nnmail-prepare-save-mail-hook)
455     (run-hooks 'nnmbox-prepare-save-mail-hook)
456     group-art))
457
458 (defun nnmbox-insert-newsgroup-line (group-art)
459   (save-excursion
460     (goto-char (point-min))
461     (if (search-forward "\n\n" nil t)
462         (progn
463           (forward-char -1)
464           (while group-art
465             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
466                             (caar group-art) (cdar group-art)
467                             (current-time-string)))
468             (setq group-art (cdr group-art)))))
469     t))
470
471 (defun nnmbox-active-number (group)
472   ;; Find the next article number in GROUP.
473   (let ((active (cadr (assoc group nnmbox-group-alist))))
474     (if active
475         (setcdr active (1+ (cdr active)))
476       ;; This group is new, so we create a new entry for it.
477       ;; This might be a bit naughty... creating groups on the drop of
478       ;; a hat, but I don't know...
479       (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
480                                      nnmbox-group-alist)))
481     (cdr active)))
482
483 (defun nnmbox-read-mbox ()
484   (nnmail-activate 'nnmbox)
485   (if (not (file-exists-p nnmbox-mbox-file))
486       (nnmail-write-region 1 1 nnmbox-mbox-file t 'nomesg))
487   (if (and nnmbox-mbox-buffer
488            (buffer-name nnmbox-mbox-buffer)
489            (save-excursion
490              (set-buffer nnmbox-mbox-buffer)
491              (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
492       ()
493     (save-excursion
494       (let ((delim (concat "^" message-unix-mail-delimiter))
495             (alist nnmbox-group-alist)
496             start end number)
497         (set-buffer (setq nnmbox-mbox-buffer 
498                           (nnheader-find-file-noselect
499                            nnmbox-mbox-file nil 'raw)))
500         (buffer-disable-undo (current-buffer))
501
502         ;; Go through the group alist and compare against
503         ;; the mbox file.
504         (while alist
505           (goto-char (point-max))
506           (when (and (re-search-backward
507                       (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
508                               (caar alist)) nil t)
509                      (>= (setq number
510                                (string-to-number 
511                                 (buffer-substring
512                                  (match-beginning 1) (match-end 1))))
513                          (cdadar alist)))
514             (setcdr (cadar alist) (1+ number)))
515           (setq alist (cdr alist)))
516         
517         (goto-char (point-min))
518         (while (re-search-forward delim nil t)
519           (setq start (match-beginning 0))
520           (if (not (search-forward "\nX-Gnus-Newsgroup: " 
521                                    (save-excursion 
522                                      (setq end
523                                            (or
524                                             (and
525                                              (re-search-forward delim nil t)
526                                              (match-beginning 0))
527                                             (point-max))))
528                                    t))
529               (save-excursion
530                 (save-restriction
531                   (narrow-to-region start end)
532                   (nnmbox-save-mail 
533                    (nnmail-article-group 'nnmbox-active-number)))))
534           (goto-char end))))))
535
536 (provide 'nnmbox)
537
538 ;;; nnmbox.el ends here