1 -----------------------------------------------------------------------------
2 -- MIME support for the Lua language.
4 -- Conforming to RFCs 2045-2049
5 -----------------------------------------------------------------------------
7 -----------------------------------------------------------------------------
8 -- Declare module and import dependencies
9 -----------------------------------------------------------------------------
11 local ltn12
= require("ltn12")
12 local mime
= require("mime.core")
13 local string = require("string")
16 -- encode, decode and wrap algorithm tables
21 -- creates a function that chooses a filter by name from a given table
22 local function choose(table)
23 return function(name
, opt1
, opt2
)
24 if base
.type(name
) ~= "string" then
25 name
, opt1
, opt2
= "default", name
, opt1
27 local f
= table[name
or "nil"]
29 base
.error("unknown key (" .. base
.tostring(name
) .. ")", 3)
30 else return f(opt1
, opt2
) end
34 -- define the encoding filters
35 encodet
['base64'] = function()
36 return ltn12
.filter
.cycle(b64
, "")
39 encodet
['quoted-printable'] = function(mode
)
40 return ltn12
.filter
.cycle(qp
, "",
41 (mode
== "binary") and "=0D=0A" or "\r\n")
44 -- define the decoding filters
45 decodet
['base64'] = function()
46 return ltn12
.filter
.cycle(unb64
, "")
49 decodet
['quoted-printable'] = function()
50 return ltn12
.filter
.cycle(unqp
, "")
53 local function format(chunk
)
55 if chunk
== "" then return "''"
56 else return string.len(chunk
) end
60 -- define the line-wrap filters
61 wrapt
['text'] = function(length
)
63 return ltn12
.filter
.cycle(wrp
, length
, length
)
65 wrapt
['base64'] = wrapt
['text']
66 wrapt
['default'] = wrapt
['text']
68 wrapt
['quoted-printable'] = function()
69 return ltn12
.filter
.cycle(qpwrp
, 76, 76)
72 -- function that choose the encoding, decoding or wrap algorithm
73 encode
= choose(encodet
)
74 decode
= choose(decodet
)
77 -- define the end-of-line normalization filter
78 function normalize(marker
)
79 return ltn12
.filter
.cycle(eol
, 0, marker
)
82 -- high level stuffing filter
84 return ltn12
.filter
.cycle(dot
, 2)