Advertisement
Wiiplay123

InfiniFTP Client

Feb 16th, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.08 KB | None | 0 0
  1. --InfiniFTP Client
  2. --Made by Wiiplay123
  3. local version = 13
  4. if peripheral.getNames == nil then
  5.     peripheral.getNames = rs.getSides
  6. end
  7. local function update(force)
  8.     local prog = shell.getRunningProgram()
  9.     local v1 = http.get("https://bitbucket.org/Wiiplay123/infiniftp/raw/master/latest.txt")
  10.     local v2 = tonumber(v1.readLine())
  11.     if v2 > version or force == true then
  12.         print((force and "Forcing update of " or "Updating ").."InfiniFTP Client v"..version.." to v"..v2.."!")
  13.         sleep(1)
  14.         local v1 = http.get("https://bitbucket.org/Wiiplay123/infiniftp/raw/master/infiniftp.lua")
  15.         local f = fs.open(shell.getRunningProgram(),"w")
  16.         f.write(v1.readAll())
  17.         f.close()
  18.         print("InfiniFTP has been updated to v"..v2.."!")
  19.         if (prog == "startup") then
  20.             os.reboot()
  21.         else
  22.             error("Restart the program to finish updating.",0)
  23.         end
  24.     end
  25. end
  26. if ({...})[1] == "update" then
  27.     update(false)
  28. elseif ({...})[1] == "forceupdate" then
  29.     update(true)
  30. end
  31. if (fs.exists(".infiniftp_temp")) then
  32.     fs.delete(".infiniftp_temp")
  33. end
  34. function ropen()
  35.     for i, v in pairs(peripheral.getNames()) do
  36.         if (peripheral.getType(v) == "modem") then
  37.             _G.rednetside = v
  38.             _G.modem = peripheral.wrap(v)
  39.             rednet.open(v)
  40.             break
  41.         end
  42.     end
  43. end
  44. ropen()
  45. function table_count(tt, item)
  46.   local count
  47.   count = 0
  48.   for ii,xx in pairs(tt) do
  49.     if item == xx then count = count + 1 end
  50.   end
  51.   return count
  52. end
  53. function table_unique(tt)
  54.   local newtable
  55.   newtable = {}
  56.   for ii,xx in ipairs(tt) do
  57.     if(table_count(newtable, xx) == 0) then
  58.       newtable[#newtable+1] = xx
  59.     end
  60.   end
  61.   return newtable
  62. end
  63. function lister(dir, val, id)
  64.     rednet.send(id,"iftplist:"..dir)
  65.     local id2, msg = rednet.receive(1)
  66.     if msg ~= nil then
  67.         local tAll = textutils.unserialize(string.sub(msg,7))
  68.         if val then
  69.             local tFiles = {}
  70.             local tDirs = {}
  71.             for n, sItem in pairs( tAll ) do
  72.                 if string.sub( sItem[1], 1, 1 ) ~= "." then
  73.                     if sItem[2] == true then
  74.                         table.insert( tDirs, sItem[1] )
  75.                     else
  76.                         table.insert( tFiles, sItem[1] )
  77.                     end
  78.                 end
  79.             end
  80.             table.sort( tDirs )
  81.             table.sort( tFiles )
  82.             if term.isColour() then
  83.                 textutils.pagedTabulate( colors.green, tDirs, colours.white, tFiles )
  84.             else
  85.                 textutils.pagedTabulate( tDirs, tFiles )
  86.             end
  87.             return true
  88.         else
  89.             return tAll
  90.         end
  91.     else
  92.         return false
  93.     end
  94. end
  95. function split(pString, pPattern)
  96.     local Table = {}
  97.     local fpat = "(.-)" .. pPattern
  98.     local last_end = 1
  99.     local s, e, cap = pString:find(fpat, 1)
  100.     while s do
  101.         if s ~= 1 or cap ~= "" then
  102.             table.insert(Table,cap)
  103.         end
  104.         last_end = e+1
  105.         s, e, cap = pString:find(fpat, last_end)
  106.     end
  107.     if last_end <= #pString then
  108.         cap = pString:sub(last_end)
  109.         table.insert(Table, cap)
  110.     end
  111.     return Table
  112. end
  113. function exists(path,id)
  114.     rednet.send(id,"exists:"..path)
  115.     for i = 1, 5 do
  116.         local id2, msg = rednet.receive(1)
  117.         if (msg ~= nil) then
  118.             if (id2 == id and type(msg) == "boolean") then
  119.                 return msg
  120.             end
  121.         end
  122.     end
  123. end
  124. function shellLoop(serv, id3)
  125.     local serv2 = serv
  126.     cpass = nil
  127.     while true do
  128.         if term.isColor() then
  129.             term.setTextColor(colors.yellow)
  130.         end
  131.         write("> ")
  132.         if term.isColor() then
  133.             term.setTextColor(colors.white)
  134.         end
  135.         local cmd = split(read()," ")
  136.         if cmd[1] == "dir" or cmd[1] == "ls" or cmd[1] == "list" then
  137.             if #cmd == 2 then
  138.                 local path1 = fs.combine(serv2,cmd[2])
  139.                 local ex1 = exists(fs.combine(serv2,cmd[2]),id3)
  140.                 if ex1 == nil then
  141.                     printError("Server is not responding.")
  142.                 else
  143.                     if ex1 then
  144.                         lister(fs.combine(serv2,cmd[2]),true,id3)
  145.                     else
  146.                         printError("No such directory.")
  147.                     end
  148.                 end
  149.             elseif #cmd == 1 then
  150.                 lister(serv2,true,id3)
  151.             end
  152.         elseif cmd[1] == "cd" then
  153.             if #cmd == 2 then
  154.                 local ex1 = exists(fs.combine(serv2,cmd[2]),id3)
  155.                 if ex1 == nil then
  156.                     printError("Server is not responding.")
  157.                 else
  158.                     if ex1 then
  159.                         serv2 = fs.combine(serv2,cmd[2])
  160.                     else
  161.                         printError("No such directory.")
  162.                     end
  163.                 end
  164.             else
  165.                 printError("Not enough arguments.")
  166.             end
  167.         elseif cmd[1] == "exit" then
  168.             cpass = nil
  169.             break
  170.         elseif cmd[1] == "upload" then
  171.             if #cmd == 2 or #cmd == 3 then
  172.                 if fs.exists(cmd[2]) then
  173.                     local pass = ""
  174.                     local f = fs.open(cmd[2],"r")
  175.                     local f2 = f.readAll()
  176.                     f.close()
  177.                     local idp, msgp = 0, " "
  178.                     if cpass == nil then
  179.                         rednet.send(id3,{"iftpupload",f2,fs.combine(serv2,(cmd[3] ~= nil and cmd[3] or cmd[2]))})
  180.                         local oclock = os.clock() + 4
  181.                         repeat
  182.                             idp, msgp = rednet.receive(1)
  183.                         until idp == id3 or os.clock() > oclock
  184.                         if msgp == "iftp_accepted" and idp == id3 then
  185.                             print("Uploaded successfully.")
  186.                         elseif idp ~= id3 then
  187.                             printError("Server is not responding.")
  188.                         end
  189.                     end
  190.                     if (msgp == "iftp_rejected" and idp == id3) or msgp == " " then
  191.                         if cpass == nil then
  192.                             write("Password: ")
  193.                             pass = read()
  194.                         else
  195.                             pass = cpass
  196.                         end
  197.                         rednet.send(id3,{"iftpupload",f2,fs.combine(serv2,(cmd[3] ~= nil and cmd[3] or cmd[2])),pass})
  198.                         idp, msgp = 0, " "
  199.                         local oclock = os.clock() + 4
  200.                         repeat
  201.                             idp, msgp = rednet.receive(1)
  202.                         until idp == id3 or os.clock() > oclock
  203.                         if msgp == "iftp_rejected" then
  204.                             cpass = nil
  205.                             printError("Incorrect password.")
  206.                         elseif msgp == "iftp_accepted" then
  207.                             cpass = pass
  208.                             print("Uploaded successfully.")
  209.                         else
  210.                             printError("Server is not responding.")
  211.                         end
  212.                     end
  213.                 else
  214.                     printError("No such file.")
  215.                 end
  216.             else
  217.                 printError("This command requires one or two arguments.")
  218.             end
  219.         elseif cmd[1] == "move" or cmd[1] == "mv" then
  220.             if #cmd == 3 then
  221.                 if exists(fs.combine(serv2,cmd[2]),id3) and exists(fs.combine(serv2,cmd[3]),id3) == false then
  222.                     local pass = ""
  223.                     local idp, msgp = 0, " "
  224.                     if cpass == nil then
  225.                         rednet.send(id3,{"iftpmove",fs.combine(serv2,cmd[2]),fs.combine(serv2,cmd[3])})
  226.                         local oclock = os.clock() + 4
  227.                         repeat
  228.                             idp, msgp = rednet.receive(1)
  229.                         until idp == id3 or os.clock() > oclock
  230.                         if msgp == "iftp_accepted" and idp == id3 then
  231.                             print("Moved successfully.")
  232.                         elseif idp ~= id3 then
  233.                             printError("Server is not responding.")
  234.                         end
  235.                     end
  236.                     if (msgp == "iftp_rejected" and idp == id3) or msgp == " " then
  237.                         if cpass == nil then
  238.                             write("Password: ")
  239.                             pass = read()
  240.                         else
  241.                             pass = cpass
  242.                         end
  243.                         rednet.send(id3,{"iftpmove",fs.combine(serv2,cmd[2]),fs.combine(serv2,cmd[3]),pass})
  244.                         idp, msgp = 0, " "
  245.                         local oclock = os.clock() + 4
  246.                         repeat
  247.                             idp, msgp = rednet.receive(1)
  248.                         until idp == id3 or os.clock() > oclock
  249.                         if msgp == "iftp_rejected" then
  250.                             cpass = nil
  251.                             printError("Incorrect password.")
  252.                         elseif msgp == "iftp_accepted" then
  253.                             cpass = pass
  254.                             print("Moved successfully.")
  255.                         else
  256.                             printError("Server is not responding.")
  257.                         end
  258.                     end
  259.                 else
  260.                     if exists(fs.combine(serv2,cmd[2]),id3) then
  261.                         printError("There is already a file at the destination.")
  262.                     else
  263.                         printError("No such file.")
  264.                     end
  265.                 end
  266.             else
  267.                 printError("This command requires two arguments.")
  268.             end
  269.         elseif cmd[1] == "copy" or cmd[1] == "cp" then
  270.             if #cmd == 3 then
  271.                 if exists(fs.combine(serv2,cmd[2]),id3) and exists(fs.combine(serv2,cmd[3]),id3) == false then
  272.                     local pass = ""
  273.                     local idp, msgp = 0, " "
  274.                     if cpass == nil then
  275.                         rednet.send(id3,{"iftpcopy",fs.combine(serv2,cmd[2]),fs.combine(serv2,cmd[3])})
  276.                         local oclock = os.clock() + 4
  277.                         repeat
  278.                             idp, msgp = rednet.receive(1)
  279.                         until idp == id3 or os.clock() > oclock
  280.                         if msgp == "iftp_accepted" and idp == id3 then
  281.                             print("Moved successfully.")
  282.                         elseif idp ~= id3 then
  283.                             printError("Server is not responding.")
  284.                         end
  285.                     end
  286.                     if (msgp == "iftp_rejected" and idp == id3) or msgp == " " or msgp == " " then
  287.                         if cpass == nil then
  288.                             write("Password: ")
  289.                             pass = read()
  290.                         else
  291.                             pass = cpass
  292.                         end
  293.                         rednet.send(id3,{"iftpcopy",fs.combine(serv2,cmd[2]),fs.combine(serv2,cmd[3]),pass})
  294.                         idp, msgp = 0, " "
  295.                         local oclock = os.clock() + 4
  296.                         repeat
  297.                             idp, msgp = rednet.receive(1)
  298.                         until idp == id3 or os.clock() > oclock
  299.                         if msgp == "iftp_rejected" then
  300.                             cpass = nil
  301.                             printError("Incorrect password.")
  302.                         elseif msgp == "iftp_accepted" then
  303.                             cpass = pass
  304.                             print("Moved successfully.")
  305.                         else
  306.                             printError("Server is not responding.")
  307.                         end
  308.                     end
  309.                 else
  310.                     if exists(fs.combine(serv2,cmd[2]),id3) then
  311.                         printError("There is already a file at the destination.")
  312.                     else
  313.                         printError("No such file.")
  314.                     end
  315.                 end
  316.             else
  317.                 printError("This command requires two arguments.")
  318.             end
  319.         elseif cmd[1] == "delete" or cmd[1] == "rm" then
  320.             if #cmd == 2 then
  321.                 if exists(fs.combine(serv2,cmd[2]),id3) then
  322.                     local pass = ""
  323.                     local idp, msgp = 0, " "
  324.                     if cpass == nil then
  325.                         rednet.send(id3,{"iftpdel",fs.combine(serv2,cmd[2])})
  326.                         local oclock = os.clock() + 4
  327.                         repeat
  328.                             idp, msgp = rednet.receive(1)
  329.                         until idp == id3 or os.clock() > oclock
  330.                         if msgp == "iftp_accepted" and idp == id3 then
  331.                             print("Deleted successfully.")
  332.                         elseif idp ~= id3 then
  333.                             printError("Server is not responding.")
  334.                         end
  335.                     end
  336.                     if (msgp == "iftp_rejected" and idp == id3) or msgp == " " then
  337.                         if cpass == nil then
  338.                             write("Password: ")
  339.                             pass = read()
  340.                         else
  341.                             pass = cpass
  342.                         end
  343.                         rednet.send(id3,{"iftpdel",fs.combine(serv2,cmd[2]),pass})
  344.                         idp, msgp = 0, " "
  345.                         local oclock = os.clock() + 4
  346.                         repeat
  347.                             idp, msgp = rednet.receive(1)
  348.                         until idp == id3 or os.clock() > oclock
  349.                         if msgp == "iftp_rejected" then
  350.                             cpass = nil
  351.                             printError("Incorrect password.")
  352.                         elseif msgp == "iftp_accepted" then
  353.                             cpass = pass
  354.                             print("Deleted successfully.")
  355.                         else
  356.                             printError("Server is not responding.")
  357.                         end
  358.                     end
  359.                 else
  360.                     printError("No such file.")
  361.                 end
  362.             else
  363.                 printError("This command requires one argument.")
  364.             end
  365.         elseif cmd[1] == "help" then
  366.             print("InfiniFTP Shell Help")
  367.             print("Use normal shell commands to navigate remote computer.")
  368.             print("Use 'ldir' to view contents of local computer.")
  369.             print("Use 'exit' to exit the InfiniFTP shell.")
  370.             print("Use 'download' to download a file to local computer.")
  371.             print("Use 'upload' to upload a file to remote computer.")
  372.             print("Use 'help' to display this help message.")
  373.             print("Download can take 2 arguments: remote file and local file.")
  374.             print("If second argument is omitted, it goes into root directory.")
  375.             print("Local file can be a directory, and file will simply retain it's name.")
  376.             print("You can also run programs just like in the normal shell.")
  377.         elseif cmd[1] == "ldir" then
  378.             shell.run("dir",unpack(cmd,2))
  379.         elseif cmd[1] == "download" then
  380.             if #cmd == 2 or #cmd == 3 then
  381.                 local ex1 = exists(fs.combine(serv2,cmd[2]),id3)
  382.                 if ex1 == nil then
  383.                     printError("Server is not responding.")
  384.                 else   
  385.                     if ex1 then
  386.                         rednet.send(id3,"ftp:"..fs.combine(serv2,cmd[2]))
  387.                         for i = 1, 5 do
  388.                             id1, msg = rednet.receive(1)
  389.                             if id1 == id3 and string.sub(msg,1,5) == "file:" then
  390.                                 break
  391.                             end
  392.                         end
  393.                         if msg == nil or id1 ~= id3 then
  394.                             printError("Server is not responding.")
  395.                         else
  396.                             if (#cmd == 2) then
  397.                                 local dir = split(cmd[2],"/")
  398.                                 cmd[3] = dir[#dir]
  399.                             elseif (#cmd == 3) then
  400.                                 dir4 = split(cmd[3],"/")
  401.                                 if (fs.isDir(cmd[3])) then
  402.                                     local dir = split(cmd[3],"/")
  403.                                     cmd[3] = fs.combine(cmd[3],dir[#dir])
  404.                                 elseif (fs.exists(table.concat(dir4,"/",1,#dir4 - 1)) == false) then
  405.                                     fs.makeDir(table.concat(dir4,"/",1,#dir4 - 1))
  406.                                 end
  407.                             end
  408.                             local f = fs.open(cmd[3],"w")
  409.                             f.write(string.sub(msg,6))
  410.                             f.close()
  411.                         end
  412.                     else
  413.                         printError("No such file.")
  414.                     end
  415.                 end
  416.             else
  417.                 printError("This command requires 1 or 2 arguments.")
  418.             end
  419.         else
  420.             if exists(fs.combine(serv2,cmd[1]),id3) == true then
  421.                 local a2 = ""
  422.                 repeat
  423.                     print("Warning: You are about to run a program from a remote server!")
  424.                     write("Are you sure? ")
  425.                     a2 = read()
  426.                 until string.lower(a2) == "y" or string.lower(a2) == "n" or string.lower(a2) == "yes" or string.lower(a2) == "no"
  427.                 if (string.lower(a2) == "y" or string.lower(a2) == "yes") then
  428.                     rednet.send(id3,"ftp:"..fs.combine(serv2,cmd[1]))
  429.                     for i = 1, 5 do
  430.                         id1, msg = rednet.receive(1)
  431.                         if id1 == id3 and string.sub(msg,1,5) == "file:" then
  432.                             break
  433.                         end
  434.                     end
  435.                     if msg == nil or id1 ~= id3 then
  436.                         printError("Server is not responding.")
  437.                     else
  438.                         local cmd5 = cmd
  439.                         table.remove(cmd5,1)
  440.                         if (fs.exists(".infiniftp_temp")) then
  441.                             fs.delete(".infiniftp_temp")
  442.                         end
  443.                         fs.makeDir(".infiniftp_temp")
  444.                         local f = fs.open(".infiniftp_temp/temp","w")
  445.                         f.write(string.sub(msg,6))
  446.                         f.close()
  447.                         shell.run(".infiniftp_temp/temp",unpack(cmd5))
  448.                         ropen()
  449.                         if (fs.exists(".infiniftp_temp")) then
  450.                             fs.delete(".infiniftp_temp")
  451.                         end
  452.                     end
  453.                 end
  454.             else
  455.                 printError("Invalid command.")
  456.             end
  457.         end
  458.     end
  459. end
  460. function dns()
  461.     local list = {}
  462.     rednet.broadcast("infiniftp_dns")
  463.     while true do
  464.         local id, msg = rednet.receive(0.2)
  465.         if (msg == nil) then
  466.             break
  467.         else
  468.             if (string.sub(msg,1,5) == "iftp:") then
  469.                 local tab = textutils.unserialize(string.sub(msg,6))
  470.                 for i, v in pairs(tab) do
  471.                     table.insert(list,{v,id})
  472.                 end
  473.             end
  474.         end
  475.     end
  476.     return table_unique(list)
  477. end
  478. local c = 0
  479. local a = ""
  480. repeat
  481. repeat
  482.     if term.isColor() then
  483.         term.setTextColor(colors.white)
  484.     end
  485.     sleep(0.2)
  486.     list = dns()
  487.     if (#list == 0) then
  488.         printError("No servers found!")
  489.         a = "exit"
  490.         break
  491.     else
  492.         term.clear()
  493.         term.setCursorPos(1,1)
  494.         if term.isColor() then
  495.             term.setTextColor(colors.yellow)
  496.         end
  497.         print("InfiniFTP v"..version.." Server List")
  498.         if term.isColor() then
  499.             term.setTextColor(colors.white)
  500.         end
  501.         for i, v in pairs(list) do
  502.             print(i..". "..v[1].." (ID: "..v[2]..")")
  503.             sleep(0.1)
  504.         end
  505.         print("Type the number before the server's name.")
  506.         print("Or type 'exit' to exit.")
  507.         if term.isColor() then
  508.             term.setTextColor(colors.yellow)
  509.         end
  510.         write("> ")
  511.         if term.isColor() then
  512.             term.setTextColor(colors.white)
  513.         end
  514.         a = read()
  515.         c = tonumber(a)
  516.     end
  517. until list[c] ~= nil or a == "exit"
  518. if string.lower(a) ~= "exit" then
  519.     term.clear()
  520.     term.setCursorPos(1,1)
  521.     if term.isColor() then
  522.         term.setTextColor(colors.yellow)
  523.     end
  524.     print("InfiniFTP v"..version.." Shell")
  525.     if term.isColor() then
  526.         term.setTextColor(colors.white)
  527.     end
  528.     shellLoop(unpack(list[c]))
  529. end
  530. until a == "exit"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement