*** 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 (eval-when-compile (require 'cl))
36
37 (defvar nnmbox-mbox-file (expand-file-name "~/mbox")
38   "The name of the mail box file in the user's home directory.")
39
40 (defvar nnmbox-active-file (expand-file-name "~/.mbox-active")
41   "The name of the active file for the mail box.")
42
43 (defvar nnmbox-get-new-mail t
44   "If non-nil, nnmbox will check the incoming mail file and split the mail.")
45
46 (defvar nnmbox-prepare-save-mail-hook nil
47   "Hook run narrowed to an article before saving.")
48
49 \f
50
51 (defconst nnmbox-version "nnmbox 1.0"
52   "nnmbox version.")
53
54 (defvar nnmbox-current-group nil
55   "Current nnmbox news group directory.")
56
57 (defconst nnmbox-mbox-buffer nil)
58
59 (defvar nnmbox-status-string "")
60
61 (defvar nnmbox-group-alist nil)
62 (defvar nnmbox-active-timestamp nil)
63
64 \f
65
66 (defvar nnmbox-current-server nil)
67 (defvar nnmbox-server-alist nil)
68 (defvar nnmbox-server-variables 
69   (list
70    (list 'nnmbox-mbox-file nnmbox-mbox-file)
71    (list 'nnmbox-active-file nnmbox-active-file)
72    (list 'nnmbox-get-new-mail nnmbox-get-new-mail)
73    '(nnmbox-current-group nil)
74    '(nnmbox-status-string "")
75    '(nnmbox-group-alist nil)))
76
77 \f
78
79 ;;; Interface functions
80
81 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
82   (save-excursion
83     (set-buffer nntp-server-buffer)
84     (erase-buffer)
85     (let ((number (length sequence))
86           (count 0)
87           article art-string start stop)
88       (nnmbox-possibly-change-newsgroup newsgroup)
89       (while sequence
90         (setq article (car sequence))
91         (setq art-string (nnmbox-article-string article))
92         (set-buffer nnmbox-mbox-buffer)
93         (if (or (search-forward art-string nil t)
94                 (progn (goto-char (point-min))
95                        (search-forward art-string nil t)))
96             (progn
97               (setq start 
98                     (save-excursion
99                       (re-search-backward 
100                        (concat "^" rmail-unix-mail-delimiter) nil t)
101                       (point)))
102               (search-forward "\n\n" nil t)
103               (setq stop (1- (point)))
104               (set-buffer nntp-server-buffer)
105               (insert (format "221 %d Article retrieved.\n" article))
106               (insert-buffer-substring nnmbox-mbox-buffer start stop)
107               (goto-char (point-max))
108               (insert ".\n")))
109         (setq sequence (cdr sequence))
110         (setq count (1+ count))
111         (and (numberp nnmail-large-newsgroup)
112              (> number nnmail-large-newsgroup)
113              (zerop (% count 20))
114              (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
115                                (/ (* count 100) number))))
116
117       (and (numberp nnmail-large-newsgroup)
118            (> number nnmail-large-newsgroup)
119            (nnheader-message 5 "nnmbox: Receiving headers...done"))
120
121       (set-buffer nntp-server-buffer)
122       (nnheader-fold-continuation-lines)
123       'headers)))
124
125 (defun nnmbox-open-server (server &optional defs)
126   (nnheader-init-server-buffer)
127   (if (equal server nnmbox-current-server)
128       t
129     (if nnmbox-current-server
130         (setq nnmbox-server-alist 
131               (cons (list nnmbox-current-server
132                           (nnheader-save-variables nnmbox-server-variables))
133                     nnmbox-server-alist)))
134     (let ((state (assoc server nnmbox-server-alist)))
135       (if state 
136           (progn
137             (nnheader-restore-variables (nth 1 state))
138             (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
139         (nnheader-set-init-variables nnmbox-server-variables defs)))
140     (setq nnmbox-current-server server)))
141
142 (defun nnmbox-close-server (&optional server)
143   (setq nnmbox-current-server nil)
144   t)
145
146 (defun nnmbox-server-opened (&optional server)
147   (and (equal server nnmbox-current-server)
148        nnmbox-mbox-buffer
149        (buffer-name nnmbox-mbox-buffer)
150        nntp-server-buffer
151        (buffer-name nntp-server-buffer)))
152
153 (defun nnmbox-status-message (&optional server)
154   nnmbox-status-string)
155
156 (defun nnmbox-request-article (article &optional newsgroup server buffer)
157   (nnmbox-possibly-change-newsgroup newsgroup)
158   (save-excursion
159     (set-buffer nnmbox-mbox-buffer)
160     (goto-char (point-min))
161     (if (search-forward (nnmbox-article-string article) nil t)
162         (let (start stop)
163           (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
164           (setq start (point))
165           (forward-line 1)
166           (or (and (re-search-forward 
167                     (concat "^" rmail-unix-mail-delimiter) nil t)
168                    (forward-line -1))
169               (goto-char (point-max)))
170           (setq stop (point))
171           (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
172             (set-buffer nntp-server-buffer)
173             (erase-buffer)
174             (insert-buffer-substring nnmbox-mbox-buffer start stop)
175             (goto-char (point-min))
176             (while (looking-at "From ")
177               (delete-char 5)
178               (insert "X-From-Line: ")
179               (forward-line 1))
180             (if (numberp article) 
181                 (cons nnmbox-current-group article)
182               (nnmbox-article-group-number)))))))
183
184 (defun nnmbox-request-group (group &optional server dont-check)
185   (let ((active (cadr (assoc group nnmbox-group-alist))))
186     (cond 
187      ((null active)
188       (nnheader-report 'nnmbox "No such group: %s" group))
189      ((null (nnmbox-possibly-change-newsgroup group))
190       (nnheader-report 'nnmbox "No such group: %s" group))
191      (dont-check
192       (nnheader-report 'nnmbox "Selected group %s" group)
193       (nnheader-insert ""))
194      (t
195       (nnheader-report 'nnmbox "Selected group %s" group)
196       (nnheader-insert "211 %d %d %d %s\n" 
197                        (1+ (- (cdr active) (car active)))
198                        (car active) (cdr active) group)
199       t))))
200
201 (defun nnmbox-request-scan (&optional group server)
202   (nnmbox-read-mbox)
203   (nnmail-get-new-mail 
204    'nnmbox 
205    (lambda ()
206      (save-excursion
207        (set-buffer nnmbox-mbox-buffer)
208        (save-buffer)))
209    nnmbox-mbox-file group
210    (lambda ()
211      (save-excursion
212        (let ((in-buf (current-buffer)))
213          (set-buffer nnmbox-mbox-buffer)
214          (goto-char (point-max))
215          (insert-buffer-substring in-buf))))))
216
217 (defun nnmbox-close-group (group &optional server)
218   t)
219
220 (defun nnmbox-request-list (&optional server)
221   (save-excursion
222     (or (nnmail-find-file nnmbox-active-file)
223         (progn
224           (setq nnmbox-group-alist (nnmail-get-active))
225           (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
226           (nnmail-find-file nnmbox-active-file)))))
227
228 (defun nnmbox-request-newgroups (date &optional server)
229   (nnmbox-request-list server))
230
231 (defun nnmbox-request-list-newsgroups (&optional server)
232   (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
233   nil)
234
235 (defun nnmbox-request-post (&optional server)
236   (mail-send-and-exit nil))
237
238 (defun nnmbox-request-expire-articles 
239   (articles newsgroup &optional server force)
240   (nnmbox-possibly-change-newsgroup newsgroup)
241   (let* ((is-old t)
242          rest)
243     (nnmail-activate 'nnmbox)
244
245     (save-excursion 
246       (set-buffer nnmbox-mbox-buffer)
247       (while (and articles is-old)
248         (goto-char (point-min))
249         (if (search-forward (nnmbox-article-string (car articles)) nil t)
250             (if (setq is-old
251                       (nnmail-expired-article-p
252                        newsgroup
253                        (buffer-substring 
254                         (point) (progn (end-of-line) (point))) force))
255                 (progn
256                   (nnheader-message 5 "Deleting article %d in %s..."
257                                     (car articles) newsgroup)
258                   (nnmbox-delete-mail))
259               (setq rest (cons (car articles) rest))))
260         (setq articles (cdr articles)))
261       (save-buffer)
262       ;; Find the lowest active article in this group.
263       (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
264         (goto-char (point-min))
265         (while (and (not (search-forward
266                           (nnmbox-article-string (car active)) nil t))
267                     (<= (car active) (cdr active)))
268           (setcar active (1+ (car active)))
269           (goto-char (point-min))))
270       (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
271       (nconc rest articles))))
272
273 (defun nnmbox-request-move-article
274   (article group server accept-form &optional last)
275   (nnmbox-possibly-change-newsgroup group)
276   (let ((buf (get-buffer-create " *nnmbox move*"))
277         result)
278     (and 
279      (nnmbox-request-article article group server)
280      (save-excursion
281        (set-buffer buf)
282        (buffer-disable-undo (current-buffer))
283        (erase-buffer)
284        (insert-buffer-substring nntp-server-buffer)
285        (goto-char (point-min))
286        (while (re-search-forward 
287                "^X-Gnus-Newsgroup:" 
288                (save-excursion (search-forward "\n\n" nil t) (point)) t)
289          (delete-region (progn (beginning-of-line) (point))
290                         (progn (forward-line 1) (point))))
291        (setq result (eval accept-form))
292        (kill-buffer buf)
293        result)
294      (save-excursion
295        (set-buffer nnmbox-mbox-buffer)
296        (goto-char (point-min))
297        (if (search-forward (nnmbox-article-string article) nil t)
298            (nnmbox-delete-mail))
299        (and last (save-buffer))))
300     result))
301
302 (defun nnmbox-request-accept-article (group &optional last)
303   (let ((buf (current-buffer))
304         result)
305     (goto-char (point-min))
306     (if (looking-at "X-From-Line: ")
307         (replace-match "From ")
308       (insert "From nobody " (current-time-string) "\n"))
309     (and 
310      (nnmail-activate 'nnmbox)
311      (progn
312        (set-buffer buf)
313        (goto-char (point-min))
314        (search-forward "\n\n" nil t)
315        (forward-line -1)
316        (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
317          (delete-region (point) (progn (forward-line 1) (point))))
318        (setq result (nnmbox-save-mail (and (stringp group) group))))
319      (save-excursion
320        (set-buffer nnmbox-mbox-buffer)
321        (insert-buffer-substring buf)
322        (and last (save-buffer))
323        result)
324      (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
325     (car result)))
326
327 (defun nnmbox-request-replace-article (article group buffer)
328   (nnmbox-possibly-change-newsgroup group)
329   (save-excursion
330     (set-buffer nnmbox-mbox-buffer)
331     (goto-char (point-min))
332     (if (not (search-forward (nnmbox-article-string article) nil t))
333         nil
334       (nnmbox-delete-mail t t)
335       (insert-buffer-substring buffer)
336       (save-buffer)
337       t)))
338
339 (defun nnmbox-request-delete-group (group &optional force server)
340   (nnmbox-possibly-change-newsgroup group)
341   ;; Delete all articles in GROUP.
342   (if (not force)
343       ()                                ; Don't delete the articles.
344     (save-excursion
345       (set-buffer nnmbox-mbox-buffer)
346       (goto-char (point-min))
347       ;; Delete all articles in this group.
348       (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
349             found)
350         (while (search-forward ident nil t)
351           (setq found t)
352           (nnmbox-delete-mail))
353         (and found (save-buffer)))))
354   ;; Remove the group from all structures.
355   (setq nnmbox-group-alist 
356         (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
357         nnmbox-current-group nil)
358   ;; Save the active file.
359   (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
360   t)
361
362 (defun nnmbox-request-rename-group (group new-name &optional server)
363   (nnmbox-possibly-change-newsgroup group)
364   (save-excursion
365     (set-buffer nnmbox-mbox-buffer)
366     (goto-char (point-min))
367     (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
368           (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
369           found)
370       (while (search-forward ident nil t)
371         (replace-match new-ident t t)
372         (setq found t))
373       (and found (save-buffer))))
374   (let ((entry (assoc group nnmbox-group-alist)))
375     (and entry (setcar entry new-name))
376     (setq nnmbox-current-group nil)
377     ;; Save the new group alist.
378     (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
379     t))
380
381 \f
382 ;;; Internal functions.
383
384 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
385 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
386 ;; delimiter line.
387 (defun nnmbox-delete-mail (&optional force leave-delim)
388   ;; Delete the current X-Gnus-Newsgroup line.
389   (or force
390       (delete-region
391        (progn (beginning-of-line) (point))
392        (progn (forward-line 1) (point))))
393   ;; Beginning of the article.
394   (save-excursion
395     (save-restriction
396       (narrow-to-region
397        (save-excursion
398          (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
399          (if leave-delim (progn (forward-line 1) (point))
400            (match-beginning 0)))
401        (progn
402          (forward-line 1)
403          (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter) 
404                                      nil t)
405                   (if (and (not (bobp)) leave-delim)
406                       (progn (forward-line -2) (point))
407                     (match-beginning 0)))
408              (point-max))))
409       (goto-char (point-min))
410       ;; Only delete the article if no other groups owns it as well.
411       (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
412           (delete-region (point-min) (point-max))))))
413
414 (defun nnmbox-possibly-change-newsgroup (newsgroup)
415   (if (or (not nnmbox-mbox-buffer)
416           (not (buffer-name nnmbox-mbox-buffer)))
417       (save-excursion
418         (set-buffer (setq nnmbox-mbox-buffer 
419                           (nnheader-find-file-noselect
420                            nnmbox-mbox-file nil 'raw)))
421         (buffer-disable-undo (current-buffer))))
422   (if (not nnmbox-group-alist)
423       (nnmail-activate 'nnmbox))
424   (if newsgroup
425       (if (assoc newsgroup nnmbox-group-alist)
426           (setq nnmbox-current-group newsgroup))
427     t))
428
429 (defun nnmbox-article-string (article)
430   (if (numberp article)
431       (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":" 
432               (int-to-string article) " ")
433     (concat "\nMessage-ID: " article)))
434
435 (defun nnmbox-article-group-number ()
436   (save-excursion
437     (goto-char (point-min))
438     (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
439                             nil t)
440          (cons (buffer-substring (match-beginning 1) (match-end 1))
441                (string-to-int
442                 (buffer-substring (match-beginning 2) (match-end 2)))))))
443
444 (defun nnmbox-save-mail (&optional group)
445   "Called narrowed to an article."
446   (let* ((nnmail-split-methods 
447           (if group (list (list group "")) nnmail-split-methods))
448          (group-art (nreverse (nnmail-article-group 'nnmbox-active-number)))
449          (delim (concat "^" rmail-unix-mail-delimiter)))
450     (goto-char (point-min))
451     ;; This might come from somewhere else.
452     (unless (looking-at delim)
453       (insert "From nobody " (current-time-string) "\n")
454       (goto-char (point-min)))
455     ;; Quote all "From " lines in the article.
456     (forward-line 1)
457     (while (re-search-forward delim nil t)
458       (beginning-of-line)
459       (insert "> "))
460     (nnmail-insert-lines)
461     (nnmail-insert-xref group-art)
462     (nnmbox-insert-newsgroup-line group-art)
463     (run-hooks 'nnmbox-prepare-save-mail-hook)
464     group-art))
465
466 (defun nnmbox-insert-newsgroup-line (group-art)
467   (save-excursion
468     (goto-char (point-min))
469     (if (search-forward "\n\n" nil t)
470         (progn
471           (forward-char -1)
472           (while group-art
473             (insert (format "X-Gnus-Newsgroup: %s:%d   %s\n" 
474                             (car (car group-art)) (cdr (car group-art))
475                             (current-time-string)))
476             (setq group-art (cdr group-art)))))
477     t))
478
479 (defun nnmbox-active-number (group)
480   ;; Find the next article number in GROUP.
481   (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
482     (if active
483         (setcdr active (1+ (cdr active)))
484       ;; This group is new, so we create a new entry for it.
485       ;; This might be a bit naughty... creating groups on the drop of
486       ;; a hat, but I don't know...
487       (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
488                                      nnmbox-group-alist)))
489     (cdr active)))
490
491 (defun nnmbox-read-mbox ()
492   (nnmail-activate 'nnmbox)
493   (if (not (file-exists-p nnmbox-mbox-file))
494       (write-region 1 1 nnmbox-mbox-file t 'nomesg))
495   (if (and nnmbox-mbox-buffer
496            (buffer-name nnmbox-mbox-buffer)
497            (save-excursion
498              (set-buffer nnmbox-mbox-buffer)
499              (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
500       ()
501     (save-excursion
502       (let ((delim (concat "^" rmail-unix-mail-delimiter))
503             start end)
504         (set-buffer (setq nnmbox-mbox-buffer 
505                           (nnheader-find-file-noselect
506                            nnmbox-mbox-file nil 'raw)))
507         (buffer-disable-undo (current-buffer))
508         (goto-char (point-min))
509         (while (re-search-forward delim nil t)
510           (setq start (match-beginning 0))
511           (if (not (search-forward "\nX-Gnus-Newsgroup: " 
512                                    (save-excursion 
513                                      (setq end
514                                            (or
515                                             (and
516                                              (re-search-forward delim nil t)
517                                              (match-beginning 0))
518                                             (point-max))))
519                                    t))
520               (save-excursion
521                 (save-restriction
522                   (narrow-to-region start end)
523                   (nnmbox-save-mail))))
524           (goto-char end))))))
525
526 (provide 'nnmbox)
527
528 ;;; nnmbox.el ends here