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