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 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 11 3) "Saidaiji"]
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 eliminate-top-spaces (string)
76 "Eliminate top sequence of space or tab in STRING."
77 (if (string-match "^[ \t]+" string)
78 (substring string (match-end 0))
81 (defsubst regexp-* (regexp)
84 (defsubst regexp-or (&rest args)
85 (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
92 (defconst std11-quoted-pair-regexp "\\\\.")
93 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
94 (defconst std11-qtext-regexp
96 (concat "[^" (apply #'string std11-non-qtext-char-list) "]"))))
97 (defconst std11-quoted-string-regexp
101 (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
108 (defconst mime-tspecials "][()<>@,\;:\\\"/?=")
109 (defconst mime-token-regexp (concat "[^" mime-tspecials "\000-\040]+"))
110 (defconst mime-charset-regexp mime-token-regexp)
112 (defconst mime-media-type/subtype-regexp
113 (concat mime-token-regexp "/" mime-token-regexp))
119 (defconst base64-token-regexp "[A-Za-z0-9+/]")
120 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
122 (defconst B-encoded-text-regexp
131 base64-token-padding-regexp
132 base64-token-padding-regexp
135 ;; (defconst eword-B-encoding-and-encoded-text-regexp
136 ;; (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
139 ;;; @@ Quoted-Printable / Q
142 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
144 (defconst quoted-printable-octet-regexp
145 (concat "=[" quoted-printable-hex-chars
146 "][" quoted-printable-hex-chars "]"))
148 (defconst Q-encoded-text-regexp
149 (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
151 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
152 ;; (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
158 (defsubst make-mime-content-type (type subtype &optional parameters)
159 (list* (cons 'type type)
160 (cons 'subtype subtype)
161 (nreverse parameters))
164 (defsubst mime-content-type-primary-type (content-type)
165 "Return primary-type of CONTENT-TYPE."
166 (cdr (car content-type)))
168 (defsubst mime-content-type-subtype (content-type)
169 "Return primary-type of CONTENT-TYPE."
170 (cdr (cadr content-type)))
172 (defsubst mime-content-type-parameters (content-type)
173 "Return primary-type of CONTENT-TYPE."
176 (defsubst mime-content-type-parameter (content-type parameter)
177 "Return PARAMETER value of CONTENT-TYPE."
178 (cdr (assoc parameter (mime-content-type-parameters content-type))))
181 (defsubst mime-type/subtype-string (type &optional subtype)
182 "Return type/subtype string from TYPE and SUBTYPE."
185 (format "%s/%s" type subtype)
186 (format "%s" type))))
189 ;;; @ Content-Disposition
192 (defsubst mime-content-disposition-type (content-disposition)
193 "Return disposition-type of CONTENT-DISPOSITION."
194 (cdr (car content-disposition)))
196 (defsubst mime-content-disposition-parameters (content-disposition)
197 "Return disposition-parameters of CONTENT-DISPOSITION."
198 (cdr content-disposition))
200 (defsubst mime-content-disposition-parameter (content-disposition parameter)
201 "Return PARAMETER value of CONTENT-DISPOSITION."
202 (cdr (assoc parameter (cdr content-disposition))))
204 (defsubst mime-content-disposition-filename (content-disposition)
205 "Return filename of CONTENT-DISPOSITION."
206 (mime-content-disposition-parameter content-disposition "filename"))
212 (defmacro make-mime-entity-internal (representation-type location
213 &optional content-type
214 children parent node-id
216 decoded-subject decoded-from
217 date message-id references
221 original-header parsed-header
222 ;; for buffer representation
224 header-start header-end
226 `(vector ,representation-type ,location
227 ,content-type nil nil ,children ,parent ,node-id
229 ,decoded-subject ,decoded-from
230 ,date ,message-id ,references
234 ,original-header ,parsed-header
235 ;; for buffer representation
236 ,buffer ,header-start ,header-end ,body-start ,body-end))
238 (defmacro mime-entity-representation-type-internal (entity)
240 (defmacro mime-entity-set-representation-type-internal (entity type)
241 `(aset ,entity 0 ,type))
242 (defmacro mime-entity-location-internal (entity)
244 (defmacro mime-entity-set-location-internal (entity location)
245 `(aset ,entity 1 ,location))
247 (defmacro mime-entity-content-type-internal (entity)
249 (defmacro mime-entity-set-content-type-internal (entity type)
250 `(aset ,entity 2 ,type))
251 (defmacro mime-entity-content-disposition-internal (entity)
253 (defmacro mime-entity-set-content-disposition-internal (entity disposition)
254 `(aset ,entity 3 ,disposition))
255 (defmacro mime-entity-encoding-internal (entity)
257 (defmacro mime-entity-set-encoding-internal (entity encoding)
258 `(aset ,entity 4 ,encoding))
260 (defmacro mime-entity-children-internal (entity)
262 (defmacro mime-entity-set-children-internal (entity children)
263 `(aset ,entity 5 ,children))
264 (defmacro mime-entity-parent-internal (entity)
266 (defmacro mime-entity-node-id-internal (entity)
269 (defmacro mime-entity-decoded-subject-internal (entity)
271 (defmacro mime-entity-set-decoded-subject-internal (entity subject)
272 `(aset ,entity 8 ,subject))
273 (defmacro mime-entity-decoded-from-internal (entity)
275 (defmacro mime-entity-set-decoded-from-internal (entity from)
276 `(aset ,entity 9 ,from))
277 (defmacro mime-entity-date-internal (entity)
279 (defmacro mime-entity-set-date-internal (entity date)
280 `(aset ,entity 10 ,date))
281 (defmacro mime-entity-message-id-internal (entity)
283 (defmacro mime-entity-set-message-id-internal (entity message-id)
284 `(aset ,entity 11 ,message-id))
285 (defmacro mime-entity-references-internal (entity)
287 (defmacro mime-entity-set-references-internal (entity references)
288 `(aset ,entity 12 ,references))
289 (defmacro mime-entity-chars-internal (entity)
291 (defmacro mime-entity-set-chars-internal (entity chars)
292 `(aset ,entity 13 ,chars))
293 (defmacro mime-entity-lines-internal (entity)
295 (defmacro mime-entity-set-lines-internal (entity lines)
296 `(aset ,entity 14 ,lines))
297 (defmacro mime-entity-xref-internal (entity)
299 (defmacro mime-entity-set-xref-internal (entity xref)
300 `(aset ,entity 15 ,xref))
302 (defmacro mime-entity-original-header-internal (entity)
304 (defmacro mime-entity-set-original-header-internal (entity header)
305 `(aset ,entity 16 ,header))
306 (defmacro mime-entity-parsed-header-internal (entity)
308 (defmacro mime-entity-set-parsed-header-internal (entity header)
309 `(aset ,entity 17 ,header))
311 (defmacro mime-entity-buffer-internal (entity)
313 (defmacro mime-entity-set-buffer-internal (entity buffer)
314 `(aset ,entity 18 ,buffer))
315 (defmacro mime-entity-header-start-internal (entity)
317 (defmacro mime-entity-set-header-start-internal (entity point)
318 `(aset ,entity 19 ,point))
319 (defmacro mime-entity-header-end-internal (entity)
321 (defmacro mime-entity-set-header-end-internal (entity point)
322 `(aset ,entity 20 ,point))
323 (defmacro mime-entity-body-start-internal (entity)
325 (defmacro mime-entity-set-body-start-internal (entity point)
326 `(aset ,entity 21 ,point))
327 (defmacro mime-entity-body-end-internal (entity)
329 (defmacro mime-entity-set-body-end-internal (entity point)
330 `(aset ,entity 22 ,point))
333 ;;; @ message structure
336 (defvar mime-message-structure nil
337 "Information about structure of message.
338 Please use reference function `mime-entity-SLOT' to get value of SLOT.
340 Following is a list of slots of the structure:
342 buffer buffer includes this entity (buffer).
343 node-id node-id (list of integers)
344 header-start minimum point of header in raw-buffer
345 header-end maximum point of header in raw-buffer
346 body-start minimum point of body in raw-buffer
347 body-end maximum point of body in raw-buffer
348 content-type content-type (content-type)
349 content-disposition content-disposition (content-disposition)
350 encoding Content-Transfer-Encoding (string or nil)
351 children entities included in this entity (list of entity)
353 If an entity includes other entities in its body, such as multipart or
354 message/rfc822, `mime-entity' structures of them are included in
355 `children', so the `mime-entity' structure become a tree.")
357 (make-variable-buffer-local 'mime-message-structure)
365 (defvar mime-entity-implementation-alist nil)
367 (defmacro mm-define-backend (type &optional parents)
368 "Define TYPE as a mm-backend.
369 If PARENTS is specified, TYPE inherits PARENTS.
370 Each parent must be backend name (symbol)."
372 `(let ((rest ',(reverse parents)))
374 (set-alist 'mime-entity-implementation-alist
377 (cdr (assq (car rest)
378 mime-entity-implementation-alist))))
379 (setq rest (cdr rest))
382 (defmacro mm-define-method (name args &rest body)
383 "Define NAME as a method function of (nth 1 (car ARGS)) backend.
385 ARGS is like an argument list of lambda, but (car ARGS) must be
386 specialized parameter. (car (car ARGS)) is name of variable and (nth
387 1 (car ARGS)) is name of backend."
388 (let* ((specializer (car args))
389 (class (nth 1 specializer))
390 (self (car specializer)))
391 `(let ((imps (cdr (assq ',class mime-entity-implementation-alist)))
392 (func (lambda ,(if self
393 (cons self (cdr args))
397 (set-alist 'mime-entity-implementation-alist
398 ',class (put-alist ',name func imps))
399 (set-alist 'mime-entity-implementation-alist
401 (list (cons ',name func)))
404 (put 'mm-define-method 'lisp-indent-function 'defun)
407 (defmacro eval-module-depended-macro (module definition)
410 (require (eval module))
412 (error `(eval-after-load ,(symbol-name (eval module)) ',definition))
416 (eval-module-depended-macro
418 (def-edebug-spec mm-define-method
419 (&define name ((arg symbolp)
421 [&optional ["&optional" arg &rest arg]]
422 &optional ["&rest" arg]
427 (defsubst mm-arglist-to-arguments (arglist)
430 (let ((arg (car arglist)))
431 (or (memq arg '(&optional &rest))
432 (setq dest (cons arg dest)))
434 (setq arglist (cdr arglist)))
438 ;;; @ for mel-backend
441 (defvar mel-service-list nil)
443 (defmacro mel-define-service (name &optional args &rest rest)
444 "Define NAME as a service for Content-Transfer-Encodings.
445 If ARGS is specified, NAME is defined as a generic function for the
448 (add-to-list 'mel-service-list ',name)
449 (defvar ,(intern (format "%s-obarray" name)) (make-vector 1 nil))
453 (funcall (mel-find-function ',name ,(car (last args)))
454 ,@(mm-arglist-to-arguments (butlast args)))
458 (put 'mel-define-service 'lisp-indent-function 'defun)
461 (defvar mel-encoding-module-alist nil)
463 (defsubst mel-find-function-from-obarray (ob-array encoding)
464 (let* ((f (intern-soft encoding ob-array)))
466 (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
470 (null (setq f (intern-soft encoding ob-array)))
472 (setq rest (cdr rest))
476 (defsubst mel-copy-method (service src-backend dst-backend)
477 (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
478 (f (mel-find-function-from-obarray oa src-backend))
481 (setq sym (intern dst-backend oa))
483 (fset sym (symbol-function f))
486 (defsubst mel-copy-backend (src-backend dst-backend)
487 (let ((services mel-service-list))
489 (mel-copy-method (car services) src-backend dst-backend)
490 (setq services (cdr services)))))
492 (defmacro mel-define-backend (type &optional parents)
493 "Define TYPE as a mel-backend.
494 If PARENTS is specified, TYPE inherits PARENTS.
495 Each parent must be backend name (string)."
497 (mapcar (lambda (parent)
498 `(mel-copy-backend ,parent ,type)
502 (defmacro mel-define-method (name args &rest body)
503 "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
504 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
505 specialized parameter. (car (car (last ARGS))) is name of variable
506 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
507 (let* ((specializer (car (last args)))
508 (class (nth 1 specializer)))
510 (mel-define-service ,name)
511 (fset (intern ,class ,(intern (format "%s-obarray" name)))
512 (lambda ,(butlast args)
515 (put 'mel-define-method 'lisp-indent-function 'defun)
517 (defmacro mel-define-method-function (spec function)
518 "Set SPEC's function definition to FUNCTION.
519 First element of SPEC is service.
520 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
521 must be specialized parameter. (car (car (last ARGS))) is name of
522 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
523 (let* ((name (car spec))
525 (specializer (car (last args)))
526 (class (nth 1 specializer)))
528 (mel-define-service ,name)
529 (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
531 (fset sym (symbol-function ,function))))))
533 (defmacro mel-define-function (function spec)
534 (let* ((name (car spec))
536 (specializer (car (last args)))
537 (class (nth 1 specializer)))
539 (define-function ,function
540 (intern ,class ,(intern (format "%s-obarray" name))))
543 (defvar base64-dl-module
544 (if (and (fboundp 'base64-encode-string)
545 (subrp (symbol-function 'base64-encode-string)))
547 (if (fboundp 'dynamic-link)
548 (let ((path (expand-file-name "base64.so" exec-directory)))
549 (and (file-exists-p path)
559 ;;; mime-def.el ends here