beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / luasocket / src / mime.lua
blob4aaccc8ea4749dd79de32bcbf65fffc218942ef2
1 -----------------------------------------------------------------------------
2 -- MIME support for the Lua language.
3 -- Author: Diego Nehab
4 -- Conforming to RFCs 2045-2049
5 -----------------------------------------------------------------------------
7 -----------------------------------------------------------------------------
8 -- Declare module and import dependencies
9 -----------------------------------------------------------------------------
10 local base = _G
11 local ltn12 = require("ltn12")
12 local mime = require("mime.core")
13 local string = require("string")
14 module("mime")
16 -- encode, decode and wrap algorithm tables
17 encodet = {}
18 decodet = {}
19 wrapt = {}
21 -- creates a function that chooses a filter by name from a given table
22 local function choose(table)
23 return function(name, opt1, opt2)
24 if base.type(name) ~= "string" then
25 name, opt1, opt2 = "default", name, opt1
26 end
27 local f = table[name or "nil"]
28 if not f then
29 base.error("unknown key (" .. base.tostring(name) .. ")", 3)
30 else return f(opt1, opt2) end
31 end
32 end
34 -- define the encoding filters
35 encodet['base64'] = function()
36 return ltn12.filter.cycle(b64, "")
37 end
39 encodet['quoted-printable'] = function(mode)
40 return ltn12.filter.cycle(qp, "",
41 (mode == "binary") and "=0D=0A" or "\r\n")
42 end
44 -- define the decoding filters
45 decodet['base64'] = function()
46 return ltn12.filter.cycle(unb64, "")
47 end
49 decodet['quoted-printable'] = function()
50 return ltn12.filter.cycle(unqp, "")
51 end
53 local function format(chunk)
54 if chunk then
55 if chunk == "" then return "''"
56 else return string.len(chunk) end
57 else return "nil" end
58 end
60 -- define the line-wrap filters
61 wrapt['text'] = function(length)
62 length = length or 76
63 return ltn12.filter.cycle(wrp, length, length)
64 end
65 wrapt['base64'] = wrapt['text']
66 wrapt['default'] = wrapt['text']
68 wrapt['quoted-printable'] = function()
69 return ltn12.filter.cycle(qpwrp, 76, 76)
70 end
72 -- function that choose the encoding, decoding or wrap algorithm
73 encode = choose(encodet)
74 decode = choose(decodet)
75 wrap = choose(wrapt)
77 -- define the end-of-line normalization filter
78 function normalize(marker)
79 return ltn12.filter.cycle(eol, 0, marker)
80 end
82 -- high level stuffing filter
83 function stuff()
84 return ltn12.filter.cycle(dot, 2)
85 end