luasocket/src/mime.lua

89 lines
2.4 KiB
Lua
Raw Normal View History

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