Advertisement
gravityio

receive.lua

Jan 15th, 2021
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.10 KB | None | 0 0
  1. local modem = peripheral.find("modem")
  2. rednet.open(peripheral.getName(modem))
  3.  
  4. local function WriteFile(message)
  5.     print("Writing received file...")
  6.     local programName = message:match("%S+")
  7.     local actualMsg = message:sub(#programName+2)
  8.    
  9.     if fs.exists(programName) then print("File already exists. Overwriting...") end
  10.     local file = fs.open(programName, "w")
  11.     file.write(actualMsg)
  12.     file.close()
  13.     print("Wrote file " .. programName .. ".")
  14. end
  15.  
  16. local function Execute(message)
  17.     local path = message:match("%S+")
  18.     local args = message:sub(#path+2)
  19.     os.run({}, path, args)
  20. end
  21.  
  22. local function ProcessMessage(message)
  23.     local cmd = message:match("%S+")
  24.     local cmdLower = cmd:lower()
  25.     local justMessage = message:sub(#cmd+2)
  26.     if cmdLower == "file" then
  27.         WriteFile(justMessage)
  28.     end
  29.     if cmdLower == "exec" then
  30.         Execute(justMessage)
  31.     end
  32. end
  33.  
  34. local function AwaitMessage()
  35.     print("\nAwaiting Message...")
  36.     local computerId, msg, protocol = rednet.receive()
  37.     print("Received Message..")
  38.     ProcessMessage(msg)
  39. end
  40.  
  41. while true do
  42.     AwaitMessage()
  43. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement