2004-05-28 08:16:43 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- MIME support for the Lua language.
|
|
|
|
-- Author: Diego Nehab
|
|
|
|
-- Conforming to RFCs 2045-2049
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2004-09-27 06:01:18 +02:00
|
|
|
-- Declare module and import dependencies
|
2004-05-28 08:16:43 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2005-06-14 06:29:23 +02:00
|
|
|
local base = _G
|
2004-06-04 17:15:45 +02:00
|
|
|
local ltn12 = require("ltn12")
|
2005-06-17 06:04:55 +02:00
|
|
|
local mime = require("mime.core")
|
2013-05-27 10:45:09 +02:00
|
|
|
local io = require("io")
|
2005-06-17 06:04:55 +02:00
|
|
|
local string = require("string")
|
2013-05-27 10:45:09 +02:00
|
|
|
local _M = mime
|
2004-05-28 08:16:43 +02:00
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
-- encode, decode and wrap algorithm tables
|
2013-05-27 10:45:09 +02:00
|
|
|
local encodet, decodet, wrapt = {},{},{}
|
|
|
|
|
|
|
|
_M.encodet = encodet
|
|
|
|
_M.decodet = decodet
|
|
|
|
_M.wrapt = wrapt
|
2004-01-19 06:41:30 +01:00
|
|
|
|
2005-11-22 09:33:29 +01:00
|
|
|
-- 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)
|
2004-03-26 07:05:20 +01:00
|
|
|
return function(name, opt1, opt2)
|
2005-11-22 09:33:29 +01:00
|
|
|
if base.type(name) ~= "string" then
|
2004-03-26 07:05:20 +01:00
|
|
|
name, opt1, opt2 = "default", name, opt1
|
|
|
|
end
|
2004-02-11 04:31:53 +01:00
|
|
|
local f = table[name or "nil"]
|
2007-06-12 01:44:54 +02:00
|
|
|
if not f then
|
|
|
|
base.error("unknown key (" .. base.tostring(name) .. ")", 3)
|
2004-03-26 07:05:20 +01:00
|
|
|
else return f(opt1, opt2) end
|
2004-01-19 06:41:30 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2004-02-04 15:29:11 +01:00
|
|
|
-- define the encoding filters
|
2005-06-13 00:02:21 +02:00
|
|
|
encodet['base64'] = function()
|
2013-05-27 10:45:09 +02:00
|
|
|
return ltn12.filter.cycle(_M.b64, "")
|
2004-01-19 06:41:30 +01:00
|
|
|
end
|
|
|
|
|
2005-06-13 00:02:21 +02:00
|
|
|
encodet['quoted-printable'] = function(mode)
|
2013-05-27 10:45:09 +02:00
|
|
|
return ltn12.filter.cycle(_M.qp, "",
|
2004-06-15 08:24:00 +02:00
|
|
|
(mode == "binary") and "=0D=0A" or "\r\n")
|
2004-01-21 02:09:50 +01:00
|
|
|
end
|
|
|
|
|
2004-02-04 15:29:11 +01:00
|
|
|
-- define the decoding filters
|
2005-06-13 00:02:21 +02:00
|
|
|
decodet['base64'] = function()
|
2013-05-27 10:45:09 +02:00
|
|
|
return ltn12.filter.cycle(_M.unb64, "")
|
2004-01-19 06:41:30 +01:00
|
|
|
end
|
|
|
|
|
2005-06-13 00:02:21 +02:00
|
|
|
decodet['quoted-printable'] = function()
|
2013-05-27 10:45:09 +02:00
|
|
|
return ltn12.filter.cycle(_M.unqp, "")
|
2004-01-21 02:09:50 +01:00
|
|
|
end
|
|
|
|
|
2004-11-28 03:36:07 +01:00
|
|
|
local function format(chunk)
|
|
|
|
if chunk then
|
|
|
|
if chunk == "" then return "''"
|
|
|
|
else return string.len(chunk) end
|
|
|
|
else return "nil" end
|
2005-11-22 09:33:29 +01:00
|
|
|
end
|
2004-11-28 03:36:07 +01:00
|
|
|
|
2004-02-04 15:29:11 +01:00
|
|
|
-- define the line-wrap filters
|
2005-06-13 00:02:21 +02:00
|
|
|
wrapt['text'] = function(length)
|
2004-01-19 06:41:30 +01:00
|
|
|
length = length or 76
|
2013-05-27 10:45:09 +02:00
|
|
|
return ltn12.filter.cycle(_M.wrp, length, length)
|
2004-01-19 06:41:30 +01:00
|
|
|
end
|
2005-06-13 00:02:21 +02:00
|
|
|
wrapt['base64'] = wrapt['text']
|
|
|
|
wrapt['default'] = wrapt['text']
|
2004-01-19 06:41:30 +01:00
|
|
|
|
2005-06-13 00:02:21 +02:00
|
|
|
wrapt['quoted-printable'] = function()
|
2013-05-27 10:45:09 +02:00
|
|
|
return ltn12.filter.cycle(_M.qpwrp, 76, 76)
|
2004-02-04 15:29:11 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- function that choose the encoding, decoding or wrap algorithm
|
2013-05-27 10:45:09 +02:00
|
|
|
_M.encode = choose(encodet)
|
|
|
|
_M.decode = choose(decodet)
|
|
|
|
_M.wrap = choose(wrapt)
|
2004-01-19 06:41:30 +01:00
|
|
|
|
2004-02-11 04:31:53 +01:00
|
|
|
-- define the end-of-line normalization filter
|
2013-05-27 10:45:09 +02:00
|
|
|
function _M.normalize(marker)
|
|
|
|
return ltn12.filter.cycle(_M.eol, 0, marker)
|
2004-01-19 06:41:30 +01:00
|
|
|
end
|
2004-06-17 08:23:13 +02:00
|
|
|
|
|
|
|
-- high level stuffing filter
|
2013-05-27 10:45:09 +02:00
|
|
|
function _M.stuff()
|
|
|
|
return ltn12.filter.cycle(_M.dot, 2)
|
2004-06-17 08:23:13 +02:00
|
|
|
end
|
2013-05-27 10:45:09 +02:00
|
|
|
|
|
|
|
return _M
|