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 2) "Kasanui"]
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 '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=)))
109 (defconst mime-token-exclude-chars-regexp
111 (concat mime-tspecial-char-list "\000-\040")))
113 (defconst mime-token-regexp
115 (concat "[^" mime-token-exclude-chars-regexp "]+")))
117 (defconst mime-charset-regexp
119 (concat "[^" mime-token-exclude-chars-regexp "*]+")))
121 (defconst mime-media-type/subtype-regexp
122 (concat mime-token-regexp "/" mime-token-regexp))
128 (defconst base64-token-regexp "[A-Za-z0-9+/]")
129 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
131 (defconst B-encoded-text-regexp
140 base64-token-padding-regexp
141 base64-token-padding-regexp
144 ;; (defconst eword-B-encoding-and-encoded-text-regexp
145 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
148 ;;; @@ Quoted-Printable / Q
151 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
153 (defconst quoted-printable-octet-regexp
154 (concat "=[" quoted-printable-hex-chars
155 "][" quoted-printable-hex-chars "]"))
157 (defconst Q-encoded-text-regexp
158 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
160 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
161 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
166 (defsubst make-mime-parameter (name &optional language charset
167 raw-values encoded-value)
169 (vector language charset raw-values encoded-value))
172 (defsubst mime-parameter-language (parm)
176 (defsubst mime-parameter-set-language (parm language)
177 (aset (cdr parm) 0 language)
180 (defsubst mime-parameter-set-charset (parm mcs)
181 (aset (cdr parm) 1 mcs)
184 (defsubst mime-parameter-charset (parm)
188 (defsubst mime-parameter-set-raw-values (parm raw-values)
189 (aset (cdr parm) 2 raw-values)
192 (defsubst mime-parameter-raw-values (parm)
196 (defsubst mime-parameter-append-raw-value (parm no encoded raw-value)
197 (aset (cdr parm) 2 (cons (cons no (cons encoded raw-value))
198 (mime-parameter-raw-values parm)))
201 (defun mime-parameter-value (parm)
203 (or (aref (cdr parm) 3)
204 (let* ((mcs (mime-parameter-charset parm))
206 (mime-parameter-set-raw-values
208 (sort (mime-parameter-raw-values parm)
209 (function (lambda (a b)
210 (< (car a) (car b)))))))
216 (setq raw (cdar sorted-raw)
221 (while (re-search-forward "%\\([0-9a-z][0-9a-z]\\)"
225 (string-to-int (buffer-substring
230 (goto-char (point-max))
232 (setq sorted-raw (cdr sorted-raw)))
233 (decode-mime-charset-region (point-min) (point-max)
236 (mapconcat #'cddr sorted-raw "")))
237 (language (mime-parameter-language parm)))
239 (put-text-property 0 (length val)
240 'mime-language language val))
241 (aset (cdr parm) 3 val)
244 (defsubst mime-parameters (parms)
245 (mapcar (function (lambda (parm)
247 (mime-parameter-value parm))))
250 (defsubst mime-parameter (parms name)
251 (let ((parm (assoc name parms)))
252 (cons (car parm) (mime-parameter-value parm))))
257 (defsubst make-mime-content-type (type subtype &optional parameters)
258 (list* (cons 'type type)
259 (cons 'subtype subtype)
260 (nreverse parameters))
263 (defsubst mime-content-type-primary-type (content-type)
264 "Return primary-type of CONTENT-TYPE."
265 (cdr (car content-type)))
267 (defsubst mime-content-type-subtype (content-type)
268 "Return primary-type of CONTENT-TYPE."
269 (cdr (cadr content-type)))
271 (defsubst mime-content-type-parameters (content-type)
272 "Return primary-type of CONTENT-TYPE."
273 (mime-parameters (cddr content-type)))
275 (defsubst mime-content-type-parameter (content-type parameter)
276 "Return PARAMETER value of CONTENT-TYPE."
277 (mime-parameter-value (assoc parameter (cddr content-type))))
280 (defsubst mime-type/subtype-string (type &optional subtype)
281 "Return type/subtype string from TYPE and SUBTYPE."
284 (format "%s/%s" type subtype)
285 (format "%s" type))))
288 ;;; @ Content-Disposition
291 (defsubst mime-content-disposition-type (content-disposition)
292 "Return disposition-type of CONTENT-DISPOSITION."
293 (cdr (car content-disposition)))
295 (defsubst mime-content-disposition-parameters (content-disposition)
296 "Return disposition-parameters of CONTENT-DISPOSITION."
297 (mime-parameters (cdr content-disposition)))
299 (defsubst mime-content-disposition-parameter (content-disposition parameter)
300 "Return PARAMETER value of CONTENT-DISPOSITION."
301 (mime-parameter-value (assoc parameter (cdr content-disposition))))
303 (defsubst mime-content-disposition-filename (content-disposition)
304 "Return filename of CONTENT-DISPOSITION."
305 (mime-content-disposition-parameter content-disposition "filename"))
313 (autoload 'mime-entity-content-type "mime")
314 (autoload 'mime-parse-multipart "mime-parse")
315 (autoload 'mime-parse-encapsulated "mime-parse")
316 (autoload 'mime-entity-content "mime")
318 (luna-define-class mime-entity ()
320 content-type children parent
322 content-disposition encoding
324 original-header parsed-header))
326 (defalias 'mime-entity-representation-type-internal 'luna-class-name)
327 (defalias 'mime-entity-set-representation-type-internal 'luna-set-class-name)
329 (luna-define-internal-accessors 'mime-entity)
331 (luna-define-method mime-entity-fetch-field ((entity mime-entity)
333 (or (symbolp field-name)
334 (setq field-name (intern (capitalize (capitalize field-name)))))
335 (cdr (assq field-name
336 (mime-entity-original-header-internal entity))))
338 (luna-define-method mime-entity-children ((entity mime-entity))
339 (let* ((content-type (mime-entity-content-type entity))
340 (primary-type (mime-content-type-primary-type content-type)))
341 (cond ((eq primary-type 'multipart)
342 (mime-parse-multipart entity)
344 ((and (eq primary-type 'message)
345 (memq (mime-content-type-subtype content-type)
346 '(rfc822 news external-body)
348 (mime-parse-encapsulated entity)
352 (luna-define-method mime-insert-text-content ((entity mime-entity))
354 (decode-mime-charset-string (mime-entity-content entity)
355 (or (mime-content-type-parameter
356 (mime-entity-content-type entity)
358 default-mime-charset)
366 (defmacro mm-expand-class-name (type)
367 `(intern (format "mime-%s-entity" ,type)))
369 (defmacro mm-define-backend (type &optional parents)
370 `(luna-define-class ,(mm-expand-class-name type)
371 ,(nconc (mapcar (lambda (parent)
372 (mm-expand-class-name parent)
377 (defmacro mm-define-method (name args &rest body)
378 (or (eq name 'initialize-instance)
379 (setq name (intern (format "mime-%s" name))))
380 (let ((spec (car args)))
382 (cons (list (car spec)
383 (mm-expand-class-name (nth 1 spec)))
385 `(luna-define-method ,name ,args ,@body)
388 (put 'mm-define-method 'lisp-indent-function 'defun)
390 (def-edebug-spec mm-define-method
391 (&define name ((arg symbolp)
393 [&optional ["&optional" arg &rest arg]]
394 &optional ["&rest" arg]
399 ;;; @ message structure
402 (defvar mime-message-structure nil
403 "Information about structure of message.
404 Please use reference function `mime-entity-SLOT' to get value of SLOT.
406 Following is a list of slots of the structure:
408 node-id node-id (list of integers)
409 content-type content-type (content-type)
410 content-disposition content-disposition (content-disposition)
411 encoding Content-Transfer-Encoding (string or nil)
412 children entities included in this entity (list of entity)
414 If an entity includes other entities in its body, such as multipart or
415 message/rfc822, `mime-entity' structures of them are included in
416 `children', so the `mime-entity' structure become a tree.")
418 (make-variable-buffer-local 'mime-message-structure)
420 (make-obsolete-variable 'mime-message-structure "should not use it.")
423 ;;; @ for mel-backend
426 (defvar mel-service-list nil)
428 (defmacro mel-define-service (name &optional args &rest rest)
429 "Define NAME as a service for Content-Transfer-Encodings.
430 If ARGS is specified, NAME is defined as a generic function for the
433 (add-to-list 'mel-service-list ',name)
434 (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
438 (funcall (mel-find-function ',name ,(car (last args)))
439 ,@(luna-arglist-to-arguments (butlast args)))
443 (put 'mel-define-service 'lisp-indent-function 'defun)
446 (defvar mel-encoding-module-alist nil)
448 (defsubst mel-find-function-from-obarray (ob-array encoding)
449 (let* ((f (intern-soft encoding ob-array)))
451 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
455 (null (setq f (intern-soft encoding ob-array)))
457 (setq rest (cdr rest))
461 (defsubst mel-copy-method (service src-backend dst-backend)
462 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
463 (f (mel-find-function-from-obarray oa src-backend))
466 (setq sym (intern dst-backend oa))
468 (fset sym (symbol-function f))
471 (defsubst mel-copy-backend (src-backend dst-backend)
472 (let ((services mel-service-list))
474 (mel-copy-method (car services) src-backend dst-backend)
475 (setq services (cdr services)))))
477 (defmacro mel-define-backend (type &optional parents)
478 "Define TYPE as a mel-backend.
479 If PARENTS is specified, TYPE inherits PARENTS.
480 Each parent must be backend name (string)."
482 (mapcar (lambda (parent)
483 `(mel-copy-backend ,parent ,type)
487 (defmacro mel-define-method (name args &rest body)
488 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
489 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
490 specialized parameter. (car (car (last ARGS))) is name of variable
491 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
492 (let* ((specializer (car (last args)))
493 (class (nth 1 specializer)))
495 (mel-define-service ,name)
496 (fset (intern ,class ,(intern (format "%s-obarray" name)))
497 (lambda ,(butlast args)
500 (put 'mel-define-method 'lisp-indent-function 'defun)
502 (defmacro mel-define-method-function (spec function)
503 "Set SPEC's function definition to FUNCTION.
504 First element of SPEC is service.
505 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
506 must be specialized parameter. (car (car (last ARGS))) is name of
507 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
508 (let* ((name (car spec))
510 (specializer (car (last args)))
511 (class (nth 1 specializer)))
513 (mel-define-service ,name)
514 (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
516 (fset sym (symbol-function ,function))))))
518 (defmacro mel-define-function (function spec)
519 (let* ((name (car spec))
521 (specializer (car (last args)))
522 (class (nth 1 specializer)))
524 (define-function ,function
525 (intern ,class ,(intern (format "%s-obarray" name))))
528 (defvar base64-dl-module
529 (if (and (fboundp 'base64-encode-string)
530 (subrp (symbol-function 'base64-encode-string)))
532 (if (fboundp 'dynamic-link)
533 (let ((path (expand-file-name "base64.so" exec-directory)))
534 (and (file-exists-p path)
544 ;;; mime-def.el ends here