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