summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/util-soc-imp-mime.lua
blob: 7a5cef27b6455c73d3ed399e8020ff068b606c84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
-- original file : mime.lua
-- for more into : see util-soc.lua

local type, tostring = type, tostring

local mime  = mime  or package.loaded.mime  or require("mime.core")
local ltn12 = ltn12 or package.loaded.ltn12 or require("ltn12")

local filtercycle = ltn12.filter.cycle

local function report(fmt,first,...)
    if logs then
        report = logs and logs.reporter("mime")
        report(fmt,first,...)
    elseif fmt then
        fmt = "mime: " .. fmt
        if first then
            print(format(fmt,first,...))
        else
            print(fmt)
        end
    end
end

mime.report = report

local encodet     = { }
local decodet     = { }
local wrapt       = { }

mime.encodet      = encodet
mime.decodet      = decodet
mime.wrapt        = wrapt

local mime_b64    = mime.b64
local mime_qp     = mime.qp
local mime_unb64  = mime.unb64
local mime_unqp   = mime.unqp
local mime_wrp    = mime.wrp
local mime_qpwrp  = mime.qpwrp
local mime_eol    = mime_eol
local mime_dot    = mime_dot

encodet['base64'] = function()
    return filtercycle(mime_b64,"")
end

encodet['quoted-printable'] = function(mode)
    return filtercycle(mime_qp, "", mode == "binary" and "=0D=0A" or "\r\n")
end

decodet['base64'] = function()
    return filtercycle(mime_unb64, "")
end

decodet['quoted-printable'] = function()
    return filtercycle(mime_unqp, "")
end

local wraptext = function(length)
    if not length then
        length = 76
    end
    return filtercycle(mime_wrp, length, length)
end

local wrapquoted = function()
    return filtercycle(mime_qpwrp, 76, 76)
end

wrapt['text']             = wraptext
wrapt['base64']           = wraptext
wrapt['default']          = wraptext
wrapt['quoted-printable'] = wrapquoted

function mime.normalize(marker)
    return filtercycle(mime_eol, 0, marker)
end

function mime.stuff()
    return filtercycle(mime_dot, 2)
end

local function choose(list)
    return function(name, opt1, opt2)
        if type(name) ~= "string" then
            name, opt1, opt2 = "default", name, opt1
        end
        local filter = list[name or "nil"]
        if filter then
            return filter(opt1, opt2)
        else
            report("error: unknown key '%s'",tostring(name))
        end
    end
end

mime.encode = choose(encodet)
mime.decode = choose(decodet)
mime.wrap   = choose(wrapt)

package.loaded["mime"] = mime

return mime