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)
60 (defvar nnmbox-active-timestamp nil)
64 (defvar nnmbox-current-server nil)
65 (defvar nnmbox-server-alist nil)
66 (defvar nnmbox-server-variables
68 (list 'nnmbox-mbox-file nnmbox-mbox-file)
69 (list 'nnmbox-active-file nnmbox-active-file)
70 (list 'nnmbox-get-new-mail nnmbox-get-new-mail)
71 '(nnmbox-current-group nil)
72 '(nnmbox-status-string "")
73 '(nnmbox-group-alist nil)))
77 ;;; Interface functions
79 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server)
81 (set-buffer nntp-server-buffer)
83 (let ((number (length sequence))
85 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))
106 (insert-buffer-substring nnmbox-mbox-buffer start stop)
107 (goto-char (point-max))
109 (setq sequence (cdr sequence))
110 (setq count (1+ count))
111 (and (numberp nnmail-large-newsgroup)
112 (> number nnmail-large-newsgroup)
114 gnus-verbose-backends
115 (message "nnmbox: Receiving headers... %d%%"
116 (/ (* count 100) number))))
118 (and (numberp nnmail-large-newsgroup)
119 (> number nnmail-large-newsgroup)
120 gnus-verbose-backends
121 (message "nnmbox: Receiving headers...done"))
123 ;; Fold continuation lines.
124 (set-buffer nntp-server-buffer)
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 (nnmail-activate 'nnmbox)
241 (set-buffer nnmbox-mbox-buffer)
242 (while (and articles is-old)
243 (goto-char (point-min))
244 (if (search-forward (nnmbox-article-string (car articles)) nil t)
247 (> (nnmail-days-between
248 (current-time-string)
250 (point) (progn (end-of-line) (point))))
253 (and gnus-verbose-backends
254 (message "Deleting article %s..." (car articles)))
255 (nnmbox-delete-mail))
256 (setq rest (cons (car articles) rest))))
257 (setq articles (cdr articles)))
259 ;; Find the lowest active article in this group.
260 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
261 (goto-char (point-min))
262 (while (and (not (search-forward
263 (nnmbox-article-string (car active)) nil t))
264 (<= (car active) (cdr active)))
265 (setcar active (1+ (car active)))
266 (goto-char (point-min))))
267 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
268 (nconc rest articles))))
270 (defun nnmbox-request-move-article
271 (article group server accept-form &optional last)
272 (nnmbox-possibly-change-newsgroup group)
273 (let ((buf (get-buffer-create " *nnmbox move*"))
276 (nnmbox-request-article article group server)
279 (buffer-disable-undo (current-buffer))
281 (insert-buffer-substring nntp-server-buffer)
282 (goto-char (point-min))
283 (while (re-search-forward
285 (save-excursion (search-forward "\n\n" nil t) (point)) t)
286 (delete-region (progn (beginning-of-line) (point))
287 (progn (forward-line 1) (point))))
288 (setq result (eval accept-form))
292 (set-buffer nnmbox-mbox-buffer)
293 (goto-char (point-min))
294 (if (search-forward (nnmbox-article-string article) nil t)
295 (nnmbox-delete-mail))
296 (and last (save-buffer))))
299 (defun nnmbox-request-accept-article (group &optional last)
300 (let ((buf (current-buffer))
302 (goto-char (point-min))
303 (if (looking-at "X-From-Line: ")
304 (replace-match "From ")
305 (insert "From nobody " (current-time-string) "\n"))
307 (nnmail-activate 'nnmbox)
310 (goto-char (point-min))
311 (search-forward "\n\n" nil t)
313 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
314 (delete-region (point) (progn (forward-line 1) (point))))
315 (setq result (nnmbox-save-mail (and (stringp group) group))))
317 (set-buffer nnmbox-mbox-buffer)
318 (insert-buffer-substring buf)
319 (and last (save-buffer))
321 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
324 (defun nnmbox-request-replace-article (article group buffer)
325 (nnmbox-possibly-change-newsgroup group)
327 (set-buffer nnmbox-mbox-buffer)
328 (goto-char (point-min))
329 (if (not (search-forward (nnmbox-article-string article) nil t))
331 (nnmbox-delete-mail t t)
332 (insert-buffer-substring buffer)
337 ;;; Internal functions.
339 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
340 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
342 (defun nnmbox-delete-mail (&optional force leave-delim)
343 ;; Delete the current X-Gnus-Newsgroup line.
346 (progn (beginning-of-line) (point))
347 (progn (forward-line 1) (point))))
348 ;; Beginning of the article.
353 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
354 (if leave-delim (progn (forward-line 1) (point))
355 (match-beginning 0)))
358 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
360 (if (and (not (bobp)) leave-delim)
361 (progn (forward-line -2) (point))
362 (match-beginning 0)))
364 (goto-char (point-min))
365 ;; Only delete the article if no other groups owns it as well.
366 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
367 (delete-region (point-min) (point-max))))))
369 (defun nnmbox-possibly-change-newsgroup (newsgroup)
370 (if (or (not nnmbox-mbox-buffer)
371 (not (buffer-name nnmbox-mbox-buffer)))
373 (set-buffer (setq nnmbox-mbox-buffer
374 (nnheader-find-file-noselect nnmbox-mbox-file)))
375 (buffer-disable-undo (current-buffer))))
376 (if (not nnmbox-group-alist)
377 (nnmail-activate 'nnmbox))
379 (if (assoc newsgroup nnmbox-group-alist)
380 (setq nnmbox-current-group newsgroup))))
382 (defun nnmbox-article-string (article)
383 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
384 (int-to-string article) " "))
386 (defun nnmbox-save-mail (&optional group)
387 "Called narrowed to an article."
388 (let* ((nnmail-split-methods
389 (if group (list (list group "")) nnmail-split-methods))
390 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
391 (nnmail-insert-lines)
392 (nnmail-insert-xref group-art)
393 (nnmbox-insert-newsgroup-line group-art)
394 (run-hooks 'nnml-prepare-save-mail-hook)
397 (defun nnmbox-insert-newsgroup-line (group-art)
399 (goto-char (point-min))
400 (if (search-forward "\n\n" nil t)
404 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
405 (car (car group-art)) (cdr (car group-art))
406 (current-time-string)))
407 (setq group-art (cdr group-art)))))
410 (defun nnmbox-active-number (group)
411 ;; Find the next article number in GROUP.
412 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
414 (setcdr active (1+ (cdr active)))
415 ;; This group is new, so we create a new entry for it.
416 ;; This might be a bit naughty... creating groups on the drop of
417 ;; a hat, but I don't know...
418 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
419 nnmbox-group-alist)))
422 (defun nnmbox-read-mbox ()
423 (nnmail-activate 'nnmbox)
424 (if (not (file-exists-p nnmbox-mbox-file))
425 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
426 (if (and nnmbox-mbox-buffer
427 (buffer-name nnmbox-mbox-buffer)
429 (set-buffer nnmbox-mbox-buffer)
430 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
433 (let ((delim (concat "^" rmail-unix-mail-delimiter))
435 (set-buffer (setq nnmbox-mbox-buffer
436 (nnheader-find-file-noselect nnmbox-mbox-file)))
437 (buffer-disable-undo (current-buffer))
438 (goto-char (point-min))
439 (while (re-search-forward delim nil t)
440 (setq start (match-beginning 0))
441 (if (not (search-forward "\nX-Gnus-Newsgroup: "
446 (re-search-forward delim nil t)
452 (narrow-to-region start end)
453 (nnmbox-save-mail))))
456 (defun nnmbox-get-new-mail (&optional group)
457 "Read new incoming mail."
458 (let* ((spools (nnmail-get-spool-files group))
462 (if (or (not nnmbox-get-new-mail) (not nnmail-spool-file))
464 ;; We go through all the existing spool files and split the
468 (file-exists-p (car spools))
469 (> (nth 7 (file-attributes (car spools))) 0)
471 (and gnus-verbose-backends
472 (message "nnmbox: Reading incoming mail..."))
475 (car spools) (concat nnmbox-mbox-file "-Incoming")))
477 (setq group (nnmail-get-split-group (car spools) group-in))
478 (let ((in-buf (nnmail-split-incoming
479 incoming 'nnmbox-save-mail t group)))
480 (set-buffer nnmbox-mbox-buffer)
481 (goto-char (point-max))
482 (insert-buffer-substring in-buf)
483 (kill-buffer in-buf)))))
484 (setq spools (cdr spools)))
485 ;; If we did indeed read any incoming spools, we save all info.
486 (and (buffer-modified-p nnmbox-mbox-buffer)
488 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
489 (set-buffer nnmbox-mbox-buffer)
491 (if incomings (run-hooks 'nnmail-read-incoming-hook))
493 (setq incoming (car incomings))
494 (and nnmail-delete-incoming
495 (file-exists-p incoming)
496 (file-writable-p incoming)
497 (delete-file incoming))
498 (setq incomings (cdr incomings))))))
503 ;;; nnmbox.el ends here