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)
83 (number (length sequence))
85 beg article art-string start stop)
86 (nnmbox-possibly-change-newsgroup newsgroup)
87 (if (stringp (car 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)))
100 (concat "^" rmail-unix-mail-delimiter) nil t)
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))
107 (insert-buffer-substring nnmbox-mbox-buffer start stop)
108 (goto-char (point-max))
110 (setq sequence (cdr sequence))
111 (setq count (1+ count))
112 (and (numberp nnmail-large-newsgroup)
113 (> number nnmail-large-newsgroup)
115 gnus-verbose-backends
116 (message "nnmbox: Receiving headers... %d%%"
117 (/ (* count 100) number))))
119 (and (numberp nnmail-large-newsgroup)
120 (> number nnmail-large-newsgroup)
121 gnus-verbose-backends
122 (message "nnmbox: Receiving headers...done"))
124 ;; Fold continuation lines.
125 (goto-char (point-min))
126 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
127 (replace-match " " t t))
130 (defun nnmbox-open-server (server &optional defs)
131 (nnheader-init-server-buffer)
132 (if (equal server nnmbox-current-server)
134 (if nnmbox-current-server
135 (setq nnmbox-server-alist
136 (cons (list nnmbox-current-server
137 (nnheader-save-variables nnmbox-server-variables))
138 nnmbox-server-alist)))
139 (let ((state (assoc server nnmbox-server-alist)))
142 (nnheader-restore-variables (nth 1 state))
143 (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
144 (nnheader-set-init-variables nnmbox-server-variables defs)))
145 (setq nnmbox-current-server server)))
147 (defun nnmbox-close-server (&optional server)
150 (defun nnmbox-server-opened (&optional server)
151 (and (equal server nnmbox-current-server)
153 (buffer-name nnmbox-mbox-buffer)
155 (buffer-name nntp-server-buffer)))
157 (defun nnmbox-status-message (&optional server)
158 nnmbox-status-string)
160 (defun nnmbox-request-article (article &optional newsgroup server buffer)
161 (nnmbox-possibly-change-newsgroup newsgroup)
162 (if (stringp article)
165 (set-buffer nnmbox-mbox-buffer)
166 (goto-char (point-min))
167 (if (search-forward (nnmbox-article-string article) nil t)
169 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
172 (or (and (re-search-forward
173 (concat "^" rmail-unix-mail-delimiter) nil t)
175 (goto-char (point-max)))
177 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
178 (set-buffer nntp-server-buffer)
180 (insert-buffer-substring nnmbox-mbox-buffer start stop)
181 (goto-char (point-min))
182 (while (looking-at "From ")
184 (insert "X-From-Line: ")
188 (defun nnmbox-request-group (group &optional server dont-check)
190 (if (nnmbox-possibly-change-newsgroup group)
193 (nnmbox-get-new-mail group)
195 (set-buffer nntp-server-buffer)
197 (let ((active (assoc group nnmbox-group-alist)))
198 (insert (format "211 %d %d %d %s\n"
199 (1+ (- (cdr (car (cdr active)))
200 (car (car (cdr active)))))
201 (car (car (cdr active)))
202 (cdr (car (cdr active)))
206 (defun nnmbox-close-group (group &optional server)
209 (defun nnmbox-request-list (&optional server)
210 (if server (nnmbox-get-new-mail))
212 (or (nnmail-find-file nnmbox-active-file)
214 (setq nnmbox-group-alist (nnmail-get-active))
215 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
216 (nnmail-find-file nnmbox-active-file)))))
218 (defun nnmbox-request-newgroups (date &optional server)
219 (nnmbox-request-list server))
221 (defun nnmbox-request-list-newsgroups (&optional server)
222 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
225 (defun nnmbox-request-post (&optional server)
226 (mail-send-and-exit nil))
228 (defalias 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
230 (defun nnmbox-request-expire-articles
231 (articles newsgroup &optional server force)
232 (nnmbox-possibly-change-newsgroup newsgroup)
233 (let* ((days (or (and nnmail-expiry-wait-function
234 (funcall nnmail-expiry-wait-function newsgroup))
238 (set-buffer nnmbox-mbox-buffer)
240 (goto-char (point-min))
241 (if (search-forward (nnmbox-article-string (car articles)) nil t)
243 (> (nnmail-days-between
244 (current-time-string)
246 (point) (progn (end-of-line) (point))))
249 (and gnus-verbose-backends
250 (message "Deleting: %s" (car articles)))
251 (nnmbox-delete-mail))
252 (setq rest (cons (car articles) rest))))
253 (setq articles (cdr articles)))
255 ;; Find the lowest active article in this group.
256 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
257 (goto-char (point-min))
258 (while (and (not (search-forward
259 (nnmbox-article-string (car active)) nil t))
260 (<= (car active) (cdr active)))
261 (setcar active (1+ (car active)))
262 (goto-char (point-min))))
263 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
266 (defun nnmbox-request-move-article
267 (article group server accept-form &optional last)
268 (nnmbox-possibly-change-newsgroup group)
269 (let ((buf (get-buffer-create " *nnmbox move*"))
272 (nnmbox-request-article article group server)
275 (buffer-disable-undo (current-buffer))
277 (insert-buffer-substring nntp-server-buffer)
278 (goto-char (point-min))
279 (while (re-search-forward
281 (save-excursion (search-forward "\n\n" nil t) (point)) t)
282 (delete-region (progn (beginning-of-line) (point))
283 (progn (forward-line 1) (point))))
284 (setq result (eval accept-form))
288 (set-buffer nnmbox-mbox-buffer)
289 (goto-char (point-min))
290 (if (search-forward (nnmbox-article-string article) nil t)
291 (nnmbox-delete-mail))
292 (and last (save-buffer))))
295 (defun nnmbox-request-accept-article (group &optional last)
296 (let ((buf (current-buffer))
298 (goto-char (point-min))
299 (if (looking-at "X-From-Line: ")
300 (replace-match "From ")
301 (insert "From nobody " (current-time-string) "\n"))
303 (nnmbox-request-list)
304 (setq nnmbox-group-alist (nnmail-get-active))
307 (goto-char (point-min))
308 (search-forward "\n\n" nil t)
310 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
311 (delete-region (point) (progn (forward-line 1) (point))))
312 (setq result (nnmbox-save-mail (and (stringp group) group))))
314 (set-buffer nnmbox-mbox-buffer)
315 (insert-buffer-substring buf)
316 (and last (save-buffer))
318 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
321 (defun nnmbox-request-replace-article (article group buffer)
322 (nnmbox-possibly-change-newsgroup group)
324 (set-buffer nnmbox-mbox-buffer)
325 (goto-char (point-min))
326 (if (not (search-forward (nnmbox-article-string article) nil t))
328 (nnmbox-delete-mail t t)
329 (insert-buffer-substring buffer)
334 ;;; Internal functions.
336 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
337 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
339 (defun nnmbox-delete-mail (&optional force leave-delim)
340 ;; Delete the current X-Gnus-Newsgroup line.
343 (progn (beginning-of-line) (point))
344 (progn (forward-line 1) (point))))
345 ;; Beginning of the article.
350 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
351 (if leave-delim (progn (forward-line 1) (point))
352 (match-beginning 0)))
355 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
357 (if (and (not (bobp)) leave-delim)
358 (progn (forward-line -2) (point))
359 (match-beginning 0)))
361 (goto-char (point-min))
362 ;; Only delete the article if no other groups owns it as well.
363 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
364 (delete-region (point-min) (point-max))))))
366 (defun nnmbox-possibly-change-newsgroup (newsgroup)
367 (if (or (not nnmbox-mbox-buffer)
368 (not (buffer-name nnmbox-mbox-buffer)))
370 (set-buffer (setq nnmbox-mbox-buffer
371 (find-file-noselect nnmbox-mbox-file)))
372 (buffer-disable-undo (current-buffer))))
373 (if (not nnmbox-group-alist)
375 (nnmbox-request-list)
376 (setq nnmbox-group-alist (nnmail-get-active))))
378 (if (assoc newsgroup nnmbox-group-alist)
379 (setq nnmbox-current-group newsgroup))))
381 (defun nnmbox-article-string (article)
382 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
383 (int-to-string article) " "))
385 (defun nnmbox-save-mail (&optional group)
386 "Called narrowed to an article."
387 (let* ((nnmail-split-methods
388 (if group (list (list group "")) nnmail-split-methods))
389 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
390 (nnmail-insert-lines)
391 (nnmail-insert-xref group-art)
392 (nnmbox-insert-newsgroup-line group-art)
393 (run-hooks 'nnml-prepare-save-mail-hook)
396 (defun nnmbox-insert-newsgroup-line (group-art)
398 (goto-char (point-min))
399 (if (search-forward "\n\n" nil t)
403 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
404 (car (car group-art)) (cdr (car group-art))
405 (current-time-string)))
406 (setq group-art (cdr group-art)))))
409 (defun nnmbox-active-number (group)
410 "Find the next article number in GROUP."
411 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
412 (setcdr active (1+ (cdr active)))
415 (defun nnmbox-read-mbox ()
416 (nnmbox-request-list)
417 (setq nnmbox-group-alist (nnmail-get-active))
418 (if (not (file-exists-p nnmbox-mbox-file))
419 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
420 (if (and nnmbox-mbox-buffer
421 (buffer-name nnmbox-mbox-buffer)
423 (set-buffer nnmbox-mbox-buffer)
424 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
427 (let ((delim (concat "^" rmail-unix-mail-delimiter))
429 (set-buffer (setq nnmbox-mbox-buffer
430 (find-file-noselect nnmbox-mbox-file)))
431 (buffer-disable-undo (current-buffer))
432 (goto-char (point-min))
433 (while (re-search-forward delim nil t)
434 (setq start (match-beginning 0))
435 (if (not (search-forward "\nX-Gnus-Newsgroup: "
440 (re-search-forward delim nil t)
446 (narrow-to-region start end)
447 (nnmbox-save-mail))))
450 (defun nnmbox-get-new-mail (&optional group)
451 "Read new incoming mail."
452 (let* ((spools (nnmail-get-spool-files group))
456 (if (or (not nnmbox-get-new-mail) (not nnmail-spool-file))
458 ;; We go through all the existing spool files and split the
462 (file-exists-p (car spools))
463 (> (nth 7 (file-attributes (car spools))) 0)
465 (and gnus-verbose-backends
466 (message "nnmbox: Reading incoming mail..."))
469 (car spools) (concat nnmbox-mbox-file "-Incoming")))
471 (setq group (nnmail-get-split-group (car spools) group))
472 (let ((in-buf (nnmail-split-incoming
473 incoming 'nnmbox-save-mail t group)))
474 (set-buffer nnmbox-mbox-buffer)
475 (goto-char (point-max))
476 (insert-buffer-substring in-buf)
477 (kill-buffer in-buf)))))
478 (setq spools (cdr spools)))
479 ;; If we did indeed read any incoming spools, we save all info.
480 (and (buffer-modified-p nnmbox-mbox-buffer)
482 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
483 (set-buffer nnmbox-mbox-buffer)
485 (if incomings (run-hooks 'nnmail-read-incoming-hook))
487 (and nnmail-delete-incoming
488 (file-writable-p incoming)
489 (delete-file incoming))
490 (setq incomings (cdr incomings))))))
495 ;;; nnmbox.el ends here