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.")
46 (defconst nnmbox-version "nnmbox 0.1"
49 (defvar nnmbox-current-group nil
50 "Current nnmbox news group directory.")
52 (defconst nnmbox-mbox-buffer " *nnmbox mbox buffer*")
54 (defvar nnmbox-status-string "")
56 (defvar nnmbox-group-alist nil)
60 (defvar nnmbox-current-server nil)
61 (defvar nnmbox-server-alist nil)
62 (defvar nnmbox-server-variables
64 (list 'nnmbox-mbox-file nnmbox-mbox-file)
65 (list 'nnmbox-active-file nnmbox-active-file)
66 (list 'nnmbox-get-new-mail nnmbox-get-new-mail)
67 '(nnmbox-current-group nil)
68 '(nnmbox-status-string "")
69 '(nnmbox-group-alist nil)))
73 ;;; Interface functions
75 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server)
77 (set-buffer nntp-server-buffer)
80 (number (length sequence))
82 beg article art-string start stop)
83 (nnmbox-possibly-change-newsgroup newsgroup)
85 (setq article (car sequence))
86 (setq art-string (nnmbox-article-string article))
87 (set-buffer nnmbox-mbox-buffer)
88 (if (or (search-forward art-string nil t)
90 (search-forward art-string nil t)))
95 (concat "^" rmail-unix-mail-delimiter) nil t)
97 (search-forward "\n\n" nil t)
98 (setq stop (1- (point)))
99 (set-buffer nntp-server-buffer)
100 (insert (format "221 %d Article retrieved.\n" article))
102 (insert-buffer-substring nnmbox-mbox-buffer start stop)
103 (goto-char (point-max))
105 (setq sequence (cdr sequence))
106 (setq count (1+ count))
107 (and (numberp nnmail-large-newsgroup)
108 (> number nnmail-large-newsgroup)
110 gnus-verbose-backends
111 (message "nnmbox: Receiving headers... %d%%"
112 (/ (* count 100) number))))
114 (and (numberp nnmail-large-newsgroup)
115 (> number nnmail-large-newsgroup)
116 gnus-verbose-backends
117 (message "nnmbox: Receiving headers... done"))
119 ;; Fold continuation lines.
121 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
122 (replace-match " " t t))
125 (defun nnmbox-open-server (server &optional defs)
126 (nnheader-init-server-buffer)
127 (if (equal server nnmbox-current-server)
129 (if nnmbox-current-server
130 (setq nnmbox-server-alist
131 (cons (list nnmbox-current-server
132 (nnheader-save-variables nnmbox-server-variables))
133 nnmbox-server-alist)))
134 (let ((state (assoc server nnmbox-server-alist)))
137 (nnheader-restore-variables (nth 1 state))
138 (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
139 (nnheader-set-init-variables nnmbox-server-variables defs)))
140 (setq nnmbox-current-server server)))
142 (defun nnmbox-close-server (&optional server)
145 (defun nnmbox-server-opened (&optional server)
146 (and (equal server nnmbox-current-server)
148 (buffer-name nntp-server-buffer)))
150 (defun nnmbox-status-message (&optional server)
151 nnmbox-status-string)
153 (defun nnmbox-request-article (article &optional newsgroup server buffer)
154 (nnmbox-possibly-change-newsgroup newsgroup)
155 (if (stringp article)
158 (set-buffer nnmbox-mbox-buffer)
160 (if (search-forward (nnmbox-article-string article) nil t)
162 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
165 (or (and (re-search-forward
166 (concat "^" rmail-unix-mail-delimiter) nil t)
168 (goto-char (point-max)))
170 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
171 (set-buffer nntp-server-buffer)
173 (insert-buffer-substring nnmbox-mbox-buffer start stop)
174 (goto-char (point-min))
175 (while (looking-at "From ")
177 (insert "X-From-Line: ")
181 (defun nnmbox-request-group (group &optional server dont-check)
183 (if (nnmbox-possibly-change-newsgroup group)
186 (nnmbox-get-new-mail)
188 (set-buffer nntp-server-buffer)
190 (let ((active (assoc group nnmbox-group-alist)))
191 (insert (format "211 %d %d %d %s\n"
192 (1+ (- (cdr (car (cdr active)))
193 (car (car (cdr active)))))
194 (car (car (cdr active)))
195 (cdr (car (cdr active)))
199 (defun nnmbox-close-group (group &optional server)
202 (defun nnmbox-request-list (&optional server)
203 (if server (nnmbox-get-new-mail))
205 (or (nnmail-find-file nnmbox-active-file)
207 (setq nnmbox-group-alist (nnmail-get-active))
208 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
209 (nnmail-find-file nnmbox-active-file)))))
211 (defun nnmbox-request-newgroups (date &optional server)
212 (nnmbox-request-list server))
214 (defun nnmbox-request-list-newsgroups (&optional server)
215 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
218 (defun nnmbox-request-post (&optional server)
219 (mail-send-and-exit nil))
221 (fset 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
223 (defun nnmbox-request-expire-articles
224 (articles newsgroup &optional server force)
225 (nnmbox-possibly-change-newsgroup newsgroup)
226 (let* ((days (or (and nnmail-expiry-wait-function
227 (funcall nnmail-expiry-wait-function newsgroup))
231 (set-buffer nnmbox-mbox-buffer)
234 (if (search-forward (nnmbox-article-string (car articles)) nil t)
236 (> (nnmail-days-between
237 (current-time-string)
239 (point) (progn (end-of-line) (point))))
242 (and gnus-verbose-backends
243 (message "Deleting: %s" (car articles)))
244 (nnmbox-delete-mail))
245 (setq rest (cons (car articles) rest))))
246 (setq articles (cdr articles)))
248 ;; Find the lowest active article in this group.
249 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
250 (goto-char (point-min))
251 (while (not (search-forward
252 (nnmbox-article-string (car active)) nil t))
253 (setcar active (1+ (car active)))
254 (goto-char (point-min))))
255 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
258 (defun nnmbox-request-move-article
259 (article group server accept-form &optional last)
260 (nnmbox-possibly-change-newsgroup group)
261 (let ((buf (get-buffer-create " *nnmbox move*"))
264 (nnmbox-request-article article group server)
267 (buffer-disable-undo (current-buffer))
269 (insert-buffer-substring nntp-server-buffer)
270 (goto-char (point-min))
271 (while (re-search-forward
273 (save-excursion (search-forward "\n\n" nil t) (point)) t)
274 (delete-region (progn (beginning-of-line) (point))
275 (progn (forward-line 1) (point))))
276 (setq result (eval accept-form))
280 (set-buffer nnmbox-mbox-buffer)
282 (if (search-forward (nnmbox-article-string article) nil t)
283 (nnmbox-delete-mail))
284 (and last (save-buffer))))
287 (defun nnmbox-request-accept-article (group &optional last)
288 (let ((buf (current-buffer))
290 (goto-char (point-min))
291 (if (looking-at "X-From-Line: ")
292 (replace-match "From ")
293 (insert "From nobody " (current-time-string) "\n"))
295 (nnmbox-request-list)
296 (setq nnmbox-group-alist (nnmail-get-active))
299 (goto-char (point-min))
300 (search-forward "\n\n" nil t)
302 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
303 (delete-region (point) (progn (forward-line 1) (point))))
304 (setq result (nnmbox-save-mail (and (stringp group) group))))
306 (set-buffer nnmbox-mbox-buffer)
307 (insert-buffer-substring buf)
308 (and last (save-buffer))
310 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
313 (defun nnmbox-request-replace-article (article group buffer)
314 (nnmbox-possibly-change-newsgroup group)
316 (set-buffer nnmbox-mbox-buffer)
318 (if (not (search-forward (nnmbox-article-string article) nil t))
320 (nnmbox-delete-mail t t)
321 (insert-buffer-substring buffer)
326 ;;; Internal functions.
328 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
329 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
331 (defun nnmbox-delete-mail (&optional force leave-delim)
332 ;; Delete the current X-Gnus-Newsgroup line.
335 (progn (beginning-of-line) (point))
336 (progn (forward-line 1) (point))))
337 ;; Beginning of the article.
342 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
343 (if leave-delim (progn (forward-line 1) (point))
344 (match-beginning 0)))
347 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
349 (if (and (not (bobp)) leave-delim)
350 (progn (forward-line -2) (point))
351 (match-beginning 0)))
353 (goto-char (point-min))
354 ;; Only delete the article if no other groups owns it as well.
355 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
356 (delete-region (point-min) (point-max))))))
358 (defun nnmbox-possibly-change-newsgroup (newsgroup)
359 (if (not (get-buffer nnmbox-mbox-buffer))
361 (set-buffer (setq nnmbox-mbox-buffer
362 (find-file-noselect nnmbox-mbox-file)))
363 (buffer-disable-undo (current-buffer))))
364 (if (not nnmbox-group-alist)
366 (nnmbox-request-list)
367 (setq nnmbox-group-alist (nnmail-get-active))))
369 (if (assoc newsgroup nnmbox-group-alist)
370 (setq nnmbox-current-group newsgroup))))
372 (defun nnmbox-article-string (article)
373 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
374 (int-to-string article) " "))
376 (defun nnmbox-save-mail (&optional group)
377 "Called narrowed to an article."
378 (let* ((nnmail-split-methods
379 (if group (list (list group "")) nnmail-split-methods))
380 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
381 (nnmail-insert-lines)
382 (nnmail-insert-xref group-art)
383 (nnmbox-insert-newsgroup-line group-art)
386 (defun nnmbox-insert-newsgroup-line (group-art)
388 (goto-char (point-min))
389 (if (search-forward "\n\n" nil t)
393 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
394 (car (car group-art)) (cdr (car group-art))
395 (current-time-string)))
396 (setq group-art (cdr group-art)))))
399 (defun nnmbox-active-number (group)
400 "Find the next article number in GROUP."
401 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
402 (setcdr active (1+ (cdr active)))
405 (defun nnmbox-read-mbox ()
406 (nnmbox-request-list)
407 (setq nnmbox-group-alist (nnmail-get-active))
408 (if (not (file-exists-p nnmbox-mbox-file))
409 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
410 (if (and nnmbox-mbox-buffer
411 (get-buffer nnmbox-mbox-buffer)
412 (buffer-name nnmbox-mbox-buffer)
414 (set-buffer nnmbox-mbox-buffer)
415 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
418 (let ((delim (concat "^" rmail-unix-mail-delimiter))
420 (set-buffer (setq nnmbox-mbox-buffer
421 (find-file-noselect nnmbox-mbox-file)))
422 (buffer-disable-undo (current-buffer))
423 (goto-char (point-min))
424 (while (re-search-forward delim nil t)
425 (setq start (match-beginning 0))
426 (if (not (search-forward "\nX-Gnus-Newsgroup: "
431 (re-search-forward delim nil t)
437 (narrow-to-region start end)
438 (nnmbox-save-mail))))
441 (defun nnmbox-get-new-mail ()
444 (if (and nnmail-spool-file nnmbox-get-new-mail
445 (file-exists-p nnmail-spool-file)
446 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
448 (and gnus-verbose-backends
449 (message "nnmbox: Reading incoming mail..."))
451 (nnmail-move-inbox nnmail-spool-file
452 (concat nnmbox-mbox-file "-Incoming")))
454 (let ((in-buf (nnmail-split-incoming
455 incoming 'nnmbox-save-mail t)))
456 (set-buffer nnmbox-mbox-buffer)
457 (goto-char (point-max))
458 (insert-buffer-substring in-buf)
459 (kill-buffer in-buf)))
460 (run-hooks 'nnmail-read-incoming-hook)
461 (and gnus-verbose-backends
462 (message "nnmbox: Reading incoming mail...done"))))
463 (and (buffer-modified-p nnmbox-mbox-buffer)
465 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
466 (set-buffer nnmbox-mbox-buffer)
469 ; (delete-file incoming))
474 ;;; nnmbox.el ends here