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