luasocket/src/mime.lua

82 lines
2.2 KiB
Lua
Raw Permalink Normal View History

2004-05-27 23:16:43 -07:00
-----------------------------------------------------------------------------
-- MIME support for the Lua language.
-- Author: Diego Nehab
-- Conforming to RFCs 2045-2049
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- Declare module and import dependencies
2004-05-27 23:16:43 -07:00
-----------------------------------------------------------------------------
2005-06-13 21:29:23 -07:00
local base = _G
2004-06-04 08:15:45 -07:00
local ltn12 = require("ltn12")
local mime = require("mime.core")
local _M = mime
2004-05-27 23:16:43 -07:00
-- encode, decode and wrap algorithm tables
local encodet, decodet, wrapt = {},{},{}
_M.encodet = encodet
_M.decodet = decodet
_M.wrapt = wrapt
-- creates a function that chooses a filter by name from a given table
2004-05-19 00:07:11 -07:00
local function choose(table)
return function(name, opt1, opt2)
if base.type(name) ~= "string" then
name, opt1, opt2 = "default", name, opt1
end
local f = table[name or "nil"]
if not f then
base.error("unknown key (" .. base.tostring(name) .. ")", 3)
else return f(opt1, opt2) end
end
end
-- define the encoding filters
2005-06-12 15:02:21 -07:00
encodet['base64'] = function()
return ltn12.filter.cycle(_M.b64, "")
end
2005-06-12 15:02:21 -07:00
encodet['quoted-printable'] = function(mode)
return ltn12.filter.cycle(_M.qp, "",
(mode == "binary") and "=0D=0A" or "\r\n")
end
-- define the decoding filters
2005-06-12 15:02:21 -07:00
decodet['base64'] = function()
return ltn12.filter.cycle(_M.unb64, "")
end
2005-06-12 15:02:21 -07:00
decodet['quoted-printable'] = function()
return ltn12.filter.cycle(_M.unqp, "")
end
-- define the line-wrap filters
2005-06-12 15:02:21 -07:00
wrapt['text'] = function(length)
length = length or 76
return ltn12.filter.cycle(_M.wrp, length, length)
end
2005-06-12 15:02:21 -07:00
wrapt['base64'] = wrapt['text']
wrapt['default'] = wrapt['text']
2005-06-12 15:02:21 -07:00
wrapt['quoted-printable'] = function()
return ltn12.filter.cycle(_M.qpwrp, 76, 76)
end
-- function that choose the encoding, decoding or wrap algorithm
_M.encode = choose(encodet)
_M.decode = choose(decodet)
_M.wrap = choose(wrapt)
-- define the end-of-line normalization filter
function _M.normalize(marker)
return ltn12.filter.cycle(_M.eol, 0, marker)
end
2004-06-16 23:23:13 -07:00
-- high level stuffing filter
function _M.stuff()
return ltn12.filter.cycle(_M.dot, 2)
2004-06-16 23:23:13 -07:00
end
return _M