(mime-entity-send): New function.
[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
292                                    &optional invisible-fields visible-fields
293                                    code-conversion)
294   "Insert before point a decoded header of ENTITY."
295   (let ((default-charset
296           (if code-conversion
297               (if (mime-charset-to-coding-system code-conversion)
298                   code-conversion
299                 default-mime-charset))))
300     (save-restriction
301       (narrow-to-region (point)(point))
302       (let ((the-buf (current-buffer))
303             (src-buf (mime-entity-buffer entity))
304             (h-end (mime-entity-header-end entity))
305             beg p end field-name len field)
306         (save-excursion
307           (set-buffer src-buf)
308           (goto-char (mime-entity-header-start entity))
309           (save-restriction
310             (narrow-to-region (point) h-end)
311             (while (re-search-forward std11-field-head-regexp nil t)
312               (setq beg (match-beginning 0)
313                     p (match-end 0)
314                     field-name (buffer-substring beg (1- p))
315                     len (string-width field-name)
316                     end (std11-field-end))
317               (when (eword-visible-field-p field-name
318                                            visible-fields invisible-fields)
319                 (setq field (intern (capitalize field-name)))
320                 (save-excursion
321                   (set-buffer the-buf)
322                   (insert field-name)
323                   (insert ":")
324                   (cond ((memq field eword-decode-ignored-field-list)
325                          ;; Don't decode
326                          (insert-buffer-substring src-buf p end)
327                          )
328                         ((memq field eword-decode-structured-field-list)
329                          ;; Decode as structured field
330                          (let ((body (save-excursion
331                                        (set-buffer src-buf)
332                                        (buffer-substring p end)))
333                                (default-mime-charset default-charset))
334                            (insert (eword-decode-and-fold-structured-field
335                                     body (1+ len)))
336                            ))
337                         (t
338                          ;; Decode as unstructured field
339                          (let ((body (save-excursion
340                                        (set-buffer src-buf)
341                                        (buffer-substring p end)))
342                                (default-mime-charset default-charset))
343                            (insert (eword-decode-unstructured-field-body
344                                     body (1+ len)))
345                            )))
346                   (insert "\n")
347                   )))))))))
348
349
350 ;;; @ Entity Attributes
351 ;;;
352
353 (defun mime-entity-uu-filename (entity)
354   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
355       (save-excursion
356         (set-buffer (mime-entity-buffer entity))
357         (goto-char (mime-entity-body-start entity))
358         (if (re-search-forward "^begin [0-9]+ "
359                                (mime-entity-body-end entity) t)
360             (if (looking-at ".+$")
361                 (buffer-substring (match-beginning 0)(match-end 0))
362               )))))
363
364 (defun mime-entity-filename (entity)
365   "Return filename of ENTITY."
366   (or (mime-entity-uu-filename entity)
367       (mime-content-disposition-filename
368        (mime-entity-content-disposition entity))
369       (cdr (let ((param (mime-content-type-parameters
370                          (mime-entity-content-type entity))))
371              (or (assoc "name" param)
372                  (assoc "x-name" param))
373              ))))
374
375
376 (defsubst mime-entity-media-type (entity)
377   (mime-content-type-primary-type (mime-entity-content-type entity)))
378 (defsubst mime-entity-media-subtype (entity)
379   (mime-content-type-subtype (mime-entity-content-type entity)))
380 (defsubst mime-entity-parameters (entity)
381   (mime-content-type-parameters (mime-entity-content-type entity)))
382 (defsubst mime-entity-type/subtype (entity-info)
383   (mime-type/subtype-string (mime-entity-media-type entity-info)
384                             (mime-entity-media-subtype entity-info)))
385
386
387 ;;; @ Entity Content
388 ;;;
389
390 (defun mime-entity-content (entity)
391   (mime-entity-send entity 'entity-content))
392
393 (defun mime-write-entity-content (entity filename)
394   "Write content of ENTITY into FILENAME."
395   (mime-entity-send entity 'write-entity-content filename))
396
397 (defun mime-write-entity (entity filename)
398   "Write ENTITY into FILENAME."
399   (save-excursion
400     (set-buffer (mime-entity-buffer entity))
401     (if (mime-entity-cooked-p entity)
402         (write-region (mime-entity-point-min entity)
403                       (mime-entity-point-max entity) filename)
404       (write-region-as-binary (mime-entity-point-min entity)
405                               (mime-entity-point-max entity) filename)
406       )))
407
408 (defun mime-write-entity-body (entity filename)
409   "Write body of ENTITY into FILENAME."
410   (save-excursion
411     (set-buffer (mime-entity-buffer entity))
412     (if (mime-entity-cooked-p entity)
413         (write-region (mime-entity-body-start entity)
414                       (mime-entity-body-end entity) filename)
415       (write-region-as-binary (mime-entity-body-start entity)
416                               (mime-entity-body-end entity) filename)
417       )))
418
419
420 ;;; @ end
421 ;;;
422
423 (provide 'mime)
424
425 ;;; mime.el ends here