* mime-def.el (mime-library-version-string): bump up to 1.9.1.
[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-buffer "mime-parse"
57   "Parse BUFFER as a MIME message.")
58
59
60 ;;; @ Entity Representation and Implementation
61 ;;;
62
63 (defsubst mime-find-function (service type)
64   (let ((imps (cdr (assq type mime-entity-implementation-alist))))
65     (if imps
66         (cdr (assq service imps))
67       (require (intern (format "mm%s" type)))
68       (cdr (assq service
69                  (cdr (assq type mime-entity-implementation-alist))))
70       )))
71
72 (defsubst mime-entity-function (entity service)
73   (mime-find-function service
74                       (mime-entity-representation-type-internal entity)))
75
76 (defsubst mime-entity-send (entity service &rest args)
77   (apply (mime-find-function
78           service (mime-entity-representation-type-internal entity))
79          entity
80          args))
81
82 (defsubst mm-arglist-to-arguments (arglist)
83   (let (dest)
84     (while arglist
85       (let ((arg (car arglist)))
86         (or (memq arg '(&optional &rest))
87             (setq dest (cons arg dest)))
88         )
89       (setq arglist (cdr arglist)))
90     (nreverse dest)))
91
92 (defmacro mm-define-generic (name args &optional doc)
93   (if doc
94       `(defun ,(intern (format "mime-%s" name)) ,args
95          ,doc
96          (mime-entity-send ,(car args) ',name
97                            ,@(mm-arglist-to-arguments (cdr args)))
98          )
99     `(defun ,(intern (format "mime-%s" name)) ,args
100        (mime-entity-send ,(car args) ',name
101                          ,@(mm-arglist-to-arguments (cdr args)))
102        )))
103
104 (put 'mm-define-generic 'lisp-indent-function 'defun)
105
106 (defun mime-open-entity (type location)
107   "Open an entity and return it.
108 TYPE is representation-type.
109 LOCATION is location of entity.  Specification of it is depended on
110 representation-type."
111   (let ((entity (make-mime-entity-internal type location)))
112     (mime-entity-send entity 'initialize-instance)
113     entity))
114
115 (mm-define-generic entity-cooked-p (entity)
116   "Return non-nil if contents of ENTITY has been already code-converted.")
117
118
119 ;;; @ Entity as node of message
120 ;;;
121
122 (defun mime-entity-children (entity)
123   (or (mime-entity-children-internal entity)
124       (mime-entity-send entity 'entity-children)))
125
126 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
127
128 (defun mime-entity-number (entity)
129   "Return entity-number of ENTITY."
130   (reverse (mime-entity-node-id-internal entity)))
131
132 (defun mime-find-entity-from-number (entity-number &optional message)
133   "Return entity from ENTITY-NUMBER in MESSAGE.
134 If MESSAGE is not specified, `mime-message-structure' is used."
135   (or message
136       (setq message mime-message-structure))
137   (let ((sn (car entity-number)))
138     (if (null sn)
139         message
140       (let ((rc (nth sn (mime-entity-children message))))
141         (if rc
142             (mime-find-entity-from-number (cdr entity-number) rc)
143           ))
144       )))
145
146 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
147   "Return entity from ENTITY-NODE-ID in MESSAGE.
148 If MESSAGE is not specified, `mime-message-structure' is used."
149   (mime-find-entity-from-number (reverse entity-node-id) message))
150
151 (defun mime-entity-parent (entity &optional message)
152   "Return mother entity of ENTITY.
153 If MESSAGE is specified, it is regarded as root entity."
154   (if (equal entity message)
155       nil
156     (mime-entity-parent-internal entity)))
157
158 (defun mime-root-entity-p (entity &optional message)
159   "Return t if ENTITY is root-entity (message).
160 If MESSAGE is specified, it is regarded as root entity."
161   (null (mime-entity-parent entity message)))
162
163
164 ;;; @ Entity Buffer
165 ;;;
166
167 (defun mime-entity-buffer (entity)
168   (or (mime-entity-buffer-internal entity)
169       (mime-entity-send entity 'entity-buffer)))
170
171 (mm-define-generic entity-point-min (entity)
172   "Return the start point of ENTITY in the buffer which contains ENTITY.")
173
174 (mm-define-generic entity-point-max (entity)
175   "Return the end point of ENTITY in the buffer which contains ENTITY.")
176
177 (defun mime-entity-header-start (entity)
178   (or (mime-entity-header-start-internal entity)
179       (mime-entity-send entity 'entity-header-start)))
180
181 (defun mime-entity-header-end (entity)
182   (or (mime-entity-header-end-internal entity)
183       (mime-entity-send entity 'entity-header-end)))
184
185 (defun mime-entity-body-start (entity)
186   (or (mime-entity-body-start-internal entity)
187       (mime-entity-send entity 'entity-body-start)))
188
189 (defun mime-entity-body-end (entity)
190   (or (mime-entity-body-end-internal entity)
191       (mime-entity-send entity 'entity-body-end)))
192
193
194 ;;; @ Entity Header
195 ;;;
196
197 (defun mime-fetch-field (field-name &optional entity)
198   (or (symbolp field-name)
199       (setq field-name (intern (capitalize (capitalize field-name)))))
200   (or entity
201       (setq entity mime-message-structure))
202   (let* ((header (mime-entity-original-header-internal entity))
203          (field-body (cdr (assq field-name header))))
204     (or field-body
205         (progn
206           (if (setq field-body
207                     (mime-entity-send entity 'fetch-field
208                                       (symbol-name field-name)))
209               (mime-entity-set-original-header-internal
210                entity (put-alist field-name field-body header))
211             )
212           field-body))))
213
214 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
215
216 (defun mime-entity-content-disposition (entity)
217   (or (mime-entity-content-disposition-internal entity)
218       (let ((ret (mime-fetch-field 'Content-Disposition entity)))
219         (if ret
220             (let ((disposition (mime-parse-Content-Disposition ret)))
221               (when disposition
222                 (mime-entity-set-content-disposition-internal
223                  entity disposition)
224                 disposition))))))
225
226 (defun mime-entity-encoding (entity &optional default-encoding)
227   (or (mime-entity-encoding-internal entity)
228       (let ((encoding
229              (or (let ((ret (mime-fetch-field
230                              'Content-Transfer-Encoding entity)))
231                    (and ret (mime-parse-Content-Transfer-Encoding ret)))
232                  default-encoding "7bit")))
233         (mime-entity-set-encoding-internal entity encoding)
234         encoding)))
235
236 (defun mime-read-field (field-name &optional entity)
237   (or (symbolp field-name)
238       (setq field-name (capitalize (capitalize field-name))))
239   (or entity
240       (setq entity mime-message-structure))
241   (cond ((eq field-name 'Content-Type)
242          (mime-entity-content-type entity)
243          )
244         ((eq field-name 'Content-Disposition)
245          (mime-entity-content-disposition entity)
246          )
247         ((eq field-name 'Content-Transfer-Encoding)
248          (mime-entity-encoding entity)
249          )
250         (t
251          (let* ((header (mime-entity-parsed-header-internal entity))
252                 (field (cdr (assq field-name header))))
253            (or field
254                (let ((field-body (mime-fetch-field field-name entity)))
255                  (when field-body
256                    (cond ((memq field-name '(From Resent-From
257                                              To Resent-To
258                                              Cc Resent-Cc
259                                              Bcc Resent-Bcc
260                                              Reply-To Resent-Reply-To))
261                           (setq field (std11-parse-addresses
262                                        (eword-lexical-analyze field-body)))
263                           )
264                          ((memq field-name '(Sender Resent-Sender))
265                           (setq field (std11-parse-address
266                                        (eword-lexical-analyze field-body)))
267                           )
268                          ((memq field-name eword-decode-ignored-field-list)
269                           (setq field field-body))
270                          ((memq field-name eword-decode-structured-field-list)
271                           (setq field (eword-decode-structured-field-body
272                                        field-body)))
273                          (t
274                           (setq field (eword-decode-unstructured-field-body
275                                        field-body))
276                           ))
277                    (mime-entity-set-parsed-header-internal
278                     entity (put-alist field-name field header))
279                    field)))))))
280
281 (mm-define-generic insert-decoded-header (entity &optional invisible-fields
282                                           visible-fields)
283   "Insert before point a decoded header of ENTITY.")
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 (mm-define-generic entity-content (entity)
327   "Return content of ENTITY as byte sequence (string).")
328
329 (mm-define-generic write-entity-content (entity filename)
330   "Write content of ENTITY into FILENAME.")
331
332 (mm-define-generic write-entity (entity filename)
333   "Write header and body of ENTITY into FILENAME.")
334
335 (mm-define-generic write-entity-body (entity filename)
336   "Write body of ENTITY into FILENAME.")
337
338
339 ;;; @ end
340 ;;;
341
342 (provide 'mime)
343
344 ;;; mime.el ends here