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
34 (eval-when-compile (require 'cl))
38 (defvar nnml-directory "~/Mail/"
39 "Mail spool directory.")
41 (defvar nnml-active-file (concat nnml-directory "active")
44 (defvar nnml-newsgroups-file (concat nnml-directory "newsgroups")
45 "Mail newsgroups description file.")
47 (defvar nnml-get-new-mail t
48 "If non-nil, nnml will check the incoming mail file and split the mail.")
50 (defvar nnml-nov-is-evil nil
51 "If non-nil, Gnus will never generate and use nov databases for mail groups.
52 Using nov databases will speed up header fetching considerably.
53 This variable shouldn't be flipped much. If you have, for some reason,
54 set this to t, and want to set it to nil again, you should always run
55 the `nnml-generate-nov-databases' command. The function will go
56 through all nnml directories and generate nov databases for them
57 all. This may very well take some time.")
59 (defvar nnml-prepare-save-mail-hook nil
60 "Hook run narrowed to an article before saving.")
64 (defconst nnml-version "nnml 1.0"
67 (defvar nnml-nov-file-name ".overview")
69 (defvar nnml-current-directory nil)
70 (defvar nnml-current-group nil)
71 (defvar nnml-status-string "")
72 (defvar nnml-nov-buffer-alist nil)
73 (defvar nnml-group-alist nil)
74 (defvar nnml-active-timestamp nil)
76 (defvar nnml-generate-active-function 'nnml-generate-active-info)
82 (defvar nnml-current-server nil)
83 (defvar nnml-server-alist nil)
84 (defvar nnml-server-variables
86 (list 'nnml-directory nnml-directory)
87 (list 'nnml-active-file nnml-active-file)
88 (list 'nnml-newsgroups-file nnml-newsgroups-file)
89 (list 'nnml-get-new-mail nnml-get-new-mail)
90 (list 'nnml-nov-is-evil nnml-nov-is-evil)
91 (list 'nnml-nov-file-name nnml-nov-file-name)
92 '(nnml-current-directory nil)
93 '(nnml-current-group nil)
94 '(nnml-status-string "")
95 '(nnml-nov-buffer-alist nil)
96 '(nnml-group-alist nil)
97 '(nnml-active-timestamp nil)))
101 ;;; Interface functions.
103 (defun nnml-retrieve-headers (sequence &optional newsgroup server fetch-old)
105 (set-buffer nntp-server-buffer)
108 (number (length sequence))
111 (if (stringp (car sequence))
113 (nnml-possibly-change-directory newsgroup)
114 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
117 (setq article (car sequence))
119 (concat nnml-current-directory (int-to-string article)))
120 (if (and (file-exists-p file)
121 (not (file-directory-p file)))
123 (insert (format "221 %d Article retrieved.\n" article))
125 (nnheader-insert-head file)
127 (if (search-forward "\n\n" nil t)
129 (goto-char (point-max))
132 (delete-region (point) (point-max))))
133 (setq sequence (cdr sequence))
134 (setq count (1+ count))
135 (and (numberp nnmail-large-newsgroup)
136 (> number nnmail-large-newsgroup)
138 gnus-verbose-backends
139 (message "nnml: Receiving headers... %d%%"
140 (/ (* count 100) number))))
142 (and (numberp nnmail-large-newsgroup)
143 (> number nnmail-large-newsgroup)
144 gnus-verbose-backends
145 (message "nnml: Receiving headers...done"))
147 ;; Fold continuation lines.
148 (goto-char (point-min))
149 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
150 (replace-match " " t t))
153 (defun nnml-open-server (server &optional defs)
154 (nnheader-init-server-buffer)
155 (if (equal server nnml-current-server)
157 (if nnml-current-server
158 (setq nnml-server-alist
159 (cons (list nnml-current-server
160 (nnheader-save-variables nnml-server-variables))
162 (let ((state (assoc server nnml-server-alist)))
165 (nnheader-restore-variables (nth 1 state))
166 (setq nnml-server-alist (delq state nnml-server-alist)))
167 (nnheader-set-init-variables nnml-server-variables defs)))
168 (setq nnml-current-server server)))
170 (defun nnml-close-server (&optional server)
171 (setq nnml-current-server nil)
174 (defun nnml-server-opened (&optional server)
175 (and (equal server nnml-current-server)
177 (buffer-name nntp-server-buffer)))
179 (defun nnml-status-message (&optional server)
182 (defun nnml-request-article (id &optional newsgroup server buffer)
183 (nnml-possibly-change-directory newsgroup)
184 (let* ((group-num (and (stringp id) (nnml-find-group-number id)))
185 (number (if (numberp id) id (cdr group-num)))
190 nnml-current-directory
191 (nnmail-group-pathname (car group-num) nnml-directory))
192 (int-to-string number))))
193 (nntp-server-buffer (or buffer nntp-server-buffer)))
196 (not (file-directory-p file))
197 (save-excursion (nnmail-find-file file))
198 ;; We return the article number.
199 (cons newsgroup (string-to-int (file-name-nondirectory file))))))
201 (defun nnml-request-group (group &optional server dont-check)
202 (if (not (nnml-possibly-change-directory group))
204 (setq nnml-status-string "Invalid group (no such directory)")
208 (nnmail-activate 'nnml)
209 (let ((active (nth 1 (assoc group nnml-group-alist))))
211 (set-buffer nntp-server-buffer)
215 (insert (format "211 %d %d %d %s\n"
216 (max (1+ (- (cdr active) (car active))) 0)
217 (car active) (cdr active) group))
220 (defun nnml-request-scan (&optional group server)
221 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
223 (defun nnml-close-group (group &optional server)
226 (defun nnml-request-close ()
227 (setq nnml-current-server nil)
228 (setq nnml-server-alist nil)
231 (defun nnml-request-create-group (group &optional server)
232 (nnmail-activate 'nnml)
233 (or (assoc group nnml-group-alist)
235 (setq nnml-group-alist (cons (list group (setq active (cons 1 0)))
237 (nnml-possibly-create-directory group)
238 (nnml-possibly-change-directory group)
240 (nnheader-directory-articles nnml-current-directory )))
243 (setcar active (apply 'min articles))
244 (setcdr active (apply 'max articles)))))
245 (nnmail-save-active nnml-group-alist nnml-active-file)))
248 (defun nnml-request-list (&optional server)
250 (nnmail-find-file nnml-active-file)
251 (setq nnml-group-alist (nnmail-get-active))))
253 (defun nnml-request-newgroups (date &optional server)
254 (nnml-request-list server))
256 (defun nnml-request-list-newsgroups (&optional server)
258 (nnmail-find-file nnml-newsgroups-file)))
260 (defun nnml-request-post (&optional server)
261 (mail-send-and-exit nil))
263 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
264 (nnml-possibly-change-directory newsgroup)
265 (let* ((active-articles
266 (nnheader-directory-articles nnml-current-directory))
267 (max-article (and active-articles (apply 'max active-articles)))
269 article rest mod-time number)
270 (nnmail-activate 'nnml)
272 (while (and articles is-old)
273 (setq article (concat nnml-current-directory
275 (setq number (pop articles)))))
276 (when (setq mod-time (nth 5 (file-attributes article)))
277 (if (and (nnml-deletable-article-p newsgroup number)
279 (nnmail-expired-article-p newsgroup mod-time force)))
281 (and gnus-verbose-backends
282 (message "Deleting article %s in %s..."
285 (funcall nnmail-delete-file-function article)
288 (setq active-articles (delq number active-articles))
289 (nnml-nov-delete-article newsgroup number))
290 (push number rest))))
291 (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
293 (setcar active (or (and active-articles
294 (apply 'min active-articles))
296 (nnmail-save-active nnml-group-alist nnml-active-file))
299 (nconc rest articles)))
301 (defun nnml-request-move-article
302 (article group server accept-form &optional last)
303 (let ((buf (get-buffer-create " *nnml move*"))
306 (nnml-deletable-article-p group article)
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 (funcall nnmail-delete-file-function
317 (concat nnml-current-directory
318 (int-to-string article)))
320 (nnml-nov-delete-article group article)
321 (and last (nnml-save-nov))))
324 (defun nnml-request-accept-article (group &optional last)
328 (nnmail-activate 'nnml)
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))))
337 (nnmail-activate 'nnml)
338 (setq result (car (nnml-save-mail)))
340 (nnmail-save-active nnml-group-alist nnml-active-file)
341 (and last (nnml-save-nov)))))
344 (defun nnml-request-replace-article (article group buffer)
345 (nnml-possibly-change-directory group)
348 (nnml-possibly-create-directory group)
349 (if (not (condition-case ()
351 (write-region (point-min) (point-max)
352 (concat nnml-current-directory
353 (int-to-string article))
354 nil (if gnus-verbose-backends nil 'nomesg))
358 (let ((chars (nnmail-insert-lines))
359 (art (concat (int-to-string article) "\t"))
361 (setq nov-line (nnml-make-nov-line chars))
362 ;; Replace the NOV line in the NOV file.
364 (set-buffer (nnml-open-nov group))
365 (goto-char (point-min))
366 (if (or (looking-at art)
367 (search-forward (concat "\n" art) nil t))
368 ;; Delete the old NOV line.
369 (delete-region (progn (beginning-of-line) (point))
370 (progn (forward-line 1) (point)))
371 ;; The line isn't here, so we have to find out where
372 ;; we should insert it. (This situation should never
373 ;; occur, but one likes to make sure...)
374 (while (and (looking-at "[0-9]+\t")
377 (match-beginning 0) (match-end 0)))
379 (zerop (forward-line 1)))))
381 (insert (int-to-string article) nov-line)
385 (defun nnml-request-delete-group (group &optional force server)
386 (nnml-possibly-change-directory group)
388 ;; Delete all articles in GROUP.
391 nnml-current-directory t
392 (concat nnheader-numerical-short-files
393 "\\|" (regexp-quote nnml-nov-file-name) "$")))
396 (setq article (pop articles))
397 (when (file-writable-p article)
398 (when gnus-verbose-backends
399 (message "Deleting article %s in %s..." article group))
400 (funcall nnmail-delete-file-function article))))
401 ;; Try to delete the directory itself.
403 (delete-directory nnml-current-directory)
405 ;; Remove the group from all structures.
406 (setq nnml-group-alist
407 (delq (assoc group nnml-group-alist) nnml-group-alist)
408 nnml-current-group nil
409 nnml-current-directory nil)
410 ;; Save the active file.
411 (nnmail-save-active nnml-group-alist nnml-active-file)
414 (defun nnml-request-rename-group (group new-name &optional server)
415 (nnml-possibly-change-directory group)
417 (and (file-writable-p nnml-current-directory)
421 (directory-file-name nnml-current-directory)
423 (nnmail-group-pathname new-name nnml-directory)))
426 ;; That went ok, so we change the internal structures.
427 (let ((entry (assoc group nnml-group-alist)))
428 (and entry (setcar entry new-name))
429 (setq nnml-current-directory nil
430 nnml-current-group nil)
431 ;; Save the new group alist.
432 (nnmail-save-active nnml-group-alist nnml-active-file)
436 ;;; Internal functions.
438 (defun nnml-deletable-article-p (group article)
439 "Say whether ARTICLE in GROUP can be deleted."
440 (or (not nnmail-keep-last-article)
441 (not (eq (cdr (nth 1 (assoc group nnml-group-alist))) article))))
443 ;; Find an article number in the current group given the Message-ID.
444 (defun nnml-find-group-number (id)
446 (set-buffer (get-buffer-create " *nnml id*"))
447 (buffer-disable-undo (current-buffer))
448 (let ((alist nnml-group-alist)
450 ;; We want to look through all .overview files, but we want to
451 ;; start with the one in the current directory. It seems most
452 ;; likely that the article we are looking for is in that group.
453 (if (setq number (nnml-find-id nnml-current-group id))
454 (cons nnml-current-group number)
455 ;; It wasn't there, so we look through the other groups as well.
456 (while (and (not number)
458 (or (string= (car (car alist)) nnml-current-group)
459 (setq number (nnml-find-id (car (car alist)) id)))
461 (setq alist (cdr alist))))
463 (cons (car (car alist)) number))))))
465 (defun nnml-find-id (group id)
467 (insert-file-contents
468 (concat (nnmail-group-pathname group nnml-directory)
471 (while (and (not found)
472 (search-forward id nil t)) ; We find the ID.
473 ;; And the id is in the fourth field.
475 "\t" (save-excursion (beginning-of-line) (point)) t 4)
479 ;; We return the article number.
482 (read (current-buffer))
487 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
488 (if (or gnus-nov-is-evil nnml-nov-is-evil)
490 (let ((first (car articles))
491 (last (progn (while (cdr articles) (setq articles (cdr articles)))
493 (nov (concat nnml-current-directory nnml-nov-file-name)))
494 (if (file-exists-p nov)
496 (set-buffer nntp-server-buffer)
498 (insert-file-contents nov)
500 (not (numberp fetch-old)))
501 t ; Don't remove anything.
503 (setq first (max 1 (- first fetch-old))))
504 (goto-char (point-min))
505 (while (and (not (eobp)) (< first (read (current-buffer))))
508 (if (not (eobp)) (delete-region 1 (point)))
509 (while (and (not (eobp)) (>= last (read (current-buffer))))
512 (if (not (eobp)) (delete-region (point) (point-max)))
515 (defun nnml-possibly-change-directory (newsgroup &optional force)
517 (let ((pathname (nnmail-group-pathname newsgroup nnml-directory)))
518 (and (or force (file-directory-p pathname))
519 (setq nnml-current-directory pathname
520 nnml-current-group newsgroup)))
523 (defun nnml-possibly-create-directory (group)
525 (setq dir (nnmail-group-pathname group nnml-directory))
526 (while (not (file-directory-p dir))
527 (setq dirs (cons dir dirs))
528 (setq dir (file-name-directory (directory-file-name dir))))
530 (make-directory (directory-file-name (car dirs)))
531 (and gnus-verbose-backends
532 (message "Creating mail directory %s" (car dirs)))
533 (setq dirs (cdr dirs)))))
535 (defun nnml-save-mail ()
536 "Called narrowed to an article."
537 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
539 (setq chars (nnmail-insert-lines))
540 (nnmail-insert-xref group-art)
541 (run-hooks 'nnml-prepare-save-mail-hook)
542 (goto-char (point-min))
543 (while (looking-at "From ")
544 (replace-match "X-From-Line: ")
546 ;; We save the article in all the newsgroups it belongs in.
550 (nnml-possibly-create-directory (car (car ga)))
551 (let ((file (concat (nnmail-group-pathname
552 (car (car ga)) nnml-directory)
553 (int-to-string (cdr (car ga))))))
555 ;; It was already saved, so we just make a hard link.
556 (add-name-to-file first file t)
558 (write-region (point-min) (point-max) file nil
559 (if gnus-verbose-backends nil 'nomesg))
562 ;; Generate a nov line for this article. We generate the nov
563 ;; line after saving, because nov generation destroys the
565 (setq nov-line (nnml-make-nov-line chars))
566 ;; Output the nov line to all nov databases that should have it.
567 (let ((ga group-art))
569 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
573 (defun nnml-active-number (group)
574 "Compute the next article number in GROUP."
575 (let ((active (car (cdr (assoc group nnml-group-alist)))))
576 ;; The group wasn't known to nnml, so we just create an active
580 (setq active (cons 1 0))
581 (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
582 (setcdr active (1+ (cdr active)))
583 (while (file-exists-p
584 (concat (nnmail-group-pathname group nnml-directory)
585 (int-to-string (cdr active))))
586 (setcdr active (1+ (cdr active))))
589 (defun nnml-add-nov (group article line)
590 "Add a nov line for the GROUP base."
592 (set-buffer (nnml-open-nov group))
593 (goto-char (point-max))
594 (insert (int-to-string article) line)))
596 (defsubst nnml-header-value ()
597 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
599 (defun nnml-make-nov-line (chars)
600 "Create a nov from the current headers."
601 (let ((case-fold-search t)
602 subject from date id references lines xref in-reply-to char)
605 (goto-char (point-min))
608 (1- (or (search-forward "\n\n" nil t) (point-max))))
609 ;; Fold continuation lines.
610 (goto-char (point-min))
611 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
612 (replace-match " " t t))
613 (subst-char-in-region (point-min) (point-max) ?\t ? )
614 ;; [number subject from date id references chars lines xref]
616 (goto-char (point-min))
617 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
620 (setq char (downcase (following-char)))
623 (setq subject (nnml-header-value)))
625 (setq from (nnml-header-value)))
627 (setq xref (buffer-substring (match-beginning 0)
628 (progn (end-of-line) (point)))))
630 (setq lines (nnml-header-value)))
632 (setq date (nnml-header-value)))
634 (setq id (setq id (nnml-header-value))))
636 (setq references (nnml-header-value)))
638 (setq in-reply-to (nnml-header-value))))
641 (and (not references)
643 (string-match "<[^>]+>" in-reply-to)
645 (substring in-reply-to (match-beginning 0)
647 ;; [number subject from date id references chars lines xref]
648 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
649 (or subject "(none)")
650 (or from "(nobody)") (or date "")
651 (or id (concat "nnml-dummy-id-"
653 (lambda (time) (int-to-string time))
654 (current-time) "-")))
656 (or chars 0) (or lines "0")
659 (defun nnml-open-nov (group)
660 (or (cdr (assoc group nnml-nov-buffer-alist))
661 (let ((buffer (find-file-noselect
662 (concat (nnmail-group-pathname group nnml-directory)
663 nnml-nov-file-name))))
666 (buffer-disable-undo (current-buffer)))
667 (setq nnml-nov-buffer-alist
668 (cons (cons group buffer) nnml-nov-buffer-alist))
671 (defun nnml-save-nov ()
673 (while nnml-nov-buffer-alist
674 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
676 (set-buffer (cdr (car nnml-nov-buffer-alist)))
677 (and (buffer-modified-p)
679 1 (point-max) (buffer-file-name) nil 'nomesg))
680 (set-buffer-modified-p nil)
681 (kill-buffer (current-buffer))))
682 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
685 (defun nnml-generate-nov-databases ()
686 "Generate nov databases in all nnml directories."
688 ;; Read the active file to make sure we don't re-use articles
689 ;; numbers in empty groups.
690 (nnmail-activate 'nnml)
691 (nnml-open-server (or nnml-current-server ""))
692 (setq nnml-directory (expand-file-name nnml-directory))
693 ;; Recurse down the directories.
694 (nnml-generate-nov-databases-1 nnml-directory)
695 ;; Save the active file.
696 (nnmail-save-active nnml-group-alist nnml-active-file))
698 (defun nnml-generate-nov-databases-1 (dir)
699 (setq dir (file-name-as-directory dir))
700 ;; We descend recursively
701 (let ((dirs (directory-files dir t nil t))
704 (setq dir (pop dirs))
705 (when (and (not (string-match "/\\.\\.?$" dir))
706 (file-directory-p dir))
707 (nnml-generate-nov-databases-1 dir))))
708 ;; Do this directory.
711 (lambda (name) (string-to-int name))
712 (directory-files dir nil "^[0-9]+$" t))
715 (funcall nnml-generate-active-function dir)
716 ;; Generate the nov file.
717 (nnml-generate-nov-file dir files))))
719 (defun nnml-generate-active-info (dir)
720 ;; Update the active info for this group.
721 (let ((group (nnmail-replace-chars-in-string
722 (substring dir (length nnml-directory))
724 (setq nnml-group-alist (delq (assoc group nnml-group-alist) nnml-group-alist))
728 (while (cdr f) (setq f (cdr f)))
732 (defun nnml-generate-nov-file (dir files)
733 (let* ((dir (file-name-as-directory dir))
734 (nov (concat dir nnml-nov-file-name))
735 (nov-buffer (get-buffer-create " *nov*"))
738 ;; Init the nov buffer.
739 (set-buffer nov-buffer)
740 (buffer-disable-undo (current-buffer))
742 (set-buffer nntp-server-buffer)
743 ;; Delete the old NOV file.
744 (when (file-exists-p nov)
745 (funcall nnmail-delete-file-function nov))
748 (insert-file-contents (concat dir (int-to-string (car files))))
750 (goto-char (point-min))
752 (search-forward "\n\n" nil t)
753 (setq chars (- (point-max) (point)))
754 (max 1 (1- (point)))))
755 (when (and (not (= 0 chars)) ; none of them empty files...
756 (not (= (point-min) (point-max))))
757 (goto-char (point-min))
758 (setq nov-line (nnml-make-nov-line chars))
760 (set-buffer nov-buffer)
761 (goto-char (point-max))
762 (insert (int-to-string (car files)) nov-line)))
764 (setq files (cdr files)))
766 (set-buffer nov-buffer)
767 (write-region 1 (point-max) (expand-file-name nov) nil
769 (kill-buffer (current-buffer))))))
771 (defun nnml-nov-delete-article (group article)
773 (set-buffer (nnml-open-nov group))
774 (goto-char (point-min))
775 (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
776 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
781 ;;; nnml.el ends here