release.
[elisp/lemi.git] / mime / mmgeneric.el
1 ;;; mmgeneric.el --- MIME generic entity module
2
3 ;; Copyright (C) 1995,96,97,98,99,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: definition, 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 'luna)
28
29 (eval-when-compile
30   (require 'eword-decode) ; mime-find-field-presentation-method
31   )
32
33
34 ;;; @ MIME entity
35 ;;;
36
37 (autoload 'mime-entity-content-type "mime")
38 (autoload 'mime-parse-multipart "mime-parse")
39 (autoload 'mime-parse-message "mime-parse")
40 ;; (autoload 'mime-parse-encapsulated "mime-parse")
41 ;; (autoload 'mime-parse-external "mime-parse")
42 (autoload 'mime-entity-content "mime")
43
44 (eval-and-compile
45   (luna-define-class mime-entity ()
46                      (location
47                       content-type children parent
48                       node-id
49                       content-disposition encoding
50                       ;; for other fields
51                       original-header parsed-header))
52
53   (luna-define-internal-accessors 'mime-entity)
54   )
55
56 (defalias 'mime-entity-representation-type-internal 'luna-class-name)
57 (defalias 'mime-entity-set-representation-type-internal 'luna-set-class-name)
58
59 (luna-define-method mime-entity-fetch-field ((entity mime-entity)
60                                              field-name)
61   (or (symbolp field-name)
62       (setq field-name (intern (capitalize field-name))))
63   (cdr (assq field-name
64              (mime-entity-original-header-internal entity))))
65
66 (luna-define-method mime-insert-text-content ((entity mime-entity))
67   (insert
68    (decode-mime-charset-string (mime-entity-content entity)
69                                (or (mime-content-type-parameter
70                                     (mime-entity-content-type entity)
71                                     "charset")
72                                    default-mime-charset)
73                                'CRLF)
74    ))
75
76
77 ;;; @ for mm-backend
78 ;;;
79
80 (defmacro mm-expand-class-name (type)
81   `(intern (format "mime-%s-entity" ,type)))
82
83 (defmacro mm-define-backend (type &optional parents)
84   `(luna-define-class ,(mm-expand-class-name type)
85                       ,(nconc (mapcar (lambda (parent)
86                                         (mm-expand-class-name parent)
87                                         )
88                                       parents)
89                               '(mime-entity))))
90
91 (defmacro mm-define-method (name args &rest body)
92   (or (eq name 'initialize-instance)
93       (setq name (intern (format "mime-%s" name))))
94   (let ((spec (car args)))
95     (setq args
96           (cons (list (car spec)
97                       (mm-expand-class-name (nth 1 spec)))
98                 (cdr args)))
99     `(luna-define-method ,name ,args ,@body)
100     ))
101
102 (put 'mm-define-method 'lisp-indent-function 'defun)
103
104 (def-edebug-spec mm-define-method
105   (&define name ((arg symbolp)
106                  [&rest arg]
107                  [&optional ["&optional" arg &rest arg]]
108                  &optional ["&rest" arg]
109                  )
110            def-body))
111
112
113 ;;; @ header filter
114 ;;;
115
116 ;; [tomo] We should think about specification of better filtering
117 ;; mechanism.  Please discuss in the emacs-mime mailing lists.
118
119 (defun mime-visible-field-p (field-name visible-fields invisible-fields)
120   (or (catch 'found
121         (while visible-fields
122           (let ((regexp (car visible-fields)))
123             (if (string-match regexp field-name)
124                 (throw 'found t)
125               ))
126           (setq visible-fields (cdr visible-fields))
127           ))
128       (catch 'found
129         (while invisible-fields
130           (let ((regexp (car invisible-fields)))
131             (if (string-match regexp field-name)
132                 (throw 'found nil)
133               ))
134           (setq invisible-fields (cdr invisible-fields))
135           )
136         t)))
137
138 (defun mime-insert-header-from-buffer (buffer start end
139                                               &optional invisible-fields
140                                               visible-fields)
141   (let ((the-buf (current-buffer))
142         (mode-obj (mime-find-field-presentation-method 'wide))
143         field-decoder
144         f-b p f-e field-name len field field-body)
145     (save-excursion
146       (set-buffer buffer)
147       (save-restriction
148         (narrow-to-region start end)
149         (goto-char start)
150         (while (re-search-forward std11-field-head-regexp nil t)
151           (setq f-b (match-beginning 0)
152                 p (match-end 0)
153                 field-name (buffer-substring f-b p)
154                 len (string-width field-name)
155                 f-e (std11-field-end))
156           (when (mime-visible-field-p field-name
157                                       visible-fields invisible-fields)
158             (setq field (intern
159                          (capitalize (buffer-substring f-b (1- p))))
160                   field-body (buffer-substring p f-e)
161                   field-decoder (inline (mime-find-field-decoder-internal
162                                          field mode-obj)))
163             (with-current-buffer the-buf
164               (insert field-name)
165               (insert (if field-decoder
166                           (funcall field-decoder field-body len)
167                         ;; Don't decode
168                         field-body))
169               (insert "\n")
170               )))))))
171
172
173 ;;; @ end
174 ;;;
175
176 (provide 'mmgeneric)
177
178 ;;; mmgeneric.el ends here