| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
local base = _G |
| 11 |
local ltn12 = ltn12 or require("ltn12") |
| 12 |
local mime = require("mime.core") |
| 13 |
local io = require("io") |
| 14 |
local string = require("string") |
| 15 |
local _M = mime |
| 16 |
|
| 17 |
|
| 18 |
local encodet, decodet, wrapt = {},{},{} |
| 19 |
|
| 20 |
_M.encodet = encodet |
| 21 |
_M.decodet = decodet |
| 22 |
_M.wrapt = wrapt |
| 23 |
|
| 24 |
|
| 25 |
local function choose(table) |
| 26 |
return function(name, opt1, opt2) |
| 27 |
if base.type(name) ~= "string" then |
| 28 |
name, opt1, opt2 = "default", name, opt1 |
| 29 |
end |
| 30 |
local f = table[name or "nil"] |
| 31 |
if not f then |
| 32 |
base.error("unknown key (" .. base.tostring(name) .. ")", 3) |
| 33 |
else return f(opt1, opt2) end |
| 34 |
end |
| 35 |
end |
| 36 |
|
| 37 |
|
| 38 |
encodet['base64'] = function() |
| 39 |
return ltn12.filter.cycle(_M.b64, "") |
| 40 |
end |
| 41 |
|
| 42 |
encodet['quoted-printable'] = function(mode) |
| 43 |
return ltn12.filter.cycle(_M.qp, "", |
| 44 |
(mode == "binary") and "=0D=0A" or "\r\n") |
| 45 |
end |
| 46 |
|
| 47 |
|
| 48 |
decodet['base64'] = function() |
| 49 |
return ltn12.filter.cycle(_M.unb64, "") |
| 50 |
end |
| 51 |
|
| 52 |
decodet['quoted-printable'] = function() |
| 53 |
return ltn12.filter.cycle(_M.unqp, "") |
| 54 |
end |
| 55 |
|
| 56 |
local function format(chunk) |
| 57 |
if chunk then |
| 58 |
if chunk == "" then return "''" |
| 59 |
else return string.len(chunk) end |
| 60 |
else return "nil" end |
| 61 |
end |
| 62 |
|
| 63 |
|
| 64 |
wrapt['text'] = function(length) |
| 65 |
length = length or 76 |
| 66 |
return ltn12.filter.cycle(_M.wrp, length, length) |
| 67 |
end |
| 68 |
wrapt['base64'] = wrapt['text'] |
| 69 |
wrapt['default'] = wrapt['text'] |
| 70 |
|
| 71 |
wrapt['quoted-printable'] = function() |
| 72 |
return ltn12.filter.cycle(_M.qpwrp, 76, 76) |
| 73 |
end |
| 74 |
|
| 75 |
|
| 76 |
_M.encode = choose(encodet) |
| 77 |
_M.decode = choose(decodet) |
| 78 |
_M.wrap = choose(wrapt) |
| 79 |
|
| 80 |
|
| 81 |
function _M.normalize(marker) |
| 82 |
return ltn12.filter.cycle(_M.eol, 0, marker) |
| 83 |
end |
| 84 |
|
| 85 |
|
| 86 |
function _M.stuff() |
| 87 |
return ltn12.filter.cycle(_M.dot, 2) |
| 88 |
end |
| 89 |
_G['mime'] = mime |
| 90 |
return _M |