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.
33 (eval-when-compile (require 'cl)) ; list*
36 (defconst mime-library-product ["FLIM" (1 12 7) "Y
\e.D
\8eþzaki"]
37 "Product name, version number and code name of MIME-library package.")
40 (defmacro mime-product-name (product)
43 (defmacro mime-product-version (product)
46 (defmacro mime-product-code-name (product)
49 (defconst mime-library-version
51 (concat (mime-product-name mime-library-product) " "
52 (mapconcat #'number-to-string
53 (mime-product-version mime-library-product) ".")
54 " - \"" (mime-product-code-name mime-library-product) "\"")))
60 (defgroup mime '((default-mime-charset custom-variable))
61 "Emacs MIME Interfaces"
65 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
66 "*List of encoding names for uuencode format."
68 :type '(repeat string))
71 ;;; @ required functions
74 (defsubst regexp-* (regexp)
77 (defsubst regexp-or (&rest args)
78 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
85 (defconst std11-quoted-pair-regexp "\\\\.")
86 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
87 (defconst std11-qtext-regexp
89 (concat "[^" std11-non-qtext-char-list "]"))))
90 (defconst std11-quoted-string-regexp
94 (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
102 (defconst mime-tspecial-char-list
103 '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=)))
104 (defconst mime-token-regexp
106 (concat "[^" mime-tspecial-char-list "\000-\040]+")))
107 (defconst mime-charset-regexp mime-token-regexp)
109 (defconst mime-media-type/subtype-regexp
110 (concat mime-token-regexp "/" mime-token-regexp))
116 (defconst base64-token-regexp "[A-Za-z0-9+/]")
117 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
119 (defconst B-encoded-text-regexp
128 base64-token-padding-regexp
129 base64-token-padding-regexp
132 ;; (defconst eword-B-encoding-and-encoded-text-regexp
133 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
136 ;;; @@ Quoted-Printable / Q
139 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
141 (defconst quoted-printable-octet-regexp
142 (concat "=[" quoted-printable-hex-chars
143 "][" quoted-printable-hex-chars "]"))
145 (defconst Q-encoded-text-regexp
146 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
148 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
149 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
155 (defsubst make-mime-content-type (type subtype &optional parameters)
156 (list* (cons 'type type)
157 (cons 'subtype subtype)
158 (nreverse parameters))
161 (defsubst mime-content-type-primary-type (content-type)
162 "Return primary-type of CONTENT-TYPE."
163 (cdr (car content-type)))
165 (defsubst mime-content-type-subtype (content-type)
166 "Return primary-type of CONTENT-TYPE."
167 (cdr (cadr content-type)))
169 (defsubst mime-content-type-parameters (content-type)
170 "Return primary-type of CONTENT-TYPE."
173 (defsubst mime-content-type-parameter (content-type parameter)
174 "Return PARAMETER value of CONTENT-TYPE."
175 (cdr (assoc parameter (mime-content-type-parameters content-type))))
178 (defsubst mime-type/subtype-string (type &optional subtype)
179 "Return type/subtype string from TYPE and SUBTYPE."
182 (format "%s/%s" type subtype)
183 (format "%s" type))))
186 ;;; @ Content-Disposition
189 (defsubst mime-content-disposition-type (content-disposition)
190 "Return disposition-type of CONTENT-DISPOSITION."
191 (cdr (car content-disposition)))
193 (defsubst mime-content-disposition-parameters (content-disposition)
194 "Return disposition-parameters of CONTENT-DISPOSITION."
195 (cdr content-disposition))
197 (defsubst mime-content-disposition-parameter (content-disposition parameter)
198 "Return PARAMETER value of CONTENT-DISPOSITION."
199 (cdr (assoc parameter (cdr content-disposition))))
201 (defsubst mime-content-disposition-filename (content-disposition)
202 "Return filename of CONTENT-DISPOSITION."
203 (mime-content-disposition-parameter content-disposition "filename"))
209 (defmacro make-mime-entity-internal (representation-type location
210 &optional content-type
211 children parent node-id
213 decoded-subject decoded-from
214 date message-id references
218 original-header parsed-header
219 ;; for buffer representation
221 header-start header-end
223 `(vector ,representation-type ,location
224 ,content-type nil nil ,children ,parent ,node-id
226 ,decoded-subject ,decoded-from
227 ,date ,message-id ,references
231 ,original-header ,parsed-header
232 ;; for buffer representation
233 ,buffer ,header-start ,header-end ,body-start ,body-end))
235 (defmacro mime-entity-representation-type-internal (entity)
237 (defmacro mime-entity-set-representation-type-internal (entity type)
238 `(aset ,entity 0 ,type))
239 (defmacro mime-entity-location-internal (entity)
241 (defmacro mime-entity-set-location-internal (entity location)
242 `(aset ,entity 1 ,location))
244 (defmacro mime-entity-content-type-internal (entity)
246 (defmacro mime-entity-set-content-type-internal (entity type)
247 `(aset ,entity 2 ,type))
248 (defmacro mime-entity-content-disposition-internal (entity)
250 (defmacro mime-entity-set-content-disposition-internal (entity disposition)
251 `(aset ,entity 3 ,disposition))
252 (defmacro mime-entity-encoding-internal (entity)
254 (defmacro mime-entity-set-encoding-internal (entity encoding)
255 `(aset ,entity 4 ,encoding))
257 (defmacro mime-entity-children-internal (entity)
259 (defmacro mime-entity-set-children-internal (entity children)
260 `(aset ,entity 5 ,children))
261 (defmacro mime-entity-parent-internal (entity)
263 (defmacro mime-entity-node-id-internal (entity)
266 (defmacro mime-entity-decoded-subject-internal (entity)
268 (defmacro mime-entity-set-decoded-subject-internal (entity subject)
269 `(aset ,entity 8 ,subject))
270 (defmacro mime-entity-decoded-from-internal (entity)
272 (defmacro mime-entity-set-decoded-from-internal (entity from)
273 `(aset ,entity 9 ,from))
274 (defmacro mime-entity-date-internal (entity)
276 (defmacro mime-entity-set-date-internal (entity date)
277 `(aset ,entity 10 ,date))
278 (defmacro mime-entity-message-id-internal (entity)
280 (defmacro mime-entity-set-message-id-internal (entity message-id)
281 `(aset ,entity 11 ,message-id))
282 (defmacro mime-entity-references-internal (entity)
284 (defmacro mime-entity-set-references-internal (entity references)
285 `(aset ,entity 12 ,references))
286 (defmacro mime-entity-chars-internal (entity)
288 (defmacro mime-entity-set-chars-internal (entity chars)
289 `(aset ,entity 13 ,chars))
290 (defmacro mime-entity-lines-internal (entity)
292 (defmacro mime-entity-set-lines-internal (entity lines)
293 `(aset ,entity 14 ,lines))
294 (defmacro mime-entity-xref-internal (entity)
296 (defmacro mime-entity-set-xref-internal (entity xref)
297 `(aset ,entity 15 ,xref))
299 (defmacro mime-entity-original-header-internal (entity)
301 (defmacro mime-entity-set-original-header-internal (entity header)
302 `(aset ,entity 16 ,header))
303 (defmacro mime-entity-parsed-header-internal (entity)
305 (defmacro mime-entity-set-parsed-header-internal (entity header)
306 `(aset ,entity 17 ,header))
308 (defmacro mime-entity-buffer-internal (entity)
310 (defmacro mime-entity-set-buffer-internal (entity buffer)
311 `(aset ,entity 18 ,buffer))
312 (defmacro mime-entity-header-start-internal (entity)
314 (defmacro mime-entity-set-header-start-internal (entity point)
315 `(aset ,entity 19 ,point))
316 (defmacro mime-entity-header-end-internal (entity)
318 (defmacro mime-entity-set-header-end-internal (entity point)
319 `(aset ,entity 20 ,point))
320 (defmacro mime-entity-body-start-internal (entity)
322 (defmacro mime-entity-set-body-start-internal (entity point)
323 `(aset ,entity 21 ,point))
324 (defmacro mime-entity-body-end-internal (entity)
326 (defmacro mime-entity-set-body-end-internal (entity point)
327 `(aset ,entity 22 ,point))
330 ;;; @ message structure
333 (defvar mime-message-structure nil
334 "Information about structure of message.
335 Please use reference function `mime-entity-SLOT' to get value of SLOT.
337 Following is a list of slots of the structure:
339 buffer buffer includes this entity (buffer).
340 node-id node-id (list of integers)
341 header-start minimum point of header in raw-buffer
342 header-end maximum point of header in raw-buffer
343 body-start minimum point of body in raw-buffer
344 body-end maximum point of body in raw-buffer
345 content-type content-type (content-type)
346 content-disposition content-disposition (content-disposition)
347 encoding Content-Transfer-Encoding (string or nil)
348 children entities included in this entity (list of entity)
350 If an entity includes other entities in its body, such as multipart or
351 message/rfc822, `mime-entity' structures of them are included in
352 `children', so the `mime-entity' structure become a tree.")
354 (make-variable-buffer-local 'mime-message-structure)
360 (defvar mime-entity-implementation-alist nil)
362 (defmacro mm-define-backend (type &optional parents)
363 "Define TYPE as a mm-backend.
364 If PARENTS is specified, TYPE inherits PARENTS.
365 Each parent must be backend name (symbol)."
367 `(let ((rest ',(reverse parents)))
369 (set-alist 'mime-entity-implementation-alist
372 (cdr (assq (car rest)
373 mime-entity-implementation-alist))))
374 (setq rest (cdr rest))
377 (defmacro mm-define-method (name args &rest body)
378 "Define NAME as a method function of (nth 1 (car ARGS)) backend.
380 ARGS is like an argument list of lambda, but (car ARGS) must be
381 specialized parameter. (car (car ARGS)) is name of variable and (nth
382 1 (car ARGS)) is name of backend."
383 (let* ((specializer (car args))
384 (class (nth 1 specializer))
385 (self (car specializer)))
386 `(let ((imps (cdr (assq ',class mime-entity-implementation-alist)))
387 (func (lambda ,(if self
388 (cons self (cdr args))
392 (set-alist 'mime-entity-implementation-alist
393 ',class (put-alist ',name func imps))
394 (set-alist 'mime-entity-implementation-alist
396 (list (cons ',name func)))
399 (put 'mm-define-method 'lisp-indent-function 'defun)
402 (defmacro eval-module-depended-macro (module definition)
405 (require (eval module))
407 (error `(eval-after-load ,(symbol-name (eval module)) ',definition))
411 (eval-module-depended-macro
413 (def-edebug-spec mm-define-method
414 (&define name ((arg symbolp)
416 [&optional ["&optional" arg &rest arg]]
417 &optional ["&rest" arg]
422 (defsubst mm-arglist-to-arguments (arglist)
425 (let ((arg (car arglist)))
426 (or (memq arg '(&optional &rest))
427 (setq dest (cons arg dest)))
429 (setq arglist (cdr arglist)))
433 ;;; @ for mel-backend
436 (defvar mel-service-list nil)
438 (defmacro mel-define-service (name &optional args &rest rest)
439 "Define NAME as a service for Content-Transfer-Encodings.
440 If ARGS is specified, NAME is defined as a generic function for the
443 (add-to-list 'mel-service-list ',name)
444 (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
448 (funcall (mel-find-function ',name ,(car (last args)))
449 ,@(mm-arglist-to-arguments (butlast args)))
453 (put 'mel-define-service 'lisp-indent-function 'defun)
456 (defvar mel-encoding-module-alist nil)
458 (defsubst mel-find-function-from-obarray (ob-array encoding)
459 (let* ((f (intern-soft encoding ob-array)))
461 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
465 (null (setq f (intern-soft encoding ob-array)))
467 (setq rest (cdr rest))
471 (defsubst mel-copy-method (service src-backend dst-backend)
472 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
473 (f (mel-find-function-from-obarray oa src-backend))
476 (setq sym (intern dst-backend oa))
478 (fset sym (symbol-function f))
481 (defsubst mel-copy-backend (src-backend dst-backend)
482 (let ((services mel-service-list))
484 (mel-copy-method (car services) src-backend dst-backend)
485 (setq services (cdr services)))))
487 (defmacro mel-define-backend (type &optional parents)
488 "Define TYPE as a mel-backend.
489 If PARENTS is specified, TYPE inherits PARENTS.
490 Each parent must be backend name (string)."
492 (mapcar (lambda (parent)
493 `(mel-copy-backend ,parent ,type)
497 (defmacro mel-define-method (name args &rest body)
498 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
499 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
500 specialized parameter. (car (car (last ARGS))) is name of variable
501 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
502 (let* ((specializer (car (last args)))
503 (class (nth 1 specializer)))
505 (mel-define-service ,name)
506 (fset (intern ,class ,(intern (format "%s-obarray" name)))
507 (lambda ,(butlast args)
510 (put 'mel-define-method 'lisp-indent-function 'defun)
512 (defmacro mel-define-method-function (spec function)
513 "Set SPEC's function definition to FUNCTION.
514 First element of SPEC is service.
515 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
516 must be specialized parameter. (car (car (last ARGS))) is name of
517 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
518 (let* ((name (car spec))
520 (specializer (car (last args)))
521 (class (nth 1 specializer)))
523 (mel-define-service ,name)
524 (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
526 (fset sym (symbol-function ,function))))))
528 (defmacro mel-define-function (function spec)
529 (let* ((name (car spec))
531 (specializer (car (last args)))
532 (class (nth 1 specializer)))
534 (define-function ,function
535 (intern ,class ,(intern (format "%s-obarray" name))))
538 (defvar base64-dl-module
539 (if (and (fboundp 'base64-encode-string)
540 (subrp (symbol-function 'base64-encode-string)))
542 (if (fboundp 'dynamic-link)
543 (let ((path (expand-file-name "base64.so" exec-directory)))
544 (and (file-exists-p path)
554 ;;; mime-def.el ends here