Advertisement
Guest User

FTP.lua

a guest
Aug 16th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.35 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local modem = component.modem
  4. local keyboard = require("keyboard")
  5. local DNSAddress = "9955471e-b28f-4001-9d66-79c28c75be2d"
  6. local homePath = "/home/"
  7. local messageArray = {}
  8. local brokenMessageArray = {}
  9. local maxSize
  10.  
  11. local port = 21
  12. local timeout = 5
  13.  
  14. function OpenModem(port)
  15.   modem.open(port)
  16.   if modem.isOpen(port) then
  17.     print("Successfully opened port " .. port)
  18.   else
  19.     print("Could not open port " .. port .. ", closing server")
  20.   end
  21. end
  22.  
  23. function Quit()
  24.   modem.close(21)
  25.   if modem.isOpen(21) == false then
  26.     print("Successfully closed port 21, closing program...")
  27.   else
  28.     print("Could not close port 21, please contact admin.")
  29.   end
  30.   os.exit()
  31. end
  32.  
  33. function EventPull(timeout)
  34.   for i=1,10 do
  35.     local type, _, foreignAddress, port, distance, message = event.pull(timeout)
  36.     local pullArray = {}
  37.     if type == "key_down" then
  38.       if keyboard.isAltDown() then
  39.         Quit()
  40.       end
  41.     elseif type == "modem_message" then
  42.       pullArray[1] = type
  43.       pullArray[2] = foreignAddress
  44.       pullArray[3] = port
  45.       pullArray[4] = distance
  46.       pullArray[5] = message
  47.       print("Message has been received from DNS on port " .. port)
  48.       --return pullArray
  49.     end
  50.     if pullArray[1] ~= nil then
  51.       return pullArray
  52.     end
  53.     i = i + 1
  54.   end
  55. end
  56.  
  57. function SendACK(remoteAddress, port)
  58.   modem.send(remoteAddress, port, "ACK")
  59.   print("ACK has been sent to " .. remoteAddress .. " on port " .. port)
  60. end
  61.  
  62. local function SendTimeout(remoteAddress, port, message, timeout)
  63.   local timeoutPull = {}
  64.   while true do
  65.     modem.send(remoteAddress, port, message)
  66.     timeoutPull = EventPull(timeout)
  67.     if timeoutPull[1] ~= nil then
  68.       break
  69.     end
  70.   end
  71. end
  72.  
  73. local function OpenFile(fileName)
  74.   local path = homePath .. fileName
  75.   local file = io.open(path, "r")
  76.   local fileSize, _ = file:seek("end")
  77.   file:seek("set")
  78.   fileString = file:read(fileSize)
  79.   file:close()
  80.   return fileString
  81. end
  82.  
  83. local function PacketBreak(inputString)
  84.   local tempArray = {}
  85.   tempArray[3] = "This is to stop stupid LUA errors"
  86.   maxSize = (modem.maxPacketSize()) - 2 --subtraction of 2 is to account for overhead of message
  87.   stringLength = string.len(inputString)
  88.   if stringLength > maxSize then
  89.     print("File is too long, splitting into multiple files...")
  90.     stringOne = string.sub(inputString, 1, math.floor(stringLength/2))
  91.     stringTwo = string.sub(inputString, (math.floor(stringLength/2)) + 1, stringLength)
  92.     tempArray[1] = stringOne
  93.     tempArray[2] = stringTwo
  94.   end
  95.   return tempArray
  96. end
  97.  
  98. local args = {...} --send/recieve, who to send to, filename
  99. if args[1] == nil then error("Provide function of the program (send/recieve).") end
  100. if args[2] == nil then error("Provide who you are sending this file to or receive from.") end
  101.  
  102. OpenModem(port)
  103. if args[1] == "send" then
  104.   if args[3] == nil then error("Provide the file name.") end
  105.   print("Preparing to send file...")
  106.   local sendString = OpenFile(args[3])
  107.   if sendString == nil then
  108.     print("The target file is empty. Choose another.")
  109.     Quit()
  110.   end
  111.   brokenMessageArray = PacketBreak(sendString)
  112.   if brokenMessageArray[1] == nil then
  113.     SendTimeout(OpenFile(args[2]), port, sendString, timeout)
  114.     print("File sent to " .. args[2] .. " over port " .. port)
  115.     SendTimeout(OpenFile(args[2]), port, "DONE", timeout)
  116.   else
  117.     for i, loopString in ipairs(brokenMessageArray) do
  118.       SendTimeout(OpenFile(args[2]), port, loopString, timeout)
  119.     end
  120.     SendTimeout(OpenFile(args[2]), port, "DONE", timeout)
  121.   end
  122. elseif args[1] == "receive" then
  123.   if args[3] == nil then error("Provide a name for the file.") end
  124.   print("Preparing to receive file at path " .. args[3])
  125.   messageArray = EventPull(timeout)
  126.   SendACK(messageArray[2], messageArray[3])
  127.   local receivedFile = io.open(homePath .. args[3], "w")
  128.   receivedFile:write(messageArray[5])
  129.   receivedFile:flush()
  130.   receivedFile:close()
  131.   local finished = false
  132.   while finished == false do
  133.     messageArray = EventPull(timeout)
  134.     SendACK(messageArray[2], messageArray[3])
  135.     if messageArray[5] == "DONE" then
  136.       finished = true
  137.     else
  138.       local receivedFile = io.open(homePath .. args[3], "a")
  139.       receivedFile:write(messageArray[5])
  140.       receivedFile:flush()
  141.       receivedFile:close()
  142.     end
  143.   end
  144.   print("File " .. args[3] .. " has been saved with the path " .. homePath .. args[3])
  145. end
  146. Quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement