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 users 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, nnml 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))
204 (or (nnmail-find-file nnmbox-active-file)
206 (setq nnmbox-group-alist (nnmail-get-active))
207 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
208 (nnmail-find-file nnmbox-active-file))))
210 (defun nnmbox-request-newgroups (date &optional server)
211 (nnmbox-request-list server))
213 (defun nnmbox-request-list-newsgroups (&optional server)
214 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
217 (defun nnmbox-request-post (&optional server)
218 (mail-send-and-exit nil))
220 (fset 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
222 (defun nnmbox-request-expire-articles
223 (articles newsgroup &optional server force)
224 (nnmbox-possibly-change-newsgroup newsgroup)
225 (let* ((days (or (and nnmail-expiry-wait-function
226 (funcall nnmail-expiry-wait-function newsgroup))
230 (set-buffer nnmbox-mbox-buffer)
233 (if (search-forward (nnmbox-article-string (car articles)) nil t)
235 (> (nnmail-days-between
236 (current-time-string)
238 (point) (progn (end-of-line) (point))))
241 (and gnus-verbose-backends
242 (message "Deleting: %s" (car articles)))
243 (nnmbox-delete-mail))
244 (setq rest (cons (car articles) rest))))
245 (setq articles (cdr articles)))
247 ;; Find the lowest active article in this group.
248 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
249 (goto-char (point-min))
250 (while (not (search-forward
251 (nnmbox-article-string (car active)) nil t))
252 (setcar active (1+ (car active)))
253 (goto-char (point-min))))
254 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
257 (defun nnmbox-request-move-article
258 (article group server accept-form &optional last)
259 (nnmbox-possibly-change-newsgroup group)
260 (let ((buf (get-buffer-create " *nnmbox move*"))
263 (nnmbox-request-article article group server)
266 (buffer-disable-undo (current-buffer))
268 (insert-buffer-substring nntp-server-buffer)
269 (goto-char (point-min))
270 (while (re-search-forward
272 (save-excursion (search-forward "\n\n" nil t) (point)) t)
273 (delete-region (progn (beginning-of-line) (point))
274 (progn (forward-line 1) (point))))
275 (setq result (eval accept-form))
279 (set-buffer nnmbox-mbox-buffer)
281 (if (search-forward (nnmbox-article-string article) nil t)
282 (nnmbox-delete-mail))
283 (and last (save-buffer))))
286 (defun nnmbox-request-accept-article (group &optional last)
287 (let ((buf (current-buffer))
289 (goto-char (point-min))
290 (if (looking-at "X-From-Line: ")
291 (replace-match "From ")
292 (insert "From nobody " (current-time-string) "\n"))
294 (nnmbox-request-list)
295 (setq nnmbox-group-alist (nnmail-get-active))
298 (goto-char (point-min))
299 (search-forward "\n\n" nil t)
301 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
302 (delete-region (point) (progn (forward-line 1) (point))))
303 (setq result (nnmbox-save-mail (and (stringp group) group))))
305 (set-buffer nnmbox-mbox-buffer)
306 (insert-buffer-substring buf)
307 (and last (save-buffer))
309 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
312 (defun nnmbox-request-replace-article (article group buffer)
313 (nnmbox-possibly-change-newsgroup group)
315 (set-buffer nnmbox-mbox-buffer)
317 (if (not (search-forward (nnmbox-article-string article) nil t))
319 (nnmbox-delete-mail t t)
320 (insert-buffer-substring buffer)
325 ;;; Internal functions.
327 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
328 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
330 (defun nnmbox-delete-mail (&optional force leave-delim)
331 ;; Delete the current X-Gnus-Newsgroup line.
334 (progn (beginning-of-line) (point))
335 (progn (forward-line 1) (point))))
336 ;; Beginning of the article.
341 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
342 (if leave-delim (progn (forward-line 1) (point))
343 (match-beginning 0)))
346 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
348 (if (and (not (bobp)) leave-delim)
349 (progn (forward-line -2) (point))
350 (match-beginning 0)))
352 (goto-char (point-min))
353 ;; Only delete the article if no other groups owns it as well.
354 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
355 (delete-region (point-min) (point-max))))))
357 (defun nnmbox-possibly-change-newsgroup (newsgroup)
358 (if (not (get-buffer nnmbox-mbox-buffer))
360 (set-buffer (setq nnmbox-mbox-buffer
361 (find-file-noselect nnmbox-mbox-file)))
362 (buffer-disable-undo (current-buffer))))
363 (if (not nnmbox-group-alist)
365 (nnmbox-request-list)
366 (setq nnmbox-group-alist (nnmail-get-active))))
368 (if (assoc newsgroup nnmbox-group-alist)
369 (setq nnmbox-current-group newsgroup))))
371 (defun nnmbox-article-string (article)
372 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
373 (int-to-string article) " "))
375 (defun nnmbox-save-mail (&optional group)
376 "Called narrowed to an article."
377 (let* ((nnmail-split-methods
378 (if group (list (list group "")) nnmail-split-methods))
379 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
380 (nnmail-insert-lines)
381 (nnmail-insert-xref group-art)
382 (nnmbox-insert-newsgroup-line group-art)
385 (defun nnmbox-insert-newsgroup-line (group-art)
387 (goto-char (point-min))
388 (if (search-forward "\n\n" nil t)
392 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
393 (car (car group-art)) (cdr (car group-art))
394 (current-time-string)))
395 (setq group-art (cdr group-art)))))
398 (defun nnmbox-active-number (group)
399 "Find the next article number in GROUP."
400 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
401 (setcdr active (1+ (cdr active)))
404 (defun nnmbox-read-mbox ()
405 (nnmbox-request-list)
406 (setq nnmbox-group-alist (nnmail-get-active))
407 (if (not (file-exists-p nnmbox-mbox-file))
408 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
409 (if (and nnmbox-mbox-buffer
410 (get-buffer nnmbox-mbox-buffer)
411 (buffer-name nnmbox-mbox-buffer)
413 (set-buffer nnmbox-mbox-buffer)
414 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
417 (let ((delim (concat "^" rmail-unix-mail-delimiter))
419 (set-buffer (setq nnmbox-mbox-buffer
420 (find-file-noselect nnmbox-mbox-file)))
421 (buffer-disable-undo (current-buffer))
422 (goto-char (point-min))
423 (while (re-search-forward delim nil t)
424 (setq start (match-beginning 0))
425 (if (not (search-forward "\nX-Gnus-Newsgroup: "
430 (re-search-forward delim nil t)
436 (narrow-to-region start end)
437 (nnmbox-save-mail))))
440 (defun nnmbox-get-new-mail ()
443 (if (and nnmail-spool-file nnmbox-get-new-mail
444 (file-exists-p nnmail-spool-file)
445 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
447 (and gnus-verbose-backends
448 (message "nnmbox: Reading incoming mail..."))
450 (nnmail-move-inbox nnmail-spool-file
451 (concat nnmbox-mbox-file "-Incoming")))
453 (let ((in-buf (nnmail-split-incoming
454 incoming 'nnmbox-save-mail t)))
455 (set-buffer nnmbox-mbox-buffer)
456 (goto-char (point-max))
457 (insert-buffer-substring in-buf)
458 (kill-buffer in-buf)))
459 (run-hooks 'nnmail-read-incoming-hook)
460 (and gnus-verbose-backends
461 (message "nnmbox: Reading incoming mail...done"))))
462 (and (buffer-modified-p nnmbox-mbox-buffer)
464 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
465 (set-buffer nnmbox-mbox-buffer)
468 ; (delete-file incoming))
473 ;;; nnmbox.el ends here