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