1 ;;; mime-def.el --- definition module about MIME
3 ;; Copyright (C) 1995,1996,1997,1998 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 FLAM (Faithful Library About MIME).
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 ["FLAM-DOODLE" (1 12 0) "
\e$B[XHi
\e(B 2.0YR3.5/4.0"]
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-tspecials "][()<>@,\;:\\\"/?=")
103 (defconst mime-token-regexp (concat "[^" mime-tspecials "\000-\040]+"))
104 (defconst mime-charset-regexp mime-token-regexp)
106 (defconst mime-media-type/subtype-regexp
107 (concat mime-token-regexp "/" mime-token-regexp))
113 (defconst base64-token-regexp "[A-Za-z0-9+/]")
114 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
116 (defconst B-encoded-text-regexp
125 base64-token-padding-regexp
126 base64-token-padding-regexp
129 ;; (defconst eword-B-encoding-and-encoded-text-regexp
130 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
133 ;;; @@ Quoted-Printable / Q
136 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
138 (defconst quoted-printable-octet-regexp
139 (concat "=[" quoted-printable-hex-chars
140 "][" quoted-printable-hex-chars "]"))
142 (defconst Q-encoded-text-regexp
143 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
145 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
146 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
152 (defsubst make-mime-content-type (type subtype &optional parameters)
153 (list* (cons 'type type)
154 (cons 'subtype subtype)
155 (nreverse parameters))
158 (defsubst mime-content-type-primary-type (content-type)
159 "Return primary-type of CONTENT-TYPE."
160 (cdr (car content-type)))
162 (defsubst mime-content-type-subtype (content-type)
163 "Return primary-type of CONTENT-TYPE."
164 (cdr (cadr content-type)))
166 (defsubst mime-content-type-parameters (content-type)
167 "Return primary-type of CONTENT-TYPE."
170 (defsubst mime-content-type-parameter (content-type parameter)
171 "Return PARAMETER value of CONTENT-TYPE."
172 (cdr (assoc parameter (mime-content-type-parameters content-type))))
175 (defsubst mime-type/subtype-string (type &optional subtype)
176 "Return type/subtype string from TYPE and SUBTYPE."
179 (format "%s/%s" type subtype)
180 (format "%s" type))))
183 ;;; @ Content-Disposition
186 (defsubst mime-content-disposition-type (content-disposition)
187 "Return disposition-type of CONTENT-DISPOSITION."
188 (cdr (car content-disposition)))
190 (defsubst mime-content-disposition-parameters (content-disposition)
191 "Return disposition-parameters of CONTENT-DISPOSITION."
192 (cdr content-disposition))
194 (defsubst mime-content-disposition-parameter (content-disposition parameter)
195 "Return PARAMETER value of CONTENT-DISPOSITION."
196 (cdr (assoc parameter (cdr content-disposition))))
198 (defsubst mime-content-disposition-filename (content-disposition)
199 "Return filename of CONTENT-DISPOSITION."
200 (mime-content-disposition-parameter content-disposition "filename"))
206 (defmacro make-mime-entity-internal (representation-type location
207 &optional content-type
208 children parent node-id
210 decoded-subject decoded-from
211 date message-id references
215 original-header parsed-header
216 ;; for buffer representation
218 header-start header-end
220 `(vector ,representation-type ,location
221 ,content-type nil nil ,children ,parent ,node-id
223 ,decoded-subject ,decoded-from
224 ,date ,message-id ,references
228 ,original-header ,parsed-header
229 ;; for buffer representation
230 ,buffer ,header-start ,header-end ,body-start ,body-end))
232 (defmacro mime-entity-representation-type-internal (entity)
234 (defmacro mime-entity-set-representation-type-internal (entity type)
235 `(aset ,entity 0 ,type))
236 (defmacro mime-entity-location-internal (entity)
238 (defmacro mime-entity-set-location-internal (entity location)
239 `(aset ,entity 1 ,location))
241 (defmacro mime-entity-content-type-internal (entity)
243 (defmacro mime-entity-set-content-type-internal (entity type)
244 `(aset ,entity 2 ,type))
245 (defmacro mime-entity-content-disposition-internal (entity)
247 (defmacro mime-entity-set-content-disposition-internal (entity disposition)
248 `(aset ,entity 3 ,disposition))
249 (defmacro mime-entity-encoding-internal (entity)
251 (defmacro mime-entity-set-encoding-internal (entity encoding)
252 `(aset ,entity 4 ,encoding))
254 (defmacro mime-entity-children-internal (entity)
256 (defmacro mime-entity-set-children-internal (entity children)
257 `(aset ,entity 5 ,children))
258 (defmacro mime-entity-parent-internal (entity)
260 (defmacro mime-entity-node-id-internal (entity)
263 (defmacro mime-entity-decoded-subject-internal (entity)
265 (defmacro mime-entity-set-decoded-subject-internal (entity subject)
266 `(aset ,entity 8 ,subject))
267 (defmacro mime-entity-decoded-from-internal (entity)
269 (defmacro mime-entity-set-decoded-from-internal (entity from)
270 `(aset ,entity 9 ,from))
271 (defmacro mime-entity-date-internal (entity)
273 (defmacro mime-entity-set-date-internal (entity date)
274 `(aset ,entity 10 ,date))
275 (defmacro mime-entity-message-id-internal (entity)
277 (defmacro mime-entity-set-message-id-internal (entity message-id)
278 `(aset ,entity 11 ,message-id))
279 (defmacro mime-entity-references-internal (entity)
281 (defmacro mime-entity-set-references-internal (entity references)
282 `(aset ,entity 12 ,references))
283 (defmacro mime-entity-chars-internal (entity)
285 (defmacro mime-entity-set-chars-internal (entity chars)
286 `(aset ,entity 13 ,chars))
287 (defmacro mime-entity-lines-internal (entity)
289 (defmacro mime-entity-set-lines-internal (entity lines)
290 `(aset ,entity 14 ,lines))
291 (defmacro mime-entity-xref-internal (entity)
293 (defmacro mime-entity-set-xref-internal (entity xref)
294 `(aset ,entity 15 ,xref))
296 (defmacro mime-entity-original-header-internal (entity)
298 (defmacro mime-entity-set-original-header-internal (entity header)
299 `(aset ,entity 16 ,header))
300 (defmacro mime-entity-parsed-header-internal (entity)
302 (defmacro mime-entity-set-parsed-header-internal (entity header)
303 `(aset ,entity 17 ,header))
305 (defmacro mime-entity-buffer-internal (entity)
307 (defmacro mime-entity-set-buffer-internal (entity buffer)
308 `(aset ,entity 18 ,buffer))
309 (defmacro mime-entity-header-start-internal (entity)
311 (defmacro mime-entity-set-header-start-internal (entity point)
312 `(aset ,entity 19 ,point))
313 (defmacro mime-entity-header-end-internal (entity)
315 (defmacro mime-entity-set-header-end-internal (entity point)
316 `(aset ,entity 20 ,point))
317 (defmacro mime-entity-body-start-internal (entity)
319 (defmacro mime-entity-set-body-start-internal (entity point)
320 `(aset ,entity 21 ,point))
321 (defmacro mime-entity-body-end-internal (entity)
323 (defmacro mime-entity-set-body-end-internal (entity point)
324 `(aset ,entity 22 ,point))
327 ;;; @ message structure
330 (defvar mime-message-structure nil
331 "Information about structure of message.
332 Please use reference function `mime-entity-SLOT' to get value of SLOT.
334 Following is a list of slots of the structure:
336 buffer buffer includes this entity (buffer).
337 node-id node-id (list of integers)
338 header-start minimum point of header in raw-buffer
339 header-end maximum point of header in raw-buffer
340 body-start minimum point of body in raw-buffer
341 body-end maximum point of body in raw-buffer
342 content-type content-type (content-type)
343 content-disposition content-disposition (content-disposition)
344 encoding Content-Transfer-Encoding (string or nil)
345 children entities included in this entity (list of entity)
347 If an entity includes other entities in its body, such as multipart or
348 message/rfc822, `mime-entity' structures of them are included in
349 `children', so the `mime-entity' structure become a tree.")
351 (make-variable-buffer-local 'mime-message-structure)
359 (defvar mime-entity-implementation-alist nil)
361 (defmacro mm-define-backend (type &optional parents)
362 "Define TYPE as a mm-backend.
363 If PARENTS is specified, TYPE inherits PARENTS.
364 Each parent must be backend name (symbol)."
366 `(let ((rest ',(reverse parents)))
368 (set-alist 'mime-entity-implementation-alist
371 (cdr (assq (car rest)
372 mime-entity-implementation-alist))))
373 (setq rest (cdr rest))
376 (defmacro mm-define-method (name args &rest body)
377 "Define NAME as a method function of (nth 1 (car ARGS)) backend.
379 ARGS is like an argument list of lambda, but (car ARGS) must be
380 specialized parameter. (car (car ARGS)) is name of variable and (nth
381 1 (car ARGS)) is name of backend."
382 (let* ((specializer (car args))
383 (class (nth 1 specializer))
384 (self (car specializer)))
385 `(let ((imps (cdr (assq ',class mime-entity-implementation-alist)))
386 (func (lambda ,(if self
387 (cons self (cdr args))
391 (set-alist 'mime-entity-implementation-alist
392 ',class (put-alist ',name func imps))
393 (set-alist 'mime-entity-implementation-alist
395 (list (cons ',name func)))
398 (put 'mm-define-method 'lisp-indent-function 'defun)
401 (defmacro eval-module-depended-macro (module definition)
404 (require (eval module))
406 (error `(eval-after-load ,(symbol-name (eval module)) ',definition))
410 (eval-module-depended-macro
412 (def-edebug-spec mm-define-method
413 (&define name ((arg symbolp)
415 [&optional ["&optional" arg &rest arg]]
416 &optional ["&rest" arg]
421 (defsubst mm-arglist-to-arguments (arglist)
424 (let ((arg (car arglist)))
425 (or (memq arg '(&optional &rest))
426 (setq dest (cons arg dest)))
428 (setq arglist (cdr arglist)))
432 ;;; @ for mel-backend
435 (defvar mel-service-list nil)
437 (defmacro mel-define-service (name &optional args &rest rest)
438 "Define NAME as a service for Content-Transfer-Encodings.
439 If ARGS is specified, NAME is defined as a generic function for the
442 (add-to-list 'mel-service-list ',name)
443 (defvar ,(intern (format "%s-obarray" name)) (make-vector 1 nil))
447 (funcall (mel-find-function ',name ,(car (last args)))
448 ,@(mm-arglist-to-arguments (butlast args)))
452 (put 'mel-define-service 'lisp-indent-function 'defun)
455 (defvar mel-encoding-module-alist nil)
457 (defsubst mel-find-function-from-obarray (ob-array encoding)
458 (let* ((f (intern-soft encoding ob-array)))
460 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
464 (null (setq f (intern-soft encoding ob-array)))
466 (setq rest (cdr rest))
470 (defsubst mel-copy-method (service src-backend dst-backend)
471 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
472 (f (mel-find-function-from-obarray oa src-backend))
475 (setq sym (intern dst-backend oa))
477 (fset sym (symbol-function f))
480 (defsubst mel-copy-backend (src-backend dst-backend)
481 (let ((services mel-service-list))
483 (mel-copy-method (car services) src-backend dst-backend)
484 (setq services (cdr services)))))
486 (defmacro mel-define-backend (type &optional parents)
487 "Define TYPE as a mel-backend.
488 If PARENTS is specified, TYPE inherits PARENTS.
489 Each parent must be backend name (string)."
491 (mapcar (lambda (parent)
492 `(mel-copy-backend ,parent ,type)
496 (defmacro mel-define-method (name args &rest body)
497 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
498 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
499 specialized parameter. (car (car (last ARGS))) is name of variable
500 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
501 (let* ((specializer (car (last args)))
502 (class (nth 1 specializer)))
504 (mel-define-service ,name)
505 (fset (intern ,class ,(intern (format "%s-obarray" name)))
506 (lambda ,(butlast args)
509 (put 'mel-define-method 'lisp-indent-function 'defun)
511 (defmacro mel-define-method-function (spec function)
512 "Set SPEC's function definition to FUNCTION.
513 First element of SPEC is service.
514 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
515 must be specialized parameter. (car (car (last ARGS))) is name of
516 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
517 (let* ((name (car spec))
519 (specializer (car (last args)))
520 (class (nth 1 specializer)))
522 (mel-define-service ,name)
523 (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
525 (fset sym (symbol-function ,function))))))
527 (defmacro mel-define-function (function spec)
528 (let* ((name (car spec))
530 (specializer (car (last args)))
531 (class (nth 1 specializer)))
533 (define-function ,function
534 (intern ,class ,(intern (format "%s-obarray" name))))
537 (defvar base64-dl-module
538 (if (and (fboundp 'base64-encode-string)
539 (subrp (symbol-function 'base64-encode-string)))
541 (if (fboundp 'dynamic-link)
542 (let ((path (expand-file-name "base64.so" exec-directory)))
543 (and (file-exists-p path)
553 ;;; mime-def.el ends here