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