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 1.0"
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 fetch-old)
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)
140 (setq nnmh-current-server nil)
143 (defun nnmh-server-opened (&optional server)
144 (and (equal server nnmh-current-server)
146 (buffer-name nntp-server-buffer)))
148 (defun nnmh-status-message (&optional server)
151 (defun nnmh-request-article (id &optional newsgroup server buffer)
152 (nnmh-possibly-change-directory newsgroup)
153 (let ((file (if (stringp id)
155 (concat nnmh-current-directory (int-to-string id))))
156 (nntp-server-buffer (or buffer nntp-server-buffer)))
159 (not (file-directory-p file))
160 (save-excursion (nnmail-find-file file))
161 (string-to-int (file-name-nondirectory file)))))
163 (defun nnmh-request-group (group &optional server dont-check)
164 (let ((pathname (nnmail-group-pathname group nnmh-directory))
166 (if (file-directory-p pathname)
168 (setq nnmh-current-directory pathname)
169 (and nnmh-get-new-mail
171 (nnmh-update-gnus-unreads group))
179 (string-to-int name)))
180 (directory-files pathname nil "^[0-9]+$" t))
183 (set-buffer nntp-server-buffer)
186 (insert (format "211 %d %d %d %s\n" (length dir)
188 (progn (while (cdr dir)
189 (setq dir (cdr dir)))
192 (insert (format "211 0 1 0 %s\n" group))))))
194 (setq nnmh-status-string "No such group")
197 (defun nnmh-request-scan (&optional group server)
198 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
200 (defun nnmh-request-list (&optional server dir)
203 (set-buffer nntp-server-buffer)
205 (setq dir (file-truename (file-name-as-directory nnmh-directory)))))
206 (setq dir (expand-file-name dir))
207 ;; Recurse down all directories.
208 (let ((dirs (and (file-readable-p dir)
209 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
210 (directory-files dir t nil t))))
212 (if (and (not (string-match "/\\.\\.?$" (car dirs)))
213 (file-directory-p (car dirs))
214 (file-readable-p (car dirs)))
215 (nnmh-request-list nil (car dirs)))
216 (setq dirs (cdr dirs))))
217 ;; For each directory, generate an active file line.
218 (if (not (string= (expand-file-name nnmh-directory) dir))
220 (lambda (name) (string-to-int name))
221 (directory-files dir nil "^[0-9]+$" t))))
225 (set-buffer nntp-server-buffer)
226 (goto-char (point-max))
232 (file-truename (file-name-as-directory
233 (expand-file-name nnmh-directory))) dir)
234 (nnmail-replace-chars-in-string
235 (substring dir (match-end 0)) ?/ ?.))
236 (apply (function max) files)
237 (apply (function min) files)))))))
238 (setq nnmh-group-alist (nnmail-get-active))
241 (defun nnmh-request-newgroups (date &optional server)
242 (nnmh-request-list server))
244 (defun nnmh-request-post (&optional server)
245 (mail-send-and-exit nil))
247 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
248 (nnmh-possibly-change-directory newsgroup)
249 (let* ((active-articles
253 (string-to-int name)))
254 (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
255 (max-article (and active-articles (apply 'max active-articles)))
257 article rest mod-time)
258 (nnmail-activate 'nnmh)
260 (while (and articles is-old)
261 (setq article (concat nnmh-current-directory
262 (int-to-string (car articles))))
263 (if (setq mod-time (nth 5 (file-attributes article)))
264 (if (and (nnmh-deletable-article-p newsgroup (car articles))
266 (nnmail-expired-article-p newsgroup mod-time force)))
268 (and gnus-verbose-backends
269 (message "Deleting article %s in %s..."
272 (funcall nnmail-delete-file-function article)
274 (setq rest (cons (car articles) rest)))))
275 (setq rest (cons (car articles) rest))))
276 (setq articles (cdr articles)))
278 (nconc rest articles)))
280 (defun nnmh-close-group (group &optional server)
283 (defun nnmh-request-move-article
284 (article group server accept-form &optional last)
285 (let ((buf (get-buffer-create " *nnmh move*"))
288 (nnmh-deletable-article-p group article)
289 (nnmh-request-article article group server)
292 (insert-buffer-substring nntp-server-buffer)
293 (setq result (eval accept-form))
294 (kill-buffer (current-buffer))
297 (funcall nnmail-delete-file-function
298 (concat nnmh-current-directory (int-to-string article)))
302 (defun nnmh-request-accept-article (group &optional last)
305 (nnmail-activate 'nnmh)
306 ;; We trick the choosing function into believing that only one
307 ;; group is availiable.
308 (let ((nnmail-split-methods (list (list group ""))))
309 (car (nnmh-save-mail))))
311 (nnmail-activate 'nnmh)
312 (car (nnmh-save-mail)))))
314 (defun nnmh-request-replace-article (article group buffer)
315 (nnmh-possibly-change-directory group)
318 (nnmh-possibly-create-directory group)
321 (write-region (point-min) (point-max)
322 (concat nnmh-current-directory (int-to-string article))
323 nil (if gnus-verbose-backends nil 'nomesg))
327 (defun nnmh-request-create-group (group &optional server)
328 (nnmail-activate 'nnmh)
329 (or (assoc group nnmh-group-alist)
331 (setq nnmh-group-alist (cons (list group (setq active (cons 1 0)))
333 (nnmh-possibly-create-directory group)
334 (nnmh-possibly-change-directory group)
335 (let ((articles (mapcar
337 (string-to-int file))
339 nnmh-current-directory nil "^[0-9]+$"))))
342 (setcar active (apply 'min articles))
343 (setcdr active (apply 'max articles)))))))
346 (defun nnmh-request-delete-group (group &optional force server)
347 (nnmh-possibly-change-directory group)
348 ;; Delete all articles in GROUP.
350 () ; Don't delete the articles.
351 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
353 (and (file-writable-p (car articles))
355 (and gnus-verbose-backends
356 (message (message "Deleting article %s in %s..."
357 (car articles) group)))
358 (funcall nnmail-delete-file-function (car articles))))
359 (setq articles (cdr articles))))
360 ;; Try to delete the directory itself.
362 (delete-directory nnmh-current-directory)
364 ;; Remove the group from all structures.
365 (setq nnmh-group-alist
366 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
367 nnmh-current-directory nil)
370 (defun nnmh-request-rename-group (group new-name &optional server)
371 (nnmh-possibly-change-directory group)
373 (and (file-writable-p nnmh-current-directory)
377 (directory-file-name nnmh-current-directory)
379 (nnmail-group-pathname new-name nnmh-directory)))
382 ;; That went ok, so we change the internal structures.
383 (let ((entry (assoc group nnmh-group-alist)))
384 (and entry (setcar entry new-name))
385 (setq nnmh-current-directory nil)
389 ;;; Internal functions.
391 (defun nnmh-possibly-change-directory (newsgroup)
393 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
394 (if (file-directory-p pathname)
395 (setq nnmh-current-directory pathname)
396 (error "No such newsgroup: %s" newsgroup)))))
398 (defun nnmh-possibly-create-directory (group)
400 (setq dir (nnmail-group-pathname group nnmh-directory))
401 (while (not (file-directory-p dir))
402 (setq dirs (cons dir dirs))
403 (setq dir (file-name-directory (directory-file-name dir))))
405 (if (make-directory (directory-file-name (car dirs)))
406 (error "Could not create directory %s" (car dirs)))
407 (and gnus-verbose-backends
408 (message "Creating mail directory %s" (car dirs)))
409 (setq dirs (cdr dirs)))))
411 (defun nnmh-save-mail ()
412 "Called narrowed to an article."
413 (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
414 (nnmail-insert-lines)
415 (nnmail-insert-xref group-art)
416 (run-hooks 'nnmh-prepare-save-mail-hook)
417 (goto-char (point-min))
418 (while (looking-at "From ")
419 (replace-match "X-From-Line: ")
421 ;; We save the article in all the newsgroups it belongs in.
425 (nnmh-possibly-create-directory (car (car ga)))
426 (let ((file (concat (nnmail-group-pathname
427 (car (car ga)) nnmh-directory)
428 (int-to-string (cdr (car ga))))))
430 ;; It was already saved, so we just make a hard link.
431 (add-name-to-file first file t)
433 (write-region (point-min) (point-max) file nil nil)
438 (defun nnmh-active-number (group)
439 "Compute the next article number in GROUP."
440 (let ((active (car (cdr (assoc group nnmh-group-alist)))))
441 ;; The group wasn't known to nnmh, so we just create an active
445 (setq active (cons 1 0))
446 (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
447 (setcdr active (1+ (cdr active)))
448 (while (file-exists-p
449 (concat (nnmail-group-pathname group nnmh-directory)
450 (int-to-string (cdr active))))
451 (setcdr active (1+ (cdr active))))
454 (defun nnmh-update-gnus-unreads (group)
455 ;; Go through the .nnmh-articles file and compare with the actual
456 ;; articles in this folder. The articles that are "new" will be
457 ;; marked as unread by Gnus.
458 (let* ((dir nnmh-current-directory)
459 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
460 (directory-files nnmh-current-directory
461 nil "^[0-9]+$" t)) '<))
462 (nnmh-file (concat dir ".nnmh-articles"))
464 ;; Load the .nnmh-articles file.
465 (if (file-exists-p nnmh-file)
467 (let (nnmh-newsgroup-articles)
468 (condition-case nil (load nnmh-file nil t t) (error nil))
469 nnmh-newsgroup-articles)))
470 ;; Add all new articles to the `new' list.
473 (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
474 (setq art (cdr art))))
475 ;; Remove all deleted articles.
476 (let ((art articles))
478 (if (not (memq (car (car art)) files))
479 (setq articles (delq (car art) articles)))
480 (setq art (cdr art))))
481 ;; Check whether the highest-numbered articles really are the ones
482 ;; that Gnus thinks they are by looking at the time-stamps.
483 (let ((art articles))
486 (nth 5 (file-attributes
487 (concat dir (int-to-string (car (car art))))))
489 (setq articles (delq (car art) articles))
490 (setq new (cons (car (car art)) new))
491 (setq art (cdr art))))
492 ;; Go through all the new articles and add them, and their
493 ;; time-stamps to the list.
499 (nth 5 (file-attributes
500 (concat dir (int-to-string (car n))))))
503 ;; Make Gnus mark all new articles as unread.
504 (or (zerop (length new))
505 (gnus-make-articles-unread
506 (gnus-group-prefixed-name group (list 'nnmh ""))
507 (setq new (sort new '<))))
508 ;; Sort the article list with highest numbers first.
509 (setq articles (sort articles (lambda (art1 art2)
510 (> (car art1) (car art2)))))
511 ;; Finally write this list back to the .nnmh-articles file.
513 (set-buffer (get-buffer-create "*nnmh out*"))
514 (insert ";; Gnus article active file for " group "\n\n")
515 (insert "(setq nnmh-newsgroup-articles '")
516 (insert (prin1-to-string articles) ")\n")
517 (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
518 (kill-buffer (current-buffer)))))
520 (defun nnmh-deletable-article-p (group article)
521 "Say whether ARTICLE in GROUP can be deleted."
522 (or (not nnmail-keep-last-article)
523 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist))) article))))
527 ;;; nnmh.el ends here