Advertisement
Tapeline

qclient

Oct 8th, 2021
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local event = require("event")
  3. local shell = require("shell")
  4. local fs = require("filesystem")
  5. local args, ops = shell.parse(...)
  6. local m = component.modem -- get primary modem component
  7. m.open(1110)
  8.  
  9. function split(inputstr, sep)
  10.   if sep == nil then
  11.     sep = "%s"
  12.   end
  13.   local t={}
  14.   for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  15.     table.insert(t, str)
  16.   end
  17.   return t
  18. end
  19.  
  20.  
  21. if (args[1] == "find") then
  22.   m.broadcast(1110, "0;0;FINDPARENT;find")
  23.   local _, _, from, _, _, msg = event.pull("modem_message")
  24.   if (msg == "FINDROUTER:ME") then
  25.     m.send(from, 1110, "0;0;FINDPARENT;" .. from)
  26.     local _, _, fr, _, _, msg = event.pull("modem_message")
  27.     if (from == fr) then
  28.       local f = fs.open("/home/qclient.conf", "w")
  29.       f:write(from .. ";" .. msg)
  30.       f:close()
  31.       print("Found and registered at " .. from .. " as " .. msg)
  32.     end
  33.   end
  34. elseif (ops['p']) then
  35.   local f = fs.open("/home/qclient.conf", "r")
  36.   local fc = f:read(fs.size("/home/qclient.conf"))
  37.   conf = {}
  38.   conf = split(fc, ";")
  39.   m.send(conf[1], 1110, conf[2] .. ";" .. ops['r'] .. ";" .. ops['d'])
  40.   f:close()
  41.   if (ops['l']) then
  42.     local _, _, from, port, _, message = event.pull("modem_message")
  43.     print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message))
  44.   end
  45. end
Advertisement
RAW Paste Data Copied
Advertisement