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