9546f6e2b3443173d3d4587f308cbb2fc7617eb5
[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-Transfer-Encoding "mime-parse"
45   "Parse STRING as field-body of Content-Transfer-Encoding field.")
46 (autoload 'mime-read-Content-Transfer-Encoding "mime-parse"
47   "Read field-body of Content-Transfer-Encoding field from
48 current-buffer, and return it.")
49
50 (autoload 'mime-parse-message "mime-parse"
51   "Parse current-buffer as a MIME message.")
52
53 (autoload 'mime-parse-buffer "mime-parse"
54   "Parse BUFFER as a MIME message.")
55
56
57 ;;; @ Content-Disposition
58 ;;;
59
60 (autoload 'mime-parse-Content-Disposition "mime-parse"
61   "Parse STRING as field-body of Content-Disposition field.")
62
63 (autoload 'mime-read-Content-Disposition "mime-parse"
64   "Read field-body of Content-Disposition field from current-buffer,
65 and return parsed it.")
66
67 (defsubst mime-content-disposition-parameter (content-disposition parameter)
68   "Return PARAMETER value of CONTENT-DISPOSITION."
69   (cdr (assoc parameter (cdr content-disposition))))
70
71 (defsubst mime-content-disposition-filename (content-disposition)
72   "Return filename of CONTENT-DISPOSITION."
73   (mime-content-disposition-parameter content-disposition "filename"))
74
75
76 ;;; @ MIME entity
77 ;;;
78
79 (defun mime-entity-fetch-field (entity field-name)
80   (or (symbolp field-name)
81       (setq field-name (intern (capitalize (capitalize field-name)))))
82   (let* ((header (mime-entity-original-header entity))
83          (field-body (cdr (assq field-name header))))
84     (or field-body
85         (progn
86           (if (save-excursion
87                 (set-buffer (mime-entity-buffer entity))
88                 (save-restriction
89                   (narrow-to-region (mime-entity-header-start entity)
90                                     (mime-entity-header-end entity))
91                   (setq field-body
92                         (std11-fetch-field (symbol-name field-name)))
93                   ))
94               (mime-entity-set-original-header
95                entity (put-alist field-name field-body header))
96             )
97           field-body))))
98
99 (defun mime-entity-read-field (entity field-name)
100   (or (symbolp field-name)
101       (setq field-name (capitalize (capitalize field-name))))
102   (cond ((eq field-name 'Content-Type)
103          (mime-entity-content-type entity)
104          )
105         ((eq field-name 'Content-Disposition)
106          (mime-entity-content-disposition entity)
107          )
108         ((eq field-name 'Content-Transfer-Encoding)
109          (mime-entity-encoding entity)
110          )
111         (t
112          (let* ((header (mime-entity-parsed-header entity))
113                 (field (cdr (assq field-name header))))
114            (or field
115                (let ((field-body (mime-entity-fetch-field entity field-name)))
116                  (when field-body
117                    (cond ((memq field-name '(From Resent-From
118                                              To Resent-To
119                                              Cc Resent-Cc
120                                              Bcc Resent-Bcc
121                                              Reply-To Resent-Reply-To))
122                           (setq field (std11-parse-addresses
123                                        (eword-lexical-analyze field-body)))
124                           )
125                          ((eq field-name '(Sender Resent-Sender))
126                           (setq field (std11-parse-address
127                                        (eword-lexical-analyze field-body)))
128                           )
129                          ((memq field-name eword-decode-ignored-field-list)
130                           (setq field field-body))
131                          ((memq field-name eword-decode-structured-field-list)
132                           (setq field (eword-decode-structured-field-body
133                                        field-body)))
134                          (t
135                           (setq field (eword-decode-unstructured-field-body
136                                        field-body))
137                           ))
138                    (mime-entity-set-parsed-header
139                     entity (put-alist field-name field header))
140                    field)))))))
141
142 (defun mime-entity-content (entity)
143   (save-excursion
144     (set-buffer (mime-entity-buffer entity))
145     (mime-decode-string (buffer-substring (mime-entity-body-start entity)
146                                           (mime-entity-body-end entity))
147                         (mime-entity-encoding entity))))
148
149 (defsubst mime-root-entity-p (entity)
150   "Return t if ENTITY is root-entity (message)."
151   (null (mime-entity-node-id entity)))
152
153
154 ;;; @ end
155 ;;;
156
157 (provide 'mime)
158
159 ;;; mime.el ends here