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 (not (search-forward
255 (nnmbox-article-string (car active)) nil t))
256 (setcar active (1+ (car active)))
257 (goto-char (point-min))))
258 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
261 (defun nnmbox-request-move-article
262 (article group server accept-form &optional last)
263 (nnmbox-possibly-change-newsgroup group)
264 (let ((buf (get-buffer-create " *nnmbox move*"))
267 (nnmbox-request-article article group server)
270 (buffer-disable-undo (current-buffer))
272 (insert-buffer-substring nntp-server-buffer)
273 (goto-char (point-min))
274 (while (re-search-forward
276 (save-excursion (search-forward "\n\n" nil t) (point)) t)
277 (delete-region (progn (beginning-of-line) (point))
278 (progn (forward-line 1) (point))))
279 (setq result (eval accept-form))
283 (set-buffer nnmbox-mbox-buffer)
284 (goto-char (point-min))
285 (if (search-forward (nnmbox-article-string article) nil t)
286 (nnmbox-delete-mail))
287 (and last (save-buffer))))
290 (defun nnmbox-request-accept-article (group &optional last)
291 (let ((buf (current-buffer))
293 (goto-char (point-min))
294 (if (looking-at "X-From-Line: ")
295 (replace-match "From ")
296 (insert "From nobody " (current-time-string) "\n"))
298 (nnmbox-request-list)
299 (setq nnmbox-group-alist (nnmail-get-active))
302 (goto-char (point-min))
303 (search-forward "\n\n" nil t)
305 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
306 (delete-region (point) (progn (forward-line 1) (point))))
307 (setq result (nnmbox-save-mail (and (stringp group) group))))
309 (set-buffer nnmbox-mbox-buffer)
310 (insert-buffer-substring buf)
311 (and last (save-buffer))
313 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
316 (defun nnmbox-request-replace-article (article group buffer)
317 (nnmbox-possibly-change-newsgroup group)
319 (set-buffer nnmbox-mbox-buffer)
320 (goto-char (point-min))
321 (if (not (search-forward (nnmbox-article-string article) nil t))
323 (nnmbox-delete-mail t t)
324 (insert-buffer-substring buffer)
329 ;;; Internal functions.
331 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
332 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
334 (defun nnmbox-delete-mail (&optional force leave-delim)
335 ;; Delete the current X-Gnus-Newsgroup line.
338 (progn (beginning-of-line) (point))
339 (progn (forward-line 1) (point))))
340 ;; Beginning of the article.
345 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
346 (if leave-delim (progn (forward-line 1) (point))
347 (match-beginning 0)))
350 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
352 (if (and (not (bobp)) leave-delim)
353 (progn (forward-line -2) (point))
354 (match-beginning 0)))
356 (goto-char (point-min))
357 ;; Only delete the article if no other groups owns it as well.
358 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
359 (delete-region (point-min) (point-max))))))
361 (defun nnmbox-possibly-change-newsgroup (newsgroup)
362 (if (not (get-buffer nnmbox-mbox-buffer))
364 (set-buffer (setq nnmbox-mbox-buffer
365 (find-file-noselect nnmbox-mbox-file)))
366 (buffer-disable-undo (current-buffer))))
367 (if (not nnmbox-group-alist)
369 (nnmbox-request-list)
370 (setq nnmbox-group-alist (nnmail-get-active))))
372 (if (assoc newsgroup nnmbox-group-alist)
373 (setq nnmbox-current-group newsgroup))))
375 (defun nnmbox-article-string (article)
376 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
377 (int-to-string article) " "))
379 (defun nnmbox-save-mail (&optional group)
380 "Called narrowed to an article."
381 (let* ((nnmail-split-methods
382 (if group (list (list group "")) nnmail-split-methods))
383 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
384 (nnmail-insert-lines)
385 (nnmail-insert-xref group-art)
386 (nnmbox-insert-newsgroup-line group-art)
387 (run-hooks 'nnml-prepare-save-mail-hook)
390 (defun nnmbox-insert-newsgroup-line (group-art)
392 (goto-char (point-min))
393 (if (search-forward "\n\n" nil t)
397 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
398 (car (car group-art)) (cdr (car group-art))
399 (current-time-string)))
400 (setq group-art (cdr group-art)))))
403 (defun nnmbox-active-number (group)
404 "Find the next article number in GROUP."
405 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
406 (setcdr active (1+ (cdr active)))
409 (defun nnmbox-read-mbox ()
410 (nnmbox-request-list)
411 (setq nnmbox-group-alist (nnmail-get-active))
412 (if (not (file-exists-p nnmbox-mbox-file))
413 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
414 (if (and nnmbox-mbox-buffer
415 (get-buffer nnmbox-mbox-buffer)
416 (buffer-name nnmbox-mbox-buffer)
418 (set-buffer nnmbox-mbox-buffer)
419 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
422 (let ((delim (concat "^" rmail-unix-mail-delimiter))
424 (set-buffer (setq nnmbox-mbox-buffer
425 (find-file-noselect nnmbox-mbox-file)))
426 (buffer-disable-undo (current-buffer))
427 (goto-char (point-min))
428 (while (re-search-forward delim nil t)
429 (setq start (match-beginning 0))
430 (if (not (search-forward "\nX-Gnus-Newsgroup: "
435 (re-search-forward delim nil t)
441 (narrow-to-region start end)
442 (nnmbox-save-mail))))
445 (defun nnmbox-get-new-mail (&optional group)
446 "Read new incoming mail."
447 (let ((spools (nnmail-get-spool-files group))
450 (if (or (not nnmbox-get-new-mail) (not nnmail-spool-file))
452 ;; We go through all the existing spool files and split the
456 (file-exists-p (car spools))
457 (> (nth 7 (file-attributes (car spools))) 0)
459 (and gnus-verbose-backends
460 (message "nnmbox: Reading incoming mail..."))
463 (car spools) (concat nnmbox-mbox-file "-Incoming")))
465 (let ((in-buf (nnmail-split-incoming
466 incoming 'nnmbox-save-mail t)))
467 (set-buffer nnmbox-mbox-buffer)
468 (goto-char (point-max))
469 (insert-buffer-substring in-buf)
470 (kill-buffer in-buf)))))
471 (setq spools (cdr spools)))
472 ;; If we did indeed read any incoming spools, we save all info.
473 (and (buffer-modified-p nnmbox-mbox-buffer)
475 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
476 (set-buffer nnmbox-mbox-buffer)
479 ;; The following has been commented away, just to make sure
480 ;; that nobody ever loses any mail. If you feel safe that
481 ;; nnfolder will never do anything strange, just remove those
482 ;; two semicolons, and avoid having lots of "Incoming*"
484 ;; (and (file-writable-p incoming) (delete-file incoming))
485 (setq incomings (cdr incomings))))))
490 ;;; nnmbox.el ends here