New module.
[elisp/flim.git] / mime-lib.el
1 ;;; mime-lib.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 (defun mime-entity-fetch-field (entity field-name)
33   (or (symbolp field-name)
34       (setq field-name (intern (capitalize (capitalize field-name)))))
35   (let* ((header (mime-entity-original-header entity))
36          (field-body (cdr (assq field-name header))))
37     (or field-body
38         (save-excursion
39           (if (save-restriction
40                 (set-buffer (mime-entity-buffer entity))
41                 (narrow-to-region (mime-entity-header-start entity)
42                                   (mime-entity-header-end entity))
43                 (setq field-body (std11-fetch-field (symbol-name field-name)))
44                 )
45               (mime-entity-set-original-header
46                entity (put-alist field-name field-body header))
47             )
48           field-body))))
49
50 (defun mime-entity-read-field (entity field-name)
51   (or (symbolp field-name)
52       (setq field-name (capitalize (capitalize field-name))))
53   (cond ((eq field-name 'Content-Type)
54          (mime-entity-content-type entity)
55          )
56         ((eq field-name 'Content-Disposition)
57          (mime-entity-content-disposition entity)
58          )
59         ((eq field-name 'Content-Transfer-Encoding)
60          (mime-entity-encoding entity)
61          )
62         (t
63          (let* ((header (mime-entity-parsed-header entity))
64                 (field (cdr (assq field-name header))))
65            (or field
66                (let ((field-body (mime-entity-fetch-field entity field-name)))
67                  (when field-body
68                    (cond ((memq field-name '(From
69                                              To Recent-To
70                                              Cc Recent-Cc
71                                              Bcc Resent-Bcc))
72                           (setq field (std11-parse-addresses
73                                        (eword-lexical-analyze field-body)))
74                           )
75                          ((eq field-name 'Sender)
76                           (setq field (std11-parse-address
77                                        (eword-lexical-analyze field-body)))
78                           )
79                          ((memq field-name eword-decode-structured-field-list)
80                           (setq field (eword-decode-structured-field-body
81                                        field-body)))
82                          (t
83                           (setq field (eword-decode-unstructured-field-body
84                                        field-body))
85                           ))
86                    (mime-entity-set-parsed-header
87                     entity (put-alist field-name field header))
88                    field)))))))
89
90
91 ;;; @ end
92 ;;;
93
94 (provide 'mime-lib)
95
96 ;;; mime-lib.el ends here