Advertisement
HawkPB

FTP

Aug 3rd, 2024 (edited)
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.14 KB | None | 0 0
  1. local comp = require "component"
  2. local shell = require("shell")
  3. local thread = require("thread")
  4. local event = require "event"
  5. local fs = require "filesystem"
  6. local modem = comp.modem
  7. local gpu = comp.gpu
  8.  
  9. local args, ops = shell.parse(...)
  10. local opsLength = (function(t)
  11.     local c = 0
  12.     for _ in pairs(t) do
  13.         c = c + 1
  14.     end
  15.     return c
  16. end)(ops)
  17.  
  18. local function splitString(input, delimiter)
  19.     local result = {}
  20.     local pattern = string.format("([^%s]+)", delimiter)
  21.  
  22.     for match in string.gmatch(input, pattern) do
  23.         table.insert(result, match)
  24.     end
  25.  
  26.     return result
  27. end
  28.  
  29. if opsLength == 0 and #args == 0 then
  30.     print("┌File Transfer Protocol")
  31.     print("│")
  32.     print("├ -l = Listen")
  33.     print("├ -p = Port")
  34.     print("├ -s = Send")
  35.     print("├ -f = File")
  36. else
  37.     if ops.l == true then
  38.         print("Mode: Listening")
  39.         if ops.p then
  40.             local port = tonumber(tostring(ops.p))
  41.             if port ~= nil then
  42.                 if (port < 65536) then
  43.                     if not modem.isOpen(port) then
  44.                         print("Opening Port " .. tostring(ops.p))
  45.                         local opennedPort = modem.open(port)
  46.                         if opennedPort then
  47.                             print("Port " .. tostring(ops.p) .. " Open")
  48.                         else
  49.                             gpu.setForeground(0xFF0000)
  50.                             print("Error: Modem error opening port")
  51.                             gpu.setForeground(0xFFFFFF)
  52.                         end
  53.                     else
  54.                         gpu.setForeground(0xFFFF00)
  55.                         print("Warn: Port " .. port .. " Already open")
  56.                         gpu.setForeground(0xFFFFFF)
  57.                         print("Using open port")
  58.                     end
  59.                     print("Creating Listening Thread")
  60.                     thread.create(
  61.                         function()
  62.                             print("{Listening Thread}:  Created")
  63.                             print("{Listening Thread}:  Activating Modem Event Listener")
  64.                             local _, _, from, port, _, message = event.pull("modem_message")
  65.                             if port == port then
  66.                                 print("{Listening Thread}: File Recieved from " .. from)
  67.                                 print("{Listening Thread}: Closing Port " .. port)
  68.                                 modem.close(port)
  69.                                 local destination =
  70.                                     fs.concat(shell.getWorkingDirectory(), splitString(message, "�")[1])
  71.                                 print("{Listening Thread}: Writing File to " .. destination)
  72.                                 local newFile = io.open(destination, "w")
  73.                                 print("i think the data is "..splitString(message, "�")[2])
  74.                                 if not file then
  75.                                     print("no file, ")
  76.                                     gpu.setForeground(0xFF0000)
  77.                                     print("Error: Could not create file, Reason: ")
  78.                                     print("{Listening Thread}: Exiting...")
  79.                                     gpu.setForeground(0xFFFFFF)
  80.                                     return
  81.                                 end
  82.                                 newFile:write(splitString(message, "�")[2])
  83.                                 print("Data: "..splitString(message, "�")[2])
  84.                                 newFile:close()
  85.                                 print("{Listening Thread}: File created!")
  86.                                 print("{Listening Thread}: Exiting...")
  87.                                 return
  88.                             end
  89.                         end
  90.                     )
  91.                 else
  92.                     gpu.setForeground(0xFF0000)
  93.                     print("Error: Max port is 65535")
  94.                     gpu.setForeground(0xFFFFFF)
  95.                 end
  96.             else
  97.                 gpu.setForeground(0xFF0000)
  98.                 print("Error: Please input a number for a port")
  99.                 gpu.setForeground(0xFFFFFF)
  100.             end
  101.         else
  102.             gpu.setForeground(0xFF0000)
  103.             print("Error: Please input a port")
  104.             gpu.setForeground(0xFFFFFF)
  105.         end
  106.     elseif ops.s == true then
  107.         print("Mode: Sending")
  108.         if ops.p then
  109.             local port = tonumber(tostring(ops.p))
  110.             if port ~= nil then
  111.                 if (port < 65536) then
  112.                     if ops.f then
  113.                         if fs.exists(fs.concat(shell.getWorkingDirectory(), tostring(ops.f))) then
  114.                             if not fs.isDirectory(fs.concat(shell.getWorkingDirectory(), tostring(ops.f))) then
  115.                                 local filePath = fs.concat(shell.getWorkingDirectory(), tostring(ops.f))
  116.                                 print("File located as " .. filePath)
  117.                                 local file = io.open(filePath, "r")
  118.                                 if not file then
  119.                                     gpu.setForeground(0xFF0000)
  120.                                     print("Error: File not found")
  121.                                     gpu.setForeground(0xFFFFFF)
  122.                                     return
  123.                                 end
  124.                                 print("File data read")
  125.                                 local content = file:read("*all")
  126.                                 file:close()
  127.                                 print("Broadcasting Data on port " .. tostring(port))
  128.                                 modem.broadcast(port, tostring(ops.f) .. "�" .. content)
  129.                             else
  130.                                 gpu.setForeground(0xFF0000)
  131.                                 print("Error: Please input a file, not a directory")
  132.                                 gpu.setForeground(0xFFFFFF)
  133.                             end
  134.                         else
  135.                             gpu.setForeground(0xFF0000)
  136.                             print(
  137.                                 "Error: There is no file or directory at " ..
  138.                                     fs.concat(shell.getWorkingDirectory(), tostring(ops.f))
  139.                             )
  140.                             gpu.setForeground(0xFFFFFF)
  141.                         end
  142.                     else
  143.                         gpu.setForeground(0xFF0000)
  144.                         print("Error: Please input a file")
  145.                         gpu.setForeground(0xFFFFFF)
  146.                     end
  147.                 else
  148.                     gpu.setForeground(0xFF0000)
  149.                     print("Error: Max port is 65535")
  150.                     gpu.setForeground(0xFFFFFF)
  151.                 end
  152.             else
  153.                 gpu.setForeground(0xFF0000)
  154.                 print("Error: Please input a number for a port")
  155.                 gpu.setForeground(0xFFFFFF)
  156.             end
  157.         else
  158.             gpu.setForeground(0xFF0000)
  159.             print("Error: Please input a port")
  160.             gpu.setForeground(0xFFFFFF)
  161.         end
  162.     end
  163. end
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement