Synch with `flim-1_14' (to be continued).
[elisp/flim.git] / mime-def.el
1 ;;; mime-def.el --- definition module about MIME -*- coding: iso-2022-jp; -*-
2
3 ;; Copyright (C) 1995,96,97,98,99,2000 Free Software Foundation, Inc.
4
5 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
6 ;; Keywords: definition, MIME, multimedia, mail, news
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
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.
14
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.
19
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.
24
25 ;;; Code:
26
27 (require 'poe)
28 (require 'poem)
29 (require 'pcustom)
30 (require 'mcharset)
31 (require 'alist)
32
33 (eval-when-compile
34   (require 'cl)   ; list*
35   (require 'luna) ; luna-arglist-to-arguments
36   )
37
38 (eval-and-compile
39   (defconst mime-library-product ["CLIME" (1 14 0) "\e$B8^4VF2\e(B"]
40     "Product name, version number and code name of MIME-library package."))
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 int-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   (cons (cons 'type type)
159         (cons (cons 'subtype subtype)
160               (nreverse parameters))))
161
162 (defsubst mime-content-type-primary-type (content-type)
163   "Return primary-type of CONTENT-TYPE."
164   (cdr (car content-type)))
165
166 (defsubst mime-content-type-subtype (content-type)
167   "Return primary-type of CONTENT-TYPE."
168   (cdr (cadr content-type)))
169
170 (defsubst mime-content-type-parameters (content-type)
171   "Return primary-type of CONTENT-TYPE."
172   (cddr content-type))
173
174 (defsubst mime-content-type-parameter (content-type parameter)
175   "Return PARAMETER value of CONTENT-TYPE."
176   (cdr (assoc parameter (mime-content-type-parameters content-type))))
177
178
179 (defsubst mime-type/subtype-string (type &optional subtype)
180   "Return type/subtype string from TYPE and SUBTYPE."
181   (if type
182       (if subtype
183           (format "%s/%s" type subtype)
184         (format "%s" type))))
185
186
187 ;;; @ Content-Disposition
188 ;;;
189
190 (defsubst mime-content-disposition-type (content-disposition)
191   "Return disposition-type of CONTENT-DISPOSITION."
192   (cdr (car content-disposition)))
193
194 (defsubst mime-content-disposition-parameters (content-disposition)
195   "Return disposition-parameters of CONTENT-DISPOSITION."
196   (cdr content-disposition))
197
198 (defsubst mime-content-disposition-parameter (content-disposition parameter)
199   "Return PARAMETER value of CONTENT-DISPOSITION."
200   (cdr (assoc parameter (cdr content-disposition))))
201
202 (defsubst mime-content-disposition-filename (content-disposition)
203   "Return filename of CONTENT-DISPOSITION."
204   (mime-content-disposition-parameter content-disposition "filename"))
205
206
207 ;;; @ message structure
208 ;;;
209
210 (defvar mime-message-structure nil
211   "Information about structure of message.
212 Please use reference function `mime-entity-SLOT' to get value of SLOT.
213
214 Following is a list of slots of the structure:
215
216 node-id                 node-id (list of integers)
217 content-type            content-type (content-type)
218 content-disposition     content-disposition (content-disposition)
219 encoding                Content-Transfer-Encoding (string or nil)
220 children                entities included in this entity (list of entity)
221
222 If an entity includes other entities in its body, such as multipart or
223 message/rfc822, `mime-entity' structures of them are included in
224 `children', so the `mime-entity' structure become a tree.")
225
226 (make-variable-buffer-local 'mime-message-structure)
227
228 (make-obsolete-variable 'mime-message-structure "should not use it.")
229
230
231 ;;; @ for mel-backend
232 ;;;
233
234 (defvar mel-service-list nil)
235
236 (defmacro mel-define-service (name &optional args &rest rest)
237   "Define NAME as a service for Content-Transfer-Encodings.
238 If ARGS is specified, NAME is defined as a generic function for the
239 service."
240   (` (progn
241        (add-to-list 'mel-service-list '(, name))
242        (defvar (, (intern (format "%s-obarray" name))) (make-vector 7 0))
243        (,@ (if args
244                (` ((defun (, name) (, args)
245                      (,@ rest)
246                      (funcall (mel-find-function '(, name)
247                                                  (, (car (last args))))
248                               (,@ (luna-arglist-to-arguments
249                                    (butlast args))))))))))))
250
251 (put 'mel-define-service 'lisp-indent-function 'defun)
252
253
254 (defvar mel-encoding-module-alist nil)
255
256 (defsubst mel-find-function-from-obarray (ob-array encoding)
257   (let* ((f (intern-soft encoding ob-array)))
258     (or f
259         (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
260           (while (and rest
261                       (progn
262                         (require (car rest))
263                         (null (setq f (intern-soft encoding ob-array)))))
264             (setq rest (cdr rest)))
265           f))))
266
267 (defsubst mel-copy-method (service src-backend dst-backend)
268   (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
269          (f (mel-find-function-from-obarray oa src-backend))
270          sym)
271     (when f
272       (setq sym (intern dst-backend oa))
273       (or (fboundp sym)
274           (fset sym (symbol-function f))))))
275
276 (defsubst mel-copy-backend (src-backend dst-backend)
277   (let ((services mel-service-list))
278     (while services
279       (mel-copy-method (car services) src-backend dst-backend)
280       (setq services (cdr services)))))
281
282 (defmacro mel-define-backend (type &optional parents)
283   "Define TYPE as a mel-backend.
284 If PARENTS is specified, TYPE inherits PARENTS.
285 Each parent must be backend name (string)."
286   (cons 'progn
287         (mapcar (function
288                  (lambda (parent)
289                    (` (mel-copy-backend (, parent) (, type)))))
290                 parents)))
291
292 (defmacro mel-define-method (name args &rest body)
293   "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
294 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
295 specialized parameter.  (car (car (last ARGS))) is name of variable
296 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
297   (let* ((specializer (car (last args)))
298          (class (nth 1 specializer)))
299     (` (progn
300          (mel-define-service (, name))
301          (fset (intern (, class) (, (intern (format "%s-obarray" name))))
302                (function
303                 (lambda (, (butlast args))
304                   (,@ body))))))))
305
306 (put 'mel-define-method 'lisp-indent-function 'defun)
307
308 (defmacro mel-define-method-function (spec function)
309   "Set SPEC's function definition to FUNCTION.
310 First element of SPEC is service.
311 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
312 must be specialized parameter.  (car (car (last ARGS))) is name of
313 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
314   (let* ((name (car spec))
315          (args (cdr spec))
316          (specializer (car (last args)))
317          (class (nth 1 specializer)))
318     (` (let (sym)
319          (mel-define-service (, name))
320          (setq sym (intern (, class) (, (intern (format "%s-obarray" name)))))
321          (or (fboundp sym)
322              (fset sym (symbol-function (, function))))))))
323
324 (defmacro mel-define-function (function spec)
325   (let* ((name (car spec))
326          (args (cdr spec))
327          (specializer (car (last args)))
328          (class (nth 1 specializer)))
329     (` (progn
330          (define-function (, function)
331            (intern (, class) (, (intern (format "%s-obarray" name)))))))))
332
333 (defvar base64-dl-module
334   (if (and (fboundp 'base64-encode-string)
335            (subrp (symbol-function 'base64-encode-string)))
336       nil
337     (if (fboundp 'dynamic-link)
338         (let ((path (expand-file-name "base64.so" exec-directory)))
339           (and (file-exists-p path)
340                path)))))
341
342
343 ;;; @ end
344 ;;;
345
346 (provide 'mime-def)
347
348 ;;; mime-def.el ends here