Merge semi21-D20010129.
[elisp/lemi.git] / mime / 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   "Return list of entities included in the ENTITY."
95   (or (mime-entity-children-internal entity)
96       (luna-send entity 'mime-entity-children entity)))
97
98 (defun mime-entity-node-id (entity)
99   "Return node-id of ENTITY."
100   (mime-entity-node-id-internal entity))
101
102 (defun mime-entity-number (entity)
103   "Return entity-number of ENTITY."
104   (reverse (mime-entity-node-id-internal entity)))
105
106 (defun mime-find-entity-from-number (entity-number message)
107   "Return entity from ENTITY-NUMBER in MESSAGE."
108   (let ((sn (car entity-number)))
109     (if (null sn)
110         message
111       (let ((rc (nth sn (mime-entity-children message))))
112         (if rc
113             (mime-find-entity-from-number (cdr entity-number) rc)
114           ))
115       )))
116
117 (defun mime-find-entity-from-node-id (entity-node-id message)
118   "Return entity from ENTITY-NODE-ID in MESSAGE."
119   (mime-find-entity-from-number (reverse entity-node-id) message))
120
121 (defun mime-find-entity-from-content-id (cid message)
122   "Return entity from CID in MESSAGE."
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 (defun mime-find-root-entity (entity)
146   "Return root entity of ENTITY."
147   (let ((p (mime-entity-parent entity)))
148     (if (null p)
149         entity
150       (mime-entity-parent p))))
151
152
153 ;;; @ Header buffer (obsolete)
154 ;;;
155
156 ;; (luna-define-generic mime-entity-header-buffer (entity))
157
158 ;; (luna-define-generic mime-goto-header-start-point (entity)
159 ;;   "Set buffer and point to header-start-position of ENTITY.")
160
161 ;; (luna-define-generic mime-entity-header-start-point (entity)
162 ;;   "Return header-start-position of ENTITY.")
163
164 ;; (luna-define-generic mime-entity-header-end-point (entity)
165 ;;   "Return header-end-position of ENTITY.")
166
167 ;; (make-obsolete 'mime-entity-header-buffer "don't use it.")
168 ;; (make-obsolete 'mime-goto-header-start-point "don't use it.")
169 ;; (make-obsolete 'mime-entity-header-start-point "don't use it.")
170 ;; (make-obsolete 'mime-entity-header-end-point "don't use it.")
171
172
173 ;;; @ Body buffer (obsolete)
174 ;;;
175
176 ;; (luna-define-generic mime-entity-body-buffer (entity))
177
178 ;; (luna-define-generic mime-goto-body-start-point (entity)
179 ;;   "Set buffer and point to body-start-position of ENTITY.")
180
181 ;; (luna-define-generic mime-goto-body-end-point (entity)
182 ;;   "Set buffer and point to body-end-position of ENTITY.")
183
184 ;; (luna-define-generic mime-entity-body-start-point (entity)
185 ;;   "Return body-start-position of ENTITY.")
186
187 ;; (luna-define-generic mime-entity-body-end-point (entity)
188 ;;   "Return body-end-position of ENTITY.")
189
190 ;; (defalias 'mime-entity-body-start 'mime-entity-body-start-point)
191 ;; (defalias 'mime-entity-body-end 'mime-entity-body-end-point)
192
193 ;; (make-obsolete 'mime-entity-body-buffer "don't use it.")
194 ;; (make-obsolete 'mime-goto-body-start-point "don't use it.")
195 ;; (make-obsolete 'mime-goto-body-end-point "don't use it.")
196 ;; (make-obsolete 'mime-entity-body-start-point "don't use it.")
197 ;; (make-obsolete 'mime-entity-body-end-point "don't use it.")
198 ;; (make-obsolete 'mime-entity-body-start "don't use it.")
199 ;; (make-obsolete 'mime-entity-body-end "don't use it.")
200
201
202 ;;; @ Entity buffer (obsolete)
203 ;;;
204
205 ;; (luna-define-generic mime-entity-buffer (entity))
206 ;; (make-obsolete 'mime-entity-buffer "don't use it.")
207
208 ;; (luna-define-generic mime-entity-point-min (entity))
209 ;; (make-obsolete 'mime-entity-point-min "don't use it.")
210
211 ;; (luna-define-generic mime-entity-point-max (entity))
212 ;; (make-obsolete 'mime-entity-point-max "don't use it.")
213
214
215 ;;; @ Entity
216 ;;;
217
218 (luna-define-generic mime-insert-entity (entity)
219   "Insert header and body of ENTITY at point.")
220
221 (luna-define-generic mime-write-entity (entity filename)
222   "Write header and body of ENTITY into FILENAME.")
223
224
225 ;;; @ Entity Body
226 ;;;
227
228 (luna-define-generic mime-entity-body (entity)
229   "Return network representation of ENTITY body.")
230
231 (luna-define-generic mime-insert-entity-body (entity)
232   "Insert network representation of ENTITY body at point.")
233
234 (luna-define-generic mime-write-entity-body (entity filename)
235   "Write body of ENTITY into FILENAME.")
236
237
238 ;;; @ Entity Content
239 ;;;
240
241 (luna-define-generic mime-entity-content (entity)
242   "Return content of ENTITY as byte sequence (string).")
243
244 (luna-define-generic mime-insert-entity-content (entity)
245   "Insert content of ENTITY at point.")
246
247 (luna-define-generic mime-write-entity-content (entity filename)
248   "Write content of ENTITY into FILENAME.")
249
250 (luna-define-generic mime-insert-text-content (entity)
251   "Insert decoded text body of ENTITY.")
252
253
254 ;;; @ Header fields
255 ;;;
256
257 (luna-define-generic mime-entity-fetch-field (entity field-name)
258   "Return the value of the ENTITY's header field whose type is FIELD-NAME.")
259
260 ;; (defun mime-fetch-field (field-name &optional entity)
261 ;;   "Return the value of the ENTITY's header field whose type is FIELD-NAME."
262 ;;   (if (symbolp field-name)
263 ;;       (setq field-name (symbol-name field-name))
264 ;;     )
265 ;;   (or entity
266 ;;       (setq entity mime-message-structure))
267 ;;   (mime-entity-fetch-field entity field-name)
268 ;;   )
269 ;; (make-obsolete 'mime-fetch-field 'mime-entity-fetch-field)
270
271 (defun mime-entity-content-type (entity)
272   "Return content-type of ENTITY."
273   (or (mime-entity-content-type-internal entity)
274       (let ((ret (mime-entity-fetch-field entity "Content-Type")))
275         (if ret
276             (mime-entity-set-content-type-internal
277              entity (mime-parse-Content-Type ret))
278           ))))
279
280 (defun mime-entity-content-disposition (entity)
281   "Return content-disposition of ENTITY."
282   (or (mime-entity-content-disposition-internal entity)
283       (let ((ret (mime-entity-fetch-field entity "Content-Disposition")))
284         (if ret
285             (mime-entity-set-content-disposition-internal
286              entity (mime-parse-Content-Disposition ret))
287           ))))
288
289 (defun mime-entity-encoding (entity &optional default-encoding)
290   "Return content-transfer-encoding of ENTITY.
291 If the ENTITY does not have Content-Transfer-Encoding field, this
292 function returns DEFAULT-ENCODING.  If it is nil, \"7bit\" is used as
293 default value."
294   (or (mime-entity-encoding-internal entity)
295       (let ((ret (mime-entity-fetch-field entity "Content-Transfer-Encoding")))
296         (mime-entity-set-encoding-internal
297          entity
298          (or (and ret (mime-parse-Content-Transfer-Encoding ret))
299              default-encoding "7bit"))
300         )))
301
302 (defvar mime-field-parser-alist
303   '((Return-Path        . std11-parse-route-addr)
304     
305     (Reply-To           . std11-parse-addresses)
306     
307     (Sender             . std11-parse-mailbox)
308     (From               . std11-parse-addresses)
309
310     (Resent-Reply-To    . std11-parse-addresses)
311     
312     (Resent-Sender      . std11-parse-mailbox)
313     (Resent-From        . std11-parse-addresses)
314
315     (To                 . std11-parse-addresses)
316     (Resent-To          . std11-parse-addresses)
317     (Cc                 . std11-parse-addresses)
318     (Resent-Cc          . std11-parse-addresses)
319     (Bcc                . std11-parse-addresses)
320     (Resent-Bcc         . std11-parse-addresses)
321     
322     (Message-Id         . mime-parse-msg-id)
323     (Recent-Message-Id  . mime-parse-msg-id)
324     
325     (In-Reply-To        . std11-parse-msg-ids)
326     (References         . std11-parse-msg-ids)
327     
328     (Content-Id         . mime-parse-msg-id)
329     ))
330
331 (defun mime-entity-read-field (entity field-name)
332   (let ((sym (if (symbolp field-name)
333                  (prog1
334                      field-name
335                    (setq field-name (symbol-name field-name)))
336                (intern (capitalize (capitalize field-name))))))
337     (cond ((eq sym 'Content-Type)
338            (mime-entity-content-type entity)
339            )
340           ((eq sym 'Content-Disposition)
341            (mime-entity-content-disposition entity)
342            )
343           ((eq sym 'Content-Transfer-Encoding)
344            (mime-entity-encoding entity)
345            )
346           (t
347            (let* ((header (mime-entity-parsed-header-internal entity))
348                   (field (cdr (assq sym header))))
349              (or field
350                  (let ((field-body (mime-entity-fetch-field entity field-name))
351                        parser)
352                    (when field-body
353                      (setq parser
354                            (cdr (assq sym mime-field-parser-alist)))
355                      (setq field
356                            (if parser
357                                (funcall parser
358                                         (eword-lexical-analyze field-body))
359                              (mime-decode-field-body field-body sym 'plain)
360                              ))
361                      (mime-entity-set-parsed-header-internal
362                       entity (put-alist sym field header))
363                      field))))))))
364
365 ;; (defun mime-read-field (field-name &optional entity)
366 ;;   (or entity
367 ;;       (setq entity mime-message-structure))
368 ;;   (mime-entity-read-field entity field-name)
369 ;;   )
370 ;; (make-obsolete 'mime-read-field 'mime-entity-read-field)
371
372 (luna-define-generic mime-insert-header (entity &optional invisible-fields
373                                                 visible-fields)
374   "Insert before point a decoded header of ENTITY.")
375
376
377 ;;; @ Entity Attributes
378 ;;;
379
380 (luna-define-generic mime-entity-name (entity)
381   "Return name of the ENTITY.")
382
383 (defun mime-entity-uu-filename (entity)
384   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
385       (with-temp-buffer
386         (mime-insert-entity-body entity)
387         (if (re-search-forward "^begin [0-9]+ " nil t)
388             (if (looking-at ".+$")
389                 (buffer-substring (match-beginning 0)(match-end 0))
390               )))))
391
392 (defun mime-entity-filename (entity)
393   "Return filename of ENTITY."
394   (or (mime-entity-uu-filename entity)
395       (mime-content-disposition-filename
396        (mime-entity-content-disposition entity))
397       (cdr (let ((param (mime-content-type-parameters
398                          (mime-entity-content-type entity))))
399              (or (assoc "name" param)
400                  (assoc "x-name" param))
401              ))))
402
403
404 (defsubst mime-entity-media-type (entity)
405   "Return primary media-type of ENTITY."
406   (mime-content-type-primary-type (mime-entity-content-type entity)))
407
408 (defsubst mime-entity-media-subtype (entity)
409   "Return media-subtype of ENTITY."
410   (mime-content-type-subtype (mime-entity-content-type entity)))
411
412 (defsubst mime-entity-parameters (entity)
413   "Return parameters of Content-Type of ENTITY."
414   (mime-content-type-parameters (mime-entity-content-type entity)))
415
416 (defsubst mime-entity-type/subtype (entity-info)
417   "Return type/subtype of Content-Type of ENTITY."
418   (mime-type/subtype-string (mime-entity-media-type entity-info)
419                             (mime-entity-media-subtype entity-info)))
420
421 (defun mime-entity-set-content-type (entity content-type)
422   "Set ENTITY's content-type to CONTENT-TYPE."
423   (mime-entity-set-content-type-internal entity content-type))
424
425 (defun mime-entity-set-encoding (entity encoding)
426   "Set ENTITY's content-transfer-encoding to ENCODING."
427   (mime-entity-set-encoding-internal entity encoding))
428
429
430 ;;; @ end
431 ;;;
432
433 (provide 'mime)
434
435 ;;; mime.el ends here