* elmo-maildir.el (elmo-folder-message-make-temp-files): Respect
[elisp/wanderlust.git] / elmo / elmo-file.el
1 ;;; elmo-file.el --- File interface for ELMO.
2
3 ;; Copyright (C) 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 (eval-when-compile (require 'cl))
32
33 (require 'elmo)
34 (require 'elmo-map)
35 (require 'mime-edit)
36
37 (defun elmo-file-find (files)
38   "Return the first existing filename in the FILES."
39   (let (file)
40     (while files
41       (when (file-exists-p (car files))
42         (setq file (car files)
43               files nil))
44       (setq files (cdr files)))
45     (and file (expand-file-name file))))
46
47 (defcustom elmo-file-command (exec-installed-p "file")
48   "*Program name of the file type detection command `file'."
49   :type '(string :tag "Program name of the file")
50   :group 'elmo)
51
52 (defcustom elmo-file-command-argument
53   (let ((magic-file (elmo-file-find
54                      '("/usr/share/magic.mime"
55                        "/usr/share/file/magic.mime"
56                        "/cygwin/usr/share/file/magic.mime"))))
57     (if magic-file (list "-m" magic-file)))
58   "*Argument list for the `file' command.
59 \(It should return the MIME content type\)"
60   :type '(repeat string)
61   :group 'elmo)
62
63 (defcustom elmo-file-fetch-max-size (* 1024 1024)
64   "*Max size of the message fetching."
65   :type 'integer
66   :group 'elmo)
67
68 (eval-and-compile
69   (luna-define-class elmo-file-folder (elmo-map-folder) (file-path))
70   (luna-define-internal-accessors 'elmo-file-folder))
71
72 (luna-define-method elmo-folder-initialize ((folder
73                                              elmo-file-folder)
74                                             name)
75   (elmo-file-folder-set-file-path-internal folder name)
76   folder)
77
78 (luna-define-method elmo-folder-expand-msgdb-path ((folder
79                                                     elmo-file-folder))
80   (expand-file-name
81    (elmo-replace-string-as-filename (elmo-folder-name-internal folder))
82    (expand-file-name "file" elmo-msgdb-directory)))
83
84 (defun elmo-file-make-date-string (attrs)
85   (let ((s (current-time-string (nth 5 attrs))))
86     (string-match "\\`\\([A-Z][a-z][a-z]\\) +[A-Z][a-z][a-z] +[0-9][0-9]? *[0-9][0-9]?:[0-9][0-9]:[0-9][0-9] *[0-9]?[0-9]?[0-9][0-9]"
87                   s)
88     (concat (elmo-match-string 1 s) ", "
89             (timezone-make-date-arpa-standard s (current-time-zone)))))
90
91 (defun elmo-file-detect-content-type (file)
92   "Return content-type of the FILE."
93   (if (or (not (file-exists-p file))
94           (file-directory-p file))
95       "application/octet-stream"
96     (let (type)
97       (setq type (mime-find-file-type file))
98       (if (and (string= (nth 0 type) "application")
99                (string= (nth 1 type) "octet-stream"))
100           (if (and elmo-file-command
101                    elmo-file-command-argument)
102               (with-temp-buffer
103                 (if (zerop (apply 'call-process elmo-file-command
104                                   nil `(,(current-buffer) nil)
105                                   nil (append elmo-file-command-argument
106                                               (list (expand-file-name file)))))
107                     (progn
108                       (goto-char (point-min))
109                       (when (re-search-forward ": *" nil t)
110                         (setq type (buffer-substring (match-end 0)
111                                                      (point-at-eol))))
112                       (cond
113                        ((string= "empty" type)
114                         "application/octet-stream")
115                        ((string-match "text" type)
116                         "text/plain")
117                        (t
118                         (car (split-string type)))))
119                   "application/octet-stream"))
120             (concat (nth 0 type) "/" (nth 1 type)))
121         (concat (nth 0 type) "/" (nth 1 type))))))
122
123 (defun elmo-file-msgdb-create-entity (msgdb folder number)
124   "Create msgdb entity for the message in the FOLDER with NUMBER."
125   (let* ((file (elmo-message-file-name folder number))
126          (attrs (file-attributes file)))
127     (and (not (file-directory-p file))
128          attrs
129          (elmo-msgdb-make-message-entity
130           (elmo-msgdb-message-entity-handler msgdb)
131           :message-id (concat "<" (elmo-replace-in-string
132                                    file "/" ":")
133                               "@" (system-name))
134           :number number
135           :size (nth 7 attrs)
136           :date (elmo-file-make-date-string attrs)
137           :subject (file-name-nondirectory file)
138           :from (concat (user-full-name (nth 2 attrs))
139                         " <" (user-login-name (nth 2 attrs)) "@"
140                         (system-name) ">")))))
141
142 (luna-define-method elmo-folder-msgdb-create ((folder elmo-file-folder)
143                                               numlist flag-table)
144   (let ((new-msgdb (elmo-make-msgdb))
145         entity mark i percent num)
146     (setq num (length numlist))
147     (setq i 0)
148     (message "Creating msgdb...")
149     (while numlist
150       (setq entity
151             (elmo-file-msgdb-create-entity new-msgdb folder (car numlist)))
152       (when entity
153         (elmo-msgdb-append-entity new-msgdb entity '(new unread)))
154       (when (> num elmo-display-progress-threshold)
155         (setq i (1+ i))
156         (setq percent (/ (* i 100) num))
157         (elmo-display-progress
158          'elmo-folder-msgdb-create "Creating msgdb..."
159          percent))
160       (setq numlist (cdr numlist)))
161     (message "Creating msgdb...done")
162     new-msgdb))
163
164 (luna-define-method elmo-folder-message-file-p ((folder elmo-file-folder))
165   t)
166
167 (luna-define-method elmo-message-file-name ((folder elmo-file-folder)
168                                             number)
169   (expand-file-name (car (split-string
170                           (elmo-map-message-location folder number)
171                           "/"))
172                     (elmo-file-folder-file-path-internal folder)))
173
174 (luna-define-method elmo-folder-message-make-temp-file-p
175   ((folder elmo-file-folder))
176   t)
177
178 (luna-define-method elmo-folder-diff ((folder elmo-file-folder))
179   (cons nil nil))
180
181 (luna-define-method elmo-folder-message-make-temp-files ((folder
182                                                           elmo-file-folder)
183                                                          numbers
184                                                          &optional
185                                                          start-number)
186   (let ((temp-dir (elmo-folder-make-temporary-directory folder))
187         (cur-number (or start-number 0)))
188     (dolist (number numbers)
189       (elmo-copy-file
190        (elmo-message-file-name folder number)
191        (expand-file-name
192         (int-to-string (if start-number cur-number number))
193         temp-dir))
194       (incf cur-number))
195     temp-dir))
196
197 (luna-define-method elmo-map-message-fetch ((folder elmo-file-folder)
198                                             location strategy
199                                             &optional section unseen)
200   (let ((file (expand-file-name (car (split-string location "/"))
201                                 (elmo-file-folder-file-path-internal folder)))
202         charset guess uid is-text)
203     (when (file-exists-p file)
204       (set-buffer-multibyte nil)
205       (prog1
206           (insert-file-contents-as-binary file nil 0 elmo-file-fetch-max-size)
207         (unless (or (std11-field-body "To")
208                     (std11-field-body "Cc")
209                     (std11-field-body "Subject"))
210           (setq guess (elmo-file-detect-content-type file))
211           (setq is-text (string-match "^text/" guess))
212           (when is-text
213             (set-buffer-multibyte t)
214             (decode-coding-region
215              (point-min) (point-max)
216              elmo-mime-display-as-is-coding-system)
217             (setq charset (detect-mime-charset-region (point-min)
218                                                       (point-max))))
219           (goto-char (point-min))
220           (setq uid (nth 2 (file-attributes file)))
221           (insert "From: " (concat (user-full-name uid)
222                                    " <"(user-login-name uid) "@"
223                                    (system-name) ">") "\n")
224           (insert "Subject: " (file-name-nondirectory file) "\n")
225           (insert "Date: "
226                   (elmo-file-make-date-string (file-attributes file))
227                   "\n")
228           (insert "Message-ID: "
229                   (concat "<" (elmo-replace-in-string file "/" ":")
230                           "@" (system-name) ">\n"))
231           (insert "Content-Type: "
232                   guess
233                   (or (and is-text
234                            (concat
235                             "; charset=" (upcase (symbol-name charset))))
236                       "")
237                   "\nMIME-Version: 1.0\n\n")
238           (when is-text
239             (encode-mime-charset-region (point-min) (point-max) charset))
240           (set-buffer-multibyte nil))))))
241
242 (luna-define-method elmo-map-folder-list-message-locations
243   ((folder elmo-file-folder))
244   (delq nil
245         (mapcar
246          (lambda (file)
247            (when (not (file-directory-p file))
248              (concat
249               file "/"
250               (mapconcat
251                'number-to-string
252                (nth 5 (file-attributes (expand-file-name
253                                         file
254                                         (elmo-file-folder-file-path-internal
255                                          folder))))
256                ":"))))
257          (directory-files (elmo-file-folder-file-path-internal folder)))))
258
259 (luna-define-method elmo-folder-exists-p ((folder elmo-file-folder))
260   (file-directory-p (elmo-file-folder-file-path-internal folder)))
261
262 (luna-define-method elmo-folder-list-subfolders ((folder elmo-file-folder)
263                                                  &optional one-level)
264   (when (file-directory-p (elmo-file-folder-file-path-internal folder))
265     (append
266      (list (elmo-folder-name-internal folder))
267      (delq nil
268            (mapcar
269             (lambda (file)
270               (when (and (file-directory-p
271                           (expand-file-name
272                            file
273                            (elmo-file-folder-file-path-internal folder)))
274                          (not (string= file "."))
275                          (not (string= file "..")))
276                 (concat (elmo-folder-name-internal folder) "/" file)))
277             (directory-files (elmo-file-folder-file-path-internal
278                               folder)))))))
279
280 (require 'product)
281 (product-provide (provide 'elmo-file) (require 'elmo-version))
282
283 ;;; elmo-file.el ends here