update.
[elisp/flim.git] / mime.el
1 ;;; mime.el --- MIME library module
2
3 ;; Copyright (C) 1998,1999,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
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 (eval-when-compile (require 'mmgeneric))
33
34 (eval-and-compile
35
36 (autoload 'mime-encode-header-in-buffer "eword-encode"
37   "Encode header fields to network representation, such as MIME encoded-word.")
38
39 (autoload 'mime-parse-Content-Type "mime-parse"
40   "Parse STRING as field-body of Content-Type field.")
41 (autoload 'mime-read-Content-Type "mime-parse"
42   "Read field-body of Content-Type field from current-buffer,
43 and return parsed it.")
44
45 (autoload 'mime-parse-Content-Disposition "mime-parse"
46   "Parse STRING as field-body of Content-Disposition field.")
47 (autoload 'mime-read-Content-Disposition "mime-parse"
48   "Read field-body of Content-Disposition field from current-buffer,
49 and return parsed it.")
50
51 (autoload 'mime-parse-Content-Transfer-Encoding "mime-parse"
52   "Parse STRING as field-body of Content-Transfer-Encoding field.")
53 (autoload 'mime-read-Content-Transfer-Encoding "mime-parse"
54   "Read field-body of Content-Transfer-Encoding field from
55 current-buffer, and return it.")
56
57 (autoload 'mime-parse-msg-id "mime-parse"
58   "Parse TOKENS as msg-id of Content-Id or Message-Id field.")
59
60 (autoload 'mime-uri-parse-cid "mime-parse"
61   "Parse STRING as cid URI.")
62
63 (autoload 'mime-parse-buffer "mime-parse"
64   "Parse BUFFER as a MIME message.")
65
66 )
67
68 (autoload 'mime-encode-field-body "eword-encode"
69   "Encode FIELD-BODY as FIELD-NAME, and return the result.")
70
71
72 ;;; @ Entity Representation and Implementation
73 ;;;
74
75 (defmacro mime-entity-send (entity message &rest args)
76   `(luna-send ,entity ',(intern (format "mime-%s" (eval message))) ,@args))
77
78 (defun mime-open-entity (type location)
79   "Open an entity and return it.
80 TYPE is representation-type.
81 LOCATION is location of entity.  Specification of it is depended on
82 representation-type."
83   (require (intern (format "mm%s" type)))
84   (luna-make-entity (mm-expand-class-name type) :location location))
85
86 (luna-define-generic mime-entity-cooked-p (entity)
87   "Return non-nil if contents of ENTITY has been already code-converted.")
88
89
90 ;;; @ Entity as node of message
91 ;;;
92
93 (defun mime-entity-children (entity)
94   (or (mime-entity-children-internal entity)
95       (luna-send entity 'mime-entity-children entity)))
96
97 (defun mime-entity-node-id (entity)
98   (mime-entity-node-id-internal entity))
99
100 (defun mime-entity-number (entity)
101   "Return entity-number of ENTITY."
102   (reverse (mime-entity-node-id-internal entity)))
103
104 (defun mime-find-entity-from-number (entity-number message)
105   "Return entity from ENTITY-NUMBER in MESSAGE."
106   (let ((sn (car entity-number)))
107     (if (null sn)
108         message
109       (let ((rc (nth sn (mime-entity-children message))))
110         (if rc
111             (mime-find-entity-from-number (cdr entity-number) rc)
112           ))
113       )))
114
115 (defun mime-find-entity-from-node-id (entity-node-id message)
116   "Return entity from ENTITY-NODE-ID in MESSAGE."
117   (mime-find-entity-from-number (reverse entity-node-id) message))
118
119 (defun mime-find-entity-from-content-id (cid message)
120   "Return entity from CID in MESSAGE."
121   (if (equal cid (mime-entity-read-field message "Content-Id"))
122       message
123     (let ((children (mime-entity-children message))
124           ret)
125       (while (and children
126                   (null (setq ret (mime-find-entity-from-content-id
127                                    cid (car children)))))
128         (setq children (cdr children)))
129       ret)))
130
131 (defun mime-entity-parent (entity &optional message)
132   "Return mother entity of ENTITY.
133 If MESSAGE is specified, it is regarded as root entity."
134   (if (equal entity message)
135       nil
136     (mime-entity-parent-internal entity)))
137
138 (defun mime-root-entity-p (entity &optional message)
139   "Return t if ENTITY is root-entity (message).
140 If MESSAGE is specified, it is regarded as root entity."
141   (null (mime-entity-parent entity message)))
142
143 (defun mime-find-root-entity (entity)
144   "Return root entity of ENTITY."
145   (let ((p (mime-entity-parent entity)))
146     (if (null p)
147         entity
148       (mime-entity-parent p))))
149
150
151 ;;; @ Header buffer (obsolete)
152 ;;;
153
154 ;; (luna-define-generic mime-entity-header-buffer (entity))
155
156 ;; (luna-define-generic mime-goto-header-start-point (entity)
157 ;;   "Set buffer and point to header-start-position of ENTITY.")
158
159 ;; (luna-define-generic mime-entity-header-start-point (entity)
160 ;;   "Return header-start-position of ENTITY.")
161
162 ;; (luna-define-generic mime-entity-header-end-point (entity)
163 ;;   "Return header-end-position of ENTITY.")
164
165 ;; (make-obsolete 'mime-entity-header-buffer "don't use it.")
166 ;; (make-obsolete 'mime-goto-header-start-point "don't use it.")
167 ;; (make-obsolete 'mime-entity-header-start-point "don't use it.")
168 ;; (make-obsolete 'mime-entity-header-end-point "don't use it.")
169
170
171 ;;; @ Body buffer (obsolete)
172 ;;;
173
174 ;; (luna-define-generic mime-entity-body-buffer (entity))
175
176 ;; (luna-define-generic mime-goto-body-start-point (entity)
177 ;;   "Set buffer and point to body-start-position of ENTITY.")
178
179 ;; (luna-define-generic mime-goto-body-end-point (entity)
180 ;;   "Set buffer and point to body-end-position of ENTITY.")
181
182 ;; (luna-define-generic mime-entity-body-start-point (entity)
183 ;;   "Return body-start-position of ENTITY.")
184
185 ;; (luna-define-generic mime-entity-body-end-point (entity)
186 ;;   "Return body-end-position of ENTITY.")
187
188 ;; (defalias 'mime-entity-body-start 'mime-entity-body-start-point)
189 ;; (defalias 'mime-entity-body-end 'mime-entity-body-end-point)
190
191 ;; (make-obsolete 'mime-entity-body-buffer "don't use it.")
192 ;; (make-obsolete 'mime-goto-body-start-point "don't use it.")
193 ;; (make-obsolete 'mime-goto-body-end-point "don't use it.")
194 ;; (make-obsolete 'mime-entity-body-start-point "don't use it.")
195 ;; (make-obsolete 'mime-entity-body-end-point "don't use it.")
196 ;; (make-obsolete 'mime-entity-body-start "don't use it.")
197 ;; (make-obsolete 'mime-entity-body-end "don't use it.")
198
199
200 ;;; @ Entity buffer (obsolete)
201 ;;;
202
203 ;; (luna-define-generic mime-entity-buffer (entity))
204 ;; (make-obsolete 'mime-entity-buffer "don't use it.")
205
206 ;; (luna-define-generic mime-entity-point-min (entity))
207 ;; (make-obsolete 'mime-entity-point-min "don't use it.")
208
209 ;; (luna-define-generic mime-entity-point-max (entity))
210 ;; (make-obsolete 'mime-entity-point-max "don't use it.")
211
212
213 ;;; @ Entity
214 ;;;
215
216 (luna-define-generic mime-insert-entity (entity)
217   "Insert header and body of ENTITY at point.")
218
219 (luna-define-generic mime-write-entity (entity filename)
220   "Write header and body of ENTITY into FILENAME.")
221
222
223 ;;; @ Entity Body
224 ;;;
225
226 (luna-define-generic mime-entity-body (entity)
227   "Return network representation of ENTITY body.")
228
229 (luna-define-generic mime-insert-entity-body (entity)
230   "Insert network representation of ENTITY body at point.")
231
232 (luna-define-generic mime-write-entity-body (entity filename)
233   "Write body of ENTITY into FILENAME.")
234
235
236 ;;; @ Entity Content
237 ;;;
238
239 (luna-define-generic mime-entity-content (entity)
240   "Return content of ENTITY as byte sequence (string).")
241
242 (luna-define-generic mime-insert-entity-content (entity)
243   "Insert content of ENTITY at point.")
244
245 (luna-define-generic mime-write-entity-content (entity filename)
246   "Write content of ENTITY into FILENAME.")
247
248 (luna-define-generic mime-insert-text-content (entity)
249   "Insert decoded text body of ENTITY.")
250
251
252 ;;; @ Header fields
253 ;;;
254
255 (luna-define-generic mime-entity-fetch-field (entity field-name)
256   "Return the value of the ENTITY's header field whose type is FIELD-NAME.")
257
258 ;; (defun mime-fetch-field (field-name &optional entity)
259 ;;   "Return the value of the ENTITY's header field whose type is FIELD-NAME."
260 ;;   (if (symbolp field-name)
261 ;;       (setq field-name (symbol-name field-name))
262 ;;     )
263 ;;   (or entity
264 ;;       (setq entity mime-message-structure))
265 ;;   (mime-entity-fetch-field entity field-name)
266 ;;   )
267 ;; (make-obsolete 'mime-fetch-field 'mime-entity-fetch-field)
268
269 (defun mime-entity-content-type (entity)
270   (or (mime-entity-content-type-internal entity)
271       (let ((ret (mime-entity-fetch-field entity "Content-Type")))
272         (if ret
273             (mime-entity-set-content-type-internal
274              entity (mime-parse-Content-Type ret))
275           ))))
276
277 (defun mime-entity-content-disposition (entity)
278   (or (mime-entity-content-disposition-internal entity)
279       (let ((ret (mime-entity-fetch-field entity "Content-Disposition")))
280         (if ret
281             (mime-entity-set-content-disposition-internal
282              entity (mime-parse-Content-Disposition ret))
283           ))))
284
285 (defun mime-entity-encoding (entity &optional default-encoding)
286   (or (mime-entity-encoding-internal entity)
287       (let ((ret (mime-entity-fetch-field entity "Content-Transfer-Encoding")))
288         (mime-entity-set-encoding-internal
289          entity
290          (or (and ret (mime-parse-Content-Transfer-Encoding ret))
291              default-encoding "7bit"))
292         )))
293
294 (defvar mime-field-parser-alist
295   '((Return-Path        . std11-parse-route-addr)
296     
297     (Reply-To           . std11-parse-addresses)
298     
299     (Sender             . std11-parse-mailbox)
300     (From               . std11-parse-addresses)
301
302     (Resent-Reply-To    . std11-parse-addresses)
303     
304     (Resent-Sender      . std11-parse-mailbox)
305     (Resent-From        . std11-parse-addresses)
306
307     (To                 . std11-parse-addresses)
308     (Resent-To          . std11-parse-addresses)
309     (Cc                 . std11-parse-addresses)
310     (Resent-Cc          . std11-parse-addresses)
311     (Bcc                . std11-parse-addresses)
312     (Resent-Bcc         . std11-parse-addresses)
313     
314     (Message-Id         . mime-parse-msg-id)
315     (Recent-Message-Id  . mime-parse-msg-id)
316     
317     (In-Reply-To        . std11-parse-msg-ids)
318     (References         . std11-parse-msg-ids)
319     
320     (Content-Id         . mime-parse-msg-id)
321     ))
322
323 (defun mime-entity-read-field (entity field-name)
324   (let ((sym (if (symbolp field-name)
325                  (prog1
326                      field-name
327                    (setq field-name (symbol-name field-name)))
328                (intern (capitalize (capitalize field-name))))))
329     (cond ((eq sym 'Content-Type)
330            (mime-entity-content-type entity)
331            )
332           ((eq sym 'Content-Disposition)
333            (mime-entity-content-disposition entity)
334            )
335           ((eq sym 'Content-Transfer-Encoding)
336            (mime-entity-encoding entity)
337            )
338           (t
339            (let* ((header (mime-entity-parsed-header-internal entity))
340                   (field (cdr (assq sym header))))
341              (or field
342                  (let ((field-body (mime-entity-fetch-field entity field-name))
343                        parser)
344                    (when field-body
345                      (setq parser
346                            (cdr (assq sym mime-field-parser-alist)))
347                      (setq field
348                            (if parser
349                                (funcall parser
350                                         (eword-lexical-analyze field-body))
351                              (mime-decode-field-body field-body sym 'plain)
352                              ))
353                      (mime-entity-set-parsed-header-internal
354                       entity (put-alist sym field header))
355                      field))))))))
356
357 ;; (defun mime-read-field (field-name &optional entity)
358 ;;   (or entity
359 ;;       (setq entity mime-message-structure))
360 ;;   (mime-entity-read-field entity field-name)
361 ;;   )
362 ;; (make-obsolete 'mime-read-field 'mime-entity-read-field)
363
364 (luna-define-generic mime-insert-header (entity &optional invisible-fields
365                                                 visible-fields)
366   "Insert before point a decoded header of ENTITY.")
367
368
369 ;;; @ Entity Attributes
370 ;;;
371
372 (luna-define-generic mime-entity-name (entity)
373   "Return name of the ENTITY.")
374
375 (defun mime-entity-uu-filename (entity)
376   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
377       (with-temp-buffer
378         (mime-insert-entity-body entity)
379         (if (re-search-forward "^begin [0-9]+ " nil t)
380             (if (looking-at ".+$")
381                 (buffer-substring (match-beginning 0)(match-end 0))
382               )))))
383
384 (defun mime-entity-filename (entity)
385   "Return filename of ENTITY."
386   (or (mime-entity-uu-filename entity)
387       (mime-content-disposition-filename
388        (mime-entity-content-disposition entity))
389       (cdr (let ((param (mime-content-type-parameters
390                          (mime-entity-content-type entity))))
391              (or (assoc "name" param)
392                  (assoc "x-name" param))
393              ))))
394
395
396 (defsubst mime-entity-media-type (entity)
397   (mime-content-type-primary-type (mime-entity-content-type entity)))
398 (defsubst mime-entity-media-subtype (entity)
399   (mime-content-type-subtype (mime-entity-content-type entity)))
400 (defsubst mime-entity-parameters (entity)
401   (mime-content-type-parameters (mime-entity-content-type entity)))
402 (defsubst mime-entity-type/subtype (entity-info)
403   (mime-type/subtype-string (mime-entity-media-type entity-info)
404                             (mime-entity-media-subtype entity-info)))
405
406 (defun mime-entity-set-content-type (entity content-type)
407   (mime-entity-set-content-type-internal entity content-type))
408
409 (defun mime-entity-set-encoding (entity encoding)
410   (mime-entity-set-encoding-internal entity encoding))
411
412
413 ;;; @ end
414 ;;;
415
416 (provide 'mime)
417
418 ;;; mime.el ends here