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 1.0"
64 (defvar nnml-nov-file-name ".overview")
66 (defvar nnml-current-directory nil)
67 (defvar nnml-current-group nil)
68 (defvar nnml-status-string "")
69 (defvar nnml-nov-buffer-alist nil)
70 (defvar nnml-group-alist nil)
71 (defvar nnml-active-timestamp nil)
73 (defvar nnml-generate-active-function 'nnml-generate-active-info)
79 (defvar nnml-current-server nil)
80 (defvar nnml-server-alist nil)
81 (defvar nnml-server-variables
83 (list 'nnml-directory nnml-directory)
84 (list 'nnml-active-file nnml-active-file)
85 (list 'nnml-newsgroups-file nnml-newsgroups-file)
86 (list 'nnml-get-new-mail nnml-get-new-mail)
87 (list 'nnml-nov-is-evil nnml-nov-is-evil)
88 (list 'nnml-nov-file-name nnml-nov-file-name)
89 '(nnml-current-directory nil)
90 '(nnml-current-group nil)
91 '(nnml-status-string "")
92 '(nnml-nov-buffer-alist nil)
93 '(nnml-group-alist nil)
94 '(nnml-active-timestamp nil)))
98 ;;; Interface functions.
100 (defun nnml-retrieve-headers (sequence &optional newsgroup server fetch-old)
102 (set-buffer nntp-server-buffer)
105 (number (length sequence))
108 (if (stringp (car sequence))
110 (nnml-possibly-change-directory newsgroup)
111 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
114 (setq article (car sequence))
116 (concat nnml-current-directory (int-to-string article)))
117 (if (and (file-exists-p file)
118 (not (file-directory-p file)))
120 (insert (format "221 %d Article retrieved.\n" article))
122 (nnheader-insert-head file)
124 (if (search-forward "\n\n" nil t)
126 (goto-char (point-max))
129 (delete-region (point) (point-max))))
130 (setq sequence (cdr sequence))
131 (setq count (1+ count))
132 (and (numberp nnmail-large-newsgroup)
133 (> number nnmail-large-newsgroup)
135 gnus-verbose-backends
136 (message "nnml: Receiving headers... %d%%"
137 (/ (* count 100) number))))
139 (and (numberp nnmail-large-newsgroup)
140 (> number nnmail-large-newsgroup)
141 gnus-verbose-backends
142 (message "nnml: Receiving headers...done"))
144 ;; Fold continuation lines.
145 (goto-char (point-min))
146 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
147 (replace-match " " t t))
150 (defun nnml-open-server (server &optional defs)
151 (nnheader-init-server-buffer)
152 (if (equal server nnml-current-server)
154 (if nnml-current-server
155 (setq nnml-server-alist
156 (cons (list nnml-current-server
157 (nnheader-save-variables nnml-server-variables))
159 (let ((state (assoc server nnml-server-alist)))
162 (nnheader-restore-variables (nth 1 state))
163 (setq nnml-server-alist (delq state nnml-server-alist)))
164 (nnheader-set-init-variables nnml-server-variables defs)))
165 (setq nnml-current-server server)))
167 (defun nnml-close-server (&optional server)
168 (setq nnml-current-server nil)
171 (defun nnml-server-opened (&optional server)
172 (and (equal server nnml-current-server)
174 (buffer-name nntp-server-buffer)))
176 (defun nnml-status-message (&optional server)
179 (defun nnml-request-article (id &optional newsgroup server buffer)
180 (nnml-possibly-change-directory newsgroup)
181 (let* ((group-num (and (stringp id) (nnml-find-group-number id)))
182 (number (if (numberp id) id (cdr group-num)))
187 nnml-current-directory
188 (nnmail-group-pathname (car group-num) nnml-directory))
189 (int-to-string number))))
190 (nntp-server-buffer (or buffer nntp-server-buffer)))
193 (not (file-directory-p file))
194 (save-excursion (nnmail-find-file file))
195 ;; We return the article number.
196 (cons newsgroup (string-to-int (file-name-nondirectory file))))))
198 (defun nnml-request-group (group &optional server dont-check)
199 (if (not (nnml-possibly-change-directory group))
201 (setq nnml-status-string "Invalid group (no such directory)")
205 (nnmail-activate 'nnml)
206 (let ((active (nth 1 (assoc group nnml-group-alist))))
208 (set-buffer nntp-server-buffer)
212 (insert (format "211 %d %d %d %s\n"
213 (max (1+ (- (cdr active) (car active))) 0)
214 (car active) (cdr active) group))
217 (defun nnml-request-scan (&optional group server)
218 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
220 (defun nnml-close-group (group &optional server)
223 (defun nnml-request-close ()
224 (setq nnml-current-server nil)
225 (setq nnml-server-alist nil)
228 (defun nnml-request-create-group (group &optional server)
229 (nnmail-activate 'nnml)
230 (or (assoc group nnml-group-alist)
232 (setq nnml-group-alist (cons (list group (setq active (cons 1 0)))
234 (nnml-possibly-create-directory group)
235 (nnml-possibly-change-directory group)
237 (nnheader-directory-articles nnml-current-directory )))
240 (setcar active (apply 'min articles))
241 (setcdr active (apply 'max articles)))))
242 (nnmail-save-active nnml-group-alist nnml-active-file)))
245 (defun nnml-request-list (&optional server)
247 (nnmail-find-file nnml-active-file)
248 (setq nnml-group-alist (nnmail-get-active))))
250 (defun nnml-request-newgroups (date &optional server)
251 (nnml-request-list server))
253 (defun nnml-request-list-newsgroups (&optional server)
255 (nnmail-find-file nnml-newsgroups-file)))
257 (defun nnml-request-post (&optional server)
258 (mail-send-and-exit nil))
260 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
261 (nnml-possibly-change-directory newsgroup)
262 (let* ((active-articles
263 (nnheader-directory-articles nnml-current-directory))
264 (max-article (and active-articles (apply 'max active-articles)))
266 article rest mod-time number)
267 (nnmail-activate 'nnml)
269 (while (and articles is-old)
270 (setq article (concat nnml-current-directory
272 (setq number (pop articles)))))
273 (when (setq mod-time (nth 5 (file-attributes article)))
274 (if (and (nnml-deletable-article-p newsgroup number)
276 (nnmail-expired-article-p newsgroup mod-time force)))
278 (and gnus-verbose-backends
279 (message "Deleting article %s in %s..."
282 (funcall nnmail-delete-file-function article)
285 (setq active-articles (delq number active-articles))
286 (nnml-nov-delete-article newsgroup number))
287 (push number rest))))
288 (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
290 (setcar active (or (and active-articles
291 (apply 'min active-articles))
293 (nnmail-save-active nnml-group-alist nnml-active-file))
296 (nconc rest articles)))
298 (defun nnml-request-move-article
299 (article group server accept-form &optional last)
300 (let ((buf (get-buffer-create " *nnml move*"))
303 (nnml-deletable-article-p group article)
304 (nnml-request-article article group server)
307 (insert-buffer-substring nntp-server-buffer)
308 (setq result (eval accept-form))
309 (kill-buffer (current-buffer))
313 (funcall nnmail-delete-file-function
314 (concat nnml-current-directory
315 (int-to-string article)))
317 (nnml-nov-delete-article group article)
318 (and last (nnml-save-nov))))
321 (defun nnml-request-accept-article (group &optional last)
325 (nnmail-activate 'nnml)
326 ;; We trick the choosing function into believing that only one
327 ;; group is availiable.
328 (let ((nnmail-split-methods (list (list group ""))))
329 (setq result (car (nnml-save-mail))))
331 (nnmail-save-active nnml-group-alist nnml-active-file)
332 (and last (nnml-save-nov))))
334 (nnmail-activate 'nnml)
335 (setq result (car (nnml-save-mail)))
337 (nnmail-save-active nnml-group-alist nnml-active-file)
338 (and last (nnml-save-nov)))))
341 (defun nnml-request-replace-article (article group buffer)
342 (nnml-possibly-change-directory group)
345 (nnml-possibly-create-directory group)
346 (if (not (condition-case ()
348 (write-region (point-min) (point-max)
349 (concat nnml-current-directory
350 (int-to-string article))
351 nil (if gnus-verbose-backends nil 'nomesg))
355 (let ((chars (nnmail-insert-lines))
356 (art (concat (int-to-string article) "\t"))
358 (setq nov-line (nnml-make-nov-line chars))
359 ;; Replace the NOV line in the NOV file.
361 (set-buffer (nnml-open-nov group))
362 (goto-char (point-min))
363 (if (or (looking-at art)
364 (search-forward (concat "\n" art) nil t))
365 ;; Delete the old NOV line.
366 (delete-region (progn (beginning-of-line) (point))
367 (progn (forward-line 1) (point)))
368 ;; The line isn't here, so we have to find out where
369 ;; we should insert it. (This situation should never
370 ;; occur, but one likes to make sure...)
371 (while (and (looking-at "[0-9]+\t")
374 (match-beginning 0) (match-end 0)))
376 (zerop (forward-line 1)))))
378 (insert (int-to-string article) nov-line)
382 (defun nnml-request-delete-group (group &optional force server)
383 (nnml-possibly-change-directory group)
385 ;; Delete all articles in GROUP.
388 nnml-current-directory t
389 (concat nnheader-numerical-short-files
390 "\\|" (regexp-quote nnml-nov-file-name) "$")))
393 (setq article (pop articles))
394 (when (file-writable-p article)
395 (when gnus-verbose-backends
396 (message "Deleting article %s in %s..." article group))
397 (funcall nnmail-delete-file-function article))))
398 ;; Try to delete the directory itself.
400 (delete-directory nnml-current-directory)
402 ;; Remove the group from all structures.
403 (setq nnml-group-alist
404 (delq (assoc group nnml-group-alist) nnml-group-alist)
405 nnml-current-group nil
406 nnml-current-directory nil)
407 ;; Save the active file.
408 (nnmail-save-active nnml-group-alist nnml-active-file)
411 (defun nnml-request-rename-group (group new-name &optional server)
412 (nnml-possibly-change-directory group)
414 (and (file-writable-p nnml-current-directory)
418 (directory-file-name nnml-current-directory)
420 (nnmail-group-pathname new-name nnml-directory)))
423 ;; That went ok, so we change the internal structures.
424 (let ((entry (assoc group nnml-group-alist)))
425 (and entry (setcar entry new-name))
426 (setq nnml-current-directory nil
427 nnml-current-group nil)
428 ;; Save the new group alist.
429 (nnmail-save-active nnml-group-alist nnml-active-file)
433 ;;; Internal functions.
435 (defun nnml-deletable-article-p (group article)
436 "Say whether ARTICLE in GROUP can be deleted."
437 (or (not nnmail-keep-last-article)
438 (not (eq (cdr (nth 1 (assoc group nnml-group-alist))) article))))
440 ;; Find an article number in the current group given the Message-ID.
441 (defun nnml-find-group-number (id)
443 (set-buffer (get-buffer-create " *nnml id*"))
444 (buffer-disable-undo (current-buffer))
445 (let ((alist nnml-group-alist)
447 ;; We want to look through all .overview files, but we want to
448 ;; start with the one in the current directory. It seems most
449 ;; likely that the article we are looking for is in that group.
450 (if (setq number (nnml-find-id nnml-current-group id))
451 (cons nnml-current-group number)
452 ;; It wasn't there, so we look through the other groups as well.
453 (while (and (not number)
455 (or (string= (car (car alist)) nnml-current-group)
456 (setq number (nnml-find-id (car (car alist)) id)))
458 (setq alist (cdr alist))))
460 (cons (car (car alist)) number))))))
462 (defun nnml-find-id (group id)
464 (insert-file-contents
465 (concat (nnmail-group-pathname group nnml-directory)
468 (while (and (not found)
469 (search-forward id nil t)) ; We find the ID.
470 ;; And the id is in the fourth field.
472 "\t" (save-excursion (beginning-of-line) (point)) t 4)
476 ;; We return the article number.
479 (read (current-buffer))
484 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
485 (if (or gnus-nov-is-evil nnml-nov-is-evil)
487 (let ((first (car articles))
488 (last (progn (while (cdr articles) (setq articles (cdr articles)))
490 (nov (concat nnml-current-directory nnml-nov-file-name)))
491 (if (file-exists-p nov)
493 (set-buffer nntp-server-buffer)
495 (insert-file-contents nov)
497 (not (numberp fetch-old)))
498 t ; Don't remove anything.
500 (setq first (max 1 (- first fetch-old))))
501 (goto-char (point-min))
502 (while (and (not (eobp)) (< first (read (current-buffer))))
505 (if (not (eobp)) (delete-region 1 (point)))
506 (while (and (not (eobp)) (>= last (read (current-buffer))))
509 (if (not (eobp)) (delete-region (point) (point-max)))
512 (defun nnml-possibly-change-directory (newsgroup &optional force)
514 (let ((pathname (nnmail-group-pathname newsgroup nnml-directory)))
515 (and (or force (file-directory-p pathname))
516 (setq nnml-current-directory pathname
517 nnml-current-group newsgroup)))
520 (defun nnml-possibly-create-directory (group)
522 (setq dir (nnmail-group-pathname group nnml-directory))
523 (while (not (file-directory-p dir))
524 (setq dirs (cons dir dirs))
525 (setq dir (file-name-directory (directory-file-name dir))))
527 (make-directory (directory-file-name (car dirs)))
528 (and gnus-verbose-backends
529 (message "Creating mail directory %s" (car dirs)))
530 (setq dirs (cdr dirs)))))
532 (defun nnml-save-mail ()
533 "Called narrowed to an article."
534 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
536 (setq chars (nnmail-insert-lines))
537 (nnmail-insert-xref group-art)
538 (run-hooks 'nnml-prepare-save-mail-hook)
539 (goto-char (point-min))
540 (while (looking-at "From ")
541 (replace-match "X-From-Line: ")
543 ;; We save the article in all the newsgroups it belongs in.
547 (nnml-possibly-create-directory (car (car ga)))
548 (let ((file (concat (nnmail-group-pathname
549 (car (car ga)) nnml-directory)
550 (int-to-string (cdr (car ga))))))
552 ;; It was already saved, so we just make a hard link.
553 (add-name-to-file first file t)
555 (write-region (point-min) (point-max) file nil
556 (if gnus-verbose-backends nil 'nomesg))
559 ;; Generate a nov line for this article. We generate the nov
560 ;; line after saving, because nov generation destroys the
562 (setq nov-line (nnml-make-nov-line chars))
563 ;; Output the nov line to all nov databases that should have it.
564 (let ((ga group-art))
566 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
570 (defun nnml-active-number (group)
571 "Compute the next article number in GROUP."
572 (let ((active (car (cdr (assoc group nnml-group-alist)))))
573 ;; The group wasn't known to nnml, so we just create an active
577 (setq active (cons 1 0))
578 (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
579 (setcdr active (1+ (cdr active)))
580 (while (file-exists-p
581 (concat (nnmail-group-pathname group nnml-directory)
582 (int-to-string (cdr active))))
583 (setcdr active (1+ (cdr active))))
586 (defun nnml-add-nov (group article line)
587 "Add a nov line for the GROUP base."
589 (set-buffer (nnml-open-nov group))
590 (goto-char (point-max))
591 (insert (int-to-string article) line)))
593 (defsubst nnml-header-value ()
594 (buffer-substring (match-end 0) (progn (end-of-line) (point))))
596 (defun nnml-make-nov-line (chars)
597 "Create a nov from the current headers."
598 (let ((case-fold-search t)
599 subject from date id references lines xref in-reply-to char)
602 (goto-char (point-min))
605 (1- (or (search-forward "\n\n" nil t) (point-max))))
606 ;; Fold continuation lines.
607 (goto-char (point-min))
608 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
609 (replace-match " " t t))
610 (subst-char-in-region (point-min) (point-max) ?\t ? )
611 ;; [number subject from date id references chars lines xref]
613 (goto-char (point-min))
614 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
617 (setq char (downcase (following-char)))
620 (setq subject (nnml-header-value)))
622 (setq from (nnml-header-value)))
624 (setq xref (buffer-substring (match-beginning 0)
625 (progn (end-of-line) (point)))))
627 (setq lines (nnml-header-value)))
629 (setq date (nnml-header-value)))
631 (setq id (setq id (nnml-header-value))))
633 (setq references (nnml-header-value)))
635 (setq in-reply-to (nnml-header-value))))
638 (and (not references)
640 (string-match "<[^>]+>" in-reply-to)
642 (substring in-reply-to (match-beginning 0)
644 ;; [number subject from date id references chars lines xref]
645 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
646 (or subject "(none)")
647 (or from "(nobody)") (or date "")
648 (or id (concat "nnml-dummy-id-"
650 (lambda (time) (int-to-string time))
651 (current-time) "-")))
653 (or chars 0) (or lines "0")
656 (defun nnml-open-nov (group)
657 (or (cdr (assoc group nnml-nov-buffer-alist))
658 (let ((buffer (find-file-noselect
659 (concat (nnmail-group-pathname group nnml-directory)
660 nnml-nov-file-name))))
663 (buffer-disable-undo (current-buffer)))
664 (setq nnml-nov-buffer-alist
665 (cons (cons group buffer) nnml-nov-buffer-alist))
668 (defun nnml-save-nov ()
670 (while nnml-nov-buffer-alist
671 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
673 (set-buffer (cdr (car nnml-nov-buffer-alist)))
674 (and (buffer-modified-p)
676 1 (point-max) (buffer-file-name) nil 'nomesg))
677 (set-buffer-modified-p nil)
678 (kill-buffer (current-buffer))))
679 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
682 (defun nnml-generate-nov-databases ()
683 "Generate nov databases in all nnml directories."
685 ;; Read the active file to make sure we don't re-use articles
686 ;; numbers in empty groups.
687 (nnmail-activate 'nnml)
688 (nnml-open-server (or nnml-current-server ""))
689 (setq nnml-directory (expand-file-name nnml-directory))
690 ;; Recurse down the directories.
691 (nnml-generate-nov-databases-1 nnml-directory)
692 ;; Save the active file.
693 (nnmail-save-active nnml-group-alist nnml-active-file))
695 (defun nnml-generate-nov-databases-1 (dir)
696 (setq dir (file-name-as-directory dir))
697 ;; We descend recursively
698 (let ((dirs (directory-files dir t nil t))
701 (setq dir (pop dirs))
702 (when (and (not (string-match "/\\.\\.?$" dir))
703 (file-directory-p dir))
704 (nnml-generate-nov-databases-1 dir))))
705 ;; Do this directory.
708 (lambda (name) (string-to-int name))
709 (directory-files dir nil "^[0-9]+$" t))
712 (funcall nnml-generate-active-function dir)
713 ;; Generate the nov file.
714 (nnml-generate-nov-file dir files))))
716 (defun nnml-generate-active-info (dir)
717 ;; Update the active info for this group.
718 (let ((group (nnmail-replace-chars-in-string
719 (substring dir (length nnml-directory))
721 (setq nnml-group-alist (delq (assoc group nnml-group-alist)))
725 (while (cdr f) (setq f (cdr f)))
729 (defun nnml-generate-nov-file (dir files)
730 (let ((nov (concat dir "/" nnml-nov-file-name))
731 (nov-buffer (get-buffer-create " *nov*"))
734 ;; Init the nov buffer.
735 (set-buffer nov-buffer)
736 (buffer-disable-undo (current-buffer))
738 (set-buffer nntp-server-buffer)
739 ;; Delete the old NOV file.
740 (when (file-exists-p nov)
741 (funcall nnmail-delete-file-function nov))
744 (insert-file-contents (concat dir "/" (int-to-string (car files))))
746 (goto-char (point-min))
748 (search-forward "\n\n" nil t)
749 (setq chars (- (point-max) (point)))
751 (when (and (not (= 0 chars)) ; none of them empty files...
752 (not (= (point-min) (point-max))))
753 (goto-char (point-min))
754 (setq nov-line (nnml-make-nov-line chars))
756 (set-buffer nov-buffer)
757 (goto-char (point-max))
758 (insert (int-to-string (car files)) nov-line)))
760 (setq files (cdr files)))
762 (set-buffer nov-buffer)
763 (write-region 1 (point-max) (expand-file-name nov) nil
765 (kill-buffer (current-buffer))))))
767 (defun nnml-nov-delete-article (group article)
769 (set-buffer (nnml-open-nov group))
770 (goto-char (point-min))
771 (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
772 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
777 ;;; nnml.el ends here