DaGamer12345

CCFTP

Jul 31st, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.64 KB | None | 0 0
  1. --[[FTP Client/Server
  2. Code made by DaGamer12345
  3. You can use this code however you would like, as long as you give me credit for it.
  4. ]]
  5.  
  6. if not fs.exists("/.ftpLog") then -- Checks for the log file
  7.  local file = fs.open("/.ftpLog", "w")
  8.  file.write("FTP server log. All times are in GMT -4 (US Eastern Time)")
  9.  file.close()
  10. end
  11.  
  12. local function Usage() --Prints how to use the file
  13.  print("Usage:")
  14.  print("ftp start <modem side> [password]")
  15.  print("ftp stop <modem side> <server id> <password>")
  16.  print("ftp send <modem side> <server id> <filename>")
  17.  print("ftp get <modem side> <server id> <filename>")
  18.  print("ftp delete <modem side> <server id> <filename>")
  19.  print("ftp list <modem side> <server id> <dir>")
  20.  print("ftp move <modem side> <server id> <source> <destination>")
  21.  print("ftp copy <modem side> <server id> <source> <destination>")
  22.  print("ftp rename <modem side> <server id> <filename> <new name>")
  23.  print("ftp mkdir <modem side> <server id> <path>")
  24. end
  25.  
  26. local tArgs = { ... }
  27. if #tArgs < 2 or #tArgs > 5 then
  28.  Usage()
  29. end
  30.  
  31. local function getTime() --temp
  32.  local getTime = http.get("http://artemix.hu/cctime.php?timezone=America/New_York")
  33.  local timeTable = textutils.unserialize(getTime.readAll())
  34.  hour = timeTable.h
  35.  minute = timeTable.m
  36.  second = timeTable.s
  37. end
  38.  
  39. local function server() --Server loop
  40.  term.clear() --Draw GUI
  41.  term.setCursorPos(1,1)
  42.  term.write("FTP Server")
  43.  if password ~= nil then
  44.   term.setCursorPos(52 - string.len("Password: "..password), 1)
  45.   term.write("Password: "..password)
  46.  else
  47.   term.setCursorPos(36, 1)
  48.   term.write("Password: (none)")
  49.  end
  50.  term.setCursorPos(16, 7)
  51.  print("+----  --+--  +----+")
  52.  print("               |        |    |    |")
  53.  print("               +----    |    +----+")
  54.  print("               |        |    |     ")
  55.  print("               |        |    |     ")
  56.  term.setCursorPos(1,19)
  57.  term.write("[Q] Stop Server")
  58.  term.setCursorPos(52 - string.len("Id: "..os.getComputerID()), 19)
  59.  term.write("Id: "..os.getComputerID())
  60.  getTime()
  61.  while true do
  62.   local secTimer = os.setTimer(1)
  63.   local minTimer = os.setTimer(60)
  64.   local hrTimer = os.setTimer(3600)
  65.   local eventData = {os.pullEventRaw()} --Checks for message, terminate, timer, or char
  66.   if eventData[1] == "timer" then
  67.    if eventData[2] == "secTimer" then
  68.     if not (second < 60) then
  69.      second = second - 60
  70.      minute = minute + 1
  71.     else
  72.      second = second + 1
  73.     end
  74.     time = hour..":"..minute..":"..second
  75.    elseif eventData[2] == "minTimer" then
  76.     if not (minute < 60) then
  77.      minute = minute - 60
  78.      hour = hour + 1
  79.     else
  80.      second = second + 1
  81.     end
  82.     time = hour..":"..minute..":"..second
  83.    elseif eventData[2] == "hrTimer" then
  84.     if not (hour < 24) then
  85.      hour = hour - 24
  86.     else
  87.      hour = hour + 1
  88.     end
  89.     time = hour..":"..minute..":"..second
  90.    end
  91.   elseif eventData[1] == "rednet_message" then --Message
  92.    senderId = eventData[2]
  93.    message = eventData[3]
  94.    if message == "receive_file" then -- Receiving files
  95.     local event, senderId, message, distance = os.pullEvent("rednet_message")
  96.     local file = fs.open("temp", "w")
  97.     file.write(message)
  98.     file.close()
  99.     local event, senderId, message, distance = os.pullEvent("rednet_message")
  100.     if fs.exists(message) then
  101.      rednet.send(senderId, "file_exists")
  102.      local file = fs.open(".ftpLog", "a")
  103.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " tried to upload "..message.." to the server, but it already exists!")
  104.      file.close()
  105.      fs.delete("temp")
  106.     else
  107.      shell.run("rename temp "..message)
  108.      local file = fs.open(".ftpLog", "a")
  109.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " uploaded "..message.." to the server.")
  110.      file.close()
  111.     end
  112.    end
  113.    if message == "send_file" then -- Sending files
  114.     local event, senderId, message, distance = os.pullEvent("rednet_message")
  115.     if not fs.exists(message) or fs.isDir(message) then
  116.      rednet.send(senderId, "invalid_file")
  117.      local file = fs.open(".ftpLog", "a")
  118.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " tried requesting "..message.." from the server, but it doesn't exist!")
  119.      file.close()
  120.     else
  121.      rednet.send(senderId, "file_exists")
  122.      local file = fs.open(message, "r")
  123.      local text = file.readAll()
  124.      file.close()
  125.      rednet.send(senderId, text)
  126.      local file = fs.open(".ftpLog", "a")
  127.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " requested "..message.." from the server.")
  128.      file.close()
  129.     end
  130.    end
  131.    if message == "list_plz" then --Listing files
  132.     local event, senderId, message, distance = os.pullEvent("rednet_message")
  133.     if not fs.isDir(message) then
  134.      rednet.send(senderId, "invalid_dir")
  135.      local file = fs.open(".ftpLog", "a")
  136.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " tried to request contents of the dir "..message..", but it doesn't exist!")
  137.      file.close()
  138.     else
  139.      local tAll = fs.list(message)
  140.      local tFiles = {}
  141.      local tDirs = {}
  142.      for n, sItem in ipairs(tAll) do
  143.       if string.sub(sItem, 1, 1) ~= "." then
  144.        local sPath = fs.combine(message, sItem)
  145.        if fs.isDir(sPath) then
  146.         table.insert(tDirs, sItem)
  147.        else
  148.         table.insert(tFiles, sItem) --ISSUE
  149.        end
  150.       end
  151.      end
  152.      table.sort(tDirs)
  153.      local dirs = textutils.serialize(tDirs)
  154.      table.sort(tFiles)
  155.      local files = textutils.serialize(tFiles)
  156.      rednet.send(senderId, dirs)
  157.      rednet.send(senderId, files)
  158.      local file = fs.open(".ftpLog", "a")
  159.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " requested contents of the dir "..message..".")
  160.      file.close()
  161.     end
  162.    end
  163.    if message == "del_file" then -- Deleting files
  164.     local event, senderId, message, distance = os.pullEvent("rednet_message")
  165.     if not fs.exists(message) or not fs.isDir(message) then
  166.      rednet.send(senderId, "invalid_file")
  167.      local file = fs.open(".ftpLog", "a")
  168.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " tried deleting "..message.." from the server, but it doesn't exist!")
  169.      file.close()
  170.     else
  171.      rednet.send(senderId, "file_exists")
  172.      shell.run("rm "..message)
  173.      local file = fs.open(".ftpLog", "a")
  174.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " deleted "..message.." from the server.")
  175.      file.close()
  176.     end
  177.    end
  178.    if message == "stahp" then --Stopping the server
  179.     if password == nil then
  180.      rednet.send(senderId, "no_pass")
  181.      local file = fs.open(".ftpLog", "a")
  182.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " tried stopping the unstoppable server!")
  183.      file.close()
  184.     end
  185.     if password ~= nil then
  186.      local event, senderId, message, distance = os.pullEvent("rednet_message")
  187.      if message == password then
  188.       rednet.send(senderId, "correct_pass")
  189.       local file = fs.open(".ftpLog", "a")
  190.       file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.. " stopped the server.")
  191.       file.close()
  192.       rednet.close("top")
  193.       os.reboot()
  194.      end
  195.      if message ~= password then
  196.       rednet.send(senderId, "incorrect_pass")
  197.       getTime()
  198.       local file = fs.open(".ftpLog", "a")
  199.       file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried stopping the server with an invalid password! Used "..message.." as the password!")
  200.       file.close()
  201.      end
  202.     end
  203.    end
  204.    if message == "move_file" then --Moving files
  205.     local event, senderId, message, distance = os.pullEvent("rednet_message")
  206.     local src = message
  207.     if not fs.exists(src) then
  208.      rednet.send(senderId, "invalid_file")
  209.      local file = fs.open(".ftpLog", "a")
  210.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried moving "..src.." to a directory, but the file doesn't exist!")
  211.     else
  212.      rednet.send(senderId, "valid_file")
  213.      local event, senderId, message, distance = os.pullEvent("rednet_message")
  214.      local dest = message
  215.      if not fs.isDir(dest) then
  216.       rednet.send(senderId, "invalid_dir")
  217.       local file = fs.open(".ftpLog", "a")
  218.       file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried moving "..src.." to "..dest.. ", but the directory doesn't exist!")
  219.      else
  220.       rednet.send(senderId, "valid_dir")
  221.       shell.run("mv", src, dest)
  222.       local file = fs.open(".ftpLog", "a")
  223.       file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." moved "..src.." to "..dest..".")
  224.      end
  225.     end
  226.    end
  227.    if message == "rename_file" then --Renaming files
  228.     local event, senderId, message, distance = os.pullEvent("rednet_message")
  229.     local filename = message
  230.     if not fs.exists(filename) then
  231.      rednet.send(senderId, "invalid_file")
  232.      local file = fs.open(".ftpLog", "a")
  233.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried renaming "..filename..", but the file doesn't exist!")
  234.     else
  235.      rednet.send(senderId, "valid_file")
  236.      local event, senderId, message, distance = os.pullEvent("rednet_message")
  237.      local name = message
  238.      shell.run("rename", filename, name)
  239.      local file = fs.open(".ftpLog", "a")
  240.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." renamed "..filename.." to "..name..".")
  241.     end
  242.    end
  243.    if message == "make_dir" then --Making a directory
  244.     local event, senderID, message, distance = os.pullEvent("rednet_message")
  245.     local dir = message
  246.     shell.run("mkdir ", dir)
  247.     local file = fs.open(".ftpLog", "a")
  248.     file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." made directory "..dir..".")
  249.    end
  250.    if message == "copy_file" then --Copying files
  251.     local event, senderId, message, distance = os.pullEvent("rednet_message")
  252.     local src = message
  253.     if not fs.exists(src) then
  254.      rednet.send(senderId, "invalid_file")
  255.      local file = fs.open(".ftpLog", "a")
  256.      file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried copying "..src.." to a directory, but the file doesn't exist!")
  257.     else
  258.      rednet.send(senderId, "valid_file")
  259.      local event, senderId, message, distance = os.pullEvent("rednet_message")
  260.      local dest = message
  261.      if not fs.isDir(dest) then
  262.       rednet.send(senderId, "invalid_dir")
  263.       local file = fs.open(".ftpLog", "a")
  264.       file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." tried copying "..src.." to "..dest.. ", but the directory doesn't exist!")
  265.      else
  266.       rednet.send("valid_dir")
  267.       shell.run("cp", src, dest)
  268.       local file = fs.open(".ftpLog", "a")
  269.       file.writeLine("["..hour..":"..minute..":"..second"] "..senderId.." copied "..src.." to "..dest..".")
  270.      end
  271.     end
  272.    end
  273.   elseif eventData[1] == "char" then --Stopping server through keypress
  274.    if eventData[2] == "q" then
  275.     local file = fs.open(".ftpLog", "a")
  276.     file.writeLine("["..hour..":"..minute..":"..second"] Server stopped with keypress")
  277.     file.close()
  278.     term.clear()
  279.     term.setCursorPos(1,1)
  280.     print('FTP log can be found as .ftpLog (read with "edit .ftpLog").')
  281.     error()
  282.    end  
  283.   elseif eventData[1] == "terminate" then --Logs termination
  284.    local file = fs.open(".ftpLog", "a")
  285.    file.writeLine("["..hour..":"..minute..":"..second"] Server forcefully stopped")
  286.    file.close()
  287.    print("Terminated")
  288.    error()
  289.   end
  290.  end
  291. end
  292.  
  293. local cmd = tArgs[1]
  294. local modemSide = tArgs[2]
  295. if cmd == "start" then --Starts Server
  296.  rednet.open(modemSide)
  297.  if tArgs [3] == nil then
  298.   local file = fs.open(".ftpLog", "a")
  299.   file.writeLine("["..hour..":"..minute..":"..second"] Server started with no password.")
  300.   file.close()
  301.   password = nil
  302.  else
  303.   password = tArgs[3]
  304.   local file = fs.open(".ftpLog", "a")
  305.   file.writeLine("["..hour..":"..minute..":"..second"] Server started with password.")
  306.   file.close()
  307.  end
  308.  server()
  309. end
  310.  
  311. if cmd == "send" then --Sends files
  312.  if #tArgs < 4 then
  313.   Usage()
  314.  end
  315.  local serverId = tArgs[3]
  316.  serverId = tonumber(serverId)
  317.  local localFile = tArgs[4]
  318.  local path = shell.resolve(localFile)
  319.  if not fs.exists(path) or fs.isDir(path) then
  320.   print("No such file")
  321.   return
  322.  end
  323.  local sName = fs.getName(path)
  324.  local file = fs.open(path, "r")
  325.  local text = file.readAll()
  326.  file.close()
  327.  print("Sending file "..sName.." to "..serverId.."...")
  328.  rednet.open(modemSide)
  329.  rednet.send(serverId, "receive_file")
  330.  rednet.send(serverId, text)
  331.  sleep(1)
  332.  rednet.send(serverId, sName)
  333.  print("File sent as "..sName..".")
  334.  rednet.close(modemSide)
  335. end
  336.  
  337. if cmd == "get" then --Retreives files
  338.  if #tArgs < 4 then
  339.   Usage()
  340.  end
  341.  local serverId = tArgs[3]
  342.  serverId = tonumber(serverId)
  343.  local getFile = tArgs[4]
  344.  local path = shell.resolve(getFile)
  345.  if fs.exists(path) then
  346.   print("File already exists")
  347.  else
  348.   rednet.open(modemSide)
  349.   rednet.send(serverId, "send_file")
  350.   rednet.send(serverId, getFile)
  351.   local event, senderId, message, distance = os.pullEvent("rednet_message")
  352.   if message == "invalid_file" then
  353.    print("No such file.")
  354.    return
  355.   end
  356.   if message == "file_exists" then
  357.    local event, senderId, message, distance = os.pullEvent("rednet_message")
  358.    local file = fs.open(path, "w")
  359.    file.write(message)
  360.    file.close()
  361.    print("File downloaded as "..getFile)
  362.   end
  363.  end
  364.  rednet.close(modemSide)
  365. end
  366.  
  367. if cmd == "list" then -- Lists files
  368.  if #tArgs < 3 then
  369.   Usage()
  370.  end
  371.  local serverId = tArgs[3]
  372.  serverId = tonumber(serverId)
  373.  local dir = tArgs[4]
  374.  if tArgs[4] == nil then
  375.   dir = "/"
  376.  end
  377.  rednet.open(modemSide)
  378.  rednet.send(serverId, "list_plz")
  379.  rednet.send(serverId, dir)
  380.  local event, senderId, message, distance = os.pullEvent("rednet_message")
  381.  local dirs = textutils.unserialize(message)
  382.  local event, senderId, message, distance = os.pullEvent("rednet_message")
  383.  local files = textutils.unserialize(message)
  384.  textutils.pagedTabulate(dirs, files)
  385.  rednet.close(modemSide)
  386. end
  387.  
  388. if cmd == "delete" then --Deletes files
  389.  if #tArgs < 4 then
  390.   Usage()
  391.  end
  392.  local serverId = tArgs[3]
  393.  serverId = tonumber(serverId)
  394.  local file = tArgs[4]
  395.  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
  396.   print("Access denied.")
  397.  else
  398.   rednet.open(modemSide)
  399.   rednet.send(serverId, "del_file")
  400.   rednet.send(serverId, file)
  401.   local event, senderId, message, distance = os.pullEvent("rednet_message")
  402.   if message == "invalid_file" then
  403.    print("No such file.")
  404.    return
  405.   end
  406.   if message == "file_exists" then
  407.    print(file.." deleted.")
  408.   end
  409.   rednet.close(modemSide)
  410.  end
  411. end
  412.  
  413. if cmd == "stop" then --Stops server remotely
  414.  if #tArgs < 4 then
  415.   Usage()
  416.  end
  417.  local serverId = tArgs[3]
  418.  serverId = tonumber(serverId)
  419.  local password = tArgs[4]
  420.  rednet.open(modemSide)
  421.  rednet.send(serverId, "stahp")
  422.  rednet.send(serverId, password)
  423.  local event, senderId, message, distance = os.pullEvent("rednet_message")
  424.  if message == "no_pass" then
  425.   print("Server does not allow remote stop.")
  426.  end
  427.  if message == "incorrect_pass" then
  428.   print("Password incorrect!")
  429.  end
  430.  if message == "correct_pass" then
  431.   print("Password correct. Server is stopped.")
  432.  end
  433.  rednet.close(modemSide)
  434. end
  435.  
  436. if cmd == "move" then --Moves files
  437.  if #tArgs < 5 then
  438.   Usage()
  439.  end
  440.  local serverId = tArgs[3]
  441.  serverId = tonumber(serverId)
  442.  local src = tArgs[4]
  443.  local dest = tArgs[5]
  444.  rednet.open(modemSide)
  445.  rednet.send(serverId, "move_file")
  446.  rednet.send(serverId, src)
  447.  local event, senderId, message, distance = os.pullEvent("rednet_message")
  448.  if message == "invalid_file" then
  449.   print("File doesn't exist")
  450.  end
  451.  if message == "valid_file" then
  452.   rednet.send(senderId, dest)
  453.   local event, senderId, message, distance = os.pullEvent("rednet_message")
  454.   if message == "invalid_dir" then
  455.    print("Directory doesn't exist")
  456.   end
  457.   if message == "valid_dir" then
  458.    print("Moved "..src.." to "..dest)
  459.   end
  460.  end
  461.  rednet.close(modemSide)
  462. end
  463.  
  464. if cmd == "copy" then --Copies files
  465.  if #tArgs < 5 then
  466.   Usage()
  467.  end
  468.  local serverId = tArgs[3]
  469.  serverId = tonumber(serverId)
  470.  local src = tArgs[4]
  471.  local dest = tArgs[5]
  472.  rednet.open(modemSide)
  473.  rednet.send(serverId, "copy_file")
  474.  rednet.send(serverId, src)
  475.  local event, senderId, message, distance = os.pullEvent("rednet_message")
  476.  if message == "invalid_file" then
  477.   print("File doesn't exist")
  478.  end
  479.  if message == "valid_file" then
  480.   rednet.send(senderId, dest)
  481.   local event, senderId, message, distance = os.pullEvent("rednet_message")
  482.   if message == "invalid_dir" then
  483.    print("Directory doesn't exist")
  484.   end
  485.   if message == "valid_dir" then
  486.    print("Copied "..src.." to "..dest)
  487.   end
  488.  end
  489.  rednet.close(modemSide)
  490. end
  491.  
  492. if cmd == "rename" then --Renames files
  493.  if #tArgs < 5 then
  494.   Usage()
  495.  end
  496.  local serverId = tArgs[3]
  497.  serverId = tonumber(serverId)
  498.  local file = tArgs[4]
  499.  local name = tArgs[5]
  500.  rednet.open(tArgs[2])
  501.  rednet.send(serverId, "rename_file")
  502.  rednet.send(serverId, file)
  503.  local event, senderId, message, distance = os.pullEvent("rednet_message")
  504.  if message == "invalid_file" then
  505.   print("File doesn't exist")
  506.  end
  507.  if message == "valid_file" then
  508.  rednet.send(serverId, name)
  509.  print("Renamed "..file.." to "..name..".")
  510.  end
  511. end
  512.  
  513. if cmd == "mkdir" then --Makes dirs
  514.  if #tArgs < 4 then
  515.   Usage()
  516.  end
  517.  rednet.open(tArgs[2])
  518.  local serverId = tArgs[3]
  519.  serverId = tonumber(serverId)
  520.  local dir = tArgs[4]
  521.  rednet.send(serverId, "make_dir")
  522.  rednet.send(serverId, dir)
  523.  print("Made directory "..dir..".")
  524. end
Advertisement
Add Comment
Please, Sign In to add comment