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