Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local comp = require "component"
- local shell = require("shell")
- local thread = require("thread")
- local event = require "event"
- local fs = require "filesystem"
- local modem = comp.modem
- local gpu = comp.gpu
- local args, ops = shell.parse(...)
- local opsLength = (function(t)
- local c = 0
- for _ in pairs(t) do
- c = c + 1
- end
- return c
- end)(ops)
- local function splitString(input, delimiter)
- local result = {}
- local pattern = string.format("([^%s]+)", delimiter)
- for match in string.gmatch(input, pattern) do
- table.insert(result, match)
- end
- return result
- end
- if opsLength == 0 and #args == 0 then
- print("┌File Transfer Protocol")
- print("│")
- print("├ -l = Listen")
- print("├ -p = Port")
- print("├ -s = Send")
- print("├ -f = File")
- else
- if ops.l == true then
- print("Mode: Listening")
- if ops.p then
- local port = tonumber(tostring(ops.p))
- if port ~= nil then
- if (port < 65536) then
- if not modem.isOpen(port) then
- print("Opening Port " .. tostring(ops.p))
- local opennedPort = modem.open(port)
- if opennedPort then
- print("Port " .. tostring(ops.p) .. " Open")
- else
- gpu.setForeground(0xFF0000)
- print("Error: Modem error opening port")
- gpu.setForeground(0xFFFFFF)
- end
- else
- gpu.setForeground(0xFFFF00)
- print("Warn: Port " .. port .. " Already open")
- gpu.setForeground(0xFFFFFF)
- print("Using open port")
- end
- print("Creating Listening Thread")
- thread.create(
- function()
- print("{Listening Thread}: Created")
- print("{Listening Thread}: Activating Modem Event Listener")
- local _, _, from, port, _, message = event.pull("modem_message")
- if port == port then
- print("{Listening Thread}: File Recieved from " .. from)
- print("{Listening Thread}: Closing Port " .. port)
- modem.close(port)
- local destination =
- fs.concat(shell.getWorkingDirectory(), splitString(message, "�")[1])
- print("{Listening Thread}: Writing File to " .. destination)
- local newFile = io.open(destination, "w")
- print("i think the data is "..splitString(message, "�")[2])
- if not file then
- print("no file, ")
- gpu.setForeground(0xFF0000)
- print("Error: Could not create file, Reason: ")
- print("{Listening Thread}: Exiting...")
- gpu.setForeground(0xFFFFFF)
- return
- end
- newFile:write(splitString(message, "�")[2])
- print("Data: "..splitString(message, "�")[2])
- newFile:close()
- print("{Listening Thread}: File created!")
- print("{Listening Thread}: Exiting...")
- return
- end
- end
- )
- else
- gpu.setForeground(0xFF0000)
- print("Error: Max port is 65535")
- gpu.setForeground(0xFFFFFF)
- end
- else
- gpu.setForeground(0xFF0000)
- print("Error: Please input a number for a port")
- gpu.setForeground(0xFFFFFF)
- end
- else
- gpu.setForeground(0xFF0000)
- print("Error: Please input a port")
- gpu.setForeground(0xFFFFFF)
- end
- elseif ops.s == true then
- print("Mode: Sending")
- if ops.p then
- local port = tonumber(tostring(ops.p))
- if port ~= nil then
- if (port < 65536) then
- if ops.f then
- if fs.exists(fs.concat(shell.getWorkingDirectory(), tostring(ops.f))) then
- if not fs.isDirectory(fs.concat(shell.getWorkingDirectory(), tostring(ops.f))) then
- local filePath = fs.concat(shell.getWorkingDirectory(), tostring(ops.f))
- print("File located as " .. filePath)
- local file = io.open(filePath, "r")
- if not file then
- gpu.setForeground(0xFF0000)
- print("Error: File not found")
- gpu.setForeground(0xFFFFFF)
- return
- end
- print("File data read")
- local content = file:read("*all")
- file:close()
- print("Broadcasting Data on port " .. tostring(port))
- modem.broadcast(port, tostring(ops.f) .. "�" .. content)
- else
- gpu.setForeground(0xFF0000)
- print("Error: Please input a file, not a directory")
- gpu.setForeground(0xFFFFFF)
- end
- else
- gpu.setForeground(0xFF0000)
- print(
- "Error: There is no file or directory at " ..
- fs.concat(shell.getWorkingDirectory(), tostring(ops.f))
- )
- gpu.setForeground(0xFFFFFF)
- end
- else
- gpu.setForeground(0xFF0000)
- print("Error: Please input a file")
- gpu.setForeground(0xFFFFFF)
- end
- else
- gpu.setForeground(0xFF0000)
- print("Error: Max port is 65535")
- gpu.setForeground(0xFFFFFF)
- end
- else
- gpu.setForeground(0xFF0000)
- print("Error: Please input a number for a port")
- gpu.setForeground(0xFFFFFF)
- end
- else
- gpu.setForeground(0xFF0000)
- print("Error: Please input a port")
- gpu.setForeground(0xFFFFFF)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement