diff options
Diffstat (limited to 'vendor/luasocket/src/mime.lua')
-rw-r--r-- | vendor/luasocket/src/mime.lua | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/vendor/luasocket/src/mime.lua b/vendor/luasocket/src/mime.lua new file mode 100644 index 00000000..93539de6 --- /dev/null +++ b/vendor/luasocket/src/mime.lua | |||
@@ -0,0 +1,81 @@ | |||
1 | ----------------------------------------------------------------------------- | ||
2 | -- MIME support for the Lua language. | ||
3 | -- Author: Diego Nehab | ||
4 | -- Conforming to RFCs 2045-2049 | ||
5 | ----------------------------------------------------------------------------- | ||
6 | |||
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 _M = mime | ||
14 | |||
15 | -- encode, decode and wrap algorithm tables | ||
16 | local encodet, decodet, wrapt = {},{},{} | ||
17 | |||
18 | _M.encodet = encodet | ||
19 | _M.decodet = decodet | ||
20 | _M.wrapt = wrapt | ||
21 | |||
22 | -- creates a function that chooses a filter by name from a given table | ||
23 | local function choose(table) | ||
24 | return function(name, opt1, opt2) | ||
25 | if base.type(name) ~= "string" then | ||
26 | name, opt1, opt2 = "default", name, opt1 | ||
27 | end | ||
28 | local f = table[name or "nil"] | ||
29 | if not f then | ||
30 | base.error("unknown key (" .. base.tostring(name) .. ")", 3) | ||
31 | else return f(opt1, opt2) end | ||
32 | end | ||
33 | end | ||
34 | |||
35 | -- define the encoding filters | ||
36 | encodet['base64'] = function() | ||
37 | return ltn12.filter.cycle(_M.b64, "") | ||
38 | end | ||
39 | |||
40 | encodet['quoted-printable'] = function(mode) | ||
41 | return ltn12.filter.cycle(_M.qp, "", | ||
42 | (mode == "binary") and "=0D=0A" or "\r\n") | ||
43 | end | ||
44 | |||
45 | -- define the decoding filters | ||
46 | decodet['base64'] = function() | ||
47 | return ltn12.filter.cycle(_M.unb64, "") | ||
48 | end | ||
49 | |||
50 | decodet['quoted-printable'] = function() | ||
51 | return ltn12.filter.cycle(_M.unqp, "") | ||
52 | end | ||
53 | |||
54 | -- define the line-wrap filters | ||
55 | wrapt['text'] = function(length) | ||
56 | length = length or 76 | ||
57 | return ltn12.filter.cycle(_M.wrp, length, length) | ||
58 | end | ||
59 | wrapt['base64'] = wrapt['text'] | ||
60 | wrapt['default'] = wrapt['text'] | ||
61 | |||
62 | wrapt['quoted-printable'] = function() | ||
63 | return ltn12.filter.cycle(_M.qpwrp, 76, 76) | ||
64 | end | ||
65 | |||
66 | -- function that choose the encoding, decoding or wrap algorithm | ||
67 | _M.encode = choose(encodet) | ||
68 | _M.decode = choose(decodet) | ||
69 | _M.wrap = choose(wrapt) | ||
70 | |||
71 | -- define the end-of-line normalization filter | ||
72 | function _M.normalize(marker) | ||
73 | return ltn12.filter.cycle(_M.eol, 0, marker) | ||
74 | end | ||
75 | |||
76 | -- high level stuffing filter | ||
77 | function _M.stuff() | ||
78 | return ltn12.filter.cycle(_M.dot, 2) | ||
79 | end | ||
80 | |||
81 | return _M | ||