(mime-entity-content): Change message to `get-content'.
[elisp/flim.git] / mime.el
1 ;;; mime.el --- MIME library module
2
3 ;; Copyright (C) 1998 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: MIME, multimedia, mail, news
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; 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 ;;; Code:
26
27 (require 'alist)
28 (require 'std11)
29 (require 'mime-def)
30 (require 'eword-decode)
31
32 (autoload 'eword-encode-field "eword-encode"
33   "Encode header field STRING, and return the result.")
34 (autoload 'eword-encode-header "eword-encode"
35   "Encode header fields to network representation, such as MIME encoded-word.")
36
37
38 (autoload 'mime-parse-Content-Type "mime-parse"
39   "Parse STRING as field-body of Content-Type field.")
40 (autoload 'mime-read-Content-Type "mime-parse"
41   "Read field-body of Content-Type field from current-buffer,
42 and return parsed it.")
43
44 (autoload 'mime-parse-Content-Disposition "mime-parse"
45   "Parse STRING as field-body of Content-Disposition field.")
46 (autoload 'mime-read-Content-Disposition "mime-parse"
47   "Read field-body of Content-Disposition field from current-buffer,
48 and return parsed it.")
49
50 (autoload 'mime-parse-Content-Transfer-Encoding "mime-parse"
51   "Parse STRING as field-body of Content-Transfer-Encoding field.")
52 (autoload 'mime-read-Content-Transfer-Encoding "mime-parse"
53   "Read field-body of Content-Transfer-Encoding field from
54 current-buffer, and return it.")
55
56 (autoload 'mime-parse-message "mime-parse"
57   "Parse current-buffer as a MIME message.")
58
59 (autoload 'mime-parse-buffer "mime-parse"
60   "Parse BUFFER as a MIME message.")
61
62
63 ;;; @ Entity Representation and Implementation
64 ;;;
65
66 (defsubst mime-find-function (service type)
67   (let ((imps (cdr (assq type mime-entity-implementation-alist))))
68     (if imps
69         (cdr (assq service imps))
70       (require (intern (format "mm%s" type)))
71       (cdr (assq service
72                  (cdr (assq type mime-entity-implementation-alist))))
73       )))
74
75 (defsubst mime-entity-function (entity service)
76   (mime-find-function service
77                       (mime-entity-representation-type-internal entity)))
78
79 (defsubst mime-entity-send (entity service &rest args)
80   (apply (mime-find-function
81           service (mime-entity-representation-type-internal entity))
82          entity
83          args))
84
85 (defun mime-open-entity (type location)
86   "Open an entity and return it.
87 TYPE is representation-type.
88 LOCATION is location of entity.  Specification of it is depended on
89 representation-type."
90   (funcall (mime-find-function 'open type) location))
91
92 (defun mime-entity-cooked-p (entity)
93   "Return non-nil if contents of ENTITY has been already code-converted."
94   (mime-entity-send entity 'cooked-p))
95
96
97 ;;; @ Entity as node of message
98 ;;;
99
100 (defalias 'mime-entity-children 'mime-entity-children-internal)
101
102 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
103
104 (defun mime-entity-number (entity)
105   "Return entity-number of ENTITY."
106   (reverse (mime-entity-node-id-internal entity)))
107
108 (defun mime-find-entity-from-number (entity-number &optional message)
109   "Return entity from ENTITY-NUMBER in MESSAGE.
110 If MESSAGE is not specified, `mime-message-structure' is used."
111   (or message
112       (setq message mime-message-structure))
113   (let ((sn (car entity-number)))
114     (if (null sn)
115         message
116       (let ((rc (nth sn (mime-entity-children message))))
117         (if rc
118             (mime-find-entity-from-number (cdr entity-number) rc)
119           ))
120       )))
121
122 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
123   "Return entity from ENTITY-NODE-ID in MESSAGE.
124 If MESSAGE is not specified, `mime-message-structure' is used."
125   (mime-find-entity-from-number (reverse entity-node-id) message))
126
127 (defun mime-entity-parent (entity &optional message)
128   "Return mother entity of ENTITY.
129 If MESSAGE is not specified, `mime-message-structure' in the buffer of
130 ENTITY is used."
131   (mime-find-entity-from-node-id
132    (cdr (mime-entity-node-id entity))
133    (or message
134        (save-excursion
135          (set-buffer (mime-entity-buffer entity))
136          mime-message-structure))))
137
138 (defun mime-root-entity-p (entity)
139   "Return t if ENTITY is root-entity (message)."
140   (null (mime-entity-node-id entity)))
141
142
143 ;;; @ Entity Buffer
144 ;;;
145
146 (defun mime-entity-buffer (entity)
147   (or (mime-entity-buffer-internal entity)
148       (mime-entity-send entity 'entity-buffer)))
149
150 (defun mime-entity-point-min (entity)
151   (mime-entity-send entity 'point-min))
152
153 (defun mime-entity-point-max (entity)
154   (mime-entity-send entity 'point-max))
155
156 (defun mime-entity-header-start (entity)
157   (or (mime-entity-header-start-internal entity)
158       (mime-entity-send entity 'header-start)))
159
160 (defun mime-entity-header-end (entity)
161   (or (mime-entity-header-end-internal entity)
162       (mime-entity-send entity 'header-end)))
163
164 (defun mime-entity-body-start (entity)
165   (or (mime-entity-body-start-internal entity)
166       (mime-entity-send entity 'body-start)))
167
168 (defun mime-entity-body-end (entity)
169   (or (mime-entity-body-end-internal entity)
170       (mime-entity-send entity 'body-end)))
171
172
173 ;;; @ Entity Header
174 ;;;
175
176 (defun mime-fetch-field (field-name &optional entity)
177   (or (symbolp field-name)
178       (setq field-name (intern (capitalize (capitalize field-name)))))
179   (or entity
180       (setq entity mime-message-structure))
181   (let* ((header (mime-entity-original-header-internal entity))
182          (field-body (cdr (assq field-name header))))
183     (or field-body
184         (progn
185           (if (setq field-body
186                     (mime-entity-send entity 'fetch-field
187                                       (symbol-name field-name)))
188               (mime-entity-set-original-header-internal
189                entity (put-alist field-name field-body header))
190             )
191           field-body))))
192
193 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
194
195 (defun mime-entity-content-disposition (entity)
196   (or (mime-entity-content-disposition-internal entity)
197       (let ((ret (mime-fetch-field 'Content-Disposition entity)))
198         (if ret
199             (let ((disposition (mime-parse-Content-Disposition ret)))
200               (when disposition
201                 (mime-entity-set-content-disposition-internal
202                  entity disposition)
203                 disposition))))))
204
205 (defun mime-entity-encoding (entity &optional default-encoding)
206   (or (mime-entity-encoding-internal entity)
207       (let ((encoding
208              (or (let ((ret (mime-fetch-field
209                              'Content-Transfer-Encoding entity)))
210                    (and ret (mime-parse-Content-Transfer-Encoding ret)))
211                  default-encoding "7bit")))
212         (mime-entity-set-encoding-internal entity encoding)
213         encoding)))
214
215 (defun mime-read-field (field-name &optional entity)
216   (or (symbolp field-name)
217       (setq field-name (capitalize (capitalize field-name))))
218   (or entity
219       (setq entity mime-message-structure))
220   (cond ((eq field-name 'Content-Type)
221          (mime-entity-content-type entity)
222          )
223         ((eq field-name 'Content-Disposition)
224          (mime-entity-content-disposition entity)
225          )
226         ((eq field-name 'Content-Transfer-Encoding)
227          (mime-entity-encoding entity)
228          )
229         (t
230          (let* ((header (mime-entity-parsed-header-internal entity))
231                 (field (cdr (assq field-name header))))
232            (or field
233                (let ((field-body (mime-fetch-field field-name entity)))
234                  (when field-body
235                    (cond ((memq field-name '(From Resent-From
236                                              To Resent-To
237                                              Cc Resent-Cc
238                                              Bcc Resent-Bcc
239                                              Reply-To Resent-Reply-To))
240                           (setq field (std11-parse-addresses
241                                        (eword-lexical-analyze field-body)))
242                           )
243                          ((memq field-name '(Sender Resent-Sender))
244                           (setq field (std11-parse-address
245                                        (eword-lexical-analyze field-body)))
246                           )
247                          ((memq field-name eword-decode-ignored-field-list)
248                           (setq field field-body))
249                          ((memq field-name eword-decode-structured-field-list)
250                           (setq field (eword-decode-structured-field-body
251                                        field-body)))
252                          (t
253                           (setq field (eword-decode-unstructured-field-body
254                                        field-body))
255                           ))
256                    (mime-entity-set-parsed-header-internal
257                     entity (put-alist field-name field header))
258                    field)))))))
259
260 (defun eword-visible-field-p (field-name visible-fields invisible-fields)
261   (or (catch 'found
262         (while visible-fields
263           (let ((regexp (car visible-fields)))
264             (if (string-match regexp field-name)
265                 (throw 'found t)
266               ))
267           (setq visible-fields (cdr visible-fields))
268           ))
269       (catch 'found
270         (while invisible-fields
271           (let ((regexp (car invisible-fields)))
272             (if (string-match regexp field-name)
273                 (throw 'found nil)
274               ))
275           (setq invisible-fields (cdr invisible-fields))
276           )
277         t)))
278
279 (defun mime-insert-decoded-header (entity &optional invisible-fields
280                                           visible-fields)
281   "Insert before point a decoded header of ENTITY."
282   (mime-entity-send entity 'insert-decoded-header
283                     invisible-fields visible-fields))
284
285
286 ;;; @ Entity Attributes
287 ;;;
288
289 (defun mime-entity-uu-filename (entity)
290   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
291       (save-excursion
292         (set-buffer (mime-entity-buffer entity))
293         (goto-char (mime-entity-body-start entity))
294         (if (re-search-forward "^begin [0-9]+ "
295                                (mime-entity-body-end entity) t)
296             (if (looking-at ".+$")
297                 (buffer-substring (match-beginning 0)(match-end 0))
298               )))))
299
300 (defun mime-entity-filename (entity)
301   "Return filename of ENTITY."
302   (or (mime-entity-uu-filename entity)
303       (mime-content-disposition-filename
304        (mime-entity-content-disposition entity))
305       (cdr (let ((param (mime-content-type-parameters
306                          (mime-entity-content-type entity))))
307              (or (assoc "name" param)
308                  (assoc "x-name" param))
309              ))))
310
311
312 (defsubst mime-entity-media-type (entity)
313   (mime-content-type-primary-type (mime-entity-content-type entity)))
314 (defsubst mime-entity-media-subtype (entity)
315   (mime-content-type-subtype (mime-entity-content-type entity)))
316 (defsubst mime-entity-parameters (entity)
317   (mime-content-type-parameters (mime-entity-content-type entity)))
318 (defsubst mime-entity-type/subtype (entity-info)
319   (mime-type/subtype-string (mime-entity-media-type entity-info)
320                             (mime-entity-media-subtype entity-info)))
321
322
323 ;;; @ Entity Content
324 ;;;
325
326 (defun mime-entity-content (entity)
327   "Return content of ENTITY as byte sequence (string)."
328   (mime-entity-send entity 'get-content))
329
330 (defun mime-write-entity-content (entity filename)
331   "Write content of ENTITY into FILENAME."
332   (mime-entity-send entity 'write-entity-content filename))
333
334 (defun mime-write-entity (entity filename)
335   "Write ENTITY into FILENAME."
336   (mime-entity-send entity 'write-entity filename))
337
338 (defun mime-write-entity-body (entity filename)
339   "Write body of ENTITY into FILENAME."
340   (mime-entity-send entity 'write-entity-body filename))
341
342
343 ;;; @ end
344 ;;;
345
346 (provide 'mime)
347
348 ;;; mime.el ends here