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-create-group (group &optional server)
181 (setq nnml-group-alist (nnmail-get-active))
182 (or (assoc group nnml-group-alist)
184 (setq nnml-group-alist (cons (list group (setq active (cons 0 0)))
186 (nnml-possibly-create-directory group)
187 (nnml-possibly-change-directory group)
188 (let ((articles (mapcar
190 (int-to-string file))
192 nnml-current-directory nil "^[0-9]+$"))))
195 (setcar active (apply 'min articles))
196 (setcdr active (apply 'max articles)))))
197 (nnmail-save-active nnml-group-alist nnml-active-file)))
200 (defun nnml-request-list (&optional server)
201 (if server (nnml-get-new-mail))
203 (nnmail-find-file nnml-active-file)))
205 (defun nnml-request-newgroups (date &optional server)
206 (nnml-request-list server))
208 (defun nnml-request-list-newsgroups (&optional server)
210 (nnmail-find-file nnml-newsgroups-file)))
212 (defun nnml-request-post (&optional server)
213 (mail-send-and-exit nil))
215 (fset 'nnml-request-post-buffer 'nnmail-request-post-buffer)
217 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
218 (nnml-possibly-change-directory newsgroup)
219 (let* ((days (or (and nnmail-expiry-wait-function
220 (funcall nnmail-expiry-wait-function newsgroup))
226 (string-to-int name)))
227 (directory-files nnml-current-directory nil "^[0-9]+$" t)))
228 (max-article (and active-articles (apply 'max active-articles)))
229 article rest mod-time)
231 (setq article (concat nnml-current-directory (int-to-string
233 (if (setq mod-time (nth 5 (file-attributes article)))
234 (if (and (or (not nnmail-keep-last-article)
236 (not (= (car articles) max-article)))
238 (> (nnmail-days-between
239 (current-time-string)
240 (current-time-string mod-time))
243 (and gnus-verbose-backends (message "Deleting %s..." article))
245 (delete-file article)
247 (setq active-articles (delq (car articles) active-articles))
248 (nnml-nov-delete-article newsgroup (car articles)))
249 (setq rest (cons (car articles) rest))))
250 (setq articles (cdr articles)))
251 (let ((active (nth 1 (assoc newsgroup nnml-group-alist))))
252 (setcar active (or (and active-articles (apply 'min active-articles))
254 (nnmail-save-active nnml-group-alist nnml-active-file))
258 (defun nnml-request-move-article
259 (article group server accept-form &optional last)
260 (let ((buf (get-buffer-create " *nnml move*"))
263 (nnml-request-article article group server)
266 (insert-buffer-substring nntp-server-buffer)
267 (setq result (eval accept-form))
268 (kill-buffer (current-buffer))
272 (delete-file (concat nnml-current-directory
273 (int-to-string article)))
275 (nnml-nov-delete-article group article)
276 (and last (nnml-save-nov))))
279 (defun nnml-request-accept-article (group &optional last)
284 (setq nnml-group-alist (nnmail-get-active))
285 ;; We trick the choosing function into believing that only one
286 ;; group is availiable.
287 (let ((nnmail-split-methods (list (list group ""))))
288 (setq result (car (nnml-save-mail))))
290 (nnmail-save-active nnml-group-alist nnml-active-file)
291 (and last (nnml-save-nov))))
294 (setq nnml-group-alist (nnmail-get-active))
295 (setq result (car (nnml-save-mail)))
297 (nnmail-save-active nnml-group-alist nnml-active-file)
298 (and last (nnml-save-nov)))))
301 (defun nnml-request-replace-article (article group buffer)
302 (nnml-possibly-change-directory group)
305 (nnml-possibly-create-directory group)
306 (if (not (condition-case ()
308 (write-region (point-min) (point-max)
309 (concat nnml-current-directory
310 (int-to-string article))
311 nil (if gnus-verbose-backends nil 'nomesg))
315 (let ((chars (nnmail-insert-lines))
316 (art (concat (int-to-string article) "\t"))
318 (setq nov-line (nnml-make-nov-line chars))
320 (set-buffer (nnml-open-nov group))
321 (goto-char (point-min))
322 (if (or (looking-at art)
323 (search-forward (concat "\n" art)))
325 (delete-region (progn (beginning-of-line) (point))
326 (progn (forward-line 1) (point)))
327 (insert (int-to-string article) nov-line)
329 (kill-buffer (current-buffer)))
334 ;;; Internal functions
336 (defun nnml-retrieve-headers-with-nov (articles)
337 (if (or gnus-nov-is-evil nnml-nov-is-evil)
339 (let ((first (car articles))
340 (last (progn (while (cdr articles) (setq articles (cdr articles)))
342 (nov (concat nnml-current-directory ".nov")))
343 (if (file-exists-p nov)
345 (set-buffer nntp-server-buffer)
347 (insert-file-contents nov)
349 (while (and (not (eobp)) (< first (read (current-buffer))))
352 (if (not (eobp)) (delete-region 1 (point)))
353 (while (and (not (eobp)) (>= last (read (current-buffer))))
356 (if (not (eobp)) (delete-region (point) (point-max)))
359 (defun nnml-possibly-change-directory (newsgroup &optional force)
361 (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
362 (and (or force (file-directory-p pathname))
363 (setq nnml-current-directory pathname)))
366 (defun nnml-possibly-create-directory (group)
368 (setq dir (nnmail-article-pathname group nnml-directory))
369 (while (not (file-directory-p dir))
370 (setq dirs (cons dir dirs))
371 (setq dir (file-name-directory (directory-file-name dir))))
373 (if (make-directory (directory-file-name (car dirs)))
374 (error "Could not create directory %s" (car dirs)))
375 (and gnus-verbose-backends
376 (message "Creating mail directory %s" (car dirs)))
377 (setq dirs (cdr dirs)))))
379 (defun nnml-save-mail ()
380 "Called narrowed to an article."
381 (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
383 (setq chars (nnmail-insert-lines))
384 (nnmail-insert-xref group-art)
385 (goto-char (point-min))
386 (while (looking-at "From ")
387 (replace-match "X-From-Line: ")
389 ;; We save the article in all the newsgroups it belongs in.
393 (nnml-possibly-create-directory (car (car ga)))
394 (let ((file (concat (nnmail-article-pathname
395 (car (car ga)) nnml-directory)
396 (int-to-string (cdr (car ga))))))
398 ;; It was already saved, so we just make a hard link.
399 (add-name-to-file first file t)
401 (write-region (point-min) (point-max) file nil
402 (if gnus-verbose-backends nil 'nomesg))
405 ;; Generate a nov line for this article. We generate the nov
406 ;; line after saving, because nov generation destroys the
408 (setq nov-line (nnml-make-nov-line chars))
409 ;; Output the nov line to all nov databases that should have it.
410 (let ((ga group-art))
412 (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
416 (defun nnml-active-number (group)
417 "Compute the next article number in GROUP."
418 (let ((active (car (cdr (assoc group nnml-group-alist)))))
421 (setq active (cons 1 0))
422 (setq nnml-group-alist (cons (list group active) nnml-group-alist))))
423 (setcdr active (1+ (cdr active)))
425 (while (file-exists-p
426 (setq file (concat (nnmail-article-pathname
427 group nnml-directory)
428 (int-to-string (cdr active)))))
429 (setcdr active (1+ (cdr active)))))
432 (defun nnml-get-new-mail ()
433 "Read new incoming mail."
435 (if (and nnml-get-new-mail nnmail-spool-file
436 (file-exists-p nnmail-spool-file)
437 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
439 (and gnus-verbose-backends
440 (message "nnml: Reading incoming mail..."))
442 (nnmail-move-inbox nnmail-spool-file
443 (concat nnml-directory "Incoming")))
445 (setq nnml-group-alist (nnmail-get-active))
446 (nnmail-split-incoming incoming 'nnml-save-mail)
447 (nnmail-save-active nnml-group-alist nnml-active-file)
449 (run-hooks 'nnmail-read-incoming-hook)
450 ;; The following has been commented away, just to make sure
451 ;; that nobody ever loses any mail. If you feel safe that
452 ;; nnml will never do anything strange, just remove those
453 ;; two semicolons, and avoid having lots of "Incoming*"
455 ;; (delete-file incoming)
456 (and gnus-verbose-backends
457 (message "nnml: Reading incoming mail...done"))))))
460 (defun nnml-add-nov (group article line)
461 "Add a nov line for the GROUP base."
463 (set-buffer (nnml-open-nov group))
464 (goto-char (point-max))
465 (insert (int-to-string article) line)))
467 (defsubst nnml-header-value ()
468 (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
470 (defun nnml-make-nov-line (chars)
471 "Create a nov from the current headers."
472 (let ((case-fold-search t)
473 subject from date id references lines xref in-reply-to char)
476 (goto-char (point-min))
479 (1- (or (search-forward "\n\n" nil t) (point-max))))
480 ;; Fold continuation lines.
481 (goto-char (point-min))
482 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
483 (replace-match " " t t))
484 (subst-char-in-region (point-min) (point-max) ?\t ? )
485 ;; [number subject from date id references chars lines xref]
487 (goto-char (point-min))
488 (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
491 (setq char (downcase (following-char)))
494 (setq subject (nnml-header-value)))
496 (setq from (nnml-header-value)))
498 (setq xref (nnml-header-value)))
500 (setq lines (nnml-header-value)))
502 (setq date (nnml-header-value)))
504 (setq id (setq id (nnml-header-value))))
506 (setq references (nnml-header-value)))
508 (setq in-reply-to (nnml-header-value))))
511 (and (not references)
513 (string-match "<[^>]+>" in-reply-to)
515 (substring in-reply-to (match-beginning 0)
517 ;; [number subject from date id references chars lines xref]
518 (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
519 (or subject "(none)")
520 (or from "(nobody)") (or date "")
521 (or id (concat "nnml-dummy-id-"
523 (lambda (time) (int-to-string time))
524 (current-time) "-")))
526 (or chars 0) (or lines "0") (or xref ""))))))
528 (defun nnml-open-nov (group)
529 (or (cdr (assoc group nnml-nov-buffer-alist))
530 (let ((buffer (find-file-noselect
531 (concat (nnmail-article-pathname
532 group nnml-directory) ".nov"))))
535 (buffer-disable-undo (current-buffer)))
536 (setq nnml-nov-buffer-alist
537 (cons (cons group buffer) nnml-nov-buffer-alist))
540 (defun nnml-save-nov ()
542 (while nnml-nov-buffer-alist
543 (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
545 (set-buffer (cdr (car nnml-nov-buffer-alist)))
546 (and (buffer-modified-p)
548 1 (point-max) (buffer-file-name) nil 'nomesg))
549 (set-buffer-modified-p nil)
550 (kill-buffer (current-buffer))))
551 (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
554 (defun nnml-generate-nov-databases (dir)
555 "Generate nov databases in all nnml mail newsgroups."
558 (setq nnml-group-alist nil)
559 (list nnml-directory)))
560 (nnml-open-server (system-name))
561 (let ((dirs (directory-files dir t nil t)))
563 (if (and (not (string-match "/\\.\\.$" (car dirs)))
564 (not (string-match "/\\.$" (car dirs)))
565 (file-directory-p (car dirs)))
566 (nnml-generate-nov-databases (car dirs)))
567 (setq dirs (cdr dirs))))
572 (string-to-int name)))
573 (directory-files dir nil "^[0-9]+$" t))
575 (nov (concat dir "/.nov"))
576 (nov-buffer (get-buffer-create "*nov*"))
579 (setq nnml-group-alist
580 (cons (list (nnmail-replace-chars-in-string
581 (substring (expand-file-name dir)
582 (length (expand-file-name
587 (while (cdr f) (setq f (cdr f)))
592 (set-buffer nntp-server-buffer)
593 (if (file-exists-p nov)
596 (set-buffer nov-buffer)
597 (buffer-disable-undo (current-buffer))
601 (insert-file-contents (concat dir "/" (int-to-string (car files))))
603 (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
604 (setq chars (- (point-max)
607 (if (not (= 0 chars)) ; none of them empty files...
609 (setq nov-line (nnml-make-nov-line chars))
611 (set-buffer nov-buffer)
612 (goto-char (point-max))
613 (insert (int-to-string (car files)) nov-line))))
615 (setq files (cdr files)))
617 (set-buffer nov-buffer)
618 (write-region 1 (point-max) (expand-file-name nov) nil
620 (kill-buffer (current-buffer)))))
621 (nnmail-save-active nnml-group-alist nnml-active-file)))
623 (defun nnml-nov-delete-article (group article)
625 (set-buffer (nnml-open-nov group))
627 (if (re-search-forward (concat "^" (int-to-string article) "\t"))
628 (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
633 ;;; nnml.el ends here