Merged the change of elmo-mark branch at 2003-08-30.
[elisp/wanderlust.git] / elmo / elmo-maildir.el
1 ;;; elmo-maildir.el --- Maildir interface for ELMO.
2
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
4
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
7
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
9
10 ;; This program 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 ;; This program 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
26 ;;; Commentary:
27 ;;
28
29 ;;; Code:
30 ;;
31
32 (eval-when-compile (require 'cl))
33
34 (require 'elmo-util)
35 (require 'elmo)
36 (require 'elmo-map)
37
38 (defcustom elmo-maildir-folder-path "~/Maildir"
39   "*Maildir folder path."
40   :type 'directory
41   :group 'elmo)
42
43 ;;; ELMO Maildir folder
44 (eval-and-compile
45   (luna-define-class elmo-maildir-folder
46                      (elmo-map-folder)
47                      (directory unread-locations
48                                 flagged-locations
49                                 answered-locations))
50   (luna-define-internal-accessors 'elmo-maildir-folder))
51
52 (luna-define-method elmo-folder-initialize ((folder
53                                              elmo-maildir-folder)
54                                             name)
55   (if (file-name-absolute-p name)
56       (elmo-maildir-folder-set-directory-internal
57        folder
58        (expand-file-name name))
59     (elmo-maildir-folder-set-directory-internal
60      folder
61      (expand-file-name
62       name
63       elmo-maildir-folder-path)))
64   folder)
65
66 (luna-define-method elmo-folder-expand-msgdb-path ((folder
67                                                     elmo-maildir-folder))
68   (expand-file-name
69    (elmo-replace-string-as-filename
70     (elmo-maildir-folder-directory-internal folder))
71    (expand-file-name
72     "maildir"
73     elmo-msgdb-directory)))
74
75 (defun elmo-maildir-message-file-name (folder location)
76   "Get a file name of the message from FOLDER which corresponded to
77 LOCATION."
78   (let ((file (file-name-completion
79                location
80                (expand-file-name
81                 "cur"
82                 (elmo-maildir-folder-directory-internal folder)))))
83     (if file
84         (expand-file-name
85          (if (eq file t) location file)
86          (expand-file-name
87           "cur"
88           (elmo-maildir-folder-directory-internal folder))))))
89
90 (defsubst elmo-maildir-list-location (dir &optional child-dir)
91   (let* ((cur-dir (expand-file-name (or child-dir "cur") dir))
92          (cur (directory-files cur-dir
93                                nil "^[^.].*$" t))
94          unread-locations flagged-locations answered-locations
95          seen flagged answered sym locations)
96     (setq locations
97           (mapcar
98            (lambda (x)
99              (if (string-match "^\\([^:]+\\):\\([^:]+\\)$" x)
100                  (progn
101                    (setq seen nil answered nil flagged nil)
102                    (save-match-data
103                      (cond
104                       ((string-match "F" (elmo-match-string 2 x))
105                        (setq flagged t))
106                       ((string-match "R" (elmo-match-string 2 x))
107                        (setq answered t))
108                       ((string-match "S" (elmo-match-string 2 x))
109                        (setq seen t))))
110                    (setq sym (elmo-match-string 1 x))
111                    (cond
112                     (flagged (setq flagged-locations
113                                    (cons sym flagged-locations)))
114                     (answered (setq answered-locations
115                                     (cons sym answered-locations)))
116                     (seen)
117                     (t
118                      (setq unread-locations (cons sym unread-locations))))
119                    sym)
120                x))
121            cur))
122     (list locations unread-locations flagged-locations answered-locations)))
123
124 (luna-define-method elmo-map-folder-list-message-locations
125   ((folder elmo-maildir-folder))
126   (elmo-maildir-update-current folder)
127   (let ((locs (elmo-maildir-list-location
128                (elmo-maildir-folder-directory-internal folder))))
129     ;; 0: locations, 1: unread-locs, 2: flagged-locs 3: answered-locs
130     (elmo-maildir-folder-set-unread-locations-internal folder (nth 1 locs))
131     (elmo-maildir-folder-set-flagged-locations-internal folder (nth 2 locs))
132     (elmo-maildir-folder-set-answered-locations-internal folder (nth 3 locs))
133     (nth 0 locs)))
134
135 (luna-define-method elmo-map-folder-list-unreads
136   ((folder elmo-maildir-folder))
137   (elmo-maildir-folder-unread-locations-internal folder))
138
139 (luna-define-method elmo-map-folder-list-importants
140   ((folder elmo-maildir-folder))
141   (elmo-maildir-folder-flagged-locations-internal folder))
142
143 (luna-define-method elmo-map-folder-list-answereds
144   ((folder elmo-maildir-folder))
145   (elmo-maildir-folder-answered-locations-internal folder))
146
147 (luna-define-method elmo-folder-msgdb-create 
148   ((folder elmo-maildir-folder) numbers flag-table)
149   (let* ((unread-list (elmo-maildir-folder-unread-locations-internal folder))
150          (flagged-list (elmo-maildir-folder-flagged-locations-internal folder))
151          (answered-list (elmo-maildir-folder-answered-locations-internal
152                          folder))
153          (len (length numbers))
154          (new-msgdb (elmo-make-msgdb))
155          (i 0)
156          entity message-id flag
157          file location pair mark cache-status file-flag)
158     (message "Creating msgdb...")
159     (dolist (number numbers)
160       (setq location (elmo-map-message-location folder number))
161       (setq entity
162             (elmo-msgdb-create-overview-entity-from-file
163              number
164              (setq file
165                    (elmo-maildir-message-file-name folder location))))
166       (when entity
167         (setq message-id (elmo-message-entity-field
168                           entity 'message-id)
169               ;; Precede flag-table to file-info.
170               flag (elmo-flag-table-get flag-table message-id)
171               file-flag nil
172               mark nil
173               cache-status
174               (elmo-file-cache-status (elmo-file-cache-get message-id)))
175         
176         ;; Already flagged on filename (precede it to flag-table).
177         (cond
178          ((member location flagged-list)
179           (setq file-flag 'important
180                 mark elmo-msgdb-important-mark))
181          ((member location answered-list)
182           (setq file-flag 'answered
183                 mark (elmo-msgdb-mark 'answered cache-status)))
184          ((member location unread-list)
185           (setq file-flag 'unread
186                 mark (elmo-msgdb-mark 'unread cache-status)))
187          (t (setq file-flag 'read)))
188
189         ;; Set mark according to flag-table if file status is unread or read.
190         (when (or (eq file-flag 'read)
191                   (eq file-flag 'unread))
192           ;; 
193           (unless (eq 'read flag)
194             (setq mark (elmo-msgdb-mark flag cache-status 'new)))
195           ;; Update filename's info portion according to the flag-table.
196           (cond
197            ((and (or (eq flag 'important)
198                      (setq mark (elmo-msgdb-global-mark-get
199                                  (elmo-message-entity-field
200                                   entity 'message-id))))
201                  (not (eq file-flag 'important)))
202             (elmo-maildir-set-mark file ?F)
203             ;; Delete from unread location list.
204             (elmo-maildir-folder-set-unread-locations-internal
205              folder
206              (delete location
207                      (elmo-maildir-folder-unread-locations-internal
208                       folder)))
209             ;; Append to flagged location list.
210             (elmo-maildir-folder-set-flagged-locations-internal
211              folder
212              (cons location
213                    (elmo-maildir-folder-flagged-locations-internal
214                     folder))))
215            ((and (eq flag 'answered)
216                  (not (eq file-flag 'answered)))
217             (elmo-maildir-set-mark file ?R)
218             ;; Delete from unread locations.
219             (elmo-maildir-folder-set-unread-locations-internal
220              folder
221              (delete location
222                      (elmo-maildir-folder-unread-locations-internal folder)))
223             ;; Append to answered location list.
224             (elmo-maildir-folder-set-answered-locations-internal
225              folder
226              (cons location
227                    (elmo-maildir-folder-answered-locations-internal folder))))
228            ((and (eq flag 'read)
229                  (not (eq file-flag 'read)))
230             (elmo-maildir-set-mark file ?S)
231             ;; Delete from unread locations.
232             (elmo-maildir-folder-set-unread-locations-internal
233              folder
234              (delete location
235                      (elmo-maildir-folder-unread-locations-internal
236                       folder))))))
237         (elmo-msgdb-append-entity new-msgdb entity mark)
238         (when (> len elmo-display-progress-threshold)
239           (setq i (1+ i))
240           (elmo-display-progress
241            'elmo-maildir-msgdb-create "Creating msgdb..."
242            (/ (* i 100) len)))))
243     (message "Creating msgdb...done")
244     (elmo-msgdb-sort-by-date new-msgdb)))
245
246 (defun elmo-maildir-cleanup-temporal (dir)
247   ;; Delete files in the tmp dir which are not accessed
248   ;; for more than 36 hours.
249   (let ((cur-time (current-time))
250         (count 0)
251         last-accessed)
252     (mapcar (function
253              (lambda (file)
254                (setq last-accessed (nth 4 (file-attributes file)))
255                (when (or (> (- (car cur-time)(car last-accessed)) 1)
256                          (and (eq (- (car cur-time)(car last-accessed)) 1)
257                               (> (- (cadr cur-time)(cadr last-accessed))
258                                  64064))) ; 36 hours.
259                  (message "Maildir: %d tmp file(s) are cleared."
260                           (setq count (1+ count)))
261                  (delete-file file))))
262             (directory-files (expand-file-name "tmp" dir)
263                              t ; full
264                              "^[^.].*$" t))))
265
266 (defun elmo-maildir-update-current (folder)
267   "Move all new msgs to cur in the maildir."
268   (let* ((maildir (elmo-maildir-folder-directory-internal folder))
269          (news (directory-files (expand-file-name "new"
270                                                   maildir)
271                                 nil
272                                 "^[^.].*$" t)))
273     ;; cleanup tmp directory.
274     (elmo-maildir-cleanup-temporal maildir)
275     ;; move new msgs to cur directory.
276     (while news
277       (rename-file
278        (expand-file-name (car news) (expand-file-name "new" maildir))
279        (expand-file-name (concat
280                           (car news)
281                           (unless (string-match ":2,[A-Z]*$" (car news))
282                             ":2,"))
283                          (expand-file-name "cur" maildir)))
284       (setq news (cdr news)))))
285
286 (defun elmo-maildir-set-mark (filename mark)
287   "Mark the FILENAME file in the maildir.  MARK is a character."
288   (if (string-match "^\\([^:]+:[12],\\)\\(.*\\)$" filename)
289       (let ((flaglist (string-to-char-list (elmo-match-string
290                                             2 filename))))
291         (unless (memq mark flaglist)
292           (setq flaglist (sort (cons mark flaglist) '<))
293           (rename-file filename
294                        (concat (elmo-match-string 1 filename)
295                                (char-list-to-string flaglist)))))
296     ;; Rescue no info file in maildir.
297     (rename-file filename
298                  (concat filename ":2," (char-to-string mark))))
299   t)
300
301 (defun elmo-maildir-delete-mark (filename mark)
302   "Mark the FILENAME file in the maildir.  MARK is a character."
303   (if (string-match "^\\([^:]+:2,\\)\\(.*\\)$" filename)
304       (let ((flaglist (string-to-char-list (elmo-match-string
305                                             2 filename))))
306         (when (memq mark flaglist)
307           (setq flaglist (delq mark flaglist))
308           (rename-file filename
309                        (concat (elmo-match-string 1 filename)
310                                (if flaglist
311                                    (char-list-to-string flaglist))))))))
312
313 (defsubst elmo-maildir-set-mark-msgs (folder locs mark)
314   (dolist (loc locs)
315     (elmo-maildir-set-mark
316      (elmo-maildir-message-file-name folder loc)
317      mark))
318   t)
319
320 (defsubst elmo-maildir-delete-mark-msgs (folder locs mark)
321   (dolist (loc locs)
322     (elmo-maildir-delete-mark
323      (elmo-maildir-message-file-name folder loc)
324      mark))
325   t)
326
327 (luna-define-method elmo-map-folder-mark-as-important ((folder elmo-maildir-folder)
328                                                        locs)
329   (elmo-maildir-set-mark-msgs folder locs ?F))
330   
331 (luna-define-method elmo-map-folder-unmark-important ((folder elmo-maildir-folder)
332                                                       locs)
333   (elmo-maildir-delete-mark-msgs folder locs ?F))
334
335 (luna-define-method elmo-map-folder-mark-as-read ((folder elmo-maildir-folder)
336                                                   locs)
337   (elmo-maildir-set-mark-msgs folder locs ?S))
338
339 (luna-define-method elmo-map-folder-unmark-read ((folder elmo-maildir-folder)
340                                                  locs)
341   (elmo-maildir-delete-mark-msgs folder locs ?S))
342
343 (luna-define-method elmo-map-folder-mark-as-answered ((folder
344                                                        elmo-maildir-folder)
345                                                       locs)
346   (elmo-maildir-set-mark-msgs folder locs ?R))
347
348 (luna-define-method elmo-map-folder-unmark-answered ((folder
349                                                       elmo-maildir-folder)
350                                                      locs)
351   (elmo-maildir-delete-mark-msgs folder locs ?R))
352
353 (luna-define-method elmo-folder-list-subfolders
354   ((folder elmo-maildir-folder) &optional one-level)
355   (let ((prefix (concat (elmo-folder-name-internal folder)
356                         (unless (string= (elmo-folder-prefix-internal folder)
357                                          (elmo-folder-name-internal folder))
358                           elmo-path-sep)))
359         (elmo-list-subdirectories-ignore-regexp
360          "^\\(\\.\\.?\\|cur\\|tmp\\|new\\)$")
361         elmo-have-link-count)
362     (append
363      (list (elmo-folder-name-internal folder))
364      (elmo-mapcar-list-of-list
365       (function (lambda (x) (concat prefix x)))
366       (elmo-list-subdirectories
367        (elmo-maildir-folder-directory-internal folder)
368        ""
369        one-level)))))
370
371 (defvar elmo-maildir-sequence-number-internal 0)
372
373 (static-cond
374  ((>= emacs-major-version 19)
375   (defun elmo-maildir-make-unique-string ()
376     "This function generates a string that can be used as a unique
377 file name for maildir directories."
378      (let ((cur-time (current-time)))
379        (format "%.0f.%d_%d.%s"
380               (+ (* (car cur-time)
381                     (float 65536)) (cadr cur-time))
382               (emacs-pid)
383               (incf elmo-maildir-sequence-number-internal)
384               (system-name)))))
385  ((eq emacs-major-version 18)
386   ;; A fake function for v18
387   (defun elmo-maildir-make-unique-string ()
388     "This function generates a string that can be used as a unique
389 file name for maildir directories."
390     (unless (fboundp 'float-to-string)
391       (load-library "float"))
392     (let ((time (current-time)))
393       (format "%s%d.%d.%s"
394               (substring
395                (float-to-string
396                 (f+ (f* (f (car time))
397                         (f 65536))
398                     (f (cadr time))))
399                0 5)
400               (cadr time)
401               (% (abs (random t)) 10000); dummy pid
402               (system-name))))))
403
404 (defun elmo-maildir-temporal-filename (basedir)
405   (let ((filename (expand-file-name
406                    (concat "tmp/" (elmo-maildir-make-unique-string))
407                    basedir)))
408     (unless (file-exists-p (file-name-directory filename))
409       (make-directory (file-name-directory filename)))
410     (while (file-exists-p filename)
411 ;;; I don't want to wait.
412 ;;;   (sleep-for 2)
413       (setq filename
414             (expand-file-name
415              (concat "tmp/" (elmo-maildir-make-unique-string))
416              basedir)))
417     filename))
418
419 (luna-define-method elmo-folder-append-buffer ((folder elmo-maildir-folder)
420                                                &optional status number)
421   (let ((basedir (elmo-maildir-folder-directory-internal folder))
422         (src-buf (current-buffer))
423         dst-buf filename)
424     (condition-case nil
425         (with-temp-buffer
426           (setq filename (elmo-maildir-temporal-filename basedir))
427           (setq dst-buf (current-buffer))
428           (with-current-buffer src-buf
429             (copy-to-buffer dst-buf (point-min) (point-max)))
430           (as-binary-output-file
431            (write-region (point-min) (point-max) filename nil 'no-msg))
432           ;; add link from new.
433           (elmo-add-name-to-file
434            filename
435            (expand-file-name
436             (concat "new/" (file-name-nondirectory filename))
437             basedir))
438           t)
439       ;; If an error occured, return nil.
440       (error))))
441
442 (luna-define-method elmo-folder-message-file-p ((folder elmo-maildir-folder))
443   t)
444
445 (luna-define-method elmo-message-file-name ((folder elmo-maildir-folder)
446                                             number)
447   (elmo-maildir-message-file-name
448    folder
449    (elmo-map-message-location folder number)))
450
451 (luna-define-method elmo-folder-message-make-temp-file-p
452   ((folder elmo-maildir-folder))
453   t)
454
455 (luna-define-method elmo-folder-message-make-temp-files ((folder
456                                                           elmo-maildir-folder)
457                                                          numbers
458                                                          &optional
459                                                          start-number)
460   (let ((temp-dir (elmo-folder-make-temporary-directory folder))
461         (cur-number (if start-number 0)))
462     (dolist (number numbers)
463       (elmo-copy-file
464        (elmo-message-file-name folder number)
465        (expand-file-name
466         (int-to-string (if start-number (incf cur-number) number))
467         temp-dir)))
468     temp-dir))
469
470 (luna-define-method elmo-folder-append-messages :around
471   ((folder elmo-maildir-folder)
472    src-folder numbers &optional same-number)
473   (if (elmo-folder-message-file-p src-folder)
474       (let ((src-msgdb-exists (not (zerop (elmo-folder-length src-folder))))
475             (dir (elmo-maildir-folder-directory-internal folder))
476             (table (elmo-flag-table-load (elmo-folder-msgdb-path folder)))
477             (succeeds numbers)
478             filename mark flag id)
479         (dolist (number numbers)
480           (setq mark (and src-msgdb-exists
481                           (elmo-message-mark src-folder (car numbers)))
482                 flag (cond
483                       ((null mark) 'read)
484                       ((member mark (elmo-msgdb-answered-marks))
485                        'answered)
486                       ((not (member mark (elmo-msgdb-unread-marks)))
487                        'read))
488                 filename (elmo-maildir-temporal-filename dir))
489           (elmo-copy-file
490            (elmo-message-file-name src-folder number)
491            filename)
492           (elmo-add-name-to-file
493            filename
494            (expand-file-name
495             (concat "new/" (file-name-nondirectory filename))
496             dir))
497           ;; src folder's msgdb is loaded.
498           (when (setq id (and src-msgdb-exists
499                               (elmo-message-field src-folder (car numbers)
500                                                   'message-id)))
501             (elmo-flag-table-set table id flag))
502           (elmo-progress-notify 'elmo-folder-move-messages))
503         (when (elmo-folder-persistent-p folder)
504           (elmo-flag-table-save (elmo-folder-msgdb-path folder) table))
505         succeeds)
506     (luna-call-next-method)))
507
508 (luna-define-method elmo-map-folder-delete-messages
509   ((folder elmo-maildir-folder) locations)
510   (let (file)
511     (dolist (location locations)
512       (setq file (elmo-maildir-message-file-name folder location))
513       (if (and file
514                (file-writable-p file)
515                (not (file-directory-p file)))
516           (delete-file file)))))
517
518 (luna-define-method elmo-map-message-fetch ((folder elmo-maildir-folder)
519                                             location strategy
520                                             &optional section unseen)
521   (let ((file (elmo-maildir-message-file-name folder location)))
522     (when (file-exists-p file)
523       (insert-file-contents-as-binary file))))
524
525 (luna-define-method elmo-folder-exists-p ((folder elmo-maildir-folder))
526   (let ((basedir (elmo-maildir-folder-directory-internal folder)))
527     (and (file-directory-p (expand-file-name "new" basedir))
528          (file-directory-p (expand-file-name "cur" basedir))
529          (file-directory-p (expand-file-name "tmp" basedir)))))
530
531 (luna-define-method elmo-folder-diff ((folder elmo-maildir-folder))
532   (let* ((dir (elmo-maildir-folder-directory-internal folder))
533          (new-len (length (car (elmo-maildir-list-location dir "new"))))
534          (cur-len (length (car (elmo-maildir-list-location dir "cur")))))
535     (cons new-len (+ new-len cur-len))))
536
537 (luna-define-method elmo-folder-creatable-p ((folder elmo-maildir-folder))
538   t)
539
540 (luna-define-method elmo-folder-writable-p ((folder elmo-maildir-folder))
541   t)
542
543 (luna-define-method elmo-folder-create ((folder elmo-maildir-folder))
544   (let ((basedir (elmo-maildir-folder-directory-internal folder)))
545     (condition-case nil
546         (progn
547           (dolist (dir '("." "new" "cur" "tmp"))
548             (setq dir (expand-file-name dir basedir))
549             (or (file-directory-p dir)
550                 (progn
551                   (elmo-make-directory dir)
552                   (set-file-modes dir 448))))
553           t)
554       (error))))
555
556 (luna-define-method elmo-folder-delete ((folder elmo-maildir-folder))
557   (let ((msgs (and (elmo-folder-exists-p folder)
558                    (elmo-folder-list-messages folder))))
559     (when (yes-or-no-p (format "%sDelete msgdb and substance of \"%s\"? "
560                                (if (> (length msgs) 0)
561                                    (format "%d msg(s) exists. " (length msgs))
562                                  "")
563                                (elmo-folder-name-internal folder)))
564       (let ((basedir (elmo-maildir-folder-directory-internal folder)))
565         (condition-case nil
566             (let ((tmp-files (directory-files
567                               (expand-file-name "tmp" basedir)
568                               t "[^.].*")))
569               ;; Delete files in tmp.
570               (dolist (file tmp-files)
571                 (delete-file file))
572               (dolist (dir '("new" "cur" "tmp" "."))
573                 (setq dir (expand-file-name dir basedir))
574                 (if (not (file-directory-p dir))
575                     (error nil)
576                   (elmo-delete-directory dir t))))
577           (error nil)))
578       (elmo-msgdb-delete-path folder)
579       t)))
580
581 (luna-define-method elmo-folder-rename-internal ((folder elmo-maildir-folder)
582                                                  new-folder)
583   (let* ((old (elmo-maildir-folder-directory-internal folder))
584          (new (elmo-maildir-folder-directory-internal new-folder))
585          (new-dir (directory-file-name (file-name-directory new))))
586     (unless (file-directory-p old)
587       (error "No such directory: %s" old))
588     (when (file-exists-p new)
589       (error "Already exists directory: %s" new))
590     (unless (file-directory-p new-dir)
591       (elmo-make-directory new-dir))
592     (rename-file old new)
593     t))
594
595 (require 'product)
596 (product-provide (provide 'elmo-maildir) (require 'elmo-version))
597
598 ;;; elmo-maildir.el ends here