Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = { ... }
- if #args < 2 then
- error("Usage: ftp <side> <id>")
- end
- local side = args[1]
- local id = tonumber(args[2])
- local username
- local password = ""
- local dir = ""
- local function split(s, del)
- local ret = {}
- local del = del or " "
- local pattern = string.format("([^%s]+)", del)
- s:gsub(pattern, function(c) ret[#ret+1] = c end)
- return ret
- end
- local function receive()
- while true do
- local src,mes,dst = rednet.receive(1)
- if tonumber(src) == id then
- return mes
- elseif src == nil or mes == nil then
- return ""
- end
- end
- end
- local function getCode(s)
- local splt = split(s, " ")
- return tonumber(splt[1])
- end
- local function connect()
- while true do
- print("Connecting to " .. id .. ", please enter username:")
- username = read()
- rednet.send(id, username)
- local code = getCode(receive())
- if code == 230 then -- Login successful
- print("Login successful.")
- return
- elseif code == 331 then -- Password required
- print("Password required for " .. username .. ":")
- password = read()
- rednet.send(id, password)
- code = getCode(receive())
- if code == 230 then
- print("Login successful.")
- return
- else
- print("Wrong password.")
- end
- elseif code == 500 then
- print("Already logged in.")
- return
- else
- print("Wrong username.")
- end
- end
- end
- --Init
- rednet.open(side)
- term.clear()
- term.setCursorPos(1,1)
- connect()
- -- Mainloop
- while true do
- local command = read()
- local cmdargs = split(command, " ")
- if #cmdargs ~= 0 and cmdargs[1] ~= "" then
- local cmd = cmdargs[1]:lower()
- if cmd == "lcd" then
- if #cmdargs < 2 then
- print("Not enough parameters.")
- else
- dir = fs.combine(dir, cmdargs[2])
- end
- elseif cmd == "ls" then
- rednet.send(id, command)
- local ls = split(receive(), " ")
- local rec = receive()
- local code = getCode(rec)
- if code == 200 then
- textutils.pagedTabulate(ls)
- else
- print(rec)
- end
- elseif cmd == "lls" then
- local directory
- if #cmdargs < 2 then
- directory = dir
- else
- directory = fs.combine(dir, cmdargs[2])
- end
- if not fs.exists(directory) then
- print("File or directory not found.")
- else
- local list = fs.list(directory)
- textutils.pagedTabulate(list)
- end
- elseif cmd == "get" then
- if #cmdargs < 2 then
- print("Not enough parameters.")
- else
- local locfile
- if #cmdargs >= 3 then
- locfile = fs.combine(dir, cmdargs[3])
- else
- locfile = fs.combine(dir, fs.getName(cmdargs[2]))
- end
- rednet.send(id, "GET " .. cmdargs[2])
- local content = receive()
- local rec = receive()
- local code = getCode(rec)
- if code == 200 then
- local file = io.open(locfile, "w")
- if file then
- file:write(content)
- file:close()
- print("File " .. fs.getName(locfile) .. " saved successfully.")
- else
- print("Could not open file for writing.")
- end
- else
- print(rec)
- end
- end
- elseif cmd == "put" then
- if #cmdargs < 1 then
- print("Not enough parameters.")
- else
- local remfile
- rednet.send(id, "pwd")
- local pwd = receive()
- if getCode(receive()) == 200 then
- if #cmdargs >= 3 then
- remfile = fs.combine(pwd, cmdargs[3])
- else
- remfile = fs.combine(pwd, fs.getName(cmdargs[2]))
- end
- rednet.send(id, "PUT " .. remfile)
- local rec = receive()
- if getCode(rec) == 150 then
- local file = io.open(fs.combine(dir, cmdargs[2]), "r")
- if file then
- local content = file:read("*a")
- file:close()
- rednet.send(id, content)
- local rec = receive()
- if getCode(rec) == 200 then
- print("File " .. fs.getName(remfile) .. " uploaded successfully.")
- else
- print(rec)
- end
- else
- print("Could not open file for reading.")
- print("Debug: " .. fs.combine(dir, cmdargs[2]))
- end
- else
- print(rec)
- end
- end
- end
- elseif cmd == "help" then
- print("Commands:")
- textutils.pagedTabulate({"bye", "quit", "cd", "rm", "get", "help", "lcd", "ls", "lls", "mkdir", "put", "pwd"})
- elseif cmd == "pwd" then
- rednet.send(id, command)
- local pwd = receive()
- local rec = receive()
- local code = getCode(rec)
- if code == 200 then
- print(pwd)
- else
- print(rec)
- end
- else
- rednet.send(id, command)
- local rec = receive()
- print(rec)
- if getCode(rec) == 221 then
- break
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement