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