Merge flim-1_12_6.
[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
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
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 'mcharset)
28
29 (eval-and-compile
30   (defconst mime-library-product ["FLIM" (1 12 6) "Family-K\e.D\8eòenmae"]
31     "Product name, version number and code name of MIME-library package.")
32   )
33
34 (defmacro mime-product-name (product)
35   `(aref ,product 0))
36
37 (defmacro mime-product-version (product)
38   `(aref ,product 1))
39
40 (defmacro mime-product-code-name (product)
41   `(aref ,product 2))
42
43 (defconst mime-library-version
44   (eval-when-compile
45     (concat (mime-product-name mime-library-product) " "
46             (mapconcat #'number-to-string
47                        (mime-product-version mime-library-product) ".")
48             " - \"" (mime-product-code-name mime-library-product) "\"")))
49
50
51 ;;; @ variables
52 ;;;
53
54 (require 'custom)
55
56 (eval-when-compile (require 'cl))
57
58 (defgroup mime nil
59   "Emacs MIME Interfaces"
60   :group 'news
61   :group 'mail)
62
63 (custom-handle-keyword 'default-mime-charset :group 'mime
64                        'custom-variable)
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
82 ;;; @ about STD 11
83 ;;;
84
85 (eval-and-compile
86   (defconst std11-quoted-pair-regexp "\\\\.")
87   (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
88   (defconst std11-qtext-regexp
89     (eval-when-compile
90       (concat "[^" std11-non-qtext-char-list "]"))))
91 (defconst std11-quoted-string-regexp
92   (eval-when-compile
93     (concat "\""
94             (regexp-*
95              (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
96             "\"")))
97
98
99 ;;; @ about MIME
100 ;;;
101
102 (eval-and-compile
103   (defconst mime-tspecial-char-list
104     '(?\] ?\[ ?\( ?\) ?< ?> ?@ ?, ?\; ?: ?\\ ?\" ?/ ?? ?=)))
105 (defconst mime-token-regexp
106   (eval-when-compile
107     (concat "[^" mime-tspecial-char-list "\000-\040]+")))
108 (defconst mime-charset-regexp mime-token-regexp)
109
110 (defconst mime-media-type/subtype-regexp
111   (concat mime-token-regexp "/" mime-token-regexp))
112
113
114 ;;; @@ base64 / B
115 ;;;
116
117 (defconst base64-token-regexp "[A-Za-z0-9+/]")
118 (defconst base64-token-padding-regexp "[A-Za-z0-9+/=]")
119
120 (defconst B-encoded-text-regexp
121   (concat "\\(\\("
122           base64-token-regexp
123           base64-token-regexp
124           base64-token-regexp
125           base64-token-regexp
126           "\\)*"
127           base64-token-regexp
128           base64-token-regexp
129           base64-token-padding-regexp
130           base64-token-padding-regexp
131           "\\)"))
132
133 ;; (defconst eword-B-encoding-and-encoded-text-regexp
134 ;;   (concat "\\(B\\)\\?" eword-B-encoded-text-regexp))
135
136
137 ;;; @@ Quoted-Printable / Q
138 ;;;
139
140 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
141
142 (defconst quoted-printable-octet-regexp
143   (concat "=[" quoted-printable-hex-chars
144           "][" quoted-printable-hex-chars "]"))
145
146 (defconst Q-encoded-text-regexp
147   (concat "\\([^=?]\\|" quoted-printable-octet-regexp "\\)+"))
148
149 ;; (defconst eword-Q-encoding-and-encoded-text-regexp
150 ;;   (concat "\\(Q\\)\\?" eword-Q-encoded-text-regexp))
151
152
153 ;;; @ Content-Type
154 ;;;
155
156 (defsubst make-mime-content-type (type subtype &optional parameters)
157   (list* (cons 'type type)
158          (cons 'subtype subtype)
159          (nreverse parameters))
160   )
161
162 (defsubst mime-content-type-primary-type (content-type)
163   "Return primary-type of CONTENT-TYPE."
164   (cdr (car content-type)))
165
166 (defsubst mime-content-type-subtype (content-type)
167   "Return primary-type of CONTENT-TYPE."
168   (cdr (cadr content-type)))
169
170 (defsubst mime-content-type-parameters (content-type)
171   "Return primary-type of CONTENT-TYPE."
172   (cddr content-type))
173
174 (defsubst mime-content-type-parameter (content-type parameter)
175   "Return PARAMETER value of CONTENT-TYPE."
176   (cdr (assoc parameter (mime-content-type-parameters content-type))))
177
178
179 (defsubst mime-type/subtype-string (type &optional subtype)
180   "Return type/subtype string from TYPE and SUBTYPE."
181   (if type
182       (if subtype
183           (format "%s/%s" type subtype)
184         (format "%s" type))))
185
186
187 ;;; @ Content-Disposition
188 ;;;
189
190 (defsubst mime-content-disposition-type (content-disposition)
191   "Return disposition-type of CONTENT-DISPOSITION."
192   (cdr (car content-disposition)))
193
194 (defsubst mime-content-disposition-parameters (content-disposition)
195   "Return disposition-parameters of CONTENT-DISPOSITION."
196   (cdr content-disposition))
197
198 (defsubst mime-content-disposition-parameter (content-disposition parameter)
199   "Return PARAMETER value of CONTENT-DISPOSITION."
200   (cdr (assoc parameter (cdr content-disposition))))
201
202 (defsubst mime-content-disposition-filename (content-disposition)
203   "Return filename of CONTENT-DISPOSITION."
204   (mime-content-disposition-parameter content-disposition "filename"))
205
206
207 ;;; @ MIME entity
208 ;;;
209
210 (defmacro make-mime-entity-internal (representation-type location
211                                      &optional content-type
212                                      children parent node-id
213                                      ;; for NOV
214                                      decoded-subject decoded-from
215                                      date message-id references
216                                      chars lines
217                                      xref
218                                      ;; for other fields
219                                      original-header parsed-header
220                                      ;; for buffer representation
221                                      buffer
222                                      header-start header-end
223                                      body-start body-end)
224   `(vector ,representation-type ,location
225            ,content-type nil nil ,children ,parent ,node-id
226            ;; for NOV
227            ,decoded-subject ,decoded-from
228            ,date ,message-id ,references
229            ,chars ,lines
230            ,xref
231            ;; for other fields
232            ,original-header ,parsed-header
233            ;; for buffer representation
234            ,buffer ,header-start ,header-end ,body-start ,body-end))
235
236 (defmacro mime-entity-representation-type-internal (entity)
237   `(aref ,entity 0))
238 (defmacro mime-entity-set-representation-type-internal (entity type)
239   `(aset ,entity 0 ,type))
240 (defmacro mime-entity-location-internal (entity)
241   `(aref ,entity 1))
242 (defmacro mime-entity-set-location-internal (entity location)
243   `(aset ,entity 1 ,location))
244
245 (defmacro mime-entity-content-type-internal (entity)
246   `(aref ,entity 2))
247 (defmacro mime-entity-set-content-type-internal (entity type)
248   `(aset ,entity 2 ,type))
249 (defmacro mime-entity-content-disposition-internal (entity)
250   `(aref ,entity 3))
251 (defmacro mime-entity-set-content-disposition-internal (entity disposition)
252   `(aset ,entity 3 ,disposition))
253 (defmacro mime-entity-encoding-internal (entity)
254   `(aref ,entity 4))
255 (defmacro mime-entity-set-encoding-internal (entity encoding)
256   `(aset ,entity 4 ,encoding))
257
258 (defmacro mime-entity-children-internal (entity)
259   `(aref ,entity 5))
260 (defmacro mime-entity-set-children-internal (entity children)
261   `(aset ,entity 5 ,children))
262 (defmacro mime-entity-parent-internal (entity)
263   `(aref ,entity 6))
264 (defmacro mime-entity-node-id-internal (entity)
265   `(aref ,entity 7))
266
267 (defmacro mime-entity-decoded-subject-internal (entity)
268   `(aref ,entity 8))
269 (defmacro mime-entity-set-decoded-subject-internal (entity subject)
270   `(aset ,entity 8 ,subject))
271 (defmacro mime-entity-decoded-from-internal (entity)
272   `(aref ,entity 9))
273 (defmacro mime-entity-set-decoded-from-internal (entity from)
274   `(aset ,entity 9 ,from))
275 (defmacro mime-entity-date-internal (entity)
276   `(aref ,entity 10))
277 (defmacro mime-entity-set-date-internal (entity date)
278   `(aset ,entity 10 ,date))
279 (defmacro mime-entity-message-id-internal (entity)
280   `(aref ,entity 11))
281 (defmacro mime-entity-set-message-id-internal (entity message-id)
282   `(aset ,entity 11 ,message-id))
283 (defmacro mime-entity-references-internal (entity)
284   `(aref ,entity 12))
285 (defmacro mime-entity-set-references-internal (entity references)
286   `(aset ,entity 12 ,references))
287 (defmacro mime-entity-chars-internal (entity)
288   `(aref ,entity 13))
289 (defmacro mime-entity-set-chars-internal (entity chars)
290   `(aset ,entity 13 ,chars))
291 (defmacro mime-entity-lines-internal (entity)
292   `(aref ,entity 14))
293 (defmacro mime-entity-set-lines-internal (entity lines)
294   `(aset ,entity 14 ,lines))
295 (defmacro mime-entity-xref-internal (entity)
296   `(aref ,entity 15))
297 (defmacro mime-entity-set-xref-internal (entity xref)
298   `(aset ,entity 15 ,xref))
299
300 (defmacro mime-entity-original-header-internal (entity)
301   `(aref ,entity 16))
302 (defmacro mime-entity-set-original-header-internal (entity header)
303   `(aset ,entity 16 ,header))
304 (defmacro mime-entity-parsed-header-internal (entity)
305   `(aref ,entity 17))
306 (defmacro mime-entity-set-parsed-header-internal (entity header)
307   `(aset ,entity 17 ,header))
308
309 (defmacro mime-entity-buffer-internal (entity)
310   `(aref ,entity 18))
311 (defmacro mime-entity-set-buffer-internal (entity buffer)
312   `(aset ,entity 18 ,buffer))
313 (defmacro mime-entity-header-start-internal (entity)
314   `(aref ,entity 19))
315 (defmacro mime-entity-set-header-start-internal (entity point)
316   `(aset ,entity 19 ,point))
317 (defmacro mime-entity-header-end-internal (entity)
318   `(aref ,entity 20))
319 (defmacro mime-entity-set-header-end-internal (entity point)
320   `(aset ,entity 20 ,point))
321 (defmacro mime-entity-body-start-internal (entity)
322   `(aref ,entity 21))
323 (defmacro mime-entity-set-body-start-internal (entity point)
324   `(aset ,entity 21 ,point))
325 (defmacro mime-entity-body-end-internal (entity)
326   `(aref ,entity 22))
327 (defmacro mime-entity-set-body-end-internal (entity point)
328   `(aset ,entity 22 ,point))
329
330
331 ;;; @ message structure
332 ;;;
333
334 (defvar mime-message-structure nil
335   "Information about structure of message.
336 Please use reference function `mime-entity-SLOT' to get value of SLOT.
337
338 Following is a list of slots of the structure:
339
340 buffer                  buffer includes this entity (buffer).
341 node-id                 node-id (list of integers)
342 header-start            minimum point of header in raw-buffer
343 header-end              maximum point of header in raw-buffer
344 body-start              minimum point of body in raw-buffer
345 body-end                maximum point of body in raw-buffer
346 content-type            content-type (content-type)
347 content-disposition     content-disposition (content-disposition)
348 encoding                Content-Transfer-Encoding (string or nil)
349 children                entities included in this entity (list of entity)
350
351 If an entity includes other entities in its body, such as multipart or
352 message/rfc822, `mime-entity' structures of them are included in
353 `children', so the `mime-entity' structure become a tree.")
354
355 (make-variable-buffer-local 'mime-message-structure)
356
357
358 ;;; @ for mm-backend
359 ;;;
360
361 (require 'alist)
362
363 (defvar mime-entity-implementation-alist nil)
364
365 (defmacro mm-define-backend (type &optional parents)
366   "Define TYPE as a mm-backend.
367 If PARENTS is specified, TYPE inherits PARENTS.
368 Each parent must be backend name (symbol)."
369   (if parents
370       `(let ((rest ',(reverse parents)))
371          (while rest
372            (set-alist 'mime-entity-implementation-alist
373                       ',type
374                       (copy-alist
375                        (cdr (assq (car rest)
376                                   mime-entity-implementation-alist))))
377            (setq rest (cdr rest))
378            ))))
379
380 (defmacro mm-define-method (name args &rest body)
381   "Define NAME as a method function of (nth 1 (car ARGS)) backend.
382
383 ARGS is like an argument list of lambda, but (car ARGS) must be
384 specialized parameter.  (car (car ARGS)) is name of variable and (nth
385 1 (car ARGS)) is name of backend."
386   (let* ((specializer (car args))
387          (class (nth 1 specializer))
388          (self (car specializer)))
389     `(let ((imps (cdr (assq ',class mime-entity-implementation-alist)))
390            (func (lambda ,(if self
391                               (cons self (cdr args))
392                             (cdr args))
393                    ,@body)))
394        (if imps
395            (set-alist 'mime-entity-implementation-alist
396                       ',class (put-alist ',name func imps))
397          (set-alist 'mime-entity-implementation-alist
398                     ',class
399                     (list (cons ',name func)))
400          ))))
401
402 (put 'mm-define-method 'lisp-indent-function 'defun)
403
404 (eval-when-compile
405   (defmacro eval-module-depended-macro (module definition)
406     (condition-case nil
407         (progn
408           (require (eval module))
409           definition)
410       (error `(eval-after-load ,(symbol-name (eval module)) ',definition))
411       ))
412   )
413
414 (eval-module-depended-macro
415  'edebug
416  (def-edebug-spec mm-define-method
417    (&define name ((arg symbolp)
418                   [&rest arg]
419                   [&optional ["&optional" arg &rest arg]]
420                   &optional ["&rest" arg]
421                   )
422             def-body))
423  )
424
425 (defsubst mm-arglist-to-arguments (arglist)
426   (let (dest)
427     (while arglist
428       (let ((arg (car arglist)))
429         (or (memq arg '(&optional &rest))
430             (setq dest (cons arg dest)))
431         )
432       (setq arglist (cdr arglist)))
433     (nreverse dest)))
434
435
436 ;;; @ for mel-backend
437 ;;;
438
439 (defvar mel-service-list nil)
440
441 (defmacro mel-define-service (name &optional args &rest rest)
442   "Define NAME as a service for Content-Transfer-Encodings.
443 If ARGS is specified, NAME is defined as a generic function for the
444 service."
445   `(progn
446      (add-to-list 'mel-service-list ',name)
447      (defvar ,(intern (format "%s-obarray" name)) (make-vector 7 0))
448      ,@(if args
449            `((defun ,name ,args
450                ,@rest
451                (funcall (mel-find-function ',name ,(car (last args)))
452                         ,@(mm-arglist-to-arguments (butlast args)))
453                )))
454      ))
455
456 (put 'mel-define-service 'lisp-indent-function 'defun)
457
458
459 (defvar mel-encoding-module-alist nil)
460
461 (defsubst mel-find-function-from-obarray (ob-array encoding)
462   (let* ((f (intern-soft encoding ob-array)))
463     (or f
464         (let ((rest (cdr (assoc encoding mel-encoding-module-alist))))
465           (while (and rest
466                       (progn
467                         (require (car rest))
468                         (null (setq f (intern-soft encoding ob-array)))
469                         ))
470             (setq rest (cdr rest))
471             )
472           f))))
473
474 (defsubst mel-copy-method (service src-backend dst-backend)
475   (let* ((oa (symbol-value (intern (format "%s-obarray" service))))
476          (f (mel-find-function-from-obarray oa src-backend))
477          sym)
478     (when f
479       (setq sym (intern dst-backend oa))
480       (or (fboundp sym)
481           (fset sym (symbol-function f))
482           ))))
483        
484 (defsubst mel-copy-backend (src-backend dst-backend)
485   (let ((services mel-service-list))
486     (while services
487       (mel-copy-method (car services) src-backend dst-backend)
488       (setq services (cdr services)))))
489
490 (defmacro mel-define-backend (type &optional parents)
491   "Define TYPE as a mel-backend.
492 If PARENTS is specified, TYPE inherits PARENTS.
493 Each parent must be backend name (string)."
494   (cons 'progn
495         (mapcar (lambda (parent)
496                   `(mel-copy-backend ,parent ,type)
497                   )
498                 parents)))
499
500 (defmacro mel-define-method (name args &rest body)
501   "Define NAME as a method function of (nth 1 (car (last ARGS))) backend.
502 ARGS is like an argument list of lambda, but (car (last ARGS)) must be
503 specialized parameter.  (car (car (last ARGS))) is name of variable
504 and (nth 1 (car (last ARGS))) is name of backend (encoding)."
505   (let* ((specializer (car (last args)))
506          (class (nth 1 specializer)))
507     `(progn
508        (mel-define-service ,name)
509        (fset (intern ,class ,(intern (format "%s-obarray" name)))
510              (lambda ,(butlast args)
511                ,@body)))))
512
513 (put 'mel-define-method 'lisp-indent-function 'defun)
514
515 (defmacro mel-define-method-function (spec function)
516   "Set SPEC's function definition to FUNCTION.
517 First element of SPEC is service.
518 Rest of ARGS is like an argument list of lambda, but (car (last ARGS))
519 must be specialized parameter.  (car (car (last ARGS))) is name of
520 variable and (nth 1 (car (last ARGS))) is name of backend (encoding)."
521   (let* ((name (car spec))
522          (args (cdr spec))
523          (specializer (car (last args)))
524          (class (nth 1 specializer)))
525     `(let (sym)
526        (mel-define-service ,name)
527        (setq sym (intern ,class ,(intern (format "%s-obarray" name))))
528        (or (fboundp sym)
529            (fset sym (symbol-function ,function))))))
530
531 (defmacro mel-define-function (function spec)
532   (let* ((name (car spec))
533          (args (cdr spec))
534          (specializer (car (last args)))
535          (class (nth 1 specializer)))
536     `(progn
537        (define-function ,function
538          (intern ,class ,(intern (format "%s-obarray" name))))
539        )))
540
541 (defvar base64-dl-module
542   (if (and (fboundp 'base64-encode-string)
543            (subrp (symbol-function 'base64-encode-string)))
544       nil
545     (if (fboundp 'dynamic-link)
546         (let ((path (expand-file-name "base64.so" exec-directory)))
547           (and (file-exists-p path)
548                path)
549           ))))
550
551
552 ;;; @ end
553 ;;;
554
555 (provide 'mime-def)
556
557 ;;; mime-def.el ends here