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