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