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>.
33 (defvar nnml-directory "~/Mail/"
36 (defvar nnml-active-file (concat nnml-directory "active")
39 (defvar nnml-newsgroups-file (concat nnml-directory "newsgroups")
40 "Mail newsgroups description file.")
42 (defvar nnml-get-new-mail t
43 "If non-nil, nnml will check the incoming mail file and split the mail.")
45 (defvar nnml-nov-is-evil nil
46 "If non-nil, Gnus will never generate and use nov databases for mail groups.
47 Using nov databases will speed up header fetching considerably.
48 This variable shouldn't be flipped much. If you have, for some reason,
49 set this to t, and want to set it to nil again, you should always run
50 the `nnml-generate-nov-databases' command. The function will go
51 through all nnml directories and generate nov databases for them
52 all. This may very well take some time.")
56 (defconst nnml-version "nnml 0.2"
59 (defvar nnml-current-directory nil
60 "Current news group directory.")
62 (defvar nnml-status-string "")
64 (defvar nnml-nov-buffer-alist nil)
66 (defvar nnml-group-alist nil)
67 (defvar nnml-active-timestamp nil)
71 ;;; Interface functions.
73 (defun nnml-retrieve-headers (sequence &optional newsgroup server)
74 "Retrieve the headers for the articles in SEQUENCE.
75 Newsgroup must be selected before calling this function."
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)
131 (defun nnml-server-opened (&optional server)
132 "Return server process status, T or NIL.
133 If the stream is opened, return T, otherwise return NIL."
134 (and nntp-server-buffer
135 (get-buffer nntp-server-buffer)))
137 (defun nnml-status-message (&optional server)
138 "Return server status response as string."
141 (defun nnml-request-article (id &optional newsgroup server buffer)
142 "Select article by message ID (or number)."
143 (nnml-possibly-change-directory newsgroup)
144 (let ((file (if (stringp id)
146 (concat nnml-current-directory (prin1-to-string id))))
147 (nntp-server-buffer (or buffer nntp-server-buffer)))
148 (if (and (stringp file)
150 (not (file-directory-p file)))
152 (nnmail-find-file file)))))
154 (defun nnml-request-group (group &optional server dont-check)
156 (if (not (nnml-possibly-change-directory group))
158 (setq nnml-status-string "Invalid group (no such directory)")
163 (let ((timestamp (nth 5 (file-attributes nnml-active-file))))
164 (if (or (not nnml-active-timestamp)
165 (> (nth 0 timestamp) (nth 0 nnml-active-timestamp))
166 (> (nth 1 timestamp) (nth 1 nnml-active-timestamp)))
168 (setq nnml-active-timestamp timestamp)
170 (setq nnml-group-alist (nnmail-get-active))))
171 (let ((active (nth 1 (assoc group nnml-group-alist))))
173 (set-buffer nntp-server-buffer)
177 (insert (format "211 %d %d %d %s\n"
178 (max (1+ (- (cdr active) (car active))) 0)
179 (car active) (cdr active) group))
182 (defun nnml-close-group (group &optional server)
185 (defun nnml-request-list (&optional server)
186 "List active newsgoups."
187 (if server (nnml-get-new-mail))
189 (nnmail-find-file nnml-active-file)))
191 (defun nnml-request-newgroups (date &optional server)
192 "List groups created after DATE."
193 (nnml-request-list server))
195 (defun nnml-request-list-newsgroups (&optional server)
196 "List newsgroups (defined in NNTP2)."
198 (nnmail-find-file nnml-newsgroups-file)))
200 (defun nnml-request-post (&optional server)
201 "Post a new news in current buffer."
202 (mail-send-and-exit nil))
204 (fset 'nnml-request-post-buffer 'nnmail-request-post-buffer)
206 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
207 "Expire all articles in the ARTICLES list in group GROUP.
208 The list of unexpired articles will be returned (ie. all articles that
209 were too fresh to be expired).
210 If FORCE is non-nil, ARTICLES will be deleted whether they are old or not."
211 (nnml-possibly-change-directory newsgroup)
212 (let* ((days (or (and nnmail-expiry-wait-function
213 (funcall nnmail-expiry-wait-function newsgroup))
215 article rest mod-time)
216 (if nnmail-keep-last-article
218 (setq articles (sort articles '>))
219 (setq rest (cons (car articles) rest))
220 (setq articles (cdr articles))))
222 (setq article (concat nnml-current-directory (int-to-string
224 (if (setq mod-time (nth 5 (file-attributes article)))
226 (> (nnmail-days-between
227 (current-time-string)
228 (current-time-string mod-time))
231 (and gnus-verbose-backends (message "Deleting %s..." article))
233 (delete-file article)
235 (nnml-nov-delete-article newsgroup (car articles)))
236 (setq rest (cons (car articles) rest))))
237 (setq articles (cdr articles)))
241 (defun nnml-request-move-article
242 (article group server accept-form &optional last)
243 (let ((buf (get-buffer-create " *nnml move*"))
246 (nnml-request-article article group server)
249 (insert-buffer-substring nntp-server-buffer)
250 (setq result (eval accept-form))
251 (kill-buffer (current-buffer))
255 (delete-file (concat nnml-current-directory
256 (int-to-string article)))
258 (nnml-nov-delete-article group article)
259 (and last (nnml-save-nov))))
262 (defun nnml-request-accept-article (group &optional last)
267 (setq nnml-group-alist (nnmail-get-active))
268 ;; We trick the choosing function into believing that only one
269 ;; group is availiable.
270 (let ((nnmail-split-methods (list (list group ""))))
271 (setq result (car (nnml-save-mail))))
273 (nnmail-save-active nnml-group-alist nnml-active-file)
274 (and last (nnml-save-nov))))
277 (setq nnml-group-alist (nnmail-get-active))
278 (setq result (car (nnml-save-mail)))
280 (nnmail-save-active nnml-group-alist nnml-active-file)
281 (and last (nnml-save-nov)))))
284 (defun nnml-request-replace-article (article group buffer)
285 (nnml-possibly-change-directory group)
288 (if (not (condition-case ()
290 (write-region (point-min) (point-max)
291 (concat nnml-current-directory
292 (int-to-string article))
293 nil (if gnus-verbose-backends nil 'nomesg))
297 (let ((chars (nnmail-insert-lines))
298 (art (concat (int-to-string article) "\t"))
300 (setq nov-line (nnml-make-nov-line chars))
302 (set-buffer (nnml-open-nov group))
303 (goto-char (point-min))
304 (if (or (looking-at art)
305 (search-forward (concat "\n" art)))
307 (delete-region (progn (beginning-of-line) (point))
308 (progn (forward-line 1) (point)))
309 (insert (int-to-string article) nov-line)
311 (kill-buffer (current-buffer)))
316 ;;; Internal functions
318 (defun nnml-retrieve-headers-with-nov (articles)
319 (if (or gnus-nov-is-evil nnml-nov-is-evil)
321 (let ((first (car articles))
322 (last (progn (while (cdr articles) (setq articles (cdr articles)))
324 (nov (concat nnml-current-directory ".nov")))
325 (if (file-exists-p nov)
327 (set-buffer nntp-server-buffer)
329 (insert-file-contents nov)
331 (while (and (not (eobp)) (< first (read (current-buffer))))
334 (if (not (eobp)) (delete-region 1 (point)))
335 (while (and (not (eobp)) (>= last (read (current-buffer))))
338 (if (not (eobp)) (delete-region (point) (point-max)))
341 (defun nnml-possibly-change-directory (newsgroup)
343 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
344 (and (file-directory-p pathname)
345 (setq nnml-current-directory pathname)))
348 (defun nnml-create-directories ()
349 (let ((methods nnmail-split-methods)
352 (setq dir (nnmail-article-pathname (car (car methods)) nnml-directory))
353 (while (not (file-directory-p dir))
354 (setq dirs (cons dir dirs))
355 (setq dir (file-name-directory (directory-file-name dir))))
357 (if (make-directory (directory-file-name (car dirs)))
358 (error "Could not create directory %s" (car dirs)))
359 (and gnus-verbose-backends
360 (message "Creating mail directory %s" (car dirs)))
361 (setq dirs (cdr dirs)))
362 (setq methods (cdr methods)))))
364 (defun nnml-save-mail ()
365 "Called narrowed to an article."
366 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
368 (setq chars (nnmail-insert-lines))
369 (nnmail-insert-xref group-art)
370 (goto-char (point-min))
371 (while (looking-at "From ")
372 (replace-match "X-From-Line: ")
374 ;; We save the article in all the newsgroups it belongs in.
378 (let ((file (concat (nnmail-article-pathname
379 (car (car ga)) nnml-directory)
380 (int-to-string (cdr (car ga))))))
382 ;; It was already saved, so we just make a hard link.
383 (add-name-to-file first file t)
385 (write-region (point-min) (point-max) file nil
386 (if gnus-verbose-backends nil 'nomesg))
389 ;; Generate a nov line for this article. We generate the nov
390 ;; line after saving, because nov generation destroys the
392 (setq nov-line (nnml-make-nov-line chars))
393 ;; Output the nov line to all nov databases that should have it.
394 (let ((ga group-art))
396 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
400 (defun nnml-active-number (group)
401 "Compute the next article number in GROUP."
402 (let ((active (car (cdr (assoc group nnml-group-alist)))))
403 (setcdr active (1+ (cdr active)))
405 (while (file-exists-p
406 (setq file (concat (nnmail-article-pathname
407 group nnml-directory)
408 (int-to-string (cdr active)))))
409 (setcdr active (1+ (cdr active)))))
412 (defun nnml-get-new-mail ()
413 "Read new incoming mail."
415 (nnml-create-directories)
416 (if (and nnml-get-new-mail nnmail-spool-file
417 (file-exists-p nnmail-spool-file)
418 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
420 (and gnus-verbose-backends
421 (message "nnml: Reading incoming mail..."))
423 (nnmail-move-inbox nnmail-spool-file
424 (concat nnml-directory "Incoming")))
426 (setq nnml-group-alist (nnmail-get-active))
427 (nnmail-split-incoming incoming 'nnml-save-mail)
428 (nnmail-save-active nnml-group-alist nnml-active-file)
430 (run-hooks 'nnmail-read-incoming-hook)
431 ;; The following has been commented away, just to make sure
432 ;; that nobody ever loses any mail. If you feel safe that
433 ;; nnml will never do anything strange, just remove those
434 ;; two semicolons, and avoid having lots of "Incoming*"
436 ;; (delete-file incoming)
437 (and gnus-verbose-backends
438 (message "nnml: Reading incoming mail...done"))))))
441 (defun nnml-add-nov (group article line)
442 "Add a nov line for the GROUP base."
444 (set-buffer (nnml-open-nov group))
445 (goto-char (point-max))
446 (insert (int-to-string article) line)))
448 (defsubst nnml-header-value ()
449 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
451 (defun nnml-make-nov-line (chars)
452 "Create a nov from the current headers."
453 (let ((case-fold-search t)
454 subject from date id references lines xref in-reply-to char)
457 (goto-char (point-min))
460 (1- (or (search-forward "\n\n" nil t) (point-max))))
461 ;; Fold continuation lines.
462 (goto-char (point-min))
463 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
464 (replace-match " " t t))
465 (subst-char-in-region (point-min) (point-max) ?\t ? )
466 ;; [number subject from date id references chars lines xref]
468 (goto-char (point-min))
469 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
472 (setq char (downcase (following-char)))
475 (setq subject (nnml-header-value)))
477 (setq from (nnml-header-value)))
479 (setq xref (nnml-header-value)))
481 (setq lines (nnml-header-value)))
483 (setq date (nnml-header-value)))
485 (setq id (setq id (nnml-header-value))))
487 (setq references (nnml-header-value)))
489 (setq in-reply-to (nnml-header-value))))
492 (and (not references)
494 (string-match "<[^>]+>" in-reply-to)
496 (substring in-reply-to (match-beginning 0)
498 ;; [number subject from date id references chars lines xref]
499 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
500 (or subject "(none)")
501 (or from "(nobody)") (or date "")
502 (or id "") (or references "")
503 (or chars 0) (or lines "0") (or xref ""))))))
505 (defun nnml-open-nov (group)
506 (or (cdr (assoc group nnml-nov-buffer-alist))
507 (let ((buffer (find-file-noselect
508 (concat (nnmail-article-pathname
509 group nnml-directory) ".nov"))))
512 (buffer-disable-undo (current-buffer)))
513 (setq nnml-nov-buffer-alist
514 (cons (cons group buffer) nnml-nov-buffer-alist))
517 (defun nnml-save-nov ()
519 (while nnml-nov-buffer-alist
520 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
522 (set-buffer (cdr (car nnml-nov-buffer-alist)))
523 (and (buffer-modified-p)
525 1 (point-max) (buffer-file-name) nil 'nomesg))
526 (set-buffer-modified-p nil)
527 (kill-buffer (current-buffer))))
528 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
531 (defun nnml-generate-nov-databases (dir)
532 "Generate nov databases in all nnml mail newsgroups."
535 (setq nnml-group-alist nil)
536 (list nnml-directory)))
537 (nnml-open-server (system-name))
538 (let ((dirs (directory-files dir t nil t)))
540 (if (and (not (string-match "/\\.\\.$" (car dirs)))
541 (not (string-match "/\\.$" (car dirs)))
542 (file-directory-p (car dirs)))
543 (nnml-generate-nov-databases (car dirs)))
544 (setq dirs (cdr dirs))))
549 (string-to-int name)))
550 (directory-files dir nil "^[0-9]+$" t))
552 (nov (concat dir "/.nov"))
553 (nov-buffer (get-buffer-create "*nov*"))
556 (setq nnml-group-alist
557 (cons (list (nnmail-replace-chars-in-string
558 (substring (expand-file-name dir)
559 (length (expand-file-name
564 (while (cdr f) (setq f (cdr f)))
569 (set-buffer nntp-server-buffer)
570 (if (file-exists-p nov)
573 (set-buffer nov-buffer)
574 (buffer-disable-undo (current-buffer))
578 (insert-file-contents (concat dir "/" (int-to-string (car files))))
580 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
581 (setq chars (- (point-max)
584 (setq nov-line (nnml-make-nov-line chars))
586 (set-buffer nov-buffer)
587 (goto-char (point-max))
588 (insert (int-to-string (car files)) nov-line))
590 (setq files (cdr files)))
592 (set-buffer nov-buffer)
593 (write-region 1 (point-max) (expand-file-name nov) nil
595 (kill-buffer (current-buffer)))))
596 (nnmail-save-active nnml-group-alist nnml-active-file)))
598 (defun nnml-nov-delete-article (group article)
600 (set-buffer (nnml-open-nov group))
602 (if (re-search-forward (concat "^" (int-to-string article) "\t"))
603 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
608 ;;; nnml.el ends here