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