Sync up with chao-1_6_0.
[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.6.1 - \"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 (buffer
186                             header-start header-end body-start body-end
187                             &optional node-id
188                             content-type content-disposition
189                             encoding children)
190   (vector buffer header-start header-end body-start body-end
191           node-id content-type content-disposition encoding nil
192           children nil))
193
194 (defsubst mime-entity-buffer (entity)              (aref entity  0))
195 (defsubst mime-entity-header-start (entity)        (aref entity  1))
196 (defsubst mime-entity-header-end (entity)          (aref entity  2))
197 (defsubst mime-entity-body-start (entity)          (aref entity  3))
198 (defsubst mime-entity-body-end (entity)            (aref entity  4))
199 (defsubst mime-entity-node-id (entity)             (aref entity  5))
200 (defsubst mime-entity-content-type (entity)        (aref entity  6))
201 (defsubst mime-entity-content-disposition (entity) (aref entity  7))
202 (defsubst mime-entity-encoding (entity)            (aref entity  8))
203 (defsubst mime-entity-original-header (entity)     (aref entity  9))
204 (defsubst mime-entity-children (entity)            (aref entity 10))
205 (defsubst mime-entity-parsed-header (entity)       (aref entity 11))
206
207 (defsubst mime-entity-set-original-header (entity header)
208   (aset entity  9 header))
209 (defsubst mime-entity-set-children (entity children)
210   (aset entity 10 children))
211 (defsubst mime-entity-set-parsed-header (entity header)
212   (aset entity 11 header))
213
214 (defsubst mime-entity-number (entity)
215   (reverse (mime-entity-node-id entity)))
216
217 (defalias 'mime-entity-point-min 'mime-entity-header-start)
218 (defalias 'mime-entity-point-max 'mime-entity-body-end)
219
220 (defsubst mime-entity-media-type (entity)
221   (mime-content-type-primary-type (mime-entity-content-type entity)))
222 (defsubst mime-entity-media-subtype (entity)
223   (mime-content-type-subtype (mime-entity-content-type entity)))
224 (defsubst mime-entity-parameters (entity)
225   (mime-content-type-parameters (mime-entity-content-type entity)))
226
227 (defsubst mime-entity-type/subtype (entity-info)
228   (mime-type/subtype-string (mime-entity-media-type entity-info)
229                             (mime-entity-media-subtype entity-info)))
230
231
232 ;;; @ message structure
233 ;;;
234
235 (defvar mime-message-structure nil
236   "Information about structure of message.
237 Please use reference function `mime-entity-SLOT' to get value of SLOT.
238
239 Following is a list of slots of the structure:
240
241 buffer                  buffer includes this entity (buffer).
242 node-id                 node-id (list of integers)
243 header-start            minimum point of header in raw-buffer
244 header-end              maximum point of header in raw-buffer
245 body-start              minimum point of body in raw-buffer
246 body-end                maximum point of body in raw-buffer
247 content-type            content-type (content-type)
248 content-disposition     content-disposition (content-disposition)
249 encoding                Content-Transfer-Encoding (string or nil)
250 children                entities included in this entity (list of entity)
251
252 If an entity includes other entities in its body, such as multipart or
253 message/rfc822, `mime-entity' structures of them are included in
254 `children', so the `mime-entity' structure become a tree.")
255
256 (make-variable-buffer-local 'mime-message-structure)
257
258
259 ;;; @ end
260 ;;;
261
262 (provide 'mime-def)
263
264 ;;; mime-def.el ends here