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 (equal server nnmbox-current-server))
148 (defun nnmbox-status-message (&optional server)
149 nnmbox-status-string)
151 (defun nnmbox-request-article (article &optional newsgroup server buffer)
152 (nnmbox-possibly-change-newsgroup newsgroup)
153 (if (stringp article)
156 (set-buffer nnmbox-mbox-buffer)
158 (if (search-forward (nnmbox-article-string article) nil t)
160 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
163 (or (and (re-search-forward
164 (concat "^" rmail-unix-mail-delimiter) nil t)
166 (goto-char (point-max)))
168 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
169 (set-buffer nntp-server-buffer)
171 (insert-buffer-substring nnmbox-mbox-buffer start stop)
172 (goto-char (point-min))
173 (while (looking-at "From ")
175 (insert "X-From-Line: ")
179 (defun nnmbox-request-group (group &optional server dont-check)
181 (if (nnmbox-possibly-change-newsgroup group)
184 (nnmbox-get-new-mail)
186 (set-buffer nntp-server-buffer)
188 (let ((active (assoc group nnmbox-group-alist)))
189 (insert (format "211 %d %d %d %s\n"
190 (1+ (- (cdr (car (cdr active)))
191 (car (car (cdr active)))))
192 (car (car (cdr active)))
193 (cdr (car (cdr active)))
197 (defun nnmbox-close-group (group &optional server)
200 (defun nnmbox-request-list (&optional server)
201 (if server (nnmbox-get-new-mail))
202 (or (nnmail-find-file nnmbox-active-file)
204 (setq nnmbox-group-alist (nnmail-get-active))
205 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
206 (nnmail-find-file nnmbox-active-file))))
208 (defun nnmbox-request-newgroups (date &optional server)
209 (nnmbox-request-list server))
211 (defun nnmbox-request-list-newsgroups (&optional server)
212 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
215 (defun nnmbox-request-post (&optional server)
216 (mail-send-and-exit nil))
218 (fset 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
220 (defun nnmbox-request-expire-articles
221 (articles newsgroup &optional server force)
222 (nnmbox-possibly-change-newsgroup newsgroup)
223 (let* ((days (or (and nnmail-expiry-wait-function
224 (funcall nnmail-expiry-wait-function newsgroup))
228 (set-buffer nnmbox-mbox-buffer)
231 (if (search-forward (nnmbox-article-string (car articles)) nil t)
233 (> (nnmail-days-between
234 (current-time-string)
236 (point) (progn (end-of-line) (point))))
239 (and gnus-verbose-backends
240 (message "Deleting: %s" (car articles)))
241 (nnmbox-delete-mail))
242 (setq rest (cons (car articles) rest))))
243 (setq articles (cdr articles)))
245 ;; Find the lowest active article in this group.
246 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
247 (goto-char (point-min))
248 (while (not (search-forward
249 (nnmbox-article-string (car active)) nil t))
250 (setcar (car active) (1+ (car active)))
251 (goto-char (point-min))))
252 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
255 (defun nnmbox-request-move-article
256 (article group server accept-form &optional last)
257 (nnmbox-possibly-change-newsgroup group)
258 (let ((buf (get-buffer-create " *nnmbox move*"))
261 (nnmbox-request-article article group server)
264 (buffer-disable-undo (current-buffer))
266 (insert-buffer-substring nntp-server-buffer)
267 (goto-char (point-min))
268 (while (re-search-forward
270 (save-excursion (search-forward "\n\n" nil t) (point)) t)
271 (delete-region (progn (beginning-of-line) (point))
272 (progn (forward-line 1) (point))))
273 (setq result (eval accept-form))
277 (set-buffer nnmbox-mbox-buffer)
279 (if (search-forward (nnmbox-article-string article) nil t)
280 (nnmbox-delete-mail))
281 (and last (save-buffer))))
284 (defun nnmbox-request-accept-article (group &optional last)
285 (let ((buf (current-buffer))
287 (goto-char (point-min))
288 (if (looking-at "X-From-Line: ")
289 (replace-match "From ")
290 (insert "From nobody " (current-time-string) "\n"))
292 (nnmbox-request-list)
293 (setq nnmbox-group-alist (nnmail-get-active))
296 (goto-char (point-min))
297 (search-forward "\n\n" nil t)
299 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
300 (delete-region (point) (progn (forward-line 1) (point))))
301 (setq result (nnmbox-save-mail (and (stringp group) group))))
303 (set-buffer nnmbox-mbox-buffer)
304 (insert-buffer-substring buf)
305 (and last (save-buffer))
307 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
310 (defun nnmbox-request-replace-article (article group buffer)
311 (nnmbox-possibly-change-newsgroup group)
313 (set-buffer nnmbox-mbox-buffer)
315 (if (not (search-forward (nnmbox-article-string article) nil t))
317 (nnmbox-delete-mail t t)
318 (insert-buffer-substring buffer)
323 ;;; Internal functions.
325 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
326 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
328 (defun nnmbox-delete-mail (&optional force leave-delim)
329 ;; Delete the current X-Gnus-Newsgroup line.
332 (progn (beginning-of-line) (point))
333 (progn (forward-line 1) (point))))
334 ;; Beginning of the article.
339 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
340 (if leave-delim (progn (forward-line 1) (point))
341 (match-beginning 0)))
344 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
346 (if (and (not (bobp)) leave-delim)
347 (progn (forward-line -2) (point))
348 (match-beginning 0)))
350 (goto-char (point-min))
351 ;; Only delete the article if no other groups owns it as well.
352 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
353 (delete-region (point-min) (point-max))))))
355 (defun nnmbox-possibly-change-newsgroup (newsgroup)
356 (if (not (get-buffer nnmbox-mbox-buffer))
358 (set-buffer (setq nnmbox-mbox-buffer
359 (find-file-noselect nnmbox-mbox-file)))
360 (buffer-disable-undo (current-buffer))))
361 (if (not nnmbox-group-alist)
363 (nnmbox-request-list)
364 (setq nnmbox-group-alist (nnmail-get-active))))
366 (if (assoc newsgroup nnmbox-group-alist)
367 (setq nnmbox-current-group newsgroup))))
369 (defun nnmbox-article-string (article)
370 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
371 (int-to-string article) " "))
373 (defun nnmbox-save-mail (&optional group)
374 "Called narrowed to an article."
375 (let* ((nnmail-split-methods
376 (if group (list (list group "")) nnmail-split-methods))
377 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
378 (nnmail-insert-lines)
379 (nnmail-insert-xref group-art)
380 (nnmbox-insert-newsgroup-line group-art)
383 (defun nnmbox-insert-newsgroup-line (group-art)
385 (goto-char (point-min))
386 (if (search-forward "\n\n" nil t)
390 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
391 (car (car group-art)) (cdr (car group-art))
392 (current-time-string)))
393 (setq group-art (cdr group-art)))))
396 (defun nnmbox-active-number (group)
397 "Find the next article number in GROUP."
398 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
399 (setcdr active (1+ (cdr active)))
402 (defun nnmbox-read-mbox ()
403 (nnmbox-request-list)
404 (setq nnmbox-group-alist (nnmail-get-active))
405 (if (not (file-exists-p nnmbox-mbox-file))
406 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
407 (if (and nnmbox-mbox-buffer
408 (get-buffer nnmbox-mbox-buffer)
409 (buffer-name nnmbox-mbox-buffer)
411 (set-buffer nnmbox-mbox-buffer)
412 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
415 (let ((delim (concat "^" rmail-unix-mail-delimiter))
417 (set-buffer (setq nnmbox-mbox-buffer
418 (find-file-noselect nnmbox-mbox-file)))
419 (buffer-disable-undo (current-buffer))
420 (goto-char (point-min))
421 (while (re-search-forward delim nil t)
422 (setq start (match-beginning 0))
423 (if (not (search-forward "\nX-Gnus-Newsgroup: "
428 (re-search-forward delim nil t)
434 (narrow-to-region start end)
435 (nnmbox-save-mail))))
438 (defun nnmbox-get-new-mail ()
441 (if (and nnmail-spool-file nnmbox-get-new-mail
442 (file-exists-p nnmail-spool-file)
443 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
445 (and gnus-verbose-backends
446 (message "nnmbox: Reading incoming mail..."))
448 (nnmail-move-inbox nnmail-spool-file
449 (concat nnmbox-mbox-file "-Incoming")))
451 (let ((in-buf (nnmail-split-incoming
452 incoming 'nnmbox-save-mail t)))
453 (set-buffer nnmbox-mbox-buffer)
454 (goto-char (point-max))
455 (insert-buffer-substring in-buf)
456 (kill-buffer in-buf)))
457 (run-hooks 'nnmail-read-incoming-hook)
458 (and gnus-verbose-backends
459 (message "nnmbox: Reading incoming mail...done"))))
460 (and (buffer-modified-p nnmbox-mbox-buffer)
462 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
463 (set-buffer nnmbox-mbox-buffer)
466 ; (delete-file incoming))
471 ;;; nnmbox.el ends here