1 ;;; nnml.el --- mail spool access for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Author: Lars 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)
70 ;;; Interface functions.
72 (defun nnml-retrieve-headers (sequence &optional newsgroup server)
73 "Retrieve the headers for the articles in SEQUENCE.
74 Newsgroup must be selected before calling this function."
76 (set-buffer nntp-server-buffer)
79 (number (length sequence))
82 (nnml-possibly-change-directory newsgroup)
83 (if (nnml-retrieve-headers-with-nov sequence)
86 (setq article (car sequence))
88 (concat nnml-current-directory (prin1-to-string article)))
89 (if (and (file-exists-p file)
90 (not (file-directory-p file)))
92 (insert (format "221 %d Article retrieved.\n" article))
94 (insert-file-contents file)
96 (if (search-forward "\n\n" nil t)
98 (goto-char (point-max))
101 (delete-region (point) (point-max))))
102 (setq sequence (cdr sequence))
103 (setq count (1+ count))
104 (and (numberp nnmail-large-newsgroup)
105 (> number nnmail-large-newsgroup)
107 gnus-verbose-backends
108 (message "nnml: Receiving headers... %d%%"
109 (/ (* count 100) number))))
111 (and (numberp nnmail-large-newsgroup)
112 (> number nnmail-large-newsgroup)
113 gnus-verbose-backends
114 (message "nnml: Receiving headers... done"))
116 ;; Fold continuation lines.
118 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
119 (replace-match " " t t))
122 (defun nnml-open-server (host &optional service)
123 (setq nnml-status-string "")
124 (nnheader-init-server-buffer))
126 (defun nnml-close-server (&optional server)
130 (defun nnml-server-opened (&optional server)
131 "Return server process status, T or NIL.
132 If the stream is opened, return T, otherwise return NIL."
133 (and nntp-server-buffer
134 (get-buffer nntp-server-buffer)))
136 (defun nnml-status-message ()
137 "Return server status response as string."
140 (defun nnml-request-article (id &optional newsgroup server buffer)
141 "Select article by message ID (or number)."
142 (nnml-possibly-change-directory newsgroup)
143 (let ((file (if (stringp id)
145 (concat nnml-current-directory (prin1-to-string id))))
146 (nntp-server-buffer (or buffer nntp-server-buffer)))
147 (if (and (stringp file)
149 (not (file-directory-p file)))
151 (nnmail-find-file file)))))
153 (defun nnml-request-group (group &optional server dont-check)
155 (nnml-possibly-change-directory group)
156 (or dont-check (nnml-get-new-mail))
160 (setq nnml-group-alist (nnmail-get-active))))
161 (let ((active (nth 1 (assoc group nnml-group-alist))))
163 (set-buffer nntp-server-buffer)
167 (insert (format "211 %d %d %d %s\n"
168 (max (1+ (- (cdr active) (car active))) 0)
169 (car active) (cdr active) group))
172 (defun nnml-close-group (group &optional server)
175 (defun nnml-request-list (&optional server)
176 "List active newsgoups."
177 (if server (nnml-get-new-mail))
179 (nnmail-find-file nnml-active-file)))
181 (defun nnml-request-newgroups (date &optional server)
182 "List groups created after DATE."
183 (nnml-request-list server))
185 (defun nnml-request-list-newsgroups (&optional server)
186 "List newsgroups (defined in NNTP2)."
188 (nnmail-find-file nnml-newsgroups-file)))
190 (defun nnml-request-post (&optional server)
191 "Post a new news in current buffer."
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 "Expire all articles in the ARTICLES list in group GROUP.
198 The list of unexpired articles will be returned (ie. all articles that
199 were too fresh to be expired).
200 If FORCE is non-nil, ARTICLES will be deleted whether they are old or not."
201 (nnml-possibly-change-directory newsgroup)
202 (let* ((days (or (and nnmail-expiry-wait-function
203 (funcall nnmail-expiry-wait-function newsgroup))
205 article rest mod-time)
207 (setq article (concat nnml-current-directory (int-to-string
209 (if (setq mod-time (nth 5 (file-attributes article)))
211 (> (nnmail-days-between
212 (current-time-string)
213 (current-time-string mod-time))
216 (and gnus-verbose-backends (message "Deleting %s..." article))
218 (delete-file article)
220 (nnml-nov-delete-article newsgroup (car articles)))
221 (setq rest (cons (car articles) rest))))
222 (setq articles (cdr articles)))
226 (defun nnml-request-move-article (article group server accept-form)
227 (let ((buf (get-buffer-create " *nnml move*"))
230 (nnml-request-article article group server)
233 (insert-buffer-substring nntp-server-buffer)
234 (setq result (eval accept-form))
235 (kill-buffer (current-buffer))
239 (delete-file (concat nnml-current-directory
240 (int-to-string article)))
242 (nnml-nov-delete-article group article)
246 (defun nnml-request-accept-article (group)
251 (setq nnml-group-alist (nnmail-get-active))
252 ;; We trick the choosing function into believing that only one
253 ;; group is availiable.
254 (let ((nnmail-split-methods (list (list group ""))))
255 (setq result (car (nnml-save-mail))))
257 (nnmail-save-active nnml-group-alist nnml-active-file)
261 (setq nnml-group-alist (nnmail-get-active))
262 (setq result (car (nnml-save-mail)))
264 (nnmail-save-active nnml-group-alist nnml-active-file)
268 (defun nnml-request-replace-article (article group buffer)
269 (nnml-possibly-change-directory group)
272 (if (not (condition-case ()
274 (write-region (point-min) (point-max)
275 (concat nnml-current-directory
276 (int-to-string article))
277 nil (if gnus-verbose-backends nil 'nomesg))
281 (let ((chars (nnmail-insert-lines))
282 (art (concat (int-to-string article) "\t"))
284 (setq nov-line (nnml-make-nov-line chars))
286 (set-buffer (nnml-open-nov group))
287 (goto-char (point-min))
288 (if (or (looking-at art)
289 (search-forward (concat "\n" art)))
291 (delete-region (progn (beginning-of-line) (point))
292 (progn (forward-line 1) (point)))
293 (insert (int-to-string article) nov-line)
295 (kill-buffer (current-buffer)))
300 ;;; Internal functions
302 (defun nnml-retrieve-headers-with-nov (articles)
303 (if (or gnus-nov-is-evil nnml-nov-is-evil)
305 (let ((first (car articles))
306 (last (progn (while (cdr articles) (setq articles (cdr articles)))
308 (nov (concat nnml-current-directory ".nov")))
309 (if (file-exists-p nov)
311 (set-buffer nntp-server-buffer)
313 (insert-file-contents nov)
315 (while (and (not (eobp)) (< first (read (current-buffer))))
318 (if (not (eobp)) (delete-region 1 (point)))
319 (while (and (not (eobp)) (>= last (read (current-buffer))))
322 (if (not (eobp)) (delete-region (point) (point-max)))
325 (defun nnml-possibly-change-directory (newsgroup)
327 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
328 (if (file-directory-p pathname)
329 (setq nnml-current-directory pathname)
330 (error "No such newsgroup: %s" newsgroup)))))
332 (defun nnml-create-directories ()
333 (let ((methods nnmail-split-methods)
336 (setq dir (nnmail-article-pathname (car (car methods)) nnml-directory))
337 (while (not (file-directory-p dir))
338 (setq dirs (cons dir dirs))
339 (setq dir (file-name-directory (directory-file-name dir))))
341 (if (make-directory (directory-file-name (car dirs)))
342 (error "Could not create directory %s" (car dirs)))
343 (and gnus-verbose-backends
344 (message "Creating mail directory %s" (car dirs)))
345 (setq dirs (cdr dirs)))
346 (setq methods (cdr methods)))))
348 (defun nnml-save-mail ()
349 "Called narrowed to an article."
350 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
352 (setq chars (nnmail-insert-lines))
353 (nnmail-insert-xref group-art)
354 (goto-char (point-min))
355 (while (looking-at "From ")
356 (replace-match "X-From-Line: ")
358 ;; We save the article in all the newsgroups it belongs in.
362 (let ((file (concat (nnmail-article-pathname
363 (car (car ga)) nnml-directory)
364 (int-to-string (cdr (car ga))))))
366 ;; It was already saved, so we just make a hard link.
367 (add-name-to-file first file t)
369 (write-region (point-min) (point-max) file nil
370 (if gnus-verbose-backends nil 'nomesg))
373 ;; Generate a nov line for this article. We generate the nov
374 ;; line after saving, because nov generation destroys the
376 (setq nov-line (nnml-make-nov-line chars))
377 ;; Output the nov line to all nov databases that should have it.
378 (let ((ga group-art))
380 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
384 (defun nnml-active-number (group)
385 "Compute the next article number in GROUP."
386 (let ((active (car (cdr (assoc group nnml-group-alist)))))
387 (setcdr active (1+ (cdr active)))
389 (while (file-exists-p
390 (setq file (concat (nnmail-article-pathname
391 group nnml-directory)
392 (int-to-string (cdr active)))))
393 (setcdr active (1+ (cdr active)))))
396 (defun nnml-get-new-mail ()
397 "Read new incoming mail."
399 (nnml-create-directories)
400 (if (and nnml-get-new-mail nnmail-spool-file
401 (file-exists-p nnmail-spool-file)
402 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
404 (and gnus-verbose-backends
405 (message "nnml: Reading incoming mail..."))
407 (nnmail-move-inbox nnmail-spool-file
408 (concat nnml-directory "Incoming")))
410 (setq nnml-group-alist (nnmail-get-active))
411 (nnmail-split-incoming incoming 'nnml-save-mail)
412 (nnmail-save-active nnml-group-alist nnml-active-file)
414 (run-hooks 'nnmail-read-incoming-hook)
415 ;; The following has been commented away, just to make sure
416 ;; that nobody ever loses any mail. If you feel safe that
417 ;; nnml will never do anything strange, just remove those
418 ;; two semicolons, and avoid having lots of "Incoming*"
420 ;; (delete-file incoming)
421 (and gnus-verbose-backends
422 (message "nnml: Reading incoming mail...done"))))))
425 (defun nnml-add-nov (group article line)
426 "Add a nov line for the GROUP base."
428 (set-buffer (nnml-open-nov group))
429 (goto-char (point-max))
430 (insert (int-to-string article) line)))
432 (defsubst nnml-header-value ()
433 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
435 (defun nnml-make-nov-line (chars)
436 "Create a nov from the current headers."
437 (let ((case-fold-search t)
438 subject from date id references lines xref in-reply-to char)
441 (goto-char (point-min))
444 (1- (or (search-forward "\n\n" nil t) (point-max))))
445 ;; Fold continuation lines.
446 (goto-char (point-min))
447 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
448 (replace-match " " t t))
449 (subst-char-in-region (point-min) (point-max) ?\t ? )
450 ;; [number subject from date id references chars lines xref]
452 (goto-char (point-min))
453 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
456 (setq char (downcase (following-char)))
459 (setq subject (nnml-header-value)))
461 (setq from (nnml-header-value)))
463 (setq xref (nnml-header-value)))
465 (setq lines (nnml-header-value)))
467 (setq date (nnml-header-value)))
469 (setq id (setq id (nnml-header-value))))
471 (setq references (nnml-header-value)))
473 (setq in-reply-to (nnml-header-value))))
476 (and (not references)
478 (string-match "<[^>]+>" in-reply-to)
480 (substring in-reply-to (match-beginning 0)
482 ;; [number subject from date id references chars lines xref]
483 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
484 (or subject "(none)")
485 (or from "(nobody)") (or date "")
486 (or id "") (or references "")
487 (or chars 0) (or lines "0") (or xref ""))))))
489 (defun nnml-open-nov (group)
490 (or (cdr (assoc group nnml-nov-buffer-alist))
491 (let ((buffer (find-file-noselect
492 (concat (nnmail-article-pathname
493 group nnml-directory) ".nov"))))
496 (buffer-disable-undo (current-buffer)))
497 (setq nnml-nov-buffer-alist
498 (cons (cons group buffer) nnml-nov-buffer-alist))
501 (defun nnml-save-nov ()
503 (while nnml-nov-buffer-alist
504 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
506 (set-buffer (cdr (car nnml-nov-buffer-alist)))
507 (write-region 1 (point-max) (buffer-file-name) nil 'nomesg)
508 (set-buffer-modified-p nil)
509 (kill-buffer (current-buffer))))
510 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
512 (defun nnml-generate-nov-databases (dir)
513 "Generate nov databases in all nnml mail newsgroups."
516 (setq nnml-group-alist nil)
517 (list nnml-directory)))
518 (nnml-open-server (system-name))
519 (let ((dirs (directory-files dir t nil t)))
521 (if (and (not (string-match "/\\.\\.$" (car dirs)))
522 (not (string-match "/\\.$" (car dirs)))
523 (file-directory-p (car dirs)))
524 (nnml-generate-nov-databases (car dirs)))
525 (setq dirs (cdr dirs))))
530 (string-to-int name)))
531 (directory-files dir nil "^[0-9]+$" t))
533 (nov (concat dir "/.nov"))
534 (nov-buffer (get-buffer-create "*nov*"))
537 (setq nnml-group-alist
538 (cons (list (nnmail-replace-chars-in-string
539 (substring (expand-file-name dir)
540 (length (expand-file-name
545 (while (cdr f) (setq f (cdr f)))
550 (set-buffer nntp-server-buffer)
551 (if (file-exists-p nov)
554 (set-buffer nov-buffer)
555 (buffer-disable-undo (current-buffer))
559 (insert-file-contents (concat dir "/" (int-to-string (car files))))
561 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
562 (setq chars (- (point-max)
565 (setq nov-line (nnml-make-nov-line chars))
567 (set-buffer nov-buffer)
568 (goto-char (point-max))
569 (insert (int-to-string (car files)) nov-line))
571 (setq files (cdr files)))
573 (set-buffer nov-buffer)
574 (write-region 1 (point-max) (expand-file-name nov) nil
576 (kill-buffer (current-buffer)))))
577 (nnmail-save-active nnml-group-alist nnml-active-file)))
579 (defun nnml-nov-delete-article (group article)
581 (set-buffer (nnml-open-nov group))
583 (if (re-search-forward (concat "^" (int-to-string article) "\t"))
584 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
589 ;;; nnml.el ends here