5 -- MIME implementation in Lua
12 local tostring = tostring
14 local setfenv
= setfenv
19 local join
= function (...)
21 for i
= 1, select('#', ...) do
22 ret
= ret
.. tostring(select(i
, ...))
27 -- this collects both the table of named captures and the
28 -- substitution captureof the given pattern
29 lpeg
.Cst
= function (patt
)
30 return (#lpeg
.Cs(patt
) * lpeg
.Ct(patt
)) / function (s
, t
)
33 for k
, v
in pairs(t
) do
34 if type(k
)~='number' then
42 -- rfc2822 Section 2.1:
44 -- A message consists of header fields (collectively called "the header
45 -- of the message") followed, optionally, by a body. The header is a
46 -- sequence of lines of characters with special syntax as defined in
47 -- this standard. The body is simply a sequence of characters that
48 -- follows the header and is separated from the header by an empty line
49 -- (i.e., a line with nothing preceding the CRLF).
51 local split_content
= function (c
)
52 -- NOTE: there is no guarantee that the CRLF separating header
53 -- and body is the first one. However no headers admit a blank
55 local h
, b
= string.match(c
, '^(.-)'..CRLF
..CRLF
..'(.*)$')
56 assert(type(h
)=='string' and type(b
)=='string')
60 -- rfc2822 Section 2.2.3
62 -- The process of moving from this folded multiple-line representation
63 -- of a header field to its single line representation is called
64 -- "unfolding". Unfolding is accomplished by simply removing any CRLF
65 -- that is immediately followed by WSP. Each header field should be
66 -- treated in its unfolded form for further syntactic and semantic
69 local unfold_header
= function (h
)
70 return string.gsub(h
, CRLF
..'[ \t]', ' ')
73 -- rfc2822 Section 2.1
75 -- At the most basic level, a message is a series of characters. A
76 -- message that is conformant with this standard is comprised of
77 -- characters with values in the range 1 through 127 and interpreted as
78 -- US-ASCII characters [ASCII]. For brevity, this document sometimes
79 -- refers to this range of characters as simply "US-ASCII characters".
82 -- rfc2822 Section 2.1.1
84 -- There are two limits that this standard places on the number of
85 -- characters in a line. Each line of characters MUST be no more than
86 -- 998 characters, and SHOULD be no more than 78 characters, excluding
89 local check_content
= function (c
)
91 -- TODO: implement in a portable way
95 -- rfc2822 Section 2.3
97 -- The body of a message is simply lines of US-ASCII characters. The
98 -- only two limitations on the body are as follows:
100 -- - CR and LF MUST only occur together as CRLF; they MUST NOT appear
101 -- independently in the body.
103 -- - Lines of characters in the body MUST be limited to 998 characters,
105 -- and SHOULD be limited to 78 characters, excluding the CRLF.
107 local check_body
= function (b
)
113 -- some core values (not implemented literally)
114 -- see RFC 2234 Section 6.1
115 local core_values
= {
116 ["ALPHA"] = lpeg
.R("AZ", "az"),
117 ["BIT"] = lpeg
.P("0") + lpeg
.P("1"),
118 ["CHAR"] = lpeg
.R("\01\127"),
119 ["CR"] = lpeg
.P("\13"),
120 ["CRLF"] = lpeg
.P("\13\10"),
121 ["DIGIT"] = lpeg
.R("09"),
122 ["LF"] = lpeg
.P("\10"),
123 ["WSP"] = lpeg
.S("\32\09"),
126 -- lexical tokens used in the specification
127 -- TODO: could be optimized
128 -- check for non-obfuscated optimizations
129 -- TODO: write in a less obfuscated way
130 -- check if parenthesis can be removed
131 local lex_tokens
= function ()
134 -- control characters without whitespaces
135 ["NO-WS-CTL"] = R("\1\8") +
139 P("\127"), -- RFC 2822 Section 3.2.1
140 -- a character in a text
141 ["text"] = R("\1\9") +
144 R("\14\127"), -- RFC 2822 Section 3.2.1
145 -- a special character
146 ["specials"] = P("(") + P(")") +
152 P("\""), -- RFC 2822 Section 3.2.1
153 -- a quoted pair should return only the second character
154 ["quoted-pair"] = (P("\\") * C(V("text"))) + V("obs-qp"), -- RFC 2822 Section 3.2.2
155 -- a folding white space (a whitespace that can include a CRLF)
156 -- should be substituted by a single whitespace
157 ["FWS"] = (((V("WSP")^
0 * V("CRLF"))^
-1 * V("WSP")^
1) + V("obs-FWS")) / " ", -- RFC 2822 Section 3.2.3
158 -- a text character allowed inside a comment
159 ["ctext"] = V("NO-WS-CTL") +
162 R("\93\126"), -- RFC 2822 Section 3.2.3
163 -- the content of a comment (comments can nest)
164 ["ccontent"] = V("ctext") + V("quoted-pair") + V("comment"), -- RFC 2822 Section 3.2.3
166 -- should be substituted by a single whitespace
167 ["comment"] = (P("(") * (V("FWS")^
-1 * V("ccontent"))^
0 * V("FWS")^
-1 * P(")")) / " ", -- RFC 2822 Section 3.2.3
168 -- a comment or a folding white space
169 -- should be substituted by a single whitespace
171 -- Folding white spaces should not be placed in a way that
172 -- creates lines containing only whitespaces.
173 -- This requirement Is not necessarily enforced by this grammar
174 ["CFWS"] = ( (V("FWS") + (V("comment")*V("FWS")^
-1)) * (V("comment")*V("FWS")^
-1)^
0 ) / " ", -- RFC 2822 Section 3.2.3
175 -- character that can appear in an atom
176 ["atext"] = V("ALPHA") + V("DIGIT") +
186 P("~"), -- RFC 2822 Section 3.2.4
187 -- an atom is equal to the content only discarding comments and whitespace
188 ["atom"] = V("CFWS")^
-1 * C(V("atext")^
1) * V("CFWS")^
-1, -- RFC 2822 Section 3.2.4
189 -- an atom with dots is only the content, discarding CFWSs
190 ["dot-atom"] = Cs((V("CFWS")^
-1/'') * C(V("dot-atom-text")) * (V("CFWS")^
-1/'')), -- RFC 2822 Section 3.2.4
191 -- the content of an atom text with dots
192 ["dot-atom-text"] = V("atext")^
1 * (P(".") * V("atext")^
1)^
0, -- RFC 2822 Section 3.2.4
193 -- character that can appear in a quoted string
194 ["qtext"] = V("NO-WS-CTL") +
197 R("\93\126"), -- RFC 2822 Section 3.2.5
198 -- character or quoted pair (both can appear in a quoted string)
199 -- it is equivalent to the character itself or to the result of the
201 ["qcontent"] = C(V("qtext")) + V("quoted-pair"), -- RFC 2822 Section 3.2.5
202 -- a quoted string is equal to its content
203 ["quoted-string"] = V("CFWS")^
-1 *
204 P("\"") * Cs((V("FWS")^
-1 * V("qcontent"))^
0 * V("FWS")^
-1) * P("\"") * V("CFWS")^
-1, -- RFC 2822 Section 3.2.5
205 -- unstructured patterns for unspecified headers
206 -- what should these be equal to?
209 ["word"] = V("atom") + V("quoted-string"), -- RFC 2822 Section 3.2.6
211 ["phrase"] = V("word")^
1 + V("obs-phrase"), -- RFC 2822 Section 3.2.6
212 -- a character for unstructured text
213 ["utext"] = V("NO-WS-CTL") + R("\33\126") + V("obs-utext"), -- RFC 2822 Section 3.2.6
214 -- an unstructured text
215 ["unstructured"] = (V("FWS")^
-1 * V("utext"))^
0 * V("FWS")^
-1, -- RFC 2822 Section 3.2.6
220 local deb
= function(...) for i
=1,select('#',...) do local v
=select(i
,...)print(type(v
), v
)end end
222 local date_time
= function ()
225 -- date and time specification
226 -- dates and times should be valid
227 -- this grammar does not enforce this yet
228 ["date-time"] = Cst(( V
"day-of-week" * P
"," )^
-1 * V
"date" * V
"FWS" * V
"time" * V
"CFWS"^
-1), -- RFC 2822 Section 3.3
229 ["day-of-week"] = ( V
"FWS"^
-1 * V
"day-name" ) + V
"obs-day-of-week", -- RFC 2822 Section 3.3
230 ["day-name"] = Cg(P
"Mon" + P
"Tue" + P
"Wed" + P
"Thu" + P
"Fri" + P
"Sat" + P
"Sun", "weekday"), -- RFC 2822 Section 3.3
231 ["date"] = V
"day" * V
"month" * V
"year", -- RFC 2822 Section 3.3
232 ["year"] = Cg(C(V
"DIGIT"^
4), "year") + V
"obs-year", -- RFC 2822 Section 3.3
233 ["month"] = (V
"FWS" * V
"month-name" * V
"FWS") + V
"obs-month", -- RFC 2822 Section 3.3
234 ["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
235 ["day"] = (V
"FWS"^
-1 * Cg(C(V
"DIGIT" * V
"DIGIT"^
-1), "day")) + V
"obs-day", -- RFC 2822 Section 3.3
236 ["time"] = V
"time-of-day" * V
"FWS" * V
"zone", -- RFC 2822 Section 3.3
237 ["time-of-day"] = V
"hour" * P
":" * V
"minute" * (P
":" * V
"second")^
-1, -- RFC 2822 Section 3.3
238 ["hour"] = Cg(C(V
"DIGIT" * V
"DIGIT"), "hour") + V
"obs-hour", -- RFC 2822 Section 3.3
239 ["minute"] = Cg((V
"DIGIT" * V
"DIGIT"), "minute") + V
"obs-minute", -- RFC 2822 Section 3.3
240 ["second"] = Cg((V
"DIGIT" * V
"DIGIT"), "second") + V
"obs-second", -- RFC 2822 Section 3.3
241 ["zone"] = Cg( (P
"+" + P
"-") * V
"DIGIT" * V
"DIGIT" * V
"DIGIT" * V
"DIGIT", "zone" ) + V
"obs-zone", -- RFC 2822 Section 3.3
245 local address
= function ()
248 -- address specification
249 -- dates and times should be valid
250 -- this grammar does not enforce this yet
251 ["address"] = V
"mailbox" + V
"group", -- RFC 2822 Section 3.4
252 ["mailbox"] = V
"name-addr" + V
"addr-spec", -- RFC 2822 Section 3.4
253 ["name-addr"] = V
"display-name"^
-1 * V
"angle-addr", -- RFC 2822 Section 3.4
254 ["angle-addr"] = (V
"CFWS"^
-1 * P
"<" * V
"addr-spec" * P
">" * V
"CFWS"^
-1) + V
"obs-angle-addr", -- RFC 2822 Section 3.4
255 ["group"] = V
"display-name" * P
":" * (V
"mailbox-list" + V
"CFWS") * P
";" * V
"CFWS"^
-1, -- RFC 2822 Section 3.4
256 ["display-name"] = V
"phrase", -- RFC 2822 Section 3.4
257 ["mailbox-list"] = (V
"mailbox" * (P
"," * V
"mailbox")^
0) + V
"obs-mailbox-list", -- RFC 2822 Section 3.4
258 ["address-list"] = (V
"address" * (P
"," * V
"address")^
0) + V
"obs-address-list", -- RFC 2822 Section 3.4
259 -- address specification (name@host.domain)
260 ["addr-spec"] = V
"local-part" * P
"@" * V
"domain", -- RFC 2822 Section 3.4.1
261 ["local-part"] = V
"dot-atom" + V
"quoted-string" + V
"obs-local-part", -- RFC 2822 Section 3.4.1
262 ["domain"] = V
"dot-atom" + V
"domain-literal" + V
"obs-domain", -- RFC 2822 Section 3.4.1
263 ["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
264 ["dcontent"] = V
"dtext" + V
"quoted-pair", -- RFC 2822 Section 3.4.1
265 ["dtext"] = V
"NO-WS-CTL" + R
"\33\90" + R
"\94\126", -- RFC 2822 Section 3.4.1
269 local overall_message
= function ()
272 -- overall message specification
274 -- RRC 2822 Section 3.5:
276 -- A message consists of header fields, optionally followed by a message
277 -- body. Lines in a message MUST be a maximum of 998 characters
278 -- excluding the CRLF, but it is RECOMMENDED that lines be limited to 78
279 -- characters excluding the CRLF.
281 -- this grammar does not enforce the 78 chars limit yet
282 ["message"] = (V
"fields" + V
"obs-fields") * (V
"CRLF" * V
"body")^
-1, -- RFC 2822 Section 3.5
283 ["body"] = (V
"text"^
-998 * V
"CRLF")^
0 * V
"text"^
-998, -- RFC 2822 Section 3.5
287 local fields
= function ()
290 -- spec for all fields in the header
292 -- RRC 2822 Section 3.6
294 --the "fields" rule is incomplete
295 ["fields"] = -- trace fields missing
296 (V
"orig-date" + V
"from" + V
"sender" + V
"reply-to" + V
"to" + V
"cc" + V
"bcc"
297 + V
"message-id" + V
"in-reply-to" + V
"to" + V
"cc" + V
"bcc")^
0, -- RFC 2822 Section 3.6
298 -- origination date field: the time at which the author decided
299 -- the message was ready to be sent (e.g. hit the button)
300 ["orig-date"] = P
"Date:" * V
"date-time" * V
"CRLF", -- RFC 2822 Section 3.6.1
301 -- the author of the message
302 ["from"] = P
"From:" * V
"mailbox-list" * V
"CRLF", -- RFC 2822 Section 3.6.2
303 -- the actual agent that sent the message
304 ["sender"] = P
"Sender:" * V
"mailbox" * V
"CRLF", -- RFC 2822 Section 3.6.2
305 -- address which should be replied to
306 -- see Section 3.6.3 for forming replies
307 ["reply-to"] = P
"Reply-To:" * V
"address-list" * V
"CRLF", -- RFC 2822 Section 3.6.2
308 -- The "To:" field contains the address(es) of the primary recipient(s)
310 ["to"] = P
"To:" * V
"address-list" * V
"CRLF", -- RFC 2822 Section 3.6.3
311 -- The "Cc:" field contains the addresses of others who are to receive
312 -- the message, though the content of the message may not be directed
314 ["cc"] = P
"Cc:" * V
"address-list" * V
"CRLF", -- RFC 2822 Section 3.6.3
315 -- The "Bcc:" field contains addresses of recipients of the message
316 -- whose addresses are not to be revealed to other recipients of the
318 ["bcc"] = P
"Bcc:" * (V
"address-list" + V
"CFWS") * V
"CRLF", -- RFC 2822 Section 3.6.3
319 ["message-id"] = P
"Message-Id:" * V
"msg-id" * V
"CRLF", -- RFC 2822 Section 3.6.4
320 ["in-reply-to"] = P
"In-Reply-To:" * V
"msg-id"^
1 * V
"CRLF", -- RFC 2822 Section 3.6.4
321 ["references"] = P
"References:" * V
"msg-id"^
1 * V
"CRLF", -- RFC 2822 Section 3.6.4
322 ["msg-id"] = V
"CFWS"^
-1 * P
"<" * V
"id-left" * P
"@" * V
"id-right" * P
">" * V
"CFWS"^
-1, -- RFC 2822 Section 3.6.4
323 ["id-left"] = V
"dot-atom-text" + V
"no-fold-quote" + V
"obs-id-left", -- RFC 2822 Section 3.6.4
324 ["id-right"] = V
"dot-atom-text" + V
"no-fold-literal" + V
"obs-id-right", -- RFC 2822 Section 3.6.4
325 ["no-fold-quote"] = P
"\"" * (V
"qtext" + V
"quoted-pair")^
0 * P
"\"", -- RFC 2822 Section 3.6.4
326 ["no-fold-literal"] = P
"[" * (V
"dtext" + V
"quoted-pair")^
0 * P
"]", -- RFC 2822 Section 3.6.4
331 ["obs-FWS"] = lpeg
.P(false),
332 ["obs-qp"] = lpeg
.P(false),
333 ["obs-phrase"] = lpeg
.P(false),
334 ["obs-utext"] = lpeg
.P(false),
335 ["obs-day-of-week"] = lpeg
.P(false),
336 ["obs-year"] = lpeg
.P(false),
337 ["obs-month"] = lpeg
.P(false),
338 ["obs-day"] = lpeg
.P(false),
339 ["obs-hour"] = lpeg
.P(false),
340 ["obs-minute"] = lpeg
.P(false),
341 ["obs-second"] = lpeg
.P(false),
342 ["obs-zone"] = lpeg
.P(false),
343 ["obs-angle-addr"] = lpeg
.P(false),
344 ["obs-mailbox-list"] = lpeg
.P(false),
345 ["obs-address-list"] = lpeg
.P(false),
346 ["obs-local-part"] = lpeg
.P(false),
347 ["obs-domain"] = lpeg
.P(false),
348 ["obs-fields"] = lpeg
.P(false),
349 ["obs-id-left"] = lpeg
.P(false),
350 ["obs-id-right"] = lpeg
.P(false),
353 local os
= setmetatable({}, {__index
= function() return lpeg
.P(false) end})
355 local join_set
= function (...)
356 local n
= select('#', ...)
359 local t
= select(i
, ...)
360 if type(t
)=='table' then
361 for k
, v
in pairs(t
) do
364 elseif type(t
)=='string' then
366 elseif type(t
)=='boolean' then
367 -- TODO: check no overwrite
369 error('join_set: bad argument number '..i
..' of type '..type(t
))
375 local gr
= join_set(core_values
, lex_tokens(), date_time(), address(), overall_message(), fields(), obs_strict
)