removed unused code
[mime-lua.git] / mime.lua
blob645c0057d4915ffc99e04481ab464d11d9e1d752
1 #!/usr/bin/lua
3 require 'lpeg'
5 -- MIME implementation in Lua
7 -- XXX: at section 3.4
10 local string = string
11 local type = type
12 local tostring = tostring
13 local select = select
14 local setfenv = setfenv
15 local CRLF = '\r\n'
16 local lpeg = lpeg
17 local print = print
19 -- this collects both the table of named captures and the
20 -- substitution captureof the given pattern
21 lpeg.Cst = function (patt)
22 return (#lpeg.Cs(patt) * lpeg.Ct(patt)) / function (s, t)
23 local ret = {}
24 ret [1] = s
25 for k, v in pairs(t) do
26 if type(k)~='number' then
27 ret[k] = v
28 end
29 end
30 return ret
31 end
32 end
34 -- rfc2822 Section 2.1:
36 -- A message consists of header fields (collectively called "the header
37 -- of the message") followed, optionally, by a body. The header is a
38 -- sequence of lines of characters with special syntax as defined in
39 -- this standard. The body is simply a sequence of characters that
40 -- follows the header and is separated from the header by an empty line
41 -- (i.e., a line with nothing preceding the CRLF).
43 local split_content = function (c)
44 -- NOTE: there is no guarantee that the CRLF separating header
45 -- and body is the first one. However no headers admit a blank
46 -- line so far
47 local h, b = string.match(c, '^(.-)'..CRLF..CRLF..'(.*)$')
48 assert(type(h)=='string' and type(b)=='string')
49 return h, b
50 end
52 -- rfc2822 Section 2.2.3
54 -- The process of moving from this folded multiple-line representation
55 -- of a header field to its single line representation is called
56 -- "unfolding". Unfolding is accomplished by simply removing any CRLF
57 -- that is immediately followed by WSP. Each header field should be
58 -- treated in its unfolded form for further syntactic and semantic
59 -- evaluation.
61 local unfold_header = function (h)
62 return string.gsub(h, CRLF..'[ \t]', ' ')
63 end
65 -- rfc2822 Section 2.1
67 -- At the most basic level, a message is a series of characters. A
68 -- message that is conformant with this standard is comprised of
69 -- characters with values in the range 1 through 127 and interpreted as
70 -- US-ASCII characters [ASCII]. For brevity, this document sometimes
71 -- refers to this range of characters as simply "US-ASCII characters".
74 -- rfc2822 Section 2.1.1
76 -- There are two limits that this standard places on the number of
77 -- characters in a line. Each line of characters MUST be no more than
78 -- 998 characters, and SHOULD be no more than 78 characters, excluding
79 -- the CRLF.
81 local check_content = function (c)
82 local err, warn
83 -- TODO: implement in a portable way
84 return err, warn
85 end
87 -- rfc2822 Section 2.3
89 -- The body of a message is simply lines of US-ASCII characters. The
90 -- only two limitations on the body are as follows:
92 -- - CR and LF MUST only occur together as CRLF; they MUST NOT appear
93 -- independently in the body.
95 -- - Lines of characters in the body MUST be limited to 998 characters,
97 -- and SHOULD be limited to 78 characters, excluding the CRLF.
99 local check_body = function (b)
100 local err, warn
101 -- TODO: implement
102 return err, warn
105 -- some core values (not implemented literally)
106 -- see RFC 2234 Section 6.1
107 local core_values = {
108 ["ALPHA"] = lpeg.R("AZ", "az"),
109 ["BIT"] = lpeg.P("0") + lpeg.P("1"),
110 ["CHAR"] = lpeg.R("\01\127"),
111 ["CR"] = lpeg.P("\13"),
112 ["CRLF"] = lpeg.P("\13\10"),
113 ["DIGIT"] = lpeg.R("09"),
114 ["LF"] = lpeg.P("\10"),
115 ["WSP"] = lpeg.S("\32\09"),
118 -- lexical tokens used in the specification
119 -- TODO: could be optimized
120 -- check for non-obfuscated optimizations
121 -- TODO: write in a less obfuscated way
122 -- check if parenthesis can be removed
123 local lex_tokens = function ()
124 setfenv(1, lpeg)
125 return {
126 -- control characters without whitespaces
127 ["NO-WS-CTL"] = R("\1\8") +
128 P("\11") +
129 P("\12") +
130 R("\14\13") +
131 P("\127"), -- RFC 2822 Section 3.2.1
132 -- a character in a text
133 ["text"] = R("\1\9") +
134 P("\11") +
135 P("\12") +
136 R("\14\127"), -- RFC 2822 Section 3.2.1
137 -- a special character
138 ["specials"] = P("(") + P(")") +
139 P("<") + P(">") +
140 P("[") + P("]") +
141 P(":") + P(";") +
142 P("@") + P("\\") +
143 P(",") + P(".") +
144 P("\""), -- RFC 2822 Section 3.2.1
145 -- a quoted pair should return only the second character
146 ["quoted-pair"] = (P("\\") * C(V("text"))) + V("obs-qp"), -- RFC 2822 Section 3.2.2
147 -- a folding white space (a whitespace that can include a CRLF)
148 -- should be substituted by a single whitespace
149 ["FWS"] = (((V("WSP")^0 * V("CRLF"))^-1 * V("WSP")^1) + V("obs-FWS")) / " ", -- RFC 2822 Section 3.2.3
150 -- a text character allowed inside a comment
151 ["ctext"] = V("NO-WS-CTL") +
152 R("\33\39") +
153 R("\42\91") +
154 R("\93\126"), -- RFC 2822 Section 3.2.3
155 -- the content of a comment (comments can nest)
156 ["ccontent"] = V("ctext") + V("quoted-pair") + V("comment"), -- RFC 2822 Section 3.2.3
157 -- an actual comment
158 -- should be substituted by a single whitespace
159 ["comment"] = (P("(") * (V("FWS")^-1 * V("ccontent"))^0 * V("FWS")^-1 * P(")")) / " ", -- RFC 2822 Section 3.2.3
160 -- a comment or a folding white space
161 -- should be substituted by a single whitespace
163 -- Folding white spaces should not be placed in a way that
164 -- creates lines containing only whitespaces.
165 -- This requirement Is not necessarily enforced by this grammar
166 ["CFWS"] = ( (V("FWS") + (V("comment")*V("FWS")^-1)) * (V("comment")*V("FWS")^-1)^0 ) / " ", -- RFC 2822 Section 3.2.3
167 -- character that can appear in an atom
168 ["atext"] = V("ALPHA") + V("DIGIT") +
169 P("!") + P("#") +
170 P("$") + P("%") +
171 P("&") + P("'") +
172 P("*") + P("+") +
173 P("-") + P("/") +
174 P("=") + P("?") +
175 P("^") + P("_") +
176 P("`") + P("{") +
177 P("|") + P("}") +
178 P("~"), -- RFC 2822 Section 3.2.4
179 -- an atom is equal to the content only discarding comments and whitespace
180 ["atom"] = V("CFWS")^-1 * C(V("atext")^1) * V("CFWS")^-1, -- RFC 2822 Section 3.2.4
181 -- an atom with dots is only the content, discarding CFWSs
182 ["dot-atom"] = Cs((V("CFWS")^-1/'') * C(V("dot-atom-text")) * (V("CFWS")^-1/'')), -- RFC 2822 Section 3.2.4
183 -- the content of an atom text with dots
184 ["dot-atom-text"] = V("atext")^1 * (P(".") * V("atext")^1)^0, -- RFC 2822 Section 3.2.4
185 -- character that can appear in a quoted string
186 ["qtext"] = V("NO-WS-CTL") +
187 P("\33") +
188 R("\35\91") +
189 R("\93\126"), -- RFC 2822 Section 3.2.5
190 -- character or quoted pair (both can appear in a quoted string)
191 -- it is equivalent to the character itself or to the result of the
192 -- quoted pair
193 ["qcontent"] = C(V("qtext")) + V("quoted-pair"), -- RFC 2822 Section 3.2.5
194 -- a quoted string is equal to its content
195 ["quoted-string"] = V("CFWS")^-1 *
196 P("\"") * Cs((V("FWS")^-1 * V("qcontent"))^0 * V("FWS")^-1) * P("\"") * V("CFWS")^-1, -- RFC 2822 Section 3.2.5
197 -- unstructured patterns for unspecified headers
198 -- what should these be equal to?
200 -- an generic word
201 ["word"] = V("atom") + V("quoted-string"), -- RFC 2822 Section 3.2.6
202 -- an generic phrase
203 ["phrase"] = V("word")^1 + V("obs-phrase"), -- RFC 2822 Section 3.2.6
204 -- a character for unstructured text
205 ["utext"] = V("NO-WS-CTL") + R("\33\126") + V("obs-utext"), -- RFC 2822 Section 3.2.6
206 -- an unstructured text
207 ["unstructured"] = (V("FWS")^-1 * V("utext"))^0 * V("FWS")^-1, -- RFC 2822 Section 3.2.6
211 local show = my.show
212 local deb = function(...) for i=1,select('#',...) do local v=select(i,...)print(type(v), v)end end
214 local date_time = function ()
215 setfenv(1, lpeg)
216 return {
217 -- date and time specification
218 -- dates and times should be valid
219 -- this grammar does not enforce this yet
220 ["date-time"] = Cst(( V"day-of-week" * P"," )^-1 * V"date" * V"FWS" * V"time" * V"CFWS"^-1), -- RFC 2822 Section 3.3
221 ["day-of-week"] = ( V"FWS"^-1 * V"day-name" ) + V"obs-day-of-week", -- RFC 2822 Section 3.3
222 ["day-name"] = Cg(P"Mon" + P"Tue" + P"Wed" + P"Thu" + P"Fri" + P"Sat" + P"Sun", "weekday"), -- RFC 2822 Section 3.3
223 ["date"] = V"day" * V"month" * V"year", -- RFC 2822 Section 3.3
224 ["year"] = Cg(C(V"DIGIT"^4), "year") + V"obs-year", -- RFC 2822 Section 3.3
225 ["month"] = (V"FWS" * V"month-name" * V"FWS") + V"obs-month", -- RFC 2822 Section 3.3
226 ["month-name"] = Cg(C"Jan" + C"Feb" + C"Mar" + C"Apr" + C"May" + C"Jun" + C"Jul" + C"Aug" + C"Sep" + C"Oct" + C"Nov" + C"Dec", "month"), -- RFC 2822 Section 3.3
227 ["day"] = (V"FWS"^-1 * Cg(C(V"DIGIT" * V"DIGIT"^-1), "day")) + V"obs-day", -- RFC 2822 Section 3.3
228 ["time"] = V"time-of-day" * V"FWS" * V"zone", -- RFC 2822 Section 3.3
229 ["time-of-day"] = V"hour" * P":" * V"minute" * (P":" * V"second")^-1, -- RFC 2822 Section 3.3
230 ["hour"] = Cg(C(V"DIGIT" * V"DIGIT"), "hour") + V"obs-hour", -- RFC 2822 Section 3.3
231 ["minute"] = Cg((V"DIGIT" * V"DIGIT"), "minute") + V"obs-minute", -- RFC 2822 Section 3.3
232 ["second"] = Cg((V"DIGIT" * V"DIGIT"), "second") + V"obs-second", -- RFC 2822 Section 3.3
233 ["zone"] = Cg( (P"+" + P"-") * V"DIGIT" * V"DIGIT" * V"DIGIT" * V"DIGIT", "zone" ) + V"obs-zone", -- RFC 2822 Section 3.3
237 local address = function ()
238 setfenv(1, lpeg)
239 return {
240 -- address specification
241 -- dates and times should be valid
242 -- this grammar does not enforce this yet
243 ["address"] = V"mailbox" + V"group", -- RFC 2822 Section 3.4
244 ["mailbox"] = V"name-addr" + V"addr-spec", -- RFC 2822 Section 3.4
245 ["name-addr"] = V"display-name"^-1 * V"angle-addr", -- RFC 2822 Section 3.4
246 ["angle-addr"] = (V"CFWS"^-1 * P"<" * V"addr-spec" * P">" * V"CFWS"^-1) + V"obs-angle-addr", -- RFC 2822 Section 3.4
247 ["group"] = V"display-name" * P":" * (V"mailbox-list" + V"CFWS") * P";" * V"CFWS"^-1, -- RFC 2822 Section 3.4
248 ["display-name"] = V"phrase", -- RFC 2822 Section 3.4
249 ["mailbox-list"] = (V"mailbox" * (P"," * V"mailbox")^0) + V"obs-mailbox-list", -- RFC 2822 Section 3.4
250 ["address-list"] = (V"address" * (P"," * V"address")^0) + V"obs-address-list", -- RFC 2822 Section 3.4
251 -- address specification (name@host.domain)
252 ["addr-spec"] = V"local-part" * P"@" * V"domain", -- RFC 2822 Section 3.4.1
253 ["local-part"] = V"dot-atom" + V"quoted-string" + V"obs-local-part", -- RFC 2822 Section 3.4.1
254 ["domain"] = V"dot-atom" + V"domain-literal" + V"obs-domain", -- RFC 2822 Section 3.4.1
255 ["domain-literal"] = V"CFWS"^-1 * P"[" * (V"FWS"^-1 * V"dcontent")^0 * V"FWS"^-1 * P"]" * V"CFWS"^-1, -- RFC 2822 Section 3.4.1
256 ["dcontent"] = V"dtext" + V"quoted-pair", -- RFC 2822 Section 3.4.1
257 ["dtext"] = V"NO-WS-CTL" + R"\33\90" + R"\94\126", -- RFC 2822 Section 3.4.1
261 local overall_message = function ()
262 setfenv(1, lpeg)
263 return {
264 -- overall message specification
266 -- RRC 2822 Section 3.5:
268 -- A message consists of header fields, optionally followed by a message
269 -- body. Lines in a message MUST be a maximum of 998 characters
270 -- excluding the CRLF, but it is RECOMMENDED that lines be limited to 78
271 -- characters excluding the CRLF.
273 -- this grammar does not enforce the 78 chars limit yet
274 ["message"] = (V"fields" + V"obs-fields") * (V"CRLF" * V"body")^-1, -- RFC 2822 Section 3.5
275 ["body"] = (V"text"^-998 * V"CRLF")^0 * V"text"^-998, -- RFC 2822 Section 3.5
279 local fields = function ()
280 setfenv(1, lpeg)
281 return {
282 -- spec for all fields in the header
284 -- RRC 2822 Section 3.6
286 --the "fields" rule is incomplete
287 ["fields"] = -- trace fields missing
288 (V"orig-date" + V"from" + V"sender" + V"reply-to" + V"to" + V"cc" + V"bcc"
289 + V"message-id" + V"in-reply-to" + V"to" + V"cc" + V"bcc")^0, -- RFC 2822 Section 3.6
290 -- origination date field: the time at which the author decided
291 -- the message was ready to be sent (e.g. hit the button)
292 ["orig-date"] = P"Date:" * V"date-time" * V"CRLF", -- RFC 2822 Section 3.6.1
293 -- the author of the message
294 ["from"] = P"From:" * V"mailbox-list" * V"CRLF", -- RFC 2822 Section 3.6.2
295 -- the actual agent that sent the message
296 ["sender"] = P"Sender:" * V"mailbox" * V"CRLF", -- RFC 2822 Section 3.6.2
297 -- address which should be replied to
298 -- see Section 3.6.3 for forming replies
299 ["reply-to"] = P"Reply-To:" * V"address-list" * V"CRLF", -- RFC 2822 Section 3.6.2
300 -- The "To:" field contains the address(es) of the primary recipient(s)
301 -- of the message.
302 ["to"] = P"To:" * V"address-list" * V"CRLF", -- RFC 2822 Section 3.6.3
303 -- The "Cc:" field contains the addresses of others who are to receive
304 -- the message, though the content of the message may not be directed
305 -- at them.
306 ["cc"] = P"Cc:" * V"address-list" * V"CRLF", -- RFC 2822 Section 3.6.3
307 -- The "Bcc:" field contains addresses of recipients of the message
308 -- whose addresses are not to be revealed to other recipients of the
309 -- message.
310 ["bcc"] = P"Bcc:" * (V"address-list" + V"CFWS") * V"CRLF", -- RFC 2822 Section 3.6.3
311 ["message-id"] = P"Message-Id:" * V"msg-id" * V"CRLF", -- RFC 2822 Section 3.6.4
312 ["in-reply-to"] = P"In-Reply-To:" * V"msg-id"^1 * V"CRLF", -- RFC 2822 Section 3.6.4
313 ["references"] = P"References:" * V"msg-id"^1 * V"CRLF", -- RFC 2822 Section 3.6.4
314 ["msg-id"] = V"CFWS"^-1 * P"<" * V"id-left" * P"@" * V"id-right" * P">" * V"CFWS"^-1, -- RFC 2822 Section 3.6.4
315 ["id-left"] = V"dot-atom-text" + V"no-fold-quote" + V"obs-id-left", -- RFC 2822 Section 3.6.4
316 ["id-right"] = V"dot-atom-text" + V"no-fold-literal" + V"obs-id-right", -- RFC 2822 Section 3.6.4
317 ["no-fold-quote"] = P"\"" * (V"qtext" + V"quoted-pair")^0 * P"\"", -- RFC 2822 Section 3.6.4
318 ["no-fold-literal"] = P"[" * (V"dtext" + V"quoted-pair")^0 * P"]", -- RFC 2822 Section 3.6.4
322 local obs_strict = {
323 ["obs-FWS"] = lpeg.P(false),
324 ["obs-qp"] = lpeg.P(false),
325 ["obs-phrase"] = lpeg.P(false),
326 ["obs-utext"] = lpeg.P(false),
327 ["obs-day-of-week"] = lpeg.P(false),
328 ["obs-year"] = lpeg.P(false),
329 ["obs-month"] = lpeg.P(false),
330 ["obs-day"] = lpeg.P(false),
331 ["obs-hour"] = lpeg.P(false),
332 ["obs-minute"] = lpeg.P(false),
333 ["obs-second"] = lpeg.P(false),
334 ["obs-zone"] = lpeg.P(false),
335 ["obs-angle-addr"] = lpeg.P(false),
336 ["obs-mailbox-list"] = lpeg.P(false),
337 ["obs-address-list"] = lpeg.P(false),
338 ["obs-local-part"] = lpeg.P(false),
339 ["obs-domain"] = lpeg.P(false),
340 ["obs-fields"] = lpeg.P(false),
341 ["obs-id-left"] = lpeg.P(false),
342 ["obs-id-right"] = lpeg.P(false),
345 local os = setmetatable({}, {__index = function() return lpeg.P(false) end})
347 local join_set = function (...)
348 local n = select('#', ...)
349 local ret = {}
350 for i = 1, n do
351 local t = select(i, ...)
352 if type(t)=='table' then
353 for k, v in pairs(t) do
354 ret[k] = v
356 elseif type(t)=='string' then
357 ret[1] = t
358 elseif type(t)=='boolean' then
359 -- TODO: check no overwrite
360 else
361 error('join_set: bad argument number '..i..' of type '..type(t))
364 return ret
367 local gr = join_set(core_values, lex_tokens(), date_time(), address(), overall_message(), fields(), obs_strict)
369 return gr