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.")
56 (defvar nnml-prepare-save-mail-hook nil
57 "Hook run narrowed to an article before saving.")
61 (defconst nnml-version "nnml 0.2"
64 (defvar nnml-nov-file-name ".overview")
66 (defvar nnml-current-directory nil)
67 (defvar nnml-status-string "")
68 (defvar nnml-nov-buffer-alist nil)
69 (defvar nnml-group-alist nil)
70 (defvar nnml-active-timestamp nil)
76 (defvar nnml-current-server nil)
77 (defvar nnml-server-alist nil)
78 (defvar nnml-server-variables
80 (list 'nnml-directory nnml-directory)
81 (list 'nnml-active-file nnml-active-file)
82 (list 'nnml-newsgroups-file nnml-newsgroups-file)
83 (list 'nnml-get-new-mail nnml-get-new-mail)
84 (list 'nnml-nov-is-evil nnml-nov-is-evil)
85 (list 'nnml-nov-file-name nnml-nov-file-name)
86 '(nnml-current-directory nil)
87 '(nnml-status-string "")
88 '(nnml-nov-buffer-alist nil)
89 '(nnml-group-alist nil)
90 '(nnml-active-timestamp nil)))
94 ;;; Interface functions.
96 (defun nnml-retrieve-headers (sequence &optional newsgroup server)
98 (set-buffer nntp-server-buffer)
101 (number (length sequence))
104 (nnml-possibly-change-directory newsgroup)
105 (if (nnml-retrieve-headers-with-nov sequence)
108 (setq article (car sequence))
110 (concat nnml-current-directory (prin1-to-string article)))
111 (if (and (file-exists-p file)
112 (not (file-directory-p file)))
114 (insert (format "221 %d Article retrieved.\n" article))
116 (nnheader-insert-head file)
118 (if (search-forward "\n\n" nil t)
120 (goto-char (point-max))
123 (delete-region (point) (point-max))))
124 (setq sequence (cdr sequence))
125 (setq count (1+ count))
126 (and (numberp nnmail-large-newsgroup)
127 (> number nnmail-large-newsgroup)
129 gnus-verbose-backends
130 (message "nnml: Receiving headers... %d%%"
131 (/ (* count 100) number))))
133 (and (numberp nnmail-large-newsgroup)
134 (> number nnmail-large-newsgroup)
135 gnus-verbose-backends
136 (message "nnml: Receiving headers... done"))
138 ;; Fold continuation lines.
139 (goto-char (point-min))
140 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
141 (replace-match " " t t))
144 (defun nnml-open-server (server &optional defs)
145 (nnheader-init-server-buffer)
146 (if (equal server nnml-current-server)
148 (if nnml-current-server
149 (setq nnml-server-alist
150 (cons (list nnml-current-server
151 (nnheader-save-variables nnml-server-variables))
153 (let ((state (assoc server nnml-server-alist)))
156 (nnheader-restore-variables (nth 1 state))
157 (setq nnml-server-alist (delq state nnml-server-alist)))
158 (nnheader-set-init-variables nnml-server-variables defs)))
159 (setq nnml-current-server server)))
161 (defun nnml-close-server (&optional server)
164 (defun nnml-server-opened (&optional server)
165 (and (equal server nnml-current-server)
167 (buffer-name nntp-server-buffer)))
169 (defun nnml-status-message (&optional server)
172 (defun nnml-request-article (id &optional newsgroup server buffer)
173 (nnml-possibly-change-directory newsgroup)
174 (let ((file (if (stringp id)
176 (concat nnml-current-directory (prin1-to-string id))))
177 (nntp-server-buffer (or buffer nntp-server-buffer)))
178 (if (and (stringp file)
180 (not (file-directory-p file)))
182 (nnmail-find-file file)))))
184 (defun nnml-request-group (group &optional server dont-check)
185 (if (not (nnml-possibly-change-directory group))
187 (setq nnml-status-string "Invalid group (no such directory)")
191 (nnml-get-new-mail group)
192 (let ((timestamp (nth 5 (file-attributes nnml-active-file))))
193 (if (or (not nnml-active-timestamp)
194 (> (nth 0 timestamp) (nth 0 nnml-active-timestamp))
195 (> (nth 1 timestamp) (nth 1 nnml-active-timestamp)))
197 (setq nnml-active-timestamp timestamp)
199 (setq nnml-group-alist (nnmail-get-active))))
200 (let ((active (nth 1 (assoc group nnml-group-alist))))
202 (set-buffer nntp-server-buffer)
206 (insert (format "211 %d %d %d %s\n"
207 (max (1+ (- (cdr active) (car active))) 0)
208 (car active) (cdr active) group))
211 (defun nnml-close-group (group &optional server)
214 (defun nnml-request-close ()
215 (setq nnml-current-server nil)
216 (setq nnml-server-alist nil)
219 (defun nnml-request-create-group (group &optional server)
221 (setq nnml-group-alist (nnmail-get-active))
222 (or (assoc group nnml-group-alist)
224 (setq nnml-group-alist (cons (list group (setq active (cons 0 0)))
226 (nnml-possibly-create-directory group)
227 (nnml-possibly-change-directory group)
228 (let ((articles (mapcar
230 (string-to-int file))
232 nnml-current-directory nil "^[0-9]+$"))))
235 (setcar active (apply 'min articles))
236 (setcdr active (apply 'max articles)))))
237 (nnmail-save-active nnml-group-alist nnml-active-file)))
240 (defun nnml-request-list (&optional server)
241 (if server (nnml-get-new-mail))
243 (nnmail-find-file nnml-active-file)
244 (setq nnml-group-alist (nnmail-get-active))))
246 (defun nnml-request-newgroups (date &optional server)
247 (nnml-request-list server))
249 (defun nnml-request-list-newsgroups (&optional server)
251 (nnmail-find-file nnml-newsgroups-file)))
253 (defun nnml-request-post (&optional server)
254 (mail-send-and-exit nil))
256 (fset 'nnml-request-post-buffer 'nnmail-request-post-buffer)
258 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
259 (nnml-possibly-change-directory newsgroup)
260 (let* ((days (or (and nnmail-expiry-wait-function
261 (funcall nnmail-expiry-wait-function newsgroup))
267 (string-to-int name)))
268 (directory-files nnml-current-directory nil "^[0-9]+$" t)))
269 (max-article (and active-articles (apply 'max active-articles)))
270 article rest mod-time)
272 (setq article (concat nnml-current-directory (int-to-string
274 (if (setq mod-time (nth 5 (file-attributes article)))
275 (if (and (or (not nnmail-keep-last-article)
277 (not (= (car articles) max-article)))
279 (> (nnmail-days-between
280 (current-time-string)
281 (current-time-string mod-time))
284 (and gnus-verbose-backends (message "Deleting %s..." article))
286 (delete-file article)
288 (setq active-articles (delq (car articles) active-articles))
289 (nnml-nov-delete-article newsgroup (car articles)))
290 (setq rest (cons (car articles) rest))))
291 (setq articles (cdr articles)))
292 (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
294 (setcar active (or (and active-articles
295 (apply 'min active-articles))
297 (nnmail-save-active nnml-group-alist nnml-active-file))
302 (defun nnml-request-move-article
303 (article group server accept-form &optional last)
304 (let ((buf (get-buffer-create " *nnml move*"))
307 (nnml-request-article article group server)
310 (insert-buffer-substring nntp-server-buffer)
311 (setq result (eval accept-form))
312 (kill-buffer (current-buffer))
316 (delete-file (concat nnml-current-directory
317 (int-to-string article)))
319 (nnml-nov-delete-article group article)
320 (and last (nnml-save-nov))))
323 (defun nnml-request-accept-article (group &optional last)
328 (setq nnml-group-alist (nnmail-get-active))
329 ;; We trick the choosing function into believing that only one
330 ;; group is availiable.
331 (let ((nnmail-split-methods (list (list group ""))))
332 (setq result (car (nnml-save-mail))))
334 (nnmail-save-active nnml-group-alist nnml-active-file)
335 (and last (nnml-save-nov))))
338 (setq nnml-group-alist (nnmail-get-active))
339 (setq result (car (nnml-save-mail)))
341 (nnmail-save-active nnml-group-alist nnml-active-file)
342 (and last (nnml-save-nov)))))
345 (defun nnml-request-replace-article (article group buffer)
346 (nnml-possibly-change-directory group)
349 (nnml-possibly-create-directory group)
350 (if (not (condition-case ()
352 (write-region (point-min) (point-max)
353 (concat nnml-current-directory
354 (int-to-string article))
355 nil (if gnus-verbose-backends nil 'nomesg))
359 (let ((chars (nnmail-insert-lines))
360 (art (concat (int-to-string article) "\t"))
362 (setq nov-line (nnml-make-nov-line chars))
364 (set-buffer (nnml-open-nov group))
365 (goto-char (point-min))
366 (if (or (looking-at art)
367 (search-forward (concat "\n" art)))
369 (delete-region (progn (beginning-of-line) (point))
370 (progn (forward-line 1) (point)))
371 (insert (int-to-string article) nov-line)
373 (kill-buffer (current-buffer)))
378 ;;; Internal functions
380 (defun nnml-retrieve-headers-with-nov (articles)
381 (if (or gnus-nov-is-evil nnml-nov-is-evil)
383 (let ((first (car articles))
384 (last (progn (while (cdr articles) (setq articles (cdr articles)))
386 (nov (concat nnml-current-directory nnml-nov-file-name)))
387 (if (file-exists-p nov)
389 (set-buffer nntp-server-buffer)
391 (insert-file-contents nov)
392 (goto-char (point-min))
393 (while (and (not (eobp)) (< first (read (current-buffer))))
396 (if (not (eobp)) (delete-region 1 (point)))
397 (while (and (not (eobp)) (>= last (read (current-buffer))))
400 (if (not (eobp)) (delete-region (point) (point-max)))
403 (defun nnml-possibly-change-directory (newsgroup &optional force)
405 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
406 (and (or force (file-directory-p pathname))
407 (setq nnml-current-directory pathname)))
410 (defun nnml-possibly-create-directory (group)
412 (setq dir (nnmail-article-pathname group nnml-directory))
413 (while (not (file-directory-p dir))
414 (setq dirs (cons dir dirs))
415 (setq dir (file-name-directory (directory-file-name dir))))
417 (if (make-directory (directory-file-name (car dirs)))
418 (error "Could not create directory %s" (car dirs)))
419 (and gnus-verbose-backends
420 (message "Creating mail directory %s" (car dirs)))
421 (setq dirs (cdr dirs)))))
423 (defun nnml-save-mail ()
424 "Called narrowed to an article."
425 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
427 (setq chars (nnmail-insert-lines))
428 (nnmail-insert-xref group-art)
429 (run-hooks 'nnml-prepare-save-mail-hook)
430 (goto-char (point-min))
431 (while (looking-at "From ")
432 (replace-match "X-From-Line: ")
434 ;; We save the article in all the newsgroups it belongs in.
438 (nnml-possibly-create-directory (car (car ga)))
439 (let ((file (concat (nnmail-article-pathname
440 (car (car ga)) nnml-directory)
441 (int-to-string (cdr (car ga))))))
443 ;; It was already saved, so we just make a hard link.
444 (add-name-to-file first file t)
446 (write-region (point-min) (point-max) file nil
447 (if gnus-verbose-backends nil 'nomesg))
450 ;; Generate a nov line for this article. We generate the nov
451 ;; line after saving, because nov generation destroys the
453 (setq nov-line (nnml-make-nov-line chars))
454 ;; Output the nov line to all nov databases that should have it.
455 (let ((ga group-art))
457 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
461 (defun nnml-active-number (group)
462 "Compute the next article number in GROUP."
463 (let ((active (car (cdr (assoc group nnml-group-alist)))))
466 (setq active (cons 1 0))
467 (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
468 (setcdr active (1+ (cdr active)))
470 (while (file-exists-p
471 (setq file (concat (nnmail-article-pathname
472 group nnml-directory)
473 (int-to-string (cdr active)))))
474 (setcdr active (1+ (cdr active)))))
477 (defun nnml-get-new-mail (&optional group)
478 "Read new incoming mail."
479 (let ((spools (nnmail-get-spool-files group))
481 (if (or (not nnml-get-new-mail) (not nnmail-spool-file))
483 ;; We first activate all the groups.
485 (setq nnml-group-alist (nnmail-get-active))
486 ;; The we go through all the existing spool files and split the
490 (file-exists-p (car spools))
491 (> (nth 7 (file-attributes (car spools))) 0)
493 (and gnus-verbose-backends
494 (message "nnml: Reading incoming mail..."))
497 (car spools) (concat nnml-directory "Incoming")))
498 (nnmail-split-incoming incoming 'nnml-save-mail)
499 (setq incomings (cons incoming incomings))
500 ;; The following has been commented away, just to make sure
501 ;; that nobody ever loses any mail. If you feel safe that
502 ;; nnml will never do anything strange, just remove those
503 ;; two semicolons, and avoid having lots of "Incoming*"
505 ;; (delete-file incoming)
507 (setq spools (cdr spools)))
508 ;; If we did indeed read any incoming spools, we save all info.
511 (nnmail-save-active nnml-group-alist nnml-active-file)
513 (run-hooks 'nnmail-read-incoming-hook)
514 (and gnus-verbose-backends
515 (message "nnml: Reading incoming mail...done"))))
517 ;; The following has been commented away, just to make sure
518 ;; that nobody ever loses any mail. If you feel safe that
519 ;; nnfolder will never do anything strange, just remove those
520 ;; two semicolons, and avoid having lots of "Incoming*"
522 ;; (and (file-writable-p incoming) (delete-file incoming))
523 (setq incomings (cdr incomings))))))
526 (defun nnml-add-nov (group article line)
527 "Add a nov line for the GROUP base."
529 (set-buffer (nnml-open-nov group))
530 (goto-char (point-max))
531 (insert (int-to-string article) line)))
533 (defsubst nnml-header-value ()
534 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
536 (defun nnml-make-nov-line (chars)
537 "Create a nov from the current headers."
538 (let ((case-fold-search t)
539 subject from date id references lines xref in-reply-to char)
542 (goto-char (point-min))
545 (1- (or (search-forward "\n\n" nil t) (point-max))))
546 ;; Fold continuation lines.
547 (goto-char (point-min))
548 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
549 (replace-match " " t t))
550 (subst-char-in-region (point-min) (point-max) ?\t ? )
551 ;; [number subject from date id references chars lines xref]
553 (goto-char (point-min))
554 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
557 (setq char (downcase (following-char)))
560 (setq subject (nnml-header-value)))
562 (setq from (nnml-header-value)))
564 (setq xref (nnml-header-value)))
566 (setq lines (nnml-header-value)))
568 (setq date (nnml-header-value)))
570 (setq id (setq id (nnml-header-value))))
572 (setq references (nnml-header-value)))
574 (setq in-reply-to (nnml-header-value))))
577 (and (not references)
579 (string-match "<[^>]+>" in-reply-to)
581 (substring in-reply-to (match-beginning 0)
583 ;; [number subject from date id references chars lines xref]
584 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
585 (or subject "(none)")
586 (or from "(nobody)") (or date "")
587 (or id (concat "nnml-dummy-id-"
589 (lambda (time) (int-to-string time))
590 (current-time) "-")))
592 (or chars 0) (or lines "0") (or xref ""))))))
594 (defun nnml-open-nov (group)
595 (or (cdr (assoc group nnml-nov-buffer-alist))
596 (let ((buffer (find-file-noselect
597 (concat (nnmail-article-pathname
598 group nnml-directory) nnml-nov-file-name))))
601 (buffer-disable-undo (current-buffer)))
602 (setq nnml-nov-buffer-alist
603 (cons (cons group buffer) nnml-nov-buffer-alist))
606 (defun nnml-save-nov ()
608 (while nnml-nov-buffer-alist
609 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
611 (set-buffer (cdr (car nnml-nov-buffer-alist)))
612 (and (buffer-modified-p)
614 1 (point-max) (buffer-file-name) nil 'nomesg))
615 (set-buffer-modified-p nil)
616 (kill-buffer (current-buffer))))
617 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
620 (defun nnml-generate-nov-databases (dir)
621 "Generate nov databases in all nnml mail newsgroups."
624 (setq nnml-group-alist nil)
625 (list nnml-directory)))
626 (nnml-open-server (or nnml-current-server ""))
627 (let ((dirs (directory-files dir t nil t)))
629 (if (and (not (string-match "/\\.\\.$" (car dirs)))
630 (not (string-match "/\\.$" (car dirs)))
631 (file-directory-p (car dirs)))
632 (nnml-generate-nov-databases (car dirs)))
633 (setq dirs (cdr dirs))))
638 (string-to-int name)))
639 (directory-files dir nil "^[0-9]+$" t))
641 (nov (concat dir "/" nnml-nov-file-name))
642 (nov-buffer (get-buffer-create "*nov*"))
645 (setq nnml-group-alist
646 (cons (list (nnmail-replace-chars-in-string
647 (substring (expand-file-name dir)
648 (length (expand-file-name
653 (while (cdr f) (setq f (cdr f)))
658 (set-buffer nntp-server-buffer)
659 (if (file-exists-p nov)
662 (set-buffer nov-buffer)
663 (buffer-disable-undo (current-buffer))
667 (insert-file-contents (concat dir "/" (int-to-string (car files))))
668 (goto-char (point-min))
669 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
670 (setq chars (- (point-max)
673 (if (not (= 0 chars)) ; none of them empty files...
675 (setq nov-line (nnml-make-nov-line chars))
677 (set-buffer nov-buffer)
678 (goto-char (point-max))
679 (insert (int-to-string (car files)) nov-line))))
681 (setq files (cdr files)))
683 (set-buffer nov-buffer)
684 (write-region 1 (point-max) (expand-file-name nov) nil
686 (kill-buffer (current-buffer)))))
687 (nnmail-save-active nnml-group-alist nnml-active-file)))
689 (defun nnml-nov-delete-article (group article)
691 (set-buffer (nnml-open-nov group))
692 (goto-char (point-min))
693 (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
694 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
699 ;;; nnml.el ends here