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/"
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.")
58 (defconst nnml-version "nnml 0.2"
61 (defvar nnml-current-directory nil
62 "Current news group directory.")
64 (defvar nnml-status-string "")
66 (defvar nnml-nov-buffer-alist nil)
68 (defvar nnml-group-alist nil)
69 (defvar nnml-active-timestamp nil)
75 (defvar nnml-current-server nil)
76 (defvar nnml-server-alist nil)
77 (defvar nnml-server-variables
79 (list 'nnml-directory nnml-directory)
80 (list 'nnml-active-file nnml-active-file)
81 (list 'nnml-newsgroups-file nnml-newsgroups-file)
82 (list 'nnml-get-new-mail nnml-get-new-mail)
83 (list 'nnml-nov-is-evil nnml-nov-is-evil)
84 '(nnml-current-directory nil)
85 '(nnml-status-string "")
86 '(nnml-nov-buffer-alist nil)
87 '(nnml-group-alist nil)
88 '(nnml-active-timestamp nil)))
92 ;;; Interface functions.
94 (defun nnml-retrieve-headers (sequence &optional newsgroup server)
96 (set-buffer nntp-server-buffer)
99 (number (length sequence))
102 (nnml-possibly-change-directory newsgroup)
103 (if (nnml-retrieve-headers-with-nov sequence)
106 (setq article (car sequence))
108 (concat nnml-current-directory (prin1-to-string article)))
109 (if (and (file-exists-p file)
110 (not (file-directory-p file)))
112 (insert (format "221 %d Article retrieved.\n" article))
114 (insert-file-contents file)
116 (if (search-forward "\n\n" nil t)
118 (goto-char (point-max))
121 (delete-region (point) (point-max))))
122 (setq sequence (cdr sequence))
123 (setq count (1+ count))
124 (and (numberp nnmail-large-newsgroup)
125 (> number nnmail-large-newsgroup)
127 gnus-verbose-backends
128 (message "nnml: Receiving headers... %d%%"
129 (/ (* count 100) number))))
131 (and (numberp nnmail-large-newsgroup)
132 (> number nnmail-large-newsgroup)
133 gnus-verbose-backends
134 (message "nnml: Receiving headers... done"))
136 ;; Fold continuation lines.
138 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
139 (replace-match " " t t))
142 (defun nnml-open-server (server &optional defs)
143 (nnheader-init-server-buffer)
144 (if (equal server nnml-current-server)
146 (if nnml-current-server
147 (setq nnml-server-alist
148 (cons (list nnml-current-server
149 (nnheader-save-variables nnml-server-variables))
151 (let ((state (assoc server nnml-server-alist)))
154 (nnheader-restore-variables (nth 1 state))
155 (setq nnml-server-alist (delq state nnml-server-alist)))
156 (nnheader-set-init-variables nnml-server-variables defs)))
157 (setq nnml-current-server server)))
159 (defun nnml-close-server (&optional server)
162 (defun nnml-server-opened (&optional server)
163 (equal server nnml-current-server))
165 (defun nnml-status-message (&optional server)
168 (defun nnml-request-article (id &optional newsgroup server buffer)
169 (nnml-possibly-change-directory newsgroup)
170 (let ((file (if (stringp id)
172 (concat nnml-current-directory (prin1-to-string id))))
173 (nntp-server-buffer (or buffer nntp-server-buffer)))
174 (if (and (stringp file)
176 (not (file-directory-p file)))
178 (nnmail-find-file file)))))
180 (defun nnml-request-group (group &optional server dont-check)
181 (if (not (nnml-possibly-change-directory group))
183 (setq nnml-status-string "Invalid group (no such directory)")
188 (let ((timestamp (nth 5 (file-attributes nnml-active-file))))
189 (if (or (not nnml-active-timestamp)
190 (> (nth 0 timestamp) (nth 0 nnml-active-timestamp))
191 (> (nth 1 timestamp) (nth 1 nnml-active-timestamp)))
193 (setq nnml-active-timestamp timestamp)
195 (setq nnml-group-alist (nnmail-get-active))))
196 (let ((active (nth 1 (assoc group nnml-group-alist))))
198 (set-buffer nntp-server-buffer)
202 (insert (format "211 %d %d %d %s\n"
203 (max (1+ (- (cdr active) (car active))) 0)
204 (car active) (cdr active) group))
207 (defun nnml-close-group (group &optional server)
210 (defun nnml-request-create-group (group &optional server)
212 (setq nnml-group-alist (nnmail-get-active))
213 (or (assoc group nnml-group-alist)
215 (setq nnml-group-alist (cons (list group (setq active (cons 0 0)))
217 (nnml-possibly-create-directory group)
218 (nnml-possibly-change-directory group)
219 (let ((articles (mapcar
221 (int-to-string file))
223 nnml-current-directory nil "^[0-9]+$"))))
226 (setcar active (apply 'min articles))
227 (setcdr active (apply 'max articles)))))
228 (nnmail-save-active nnml-group-alist nnml-active-file)))
231 (defun nnml-request-list (&optional server)
232 (if server (nnml-get-new-mail))
234 (nnmail-find-file nnml-active-file)))
236 (defun nnml-request-newgroups (date &optional server)
237 (nnml-request-list server))
239 (defun nnml-request-list-newsgroups (&optional server)
241 (nnmail-find-file nnml-newsgroups-file)))
243 (defun nnml-request-post (&optional server)
244 (mail-send-and-exit nil))
246 (fset 'nnml-request-post-buffer 'nnmail-request-post-buffer)
248 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
249 (nnml-possibly-change-directory newsgroup)
250 (let* ((days (or (and nnmail-expiry-wait-function
251 (funcall nnmail-expiry-wait-function newsgroup))
257 (string-to-int name)))
258 (directory-files nnml-current-directory nil "^[0-9]+$" t)))
259 (max-article (and active-articles (apply 'max active-articles)))
260 article rest mod-time)
262 (setq article (concat nnml-current-directory (int-to-string
264 (if (setq mod-time (nth 5 (file-attributes article)))
265 (if (and (or (not nnmail-keep-last-article)
267 (not (= (car articles) max-article)))
269 (> (nnmail-days-between
270 (current-time-string)
271 (current-time-string mod-time))
274 (and gnus-verbose-backends (message "Deleting %s..." article))
276 (delete-file article)
278 (setq active-articles (delq (car articles) active-articles))
279 (nnml-nov-delete-article newsgroup (car articles)))
280 (setq rest (cons (car articles) rest))))
281 (setq articles (cdr articles)))
282 (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
283 (setcar active (or (and active-articles (apply 'min active-articles))
285 (nnmail-save-active nnml-group-alist nnml-active-file))
289 (defun nnml-request-move-article
290 (article group server accept-form &optional last)
291 (let ((buf (get-buffer-create " *nnml move*"))
294 (nnml-request-article article group server)
297 (insert-buffer-substring nntp-server-buffer)
298 (setq result (eval accept-form))
299 (kill-buffer (current-buffer))
303 (delete-file (concat nnml-current-directory
304 (int-to-string article)))
306 (nnml-nov-delete-article group article)
307 (and last (nnml-save-nov))))
310 (defun nnml-request-accept-article (group &optional last)
315 (setq nnml-group-alist (nnmail-get-active))
316 ;; We trick the choosing function into believing that only one
317 ;; group is availiable.
318 (let ((nnmail-split-methods (list (list group ""))))
319 (setq result (car (nnml-save-mail))))
321 (nnmail-save-active nnml-group-alist nnml-active-file)
322 (and last (nnml-save-nov))))
325 (setq nnml-group-alist (nnmail-get-active))
326 (setq result (car (nnml-save-mail)))
328 (nnmail-save-active nnml-group-alist nnml-active-file)
329 (and last (nnml-save-nov)))))
332 (defun nnml-request-replace-article (article group buffer)
333 (nnml-possibly-change-directory group)
336 (nnml-possibly-create-directory group)
337 (if (not (condition-case ()
339 (write-region (point-min) (point-max)
340 (concat nnml-current-directory
341 (int-to-string article))
342 nil (if gnus-verbose-backends nil 'nomesg))
346 (let ((chars (nnmail-insert-lines))
347 (art (concat (int-to-string article) "\t"))
349 (setq nov-line (nnml-make-nov-line chars))
351 (set-buffer (nnml-open-nov group))
352 (goto-char (point-min))
353 (if (or (looking-at art)
354 (search-forward (concat "\n" art)))
356 (delete-region (progn (beginning-of-line) (point))
357 (progn (forward-line 1) (point)))
358 (insert (int-to-string article) nov-line)
360 (kill-buffer (current-buffer)))
365 ;;; Internal functions
367 (defun nnml-retrieve-headers-with-nov (articles)
368 (if (or gnus-nov-is-evil nnml-nov-is-evil)
370 (let ((first (car articles))
371 (last (progn (while (cdr articles) (setq articles (cdr articles)))
373 (nov (concat nnml-current-directory ".nov")))
374 (if (file-exists-p nov)
376 (set-buffer nntp-server-buffer)
378 (insert-file-contents nov)
380 (while (and (not (eobp)) (< first (read (current-buffer))))
383 (if (not (eobp)) (delete-region 1 (point)))
384 (while (and (not (eobp)) (>= last (read (current-buffer))))
387 (if (not (eobp)) (delete-region (point) (point-max)))
390 (defun nnml-possibly-change-directory (newsgroup &optional force)
392 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
393 (and (or force (file-directory-p pathname))
394 (setq nnml-current-directory pathname)))
397 (defun nnml-possibly-create-directory (group)
399 (setq dir (nnmail-article-pathname group nnml-directory))
400 (while (not (file-directory-p dir))
401 (setq dirs (cons dir dirs))
402 (setq dir (file-name-directory (directory-file-name dir))))
404 (if (make-directory (directory-file-name (car dirs)))
405 (error "Could not create directory %s" (car dirs)))
406 (and gnus-verbose-backends
407 (message "Creating mail directory %s" (car dirs)))
408 (setq dirs (cdr dirs)))))
410 (defun nnml-save-mail ()
411 "Called narrowed to an article."
412 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
414 (setq chars (nnmail-insert-lines))
415 (nnmail-insert-xref group-art)
416 (goto-char (point-min))
417 (while (looking-at "From ")
418 (replace-match "X-From-Line: ")
420 ;; We save the article in all the newsgroups it belongs in.
424 (nnml-possibly-create-directory (car (car ga)))
425 (let ((file (concat (nnmail-article-pathname
426 (car (car ga)) nnml-directory)
427 (int-to-string (cdr (car ga))))))
429 ;; It was already saved, so we just make a hard link.
430 (add-name-to-file first file t)
432 (write-region (point-min) (point-max) file nil
433 (if gnus-verbose-backends nil 'nomesg))
436 ;; Generate a nov line for this article. We generate the nov
437 ;; line after saving, because nov generation destroys the
439 (setq nov-line (nnml-make-nov-line chars))
440 ;; Output the nov line to all nov databases that should have it.
441 (let ((ga group-art))
443 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
447 (defun nnml-active-number (group)
448 "Compute the next article number in GROUP."
449 (let ((active (car (cdr (assoc group nnml-group-alist)))))
452 (setq active (cons 1 0))
453 (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
454 (setcdr active (1+ (cdr active)))
456 (while (file-exists-p
457 (setq file (concat (nnmail-article-pathname
458 group nnml-directory)
459 (int-to-string (cdr active)))))
460 (setcdr active (1+ (cdr active)))))
463 (defun nnml-get-new-mail ()
464 "Read new incoming mail."
466 (if (and nnml-get-new-mail nnmail-spool-file
467 (file-exists-p nnmail-spool-file)
468 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
470 (and gnus-verbose-backends
471 (message "nnml: Reading incoming mail..."))
473 (nnmail-move-inbox nnmail-spool-file
474 (concat nnml-directory "Incoming")))
476 (setq nnml-group-alist (nnmail-get-active))
477 (nnmail-split-incoming incoming 'nnml-save-mail)
478 (nnmail-save-active nnml-group-alist nnml-active-file)
480 (run-hooks 'nnmail-read-incoming-hook)
481 ;; The following has been commented away, just to make sure
482 ;; that nobody ever loses any mail. If you feel safe that
483 ;; nnml will never do anything strange, just remove those
484 ;; two semicolons, and avoid having lots of "Incoming*"
486 ;; (delete-file incoming)
487 (and gnus-verbose-backends
488 (message "nnml: Reading incoming mail...done"))))))
491 (defun nnml-add-nov (group article line)
492 "Add a nov line for the GROUP base."
494 (set-buffer (nnml-open-nov group))
495 (goto-char (point-max))
496 (insert (int-to-string article) line)))
498 (defsubst nnml-header-value ()
499 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
501 (defun nnml-make-nov-line (chars)
502 "Create a nov from the current headers."
503 (let ((case-fold-search t)
504 subject from date id references lines xref in-reply-to char)
507 (goto-char (point-min))
510 (1- (or (search-forward "\n\n" nil t) (point-max))))
511 ;; Fold continuation lines.
512 (goto-char (point-min))
513 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
514 (replace-match " " t t))
515 (subst-char-in-region (point-min) (point-max) ?\t ? )
516 ;; [number subject from date id references chars lines xref]
518 (goto-char (point-min))
519 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
522 (setq char (downcase (following-char)))
525 (setq subject (nnml-header-value)))
527 (setq from (nnml-header-value)))
529 (setq xref (nnml-header-value)))
531 (setq lines (nnml-header-value)))
533 (setq date (nnml-header-value)))
535 (setq id (setq id (nnml-header-value))))
537 (setq references (nnml-header-value)))
539 (setq in-reply-to (nnml-header-value))))
542 (and (not references)
544 (string-match "<[^>]+>" in-reply-to)
546 (substring in-reply-to (match-beginning 0)
548 ;; [number subject from date id references chars lines xref]
549 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
550 (or subject "(none)")
551 (or from "(nobody)") (or date "")
552 (or id (concat "nnml-dummy-id-"
554 (lambda (time) (int-to-string time))
555 (current-time) "-")))
557 (or chars 0) (or lines "0") (or xref ""))))))
559 (defun nnml-open-nov (group)
560 (or (cdr (assoc group nnml-nov-buffer-alist))
561 (let ((buffer (find-file-noselect
562 (concat (nnmail-article-pathname
563 group nnml-directory) ".nov"))))
566 (buffer-disable-undo (current-buffer)))
567 (setq nnml-nov-buffer-alist
568 (cons (cons group buffer) nnml-nov-buffer-alist))
571 (defun nnml-save-nov ()
573 (while nnml-nov-buffer-alist
574 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
576 (set-buffer (cdr (car nnml-nov-buffer-alist)))
577 (and (buffer-modified-p)
579 1 (point-max) (buffer-file-name) nil 'nomesg))
580 (set-buffer-modified-p nil)
581 (kill-buffer (current-buffer))))
582 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
585 (defun nnml-generate-nov-databases (dir)
586 "Generate nov databases in all nnml mail newsgroups."
589 (setq nnml-group-alist nil)
590 (list nnml-directory)))
591 (nnml-open-server (system-name))
592 (let ((dirs (directory-files dir t nil t)))
594 (if (and (not (string-match "/\\.\\.$" (car dirs)))
595 (not (string-match "/\\.$" (car dirs)))
596 (file-directory-p (car dirs)))
597 (nnml-generate-nov-databases (car dirs)))
598 (setq dirs (cdr dirs))))
603 (string-to-int name)))
604 (directory-files dir nil "^[0-9]+$" t))
606 (nov (concat dir "/.nov"))
607 (nov-buffer (get-buffer-create "*nov*"))
610 (setq nnml-group-alist
611 (cons (list (nnmail-replace-chars-in-string
612 (substring (expand-file-name dir)
613 (length (expand-file-name
618 (while (cdr f) (setq f (cdr f)))
623 (set-buffer nntp-server-buffer)
624 (if (file-exists-p nov)
627 (set-buffer nov-buffer)
628 (buffer-disable-undo (current-buffer))
632 (insert-file-contents (concat dir "/" (int-to-string (car files))))
634 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
635 (setq chars (- (point-max)
638 (if (not (= 0 chars)) ; none of them empty files...
640 (setq nov-line (nnml-make-nov-line chars))
642 (set-buffer nov-buffer)
643 (goto-char (point-max))
644 (insert (int-to-string (car files)) nov-line))))
646 (setq files (cdr files)))
648 (set-buffer nov-buffer)
649 (write-region 1 (point-max) (expand-file-name nov) nil
651 (kill-buffer (current-buffer)))))
652 (nnmail-save-active nnml-group-alist nnml-active-file)))
654 (defun nnml-nov-delete-article (group article)
656 (set-buffer (nnml-open-nov group))
658 (if (re-search-forward (concat "^" (int-to-string article) "\t"))
659 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
664 ;;; nnml.el ends here