Advertisement
Guest User

relay.lua

a guest
Jan 1st, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.37 KB | None | 0 0
  1. local component = require("component")
  2. local f = require("filesystem")
  3. local event = require("event")
  4. local m = component.modem
  5. local t = component.tunnel
  6. local arch
  7.  
  8. arch = "relay"
  9.  
  10. for x in f.list("/mnt/") do
  11.   for y in f.list("/mnt/"..x) do
  12.     if y == arch then
  13.       externaldisk = x
  14.     end
  15.   end
  16. end
  17.  
  18. print(externaldisk .. " ready!")
  19.  
  20. function loadfile(file)
  21.   for i in f.list("/mnt/" .. externaldisk) do
  22.     if i == file then
  23.       local result = "/mnt/" .. externaldisk .. i
  24.       print(result)
  25.       return tostring(result)
  26.     end
  27.   end
  28. end
  29.  
  30.  
  31. function readfile(file)
  32.   for line in io.lines(file) do
  33.     print(line)
  34.     return line
  35.  
  36.   end
  37. end
  38.  
  39.  
  40.  
  41. function split(string)
  42.   local result = {}
  43.   local one, two = string.match(string,"(.-);(.+)")
  44.  
  45.   table.insert(result,one)
  46.   table.insert(result,two)
  47.  
  48.   return result
  49. end
  50.  
  51.  
  52.  
  53. local cmd
  54. local msg
  55. m.setStrength(255)
  56.  
  57. local localport = tonumber(readfile(loadfile("port")))
  58. m.open(localport)
  59.  
  60. print("begin code!")
  61.  
  62. while true do
  63.  
  64.   print(arch)
  65.   print(localport)
  66.   print(externaldisk)
  67.   print("ready")
  68.  
  69.   local eventName, myAddress, itsAddress, port, distance, message = event.pull("modem_message")
  70.  
  71.   cmd = split(message)
  72.  
  73.   print(cmd[1])
  74.   print(cmd[2])
  75.  
  76.   if cmd[1]=="msg" then
  77.     msg = split(cmd[2])
  78.     if msg[1]=="hard" then
  79.       t.send(msg[2])
  80.     else
  81.       m.broadcast(msg[1],msg[2])
  82.     end
  83.   else
  84.     os.execute(cmd[2])
  85.   end
  86.  
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement