summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/lua-addons/addons/libs/mime.lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-15 13:53:59 +0800
committerchai <chaifix@163.com>2021-11-15 13:53:59 +0800
commit942a030afd348ab2e02eac8054b43e3c3a72ea48 (patch)
treea13459f39a3d2f1b533fbd1b5ab523d7a621f673 /Data/BuiltIn/Libraries/lua-addons/addons/libs/mime.lua
parente307051a56a54c27f10438fd2025edf61d0dfeed (diff)
*rename
Diffstat (limited to 'Data/BuiltIn/Libraries/lua-addons/addons/libs/mime.lua')
-rw-r--r--Data/BuiltIn/Libraries/lua-addons/addons/libs/mime.lua90
1 files changed, 90 insertions, 0 deletions
diff --git a/Data/BuiltIn/Libraries/lua-addons/addons/libs/mime.lua b/Data/BuiltIn/Libraries/lua-addons/addons/libs/mime.lua
new file mode 100644
index 0000000..642cd9c
--- /dev/null
+++ b/Data/BuiltIn/Libraries/lua-addons/addons/libs/mime.lua
@@ -0,0 +1,90 @@
+-----------------------------------------------------------------------------
+-- MIME support for the Lua language.
+-- Author: Diego Nehab
+-- Conforming to RFCs 2045-2049
+-----------------------------------------------------------------------------
+
+-----------------------------------------------------------------------------
+-- Declare module and import dependencies
+-----------------------------------------------------------------------------
+local base = _G
+local ltn12 = require("ltn12")
+local mime = require("mime.core")
+local io = require("io")
+local string = require("string")
+local _M = mime
+
+-- 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
+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
+encodet['base64'] = function()
+ return ltn12.filter.cycle(_M.b64, "")
+end
+
+encodet['quoted-printable'] = function(mode)
+ return ltn12.filter.cycle(_M.qp, "",
+ (mode == "binary") and "=0D=0A" or "\r\n")
+end
+
+-- define the decoding filters
+decodet['base64'] = function()
+ return ltn12.filter.cycle(_M.unb64, "")
+end
+
+decodet['quoted-printable'] = function()
+ return ltn12.filter.cycle(_M.unqp, "")
+end
+
+local function format(chunk)
+ if chunk then
+ if chunk == "" then return "''"
+ else return string.len(chunk) end
+ else return "nil" end
+end
+
+-- define the line-wrap filters
+wrapt['text'] = function(length)
+ length = length or 76
+ return ltn12.filter.cycle(_M.wrp, length, length)
+end
+wrapt['base64'] = wrapt['text']
+wrapt['default'] = wrapt['text']
+
+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
+
+-- high level stuffing filter
+function _M.stuff()
+ return ltn12.filter.cycle(_M.dot, 2)
+end
+
+return _M \ No newline at end of file