8e3615bc7418911e36af5e95dcb54240d782f17b
[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 as node of message
64 ;;;
65
66 (defun mime-find-entity-from-number (entity-number &optional message)
67   "Return entity from ENTITY-NUMBER in MESSAGE.
68 If MESSAGE is not specified, `mime-message-structure' is used."
69   (or message
70       (setq message mime-message-structure))
71   (let ((sn (car entity-number)))
72     (if (null sn)
73         message
74       (let ((rc (nth sn (mime-entity-children message))))
75         (if rc
76             (mime-find-entity-from-number (cdr entity-number) rc)
77           ))
78       )))
79
80 (defsubst mime-find-entity-from-node-id (entity-node-id &optional message)
81   "Return entity from ENTITY-NODE-ID in MESSAGE.
82 If MESSAGE is not specified, `mime-message-structure' is used."
83   (mime-find-entity-from-number (reverse entity-node-id) message))
84
85 (defsubst mime-entity-parent (entity &optional message)
86   "Return mother entity of ENTITY.
87 If MESSAGE is not specified, `mime-message-structure' in the buffer of
88 ENTITY is used."
89   (mime-find-entity-from-node-id
90    (cdr (mime-entity-node-id entity))
91    (or message
92        (save-excursion
93          (set-buffer (mime-entity-buffer entity))
94          mime-message-structure))))
95
96 (defsubst mime-root-entity-p (entity)
97   "Return t if ENTITY is root-entity (message)."
98   (null (mime-entity-node-id entity)))
99
100
101 ;;; @ Entity Header
102 ;;;
103
104 (defun mime-fetch-field (field-name &optional entity)
105   (or (symbolp field-name)
106       (setq field-name (intern (capitalize (capitalize field-name)))))
107   (or entity
108       (setq entity mime-message-structure))
109   (let* ((header (mime-entity-original-header entity))
110          (field-body (cdr (assq field-name header))))
111     (or field-body
112         (progn
113           (if (save-excursion
114                 (set-buffer (mime-entity-buffer entity))
115                 (save-restriction
116                   (narrow-to-region (mime-entity-header-start entity)
117                                     (mime-entity-header-end entity))
118                   (setq field-body
119                         (std11-fetch-field (symbol-name field-name)))
120                   ))
121               (mime-entity-set-original-header
122                entity (put-alist field-name field-body header))
123             )
124           field-body))))
125
126 (defun mime-read-field (field-name &optional entity)
127   (or (symbolp field-name)
128       (setq field-name (capitalize (capitalize field-name))))
129   (or entity
130       (setq entity mime-message-structure))
131   (cond ((eq field-name 'Content-Type)
132          (mime-entity-content-type entity)
133          )
134         ((eq field-name 'Content-Disposition)
135          (mime-entity-content-disposition entity)
136          )
137         ((eq field-name 'Content-Transfer-Encoding)
138          (mime-entity-encoding entity)
139          )
140         (t
141          (let* ((header (mime-entity-parsed-header entity))
142                 (field (cdr (assq field-name header))))
143            (or field
144                (let ((field-body (mime-fetch-field field-name entity)))
145                  (when field-body
146                    (cond ((memq field-name '(From Resent-From
147                                              To Resent-To
148                                              Cc Resent-Cc
149                                              Bcc Resent-Bcc
150                                              Reply-To Resent-Reply-To))
151                           (setq field (std11-parse-addresses
152                                        (eword-lexical-analyze field-body)))
153                           )
154                          ((memq field-name '(Sender Resent-Sender))
155                           (setq field (std11-parse-address
156                                        (eword-lexical-analyze field-body)))
157                           )
158                          ((memq field-name eword-decode-ignored-field-list)
159                           (setq field field-body))
160                          ((memq field-name eword-decode-structured-field-list)
161                           (setq field (eword-decode-structured-field-body
162                                        field-body)))
163                          (t
164                           (setq field (eword-decode-unstructured-field-body
165                                        field-body))
166                           ))
167                    (mime-entity-set-parsed-header
168                     entity (put-alist field-name field header))
169                    field)))))))
170
171
172 ;;; @ Entity Content
173 ;;;
174
175 (defun mime-entity-content (entity)
176   (save-excursion
177     (set-buffer (mime-entity-buffer entity))
178     (mime-decode-string (buffer-substring (mime-entity-body-start entity)
179                                           (mime-entity-body-end entity))
180                         (mime-entity-encoding entity))))
181
182
183 ;;; @ Another Entity Information
184 ;;;
185
186 (defun mime-entity-uu-filename (entity)
187   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
188       (save-excursion
189         (set-buffer (mime-entity-buffer entity))
190         (goto-char (mime-entity-body-start entity))
191         (if (re-search-forward "^begin [0-9]+ "
192                                (mime-entity-body-end entity) t)
193             (if (looking-at ".+$")
194                 (buffer-substring (match-beginning 0)(match-end 0))
195               )))))
196
197 (defun mime-entity-filename (entity)
198   "Return filename of ENTITY."
199   (or (mime-entity-uu-filename entity)
200       (mime-content-disposition-filename
201        (mime-entity-content-disposition entity))
202       (cdr (let ((param (mime-content-type-parameters
203                          (mime-entity-content-type entity))))
204              (or (assoc "name" param)
205                  (assoc "x-name" param))
206              ))))
207
208
209 ;;; @ end
210 ;;;
211
212 (provide 'mime)
213
214 ;;; mime.el ends here