diff options
Diffstat (limited to 'src/mime.lua')
| -rw-r--r-- | src/mime.lua | 71 |
1 files changed, 29 insertions, 42 deletions
diff --git a/src/mime.lua b/src/mime.lua index 30c6b38..369567f 100644 --- a/src/mime.lua +++ b/src/mime.lua | |||
| @@ -15,70 +15,57 @@ local et = {} | |||
| 15 | local dt = {} | 15 | local dt = {} |
| 16 | local wt = {} | 16 | local wt = {} |
| 17 | 17 | ||
| 18 | -- creates a function that chooses an algorithm from a given table | 18 | -- creates a function that chooses a filter from a given table |
| 19 | local function choose(table) | 19 | local function choose(table) |
| 20 | return function(method, ...) | 20 | return function(filter, ...) |
| 21 | local f = table[method or "nil"] | 21 | local f = table[filter or "nil"] |
| 22 | if not f then error("unknown method (" .. tostring(method) .. ")", 3) | 22 | if not f then error("unknown filter (" .. tostring(filter) .. ")", 3) |
| 23 | else return f(unpack(arg)) end | 23 | else return f(unpack(arg)) end |
| 24 | end | 24 | end |
| 25 | end | 25 | end |
| 26 | 26 | ||
| 27 | -- creates a function that cicles a filter with a given initial | 27 | -- define the encoding filters |
| 28 | -- context and extra arguments | ||
| 29 | local function cicle(f, ctx, ...) | ||
| 30 | return function(chunk) | ||
| 31 | local ret | ||
| 32 | ret, ctx = f(ctx, chunk, unpack(arg)) | ||
| 33 | return ret | ||
| 34 | end | ||
| 35 | end | ||
| 36 | |||
| 37 | -- function that choose the encoding, decoding or wrap algorithm | ||
| 38 | encode = choose(et) | ||
| 39 | decode = choose(dt) | ||
| 40 | |||
| 41 | -- the wrap filter has default parameters | ||
| 42 | local cwt = choose(wt) | ||
| 43 | function wrap(...) | ||
| 44 | if not arg[1] or type(arg[1]) ~= "string" then | ||
| 45 | table.insert(arg, 1, "base64") | ||
| 46 | end | ||
| 47 | return cwt(unpack(arg)) | ||
| 48 | end | ||
| 49 | |||
| 50 | -- define the encoding algorithms | ||
| 51 | et['base64'] = function() | 28 | et['base64'] = function() |
| 52 | return cicle(b64, "") | 29 | return socket.cicle(b64, "") |
| 53 | end | 30 | end |
| 54 | 31 | ||
| 55 | et['quoted-printable'] = function(mode) | 32 | et['quoted-printable'] = function(mode) |
| 56 | return cicle(qp, "", (mode == "binary") and "=0D=0A" or "\13\10") | 33 | return socket.cicle(qp, "", (mode == "binary") and "=0D=0A" or "\13\10") |
| 57 | end | 34 | end |
| 58 | 35 | ||
| 59 | -- define the decoding algorithms | 36 | -- define the decoding filters |
| 60 | dt['base64'] = function() | 37 | dt['base64'] = function() |
| 61 | return cicle(unb64, "") | 38 | return socket.cicle(unb64, "") |
| 62 | end | 39 | end |
| 63 | 40 | ||
| 64 | dt['quoted-printable'] = function() | 41 | dt['quoted-printable'] = function() |
| 65 | return cicle(unqp, "") | 42 | return socket.cicle(unqp, "") |
| 66 | end | 43 | end |
| 67 | 44 | ||
| 68 | -- define the wrap algorithms | 45 | -- define the line-wrap filters |
| 69 | wt['base64'] = function(length, marker) | 46 | wt['text'] = function(length) |
| 70 | length = length or 76 | 47 | length = length or 76 |
| 71 | return cicle(wrp, length, length, marker) | 48 | return socket.cicle(wrp, length, length) |
| 72 | end | 49 | end |
| 50 | wt['base64'] = wt['text'] | ||
| 73 | 51 | ||
| 74 | wt['quoted-printable'] = function(length) | 52 | wt['quoted-printable'] = function() |
| 75 | length = length or 76 | 53 | return socket.cicle(qpwrp, 76, 76) |
| 76 | return cicle(qpwrp, length, length) | 54 | end |
| 55 | |||
| 56 | -- function that choose the encoding, decoding or wrap algorithm | ||
| 57 | encode = choose(et) | ||
| 58 | decode = choose(dt) | ||
| 59 | -- there is a default wrap filter | ||
| 60 | local cwt = choose(wt) | ||
| 61 | function wrap(...) | ||
| 62 | if type(arg[1]) ~= "string" then table.insert(arg, 1, "text") end | ||
| 63 | return cwt(unpack(arg)) | ||
| 77 | end | 64 | end |
| 78 | 65 | ||
| 79 | -- define the end-of-line translation function | 66 | -- define the end-of-line translation filter |
| 80 | function canonic(marker) | 67 | function canonic(marker) |
| 81 | return cicle(eol, "", marker) | 68 | return socket.cicle(eol, "", marker) |
| 82 | end | 69 | end |
| 83 | 70 | ||
| 84 | -- chains several filters together | 71 | -- chains several filters together |
| @@ -104,4 +91,4 @@ function chain(...) | |||
| 104 | end | 91 | end |
| 105 | end | 92 | end |
| 106 | 93 | ||
| 107 | return code | 94 | return mime |
