]> code.delx.au - gnu-emacs/blob - lisp/gnus/nnmh.el
(comint-postoutput-scroll-to-bottom): Cope with unset
[gnu-emacs] / lisp / gnus / nnmh.el
1 ;;; nnmh.el --- mhspool access for Gnus
2 ;; Copyright (C) 1995,96,97 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news, mail
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
28 ;; For an overview of what the interface functions do, please see the
29 ;; Gnus sources.
30
31 ;;; Code:
32
33 (require 'nnheader)
34 (require 'nnmail)
35 (require 'gnus-start)
36 (require 'nnoo)
37 (eval-when-compile (require 'cl))
38
39 (nnoo-declare nnmh)
40
41 (defvoo nnmh-directory message-directory
42 "*Mail spool directory.")
43
44 (defvoo nnmh-get-new-mail t
45 "*If non-nil, nnmh will check the incoming mail file and split the mail.")
46
47 (defvoo nnmh-prepare-save-mail-hook nil
48 "*Hook run narrowed to an article before saving.")
49
50 (defvoo nnmh-be-safe nil
51 "*If non-nil, nnmh will check all articles to make sure whether they are new or not.")
52
53 \f
54
55 (defconst nnmh-version "nnmh 1.0"
56 "nnmh version.")
57
58 (defvoo nnmh-current-directory nil
59 "Current news group directory.")
60
61 (defvoo nnmh-status-string "")
62 (defvoo nnmh-group-alist nil)
63
64 \f
65
66 ;;; Interface functions.
67
68 (nnoo-define-basics nnmh)
69
70 (deffoo nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
71 (save-excursion
72 (set-buffer nntp-server-buffer)
73 (erase-buffer)
74 (let* ((file nil)
75 (number (length articles))
76 (large (and (numberp nnmail-large-newsgroup)
77 (> number nnmail-large-newsgroup)))
78 (count 0)
79 ;; 1997/8/12 by MORIOKA Tomohiko
80 (file-name-coding-system 'binary) ; for Emacs 20
81 (pathname-coding-system 'binary) ; for XEmacs/mule
82 beg article)
83 (nnmh-possibly-change-directory newsgroup server)
84 ;; We don't support fetching by Message-ID.
85 (if (stringp (car articles))
86 'headers
87 (while articles
88 (when (and (file-exists-p
89 (setq file (concat (file-name-as-directory
90 nnmh-current-directory)
91 (int-to-string
92 (setq article (pop articles))))))
93 (not (file-directory-p file)))
94 (insert (format "221 %d Article retrieved.\n" article))
95 (setq beg (point))
96 (nnheader-insert-head file)
97 (goto-char beg)
98 (if (search-forward "\n\n" nil t)
99 (forward-char -1)
100 (goto-char (point-max))
101 (insert "\n\n"))
102 (insert ".\n")
103 (delete-region (point) (point-max)))
104 (setq count (1+ count))
105
106 (and large
107 (zerop (% count 20))
108 (message "nnmh: Receiving headers... %d%%"
109 (/ (* count 100) number))))
110
111 (when large
112 (message "nnmh: Receiving headers...done"))
113
114 (nnheader-fold-continuation-lines)
115 'headers))))
116
117 (deffoo nnmh-open-server (server &optional defs)
118 (nnoo-change-server 'nnmh server defs)
119 (when (not (file-exists-p nnmh-directory))
120 (condition-case ()
121 (make-directory nnmh-directory t)
122 (error t)))
123 (cond
124 ((not (file-exists-p nnmh-directory))
125 (nnmh-close-server)
126 (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
127 ((not (file-directory-p (file-truename nnmh-directory)))
128 (nnmh-close-server)
129 (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
130 (t
131 (nnheader-report 'nnmh "Opened server %s using directory %s"
132 server nnmh-directory)
133 t)))
134
135 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
136 (nnmh-possibly-change-directory newsgroup server)
137 (let ((file (if (stringp id)
138 nil
139 (concat nnmh-current-directory (int-to-string id))))
140 ;; 1997/8/12 by MORIOKA Tomohiko
141 (file-name-coding-system 'binary) ; for Emacs 20
142 (pathname-coding-system 'binary) ; for XEmacs/mule
143 (nntp-server-buffer (or buffer nntp-server-buffer)))
144 (and (stringp file)
145 (file-exists-p file)
146 (not (file-directory-p file))
147 (save-excursion (nnmail-find-file file))
148 (string-to-int (file-name-nondirectory file)))))
149
150 (deffoo nnmh-request-group (group &optional server dont-check)
151 (let ((pathname (nnmail-group-pathname group nnmh-directory))
152 ;; 1997/8/12 by MORIOKA Tomohiko
153 (file-name-coding-system 'binary) ; for Emacs 20
154 (pathname-coding-system 'binary) ; for XEmacs/mule.
155 dir)
156 (cond
157 ((not (file-directory-p pathname))
158 (nnheader-report
159 'nnmh "Can't select group (no such directory): %s" group))
160 (t
161 (setq nnmh-current-directory pathname)
162 (and nnmh-get-new-mail
163 nnmh-be-safe
164 (nnmh-update-gnus-unreads group))
165 (cond
166 (dont-check
167 (nnheader-report 'nnmh "Selected group %s" group)
168 t)
169 (t
170 ;; Re-scan the directory if it's on a foreign system.
171 (nnheader-re-read-dir pathname)
172 (setq dir
173 (sort
174 (mapcar (lambda (name) (string-to-int name))
175 (directory-files pathname nil "^[0-9]+$" t))
176 '<))
177 (cond
178 (dir
179 (nnheader-report 'nnmh "Selected group %s" group)
180 (nnheader-insert
181 "211 %d %d %d %s\n" (length dir) (car dir)
182 (progn (while (cdr dir) (setq dir (cdr dir))) (car dir))
183 group))
184 (t
185 (nnheader-report 'nnmh "Empty group %s" group)
186 (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
187
188 (deffoo nnmh-request-scan (&optional group server)
189 (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
190
191 (deffoo nnmh-request-list (&optional server dir)
192 (nnheader-insert "")
193 (let ((file-name-coding-system 'binary)
194 (pathname-coding-system 'binary)
195 (nnmh-toplev
196 (file-truename (or dir (file-name-as-directory nnmh-directory)))))
197 (nnmh-request-list-1 nnmh-toplev))
198 (setq nnmh-group-alist (nnmail-get-active))
199 t)
200
201 (defvar nnmh-toplev)
202 (defun nnmh-request-list-1 (dir)
203 (setq dir (expand-file-name dir))
204 ;; Recurse down all directories.
205 (let ((dirs (and (file-readable-p dir)
206 (> (nth 1 (file-attributes (file-chase-links dir))) 2)
207 (directory-files dir t nil t)))
208 dir)
209 ;; Recurse down directories.
210 (while (setq dir (pop dirs))
211 (when (and (not (member (file-name-nondirectory dir) '("." "..")))
212 (file-directory-p dir)
213 (file-readable-p dir))
214 (nnmh-request-list-1 dir))))
215 ;; For each directory, generate an active file line.
216 (unless (string= (expand-file-name nnmh-toplev) dir)
217 (let ((files (mapcar
218 (lambda (name) (string-to-int name))
219 (directory-files dir nil "^[0-9]+$" t))))
220 (when files
221 (save-excursion
222 (set-buffer nntp-server-buffer)
223 (goto-char (point-max))
224 (insert
225 (format
226 "%s %d %d y\n"
227 (progn
228 (string-match
229 (regexp-quote
230 (file-truename (file-name-as-directory
231 (expand-file-name nnmh-toplev))))
232 dir)
233 (nnheader-replace-chars-in-string
234 (decode-coding-string (substring dir (match-end 0))
235 nnmail-pathname-coding-system)
236 ?/ ?.))
237 (apply 'max files)
238 (apply 'min files)))))))
239 t)
240
241 (deffoo nnmh-request-newgroups (date &optional server)
242 (nnmh-request-list server))
243
244 (deffoo nnmh-request-expire-articles (articles newsgroup
245 &optional server force)
246 (nnmh-possibly-change-directory newsgroup server)
247 (let* ((active-articles
248 (mapcar
249 (function
250 (lambda (name)
251 (string-to-int name)))
252 (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
253 (is-old t)
254 article rest mod-time)
255 (nnmail-activate 'nnmh)
256
257 (while (and articles is-old)
258 (setq article (concat nnmh-current-directory
259 (int-to-string (car articles))))
260 (when (setq mod-time (nth 5 (file-attributes article)))
261 (if (and (nnmh-deletable-article-p newsgroup (car articles))
262 (setq is-old
263 (nnmail-expired-article-p newsgroup mod-time force)))
264 (progn
265 (nnheader-message 5 "Deleting article %s in %s..."
266 article newsgroup)
267 (condition-case ()
268 (funcall nnmail-delete-file-function article)
269 (file-error
270 (nnheader-message 1 "Couldn't delete article %s in %s"
271 article newsgroup)
272 (push (car articles) rest))))
273 (push (car articles) rest)))
274 (setq articles (cdr articles)))
275 (message "")
276 (nconc rest articles)))
277
278 (deffoo nnmh-close-group (group &optional server)
279 t)
280
281 (deffoo nnmh-request-move-article
282 (article group server accept-form &optional last)
283 (let ((buf (get-buffer-create " *nnmh move*"))
284 result)
285 (and
286 (nnmh-deletable-article-p group article)
287 (nnmh-request-article article group server)
288 (save-excursion
289 (set-buffer buf)
290 (erase-buffer)
291 (insert-buffer-substring nntp-server-buffer)
292 (setq result (eval accept-form))
293 (kill-buffer (current-buffer))
294 result)
295 (progn
296 (nnmh-possibly-change-directory group server)
297 (condition-case ()
298 (funcall nnmail-delete-file-function
299 (concat nnmh-current-directory (int-to-string article)))
300 (file-error nil))))
301 result))
302
303 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
304 (nnmh-possibly-change-directory group server)
305 (nnmail-check-syntax)
306 (when nnmail-cache-accepted-message-ids
307 (nnmail-cache-insert (nnmail-fetch-field "message-id")))
308 (prog1
309 (if (stringp group)
310 (and
311 (nnmail-activate 'nnmh)
312 (car (nnmh-save-mail
313 (list (cons group (nnmh-active-number group)))
314 noinsert)))
315 (and
316 (nnmail-activate 'nnmh)
317 (let ((res (nnmail-article-group 'nnmh-active-number)))
318 (if (and (null res)
319 (yes-or-no-p "Moved to `junk' group; delete article? "))
320 'junk
321 (car (nnmh-save-mail res noinsert))))))
322 (when (and last nnmail-cache-accepted-message-ids)
323 (nnmail-cache-close))))
324
325 (deffoo nnmh-request-replace-article (article group buffer)
326 (nnmh-possibly-change-directory group)
327 (save-excursion
328 (set-buffer buffer)
329 (nnmh-possibly-create-directory group)
330 (ignore-errors
331 (nnmail-write-region
332 (point-min) (point-max)
333 (concat nnmh-current-directory (int-to-string article))
334 nil (if (nnheader-be-verbose 5) nil 'nomesg))
335 t)))
336
337 (deffoo nnmh-request-create-group (group &optional server args)
338 (nnmail-activate 'nnmh)
339 (unless (assoc group nnmh-group-alist)
340 (let (active)
341 (push (list group (setq active (cons 1 0)))
342 nnmh-group-alist)
343 (nnmh-possibly-create-directory group)
344 (nnmh-possibly-change-directory group server)
345 (let ((articles (mapcar
346 (lambda (file)
347 (string-to-int file))
348 (directory-files
349 nnmh-current-directory nil "^[0-9]+$"))))
350 (when articles
351 (setcar active (apply 'min articles))
352 (setcdr active (apply 'max articles))))))
353 t)
354
355 (deffoo nnmh-request-delete-group (group &optional force server)
356 (nnmh-possibly-change-directory group server)
357 ;; Delete all articles in GROUP.
358 (if (not force)
359 () ; Don't delete the articles.
360 (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
361 (while articles
362 (when (file-writable-p (car articles))
363 (nnheader-message 5 "Deleting article %s in %s..."
364 (car articles) group)
365 (funcall nnmail-delete-file-function (car articles)))
366 (setq articles (cdr articles))))
367 ;; Try to delete the directory itself.
368 (ignore-errors
369 (delete-directory nnmh-current-directory)))
370 ;; Remove the group from all structures.
371 (setq nnmh-group-alist
372 (delq (assoc group nnmh-group-alist) nnmh-group-alist)
373 nnmh-current-directory nil)
374 t)
375
376 (deffoo nnmh-request-rename-group (group new-name &optional server)
377 (nnmh-possibly-change-directory group server)
378 (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
379 (old-dir (nnmail-group-pathname group nnmh-directory)))
380 (when (ignore-errors
381 (make-directory new-dir t)
382 t)
383 ;; We move the articles file by file instead of renaming
384 ;; the directory -- there may be subgroups in this group.
385 ;; One might be more clever, I guess.
386 (let ((files (nnheader-article-to-file-alist old-dir)))
387 (while files
388 (rename-file
389 (concat old-dir (cdar files))
390 (concat new-dir (cdar files)))
391 (pop files)))
392 (when (<= (length (directory-files old-dir)) 2)
393 (ignore-errors
394 (delete-directory old-dir)))
395 ;; That went ok, so we change the internal structures.
396 (let ((entry (assoc group nnmh-group-alist)))
397 (when entry
398 (setcar entry new-name))
399 (setq nnmh-current-directory nil)
400 t))))
401
402 (nnoo-define-skeleton nnmh)
403
404 \f
405 ;;; Internal functions.
406
407 (defun nnmh-possibly-change-directory (newsgroup &optional server)
408 (when (and server
409 (not (nnmh-server-opened server)))
410 (nnmh-open-server server))
411 (when newsgroup
412 (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
413 ;; 1997/8/12 by MORIOKA Tomohiko
414 (file-name-coding-system 'binary) ; for Emacs 20
415 (pathname-coding-system 'binary)) ; for XEmacs/mule
416 (if (file-directory-p pathname)
417 (setq nnmh-current-directory pathname)
418 (error "No such newsgroup: %s" newsgroup)))))
419
420 (defun nnmh-possibly-create-directory (group)
421 (let (dir dirs)
422 (setq dir (nnmail-group-pathname group nnmh-directory))
423 (while (not (file-directory-p dir))
424 (push dir dirs)
425 (setq dir (file-name-directory (directory-file-name dir))))
426 (while dirs
427 (when (make-directory (directory-file-name (car dirs)))
428 (error "Could not create directory %s" (car dirs)))
429 (nnheader-message 5 "Creating mail directory %s" (car dirs))
430 (setq dirs (cdr dirs)))))
431
432 (defun nnmh-save-mail (group-art &optional noinsert)
433 "Called narrowed to an article."
434 (unless noinsert
435 (nnmail-insert-lines)
436 (nnmail-insert-xref group-art))
437 (run-hooks 'nnmail-prepare-save-mail-hook)
438 (run-hooks 'nnmh-prepare-save-mail-hook)
439 (goto-char (point-min))
440 (while (looking-at "From ")
441 (replace-match "X-From-Line: ")
442 (forward-line 1))
443 ;; We save the article in all the newsgroups it belongs in.
444 (let ((ga group-art)
445 first)
446 (while ga
447 (nnmh-possibly-create-directory (caar ga))
448 (let ((file (concat (nnmail-group-pathname
449 (caar ga) nnmh-directory)
450 (int-to-string (cdar ga)))))
451 (if first
452 ;; It was already saved, so we just make a hard link.
453 (funcall nnmail-crosspost-link-function first file t)
454 ;; Save the article.
455 (nnmail-write-region (point-min) (point-max) file nil nil)
456 (setq first file)))
457 (setq ga (cdr ga))))
458 group-art)
459
460 (defun nnmh-active-number (group)
461 "Compute the next article number in GROUP."
462 (let ((active (cadr (assoc group nnmh-group-alist)))
463 (dir (nnmail-group-pathname group nnmh-directory))
464 ;; 1997/8/14 by MORIOKA Tomohiko
465 (file-name-coding-system 'binary) ; for Emacs 20
466 (pathname-coding-system 'binary)) ; for XEmacs/mule
467 (unless active
468 ;; The group wasn't known to nnmh, so we just create an active
469 ;; entry for it.
470 (setq active (cons 1 0))
471 (push (list group active) nnmh-group-alist)
472 (unless (file-exists-p dir)
473 (make-directory dir))
474 ;; Find the highest number in the group.
475 (let ((files (sort
476 (mapcar
477 (lambda (f)
478 (string-to-int f))
479 (directory-files dir nil "^[0-9]+$"))
480 '>)))
481 (when files
482 (setcdr active (car files)))))
483 (setcdr active (1+ (cdr active)))
484 (while (file-exists-p
485 (concat (nnmail-group-pathname group nnmh-directory)
486 (int-to-string (cdr active))))
487 (setcdr active (1+ (cdr active))))
488 (cdr active)))
489
490 (defun nnmh-update-gnus-unreads (group)
491 ;; Go through the .nnmh-articles file and compare with the actual
492 ;; articles in this folder. The articles that are "new" will be
493 ;; marked as unread by Gnus.
494 (let* ((dir nnmh-current-directory)
495 (files (sort (mapcar (function (lambda (name) (string-to-int name)))
496 (directory-files nnmh-current-directory
497 nil "^[0-9]+$" t))
498 '<))
499 (nnmh-file (concat dir ".nnmh-articles"))
500 new articles)
501 ;; Load the .nnmh-articles file.
502 (when (file-exists-p nnmh-file)
503 (setq articles
504 (let (nnmh-newsgroup-articles)
505 (ignore-errors (load nnmh-file nil t t))
506 nnmh-newsgroup-articles)))
507 ;; Add all new articles to the `new' list.
508 (let ((art files))
509 (while art
510 (unless (assq (car art) articles)
511 (push (car art) new))
512 (setq art (cdr art))))
513 ;; Remove all deleted articles.
514 (let ((art articles))
515 (while art
516 (unless (memq (caar art) files)
517 (setq articles (delq (car art) articles)))
518 (setq art (cdr art))))
519 ;; Check whether the articles really are the ones that Gnus thinks
520 ;; they are by looking at the time-stamps.
521 (let ((arts articles)
522 art)
523 (while (setq art (pop arts))
524 (when (not (equal
525 (nth 5 (file-attributes
526 (concat dir (int-to-string (car art)))))
527 (cdr art)))
528 (setq articles (delq art articles))
529 (push (car art) new))))
530 ;; Go through all the new articles and add them, and their
531 ;; time-stamps, to the list.
532 (setq articles
533 (nconc articles
534 (mapcar
535 (lambda (art)
536 (cons art
537 (nth 5 (file-attributes
538 (concat dir (int-to-string art))))))
539 new)))
540 ;; Make Gnus mark all new articles as unread.
541 (when new
542 (gnus-make-articles-unread
543 (gnus-group-prefixed-name group (list 'nnmh ""))
544 (setq new (sort new '<))))
545 ;; Sort the article list with highest numbers first.
546 (setq articles (sort articles (lambda (art1 art2)
547 (> (car art1) (car art2)))))
548 ;; Finally write this list back to the .nnmh-articles file.
549 (nnheader-temp-write nnmh-file
550 (insert ";; Gnus article active file for " group "\n\n")
551 (insert "(setq nnmh-newsgroup-articles '")
552 (gnus-prin1 articles)
553 (insert ")\n"))))
554
555 (defun nnmh-deletable-article-p (group article)
556 "Say whether ARTICLE in GROUP can be deleted."
557 (let ((path (concat nnmh-current-directory (int-to-string article))))
558 ;; Writable.
559 (and (file-writable-p path)
560 ;; We can never delete the last article in the group.
561 (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
562 article)))))
563
564 (provide 'nnmh)
565
566 ;;; nnmh.el ends here