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