685318ba81a4e972dbf19966ef506a882339c673
[elisp/flim.git] / mime-def.el
1 ;;; mime-def.el --- definition module about MIME
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4 ;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
5 ;; Licensed to the Free Software Foundation.
6
7 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
8 ;; Keywords: definition, MIME, multimedia, mail, news
9
10 ;; This file is part of FLIM (Faithful Library about Internet Message).
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Code:
28
29 (require 'poe)
30 (require 'poem)
31 (require 'pcustom)
32 (require 'mcharset)
33 (require 'alist)
34
35 (eval-when-compile (require 'cl))       ; list*
36
37 (eval-and-compile
38   (defconst mime-library-product ["Chao" (1 13 0) "JR Fujinomori"]
39     "Product name, version number and code name of MIME-library package.")
40   )
41
42 (defmacro mime-product-name (product)
43   `(aref ,product 0))
44
45 (defmacro mime-product-version (product)
46   `(aref ,product 1))
47
48 (defmacro mime-product-code-name (product)
49   `(aref ,product 2))
50
51 (defconst mime-library-version
52   (eval-when-compile
53     (concat (mime-product-name mime-library-product) " "
54             (mapconcat #'number-to-string
55                        (mime-product-version mime-library-product) ".")
56             " - \"" (mime-product-code-name mime-library-product) "\"")))
57
58
59 ;;; @ variables
60 ;;;
61
62 (require 'custom)
63
64 (defgroup mime '((default-mime-charset custom-variable))
65   "Emacs MIME Interfaces"
66   :group 'news
67   :group 'mail)
68
69 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
70   "*List of encoding names for uuencode format."
71   :group 'mime
72   :type '(repeat string))
73
74
75 ;;; @ required functions
76 ;;;
77
78 (defsubst regexp-* (regexp)
79   (concat regexp "*"))
80
81 (defsubst regexp-or (&rest args)
82   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
83
84
85 ;;; @ about STD 11
86 ;;;
87
88 (eval-and-compile
89   (defconst std11-quoted-pair-regexp "\\\\.")
90   (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
91   (defconst std11-qtext-regexp
92     (eval-when-compile
93       (concat "[^" std11-non-qtext-char-list "]"))))
94 (defconst std11-quoted-string-regexp
95   (eval-when-compile
96     (concat "\""
97             (regexp-*
98              (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
99             "\"")))
100
101
102 ;;; @ about MIME
103 ;;;
104
105 (eval-and-compile
106   (defconst mime-tspecial-char-list
107     '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=)))
108 (defconst mime-token-regexp
109   (eval-when-compile
110     (concat "[^" mime-tspecial-char-list "\000-\040]+")))
111 (defconst mime-charset-regexp mime-token-regexp)
112
113 (defconst mime-media-type/subtype-regexp
114   (concat mime-token-regexp "/" mime-token-regexp))
115
116
117 ;;; @@ base64 / B
118 ;;;
119
120 (defconst base64-token-regexp "[A-Za-z0-9+/]")
121 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
122
123 (defconst B-encoded-text-regexp
124   (concat "\\(\\("
125           base64-token-regexp
126           base64-token-regexp
127           base64-token-regexp
128           base64-token-regexp
129           "\\)*"
130           base64-token-regexp
131           base64-token-regexp
132           base64-token-padding-regexp
133           base64-token-padding-regexp
134           "\\)"))
135
136 ;; (defconst eword-B-encoding-and-encoded-text-regexp
137 ;;   (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
138
139
140 ;;; @@ Quoted-Printable / Q
141 ;;;
142
143 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
144
145 (defconst quoted-printable-octet-regexp
146   (concat "=[" quoted-printable-hex-chars
147           "][" quoted-printable-hex-chars "]"))
148
149 (defconst Q-encoded-text-regexp
150   (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
151
152 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
153 ;;   (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
154
155
156 ;;; @ Content-Type
157 ;;;
158
159 (defsubst make-mime-content-type (type subtype &optional parameters)
160   (list* (cons 'type type)
161          (cons 'subtype subtype)
162          (nreverse parameters))
163   )
164
165 (defsubst mime-content-type-primary-type (content-type)
166   "Return primary-type of CONTENT-TYPE."
167   (cdr (car content-type)))
168
169 (defsubst mime-content-type-subtype (content-type)
170   "Return primary-type of CONTENT-TYPE."
171   (cdr (cadr content-type)))
172
173 (defsubst mime-content-type-parameters (content-type)
174   "Return primary-type of CONTENT-TYPE."
175   (cddr content-type))
176
177 (defsubst mime-content-type-parameter (content-type parameter)
178   "Return PARAMETER value of CONTENT-TYPE."
179   (cdr (assoc parameter (mime-content-type-parameters content-type))))
180
181
182 (defsubst mime-type/subtype-string (type &optional subtype)
183   "Return type/subtype string from TYPE and SUBTYPE."
184   (if type
185       (if subtype
186           (format "%s/%s" type subtype)
187         (format "%s" type))))
188
189
190 ;;; @ Content-Disposition
191 ;;;
192
193 (defsubst mime-content-disposition-type (content-disposition)
194   "Return disposition-type of CONTENT-DISPOSITION."
195   (cdr (car content-disposition)))
196
197 (defsubst mime-content-disposition-parameters (content-disposition)
198   "Return disposition-parameters of CONTENT-DISPOSITION."
199   (cdr content-disposition))
200
201 (defsubst mime-content-disposition-parameter (content-disposition parameter)
202   "Return PARAMETER value of CONTENT-DISPOSITION."
203   (cdr (assoc parameter (cdr content-disposition))))
204
205 (defsubst mime-content-disposition-filename (content-disposition)
206   "Return filename of CONTENT-DISPOSITION."
207   (mime-content-disposition-parameter content-disposition "filename"))
208
209
210 ;;; @ MIME entity
211 ;;;
212
213 (require 'luna)
214
215 (luna-define-class mime-entity ()
216                    (location
217                     content-type children parent
218                     node-id
219                     content-disposition encoding
220                     ;; for other fields
221                     original-header parsed-header))
222
223 (defalias 'mime-entity-representation-type-internal 'luna-class-name)
224 (defalias 'mime-entity-set-representation-type-internal 'luna-set-class-name)
225
226 (luna-define-internal-accessors 'mime-entity)
227
228 (luna-define-method mime-entity-fetch-field ((entity mime-entity)
229                                              field-name)
230   (or (symbolp field-name)
231       (setq field-name (intern (capitalize (capitalize field-name)))))
232   (cdr (assq field-name
233              (mime-entity-original-header-internal entity))))
234
235
236 ;;; @ for mm-backend
237 ;;;
238
239 (defmacro mm-expand-class-name (type)
240   `(intern (format "mime-%s-entity" ,type)))
241
242 (defmacro mm-define-backend (type &optional parents)
243   `(luna-define-class ,(mm-expand-class-name type)
244                       ,(nconc (mapcar (lambda (parent)
245                                         (mm-expand-class-name parent)
246                                         )
247                                       parents)
248                               '(mime-entity))))
249
250 (defmacro mm-define-method (name args &rest body)
251   (or (eq name 'initialize-instance)
252       (setq name (intern (format "mime-%s" name))))
253   (let ((spec (car args)))
254     (setq args
255           (cons (list (car spec)
256                       (mm-expand-class-name (nth 1 spec)))
257                 (cdr args)))
258     `(luna-define-method ,name ,args ,@body)
259     ))
260
261 (put 'mm-define-method 'lisp-indent-function 'defun)
262
263 (def-edebug-spec mm-define-method
264   (&define name ((arg symbolp)
265                  [&rest arg]
266                  [&optional ["&optional" arg &rest arg]]
267                  &optional ["&rest" arg]
268                  )
269            def-body))
270
271
272 ;;; @ message structure
273 ;;;
274
275 (defvar mime-message-structure nil
276   "Information about structure of message.
277 Please use reference function `mime-entity-SLOT' to get value of SLOT.
278
279 Following is a list of slots of the structure:
280
281 node-id                 node-id (list of integers)
282 content-type            content-type (content-type)
283 content-disposition     content-disposition (content-disposition)
284 encoding                Content-Transfer-Encoding (string or nil)
285 children                entities included in this entity (list of entity)
286
287 If an entity includes other entities in its body, such as multipart or
288 message/rfc822, `mime-entity' structures of them are included in
289 `children', so the `mime-entity' structure become a tree.")
290
291 (make-variable-buffer-local 'mime-message-structure)
292
293
294 ;;; @ for mm-backend
295 ;;;
296
297 (defvar mime-entity-implementation-alist nil)
298
299 (defmacro mm-define-backend (type &optional parents)
300   "Define TYPE as a mm-backend.
301 If PARENTS is specified, TYPE inherits PARENTS.
302 Each parent must be backend name (symbol)."
303   (if parents
304       `(let ((rest ',(reverse parents)))
305          (while rest
306            (set-alist 'mime-entity-implementation-alist
307                       ',type
308                       (copy-alist
309                        (cdr (assq (car rest)
310                                   mime-entity-implementation-alist))))
311            (setq rest (cdr rest))
312            ))))
313
314 (defmacro mm-define-method (name args &rest body)
315   "Define NAME as a method function of (nth 1 (car ARGS)) backend.
316
317 ARGS is like an argument list of lambda, but (car ARGS) must be
318 specialized parameter.  (car (car ARGS)) is name of variable and (nth
319 1 (car ARGS)) is name of backend."
320   (let* ((specializer (car args))
321          (class (nth 1 specializer))
322          (self (car specializer)))
323     `(let ((imps (cdr (assq ',class mime-entity-implementation-alist)))
324            (func (lambda ,(if self
325                               (cons self (cdr args))
326                             (cdr args))
327                    ,@body)))
328        (if imps
329            (set-alist 'mime-entity-implementation-alist
330                       ',class (put-alist ',name func imps))
331          (set-alist 'mime-entity-implementation-alist
332                     ',class
333                     (list (cons ',name func)))
334          ))))
335
336 (put 'mm-define-method 'lisp-indent-function 'defun)
337
338 (eval-when-compile
339   (defmacro eval-module-depended-macro (module definition)
340     (condition-case nil
341         (progn
342           (require (eval module))
343           definition)
344       (error `(eval-after-load ,(symbol-name (eval module)) ',definition))
345       ))
346   )
347
348 (make-obsolete-variable 'mime-message-structure "should not use it.")
349
350
351 ;;; @ for mel-backend
352 ;;;
353
354 (defvar mel-service-list nil)
355
356 (defmacro mel-define-service (name &optional args &rest rest)
357   "Define NAME as a service for Content-Transfer-Encodings.
358 If ARGS is specified, NAME is defined as a generic function for the
359 service."
360   `(progn
361      (add-to-list 'mel-service-list ',name)
362      (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
363      ,@(if args
364            `((defun ,name ,args
365                ,@rest
366                (funcall (mel-find-function ',name ,(car (last args)))
367                         ,@(luna-arglist-to-arguments (butlast args)))
368                )))
369      ))
370
371 (put 'mel-define-service 'lisp-indent-function 'defun)
372
373
374 (defvar mel-encoding-module-alist nil)
375
376 (defsubst mel-find-function-from-obarray (ob-array encoding)
377   (let* ((f (intern-soft encoding ob-array)))
378     (or f
379         (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
380           (while (and rest
381                       (progn
382                         (require (car rest))
383                         (null (setq f (intern-soft encoding ob-array)))
384                         ))
385             (setq rest (cdr rest))
386             )
387           f))))
388
389 (defsubst mel-copy-method (service src-backend dst-backend)
390   (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
391          (f (mel-find-function-from-obarray oa src-backend))
392          sym)
393     (when f
394       (setq sym (intern dst-backend oa))
395       (or (fboundp sym)
396           (fset sym (symbol-function f))
397           ))))
398        
399 (defsubst mel-copy-backend (src-backend dst-backend)
400   (let ((services mel-service-list))
401     (while services
402       (mel-copy-method (car services) src-backend dst-backend)
403       (setq services (cdr services)))))
404
405 (defmacro mel-define-backend (type &optional parents)
406   "Define TYPE as a mel-backend.
407 If PARENTS is specified, TYPE inherits PARENTS.
408 Each parent must be backend name (string)."
409   (cons 'progn
410         (mapcar (lambda (parent)
411                   `(mel-copy-backend ,parent ,type)
412                   )
413                 parents)))
414
415 (defmacro mel-define-method (name args &rest body)
416   "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
417 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
418 specialized parameter.  (car (car (last ARGS))) is name of variable
419 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
420   (let* ((specializer (car (last args)))
421          (class (nth 1 specializer)))
422     `(progn
423        (mel-define-service ,name)
424        (fset (intern ,class ,(intern (format "%s-obarray" name)))
425              (lambda ,(butlast args)
426                ,@body)))))
427
428 (put 'mel-define-method 'lisp-indent-function 'defun)
429
430 (defmacro mel-define-method-function (spec function)
431   "Set SPEC's function definition to FUNCTION.
432 First element of SPEC is service.
433 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
434 must be specialized parameter.  (car (car (last ARGS))) is name of
435 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
436   (let* ((name (car spec))
437          (args (cdr spec))
438          (specializer (car (last args)))
439          (class (nth 1 specializer)))
440     `(let (sym)
441        (mel-define-service ,name)
442        (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
443        (or (fboundp sym)
444            (fset sym (symbol-function ,function))))))
445
446 (defmacro mel-define-function (function spec)
447   (let* ((name (car spec))
448          (args (cdr spec))
449          (specializer (car (last args)))
450          (class (nth 1 specializer)))
451     `(progn
452        (define-function ,function
453          (intern ,class ,(intern (format "%s-obarray" name))))
454        )))
455
456 (defvar base64-dl-module
457   (if (and (fboundp 'base64-encode-string)
458            (subrp (symbol-function 'base64-encode-string)))
459       nil
460     (if (fboundp 'dynamic-link)
461         (let ((path (expand-file-name "base64.so" exec-directory)))
462           (and (file-exists-p path)
463                path)
464           ))))
465
466
467 ;;; @ end
468 ;;;
469
470 (provide 'mime-def)
471
472 ;;; mime-def.el ends here