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