2d61fa72e505b2fe31598bb8846b842921a8a663
[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 message &rest args)
77   "Send MESSAGE to ENTITY with ARGS, and return the result."
78   (apply (mime-find-function
79           message (mime-entity-representation-type-internal entity))
80          entity
81          args))
82
83 (defsubst mm-arglist-to-arguments (arglist)
84   (let (dest)
85     (while arglist
86       (let ((arg (car arglist)))
87         (or (memq arg '(&optional &rest))
88             (setq dest (cons arg dest)))
89         )
90       (setq arglist (cdr arglist)))
91     (nreverse dest)))
92
93 (defmacro mm-define-generic (name args &optional doc)
94   (if doc
95       `(defun ,(intern (format "mime-%s" name)) ,args
96          ,doc
97          (mime-entity-send ,(car args) ',name
98                            ,@(mm-arglist-to-arguments (cdr args)))
99          )
100     `(defun ,(intern (format "mime-%s" name)) ,args
101        (mime-entity-send ,(car args) ',name
102                          ,@(mm-arglist-to-arguments (cdr args)))
103        )))
104
105 (put 'mm-define-generic 'lisp-indent-function 'defun)
106
107 (defun mime-open-entity (type location)
108   "Open an entity and return it.
109 TYPE is representation-type.
110 LOCATION is location of entity.  Specification of it is depended on
111 representation-type."
112   (let ((entity (make-mime-entity-internal type location)))
113     (mime-entity-send entity 'initialize-instance)
114     entity))
115
116 (mm-define-generic entity-cooked-p (entity)
117   "Return non-nil if contents of ENTITY has been already code-converted.")
118
119
120 ;;; @ Entity as node of message
121 ;;;
122
123 (defun mime-entity-children (entity)
124   (or (mime-entity-children-internal entity)
125       (mime-entity-send entity 'entity-children)))
126
127 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
128
129 (defun mime-entity-number (entity)
130   "Return entity-number of ENTITY."
131   (reverse (mime-entity-node-id-internal entity)))
132
133 (defun mime-find-entity-from-number (entity-number &optional message)
134   "Return entity from ENTITY-NUMBER in MESSAGE.
135 If MESSAGE is not specified, `mime-message-structure' is used."
136   (or message
137       (setq message mime-message-structure))
138   (let ((sn (car entity-number)))
139     (if (null sn)
140         message
141       (let ((rc (nth sn (mime-entity-children message))))
142         (if rc
143             (mime-find-entity-from-number (cdr entity-number) rc)
144           ))
145       )))
146
147 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
148   "Return entity from ENTITY-NODE-ID in MESSAGE.
149 If MESSAGE is not specified, `mime-message-structure' is used."
150   (mime-find-entity-from-number (reverse entity-node-id) message))
151
152 (defun mime-entity-parent (entity &optional message)
153   "Return mother entity of ENTITY.
154 If MESSAGE is specified, it is regarded as root entity."
155   (if (equal entity message)
156       nil
157     (mime-entity-parent-internal entity)))
158
159 (defun mime-root-entity-p (entity &optional message)
160   "Return t if ENTITY is root-entity (message).
161 If MESSAGE is specified, it is regarded as root entity."
162   (null (mime-entity-parent entity message)))
163
164
165 ;;; @ Entity Buffer
166 ;;;
167
168 (defun mime-entity-buffer (entity)
169   (or (mime-entity-buffer-internal entity)
170       (mime-entity-send entity 'entity-buffer)))
171
172 (mm-define-generic entity-point-min (entity)
173   "Return the start point of ENTITY in the buffer which contains ENTITY.")
174
175 (mm-define-generic entity-point-max (entity)
176   "Return the end point of ENTITY in the buffer which contains ENTITY.")
177
178 (defun mime-entity-header-start (entity)
179   (or (mime-entity-header-start-internal entity)
180       (mime-entity-send entity 'entity-header-start)))
181
182 (defun mime-entity-header-end (entity)
183   (or (mime-entity-header-end-internal entity)
184       (mime-entity-send entity 'entity-header-end)))
185
186 (defun mime-entity-body-start (entity)
187   (or (mime-entity-body-start-internal entity)
188       (mime-entity-send entity 'entity-body-start)))
189
190 (defun mime-entity-body-end (entity)
191   (or (mime-entity-body-end-internal entity)
192       (mime-entity-send entity 'entity-body-end)))
193
194
195 ;;; @ Entity Header
196 ;;;
197
198 (defun mime-fetch-field (field-name &optional entity)
199   (or (symbolp field-name)
200       (setq field-name (intern (capitalize (capitalize field-name)))))
201   (or entity
202       (setq entity mime-message-structure))
203   (let* ((header (mime-entity-original-header-internal entity))
204          (field-body (cdr (assq field-name header))))
205     (or field-body
206         (progn
207           (if (setq field-body
208                     (mime-entity-send entity 'fetch-field
209                                       (symbol-name field-name)))
210               (mime-entity-set-original-header-internal
211                entity (put-alist field-name field-body header))
212             )
213           field-body))))
214
215 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
216
217 (defun mime-entity-content-disposition (entity)
218   (or (mime-entity-content-disposition-internal entity)
219       (let ((ret (mime-fetch-field 'Content-Disposition entity)))
220         (if ret
221             (let ((disposition (mime-parse-Content-Disposition ret)))
222               (when disposition
223                 (mime-entity-set-content-disposition-internal
224                  entity disposition)
225                 disposition))))))
226
227 (defun mime-entity-encoding (entity &optional default-encoding)
228   (or (mime-entity-encoding-internal entity)
229       (let ((encoding
230              (or (let ((ret (mime-fetch-field
231                              'Content-Transfer-Encoding entity)))
232                    (and ret (mime-parse-Content-Transfer-Encoding ret)))
233                  default-encoding "7bit")))
234         (mime-entity-set-encoding-internal entity encoding)
235         encoding)))
236
237 (defun mime-read-field (field-name &optional entity)
238   (or (symbolp field-name)
239       (setq field-name (capitalize (capitalize field-name))))
240   (or entity
241       (setq entity mime-message-structure))
242   (cond ((eq field-name 'Content-Type)
243          (mime-entity-content-type entity)
244          )
245         ((eq field-name 'Content-Disposition)
246          (mime-entity-content-disposition entity)
247          )
248         ((eq field-name 'Content-Transfer-Encoding)
249          (mime-entity-encoding entity)
250          )
251         (t
252          (let* ((header (mime-entity-parsed-header-internal entity))
253                 (field (cdr (assq field-name header))))
254            (or field
255                (let ((field-body (mime-fetch-field field-name entity)))
256                  (when field-body
257                    (cond ((memq field-name '(From Resent-From
258                                              To Resent-To
259                                              Cc Resent-Cc
260                                              Bcc Resent-Bcc
261                                              Reply-To Resent-Reply-To))
262                           (setq field (std11-parse-addresses
263                                        (eword-lexical-analyze field-body)))
264                           )
265                          ((memq field-name '(Sender Resent-Sender))
266                           (setq field (std11-parse-address
267                                        (eword-lexical-analyze field-body)))
268                           )
269                          ((memq field-name eword-decode-ignored-field-list)
270                           (setq field field-body))
271                          ((memq field-name eword-decode-structured-field-list)
272                           (setq field (eword-decode-structured-field-body
273                                        field-body)))
274                          (t
275                           (setq field (eword-decode-unstructured-field-body
276                                        field-body))
277                           ))
278                    (mime-entity-set-parsed-header-internal
279                     entity (put-alist field-name field header))
280                    field)))))))
281
282 (mm-define-generic insert-decoded-header (entity &optional invisible-fields
283                                           visible-fields)
284   "Insert before point a decoded header of ENTITY.")
285
286
287 ;;; @ Entity Attributes
288 ;;;
289
290 (defun mime-entity-uu-filename (entity)
291   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
292       (save-excursion
293         (set-buffer (mime-entity-buffer entity))
294         (goto-char (mime-entity-body-start entity))
295         (if (re-search-forward "^begin [0-9]+ "
296                                (mime-entity-body-end entity) t)
297             (if (looking-at ".+$")
298                 (buffer-substring (match-beginning 0)(match-end 0))
299               )))))
300
301 (defun mime-entity-filename (entity)
302   "Return filename of ENTITY."
303   (or (mime-entity-uu-filename entity)
304       (mime-content-disposition-filename
305        (mime-entity-content-disposition entity))
306       (cdr (let ((param (mime-content-type-parameters
307                          (mime-entity-content-type entity))))
308              (or (assoc "name" param)
309                  (assoc "x-name" param))
310              ))))
311
312
313 (defsubst mime-entity-media-type (entity)
314   (mime-content-type-primary-type (mime-entity-content-type entity)))
315 (defsubst mime-entity-media-subtype (entity)
316   (mime-content-type-subtype (mime-entity-content-type entity)))
317 (defsubst mime-entity-parameters (entity)
318   (mime-content-type-parameters (mime-entity-content-type entity)))
319 (defsubst mime-entity-type/subtype (entity-info)
320   (mime-type/subtype-string (mime-entity-media-type entity-info)
321                             (mime-entity-media-subtype entity-info)))
322
323
324 ;;; @ Entity Content
325 ;;;
326
327 (mm-define-generic entity-content (entity)
328   "Return content of ENTITY as byte sequence (string).")
329
330 (mm-define-generic write-entity-content (entity filename)
331   "Write content of ENTITY into FILENAME.")
332
333 (mm-define-generic write-entity (entity filename)
334   "Write header and body of ENTITY into FILENAME.")
335
336 (mm-define-generic write-entity-body (entity filename)
337   "Write body of ENTITY into FILENAME.")
338
339
340 ;;; @ end
341 ;;;
342
343 (provide 'mime)
344
345 ;;; mime.el ends here