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