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 fetch-old)
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)
347 (defun nnmbox-request-delete-group (group &optional force server)
348 (nnmbox-possibly-change-newsgroup group)
349 ;; Delete all articles in GROUP.
351 () ; Don't delete the articles.
353 (set-buffer nnmbox-mbox-buffer)
354 (goto-char (point-min))
355 ;; Delete all articles in this group.
356 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
358 (while (search-forward ident nil t)
360 (nnmbox-delete-mail))
361 (and found (save-buffer)))))
362 ;; Remove the group from all structures.
363 (setq nnmbox-group-alist
364 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
365 nnmbox-current-group nil)
366 ;; Save the active file.
367 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
370 (defun nnmbox-request-rename-group (group new-name &optional server)
371 (nnmbox-possibly-change-newsgroup group)
373 (set-buffer nnmbox-mbox-buffer)
374 (goto-char (point-min))
375 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
376 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
378 (while (search-forward ident nil t)
379 (replace-match new-ident t t)
381 (and found (save-buffer))))
382 (let ((entry (assoc group nnmbox-group-alist)))
383 (and entry (setcar entry new-name))
384 (setq nnmbox-current-group nil)
385 ;; Save the new group alist.
386 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
390 ;;; Internal functions.
392 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
393 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
395 (defun nnmbox-delete-mail (&optional force leave-delim)
396 ;; Delete the current X-Gnus-Newsgroup line.
399 (progn (beginning-of-line) (point))
400 (progn (forward-line 1) (point))))
401 ;; Beginning of the article.
406 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
407 (if leave-delim (progn (forward-line 1) (point))
408 (match-beginning 0)))
411 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
413 (if (and (not (bobp)) leave-delim)
414 (progn (forward-line -2) (point))
415 (match-beginning 0)))
417 (goto-char (point-min))
418 ;; Only delete the article if no other groups owns it as well.
419 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
420 (delete-region (point-min) (point-max))))))
422 (defun nnmbox-possibly-change-newsgroup (newsgroup)
423 (if (or (not nnmbox-mbox-buffer)
424 (not (buffer-name nnmbox-mbox-buffer)))
426 (set-buffer (setq nnmbox-mbox-buffer
427 (nnheader-find-file-noselect
428 nnmbox-mbox-file nil 'raw)))
429 (buffer-disable-undo (current-buffer))))
430 (if (not nnmbox-group-alist)
431 (nnmail-activate 'nnmbox))
433 (if (assoc newsgroup nnmbox-group-alist)
434 (setq nnmbox-current-group newsgroup))))
436 (defun nnmbox-article-string (article)
437 (if (numberp article)
438 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
439 (int-to-string article) " ")
440 (concat "\nMessage-ID: " article)))
442 (defun nnmbox-article-group-number ()
444 (goto-char (point-min))
445 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
447 (cons (buffer-substring (match-beginning 1) (match-end 1))
449 (buffer-substring (match-beginning 2) (match-end 2)))))))
451 (defun nnmbox-save-mail (&optional group)
452 "Called narrowed to an article."
453 (let* ((nnmail-split-methods
454 (if group (list (list group "")) nnmail-split-methods))
455 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number))))
456 (nnmail-insert-lines)
457 (nnmail-insert-xref group-art)
458 (nnmbox-insert-newsgroup-line group-art)
459 (run-hooks 'nnmbox-prepare-save-mail-hook)
462 (defun nnmbox-insert-newsgroup-line (group-art)
464 (goto-char (point-min))
465 (if (search-forward "\n\n" nil t)
469 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
470 (car (car group-art)) (cdr (car group-art))
471 (current-time-string)))
472 (setq group-art (cdr group-art)))))
475 (defun nnmbox-active-number (group)
476 ;; Find the next article number in GROUP.
477 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
479 (setcdr active (1+ (cdr active)))
480 ;; This group is new, so we create a new entry for it.
481 ;; This might be a bit naughty... creating groups on the drop of
482 ;; a hat, but I don't know...
483 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
484 nnmbox-group-alist)))
487 (defun nnmbox-read-mbox ()
488 (nnmail-activate 'nnmbox)
489 (if (not (file-exists-p nnmbox-mbox-file))
490 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
491 (if (and nnmbox-mbox-buffer
492 (buffer-name nnmbox-mbox-buffer)
494 (set-buffer nnmbox-mbox-buffer)
495 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
498 (let ((delim (concat "^" rmail-unix-mail-delimiter))
500 (set-buffer (setq nnmbox-mbox-buffer
501 (nnheader-find-file-noselect
502 nnmbox-mbox-file nil 'raw)))
503 (buffer-disable-undo (current-buffer))
504 (goto-char (point-min))
505 (while (re-search-forward delim nil t)
506 (setq start (match-beginning 0))
507 (if (not (search-forward "\nX-Gnus-Newsgroup: "
512 (re-search-forward delim nil t)
518 (narrow-to-region start end)
519 (nnmbox-save-mail))))
524 ;;; nnmbox.el ends here