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)
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)
82 (let ((number (length sequence))
84 article art-string start stop)
85 (nnmbox-possibly-change-newsgroup newsgroup)
86 (if (stringp (car sequence))
89 (setq article (car sequence))
90 (setq art-string (nnmbox-article-string article))
91 (set-buffer nnmbox-mbox-buffer)
92 (if (or (search-forward art-string nil t)
93 (progn (goto-char (point-min))
94 (search-forward art-string nil t)))
99 (concat "^" rmail-unix-mail-delimiter) nil t)
101 (search-forward "\n\n" nil t)
102 (setq stop (1- (point)))
103 (set-buffer nntp-server-buffer)
104 (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 (set-buffer nntp-server-buffer)
124 (goto-char (point-min))
125 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
126 (replace-match " " t t))
129 (defun nnmbox-open-server (server &optional defs)
130 (nnheader-init-server-buffer)
131 (if (equal server nnmbox-current-server)
133 (if nnmbox-current-server
134 (setq nnmbox-server-alist
135 (cons (list nnmbox-current-server
136 (nnheader-save-variables nnmbox-server-variables))
137 nnmbox-server-alist)))
138 (let ((state (assoc server nnmbox-server-alist)))
141 (nnheader-restore-variables (nth 1 state))
142 (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
143 (nnheader-set-init-variables nnmbox-server-variables defs)))
144 (setq nnmbox-current-server server)))
146 (defun nnmbox-close-server (&optional server)
149 (defun nnmbox-server-opened (&optional server)
150 (and (equal server nnmbox-current-server)
152 (buffer-name nnmbox-mbox-buffer)
154 (buffer-name nntp-server-buffer)))
156 (defun nnmbox-status-message (&optional server)
157 nnmbox-status-string)
159 (defun nnmbox-request-article (article &optional newsgroup server buffer)
160 (nnmbox-possibly-change-newsgroup newsgroup)
161 (if (stringp article)
164 (set-buffer nnmbox-mbox-buffer)
165 (goto-char (point-min))
166 (if (search-forward (nnmbox-article-string article) nil t)
168 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
171 (or (and (re-search-forward
172 (concat "^" rmail-unix-mail-delimiter) nil t)
174 (goto-char (point-max)))
176 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
177 (set-buffer nntp-server-buffer)
179 (insert-buffer-substring nnmbox-mbox-buffer start stop)
180 (goto-char (point-min))
181 (while (looking-at "From ")
183 (insert "X-From-Line: ")
187 (defun nnmbox-request-group (group &optional server dont-check)
189 (if (nnmbox-possibly-change-newsgroup group)
192 (nnmbox-get-new-mail group)
194 (set-buffer nntp-server-buffer)
196 (let ((active (assoc group nnmbox-group-alist)))
197 (insert (format "211 %d %d %d %s\n"
198 (1+ (- (cdr (car (cdr active)))
199 (car (car (cdr active)))))
200 (car (car (cdr active)))
201 (cdr (car (cdr active)))
205 (defun nnmbox-close-group (group &optional server)
208 (defun nnmbox-request-list (&optional server)
209 (if server (nnmbox-get-new-mail))
211 (or (nnmail-find-file nnmbox-active-file)
213 (setq nnmbox-group-alist (nnmail-get-active))
214 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
215 (nnmail-find-file nnmbox-active-file)))))
217 (defun nnmbox-request-newgroups (date &optional server)
218 (nnmbox-request-list server))
220 (defun nnmbox-request-list-newsgroups (&optional server)
221 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
224 (defun nnmbox-request-post (&optional server)
225 (mail-send-and-exit nil))
227 (defalias 'nnmbox-request-post-buffer 'nnmail-request-post-buffer)
229 (defun nnmbox-request-expire-articles
230 (articles newsgroup &optional server force)
231 (nnmbox-possibly-change-newsgroup newsgroup)
232 (let* ((days (or (and nnmail-expiry-wait-function
233 (funcall nnmail-expiry-wait-function newsgroup))
238 (set-buffer nnmbox-mbox-buffer)
240 (goto-char (point-min))
241 (if (search-forward (nnmbox-article-string (car articles)) nil t)
244 (> (nnmail-days-between
245 (current-time-string)
247 (point) (progn (end-of-line) (point))))
250 (and gnus-verbose-backends
251 (message "Deleting: %s" (car articles)))
252 (nnmbox-delete-mail))
253 (setq rest (cons (car articles) rest))))
254 (setq articles (cdr articles)))
256 ;; Find the lowest active article in this group.
257 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
258 (goto-char (point-min))
259 (while (and (not (search-forward
260 (nnmbox-article-string (car active)) nil t))
261 (<= (car active) (cdr active)))
262 (setcar active (1+ (car active)))
263 (goto-char (point-min))))
264 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
265 (nconc rest articles))))
267 (defun nnmbox-request-move-article
268 (article group server accept-form &optional last)
269 (nnmbox-possibly-change-newsgroup group)
270 (let ((buf (get-buffer-create " *nnmbox move*"))
273 (nnmbox-request-article article group server)
276 (buffer-disable-undo (current-buffer))
278 (insert-buffer-substring nntp-server-buffer)
279 (goto-char (point-min))
280 (while (re-search-forward
282 (save-excursion (search-forward "\n\n" nil t) (point)) t)
283 (delete-region (progn (beginning-of-line) (point))
284 (progn (forward-line 1) (point))))
285 (setq result (eval accept-form))
289 (set-buffer nnmbox-mbox-buffer)
290 (goto-char (point-min))
291 (if (search-forward (nnmbox-article-string article) nil t)
292 (nnmbox-delete-mail))
293 (and last (save-buffer))))
296 (defun nnmbox-request-accept-article (group &optional last)
297 (let ((buf (current-buffer))
299 (goto-char (point-min))
300 (if (looking-at "X-From-Line: ")
301 (replace-match "From ")
302 (insert "From nobody " (current-time-string) "\n"))
304 (nnmbox-request-list)
305 (setq nnmbox-group-alist (nnmail-get-active))
308 (goto-char (point-min))
309 (search-forward "\n\n" nil t)
311 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
312 (delete-region (point) (progn (forward-line 1) (point))))
313 (setq result (nnmbox-save-mail (and (stringp group) group))))
315 (set-buffer nnmbox-mbox-buffer)
316 (insert-buffer-substring buf)
317 (and last (save-buffer))
319 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
322 (defun nnmbox-request-replace-article (article group buffer)
323 (nnmbox-possibly-change-newsgroup group)
325 (set-buffer nnmbox-mbox-buffer)
326 (goto-char (point-min))
327 (if (not (search-forward (nnmbox-article-string article) nil t))
329 (nnmbox-delete-mail t t)
330 (insert-buffer-substring buffer)
335 ;;; Internal functions.
337 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
338 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
340 (defun nnmbox-delete-mail (&optional force leave-delim)
341 ;; Delete the current X-Gnus-Newsgroup line.
344 (progn (beginning-of-line) (point))
345 (progn (forward-line 1) (point))))
346 ;; Beginning of the article.
351 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
352 (if leave-delim (progn (forward-line 1) (point))
353 (match-beginning 0)))
356 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
358 (if (and (not (bobp)) leave-delim)
359 (progn (forward-line -2) (point))
360 (match-beginning 0)))
362 (goto-char (point-min))
363 ;; Only delete the article if no other groups owns it as well.
364 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
365 (delete-region (point-min) (point-max))))))
367 (defun nnmbox-possibly-change-newsgroup (newsgroup)
368 (if (or (not nnmbox-mbox-buffer)
369 (not (buffer-name nnmbox-mbox-buffer)))
371 (set-buffer (setq nnmbox-mbox-buffer
372 (find-file-noselect nnmbox-mbox-file)))
373 (buffer-disable-undo (current-buffer))))
374 (if (not nnmbox-group-alist)
376 (nnmbox-request-list)
377 (setq nnmbox-group-alist (nnmail-get-active))))
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)))))
413 (setcdr active (1+ (cdr active)))
416 (defun nnmbox-read-mbox ()
417 (nnmbox-request-list)
418 (setq nnmbox-group-alist (nnmail-get-active))
419 (if (not (file-exists-p nnmbox-mbox-file))
420 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
421 (if (and nnmbox-mbox-buffer
422 (buffer-name nnmbox-mbox-buffer)
424 (set-buffer nnmbox-mbox-buffer)
425 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
428 (let ((delim (concat "^" rmail-unix-mail-delimiter))
430 (set-buffer (setq nnmbox-mbox-buffer
431 (find-file-noselect nnmbox-mbox-file)))
432 (buffer-disable-undo (current-buffer))
433 (goto-char (point-min))
434 (while (re-search-forward delim nil t)
435 (setq start (match-beginning 0))
436 (if (not (search-forward "\nX-Gnus-Newsgroup: "
441 (re-search-forward delim nil t)
447 (narrow-to-region start end)
448 (nnmbox-save-mail))))
451 (defun nnmbox-get-new-mail (&optional group)
452 "Read new incoming mail."
453 (let* ((spools (nnmail-get-spool-files group))
457 (if (or (not nnmbox-get-new-mail) (not nnmail-spool-file))
459 ;; We go through all the existing spool files and split the
463 (file-exists-p (car spools))
464 (> (nth 7 (file-attributes (car spools))) 0)
466 (and gnus-verbose-backends
467 (message "nnmbox: Reading incoming mail..."))
470 (car spools) (concat nnmbox-mbox-file "-Incoming")))
472 (setq group (nnmail-get-split-group (car spools) group-in))
473 (let ((in-buf (nnmail-split-incoming
474 incoming 'nnmbox-save-mail t group)))
475 (set-buffer nnmbox-mbox-buffer)
476 (goto-char (point-max))
477 (insert-buffer-substring in-buf)
478 (kill-buffer in-buf)))))
479 (setq spools (cdr spools)))
480 ;; If we did indeed read any incoming spools, we save all info.
481 (and (buffer-modified-p nnmbox-mbox-buffer)
483 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
484 (set-buffer nnmbox-mbox-buffer)
486 (if incomings (run-hooks 'nnmail-read-incoming-hook))
488 (and nnmail-delete-incoming
489 (file-writable-p incoming)
490 (delete-file incoming))
491 (setq incomings (cdr incomings))))))
496 ;;; nnmbox.el ends here