* mmelmo-imap4-2.el (mmelmo-imap4-get-mime-entity):
[elisp/wanderlust.git] / elmo / elmo-maildir.el
1 ;;; elmo-maildir.el -- Maildir interface for ELMO.
2
3 ;; Copyright 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 (require 'elmo-util)
34 (require 'elmo-localdir)
35
36 (defvar elmo-maildir-sequence-number-internal 0
37   "Sequence number for the pid part of unique filename.
38 This variable should not be used in elsewhere.")
39
40 (defsubst elmo-maildir-get-folder-directory (spec)
41   (if (file-name-absolute-p (nth 1 spec))
42       (nth 1 spec) ; already full path.
43     (expand-file-name (nth 1 spec)
44                       elmo-maildir-folder-path)))
45
46 (defun elmo-maildir-number-to-filename (dir number loc-alist)
47   (let ((location (cdr (assq number loc-alist))))
48     (and location (elmo-maildir-get-filename location dir))))
49
50 (defun elmo-maildir-get-filename (location dir)
51   "Get a filename that is corresponded to LOCATION in DIR."
52   (expand-file-name
53    (let ((file (file-name-completion (symbol-name location)
54                                      (expand-file-name "cur" dir))))
55      (if (eq file t) location file))
56    (expand-file-name "cur" dir)))
57
58 (defsubst elmo-maildir-list-location (dir &optional child-dir)
59   (let* ((cur-dir (expand-file-name (or child-dir "cur") dir))
60          (cur (directory-files cur-dir
61                                nil "^[^.].*$" t))
62          seen-list seen sym list)
63     (setq list
64           (mapcar
65            (lambda (x)
66              (if (string-match "^\\([^:]+\\):\\([^:]+\\)$" x)
67                  (progn
68                    (setq seen nil)
69                    (save-match-data
70                      (if (string-match
71                           "S"
72                           (elmo-match-string 2 x))
73                          (setq seen t)))
74                    (setq sym (intern (elmo-match-string 1 x)))
75                    (if seen
76                        (setq seen-list (cons sym seen-list)))
77                    sym)
78                (intern x)))
79            cur))
80     (cons list seen-list)))
81
82 (defun elmo-maildir-msgdb-create-entity (dir number loc-alist)
83   (elmo-localdir-msgdb-create-overview-entity-from-file
84    number
85    (elmo-maildir-number-to-filename dir number loc-alist)))
86
87 (defun elmo-maildir-cleanup-temporal (dir)
88   ;; Delete files in the tmp dir which are not accessed
89   ;; for more than 36 hours.
90   (let ((cur-time (current-time))
91         (count 0)
92         last-accessed)
93     (mapcar (function
94              (lambda (file)
95                (setq last-accessed (nth 4 (file-attributes file)))
96                (when (or (> (- (car cur-time)(car last-accessed)) 1)
97                          (and (eq (- (car cur-time)(car last-accessed)) 1)
98                               (> (- (cadr cur-time)(cadr last-accessed))
99                                  64064))) ; 36 hours.
100                  (message "Maildir: %d tmp file(s) are cleared."
101                           (setq count (1+ count)))
102                  (delete-file file))))
103             (directory-files (expand-file-name "tmp" dir)
104                              t ; full
105                              "^[^.].*$" t))))
106
107 (defun elmo-maildir-update-current (spec)
108   "Move all new msgs to cur in the maildir"
109   (let* ((maildir (elmo-maildir-get-folder-directory spec))
110          (news (directory-files (expand-file-name "new"
111                                                   maildir)
112                                 nil
113                                 "^[^.].*$" t)))
114     ;; cleanup tmp directory.
115     (elmo-maildir-cleanup-temporal maildir)
116     ;; move new msgs to cur directory.
117     (mapcar (lambda (x)
118               (rename-file
119                (expand-file-name x (expand-file-name "new" maildir))
120                (expand-file-name (concat x ":2,")
121                                  (expand-file-name "cur" maildir))))
122             news)))
123
124 (defun elmo-maildir-set-mark (filename mark)
125   "Mark the file in the maildir. MARK is a character."
126   (if (string-match "^\\([^:]+:2,\\)\\(.*\\)$" filename)
127       (let ((flaglist (string-to-char-list (elmo-match-string
128                                             2 filename))))
129         (unless (memq mark flaglist)
130           (setq flaglist (sort (cons mark flaglist) '<))
131           (rename-file filename
132                        (concat (elmo-match-string 1 filename)
133                                (char-list-to-string flaglist)))))))
134
135 (defun elmo-maildir-delete-mark (filename mark)
136   "Mark the file in the maildir. MARK is a character."
137   (if (string-match "^\\([^:]+:2,\\)\\(.*\\)$" filename)
138       (let ((flaglist (string-to-char-list (elmo-match-string
139                                             2 filename))))
140         (when (memq mark flaglist)
141           (setq flaglist (delq mark flaglist))
142           (rename-file filename
143                        (concat (elmo-match-string 1 filename)
144                                (if flaglist
145                                    (char-list-to-string flaglist))))))))
146
147 (defsubst elmo-maildir-set-mark-msgs (spec mark msgs msgdb)
148   (let ((dir (elmo-maildir-get-folder-directory spec))
149         (locs (if msgdb
150                   (elmo-msgdb-get-location msgdb)
151                 (elmo-msgdb-location-load (elmo-msgdb-expand-path nil spec))))
152         file)
153     (while msgs
154       (if (setq file (elmo-maildir-number-to-filename dir (car msgs) locs))
155           (elmo-maildir-set-mark file mark))
156       (setq msgs (cdr msgs)))))
157
158 (defsubst elmo-maildir-delete-mark-msgs (spec mark msgs msgdb)
159   (let ((dir (elmo-maildir-get-folder-directory spec))
160         (locs (if msgdb
161                   (elmo-msgdb-get-location msgdb)
162                 (elmo-msgdb-location-load (elmo-msgdb-expand-path nil spec))))
163         file)
164     (while msgs
165       (if (setq file (elmo-maildir-number-to-filename dir (car msgs) locs))
166           (elmo-maildir-delete-mark file mark))
167       (setq msgs (cdr msgs)))))
168
169 (defun elmo-maildir-mark-as-important (spec msgs &optional msgdb)
170   (elmo-maildir-set-mark-msgs spec ?F msgs msgdb))
171   
172 (defun elmo-maildir-unmark-important (spec msgs &optional msgdb)
173   (elmo-maildir-delete-mark-msgs spec ?F msgs msgdb))
174
175 (defun elmo-maildir-mark-as-read (spec msgs &optional msgdb)
176   (elmo-maildir-set-mark-msgs spec ?S msgs msgdb))
177
178 (defun elmo-maildir-mark-as-unread (spec msgs &optional msgdb)
179   (elmo-maildir-delete-mark-msgs spec ?S msgs msgdb))
180
181 (defun elmo-maildir-msgdb-create (spec numlist new-mark
182                                        already-mark seen-mark
183                                        important-mark
184                                        seen-list
185                                        &optional msgdb)
186   (when numlist
187     (let* ((dir (elmo-maildir-get-folder-directory spec))
188            (loc-alist (if msgdb (elmo-msgdb-get-location msgdb)
189                         (elmo-msgdb-location-load (elmo-msgdb-expand-path
190                                                    nil spec))))
191            (loc-seen (elmo-maildir-list-location dir))
192            (loc-list  (car loc-seen))
193            (seen-list (cdr loc-seen))
194            overview number-alist mark-alist entity
195            i percent num location pair)
196       (setq num (length numlist))
197       (setq i 0)
198       (message "Creating msgdb...")
199       (while numlist
200         (setq entity
201               (elmo-maildir-msgdb-create-entity
202                dir (car numlist) loc-alist))
203         (if (null entity)
204             ()
205           (setq overview
206                 (elmo-msgdb-append-element
207                  overview entity))
208           (setq number-alist
209                 (elmo-msgdb-number-add number-alist
210                                        (elmo-msgdb-overview-entity-get-number
211                                         entity)
212                                        (elmo-msgdb-overview-entity-get-id
213                                         entity)))
214           (setq location (cdr (assq (car numlist) loc-alist)))
215           (unless (member location seen-list)
216             (setq mark-alist
217                   (elmo-msgdb-mark-append
218                    mark-alist
219                    (elmo-msgdb-overview-entity-get-number
220                     entity)
221                    (or (elmo-msgdb-global-mark-get
222                         (elmo-msgdb-overview-entity-get-id
223                          entity))
224                        new-mark)))))
225         (when (> num elmo-display-progress-threshold)
226           (setq i (1+ i))
227           (setq percent (/ (* i 100) num))
228           (elmo-display-progress
229            'elmo-maildir-msgdb-create "Creating msgdb..."
230            percent))
231         (setq numlist (cdr numlist)))
232       (message "Creating msgdb...done.")
233       (elmo-msgdb-sort-by-date
234        (list overview number-alist mark-alist loc-alist)))))
235
236 (defalias 'elmo-maildir-msgdb-create-as-numlist 'elmo-maildir-msgdb-create)
237
238 (defun elmo-maildir-list-folders (spec &optional hierarchy)
239   (let ((elmo-localdir-folder-path elmo-maildir-folder-path)
240         (elmo-localdir-list-folders-spec-string ".")
241         (elmo-localdir-list-folders-filter-regexp
242          "^\\(\\.\\.?\\|cur\\|tmp\\|new\\)$")
243         elmo-have-link-count folders)
244     (setq folders (elmo-localdir-list-folders spec hierarchy))
245     (if (eq (length (nth 1 spec)) 0) ; top
246         (setq folders (append
247                        (list (concat elmo-localdir-list-folders-spec-string
248                                      (nth 1 spec)))
249                        folders)))
250     (elmo-delete-if
251      (function (lambda (folder)
252                  (not (or (listp folder) (elmo-folder-exists-p folder)))))
253      folders)))
254
255 (static-cond
256  ((>= emacs-major-version 19)
257   (defun elmo-maildir-make-unique-string ()
258     "This function generates a string that can be used as a unique
259 file name for maildir directories."
260      (let ((cur-time (current-time)))
261        (format "%.0f.%d_%d.%s"
262               (+ (* (car cur-time)
263                     (float 65536)) (cadr cur-time))
264               (emacs-pid)
265               (incf elmo-maildir-sequence-number-internal)
266               (system-name)))))
267  ((eq emacs-major-version 18)
268   ;; A fake function for v18
269   (defun elmo-maildir-make-unique-string ()
270     "This function generates a string that can be used as a unique
271 file name for maildir directories."
272     (unless (fboundp 'float-to-string)
273       (load-library "float"))
274     (let ((time (current-time)))
275       (format "%s%d.%d.%s"
276               (substring
277                (float-to-string
278                 (f+ (f* (f (car time))
279                         (f 65536))
280                     (f (cadr time))))
281                0 5)
282               (cadr time)
283               (% (abs (random t)) 10000); dummy pid
284               (system-name))))))
285
286 (defun elmo-maildir-temporal-filename (basedir)
287   (let ((filename (expand-file-name
288                    (concat "tmp/" (elmo-maildir-make-unique-string))
289                    basedir)))
290     (unless (file-exists-p (file-name-directory filename))
291       (make-directory (file-name-directory filename)))
292     (while (file-exists-p filename)
293       ;; (sleep-for 2) ; I don't want to wait.
294       (setq filename
295             (expand-file-name
296              (concat "tmp/" (elmo-maildir-make-unique-string))
297              basedir)))
298     filename))
299
300 (defun elmo-maildir-append-msg (spec string &optional msg no-see)
301   (let ((basedir (elmo-maildir-get-folder-directory spec))
302         filename)
303     (condition-case nil
304         (with-temp-buffer
305           (setq filename (elmo-maildir-temporal-filename basedir))
306           (insert string)
307           (as-binary-output-file
308            (write-region (point-min) (point-max) filename nil 'no-msg))
309           ;; add link from new.
310           (elmo-add-name-to-file
311            filename
312            (expand-file-name
313             (concat "new/" (file-name-nondirectory filename))
314             basedir))
315           t)
316       ;; If an error occured, return nil.
317       (error))))
318
319 (defun elmo-maildir-delete-msg (spec number loc-alist)
320   (let ((dir (elmo-maildir-get-folder-directory spec))
321         file)
322     (setq file (elmo-maildir-number-to-filename dir number loc-alist))
323     (if (and (file-writable-p file)
324              (not (file-directory-p file)))
325         (progn (delete-file file)
326                t))))
327
328 (defun elmo-maildir-read-msg (spec number outbuf &optional msgdb)
329   (save-excursion
330     (let* ((loc-alist (if msgdb (elmo-msgdb-get-location msgdb)
331                         (elmo-msgdb-location-load (elmo-msgdb-expand-path
332                                                    nil spec))))
333            (dir (elmo-maildir-get-folder-directory spec))
334            (file (elmo-maildir-number-to-filename dir number loc-alist)))
335       (set-buffer outbuf)
336       (erase-buffer)
337       (when (file-exists-p file)
338         (as-binary-input-file (insert-file-contents file))
339         (elmo-delete-cr-get-content-type)))))
340
341 (defun elmo-maildir-delete-msgs (spec msgs &optional msgdb)
342   (let ((loc-alist (if msgdb (elmo-msgdb-get-location msgdb)
343                      (elmo-msgdb-location-load (elmo-msgdb-expand-path
344                                                 nil spec)))))
345     (mapcar '(lambda (msg) (elmo-maildir-delete-msg spec msg
346                                                     loc-alist))
347             msgs)))
348
349 (defsubst elmo-maildir-list-folder-subr (spec &optional nonsort)
350   (let* ((dir (elmo-maildir-get-folder-directory spec))
351          (flist (elmo-list-folder-by-location
352                  spec
353                  (car (elmo-maildir-list-location dir))))
354          (news (car (elmo-maildir-list-location dir "new"))))
355     (if nonsort
356         (cons (+ (or (elmo-max-of-list flist) 0) (length news))
357               (+ (length flist) (length news)))
358       (sort flist '<))))
359
360 (defun elmo-maildir-list-folder (spec)
361   (elmo-maildir-update-current spec)
362   (let ((killed (and elmo-use-killed-list
363                      (elmo-msgdb-killed-list-load
364                       (elmo-msgdb-expand-path nil spec))))
365         numbers)
366     (setq numbers (elmo-maildir-list-folder-subr spec))
367     (if killed
368         (delq nil
369               (mapcar (lambda (number)
370                         (unless (memq number killed) number))
371                       numbers))
372       numbers)))
373
374 (defun elmo-maildir-max-of-folder (spec)
375   (elmo-maildir-list-folder-subr spec t))
376
377 (defalias 'elmo-maildir-check-validity 'elmo-localdir-check-validity)
378
379 (defalias 'elmo-maildir-sync-validity  'elmo-localdir-sync-validity)
380
381 (defun elmo-maildir-folder-exists-p (spec)
382   (let ((basedir (elmo-maildir-get-folder-directory spec)))
383     (and (file-directory-p (expand-file-name "new" basedir))
384          (file-directory-p (expand-file-name "cur" basedir))
385          (file-directory-p (expand-file-name "tmp" basedir)))))
386
387 (defun elmo-maildir-folder-creatable-p (spec)
388   t)
389
390 (defun elmo-maildir-create-folder (spec)
391   (let ((basedir (elmo-maildir-get-folder-directory spec)))
392     (condition-case nil
393         (progn
394           (mapcar (function (lambda (dir)
395                               (setq dir (expand-file-name dir basedir))
396                               (or (file-directory-p dir)
397                                   (progn
398                                     (elmo-make-directory dir)
399                                     (set-file-modes dir 448)))))
400                   '("." "new" "cur" "tmp"))
401           t)
402       (error))))
403
404 (defun elmo-maildir-delete-folder (spec)
405   (let ((basedir (elmo-maildir-get-folder-directory spec)))
406     (condition-case nil
407         (let ((tmp-files (directory-files
408                           (expand-file-name "tmp" basedir)
409                           t "[^.].*")))
410           ;; Delete files in tmp.
411           (and tmp-files (mapcar 'delete-file tmp-files))
412           (mapcar
413            (function
414             (lambda (dir)
415               (setq dir (expand-file-name dir basedir))
416               (if (not (file-directory-p dir))
417                   (error nil)
418                 (elmo-delete-directory dir t))))
419            '("new" "cur" "tmp" "."))
420           t)
421       (error nil))))
422
423 (defun elmo-maildir-search (spec condition &optional from-msgs msgdb)
424   (save-excursion
425     (let* ((msgs (or from-msgs (elmo-maildir-list-folder spec)))
426            (loc-alist (if msgdb (elmo-msgdb-get-location msgdb)
427                         (elmo-msgdb-location-load (elmo-msgdb-expand-path
428                                                    nil spec))))
429            (dir (elmo-maildir-get-folder-directory spec))
430            (i 0)
431            case-fold-search ret-val
432            percent num
433            (num (length msgs))
434            msg-num)
435       (while msgs
436         (setq msg-num (car msgs))
437         (if (elmo-file-field-condition-match
438              (elmo-maildir-number-to-filename
439               dir (car msgs) loc-alist)
440              condition)
441             (setq ret-val (append ret-val (list msg-num))))
442         (setq i (1+ i))
443         (setq percent (/ (* i 100) num))
444         (elmo-display-progress
445          'elmo-maildir-search "Searching..."
446          percent)
447         (setq msgs (cdr msgs)))
448       ret-val)))
449
450 ;;; (maildir) -> maildir
451 (defun elmo-maildir-copy-msgs (dst-spec msgs src-spec
452                                         &optional loc-alist same-number)
453   (let (srcfile)
454     (while msgs
455       (setq srcfile
456             (elmo-maildir-get-msg-filename src-spec (car msgs) loc-alist))
457       (elmo-copy-file
458        ;; src file
459        srcfile
460        ;; dst file
461        (expand-file-name
462         (file-name-nondirectory srcfile)
463         (concat (elmo-maildir-get-folder-directory dst-spec) "/cur")))
464       (setq msgs (cdr msgs))))
465   t)
466
467 (defun elmo-maildir-use-cache-p (spec number)
468   nil)
469
470 (defun elmo-maildir-local-file-p (spec number)
471   t)
472
473 (defun elmo-maildir-get-msg-filename (spec number &optional loc-alist)
474   (elmo-maildir-number-to-filename
475    (elmo-maildir-get-folder-directory spec)
476    number (or loc-alist (elmo-msgdb-location-load
477                          (elmo-msgdb-expand-path
478                           nil spec)))))
479
480 (defalias 'elmo-maildir-sync-number-alist
481   'elmo-generic-sync-number-alist)
482 (defalias 'elmo-maildir-list-folder-unread
483   'elmo-generic-list-folder-unread)
484 (defalias 'elmo-maildir-list-folder-important
485   'elmo-generic-list-folder-important)
486 (defalias 'elmo-maildir-commit 'elmo-generic-commit)
487
488 (provide 'elmo-maildir)
489
490 ;;; elmo-maildir.el ends here