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