fixed some captures
[mime-lua.git] / mime.lua
blob6aa9e5c68275152dca2015a8d5e6926f8696fbdb
1 #!/usr/bin/lua
2 --
3 -- Copyright (c) 2009 Mauro Iazzi
4 --
5 -- Permission is hereby granted, free of charge, to any person
6 -- obtaining a copy of this software and associated documentation
7 -- files (the "Software"), to deal in the Software without
8 -- restriction, including without limitation the rights to use,
9 -- copy, modify, merge, publish, distribute, sublicense, and/or sell
10 -- copies of the Software, and to permit persons to whom the
11 -- Software is furnished to do so, subject to the following
12 -- conditions:
14 -- The above copyright notice and this permission notice shall be
15 -- included in all copies or substantial portions of the Software.
17 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 -- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 -- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 -- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 -- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 -- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 -- OTHER DEALINGS IN THE SOFTWARE.
25 --/
27 require 'lpeg'
29 -- MIME implementation in Lua
31 -- XXX: at section 3.4
34 local string = string
35 local type = type
36 local tostring = tostring
37 local select = select
38 local setfenv = setfenv
39 local CRLF = '\r\n'
40 local lpeg = lpeg
41 local print = print
43 -- this creates a named and substitution capture for the given pattern
44 lpeg.Csg = function (patt, name)
45 return (#lpeg.Cg(patt, name)) * lpeg.Cs(patt)
46 end
47 -- this collects both the table of named captures and the
48 -- substitution capture of the given pattern
49 lpeg.Cst = function (patt)
50 return (#lpeg.Cs(patt) * lpeg.Ct(patt)) / function (s, t)
51 local ret = {}
52 ret [1] = s
53 for k, v in pairs(t) do
54 if type(k)~='number' then
55 ret[k] = v
56 end
57 end
58 return ret
59 end
60 end
62 -- rfc2822 Section 2.1:
64 -- A message consists of header fields (collectively called "the header
65 -- of the message") followed, optionally, by a body. The header is a
66 -- sequence of lines of characters with special syntax as defined in
67 -- this standard. The body is simply a sequence of characters that
68 -- follows the header and is separated from the header by an empty line
69 -- (i.e., a line with nothing preceding the CRLF).
71 local split_content = function (c)
72 -- NOTE: there is no guarantee that the CRLF separating header
73 -- and body is the first one. However no headers admit a blank
74 -- line so far
75 local h, b = string.match(c, '^(.-)'..CRLF..CRLF..'(.*)$')
76 assert(type(h)=='string' and type(b)=='string')
77 return h, b
78 end
80 -- rfc2822 Section 2.2.3
82 -- The process of moving from this folded multiple-line representation
83 -- of a header field to its single line representation is called
84 -- "unfolding". Unfolding is accomplished by simply removing any CRLF
85 -- that is immediately followed by WSP. Each header field should be
86 -- treated in its unfolded form for further syntactic and semantic
87 -- evaluation.
89 local unfold_header = function (h)
90 return string.gsub(h, CRLF..'[ \t]', ' ')
91 end
93 -- rfc2822 Section 2.1
95 -- At the most basic level, a message is a series of characters. A
96 -- message that is conformant with this standard is comprised of
97 -- characters with values in the range 1 through 127 and interpreted as
98 -- US-ASCII characters [ASCII]. For brevity, this document sometimes
99 -- refers to this range of characters as simply "US-ASCII characters".
102 -- rfc2822 Section 2.1.1
104 -- There are two limits that this standard places on the number of
105 -- characters in a line. Each line of characters MUST be no more than
106 -- 998 characters, and SHOULD be no more than 78 characters, excluding
107 -- the CRLF.
109 local check_content = function (c)
110 local err, warn
111 -- TODO: implement in a portable way
112 return err, warn
115 -- rfc2822 Section 2.3
117 -- The body of a message is simply lines of US-ASCII characters. The
118 -- only two limitations on the body are as follows:
120 -- - CR and LF MUST only occur together as CRLF; they MUST NOT appear
121 -- independently in the body.
123 -- - Lines of characters in the body MUST be limited to 998 characters,
125 -- and SHOULD be limited to 78 characters, excluding the CRLF.
127 local check_body = function (b)
128 local err, warn
129 -- TODO: implement
130 return err, warn
133 -- some core values (not implemented literally)
134 -- see RFC 2234 Section 6.1
135 local core_values = {
136 ["ALPHA"] = lpeg.R("AZ", "az"),
137 ["BIT"] = lpeg.P("0") + lpeg.P("1"),
138 ["CHAR"] = lpeg.R("\01\127"),
139 ["CR"] = lpeg.P("\13"),
140 ["CRLF"] = lpeg.P("\13\10"),
141 ["DIGIT"] = lpeg.R("09"),
142 ["LF"] = lpeg.P("\10"),
143 ["WSP"] = lpeg.S("\32\09"),
146 -- lexical tokens used in the specification
147 -- TODO: could be optimized
148 -- check for non-obfuscated optimizations
149 -- TODO: write in a less obfuscated way
150 -- check if parenthesis can be removed
151 local lex_tokens = function ()
152 setfenv(1, lpeg)
153 return {
154 -- control characters without whitespaces
155 ["NO-WS-CTL"] = R("\1\8") +
156 P("\11") +
157 P("\12") +
158 R("\14\13") +
159 P("\127"), -- RFC 2822 Section 3.2.1
160 -- a character in a text
161 ["text"] = R("\1\9") +
162 P("\11") +
163 P("\12") +
164 R("\14\127"), -- RFC 2822 Section 3.2.1
165 -- a special character
166 ["specials"] = P("(") + P(")") +
167 P("<") + P(">") +
168 P("[") + P("]") +
169 P(":") + P(";") +
170 P("@") + P("\\") +
171 P(",") + P(".") +
172 P("\""), -- RFC 2822 Section 3.2.1
173 -- a quoted pair should return only the second character
174 ["quoted-pair"] = (P("\\")/'' * V("text")) + V("obs-qp"), -- RFC 2822 Section 3.2.2
175 -- a folding white space (a whitespace that can include a CRLF)
176 -- should be substituted by a single whitespace
177 ["FWS"] = (((V("WSP")^0 * V("CRLF"))^-1 * V("WSP")^1) + V("obs-FWS")) / " ", -- RFC 2822 Section 3.2.3
178 -- a text character allowed inside a comment
179 ["ctext"] = V("NO-WS-CTL") +
180 R("\33\39") +
181 R("\42\91") +
182 R("\93\126"), -- RFC 2822 Section 3.2.3
183 -- the content of a comment (comments can nest)
184 ["ccontent"] = V("ctext") + V("quoted-pair") + V("comment"), -- RFC 2822 Section 3.2.3
185 -- an actual comment
186 -- should be substituted by a single whitespace
187 ["comment"] = (P("(") * (V("FWS")^-1 * V("ccontent"))^0 * V("FWS")^-1 * P(")")) / " ", -- RFC 2822 Section 3.2.3
188 -- a comment or a folding white space
189 -- should be substituted by a single whitespace
191 -- Folding white spaces should not be placed in a way that
192 -- creates lines containing only whitespaces.
193 -- This requirement Is not necessarily enforced by this grammar
194 ["CFWS"] = ( (V("FWS") + (V("comment")*V("FWS")^-1)) * (V("comment")*V("FWS")^-1)^0 ) / " ", -- RFC 2822 Section 3.2.3
195 -- character that can appear in an atom
196 ["atext"] = V("ALPHA") + V("DIGIT") +
197 P("!") + P("#") +
198 P("$") + P("%") +
199 P("&") + P("'") +
200 P("*") + P("+") +
201 P("-") + P("/") +
202 P("=") + P("?") +
203 P("^") + P("_") +
204 P("`") + P("{") +
205 P("|") + P("}") +
206 P("~"), -- RFC 2822 Section 3.2.4
207 -- an atom is equal to the content only discarding comments and whitespace
208 ["atom"] = V("CFWS")^-1 * C(V("atext")^1) * V("CFWS")^-1, -- RFC 2822 Section 3.2.4
209 -- an atom with dots is only the content, discarding CFWSs
210 ["dot-atom"] = V("CFWS")^-1 * C(V("dot-atom-text")) * V("CFWS")^-1, -- RFC 2822 Section 3.2.4
211 -- the content of an atom text with dots
212 ["dot-atom-text"] = V("atext")^1 * (P(".") * V("atext")^1)^0, -- RFC 2822 Section 3.2.4
213 -- character that can appear in a quoted string
214 ["qtext"] = V("NO-WS-CTL") +
215 P("\33") +
216 R("\35\91") +
217 R("\93\126"), -- RFC 2822 Section 3.2.5
218 -- character or quoted pair (both can appear in a quoted string)
219 -- it is equivalent to the character itself or to the result of the
220 -- quoted pair
221 ["qcontent"] = C(V("qtext")) + V("quoted-pair"), -- RFC 2822 Section 3.2.5
222 -- a quoted string is equal to its content
223 ["quoted-string"] = V("CFWS")^-1 *
224 P("\"") * (V("FWS")^-1 * V("qcontent"))^0 * V("FWS")^-1 * P("\"") * V("CFWS")^-1, -- RFC 2822 Section 3.2.5
225 -- unstructured patterns for unspecified headers
226 -- what should these be equal to?
228 -- an generic word
229 ["word"] = V("atom") + V("quoted-string"), -- RFC 2822 Section 3.2.6
230 -- an generic phrase
231 ["phrase"] = Cs(V("word")^1) + V("obs-phrase"), -- RFC 2822 Section 3.2.6
232 -- a character for unstructured text
233 ["utext"] = V("NO-WS-CTL") + R("\33\126") + V("obs-utext"), -- RFC 2822 Section 3.2.6
234 -- an unstructured text
235 ["unstructured"] = (V("FWS")^-1 * V("utext"))^0 * V("FWS")^-1, -- RFC 2822 Section 3.2.6
239 local show = my.show
240 local deb = function(...) for i=1,select('#',...) do local v=select(i,...)print(type(v), v)end end
242 local date_time = function ()
243 setfenv(1, lpeg)
244 return {
245 -- date and time specification
246 -- dates and times should be valid
247 -- this grammar does not enforce this yet
248 ["date-time"] = Cst(( V"day-of-week" * P"," )^-1 * V"date" * V"FWS" * V"time" * V"CFWS"^-1), -- RFC 2822 Section 3.3
249 ["day-of-week"] = ( V"FWS"^-1 * V"day-name" ) + V"obs-day-of-week", -- RFC 2822 Section 3.3
250 ["day-name"] = Cg(P"Mon" + P"Tue" + P"Wed" + P"Thu" + P"Fri" + P"Sat" + P"Sun", "weekday"), -- RFC 2822 Section 3.3
251 ["date"] = V"day" * V"month" * V"year", -- RFC 2822 Section 3.3
252 ["year"] = Cg(C(V"DIGIT"^4), "year") + V"obs-year", -- RFC 2822 Section 3.3
253 ["month"] = (V"FWS" * V"month-name" * V"FWS") + V"obs-month", -- RFC 2822 Section 3.3
254 ["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
255 ["day"] = (V"FWS"^-1 * Cg(C(V"DIGIT" * V"DIGIT"^-1), "day")) + V"obs-day", -- RFC 2822 Section 3.3
256 ["time"] = V"time-of-day" * V"FWS" * V"zone", -- RFC 2822 Section 3.3
257 ["time-of-day"] = V"hour" * P":" * V"minute" * (P":" * V"second")^-1, -- RFC 2822 Section 3.3
258 ["hour"] = Cg(C(V"DIGIT" * V"DIGIT"), "hour") + V"obs-hour", -- RFC 2822 Section 3.3
259 ["minute"] = Cg((V"DIGIT" * V"DIGIT"), "minute") + V"obs-minute", -- RFC 2822 Section 3.3
260 ["second"] = Cg((V"DIGIT" * V"DIGIT"), "second") + V"obs-second", -- RFC 2822 Section 3.3
261 ["zone"] = Cg( (P"+" + P"-") * V"DIGIT" * V"DIGIT" * V"DIGIT" * V"DIGIT", "zone" ) + V"obs-zone", -- RFC 2822 Section 3.3
265 local address = function ()
266 setfenv(1, lpeg)
267 return {
268 -- address specification
269 -- dates and times should be valid
270 -- this grammar does not enforce this yet
271 ["address"] = V"mailbox" + V"group", -- RFC 2822 Section 3.4
272 ["mailbox"] = V"name-addr" + V"addr-spec", -- RFC 2822 Section 3.4
273 ["name-addr"] = V"display-name"^-1 * V"angle-addr", -- RFC 2822 Section 3.4
274 ["angle-addr"] = (V"CFWS"^-1 * P"<" * V"addr-spec" * P">" * V"CFWS"^-1) + V"obs-angle-addr", -- RFC 2822 Section 3.4
275 ["group"] = V"display-name" * P":" * (V"mailbox-list" + V"CFWS") * P";" * V"CFWS"^-1, -- RFC 2822 Section 3.4
276 ["display-name"] = V"phrase", -- RFC 2822 Section 3.4
277 ["mailbox-list"] = (V"mailbox" * (P"," * V"mailbox")^0) + V"obs-mailbox-list", -- RFC 2822 Section 3.4
278 ["address-list"] = (V"address" * (P"," * V"address")^0) + V"obs-address-list", -- RFC 2822 Section 3.4
279 -- address specification (name@host.domain)
280 ["addr-spec"] = Cst(V"local-part" * P"@" * V"domain"), -- RFC 2822 Section 3.4.1
281 ["local-part"] = Csg(V"dot-atom" + V"quoted-string" + V"obs-local-part", "box"), -- RFC 2822 Section 3.4.1
282 ["domain"] = Cg(V"dot-atom" + V"domain-literal" + V"obs-domain", "domain"), -- RFC 2822 Section 3.4.1
283 ["domain-literal"] = Cs(V"CFWS"^-1 * P"[" * (V"FWS"^-1 * V"dcontent")^0 * V"FWS"^-1 * P"]" * V"CFWS"^-1), -- RFC 2822 Section 3.4.1
284 ["dcontent"] = V"dtext" + V"quoted-pair", -- RFC 2822 Section 3.4.1
285 ["dtext"] = V"NO-WS-CTL" + R"\33\90" + R"\94\126", -- RFC 2822 Section 3.4.1
289 local overall_message = function ()
290 setfenv(1, lpeg)
291 return {
292 -- overall message specification
294 -- RRC 2822 Section 3.5:
296 -- A message consists of header fields, optionally followed by a message
297 -- body. Lines in a message MUST be a maximum of 998 characters
298 -- excluding the CRLF, but it is RECOMMENDED that lines be limited to 78
299 -- characters excluding the CRLF.
301 -- this grammar does not enforce the 78 chars limit yet
302 ["message"] = (V"fields" + V"obs-fields") * (V"CRLF" * V"body")^-1, -- RFC 2822 Section 3.5
303 ["body"] = (V"text"^-998 * V"CRLF")^0 * V"text"^-998, -- RFC 2822 Section 3.5
307 local fields = function ()
308 setfenv(1, lpeg)
309 return {
310 -- spec for all fields in the header
312 -- RRC 2822 Section 3.6
314 --the "fields" rule is incomplete
315 ["fields"] = -- trace fields missing
316 (V"orig-date" + V"from" + V"sender" + V"reply-to" + V"to" + V"cc" + V"bcc"
317 + V"message-id" + V"in-reply-to" + V"to" + V"cc" + V"bcc")^0, -- RFC 2822 Section 3.6
318 -- origination date field: the time at which the author decided
319 -- the message was ready to be sent (e.g. hit the button)
320 ["orig-date"] = P"Date:" * V"date-time" * V"CRLF", -- RFC 2822 Section 3.6.1
321 -- the author of the message
322 ["from"] = P"From:" * V"mailbox-list" * V"CRLF", -- RFC 2822 Section 3.6.2
323 -- the actual agent that sent the message
324 ["sender"] = P"Sender:" * V"mailbox" * V"CRLF", -- RFC 2822 Section 3.6.2
325 -- address which should be replied to
326 -- see Section 3.6.3 for forming replies
327 ["reply-to"] = P"Reply-To:" * V"address-list" * V"CRLF", -- RFC 2822 Section 3.6.2
328 -- The "To:" field contains the address(es) of the primary recipient(s)
329 -- of the message.
330 ["to"] = P"To:" * V"address-list" * V"CRLF", -- RFC 2822 Section 3.6.3
331 -- The "Cc:" field contains the addresses of others who are to receive
332 -- the message, though the content of the message may not be directed
333 -- at them.
334 ["cc"] = P"Cc:" * V"address-list" * V"CRLF", -- RFC 2822 Section 3.6.3
335 -- The "Bcc:" field contains addresses of recipients of the message
336 -- whose addresses are not to be revealed to other recipients of the
337 -- message.
338 ["bcc"] = P"Bcc:" * (V"address-list" + V"CFWS") * V"CRLF", -- RFC 2822 Section 3.6.3
339 ["message-id"] = P"Message-Id:" * V"msg-id" * V"CRLF", -- RFC 2822 Section 3.6.4
340 ["in-reply-to"] = P"In-Reply-To:" * V"msg-id"^1 * V"CRLF", -- RFC 2822 Section 3.6.4
341 ["references"] = P"References:" * V"msg-id"^1 * V"CRLF", -- RFC 2822 Section 3.6.4
342 ["msg-id"] = V"CFWS"^-1 * P"<" * V"id-left" * P"@" * V"id-right" * P">" * V"CFWS"^-1, -- RFC 2822 Section 3.6.4
343 ["id-left"] = V"dot-atom-text" + V"no-fold-quote" + V"obs-id-left", -- RFC 2822 Section 3.6.4
344 ["id-right"] = V"dot-atom-text" + V"no-fold-literal" + V"obs-id-right", -- RFC 2822 Section 3.6.4
345 ["no-fold-quote"] = P"\"" * (V"qtext" + V"quoted-pair")^0 * P"\"", -- RFC 2822 Section 3.6.4
346 ["no-fold-literal"] = P"[" * (V"dtext" + V"quoted-pair")^0 * P"]", -- RFC 2822 Section 3.6.4
350 local obs_strict = {
351 ["obs-FWS"] = lpeg.P(false),
352 ["obs-qp"] = lpeg.P(false),
353 ["obs-phrase"] = lpeg.P(false),
354 ["obs-utext"] = lpeg.P(false),
355 ["obs-day-of-week"] = lpeg.P(false),
356 ["obs-year"] = lpeg.P(false),
357 ["obs-month"] = lpeg.P(false),
358 ["obs-day"] = lpeg.P(false),
359 ["obs-hour"] = lpeg.P(false),
360 ["obs-minute"] = lpeg.P(false),
361 ["obs-second"] = lpeg.P(false),
362 ["obs-zone"] = lpeg.P(false),
363 ["obs-angle-addr"] = lpeg.P(false),
364 ["obs-mailbox-list"] = lpeg.P(false),
365 ["obs-address-list"] = lpeg.P(false),
366 ["obs-local-part"] = lpeg.P(false),
367 ["obs-domain"] = lpeg.P(false),
368 ["obs-fields"] = lpeg.P(false),
369 ["obs-id-left"] = lpeg.P(false),
370 ["obs-id-right"] = lpeg.P(false),
373 local os = setmetatable({}, {__index = function() return lpeg.P(false) end})
375 local join_set = function (...)
376 local n = select('#', ...)
377 local ret = {}
378 for i = 1, n do
379 local t = select(i, ...)
380 if type(t)=='table' then
381 for k, v in pairs(t) do
382 ret[k] = v
384 elseif type(t)=='string' then
385 ret[1] = t
386 elseif type(t)=='boolean' then
387 -- TODO: check no overwrite
388 else
389 error('join_set: bad argument number '..i..' of type '..type(t))
392 return ret
395 local gr = join_set(core_values, lex_tokens(), date_time(), address(), overall_message(), fields(), obs_strict)
397 return gr