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)
73 ;;; Interface functions.
75 (defun nnml-retrieve-headers (sequence &optional newsgroup server)
77 (set-buffer nntp-server-buffer)
80 (number (length sequence))
83 (nnml-possibly-change-directory newsgroup)
84 (if (nnml-retrieve-headers-with-nov sequence)
87 (setq article (car sequence))
89 (concat nnml-current-directory (prin1-to-string article)))
90 (if (and (file-exists-p file)
91 (not (file-directory-p file)))
93 (insert (format "221 %d Article retrieved.\n" article))
95 (insert-file-contents file)
97 (if (search-forward "\n\n" nil t)
99 (goto-char (point-max))
102 (delete-region (point) (point-max))))
103 (setq sequence (cdr sequence))
104 (setq count (1+ count))
105 (and (numberp nnmail-large-newsgroup)
106 (> number nnmail-large-newsgroup)
108 gnus-verbose-backends
109 (message "nnml: Receiving headers... %d%%"
110 (/ (* count 100) number))))
112 (and (numberp nnmail-large-newsgroup)
113 (> number nnmail-large-newsgroup)
114 gnus-verbose-backends
115 (message "nnml: Receiving headers... done"))
117 ;; Fold continuation lines.
119 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
120 (replace-match " " t t))
123 (defun nnml-open-server (host &optional service)
124 (setq nnml-status-string "")
125 (nnheader-init-server-buffer))
127 (defun nnml-close-server (&optional server)
130 (defun nnml-server-opened (&optional server)
131 (and nntp-server-buffer
132 (get-buffer nntp-server-buffer)))
134 (defun nnml-status-message (&optional server)
137 (defun nnml-request-article (id &optional newsgroup server buffer)
138 (nnml-possibly-change-directory newsgroup)
139 (let ((file (if (stringp id)
141 (concat nnml-current-directory (prin1-to-string id))))
142 (nntp-server-buffer (or buffer nntp-server-buffer)))
143 (if (and (stringp file)
145 (not (file-directory-p file)))
147 (nnmail-find-file file)))))
149 (defun nnml-request-group (group &optional server dont-check)
150 (if (not (nnml-possibly-change-directory group))
152 (setq nnml-status-string "Invalid group (no such directory)")
157 (let ((timestamp (nth 5 (file-attributes nnml-active-file))))
158 (if (or (not nnml-active-timestamp)
159 (> (nth 0 timestamp) (nth 0 nnml-active-timestamp))
160 (> (nth 1 timestamp) (nth 1 nnml-active-timestamp)))
162 (setq nnml-active-timestamp timestamp)
164 (setq nnml-group-alist (nnmail-get-active))))
165 (let ((active (nth 1 (assoc group nnml-group-alist))))
167 (set-buffer nntp-server-buffer)
171 (insert (format "211 %d %d %d %s\n"
172 (max (1+ (- (cdr active) (car active))) 0)
173 (car active) (cdr active) group))
176 (defun nnml-close-group (group &optional server)
179 (defun nnml-request-list (&optional server)
180 (if server (nnml-get-new-mail))
182 (nnmail-find-file nnml-active-file)))
184 (defun nnml-request-newgroups (date &optional server)
185 (nnml-request-list server))
187 (defun nnml-request-list-newsgroups (&optional server)
189 (nnmail-find-file nnml-newsgroups-file)))
191 (defun nnml-request-post (&optional server)
192 (mail-send-and-exit nil))
194 (fset 'nnml-request-post-buffer 'nnmail-request-post-buffer)
196 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
197 (nnml-possibly-change-directory newsgroup)
198 (let* ((days (or (and nnmail-expiry-wait-function
199 (funcall nnmail-expiry-wait-function newsgroup))
205 (string-to-int name)))
206 (directory-files nnml-current-directory nil "^[0-9]+$" t)))
207 (max-article (max active-articles))
208 article rest mod-time)
210 (setq article (concat nnml-current-directory (int-to-string
212 (if (setq mod-time (nth 5 (file-attributes article)))
213 (if (and (or (not nnmail-keep-last-article)
214 (not (= (car articles) max-article)))
216 (> (nnmail-days-between
217 (current-time-string)
218 (current-time-string mod-time))
221 (and gnus-verbose-backends (message "Deleting %s..." article))
223 (delete-file article)
225 (setq active-articles (delq (car articles) active-articles))
226 (nnml-nov-delete-article newsgroup (car articles)))
227 (setq rest (cons (car articles) rest))))
228 (setq articles (cdr articles)))
229 (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
230 (setcar active (min active-articles))
231 (nnmail-save-active nnml-group-alist nnml-active-file))
235 (defun nnml-request-move-article
236 (article group server accept-form &optional last)
237 (let ((buf (get-buffer-create " *nnml move*"))
240 (nnml-request-article article group server)
243 (insert-buffer-substring nntp-server-buffer)
244 (setq result (eval accept-form))
245 (kill-buffer (current-buffer))
249 (delete-file (concat nnml-current-directory
250 (int-to-string article)))
252 (nnml-nov-delete-article group article)
253 (and last (nnml-save-nov))))
256 (defun nnml-request-accept-article (group &optional last)
261 (setq nnml-group-alist (nnmail-get-active))
262 ;; We trick the choosing function into believing that only one
263 ;; group is availiable.
264 (let ((nnmail-split-methods (list (list group ""))))
265 (setq result (car (nnml-save-mail))))
267 (nnmail-save-active nnml-group-alist nnml-active-file)
268 (and last (nnml-save-nov))))
271 (setq nnml-group-alist (nnmail-get-active))
272 (setq result (car (nnml-save-mail)))
274 (nnmail-save-active nnml-group-alist nnml-active-file)
275 (and last (nnml-save-nov)))))
278 (defun nnml-request-replace-article (article group buffer)
279 (nnml-possibly-change-directory group)
282 (if (not (condition-case ()
284 (write-region (point-min) (point-max)
285 (concat nnml-current-directory
286 (int-to-string article))
287 nil (if gnus-verbose-backends nil 'nomesg))
291 (let ((chars (nnmail-insert-lines))
292 (art (concat (int-to-string article) "\t"))
294 (setq nov-line (nnml-make-nov-line chars))
296 (set-buffer (nnml-open-nov group))
297 (goto-char (point-min))
298 (if (or (looking-at art)
299 (search-forward (concat "\n" art)))
301 (delete-region (progn (beginning-of-line) (point))
302 (progn (forward-line 1) (point)))
303 (insert (int-to-string article) nov-line)
305 (kill-buffer (current-buffer)))
310 ;;; Internal functions
312 (defun nnml-retrieve-headers-with-nov (articles)
313 (if (or gnus-nov-is-evil nnml-nov-is-evil)
315 (let ((first (car articles))
316 (last (progn (while (cdr articles) (setq articles (cdr articles)))
318 (nov (concat nnml-current-directory ".nov")))
319 (if (file-exists-p nov)
321 (set-buffer nntp-server-buffer)
323 (insert-file-contents nov)
325 (while (and (not (eobp)) (< first (read (current-buffer))))
328 (if (not (eobp)) (delete-region 1 (point)))
329 (while (and (not (eobp)) (>= last (read (current-buffer))))
332 (if (not (eobp)) (delete-region (point) (point-max)))
335 (defun nnml-possibly-change-directory (newsgroup)
337 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
338 (and (file-directory-p pathname)
339 (setq nnml-current-directory pathname)))
342 (defun nnml-create-directories ()
343 (let ((methods nnmail-split-methods)
346 (setq dir (nnmail-article-pathname (car (car methods)) nnml-directory))
347 (while (not (file-directory-p dir))
348 (setq dirs (cons dir dirs))
349 (setq dir (file-name-directory (directory-file-name dir))))
351 (if (make-directory (directory-file-name (car dirs)))
352 (error "Could not create directory %s" (car dirs)))
353 (and gnus-verbose-backends
354 (message "Creating mail directory %s" (car dirs)))
355 (setq dirs (cdr dirs)))
356 (setq methods (cdr methods)))))
358 (defun nnml-save-mail ()
359 "Called narrowed to an article."
360 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
362 (setq chars (nnmail-insert-lines))
363 (nnmail-insert-xref group-art)
364 (goto-char (point-min))
365 (while (looking-at "From ")
366 (replace-match "X-From-Line: ")
368 ;; We save the article in all the newsgroups it belongs in.
372 (let ((file (concat (nnmail-article-pathname
373 (car (car ga)) nnml-directory)
374 (int-to-string (cdr (car ga))))))
376 ;; It was already saved, so we just make a hard link.
377 (add-name-to-file first file t)
379 (write-region (point-min) (point-max) file nil
380 (if gnus-verbose-backends nil 'nomesg))
383 ;; Generate a nov line for this article. We generate the nov
384 ;; line after saving, because nov generation destroys the
386 (setq nov-line (nnml-make-nov-line chars))
387 ;; Output the nov line to all nov databases that should have it.
388 (let ((ga group-art))
390 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
394 (defun nnml-active-number (group)
395 "Compute the next article number in GROUP."
396 (let ((active (car (cdr (assoc group nnml-group-alist)))))
397 (setcdr active (1+ (cdr active)))
399 (while (file-exists-p
400 (setq file (concat (nnmail-article-pathname
401 group nnml-directory)
402 (int-to-string (cdr active)))))
403 (setcdr active (1+ (cdr active)))))
406 (defun nnml-get-new-mail ()
407 "Read new incoming mail."
409 (nnml-create-directories)
410 (if (and nnml-get-new-mail nnmail-spool-file
411 (file-exists-p nnmail-spool-file)
412 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
414 (and gnus-verbose-backends
415 (message "nnml: Reading incoming mail..."))
417 (nnmail-move-inbox nnmail-spool-file
418 (concat nnml-directory "Incoming")))
420 (setq nnml-group-alist (nnmail-get-active))
421 (nnmail-split-incoming incoming 'nnml-save-mail)
422 (nnmail-save-active nnml-group-alist nnml-active-file)
424 (run-hooks 'nnmail-read-incoming-hook)
425 ;; The following has been commented away, just to make sure
426 ;; that nobody ever loses any mail. If you feel safe that
427 ;; nnml will never do anything strange, just remove those
428 ;; two semicolons, and avoid having lots of "Incoming*"
430 ;; (delete-file incoming)
431 (and gnus-verbose-backends
432 (message "nnml: Reading incoming mail...done"))))))
435 (defun nnml-add-nov (group article line)
436 "Add a nov line for the GROUP base."
438 (set-buffer (nnml-open-nov group))
439 (goto-char (point-max))
440 (insert (int-to-string article) line)))
442 (defsubst nnml-header-value ()
443 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
445 (defun nnml-make-nov-line (chars)
446 "Create a nov from the current headers."
447 (let ((case-fold-search t)
448 subject from date id references lines xref in-reply-to char)
451 (goto-char (point-min))
454 (1- (or (search-forward "\n\n" nil t) (point-max))))
455 ;; Fold continuation lines.
456 (goto-char (point-min))
457 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
458 (replace-match " " t t))
459 (subst-char-in-region (point-min) (point-max) ?\t ? )
460 ;; [number subject from date id references chars lines xref]
462 (goto-char (point-min))
463 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
466 (setq char (downcase (following-char)))
469 (setq subject (nnml-header-value)))
471 (setq from (nnml-header-value)))
473 (setq xref (nnml-header-value)))
475 (setq lines (nnml-header-value)))
477 (setq date (nnml-header-value)))
479 (setq id (setq id (nnml-header-value))))
481 (setq references (nnml-header-value)))
483 (setq in-reply-to (nnml-header-value))))
486 (and (not references)
488 (string-match "<[^>]+>" in-reply-to)
490 (substring in-reply-to (match-beginning 0)
492 ;; [number subject from date id references chars lines xref]
493 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
494 (or subject "(none)")
495 (or from "(nobody)") (or date "")
496 (or id "") (or references "")
497 (or chars 0) (or lines "0") (or xref ""))))))
499 (defun nnml-open-nov (group)
500 (or (cdr (assoc group nnml-nov-buffer-alist))
501 (let ((buffer (find-file-noselect
502 (concat (nnmail-article-pathname
503 group nnml-directory) ".nov"))))
506 (buffer-disable-undo (current-buffer)))
507 (setq nnml-nov-buffer-alist
508 (cons (cons group buffer) nnml-nov-buffer-alist))
511 (defun nnml-save-nov ()
513 (while nnml-nov-buffer-alist
514 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
516 (set-buffer (cdr (car nnml-nov-buffer-alist)))
517 (and (buffer-modified-p)
519 1 (point-max) (buffer-file-name) nil 'nomesg))
520 (set-buffer-modified-p nil)
521 (kill-buffer (current-buffer))))
522 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
525 (defun nnml-generate-nov-databases (dir)
526 "Generate nov databases in all nnml mail newsgroups."
529 (setq nnml-group-alist nil)
530 (list nnml-directory)))
531 (nnml-open-server (system-name))
532 (let ((dirs (directory-files dir t nil t)))
534 (if (and (not (string-match "/\\.\\.$" (car dirs)))
535 (not (string-match "/\\.$" (car dirs)))
536 (file-directory-p (car dirs)))
537 (nnml-generate-nov-databases (car dirs)))
538 (setq dirs (cdr dirs))))
543 (string-to-int name)))
544 (directory-files dir nil "^[0-9]+$" t))
546 (nov (concat dir "/.nov"))
547 (nov-buffer (get-buffer-create "*nov*"))
550 (setq nnml-group-alist
551 (cons (list (nnmail-replace-chars-in-string
552 (substring (expand-file-name dir)
553 (length (expand-file-name
558 (while (cdr f) (setq f (cdr f)))
563 (set-buffer nntp-server-buffer)
564 (if (file-exists-p nov)
567 (set-buffer nov-buffer)
568 (buffer-disable-undo (current-buffer))
572 (insert-file-contents (concat dir "/" (int-to-string (car files))))
574 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
575 (setq chars (- (point-max)
578 (setq nov-line (nnml-make-nov-line chars))
580 (set-buffer nov-buffer)
581 (goto-char (point-max))
582 (insert (int-to-string (car files)) nov-line))
584 (setq files (cdr files)))
586 (set-buffer nov-buffer)
587 (write-region 1 (point-max) (expand-file-name nov) nil
589 (kill-buffer (current-buffer)))))
590 (nnmail-save-active nnml-group-alist nnml-active-file)))
592 (defun nnml-nov-delete-article (group article)
594 (set-buffer (nnml-open-nov group))
596 (if (re-search-forward (concat "^" (int-to-string article) "\t"))
597 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
602 ;;; nnml.el ends here