Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- files = {
- "main = \"http://mew.tom.systems:7000/\"\
- fallback = \"http://173.208.245.106:7000/\"\
- os.loadAPI(\"json\")\
- \
- _G.ccloud = {}\
- _G.ccloud.username = \"\"\
- _G.ccloud.token = \"\"\
- _G.ccloud.id = \"\"\
- _G.ccloud.fallback = true\
- \
- function _G.ccloud.post(_sPath,_sData,_tHeader)\
- \009local _URL\
- \009local _HTTP_HANDLE\
- \009local _HTTP_RETURN\
- \009if _G.ccloud.fallback == true then\
- \009\009_URL = fallback .. _sPath\
- \009else\
- \009\009_URL = main .. _sPath\
- \009end\
- \009_HTTP_HANDLE = http.post(_URL,_sData,_tHeader)\
- \009if _HTTP_HANDLE == nil then\
- \009\009if _G.ccloud.fallback == true then\
- \009\009\009return '{\"error\": true, \"reason\": \"Unable to contact the server\"}'\
- \009\009end\
- \009\009_G.ccloud.fallback = true\
- \009\009return _G.ccloud.post(_sPath,_sData,_tHeader)\
- \009else\
- \009\009_HTTP_RETURN = _HTTP_HANDLE.readAll()\
- \009\009_HTTP_HANDLE.close()\
- \009\009return _HTTP_RETURN\
- \009end\
- end\
- \
- function _G.ccloud.login(_sUsername,_sPassword)\
- \009local _RETURN_DEC\
- \009local _HTTP_RETURN \
- \009_HTTP_RETURN = _G.ccloud.post(\"go\",\"username=\" .. _sUsername .. \"&password=\" .. _sPassword,{})\
- \009_RETURN_DEC = json.decode(_HTTP_RETURN)\
- \009if _RETURN_DEC['error'] == true then\
- \009\009return false, _RETURN_DEC['reason']\
- \009else\
- \009\009_G.ccloud.username = _sUsername\
- \009\009_G.ccloud.id = _RETURN_DEC['id']\
- \009\009_G.ccloud.token = _RETURN_DEC['token']\
- \009\009return true, _RETURN_DEC['id']\
- \009end\
- end\
- \
- function _G.ccloud.directory(_sPath)\
- \009local _RETURN_DEC\
- \009local _HTTP_RETURN \
- \009local _HEADER = {}\
- \009_HEADER['Authorization'] = _G.ccloud.token\
- \009_HTTP_RETURN = _G.ccloud.post(\"dir\",\"path=\" .. _sPath,_HEADER)\
- \009_RETURN_DEC = json.decode(_HTTP_RETURN)\
- \009if _RETURN_DEC['error'] == true then\
- \009\009return false, _RETURN_DEC['reason']\
- \009else\
- \009\009return true, _RETURN_DEC['content']\
- \009end\
- end \
- \
- function _G.ccloud.getsize(_sPath)\
- \009local _RETURN_DEC\
- \009local _HTTP_RETURN \
- \009local _HEADER = {}\
- \009_HEADER['Authorization'] = _G.ccloud.token\
- \009_HTTP_RETURN = _G.ccloud.post(\"size\",\"path=\" .. _sPath,_HEADER)\
- \009_RETURN_DEC = json.decode(_HTTP_RETURN)\
- \009if _RETURN_DEC['error'] == true then\
- \009\009return false, _RETURN_DEC['reason']\
- \009else\
- \009\009return true, _RETURN_DEC['size']\
- \009end\
- end \
- \
- function _G.ccloud.getfile(_sPath)\
- \009local _RETURN_DEC\
- \009local _HTTP_RETURN\
- \009local _HEADER = {}\
- \009_HEADER['Authorization'] = _G.ccloud.token\
- \009_HTTP_RETURN = _G.ccloud.post(\"get\",\"path=\" .. _sPath,_HEADER)\
- \009_RETURN_DEC = json.decode(_HTTP_RETURN)\
- \009if _RETURN_DEC['error'] == true then\
- \009\009return false, _RETURN_DEC['reason']\
- \009else\
- \009\009return true, _RETURN_DEC['content']\
- \009end\
- end\
- \
- function _G.ccloud.deletefile(_sPath)\
- \009local _RETURN_DEC\
- \009local _HTTP_RETURN\
- \009local _HEADER = {}\
- \009_HEADER['Authorization'] = _G.ccloud.token\
- \009_HTTP_RETURN = _G.ccloud.post(\"delete\",\"path=\" .. _sPath,_HEADER)\
- \009_RETURN_DEC = json.decode(_HTTP_RETURN)\
- \009if _RETURN_DEC['error'] == true then\
- \009\009return false, _RETURN_DEC['reason']\
- \009else\
- \009\009return true, \"\"\
- \009end\
- end\
- \
- function _G.ccloud.uploadfile(_sPath,_sUploadFrom)\
- \009local _RETURN_DEC\
- \009local _HTTP_RETURN\
- \009local _FILE_DATA\
- \009local _HEADER = {}\
- \009local _FILE = fs.open(_sUploadFrom,\"r\")\
- \009_FILE_DATA = _FILE.readAll()\
- \009_FILE.close()\
- \009_HEADER['Authorization'] = _G.ccloud.token\
- \009_HTTP_RETURN = _G.ccloud.post(\"upload\",\"path=\" .. _sPath .. \"&data=\" .. textutils.urlEncode(_FILE_DATA),_HEADER)\
- \009_RETURN_DEC = json.decode(_HTTP_RETURN)\
- \009if _RETURN_DEC['error'] == true then\
- \009\009return false, _RETURN_DEC['reason']\
- \009else\
- \009\009return true, \"\"\
- \009end\
- end\
- \
- function _G.ccloud.uploadstring(_sPath,_sString)\
- \009local _RETURN_DEC\
- \009local _HTTP_RETURN\
- \009local _HEADER = {}\
- \009_HEADER['Authorization'] = _G.ccloud.token\
- \009_HTTP_RETURN = _G.ccloud.post(\"upload\",\"path=\" .. _sPath .. \"&data=\" .. textutils.urlEncode(_sString),_HEADER)\
- \009_RETURN_DEC = json.decode(_HTTP_RETURN)\
- \009if _RETURN_DEC['error'] == true then\
- \009\009return false, _RETURN_DEC['reason']\
- \009else\
- \009\009return true, \"\"\
- \009end\
- end",
- "os.loadAPI(\"/CCloud\")\
- term.clear()\
- w,h = term.getSize()\
- h = math.floor(h)\
- paintutils.drawFilledBox(1,1,w,h,colours.blue)\
- \
- function convs(int)\
- if int < 1000 then\
- return tostring(int) .. \"B\"\
- else\
- return tostring(math.ceil(int/10)/100) .. \"K\"\
- end\
- end\
- \
- function centre(text)\
- _,y = term.getCursorPos()\
- term.setCursorPos((w-#text)/2,y)\
- term.write(text)\
- end\
- \
- function len(t)\
- local _l = 0\
- for i,k in pairs(t) do\
- _l = _l + 1\
- end\
- return _l\
- end\
- \
- function login(texto)\
- term.setBackgroundColour(colours.blue)\
- term.setTextColour(colours.white)\
- term.clear()\
- u = \"\"\
- p = \"\"\
- cw = 0\
- term.setCursorPos(2,2)\
- term.write(\"CCloud\")\
- paintutils.drawFilledBox(2,h/2-2,w-1,h/2,colours.white)\
- paintutils.drawFilledBox(2,h/2+2,w-1,h/2+4,colours.white)\
- paintutils.drawFilledBox(2,h/2+6,10,h/2+8,colours.cyan)\
- if texto ~= \"\" then\
- term.setCursorPos(12,h/2+7)\
- term.setBackgroundColour(colours.blue)\
- term.setTextColour(colours.white)\
- term.write(texto)\
- term.setBackgroundColour(colours.blue)\
- term.setTextColour(colours.white)\
- end\
- term.setTextColour(colours.white)\
- term.setCursorPos(4,h/2+7)\
- term.write(\"Login\")\
- term.setTextColour(colours.grey)\
- while true do\
- term.setBackgroundColour(colours.white)\
- term.setCursorPos(4,h/2-1)\
- if #u == 0 then\
- term.setTextColour(colours.lightGrey)\
- term.write(\"Username\")\
- term.setTextColour(colours.grey)\
- else\
- term.write(u .. string.rep(\" \",(w-5)-#u))\
- end\
- term.setCursorPos(4,h/2+3)\
- if #p == 0 then\
- term.setTextColour(colours.lightGrey)\
- term.write(\"Password\")\
- term.setTextColour(colours.grey)\
- else\
- term.write(string.rep(\"\007\",#p) .. string.rep(\" \",(w-5)-#p))\
- end\
- _EVENT = {os.pullEvent()}\
- if _EVENT[1] == \"mouse_click\" then\
- x,y = _EVENT[3],_EVENT[4]\
- if x >= 2 and x <= w-1 and y <= h/2 and y >= h/2-3 then\
- cw = 1\
- paintutils.drawFilledBox(2,h/2-2,2,h/2,colours.cyan)\
- paintutils.drawFilledBox(2,h/2+2,2,h/2+4,colours.white)\
- elseif x >= 2 and x <= w-1 and y <= h/2+4 and y >= h/2+1 then\
- cw = 2\
- paintutils.drawFilledBox(2,h/2-2,2,h/2,colours.white)\
- paintutils.drawFilledBox(2,h/2+2,2,h/2+4,colours.cyan)\
- elseif x >= 2 and x <= 10 and y <= h/2+8 and y >= h/2+5 then\
- if u ~= \"\" and p ~= \"\" then\
- break\
- end\
- else\
- cw = 0\
- end\
- end\
- if _EVENT[1] == \"key\" then\
- if _EVENT[2] == 28 then\
- if u ~= \"\" and p ~= \"\" then\
- break\
- end\
- elseif _EVENT[2] == 14 then\
- if cw == 1 then\
- u = string.sub(u,1,#u-1)\
- elseif cw == 2 then\
- p = string.sub(p,1,#p-1)\
- end\
- elseif _EVENT[2] == 15 then\
- if cw == 1 then\
- cw = 2\
- paintutils.drawFilledBox(2,h/2-2,2,h/2,colours.white)\
- paintutils.drawFilledBox(2,h/2+2,2,h/2+4,colours.cyan)\
- else\
- cw = 1\
- paintutils.drawFilledBox(2,h/2-2,2,h/2,colours.cyan)\
- paintutils.drawFilledBox(2,h/2+2,2,h/2+4,colours.white)\
- end\
- end\
- end\
- if _EVENT[1] == \"char\" then\
- if cw == 1 then\
- if #u < w-5 then\
- u = u .. _EVENT[2]\
- end\
- elseif cw == 2 then\
- if #p < w-5 then\
- p = p .. _EVENT[2]\
- end\
- end\
- end\
- end\
- end\
- function doload(x,y,t,b)\
- a = {\" / \",\" - \",\" \\\\ \",\" | \"}\
- term.setCursorPos(x,y)\
- c = 1\
- term.setTextColour(t)\
- term.setBackgroundColour(b)\
- while true do\
- term.setCursorPos(x,y)\
- term.write(a[c])\
- if c == #a then\
- c = 0\
- end\
- c = c + 1\
- sleep(0)\
- end\
- end\
- \
- resp = \"\"\
- function dologin()\
- while true do\
- login(resp)\
- resp = \"\"\
- function loada()\
- worked, resp = ccloud.login(u,p)\
- end\
- function loadb()\
- doload(4,h/2+7,colours.white,colours.cyan)\
- end\
- parallel.waitForAny(loada,loadb)\
- if worked == true then\
- break\
- end\
- end\
- end\
- \
- dologin()\
- \
- function hook(...)\
- _a = {...}\
- func = _a[1]\
- arg = _a[2]\
- arg2 = _a[3]\
- b = {}\
- b[1] = false\
- while b[1] == false do\
- b = {func(arg,arg2)}\
- if b[1] == false and b[2] == \"Invalid token\" then\
- dologin(\"Session has been terminated\")\
- main()\
- end\
- end\
- return b[1],b[2]\
- end\
- \
- function hasattr(r,k)\
- for i,_ in pairs(r) do\
- if tostring(i) == tostring(k) then\
- return true\
- end\
- end\
- return false\
- end\
- \
- sel = {}\
- function main()\
- path = \"/\"\
- term.clear()\
- worked, files = hook(ccloud.directory,path)\
- term.setBackgroundColour(colours.black)\
- term.setTextColour(colours.white)\
- paintutils.drawFilledBox(1,1,w,3,colours.blue)\
- paintutils.drawFilledBox(1,4,w,h-1,colours.white)\
- paintutils.drawFilledBox(1,h,w,h,colours.blue)\
- ref = true\
- while true do\
- if ref == true then\
- ref = false\
- term.setTextColour(colours.white)\
- term.setBackgroundColour(colours.blue)\
- if len(sel) > 0 then\
- term.setCursorPos(1,2)\
- print(\" Refresh Upload Delete Download Edit Logout\")\
- else\
- term.setCursorPos(1,2)\
- print(\" Refresh Upload Logout\")\
- end\
- worked,size = hook(ccloud.getsize,path)\
- term.setCursorPos(1,h)\
- if worked == true then\
- term.write(convs(size) .. string.rep(\" \",w-#convs(size)-#path) .. path)\
- else\
- term.write(string.rep(\" \",w-#path) .. path)\
- end\
- term.setCursorPos(1,4)\
- term.setTextColour(colours.grey)\
- for i,file in pairs(files) do\
- if i ~= \"path\" then\
- _,y = term.getCursorPos()\
- if hasattr(sel,y-3) then\
- term.setBackgroundColour(colours.lightGrey)\
- else\
- term.setBackgroundColour(colours.white)\
- end\
- print(file['name'] .. string.rep(\" \",w-(#file['name'] + #tostring(convs(file['size'])))) .. convs(file['size']))\
- end\
- end\
- end\
- _EVENT = {os.pullEvent()}\
- if _EVENT[1] == \"mouse_click\" then\
- x,y = _EVENT[3],_EVENT[4]\
- if y <= 3 and _EVENT[2] == 1 then\
- if x >= 1 and x <= 9 then\
- -- refresh\
- worked, files = hook(ccloud.directory,path)\
- ref = true\
- elseif x >= 10 and x <= 17 then\
- -- upload\
- term.setCursorPos(1,h)\
- term.clearLine()\
- term.setBackgroundColour(colours.blue)\
- term.setTextColour(colours.white)\
- term.write(\"Path:\")\
- pathwindow = window.create(term.native(),6,h,w,h)\
- cur = term.current()\
- term.redirect(pathwindow)\
- term.setBackgroundColour(colours.blue)\
- term.setTextColour(colours.white)\
- term.clear()\
- filepath = read()\
- term.redirect(cur)\
- if fs.exists(filepath) then\
- term.setCursorPos(1,h)\
- term.clearLine()\
- term.write(\"SaveAs:\")\
- pathwindow.reposition(8,h,w-8,1)\
- term.redirect(pathwindow)\
- exists = true\
- while exists == true do\
- term.setCursorPos(1,1)\
- term.clear()\
- tosave = read()\
- for _,f in pairs(files) do\
- if f.name == tosave then\
- exists = true\
- end\
- end\
- exists = false\
- if exists == true then\
- term.clearLine()\
- term.write(\"A file of this name already exists\")\
- sleep(1)\
- else\
- worked, error = hook(ccloud.uploadfile,path + saveas,filepath)\
- if worked == false then\
- term.write(\"Failed to upload file\")\
- end\
- end\
- end\
- else\
- -- do\
- end\
- term.redirect(cur)\
- pathwindow.setVisible(false)\
- ref = true\
- elseif x >= 18 and x <= 25 then\
- -- delete\
- elseif x >= 26 and x <= 35 then\
- -- download\
- elseif x >= 36 and x <= 41 then\
- -- edit\
- elseif x >= 42 then\
- -- logout\
- end\
- elseif y >= 4 and y <= h-1 then\
- if hasattr(files,tostring(y-3)) then\
- if _EVENT[2] == 2 then\
- if hasattr(sel,y-3) then\
- sel[tostring(y-3)] = nil\
- ref = true\
- else\
- sel[tostring(y-3)] = true\
- ref = true\
- end\
- end\
- else\
- if _EVENT[2] == 1 then\
- sel = {}\
- ref = true\
- end\
- end\
- end\
- end\
- term.setTextColour(colours.lightGrey)\
- term.setBackgroundColour(colours.white)\
- end\
- end\
- \
- \
- \
- main()",
- "------------------------------------------------------------------ utils\
- local controls = {[\"\\n\"]=\"\\\\n\", [\"\\r\"]=\"\\\\r\", [\"\\t\"]=\"\\\\t\", [\"\\b\"]=\"\\\\b\", [\"\\f\"]=\"\\\\f\", [\"\\\"\"]=\"\\\\\\\"\", [\"\\\\\"]=\"\\\\\\\\\"}\
- \
- local function isArray(t)\
- \009local max = 0\
- \009for k,v in pairs(t) do\
- \009\009if type(k) ~= \"number\" then\
- \009\009\009return false\
- \009\009elseif k > max then\
- \009\009\009max = k\
- \009\009end\
- \009end\
- \009return max == #t\
- end\
- \
- local whites = {['\\n']=true; ['\\r']=true; ['\\t']=true; [' ']=true; [',']=true; [':']=true}\
- function removeWhite(str)\
- \009while whites[str:sub(1, 1)] do\
- \009\009str = str:sub(2)\
- \009end\
- \009return str\
- end\
- \
- ------------------------------------------------------------------ encoding\
- \
- local function encodeCommon(val, pretty, tabLevel, tTracking)\
- \009local str = \"\"\
- \
- \009-- Tabbing util\
- \009local function tab(s)\
- \009\009str = str .. (\"\\t\"):rep(tabLevel) .. s\
- \009end\
- \
- \009local function arrEncoding(val, bracket, closeBracket, iterator, loopFunc)\
- \009\009str = str .. bracket\
- \009\009if pretty then\
- \009\009\009str = str .. \"\\n\"\
- \009\009\009tabLevel = tabLevel + 1\
- \009\009end\
- \009\009for k,v in iterator(val) do\
- \009\009\009tab(\"\")\
- \009\009\009loopFunc(k,v)\
- \009\009\009str = str .. \",\"\
- \009\009\009if pretty then str = str .. \"\\n\" end\
- \009\009end\
- \009\009if pretty then\
- \009\009\009tabLevel = tabLevel - 1\
- \009\009end\
- \009\009if str:sub(-2) == \",\\n\" then\
- \009\009\009str = str:sub(1, -3) .. \"\\n\"\
- \009\009elseif str:sub(-1) == \",\" then\
- \009\009\009str = str:sub(1, -2)\
- \009\009end\
- \009\009tab(closeBracket)\
- \009end\
- \
- \009-- Table encoding\
- \009if type(val) == \"table\" then\
- \009\009assert(not tTracking[val], \"Cannot encode a table holding itself recursively\")\
- \009\009tTracking[val] = true\
- \009\009if isArray(val) then\
- \009\009\009arrEncoding(val, \"[\", \"]\", ipairs, function(k,v)\
- \009\009\009\009str = str .. encodeCommon(v, pretty, tabLevel, tTracking)\
- \009\009\009end)\
- \009\009else\
- \009\009\009arrEncoding(val, \"{\", \"}\", pairs, function(k,v)\
- \009\009\009\009assert(type(k) == \"string\", \"JSON object keys must be strings\", 2)\
- \009\009\009\009str = str .. encodeCommon(k, pretty, tabLevel, tTracking)\
- \009\009\009\009str = str .. (pretty and \": \" or \":\") .. encodeCommon(v, pretty, tabLevel, tTracking)\
- \009\009\009end)\
- \009\009end\
- \009-- String encoding\
- \009elseif type(val) == \"string\" then\
- \009\009str = '\"' .. val:gsub(\"[%c\\\"\\\\]\", controls) .. '\"'\
- \009-- Number encoding\
- \009elseif type(val) == \"number\" or type(val) == \"boolean\" then\
- \009\009str = tostring(val)\
- \009else\
- \009\009error(\"JSON only supports arrays, objects, numbers, booleans, and strings\", 2)\
- \009end\
- \009return str\
- end\
- \
- function encode(val)\
- \009return encodeCommon(val, false, 0, {})\
- end\
- \
- function encodePretty(val)\
- \009return encodeCommon(val, true, 0, {})\
- end\
- \
- ------------------------------------------------------------------ decoding\
- \
- local decodeControls = {}\
- for k,v in pairs(controls) do\
- \009decodeControls[v] = k\
- end\
- \
- function parseBoolean(str)\
- \009if str:sub(1, 4) == \"true\" then\
- \009\009return true, removeWhite(str:sub(5))\
- \009else\
- \009\009return false, removeWhite(str:sub(6))\
- \009end\
- end\
- \
- function parseNull(str)\
- \009return nil, removeWhite(str:sub(5))\
- end\
- \
- local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true}\
- function parseNumber(str)\
- \009local i = 1\
- \009while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do\
- \009\009i = i + 1\
- \009end\
- \009local val = tonumber(str:sub(1, i - 1))\
- \009str = removeWhite(str:sub(i))\
- \009return val, str\
- end\
- \
- function parseString(str)\
- \009str = str:sub(2)\
- \009local s = \"\"\
- \009while str:sub(1,1) ~= \"\\\"\" do\
- \009\009local next = str:sub(1,1)\
- \009\009str = str:sub(2)\
- \009\009assert(next ~= \"\\n\", \"Unclosed string\")\
- \
- \009\009if next == \"\\\\\" then\
- \009\009\009local escape = str:sub(1,1)\
- \009\009\009str = str:sub(2)\
- \
- \009\009\009next = assert(decodeControls[next..escape], \"Invalid escape character\")\
- \009\009end\
- \
- \009\009s = s .. next\
- \009end\
- \009return s, removeWhite(str:sub(2))\
- end\
- \
- function parseArray(str)\
- \009str = removeWhite(str:sub(2))\
- \
- \009local val = {}\
- \009local i = 1\
- \009while str:sub(1, 1) ~= \"]\" do\
- \009\009local v = nil\
- \009\009v, str = parseValue(str)\
- \009\009val[i] = v\
- \009\009i = i + 1\
- \009\009str = removeWhite(str)\
- \009end\
- \009str = removeWhite(str:sub(2))\
- \009return val, str\
- end\
- \
- function parseObject(str)\
- \009str = removeWhite(str:sub(2))\
- \
- \009local val = {}\
- \009while str:sub(1, 1) ~= \"}\" do\
- \009\009local k, v = nil, nil\
- \009\009k, v, str = parseMember(str)\
- \009\009val[k] = v\
- \009\009str = removeWhite(str)\
- \009end\
- \009str = removeWhite(str:sub(2))\
- \009return val, str\
- end\
- \
- function parseMember(str)\
- \009local k = nil\
- \009k, str = parseValue(str)\
- \009local val = nil\
- \009val, str = parseValue(str)\
- \009return k, val, str\
- end\
- \
- function parseValue(str)\
- \009local fchar = str:sub(1, 1)\
- \009if fchar == \"{\" then\
- \009\009return parseObject(str)\
- \009elseif fchar == \"[\" then\
- \009\009return parseArray(str)\
- \009elseif tonumber(fchar) ~= nil or numChars[fchar] then\
- \009\009return parseNumber(str)\
- \009elseif str:sub(1, 4) == \"true\" or str:sub(1, 5) == \"false\" then\
- \009\009return parseBoolean(str)\
- \009elseif fchar == \"\\\"\" then\
- \009\009return parseString(str)\
- \009elseif str:sub(1, 4) == \"null\" then\
- \009\009return parseNull(str)\
- \009end\
- \009return nil\
- end\
- \
- function decode(str)\
- \009str = removeWhite(str)\
- \009t = parseValue(str)\
- \009return t\
- end\
- \
- function decodeFromFile(path)\
- \009local file = assert(fs.open(path, \"r\"))\
- \009local decoded = decode(file.readAll())\
- \009file.close()\
- \009return decoded\
- end",
- },
- struc = {
- "C",
- "CGui",
- "json",
- },
- beep = "boop",
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement