luasocket/src/mime.lua

67 lines
1.7 KiB
Lua
Raw Normal View History

if not ltn12 then error('Requires LTN12 module') end
-- create mime namespace
mime = mime or {}
-- make all module globals fall into mime namespace
setmetatable(mime, { __index = _G })
setfenv(1, mime)
-- encode, decode and wrap algorithm tables
2004-03-19 06:04:03 +01:00
encodet = {}
decodet = {}
wrapt = {}
-- creates a function that chooses a filter by name from a given table
2004-05-19 09:07:11 +02:00
local function choose(table)
return function(name, opt1, opt2)
if type(name) ~= "string" then
name, opt1, opt2 = "default", name, opt1
end
local f = table[name or "nil"]
if not f then error("unknown key (" .. tostring(name) .. ")", 3)
else return f(opt1, opt2) end
end
end
-- define the encoding filters
2004-03-19 06:04:03 +01:00
encodet['base64'] = function()
return ltn12.filter.cycle(b64, "")
end
2004-03-19 06:04:03 +01:00
encodet['quoted-printable'] = function(mode)
return ltn12.filter.cycle(qp, "",
(mode == "binary") and "=0D=0A" or "\13\10")
end
-- define the decoding filters
2004-03-19 06:04:03 +01:00
decodet['base64'] = function()
return ltn12.filter.cycle(unb64, "")
end
2004-03-19 06:04:03 +01:00
decodet['quoted-printable'] = function()
return ltn12.filter.cycle(unqp, "")
end
-- define the line-wrap filters
2004-03-19 06:04:03 +01:00
wrapt['text'] = function(length)
length = length or 76
return ltn12.filter.cycle(wrp, length, length)
end
2004-03-19 06:04:03 +01:00
wrapt['base64'] = wrapt['text']
wrapt['default'] = wrapt['text']
2004-03-19 06:04:03 +01:00
wrapt['quoted-printable'] = function()
return ltn12.filter.cycle(qpwrp, 76, 76)
end
-- function that choose the encoding, decoding or wrap algorithm
2004-03-19 06:04:03 +01:00
encode = choose(encodet)
decode = choose(decodet)
wrap = choose(wrapt)
-- define the end-of-line normalization filter
function normalize(marker)
return ltn12.filter.cycle(eol, 0, marker)
end
return mime