Sxw1212

EnderCC

Jul 13th, 2014
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.12 KB | None | 0 0
  1. -- JSON
  2. local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"}
  3.  
  4. local whites = {['\n']=true; ['r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true}
  5. function removeWhite(str)
  6.     while whites[str:sub(1, 1)] do
  7.         str = str:sub(2)
  8.     end
  9.     return str
  10. end
  11.  
  12. function parseBoolean(str)
  13.     if str:sub(1, 4) == "true" then
  14.         return true, removeWhite(str:sub(5))
  15.     else
  16.         return false, removeWhite(str:sub(6))
  17.     end
  18. end
  19.  
  20. function parseNull(str)
  21.     return nil, removeWhite(str:sub(5))
  22. end
  23.  
  24. local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true}
  25. function parseNumber(str)
  26.     local i = 1
  27.     while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do
  28.         i = i + 1
  29.     end
  30.     local val = tonumber(str:sub(1, i - 1))
  31.     str = removeWhite(str:sub(i))
  32.     return val, str
  33. end
  34.  
  35. function parseString(str)
  36.     local i,j = str:find('[^\\]"')
  37.     local s = str:sub(2, j - 1)
  38.  
  39.     for k,v in pairs(controls) do
  40.         s = s:gsub(v, k)
  41.     end
  42.     str = removeWhite(str:sub(j + 1))
  43.     return s, str
  44. end
  45.  
  46. function parseArray(str)
  47.     str = removeWhite(str:sub(2))
  48.    
  49.     local val = {}
  50.     local i = 1
  51.     while str:sub(1, 1) ~= "]" do
  52.         local v = nil
  53.         v, str = parseValue(str)
  54.         val[i] = v
  55.         i = i + 1
  56.         str = removeWhite(str)
  57.     end
  58.     str = removeWhite(str:sub(2))
  59.     return val, str
  60. end
  61.  
  62. function parseObject(str)
  63.     str = removeWhite(str:sub(2))
  64.    
  65.     local val = {}
  66.     while str:sub(1, 1) ~= "}" do
  67.         local k, v = nil, nil
  68.         k, v, str = parseMember(str)
  69.         val[k] = v
  70.         str = removeWhite(str)
  71.     end
  72.     str = removeWhite(str:sub(2))
  73.     return val, str
  74. end
  75.  
  76. function parseMember(str)
  77.     local k = nil
  78.     k, str = parseValue(str)
  79.     local val = nil
  80.     val, str = parseValue(str)
  81.     return k, val, str
  82. end
  83.  
  84. function parseValue(str)
  85.     local fchar = str:sub(1, 1)
  86.     if fchar == "{" then
  87.         return parseObject(str)
  88.     elseif fchar == "[" then
  89.         return parseArray(str)
  90.     elseif tonumber(fchar) ~= nil or numChars[fchar] then
  91.         return parseNumber(str)
  92.     elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then
  93.         return parseBoolean(str)
  94.     elseif fchar == "\"" then
  95.         return parseString(str)
  96.     elseif str:sub(1, 4) == "null" then
  97.         return parseNull(str)
  98.     end
  99.     return nil
  100. end
  101.  
  102. function decode(str)
  103.     str = removeWhite(str)
  104.     t = parseValue(str)
  105.     return t
  106. end
  107.  
  108. -- Usage: decode(str) encode(val)
  109.  
  110. local url = "http://ender.fluidnode.com/"
  111.  
  112. function rawStart(chan)
  113.     local res = http.get(url .. "start?channel=" .. chan)
  114.     if res then
  115.         local msg = decode(res.readAll())
  116.         if msg then
  117.             if msg.err then
  118.                 return false, msg.err
  119.             end
  120.             return true, msg.uuid, msg.name
  121.         else
  122.             return false
  123.         end
  124.     else
  125.         return false
  126.     end
  127. end
  128.  
  129. function rawPoll(uuid)
  130.     local res = http.get(url .. "poll?uuid=" .. uuid);
  131.     if res then
  132.         local msg = decode(res.readAll())
  133.         if msg then
  134.             if msg.err then
  135.                 return false, msg.err
  136.             end
  137.             if not msg.res then
  138.                 return false
  139.             end
  140.             return true, msg.res, msg.from
  141.         else
  142.             return false
  143.         end
  144.     else
  145.         return false
  146.     end
  147. end
  148.  
  149. function rawSend(msg, uuid)
  150.     local res = http.get(url .. "send?uuid=" .. uuid .. "&message=" .. textutils.urlEncode(msg));
  151.     if res then
  152.         local msg = decode(res.readAll())
  153.         if msg then
  154.             if msg.err then
  155.                 return false, msg.err
  156.             end
  157.             return true
  158.         else
  159.             return false
  160.         end
  161.     else
  162.         return false
  163.     end
  164. end
  165.  
  166. function rawCheck(uuid)
  167.     local res = http.get(url .. "check?uuid=" .. uuid)
  168.     if res then
  169.         return res.readAll() == "true"
  170.     else
  171.         return false
  172.     end
  173. end
  174.  
  175. local chans = {}
  176.  
  177. function connect(channel)
  178.     if chans[channel] then
  179.         if rawCheck(chans[channel]) then
  180.             return chans[channel][1]
  181.         else
  182.             chans[channel] = nil
  183.             return connect(channel)
  184.         end
  185.     else
  186.         local res, uuid, name = rawStart(channel)
  187.         if res then
  188.             chans[channel] = {uuid, name}
  189.             return uuid
  190.         else
  191.             return connect(channel)
  192.         end
  193.     end
  194. end
  195.  
  196. function getName(channel)
  197.     connect(channel)
  198.     return chans[channel][2]
  199. end
  200.  
  201. function send(channel, message)
  202.     local uuid = connect(channel)
  203.     return rawSend(message, uuid)
  204. end
  205.  
  206. function receive(channel)
  207.     local uuid = connect(channel)
  208.     return rawPoll(uuid)
  209. end
Advertisement
Add Comment
Please, Sign In to add comment