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.
35 (eval-when-compile (require 'cl)) ; list*
38 (defconst mime-library-product ["FLIM" (1 13 1) "Tawaramoto"]
39 "Product name, version number and code name of MIME-library package.")
42 (defmacro mime-product-name (product)
45 (defmacro mime-product-version (product)
48 (defmacro mime-product-code-name (product)
51 (defconst mime-library-version
53 (concat (mime-product-name mime-library-product) " "
54 (mapconcat #'number-to-string
55 (mime-product-version mime-library-product) ".")
56 " - \"" (mime-product-code-name mime-library-product) "\"")))
64 (defgroup mime '((default-mime-charset custom-variable))
65 "Emacs MIME Interfaces"
69 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
70 "*List of encoding names for uuencode format."
72 :type '(repeat string))
75 ;;; @ required functions
78 (defsubst regexp-* (regexp)
81 (defsubst regexp-or (&rest args)
82 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
89 (defconst std11-quoted-pair-regexp "\\\\.")
90 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
91 (defconst std11-qtext-regexp
93 (concat "[^" std11-non-qtext-char-list "]"))))
94 (defconst std11-quoted-string-regexp
98 (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
106 (defconst mime-tspecial-char-list
107 '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=)))
108 (defconst mime-token-regexp
110 (concat "[^" mime-tspecial-char-list "\000-\040]+")))
111 (defconst mime-charset-regexp mime-token-regexp)
113 (defconst mime-media-type/subtype-regexp
114 (concat mime-token-regexp "/" mime-token-regexp))
120 (defconst base64-token-regexp "[A-Za-z0-9+/]")
121 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
123 (defconst B-encoded-text-regexp
132 base64-token-padding-regexp
133 base64-token-padding-regexp
136 ;; (defconst eword-B-encoding-and-encoded-text-regexp
137 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
140 ;;; @@ Quoted-Printable / Q
143 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
145 (defconst quoted-printable-octet-regexp
146 (concat "=[" quoted-printable-hex-chars
147 "][" quoted-printable-hex-chars "]"))
149 (defconst Q-encoded-text-regexp
150 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
152 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
153 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
159 (defsubst make-mime-content-type (type subtype &optional parameters)
160 (list* (cons 'type type)
161 (cons 'subtype subtype)
162 (nreverse parameters))
165 (defsubst mime-content-type-primary-type (content-type)
166 "Return primary-type of CONTENT-TYPE."
167 (cdr (car content-type)))
169 (defsubst mime-content-type-subtype (content-type)
170 "Return primary-type of CONTENT-TYPE."
171 (cdr (cadr content-type)))
173 (defsubst mime-content-type-parameters (content-type)
174 "Return primary-type of CONTENT-TYPE."
177 (defsubst mime-content-type-parameter (content-type parameter)
178 "Return PARAMETER value of CONTENT-TYPE."
179 (cdr (assoc parameter (mime-content-type-parameters content-type))))
182 (defsubst mime-type/subtype-string (type &optional subtype)
183 "Return type/subtype string from TYPE and SUBTYPE."
186 (format "%s/%s" type subtype)
187 (format "%s" type))))
190 ;;; @ Content-Disposition
193 (defsubst mime-content-disposition-type (content-disposition)
194 "Return disposition-type of CONTENT-DISPOSITION."
195 (cdr (car content-disposition)))
197 (defsubst mime-content-disposition-parameters (content-disposition)
198 "Return disposition-parameters of CONTENT-DISPOSITION."
199 (cdr content-disposition))
201 (defsubst mime-content-disposition-parameter (content-disposition parameter)
202 "Return PARAMETER value of CONTENT-DISPOSITION."
203 (cdr (assoc parameter (cdr content-disposition))))
205 (defsubst mime-content-disposition-filename (content-disposition)
206 "Return filename of CONTENT-DISPOSITION."
207 (mime-content-disposition-parameter content-disposition "filename"))
215 (autoload 'mime-entity-content-type "mime")
216 (autoload 'mime-parse-multipart "mime-parse")
217 (autoload 'mime-parse-encapsulated "mime-parse")
219 (luna-define-class mime-entity ()
221 content-type children parent
223 content-disposition encoding
225 original-header parsed-header))
227 (defalias 'mime-entity-representation-type-internal 'luna-class-name)
228 (defalias 'mime-entity-set-representation-type-internal 'luna-set-class-name)
230 (luna-define-internal-accessors 'mime-entity)
232 (luna-define-method mime-entity-fetch-field ((entity mime-entity)
234 (or (symbolp field-name)
235 (setq field-name (intern (capitalize (capitalize field-name)))))
236 (cdr (assq field-name
237 (mime-entity-original-header-internal entity))))
239 (luna-define-method mime-entity-children ((entity mime-entity))
240 (let* ((content-type (mime-entity-content-type entity))
241 (primary-type (mime-content-type-primary-type content-type)))
242 (cond ((eq primary-type 'multipart)
243 (mime-parse-multipart entity)
245 ((and (eq primary-type 'message)
246 (memq (mime-content-type-subtype content-type)
247 '(rfc822 news external-body)
249 (mime-parse-encapsulated entity)
257 (defmacro mm-expand-class-name (type)
258 `(intern (format "mime-%s-entity" ,type)))
260 (defmacro mm-define-backend (type &optional parents)
261 `(luna-define-class ,(mm-expand-class-name type)
262 ,(nconc (mapcar (lambda (parent)
263 (mm-expand-class-name parent)
268 (defmacro mm-define-method (name args &rest body)
269 (or (eq name 'initialize-instance)
270 (setq name (intern (format "mime-%s" name))))
271 (let ((spec (car args)))
273 (cons (list (car spec)
274 (mm-expand-class-name (nth 1 spec)))
276 `(luna-define-method ,name ,args ,@body)
279 (put 'mm-define-method 'lisp-indent-function 'defun)
281 (def-edebug-spec mm-define-method
282 (&define name ((arg symbolp)
284 [&optional ["&optional" arg &rest arg]]
285 &optional ["&rest" arg]
290 ;;; @ message structure
293 (defvar mime-message-structure nil
294 "Information about structure of message.
295 Please use reference function `mime-entity-SLOT' to get value of SLOT.
297 Following is a list of slots of the structure:
299 node-id node-id (list of integers)
300 content-type content-type (content-type)
301 content-disposition content-disposition (content-disposition)
302 encoding Content-Transfer-Encoding (string or nil)
303 children entities included in this entity (list of entity)
305 If an entity includes other entities in its body, such as multipart or
306 message/rfc822, `mime-entity' structures of them are included in
307 `children', so the `mime-entity' structure become a tree.")
309 (make-variable-buffer-local 'mime-message-structure)
311 (make-obsolete-variable 'mime-message-structure "should not use it.")
314 ;;; @ for mel-backend
317 (defvar mel-service-list nil)
319 (defmacro mel-define-service (name &optional args &rest rest)
320 "Define NAME as a service for Content-Transfer-Encodings.
321 If ARGS is specified, NAME is defined as a generic function for the
324 (add-to-list 'mel-service-list ',name)
325 (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
329 (funcall (mel-find-function ',name ,(car (last args)))
330 ,@(luna-arglist-to-arguments (butlast args)))
334 (put 'mel-define-service 'lisp-indent-function 'defun)
337 (defvar mel-encoding-module-alist nil)
339 (defsubst mel-find-function-from-obarray (ob-array encoding)
340 (let* ((f (intern-soft encoding ob-array)))
342 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
346 (null (setq f (intern-soft encoding ob-array)))
348 (setq rest (cdr rest))
352 (defsubst mel-copy-method (service src-backend dst-backend)
353 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
354 (f (mel-find-function-from-obarray oa src-backend))
357 (setq sym (intern dst-backend oa))
359 (fset sym (symbol-function f))
362 (defsubst mel-copy-backend (src-backend dst-backend)
363 (let ((services mel-service-list))
365 (mel-copy-method (car services) src-backend dst-backend)
366 (setq services (cdr services)))))
368 (defmacro mel-define-backend (type &optional parents)
369 "Define TYPE as a mel-backend.
370 If PARENTS is specified, TYPE inherits PARENTS.
371 Each parent must be backend name (string)."
373 (mapcar (lambda (parent)
374 `(mel-copy-backend ,parent ,type)
378 (defmacro mel-define-method (name args &rest body)
379 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
380 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
381 specialized parameter. (car (car (last ARGS))) is name of variable
382 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
383 (let* ((specializer (car (last args)))
384 (class (nth 1 specializer)))
386 (mel-define-service ,name)
387 (fset (intern ,class ,(intern (format "%s-obarray" name)))
388 (lambda ,(butlast args)
391 (put 'mel-define-method 'lisp-indent-function 'defun)
393 (defmacro mel-define-method-function (spec function)
394 "Set SPEC's function definition to FUNCTION.
395 First element of SPEC is service.
396 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
397 must be specialized parameter. (car (car (last ARGS))) is name of
398 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
399 (let* ((name (car spec))
401 (specializer (car (last args)))
402 (class (nth 1 specializer)))
404 (mel-define-service ,name)
405 (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
407 (fset sym (symbol-function ,function))))))
409 (defmacro mel-define-function (function spec)
410 (let* ((name (car spec))
412 (specializer (car (last args)))
413 (class (nth 1 specializer)))
415 (define-function ,function
416 (intern ,class ,(intern (format "%s-obarray" name))))
419 (defvar base64-dl-module
420 (if (and (fboundp 'base64-encode-string)
421 (subrp (symbol-function 'base64-encode-string)))
423 (if (fboundp 'dynamic-link)
424 (let ((path (expand-file-name "base64.so" exec-directory)))
425 (and (file-exists-p path)
435 ;;; mime-def.el ends here