Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local pkg={["fwhapi"]="local id = 62000\
- local pass = \"\"\
- local site = \"\"\
- \
- if not fs.exists(\"/fwh\") then\
- fs.makeDir(\"/fwh\")\
- end\
- \
- for i,v in pairs(rs.getSides()) do\
- if peripheral.getType(v) == \"modem\" then\
- rednet.open(v)\
- end\
- end\
- \
- function urlenc(str)\
- return str:gsub(\"[&$;/:%.\\\"\\'\\n]\", function(s) return \"$\"..string.byte(s)..\";\" end)\
- end\
- \
- function request(str)\
- if pass == \"\" or site == \"\" then\
- error(\"Specify the password and site name first!\")\
- end\
- --print(str)\
- rednet.send(id, \"fwh:\"..str)\
- local _, msg = rednet.receive(0.5)\
- --print(tostring(msg))\
- if msg then\
- local ok, resp = msg:match(\"([01])(.+)\")\
- --print(ok..\" \"..tostring(ok==\"1\")..\" \"..resp)\
- if ok and resp then\
- return ok==\"1\", resp\
- else\
- return false, msg\
- end\
- else\
- return false, \"No Response\"\
- end\
- end\
- \
- function setPass(s)\
- pass = urlenc(s)\
- end\
- \
- function setSite(s)\
- site = urlenc(s)\
- end\
- \
- function verify()\
- return request(\"cmd=verify&site=\"..site..\"&pass=\"..pass)\
- end\
- \
- function uploadRaw(servpath, contents)\
- return request(\"cmd=upload&site=\"..site..\"&pass=\"..pass..\"&name=\"..urlenc(servpath)..\"&contents=\"..urlenc(contents))\
- end\
- \
- function upload(servpath, filename)\
- local f = io.open(filename, \"r\")\
- assert(f, \"File does not exist: \"..filename)\
- local data = f:read(\"*a\")\
- f:close()\
- return uploadRaw(servpath, data)\
- end\
- \
- function list(servpath)\
- local ok, ls = request(\"cmd=list&site=\"..site..\"&pass=\"..pass..\"&dir=\"..urlenc(servpath))\
- return ok, textutils.unserialize(ls)\
- end\
- \
- function delete(servpath)\
- return request(\"cmd=delete&site=\"..site..\"&pass=\"..pass..\"&name=\"..urlenc(servpath))\
- end\
- \
- function mkdir(servpath)\
- return request(\"cmd=mkdir&site=\"..site..\"&pass=\"..pass..\"&name=\"..urlenc(servpath))\
- end\
- \
- function isdir(servpath)\
- local ok, resp = request(\"cmd=isdir&site=\"..site..\"&pass=\"..pass..\"&name=\"..urlenc(servpath))\
- if resp == \"true\" then\
- return ok, true\
- elseif resp == \"false\" then\
- return ok, false\
- else\
- return ok, resp\
- end\
- end\
- \
- function get(servpath)\
- return request(\"cmd=get&site=\"..site..\"&pass=\"..pass..\"&name=\"..urlenc(servpath))\
- end\
- \
- function save(servpath, filename)\
- local ok, resp = get(servpath)\
- if not ok then\
- return false, resp\
- else\
- local f = io.open(filename, \"w\")\
- if not f then\
- return false, \"Could not open file\"\
- else\
- f:write(resp)\
- f:close()\
- return true, \"\"\
- end\
- end\
- end\
- \
- function loadConfig(server)\
- setSite(server)\
- if fs.exists(\"/fwh/.cfg_\"..server) then\
- local f = io.open(\"/fwh/.cfg_\"..server, \"r\")\
- setPass(f:read(\"*l\"))\
- f:close()\
- return true\
- else\
- write(\"Password: \")\
- setPass(read(\"*\"))\
- if verify() then\
- local f = io.open(\"/fwh/.cfg_\"..server, \"w\")\
- f:write(pass)\
- f:close()\
- return true\
- else\
- return false\
- end\
- end\
- end",["download"]="shell.run(\"/fwh/fwhapi\")\
- \
- local tArgs = {...}\
- if #tArgs ~= 2 then\
- print([[Usage:\
- ]]..shell.getRunningProgram()..[[ sitename folder]])\
- error(\"\", 0)\
- end\
- \
- tArgs[2] = tArgs[2]:match(\"^(.-)/?$\")\
- \
- if not fs.exists(tArgs[2]) then\
- fs.makeDir(tArgs[2])\
- end\
- \
- if not loadConfig(tArgs[1]) then\
- error(\"Invalid Password\")\
- end\
- \
- local function rdl(dir)\
- local ok, ls = list(dir)\
- if not ok then print(\"Error listing dir: \"..ls) return end\
- for i,v in pairs(ls) do\
- local ok, d = isdir(dir..\"/\"..v)\
- if d then\
- print(\"Making dir \"..tArgs[2]..dir..\"/\"..v)\
- fs.makeDir(tArgs[2]..dir..\"/\"..v)\
- rdl(dir..\"/\"..v)\
- else\
- print(\"Saving file \"..tArgs[2]..dir..\"/\"..v)\
- local ok, err = save(dir..\"/\"..v, tArgs[2]..dir..\"/\"..v)\
- end\
- if not ok then print(err) end\
- end\
- end\
- \
- rdl(\"\")",["sync"]="shell.run(\"/fwh/fwhapi\")\
- \
- local tArgs = {...}\
- if #tArgs ~= 2 then\
- print([[Usage:\
- ]]..shell.getRunningProgram()..[[ folder sitename]])\
- error(\"\", 0)\
- end\
- \
- if not fs.exists(tArgs[1]) then\
- error(\"Folder does not exist\")\
- end\
- \
- if not loadConfig(tArgs[2]) then\
- error(\"Invalid Password\")\
- end\
- \
- local ok, ls = list(\"\")\
- for i,v in pairs(ls) do\
- delete(v)\
- end\
- \
- local function rsync(dir)\
- for i,v in pairs(fs.list(tArgs[2]..dir)) do\
- if fs.isDir(tArgs[2]..dir..\"/\"..v) then\
- print(\"Making dir \"..dir..\"/\"..v)\
- mkdir(dir..\"/\"..v)\
- rsync(dir..\"/\"..v)\
- else\
- print(\"Uploading file \"..dir..\"/\"..v)\
- upload(dir..\"/\"..v, tArgs[2]..dir..\"/\"..v)\
- end\
- end\
- end\
- \
- rsync(\"\")",}
- local function makeFile(_path,_content)
- local file=fs.open(_path,"w")
- file.write(_content)
- file.close()
- end
- local function makeFolder(_path,_content)
- fs.makeDir(_path)
- for k,v in pairs(_content) do
- if type(v)=="table" then
- makeFolder(_path.."/"..k,v)
- else
- makeFile(_path.."/"..k,v)
- end
- end
- end
- local sDest=shell.resolve("fwh") or "/"
- if sDest=="root" then
- sDest="/"
- end
- local tPackage=pkg
- makeFolder(sDest,tPackage)
- print("Package Extracted to '"..sDest.."'!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement