1 ;;; mime-def.el --- definition module about MIME
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4 ;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
5 ;; Licensed to the Free Software Foundation.
7 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
8 ;; Keywords: definition, MIME, multimedia, mail, news
10 ;; This file is part of FLIM (Faithful Library about Internet Message).
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
36 (defconst mime-library-product ["CLIME" (1 13 3) "
\e$BJ?C<
\e(B"]
37 "Product name, version number and code name of MIME-library package.")
40 (defmacro mime-product-name (product)
41 (` (aref (, product) 0)))
43 (defmacro mime-product-version (product)
44 (` (aref (, product) 1)))
46 (defmacro mime-product-code-name (product)
47 (` (aref (, product) 2)))
49 (defconst mime-library-version
51 (concat (mime-product-name mime-library-product) " "
52 (mapconcat (function int-to-string)
53 (mime-product-version mime-library-product) ".")
54 " - \"" (mime-product-code-name mime-library-product) "\"")))
60 (defgroup mime '((default-mime-charset custom-variable))
61 "Emacs MIME Interfaces"
65 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
66 "*List of encoding names for uuencode format."
68 :type '(repeat string))
71 ;;; @ required functions
74 (defsubst regexp-* (regexp)
77 (defsubst regexp-or (&rest args)
78 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
85 (defconst std11-quoted-pair-regexp "\\\\.")
86 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
87 (defconst std11-qtext-regexp
89 (concat "[^" std11-non-qtext-char-list "]"))))
90 (defconst std11-quoted-string-regexp
94 (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
102 (defconst mime-tspecial-char-list
103 '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=)))
104 (defconst mime-token-regexp
106 (concat "[^" mime-tspecial-char-list "\000-\040]+")))
107 (defconst mime-charset-regexp mime-token-regexp)
109 (defconst mime-media-type/subtype-regexp
110 (concat mime-token-regexp "/" mime-token-regexp))
116 (defconst base64-token-regexp "[A-Za-z0-9+/]")
117 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
119 (defconst B-encoded-text-regexp
128 base64-token-padding-regexp
129 base64-token-padding-regexp
132 ;; (defconst eword-B-encoding-and-encoded-text-regexp
133 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
136 ;;; @@ Quoted-Printable / Q
139 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
141 (defconst quoted-printable-octet-regexp
142 (concat "=[" quoted-printable-hex-chars
143 "][" quoted-printable-hex-chars "]"))
145 (defconst Q-encoded-text-regexp
146 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
148 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
149 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
155 (defsubst make-mime-content-type (type subtype &optional parameters)
156 (cons (cons 'type type)
157 (cons (cons 'subtype subtype)
158 (nreverse parameters))))
160 (defsubst mime-content-type-primary-type (content-type)
161 "Return primary-type of CONTENT-TYPE."
162 (cdr (car content-type)))
164 (defsubst mime-content-type-subtype (content-type)
165 "Return primary-type of CONTENT-TYPE."
166 (cdr (cadr content-type)))
168 (defsubst mime-content-type-parameters (content-type)
169 "Return primary-type of CONTENT-TYPE."
172 (defsubst mime-content-type-parameter (content-type parameter)
173 "Return PARAMETER value of CONTENT-TYPE."
174 (cdr (assoc parameter (mime-content-type-parameters content-type))))
177 (defsubst mime-type/subtype-string (type &optional subtype)
178 "Return type/subtype string from TYPE and SUBTYPE."
181 (format "%s/%s" type subtype)
182 (format "%s" type))))
185 ;;; @ Content-Disposition
188 (defsubst mime-content-disposition-type (content-disposition)
189 "Return disposition-type of CONTENT-DISPOSITION."
190 (cdr (car content-disposition)))
192 (defsubst mime-content-disposition-parameters (content-disposition)
193 "Return disposition-parameters of CONTENT-DISPOSITION."
194 (cdr content-disposition))
196 (defsubst mime-content-disposition-parameter (content-disposition parameter)
197 "Return PARAMETER value of CONTENT-DISPOSITION."
198 (cdr (assoc parameter (cdr content-disposition))))
200 (defsubst mime-content-disposition-filename (content-disposition)
201 "Return filename of CONTENT-DISPOSITION."
202 (mime-content-disposition-parameter content-disposition "filename"))
210 (autoload 'mime-entity-content-type "mime")
211 (autoload 'mime-parse-multipart "mime-parse")
212 (autoload 'mime-parse-encapsulated "mime-parse")
213 (autoload 'mime-entity-content "mime")
215 (luna-define-class mime-entity ()
217 content-type children parent
219 content-disposition encoding
221 original-header parsed-header))
223 (defalias 'mime-entity-representation-type-internal 'luna-class-name)
224 (defalias 'mime-entity-set-representation-type-internal 'luna-set-class-name)
226 (luna-define-internal-accessors 'mime-entity)
228 (luna-define-method mime-entity-fetch-field ((entity mime-entity)
230 (or (symbolp field-name)
231 (setq field-name (intern (capitalize (capitalize field-name)))))
232 (cdr (assq field-name
233 (mime-entity-original-header-internal entity))))
235 (luna-define-method mime-entity-children ((entity mime-entity))
236 (let* ((content-type (mime-entity-content-type entity))
237 (primary-type (mime-content-type-primary-type content-type)))
238 (cond ((eq primary-type 'multipart)
239 (mime-parse-multipart entity)
241 ((and (eq primary-type 'message)
242 (memq (mime-content-type-subtype content-type)
243 '(rfc822 news external-body)
245 (mime-parse-encapsulated entity)
249 (luna-define-method mime-insert-text-content ((entity mime-entity))
251 (decode-mime-charset-string (mime-entity-content entity)
252 (or (mime-content-type-parameter
253 (mime-entity-content-type entity)
255 default-mime-charset)
263 (defmacro mm-expand-class-name (type)
264 (` (intern (format "mime-%s-entity" (, type)))))
266 (defmacro mm-define-backend (type &optional parents)
267 (` (luna-define-class (, (mm-expand-class-name type))
268 (, (nconc (mapcar (function
270 (mm-expand-class-name parent)
275 (defmacro mm-define-method (name args &rest body)
276 (or (eq name 'initialize-instance)
277 (setq name (intern (format "mime-%s" name))))
278 (let ((spec (car args)))
280 (cons (list (car spec)
281 (mm-expand-class-name (nth 1 spec)))
283 (` (luna-define-method (, name) (, args) (,@ body)))
286 (put 'mm-define-method 'lisp-indent-function 'defun)
288 (def-edebug-spec mm-define-method
289 (&define name ((arg symbolp)
291 [&optional ["&optional" arg &rest arg]]
292 &optional ["&rest" arg]
297 ;;; @ message structure
300 (defvar mime-message-structure nil
301 "Information about structure of message.
302 Please use reference function `mime-entity-SLOT' to get value of SLOT.
304 Following is a list of slots of the structure:
306 node-id node-id (list of integers)
307 content-type content-type (content-type)
308 content-disposition content-disposition (content-disposition)
309 encoding Content-Transfer-Encoding (string or nil)
310 children entities included in this entity (list of entity)
312 If an entity includes other entities in its body, such as multipart or
313 message/rfc822, `mime-entity' structures of them are included in
314 `children', so the `mime-entity' structure become a tree.")
316 (make-variable-buffer-local 'mime-message-structure)
318 (make-obsolete-variable 'mime-message-structure "should not use it.")
321 ;;; @ for mel-backend
324 (defvar mel-service-list nil)
326 (defmacro mel-define-service (name &optional args &rest rest)
327 "Define NAME as a service for Content-Transfer-Encodings.
328 If ARGS is specified, NAME is defined as a generic function for the
331 (add-to-list 'mel-service-list '(, name))
332 (defvar (, (intern (format "%s-obarray" name))) (make-vector 7 0))
334 (` ((defun (, name) (, args)
336 (funcall (mel-find-function '(, name)
337 (, (car (last args))))
338 (,@ (luna-arglist-to-arguments (butlast args))))
342 (put 'mel-define-service 'lisp-indent-function 'defun)
345 (defvar mel-encoding-module-alist nil)
347 (defsubst mel-find-function-from-obarray (ob-array encoding)
348 (let* ((f (intern-soft encoding ob-array)))
350 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
354 (null (setq f (intern-soft encoding ob-array)))
356 (setq rest (cdr rest))
360 (defsubst mel-copy-method (service src-backend dst-backend)
361 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
362 (f (mel-find-function-from-obarray oa src-backend))
365 (setq sym (intern dst-backend oa))
367 (fset sym (symbol-function f))
370 (defsubst mel-copy-backend (src-backend dst-backend)
371 (let ((services mel-service-list))
373 (mel-copy-method (car services) src-backend dst-backend)
374 (setq services (cdr services)))))
376 (defmacro mel-define-backend (type &optional parents)
377 "Define TYPE as a mel-backend.
378 If PARENTS is specified, TYPE inherits PARENTS.
379 Each parent must be backend name (string)."
383 (` (mel-copy-backend (, parent) (, type)))
387 (defmacro mel-define-method (name args &rest body)
388 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
389 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
390 specialized parameter. (car (car (last ARGS))) is name of variable
391 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
392 (let* ((specializer (car (last args)))
393 (class (nth 1 specializer)))
395 (mel-define-service (, name))
396 (fset (intern (, class) (, (intern (format "%s-obarray" name))))
398 (lambda (, (butlast args))
401 (put 'mel-define-method 'lisp-indent-function 'defun)
403 (defmacro mel-define-method-function (spec function)
404 "Set SPEC's function definition to FUNCTION.
405 First element of SPEC is service.
406 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
407 must be specialized parameter. (car (car (last ARGS))) is name of
408 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
409 (let* ((name (car spec))
411 (specializer (car (last args)))
412 (class (nth 1 specializer)))
414 (mel-define-service (, name))
415 (setq sym (intern (, class) (, (intern (format "%s-obarray" name)))))
417 (fset sym (symbol-function (, function))))))))
419 (defmacro mel-define-function (function spec)
420 (let* ((name (car spec))
422 (specializer (car (last args)))
423 (class (nth 1 specializer)))
425 (define-function (, function)
426 (intern (, class) (, (intern (format "%s-obarray" name)))))
429 (defvar base64-dl-module
430 (if (and (fboundp 'base64-encode-string)
431 (subrp (symbol-function 'base64-encode-string)))
433 (if (fboundp 'dynamic-link)
434 (let ((path (expand-file-name "base64.so" exec-directory)))
435 (and (file-exists-p path)
445 ;;; mime-def.el ends here