1 ;;; mime-def.el --- definition module about MIME -*- coding: iso-8859-4; -*-
3 ;; Copyright (C) 1995,96,97,98,99,2000,2001,2002,2003
4 ;; Free Software Foundation, Inc.
6 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
7 ;; Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
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.
33 (eval-when-compile (require 'luna)) ; luna-arglist-to-arguments
36 (defconst mime-library-product ["FLIM" (1 14 6) "Marutamachi"]
37 "Product name, version number and code name of MIME-library package."))
39 (defmacro mime-product-name (product)
42 (defmacro mime-product-version (product)
45 (defmacro mime-product-code-name (product)
48 (defconst mime-library-version
50 (concat (mime-product-name mime-library-product) " "
51 (mapconcat #'number-to-string
52 (mime-product-version mime-library-product) ".")
53 " - \"" (mime-product-code-name mime-library-product) "\"")))
59 (defgroup mime '((default-mime-charset custom-variable))
60 "Emacs MIME Interfaces"
64 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
65 "*List of encoding names for uuencode format."
67 :type '(repeat string))
70 ;;; @@ for encoded-word
73 (defgroup mime-header nil
74 "Header representation, specially encoded-word"
80 (defcustom mime-field-decoding-max-size 1000
81 "*Max size to decode header field."
83 :type '(choice (integer :tag "Limit (bytes)")
84 (const :tag "Don't limit" nil)))
86 (defcustom mime-header-accept-quoted-encoded-words nil
87 "*Accept encoded-words in quoted-strings."
95 (defcustom mime-field-encoding-method-alist
96 '(("X-Nsubject" . iso-2022-jp-2)
101 "*Alist to specify field encoding method.
102 Its key is field-name, value is encoding method.
104 If method is `mime', this field will be encoded into MIME format.
106 If method is a MIME-charset, this field will be encoded as the charset
107 when it must be convert into network-code.
109 If method is `default-mime-charset', this field will be encoded as
110 variable `default-mime-charset' when it must be convert into
113 If method is nil, this field will not be encoded."
115 :type '(repeat (cons (choice :tag "Field"
117 (const :tag "Default" t))
118 (choice :tag "Method"
119 (const :tag "MIME conversion" mime)
120 (symbol :tag "non-MIME conversion")
121 (const :tag "no-conversion" nil)))))
124 ;;; @ required functions
127 (defsubst regexp-* (regexp)
130 (defsubst regexp-or (&rest args)
131 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
133 (or (fboundp 'char-int)
134 (defalias 'char-int 'identity))
140 (defconst mime-tspecial-char-list
141 '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=))
142 (defconst mime-token-regexp
143 (concat "[^" mime-tspecial-char-list "\000-\040]+"))
144 (defconst mime-attribute-char-regexp
145 (concat "[^" mime-tspecial-char-list "\000-\040"
146 "*'%" ; introduced in RFC 2231.
149 (defconst mime-charset-regexp
150 (concat "[^" mime-tspecial-char-list "\000-\040"
151 "*'%" ; should not include "%"?
154 ;; More precisely, length of "[A-Za-z]+" is limited to at most 8.
155 ;; (defconst mime-language-regexp "[A-Za-z]+\\(-[A-Za-z]+\\)*")
156 (defconst mime-language-regexp "[-A-Za-z]+")
158 (defconst mime-encoding-regexp mime-token-regexp)
164 (defconst base64-token-regexp "[A-Za-z0-9+/]")
165 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
167 (defconst B-encoded-text-regexp
176 base64-token-padding-regexp
177 base64-token-padding-regexp
180 ;; (defconst eword-B-encoding-and-encoded-text-regexp
181 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
184 ;;; @@ Quoted-Printable / Q
187 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
189 (defconst quoted-printable-octet-regexp
190 (concat "=[" quoted-printable-hex-chars
191 "][" quoted-printable-hex-chars "]"))
193 (defconst Q-encoded-text-regexp
194 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
196 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
197 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
203 (defsubst make-mime-content-type (type subtype &optional parameters)
204 (cons (cons 'type type)
205 (cons (cons 'subtype subtype)
208 (defsubst mime-content-type-primary-type (content-type)
209 "Return primary-type of CONTENT-TYPE."
210 (cdr (car content-type)))
212 (defsubst mime-content-type-subtype (content-type)
213 "Return subtype of CONTENT-TYPE."
214 (cdr (car (cdr content-type))))
216 (defsubst mime-content-type-parameters (content-type)
217 "Return parameters of CONTENT-TYPE."
218 (cdr (cdr content-type)))
220 (defsubst mime-content-type-parameter (content-type parameter)
221 "Return PARAMETER value of CONTENT-TYPE."
222 (cdr (assoc parameter (cdr (cdr content-type)))))
225 (defsubst mime-type/subtype-string (type &optional subtype)
226 "Return type/subtype string from TYPE and SUBTYPE."
229 (format "%s/%s" type subtype)
230 (format "%s" type))))
233 ;;; @ Content-Disposition
236 (defsubst make-mime-content-disposition (type &optional parameters)
237 (cons (cons 'type type)
240 (defsubst mime-content-disposition-type (content-disposition)
241 "Return disposition-type of CONTENT-DISPOSITION."
242 (cdr (car content-disposition)))
244 (defsubst mime-content-disposition-parameters (content-disposition)
245 "Return disposition-parameters of CONTENT-DISPOSITION."
246 (cdr content-disposition))
248 (defsubst mime-content-disposition-parameter (content-disposition parameter)
249 "Return PARAMETER value of CONTENT-DISPOSITION."
250 (cdr (assoc parameter (cdr content-disposition))))
252 (defsubst mime-content-disposition-filename (content-disposition)
253 "Return filename of CONTENT-DISPOSITION."
254 (mime-content-disposition-parameter content-disposition "filename"))
257 ;;; @ message structure
260 (defvar mime-message-structure nil
261 "Information about structure of message.
262 Please use reference function `mime-entity-SLOT' to get value of SLOT.
264 Following is a list of slots of the structure:
266 node-id node-id (list of integers)
267 content-type content-type (content-type)
268 content-disposition content-disposition (content-disposition)
269 encoding Content-Transfer-Encoding (string or nil)
270 children entities included in this entity (list of entity)
272 If an entity includes other entities in its body, such as multipart or
273 message/rfc822, `mime-entity' structures of them are included in
274 `children', so the `mime-entity' structure become a tree.")
276 (make-variable-buffer-local 'mime-message-structure)
278 (make-obsolete-variable 'mime-message-structure "should not use it.")
281 ;;; @ for mel-backend
284 (defvar mel-service-list nil)
286 (defmacro mel-define-service (name &optional args &rest rest)
287 "Define NAME as a service for Content-Transfer-Encodings.
288 If ARGS is specified, NAME is defined as a generic function for the
291 (add-to-list 'mel-service-list ',name)
292 (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
296 (funcall (mel-find-function ',name ,(car (last args)))
297 ,@(luna-arglist-to-arguments (butlast args)))
301 (put 'mel-define-service 'lisp-indent-function 'defun)
304 (defvar mel-encoding-module-alist nil)
306 (defsubst mel-find-function-from-obarray (ob-array encoding)
307 (let* ((f (intern-soft encoding ob-array)))
309 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
313 (null (setq f (intern-soft encoding ob-array)))
315 (setq rest (cdr rest))
319 (defsubst mel-copy-method (service src-backend dst-backend)
320 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
321 (f (mel-find-function-from-obarray oa src-backend))
324 (setq sym (intern dst-backend oa))
326 (fset sym (symbol-function f))
329 (defsubst mel-copy-backend (src-backend dst-backend)
330 (let ((services mel-service-list))
332 (mel-copy-method (car services) src-backend dst-backend)
333 (setq services (cdr services)))))
335 (defmacro mel-define-backend (type &optional parents)
336 "Define TYPE as a mel-backend.
337 If PARENTS is specified, TYPE inherits PARENTS.
338 Each parent must be backend name (string)."
340 (mapcar (lambda (parent)
341 `(mel-copy-backend ,parent ,type)
345 (defmacro mel-define-method (name args &rest body)
346 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
347 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
348 specialized parameter. (car (car (last ARGS))) is name of variable
349 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
350 (let* ((specializer (car (last args)))
351 (class (nth 1 specializer)))
353 (mel-define-service ,name)
354 (fset (intern ,class ,(intern (format "%s-obarray" name)))
355 (lambda ,(butlast args)
358 (put 'mel-define-method 'lisp-indent-function 'defun)
360 (defmacro mel-define-method-function (spec function)
361 "Set SPEC's function definition to FUNCTION.
362 First element of SPEC is service.
363 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
364 must be specialized parameter. (car (car (last ARGS))) is name of
365 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
366 (let* ((name (car spec))
368 (specializer (car (last args)))
369 (class (nth 1 specializer)))
371 (mel-define-service ,name)
372 (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
374 (fset sym (symbol-function ,function))))))
376 (defmacro mel-define-function (function spec)
377 (let* ((name (car spec))
379 (specializer (car (last args)))
380 (class (nth 1 specializer)))
382 (define-function ,function
383 (intern ,class ,(intern (format "%s-obarray" name))))
386 (defvar base64-dl-module
387 (if (and (fboundp 'base64-encode-string)
388 (subrp (symbol-function 'base64-encode-string)))
390 (if (fboundp 'dynamic-link)
391 (let ((path (expand-file-name "base64.so" exec-directory)))
392 (and (file-exists-p path)
402 ;;; mime-def.el ends here