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 "[^" (apply #'string std11-non-qtext-char-list) "]"))))
91 (defconst std11-quoted-string-regexp
95 (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)
362 (defvar mime-entity-implementation-alist nil)
364 (defmacro mm-define-backend (type &optional parents)
365 "Define TYPE as a mm-backend.
366 If PARENTS is specified, TYPE inherits PARENTS.
367 Each parent must be backend name (symbol)."
369 `(let ((rest ',(reverse parents)))
371 (set-alist 'mime-entity-implementation-alist
374 (cdr (assq (car rest)
375 mime-entity-implementation-alist))))
376 (setq rest (cdr rest))
379 (defmacro mm-define-method (name args &rest body)
380 "Define NAME as a method function of (nth 1 (car ARGS)) backend.
382 ARGS is like an argument list of lambda, but (car ARGS) must be
383 specialized parameter. (car (car ARGS)) is name of variable and (nth
384 1 (car ARGS)) is name of backend."
385 (let* ((specializer (car args))
386 (class (nth 1 specializer))
387 (self (car specializer)))
388 `(let ((imps (cdr (assq ',class mime-entity-implementation-alist)))
389 (func (lambda ,(if self
390 (cons self (cdr args))
394 (set-alist 'mime-entity-implementation-alist
395 ',class (put-alist ',name func imps))
396 (set-alist 'mime-entity-implementation-alist
398 (list (cons ',name func)))
401 (put 'mm-define-method 'lisp-indent-function 'defun)
404 (defmacro eval-module-depended-macro (module definition)
407 (require (eval module))
409 (error `(eval-after-load ,(symbol-name (eval module)) ',definition))
413 (eval-module-depended-macro
415 (def-edebug-spec mm-define-method
416 (&define name ((arg symbolp)
418 [&optional ["&optional" arg &rest arg]]
419 &optional ["&rest" arg]
424 (defsubst mm-arglist-to-arguments (arglist)
427 (let ((arg (car arglist)))
428 (or (memq arg '(&optional &rest))
429 (setq dest (cons arg dest)))
431 (setq arglist (cdr arglist)))
435 ;;; @ for mel-backend
438 (defvar mel-service-list nil)
440 (defmacro mel-define-service (name &optional args &rest rest)
441 "Define NAME as a service for Content-Transfer-Encodings.
442 If ARGS is specified, NAME is defined as a generic function for the
445 (add-to-list 'mel-service-list ',name)
446 (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
450 (funcall (mel-find-function ',name ,(car (last args)))
451 ,@(mm-arglist-to-arguments (butlast args)))
455 (put 'mel-define-service 'lisp-indent-function 'defun)
458 (defvar mel-encoding-module-alist nil)
460 (defsubst mel-find-function-from-obarray (ob-array encoding)
461 (let* ((f (intern-soft encoding ob-array)))
463 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
467 (null (setq f (intern-soft encoding ob-array)))
469 (setq rest (cdr rest))
473 (defsubst mel-copy-method (service src-backend dst-backend)
474 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
475 (f (mel-find-function-from-obarray oa src-backend))
478 (setq sym (intern dst-backend oa))
480 (fset sym (symbol-function f))
483 (defsubst mel-copy-backend (src-backend dst-backend)
484 (let ((services mel-service-list))
486 (mel-copy-method (car services) src-backend dst-backend)
487 (setq services (cdr services)))))
489 (defmacro mel-define-backend (type &optional parents)
490 "Define TYPE as a mel-backend.
491 If PARENTS is specified, TYPE inherits PARENTS.
492 Each parent must be backend name (string)."
494 (mapcar (lambda (parent)
495 `(mel-copy-backend ,parent ,type)
499 (defmacro mel-define-method (name args &rest body)
500 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
501 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
502 specialized parameter. (car (car (last ARGS))) is name of variable
503 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
504 (let* ((specializer (car (last args)))
505 (class (nth 1 specializer)))
507 (mel-define-service ,name)
508 (fset (intern ,class ,(intern (format "%s-obarray" name)))
509 (lambda ,(butlast args)
512 (put 'mel-define-method 'lisp-indent-function 'defun)
514 (defmacro mel-define-method-function (spec function)
515 "Set SPEC's function definition to FUNCTION.
516 First element of SPEC is service.
517 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
518 must be specialized parameter. (car (car (last ARGS))) is name of
519 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
520 (let* ((name (car spec))
522 (specializer (car (last args)))
523 (class (nth 1 specializer)))
525 (mel-define-service ,name)
526 (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
528 (fset sym (symbol-function ,function))))))
530 (defmacro mel-define-function (function spec)
531 (let* ((name (car spec))
533 (specializer (car (last args)))
534 (class (nth 1 specializer)))
536 (define-function ,function
537 (intern ,class ,(intern (format "%s-obarray" name))))
540 (defvar base64-dl-module
541 (if (and (fboundp 'base64-encode-string)
542 (subrp (symbol-function 'base64-encode-string)))
544 (if (fboundp 'dynamic-link)
545 (let ((path (expand-file-name "base64.so" exec-directory)))
546 (and (file-exists-p path)
556 ;;; mime-def.el ends here