1 ;;; elmo-maildir.el --- Maildir interface for ELMO.
3 ;; Copyright (C) 1998,1999,2000 Yuuichi Teranishi <teranisi@gohome.org>
5 ;; Author: Yuuichi Teranishi <teranisi@gohome.org>
6 ;; Keywords: mail, net news
8 ;; This file is part of ELMO (Elisp Library for Message Orchestration).
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)
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.
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.
32 (eval-when-compile (require 'cl))
38 (defcustom elmo-maildir-folder-path "~/Maildir"
39 "*Maildir folder path."
43 ;;; ELMO Maildir folder
45 (luna-define-class elmo-maildir-folder
47 (directory unread-locations
50 (luna-define-internal-accessors 'elmo-maildir-folder))
52 (luna-define-method elmo-folder-initialize ((folder
55 (if (file-name-absolute-p name)
56 (elmo-maildir-folder-set-directory-internal
58 (expand-file-name name))
59 (elmo-maildir-folder-set-directory-internal
63 elmo-maildir-folder-path)))
66 (luna-define-method elmo-folder-expand-msgdb-path ((folder
69 (elmo-replace-string-as-filename
70 (elmo-maildir-folder-directory-internal folder))
73 elmo-msgdb-directory)))
75 (defun elmo-maildir-message-file-name (folder location)
76 "Get a file name of the message from FOLDER which corresponded to
78 (let ((file (file-name-completion
82 (elmo-maildir-folder-directory-internal folder)))))
85 (if (eq file t) location file)
88 (elmo-maildir-folder-directory-internal folder))))))
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
94 unread-locations flagged-locations answered-locations
95 seen flagged answered sym locations)
99 (if (string-match "^\\([^:]+\\):\\([^:]+\\)$" x)
101 (setq seen nil answered nil flagged nil)
104 ((string-match "F" (elmo-match-string 2 x))
106 ((string-match "R" (elmo-match-string 2 x))
108 ((string-match "S" (elmo-match-string 2 x))
110 (setq sym (elmo-match-string 1 x))
112 (flagged (setq flagged-locations
113 (cons sym flagged-locations)))
114 (answered (setq answered-locations
115 (cons sym answered-locations)))
118 (setq unread-locations (cons sym unread-locations))))
122 (list locations unread-locations flagged-locations answered-locations)))
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))
135 (luna-define-method elmo-map-folder-list-unreads
136 ((folder elmo-maildir-folder))
137 (elmo-maildir-folder-unread-locations-internal folder))
139 (luna-define-method elmo-map-folder-list-importants
140 ((folder elmo-maildir-folder))
141 (elmo-maildir-folder-flagged-locations-internal folder))
143 (luna-define-method elmo-map-folder-list-answereds
144 ((folder elmo-maildir-folder))
145 (elmo-maildir-folder-answered-locations-internal folder))
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
153 (len (length numbers))
154 (new-msgdb (elmo-make-msgdb))
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))
162 (elmo-msgdb-create-overview-entity-from-file
165 (elmo-maildir-message-file-name folder location))))
167 (setq message-id (elmo-message-entity-field
169 ;; Precede flag-table to file-info.
170 flag (elmo-flag-table-get flag-table message-id)
174 (elmo-file-cache-status (elmo-file-cache-get message-id)))
176 ;; Already flagged on filename (precede it to flag-table).
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)))
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))
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.
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
207 (elmo-maildir-folder-unread-locations-internal
209 ;; Append to flagged location list.
210 (elmo-maildir-folder-set-flagged-locations-internal
213 (elmo-maildir-folder-flagged-locations-internal
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
222 (elmo-maildir-folder-unread-locations-internal folder)))
223 ;; Append to answered location list.
224 (elmo-maildir-folder-set-answered-locations-internal
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
235 (elmo-maildir-folder-unread-locations-internal
237 (elmo-msgdb-append-entity new-msgdb entity mark)
238 (when (> len elmo-display-progress-threshold)
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)))
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))
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))
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)
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"
273 ;; cleanup tmp directory.
274 (elmo-maildir-cleanup-temporal maildir)
275 ;; move new msgs to cur directory.
278 (expand-file-name (car news) (expand-file-name "new" maildir))
279 (expand-file-name (concat
281 (unless (string-match ":2,[A-Z]*$" (car news))
283 (expand-file-name "cur" maildir)))
284 (setq news (cdr news)))))
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
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))))
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
306 (when (memq mark flaglist)
307 (setq flaglist (delq mark flaglist))
308 (rename-file filename
309 (concat (elmo-match-string 1 filename)
311 (char-list-to-string flaglist))))))))
313 (defsubst elmo-maildir-set-mark-msgs (folder locs mark)
315 (elmo-maildir-set-mark
316 (elmo-maildir-message-file-name folder loc)
320 (defsubst elmo-maildir-delete-mark-msgs (folder locs mark)
322 (elmo-maildir-delete-mark
323 (elmo-maildir-message-file-name folder loc)
327 (luna-define-method elmo-map-folder-mark-as-important ((folder elmo-maildir-folder)
329 (elmo-maildir-set-mark-msgs folder locs ?F))
331 (luna-define-method elmo-map-folder-unmark-important ((folder elmo-maildir-folder)
333 (elmo-maildir-delete-mark-msgs folder locs ?F))
335 (luna-define-method elmo-map-folder-mark-as-read ((folder elmo-maildir-folder)
337 (elmo-maildir-set-mark-msgs folder locs ?S))
339 (luna-define-method elmo-map-folder-unmark-read ((folder elmo-maildir-folder)
341 (elmo-maildir-delete-mark-msgs folder locs ?S))
343 (luna-define-method elmo-map-folder-mark-as-answered ((folder
346 (elmo-maildir-set-mark-msgs folder locs ?R))
348 (luna-define-method elmo-map-folder-unmark-answered ((folder
351 (elmo-maildir-delete-mark-msgs folder locs ?R))
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))
359 (elmo-list-subdirectories-ignore-regexp
360 "^\\(\\.\\.?\\|cur\\|tmp\\|new\\)$")
361 elmo-have-link-count)
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)
371 (defvar elmo-maildir-sequence-number-internal 0)
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"
381 (float 65536)) (cadr cur-time))
383 (incf elmo-maildir-sequence-number-internal)
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)))
396 (f+ (f* (f (car time))
401 (% (abs (random t)) 10000); dummy pid
404 (defun elmo-maildir-temporal-filename (basedir)
405 (let ((filename (expand-file-name
406 (concat "tmp/" (elmo-maildir-make-unique-string))
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.
415 (concat "tmp/" (elmo-maildir-make-unique-string))
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))
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
436 (concat "new/" (file-name-nondirectory filename))
439 ;; If an error occured, return nil.
442 (luna-define-method elmo-folder-message-file-p ((folder elmo-maildir-folder))
445 (luna-define-method elmo-message-file-name ((folder elmo-maildir-folder)
447 (elmo-maildir-message-file-name
449 (elmo-map-message-location folder number)))
451 (luna-define-method elmo-folder-message-make-temp-file-p
452 ((folder elmo-maildir-folder))
455 (luna-define-method elmo-folder-message-make-temp-files ((folder
460 (let ((temp-dir (elmo-folder-make-temporary-directory folder))
461 (cur-number (if start-number 0)))
462 (dolist (number numbers)
464 (elmo-message-file-name folder number)
466 (int-to-string (if start-number (incf cur-number) number))
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)))
478 filename mark flag id)
479 (dolist (number numbers)
480 (setq mark (and src-msgdb-exists
481 (elmo-message-mark src-folder (car numbers)))
484 ((member mark (elmo-msgdb-answered-marks))
486 ((not (member mark (elmo-msgdb-unread-marks)))
488 filename (elmo-maildir-temporal-filename dir))
490 (elmo-message-file-name src-folder number)
492 (elmo-add-name-to-file
495 (concat "new/" (file-name-nondirectory filename))
497 ;; src folder's msgdb is loaded.
498 (when (setq id (and src-msgdb-exists
499 (elmo-message-field src-folder (car numbers)
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))
506 (luna-call-next-method)))
508 (luna-define-method elmo-map-folder-delete-messages
509 ((folder elmo-maildir-folder) locations)
511 (dolist (location locations)
512 (setq file (elmo-maildir-message-file-name folder location))
514 (file-writable-p file)
515 (not (file-directory-p file)))
516 (delete-file file)))))
518 (luna-define-method elmo-map-message-fetch ((folder elmo-maildir-folder)
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))))
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)))))
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))))
537 (luna-define-method elmo-folder-creatable-p ((folder elmo-maildir-folder))
540 (luna-define-method elmo-folder-writable-p ((folder elmo-maildir-folder))
543 (luna-define-method elmo-folder-create ((folder elmo-maildir-folder))
544 (let ((basedir (elmo-maildir-folder-directory-internal folder)))
547 (dolist (dir '("." "new" "cur" "tmp"))
548 (setq dir (expand-file-name dir basedir))
549 (or (file-directory-p dir)
551 (elmo-make-directory dir)
552 (set-file-modes dir 448))))
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))
563 (elmo-folder-name-internal folder)))
564 (let ((basedir (elmo-maildir-folder-directory-internal folder)))
566 (let ((tmp-files (directory-files
567 (expand-file-name "tmp" basedir)
569 ;; Delete files in tmp.
570 (dolist (file tmp-files)
572 (dolist (dir '("new" "cur" "tmp" "."))
573 (setq dir (expand-file-name dir basedir))
574 (if (not (file-directory-p dir))
576 (elmo-delete-directory dir t))))
578 (elmo-msgdb-delete-path folder)
581 (luna-define-method elmo-folder-rename-internal ((folder elmo-maildir-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)
596 (product-provide (provide 'elmo-maildir) (require 'elmo-version))
598 ;;; elmo-maildir.el ends here