release.
[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-and-compile
36   (defconst mime-library-product ["CLIME" (1 13 6) "\e$BCf%N>1\e(B"]
37     "Product name, version number and code name of MIME-library package.")
38   )
39
40 (defmacro mime-product-name (product)
41   (` (aref (, product) 0)))
42
43 (defmacro mime-product-version (product)
44   (` (aref (, product) 1)))
45
46 (defmacro mime-product-code-name (product)
47   (` (aref (, product) 2)))
48
49 (defconst mime-library-version
50   (eval-when-compile
51     (concat (mime-product-name mime-library-product) " "
52             (mapconcat (function int-to-string)
53                        (mime-product-version mime-library-product) ".")
54             " - \"" (mime-product-code-name mime-library-product) "\"")))
55
56
57 ;;; @ variables
58 ;;;
59
60 (defgroup mime '((default-mime-charset custom-variable))
61   "Emacs MIME Interfaces"
62   :group 'news
63   :group 'mail)
64
65 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
66   "*List of encoding names for uuencode format."
67   :group 'mime
68   :type '(repeat string))
69
70
71 ;;; @ required functions
72 ;;;
73
74 (defsubst regexp-* (regexp)
75   (concat regexp "*"))
76
77 (defsubst regexp-or (&rest args)
78   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
79
80
81 ;;; @ about STD 11
82 ;;;
83
84 (eval-and-compile
85   (defconst std11-quoted-pair-regexp "\\\\.")
86   (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
87   (defconst std11-qtext-regexp
88     (eval-when-compile
89       (concat "[^" std11-non-qtext-char-list "]"))))
90 (defconst std11-quoted-string-regexp
91   (eval-when-compile
92     (concat "\""
93             (regexp-*
94              (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
95             "\"")))
96
97
98 ;;; @ about MIME
99 ;;;
100
101 (eval-and-compile
102   (defconst mime-tspecial-char-list
103     '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=)))
104 (defconst mime-token-regexp
105   (eval-when-compile
106     (concat "[^" mime-tspecial-char-list "\000-\040]+")))
107 (defconst mime-charset-regexp mime-token-regexp)
108
109 (defconst mime-media-type/subtype-regexp
110   (concat mime-token-regexp "/" mime-token-regexp))
111
112
113 ;;; @@ base64 / B
114 ;;;
115
116 (defconst base64-token-regexp "[A-Za-z0-9+/]")
117 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
118
119 (defconst B-encoded-text-regexp
120   (concat "\\(\\("
121           base64-token-regexp
122           base64-token-regexp
123           base64-token-regexp
124           base64-token-regexp
125           "\\)*"
126           base64-token-regexp
127           base64-token-regexp
128           base64-token-padding-regexp
129           base64-token-padding-regexp
130           "\\)"))
131
132 ;; (defconst eword-B-encoding-and-encoded-text-regexp
133 ;;   (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
134
135
136 ;;; @@ Quoted-Printable / Q
137 ;;;
138
139 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
140
141 (defconst quoted-printable-octet-regexp
142   (concat "=[" quoted-printable-hex-chars
143           "][" quoted-printable-hex-chars "]"))
144
145 (defconst Q-encoded-text-regexp
146   (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
147
148 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
149 ;;   (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
150
151
152 ;;; @ Content-Type
153 ;;;
154
155 (defsubst make-mime-content-type (type subtype &optional parameters)
156   (cons (cons 'type type)
157         (cons (cons 'subtype subtype)
158               (nreverse parameters))))
159
160 (defsubst mime-content-type-primary-type (content-type)
161   "Return primary-type of CONTENT-TYPE."
162   (cdr (car content-type)))
163
164 (defsubst mime-content-type-subtype (content-type)
165   "Return primary-type of CONTENT-TYPE."
166   (cdr (cadr content-type)))
167
168 (defsubst mime-content-type-parameters (content-type)
169   "Return primary-type of CONTENT-TYPE."
170   (cddr content-type))
171
172 (defsubst mime-content-type-parameter (content-type parameter)
173   "Return PARAMETER value of CONTENT-TYPE."
174   (cdr (assoc parameter (mime-content-type-parameters content-type))))
175
176
177 (defsubst mime-type/subtype-string (type &optional subtype)
178   "Return type/subtype string from TYPE and SUBTYPE."
179   (if type
180       (if subtype
181           (format "%s/%s" type subtype)
182         (format "%s" type))))
183
184
185 ;;; @ Content-Disposition
186 ;;;
187
188 (defsubst mime-content-disposition-type (content-disposition)
189   "Return disposition-type of CONTENT-DISPOSITION."
190   (cdr (car content-disposition)))
191
192 (defsubst mime-content-disposition-parameters (content-disposition)
193   "Return disposition-parameters of CONTENT-DISPOSITION."
194   (cdr content-disposition))
195
196 (defsubst mime-content-disposition-parameter (content-disposition parameter)
197   "Return PARAMETER value of CONTENT-DISPOSITION."
198   (cdr (assoc parameter (cdr content-disposition))))
199
200 (defsubst mime-content-disposition-filename (content-disposition)
201   "Return filename of CONTENT-DISPOSITION."
202   (mime-content-disposition-parameter content-disposition "filename"))
203
204
205 ;;; @ MIME entity
206 ;;;
207
208 (require 'luna)
209
210 (autoload 'mime-entity-content-type "mime")
211 (autoload 'mime-parse-multipart "mime-parse")
212 (autoload 'mime-parse-encapsulated "mime-parse")
213 (autoload 'mime-entity-content "mime")
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 (luna-define-method mime-entity-children ((entity mime-entity))
236   (let* ((content-type (mime-entity-content-type entity))
237          (primary-type (mime-content-type-primary-type content-type)))
238     (cond ((eq primary-type 'multipart)
239            (mime-parse-multipart entity)
240            )
241           ((and (eq primary-type 'message)
242                 (memq (mime-content-type-subtype content-type)
243                       '(rfc822 news external-body)
244                       ))
245            (mime-parse-encapsulated entity)
246            ))
247     ))
248
249 (luna-define-method mime-insert-text-content ((entity mime-entity))
250   (insert
251    (decode-mime-charset-string (mime-entity-content entity)
252                                (or (mime-content-type-parameter
253                                     (mime-entity-content-type entity)
254                                     "charset")
255                                    default-mime-charset)
256                                'CRLF)
257    ))
258
259
260 ;;; @ for mm-backend
261 ;;;
262
263 (defmacro mm-expand-class-name (type)
264   (` (intern (format "mime-%s-entity" (, type)))))
265
266 (defmacro mm-define-backend (type &optional parents)
267   (` (luna-define-class (, (mm-expand-class-name type))
268                         (, (nconc (mapcar (function
269                                            (lambda (parent)
270                                              (mm-expand-class-name parent)
271                                              ))
272                                           parents)
273                                   '(mime-entity))))))
274
275 (defmacro mm-define-method (name args &rest body)
276   (or (eq name 'initialize-instance)
277       (setq name (intern (format "mime-%s" name))))
278   (let ((spec (car args)))
279     (setq args
280           (cons (list (car spec)
281                       (mm-expand-class-name (nth 1 spec)))
282                 (cdr args)))
283     (` (luna-define-method (, name) (, args) (,@ body)))
284     ))
285
286 (put 'mm-define-method 'lisp-indent-function 'defun)
287
288 (def-edebug-spec mm-define-method
289   (&define name ((arg symbolp)
290                  [&rest arg]
291                  [&optional ["&optional" arg &rest arg]]
292                  &optional ["&rest" arg]
293                  )
294            def-body))
295
296
297 ;;; @ message structure
298 ;;;
299
300 (defvar mime-message-structure nil
301   "Information about structure of message.
302 Please use reference function `mime-entity-SLOT' to get value of SLOT.
303
304 Following is a list of slots of the structure:
305
306 node-id                 node-id (list of integers)
307 content-type            content-type (content-type)
308 content-disposition     content-disposition (content-disposition)
309 encoding                Content-Transfer-Encoding (string or nil)
310 children                entities included in this entity (list of entity)
311
312 If an entity includes other entities in its body, such as multipart or
313 message/rfc822, `mime-entity' structures of them are included in
314 `children', so the `mime-entity' structure become a tree.")
315
316 (make-variable-buffer-local 'mime-message-structure)
317
318 (make-obsolete-variable 'mime-message-structure "should not use it.")
319
320
321 ;;; @ for mel-backend
322 ;;;
323
324 (defvar mel-service-list nil)
325
326 (defmacro mel-define-service (name &optional args &rest rest)
327   "Define NAME as a service for Content-Transfer-Encodings.
328 If ARGS is specified, NAME is defined as a generic function for the
329 service."
330   (` (progn
331        (add-to-list 'mel-service-list '(, name))
332        (defvar (, (intern (format "%s-obarray" name))) (make-vector 7 0))
333        (,@ (if args
334                (` ((defun (, name) (, args)
335                      (,@ rest)
336                      (funcall (mel-find-function '(, name)
337                                                  (, (car (last args))))
338                               (,@ (luna-arglist-to-arguments (butlast args))))
339                      )))))
340        )))
341
342 (put 'mel-define-service 'lisp-indent-function 'defun)
343
344
345 (defvar mel-encoding-module-alist nil)
346
347 (defsubst mel-find-function-from-obarray (ob-array encoding)
348   (let* ((f (intern-soft encoding ob-array)))
349     (or f
350         (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
351           (while (and rest
352                       (progn
353                         (require (car rest))
354                         (null (setq f (intern-soft encoding ob-array)))
355                         ))
356             (setq rest (cdr rest))
357             )
358           f))))
359
360 (defsubst mel-copy-method (service src-backend dst-backend)
361   (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
362          (f (mel-find-function-from-obarray oa src-backend))
363          sym)
364     (when f
365       (setq sym (intern dst-backend oa))
366       (or (fboundp sym)
367           (fset sym (symbol-function f))
368           ))))
369        
370 (defsubst mel-copy-backend (src-backend dst-backend)
371   (let ((services mel-service-list))
372     (while services
373       (mel-copy-method (car services) src-backend dst-backend)
374       (setq services (cdr services)))))
375
376 (defmacro mel-define-backend (type &optional parents)
377   "Define TYPE as a mel-backend.
378 If PARENTS is specified, TYPE inherits PARENTS.
379 Each parent must be backend name (string)."
380   (cons 'progn
381         (mapcar (function
382                  (lambda (parent)
383                    (` (mel-copy-backend (, parent) (, type)))
384                    ))
385                 parents)))
386
387 (defmacro mel-define-method (name args &rest body)
388   "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
389 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
390 specialized parameter.  (car (car (last ARGS))) is name of variable
391 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
392   (let* ((specializer (car (last args)))
393          (class (nth 1 specializer)))
394     (` (progn
395          (mel-define-service (, name))
396          (fset (intern (, class) (, (intern (format "%s-obarray" name))))
397                (function
398                 (lambda (, (butlast args))
399                   (,@ body))))))))
400
401 (put 'mel-define-method 'lisp-indent-function 'defun)
402
403 (defmacro mel-define-method-function (spec function)
404   "Set SPEC's function definition to FUNCTION.
405 First element of SPEC is service.
406 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
407 must be specialized parameter.  (car (car (last ARGS))) is name of
408 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
409   (let* ((name (car spec))
410          (args (cdr spec))
411          (specializer (car (last args)))
412          (class (nth 1 specializer)))
413     (` (let (sym)
414          (mel-define-service (, name))
415          (setq sym (intern (, class) (, (intern (format "%s-obarray" name)))))
416          (or (fboundp sym)
417              (fset sym (symbol-function (, function))))))))
418
419 (defmacro mel-define-function (function spec)
420   (let* ((name (car spec))
421          (args (cdr spec))
422          (specializer (car (last args)))
423          (class (nth 1 specializer)))
424     (` (progn
425          (define-function (, function)
426            (intern (, class) (, (intern (format "%s-obarray" name)))))
427          ))))
428
429 (defvar base64-dl-module
430   (if (and (fboundp 'base64-encode-string)
431            (subrp (symbol-function 'base64-encode-string)))
432       nil
433     (if (fboundp 'dynamic-link)
434         (let ((path (expand-file-name "base64.so" exec-directory)))
435           (and (file-exists-p path)
436                path)
437           ))))
438
439
440 ;;; @ end
441 ;;;
442
443 (provide 'mime-def)
444
445 ;;; mime-def.el ends here