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