*** empty log message ***
[gnus] / lisp / nnml.el
1 ;;; nnml.el --- mail spool 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
28 ;;; Code:
29
30 (require 'nnheader)
31 (require 'nnmail)
32
33 (defvar nnml-directory "~/Mail/"
34   "Mail directory.")
35
36 (defvar nnml-active-file (concat nnml-directory "active")
37   "Mail active file.")
38
39 (defvar nnml-newsgroups-file (concat nnml-directory "newsgroups")
40   "Mail newsgroups description file.")
41
42 (defvar nnml-get-new-mail t
43   "If non-nil, nnml will check the incoming mail file and split the mail.")
44
45 (defvar nnml-nov-is-evil nil
46   "If non-nil, Gnus will never generate and use nov databases for mail groups.
47 Using nov databases will speed up header fetching considerably.
48 This variable shouldn't be flipped much. If you have, for some reason,
49 set this to t, and want to set it to nil again, you should always run
50 the `nnml-generate-nov-databases' command. The function will go
51 through all nnml directories and generate nov databases for them
52 all. This may very well take some time.")
53
54 \f
55
56 (defconst nnml-version "nnml 0.2"
57   "nnml version.")
58
59 (defvar nnml-current-directory nil
60   "Current news group directory.")
61
62 (defvar nnml-status-string "")
63
64 (defvar nnml-nov-buffer-alist nil)
65
66 (defvar nnml-group-alist nil)
67 (defvar nnml-active-timestamp nil)
68
69 \f
70
71 ;;; Interface functions.
72
73 (defun nnml-retrieve-headers (sequence &optional newsgroup server)
74   "Retrieve the headers for the articles in SEQUENCE.
75 Newsgroup must be selected before calling this function."
76   (save-excursion
77     (set-buffer nntp-server-buffer)
78     (erase-buffer)
79     (let ((file nil)
80           (number (length sequence))
81           (count 0)
82           beg article)
83       (nnml-possibly-change-directory newsgroup)
84       (if (nnml-retrieve-headers-with-nov sequence)
85           'nov
86         (while sequence
87           (setq article (car sequence))
88           (setq file
89                 (concat nnml-current-directory (prin1-to-string article)))
90           (if (and (file-exists-p file)
91                    (not (file-directory-p file)))
92               (progn
93                 (insert (format "221 %d Article retrieved.\n" article))
94                 (setq beg (point))
95                 (insert-file-contents file)
96                 (goto-char beg)
97                 (if (search-forward "\n\n" nil t)
98                     (forward-char -1)
99                   (goto-char (point-max))
100                   (insert "\n\n"))
101                 (insert ".\n")
102                 (delete-region (point) (point-max))))
103           (setq sequence (cdr sequence))
104           (setq count (1+ count))
105           (and (numberp nnmail-large-newsgroup)
106                (> number nnmail-large-newsgroup)
107                (zerop (% count 20))
108                gnus-verbose-backends
109                (message "nnml: Receiving headers... %d%%"
110                         (/ (* count 100) number))))
111
112         (and (numberp nnmail-large-newsgroup)
113              (> number nnmail-large-newsgroup)
114              gnus-verbose-backends
115              (message "nnml: Receiving headers... done"))
116
117         ;; Fold continuation lines.
118         (goto-char 1)
119         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
120           (replace-match " " t t))
121         'headers))))
122
123 (defun nnml-open-server (host &optional service)
124   (setq nnml-status-string "")
125   (nnheader-init-server-buffer))
126
127 (defun nnml-close-server (&optional server)
128   "Close news server."
129   t)
130
131 (defun nnml-server-opened (&optional server)
132   "Return server process status, T or NIL.
133 If the stream is opened, return T, otherwise return NIL."
134   (and nntp-server-buffer
135        (get-buffer nntp-server-buffer)))
136
137 (defun nnml-status-message (&optional server)
138   "Return server status response as string."
139   nnml-status-string)
140
141 (defun nnml-request-article (id &optional newsgroup server buffer)
142   "Select article by message ID (or number)."
143   (nnml-possibly-change-directory newsgroup)
144   (let ((file (if (stringp id)
145                   nil
146                 (concat nnml-current-directory (prin1-to-string id))))
147         (nntp-server-buffer (or buffer nntp-server-buffer)))
148     (if (and (stringp file)
149              (file-exists-p file)
150              (not (file-directory-p file)))
151         (save-excursion
152           (nnmail-find-file file)))))
153
154 (defun nnml-request-group (group &optional server dont-check)
155   "Select news GROUP."
156   (if (not (nnml-possibly-change-directory group))
157       (progn
158         (setq nnml-status-string "Invalid group (no such directory)")
159         nil)
160     (if dont-check 
161         t
162       (nnml-get-new-mail)
163       (let ((timestamp (nth 5 (file-attributes nnml-active-file))))
164         (if (or (not nnml-active-timestamp)
165                 (> (nth 0 timestamp) (nth 0 nnml-active-timestamp))
166                 (> (nth 1 timestamp) (nth 1 nnml-active-timestamp)))
167             (progn
168               (setq nnml-active-timestamp timestamp)
169               (nnml-request-list)
170               (setq nnml-group-alist (nnmail-get-active))))
171         (let ((active (nth 1 (assoc group nnml-group-alist))))
172           (save-excursion
173             (set-buffer nntp-server-buffer)
174             (erase-buffer)
175             (if (not active)
176                 ()
177               (insert (format "211 %d %d %d %s\n" 
178                               (max (1+ (- (cdr active) (car active))) 0)
179                               (car active) (cdr active) group))
180               t)))))))
181
182 (defun nnml-close-group (group &optional server)
183   t)
184
185 (defun nnml-request-list (&optional server)
186   "List active newsgoups."
187   (if server (nnml-get-new-mail))
188   (save-excursion
189     (nnmail-find-file nnml-active-file)))
190
191 (defun nnml-request-newgroups (date &optional server)
192   "List groups created after DATE."
193   (nnml-request-list server))
194
195 (defun nnml-request-list-newsgroups (&optional server)
196   "List newsgroups (defined in NNTP2)."
197   (save-excursion
198     (nnmail-find-file nnml-newsgroups-file)))
199
200 (defun nnml-request-post (&optional server)
201   "Post a new news in current buffer."
202   (mail-send-and-exit nil))
203
204 (fset 'nnml-request-post-buffer 'nnmail-request-post-buffer)
205
206 (defun nnml-request-expire-articles (articles newsgroup &optional server force)
207   "Expire all articles in the ARTICLES list in group GROUP.
208 The list of unexpired articles will be returned (ie. all articles that
209 were too fresh to be expired).
210 If FORCE is non-nil, ARTICLES will be deleted whether they are old or not."
211   (nnml-possibly-change-directory newsgroup)
212   (let* ((days (or (and nnmail-expiry-wait-function
213                         (funcall nnmail-expiry-wait-function newsgroup))
214                    nnmail-expiry-wait))
215          article rest mod-time)
216     (if nnmail-keep-last-article
217         (progn
218           (setq articles (sort articles '>))
219           (setq rest (cons (car articles) rest))
220           (setq articles (cdr articles))))
221     (while articles
222       (setq article (concat nnml-current-directory (int-to-string
223                                                       (car articles))))
224       (if (setq mod-time (nth 5 (file-attributes article)))
225           (if (or force
226                   (> (nnmail-days-between
227                       (current-time-string)
228                       (current-time-string mod-time))
229                      days))
230               (progn
231                 (and gnus-verbose-backends (message "Deleting %s..." article))
232                 (condition-case ()
233                     (delete-file article)
234                   (file-error nil))
235                 (nnml-nov-delete-article newsgroup (car articles)))
236             (setq rest (cons (car articles) rest))))
237       (setq articles (cdr articles)))
238     (nnml-save-nov)
239     rest))
240
241 (defun nnml-request-move-article 
242   (article group server accept-form &optional last)
243   (let ((buf (get-buffer-create " *nnml move*"))
244         result)
245     (and 
246      (nnml-request-article article group server)
247      (save-excursion
248        (set-buffer buf)
249        (insert-buffer-substring nntp-server-buffer)
250        (setq result (eval accept-form))
251        (kill-buffer (current-buffer))
252        result)
253      (progn
254        (condition-case ()
255            (delete-file (concat nnml-current-directory 
256                                 (int-to-string article)))
257          (file-error nil))
258        (nnml-nov-delete-article group article)
259        (and last (nnml-save-nov))))
260     result))
261
262 (defun nnml-request-accept-article (group &optional last)
263   (let (result)
264     (if (stringp group)
265         (and 
266          (nnml-request-list)
267          (setq nnml-group-alist (nnmail-get-active))
268          ;; We trick the choosing function into believing that only one
269          ;; group is availiable.  
270          (let ((nnmail-split-methods (list (list group ""))))
271            (setq result (car (nnml-save-mail))))
272          (progn
273            (nnmail-save-active nnml-group-alist nnml-active-file)
274            (and last (nnml-save-nov))))
275       (and
276        (nnml-request-list)
277        (setq nnml-group-alist (nnmail-get-active))
278        (setq result (car (nnml-save-mail)))
279        (progn
280          (nnmail-save-active nnml-group-alist nnml-active-file)
281          (and last (nnml-save-nov)))))
282     result))
283
284 (defun nnml-request-replace-article (article group buffer)
285   (nnml-possibly-change-directory group)
286   (save-excursion
287     (set-buffer buffer)
288     (if (not (condition-case ()
289                  (progn
290                    (write-region (point-min) (point-max)
291                                  (concat nnml-current-directory 
292                                          (int-to-string article))
293                                  nil (if gnus-verbose-backends nil 'nomesg))
294                    t)
295                (error nil)))
296         ()
297       (let ((chars (nnmail-insert-lines))
298             (art (concat (int-to-string article) "\t"))
299             nov-line)
300         (setq nov-line (nnml-make-nov-line chars))
301         (save-excursion 
302           (set-buffer (nnml-open-nov group))
303           (goto-char (point-min))
304           (if (or (looking-at art)
305                   (search-forward (concat "\n" art)))
306               (progn
307                 (delete-region (progn (beginning-of-line) (point))
308                                (progn (forward-line 1) (point)))
309                 (insert (int-to-string article) nov-line)
310                 (nnml-save-nov))
311             (kill-buffer (current-buffer)))
312           t)))))
313
314
315 \f
316 ;;; Internal functions
317
318 (defun nnml-retrieve-headers-with-nov (articles)
319   (if (or gnus-nov-is-evil nnml-nov-is-evil)
320       nil
321     (let ((first (car articles))
322           (last (progn (while (cdr articles) (setq articles (cdr articles)))
323                        (car articles)))
324           (nov (concat nnml-current-directory ".nov")))
325       (if (file-exists-p nov)
326           (save-excursion
327             (set-buffer nntp-server-buffer)
328             (erase-buffer)
329             (insert-file-contents nov)
330             (goto-char 1)
331             (while (and (not (eobp)) (< first (read (current-buffer))))
332               (forward-line 1))
333             (beginning-of-line)
334             (if (not (eobp)) (delete-region 1 (point)))
335             (while (and (not (eobp)) (>= last (read (current-buffer))))
336               (forward-line 1))
337             (beginning-of-line)
338             (if (not (eobp)) (delete-region (point) (point-max)))
339             t)))))
340
341 (defun nnml-possibly-change-directory (newsgroup)
342   (if newsgroup
343       (let ((pathname (nnmail-article-pathname newsgroup nnml-directory)))
344         (and (file-directory-p pathname)
345              (setq nnml-current-directory pathname)))
346     t))
347              
348 (defun nnml-create-directories ()
349   (let ((methods nnmail-split-methods)
350         dir dirs)
351     (while methods
352       (setq dir (nnmail-article-pathname (car (car methods)) nnml-directory))
353       (while (not (file-directory-p dir))
354         (setq dirs (cons dir dirs))
355         (setq dir (file-name-directory (directory-file-name dir))))
356       (while dirs
357         (if (make-directory (directory-file-name (car dirs)))
358             (error "Could not create directory %s" (car dirs)))
359         (and gnus-verbose-backends 
360              (message "Creating mail directory %s" (car dirs)))
361         (setq dirs (cdr dirs)))
362       (setq methods (cdr methods)))))
363
364 (defun nnml-save-mail ()
365   "Called narrowed to an article."
366   (let ((group-art (nreverse (nnmail-article-group 'nnml-active-number)))
367         chars nov-line)
368     (setq chars (nnmail-insert-lines))
369     (nnmail-insert-xref group-art)
370     (goto-char (point-min))
371     (while (looking-at "From ")
372       (replace-match "X-From-Line: ")
373       (forward-line 1))
374     ;; We save the article in all the newsgroups it belongs in.
375     (let ((ga group-art)
376           first)
377       (while ga
378         (let ((file (concat (nnmail-article-pathname 
379                              (car (car ga)) nnml-directory)
380                             (int-to-string (cdr (car ga))))))
381           (if first
382               ;; It was already saved, so we just make a hard link.
383               (add-name-to-file first file t)
384             ;; Save the article.
385             (write-region (point-min) (point-max) file nil 
386                           (if gnus-verbose-backends nil 'nomesg))
387             (setq first file)))
388         (setq ga (cdr ga))))
389     ;; Generate a nov line for this article. We generate the nov
390     ;; line after saving, because nov generation destroys the
391     ;; header. 
392     (setq nov-line (nnml-make-nov-line chars))
393     ;; Output the nov line to all nov databases that should have it.
394     (let ((ga group-art))
395       (while ga
396         (nnml-add-nov (car (car ga)) (cdr (car ga)) nov-line)
397         (setq ga (cdr ga))))
398     group-art))
399
400 (defun nnml-active-number (group)
401   "Compute the next article number in GROUP."
402   (let ((active (car (cdr (assoc group nnml-group-alist)))))
403     (setcdr active (1+ (cdr active)))
404     (let (file)
405       (while (file-exists-p
406               (setq file (concat (nnmail-article-pathname 
407                                   group nnml-directory)
408                                  (int-to-string (cdr active)))))
409         (setcdr active (1+ (cdr active)))))
410     (cdr active)))
411
412 (defun nnml-get-new-mail ()
413   "Read new incoming mail."
414   (let (incoming)
415     (nnml-create-directories)
416     (if (and nnml-get-new-mail nnmail-spool-file
417              (file-exists-p nnmail-spool-file)
418              (> (nth 7 (file-attributes nnmail-spool-file)) 0))
419         (progn
420           (and gnus-verbose-backends 
421                (message "nnml: Reading incoming mail..."))
422           (setq incoming 
423                 (nnmail-move-inbox nnmail-spool-file 
424                                    (concat nnml-directory "Incoming")))
425           (nnml-request-list)
426           (setq nnml-group-alist (nnmail-get-active))
427           (nnmail-split-incoming incoming 'nnml-save-mail)
428           (nnmail-save-active nnml-group-alist nnml-active-file)
429           (nnml-save-nov)
430           (run-hooks 'nnmail-read-incoming-hook)
431           ;; The following has been commented away, just to make sure
432           ;; that nobody ever loses any mail. If you feel safe that
433           ;; nnml will never do anything strange, just remove those
434           ;; two semicolons, and avoid having lots of "Incoming*"
435           ;; files. 
436 ;;         (delete-file incoming)
437           (and gnus-verbose-backends
438                (message "nnml: Reading incoming mail...done"))))))
439
440
441 (defun nnml-add-nov (group article line)
442   "Add a nov line for the GROUP base."
443   (save-excursion 
444     (set-buffer (nnml-open-nov group))
445     (goto-char (point-max))
446     (insert (int-to-string article) line)))
447
448 (defsubst nnml-header-value ()
449   (buffer-substring (match-end 0) (save-excursion (end-of-line) (point))))
450
451 (defun nnml-make-nov-line (chars)
452   "Create a nov from the current headers."
453   (let ((case-fold-search t)
454         subject from date id references lines xref in-reply-to char)
455     (save-excursion
456       (save-restriction
457         (goto-char (point-min))
458         (narrow-to-region 
459          (point)
460          (1- (or (search-forward "\n\n" nil t) (point-max))))
461         ;; Fold continuation lines.
462         (goto-char (point-min))
463         (while (re-search-forward "\\(\r?\n[ \t]+\\)+" nil t)
464           (replace-match " " t t))
465         (subst-char-in-region (point-min) (point-max) ?\t ? )
466         ;; [number subject from date id references chars lines xref]
467         (save-excursion
468           (goto-char (point-min))
469           (while (re-search-forward "^\\(from\\|subject\\|message-id\\|date\\|lines\\|xref\\|references\\|in-reply-to\\): "
470                                     nil t)
471             (beginning-of-line)
472             (setq char (downcase (following-char))) 
473             (cond
474              ((eq char ?s)
475               (setq subject (nnml-header-value)))
476              ((eq char ?f)
477               (setq from (nnml-header-value)))
478              ((eq char ?x)
479               (setq xref (nnml-header-value)))
480              ((eq char ?l)
481               (setq lines (nnml-header-value)))
482              ((eq char ?d)
483               (setq date (nnml-header-value)))
484              ((eq char ?m)
485               (setq id (setq id (nnml-header-value))))
486              ((eq char ?r)
487               (setq references (nnml-header-value)))
488              ((eq char ?i)
489               (setq in-reply-to (nnml-header-value))))
490             (forward-line 1))
491       
492           (and (not references)
493                in-reply-to
494                (string-match "<[^>]+>" in-reply-to)
495                (setq references
496                      (substring in-reply-to (match-beginning 0)
497                                 (match-end 0)))))
498         ;; [number subject from date id references chars lines xref]
499         (format "\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\t\n"
500                 (or subject "(none)")
501                 (or from "(nobody)") (or date "")
502                 (or id "") (or references "")
503                 (or chars 0) (or lines "0") (or xref ""))))))
504
505 (defun nnml-open-nov (group)
506   (or (cdr (assoc group nnml-nov-buffer-alist))
507       (let ((buffer (find-file-noselect 
508                      (concat (nnmail-article-pathname 
509                               group nnml-directory) ".nov"))))
510         (save-excursion
511           (set-buffer buffer)
512           (buffer-disable-undo (current-buffer)))
513         (setq nnml-nov-buffer-alist 
514               (cons (cons group buffer) nnml-nov-buffer-alist))
515         buffer)))
516
517 (defun nnml-save-nov ()
518   (save-excursion
519     (while nnml-nov-buffer-alist
520       (if (buffer-name (cdr (car nnml-nov-buffer-alist)))
521           (progn
522             (set-buffer (cdr (car nnml-nov-buffer-alist)))
523             (and (buffer-modified-p)
524                  (write-region 
525                   1 (point-max) (buffer-file-name) nil 'nomesg))
526             (set-buffer-modified-p nil)
527             (kill-buffer (current-buffer))))
528       (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
529
530 ;;;###autoload
531 (defun nnml-generate-nov-databases (dir)
532   "Generate nov databases in all nnml mail newsgroups."
533   (interactive 
534    (progn   
535      (setq nnml-group-alist nil)
536      (list nnml-directory)))
537   (nnml-open-server (system-name))
538   (let ((dirs (directory-files dir t nil t)))
539     (while dirs 
540       (if (and (not (string-match "/\\.\\.$" (car dirs)))
541                (not (string-match "/\\.$" (car dirs)))
542                (file-directory-p (car dirs)))
543           (nnml-generate-nov-databases (car dirs)))
544       (setq dirs (cdr dirs))))
545   (let ((files (sort
546                 (mapcar
547                  (function
548                   (lambda (name)
549                     (string-to-int name)))
550                  (directory-files dir nil "^[0-9]+$" t))
551                 (function <)))
552         (nov (concat dir "/.nov"))
553         (nov-buffer (get-buffer-create "*nov*"))
554         nov-line chars)
555     (if files
556         (setq nnml-group-alist 
557               (cons (list (nnmail-replace-chars-in-string 
558                            (substring (expand-file-name dir)
559                                       (length (expand-file-name 
560                                                nnml-directory)))
561                            ?/ ?.)
562                           (cons (car files)
563                                 (let ((f files))
564                                   (while (cdr f) (setq f (cdr f)))
565                                   (car f))))
566                     nnml-group-alist)))
567     (if files
568         (save-excursion
569           (set-buffer nntp-server-buffer)
570           (if (file-exists-p nov)
571               (delete-file nov))
572           (save-excursion
573             (set-buffer nov-buffer)
574             (buffer-disable-undo (current-buffer))
575             (erase-buffer))
576           (while files
577             (erase-buffer)
578             (insert-file-contents (concat dir "/" (int-to-string (car files))))
579             (goto-char 1)
580             (narrow-to-region 1 (save-excursion (search-forward "\n\n" nil t)
581                                                 (setq chars (- (point-max) 
582                                                                (point)))
583                                                 (point)))
584             (setq nov-line (nnml-make-nov-line chars))
585             (save-excursion
586               (set-buffer nov-buffer)
587               (goto-char (point-max))
588               (insert (int-to-string (car files)) nov-line))
589             (widen)
590             (setq files (cdr files)))
591           (save-excursion
592             (set-buffer nov-buffer)
593             (write-region 1 (point-max) (expand-file-name nov) nil
594                           'nomesg)
595             (kill-buffer (current-buffer)))))
596     (nnmail-save-active nnml-group-alist nnml-active-file)))
597
598 (defun nnml-nov-delete-article (group article)
599   (save-excursion
600     (set-buffer (nnml-open-nov group))
601     (goto-char 1)
602     (if (re-search-forward (concat "^" (int-to-string article) "\t"))
603         (delete-region (match-beginning 0) (progn (forward-line 1) (point))))
604     t))
605
606 (provide 'nnml)
607
608 ;;; nnml.el ends here