Sync with semi21-1_14_0-pre4-2.
[elisp/flim.git] / mime-def.el
1 ;;; mime-def.el --- definition module about MIME -*- coding: iso-8859-4; -*-
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 'custom)
28 (require 'mcharset)
29 (require 'alist)
30
31 (eval-when-compile
32   (require 'cl)   ; list*
33   (require 'luna) ; luna-arglist-to-arguments
34   )
35
36 (eval-and-compile
37   (defconst mime-library-product ["FLIM" (1 14 0) "Ninokuchi"]
38     "Product name, version number and code name of MIME-library package."))
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 #'number-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 (or (fboundp 'char-int)
81     (defalias 'char-int 'identity))
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 ;;; @ message structure
210 ;;;
211
212 (defvar mime-message-structure nil
213   "Information about structure of message.
214 Please use reference function `mime-entity-SLOT' to get value of SLOT.
215
216 Following is a list of slots of the structure:
217
218 node-id                 node-id (list of integers)
219 content-type            content-type (content-type)
220 content-disposition     content-disposition (content-disposition)
221 encoding                Content-Transfer-Encoding (string or nil)
222 children                entities included in this entity (list of entity)
223
224 If an entity includes other entities in its body, such as multipart or
225 message/rfc822, `mime-entity' structures of them are included in
226 `children', so the `mime-entity' structure become a tree.")
227
228 (make-variable-buffer-local 'mime-message-structure)
229
230 (make-obsolete-variable 'mime-message-structure "should not use it.")
231
232
233 ;;; @ for mel-backend
234 ;;;
235
236 (defvar mel-service-list nil)
237
238 (defmacro mel-define-service (name &optional args &rest rest)
239   "Define NAME as a service for Content-Transfer-Encodings.
240 If ARGS is specified, NAME is defined as a generic function for the
241 service."
242   `(progn
243      (add-to-list 'mel-service-list ',name)
244      (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
245      ,@(if args
246            `((defun ,name ,args
247                ,@rest
248                (funcall (mel-find-function ',name ,(car (last args)))
249                         ,@(luna-arglist-to-arguments (butlast args)))
250                )))
251      ))
252
253 (put 'mel-define-service 'lisp-indent-function 'defun)
254
255
256 (defvar mel-encoding-module-alist nil)
257
258 (defsubst mel-find-function-from-obarray (ob-array encoding)
259   (let* ((f (intern-soft encoding ob-array)))
260     (or f
261         (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
262           (while (and rest
263                       (progn
264                         (require (car rest))
265                         (null (setq f (intern-soft encoding ob-array)))
266                         ))
267             (setq rest (cdr rest))
268             )
269           f))))
270
271 (defsubst mel-copy-method (service src-backend dst-backend)
272   (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
273          (f (mel-find-function-from-obarray oa src-backend))
274          sym)
275     (when f
276       (setq sym (intern dst-backend oa))
277       (or (fboundp sym)
278           (fset sym (symbol-function f))
279           ))))
280        
281 (defsubst mel-copy-backend (src-backend dst-backend)
282   (let ((services mel-service-list))
283     (while services
284       (mel-copy-method (car services) src-backend dst-backend)
285       (setq services (cdr services)))))
286
287 (defmacro mel-define-backend (type &optional parents)
288   "Define TYPE as a mel-backend.
289 If PARENTS is specified, TYPE inherits PARENTS.
290 Each parent must be backend name (string)."
291   (cons 'progn
292         (mapcar (lambda (parent)
293                   `(mel-copy-backend ,parent ,type)
294                   )
295                 parents)))
296
297 (defmacro mel-define-method (name args &rest body)
298   "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
299 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
300 specialized parameter.  (car (car (last ARGS))) is name of variable
301 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
302   (let* ((specializer (car (last args)))
303          (class (nth 1 specializer)))
304     `(progn
305        (mel-define-service ,name)
306        (fset (intern ,class ,(intern (format "%s-obarray" name)))
307              (lambda ,(butlast args)
308                ,@body)))))
309
310 (put 'mel-define-method 'lisp-indent-function 'defun)
311
312 (defmacro mel-define-method-function (spec function)
313   "Set SPEC's function definition to FUNCTION.
314 First element of SPEC is service.
315 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
316 must be specialized parameter.  (car (car (last ARGS))) is name of
317 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
318   (let* ((name (car spec))
319          (args (cdr spec))
320          (specializer (car (last args)))
321          (class (nth 1 specializer)))
322     `(let (sym)
323        (mel-define-service ,name)
324        (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
325        (or (fboundp sym)
326            (fset sym (symbol-function ,function))))))
327
328 (defmacro mel-define-function (function spec)
329   (let* ((name (car spec))
330          (args (cdr spec))
331          (specializer (car (last args)))
332          (class (nth 1 specializer)))
333     `(progn
334        (define-function ,function
335          (intern ,class ,(intern (format "%s-obarray" name))))
336        )))
337
338 (defvar base64-dl-module
339   (if (and (fboundp 'base64-encode-string)
340            (subrp (symbol-function 'base64-encode-string)))
341       nil
342     (if (fboundp 'dynamic-link)
343         (let ((path (expand-file-name "base64.so" exec-directory)))
344           (and (file-exists-p path)
345                path)
346           ))))
347
348
349 ;;; @ end
350 ;;;
351
352 (provide 'mime-def)
353
354 ;;; mime-def.el ends here