(mime-entity-fetch-field): New method of luna-class `mime-entity'.
[elisp/flim.git] / mime-def.el
1 ;;; mime-def.el --- definition module about MIME
2
3 ;; Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
4 ;; Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
5 ;; Licensed to the Free Software Foundation.
6
7 ;; Author: MORIOKA Tomohiko <tomo@m17n.org>
8 ;; Keywords: definition, MIME, multimedia, mail, news
9
10 ;; This file is part of FLIM (Faithful Library about Internet Message).
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Code:
28
29 (require 'mcharset)
30
31 (eval-and-compile
32   (defconst mime-library-product ["Chao" (1 13 0) "JR Fujinomori"]
33     "Product name, version number and code name of MIME-library package.")
34   )
35
36 (defmacro mime-product-name (product)
37   `(aref ,product 0))
38
39 (defmacro mime-product-version (product)
40   `(aref ,product 1))
41
42 (defmacro mime-product-code-name (product)
43   `(aref ,product 2))
44
45 (defconst mime-library-version
46   (eval-when-compile
47     (concat (mime-product-name mime-library-product) " "
48             (mapconcat #'number-to-string
49                        (mime-product-version mime-library-product) ".")
50             " - \"" (mime-product-code-name mime-library-product) "\"")))
51
52
53 ;;; @ variables
54 ;;;
55
56 (require 'custom)
57
58 (eval-when-compile (require 'cl))
59
60 (defgroup mime nil
61   "Emacs MIME Interfaces"
62   :group 'news
63   :group 'mail)
64
65 (custom-handle-keyword 'default-mime-charset :group 'mime
66                        'custom-variable)
67
68 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
69   "*List of encoding names for uuencode format."
70   :group 'mime
71   :type '(repeat string))
72
73
74 ;;; @ required functions
75 ;;;
76
77 (defsubst regexp-* (regexp)
78   (concat regexp "*"))
79
80 (defsubst regexp-or (&rest args)
81   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
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 ;;; @ MIME entity
210 ;;;
211
212 (require 'luna)
213
214 (luna-define-class mime-entity ()
215                    (location
216                     content-type children parent
217                     node-id
218                     content-disposition encoding
219                     ;; for other fields
220                     original-header parsed-header))
221
222 (defalias 'mime-entity-representation-type-internal 'luna-class-name)
223 (defalias 'mime-entity-set-representation-type-internal 'luna-set-class-name)
224
225 (luna-define-internal-accessors 'mime-entity)
226
227 (luna-define-method mime-entity-fetch-field ((entity mime-entity)
228                                              field-name)
229   (or (symbolp field-name)
230       (setq field-name (intern (capitalize (capitalize field-name)))))
231   (cdr (assq field-name
232              (mime-entity-original-header-internal entity))))
233
234
235 ;;; @ for mm-backend
236 ;;;
237
238 (defmacro mm-expand-class-name (type)
239   `(intern (format "mime-%s-entity" ,type)))
240
241 (defmacro mm-define-backend (type &optional parents)
242   `(luna-define-class ,(mm-expand-class-name type)
243                       ,(nconc (mapcar (lambda (parent)
244                                         (mm-expand-class-name parent)
245                                         )
246                                       parents)
247                               '(mime-entity))))
248
249 (defmacro mm-define-method (name args &rest body)
250   (or (eq name 'initialize-instance)
251       (setq name (intern (format "mime-%s" name))))
252   (let ((spec (car args)))
253     (setq args
254           (cons (list (car spec)
255                       (mm-expand-class-name (nth 1 spec)))
256                 (cdr args)))
257     `(luna-define-method ,name ,args ,@body)
258     ))
259
260 (put 'mm-define-method 'lisp-indent-function 'defun)
261
262 (def-edebug-spec mm-define-method
263   (&define name ((arg symbolp)
264                  [&rest arg]
265                  [&optional ["&optional" arg &rest arg]]
266                  &optional ["&rest" arg]
267                  )
268            def-body))
269
270
271 ;;; @ message structure
272 ;;;
273
274 (defvar mime-message-structure nil
275   "Information about structure of message.
276 Please use reference function `mime-entity-SLOT' to get value of SLOT.
277
278 Following is a list of slots of the structure:
279
280 node-id                 node-id (list of integers)
281 content-type            content-type (content-type)
282 content-disposition     content-disposition (content-disposition)
283 encoding                Content-Transfer-Encoding (string or nil)
284 children                entities included in this entity (list of entity)
285
286 If an entity includes other entities in its body, such as multipart or
287 message/rfc822, `mime-entity' structures of them are included in
288 `children', so the `mime-entity' structure become a tree.")
289
290 (make-variable-buffer-local 'mime-message-structure)
291
292 (make-obsolete-variable 'mime-message-structure "should not use it.")
293
294
295 ;;; @ for mel-backend
296 ;;;
297
298 (defvar mel-service-list nil)
299
300 (defmacro mel-define-service (name &optional args &rest rest)
301   "Define NAME as a service for Content-Transfer-Encodings.
302 If ARGS is specified, NAME is defined as a generic function for the
303 service."
304   `(progn
305      (add-to-list 'mel-service-list ',name)
306      (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
307      ,@(if args
308            `((defun ,name ,args
309                ,@rest
310                (funcall (mel-find-function ',name ,(car (last args)))
311                         ,@(luna-arglist-to-arguments (butlast args)))
312                )))
313      ))
314
315 (put 'mel-define-service 'lisp-indent-function 'defun)
316
317
318 (defvar mel-encoding-module-alist nil)
319
320 (defsubst mel-find-function-from-obarray (ob-array encoding)
321   (let* ((f (intern-soft encoding ob-array)))
322     (or f
323         (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
324           (while (and rest
325                       (progn
326                         (require (car rest))
327                         (null (setq f (intern-soft encoding ob-array)))
328                         ))
329             (setq rest (cdr rest))
330             )
331           f))))
332
333 (defsubst mel-copy-method (service src-backend dst-backend)
334   (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
335          (f (mel-find-function-from-obarray oa src-backend))
336          sym)
337     (when f
338       (setq sym (intern dst-backend oa))
339       (or (fboundp sym)
340           (fset sym (symbol-function f))
341           ))))
342        
343 (defsubst mel-copy-backend (src-backend dst-backend)
344   (let ((services mel-service-list))
345     (while services
346       (mel-copy-method (car services) src-backend dst-backend)
347       (setq services (cdr services)))))
348
349 (defmacro mel-define-backend (type &optional parents)
350   "Define TYPE as a mel-backend.
351 If PARENTS is specified, TYPE inherits PARENTS.
352 Each parent must be backend name (string)."
353   (cons 'progn
354         (mapcar (lambda (parent)
355                   `(mel-copy-backend ,parent ,type)
356                   )
357                 parents)))
358
359 (defmacro mel-define-method (name args &rest body)
360   "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
361 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
362 specialized parameter.  (car (car (last ARGS))) is name of variable
363 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
364   (let* ((specializer (car (last args)))
365          (class (nth 1 specializer)))
366     `(progn
367        (mel-define-service ,name)
368        (fset (intern ,class ,(intern (format "%s-obarray" name)))
369              (lambda ,(butlast args)
370                ,@body)))))
371
372 (put 'mel-define-method 'lisp-indent-function 'defun)
373
374 (defmacro mel-define-method-function (spec function)
375   "Set SPEC's function definition to FUNCTION.
376 First element of SPEC is service.
377 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
378 must be specialized parameter.  (car (car (last ARGS))) is name of
379 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
380   (let* ((name (car spec))
381          (args (cdr spec))
382          (specializer (car (last args)))
383          (class (nth 1 specializer)))
384     `(let (sym)
385        (mel-define-service ,name)
386        (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
387        (or (fboundp sym)
388            (fset sym (symbol-function ,function))))))
389
390 (defmacro mel-define-function (function spec)
391   (let* ((name (car spec))
392          (args (cdr spec))
393          (specializer (car (last args)))
394          (class (nth 1 specializer)))
395     `(progn
396        (define-function ,function
397          (intern ,class ,(intern (format "%s-obarray" name))))
398        )))
399
400 (defvar base64-dl-module
401   (if (and (fboundp 'base64-encode-string)
402            (subrp (symbol-function 'base64-encode-string)))
403       nil
404     (if (fboundp 'dynamic-link)
405         (let ((path (expand-file-name "base64.so" exec-directory)))
406           (and (file-exists-p path)
407                path)
408           ))))
409
410
411 ;;; @ end
412 ;;;
413
414 (provide 'mime-def)
415
416 ;;; mime-def.el ends here