Advertisement
Imgoodisher

fwh site kit

Jul 6th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.13 KB | None | 0 0
  1. local pkg={["fwhapi"]="local id = 62000\
  2. local pass = \"\"\
  3. local site = \"\"\
  4. \
  5. if not fs.exists(\"/fwh\") then\
  6.     fs.makeDir(\"/fwh\")\
  7. end\
  8. \
  9. for i,v in pairs(rs.getSides()) do\
  10.     if peripheral.getType(v) == \"modem\" then\
  11.         rednet.open(v)\
  12.     end\
  13. end\
  14. \
  15. function urlenc(str)\
  16.     return str:gsub(\"[&$;/:%.\\\"\\'\\n]\", function(s) return \"$\"..string.byte(s)..\";\" end)\
  17. end\
  18. \
  19. function request(str)\
  20.     if pass == \"\" or site == \"\" then\
  21.         error(\"Specify the password and site name first!\")\
  22.     end\
  23.     --print(str)\
  24.     rednet.send(id, \"fwh:\"..str)\
  25.     local _, msg = rednet.receive(0.5)\
  26.     --print(tostring(msg))\
  27.     if msg then\
  28.         local ok, resp = msg:match(\"([01])(.+)\")\
  29.         --print(ok..\" \"..tostring(ok==\"1\")..\" \"..resp)\
  30.         if ok and resp then\
  31.             return ok==\"1\", resp\
  32.         else\
  33.             return false, msg\
  34.         end\
  35.     else\
  36.         return false, \"No Response\"\
  37.     end\
  38. end\
  39. \
  40. function setPass(s)\
  41.     pass = urlenc(s)\
  42. end\
  43. \
  44. function setSite(s)\
  45.     site = urlenc(s)\
  46. end\
  47. \
  48. function verify()\
  49.     return request(\"cmd=verify&site=\"..site..\"&pass=\"..pass)\
  50. end\
  51. \
  52. function uploadRaw(servpath, contents)\
  53.     return request(\"cmd=upload&site=\"..site..\"&pass=\"..pass..\"&name=\"..urlenc(servpath)..\"&contents=\"..urlenc(contents))\
  54. end\
  55. \
  56. function upload(servpath, filename)\
  57.     local f = io.open(filename, \"r\")\
  58.     assert(f, \"File does not exist: \"..filename)\
  59.     local data = f:read(\"*a\")\
  60.     f:close()\
  61.     return uploadRaw(servpath, data)\
  62. end\
  63. \
  64. function list(servpath)\
  65.     local ok, ls = request(\"cmd=list&site=\"..site..\"&pass=\"..pass..\"&dir=\"..urlenc(servpath))\
  66.     return ok, textutils.unserialize(ls)\
  67. end\
  68. \
  69. function delete(servpath)\
  70.     return request(\"cmd=delete&site=\"..site..\"&pass=\"..pass..\"&name=\"..urlenc(servpath))\
  71. end\
  72. \
  73. function mkdir(servpath)\
  74.     return request(\"cmd=mkdir&site=\"..site..\"&pass=\"..pass..\"&name=\"..urlenc(servpath))\
  75. end\
  76. \
  77. function isdir(servpath)\
  78.     local ok, resp = request(\"cmd=isdir&site=\"..site..\"&pass=\"..pass..\"&name=\"..urlenc(servpath))\
  79.     if resp == \"true\" then\
  80.         return ok, true\
  81.     elseif resp == \"false\" then\
  82.         return ok, false\
  83.     else\
  84.         return ok, resp\
  85.     end\
  86. end\
  87. \
  88. function get(servpath)\
  89.     return request(\"cmd=get&site=\"..site..\"&pass=\"..pass..\"&name=\"..urlenc(servpath))\
  90. end\
  91. \
  92. function save(servpath, filename)\
  93.     local ok, resp = get(servpath)\
  94.     if not ok then\
  95.         return false, resp\
  96.     else\
  97.         local f = io.open(filename, \"w\")\
  98.         if not f then\
  99.             return false, \"Could not open file\"\
  100.         else\
  101.             f:write(resp)\
  102.             f:close()\
  103.             return true, \"\"\
  104.         end\
  105.     end\
  106. end\
  107. \
  108. function loadConfig(server)\
  109.     setSite(server)\
  110.     if fs.exists(\"/fwh/.cfg_\"..server) then\
  111.         local f = io.open(\"/fwh/.cfg_\"..server, \"r\")\
  112.         setPass(f:read(\"*l\"))\
  113.         f:close()\
  114.         return true\
  115.     else\
  116.         write(\"Password: \")\
  117.         setPass(read(\"*\"))\
  118.         if verify() then\
  119.             local f = io.open(\"/fwh/.cfg_\"..server, \"w\")\
  120.             f:write(pass)\
  121.             f:close()\
  122.             return true\
  123.         else\
  124.             return false\
  125.         end\
  126.     end\
  127. end",["download"]="shell.run(\"/fwh/fwhapi\")\
  128. \
  129. local tArgs = {...}\
  130. if #tArgs ~= 2 then\
  131.     print([[Usage:\
  132. ]]..shell.getRunningProgram()..[[ sitename folder]])\
  133.     error(\"\", 0)\
  134. end\
  135. \
  136. tArgs[2] = tArgs[2]:match(\"^(.-)/?$\")\
  137. \
  138. if not fs.exists(tArgs[2]) then\
  139.     fs.makeDir(tArgs[2])\
  140. end\
  141. \
  142. if not loadConfig(tArgs[1]) then\
  143.     error(\"Invalid Password\")\
  144. end\
  145. \
  146. local function rdl(dir)\
  147.     local ok, ls = list(dir)\
  148.     if not ok then print(\"Error listing dir: \"..ls) return end\
  149.     for i,v in pairs(ls) do\
  150.         local ok, d = isdir(dir..\"/\"..v)\
  151.         if d then\
  152.             print(\"Making dir \"..tArgs[2]..dir..\"/\"..v)\
  153.             fs.makeDir(tArgs[2]..dir..\"/\"..v)\
  154.             rdl(dir..\"/\"..v)\
  155.         else\
  156.             print(\"Saving file \"..tArgs[2]..dir..\"/\"..v)\
  157.             local ok, err = save(dir..\"/\"..v, tArgs[2]..dir..\"/\"..v)\
  158.         end\
  159.         if not ok then print(err) end\
  160.     end\
  161. end\
  162. \
  163. rdl(\"\")",["sync"]="shell.run(\"/fwh/fwhapi\")\
  164. \
  165. local tArgs = {...}\
  166. if #tArgs ~= 2 then\
  167.     print([[Usage:\
  168. ]]..shell.getRunningProgram()..[[ folder sitename]])\
  169.     error(\"\", 0)\
  170. end\
  171. \
  172. if not fs.exists(tArgs[1]) then\
  173.     error(\"Folder does not exist\")\
  174. end\
  175. \
  176. if not loadConfig(tArgs[2]) then\
  177.     error(\"Invalid Password\")\
  178. end\
  179. \
  180. local ok, ls = list(\"\")\
  181. for i,v in pairs(ls) do\
  182.     delete(v)\
  183. end\
  184. \
  185. local function rsync(dir)\
  186.     for i,v in pairs(fs.list(tArgs[2]..dir)) do\
  187.         if fs.isDir(tArgs[2]..dir..\"/\"..v) then\
  188.             print(\"Making dir \"..dir..\"/\"..v)\
  189.             mkdir(dir..\"/\"..v)\
  190.             rsync(dir..\"/\"..v)\
  191.         else\
  192.             print(\"Uploading file \"..dir..\"/\"..v)\
  193.             upload(dir..\"/\"..v, tArgs[2]..dir..\"/\"..v)\
  194.         end\
  195.     end\
  196. end\
  197. \
  198. rsync(\"\")",}
  199. local function makeFile(_path,_content)
  200.  local file=fs.open(_path,"w")
  201.  file.write(_content)
  202.  file.close()
  203. end
  204. local function makeFolder(_path,_content)
  205.  fs.makeDir(_path)
  206.  for k,v in pairs(_content) do
  207.   if type(v)=="table" then
  208.    makeFolder(_path.."/"..k,v)
  209.   else
  210.    makeFile(_path.."/"..k,v)
  211.   end
  212.  end
  213. end
  214. local sDest=shell.resolve("fwh") or "/"
  215. if sDest=="root" then
  216.  sDest="/"
  217. end
  218. local tPackage=pkg
  219. makeFolder(sDest,tPackage)
  220. print("Package Extracted to '"..sDest.."'!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement