1 ;;; nnml.el --- mail spool access for Gnus
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
7 ;; Keywords: news, mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
29 ;; For an overview of what the interface functions do, please see the
37 (defvar nnml-directory
"~/Mail/"
38 "Mail spool directory.")
40 (defvar nnml-active-file
(concat nnml-directory
"active")
43 (defvar nnml-newsgroups-file
(concat nnml-directory
"newsgroups")
44 "Mail newsgroups description file.")
46 (defvar nnml-get-new-mail t
47 "If non-nil, nnml will check the incoming mail file and split the mail.")
49 (defvar nnml-nov-is-evil nil
50 "If non-nil, Gnus will never generate and use nov databases for mail groups.
51 Using nov databases will speed up header fetching considerably.
52 This variable shouldn't be flipped much. If you have, for some reason,
53 set this to t, and want to set it to nil again, you should always run
54 the `nnml-generate-nov-databases' command. The function will go
55 through all nnml directories and generate nov databases for them
56 all. This may very well take some time.")
58 (defvar nnml-prepare-save-mail-hook nil
59 "Hook run narrowed to an article before saving.")
63 (defconst nnml-version
"nnml 1.0"
66 (defvar nnml-nov-file-name
".overview")
68 (defvar nnml-current-directory nil
)
69 (defvar nnml-status-string
"")
70 (defvar nnml-nov-buffer-alist nil
)
71 (defvar nnml-group-alist nil
)
72 (defvar nnml-active-timestamp nil
)
78 (defvar nnml-current-server nil
)
79 (defvar nnml-server-alist nil
)
80 (defvar nnml-server-variables
82 (list 'nnml-directory nnml-directory
)
83 (list 'nnml-active-file nnml-active-file
)
84 (list 'nnml-newsgroups-file nnml-newsgroups-file
)
85 (list 'nnml-get-new-mail nnml-get-new-mail
)
86 (list 'nnml-nov-is-evil nnml-nov-is-evil
)
87 (list 'nnml-nov-file-name nnml-nov-file-name
)
88 '(nnml-current-directory 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
)
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
)
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 ((file (if (stringp id
)
180 (concat nnml-current-directory
(int-to-string id
))))
181 (nntp-server-buffer (or buffer nntp-server-buffer
)))
182 (if (and (stringp file
)
184 (not (file-directory-p file
)))
186 (nnmail-find-file file
)))))
188 (defun nnml-request-group (group &optional server dont-check
)
189 (if (not (nnml-possibly-change-directory group
))
191 (setq nnml-status-string
"Invalid group (no such directory)")
195 (nnml-get-new-mail group
)
196 (nnmail-activate 'nnml
)
197 (let ((active (nth 1 (assoc group nnml-group-alist
))))
199 (set-buffer nntp-server-buffer
)
203 (insert (format "211 %d %d %d %s\n"
204 (max (1+ (- (cdr active
) (car active
))) 0)
205 (car active
) (cdr active
) group
))
208 (defun nnml-close-group (group &optional server
)
211 (defun nnml-request-close ()
212 (setq nnml-current-server nil
)
213 (setq nnml-server-alist nil
)
216 (defun nnml-request-create-group (group &optional server
)
217 (nnmail-activate 'nnml
)
218 (or (assoc group nnml-group-alist
)
220 (setq nnml-group-alist
(cons (list group
(setq active
(cons 1 0)))
222 (nnml-possibly-create-directory group
)
223 (nnml-possibly-change-directory group
)
224 (let ((articles (mapcar
226 (string-to-int file
))
228 nnml-current-directory nil
"^[0-9]+$"))))
231 (setcar active
(apply 'min articles
))
232 (setcdr active
(apply 'max articles
)))))
233 (nnmail-save-active nnml-group-alist nnml-active-file
)))
236 (defun nnml-request-list (&optional server
)
237 (if server
(nnml-get-new-mail))
239 (nnmail-find-file nnml-active-file
)
240 (setq nnml-group-alist
(nnmail-get-active))))
242 (defun nnml-request-newgroups (date &optional server
)
243 (nnml-request-list server
))
245 (defun nnml-request-list-newsgroups (&optional server
)
247 (nnmail-find-file nnml-newsgroups-file
)))
249 (defun nnml-request-post (&optional server
)
250 (mail-send-and-exit nil
))
252 (defalias 'nnml-request-post-buffer
'nnmail-request-post-buffer
)
254 (defun nnml-request-expire-articles (articles newsgroup
&optional server force
)
255 (nnml-possibly-change-directory newsgroup
)
256 (let* ((days (or (and nnmail-expiry-wait-function
257 (funcall nnmail-expiry-wait-function newsgroup
))
263 (string-to-int name
)))
264 (directory-files nnml-current-directory nil
"^[0-9]+$" t
)))
265 (max-article (and active-articles
(apply 'max active-articles
)))
267 article rest mod-time
)
268 (nnmail-activate 'nnml
)
270 (while (and articles is-old
)
271 (setq article
(concat nnml-current-directory
272 (int-to-string (car articles
))))
273 (if (setq mod-time
(nth 5 (file-attributes article
)))
274 (if (and (or (not nnmail-keep-last-article
)
276 (not (= (car articles
) max-article
)))
278 (and (not (equal mod-time
'(0 0)))
280 (> (nnmail-days-between
281 (current-time-string)
282 (current-time-string mod-time
))
285 (and gnus-verbose-backends
286 (message "Deleting article %s..." article
))
288 (delete-file article
)
290 (setq rest
(cons (car articles
) rest
))))
291 (setq active-articles
(delq (car articles
) active-articles
))
292 (nnml-nov-delete-article newsgroup
(car articles
)))
293 (setq rest
(cons (car articles
) rest
))))
294 (setq articles
(cdr articles
)))
295 (let ((active (nth 1 (assoc newsgroup nnml-group-alist
))))
297 (setcar active
(or (and active-articles
298 (apply 'min active-articles
))
300 (nnmail-save-active nnml-group-alist nnml-active-file
))
303 (nconc rest articles
)))
305 (defun nnml-request-move-article
306 (article group server accept-form
&optional last
)
307 (let ((buf (get-buffer-create " *nnml move*"))
310 (nnml-request-article article group server
)
313 (insert-buffer-substring nntp-server-buffer
)
314 (setq result
(eval accept-form
))
315 (kill-buffer (current-buffer))
319 (delete-file (concat nnml-current-directory
320 (int-to-string article
)))
322 (nnml-nov-delete-article group article
)
323 (and last
(nnml-save-nov))))
326 (defun nnml-request-accept-article (group &optional last
)
330 (nnmail-activate 'nnml
)
331 ;; We trick the choosing function into believing that only one
332 ;; group is available.
333 (let ((nnmail-split-methods (list (list group
""))))
334 (setq result
(car (nnml-save-mail))))
336 (nnmail-save-active nnml-group-alist nnml-active-file
)
337 (and last
(nnml-save-nov))))
339 (nnmail-activate 'nnml
)
340 (setq result
(car (nnml-save-mail)))
342 (nnmail-save-active nnml-group-alist nnml-active-file
)
343 (and last
(nnml-save-nov)))))
346 (defun nnml-request-replace-article (article group buffer
)
347 (nnml-possibly-change-directory group
)
350 (nnml-possibly-create-directory group
)
351 (if (not (condition-case ()
353 (write-region (point-min) (point-max)
354 (concat nnml-current-directory
355 (int-to-string article
))
356 nil
(if gnus-verbose-backends nil
'nomesg
))
360 (let ((chars (nnmail-insert-lines))
361 (art (concat (int-to-string article
) "\t"))
363 (setq nov-line
(nnml-make-nov-line chars
))
364 ;; Replace the NOV line in the NOV file.
366 (set-buffer (nnml-open-nov group
))
367 (goto-char (point-min))
368 (if (or (looking-at art
)
369 (search-forward (concat "\n" art
) nil t
))
370 ;; Delete the old NOV line.
371 (delete-region (progn (beginning-of-line) (point))
372 (progn (forward-line 1) (point)))
373 ;; The line isn't here, so we have to find out where
374 ;; we should insert it. (This situation should never
375 ;; occur, but one likes to make sure...)
376 (while (and (looking-at "[0-9]+\t")
379 (match-beginning 0) (match-end 0)))
381 (zerop (forward-line 1)))))
383 (insert (int-to-string article
) nov-line
)
389 ;;; Internal functions
391 (defun nnml-retrieve-headers-with-nov (articles)
392 (if (or gnus-nov-is-evil nnml-nov-is-evil
)
394 (let ((first (car articles
))
395 (last (progn (while (cdr articles
) (setq articles
(cdr articles
)))
397 (nov (concat nnml-current-directory nnml-nov-file-name
)))
398 (if (file-exists-p nov
)
400 (set-buffer nntp-server-buffer
)
402 (insert-file-contents nov
)
403 (goto-char (point-min))
404 (while (and (not (eobp)) (< first
(read (current-buffer))))
407 (if (not (eobp)) (delete-region 1 (point)))
408 (while (and (not (eobp)) (>= last
(read (current-buffer))))
411 (if (not (eobp)) (delete-region (point) (point-max)))
414 (defun nnml-possibly-change-directory (newsgroup &optional force
)
416 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory
)))
417 (and (or force
(file-directory-p pathname
))
418 (setq nnml-current-directory pathname
)))
421 (defun nnml-possibly-create-directory (group)
423 (setq dir
(nnmail-article-pathname group nnml-directory
))
424 (while (not (file-directory-p dir
))
425 (setq dirs
(cons dir dirs
))
426 (setq dir
(file-name-directory (directory-file-name dir
))))
428 (make-directory (directory-file-name (car dirs
)))
429 (and gnus-verbose-backends
430 (message "Creating mail directory %s" (car dirs
)))
431 (setq dirs
(cdr dirs
)))))
433 (defun nnml-save-mail ()
434 "Called narrowed to an article."
435 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number
)))
437 (setq chars
(nnmail-insert-lines))
438 (nnmail-insert-xref group-art
)
439 (run-hooks 'nnml-prepare-save-mail-hook
)
440 (goto-char (point-min))
441 (while (looking-at "From ")
442 (replace-match "X-From-Line: ")
444 ;; We save the article in all the newsgroups it belongs in.
448 (nnml-possibly-create-directory (car (car ga
)))
449 (let ((file (concat (nnmail-article-pathname
450 (car (car ga
)) nnml-directory
)
451 (int-to-string (cdr (car ga
))))))
453 ;; It was already saved, so we just make a hard link.
454 (add-name-to-file first file t
)
456 (write-region (point-min) (point-max) file nil
457 (if gnus-verbose-backends nil
'nomesg
))
460 ;; Generate a nov line for this article. We generate the nov
461 ;; line after saving, because nov generation destroys the
463 (setq nov-line
(nnml-make-nov-line chars
))
464 ;; Output the nov line to all nov databases that should have it.
465 (let ((ga group-art
))
467 (nnml-add-nov (car (car ga
)) (cdr (car ga
)) nov-line
)
471 (defun nnml-active-number (group)
472 "Compute the next article number in GROUP."
473 (let ((active (car (cdr (assoc group nnml-group-alist
)))))
474 ;; The group wasn't known to nnml, so we just create an active
478 (setq active
(cons 1 0))
479 (setq nnml-group-alist
(cons (list group active
) nnml-group-alist
))))
480 (setcdr active
(1+ (cdr active
)))
481 (while (file-exists-p
482 (concat (nnmail-article-pathname group nnml-directory
)
483 (int-to-string (cdr active
))))
484 (setcdr active
(1+ (cdr active
))))
487 (defun nnml-get-new-mail (&optional group
)
488 "Read new incoming mail."
489 (let* ((spools (nnmail-get-spool-files group
))
492 (if (or (not nnml-get-new-mail
) (not nnmail-spool-file
))
494 ;; We first activate all the groups.
495 (nnmail-activate 'nnml
)
496 ;; The we go through all the existing spool files and split the
500 (file-exists-p (car spools
))
501 (> (nth 7 (file-attributes (car spools
))) 0)
503 (and gnus-verbose-backends
504 (message "nnml: Reading incoming mail..."))
505 (if (not (setq incoming
507 (car spools
) (concat nnml-directory
"Incoming"))))
509 (setq group
(nnmail-get-split-group (car spools
) group-in
))
510 (nnmail-split-incoming incoming
'nnml-save-mail nil group
)
511 (setq incomings
(cons incoming incomings
)))))
512 (setq spools
(cdr spools
)))
513 ;; If we did indeed read any incoming spools, we save all info.
516 (nnmail-save-active nnml-group-alist nnml-active-file
)
518 (run-hooks 'nnmail-read-incoming-hook
)
519 (and gnus-verbose-backends
520 (message "nnml: Reading incoming mail...done"))))
522 (setq incoming
(car incomings
))
523 (and nnmail-delete-incoming
524 (file-exists-p incoming
)
525 (file-writable-p incoming
)
526 (delete-file incoming
))
527 (setq incomings
(cdr incomings
))))))
530 (defun nnml-add-nov (group article line
)
531 "Add a nov line for the GROUP base."
533 (set-buffer (nnml-open-nov group
))
534 (goto-char (point-max))
535 (insert (int-to-string article
) line
)))
537 (defsubst nnml-header-value
()
538 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
540 (defun nnml-make-nov-line (chars)
541 "Create a nov from the current headers."
542 (let ((case-fold-search t
)
543 subject from date id references lines xref in-reply-to char
)
546 (goto-char (point-min))
549 (1- (or (search-forward "\n\n" nil t
) (point-max))))
550 ;; Fold continuation lines.
551 (goto-char (point-min))
552 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t
)
553 (replace-match " " t t
))
554 (subst-char-in-region (point-min) (point-max) ?
\t ?
)
555 ;; [number subject from date id references chars lines xref]
557 (goto-char (point-min))
558 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
561 (setq char
(downcase (following-char)))
564 (setq subject
(nnml-header-value)))
566 (setq from
(nnml-header-value)))
568 (setq xref
(nnml-header-value)))
570 (setq lines
(nnml-header-value)))
572 (setq date
(nnml-header-value)))
574 (setq id
(setq id
(nnml-header-value))))
576 (setq references
(nnml-header-value)))
578 (setq in-reply-to
(nnml-header-value))))
581 (and (not references
)
583 (string-match "<[^>]+>" in-reply-to
)
585 (substring in-reply-to
(match-beginning 0)
587 ;; [number subject from date id references chars lines xref]
588 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
589 (or subject
"(none)")
590 (or from
"(nobody)") (or date
"")
591 (or id
(concat "nnml-dummy-id-"
593 (lambda (time) (int-to-string time
))
594 (current-time) "-")))
596 (or chars
0) (or lines
"0") (or xref
""))))))
598 (defun nnml-open-nov (group)
599 (or (cdr (assoc group nnml-nov-buffer-alist
))
600 (let ((buffer (find-file-noselect
601 (concat (nnmail-article-pathname
602 group nnml-directory
) nnml-nov-file-name
))))
605 (buffer-disable-undo (current-buffer)))
606 (setq nnml-nov-buffer-alist
607 (cons (cons group buffer
) nnml-nov-buffer-alist
))
610 (defun nnml-save-nov ()
612 (while nnml-nov-buffer-alist
613 (if (buffer-name (cdr (car nnml-nov-buffer-alist
)))
615 (set-buffer (cdr (car nnml-nov-buffer-alist
)))
616 (and (buffer-modified-p)
618 1 (point-max) (buffer-file-name) nil
'nomesg
))
619 (set-buffer-modified-p nil
)
620 (kill-buffer (current-buffer))))
621 (setq nnml-nov-buffer-alist
(cdr nnml-nov-buffer-alist
)))))
624 (defun nnml-generate-nov-databases (dir)
625 "Generate nov databases in all nnml mail newsgroups."
628 (setq nnml-group-alist nil
)
629 (list nnml-directory
)))
630 (nnml-open-server (or nnml-current-server
""))
631 (let ((dirs (directory-files dir t nil t
)))
633 (if (and (not (string-match "/\\.\\.$" (car dirs
)))
634 (not (string-match "/\\.$" (car dirs
)))
635 (file-directory-p (car dirs
)))
636 (nnml-generate-nov-databases (car dirs
)))
637 (setq dirs
(cdr dirs
))))
642 (string-to-int name
)))
643 (directory-files dir nil
"^[0-9]+$" t
))
645 (nov (concat dir
"/" nnml-nov-file-name
))
646 (nov-buffer (get-buffer-create "*nov*"))
649 (setq nnml-group-alist
650 (cons (list (nnmail-replace-chars-in-string
651 (substring (expand-file-name dir
)
652 (length (expand-file-name
657 (while (cdr f
) (setq f
(cdr f
)))
662 (set-buffer nntp-server-buffer
)
663 (if (file-exists-p nov
)
666 (set-buffer nov-buffer
)
667 (buffer-disable-undo (current-buffer))
671 (insert-file-contents (concat dir
"/" (int-to-string (car files
))))
672 (goto-char (point-min))
673 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t
)
674 (setq chars
(- (point-max)
677 (if (not (= 0 chars
)) ; none of them empty files...
679 (setq nov-line
(nnml-make-nov-line chars
))
681 (set-buffer nov-buffer
)
682 (goto-char (point-max))
683 (insert (int-to-string (car files
)) nov-line
))))
685 (setq files
(cdr files
)))
687 (set-buffer nov-buffer
)
688 (write-region 1 (point-max) (expand-file-name nov
) nil
690 (kill-buffer (current-buffer)))))
691 (nnmail-save-active nnml-group-alist nnml-active-file
)))
693 (defun nnml-nov-delete-article (group article
)
695 (set-buffer (nnml-open-nov group
))
696 (goto-char (point-min))
697 (if (re-search-forward (concat "^" (int-to-string article
) "\t") nil t
)
698 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
703 ;;; nnml.el ends here