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 1.0"
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)
88 (setq article (car sequence))
89 (setq art-string (nnmbox-article-string article))
90 (set-buffer nnmbox-mbox-buffer)
91 (if (or (search-forward art-string nil t)
92 (progn (goto-char (point-min))
93 (search-forward art-string nil t)))
98 (concat "^" rmail-unix-mail-delimiter) nil t)
100 (search-forward "\n\n" nil t)
101 (setq stop (1- (point)))
102 (set-buffer nntp-server-buffer)
103 (insert (format "221 %d Article retrieved.\n" article))
104 (insert-buffer-substring nnmbox-mbox-buffer start stop)
105 (goto-char (point-max))
107 (setq sequence (cdr sequence))
108 (setq count (1+ count))
109 (and (numberp nnmail-large-newsgroup)
110 (> number nnmail-large-newsgroup)
112 gnus-verbose-backends
113 (message "nnmbox: Receiving headers... %d%%"
114 (/ (* count 100) number))))
116 (and (numberp nnmail-large-newsgroup)
117 (> number nnmail-large-newsgroup)
118 gnus-verbose-backends
119 (message "nnmbox: Receiving headers...done"))
121 ;; Fold continuation lines.
122 (set-buffer nntp-server-buffer)
123 (goto-char (point-min))
124 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
125 (replace-match " " t t))
128 (defun nnmbox-open-server (server &optional defs)
129 (nnheader-init-server-buffer)
130 (if (equal server nnmbox-current-server)
132 (if nnmbox-current-server
133 (setq nnmbox-server-alist
134 (cons (list nnmbox-current-server
135 (nnheader-save-variables nnmbox-server-variables))
136 nnmbox-server-alist)))
137 (let ((state (assoc server nnmbox-server-alist)))
140 (nnheader-restore-variables (nth 1 state))
141 (setq nnmbox-server-alist (delq state nnmbox-server-alist)))
142 (nnheader-set-init-variables nnmbox-server-variables defs)))
143 (setq nnmbox-current-server server)))
145 (defun nnmbox-close-server (&optional server)
148 (defun nnmbox-server-opened (&optional server)
149 (and (equal server nnmbox-current-server)
151 (buffer-name nnmbox-mbox-buffer)
153 (buffer-name nntp-server-buffer)))
155 (defun nnmbox-status-message (&optional server)
156 nnmbox-status-string)
158 (defun nnmbox-request-article (article &optional newsgroup server buffer)
159 (nnmbox-possibly-change-newsgroup newsgroup)
161 (set-buffer nnmbox-mbox-buffer)
162 (goto-char (point-min))
163 (if (search-forward (nnmbox-article-string article) nil t)
165 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
168 (or (and (re-search-forward
169 (concat "^" rmail-unix-mail-delimiter) nil t)
171 (goto-char (point-max)))
173 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
174 (set-buffer nntp-server-buffer)
176 (insert-buffer-substring nnmbox-mbox-buffer start stop)
177 (goto-char (point-min))
178 (while (looking-at "From ")
180 (insert "X-From-Line: ")
182 (if (numberp article)
183 (cons nnmbox-current-group article)
184 (nnmbox-article-group-number)))))))
186 (defun nnmbox-request-group (group &optional server dont-check)
188 (if (nnmbox-possibly-change-newsgroup group)
192 (set-buffer nntp-server-buffer)
194 (let ((active (assoc group nnmbox-group-alist)))
195 (insert (format "211 %d %d %d %s\n"
196 (1+ (- (cdr (car (cdr active)))
197 (car (car (cdr active)))))
198 (car (car (cdr active)))
199 (cdr (car (cdr active)))
203 (defun nnmbox-request-scan (&optional group server)
209 (set-buffer nnmbox-mbox-buffer)
211 nnmbox-mbox-file group
214 (let ((in-buf (current-buffer)))
215 (set-buffer nnmbox-mbox-buffer)
216 (goto-char (point-max))
217 (insert-buffer-substring in-buf))))))
219 (defun nnmbox-close-group (group &optional server)
222 (defun nnmbox-request-list (&optional server)
224 (or (nnmail-find-file nnmbox-active-file)
226 (setq nnmbox-group-alist (nnmail-get-active))
227 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
228 (nnmail-find-file nnmbox-active-file)))))
230 (defun nnmbox-request-newgroups (date &optional server)
231 (nnmbox-request-list server))
233 (defun nnmbox-request-list-newsgroups (&optional server)
234 (setq nnmbox-status-string "nnmbox: LIST NEWSGROUPS is not implemented.")
237 (defun nnmbox-request-post (&optional server)
238 (mail-send-and-exit nil))
240 (defun nnmbox-request-expire-articles
241 (articles newsgroup &optional server force)
242 (nnmbox-possibly-change-newsgroup newsgroup)
243 (let* ((days (or (and nnmail-expiry-wait-function
244 (funcall nnmail-expiry-wait-function newsgroup))
248 (nnmail-activate 'nnmbox)
251 (set-buffer nnmbox-mbox-buffer)
252 (while (and articles is-old)
253 (goto-char (point-min))
254 (if (search-forward (nnmbox-article-string (car articles)) nil t)
257 (> (nnmail-days-between
258 (current-time-string)
260 (point) (progn (end-of-line) (point))))
263 (and gnus-verbose-backends
264 (message "Deleting article %d in %s..."
265 (car articles) newsgroup))
266 (nnmbox-delete-mail))
267 (setq rest (cons (car articles) rest))))
268 (setq articles (cdr articles)))
270 ;; Find the lowest active article in this group.
271 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
272 (goto-char (point-min))
273 (while (and (not (search-forward
274 (nnmbox-article-string (car active)) nil t))
275 (<= (car active) (cdr active)))
276 (setcar active (1+ (car active)))
277 (goto-char (point-min))))
278 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
279 (nconc rest articles))))
281 (defun nnmbox-request-move-article
282 (article group server accept-form &optional last)
283 (nnmbox-possibly-change-newsgroup group)
284 (let ((buf (get-buffer-create " *nnmbox move*"))
287 (nnmbox-request-article article group server)
290 (buffer-disable-undo (current-buffer))
292 (insert-buffer-substring nntp-server-buffer)
293 (goto-char (point-min))
294 (while (re-search-forward
296 (save-excursion (search-forward "\n\n" nil t) (point)) t)
297 (delete-region (progn (beginning-of-line) (point))
298 (progn (forward-line 1) (point))))
299 (setq result (eval accept-form))
303 (set-buffer nnmbox-mbox-buffer)
304 (goto-char (point-min))
305 (if (search-forward (nnmbox-article-string article) nil t)
306 (nnmbox-delete-mail))
307 (and last (save-buffer))))
310 (defun nnmbox-request-accept-article (group &optional last)
311 (let ((buf (current-buffer))
313 (goto-char (point-min))
314 (if (looking-at "X-From-Line: ")
315 (replace-match "From ")
316 (insert "From nobody " (current-time-string) "\n"))
318 (nnmail-activate 'nnmbox)
321 (goto-char (point-min))
322 (search-forward "\n\n" nil t)
324 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
325 (delete-region (point) (progn (forward-line 1) (point))))
326 (setq result (nnmbox-save-mail (and (stringp group) group))))
328 (set-buffer nnmbox-mbox-buffer)
329 (insert-buffer-substring buf)
330 (and last (save-buffer))
332 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
335 (defun nnmbox-request-replace-article (article group buffer)
336 (nnmbox-possibly-change-newsgroup group)
338 (set-buffer nnmbox-mbox-buffer)
339 (goto-char (point-min))
340 (if (not (search-forward (nnmbox-article-string article) nil t))
342 (nnmbox-delete-mail t t)
343 (insert-buffer-substring buffer)
348 ;;; Internal functions.
350 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
351 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
353 (defun nnmbox-delete-mail (&optional force leave-delim)
354 ;; Delete the current X-Gnus-Newsgroup line.
357 (progn (beginning-of-line) (point))
358 (progn (forward-line 1) (point))))
359 ;; Beginning of the article.
364 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
365 (if leave-delim (progn (forward-line 1) (point))
366 (match-beginning 0)))
369 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
371 (if (and (not (bobp)) leave-delim)
372 (progn (forward-line -2) (point))
373 (match-beginning 0)))
375 (goto-char (point-min))
376 ;; Only delete the article if no other groups owns it as well.
377 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
378 (delete-region (point-min) (point-max))))))
380 (defun nnmbox-possibly-change-newsgroup (newsgroup)
381 (if (or (not nnmbox-mbox-buffer)
382 (not (buffer-name nnmbox-mbox-buffer)))
384 (set-buffer (setq nnmbox-mbox-buffer
385 (nnheader-find-file-noselect
386 nnmbox-mbox-file nil 'raw)))
387 (buffer-disable-undo (current-buffer))))
388 (if (not nnmbox-group-alist)
389 (nnmail-activate 'nnmbox))
391 (if (assoc newsgroup nnmbox-group-alist)
392 (setq nnmbox-current-group newsgroup))))
394 (defun nnmbox-article-string (article)
395 (if (numberp article)
396 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
397 (int-to-string article) " ")
398 (concat "\nMessage-ID: " article)))
400 (defun nnmbox-article-group-number ()
402 (goto-char (point-min))
403 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
405 (cons (buffer-substring (match-beginning 1) (match-end 1))
407 (buffer-substring (match-beginning 2) (match-end 2)))))))
409 (defun nnmbox-save-mail (&optional group)
410 "Called narrowed to an article."
411 (let* ((nnmail-split-methods
412 (if group (list (list group "")) nnmail-split-methods))
413 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
414 (nnmail-insert-lines)
415 (nnmail-insert-xref group-art)
416 (nnmbox-insert-newsgroup-line group-art)
417 (run-hooks 'nnml-prepare-save-mail-hook)
420 (defun nnmbox-insert-newsgroup-line (group-art)
422 (goto-char (point-min))
423 (if (search-forward "\n\n" nil t)
427 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
428 (car (car group-art)) (cdr (car group-art))
429 (current-time-string)))
430 (setq group-art (cdr group-art)))))
433 (defun nnmbox-active-number (group)
434 ;; Find the next article number in GROUP.
435 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
437 (setcdr active (1+ (cdr active)))
438 ;; This group is new, so we create a new entry for it.
439 ;; This might be a bit naughty... creating groups on the drop of
440 ;; a hat, but I don't know...
441 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
442 nnmbox-group-alist)))
445 (defun nnmbox-read-mbox ()
446 (nnmail-activate 'nnmbox)
447 (if (not (file-exists-p nnmbox-mbox-file))
448 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
449 (if (and nnmbox-mbox-buffer
450 (buffer-name nnmbox-mbox-buffer)
452 (set-buffer nnmbox-mbox-buffer)
453 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
456 (let ((delim (concat "^" rmail-unix-mail-delimiter))
458 (set-buffer (setq nnmbox-mbox-buffer
459 (nnheader-find-file-noselect
460 nnmbox-mbox-file nil 'raw)))
461 (buffer-disable-undo (current-buffer))
462 (goto-char (point-min))
463 (while (re-search-forward delim nil t)
464 (setq start (match-beginning 0))
465 (if (not (search-forward "\nX-Gnus-Newsgroup: "
470 (re-search-forward delim nil t)
476 (narrow-to-region start end)
477 (nnmbox-save-mail))))
482 ;;; nnmbox.el ends here