1 ;;; nnml.el --- mail spool 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 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
27 ;; For an overview of what the interface functions do, please see the
35 (defvar nnml-directory "~/Mail/"
36 "Mail spool directory.")
38 (defvar nnml-active-file (concat nnml-directory "active")
41 (defvar nnml-newsgroups-file (concat nnml-directory "newsgroups")
42 "Mail newsgroups description file.")
44 (defvar nnml-get-new-mail t
45 "If non-nil, nnml will check the incoming mail file and split the mail.")
47 (defvar nnml-nov-is-evil nil
48 "If non-nil, Gnus will never generate and use nov databases for mail groups.
49 Using nov databases will speed up header fetching considerably.
50 This variable shouldn't be flipped much. If you have, for some reason,
51 set this to t, and want to set it to nil again, you should always run
52 the `nnml-generate-nov-databases' command. The function will go
53 through all nnml directories and generate nov databases for them
54 all. This may very well take some time.")
58 (defconst nnml-version "nnml 0.2"
61 (defvar nnml-nov-file-name ".overview")
63 (defvar nnml-current-directory nil)
64 (defvar nnml-status-string "")
65 (defvar nnml-nov-buffer-alist nil)
66 (defvar nnml-group-alist nil)
67 (defvar nnml-active-timestamp nil)
73 (defvar nnml-current-server nil)
74 (defvar nnml-server-alist nil)
75 (defvar nnml-server-variables
77 (list 'nnml-directory nnml-directory)
78 (list 'nnml-active-file nnml-active-file)
79 (list 'nnml-newsgroups-file nnml-newsgroups-file)
80 (list 'nnml-get-new-mail nnml-get-new-mail)
81 (list 'nnml-nov-is-evil nnml-nov-is-evil)
82 (list 'nnml-nov-file-name nnml-nov-file-name)
83 '(nnml-current-directory nil)
84 '(nnml-status-string "")
85 '(nnml-nov-buffer-alist nil)
86 '(nnml-group-alist nil)
87 '(nnml-active-timestamp nil)))
91 ;;; Interface functions.
93 (defun nnml-retrieve-headers (sequence &optional newsgroup server)
95 (set-buffer nntp-server-buffer)
98 (number (length sequence))
101 (nnml-possibly-change-directory newsgroup)
102 (if (nnml-retrieve-headers-with-nov sequence)
105 (setq article (car sequence))
107 (concat nnml-current-directory (prin1-to-string article)))
108 (if (and (file-exists-p file)
109 (not (file-directory-p file)))
111 (insert (format "221 %d Article retrieved.\n" article))
113 (insert-file-contents file)
115 (if (search-forward "\n\n" nil t)
117 (goto-char (point-max))
120 (delete-region (point) (point-max))))
121 (setq sequence (cdr sequence))
122 (setq count (1+ count))
123 (and (numberp nnmail-large-newsgroup)
124 (> number nnmail-large-newsgroup)
126 gnus-verbose-backends
127 (message "nnml: Receiving headers... %d%%"
128 (/ (* count 100) number))))
130 (and (numberp nnmail-large-newsgroup)
131 (> number nnmail-large-newsgroup)
132 gnus-verbose-backends
133 (message "nnml: Receiving headers... done"))
135 ;; Fold continuation lines.
137 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
138 (replace-match " " t t))
141 (defun nnml-open-server (server &optional defs)
142 (nnheader-init-server-buffer)
143 (if (equal server nnml-current-server)
145 (if nnml-current-server
146 (setq nnml-server-alist
147 (cons (list nnml-current-server
148 (nnheader-save-variables nnml-server-variables))
150 (let ((state (assoc server nnml-server-alist)))
153 (nnheader-restore-variables (nth 1 state))
154 (setq nnml-server-alist (delq state nnml-server-alist)))
155 (nnheader-set-init-variables nnml-server-variables defs)))
156 (setq nnml-current-server server)))
158 (defun nnml-close-server (&optional server)
161 (defun nnml-server-opened (&optional server)
162 (and (equal server nnml-current-server)
164 (buffer-name nntp-server-buffer)))
166 (defun nnml-status-message (&optional server)
169 (defun nnml-request-article (id &optional newsgroup server buffer)
170 (nnml-possibly-change-directory newsgroup)
171 (let ((file (if (stringp id)
173 (concat nnml-current-directory (prin1-to-string id))))
174 (nntp-server-buffer (or buffer nntp-server-buffer)))
175 (if (and (stringp file)
177 (not (file-directory-p file)))
179 (nnmail-find-file file)))))
181 (defun nnml-request-group (group &optional server dont-check)
182 (if (not (nnml-possibly-change-directory group))
184 (setq nnml-status-string "Invalid group (no such directory)")
189 (let ((timestamp (nth 5 (file-attributes nnml-active-file))))
190 (if (or (not nnml-active-timestamp)
191 (> (nth 0 timestamp) (nth 0 nnml-active-timestamp))
192 (> (nth 1 timestamp) (nth 1 nnml-active-timestamp)))
194 (setq nnml-active-timestamp timestamp)
196 (setq nnml-group-alist (nnmail-get-active))))
197 (let ((active (nth 1 (assoc group nnml-group-alist))))
199 (set-buffer nntp-server-buffer)
203 (insert (format "211 %d %d %d %s\n"
204 (max (1+ (- (cdr active) (car active))) 0)
205 (car active) (cdr active) group))
208 (defun nnml-close-group (group &optional server)
211 (defun nnml-request-create-group (group &optional server)
213 (setq nnml-group-alist (nnmail-get-active))
214 (or (assoc group nnml-group-alist)
216 (setq nnml-group-alist (cons (list group (setq active (cons 0 0)))
218 (nnml-possibly-create-directory group)
219 (nnml-possibly-change-directory group)
220 (let ((articles (mapcar
222 (int-to-string file))
224 nnml-current-directory nil "^[0-9]+$"))))
227 (setcar active (apply 'min articles))
228 (setcdr active (apply 'max articles)))))
229 (nnmail-save-active nnml-group-alist nnml-active-file)))
232 (defun nnml-request-list (&optional server)
233 (if server (nnml-get-new-mail))
235 (nnmail-find-file nnml-active-file)))
237 (defun nnml-request-newgroups (date &optional server)
238 (nnml-request-list server))
240 (defun nnml-request-list-newsgroups (&optional server)
242 (nnmail-find-file nnml-newsgroups-file)))
244 (defun nnml-request-post (&optional server)
245 (mail-send-and-exit nil))
247 (fset 'nnml-request-post-buffer 'nnmail-request-post-buffer)
249 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
250 (nnml-possibly-change-directory newsgroup)
251 (let* ((days (or (and nnmail-expiry-wait-function
252 (funcall nnmail-expiry-wait-function newsgroup))
258 (string-to-int name)))
259 (directory-files nnml-current-directory nil "^[0-9]+$" t)))
260 (max-article (and active-articles (apply 'max active-articles)))
261 article rest mod-time)
263 (setq article (concat nnml-current-directory (int-to-string
265 (if (setq mod-time (nth 5 (file-attributes article)))
266 (if (and (or (not nnmail-keep-last-article)
268 (not (= (car articles) max-article)))
270 (> (nnmail-days-between
271 (current-time-string)
272 (current-time-string mod-time))
275 (and gnus-verbose-backends (message "Deleting %s..." article))
277 (delete-file article)
279 (setq active-articles (delq (car articles) active-articles))
280 (nnml-nov-delete-article newsgroup (car articles)))
281 (setq rest (cons (car articles) rest))))
282 (setq articles (cdr articles)))
283 (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
284 (setcar active (or (and active-articles (apply 'min active-articles))
286 (nnmail-save-active nnml-group-alist nnml-active-file))
290 (defun nnml-request-move-article
291 (article group server accept-form &optional last)
292 (let ((buf (get-buffer-create " *nnml move*"))
295 (nnml-request-article article group server)
298 (insert-buffer-substring nntp-server-buffer)
299 (setq result (eval accept-form))
300 (kill-buffer (current-buffer))
304 (delete-file (concat nnml-current-directory
305 (int-to-string article)))
307 (nnml-nov-delete-article group article)
308 (and last (nnml-save-nov))))
311 (defun nnml-request-accept-article (group &optional last)
316 (setq nnml-group-alist (nnmail-get-active))
317 ;; We trick the choosing function into believing that only one
318 ;; group is availiable.
319 (let ((nnmail-split-methods (list (list group ""))))
320 (setq result (car (nnml-save-mail))))
322 (nnmail-save-active nnml-group-alist nnml-active-file)
323 (and last (nnml-save-nov))))
326 (setq nnml-group-alist (nnmail-get-active))
327 (setq result (car (nnml-save-mail)))
329 (nnmail-save-active nnml-group-alist nnml-active-file)
330 (and last (nnml-save-nov)))))
333 (defun nnml-request-replace-article (article group buffer)
334 (nnml-possibly-change-directory group)
337 (nnml-possibly-create-directory group)
338 (if (not (condition-case ()
340 (write-region (point-min) (point-max)
341 (concat nnml-current-directory
342 (int-to-string article))
343 nil (if gnus-verbose-backends nil 'nomesg))
347 (let ((chars (nnmail-insert-lines))
348 (art (concat (int-to-string article) "\t"))
350 (setq nov-line (nnml-make-nov-line chars))
352 (set-buffer (nnml-open-nov group))
353 (goto-char (point-min))
354 (if (or (looking-at art)
355 (search-forward (concat "\n" art)))
357 (delete-region (progn (beginning-of-line) (point))
358 (progn (forward-line 1) (point)))
359 (insert (int-to-string article) nov-line)
361 (kill-buffer (current-buffer)))
366 ;;; Internal functions
368 (defun nnml-retrieve-headers-with-nov (articles)
369 (if (or gnus-nov-is-evil nnml-nov-is-evil)
371 (let ((first (car articles))
372 (last (progn (while (cdr articles) (setq articles (cdr articles)))
374 (nov (concat nnml-current-directory nnml-nov-file-name)))
375 (if (file-exists-p nov)
377 (set-buffer nntp-server-buffer)
379 (insert-file-contents nov)
381 (while (and (not (eobp)) (< first (read (current-buffer))))
384 (if (not (eobp)) (delete-region 1 (point)))
385 (while (and (not (eobp)) (>= last (read (current-buffer))))
388 (if (not (eobp)) (delete-region (point) (point-max)))
391 (defun nnml-possibly-change-directory (newsgroup &optional force)
393 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
394 (and (or force (file-directory-p pathname))
395 (setq nnml-current-directory pathname)))
398 (defun nnml-possibly-create-directory (group)
400 (setq dir (nnmail-article-pathname group nnml-directory))
401 (while (not (file-directory-p dir))
402 (setq dirs (cons dir dirs))
403 (setq dir (file-name-directory (directory-file-name dir))))
405 (if (make-directory (directory-file-name (car dirs)))
406 (error "Could not create directory %s" (car dirs)))
407 (and gnus-verbose-backends
408 (message "Creating mail directory %s" (car dirs)))
409 (setq dirs (cdr dirs)))))
411 (defun nnml-save-mail ()
412 "Called narrowed to an article."
413 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
415 (setq chars (nnmail-insert-lines))
416 (nnmail-insert-xref group-art)
417 (goto-char (point-min))
418 (while (looking-at "From ")
419 (replace-match "X-From-Line: ")
421 ;; We save the article in all the newsgroups it belongs in.
425 (nnml-possibly-create-directory (car (car ga)))
426 (let ((file (concat (nnmail-article-pathname
427 (car (car ga)) nnml-directory)
428 (int-to-string (cdr (car ga))))))
430 ;; It was already saved, so we just make a hard link.
431 (add-name-to-file first file t)
433 (write-region (point-min) (point-max) file nil
434 (if gnus-verbose-backends nil 'nomesg))
437 ;; Generate a nov line for this article. We generate the nov
438 ;; line after saving, because nov generation destroys the
440 (setq nov-line (nnml-make-nov-line chars))
441 ;; Output the nov line to all nov databases that should have it.
442 (let ((ga group-art))
444 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
448 (defun nnml-active-number (group)
449 "Compute the next article number in GROUP."
450 (let ((active (car (cdr (assoc group nnml-group-alist)))))
453 (setq active (cons 1 0))
454 (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
455 (setcdr active (1+ (cdr active)))
457 (while (file-exists-p
458 (setq file (concat (nnmail-article-pathname
459 group nnml-directory)
460 (int-to-string (cdr active)))))
461 (setcdr active (1+ (cdr active)))))
464 (defun nnml-get-new-mail ()
465 "Read new incoming mail."
467 (if (and nnml-get-new-mail nnmail-spool-file
468 (file-exists-p nnmail-spool-file)
469 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
471 (and gnus-verbose-backends
472 (message "nnml: Reading incoming mail..."))
474 (nnmail-move-inbox nnmail-spool-file
475 (concat nnml-directory "Incoming")))
477 (setq nnml-group-alist (nnmail-get-active))
478 (nnmail-split-incoming incoming 'nnml-save-mail)
479 (nnmail-save-active nnml-group-alist nnml-active-file)
481 (run-hooks 'nnmail-read-incoming-hook)
482 ;; The following has been commented away, just to make sure
483 ;; that nobody ever loses any mail. If you feel safe that
484 ;; nnml will never do anything strange, just remove those
485 ;; two semicolons, and avoid having lots of "Incoming*"
487 ;; (delete-file incoming)
488 (and gnus-verbose-backends
489 (message "nnml: Reading incoming mail...done"))))))
492 (defun nnml-add-nov (group article line)
493 "Add a nov line for the GROUP base."
495 (set-buffer (nnml-open-nov group))
496 (goto-char (point-max))
497 (insert (int-to-string article) line)))
499 (defsubst nnml-header-value ()
500 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
502 (defun nnml-make-nov-line (chars)
503 "Create a nov from the current headers."
504 (let ((case-fold-search t)
505 subject from date id references lines xref in-reply-to char)
508 (goto-char (point-min))
511 (1- (or (search-forward "\n\n" nil t) (point-max))))
512 ;; Fold continuation lines.
513 (goto-char (point-min))
514 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
515 (replace-match " " t t))
516 (subst-char-in-region (point-min) (point-max) ?\t ? )
517 ;; [number subject from date id references chars lines xref]
519 (goto-char (point-min))
520 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
523 (setq char (downcase (following-char)))
526 (setq subject (nnml-header-value)))
528 (setq from (nnml-header-value)))
530 (setq xref (nnml-header-value)))
532 (setq lines (nnml-header-value)))
534 (setq date (nnml-header-value)))
536 (setq id (setq id (nnml-header-value))))
538 (setq references (nnml-header-value)))
540 (setq in-reply-to (nnml-header-value))))
543 (and (not references)
545 (string-match "<[^>]+>" in-reply-to)
547 (substring in-reply-to (match-beginning 0)
549 ;; [number subject from date id references chars lines xref]
550 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
551 (or subject "(none)")
552 (or from "(nobody)") (or date "")
553 (or id (concat "nnml-dummy-id-"
555 (lambda (time) (int-to-string time))
556 (current-time) "-")))
558 (or chars 0) (or lines "0") (or xref ""))))))
560 (defun nnml-open-nov (group)
561 (or (cdr (assoc group nnml-nov-buffer-alist))
562 (let ((buffer (find-file-noselect
563 (concat (nnmail-article-pathname
564 group nnml-directory) nnml-nov-file-name))))
567 (buffer-disable-undo (current-buffer)))
568 (setq nnml-nov-buffer-alist
569 (cons (cons group buffer) nnml-nov-buffer-alist))
572 (defun nnml-save-nov ()
574 (while nnml-nov-buffer-alist
575 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
577 (set-buffer (cdr (car nnml-nov-buffer-alist)))
578 (and (buffer-modified-p)
580 1 (point-max) (buffer-file-name) nil 'nomesg))
581 (set-buffer-modified-p nil)
582 (kill-buffer (current-buffer))))
583 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
586 (defun nnml-generate-nov-databases (dir)
587 "Generate nov databases in all nnml mail newsgroups."
590 (setq nnml-group-alist nil)
591 (list nnml-directory)))
592 (nnml-open-server (system-name))
593 (let ((dirs (directory-files dir t nil t)))
595 (if (and (not (string-match "/\\.\\.$" (car dirs)))
596 (not (string-match "/\\.$" (car dirs)))
597 (file-directory-p (car dirs)))
598 (nnml-generate-nov-databases (car dirs)))
599 (setq dirs (cdr dirs))))
604 (string-to-int name)))
605 (directory-files dir nil "^[0-9]+$" t))
607 (nov (concat dir "/" nnml-nov-file-name))
608 (nov-buffer (get-buffer-create "*nov*"))
611 (setq nnml-group-alist
612 (cons (list (nnmail-replace-chars-in-string
613 (substring (expand-file-name dir)
614 (length (expand-file-name
619 (while (cdr f) (setq f (cdr f)))
624 (set-buffer nntp-server-buffer)
625 (if (file-exists-p nov)
628 (set-buffer nov-buffer)
629 (buffer-disable-undo (current-buffer))
633 (insert-file-contents (concat dir "/" (int-to-string (car files))))
635 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
636 (setq chars (- (point-max)
639 (if (not (= 0 chars)) ; none of them empty files...
641 (setq nov-line (nnml-make-nov-line chars))
643 (set-buffer nov-buffer)
644 (goto-char (point-max))
645 (insert (int-to-string (car files)) nov-line))))
647 (setq files (cdr files)))
649 (set-buffer nov-buffer)
650 (write-region 1 (point-max) (expand-file-name nov) nil
652 (kill-buffer (current-buffer)))))
653 (nnmail-save-active nnml-group-alist nnml-active-file)))
655 (defun nnml-nov-delete-article (group article)
657 (set-buffer (nnml-open-nov group))
659 (if (re-search-forward (concat "^" (int-to-string article) "\t"))
660 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
665 ;;; nnml.el ends here