1 ;;; nnmbox.el --- mail mbox access for Gnus
2 ;; Copyright (C) 1995,96 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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; For an overview of what the interface functions do, please see the
35 (eval-when-compile (require 'cl))
37 (defvar nnmbox-mbox-file (expand-file-name "~/mbox")
38 "The name of the mail box file in the user's home directory.")
40 (defvar nnmbox-active-file (expand-file-name "~/.mbox-active")
41 "The name of the active file for the mail box.")
43 (defvar nnmbox-get-new-mail t
44 "If non-nil, nnmbox will check the incoming mail file and split the mail.")
46 (defvar nnmbox-prepare-save-mail-hook nil
47 "Hook run narrowed to an article before saving.")
51 (defconst nnmbox-version "nnmbox 1.0"
54 (defvar nnmbox-current-group nil
55 "Current nnmbox news group directory.")
57 (defconst nnmbox-mbox-buffer nil)
59 (defvar nnmbox-status-string "")
61 (defvar nnmbox-group-alist nil)
62 (defvar nnmbox-active-timestamp nil)
66 (defvar nnmbox-current-server nil)
67 (defvar nnmbox-server-alist nil)
68 (defvar nnmbox-server-variables
69 `((nnmbox-mbox-file ,nnmbox-mbox-file)
70 (nnmbox-active-file ,nnmbox-active-file)
71 (nnmbox-get-new-mail ,nnmbox-get-new-mail)
72 (nnmbox-current-group nil)
73 (nnmbox-active-timestamp nil)
74 (nnmbox-prepare-save-mail-hook nil)
75 (nnmbox-status-string "")
76 (nnmbox-group-alist nil)))
80 ;;; Interface functions
82 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
84 (set-buffer nntp-server-buffer)
86 (let ((number (length sequence))
88 article art-string start stop)
89 (nnmbox-possibly-change-newsgroup newsgroup server)
91 (setq article (car sequence))
92 (setq art-string (nnmbox-article-string article))
93 (set-buffer nnmbox-mbox-buffer)
94 (if (or (search-forward art-string nil t)
95 (progn (goto-char (point-min))
96 (search-forward art-string nil t)))
101 (concat "^" rmail-unix-mail-delimiter) nil t)
103 (search-forward "\n\n" nil t)
104 (setq stop (1- (point)))
105 (set-buffer nntp-server-buffer)
106 (insert (format "221 %d Article retrieved.\n" article))
107 (insert-buffer-substring nnmbox-mbox-buffer start stop)
108 (goto-char (point-max))
110 (setq sequence (cdr sequence))
111 (setq count (1+ count))
112 (and (numberp nnmail-large-newsgroup)
113 (> number nnmail-large-newsgroup)
115 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
116 (/ (* count 100) number))))
118 (and (numberp nnmail-large-newsgroup)
119 (> number nnmail-large-newsgroup)
120 (nnheader-message 5 "nnmbox: Receiving headers...done"))
122 (set-buffer nntp-server-buffer)
123 (nnheader-fold-continuation-lines)
126 (defun nnmbox-open-server (server &optional defs)
127 (nnheader-change-server 'nnmbox server defs)
129 ((not (file-exists-p nnmbox-mbox-file))
130 (nnmbox-close-server)
131 (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
132 ((file-directory-p nnmbox-mbox-file)
133 (nnmbox-close-server)
134 (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
136 (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
140 (defun nnmbox-close-server (&optional server)
141 (when (and nnmbox-mbox-buffer
142 (buffer-name nnmbox-mbox-buffer))
143 (kill-buffer nnmbox-mbox-buffer))
144 (setq nnmbox-current-server nil)
147 (defun nnmbox-server-opened (&optional server)
148 (and (equal server nnmbox-current-server)
150 (buffer-name nnmbox-mbox-buffer)
152 (buffer-name nntp-server-buffer)))
154 (defun nnmbox-status-message (&optional server)
155 nnmbox-status-string)
157 (defun nnmbox-request-article (article &optional newsgroup server buffer)
158 (nnmbox-possibly-change-newsgroup newsgroup server)
160 (set-buffer nnmbox-mbox-buffer)
161 (goto-char (point-min))
162 (if (search-forward (nnmbox-article-string article) nil t)
164 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
167 (or (and (re-search-forward
168 (concat "^" rmail-unix-mail-delimiter) nil t)
170 (goto-char (point-max)))
172 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
173 (set-buffer nntp-server-buffer)
175 (insert-buffer-substring nnmbox-mbox-buffer start stop)
176 (goto-char (point-min))
177 (while (looking-at "From ")
179 (insert "X-From-Line: ")
181 (if (numberp article)
182 (cons nnmbox-current-group article)
183 (nnmbox-article-group-number)))))))
185 (defun nnmbox-request-group (group &optional server dont-check)
186 (let ((active (cadr (assoc group nnmbox-group-alist))))
189 (nnheader-report 'nnmbox "No such group: %s" group))
190 ((null (nnmbox-possibly-change-newsgroup group server))
191 (nnheader-report 'nnmbox "No such group: %s" group))
193 (nnheader-report 'nnmbox "Selected group %s" group)
194 (nnheader-insert ""))
196 (nnheader-report 'nnmbox "Selected group %s" group)
197 (nnheader-insert "211 %d %d %d %s\n"
198 (1+ (- (cdr active) (car active)))
199 (car active) (cdr active) group)
202 (defun nnmbox-request-scan (&optional group server)
208 (set-buffer nnmbox-mbox-buffer)
210 nnmbox-mbox-file group
213 (let ((in-buf (current-buffer)))
214 (set-buffer nnmbox-mbox-buffer)
215 (goto-char (point-max))
216 (insert-buffer-substring in-buf))))))
218 (defun nnmbox-close-group (group &optional server)
221 (defun nnmbox-request-list (&optional server)
223 (or (nnmail-find-file nnmbox-active-file)
225 (setq nnmbox-group-alist (nnmail-get-active))
226 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
227 (nnmail-find-file nnmbox-active-file)))))
229 (defun nnmbox-request-newgroups (date &optional server)
230 (nnmbox-request-list server))
232 (defun nnmbox-request-list-newsgroups (&optional server)
233 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
235 (defun nnmbox-request-expire-articles
236 (articles newsgroup &optional server force)
237 (nnmbox-possibly-change-newsgroup newsgroup server)
240 (nnmail-activate 'nnmbox)
243 (set-buffer nnmbox-mbox-buffer)
244 (while (and articles is-old)
245 (goto-char (point-min))
246 (if (search-forward (nnmbox-article-string (car articles)) nil t)
248 (nnmail-expired-article-p
251 (point) (progn (end-of-line) (point))) force))
253 (nnheader-message 5 "Deleting article %d in %s..."
254 (car articles) newsgroup)
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 server)
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 server last)
300 (nnmbox-possibly-change-newsgroup group server)
301 (let ((buf (current-buffer))
303 (goto-char (point-min))
304 (if (looking-at "X-From-Line: ")
305 (replace-match "From ")
306 (insert "From nobody " (current-time-string) "\n"))
308 (nnmail-activate 'nnmbox)
311 (goto-char (point-min))
312 (search-forward "\n\n" nil t)
314 (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
315 (delete-region (point) (progn (forward-line 1) (point))))
316 (setq result (nnmbox-save-mail (and (stringp group) group))))
318 (set-buffer nnmbox-mbox-buffer)
319 (goto-char (point-max))
320 (insert-buffer-substring buf)
321 (and last (save-buffer))
323 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
326 (defun nnmbox-request-replace-article (article group buffer)
327 (nnmbox-possibly-change-newsgroup group)
329 (set-buffer nnmbox-mbox-buffer)
330 (goto-char (point-min))
331 (if (not (search-forward (nnmbox-article-string article) nil t))
333 (nnmbox-delete-mail t t)
334 (insert-buffer-substring buffer)
338 (defun nnmbox-request-delete-group (group &optional force server)
339 (nnmbox-possibly-change-newsgroup group server)
340 ;; Delete all articles in GROUP.
342 () ; Don't delete the articles.
344 (set-buffer nnmbox-mbox-buffer)
345 (goto-char (point-min))
346 ;; Delete all articles in this group.
347 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
349 (while (search-forward ident nil t)
351 (nnmbox-delete-mail))
352 (and found (save-buffer)))))
353 ;; Remove the group from all structures.
354 (setq nnmbox-group-alist
355 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
356 nnmbox-current-group nil)
357 ;; Save the active file.
358 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
361 (defun nnmbox-request-rename-group (group new-name &optional server)
362 (nnmbox-possibly-change-newsgroup group server)
364 (set-buffer nnmbox-mbox-buffer)
365 (goto-char (point-min))
366 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
367 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
369 (while (search-forward ident nil t)
370 (replace-match new-ident t t)
372 (and found (save-buffer))))
373 (let ((entry (assoc group nnmbox-group-alist)))
374 (and entry (setcar entry new-name))
375 (setq nnmbox-current-group nil)
376 ;; Save the new group alist.
377 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
381 ;;; Internal functions.
383 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
384 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
386 (defun nnmbox-delete-mail (&optional force leave-delim)
387 ;; Delete the current X-Gnus-Newsgroup line.
390 (progn (beginning-of-line) (point))
391 (progn (forward-line 1) (point))))
392 ;; Beginning of the article.
397 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
398 (if leave-delim (progn (forward-line 1) (point))
399 (match-beginning 0)))
402 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
404 (if (and (not (bobp)) leave-delim)
405 (progn (forward-line -2) (point))
406 (match-beginning 0)))
408 (goto-char (point-min))
409 ;; Only delete the article if no other groups owns it as well.
410 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
411 (delete-region (point-min) (point-max))))))
413 (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
415 (not (nnmbox-server-opened server)))
416 (nnmbox-open-server server))
417 (if (or (not nnmbox-mbox-buffer)
418 (not (buffer-name nnmbox-mbox-buffer)))
420 (set-buffer (setq nnmbox-mbox-buffer
421 (nnheader-find-file-noselect
422 nnmbox-mbox-file nil 'raw)))
423 (buffer-disable-undo (current-buffer))))
424 (if (not nnmbox-group-alist)
425 (nnmail-activate 'nnmbox))
427 (if (assoc newsgroup nnmbox-group-alist)
428 (setq nnmbox-current-group newsgroup))
431 (defun nnmbox-article-string (article)
432 (if (numberp article)
433 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
434 (int-to-string article) " ")
435 (concat "\nMessage-ID: " article)))
437 (defun nnmbox-article-group-number ()
439 (goto-char (point-min))
440 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
442 (cons (buffer-substring (match-beginning 1) (match-end 1))
444 (buffer-substring (match-beginning 2) (match-end 2)))))))
446 (defun nnmbox-save-mail (&optional group)
447 "Called narrowed to an article."
448 (let* ((nnmail-split-methods
449 (if group (list (list group "")) nnmail-split-methods))
450 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number)))
451 (delim (concat "^" rmail-unix-mail-delimiter)))
452 (goto-char (point-min))
453 ;; This might come from somewhere else.
454 (unless (looking-at delim)
455 (insert "From nobody " (current-time-string) "\n")
456 (goto-char (point-min)))
457 ;; Quote all "From " lines in the article.
459 (while (re-search-forward delim nil t)
462 (nnmail-insert-lines)
463 (nnmail-insert-xref group-art)
464 (nnmbox-insert-newsgroup-line group-art)
465 (run-hooks 'nnmbox-prepare-save-mail-hook)
468 (defun nnmbox-insert-newsgroup-line (group-art)
470 (goto-char (point-min))
471 (if (search-forward "\n\n" nil t)
475 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
476 (caar group-art) (cdar group-art)
477 (current-time-string)))
478 (setq group-art (cdr group-art)))))
481 (defun nnmbox-active-number (group)
482 ;; Find the next article number in GROUP.
483 (let ((active (cadr (assoc group nnmbox-group-alist))))
485 (setcdr active (1+ (cdr active)))
486 ;; This group is new, so we create a new entry for it.
487 ;; This might be a bit naughty... creating groups on the drop of
488 ;; a hat, but I don't know...
489 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
490 nnmbox-group-alist)))
493 (defun nnmbox-read-mbox ()
494 (nnmail-activate 'nnmbox)
495 (if (not (file-exists-p nnmbox-mbox-file))
496 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
497 (if (and nnmbox-mbox-buffer
498 (buffer-name nnmbox-mbox-buffer)
500 (set-buffer nnmbox-mbox-buffer)
501 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
504 (let ((delim (concat "^" rmail-unix-mail-delimiter))
506 (set-buffer (setq nnmbox-mbox-buffer
507 (nnheader-find-file-noselect
508 nnmbox-mbox-file nil 'raw)))
509 (buffer-disable-undo (current-buffer))
510 (goto-char (point-min))
511 (while (re-search-forward delim nil t)
512 (setq start (match-beginning 0))
513 (if (not (search-forward "\nX-Gnus-Newsgroup: "
518 (re-search-forward delim nil t)
524 (narrow-to-region start end)
525 (nnmbox-save-mail))))
530 ;;; nnmbox.el ends here