Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --InfiniFTP Client
- --Made by Wiiplay123
- local version = 13
- if peripheral.getNames == nil then
- peripheral.getNames = rs.getSides
- end
- local function update(force)
- local prog = shell.getRunningProgram()
- local v1 = http.get("https://bitbucket.org/Wiiplay123/infiniftp/raw/master/latest.txt")
- local v2 = tonumber(v1.readLine())
- if v2 > version or force == true then
- print((force and "Forcing update of " or "Updating ").."InfiniFTP Client v"..version.." to v"..v2.."!")
- sleep(1)
- local v1 = http.get("https://bitbucket.org/Wiiplay123/infiniftp/raw/master/infiniftp.lua")
- local f = fs.open(shell.getRunningProgram(),"w")
- f.write(v1.readAll())
- f.close()
- print("InfiniFTP has been updated to v"..v2.."!")
- if (prog == "startup") then
- os.reboot()
- else
- error("Restart the program to finish updating.",0)
- end
- end
- end
- if ({...})[1] == "update" then
- update(false)
- elseif ({...})[1] == "forceupdate" then
- update(true)
- end
- if (fs.exists(".infiniftp_temp")) then
- fs.delete(".infiniftp_temp")
- end
- function ropen()
- for i, v in pairs(peripheral.getNames()) do
- if (peripheral.getType(v) == "modem") then
- _G.rednetside = v
- _G.modem = peripheral.wrap(v)
- rednet.open(v)
- break
- end
- end
- end
- ropen()
- function table_count(tt, item)
- local count
- count = 0
- for ii,xx in pairs(tt) do
- if item == xx then count = count + 1 end
- end
- return count
- end
- function table_unique(tt)
- local newtable
- newtable = {}
- for ii,xx in ipairs(tt) do
- if(table_count(newtable, xx) == 0) then
- newtable[#newtable+1] = xx
- end
- end
- return newtable
- end
- function lister(dir, val, id)
- rednet.send(id,"iftplist:"..dir)
- local id2, msg = rednet.receive(1)
- if msg ~= nil then
- local tAll = textutils.unserialize(string.sub(msg,7))
- if val then
- local tFiles = {}
- local tDirs = {}
- for n, sItem in pairs( tAll ) do
- if string.sub( sItem[1], 1, 1 ) ~= "." then
- if sItem[2] == true then
- table.insert( tDirs, sItem[1] )
- else
- table.insert( tFiles, sItem[1] )
- end
- end
- end
- table.sort( tDirs )
- table.sort( tFiles )
- if term.isColour() then
- textutils.pagedTabulate( colors.green, tDirs, colours.white, tFiles )
- else
- textutils.pagedTabulate( tDirs, tFiles )
- end
- return true
- else
- return tAll
- end
- else
- return false
- end
- end
- function split(pString, pPattern)
- local Table = {}
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table,cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- function exists(path,id)
- rednet.send(id,"exists:"..path)
- for i = 1, 5 do
- local id2, msg = rednet.receive(1)
- if (msg ~= nil) then
- if (id2 == id and type(msg) == "boolean") then
- return msg
- end
- end
- end
- end
- function shellLoop(serv, id3)
- local serv2 = serv
- cpass = nil
- while true do
- if term.isColor() then
- term.setTextColor(colors.yellow)
- end
- write("> ")
- if term.isColor() then
- term.setTextColor(colors.white)
- end
- local cmd = split(read()," ")
- if cmd[1] == "dir" or cmd[1] == "ls" or cmd[1] == "list" then
- if #cmd == 2 then
- local path1 = fs.combine(serv2,cmd[2])
- local ex1 = exists(fs.combine(serv2,cmd[2]),id3)
- if ex1 == nil then
- printError("Server is not responding.")
- else
- if ex1 then
- lister(fs.combine(serv2,cmd[2]),true,id3)
- else
- printError("No such directory.")
- end
- end
- elseif #cmd == 1 then
- lister(serv2,true,id3)
- end
- elseif cmd[1] == "cd" then
- if #cmd == 2 then
- local ex1 = exists(fs.combine(serv2,cmd[2]),id3)
- if ex1 == nil then
- printError("Server is not responding.")
- else
- if ex1 then
- serv2 = fs.combine(serv2,cmd[2])
- else
- printError("No such directory.")
- end
- end
- else
- printError("Not enough arguments.")
- end
- elseif cmd[1] == "exit" then
- cpass = nil
- break
- elseif cmd[1] == "upload" then
- if #cmd == 2 or #cmd == 3 then
- if fs.exists(cmd[2]) then
- local pass = ""
- local f = fs.open(cmd[2],"r")
- local f2 = f.readAll()
- f.close()
- local idp, msgp = 0, " "
- if cpass == nil then
- rednet.send(id3,{"iftpupload",f2,fs.combine(serv2,(cmd[3] ~= nil and cmd[3] or cmd[2]))})
- local oclock = os.clock() + 4
- repeat
- idp, msgp = rednet.receive(1)
- until idp == id3 or os.clock() > oclock
- if msgp == "iftp_accepted" and idp == id3 then
- print("Uploaded successfully.")
- elseif idp ~= id3 then
- printError("Server is not responding.")
- end
- end
- if (msgp == "iftp_rejected" and idp == id3) or msgp == " " then
- if cpass == nil then
- write("Password: ")
- pass = read()
- else
- pass = cpass
- end
- rednet.send(id3,{"iftpupload",f2,fs.combine(serv2,(cmd[3] ~= nil and cmd[3] or cmd[2])),pass})
- idp, msgp = 0, " "
- local oclock = os.clock() + 4
- repeat
- idp, msgp = rednet.receive(1)
- until idp == id3 or os.clock() > oclock
- if msgp == "iftp_rejected" then
- cpass = nil
- printError("Incorrect password.")
- elseif msgp == "iftp_accepted" then
- cpass = pass
- print("Uploaded successfully.")
- else
- printError("Server is not responding.")
- end
- end
- else
- printError("No such file.")
- end
- else
- printError("This command requires one or two arguments.")
- end
- elseif cmd[1] == "move" or cmd[1] == "mv" then
- if #cmd == 3 then
- if exists(fs.combine(serv2,cmd[2]),id3) and exists(fs.combine(serv2,cmd[3]),id3) == false then
- local pass = ""
- local idp, msgp = 0, " "
- if cpass == nil then
- rednet.send(id3,{"iftpmove",fs.combine(serv2,cmd[2]),fs.combine(serv2,cmd[3])})
- local oclock = os.clock() + 4
- repeat
- idp, msgp = rednet.receive(1)
- until idp == id3 or os.clock() > oclock
- if msgp == "iftp_accepted" and idp == id3 then
- print("Moved successfully.")
- elseif idp ~= id3 then
- printError("Server is not responding.")
- end
- end
- if (msgp == "iftp_rejected" and idp == id3) or msgp == " " then
- if cpass == nil then
- write("Password: ")
- pass = read()
- else
- pass = cpass
- end
- rednet.send(id3,{"iftpmove",fs.combine(serv2,cmd[2]),fs.combine(serv2,cmd[3]),pass})
- idp, msgp = 0, " "
- local oclock = os.clock() + 4
- repeat
- idp, msgp = rednet.receive(1)
- until idp == id3 or os.clock() > oclock
- if msgp == "iftp_rejected" then
- cpass = nil
- printError("Incorrect password.")
- elseif msgp == "iftp_accepted" then
- cpass = pass
- print("Moved successfully.")
- else
- printError("Server is not responding.")
- end
- end
- else
- if exists(fs.combine(serv2,cmd[2]),id3) then
- printError("There is already a file at the destination.")
- else
- printError("No such file.")
- end
- end
- else
- printError("This command requires two arguments.")
- end
- elseif cmd[1] == "copy" or cmd[1] == "cp" then
- if #cmd == 3 then
- if exists(fs.combine(serv2,cmd[2]),id3) and exists(fs.combine(serv2,cmd[3]),id3) == false then
- local pass = ""
- local idp, msgp = 0, " "
- if cpass == nil then
- rednet.send(id3,{"iftpcopy",fs.combine(serv2,cmd[2]),fs.combine(serv2,cmd[3])})
- local oclock = os.clock() + 4
- repeat
- idp, msgp = rednet.receive(1)
- until idp == id3 or os.clock() > oclock
- if msgp == "iftp_accepted" and idp == id3 then
- print("Moved successfully.")
- elseif idp ~= id3 then
- printError("Server is not responding.")
- end
- end
- if (msgp == "iftp_rejected" and idp == id3) or msgp == " " or msgp == " " then
- if cpass == nil then
- write("Password: ")
- pass = read()
- else
- pass = cpass
- end
- rednet.send(id3,{"iftpcopy",fs.combine(serv2,cmd[2]),fs.combine(serv2,cmd[3]),pass})
- idp, msgp = 0, " "
- local oclock = os.clock() + 4
- repeat
- idp, msgp = rednet.receive(1)
- until idp == id3 or os.clock() > oclock
- if msgp == "iftp_rejected" then
- cpass = nil
- printError("Incorrect password.")
- elseif msgp == "iftp_accepted" then
- cpass = pass
- print("Moved successfully.")
- else
- printError("Server is not responding.")
- end
- end
- else
- if exists(fs.combine(serv2,cmd[2]),id3) then
- printError("There is already a file at the destination.")
- else
- printError("No such file.")
- end
- end
- else
- printError("This command requires two arguments.")
- end
- elseif cmd[1] == "delete" or cmd[1] == "rm" then
- if #cmd == 2 then
- if exists(fs.combine(serv2,cmd[2]),id3) then
- local pass = ""
- local idp, msgp = 0, " "
- if cpass == nil then
- rednet.send(id3,{"iftpdel",fs.combine(serv2,cmd[2])})
- local oclock = os.clock() + 4
- repeat
- idp, msgp = rednet.receive(1)
- until idp == id3 or os.clock() > oclock
- if msgp == "iftp_accepted" and idp == id3 then
- print("Deleted successfully.")
- elseif idp ~= id3 then
- printError("Server is not responding.")
- end
- end
- if (msgp == "iftp_rejected" and idp == id3) or msgp == " " then
- if cpass == nil then
- write("Password: ")
- pass = read()
- else
- pass = cpass
- end
- rednet.send(id3,{"iftpdel",fs.combine(serv2,cmd[2]),pass})
- idp, msgp = 0, " "
- local oclock = os.clock() + 4
- repeat
- idp, msgp = rednet.receive(1)
- until idp == id3 or os.clock() > oclock
- if msgp == "iftp_rejected" then
- cpass = nil
- printError("Incorrect password.")
- elseif msgp == "iftp_accepted" then
- cpass = pass
- print("Deleted successfully.")
- else
- printError("Server is not responding.")
- end
- end
- else
- printError("No such file.")
- end
- else
- printError("This command requires one argument.")
- end
- elseif cmd[1] == "help" then
- print("InfiniFTP Shell Help")
- print("Use normal shell commands to navigate remote computer.")
- print("Use 'ldir' to view contents of local computer.")
- print("Use 'exit' to exit the InfiniFTP shell.")
- print("Use 'download' to download a file to local computer.")
- print("Use 'upload' to upload a file to remote computer.")
- print("Use 'help' to display this help message.")
- print("Download can take 2 arguments: remote file and local file.")
- print("If second argument is omitted, it goes into root directory.")
- print("Local file can be a directory, and file will simply retain it's name.")
- print("You can also run programs just like in the normal shell.")
- elseif cmd[1] == "ldir" then
- shell.run("dir",unpack(cmd,2))
- elseif cmd[1] == "download" then
- if #cmd == 2 or #cmd == 3 then
- local ex1 = exists(fs.combine(serv2,cmd[2]),id3)
- if ex1 == nil then
- printError("Server is not responding.")
- else
- if ex1 then
- rednet.send(id3,"ftp:"..fs.combine(serv2,cmd[2]))
- for i = 1, 5 do
- id1, msg = rednet.receive(1)
- if id1 == id3 and string.sub(msg,1,5) == "file:" then
- break
- end
- end
- if msg == nil or id1 ~= id3 then
- printError("Server is not responding.")
- else
- if (#cmd == 2) then
- local dir = split(cmd[2],"/")
- cmd[3] = dir[#dir]
- elseif (#cmd == 3) then
- dir4 = split(cmd[3],"/")
- if (fs.isDir(cmd[3])) then
- local dir = split(cmd[3],"/")
- cmd[3] = fs.combine(cmd[3],dir[#dir])
- elseif (fs.exists(table.concat(dir4,"/",1,#dir4 - 1)) == false) then
- fs.makeDir(table.concat(dir4,"/",1,#dir4 - 1))
- end
- end
- local f = fs.open(cmd[3],"w")
- f.write(string.sub(msg,6))
- f.close()
- end
- else
- printError("No such file.")
- end
- end
- else
- printError("This command requires 1 or 2 arguments.")
- end
- else
- if exists(fs.combine(serv2,cmd[1]),id3) == true then
- local a2 = ""
- repeat
- print("Warning: You are about to run a program from a remote server!")
- write("Are you sure? ")
- a2 = read()
- until string.lower(a2) == "y" or string.lower(a2) == "n" or string.lower(a2) == "yes" or string.lower(a2) == "no"
- if (string.lower(a2) == "y" or string.lower(a2) == "yes") then
- rednet.send(id3,"ftp:"..fs.combine(serv2,cmd[1]))
- for i = 1, 5 do
- id1, msg = rednet.receive(1)
- if id1 == id3 and string.sub(msg,1,5) == "file:" then
- break
- end
- end
- if msg == nil or id1 ~= id3 then
- printError("Server is not responding.")
- else
- local cmd5 = cmd
- table.remove(cmd5,1)
- if (fs.exists(".infiniftp_temp")) then
- fs.delete(".infiniftp_temp")
- end
- fs.makeDir(".infiniftp_temp")
- local f = fs.open(".infiniftp_temp/temp","w")
- f.write(string.sub(msg,6))
- f.close()
- shell.run(".infiniftp_temp/temp",unpack(cmd5))
- ropen()
- if (fs.exists(".infiniftp_temp")) then
- fs.delete(".infiniftp_temp")
- end
- end
- end
- else
- printError("Invalid command.")
- end
- end
- end
- end
- function dns()
- local list = {}
- rednet.broadcast("infiniftp_dns")
- while true do
- local id, msg = rednet.receive(0.2)
- if (msg == nil) then
- break
- else
- if (string.sub(msg,1,5) == "iftp:") then
- local tab = textutils.unserialize(string.sub(msg,6))
- for i, v in pairs(tab) do
- table.insert(list,{v,id})
- end
- end
- end
- end
- return table_unique(list)
- end
- local c = 0
- local a = ""
- repeat
- repeat
- if term.isColor() then
- term.setTextColor(colors.white)
- end
- sleep(0.2)
- list = dns()
- if (#list == 0) then
- printError("No servers found!")
- a = "exit"
- break
- else
- term.clear()
- term.setCursorPos(1,1)
- if term.isColor() then
- term.setTextColor(colors.yellow)
- end
- print("InfiniFTP v"..version.." Server List")
- if term.isColor() then
- term.setTextColor(colors.white)
- end
- for i, v in pairs(list) do
- print(i..". "..v[1].." (ID: "..v[2]..")")
- sleep(0.1)
- end
- print("Type the number before the server's name.")
- print("Or type 'exit' to exit.")
- if term.isColor() then
- term.setTextColor(colors.yellow)
- end
- write("> ")
- if term.isColor() then
- term.setTextColor(colors.white)
- end
- a = read()
- c = tonumber(a)
- end
- until list[c] ~= nil or a == "exit"
- if string.lower(a) ~= "exit" then
- term.clear()
- term.setCursorPos(1,1)
- if term.isColor() then
- term.setTextColor(colors.yellow)
- end
- print("InfiniFTP v"..version.." Shell")
- if term.isColor() then
- term.setTextColor(colors.white)
- end
- shellLoop(unpack(list[c]))
- end
- until a == "exit"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement