Delete mmgeneric.el.
[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 (luna-define-generic mime-entity-header-buffer (entity))
150
151 (luna-define-generic mime-entity-body-buffer (entity))
152
153 (luna-define-generic mime-entity-point-min (entity))
154
155 (luna-define-generic mime-entity-point-max (entity))
156
157
158 ;;; @ Entity Header
159 ;;;
160
161 (luna-define-generic mime-goto-header-start-point (entity)
162   "Set buffer and point to header-start-position of ENTITY.")
163
164 (luna-define-generic mime-entity-fetch-field (entity field-name)
165   "Return the value of the ENTITY's header field whose type is FIELD-NAME.")
166
167 (defun mime-fetch-field (field-name &optional entity)
168   "Return the value of the ENTITY's header field whose type is FIELD-NAME."
169   (if (symbolp field-name)
170       (setq field-name (symbol-name field-name))
171     )
172   (or entity
173       (setq entity mime-message-structure))
174   (mime-entity-fetch-field entity field-name)
175   )
176 (make-obsolete 'mime-fetch-field 'mime-entity-fetch-field)
177
178 (defun mime-entity-content-type (entity)
179   (or (mime-entity-content-type-internal entity)
180       (let ((ret (mime-entity-fetch-field entity "Content-Type")))
181         (if ret
182             (mime-entity-set-content-type-internal
183              entity (mime-parse-Content-Type ret))
184           ))))
185
186 (defun mime-entity-content-disposition (entity)
187   (or (mime-entity-content-disposition-internal entity)
188       (let ((ret (mime-entity-fetch-field entity "Content-Disposition")))
189         (if ret
190             (mime-entity-set-content-disposition-internal
191              entity (mime-parse-Content-Disposition ret))
192           ))))
193
194 (defun mime-entity-encoding (entity &optional default-encoding)
195   (or (mime-entity-encoding-internal entity)
196       (let ((ret (mime-entity-fetch-field entity "Content-Transfer-Encoding")))
197         (mime-entity-set-encoding-internal
198          entity
199          (or (and ret (mime-parse-Content-Transfer-Encoding ret))
200              default-encoding "7bit"))
201         )))
202
203 (defvar mime-field-parser-alist
204   '((Return-Path        . std11-parse-route-addr)
205     
206     (Reply-To           . std11-parse-addresses)
207     
208     (Sender             . std11-parse-mailbox)
209     (From               . std11-parse-addresses)
210
211     (Resent-Reply-To    . std11-parse-addresses)
212     
213     (Resent-Sender      . std11-parse-mailbox)
214     (Resent-From        . std11-parse-addresses)
215
216     (To                 . std11-parse-addresses)
217     (Resent-To          . std11-parse-addresses)
218     (Cc                 . std11-parse-addresses)
219     (Resent-Cc          . std11-parse-addresses)
220     (Bcc                . std11-parse-addresses)
221     (Resent-Bcc         . std11-parse-addresses)
222     
223     (Message-Id         . mime-parse-msg-id)
224     (Recent-Message-Id  . mime-parse-msg-id)
225     
226     (In-Reply-To        . std11-parse-msg-ids)
227     (References         . std11-parse-msg-ids)
228     
229     (Content-Id         . mime-parse-msg-id)
230     ))
231
232 (defun mime-entity-read-field (entity field-name)
233   (let ((sym (if (symbolp field-name)
234                  (prog1
235                      field-name
236                    (setq field-name (symbol-name field-name)))
237                (capitalize (capitalize field-name)))))
238     (cond ((eq sym 'Content-Type)
239            (mime-entity-content-type entity)
240            )
241           ((eq sym 'Content-Disposition)
242            (mime-entity-content-disposition entity)
243            )
244           ((eq sym 'Content-Transfer-Encoding)
245            (mime-entity-encoding entity)
246            )
247           (t
248            (let* ((header (mime-entity-parsed-header-internal entity))
249                   (field (cdr (assq sym header))))
250              (or field
251                  (let ((field-body (mime-entity-fetch-field entity field-name))
252                        parser)
253                    (when field-body
254                      (setq parser
255                            (cdr (assq sym mime-field-parser-alist)))
256                      (setq field
257                            (if parser
258                                (funcall parser
259                                         (eword-lexical-analyze field-body))
260                              (mime-decode-field-body field-body sym 'plain)
261                              ))
262                      (mime-entity-set-parsed-header-internal
263                       entity (put-alist sym field header))
264                      field))))))))
265
266 (defun mime-read-field (field-name &optional entity)
267   (or entity
268       (setq entity mime-message-structure))
269   (mime-entity-read-field entity field-name)
270   )
271 (make-obsolete 'mime-read-field 'mime-entity-read-field)
272
273 (luna-define-generic mime-insert-header (entity &optional invisible-fields
274                                                 visible-fields)
275   "Insert before point a decoded header of ENTITY.")
276
277
278 ;;; @ Entity Attributes
279 ;;;
280
281 (luna-define-generic mime-entity-name (entity)
282   "Return name of the ENTITY.")
283
284 (defun mime-entity-uu-filename (entity)
285   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
286       (save-excursion
287         (set-buffer (mime-entity-buffer entity))
288         (goto-char (mime-entity-body-start entity))
289         (if (re-search-forward "^begin [0-9]+ "
290                                (mime-entity-body-end entity) t)
291             (if (looking-at ".+$")
292                 (buffer-substring (match-beginning 0)(match-end 0))
293               )))))
294
295 (defun mime-entity-filename (entity)
296   "Return filename of ENTITY."
297   (or (mime-entity-uu-filename entity)
298       (mime-content-disposition-filename
299        (mime-entity-content-disposition entity))
300       (cdr (let ((param (mime-content-type-parameters
301                          (mime-entity-content-type entity))))
302              (or (assoc "name" param)
303                  (assoc "x-name" param))
304              ))))
305
306
307 (defsubst mime-entity-media-type (entity)
308   (mime-content-type-primary-type (mime-entity-content-type entity)))
309 (defsubst mime-entity-media-subtype (entity)
310   (mime-content-type-subtype (mime-entity-content-type entity)))
311 (defsubst mime-entity-parameters (entity)
312   (mime-content-type-parameters (mime-entity-content-type entity)))
313 (defsubst mime-entity-type/subtype (entity-info)
314   (mime-type/subtype-string (mime-entity-media-type entity-info)
315                             (mime-entity-media-subtype entity-info)))
316
317
318 ;;; @ Entity Content
319 ;;;
320
321 (luna-define-generic mime-entity-content (entity)
322   "Return content of ENTITY as byte sequence (string).")
323
324 (luna-define-generic mime-insert-entity-content (entity)
325   "Insert content of ENTITY at point.")
326
327 (luna-define-generic mime-write-entity-content (entity filename)
328   "Write content of ENTITY into FILENAME.")
329
330 (luna-define-generic mime-insert-text-content (entity)
331   "Insert decoded text body of ENTITY.")
332
333 (luna-define-generic mime-insert-entity (entity)
334   "Insert header and body of ENTITY at point.")
335
336 (luna-define-generic mime-write-entity (entity filename)
337   "Write header and body of ENTITY into FILENAME.")
338
339 (luna-define-generic mime-write-entity-body (entity filename)
340   "Write body of ENTITY into FILENAME.")
341
342
343 ;;; @ end
344 ;;;
345
346 (provide 'mime)
347
348 ;;; mime.el ends here