1 ;;; nnmh.el --- mhspool 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
37 (defvar nnmh-directory "~/Mail/"
38 "*Mail spool directory.")
40 (defvar nnmh-get-new-mail t
41 "*If non-nil, nnmh will check the incoming mail file and split the mail.")
43 (defvar nnmh-prepare-save-mail-hook nil
44 "*Hook run narrowed to an article before saving.")
46 (defvar nnmh-be-safe nil
47 "*If non-nil, nnmh will check all articles to make sure whether they are new or not.")
51 (defconst nnmh-version "nnmh 0.1"
54 (defvar nnmh-current-directory nil
55 "Current news group directory.")
57 (defvar nnmh-status-string "")
58 (defvar nnmh-group-alist nil)
62 (defvar nnmh-current-server nil)
63 (defvar nnmh-server-alist nil)
64 (defvar nnmh-server-variables
66 (list 'nnmh-directory nnmh-directory)
67 (list 'nnmh-get-new-mail nnmh-get-new-mail)
68 '(nnmh-current-directory nil)
69 '(nnmh-status-string "")
74 ;;; Interface functions.
76 (defun nnmh-retrieve-headers (sequence &optional newsgroup server)
78 (set-buffer nntp-server-buffer)
81 (number (length sequence))
82 (large (and (numberp nnmail-large-newsgroup)
83 (> number nnmail-large-newsgroup)))
86 (nnmh-possibly-change-directory newsgroup)
87 (if (stringp (car sequence))
90 (setq article (car sequence))
92 (concat nnmh-current-directory (int-to-string article)))
93 (if (and (file-exists-p file)
94 (not (file-directory-p file)))
96 (insert (format "221 %d Article retrieved.\n" article))
98 (nnheader-insert-head file)
100 (if (search-forward "\n\n" nil t)
102 (goto-char (point-max))
105 (delete-region (point) (point-max))))
106 (setq sequence (cdr sequence))
107 (setq count (1+ count))
111 (message "nnmh: Receiving headers... %d%%"
112 (/ (* count 100) number))))
114 (and large (message "nnmh: Receiving headers...done"))
116 ;; Fold continuation lines.
117 (goto-char (point-min))
118 (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
119 (replace-match " " t t))
122 (defun nnmh-open-server (server &optional defs)
123 (nnheader-init-server-buffer)
124 (if (equal server nnmh-current-server)
126 (if nnmh-current-server
127 (setq nnmh-server-alist
128 (cons (list nnmh-current-server
129 (nnheader-save-variables nnmh-server-variables))
131 (let ((state (assoc server nnmh-server-alist)))
134 (nnheader-restore-variables (nth 1 state))
135 (setq nnmh-server-alist (delq state nnmh-server-alist)))
136 (nnheader-set-init-variables nnmh-server-variables defs)))
137 (setq nnmh-current-server server)))
139 (defun nnmh-close-server (&optional server)
142 (defun nnmh-server-opened (&optional server)
143 (and (equal server nnmh-current-server)
145 (buffer-name nntp-server-buffer)))
147 (defun nnmh-status-message (&optional server)
150 (defun nnmh-request-article (id &optional newsgroup server buffer)
151 (nnmh-possibly-change-directory newsgroup)
152 (let ((file (if (stringp id)
154 (concat nnmh-current-directory (int-to-string id))))
155 (nntp-server-buffer (or buffer nntp-server-buffer)))
158 (not (file-directory-p file))
159 (save-excursion (nnmail-find-file file)))))
161 (defun nnmh-request-group (group &optional server dont-check)
162 (and nnmh-get-new-mail (or dont-check (nnmh-get-new-mail group)))
163 (let ((pathname (nnmh-article-pathname group nnmh-directory))
165 (if (file-directory-p pathname)
167 (setq nnmh-current-directory pathname)
168 (and nnmh-get-new-mail
170 (nnmh-update-gnus-unreads group))
178 (string-to-int name)))
179 (directory-files pathname nil "^[0-9]+$" t))
182 (set-buffer nntp-server-buffer)
185 (insert (format "211 %d %d %d %s\n" (length dir)
187 (progn (while (cdr dir)
188 (setq dir (cdr dir)))
191 (insert (format "211 0 1 0 %s\n" group))))))
194 (defun nnmh-request-list (&optional server dir)
197 (set-buffer nntp-server-buffer)
199 (setq dir nnmh-directory)))
200 (setq dir (expand-file-name dir))
201 ;; Recurse down all directories.
202 (let ((dirs (and (file-readable-p dir)
203 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
204 (directory-files dir t nil t))))
206 (if (and (not (string-match "/\\.\\.?$" (car dirs)))
207 (file-directory-p (car dirs))
208 (file-readable-p (car dirs)))
209 (nnmh-request-list nil (car dirs)))
210 (setq dirs (cdr dirs))))
211 ;; For each directory, generate an active file line.
212 (if (not (string= (expand-file-name nnmh-directory) dir))
214 (lambda (name) (string-to-int name))
215 (directory-files dir nil "^[0-9]+$" t))))
219 (set-buffer nntp-server-buffer)
220 (goto-char (point-max))
225 (string-match (expand-file-name nnmh-directory) dir)
226 (nnmail-replace-chars-in-string
227 (substring dir (match-end 0)) ?/ ?.))
228 (apply (function max) files)
229 (apply (function min) files)))))))
230 (setq nnmh-group-alist (nnmail-get-active))
231 (and server nnmh-get-new-mail (nnmh-get-new-mail))
234 (defun nnmh-request-newgroups (date &optional server)
235 (nnmh-request-list server))
237 (defun nnmh-request-post (&optional server)
238 (mail-send-and-exit nil))
240 (defalias 'nnmh-request-post-buffer 'nnmail-request-post-buffer)
242 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
243 (nnmh-possibly-change-directory newsgroup)
244 (let* ((days (or (and nnmail-expiry-wait-function
245 (funcall nnmail-expiry-wait-function newsgroup))
247 article rest mod-time)
248 (if nnmail-keep-last-article
250 (setq articles (sort articles '>))
251 (setq rest (cons (car articles) rest))
252 (setq articles (cdr articles))))
254 (setq article (concat nnmh-current-directory (int-to-string
256 (if (setq mod-time (nth 5 (file-attributes article)))
258 (> (nnmail-days-between
259 (current-time-string)
260 (current-time-string mod-time))
263 (message "Deleting %s..." article)
266 (delete-file article)
269 (setq rest (cons (car articles) rest))))
270 (setq articles (cdr articles)))
273 (defun nnmh-close-group (group &optional server)
276 (defun nnmh-request-move-article
277 (article group server accept-form &optional last)
278 (let ((buf (get-buffer-create " *nnmh move*"))
281 (nnmh-request-article article group server)
284 (insert-buffer-substring nntp-server-buffer)
285 (setq result (eval accept-form))
286 (kill-buffer (current-buffer))
289 (delete-file (concat nnmh-current-directory
290 (int-to-string article)))
294 (defun nnmh-request-accept-article (group &optional last)
298 (setq nnmh-group-alist (nnmail-get-active))
299 ;; We trick the choosing function into believing that only one
300 ;; group is availiable.
301 (let ((nnmail-split-methods (list (list group ""))))
302 (car (nnmh-save-mail))))
305 (setq nnmh-group-alist (nnmail-get-active))
306 (car (nnmh-save-mail)))))
308 (defun nnmh-request-replace-article (article group buffer)
309 (nnmh-possibly-change-directory group)
312 (nnmh-possibly-create-directory group)
315 (write-region (point-min) (point-max)
316 (concat nnmh-current-directory (int-to-string article))
317 nil (if gnus-verbose-backends nil 'nomesg))
322 ;;; Internal functions.
324 (defun nnmh-possibly-change-directory (newsgroup)
326 (let ((pathname (nnmh-article-pathname newsgroup nnmh-directory)))
327 (if (file-directory-p pathname)
328 (setq nnmh-current-directory pathname)
329 (error "No such newsgroup: %s" newsgroup)))))
331 (defun nnmh-possibly-create-directory (group)
333 (setq dir (nnmail-article-pathname group nnmh-directory))
334 (while (not (file-directory-p dir))
335 (setq dirs (cons dir dirs))
336 (setq dir (file-name-directory (directory-file-name dir))))
338 (if (make-directory (directory-file-name (car dirs)))
339 (error "Could not create directory %s" (car dirs)))
340 (and gnus-verbose-backends
341 (message "Creating mail directory %s" (car dirs)))
342 (setq dirs (cdr dirs)))))
344 (defun nnmh-save-mail ()
345 "Called narrowed to an article."
346 (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number)))
347 chars nov-line lines hbeg hend)
348 (setq chars (nnmail-insert-lines))
349 (nnmail-insert-xref group-art)
350 (run-hooks 'nnmh-prepare-save-mail-hook)
351 (goto-char (point-min))
352 (while (looking-at "From ")
353 (replace-match "X-From-Line: ")
355 ;; We save the article in all the newsgroups it belongs in.
359 (nnmh-possibly-create-directory (car (car ga)))
360 (let ((file (concat (nnmh-article-pathname
361 (car (car ga)) nnmh-directory)
362 (int-to-string (cdr (car ga))))))
364 ;; It was already saved, so we just make a hard link.
365 (add-name-to-file first file t)
367 (write-region (point-min) (point-max) file nil nil)
372 (defun nnmh-active-number (group)
373 "Compute the next article number in GROUP."
374 (let ((active (car (cdr (assoc group nnmh-group-alist)))))
375 (setcdr active (1+ (cdr active)))
377 (while (file-exists-p
378 (setq file (concat (nnmh-article-pathname
379 group nnmh-directory)
380 (int-to-string (cdr active)))))
381 (setcdr active (1+ (cdr active)))))
384 (defun nnmh-article-pathname (group mail-dir)
385 "Make pathname for GROUP."
386 (let ((mail-dir (file-name-as-directory (expand-file-name mail-dir))))
387 (if (file-directory-p (concat mail-dir group))
388 (concat mail-dir group "/")
389 (concat mail-dir (nnmail-replace-chars-in-string group ?. ?/) "/"))))
391 (defun nnmh-get-new-mail (&optional group)
392 "Read new incoming mail."
393 (let ((spools (nnmail-get-spool-files group))
395 (if (or (not nnmh-get-new-mail) (not nnmail-spool-file))
397 ;; We first activate all the groups.
400 ;; The we go through all the existing spool files and split the
404 (file-exists-p (car spools))
405 (> (nth 7 (file-attributes (car spools))) 0)
407 (and gnus-verbose-backends
408 (message "nnmh: Reading incoming mail..."))
411 (car spools) (concat nnmh-directory "Incoming")))
412 (setq incomings (cons incoming incomings))
413 (nnmail-split-incoming incoming 'nnmh-save-mail nil group)))
414 (setq spools (cdr spools)))
415 ;; If we did indeed read any incoming spools, we save all info.
417 (message "nnmh: Reading incoming mail...done"))
419 (and nnmail-delete-incoming
420 (file-writable-p incoming)
421 (delete-file incoming))
422 (setq incomings (cdr incomings))))))
425 (defun nnmh-update-gnus-unreads (group)
426 ;; Go through the .nnmh-articles file and compare with the actual
427 ;; articles in this folder. The articles that are "new" will be
428 ;; marked as unread by Gnus.
429 (let* ((dir nnmh-current-directory)
430 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
431 (directory-files nnmh-current-directory
432 nil "^[0-9]+$" t)) '<))
433 (nnmh-file (concat dir ".nnmh-articles"))
435 ;; Load the .nnmh-articles file.
436 (if (file-exists-p nnmh-file)
438 (let (nnmh-newsgroup-articles)
439 (condition-case nil (load nnmh-file nil t t) (error nil))
440 nnmh-newsgroup-articles)))
441 ;; Add all new articles to the `new' list.
444 (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
445 (setq art (cdr art))))
446 ;; Remove all deleted articles.
447 (let ((art articles))
449 (if (not (memq (car (car art)) files))
450 (setq articles (delq (car art) articles)))
451 (setq art (cdr art))))
452 ;; Check whether the highest-numbered articles really are the ones
453 ;; that Gnus thinks they are by looking at the time-stamps.
454 (let ((art articles))
457 (nth 5 (file-attributes
458 (concat dir (int-to-string (car (car art))))))
460 (setq articles (delq (car art) articles))
461 (setq new (cons (car (car art)) new))
462 (setq art (cdr art))))
463 ;; Go through all the new articles and add them, and their
464 ;; time-stamps to the list.
470 (nth 5 (file-attributes
471 (concat dir (int-to-string (car n))))))
474 ;; Make Gnus mark all new articles as unread.
475 (or (zerop (length new))
476 (gnus-make-articles-unread
477 (gnus-group-prefixed-name group (list 'nnmh ""))
478 (setq new (sort new '<))))
479 ;; Sort the article list with highest numbers first.
480 (setq articles (sort articles (lambda (art1 art2)
481 (> (car art1) (car art2)))))
482 ;; Finally write this list back to the .nnmh-articles file.
484 (set-buffer (get-buffer-create "*nnmh out*"))
485 (insert ";; Gnus article active file for " group "\n\n")
486 (insert "(setq nnmh-newsgroup-articles '")
487 (insert (prin1-to-string articles) ")\n")
488 (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
489 (kill-buffer (current-buffer)))))
493 ;;; nnmh.el ends here