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 " *nnmbox mbox buffer*")
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)
83 (number (length sequence))
85 beg article art-string start stop)
86 (nnmbox-possibly-change-newsgroup newsgroup)
88 (setq article (car sequence))
89 (setq art-string (nnmbox-article-string article))
90 (set-buffer nnmbox-mbox-buffer)
91 (if (or (search-forward art-string nil t)
92 (progn (goto-char (point-min))
93 (search-forward art-string nil t)))
98 (concat "^" rmail-unix-mail-delimiter) nil t)
100 (search-forward "\n\n" nil t)
101 (setq stop (1- (point)))
102 (set-buffer nntp-server-buffer)
103 (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 nntp-server-buffer)))
153 (defun nnmbox-status-message (&optional server)
154 nnmbox-status-string)
156 (defun nnmbox-request-article (article &optional newsgroup server buffer)
157 (nnmbox-possibly-change-newsgroup newsgroup)
158 (if (stringp article)
161 (set-buffer nnmbox-mbox-buffer)
162 (goto-char (point-min))
163 (if (search-forward (nnmbox-article-string article) nil t)
165 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
168 (or (and (re-search-forward
169 (concat "^" rmail-unix-mail-delimiter) nil t)
171 (goto-char (point-max)))
173 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
174 (set-buffer nntp-server-buffer)
176 (insert-buffer-substring nnmbox-mbox-buffer start stop)
177 (goto-char (point-min))
178 (while (looking-at "From ")
180 (insert "X-From-Line: ")
184 (defun nnmbox-request-group (group &optional server dont-check)
186 (if (nnmbox-possibly-change-newsgroup group)
189 (nnmbox-get-new-mail group)
191 (set-buffer nntp-server-buffer)
193 (let ((active (assoc group nnmbox-group-alist)))
194 (insert (format "211 %d %d %d %s\n"
195 (1+ (- (cdr (car (cdr active)))
196 (car (car (cdr active)))))
197 (car (car (cdr active)))
198 (cdr (car (cdr active)))
202 (defun nnmbox-close-group (group &optional server)
205 (defun nnmbox-request-list (&optional server)
206 (if server (nnmbox-get-new-mail))
208 (or (nnmail-find-file nnmbox-active-file)
210 (setq nnmbox-group-alist (nnmail-get-active))
211 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
212 (nnmail-find-file nnmbox-active-file)))))
214 (defun nnmbox-request-newgroups (date &optional server)
215 (nnmbox-request-list server))
217 (defun nnmbox-request-list-newsgroups (&optional server)
218 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
221 (defun nnmbox-request-post (&optional server)
222 (mail-send-and-exit nil))
224 (fset 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
226 (defun nnmbox-request-expire-articles
227 (articles newsgroup &optional server force)
228 (nnmbox-possibly-change-newsgroup newsgroup)
229 (let* ((days (or (and nnmail-expiry-wait-function
230 (funcall nnmail-expiry-wait-function newsgroup))
234 (set-buffer nnmbox-mbox-buffer)
236 (goto-char (point-min))
237 (if (search-forward (nnmbox-article-string (car articles)) nil t)
239 (> (nnmail-days-between
240 (current-time-string)
242 (point) (progn (end-of-line) (point))))
245 (and gnus-verbose-backends
246 (message "Deleting: %s" (car articles)))
247 (nnmbox-delete-mail))
248 (setq rest (cons (car articles) rest))))
249 (setq articles (cdr articles)))
251 ;; Find the lowest active article in this group.
252 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
253 (goto-char (point-min))
254 (while (and (not (search-forward
255 (nnmbox-article-string (car active)) nil t))
256 (<= (car active) (cdr active)))
257 (setcar active (1+ (car active)))
258 (goto-char (point-min))))
259 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
262 (defun nnmbox-request-move-article
263 (article group server accept-form &optional last)
264 (nnmbox-possibly-change-newsgroup group)
265 (let ((buf (get-buffer-create " *nnmbox move*"))
268 (nnmbox-request-article article group server)
271 (buffer-disable-undo (current-buffer))
273 (insert-buffer-substring nntp-server-buffer)
274 (goto-char (point-min))
275 (while (re-search-forward
277 (save-excursion (search-forward "\n\n" nil t) (point)) t)
278 (delete-region (progn (beginning-of-line) (point))
279 (progn (forward-line 1) (point))))
280 (setq result (eval accept-form))
284 (set-buffer nnmbox-mbox-buffer)
285 (goto-char (point-min))
286 (if (search-forward (nnmbox-article-string article) nil t)
287 (nnmbox-delete-mail))
288 (and last (save-buffer))))
291 (defun nnmbox-request-accept-article (group &optional last)
292 (let ((buf (current-buffer))
294 (goto-char (point-min))
295 (if (looking-at "X-From-Line: ")
296 (replace-match "From ")
297 (insert "From nobody " (current-time-string) "\n"))
299 (nnmbox-request-list)
300 (setq nnmbox-group-alist (nnmail-get-active))
303 (goto-char (point-min))
304 (search-forward "\n\n" nil t)
306 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
307 (delete-region (point) (progn (forward-line 1) (point))))
308 (setq result (nnmbox-save-mail (and (stringp group) group))))
310 (set-buffer nnmbox-mbox-buffer)
311 (insert-buffer-substring buf)
312 (and last (save-buffer))
314 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
317 (defun nnmbox-request-replace-article (article group buffer)
318 (nnmbox-possibly-change-newsgroup group)
320 (set-buffer nnmbox-mbox-buffer)
321 (goto-char (point-min))
322 (if (not (search-forward (nnmbox-article-string article) nil t))
324 (nnmbox-delete-mail t t)
325 (insert-buffer-substring buffer)
330 ;;; Internal functions.
332 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
333 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
335 (defun nnmbox-delete-mail (&optional force leave-delim)
336 ;; Delete the current X-Gnus-Newsgroup line.
339 (progn (beginning-of-line) (point))
340 (progn (forward-line 1) (point))))
341 ;; Beginning of the article.
346 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
347 (if leave-delim (progn (forward-line 1) (point))
348 (match-beginning 0)))
351 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
353 (if (and (not (bobp)) leave-delim)
354 (progn (forward-line -2) (point))
355 (match-beginning 0)))
357 (goto-char (point-min))
358 ;; Only delete the article if no other groups owns it as well.
359 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
360 (delete-region (point-min) (point-max))))))
362 (defun nnmbox-possibly-change-newsgroup (newsgroup)
363 (if (not (get-buffer nnmbox-mbox-buffer))
365 (set-buffer (setq nnmbox-mbox-buffer
366 (find-file-noselect nnmbox-mbox-file)))
367 (buffer-disable-undo (current-buffer))))
368 (if (not nnmbox-group-alist)
370 (nnmbox-request-list)
371 (setq nnmbox-group-alist (nnmail-get-active))))
373 (if (assoc newsgroup nnmbox-group-alist)
374 (setq nnmbox-current-group newsgroup))))
376 (defun nnmbox-article-string (article)
377 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
378 (int-to-string article) " "))
380 (defun nnmbox-save-mail (&optional group)
381 "Called narrowed to an article."
382 (let* ((nnmail-split-methods
383 (if group (list (list group "")) nnmail-split-methods))
384 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
385 (nnmail-insert-lines)
386 (nnmail-insert-xref group-art)
387 (nnmbox-insert-newsgroup-line group-art)
388 (run-hooks 'nnml-prepare-save-mail-hook)
391 (defun nnmbox-insert-newsgroup-line (group-art)
393 (goto-char (point-min))
394 (if (search-forward "\n\n" nil t)
398 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
399 (car (car group-art)) (cdr (car group-art))
400 (current-time-string)))
401 (setq group-art (cdr group-art)))))
404 (defun nnmbox-active-number (group)
405 "Find the next article number in GROUP."
406 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
407 (setcdr active (1+ (cdr active)))
410 (defun nnmbox-read-mbox ()
411 (nnmbox-request-list)
412 (setq nnmbox-group-alist (nnmail-get-active))
413 (if (not (file-exists-p nnmbox-mbox-file))
414 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
415 (if (and nnmbox-mbox-buffer
416 (get-buffer nnmbox-mbox-buffer)
417 (buffer-name nnmbox-mbox-buffer)
419 (set-buffer nnmbox-mbox-buffer)
420 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
423 (let ((delim (concat "^" rmail-unix-mail-delimiter))
425 (set-buffer (setq nnmbox-mbox-buffer
426 (find-file-noselect nnmbox-mbox-file)))
427 (buffer-disable-undo (current-buffer))
428 (goto-char (point-min))
429 (while (re-search-forward delim nil t)
430 (setq start (match-beginning 0))
431 (if (not (search-forward "\nX-Gnus-Newsgroup: "
436 (re-search-forward delim nil t)
442 (narrow-to-region start end)
443 (nnmbox-save-mail))))
446 (defun nnmbox-get-new-mail (&optional group)
447 "Read new incoming mail."
448 (let ((spools (nnmail-get-spool-files group))
451 (if (or (not nnmbox-get-new-mail) (not nnmail-spool-file))
453 ;; We go through all the existing spool files and split the
457 (file-exists-p (car spools))
458 (> (nth 7 (file-attributes (car spools))) 0)
460 (and gnus-verbose-backends
461 (message "nnmbox: Reading incoming mail..."))
464 (car spools) (concat nnmbox-mbox-file "-Incoming")))
466 (let ((in-buf (nnmail-split-incoming
467 incoming 'nnmbox-save-mail t)))
468 (set-buffer nnmbox-mbox-buffer)
469 (goto-char (point-max))
470 (insert-buffer-substring in-buf)
471 (kill-buffer in-buf)))))
472 (setq spools (cdr spools)))
473 ;; If we did indeed read any incoming spools, we save all info.
474 (and (buffer-modified-p nnmbox-mbox-buffer)
476 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
477 (set-buffer nnmbox-mbox-buffer)
480 ;; The following has been commented away, just to make sure
481 ;; that nobody ever loses any mail. If you feel safe that
482 ;; nnfolder will never do anything strange, just remove those
483 ;; two semicolons, and avoid having lots of "Incoming*"
485 ;; (and (file-writable-p incoming) (delete-file incoming))
486 (setq incomings (cdr incomings))))))
491 ;;; nnmbox.el ends here