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 (if (stringp (car sequence))
106 (nnml-possibly-change-directory newsgroup)
107 (if (nnml-retrieve-headers-with-nov sequence)
110 (setq article (car sequence))
112 (concat nnml-current-directory (int-to-string article)))
113 (if (and (file-exists-p file)
114 (not (file-directory-p file)))
116 (insert (format "221 %d Article retrieved.\n" article))
118 (nnheader-insert-head file)
120 (if (search-forward "\n\n" nil t)
122 (goto-char (point-max))
125 (delete-region (point) (point-max))))
126 (setq sequence (cdr sequence))
127 (setq count (1+ count))
128 (and (numberp nnmail-large-newsgroup)
129 (> number nnmail-large-newsgroup)
131 gnus-verbose-backends
132 (message "nnml: Receiving headers... %d%%"
133 (/ (* count 100) number))))
135 (and (numberp nnmail-large-newsgroup)
136 (> number nnmail-large-newsgroup)
137 gnus-verbose-backends
138 (message "nnml: Receiving headers...done"))
140 ;; Fold continuation lines.
141 (goto-char (point-min))
142 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
143 (replace-match " " t t))
146 (defun nnml-open-server (server &optional defs)
147 (nnheader-init-server-buffer)
148 (if (equal server nnml-current-server)
150 (if nnml-current-server
151 (setq nnml-server-alist
152 (cons (list nnml-current-server
153 (nnheader-save-variables nnml-server-variables))
155 (let ((state (assoc server nnml-server-alist)))
158 (nnheader-restore-variables (nth 1 state))
159 (setq nnml-server-alist (delq state nnml-server-alist)))
160 (nnheader-set-init-variables nnml-server-variables defs)))
161 (setq nnml-current-server server)))
163 (defun nnml-close-server (&optional server)
166 (defun nnml-server-opened (&optional server)
167 (and (equal server nnml-current-server)
169 (buffer-name nntp-server-buffer)))
171 (defun nnml-status-message (&optional server)
174 (defun nnml-request-article (id &optional newsgroup server buffer)
175 (nnml-possibly-change-directory newsgroup)
176 (let ((file (if (stringp id)
178 (concat nnml-current-directory (int-to-string id))))
179 (nntp-server-buffer (or buffer nntp-server-buffer)))
180 (if (and (stringp file)
182 (not (file-directory-p file)))
184 (nnmail-find-file file)))))
186 (defun nnml-request-group (group &optional server dont-check)
187 (if (not (nnml-possibly-change-directory group))
189 (setq nnml-status-string "Invalid group (no such directory)")
193 (nnml-get-new-mail group)
194 (let ((timestamp (nth 5 (file-attributes nnml-active-file))))
195 (if (or (not nnml-active-timestamp)
196 (> (nth 0 timestamp) (nth 0 nnml-active-timestamp))
197 (> (nth 1 timestamp) (nth 1 nnml-active-timestamp)))
199 (setq nnml-active-timestamp timestamp)
201 (setq nnml-group-alist (nnmail-get-active))))
202 (let ((active (nth 1 (assoc group nnml-group-alist))))
204 (set-buffer nntp-server-buffer)
208 (insert (format "211 %d %d %d %s\n"
209 (max (1+ (- (cdr active) (car active))) 0)
210 (car active) (cdr active) group))
213 (defun nnml-close-group (group &optional server)
216 (defun nnml-request-close ()
217 (setq nnml-current-server nil)
218 (setq nnml-server-alist nil)
221 (defun nnml-request-create-group (group &optional server)
223 (setq nnml-group-alist (nnmail-get-active))
224 (or (assoc group nnml-group-alist)
226 (setq nnml-group-alist (cons (list group (setq active (cons 0 0)))
228 (nnml-possibly-create-directory group)
229 (nnml-possibly-change-directory group)
230 (let ((articles (mapcar
232 (string-to-int file))
234 nnml-current-directory nil "^[0-9]+$"))))
237 (setcar active (apply 'min articles))
238 (setcdr active (apply 'max articles)))))
239 (nnmail-save-active nnml-group-alist nnml-active-file)))
242 (defun nnml-request-list (&optional server)
243 (if server (nnml-get-new-mail))
245 (nnmail-find-file nnml-active-file)
246 (setq nnml-group-alist (nnmail-get-active))))
248 (defun nnml-request-newgroups (date &optional server)
249 (nnml-request-list server))
251 (defun nnml-request-list-newsgroups (&optional server)
253 (nnmail-find-file nnml-newsgroups-file)))
255 (defun nnml-request-post (&optional server)
256 (mail-send-and-exit nil))
258 (defalias 'nnml-request-post-buffer 'nnmail-request-post-buffer)
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)))
272 article rest mod-time)
274 (setq article (concat nnml-current-directory
275 (int-to-string (car articles))))
276 (if (setq mod-time (nth 5 (file-attributes article)))
277 (if (and (or (not nnmail-keep-last-article)
279 (not (= (car articles) max-article)))
280 (not (equal mod-time '(0 0)))
282 (> (nnmail-days-between
283 (current-time-string)
284 (current-time-string mod-time))
287 (and gnus-verbose-backends (message "Deleting %s..." article))
289 (delete-file article)
291 (setq rest (cons (car articles) rest))))
292 (setq active-articles (delq (car articles) active-articles))
293 (nnml-nov-delete-article newsgroup (car articles)))
294 (setq rest (cons (car articles) rest))))
295 (setq articles (cdr articles)))
296 (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
298 (setcar active (or (and active-articles
299 (apply 'min active-articles))
301 (nnmail-save-active nnml-group-alist nnml-active-file))
306 (defun nnml-request-move-article
307 (article group server accept-form &optional last)
308 (let ((buf (get-buffer-create " *nnml move*"))
311 (nnml-request-article article group server)
314 (insert-buffer-substring nntp-server-buffer)
315 (setq result (eval accept-form))
316 (kill-buffer (current-buffer))
320 (delete-file (concat nnml-current-directory
321 (int-to-string article)))
323 (nnml-nov-delete-article group article)
324 (and last (nnml-save-nov))))
327 (defun nnml-request-accept-article (group &optional last)
332 (setq nnml-group-alist (nnmail-get-active))
333 ;; We trick the choosing function into believing that only one
334 ;; group is availiable.
335 (let ((nnmail-split-methods (list (list group ""))))
336 (setq result (car (nnml-save-mail))))
338 (nnmail-save-active nnml-group-alist nnml-active-file)
339 (and last (nnml-save-nov))))
342 (setq nnml-group-alist (nnmail-get-active))
343 (setq result (car (nnml-save-mail)))
345 (nnmail-save-active nnml-group-alist nnml-active-file)
346 (and last (nnml-save-nov)))))
349 (defun nnml-request-replace-article (article group buffer)
350 (nnml-possibly-change-directory group)
353 (nnml-possibly-create-directory group)
354 (if (not (condition-case ()
356 (write-region (point-min) (point-max)
357 (concat nnml-current-directory
358 (int-to-string article))
359 nil (if gnus-verbose-backends nil 'nomesg))
363 (let ((chars (nnmail-insert-lines))
364 (art (concat (int-to-string article) "\t"))
366 (setq nov-line (nnml-make-nov-line chars))
368 (set-buffer (nnml-open-nov group))
369 (goto-char (point-min))
370 (if (or (looking-at art)
371 (search-forward (concat "\n" art)))
373 (delete-region (progn (beginning-of-line) (point))
374 (progn (forward-line 1) (point)))
375 (insert (int-to-string article) nov-line)
377 (kill-buffer (current-buffer)))
382 ;;; Internal functions
384 (defun nnml-retrieve-headers-with-nov (articles)
385 (if (or gnus-nov-is-evil nnml-nov-is-evil)
387 (let ((first (car articles))
388 (last (progn (while (cdr articles) (setq articles (cdr articles)))
390 (nov (concat nnml-current-directory nnml-nov-file-name)))
391 (if (file-exists-p nov)
393 (set-buffer nntp-server-buffer)
395 (insert-file-contents nov)
396 (goto-char (point-min))
397 (while (and (not (eobp)) (< first (read (current-buffer))))
400 (if (not (eobp)) (delete-region 1 (point)))
401 (while (and (not (eobp)) (>= last (read (current-buffer))))
404 (if (not (eobp)) (delete-region (point) (point-max)))
407 (defun nnml-possibly-change-directory (newsgroup &optional force)
409 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
410 (and (or force (file-directory-p pathname))
411 (setq nnml-current-directory pathname)))
414 (defun nnml-possibly-create-directory (group)
416 (setq dir (nnmail-article-pathname group nnml-directory))
417 (while (not (file-directory-p dir))
418 (setq dirs (cons dir dirs))
419 (setq dir (file-name-directory (directory-file-name dir))))
421 (if (make-directory (directory-file-name (car dirs)))
422 (error "Could not create directory %s" (car dirs)))
423 (and gnus-verbose-backends
424 (message "Creating mail directory %s" (car dirs)))
425 (setq dirs (cdr dirs)))))
427 (defun nnml-save-mail ()
428 "Called narrowed to an article."
429 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
431 (setq chars (nnmail-insert-lines))
432 (nnmail-insert-xref group-art)
433 (run-hooks 'nnml-prepare-save-mail-hook)
434 (goto-char (point-min))
435 (while (looking-at "From ")
436 (replace-match "X-From-Line: ")
438 ;; We save the article in all the newsgroups it belongs in.
442 (nnml-possibly-create-directory (car (car ga)))
443 (let ((file (concat (nnmail-article-pathname
444 (car (car ga)) nnml-directory)
445 (int-to-string (cdr (car ga))))))
447 ;; It was already saved, so we just make a hard link.
448 (add-name-to-file first file t)
450 (write-region (point-min) (point-max) file nil
451 (if gnus-verbose-backends nil 'nomesg))
454 ;; Generate a nov line for this article. We generate the nov
455 ;; line after saving, because nov generation destroys the
457 (setq nov-line (nnml-make-nov-line chars))
458 ;; Output the nov line to all nov databases that should have it.
459 (let ((ga group-art))
461 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
465 (defun nnml-active-number (group)
466 "Compute the next article number in GROUP."
467 (let ((active (car (cdr (assoc group nnml-group-alist)))))
470 (setq active (cons 1 0))
471 (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
472 (setcdr active (1+ (cdr active)))
473 (while (file-exists-p
474 (concat (nnmail-article-pathname group nnml-directory)
475 (int-to-string (cdr active))))
476 (setcdr active (1+ (cdr active))))
479 (defun nnml-get-new-mail (&optional group)
480 "Read new incoming mail."
481 (let* ((spools (nnmail-get-spool-files group))
484 (if (or (not nnml-get-new-mail) (not nnmail-spool-file))
486 ;; We first activate all the groups.
487 (if (or (not group) (not nnml-group-alist))
490 (setq nnml-group-alist (nnmail-get-active))))
491 ;; The we go through all the existing spool files and split the
495 (file-exists-p (car spools))
496 (> (nth 7 (file-attributes (car spools))) 0)
498 (and gnus-verbose-backends
499 (message "nnml: Reading incoming mail..."))
502 (car spools) (concat nnml-directory "Incoming")))
503 (setq group (nnmail-get-split-group (car spools) group-in))
504 (nnmail-split-incoming incoming 'nnml-save-mail nil group)
505 (setq incomings (cons incoming incomings))))
506 (setq spools (cdr spools)))
507 ;; If we did indeed read any incoming spools, we save all info.
510 (nnmail-save-active nnml-group-alist nnml-active-file)
512 (run-hooks 'nnmail-read-incoming-hook)
513 (and gnus-verbose-backends
514 (message "nnml: Reading incoming mail...done"))))
516 (and nnmail-delete-incoming
517 (file-writable-p incoming)
518 (delete-file incoming))
519 (setq incomings (cdr incomings))))))
522 (defun nnml-add-nov (group article line)
523 "Add a nov line for the GROUP base."
525 (set-buffer (nnml-open-nov group))
526 (goto-char (point-max))
527 (insert (int-to-string article) line)))
529 (defsubst nnml-header-value ()
530 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
532 (defun nnml-make-nov-line (chars)
533 "Create a nov from the current headers."
534 (let ((case-fold-search t)
535 subject from date id references lines xref in-reply-to char)
538 (goto-char (point-min))
541 (1- (or (search-forward "\n\n" nil t) (point-max))))
542 ;; Fold continuation lines.
543 (goto-char (point-min))
544 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
545 (replace-match " " t t))
546 (subst-char-in-region (point-min) (point-max) ?\t ? )
547 ;; [number subject from date id references chars lines xref]
549 (goto-char (point-min))
550 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
553 (setq char (downcase (following-char)))
556 (setq subject (nnml-header-value)))
558 (setq from (nnml-header-value)))
560 (setq xref (nnml-header-value)))
562 (setq lines (nnml-header-value)))
564 (setq date (nnml-header-value)))
566 (setq id (setq id (nnml-header-value))))
568 (setq references (nnml-header-value)))
570 (setq in-reply-to (nnml-header-value))))
573 (and (not references)
575 (string-match "<[^>]+>" in-reply-to)
577 (substring in-reply-to (match-beginning 0)
579 ;; [number subject from date id references chars lines xref]
580 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
581 (or subject "(none)")
582 (or from "(nobody)") (or date "")
583 (or id (concat "nnml-dummy-id-"
585 (lambda (time) (int-to-string time))
586 (current-time) "-")))
588 (or chars 0) (or lines "0") (or xref ""))))))
590 (defun nnml-open-nov (group)
591 (or (cdr (assoc group nnml-nov-buffer-alist))
592 (let ((buffer (find-file-noselect
593 (concat (nnmail-article-pathname
594 group nnml-directory) nnml-nov-file-name))))
597 (buffer-disable-undo (current-buffer)))
598 (setq nnml-nov-buffer-alist
599 (cons (cons group buffer) nnml-nov-buffer-alist))
602 (defun nnml-save-nov ()
604 (while nnml-nov-buffer-alist
605 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
607 (set-buffer (cdr (car nnml-nov-buffer-alist)))
608 (and (buffer-modified-p)
610 1 (point-max) (buffer-file-name) nil 'nomesg))
611 (set-buffer-modified-p nil)
612 (kill-buffer (current-buffer))))
613 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
616 (defun nnml-generate-nov-databases (dir)
617 "Generate nov databases in all nnml mail newsgroups."
620 (setq nnml-group-alist nil)
621 (list nnml-directory)))
622 (nnml-open-server (or nnml-current-server ""))
623 (let ((dirs (directory-files dir t nil t)))
625 (if (and (not (string-match "/\\.\\.$" (car dirs)))
626 (not (string-match "/\\.$" (car dirs)))
627 (file-directory-p (car dirs)))
628 (nnml-generate-nov-databases (car dirs)))
629 (setq dirs (cdr dirs))))
634 (string-to-int name)))
635 (directory-files dir nil "^[0-9]+$" t))
637 (nov (concat dir "/" nnml-nov-file-name))
638 (nov-buffer (get-buffer-create "*nov*"))
641 (setq nnml-group-alist
642 (cons (list (nnmail-replace-chars-in-string
643 (substring (expand-file-name dir)
644 (length (expand-file-name
649 (while (cdr f) (setq f (cdr f)))
654 (set-buffer nntp-server-buffer)
655 (if (file-exists-p nov)
658 (set-buffer nov-buffer)
659 (buffer-disable-undo (current-buffer))
663 (insert-file-contents (concat dir "/" (int-to-string (car files))))
664 (goto-char (point-min))
665 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
666 (setq chars (- (point-max)
669 (if (not (= 0 chars)) ; none of them empty files...
671 (setq nov-line (nnml-make-nov-line chars))
673 (set-buffer nov-buffer)
674 (goto-char (point-max))
675 (insert (int-to-string (car files)) nov-line))))
677 (setq files (cdr files)))
679 (set-buffer nov-buffer)
680 (write-region 1 (point-max) (expand-file-name nov) nil
682 (kill-buffer (current-buffer)))))
683 (nnmail-save-active nnml-group-alist nnml-active-file)))
685 (defun nnml-nov-delete-article (group article)
687 (set-buffer (nnml-open-nov group))
688 (goto-char (point-min))
689 (if (re-search-forward (concat "^" (int-to-string article) "\t") nil t)
690 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
695 ;;; nnml.el ends here