Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[FTP Client/Server
- Code made by DaGamer12345
- You can use this code however you would like, as long as you give me credit for it.
- ]]
- if not fs.exists("/.ftpLog") then -- Checks for the log file
- local file = fs.open("/.ftpLog", "w")
- file.write("FTP server log. All times are in GMT -4 (US Eastern Time)")
- file.close()
- end
- local function Usage() --Prints how to use the file
- print("Usage:")
- print("ftp start <modem side> [password]")
- print("ftp stop <modem side> <server id> <password>")
- print("ftp send <modem side> <server id> <filename>")
- print("ftp get <modem side> <server id> <filename>")
- print("ftp delete <modem side> <server id> <filename>")
- print("ftp list <modem side> <server id> <dir>")
- print("ftp move <modem side> <server id> <source> <destination>")
- print("ftp copy <modem side> <server id> <source> <destination>")
- print("ftp rename <modem side> <server id> <filename> <new name>")
- print("ftp mkdir <modem side> <server id> <path>")
- end
- local tArgs = { ... }
- if #tArgs < 2 or #tArgs > 5 then
- Usage()
- end
- local function getTime() --temp
- local getTime = http.get("http://artemix.hu/cctime.php?timezone=America/New_York")
- local timeTable = textutils.unserialize(getTime.readAll())
- hour = timeTable.h
- minute = timeTable.m
- second = timeTable.s
- end
- local function server() --Server loop
- term.clear() --Draw GUI
- term.setCursorPos(1,1)
- term.write("FTP Server")
- if password ~= nil then
- term.setCursorPos(52 - string.len("Password: "..password), 1)
- term.write("Password: "..password)
- else
- term.setCursorPos(36, 1)
- term.write("Password: (none)")
- end
- term.setCursorPos(16, 7)
- print("+---- --+-- +----+")
- print(" | | | |")
- print(" +---- | +----+")
- print(" | | | ")
- print(" | | | ")
- term.setCursorPos(1,19)
- term.write("[Q] Stop Server")
- term.setCursorPos(52 - string.len("Id: "..os.getComputerID()), 19)
- term.write("Id: "..os.getComputerID())
- getTime()
- while true do
- local secTimer = os.setTimer(1)
- local minTimer = os.setTimer(60)
- local hrTimer = os.setTimer(3600)
- local eventData = {os.pullEventRaw()} --Checks for message, terminate, timer, or char
- if eventData[1] == "timer" then
- if eventData[2] == "secTimer" then
- if not (second < 60) then
- second = second - 60
- minute = minute + 1
- else
- second = second + 1
- end
- time = hour..":"..minute..":"..second
- elseif eventData[2] == "minTimer" then
- if not (minute < 60) then
- minute = minute - 60
- hour = hour + 1
- else
- second = second + 1
- end
- time = hour..":"..minute..":"..second
- elseif eventData[2] == "hrTimer" then
- if not (hour < 24) then
- hour = hour - 24
- else
- hour = hour + 1
- end
- time = hour..":"..minute..":"..second
- end
- elseif eventData[1] == "rednet_message" then --Message
- senderId = eventData[2]
- message = eventData[3]
- if message == "receive_file" then -- Receiving files
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- local file = fs.open("temp", "w")
- file.write(message)
- file.close()
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if fs.exists(message) then
- rednet.send(senderId, "file_exists")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " tried to upload "..message.." to the server, but it already exists!")
- file.close()
- fs.delete("temp")
- else
- shell.run("rename temp "..message)
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " uploaded "..message.." to the server.")
- file.close()
- end
- end
- if message == "send_file" then -- Sending files
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if not fs.exists(message) or fs.isDir(message) then
- rednet.send(senderId, "invalid_file")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " tried requesting "..message.." from the server, but it doesn't exist!")
- file.close()
- else
- rednet.send(senderId, "file_exists")
- local file = fs.open(message, "r")
- local text = file.readAll()
- file.close()
- rednet.send(senderId, text)
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " requested "..message.." from the server.")
- file.close()
- end
- end
- if message == "list_plz" then --Listing files
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if not fs.isDir(message) then
- rednet.send(senderId, "invalid_dir")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " tried to request contents of the dir "..message..", but it doesn't exist!")
- file.close()
- else
- local tAll = fs.list(message)
- local tFiles = {}
- local tDirs = {}
- for n, sItem in ipairs(tAll) do
- if string.sub(sItem, 1, 1) ~= "." then
- local sPath = fs.combine(message, sItem)
- if fs.isDir(sPath) then
- table.insert(tDirs, sItem)
- else
- table.insert(tFiles, sItem) --ISSUE
- end
- end
- end
- table.sort(tDirs)
- local dirs = textutils.serialize(tDirs)
- table.sort(tFiles)
- local files = textutils.serialize(tFiles)
- rednet.send(senderId, dirs)
- rednet.send(senderId, files)
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " requested contents of the dir "..message..".")
- file.close()
- end
- end
- if message == "del_file" then -- Deleting files
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if not fs.exists(message) or not fs.isDir(message) then
- rednet.send(senderId, "invalid_file")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " tried deleting "..message.." from the server, but it doesn't exist!")
- file.close()
- else
- rednet.send(senderId, "file_exists")
- shell.run("rm "..message)
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " deleted "..message.." from the server.")
- file.close()
- end
- end
- if message == "stahp" then --Stopping the server
- if password == nil then
- rednet.send(senderId, "no_pass")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " tried stopping the unstoppable server!")
- file.close()
- end
- if password ~= nil then
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if message == password then
- rednet.send(senderId, "correct_pass")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " stopped the server.")
- file.close()
- rednet.close("top")
- os.reboot()
- end
- if message ~= password then
- rednet.send(senderId, "incorrect_pass")
- getTime()
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried stopping the server with an invalid password! Used "..message.." as the password!")
- file.close()
- end
- end
- end
- if message == "move_file" then --Moving files
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- local src = message
- if not fs.exists(src) then
- rednet.send(senderId, "invalid_file")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried moving "..src.." to a directory, but the file doesn't exist!")
- else
- rednet.send(senderId, "valid_file")
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- local dest = message
- if not fs.isDir(dest) then
- rednet.send(senderId, "invalid_dir")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried moving "..src.." to "..dest.. ", but the directory doesn't exist!")
- else
- rednet.send(senderId, "valid_dir")
- shell.run("mv", src, dest)
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." moved "..src.." to "..dest..".")
- end
- end
- end
- if message == "rename_file" then --Renaming files
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- local filename = message
- if not fs.exists(filename) then
- rednet.send(senderId, "invalid_file")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried renaming "..filename..", but the file doesn't exist!")
- else
- rednet.send(senderId, "valid_file")
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- local name = message
- shell.run("rename", filename, name)
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." renamed "..filename.." to "..name..".")
- end
- end
- if message == "make_dir" then --Making a directory
- local event, senderID, message, distance = os.pullEvent("rednet_message")
- local dir = message
- shell.run("mkdir ", dir)
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." made directory "..dir..".")
- end
- if message == "copy_file" then --Copying files
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- local src = message
- if not fs.exists(src) then
- rednet.send(senderId, "invalid_file")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried copying "..src.." to a directory, but the file doesn't exist!")
- else
- rednet.send(senderId, "valid_file")
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- local dest = message
- if not fs.isDir(dest) then
- rednet.send(senderId, "invalid_dir")
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried copying "..src.." to "..dest.. ", but the directory doesn't exist!")
- else
- rednet.send("valid_dir")
- shell.run("cp", src, dest)
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." copied "..src.." to "..dest..".")
- end
- end
- end
- elseif eventData[1] == "char" then --Stopping server through keypress
- if eventData[2] == "q" then
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] Server stopped with keypress")
- file.close()
- term.clear()
- term.setCursorPos(1,1)
- print('FTP log can be found as .ftpLog (read with "edit .ftpLog").')
- error()
- end
- elseif eventData[1] == "terminate" then --Logs termination
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] Server forcefully stopped")
- file.close()
- print("Terminated")
- error()
- end
- end
- end
- local cmd = tArgs[1]
- local modemSide = tArgs[2]
- if cmd == "start" then --Starts Server
- rednet.open(modemSide)
- if tArgs [3] == nil then
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] Server started with no password.")
- file.close()
- password = nil
- else
- password = tArgs[3]
- local file = fs.open(".ftpLog", "a")
- file.writeLine("["..hour..":"..minute..":"..second"] Server started with password.")
- file.close()
- end
- server()
- end
- if cmd == "send" then --Sends files
- if #tArgs < 4 then
- Usage()
- end
- local serverId = tArgs[3]
- serverId = tonumber(serverId)
- local localFile = tArgs[4]
- local path = shell.resolve(localFile)
- if not fs.exists(path) or fs.isDir(path) then
- print("No such file")
- return
- end
- local sName = fs.getName(path)
- local file = fs.open(path, "r")
- local text = file.readAll()
- file.close()
- print("Sending file "..sName.." to "..serverId.."...")
- rednet.open(modemSide)
- rednet.send(serverId, "receive_file")
- rednet.send(serverId, text)
- sleep(1)
- rednet.send(serverId, sName)
- print("File sent as "..sName..".")
- rednet.close(modemSide)
- end
- if cmd == "get" then --Retreives files
- if #tArgs < 4 then
- Usage()
- end
- local serverId = tArgs[3]
- serverId = tonumber(serverId)
- local getFile = tArgs[4]
- local path = shell.resolve(getFile)
- if fs.exists(path) then
- print("File already exists")
- else
- rednet.open(modemSide)
- rednet.send(serverId, "send_file")
- rednet.send(serverId, getFile)
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if message == "invalid_file" then
- print("No such file.")
- return
- end
- if message == "file_exists" then
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- local file = fs.open(path, "w")
- file.write(message)
- file.close()
- print("File downloaded as "..getFile)
- end
- end
- rednet.close(modemSide)
- end
- if cmd == "list" then -- Lists files
- if #tArgs < 3 then
- Usage()
- end
- local serverId = tArgs[3]
- serverId = tonumber(serverId)
- local dir = tArgs[4]
- if tArgs[4] == nil then
- dir = "/"
- end
- rednet.open(modemSide)
- rednet.send(serverId, "list_plz")
- rednet.send(serverId, dir)
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- local dirs = textutils.unserialize(message)
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- local files = textutils.unserialize(message)
- textutils.pagedTabulate(dirs, files)
- rednet.close(modemSide)
- end
- if cmd == "delete" then --Deletes files
- if #tArgs < 4 then
- Usage()
- end
- local serverId = tArgs[3]
- serverId = tonumber(serverId)
- local file = tArgs[4]
- if file == "startup" or file == "ftp" or file == ".ftpLog" or file == "disk" or file == "disk1" or file == "disk2" or file == "disk3" or file == "disk4" or file == "disk5" then
- print("Access denied.")
- else
- rednet.open(modemSide)
- rednet.send(serverId, "del_file")
- rednet.send(serverId, file)
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if message == "invalid_file" then
- print("No such file.")
- return
- end
- if message == "file_exists" then
- print(file.." deleted.")
- end
- rednet.close(modemSide)
- end
- end
- if cmd == "stop" then --Stops server remotely
- if #tArgs < 4 then
- Usage()
- end
- local serverId = tArgs[3]
- serverId = tonumber(serverId)
- local password = tArgs[4]
- rednet.open(modemSide)
- rednet.send(serverId, "stahp")
- rednet.send(serverId, password)
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if message == "no_pass" then
- print("Server does not allow remote stop.")
- end
- if message == "incorrect_pass" then
- print("Password incorrect!")
- end
- if message == "correct_pass" then
- print("Password correct. Server is stopped.")
- end
- rednet.close(modemSide)
- end
- if cmd == "move" then --Moves files
- if #tArgs < 5 then
- Usage()
- end
- local serverId = tArgs[3]
- serverId = tonumber(serverId)
- local src = tArgs[4]
- local dest = tArgs[5]
- rednet.open(modemSide)
- rednet.send(serverId, "move_file")
- rednet.send(serverId, src)
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if message == "invalid_file" then
- print("File doesn't exist")
- end
- if message == "valid_file" then
- rednet.send(senderId, dest)
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if message == "invalid_dir" then
- print("Directory doesn't exist")
- end
- if message == "valid_dir" then
- print("Moved "..src.." to "..dest)
- end
- end
- rednet.close(modemSide)
- end
- if cmd == "copy" then --Copies files
- if #tArgs < 5 then
- Usage()
- end
- local serverId = tArgs[3]
- serverId = tonumber(serverId)
- local src = tArgs[4]
- local dest = tArgs[5]
- rednet.open(modemSide)
- rednet.send(serverId, "copy_file")
- rednet.send(serverId, src)
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if message == "invalid_file" then
- print("File doesn't exist")
- end
- if message == "valid_file" then
- rednet.send(senderId, dest)
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if message == "invalid_dir" then
- print("Directory doesn't exist")
- end
- if message == "valid_dir" then
- print("Copied "..src.." to "..dest)
- end
- end
- rednet.close(modemSide)
- end
- if cmd == "rename" then --Renames files
- if #tArgs < 5 then
- Usage()
- end
- local serverId = tArgs[3]
- serverId = tonumber(serverId)
- local file = tArgs[4]
- local name = tArgs[5]
- rednet.open(tArgs[2])
- rednet.send(serverId, "rename_file")
- rednet.send(serverId, file)
- local event, senderId, message, distance = os.pullEvent("rednet_message")
- if message == "invalid_file" then
- print("File doesn't exist")
- end
- if message == "valid_file" then
- rednet.send(serverId, name)
- print("Renamed "..file.." to "..name..".")
- end
- end
- if cmd == "mkdir" then --Makes dirs
- if #tArgs < 4 then
- Usage()
- end
- rednet.open(tArgs[2])
- local serverId = tArgs[3]
- serverId = tonumber(serverId)
- local dir = tArgs[4]
- rednet.send(serverId, "make_dir")
- rednet.send(serverId, dir)
- print("Made directory "..dir..".")
- end
Advertisement
Add Comment
Please, Sign In to add comment