(mime-entity-content-disposition): New implementation.
[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 (defalias 'mime-entity-children 'mime-entity-children-internal)
67
68 (defalias 'mime-entity-node-id 'mime-entity-node-id-internal)
69
70 (defsubst mime-entity-number (entity)
71   (reverse (mime-entity-node-id-internal entity)))
72
73 (defun mime-find-entity-from-number (entity-number &optional message)
74   "Return entity from ENTITY-NUMBER in MESSAGE.
75 If MESSAGE is not specified, `mime-message-structure' is used."
76   (or message
77       (setq message mime-message-structure))
78   (let ((sn (car entity-number)))
79     (if (null sn)
80         message
81       (let ((rc (nth sn (mime-entity-children message))))
82         (if rc
83             (mime-find-entity-from-number (cdr entity-number) rc)
84           ))
85       )))
86
87 (defsubst mime-find-entity-from-node-id (entity-node-id &optional message)
88   "Return entity from ENTITY-NODE-ID in MESSAGE.
89 If MESSAGE is not specified, `mime-message-structure' is used."
90   (mime-find-entity-from-number (reverse entity-node-id) message))
91
92 (defsubst mime-entity-parent (entity &optional message)
93   "Return mother entity of ENTITY.
94 If MESSAGE is not specified, `mime-message-structure' in the buffer of
95 ENTITY is used."
96   (mime-find-entity-from-node-id
97    (cdr (mime-entity-node-id entity))
98    (or message
99        (save-excursion
100          (set-buffer (mime-entity-buffer entity))
101          mime-message-structure))))
102
103 (defsubst mime-root-entity-p (entity)
104   "Return t if ENTITY is root-entity (message)."
105   (null (mime-entity-node-id entity)))
106
107
108 ;;; @ Entity Buffer
109 ;;;
110
111 (defalias 'mime-entity-buffer    'mime-entity-buffer-internal)
112 (defalias 'mime-entity-point-min 'mime-entity-header-start-internal)
113 (defalias 'mime-entity-point-max 'mime-entity-body-end-internal)
114
115
116 ;;; @ Entity Header
117 ;;;
118
119 (defalias 'mime-entity-header-start 'mime-entity-header-start-internal)
120 (defalias 'mime-entity-header-end   'mime-entity-header-end-internal)
121
122 (defun mime-fetch-field (field-name &optional entity)
123   (or (symbolp field-name)
124       (setq field-name (intern (capitalize (capitalize field-name)))))
125   (or entity
126       (setq entity mime-message-structure))
127   (let* ((header (mime-entity-original-header-internal entity))
128          (field-body (cdr (assq field-name header))))
129     (or field-body
130         (progn
131           (if (save-excursion
132                 (set-buffer (mime-entity-buffer entity))
133                 (save-restriction
134                   (narrow-to-region (mime-entity-header-start entity)
135                                     (mime-entity-header-end entity))
136                   (setq field-body
137                         (std11-fetch-field (symbol-name field-name)))
138                   ))
139               (mime-entity-set-original-header-internal
140                entity (put-alist field-name field-body header))
141             )
142           field-body))))
143
144 (defalias 'mime-entity-content-type 'mime-entity-content-type-internal)
145
146 (defun mime-entity-content-disposition (entity)
147   (or (mime-entity-content-disposition-internal entity)
148       (let ((ret (mime-fetch-field 'Content-Disposition entity)))
149         (if ret
150             (let ((disposition (mime-parse-Content-Disposition ret)))
151               (when disposition
152                 (mime-entity-set-content-disposition-internal
153                  entity disposition)
154                 disposition))))))
155
156 (defalias 'mime-entity-encoding 'mime-entity-encoding-internal)
157
158 (defun mime-read-field (field-name &optional entity)
159   (or (symbolp field-name)
160       (setq field-name (capitalize (capitalize field-name))))
161   (or entity
162       (setq entity mime-message-structure))
163   (cond ((eq field-name 'Content-Type)
164          (mime-entity-content-type entity)
165          )
166         ((eq field-name 'Content-Disposition)
167          (mime-entity-content-disposition entity)
168          )
169         ((eq field-name 'Content-Transfer-Encoding)
170          (mime-entity-encoding entity)
171          )
172         (t
173          (let* ((header (mime-entity-parsed-header-internal entity))
174                 (field (cdr (assq field-name header))))
175            (or field
176                (let ((field-body (mime-fetch-field field-name entity)))
177                  (when field-body
178                    (cond ((memq field-name '(From Resent-From
179                                              To Resent-To
180                                              Cc Resent-Cc
181                                              Bcc Resent-Bcc
182                                              Reply-To Resent-Reply-To))
183                           (setq field (std11-parse-addresses
184                                        (eword-lexical-analyze field-body)))
185                           )
186                          ((memq field-name '(Sender Resent-Sender))
187                           (setq field (std11-parse-address
188                                        (eword-lexical-analyze field-body)))
189                           )
190                          ((memq field-name eword-decode-ignored-field-list)
191                           (setq field field-body))
192                          ((memq field-name eword-decode-structured-field-list)
193                           (setq field (eword-decode-structured-field-body
194                                        field-body)))
195                          (t
196                           (setq field (eword-decode-unstructured-field-body
197                                        field-body))
198                           ))
199                    (mime-entity-set-parsed-header-internal
200                     entity (put-alist field-name field header))
201                    field)))))))
202
203 (defun eword-visible-field-p (field-name visible-fields invisible-fields)
204   (or (catch 'found
205         (while visible-fields
206           (let ((regexp (car visible-fields)))
207             (if (string-match regexp field-name)
208                 (throw 'found t)
209               ))
210           (setq visible-fields (cdr visible-fields))
211           ))
212       (catch 'found
213         (while invisible-fields
214           (let ((regexp (car invisible-fields)))
215             (if (string-match regexp field-name)
216                 (throw 'found nil)
217               ))
218           (setq invisible-fields (cdr invisible-fields))
219           )
220         t)))
221                 
222 (defun mime-insert-decoded-header (entity
223                                    &optional invisible-fields visible-fields
224                                    code-conversion)
225   "Insert before point a decoded header of ENTITY."
226   (let ((default-charset
227           (if code-conversion
228               (if (mime-charset-to-coding-system code-conversion)
229                   code-conversion
230                 default-mime-charset))))
231     (save-restriction
232       (narrow-to-region (point)(point))
233       (let ((the-buf (current-buffer))
234             (src-buf (mime-entity-buffer entity))
235             (h-end (mime-entity-header-end entity))
236             beg p end field-name len field)
237         (save-excursion
238           (set-buffer src-buf)
239           (goto-char (mime-entity-header-start entity))
240           (save-restriction
241             (narrow-to-region (point) h-end)
242             (while (re-search-forward std11-field-head-regexp nil t)
243               (setq beg (match-beginning 0)
244                     p (match-end 0)
245                     field-name (buffer-substring beg (1- p))
246                     len (string-width field-name)
247                     end (std11-field-end))
248               (when (eword-visible-field-p field-name
249                                            visible-fields invisible-fields)
250                 (setq field (intern (capitalize field-name)))
251                 (save-excursion
252                   (set-buffer the-buf)
253                   (insert field-name)
254                   (insert ":")
255                   (cond ((memq field eword-decode-ignored-field-list)
256                          ;; Don't decode
257                          (insert-buffer-substring src-buf p end)
258                          )
259                         ((memq field eword-decode-structured-field-list)
260                          ;; Decode as structured field
261                          (let ((body (save-excursion
262                                        (set-buffer src-buf)
263                                        (buffer-substring p end)))
264                                (default-mime-charset default-charset))
265                            (insert (eword-decode-and-fold-structured-field
266                                     body (1+ len)))
267                            ))
268                         (t
269                          ;; Decode as unstructured field
270                          (let ((body (save-excursion
271                                        (set-buffer src-buf)
272                                        (buffer-substring p end)))
273                                (default-mime-charset default-charset))
274                            (insert (eword-decode-unstructured-field-body
275                                     body (1+ len)))
276                            )))
277                   (insert "\n")
278                   )))))))))
279
280
281 ;;; @ Entity Content
282 ;;;
283
284 (defalias 'mime-entity-body-start 'mime-entity-body-start-internal)
285 (defalias 'mime-entity-body-end   'mime-entity-body-end-internal)
286
287 (defun mime-entity-content (entity)
288   (save-excursion
289     (set-buffer (mime-entity-buffer entity))
290     (mime-decode-string (buffer-substring (mime-entity-body-start entity)
291                                           (mime-entity-body-end entity))
292                         (mime-entity-encoding entity))))
293
294
295 ;;; @ Another Entity Information
296 ;;;
297
298 (defun mime-entity-uu-filename (entity)
299   (if (member (mime-entity-encoding entity) mime-uuencode-encoding-name-list)
300       (save-excursion
301         (set-buffer (mime-entity-buffer entity))
302         (goto-char (mime-entity-body-start entity))
303         (if (re-search-forward "^begin [0-9]+ "
304                                (mime-entity-body-end entity) t)
305             (if (looking-at ".+$")
306                 (buffer-substring (match-beginning 0)(match-end 0))
307               )))))
308
309 (defun mime-entity-filename (entity)
310   "Return filename of ENTITY."
311   (or (mime-entity-uu-filename entity)
312       (mime-content-disposition-filename
313        (mime-entity-content-disposition entity))
314       (cdr (let ((param (mime-content-type-parameters
315                          (mime-entity-content-type entity))))
316              (or (assoc "name" param)
317                  (assoc "x-name" param))
318              ))))
319
320
321 (defsubst mime-entity-media-type (entity)
322   (mime-content-type-primary-type (mime-entity-content-type entity)))
323 (defsubst mime-entity-media-subtype (entity)
324   (mime-content-type-subtype (mime-entity-content-type entity)))
325 (defsubst mime-entity-parameters (entity)
326   (mime-content-type-parameters (mime-entity-content-type entity)))
327 (defsubst mime-entity-type/subtype (entity-info)
328   (mime-type/subtype-string (mime-entity-media-type entity-info)
329                             (mime-entity-media-subtype entity-info)))
330
331
332 ;;; @ end
333 ;;;
334
335 (provide 'mime)
336
337 ;;; mime.el ends here