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)
77 (defvar nnml-current-server nil)
78 (defvar nnml-server-alist nil)
79 (defvar nnml-server-variables
81 (list 'nnml-directory nnml-directory)
82 (list 'nnml-active-file nnml-active-file)
83 (list 'nnml-newsgroups-file nnml-newsgroups-file)
84 (list 'nnml-get-new-mail nnml-get-new-mail)
85 (list 'nnml-nov-is-evil nnml-nov-is-evil)
86 (list 'nnml-nov-file-name nnml-nov-file-name)
87 '(nnml-current-directory nil)
88 '(nnml-current-group nil)
89 '(nnml-status-string "")
90 '(nnml-nov-buffer-alist nil)
91 '(nnml-group-alist nil)
92 '(nnml-active-timestamp nil)))
96 ;;; Interface functions.
98 (defun nnml-retrieve-headers (sequence &optional newsgroup server fetch-old)
100 (set-buffer nntp-server-buffer)
103 (number (length sequence))
106 (if (stringp (car sequence))
108 (nnml-possibly-change-directory newsgroup)
109 (if (nnml-retrieve-headers-with-nov sequence fetch-old)
112 (setq article (car sequence))
114 (concat nnml-current-directory (int-to-string article)))
115 (if (and (file-exists-p file)
116 (not (file-directory-p file)))
118 (insert (format "221 %d Article retrieved.\n" article))
120 (nnheader-insert-head file)
122 (if (search-forward "\n\n" nil t)
124 (goto-char (point-max))
127 (delete-region (point) (point-max))))
128 (setq sequence (cdr sequence))
129 (setq count (1+ count))
130 (and (numberp nnmail-large-newsgroup)
131 (> number nnmail-large-newsgroup)
133 gnus-verbose-backends
134 (message "nnml: Receiving headers... %d%%"
135 (/ (* count 100) number))))
137 (and (numberp nnmail-large-newsgroup)
138 (> number nnmail-large-newsgroup)
139 gnus-verbose-backends
140 (message "nnml: Receiving headers...done"))
142 ;; Fold continuation lines.
143 (goto-char (point-min))
144 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
145 (replace-match " " t t))
148 (defun nnml-open-server (server &optional defs)
149 (nnheader-init-server-buffer)
150 (if (equal server nnml-current-server)
152 (if nnml-current-server
153 (setq nnml-server-alist
154 (cons (list nnml-current-server
155 (nnheader-save-variables nnml-server-variables))
157 (let ((state (assoc server nnml-server-alist)))
160 (nnheader-restore-variables (nth 1 state))
161 (setq nnml-server-alist (delq state nnml-server-alist)))
162 (nnheader-set-init-variables nnml-server-variables defs)))
163 (setq nnml-current-server server)))
165 (defun nnml-close-server (&optional server)
168 (defun nnml-server-opened (&optional server)
169 (and (equal server nnml-current-server)
171 (buffer-name nntp-server-buffer)))
173 (defun nnml-status-message (&optional server)
176 (defun nnml-request-article (id &optional newsgroup server buffer)
177 (nnml-possibly-change-directory newsgroup)
178 (let* ((group-num (and (stringp id) (nnml-find-group-number id)))
179 (number (if (numberp id) id (cdr group-num)))
183 nnml-current-directory
185 (int-to-string number)
187 (nntp-server-buffer (or buffer nntp-server-buffer)))
190 (not (file-directory-p file))
191 (save-excursion (nnmail-find-file file))
192 ;; We return the article number.
193 (cons newsgroup (string-to-int (file-name-nondirectory file))))))
195 (defun nnml-request-group (group &optional server dont-check)
196 (if (not (nnml-possibly-change-directory group))
198 (setq nnml-status-string "Invalid group (no such directory)")
202 (nnmail-activate 'nnml)
203 (let ((active (nth 1 (assoc group nnml-group-alist))))
205 (set-buffer nntp-server-buffer)
209 (insert (format "211 %d %d %d %s\n"
210 (max (1+ (- (cdr active) (car active))) 0)
211 (car active) (cdr active) group))
214 (defun nnml-request-scan (&optional group server)
215 (nnmail-get-new-mail 'nnml 'nnml-save-nov nnml-directory group))
217 (defun nnml-close-group (group &optional server)
220 (defun nnml-request-close ()
221 (setq nnml-current-server nil)
222 (setq nnml-server-alist nil)
225 (defun nnml-request-create-group (group &optional server)
226 (nnmail-activate 'nnml)
227 (or (assoc group nnml-group-alist)
229 (setq nnml-group-alist (cons (list group (setq active (cons 1 0)))
231 (nnml-possibly-create-directory group)
232 (nnml-possibly-change-directory group)
233 (let ((articles (mapcar
235 (string-to-int file))
237 nnml-current-directory nil "^[0-9]+$"))))
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* ((days (or (and nnmail-expiry-wait-function
263 (funcall nnmail-expiry-wait-function newsgroup))
269 (string-to-int name)))
270 (directory-files nnml-current-directory nil "^[0-9]+$" t)))
271 (max-article (and active-articles (apply 'max active-articles)))
273 article rest mod-time)
274 (nnmail-activate 'nnml)
276 (while (and articles is-old)
277 (setq article (concat nnml-current-directory
278 (int-to-string (car articles))))
279 (if (setq mod-time (nth 5 (file-attributes article)))
280 (if (and (or (not nnmail-keep-last-article)
282 (not (= (car articles) max-article)))
284 (and (not (equal mod-time '(0 0)))
286 (> (nnmail-days-between
287 (current-time-string)
288 (current-time-string mod-time))
291 (and gnus-verbose-backends
292 (message "Deleting article %s in %s..."
295 (funcall nnmail-delete-file-function article)
297 (setq rest (cons (car articles) rest))))
298 (setq active-articles (delq (car articles) active-articles))
299 (nnml-nov-delete-article newsgroup (car articles)))
300 (setq rest (cons (car articles) rest))))
301 (setq articles (cdr articles)))
302 (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
304 (setcar active (or (and active-articles
305 (apply 'min active-articles))
307 (nnmail-save-active nnml-group-alist nnml-active-file))
310 (nconc rest articles)))
312 (defun nnml-request-move-article
313 (article group server accept-form &optional last)
314 (let ((buf (get-buffer-create " *nnml move*"))
317 (nnml-request-article article group server)
320 (insert-buffer-substring nntp-server-buffer)
321 (setq result (eval accept-form))
322 (kill-buffer (current-buffer))
326 (funcall nnmail-delete-file-function
327 (concat nnml-current-directory
328 (int-to-string article)))
330 (nnml-nov-delete-article group article)
331 (and last (nnml-save-nov))))
334 (defun nnml-request-accept-article (group &optional last)
338 (nnmail-activate 'nnml)
339 ;; We trick the choosing function into believing that only one
340 ;; group is availiable.
341 (let ((nnmail-split-methods (list (list group ""))))
342 (setq result (car (nnml-save-mail))))
344 (nnmail-save-active nnml-group-alist nnml-active-file)
345 (and last (nnml-save-nov))))
347 (nnmail-activate 'nnml)
348 (setq result (car (nnml-save-mail)))
350 (nnmail-save-active nnml-group-alist nnml-active-file)
351 (and last (nnml-save-nov)))))
354 (defun nnml-request-replace-article (article group buffer)
355 (nnml-possibly-change-directory group)
358 (nnml-possibly-create-directory group)
359 (if (not (condition-case ()
361 (write-region (point-min) (point-max)
362 (concat nnml-current-directory
363 (int-to-string article))
364 nil (if gnus-verbose-backends nil 'nomesg))
368 (let ((chars (nnmail-insert-lines))
369 (art (concat (int-to-string article) "\t"))
371 (setq nov-line (nnml-make-nov-line chars))
372 ;; Replace the NOV line in the NOV file.
374 (set-buffer (nnml-open-nov group))
375 (goto-char (point-min))
376 (if (or (looking-at art)
377 (search-forward (concat "\n" art) nil t))
378 ;; Delete the old NOV line.
379 (delete-region (progn (beginning-of-line) (point))
380 (progn (forward-line 1) (point)))
381 ;; The line isn't here, so we have to find out where
382 ;; we should insert it. (This situation should never
383 ;; occur, but one likes to make sure...)
384 (while (and (looking-at "[0-9]+\t")
387 (match-beginning 0) (match-end 0)))
389 (zerop (forward-line 1)))))
391 (insert (int-to-string article) nov-line)
395 (defun nnml-request-delete-group (group &optional force server)
396 (nnml-possibly-change-directory group)
397 ;; Delete all articles in GROUP.
399 () ; Don't delete the articles.
402 nnml-current-directory t
403 (concat "^[0-9]+$\\|" (regexp-quote nnml-nov-file-name) "$"))))
405 (and (file-writable-p (car articles))
407 (and gnus-verbose-backends
408 (message (message "Deleting article %s in %s..."
409 (car articles) group)))
410 (funcall nnmail-delete-file-function (car articles))))
411 (setq articles (cdr articles))))
412 ;; Try to delete the directory itself.
414 (delete-directory nnml-current-directory)
416 ;; Remove the group from all structures.
417 (setq nnml-group-alist
418 (delq (assoc group nnml-group-alist) nnml-group-alist)
419 nnml-current-group nil
420 nnml-current-directory nil)
421 ;; Save the active file.
422 (nnmail-save-active nnml-group-alist nnml-active-file)
425 (defun nnml-request-rename-group (group new-name &optional server)
426 (nnml-possibly-change-directory group)
428 (and (file-writable-p nnml-current-directory)
432 (directory-file-name nnml-current-directory)
434 (nnmail-article-pathname new-name nnml-directory)))
437 ;; That went ok, so we change the internal structures.
438 (let ((entry (assoc group nnml-group-alist)))
439 (and entry (setcar entry new-name))
440 (setq nnml-current-directory nil
441 nnml-current-group nil)
442 ;; Save the new group alist.
443 (nnmail-save-active nnml-group-alist nnml-active-file)
447 ;;; Internal functions.
449 ;; Find an article number in the current group given the Message-ID.
450 (defun nnml-find-group-number (id)
452 (set-buffer (get-buffer-create " *nnml id*"))
453 (buffer-disable-undo (current-buffer))
454 (let ((alist nnml-group-alist)
456 ;; We want to look through all .overview files, but we want to
457 ;; start with the one in the current directory. It seems most
458 ;; likely that the article we are looking for is in that group.
459 (if (setq number (nnml-find-id nnml-current-group id))
460 (cons nnml-current-group id)
461 ;; It wasn't there, so we look through the other groups as well.
462 (while (and (not number)
464 (or (string= (car (car alist)) nnml-current-group)
465 (setq number (nnml-find-id (car (car alist)) id)))
467 (setq alist (cdr alist))))
469 (cons (car (car alist)) number))))))
471 (defun nnml-find-id (group id)
473 (insert-file-contents (nnmail-article-pathname group nnml-directory))
475 (while (and (not found)
476 (search-forward id nil t)) ; We find the ID.
477 ;; And the id is in the fourth field.
479 "\t" (save-excursion (beginning-of-line) (point)) t 4)
483 ;; We return the article number.
486 (read (current-buffer))
491 (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
492 (if (or gnus-nov-is-evil nnml-nov-is-evil)
494 (let ((first (car articles))
495 (last (progn (while (cdr articles) (setq articles (cdr articles)))
497 (nov (concat nnml-current-directory nnml-nov-file-name)))
498 (if (file-exists-p nov)
500 (set-buffer nntp-server-buffer)
502 (insert-file-contents nov)
504 (not (numberp fetch-old)))
505 t ; Don't remove anything.
507 (setq first (max 1 (- first fetch-old))))
508 (goto-char (point-min))
509 (while (and (not (eobp)) (< first (read (current-buffer))))
512 (if (not (eobp)) (delete-region 1 (point)))
513 (while (and (not (eobp)) (>= last (read (current-buffer))))
516 (if (not (eobp)) (delete-region (point) (point-max)))
519 (defun nnml-possibly-change-directory (newsgroup &optional force)
521 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
522 (and (or force (file-directory-p pathname))
523 (setq nnml-current-directory pathname
524 nnml-current-group newsgroup)))
527 (defun nnml-possibly-create-directory (group)
529 (setq dir (nnmail-article-pathname group nnml-directory))
530 (while (not (file-directory-p dir))
531 (setq dirs (cons dir dirs))
532 (setq dir (file-name-directory (directory-file-name dir))))
534 (make-directory (directory-file-name (car dirs)))
535 (and gnus-verbose-backends
536 (message "Creating mail directory %s" (car dirs)))
537 (setq dirs (cdr dirs)))))
539 (defun nnml-save-mail ()
540 "Called narrowed to an article."
541 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
543 (setq chars (nnmail-insert-lines))
544 (nnmail-insert-xref group-art)
545 (run-hooks 'nnml-prepare-save-mail-hook)
546 (goto-char (point-min))
547 (while (looking-at "From ")
548 (replace-match "X-From-Line: ")
550 ;; We save the article in all the newsgroups it belongs in.
554 (nnml-possibly-create-directory (car (car ga)))
555 (let ((file (concat (nnmail-article-pathname
556 (car (car ga)) nnml-directory)
557 (int-to-string (cdr (car ga))))))
559 ;; It was already saved, so we just make a hard link.
560 (add-name-to-file first file t)
562 (write-region (point-min) (point-max) file nil
563 (if gnus-verbose-backends nil 'nomesg))
566 ;; Generate a nov line for this article. We generate the nov
567 ;; line after saving, because nov generation destroys the
569 (setq nov-line (nnml-make-nov-line chars))
570 ;; Output the nov line to all nov databases that should have it.
571 (let ((ga group-art))
573 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
577 (defun nnml-active-number (group)
578 "Compute the next article number in GROUP."
579 (let ((active (car (cdr (assoc group nnml-group-alist)))))
580 ;; The group wasn't known to nnml, so we just create an active
584 (setq active (cons 1 0))
585 (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
586 (setcdr active (1+ (cdr active)))
587 (while (file-exists-p
588 (concat (nnmail-article-pathname group nnml-directory)
589 (int-to-string (cdr active))))
590 (setcdr active (1+ (cdr active))))
593 (defun nnml-add-nov (group article line)
594 "Add a nov line for the GROUP base."
596 (set-buffer (nnml-open-nov group))
597 (goto-char (point-max))
598 (insert (int-to-string article) line)))
600 (defsubst nnml-header-value ()
601 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
603 (defun nnml-make-nov-line (chars)
604 "Create a nov from the current headers."
605 (let ((case-fold-search t)
606 subject from date id references lines xref in-reply-to char)
609 (goto-char (point-min))
612 (1- (or (search-forward "\n\n" nil t) (point-max))))
613 ;; Fold continuation lines.
614 (goto-char (point-min))
615 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
616 (replace-match " " t t))
617 (subst-char-in-region (point-min) (point-max) ?\t ? )
618 ;; [number subject from date id references chars lines xref]
620 (goto-char (point-min))
621 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
624 (setq char (downcase (following-char)))
627 (setq subject (nnml-header-value)))
629 (setq from (nnml-header-value)))
631 (setq xref (nnml-header-value)))
633 (setq lines (nnml-header-value)))
635 (setq date (nnml-header-value)))
637 (setq id (setq id (nnml-header-value))))
639 (setq references (nnml-header-value)))
641 (setq in-reply-to (nnml-header-value))))
644 (and (not references)
646 (string-match "<[^>]+>" in-reply-to)
648 (substring in-reply-to (match-beginning 0)
650 ;; [number subject from date id references chars lines xref]
651 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
652 (or subject "(none)")
653 (or from "(nobody)") (or date "")
654 (or id (concat "nnml-dummy-id-"
656 (lambda (time) (int-to-string time))
657 (current-time) "-")))
659 (or chars 0) (or lines "0") (or xref ""))))))
661 (defun nnml-open-nov (group)
662 (or (cdr (assoc group nnml-nov-buffer-alist))
663 (let ((buffer (find-file-noselect
664 (concat (nnmail-article-pathname
665 group nnml-directory) nnml-nov-file-name))))
668 (buffer-disable-undo (current-buffer)))
669 (setq nnml-nov-buffer-alist
670 (cons (cons group buffer) nnml-nov-buffer-alist))
673 (defun nnml-save-nov ()
675 (while nnml-nov-buffer-alist
676 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
678 (set-buffer (cdr (car nnml-nov-buffer-alist)))
679 (and (buffer-modified-p)
681 1 (point-max) (buffer-file-name) nil 'nomesg))
682 (set-buffer-modified-p nil)
683 (kill-buffer (current-buffer))))
684 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
687 (defun nnml-generate-nov-databases (dir)
688 "Generate nov databases in all nnml mail newsgroups."
691 (setq nnml-group-alist nil)
692 (list nnml-directory)))
693 (nnml-open-server (or nnml-current-server ""))
694 (let ((dirs (directory-files dir t nil t)))
696 (if (and (not (string-match "/\\.\\.$" (car dirs)))
697 (not (string-match "/\\.$" (car dirs)))
698 (file-directory-p (car dirs)))
699 (nnml-generate-nov-databases (car dirs)))
700 (setq dirs (cdr dirs))))
705 (string-to-int name)))
706 (directory-files dir nil "^[0-9]+$" t))
708 (nov (concat dir "/" nnml-nov-file-name))
709 (nov-buffer (get-buffer-create "*nov*"))
712 (setq nnml-group-alist
713 (cons (list (nnmail-replace-chars-in-string
714 (substring (expand-file-name dir)
715 (length (expand-file-name
720 (while (cdr f) (setq f (cdr f)))
725 (set-buffer nntp-server-buffer)
726 (if (file-exists-p nov)
727 (funcall nnmail-delete-file-function nov))
729 (set-buffer nov-buffer)
730 (buffer-disable-undo (current-buffer))
734 (insert-file-contents (concat dir "/" (int-to-string (car files))))
735 (goto-char (point-min))
736 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
737 (setq chars (- (point-max)
740 (if (not (= 0 chars)) ; none of them empty files...
742 (setq nov-line (nnml-make-nov-line chars))
744 (set-buffer nov-buffer)
745 (goto-char (point-max))
746 (insert (int-to-string (car files)) nov-line))))
748 (setq files (cdr files)))
750 (set-buffer nov-buffer)
751 (write-region 1 (point-max) (expand-file-name nov) nil
753 (kill-buffer (current-buffer)))))
754 (nnmail-save-active nnml-group-alist nnml-active-file)))
756 (defun nnml-nov-delete-article (group article)
758 (set-buffer (nnml-open-nov group))
759 (goto-char (point-min))
760 (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
761 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
766 ;;; nnml.el ends here