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