*** empty log message ***
[gnus] / lisp / nnmh.el
1 ;;; nnmh.el --- mhspool access for Gnus
2 ;; Copyright (C) 1995 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
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
28 ;; Gnus sources.  
29
30 ;;; Code:
31
32 (require 'nnheader)
33 (require 'rmail)
34 (require 'nnmail)
35 (require 'gnus)
36
37 (defvar nnmh-directory "~/Mail/"
38   "*Mail spool directory.")
39
40 (defvar nnmh-get-new-mail t
41   "*If non-nil, nnmh will check the incoming mail file and split the mail.")
42
43 (defvar nnmh-prepare-save-mail-hook nil
44   "*Hook run narrowed to an article before saving.")
45
46 (defvar nnmh-be-safe nil
47   "*If non-nil, nnmh will check all articles to make sure whether they are new or not.")
48
49 \f
50
51 (defconst nnmh-version "nnmh 1.0"
52   "nnmh version.")
53
54 (defvar nnmh-current-directory nil
55   "Current news group directory.")
56
57 (defvar nnmh-status-string "")
58 (defvar nnmh-group-alist nil)
59
60 \f
61
62 (defvar nnmh-current-server nil)
63 (defvar nnmh-server-alist nil)
64 (defvar nnmh-server-variables 
65   (list
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 "")
70    '(nnmh-group-alist)))
71
72 \f
73
74 ;;; Interface functions.
75
76 (defun nnmh-retrieve-headers (sequence &optional newsgroup server fetch-old)
77   (save-excursion
78     (set-buffer nntp-server-buffer)
79     (erase-buffer)
80     (let* ((file nil)
81            (number (length sequence))
82            (large (and (numberp nnmail-large-newsgroup)
83                        (> number nnmail-large-newsgroup)))
84            (count 0)
85            beg article)
86       (nnmh-possibly-change-directory newsgroup)
87       (if (stringp (car sequence))
88           'headers
89         (while sequence
90           (setq article (car sequence))
91           (setq file
92                 (concat nnmh-current-directory (int-to-string article)))
93           (if (and (file-exists-p file)
94                    (not (file-directory-p file)))
95               (progn
96                 (insert (format "221 %d Article retrieved.\n" article))
97                 (setq beg (point))
98                 (nnheader-insert-head file)
99                 (goto-char beg)
100                 (if (search-forward "\n\n" nil t)
101                     (forward-char -1)
102                   (goto-char (point-max))
103                   (insert "\n\n"))
104                 (insert ".\n")
105                 (delete-region (point) (point-max))))
106           (setq sequence (cdr sequence))
107           (setq count (1+ count))
108
109           (and large
110                (zerop (% count 20))
111                (message "nnmh: Receiving headers... %d%%"
112                         (/ (* count 100) number))))
113
114         (and large (message "nnmh: Receiving headers...done"))
115
116         ;; Fold continuation lines.
117         (goto-char (point-min))
118         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
119           (replace-match " " t t))
120         'headers))))
121
122 (defun nnmh-open-server (server &optional defs)
123   (nnheader-init-server-buffer)
124   (if (equal server nnmh-current-server)
125       t
126     (if nnmh-current-server
127         (setq nnmh-server-alist 
128               (cons (list nnmh-current-server
129                           (nnheader-save-variables nnmh-server-variables))
130                     nnmh-server-alist)))
131     (let ((state (assoc server nnmh-server-alist)))
132       (if state 
133           (progn
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)))
138
139 (defun nnmh-close-server (&optional server)
140   (setq nnmh-current-server nil)
141   t)
142
143 (defun nnmh-server-opened (&optional server)
144   (and (equal server nnmh-current-server)
145        nntp-server-buffer
146        (buffer-name nntp-server-buffer)))
147
148 (defun nnmh-status-message (&optional server)
149   nnmh-status-string)
150
151 (defun nnmh-request-article (id &optional newsgroup server buffer)
152   (nnmh-possibly-change-directory newsgroup)
153   (let ((file (if (stringp id)
154                   nil
155                 (concat nnmh-current-directory (int-to-string id))))
156         (nntp-server-buffer (or buffer nntp-server-buffer)))
157     (and (stringp file)
158          (file-exists-p file)
159          (not (file-directory-p file))
160          (save-excursion (nnmail-find-file file))
161          (string-to-int (file-name-nondirectory file)))))
162
163 (defun nnmh-request-group (group &optional server dont-check)
164   (let ((pathname (nnmail-group-pathname group nnmh-directory))
165         dir)
166     (if (file-directory-p pathname)
167         (progn
168           (setq nnmh-current-directory pathname)
169           (and nnmh-get-new-mail 
170                nnmh-be-safe
171                (nnmh-update-gnus-unreads group))
172           (or dont-check
173               (progn
174                 (setq dir 
175                       (sort
176                        (mapcar
177                         (function
178                          (lambda (name)
179                            (string-to-int name)))
180                         (directory-files pathname nil "^[0-9]+$" t))
181                        '<))
182                 (save-excursion
183                   (set-buffer nntp-server-buffer)
184                   (erase-buffer)
185                   (if dir
186                       (insert (format "211 %d %d %d %s\n" (length dir) 
187                                       (car dir)
188                                       (progn (while (cdr dir)
189                                                (setq dir (cdr dir)))
190                                              (car dir))
191                                       group))
192                     (insert (format "211 0 1 0 %s\n" group))))))
193           t)
194       (setq nnmh-status-string "No such group")
195       nil)))
196
197 (defun nnmh-request-scan (&optional group server)
198   (nnmail-get-new-mail 'nnmh nil nnmh-directory group))      
199
200 (defun nnmh-request-list (&optional server dir)
201   (or dir
202       (save-excursion
203         (set-buffer nntp-server-buffer)
204         (erase-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))))
211     (while dirs 
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))
219       (let ((files (mapcar
220                     (lambda (name) (string-to-int name))
221                     (directory-files dir nil "^[0-9]+$" t))))
222         (if (null files)
223             ()
224           (save-excursion
225             (set-buffer nntp-server-buffer)
226             (goto-char (point-max))
227             (insert 
228              (format 
229               "%s %d %d y\n" 
230               (progn
231                 (string-match 
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))
239   t)
240
241 (defun nnmh-request-newgroups (date &optional server)
242   (nnmh-request-list server))
243
244 (defun nnmh-request-post (&optional server)
245   (mail-send-and-exit nil))
246
247 (defun nnmh-request-expire-articles (articles newsgroup &optional server force)
248   (nnmh-possibly-change-directory newsgroup)
249   (let* ((days (or (and nnmail-expiry-wait-function
250                         (funcall nnmail-expiry-wait-function newsgroup))
251                    nnmail-expiry-wait))
252          (active-articles 
253           (mapcar
254            (function
255             (lambda (name)
256               (string-to-int name)))
257            (directory-files nnmh-current-directory nil "^[0-9]+$" t)))
258          (max-article (and active-articles (apply 'max active-articles)))
259          (is-old t)
260          article rest mod-time)
261     (nnmail-activate 'nnmh)
262
263     (while (and articles is-old)
264       (setq article (concat nnmh-current-directory 
265                             (int-to-string (car articles))))
266       (if (setq mod-time (nth 5 (file-attributes article)))
267           (if (and (nnmh-deletable-article-p newsgroup (car articles))
268                    (or force
269                        (and (not (equal mod-time '(0 0)))
270                             (setq is-old
271                                   (> (nnmail-days-between
272                                       (current-time-string)
273                                       (current-time-string mod-time))
274                                      days)))))
275               (progn
276                 (and gnus-verbose-backends 
277                      (message "Deleting article %s in %s..." 
278                               article newsgroup))
279                 (condition-case ()
280                     (funcall nnmail-delete-file-function article)
281                   (file-error
282                    (setq rest (cons (car articles) rest)))))
283             (setq rest (cons (car articles) rest))))
284       (setq articles (cdr articles)))
285     (message "")
286     (nconc rest articles)))
287
288 (defun nnmh-close-group (group &optional server)
289   t)
290
291 (defun nnmh-request-move-article 
292   (article group server accept-form &optional last)
293   (let ((buf (get-buffer-create " *nnmh move*"))
294         result)
295     (and 
296      (nnmh-deletable-article-p group article)
297      (nnmh-request-article article group server)
298      (save-excursion
299        (set-buffer buf)
300        (insert-buffer-substring nntp-server-buffer)
301        (setq result (eval accept-form))
302        (kill-buffer (current-buffer))
303        result)
304      (condition-case ()
305          (funcall nnmail-delete-file-function
306                   (concat nnmh-current-directory (int-to-string article)))
307        (file-error nil)))
308     result))
309
310 (defun nnmh-request-accept-article (group &optional last)
311   (if (stringp group)
312       (and 
313        (nnmail-activate 'nnmh)
314        ;; We trick the choosing function into believing that only one
315        ;; group is availiable.  
316        (let ((nnmail-split-methods (list (list group ""))))
317          (car (nnmh-save-mail))))
318     (and
319      (nnmail-activate 'nnmh)
320      (car (nnmh-save-mail)))))
321
322 (defun nnmh-request-replace-article (article group buffer)
323   (nnmh-possibly-change-directory group)
324   (save-excursion
325     (set-buffer buffer)
326     (nnmh-possibly-create-directory group)
327     (condition-case ()
328         (progn
329           (write-region (point-min) (point-max)
330                         (concat nnmh-current-directory (int-to-string article))
331                         nil (if gnus-verbose-backends nil 'nomesg))
332           t)
333       (error nil))))
334
335 (defun nnmh-request-create-group (group &optional server) 
336   (nnmail-activate 'nnmh)
337   (or (assoc group nnmh-group-alist)
338       (let (active)
339         (setq nnmh-group-alist (cons (list group (setq active (cons 1 0)))
340                                      nnmh-group-alist))
341         (nnmh-possibly-create-directory group)
342         (nnmh-possibly-change-directory group)
343         (let ((articles (mapcar
344                          (lambda (file)
345                            (string-to-int file))
346                          (directory-files 
347                           nnmh-current-directory nil "^[0-9]+$"))))
348           (and articles
349                (progn
350                  (setcar active (apply 'min articles))
351                  (setcdr active (apply 'max articles)))))))
352   t)
353
354 (defun nnmh-request-delete-group (group &optional force server)
355   (nnmh-possibly-change-directory group)
356   ;; Delete all articles in GROUP.
357   (if (not force)
358       ()                                ; Don't delete the articles.
359     (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
360       (while articles 
361         (and (file-writable-p (car articles))
362              (progn
363                (and gnus-verbose-backends
364                     (message (message "Deleting article %s in %s..."
365                                       (car articles) group)))
366                (funcall nnmail-delete-file-function (car articles))))
367         (setq articles (cdr articles))))
368     ;; Try to delete the directory itself.
369     (condition-case ()
370         (delete-directory nnmh-current-directory)
371       (error nil)))
372   ;; Remove the group from all structures.
373   (setq nnmh-group-alist 
374         (delq (assoc group nnmh-group-alist) nnmh-group-alist)
375         nnmh-current-directory nil)
376   t)
377
378 (defun nnmh-request-rename-group (group new-name &optional server)
379   (nnmh-possibly-change-directory group)
380   ;; Rename directory.
381   (and (file-writable-p nnmh-current-directory)
382        (condition-case ()
383            (progn
384              (rename-file 
385               (directory-file-name nnmh-current-directory)
386               (directory-file-name 
387                (nnmail-group-pathname new-name nnmh-directory)))
388              t)
389          (error nil))
390        ;; That went ok, so we change the internal structures.
391        (let ((entry (assoc group nnmh-group-alist)))
392          (and entry (setcar entry new-name))
393          (setq nnmh-current-directory nil)
394          t)))
395
396 \f
397 ;;; Internal functions.
398
399 (defun nnmh-possibly-change-directory (newsgroup)
400   (if newsgroup
401       (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory)))
402         (if (file-directory-p pathname)
403             (setq nnmh-current-directory pathname)
404           (error "No such newsgroup: %s" newsgroup)))))
405
406 (defun nnmh-possibly-create-directory (group)
407   (let (dir dirs)
408     (setq dir (nnmail-group-pathname group nnmh-directory))
409     (while (not (file-directory-p dir))
410       (setq dirs (cons dir dirs))
411       (setq dir (file-name-directory (directory-file-name dir))))
412     (while dirs
413       (if (make-directory (directory-file-name (car dirs)))
414           (error "Could not create directory %s" (car dirs)))
415       (and gnus-verbose-backends 
416            (message "Creating mail directory %s" (car dirs)))
417       (setq dirs (cdr dirs)))))
418              
419 (defun nnmh-save-mail ()
420   "Called narrowed to an article."
421   (let ((group-art (nreverse (nnmail-article-group 'nnmh-active-number))))
422     (nnmail-insert-lines)
423     (nnmail-insert-xref group-art)
424     (run-hooks 'nnmh-prepare-save-mail-hook)
425     (goto-char (point-min))
426     (while (looking-at "From ")
427       (replace-match "X-From-Line: ")
428       (forward-line 1))
429     ;; We save the article in all the newsgroups it belongs in.
430     (let ((ga group-art)
431           first)
432       (while ga
433         (nnmh-possibly-create-directory (car (car ga)))
434         (let ((file (concat (nnmail-group-pathname 
435                              (car (car ga)) nnmh-directory) 
436                             (int-to-string (cdr (car ga))))))
437           (if first
438               ;; It was already saved, so we just make a hard link.
439               (add-name-to-file first file t)
440             ;; Save the article.
441             (write-region (point-min) (point-max) file nil nil)
442             (setq first file)))
443         (setq ga (cdr ga))))
444     group-art))
445
446 (defun nnmh-active-number (group)
447   "Compute the next article number in GROUP."
448   (let ((active (car (cdr (assoc group nnmh-group-alist)))))
449     ;; The group wasn't known to nnmh, so we just create an active
450     ;; entry for it.   
451     (or active
452         (progn
453           (setq active (cons 1 0))
454           (setq nnmh-group-alist (cons (list group active) nnmh-group-alist))))
455     (setcdr active (1+ (cdr active)))
456     (while (file-exists-p
457             (concat (nnmail-group-pathname group nnmh-directory)
458                     (int-to-string (cdr active))))
459       (setcdr active (1+ (cdr active))))
460     (cdr active)))
461
462 (defun nnmh-update-gnus-unreads (group)
463   ;; Go through the .nnmh-articles file and compare with the actual
464   ;; articles in this folder. The articles that are "new" will be
465   ;; marked as unread by Gnus.
466   (let* ((dir nnmh-current-directory)
467          (files (sort (mapcar (function (lambda (name) (string-to-int name)))
468                               (directory-files nnmh-current-directory 
469                                                nil "^[0-9]+$" t)) '<))
470          (nnmh-file (concat dir ".nnmh-articles"))
471          new articles)
472     ;; Load the .nnmh-articles file.
473     (if (file-exists-p nnmh-file)
474         (setq articles 
475               (let (nnmh-newsgroup-articles)
476                 (condition-case nil (load nnmh-file nil t t) (error nil))
477                 nnmh-newsgroup-articles)))
478     ;; Add all new articles to the `new' list.
479     (let ((art files))
480       (while art
481         (if (not (assq (car art) articles)) (setq new (cons (car art) new)))
482         (setq art (cdr art))))
483     ;; Remove all deleted articles.
484     (let ((art articles))
485       (while art
486         (if (not (memq (car (car art)) files))
487             (setq articles (delq (car art) articles)))
488         (setq art (cdr art))))
489     ;; Check whether the highest-numbered articles really are the ones
490     ;; that Gnus thinks they are by looking at the time-stamps.
491     (let ((art articles))
492       (while (and art 
493                   (not (equal 
494                         (nth 5 (file-attributes 
495                                 (concat dir (int-to-string (car (car art))))))
496                         (cdr (car art)))))
497         (setq articles (delq (car art) articles))
498         (setq new (cons (car (car art)) new))
499         (setq art (cdr art))))
500     ;; Go through all the new articles and add them, and their
501     ;; time-stamps to the list.
502     (let ((n new))
503       (while n
504         (setq articles 
505               (cons (cons 
506                      (car n)
507                      (nth 5 (file-attributes 
508                              (concat dir (int-to-string (car n))))))
509                     articles))
510         (setq n (cdr n))))
511     ;; Make Gnus mark all new articles as unread.
512     (or (zerop (length new))
513         (gnus-make-articles-unread 
514          (gnus-group-prefixed-name group (list 'nnmh ""))
515          (setq new (sort new '<))))
516     ;; Sort the article list with highest numbers first.
517     (setq articles (sort articles (lambda (art1 art2) 
518                                     (> (car art1) (car art2)))))
519     ;; Finally write this list back to the .nnmh-articles file.
520     (save-excursion
521       (set-buffer (get-buffer-create "*nnmh out*"))
522       (insert ";; Gnus article active file for " group "\n\n")
523       (insert "(setq nnmh-newsgroup-articles '")
524       (insert (prin1-to-string articles) ")\n")
525       (write-region (point-min) (point-max) nnmh-file nil 'nomesg)
526       (kill-buffer (current-buffer)))))
527
528 (defun nnmh-deletable-article-p (group article)
529   "Say whether ARTICLE in GROUP can be deleted."
530   (or (not nnmail-keep-last-article)
531       (not (eq (cdr (nth 1 (assoc group nnmh-group-alist))) article))))
532
533 (provide 'nnmh)
534
535 ;;; nnmh.el ends here