1 ;;; nnmbox.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
8 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;; For an overview of what the interface functions do, please see the
35 (defvar nnmbox-mbox-file (expand-file-name "~/mbox")
36 "The name of the mail box file in the user's home directory.")
38 (defvar nnmbox-active-file (expand-file-name "~/.mbox-active")
39 "The name of the active file for the mail box.")
41 (defvar nnmbox-get-new-mail t
42 "If non-nil, nnmbox will check the incoming mail file and split the mail.")
44 (defvar nnmbox-prepare-save-mail-hook nil
45 "Hook run narrowed to an article before saving.")
49 (defconst nnmbox-version "nnmbox 0.1"
52 (defvar nnmbox-current-group nil
53 "Current nnmbox news group directory.")
55 (defconst nnmbox-mbox-buffer nil)
57 (defvar nnmbox-status-string "")
59 (defvar nnmbox-group-alist nil)
63 (defvar nnmbox-current-server nil)
64 (defvar nnmbox-server-alist nil)
65 (defvar nnmbox-server-variables
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)))
76 ;;; Interface functions
78 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server)
80 (set-buffer nntp-server-buffer)
82 (let ((number (length sequence))
84 article art-string start stop)
85 (nnmbox-possibly-change-newsgroup newsgroup)
86 (if (stringp (car 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)))
99 (concat "^" rmail-unix-mail-delimiter) nil t)
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))
108 (setq sequence (cdr sequence))
109 (setq count (1+ count))
110 (and (numberp nnmail-large-newsgroup)
111 (> number nnmail-large-newsgroup)
113 gnus-verbose-backends
114 (message "nnmbox: Receiving headers... %d%%"
115 (/ (* count 100) number))))
117 (and (numberp nnmail-large-newsgroup)
118 (> number nnmail-large-newsgroup)
119 gnus-verbose-backends
120 (message "nnmbox: Receiving headers...done"))
122 ;; Fold continuation lines.
123 (goto-char (point-min))
124 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
125 (replace-match " " t t))
128 (defun nnmbox-open-server (server &optional defs)
129 (nnheader-init-server-buffer)
130 (if (equal server nnmbox-current-server)
132 (if nnmbox-current-server
133 (setq nnmbox-server-alist
134 (cons (list nnmbox-current-server
135 (nnheader-save-variables nnmbox-server-variables))
136 nnmbox-server-alist)))
137 (let ((state (assoc server nnmbox-server-alist)))
140 (nnheader-restore-variables (nth 1 state))
141 (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
142 (nnheader-set-init-variables nnmbox-server-variables defs)))
143 (setq nnmbox-current-server server)))
145 (defun nnmbox-close-server (&optional server)
148 (defun nnmbox-server-opened (&optional server)
149 (and (equal server nnmbox-current-server)
151 (buffer-name nnmbox-mbox-buffer)
153 (buffer-name nntp-server-buffer)))
155 (defun nnmbox-status-message (&optional server)
156 nnmbox-status-string)
158 (defun nnmbox-request-article (article &optional newsgroup server buffer)
159 (nnmbox-possibly-change-newsgroup newsgroup)
160 (if (stringp article)
163 (set-buffer nnmbox-mbox-buffer)
164 (goto-char (point-min))
165 (if (search-forward (nnmbox-article-string article) nil t)
167 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
170 (or (and (re-search-forward
171 (concat "^" rmail-unix-mail-delimiter) nil t)
173 (goto-char (point-max)))
175 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
176 (set-buffer nntp-server-buffer)
178 (insert-buffer-substring nnmbox-mbox-buffer start stop)
179 (goto-char (point-min))
180 (while (looking-at "From ")
182 (insert "X-From-Line: ")
186 (defun nnmbox-request-group (group &optional server dont-check)
188 (if (nnmbox-possibly-change-newsgroup group)
191 (nnmbox-get-new-mail group)
193 (set-buffer nntp-server-buffer)
195 (let ((active (assoc group nnmbox-group-alist)))
196 (insert (format "211 %d %d %d %s\n"
197 (1+ (- (cdr (car (cdr active)))
198 (car (car (cdr active)))))
199 (car (car (cdr active)))
200 (cdr (car (cdr active)))
204 (defun nnmbox-close-group (group &optional server)
207 (defun nnmbox-request-list (&optional server)
208 (if server (nnmbox-get-new-mail))
210 (or (nnmail-find-file nnmbox-active-file)
212 (setq nnmbox-group-alist (nnmail-get-active))
213 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
214 (nnmail-find-file nnmbox-active-file)))))
216 (defun nnmbox-request-newgroups (date &optional server)
217 (nnmbox-request-list server))
219 (defun nnmbox-request-list-newsgroups (&optional server)
220 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
223 (defun nnmbox-request-post (&optional server)
224 (mail-send-and-exit nil))
226 (defalias 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
228 (defun nnmbox-request-expire-articles
229 (articles newsgroup &optional server force)
230 (nnmbox-possibly-change-newsgroup newsgroup)
231 (let* ((days (or (and nnmail-expiry-wait-function
232 (funcall nnmail-expiry-wait-function newsgroup))
236 (set-buffer nnmbox-mbox-buffer)
238 (goto-char (point-min))
239 (if (search-forward (nnmbox-article-string (car articles)) nil t)
241 (> (nnmail-days-between
242 (current-time-string)
244 (point) (progn (end-of-line) (point))))
247 (and gnus-verbose-backends
248 (message "Deleting: %s" (car articles)))
249 (nnmbox-delete-mail))
250 (setq rest (cons (car articles) rest))))
251 (setq articles (cdr articles)))
253 ;; Find the lowest active article in this group.
254 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
255 (goto-char (point-min))
256 (while (and (not (search-forward
257 (nnmbox-article-string (car active)) nil t))
258 (<= (car active) (cdr active)))
259 (setcar active (1+ (car active)))
260 (goto-char (point-min))))
261 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
264 (defun nnmbox-request-move-article
265 (article group server accept-form &optional last)
266 (nnmbox-possibly-change-newsgroup group)
267 (let ((buf (get-buffer-create " *nnmbox move*"))
270 (nnmbox-request-article article group server)
273 (buffer-disable-undo (current-buffer))
275 (insert-buffer-substring nntp-server-buffer)
276 (goto-char (point-min))
277 (while (re-search-forward
279 (save-excursion (search-forward "\n\n" nil t) (point)) t)
280 (delete-region (progn (beginning-of-line) (point))
281 (progn (forward-line 1) (point))))
282 (setq result (eval accept-form))
286 (set-buffer nnmbox-mbox-buffer)
287 (goto-char (point-min))
288 (if (search-forward (nnmbox-article-string article) nil t)
289 (nnmbox-delete-mail))
290 (and last (save-buffer))))
293 (defun nnmbox-request-accept-article (group &optional last)
294 (let ((buf (current-buffer))
296 (goto-char (point-min))
297 (if (looking-at "X-From-Line: ")
298 (replace-match "From ")
299 (insert "From nobody " (current-time-string) "\n"))
301 (nnmbox-request-list)
302 (setq nnmbox-group-alist (nnmail-get-active))
305 (goto-char (point-min))
306 (search-forward "\n\n" nil t)
308 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
309 (delete-region (point) (progn (forward-line 1) (point))))
310 (setq result (nnmbox-save-mail (and (stringp group) group))))
312 (set-buffer nnmbox-mbox-buffer)
313 (insert-buffer-substring buf)
314 (and last (save-buffer))
316 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
319 (defun nnmbox-request-replace-article (article group buffer)
320 (nnmbox-possibly-change-newsgroup group)
322 (set-buffer nnmbox-mbox-buffer)
323 (goto-char (point-min))
324 (if (not (search-forward (nnmbox-article-string article) nil t))
326 (nnmbox-delete-mail t t)
327 (insert-buffer-substring buffer)
332 ;;; Internal functions.
334 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
335 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
337 (defun nnmbox-delete-mail (&optional force leave-delim)
338 ;; Delete the current X-Gnus-Newsgroup line.
341 (progn (beginning-of-line) (point))
342 (progn (forward-line 1) (point))))
343 ;; Beginning of the article.
348 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
349 (if leave-delim (progn (forward-line 1) (point))
350 (match-beginning 0)))
353 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
355 (if (and (not (bobp)) leave-delim)
356 (progn (forward-line -2) (point))
357 (match-beginning 0)))
359 (goto-char (point-min))
360 ;; Only delete the article if no other groups owns it as well.
361 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
362 (delete-region (point-min) (point-max))))))
364 (defun nnmbox-possibly-change-newsgroup (newsgroup)
365 (if (or (not nnmbox-mbox-buffer)
366 (not (buffer-name nnmbox-mbox-buffer)))
368 (set-buffer (setq nnmbox-mbox-buffer
369 (find-file-noselect nnmbox-mbox-file)))
370 (buffer-disable-undo (current-buffer))))
371 (if (not nnmbox-group-alist)
373 (nnmbox-request-list)
374 (setq nnmbox-group-alist (nnmail-get-active))))
376 (if (assoc newsgroup nnmbox-group-alist)
377 (setq nnmbox-current-group newsgroup))))
379 (defun nnmbox-article-string (article)
380 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
381 (int-to-string article) " "))
383 (defun nnmbox-save-mail (&optional group)
384 "Called narrowed to an article."
385 (let* ((nnmail-split-methods
386 (if group (list (list group "")) nnmail-split-methods))
387 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
388 (nnmail-insert-lines)
389 (nnmail-insert-xref group-art)
390 (nnmbox-insert-newsgroup-line group-art)
391 (run-hooks 'nnml-prepare-save-mail-hook)
394 (defun nnmbox-insert-newsgroup-line (group-art)
396 (goto-char (point-min))
397 (if (search-forward "\n\n" nil t)
401 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
402 (car (car group-art)) (cdr (car group-art))
403 (current-time-string)))
404 (setq group-art (cdr group-art)))))
407 (defun nnmbox-active-number (group)
408 "Find the next article number in GROUP."
409 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
410 (setcdr active (1+ (cdr active)))
413 (defun nnmbox-read-mbox ()
414 (nnmbox-request-list)
415 (setq nnmbox-group-alist (nnmail-get-active))
416 (if (not (file-exists-p nnmbox-mbox-file))
417 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
418 (if (and nnmbox-mbox-buffer
419 (buffer-name nnmbox-mbox-buffer)
421 (set-buffer nnmbox-mbox-buffer)
422 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
425 (let ((delim (concat "^" rmail-unix-mail-delimiter))
427 (set-buffer (setq nnmbox-mbox-buffer
428 (find-file-noselect nnmbox-mbox-file)))
429 (buffer-disable-undo (current-buffer))
430 (goto-char (point-min))
431 (while (re-search-forward delim nil t)
432 (setq start (match-beginning 0))
433 (if (not (search-forward "\nX-Gnus-Newsgroup: "
438 (re-search-forward delim nil t)
444 (narrow-to-region start end)
445 (nnmbox-save-mail))))
448 (defun nnmbox-get-new-mail (&optional group)
449 "Read new incoming mail."
450 (let* ((spools (nnmail-get-spool-files group))
454 (if (or (not nnmbox-get-new-mail) (not nnmail-spool-file))
456 ;; We go through all the existing spool files and split the
460 (file-exists-p (car spools))
461 (> (nth 7 (file-attributes (car spools))) 0)
463 (and gnus-verbose-backends
464 (message "nnmbox: Reading incoming mail..."))
467 (car spools) (concat nnmbox-mbox-file "-Incoming")))
469 (setq group (nnmail-get-split-group (car spools) group-in))
470 (let ((in-buf (nnmail-split-incoming
471 incoming 'nnmbox-save-mail t group)))
472 (set-buffer nnmbox-mbox-buffer)
473 (goto-char (point-max))
474 (insert-buffer-substring in-buf)
475 (kill-buffer in-buf)))))
476 (setq spools (cdr spools)))
477 ;; If we did indeed read any incoming spools, we save all info.
478 (and (buffer-modified-p nnmbox-mbox-buffer)
480 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
481 (set-buffer nnmbox-mbox-buffer)
483 (if incomings (run-hooks 'nnmail-read-incoming-hook))
485 (and nnmail-delete-incoming
486 (file-writable-p incoming)
487 (delete-file incoming))
488 (setq incomings (cdr incomings))))))
493 ;;; nnmbox.el ends here