update.
[elisp/flim.git] / mime.el
1 ;;; mime.el --- MIME library module
2
3 ;; Copyright (C) 1998,1999 Free Software Foundation, Inc.
4 ;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
5 ;; Licensed to the Free Software Foundation.
6
7 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
8 ;; Keywords: MIME, multimedia, mail, news
9
10 ;; This file is part of FLIM (Faithful Library about Internet Message).
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Code:
28
29 (require 'alist)
30 (require 'std11)
31 (require 'mime-def)
32 (require 'eword-decode)
33
34 (eval-and-compile
35
36 (autoload 'eword-encode-header "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 ;;; @ Entity Representation and Implementation
69 ;;;
70
71 (defmacro mime-entity-send (entity message &rest args)
72   `(luna-send ,entity ',(intern (format "mime-%s" (eval message))) ,@args))
73
74 (defun mime-open-entity (type location)
75   "Open an entity and return it.
76 TYPE is representation-type.
77 LOCATION is location of entity.  Specification of it is depended on
78 representation-type."
79   (require (intern (format "mm%s" type)))
80   (luna-make-entity (mm-expand-class-name type) :location location))
81
82 (luna-define-generic mime-entity-cooked-p (entity)
83   "Return non-nil if contents of ENTITY has been already code-converted.")
84
85
86 ;;; @ Entity as node of message
87 ;;;
88
89 (defun mime-entity-children (entity)
90   (or (mime-entity-children-internal entity)
91       (luna-send entity 'mime-entity-children entity)))
92
93 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
94
95 (defun mime-entity-number (entity)
96   "Return entity-number of ENTITY."
97   (reverse (mime-entity-node-id-internal entity)))
98
99 (defun mime-find-entity-from-number (entity-number &optional message)
100   "Return entity from ENTITY-NUMBER in MESSAGE.
101 If MESSAGE is not specified, `mime-message-structure' is used."
102   (or message
103       (setq message mime-message-structure))
104   (let ((sn (car entity-number)))
105     (if (null sn)
106         message
107       (let ((rc (nth sn (mime-entity-children message))))
108         (if rc
109             (mime-find-entity-from-number (cdr entity-number) rc)
110           ))
111       )))
112
113 (defun mime-find-entity-from-node-id (entity-node-id &optional message)
114   "Return entity from ENTITY-NODE-ID in MESSAGE.
115 If MESSAGE is not specified, `mime-message-structure' is used."
116   (mime-find-entity-from-number (reverse entity-node-id) message))
117
118 (defun mime-find-entity-from-content-id (cid &optional message)
119   "Return entity from CID in MESSAGE.
120 If MESSAGE is not specified, `mime-message-structure' is used."
121   (or message
122       (setq message mime-message-structure))
123   (if (equal cid (mime-entity-read-field message "Content-Id"))
124       message
125     (let ((children (mime-entity-children message))
126           ret)
127       (while (and children
128                   (null (setq ret (mime-find-entity-from-content-id
129                                    cid (car children)))))
130         (setq children (cdr children)))
131       ret)))
132
133 (defun mime-entity-parent (entity &optional message)
134   "Return mother entity of ENTITY.
135 If MESSAGE is specified, it is regarded as root entity."
136   (if (equal entity message)
137       nil
138     (mime-entity-parent-internal entity)))
139
140 (defun mime-root-entity-p (entity &optional message)
141   "Return t if ENTITY is root-entity (message).
142 If MESSAGE is specified, it is regarded as root entity."
143   (null (mime-entity-parent entity message)))
144
145
146 ;;; @ Header buffer
147 ;;;
148
149 (luna-define-generic mime-entity-header-buffer (entity))
150
151 (luna-define-generic mime-goto-header-start-point (entity)
152   "Set buffer and point to header-start-position of ENTITY.")
153
154 (luna-define-generic mime-entity-header-start-point (entity)
155   "Return header-start-position of ENTITY.")
156
157 (luna-define-generic mime-entity-header-end-point (entity)
158   "Return header-end-position of ENTITY.")
159
160
161 ;;; @ Body buffer
162 ;;;
163
164 (luna-define-generic mime-entity-body-buffer (entity))
165
166 (luna-define-generic mime-goto-body-start-point (entity)
167   "Set buffer and point to body-start-position of ENTITY.")
168
169 (luna-define-generic mime-goto-body-end-point (entity)
170   "Set buffer and point to body-end-position of ENTITY.")
171
172 (luna-define-generic mime-entity-body-start-point (entity)
173   "Return body-start-position of ENTITY.")
174
175 (define-obsolete-function-alias
176   'mime-entity-body-start 'mime-entity-body-start-point)
177
178 (luna-define-generic mime-entity-body-end-point (entity)
179   "Return body-end-position of ENTITY.")
180
181 (define-obsolete-function-alias
182   'mime-entity-body-end 'mime-entity-body-end-point)
183
184
185 ;;; @ Entity buffer (obsolete)
186 ;;;
187
188 (luna-define-generic mime-entity-buffer (entity))
189 (make-obsolete 'mime-entity-buffer
190  "use mime-entity-header-buffer or mime-entity-body-buffer instead.")
191
192 (luna-define-generic mime-entity-point-min (entity))
193 (make-obsolete 'mime-entity-point-min 'mime-entity-header-start-point)
194
195 (luna-define-generic mime-entity-point-max (entity))
196 (make-obsolete 'mime-entity-point-max 'mime-entity-body-end-point)
197
198
199 ;;; @ Entity Header
200 ;;;
201
202 (luna-define-generic mime-entity-fetch-field (entity field-name)
203   "Return the value of the ENTITY's header field whose type is FIELD-NAME.")
204
205 (defun mime-fetch-field (field-name &optional entity)
206   "Return the value of the ENTITY's header field whose type is FIELD-NAME."
207   (if (symbolp field-name)
208       (setq field-name (symbol-name field-name))
209     )
210   (or entity
211       (setq entity mime-message-structure))
212   (mime-entity-fetch-field entity field-name)
213   )
214 (make-obsolete 'mime-fetch-field 'mime-entity-fetch-field)
215
216 (defun mime-entity-content-type (entity)
217   (or (mime-entity-content-type-internal entity)
218       (let ((ret (mime-entity-fetch-field entity "Content-Type")))
219         (if ret
220             (mime-entity-set-content-type-internal
221              entity (mime-parse-Content-Type ret))
222           ))))
223
224 (defun mime-entity-content-disposition (entity)
225   (or (mime-entity-content-disposition-internal entity)
226       (let ((ret (mime-entity-fetch-field entity "Content-Disposition")))
227         (if ret
228             (mime-entity-set-content-disposition-internal
229              entity (mime-parse-Content-Disposition ret))
230           ))))
231
232 (defun mime-entity-encoding (entity &optional default-encoding)
233   (or (mime-entity-encoding-internal entity)
234       (let ((ret (mime-entity-fetch-field entity "Content-Transfer-Encoding")))
235         (mime-entity-set-encoding-internal
236          entity
237          (or (and ret (mime-parse-Content-Transfer-Encoding ret))
238              default-encoding "7bit"))
239         )))
240
241 (defvar mime-field-parser-alist
242   '((Return-Path        . std11-parse-route-addr)
243     
244     (Reply-To           . std11-parse-addresses)
245     
246     (Sender             . std11-parse-mailbox)
247     (From               . std11-parse-addresses)
248
249     (Resent-Reply-To    . std11-parse-addresses)
250     
251     (Resent-Sender      . std11-parse-mailbox)
252     (Resent-From        . std11-parse-addresses)
253
254     (To                 . std11-parse-addresses)
255     (Resent-To          . std11-parse-addresses)
256     (Cc                 . std11-parse-addresses)
257     (Resent-Cc          . std11-parse-addresses)
258     (Bcc                . std11-parse-addresses)
259     (Resent-Bcc         . std11-parse-addresses)
260     
261     (Message-Id         . mime-parse-msg-id)
262     (Recent-Message-Id  . mime-parse-msg-id)
263     
264     (In-Reply-To        . std11-parse-msg-ids)
265     (References         . std11-parse-msg-ids)
266     
267     (Content-Id         . mime-parse-msg-id)
268     ))
269
270 (defun mime-entity-read-field (entity field-name)
271   (let ((sym (if (symbolp field-name)
272                  (prog1
273                      field-name
274                    (setq field-name (symbol-name field-name)))
275                (capitalize (capitalize field-name)))))
276     (cond ((eq sym 'Content-Type)
277            (mime-entity-content-type entity)
278            )
279           ((eq sym 'Content-Disposition)
280            (mime-entity-content-disposition entity)
281            )
282           ((eq sym 'Content-Transfer-Encoding)
283            (mime-entity-encoding entity)
284            )
285           (t
286            (let* ((header (mime-entity-parsed-header-internal entity))
287                   (field (cdr (assq sym header))))
288              (or field
289                  (let ((field-body (mime-entity-fetch-field entity field-name))
290                        parser)
291                    (when field-body
292                      (setq parser
293                            (cdr (assq sym mime-field-parser-alist)))
294                      (setq field
295                            (if parser
296                                (funcall parser
297                                         (eword-lexical-analyze field-body))
298                              (mime-decode-field-body field-body sym 'plain)
299                              ))
300                      (mime-entity-set-parsed-header-internal
301                       entity (put-alist sym field header))
302                      field))))))))
303
304 (defun mime-read-field (field-name &optional entity)
305   (or entity
306       (setq entity mime-message-structure))
307   (mime-entity-read-field entity field-name)
308   )
309 (make-obsolete 'mime-read-field 'mime-entity-read-field)
310
311 (luna-define-generic mime-insert-header (entity &optional invisible-fields
312                                                 visible-fields)
313   "Insert before point a decoded header of ENTITY.")
314
315
316 ;;; @ Entity Attributes
317 ;;;
318
319 (luna-define-generic mime-entity-name (entity)
320   "Return name of the ENTITY.")
321
322 (defun mime-entity-uu-filename (entity)
323   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
324       (save-excursion
325         (mime-goto-body-start-point entity)
326         (if (re-search-forward "^begin [0-9]+ "
327                                (mime-entity-body-end-point entity) t)
328             (if (looking-at ".+$")
329                 (buffer-substring (match-beginning 0)(match-end 0))
330               )))))
331
332 (defun mime-entity-filename (entity)
333   "Return filename of ENTITY."
334   (or (mime-entity-uu-filename entity)
335       (mime-content-disposition-filename
336        (mime-entity-content-disposition entity))
337       (cdr (let ((param (mime-content-type-parameters
338                          (mime-entity-content-type entity))))
339              (or (assoc "name" param)
340                  (assoc "x-name" param))
341              ))))
342
343
344 (defsubst mime-entity-media-type (entity)
345   (mime-content-type-primary-type (mime-entity-content-type entity)))
346 (defsubst mime-entity-media-subtype (entity)
347   (mime-content-type-subtype (mime-entity-content-type entity)))
348 (defsubst mime-entity-parameters (entity)
349   (mime-content-type-parameters (mime-entity-content-type entity)))
350 (defsubst mime-entity-type/subtype (entity-info)
351   (mime-type/subtype-string (mime-entity-media-type entity-info)
352                             (mime-entity-media-subtype entity-info)))
353
354
355 ;;; @ Entity Content
356 ;;;
357
358 (luna-define-generic mime-entity-content (entity)
359   "Return content of ENTITY as byte sequence (string).")
360
361 (luna-define-generic mime-insert-entity-content (entity)
362   "Insert content of ENTITY at point.")
363
364 (luna-define-generic mime-write-entity-content (entity filename)
365   "Write content of ENTITY into FILENAME.")
366
367 (luna-define-generic mime-insert-text-content (entity)
368   "Insert decoded text body of ENTITY.")
369
370 (luna-define-generic mime-insert-entity (entity)
371   "Insert header and body of ENTITY at point.")
372
373 (luna-define-generic mime-write-entity (entity filename)
374   "Write header and body of ENTITY into FILENAME.")
375
376 (luna-define-generic mime-write-entity-body (entity filename)
377   "Write body of ENTITY into FILENAME.")
378
379
380 ;;; @ end
381 ;;;
382
383 (provide 'mime)
384
385 ;;; mime.el ends here