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