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 (defconst mime-library-version-string "FLIM 1.7.0 - \"Iseda\"")
28
29
30 ;;; @ variables
31 ;;;
32
33 (require 'custom)
34
35 (eval-when-compile (require 'cl))
36
37 (defgroup mime nil
38   "Emacs MIME Interfaces"
39   :group 'news
40   :group 'mail)
41
42 (custom-handle-keyword 'default-mime-charset :group 'mime
43                        'custom-variable)
44
45 (defcustom mime-temp-directory (or (getenv "MIME_TMP_DIR")
46                                    (getenv "TM_TMP_DIR")
47                                    (getenv "TMPDIR")
48                                    (getenv "TMP")
49                                    (getenv "TEMP")
50                                    "/tmp/")
51   "*Directory for temporary files."
52   :group 'mime
53   :type 'directory)
54
55 (defcustom mime-uuencode-encoding-name-list '("x-uue" "x-uuencode")
56   "*List of encoding names for uuencode format."
57   :group 'mime
58   :type '(repeat string))
59
60
61 ;;; @ required functions
62 ;;;
63
64 (unless (fboundp 'butlast)
65   (defun butlast (x &optional n)
66     "Returns a copy of LIST with the last N elements removed."
67     (if (and n (<= n 0)) x
68       (nbutlast (copy-sequence x) n)))
69   
70   (defun nbutlast (x &optional n)
71     "Modifies LIST to remove the last N elements."
72     (let ((m (length x)))
73       (or n (setq n 1))
74       (and (< n m)
75            (progn
76              (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
77              x))))
78   )
79
80 (defsubst eliminate-top-spaces (string)
81   "Eliminate top sequence of space or tab in STRING."
82   (if (string-match "^[ \t]+" string)
83       (substring string (match-end 0))
84     string))
85
86 (defsubst regexp-* (regexp)
87   (concat regexp "*"))
88
89 (defsubst regexp-or (&rest args)
90   (concat "\\(" (mapconcat (function identity) args "\\|") "\\)"))
91
92
93 ;;; @ about STD 11
94 ;;;
95
96 (defconst std11-quoted-pair-regexp "\\\\.")
97 (defconst std11-non-qtext-char-list '(?\" ?\\ ?\r ?\n))
98 (defconst std11-qtext-regexp
99   (concat "[^" (char-list-to-string std11-non-qtext-char-list) "]"))
100 (defconst std11-quoted-string-regexp
101   (concat "\""
102           (regexp-*
103            (regexp-or std11-qtext-regexp std11-quoted-pair-regexp))
104           "\""))
105
106
107 ;;; @ about MIME
108 ;;;
109
110 (defconst mime-tspecials "][()<>@,\;:\\\"/?=")
111 (defconst mime-token-regexp (concat "[^" mime-tspecials "\000-\040]+"))
112 (defconst mime-charset-regexp mime-token-regexp)
113
114 (defconst mime-media-type/subtype-regexp
115   (concat mime-token-regexp "/" mime-token-regexp))
116
117
118 ;;; @@ Quoted-Printable
119 ;;;
120
121 (defconst quoted-printable-hex-chars "0123456789ABCDEF")
122
123 (defconst quoted-printable-octet-regexp
124   (concat "=[" quoted-printable-hex-chars
125           "][" quoted-printable-hex-chars "]"))
126
127
128 ;;; @ Content-Type
129 ;;;
130
131 (defsubst make-mime-content-type (type subtype &optional parameters)
132   (list* (cons 'type type)
133          (cons 'subtype subtype)
134          (nreverse parameters))
135   )
136
137 (defsubst mime-content-type-primary-type (content-type)
138   "Return primary-type of CONTENT-TYPE."
139   (cdr (car content-type)))
140
141 (defsubst mime-content-type-subtype (content-type)
142   "Return primary-type of CONTENT-TYPE."
143   (cdr (cadr content-type)))
144
145 (defsubst mime-content-type-parameters (content-type)
146   "Return primary-type of CONTENT-TYPE."
147   (cddr content-type))
148
149 (defsubst mime-content-type-parameter (content-type parameter)
150   "Return PARAMETER value of CONTENT-TYPE."
151   (cdr (assoc parameter (mime-content-type-parameters content-type))))
152
153
154 (defsubst mime-type/subtype-string (type &optional subtype)
155   "Return type/subtype string from TYPE and SUBTYPE."
156   (if type
157       (if subtype
158           (format "%s/%s" type subtype)
159         (format "%s" type))))
160
161
162 ;;; @ Content-Disposition
163 ;;;
164
165 (defsubst mime-content-disposition-type (content-disposition)
166   "Return disposition-type of CONTENT-DISPOSITION."
167   (cdr (car content-disposition)))
168
169 (defsubst mime-content-disposition-parameters (content-disposition)
170   "Return disposition-parameters of CONTENT-DISPOSITION."
171   (cdr content-disposition))
172
173 (defsubst mime-content-disposition-parameter (content-disposition parameter)
174   "Return PARAMETER value of CONTENT-DISPOSITION."
175   (cdr (assoc parameter (cdr content-disposition))))
176
177 (defsubst mime-content-disposition-filename (content-disposition)
178   "Return filename of CONTENT-DISPOSITION."
179   (mime-content-disposition-parameter content-disposition "filename"))
180
181
182 ;;; @ MIME entity
183 ;;;
184
185 (defsubst make-mime-entity-internal (buffer
186                                      header-start header-end
187                                      body-start body-end
188                                      &optional node-id
189                                      content-type children)
190   (vector buffer header-start header-end body-start body-end
191           node-id content-type nil nil nil children nil))
192
193 (defsubst mime-entity-buffer-internal (entity)              (aref entity  0))
194 (defsubst mime-entity-header-start-internal (entity)        (aref entity  1))
195 (defsubst mime-entity-header-end-internal (entity)          (aref entity  2))
196 (defsubst mime-entity-body-start-internal (entity)          (aref entity  3))
197 (defsubst mime-entity-body-end-internal (entity)            (aref entity  4))
198 (defsubst mime-entity-node-id-internal (entity)             (aref entity  5))
199 (defsubst mime-entity-content-type-internal (entity)        (aref entity  6))
200 (defsubst mime-entity-content-disposition-internal (entity) (aref entity  7))
201 (defsubst mime-entity-encoding-internal (entity)            (aref entity  8))
202 (defsubst mime-entity-original-header-internal (entity)     (aref entity  9))
203 (defsubst mime-entity-children-internal (entity)            (aref entity 10))
204 (defsubst mime-entity-parsed-header-internal (entity)       (aref entity 11))
205
206 (defsubst mime-entity-set-content-disposition-internal (entity disposition)
207   (aset entity  7 disposition))
208 (defsubst mime-entity-set-encoding-internal (entity encoding)
209   (aset entity  8 encoding))
210 (defsubst mime-entity-set-original-header-internal (entity header)
211   (aset entity  9 header))
212 (defsubst mime-entity-set-children-internal (entity children)
213   (aset entity 10 children))
214 (defsubst mime-entity-set-parsed-header-internal (entity header)
215   (aset entity 11 header))
216
217
218 ;;; @ message structure
219 ;;;
220
221 (defvar mime-message-structure nil
222   "Information about structure of message.
223 Please use reference function `mime-entity-SLOT' to get value of SLOT.
224
225 Following is a list of slots of the structure:
226
227 buffer                  buffer includes this entity (buffer).
228 node-id                 node-id (list of integers)
229 header-start            minimum point of header in raw-buffer
230 header-end              maximum point of header in raw-buffer
231 body-start              minimum point of body in raw-buffer
232 body-end                maximum point of body in raw-buffer
233 content-type            content-type (content-type)
234 content-disposition     content-disposition (content-disposition)
235 encoding                Content-Transfer-Encoding (string or nil)
236 children                entities included in this entity (list of entity)
237
238 If an entity includes other entities in its body, such as multipart or
239 message/rfc822, `mime-entity' structures of them are included in
240 `children', so the `mime-entity' structure become a tree.")
241
242 (make-variable-buffer-local 'mime-message-structure)
243
244
245 ;;; @ end
246 ;;;
247
248 (provide 'mime-def)
249
250 ;;; mime-def.el ends here