1 ;;; mime-def.el --- definition module about MIME
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: definition, MIME, multimedia, mail, news
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ;; General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
30 (defconst mime-library-product ["FLIM" (1 12 6) "Family-K
\e.D
\8eòenmae"]
31 "Product name, version number and code name of MIME-library package.")
34 (defmacro mime-product-name (product)
37 (defmacro mime-product-version (product)
40 (defmacro mime-product-code-name (product)
43 (defconst mime-library-version
45 (concat (mime-product-name mime-library-product) " "
46 (mapconcat #'number-to-string
47 (mime-product-version mime-library-product) ".")
48 " - \"" (mime-product-code-name mime-library-product) "\"")))
56 (eval-when-compile (require 'cl))
59 "Emacs MIME Interfaces"
63 (custom-handle-keyword 'default-mime-charset :group 'mime
66 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
67 "*List of encoding names for uuencode format."
69 :type '(repeat string))
72 ;;; @ required functions
75 (defsubst regexp-* (regexp)
78 (defsubst regexp-or (&rest args)
79 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
86 (defconst std11-quoted-pair-regexp "\\\\.")
87 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
88 (defconst std11-qtext-regexp
90 (concat "[^" std11-non-qtext-char-list "]"))))
91 (defconst std11-quoted-string-regexp
95 (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
103 (defconst mime-tspecial-char-list
104 '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=)))
105 (defconst mime-token-regexp
107 (concat "[^" mime-tspecial-char-list "\000-\040]+")))
108 (defconst mime-charset-regexp mime-token-regexp)
110 (defconst mime-media-type/subtype-regexp
111 (concat mime-token-regexp "/" mime-token-regexp))
117 (defconst base64-token-regexp "[A-Za-z0-9+/]")
118 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
120 (defconst B-encoded-text-regexp
129 base64-token-padding-regexp
130 base64-token-padding-regexp
133 ;; (defconst eword-B-encoding-and-encoded-text-regexp
134 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
137 ;;; @@ Quoted-Printable / Q
140 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
142 (defconst quoted-printable-octet-regexp
143 (concat "=[" quoted-printable-hex-chars
144 "][" quoted-printable-hex-chars "]"))
146 (defconst Q-encoded-text-regexp
147 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
149 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
150 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
156 (defsubst make-mime-content-type (type subtype &optional parameters)
157 (list* (cons 'type type)
158 (cons 'subtype subtype)
159 (nreverse parameters))
162 (defsubst mime-content-type-primary-type (content-type)
163 "Return primary-type of CONTENT-TYPE."
164 (cdr (car content-type)))
166 (defsubst mime-content-type-subtype (content-type)
167 "Return primary-type of CONTENT-TYPE."
168 (cdr (cadr content-type)))
170 (defsubst mime-content-type-parameters (content-type)
171 "Return primary-type of CONTENT-TYPE."
174 (defsubst mime-content-type-parameter (content-type parameter)
175 "Return PARAMETER value of CONTENT-TYPE."
176 (cdr (assoc parameter (mime-content-type-parameters content-type))))
179 (defsubst mime-type/subtype-string (type &optional subtype)
180 "Return type/subtype string from TYPE and SUBTYPE."
183 (format "%s/%s" type subtype)
184 (format "%s" type))))
187 ;;; @ Content-Disposition
190 (defsubst mime-content-disposition-type (content-disposition)
191 "Return disposition-type of CONTENT-DISPOSITION."
192 (cdr (car content-disposition)))
194 (defsubst mime-content-disposition-parameters (content-disposition)
195 "Return disposition-parameters of CONTENT-DISPOSITION."
196 (cdr content-disposition))
198 (defsubst mime-content-disposition-parameter (content-disposition parameter)
199 "Return PARAMETER value of CONTENT-DISPOSITION."
200 (cdr (assoc parameter (cdr content-disposition))))
202 (defsubst mime-content-disposition-filename (content-disposition)
203 "Return filename of CONTENT-DISPOSITION."
204 (mime-content-disposition-parameter content-disposition "filename"))
210 (defmacro make-mime-entity-internal (representation-type location
211 &optional content-type
212 children parent node-id
214 decoded-subject decoded-from
215 date message-id references
219 original-header parsed-header
220 ;; for buffer representation
222 header-start header-end
224 `(vector ,representation-type ,location
225 ,content-type nil nil ,children ,parent ,node-id
227 ,decoded-subject ,decoded-from
228 ,date ,message-id ,references
232 ,original-header ,parsed-header
233 ;; for buffer representation
234 ,buffer ,header-start ,header-end ,body-start ,body-end))
236 (defmacro mime-entity-representation-type-internal (entity)
238 (defmacro mime-entity-set-representation-type-internal (entity type)
239 `(aset ,entity 0 ,type))
240 (defmacro mime-entity-location-internal (entity)
242 (defmacro mime-entity-set-location-internal (entity location)
243 `(aset ,entity 1 ,location))
245 (defmacro mime-entity-content-type-internal (entity)
247 (defmacro mime-entity-set-content-type-internal (entity type)
248 `(aset ,entity 2 ,type))
249 (defmacro mime-entity-content-disposition-internal (entity)
251 (defmacro mime-entity-set-content-disposition-internal (entity disposition)
252 `(aset ,entity 3 ,disposition))
253 (defmacro mime-entity-encoding-internal (entity)
255 (defmacro mime-entity-set-encoding-internal (entity encoding)
256 `(aset ,entity 4 ,encoding))
258 (defmacro mime-entity-children-internal (entity)
260 (defmacro mime-entity-set-children-internal (entity children)
261 `(aset ,entity 5 ,children))
262 (defmacro mime-entity-parent-internal (entity)
264 (defmacro mime-entity-node-id-internal (entity)
267 (defmacro mime-entity-decoded-subject-internal (entity)
269 (defmacro mime-entity-set-decoded-subject-internal (entity subject)
270 `(aset ,entity 8 ,subject))
271 (defmacro mime-entity-decoded-from-internal (entity)
273 (defmacro mime-entity-set-decoded-from-internal (entity from)
274 `(aset ,entity 9 ,from))
275 (defmacro mime-entity-date-internal (entity)
277 (defmacro mime-entity-set-date-internal (entity date)
278 `(aset ,entity 10 ,date))
279 (defmacro mime-entity-message-id-internal (entity)
281 (defmacro mime-entity-set-message-id-internal (entity message-id)
282 `(aset ,entity 11 ,message-id))
283 (defmacro mime-entity-references-internal (entity)
285 (defmacro mime-entity-set-references-internal (entity references)
286 `(aset ,entity 12 ,references))
287 (defmacro mime-entity-chars-internal (entity)
289 (defmacro mime-entity-set-chars-internal (entity chars)
290 `(aset ,entity 13 ,chars))
291 (defmacro mime-entity-lines-internal (entity)
293 (defmacro mime-entity-set-lines-internal (entity lines)
294 `(aset ,entity 14 ,lines))
295 (defmacro mime-entity-xref-internal (entity)
297 (defmacro mime-entity-set-xref-internal (entity xref)
298 `(aset ,entity 15 ,xref))
300 (defmacro mime-entity-original-header-internal (entity)
302 (defmacro mime-entity-set-original-header-internal (entity header)
303 `(aset ,entity 16 ,header))
304 (defmacro mime-entity-parsed-header-internal (entity)
306 (defmacro mime-entity-set-parsed-header-internal (entity header)
307 `(aset ,entity 17 ,header))
309 (defmacro mime-entity-buffer-internal (entity)
311 (defmacro mime-entity-set-buffer-internal (entity buffer)
312 `(aset ,entity 18 ,buffer))
313 (defmacro mime-entity-header-start-internal (entity)
315 (defmacro mime-entity-set-header-start-internal (entity point)
316 `(aset ,entity 19 ,point))
317 (defmacro mime-entity-header-end-internal (entity)
319 (defmacro mime-entity-set-header-end-internal (entity point)
320 `(aset ,entity 20 ,point))
321 (defmacro mime-entity-body-start-internal (entity)
323 (defmacro mime-entity-set-body-start-internal (entity point)
324 `(aset ,entity 21 ,point))
325 (defmacro mime-entity-body-end-internal (entity)
327 (defmacro mime-entity-set-body-end-internal (entity point)
328 `(aset ,entity 22 ,point))
331 ;;; @ message structure
334 (defvar mime-message-structure nil
335 "Information about structure of message.
336 Please use reference function `mime-entity-SLOT' to get value of SLOT.
338 Following is a list of slots of the structure:
340 buffer buffer includes this entity (buffer).
341 node-id node-id (list of integers)
342 header-start minimum point of header in raw-buffer
343 header-end maximum point of header in raw-buffer
344 body-start minimum point of body in raw-buffer
345 body-end maximum point of body in raw-buffer
346 content-type content-type (content-type)
347 content-disposition content-disposition (content-disposition)
348 encoding Content-Transfer-Encoding (string or nil)
349 children entities included in this entity (list of entity)
351 If an entity includes other entities in its body, such as multipart or
352 message/rfc822, `mime-entity' structures of them are included in
353 `children', so the `mime-entity' structure become a tree.")
355 (make-variable-buffer-local 'mime-message-structure)
363 (defvar mime-entity-implementation-alist nil)
365 (defmacro mm-define-backend (type &optional parents)
366 "Define TYPE as a mm-backend.
367 If PARENTS is specified, TYPE inherits PARENTS.
368 Each parent must be backend name (symbol)."
370 `(let ((rest ',(reverse parents)))
372 (set-alist 'mime-entity-implementation-alist
375 (cdr (assq (car rest)
376 mime-entity-implementation-alist))))
377 (setq rest (cdr rest))
380 (defmacro mm-define-method (name args &rest body)
381 "Define NAME as a method function of (nth 1 (car ARGS)) backend.
383 ARGS is like an argument list of lambda, but (car ARGS) must be
384 specialized parameter. (car (car ARGS)) is name of variable and (nth
385 1 (car ARGS)) is name of backend."
386 (let* ((specializer (car args))
387 (class (nth 1 specializer))
388 (self (car specializer)))
389 `(let ((imps (cdr (assq ',class mime-entity-implementation-alist)))
390 (func (lambda ,(if self
391 (cons self (cdr args))
395 (set-alist 'mime-entity-implementation-alist
396 ',class (put-alist ',name func imps))
397 (set-alist 'mime-entity-implementation-alist
399 (list (cons ',name func)))
402 (put 'mm-define-method 'lisp-indent-function 'defun)
405 (defmacro eval-module-depended-macro (module definition)
408 (require (eval module))
410 (error `(eval-after-load ,(symbol-name (eval module)) ',definition))
414 (eval-module-depended-macro
416 (def-edebug-spec mm-define-method
417 (&define name ((arg symbolp)
419 [&optional ["&optional" arg &rest arg]]
420 &optional ["&rest" arg]
425 (defsubst mm-arglist-to-arguments (arglist)
428 (let ((arg (car arglist)))
429 (or (memq arg '(&optional &rest))
430 (setq dest (cons arg dest)))
432 (setq arglist (cdr arglist)))
436 ;;; @ for mel-backend
439 (defvar mel-service-list nil)
441 (defmacro mel-define-service (name &optional args &rest rest)
442 "Define NAME as a service for Content-Transfer-Encodings.
443 If ARGS is specified, NAME is defined as a generic function for the
446 (add-to-list 'mel-service-list ',name)
447 (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
451 (funcall (mel-find-function ',name ,(car (last args)))
452 ,@(mm-arglist-to-arguments (butlast args)))
456 (put 'mel-define-service 'lisp-indent-function 'defun)
459 (defvar mel-encoding-module-alist nil)
461 (defsubst mel-find-function-from-obarray (ob-array encoding)
462 (let* ((f (intern-soft encoding ob-array)))
464 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
468 (null (setq f (intern-soft encoding ob-array)))
470 (setq rest (cdr rest))
474 (defsubst mel-copy-method (service src-backend dst-backend)
475 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
476 (f (mel-find-function-from-obarray oa src-backend))
479 (setq sym (intern dst-backend oa))
481 (fset sym (symbol-function f))
484 (defsubst mel-copy-backend (src-backend dst-backend)
485 (let ((services mel-service-list))
487 (mel-copy-method (car services) src-backend dst-backend)
488 (setq services (cdr services)))))
490 (defmacro mel-define-backend (type &optional parents)
491 "Define TYPE as a mel-backend.
492 If PARENTS is specified, TYPE inherits PARENTS.
493 Each parent must be backend name (string)."
495 (mapcar (lambda (parent)
496 `(mel-copy-backend ,parent ,type)
500 (defmacro mel-define-method (name args &rest body)
501 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
502 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
503 specialized parameter. (car (car (last ARGS))) is name of variable
504 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
505 (let* ((specializer (car (last args)))
506 (class (nth 1 specializer)))
508 (mel-define-service ,name)
509 (fset (intern ,class ,(intern (format "%s-obarray" name)))
510 (lambda ,(butlast args)
513 (put 'mel-define-method 'lisp-indent-function 'defun)
515 (defmacro mel-define-method-function (spec function)
516 "Set SPEC's function definition to FUNCTION.
517 First element of SPEC is service.
518 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
519 must be specialized parameter. (car (car (last ARGS))) is name of
520 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
521 (let* ((name (car spec))
523 (specializer (car (last args)))
524 (class (nth 1 specializer)))
526 (mel-define-service ,name)
527 (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
529 (fset sym (symbol-function ,function))))))
531 (defmacro mel-define-function (function spec)
532 (let* ((name (car spec))
534 (specializer (car (last args)))
535 (class (nth 1 specializer)))
537 (define-function ,function
538 (intern ,class ,(intern (format "%s-obarray" name))))
541 (defvar base64-dl-module
542 (if (and (fboundp 'base64-encode-string)
543 (subrp (symbol-function 'base64-encode-string)))
545 (if (fboundp 'dynamic-link)
546 (let ((path (expand-file-name "base64.so" exec-directory)))
547 (and (file-exists-p path)
557 ;;; mime-def.el ends here