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