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