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.
32 (defconst mime-library-product ["Chao" (1 13 0) "JR Fujinomori"]
33 "Product name, version number and code name of MIME-library package.")
36 (defmacro mime-product-name (product)
39 (defmacro mime-product-version (product)
42 (defmacro mime-product-code-name (product)
45 (defconst mime-library-version
47 (concat (mime-product-name mime-library-product) " "
48 (mapconcat #'number-to-string
49 (mime-product-version mime-library-product) ".")
50 " - \"" (mime-product-code-name mime-library-product) "\"")))
58 (eval-when-compile (require 'cl))
61 "Emacs MIME Interfaces"
65 (custom-handle-keyword 'default-mime-charset :group 'mime
68 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
69 "*List of encoding names for uuencode format."
71 :type '(repeat string))
74 ;;; @ required functions
77 (defsubst regexp-* (regexp)
80 (defsubst regexp-or (&rest args)
81 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
88 (defconst std11-quoted-pair-regexp "\\\\.")
89 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
90 (defconst std11-qtext-regexp
92 (concat "[^" std11-non-qtext-char-list "]"))))
93 (defconst std11-quoted-string-regexp
97 (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
105 (defconst mime-tspecial-char-list
106 '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=)))
107 (defconst mime-token-regexp
109 (concat "[^" mime-tspecial-char-list "\000-\040]+")))
110 (defconst mime-charset-regexp mime-token-regexp)
112 (defconst mime-media-type/subtype-regexp
113 (concat mime-token-regexp "/" mime-token-regexp))
119 (defconst base64-token-regexp "[A-Za-z0-9+/]")
120 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
122 (defconst B-encoded-text-regexp
131 base64-token-padding-regexp
132 base64-token-padding-regexp
135 ;; (defconst eword-B-encoding-and-encoded-text-regexp
136 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
139 ;;; @@ Quoted-Printable / Q
142 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
144 (defconst quoted-printable-octet-regexp
145 (concat "=[" quoted-printable-hex-chars
146 "][" quoted-printable-hex-chars "]"))
148 (defconst Q-encoded-text-regexp
149 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
151 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
152 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
158 (defsubst make-mime-content-type (type subtype &optional parameters)
159 (list* (cons 'type type)
160 (cons 'subtype subtype)
161 (nreverse parameters))
164 (defsubst mime-content-type-primary-type (content-type)
165 "Return primary-type of CONTENT-TYPE."
166 (cdr (car content-type)))
168 (defsubst mime-content-type-subtype (content-type)
169 "Return primary-type of CONTENT-TYPE."
170 (cdr (cadr content-type)))
172 (defsubst mime-content-type-parameters (content-type)
173 "Return primary-type of CONTENT-TYPE."
176 (defsubst mime-content-type-parameter (content-type parameter)
177 "Return PARAMETER value of CONTENT-TYPE."
178 (cdr (assoc parameter (mime-content-type-parameters content-type))))
181 (defsubst mime-type/subtype-string (type &optional subtype)
182 "Return type/subtype string from TYPE and SUBTYPE."
185 (format "%s/%s" type subtype)
186 (format "%s" type))))
189 ;;; @ Content-Disposition
192 (defsubst mime-content-disposition-type (content-disposition)
193 "Return disposition-type of CONTENT-DISPOSITION."
194 (cdr (car content-disposition)))
196 (defsubst mime-content-disposition-parameters (content-disposition)
197 "Return disposition-parameters of CONTENT-DISPOSITION."
198 (cdr content-disposition))
200 (defsubst mime-content-disposition-parameter (content-disposition parameter)
201 "Return PARAMETER value of CONTENT-DISPOSITION."
202 (cdr (assoc parameter (cdr content-disposition))))
204 (defsubst mime-content-disposition-filename (content-disposition)
205 "Return filename of CONTENT-DISPOSITION."
206 (mime-content-disposition-parameter content-disposition "filename"))
214 (luna-define-class mime-entity ()
216 content-type children parent
218 content-disposition encoding
220 original-header parsed-header))
222 (defalias 'mime-entity-representation-type-internal 'luna-class-name)
223 (defalias 'mime-entity-set-representation-type-internal 'luna-set-class-name)
225 (luna-define-internal-accessors 'mime-entity)
228 ;;; @ message structure
231 (defvar mime-message-structure nil
232 "Information about structure of message.
233 Please use reference function `mime-entity-SLOT' to get value of SLOT.
235 Following is a list of slots of the structure:
237 node-id node-id (list of integers)
238 content-type content-type (content-type)
239 content-disposition content-disposition (content-disposition)
240 encoding Content-Transfer-Encoding (string or nil)
241 children entities included in this entity (list of entity)
243 If an entity includes other entities in its body, such as multipart or
244 message/rfc822, `mime-entity' structures of them are included in
245 `children', so the `mime-entity' structure become a tree.")
247 (make-variable-buffer-local 'mime-message-structure)
249 (make-obsolete-variable 'mime-message-structure "should not use it.")
255 (defmacro mm-expand-class-name (type)
256 `(intern (format "mime-%s-entity" ,type)))
258 (defmacro mm-define-backend (type &optional parents)
259 `(luna-define-class ,(mm-expand-class-name type)
260 ,(nconc (mapcar (lambda (parent)
261 (mm-expand-class-name parent)
266 (defmacro mm-define-method (name args &rest body)
267 (or (eq name 'initialize-instance)
268 (setq name (intern (format "mime-%s" name))))
269 (let ((spec (car args)))
271 (cons (list (car spec)
272 (mm-expand-class-name (nth 1 spec)))
274 `(luna-define-method ,name ,args ,@body)
276 (put 'mm-define-method 'lisp-indent-function 'defun)
279 (defmacro eval-module-depended-macro (module definition)
282 (require (eval module))
284 (error `(eval-after-load ,(symbol-name (eval module)) ',definition))
288 (eval-module-depended-macro
290 (def-edebug-spec mm-define-method
291 (&define name ((arg symbolp)
293 [&optional ["&optional" arg &rest arg]]
294 &optional ["&rest" arg]
300 ;;; @ for mel-backend
303 (defvar mel-service-list nil)
305 (defmacro mel-define-service (name &optional args &rest rest)
306 "Define NAME as a service for Content-Transfer-Encodings.
307 If ARGS is specified, NAME is defined as a generic function for the
310 (add-to-list 'mel-service-list ',name)
311 (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
315 (funcall (mel-find-function ',name ,(car (last args)))
316 ,@(luna-arglist-to-arguments (butlast args)))
320 (put 'mel-define-service 'lisp-indent-function 'defun)
323 (defvar mel-encoding-module-alist nil)
325 (defsubst mel-find-function-from-obarray (ob-array encoding)
326 (let* ((f (intern-soft encoding ob-array)))
328 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
332 (null (setq f (intern-soft encoding ob-array)))
334 (setq rest (cdr rest))
338 (defsubst mel-copy-method (service src-backend dst-backend)
339 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
340 (f (mel-find-function-from-obarray oa src-backend))
343 (setq sym (intern dst-backend oa))
345 (fset sym (symbol-function f))
348 (defsubst mel-copy-backend (src-backend dst-backend)
349 (let ((services mel-service-list))
351 (mel-copy-method (car services) src-backend dst-backend)
352 (setq services (cdr services)))))
354 (defmacro mel-define-backend (type &optional parents)
355 "Define TYPE as a mel-backend.
356 If PARENTS is specified, TYPE inherits PARENTS.
357 Each parent must be backend name (string)."
359 (mapcar (lambda (parent)
360 `(mel-copy-backend ,parent ,type)
364 (defmacro mel-define-method (name args &rest body)
365 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
366 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
367 specialized parameter. (car (car (last ARGS))) is name of variable
368 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
369 (let* ((specializer (car (last args)))
370 (class (nth 1 specializer)))
372 (mel-define-service ,name)
373 (fset (intern ,class ,(intern (format "%s-obarray" name)))
374 (lambda ,(butlast args)
377 (put 'mel-define-method 'lisp-indent-function 'defun)
379 (defmacro mel-define-method-function (spec function)
380 "Set SPEC's function definition to FUNCTION.
381 First element of SPEC is service.
382 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
383 must be specialized parameter. (car (car (last ARGS))) is name of
384 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
385 (let* ((name (car spec))
387 (specializer (car (last args)))
388 (class (nth 1 specializer)))
390 (mel-define-service ,name)
391 (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
393 (fset sym (symbol-function ,function))))))
395 (defmacro mel-define-function (function spec)
396 (let* ((name (car spec))
398 (specializer (car (last args)))
399 (class (nth 1 specializer)))
401 (define-function ,function
402 (intern ,class ,(intern (format "%s-obarray" name))))
405 (defvar base64-dl-module
406 (if (and (fboundp 'base64-encode-string)
407 (subrp (symbol-function 'base64-encode-string)))
409 (if (fboundp 'dynamic-link)
410 (let ((path (expand-file-name "base64.so" exec-directory)))
411 (and (file-exists-p path)
421 ;;; mime-def.el ends here