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-status-string "")
74 (nnmbox-group-alist nil)))
78 ;;; Interface functions
80 (defun nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
82 (set-buffer nntp-server-buffer)
84 (let ((number (length sequence))
86 article art-string start stop)
87 (nnmbox-possibly-change-newsgroup newsgroup)
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 (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
114 (/ (* count 100) number))))
116 (and (numberp nnmail-large-newsgroup)
117 (> number nnmail-large-newsgroup)
118 (nnheader-message 5 "nnmbox: Receiving headers...done"))
120 (set-buffer nntp-server-buffer)
121 (nnheader-fold-continuation-lines)
124 (defun nnmbox-open-server (server &optional defs)
125 (nnheader-change-server 'nnmbox server defs)
127 ((not (file-exists-p nnmbox-mbox-file))
128 (nnmbox-close-server)
129 (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
130 ((file-directory-p nnmbox-mbox-file)
131 (nnmbox-close-server)
132 (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
134 (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
138 (defun nnmbox-close-server (&optional server)
139 (when (and nnmbox-mbox-buffer
140 (buffer-name nnmbox-mbox-buffer))
141 (kill-buffer nnmbox-mbox-buffer))
142 (setq nnmbox-current-server nil)
145 (defun nnmbox-server-opened (&optional server)
146 (and (equal server nnmbox-current-server)
148 (buffer-name nnmbox-mbox-buffer)
150 (buffer-name nntp-server-buffer)))
152 (defun nnmbox-status-message (&optional server)
153 nnmbox-status-string)
155 (defun nnmbox-request-article (article &optional newsgroup server buffer)
156 (nnmbox-possibly-change-newsgroup newsgroup)
158 (set-buffer nnmbox-mbox-buffer)
159 (goto-char (point-min))
160 (if (search-forward (nnmbox-article-string article) nil t)
162 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
165 (or (and (re-search-forward
166 (concat "^" rmail-unix-mail-delimiter) nil t)
168 (goto-char (point-max)))
170 (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
171 (set-buffer nntp-server-buffer)
173 (insert-buffer-substring nnmbox-mbox-buffer start stop)
174 (goto-char (point-min))
175 (while (looking-at "From ")
177 (insert "X-From-Line: ")
179 (if (numberp article)
180 (cons nnmbox-current-group article)
181 (nnmbox-article-group-number)))))))
183 (defun nnmbox-request-group (group &optional server dont-check)
184 (let ((active (cadr (assoc group nnmbox-group-alist))))
187 (nnheader-report 'nnmbox "No such group: %s" group))
188 ((null (nnmbox-possibly-change-newsgroup group))
189 (nnheader-report 'nnmbox "No such group: %s" group))
191 (nnheader-report 'nnmbox "Selected group %s" group)
192 (nnheader-insert ""))
194 (nnheader-report 'nnmbox "Selected group %s" group)
195 (nnheader-insert "211 %d %d %d %s\n"
196 (1+ (- (cdr active) (car active)))
197 (car active) (cdr active) group)
200 (defun nnmbox-request-scan (&optional group server)
206 (set-buffer nnmbox-mbox-buffer)
208 nnmbox-mbox-file group
211 (let ((in-buf (current-buffer)))
212 (set-buffer nnmbox-mbox-buffer)
213 (goto-char (point-max))
214 (insert-buffer-substring in-buf))))))
216 (defun nnmbox-close-group (group &optional server)
219 (defun nnmbox-request-list (&optional server)
221 (or (nnmail-find-file nnmbox-active-file)
223 (setq nnmbox-group-alist (nnmail-get-active))
224 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
225 (nnmail-find-file nnmbox-active-file)))))
227 (defun nnmbox-request-newgroups (date &optional server)
228 (nnmbox-request-list server))
230 (defun nnmbox-request-list-newsgroups (&optional server)
231 (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
233 (defun nnmbox-request-post (&optional server)
234 (mail-send-and-exit nil))
236 (defun nnmbox-request-expire-articles
237 (articles newsgroup &optional server force)
238 (nnmbox-possibly-change-newsgroup newsgroup)
241 (nnmail-activate 'nnmbox)
244 (set-buffer nnmbox-mbox-buffer)
245 (while (and articles is-old)
246 (goto-char (point-min))
247 (if (search-forward (nnmbox-article-string (car articles)) nil t)
249 (nnmail-expired-article-p
252 (point) (progn (end-of-line) (point))) force))
254 (nnheader-message 5 "Deleting article %d in %s..."
255 (car articles) newsgroup)
256 (nnmbox-delete-mail))
257 (setq rest (cons (car articles) rest))))
258 (setq articles (cdr articles)))
260 ;; Find the lowest active article in this group.
261 (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
262 (goto-char (point-min))
263 (while (and (not (search-forward
264 (nnmbox-article-string (car active)) nil t))
265 (<= (car active) (cdr active)))
266 (setcar active (1+ (car active)))
267 (goto-char (point-min))))
268 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
269 (nconc rest articles))))
271 (defun nnmbox-request-move-article
272 (article group server accept-form &optional last)
273 (nnmbox-possibly-change-newsgroup group)
274 (let ((buf (get-buffer-create " *nnmbox move*"))
277 (nnmbox-request-article article group server)
280 (buffer-disable-undo (current-buffer))
282 (insert-buffer-substring nntp-server-buffer)
283 (goto-char (point-min))
284 (while (re-search-forward
286 (save-excursion (search-forward "\n\n" nil t) (point)) t)
287 (delete-region (progn (beginning-of-line) (point))
288 (progn (forward-line 1) (point))))
289 (setq result (eval accept-form))
293 (set-buffer nnmbox-mbox-buffer)
294 (goto-char (point-min))
295 (if (search-forward (nnmbox-article-string article) nil t)
296 (nnmbox-delete-mail))
297 (and last (save-buffer))))
300 (defun nnmbox-request-accept-article (group &optional last)
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 (insert-buffer-substring buf)
320 (and last (save-buffer))
322 (nnmail-save-active nnmbox-group-alist nnmbox-active-file))
325 (defun nnmbox-request-replace-article (article group buffer)
326 (nnmbox-possibly-change-newsgroup group)
328 (set-buffer nnmbox-mbox-buffer)
329 (goto-char (point-min))
330 (if (not (search-forward (nnmbox-article-string article) nil t))
332 (nnmbox-delete-mail t t)
333 (insert-buffer-substring buffer)
337 (defun nnmbox-request-delete-group (group &optional force server)
338 (nnmbox-possibly-change-newsgroup group)
339 ;; Delete all articles in GROUP.
341 () ; Don't delete the articles.
343 (set-buffer nnmbox-mbox-buffer)
344 (goto-char (point-min))
345 ;; Delete all articles in this group.
346 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
348 (while (search-forward ident nil t)
350 (nnmbox-delete-mail))
351 (and found (save-buffer)))))
352 ;; Remove the group from all structures.
353 (setq nnmbox-group-alist
354 (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
355 nnmbox-current-group nil)
356 ;; Save the active file.
357 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
360 (defun nnmbox-request-rename-group (group new-name &optional server)
361 (nnmbox-possibly-change-newsgroup group)
363 (set-buffer nnmbox-mbox-buffer)
364 (goto-char (point-min))
365 (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
366 (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
368 (while (search-forward ident nil t)
369 (replace-match new-ident t t)
371 (and found (save-buffer))))
372 (let ((entry (assoc group nnmbox-group-alist)))
373 (and entry (setcar entry new-name))
374 (setq nnmbox-current-group nil)
375 ;; Save the new group alist.
376 (nnmail-save-active nnmbox-group-alist nnmbox-active-file)
380 ;;; Internal functions.
382 ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
383 ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
385 (defun nnmbox-delete-mail (&optional force leave-delim)
386 ;; Delete the current X-Gnus-Newsgroup line.
389 (progn (beginning-of-line) (point))
390 (progn (forward-line 1) (point))))
391 ;; Beginning of the article.
396 (re-search-backward (concat "^" rmail-unix-mail-delimiter) nil t)
397 (if leave-delim (progn (forward-line 1) (point))
398 (match-beginning 0)))
401 (or (and (re-search-forward (concat "^" rmail-unix-mail-delimiter)
403 (if (and (not (bobp)) leave-delim)
404 (progn (forward-line -2) (point))
405 (match-beginning 0)))
407 (goto-char (point-min))
408 ;; Only delete the article if no other groups owns it as well.
409 (if (or force (not (re-search-forward "^X-Gnus-Newsgroup: " nil t)))
410 (delete-region (point-min) (point-max))))))
412 (defun nnmbox-possibly-change-newsgroup (newsgroup)
413 (if (or (not nnmbox-mbox-buffer)
414 (not (buffer-name nnmbox-mbox-buffer)))
416 (set-buffer (setq nnmbox-mbox-buffer
417 (nnheader-find-file-noselect
418 nnmbox-mbox-file nil 'raw)))
419 (buffer-disable-undo (current-buffer))))
420 (if (not nnmbox-group-alist)
421 (nnmail-activate 'nnmbox))
423 (if (assoc newsgroup nnmbox-group-alist)
424 (setq nnmbox-current-group newsgroup))
427 (defun nnmbox-article-string (article)
428 (if (numberp article)
429 (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
430 (int-to-string article) " ")
431 (concat "\nMessage-ID: " article)))
433 (defun nnmbox-article-group-number ()
435 (goto-char (point-min))
436 (and (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
438 (cons (buffer-substring (match-beginning 1) (match-end 1))
440 (buffer-substring (match-beginning 2) (match-end 2)))))))
442 (defun nnmbox-save-mail (&optional group)
443 "Called narrowed to an article."
444 (let* ((nnmail-split-methods
445 (if group (list (list group "")) nnmail-split-methods))
446 (group-art (nreverse (nnmail-article-group 'nnmbox-active-number)))
447 (delim (concat "^" rmail-unix-mail-delimiter)))
448 (goto-char (point-min))
449 ;; This might come from somewhere else.
450 (unless (looking-at delim)
451 (insert "From nobody " (current-time-string) "\n")
452 (goto-char (point-min)))
453 ;; Quote all "From " lines in the article.
455 (while (re-search-forward delim nil t)
458 (nnmail-insert-lines)
459 (nnmail-insert-xref group-art)
460 (nnmbox-insert-newsgroup-line group-art)
461 (run-hooks 'nnmbox-prepare-save-mail-hook)
464 (defun nnmbox-insert-newsgroup-line (group-art)
466 (goto-char (point-min))
467 (if (search-forward "\n\n" nil t)
471 (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
472 (car (car group-art)) (cdr (car group-art))
473 (current-time-string)))
474 (setq group-art (cdr group-art)))))
477 (defun nnmbox-active-number (group)
478 ;; Find the next article number in GROUP.
479 (let ((active (car (cdr (assoc group nnmbox-group-alist)))))
481 (setcdr active (1+ (cdr active)))
482 ;; This group is new, so we create a new entry for it.
483 ;; This might be a bit naughty... creating groups on the drop of
484 ;; a hat, but I don't know...
485 (setq nnmbox-group-alist (cons (list group (setq active (cons 1 1)))
486 nnmbox-group-alist)))
489 (defun nnmbox-read-mbox ()
490 (nnmail-activate 'nnmbox)
491 (if (not (file-exists-p nnmbox-mbox-file))
492 (write-region 1 1 nnmbox-mbox-file t 'nomesg))
493 (if (and nnmbox-mbox-buffer
494 (buffer-name nnmbox-mbox-buffer)
496 (set-buffer nnmbox-mbox-buffer)
497 (= (buffer-size) (nth 7 (file-attributes nnmbox-mbox-file)))))
500 (let ((delim (concat "^" rmail-unix-mail-delimiter))
502 (set-buffer (setq nnmbox-mbox-buffer
503 (nnheader-find-file-noselect
504 nnmbox-mbox-file nil 'raw)))
505 (buffer-disable-undo (current-buffer))
506 (goto-char (point-min))
507 (while (re-search-forward delim nil t)
508 (setq start (match-beginning 0))
509 (if (not (search-forward "\nX-Gnus-Newsgroup: "
514 (re-search-forward delim nil t)
520 (narrow-to-region start end)
521 (nnmbox-save-mail))))
526 ;;; nnmbox.el ends here