Merge from emacs--devo--0, emacs--rel--22
[gnus] / lisp / nnmh.el
1 ;;; nnmh.el --- mhspool access for Gnus
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;;   2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Keywords: news, mail
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27 ;;; Commentary:
28
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.
32
33 ;;; Code:
34
35 (require 'nnheader)
36 (require 'nnmail)
37 (require 'gnus-start)
38 (require 'nnoo)
39 (eval-when-compile (require 'cl))
40
41 (nnoo-declare nnmh)
42
43 (defvoo nnmh-directory message-directory
44   "Mail spool directory.")
45
46 (defvoo nnmh-get-new-mail t
47   "If non-nil, nnmh will check the incoming mail file and split the mail.")
48
49 (defvoo nnmh-prepare-save-mail-hook nil
50   "Hook run narrowed to an article before saving.")
51
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.")
57
58 \f
59
60 (defconst nnmh-version "nnmh 1.0"
61   "nnmh version.")
62
63 (defvoo nnmh-current-directory nil
64   "Current news group directory.")
65
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)
72
73 \f
74
75 ;;; Interface functions.
76
77 (nnoo-define-basics nnmh)
78
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))
111
112           (and large
113                (zerop (% count 20))
114                (nnheader-message 5 "nnmh: Receiving headers... %d%%"
115                                  (/ (* count 100) number))))
116
117         (when large
118           (nnheader-message 5 "nnmh: Receiving headers...done"))
119
120         (nnheader-fold-continuation-lines)
121         'headers))))
122
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))
136    (t
137     (nnheader-report 'nnmh "Opened server %s using directory %s"
138                      server nnmh-directory)
139     t)))
140
141 (deffoo nnmh-request-article (id &optional newsgroup server buffer)
142   (nnmh-possibly-change-directory newsgroup server)
143   (let ((file (if (stringp id)
144                   nil
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)))))
153
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))
164      (t
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)
172         t)
173        (t
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))
191          (t
192           (nnheader-report 'nnmh "Empty group %s" group)
193           (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
194
195 (deffoo nnmh-request-scan (&optional group server)
196   (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
197
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))
206   t)
207
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)))))))
246   t)
247
248 (deffoo nnmh-request-newgroups (date &optional server)
249   (nnmh-request-list server))
250
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         (nnmail-expiry-target
256          (or (gnus-group-find-parameter newsgroup 'expiry-target t)
257              nnmail-expiry-target))
258         article rest mod-time)
259     (nnheader-init-server-buffer)
260
261     (while (and articles is-old)
262       (setq article (concat nnmh-current-directory
263                             (int-to-string (car articles))))
264       (when (setq mod-time (nth 5 (file-attributes article)))
265         (if (and (nnmh-deletable-article-p newsgroup (car articles))
266                  (setq is-old
267                        (nnmail-expired-article-p newsgroup mod-time force)))
268             (progn
269               ;; Allow a special target group. -- jcn
270               (unless (eq nnmail-expiry-target 'delete)
271                 (with-temp-buffer
272                   (nnmh-request-article (car articles)
273                                         newsgroup server (current-buffer))
274                   (nnmail-expiry-target-group
275                    nnmail-expiry-target newsgroup)))
276               (nnheader-message 5 "Deleting article %s in %s..."
277                                 article newsgroup)
278               (condition-case ()
279                   (funcall nnmail-delete-file-function article)
280                 (file-error
281                  (nnheader-message 1 "Couldn't delete article %s in %s"
282                                    article newsgroup)
283                  (push (car articles) rest))))
284           (push (car articles) rest)))
285       (setq articles (cdr articles)))
286     (nnheader-message 5 "")
287     (nconc rest articles)))
288
289 (deffoo nnmh-close-group (group &optional server)
290   t)
291
292 (deffoo nnmh-request-move-article (article group server accept-form 
293                                            &optional last move-is-internal)
294   (let ((buf (get-buffer-create " *nnmh move*"))
295         result)
296     (and
297      (nnmh-deletable-article-p group article)
298      (nnmh-request-article article group server)
299      (with-current-buffer buf
300        (erase-buffer)
301        (insert-buffer-substring nntp-server-buffer)
302        (setq result (eval accept-form))
303        (kill-buffer (current-buffer))
304        result)
305      (progn
306        (nnmh-possibly-change-directory group server)
307        (condition-case ()
308            (funcall nnmail-delete-file-function
309                     (concat nnmh-current-directory (int-to-string article)))
310          (file-error nil))))
311     result))
312
313 (deffoo nnmh-request-accept-article (group &optional server last noinsert)
314   (nnmh-possibly-change-directory group server)
315   (nnmail-check-syntax)
316   (when nnmail-cache-accepted-message-ids
317     (nnmail-cache-insert (nnmail-fetch-field "message-id") 
318                          group
319                          (nnmail-fetch-field "subject")
320                          (nnmail-fetch-field "from")))
321   (nnheader-init-server-buffer)
322   (prog1
323       (if (stringp group)
324           (if noinsert
325               (nnmh-active-number group)
326             (car (nnmh-save-mail
327                   (list (cons group (nnmh-active-number group)))
328                   noinsert)))
329         (let ((res (nnmail-article-group 'nnmh-active-number)))
330           (if (and (null res)
331                    (yes-or-no-p "Moved to `junk' group; delete article? "))
332               'junk
333             (car (nnmh-save-mail res noinsert)))))
334     (when (and last nnmail-cache-accepted-message-ids)
335       (nnmail-cache-close))))
336
337 (deffoo nnmh-request-replace-article (article group buffer)
338   (nnmh-possibly-change-directory group)
339   (with-current-buffer buffer
340     (nnmh-possibly-create-directory group)
341     (ignore-errors
342       (nnmail-write-region
343        (point-min) (point-max)
344        (concat nnmh-current-directory (int-to-string article))
345        nil (if (nnheader-be-verbose 5) nil 'nomesg))
346       t)))
347
348 (deffoo nnmh-request-create-group (group &optional server args)
349   (nnheader-init-server-buffer)
350   (unless (assoc group nnmh-group-alist)
351     (let (active)
352       (push (list group (setq active (cons 1 0)))
353             nnmh-group-alist)
354       (nnmh-possibly-create-directory group)
355       (nnmh-possibly-change-directory group server)
356       (let ((articles (mapcar 'string-to-number
357                               (directory-files
358                                nnmh-current-directory nil "^[0-9]+$"))))
359         (when articles
360           (setcar active (apply 'min articles))
361           (setcdr active (apply 'max articles))))))
362   t)
363
364 (deffoo nnmh-request-delete-group (group &optional force server)
365   (nnmh-possibly-change-directory group server)
366   ;; Delete all articles in GROUP.
367   (if (not force)
368       ()                                ; Don't delete the articles.
369     (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
370       (while articles
371         (when (file-writable-p (car articles))
372           (nnheader-message 5 "Deleting article %s in %s..."
373                             (car articles) group)
374           (funcall nnmail-delete-file-function (car articles)))
375         (setq articles (cdr articles))))
376     ;; Try to delete the directory itself.
377     (ignore-errors
378       (delete-directory nnmh-current-directory)))
379   ;; Remove the group from all structures.
380   (setq nnmh-group-alist
381         (delq (assoc group nnmh-group-alist) nnmh-group-alist)
382         nnmh-current-directory nil)
383   t)
384
385 (deffoo nnmh-request-rename-group (group new-name &optional server)
386   (nnmh-possibly-change-directory group server)
387   (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
388         (old-dir (nnmail-group-pathname group nnmh-directory)))
389     (when (ignore-errors
390             (make-directory new-dir t)
391             t)
392       ;; We move the articles file by file instead of renaming
393       ;; the directory -- there may be subgroups in this group.
394       ;; One might be more clever, I guess.
395       (let ((files (nnheader-article-to-file-alist old-dir)))
396         (while files
397           (rename-file
398            (concat old-dir (cdar files))
399            (concat new-dir (cdar files)))
400           (pop files)))
401       (when (<= (length (directory-files old-dir)) 2)
402         (ignore-errors
403           (delete-directory old-dir)))
404       ;; That went ok, so we change the internal structures.
405       (let ((entry (assoc group nnmh-group-alist)))
406         (when entry
407           (setcar entry new-name))
408         (setq nnmh-current-directory nil)
409         t))))
410
411 (nnoo-define-skeleton nnmh)
412
413 \f
414 ;;; Internal functions.
415
416 (defun nnmh-possibly-change-directory (newsgroup &optional server)
417   (when (and server
418              (not (nnmh-server-opened server)))
419     (nnmh-open-server server))
420   (when newsgroup
421     (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
422           (file-name-coding-system nnmail-pathname-coding-system))
423       (if (file-directory-p pathname)
424           (setq nnmh-current-directory pathname)
425         (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory)))))
426
427 (defun nnmh-possibly-create-directory (group)
428   (let (dir dirs)
429     (setq dir (nnmail-group-pathname group nnmh-directory))
430     (while (not (file-directory-p dir))
431       (push dir dirs)
432       (setq dir (file-name-directory (directory-file-name dir))))
433     (while dirs
434       (when (make-directory (directory-file-name (car dirs)))
435         (error "Could not create directory %s" (car dirs)))
436       (nnheader-message 5 "Creating mail directory %s" (car dirs))
437       (setq dirs (cdr dirs)))))
438
439 (defun nnmh-save-mail (group-art &optional noinsert)
440   "Called narrowed to an article."
441   (unless noinsert
442     (nnmail-insert-lines)
443     (nnmail-insert-xref group-art))
444   (run-hooks 'nnmail-prepare-save-mail-hook)
445   (run-hooks 'nnmh-prepare-save-mail-hook)
446   (goto-char (point-min))
447   (while (looking-at "From ")
448     (replace-match "X-From-Line: ")
449     (forward-line 1))
450   ;; We save the article in all the newsgroups it belongs in.
451   (let ((ga group-art)
452         first)
453     (while ga
454       (nnmh-possibly-create-directory (caar ga))
455       (let ((file (concat (nnmail-group-pathname
456                            (caar ga) nnmh-directory)
457                           (int-to-string (cdar ga)))))
458         (if first
459             ;; It was already saved, so we just make a hard link.
460             (funcall nnmail-crosspost-link-function first file t)
461           ;; Save the article.
462           (nnmail-write-region (point-min) (point-max) file nil nil)
463           (setq first file)))
464       (setq ga (cdr ga))))
465   group-art)
466
467 (defun nnmh-active-number (group)
468   "Compute the next article number in GROUP."
469   (let ((active (cadr (assoc group nnmh-group-alist)))
470         (dir (nnmail-group-pathname group nnmh-directory))
471         (file-name-coding-system nnmail-pathname-coding-system)
472         file)
473     (unless active
474       ;; The group wasn't known to nnmh, so we just create an active
475       ;; entry for it.
476       (setq active (cons 1 0))
477       (push (list group active) nnmh-group-alist)
478       (unless (file-exists-p dir)
479         (gnus-make-directory dir))
480       ;; Find the highest number in the group.
481       (let ((files (sort
482                     (mapcar 'string-to-number
483                             (directory-files dir nil "^[0-9]+$"))
484                     '>)))
485         (when files
486           (setcdr active (car files)))))
487     (setcdr active (1+ (cdr active)))
488     (while (or
489             ;; See whether the file exists...
490             (file-exists-p
491              (setq file (concat (nnmail-group-pathname group nnmh-directory)
492                                 (int-to-string (cdr active)))))
493             ;; ... or there is a buffer that will make that file exist
494             ;; in the future.
495             (get-file-buffer file))
496       ;; Skip past that file.
497       (setcdr active (1+ (cdr active))))
498     (cdr active)))
499
500 (defun nnmh-update-gnus-unreads (group)
501   ;; Go through the .nnmh-articles file and compare with the actual
502   ;; articles in this folder.  The articles that are "new" will be
503   ;; marked as unread by Gnus.
504   (let* ((dir nnmh-current-directory)
505          (files (sort (mapcar 'string-to-number
506                               (directory-files nnmh-current-directory
507                                                nil "^[0-9]+$" t))
508                       '<))
509          (nnmh-file (concat dir ".nnmh-articles"))
510          new articles)
511     ;; Load the .nnmh-articles file.
512     (when (file-exists-p nnmh-file)
513       (setq articles
514             (let (nnmh-newsgroup-articles)
515               (ignore-errors (load nnmh-file nil t t))
516               nnmh-newsgroup-articles)))
517     ;; Add all new articles to the `new' list.
518     (let ((art files))
519       (while art
520         (unless (assq (car art) articles)
521           (push (car art) new))
522         (setq art (cdr art))))
523     ;; Remove all deleted articles.
524     (let ((art articles))
525       (while art
526         (unless (memq (caar art) files)
527           (setq articles (delq (car art) articles)))
528         (setq art (cdr art))))
529     ;; Check whether the articles really are the ones that Gnus thinks
530     ;; they are by looking at the time-stamps.
531     (let ((arts articles)
532           art)
533       (while (setq art (pop arts))
534         (when (not (equal
535                     (nth 5 (file-attributes
536                             (concat dir (int-to-string (car art)))))
537                     (cdr art)))
538           (setq articles (delq art articles))
539           (push (car art) new))))
540     ;; Go through all the new articles and add them, and their
541     ;; time-stamps, to the list.
542     (setq articles
543           (nconc articles
544                  (mapcar
545                   (lambda (art)
546                     (cons art
547                           (nth 5 (file-attributes
548                                   (concat dir (int-to-string art))))))
549                   new)))
550     ;; Make Gnus mark all new articles as unread.
551     (when new
552       (gnus-make-articles-unread
553        (gnus-group-prefixed-name group (list 'nnmh ""))
554        (setq new (sort new '<))))
555     ;; Sort the article list with highest numbers first.
556     (setq articles (sort articles (lambda (art1 art2)
557                                     (> (car art1) (car art2)))))
558     ;; Finally write this list back to the .nnmh-articles file.
559     (with-temp-file nnmh-file
560       (insert ";; Gnus article active file for " group "\n\n")
561       (insert "(setq nnmh-newsgroup-articles '")
562       (gnus-prin1 articles)
563       (insert ")\n"))))
564
565 (defun nnmh-deletable-article-p (group article)
566   "Say whether ARTICLE in GROUP can be deleted."
567   (let ((path (concat nnmh-current-directory (int-to-string article))))
568     ;; Writable.
569     (and (file-writable-p path)
570          (or
571           ;; We can never delete the last article in the group.
572           (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
573                    article))
574           ;; Well, we can.
575           nnmh-allow-delete-final))))
576
577 (provide 'nnmh)
578
579 ;; arch-tag: 36c12a98-3bad-44b3-9953-628078ef0e04
580 ;;; nnmh.el ends here