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