aboutsummaryrefslogtreecommitdiff
path: root/src/mime.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-02-04 14:29:11 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-02-04 14:29:11 +0000
commit0b2542d1a61fc5425ff65ab3dbf7ba7de174763f (patch)
tree8a6188e11db0c9ef6891c31e8a1bebca050b23b2 /src/mime.lua
parentf67864f86c7d703325e86b14d0ba33992c52891b (diff)
downloadluasocket-0b2542d1a61fc5425ff65ab3dbf7ba7de174763f.tar.gz
luasocket-0b2542d1a61fc5425ff65ab3dbf7ba7de174763f.tar.bz2
luasocket-0b2542d1a61fc5425ff65ab3dbf7ba7de174763f.zip
Worked on the manual.
Implemented stuffing (needs test) Added cddb and qp examples.
Diffstat (limited to 'src/mime.lua')
-rw-r--r--src/mime.lua71
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 = {}
15local dt = {} 15local dt = {}
16local wt = {} 16local 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
19local function choose(table) 19local 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
25end 25end
26 26
27-- creates a function that cicles a filter with a given initial 27-- define the encoding filters
28-- context and extra arguments
29local function cicle(f, ctx, ...)
30 return function(chunk)
31 local ret
32 ret, ctx = f(ctx, chunk, unpack(arg))
33 return ret
34 end
35end
36
37-- function that choose the encoding, decoding or wrap algorithm
38encode = choose(et)
39decode = choose(dt)
40
41-- the wrap filter has default parameters
42local cwt = choose(wt)
43function 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))
48end
49
50-- define the encoding algorithms
51et['base64'] = function() 28et['base64'] = function()
52 return cicle(b64, "") 29 return socket.cicle(b64, "")
53end 30end
54 31
55et['quoted-printable'] = function(mode) 32et['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")
57end 34end
58 35
59-- define the decoding algorithms 36-- define the decoding filters
60dt['base64'] = function() 37dt['base64'] = function()
61 return cicle(unb64, "") 38 return socket.cicle(unb64, "")
62end 39end
63 40
64dt['quoted-printable'] = function() 41dt['quoted-printable'] = function()
65 return cicle(unqp, "") 42 return socket.cicle(unqp, "")
66end 43end
67 44
68-- define the wrap algorithms 45-- define the line-wrap filters
69wt['base64'] = function(length, marker) 46wt['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)
72end 49end
50wt['base64'] = wt['text']
73 51
74wt['quoted-printable'] = function(length) 52wt['quoted-printable'] = function()
75 length = length or 76 53 return socket.cicle(qpwrp, 76, 76)
76 return cicle(qpwrp, length, length) 54end
55
56-- function that choose the encoding, decoding or wrap algorithm
57encode = choose(et)
58decode = choose(dt)
59-- there is a default wrap filter
60local cwt = choose(wt)
61function wrap(...)
62 if type(arg[1]) ~= "string" then table.insert(arg, 1, "text") end
63 return cwt(unpack(arg))
77end 64end
78 65
79-- define the end-of-line translation function 66-- define the end-of-line translation filter
80function canonic(marker) 67function canonic(marker)
81 return cicle(eol, "", marker) 68 return socket.cicle(eol, "", marker)
82end 69end
83 70
84-- chains several filters together 71-- chains several filters together
@@ -104,4 +91,4 @@ function chain(...)
104 end 91 end
105end 92end
106 93
107return code 94return mime