util-soc-imp-mime.lua /size: 2373 b    last modification: 2020-07-01 14:35
1-- original file : mime.lua
2-- for more into : see util-soc.lua
3
4local type, tostring = type, tostring
5
6local mime  = mime  or package.loaded.mime  or require("mime.core")
7local ltn12 = ltn12 or package.loaded.ltn12 or require("ltn12")
8
9local filtercycle = ltn12.filter.cycle
10
11local function report(fmt,first,...)
12    if logs then
13        report = logs and logs.reporter("mime")
14        report(fmt,first,...)
15    elseif fmt then
16        fmt = "mime: " .. fmt
17        if first then
18            print(format(fmt,first,...))
19        else
20            print(fmt)
21        end
22    end
23end
24
25mime.report = report
26
27local encodet     = { }
28local decodet     = { }
29local wrapt       = { }
30
31mime.encodet      = encodet
32mime.decodet      = decodet
33mime.wrapt        = wrapt
34
35local mime_b64    = mime.b64
36local mime_qp     = mime.qp
37local mime_unb64  = mime.unb64
38local mime_unqp   = mime.unqp
39local mime_wrp    = mime.wrp
40local mime_qpwrp  = mime.qpwrp
41local mime_eol    = mime_eol
42local mime_dot    = mime_dot
43
44encodet['base64'] = function()
45    return filtercycle(mime_b64,"")
46end
47
48encodet['quoted-printable'] = function(mode)
49    return filtercycle(mime_qp, "", mode == "binary" and "=0D=0A" or "\r\n")
50end
51
52decodet['base64'] = function()
53    return filtercycle(mime_unb64, "")
54end
55
56decodet['quoted-printable'] = function()
57    return filtercycle(mime_unqp, "")
58end
59
60local wraptext = function(length)
61    if not length then
62        length = 76
63    end
64    return filtercycle(mime_wrp, length, length)
65end
66
67local wrapquoted = function()
68    return filtercycle(mime_qpwrp, 76, 76)
69end
70
71wrapt['text']             = wraptext
72wrapt['base64']           = wraptext
73wrapt['default']          = wraptext
74wrapt['quoted-printable'] = wrapquoted
75
76function mime.normalize(marker)
77    return filtercycle(mime_eol, 0, marker)
78end
79
80function mime.stuff()
81    return filtercycle(mime_dot, 2)
82end
83
84local function choose(list)
85    return function(name, opt1, opt2)
86        if type(name) ~= "string" then
87            name, opt1, opt2 = "default", name, opt1
88        end
89        local filter = list[name or "nil"]
90        if filter then
91            return filter(opt1, opt2)
92        else
93            report("error: unknown key '%s'",tostring(name))
94        end
95    end
96end
97
98mime.encode = choose(encodet)
99mime.decode = choose(decodet)
100mime.wrap   = choose(wrapt)
101
102package.loaded["mime"] = mime
103
104return mime
105