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.
28 (defconst mime-library-product ["FLAM-DOODLE" (1 11 0) "
\e$B>.F&
\e(B 2.5R3.5/5.0"]
29 "Product name, version number and code name of MIME-library package.")
32 (defmacro mime-product-name (product)
35 (defmacro mime-product-version (product)
38 (defmacro mime-product-code-name (product)
41 (defconst mime-library-version
43 (concat (mime-product-name mime-library-product) " "
44 (mapconcat #'number-to-string
45 (mime-product-version mime-library-product) ".")
46 " - \"" (mime-product-code-name mime-library-product) "\"")))
54 (eval-when-compile (require 'cl))
57 "Emacs MIME Interfaces"
61 (custom-handle-keyword 'default-mime-charset :group 'mime
64 (defcustom mime-temp-directory (or (getenv "MIME_TMP_DIR")
70 "*Directory for temporary files."
74 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
75 "*List of encoding names for uuencode format."
77 :type '(repeat string))
80 ;;; @ required functions
83 (defsubst eliminate-top-spaces (string)
84 "Eliminate top sequence of space or tab in STRING."
85 (if (string-match "^[ \t]+" string)
86 (substring string (match-end 0))
89 (defsubst regexp-* (regexp)
92 (defsubst regexp-or (&rest args)
93 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
99 (defconst std11-quoted-pair-regexp "\\\\.")
100 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
101 (defconst std11-qtext-regexp
102 (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
103 (defconst std11-quoted-string-regexp
106 (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
113 (defconst mime-tspecials "][()<>@,\;:\\\"/?=")
114 (defconst mime-token-regexp (concat "[^" mime-tspecials "\000-\040]+"))
115 (defconst mime-charset-regexp mime-token-regexp)
117 (defconst mime-media-type/subtype-regexp
118 (concat mime-token-regexp "/" mime-token-regexp))
124 (defconst base64-token-regexp "[A-Za-z0-9+/]")
125 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
127 (defconst B-encoded-text-regexp
136 base64-token-padding-regexp
137 base64-token-padding-regexp
140 ;; (defconst eword-B-encoding-and-encoded-text-regexp
141 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
144 ;;; @@ Quoted-Printable / Q
147 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
149 (defconst quoted-printable-octet-regexp
150 (concat "=[" quoted-printable-hex-chars
151 "][" quoted-printable-hex-chars "]"))
153 (defconst Q-encoded-text-regexp
154 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
156 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
157 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
163 (defsubst make-mime-content-type (type subtype &optional parameters)
164 (list* (cons 'type type)
165 (cons 'subtype subtype)
166 (nreverse parameters))
169 (defsubst mime-content-type-primary-type (content-type)
170 "Return primary-type of CONTENT-TYPE."
171 (cdr (car content-type)))
173 (defsubst mime-content-type-subtype (content-type)
174 "Return primary-type of CONTENT-TYPE."
175 (cdr (cadr content-type)))
177 (defsubst mime-content-type-parameters (content-type)
178 "Return primary-type of CONTENT-TYPE."
181 (defsubst mime-content-type-parameter (content-type parameter)
182 "Return PARAMETER value of CONTENT-TYPE."
183 (cdr (assoc parameter (mime-content-type-parameters content-type))))
186 (defsubst mime-type/subtype-string (type &optional subtype)
187 "Return type/subtype string from TYPE and SUBTYPE."
190 (format "%s/%s" type subtype)
191 (format "%s" type))))
194 ;;; @ Content-Disposition
197 (defsubst mime-content-disposition-type (content-disposition)
198 "Return disposition-type of CONTENT-DISPOSITION."
199 (cdr (car content-disposition)))
201 (defsubst mime-content-disposition-parameters (content-disposition)
202 "Return disposition-parameters of CONTENT-DISPOSITION."
203 (cdr content-disposition))
205 (defsubst mime-content-disposition-parameter (content-disposition parameter)
206 "Return PARAMETER value of CONTENT-DISPOSITION."
207 (cdr (assoc parameter (cdr content-disposition))))
209 (defsubst mime-content-disposition-filename (content-disposition)
210 "Return filename of CONTENT-DISPOSITION."
211 (mime-content-disposition-parameter content-disposition "filename"))
217 (defmacro make-mime-entity-internal (representation-type location
218 &optional content-type
219 children parent node-id
221 decoded-subject decoded-from
222 date message-id references
226 original-header parsed-header
227 ;; for buffer representation
229 header-start header-end
231 `(vector ,representation-type ,location
232 ,content-type nil nil ,children ,parent ,node-id
234 ,decoded-subject ,decoded-from
235 ,date ,message-id ,references
239 ,original-header ,parsed-header
240 ;; for buffer representation
241 ,buffer ,header-start ,header-end ,body-start ,body-end))
243 (defmacro mime-entity-representation-type-internal (entity)
245 (defmacro mime-entity-set-representation-type-internal (entity type)
246 `(aset ,entity 0 ,type))
247 (defmacro mime-entity-location-internal (entity)
249 (defmacro mime-entity-set-location-internal (entity location)
250 `(aset ,entity 1 ,location))
252 (defmacro mime-entity-content-type-internal (entity)
254 (defmacro mime-entity-set-content-type-internal (entity type)
255 `(aset ,entity 2 ,type))
256 (defmacro mime-entity-content-disposition-internal (entity)
258 (defmacro mime-entity-set-content-disposition-internal (entity disposition)
259 `(aset ,entity 3 ,disposition))
260 (defmacro mime-entity-encoding-internal (entity)
262 (defmacro mime-entity-set-encoding-internal (entity encoding)
263 `(aset ,entity 4 ,encoding))
265 (defmacro mime-entity-children-internal (entity)
267 (defmacro mime-entity-set-children-internal (entity children)
268 `(aset ,entity 5 ,children))
269 (defmacro mime-entity-parent-internal (entity)
271 (defmacro mime-entity-node-id-internal (entity)
274 (defmacro mime-entity-decoded-subject-internal (entity)
276 (defmacro mime-entity-set-decoded-subject-internal (entity subject)
277 `(aset ,entity 8 ,subject))
278 (defmacro mime-entity-decoded-from-internal (entity)
280 (defmacro mime-entity-set-decoded-from-internal (entity from)
281 `(aset ,entity 9 ,from))
282 (defmacro mime-entity-date-internal (entity)
284 (defmacro mime-entity-set-date-internal (entity date)
285 `(aset ,entity 10 ,date))
286 (defmacro mime-entity-message-id-internal (entity)
288 (defmacro mime-entity-set-message-id-internal (entity message-id)
289 `(aset ,entity 11 ,message-id))
290 (defmacro mime-entity-references-internal (entity)
292 (defmacro mime-entity-set-references-internal (entity references)
293 `(aset ,entity 12 ,references))
294 (defmacro mime-entity-chars-internal (entity)
296 (defmacro mime-entity-set-chars-internal (entity chars)
297 `(aset ,entity 13 ,chars))
298 (defmacro mime-entity-lines-internal (entity)
300 (defmacro mime-entity-set-lines-internal (entity lines)
301 `(aset ,entity 14 ,lines))
302 (defmacro mime-entity-xref-internal (entity)
304 (defmacro mime-entity-set-xref-internal (entity xref)
305 `(aset ,entity 15 ,xref))
307 (defmacro mime-entity-original-header-internal (entity)
309 (defmacro mime-entity-set-original-header-internal (entity header)
310 `(aset ,entity 16 ,header))
311 (defmacro mime-entity-parsed-header-internal (entity)
313 (defmacro mime-entity-set-parsed-header-internal (entity header)
314 `(aset ,entity 17 ,header))
316 (defmacro mime-entity-buffer-internal (entity)
318 (defmacro mime-entity-set-buffer-internal (entity buffer)
319 `(aset ,entity 18 ,buffer))
320 (defmacro mime-entity-header-start-internal (entity)
322 (defmacro mime-entity-set-header-start-internal (entity point)
323 `(aset ,entity 19 ,point))
324 (defmacro mime-entity-header-end-internal (entity)
326 (defmacro mime-entity-set-header-end-internal (entity point)
327 `(aset ,entity 20 ,point))
328 (defmacro mime-entity-body-start-internal (entity)
330 (defmacro mime-entity-set-body-start-internal (entity point)
331 `(aset ,entity 21 ,point))
332 (defmacro mime-entity-body-end-internal (entity)
334 (defmacro mime-entity-set-body-end-internal (entity point)
335 `(aset ,entity 22 ,point))
338 ;;; @ message structure
341 (defvar mime-message-structure nil
342 "Information about structure of message.
343 Please use reference function `mime-entity-SLOT' to get value of SLOT.
345 Following is a list of slots of the structure:
347 buffer buffer includes this entity (buffer).
348 node-id node-id (list of integers)
349 header-start minimum point of header in raw-buffer
350 header-end maximum point of header in raw-buffer
351 body-start minimum point of body in raw-buffer
352 body-end maximum point of body in raw-buffer
353 content-type content-type (content-type)
354 content-disposition content-disposition (content-disposition)
355 encoding Content-Transfer-Encoding (string or nil)
356 children entities included in this entity (list of entity)
358 If an entity includes other entities in its body, such as multipart or
359 message/rfc822, `mime-entity' structures of them are included in
360 `children', so the `mime-entity' structure become a tree.")
362 (make-variable-buffer-local 'mime-message-structure)
370 (defvar mime-entity-implementation-alist nil)
372 (defmacro mm-define-backend (type &optional parents)
373 "Define TYPE as a mm-backend.
374 If PARENTS is specified, TYPE inherits PARENTS.
375 Each parent must be backend name (symbol)."
377 `(let ((rest ',(reverse parents)))
379 (set-alist 'mime-entity-implementation-alist
382 (cdr (assq (car rest)
383 mime-entity-implementation-alist))))
384 (setq rest (cdr rest))
387 (defmacro mm-define-method (name args &rest body)
388 "Define NAME as a method function of (nth 1 (car ARGS)) backend.
390 ARGS is like an argument list of lambda, but (car ARGS) must be
391 specialized parameter. (car (car ARGS)) is name of variable and (nth
392 1 (car ARGS)) is name of backend."
393 (let* ((specializer (car args))
394 (class (nth 1 specializer))
395 (self (car specializer)))
396 `(let ((imps (cdr (assq ',class mime-entity-implementation-alist)))
397 (func (lambda ,(if self
398 (cons self (cdr args))
402 (set-alist 'mime-entity-implementation-alist
403 ',class (put-alist ',name func imps))
404 (set-alist 'mime-entity-implementation-alist
406 (list (cons ',name func)))
409 (put 'mm-define-method 'lisp-indent-function 'defun)
410 (put 'mm-define-method 'edebug-form-spec
411 '(&define name ((arg symbolp) &rest arg) def-body))
413 (defsubst mm-arglist-to-arguments (arglist)
416 (let ((arg (car arglist)))
417 (or (memq arg '(&optional &rest))
418 (setq dest (cons arg dest)))
420 (setq arglist (cdr arglist)))
424 ;;; @ for mel-backend
427 (defvar mel-service-list nil)
429 (defmacro mel-define-service (name &optional args &rest rest)
430 "Define NAME as a service for Content-Transfer-Encodings.
431 If ARGS is specified, NAME is defined as a generic function for the
434 (add-to-list 'mel-service-list ',name)
435 (defvar ,(intern (format "%s-obarray" name)) (make-vector 1 nil))
439 (funcall (mel-find-function ',name ,(car (last args)))
440 ,@(mm-arglist-to-arguments (butlast args)))
444 (put 'mel-define-service 'lisp-indent-function 'defun)
447 (defvar mel-encoding-module-alist nil)
449 (defsubst mel-find-function-from-obarray (ob-array encoding)
450 (let* ((f (intern-soft encoding ob-array)))
452 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
456 (null (setq f (intern-soft encoding ob-array)))
458 (setq rest (cdr rest))
462 (defsubst mel-copy-method (service src-backend dst-backend)
463 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
464 (f (mel-find-function-from-obarray oa src-backend))
467 (setq sym (intern dst-backend oa))
469 (fset sym (symbol-function f))
472 (defsubst mel-copy-backend (src-backend dst-backend)
473 (let ((services mel-service-list))
475 (mel-copy-method (car services) src-backend dst-backend)
476 (setq services (cdr services)))))
478 (defmacro mel-define-backend (type &optional parents)
479 "Define TYPE as a mel-backend.
480 If PARENTS is specified, TYPE inherits PARENTS.
481 Each parent must be backend name (string)."
483 (mapcar (lambda (parent)
484 `(mel-copy-backend ,parent ,type)
488 (defmacro mel-define-method (name args &rest body)
489 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
490 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
491 specialized parameter. (car (car (last ARGS))) is name of variable
492 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
493 (let* ((specializer (car (last args)))
494 (class (nth 1 specializer)))
496 (mel-define-service ,name)
497 (fset (intern ,class ,(intern (format "%s-obarray" name)))
498 (lambda ,(butlast args)
501 (put 'mel-define-method 'lisp-indent-function 'defun)
503 (defmacro mel-define-method-function (spec function)
504 "Set SPEC's function definition to FUNCTION.
505 First element of SPEC is service.
506 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
507 must be specialized parameter. (car (car (last ARGS))) is name of
508 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
509 (let* ((name (car spec))
511 (specializer (car (last args)))
512 (class (nth 1 specializer)))
514 (mel-define-service ,name)
515 (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
517 (fset sym (symbol-function ,function))))))
519 (defmacro mel-define-function (function spec)
520 (let* ((name (car spec))
522 (specializer (car (last args)))
523 (class (nth 1 specializer)))
525 (define-function ,function
526 (intern ,class ,(intern (format "%s-obarray" name))))
529 (defvar base64-dl-module
530 (and (fboundp 'dynamic-link)
531 (let ((path (expand-file-name "base64.so" exec-directory)))
532 (and (file-exists-p path)
541 ;;; mime-def.el ends here