1 ;;; nnmh.el --- mail spool access for Gnus (mhspool)
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
37 (defvar nnmh-directory "~/Mail/"
40 (defvar nnmh-get-new-mail t
41 "If non-nil, nnmh will check the incoming mail file and split the mail.")
45 (defconst nnmh-version "nnmh 0.1"
48 (defvar nnmh-current-directory nil
49 "Current news group directory.")
51 (defvar nnmh-status-string "")
52 (defvar nnmh-group-alist nil)
56 (defvar nnmh-current-server nil)
57 (defvar nnmh-server-alist nil)
58 (defvar nnmh-server-variables
60 (list 'nnmh-directory nnmh-directory)
61 (list 'nnmh-get-new-mail nnmh-get-new-mail)
62 '(nnmh-current-directory nil)
63 '(nnmh-status-string "")
68 ;;; Interface functions.
70 (defun nnmh-retrieve-headers (sequence &optional newsgroup server)
72 (set-buffer nntp-server-buffer)
75 (number (length sequence))
78 (nnmh-possibly-change-directory newsgroup)
80 (setq article (car sequence))
82 (concat nnmh-current-directory (prin1-to-string article)))
83 (if (and (file-exists-p file)
84 (not (file-directory-p file)))
86 (insert (format "221 %d Article retrieved.\n" article))
88 (insert-file-contents file)
90 (if (search-forward "\n\n" nil t)
92 (goto-char (point-max))
94 (insert (format "Lines: %d\n" (count-lines (point) (point-max))))
96 (delete-region (point) (point-max))))
97 (setq sequence (cdr sequence))
98 (setq count (1+ count))
99 (and (numberp nnmail-large-newsgroup)
100 (> number nnmail-large-newsgroup)
102 (message "nnmh: Receiving headers... %d%%"
103 (/ (* count 100) number))))
105 (and (numberp nnmail-large-newsgroup)
106 (> number nnmail-large-newsgroup)
107 (message "nnmh: Receiving headers... done"))
109 ;; Fold continuation lines.
111 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
112 (replace-match " " t t))
115 (defun nnmh-open-server (server &optional defs)
116 (nnheader-init-server-buffer)
117 (if (equal server nnmh-current-server)
119 (if nnmh-current-server
120 (setq nnmh-server-alist
121 (cons (list nnmh-current-server
122 (nnheader-save-variables nnmh-server-variables))
124 (let ((state (assoc server nnmh-server-alist)))
127 (nnheader-restore-variables (nth 1 state))
128 (setq nnmh-server-alist (delq state nnmh-server-alist)))
129 (nnheader-set-init-variables nnmh-server-variables defs)))
130 (setq nnmh-current-server server)))
132 (defun nnmh-close-server (&optional server)
135 (defun nnmh-server-opened (&optional server)
136 (and (equal server nnmh-current-server)
138 (buffer-name nntp-server-buffer)))
140 (defun nnmh-status-message (&optional server)
143 (defun nnmh-request-article (id &optional newsgroup server buffer)
144 (nnmh-possibly-change-directory newsgroup)
145 (let ((file (if (stringp id)
147 (concat nnmh-current-directory (prin1-to-string id))))
148 (nntp-server-buffer (or buffer nntp-server-buffer)))
149 (if (and (stringp file)
151 (not (file-directory-p file)))
153 (nnmail-find-file file)))))
155 (defun nnmh-request-group (group &optional server dont-check)
156 (and nnmh-get-new-mail (or dont-check (nnmh-get-new-mail)))
157 (let ((pathname (nnmh-article-pathname group nnmh-directory))
159 (if (file-directory-p pathname)
161 (setq nnmh-current-directory pathname)
162 (and nnmh-get-new-mail (nnmh-update-gnus-unreads group))
170 (string-to-int name)))
171 (directory-files pathname nil "^[0-9]+$" t))
174 (set-buffer nntp-server-buffer)
177 (insert (format "211 %d %d %d %s\n" (length dir)
179 (progn (while (cdr dir)
180 (setq dir (cdr dir)))
183 (insert (format "211 0 1 0 %s\n" group))))))
186 (defun nnmh-request-list (&optional server dir)
187 (and server nnmh-get-new-mail (nnmh-get-new-mail))
190 (set-buffer nntp-server-buffer)
192 (setq dir nnmh-directory)))
193 (setq dir (expand-file-name dir))
194 ;; Recurse down all directories.
195 (let ((dirs (and (file-readable-p dir)
196 (directory-files dir t nil t))))
198 (if (and (not (string-match "/\\.\\.$" (car dirs)))
199 (not (string-match "/\\.$" (car dirs)))
200 (file-directory-p (car dirs))
201 (file-readable-p (car dirs)))
202 (nnmh-request-list server (car dirs)))
203 (setq dirs (cdr dirs))))
204 ;; For each directory, generate an active file line.
205 (if (not (string= (expand-file-name nnmh-directory) dir))
207 (lambda (name) (string-to-int name))
208 (directory-files dir nil "^[0-9]+$" t))))
210 (set-buffer nntp-server-buffer)
215 (string-match (expand-file-name nnmh-directory) dir)
216 (nnmail-replace-chars-in-string
217 (substring dir (match-end 0)) ?/ ?.))
218 (if files (apply (function max) files) 0)
219 (if files (apply (function min) files) 0))))))
222 (defun nnmh-request-newgroups (date &optional server)
223 (nnmh-request-list server))
225 (defun nnmh-request-post (&optional server)
226 (mail-send-and-exit nil))
228 (fset 'nnmh-request-post-buffer 'nnmail-request-post-buffer)
230 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
231 (nnmh-possibly-change-directory newsgroup)
232 (let* ((days (or (and nnmail-expiry-wait-function
233 (funcall nnmail-expiry-wait-function newsgroup))
235 article rest mod-time)
236 (if nnmail-keep-last-article
238 (setq articles (sort articles '>))
239 (setq rest (cons (car articles) rest))
240 (setq articles (cdr articles))))
242 (setq article (concat nnmh-current-directory (int-to-string
244 (if (setq mod-time (nth 5 (file-attributes article)))
246 (> (nnmail-days-between
247 (current-time-string)
248 (current-time-string mod-time))
251 (message "Deleting %s..." article)
253 (delete-file article)
255 (setq rest (cons (car articles) rest))))
256 (setq articles (cdr articles)))
259 (defun nnmh-close-group (group &optional server)
262 (defun nnmh-request-move-article
263 (article group server accept-form &optional last)
264 (let ((buf (get-buffer-create " *nnmh move*"))
267 (nnmh-request-article article group server)
270 (insert-buffer-substring nntp-server-buffer)
271 (setq result (eval accept-form))
272 (kill-buffer (current-buffer))
275 (delete-file (concat nnmh-current-directory
276 (int-to-string article)))
280 (defun nnmh-request-accept-article (group &optional last)
284 (setq nnmh-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 '(group "")))
288 (cons group (nnmh-save-mail))))
291 (setq nnmh-group-alist (nnmail-get-active))
294 (defun nnmh-request-replace-article (article group buffer)
295 (nnmh-possibly-change-directory group)
298 (nnmh-possibly-create-directory group)
301 (write-region (point-min) (point-max)
302 (concat nnmh-current-directory (int-to-string article))
303 nil (if gnus-verbose-backends nil 'nomesg))
308 ;;; Internal functions.
310 (defun nnmh-possibly-change-directory (newsgroup)
312 (let ((pathname (nnmh-article-pathname newsgroup nnmh-directory)))
313 (if (file-directory-p pathname)
314 (setq nnmh-current-directory pathname)
315 (error "No such newsgroup: %s" newsgroup)))))
317 (defun nnmh-possibly-create-directory (group)
319 (setq dir (nnmail-article-pathname group nnmh-directory))
320 (while (not (file-directory-p dir))
321 (setq dirs (cons dir dirs))
322 (setq dir (file-name-directory (directory-file-name dir))))
324 (if (make-directory (directory-file-name (car dirs)))
325 (error "Could not create directory %s" (car dirs)))
326 (and gnus-verbose-backends
327 (message "Creating mail directory %s" (car dirs)))
328 (setq dirs (cdr dirs)))))
330 (defun nnmh-save-mail ()
331 "Called narrowed to an article."
332 (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number)))
333 chars nov-line lines hbeg hend)
334 (setq chars (nnmail-insert-lines))
335 (nnmail-insert-xref group-art)
336 (goto-char (point-min))
337 (while (looking-at "From ")
338 (replace-match "X-From-Line: ")
340 ;; We save the article in all the newsgroups it belongs in.
344 (nnmh-possibly-create-directory (car (car ga)))
345 (let ((file (concat (nnmh-article-pathname
346 (car (car ga)) nnmh-directory)
347 (int-to-string (cdr (car ga))))))
349 ;; It was already saved, so we just make a hard link.
350 (add-name-to-file first file t)
352 (write-region (point-min) (point-max) file nil nil)
357 (defun nnmh-active-number (group)
358 "Compute the next article number in GROUP."
359 (let ((active (car (cdr (assoc group nnmh-group-alist)))))
360 (setcdr active (1+ (cdr active)))
362 (while (file-exists-p
363 (setq file (concat (nnmh-article-pathname
364 group nnmh-directory)
365 (int-to-string (cdr active)))))
366 (setcdr active (1+ (cdr active)))))
369 (defun nnmh-article-pathname (group mail-dir)
370 "Make pathname for GROUP."
371 (let ((mail-dir (file-name-as-directory (expand-file-name mail-dir))))
372 (if (file-directory-p (concat mail-dir group))
373 (concat mail-dir group "/")
374 (concat mail-dir (nnmail-replace-chars-in-string group ?. ?/) "/"))))
376 (defun nnmh-get-new-mail ()
377 "Read new incoming mail."
379 (if (and nnmh-get-new-mail nnmail-spool-file
380 (file-exists-p nnmail-spool-file)
381 (> (nth 7 (file-attributes nnmail-spool-file)) 0))
383 (message "nnmh: Reading incoming mail...")
385 (nnmail-move-inbox nnmail-spool-file
386 (concat nnmh-directory "Incoming")))
388 (setq nnmh-group-alist (nnmail-get-active))
389 (nnmail-split-incoming incoming 'nnmh-save-mail)
390 (run-hooks 'nnmail-read-incoming-hook)
391 ;; (delete-file incoming)
392 (message "nnmh: Reading incoming mail...done")))))
394 (defun nnmh-update-gnus-unreads (group)
395 ;; Go through the .nnmh-articles file and compare with the actual
396 ;; articles in this folder. The articles that are "new" will be
397 ;; marked as unread by Gnus.
398 (let* ((dir nnmh-current-directory)
399 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
400 (directory-files nnmh-current-directory
401 nil "^[0-9]+$" t)) '<))
402 (nnmh-file (concat dir ".nnmh-articles"))
404 ;; Load the .nnmh-articles file.
405 (if (file-exists-p nnmh-file)
407 (let (nnmh-newsgroup-articles)
408 (condition-case nil (load nnmh-file nil t t) (error nil))
409 nnmh-newsgroup-articles)))
410 ;; Add all new articles to the `new' list.
413 (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
414 (setq art (cdr art))))
415 ;; Remove all deleted articles.
416 (let ((art articles))
418 (if (not (memq (car (car art)) files))
419 (setq articles (delq (car art) articles)))
420 (setq art (cdr art))))
421 ;; Check whether the highest-numbered articles really are the ones
422 ;; that Gnus thinks they are by looking at the time-stamps.
423 (let ((art articles))
426 (nth 5 (file-attributes
427 (concat dir (int-to-string (car (car art))))))
429 (setq articles (delq (car art) articles))
430 (setq new (cons (car (car art)) new))
431 (setq art (cdr art))))
432 ;; Go through all the new articles and add them, and their
433 ;; time-stamps to the list.
439 (nth 5 (file-attributes
440 (concat dir (int-to-string (car n))))))
443 ;; Make Gnus mark all new articles as unread.
444 (or (zerop (length new))
445 (gnus-make-articles-unread
446 (gnus-group-prefixed-name group (list 'nnmh ""))
447 (setq new (sort new '<))))
448 ;; Sort the article list with highest numbers first.
449 (setq articles (sort articles (lambda (art1 art2)
450 (> (car art1) (car art2)))))
451 ;; Finally write this list back to the .nnmh-articles file.
453 (set-buffer (get-buffer-create "*nnmh out*"))
454 (insert ";; Gnus article active file for " group "\n\n")
455 (insert "(setq nnmh-newsgroup-articles '")
456 (insert (prin1-to-string articles) ")\n")
457 (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
458 (kill-buffer (current-buffer)))))
462 ;;; nnmh.el ends here