Advertisement
Guest User

spy 2.0

a guest
Feb 17th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.86 KB | None | 0 0
  1. local modems = { }
  2. local ports = { }
  3.  
  4. local old_message
  5.  
  6. siit = function(tbl, nd)
  7.     local str = ""
  8.     for i, v in pairs(tbl) do
  9.         if type(v) == "table" then
  10.             str = str.."["..i.."]"..string.rep(">", nd).." "..siit(v, nd + 1)..string.rep("<", nd).." "
  11.         else
  12.             str = str..i..":"..v.."; "
  13.         end
  14.     end
  15.     return str
  16. end
  17.  
  18. client = function()
  19.     if pcall( loadfile("client.cfg") ) then
  20.         local tconfig = loadfile("client.cfg")()
  21.         local native_pullEvent = os.pullEventRaw
  22.        
  23.         local save_file = false
  24.         local modem_names = false
  25.        
  26.         if type(tconfig.whitelist) == "table" then
  27.        
  28.         end
  29.         if type(tconfig.blacklist) == "table" then
  30.            
  31.         end
  32.         if tconfig.file.save then
  33.             if type(tconfig.file.name) == "string" then
  34.                 local savetype = tconfig.file.saveType or "w"
  35.                 save_file = true
  36.             else
  37.                 error("fileName in \"client.cfg\" expected", 0)
  38.             end
  39.         else
  40.             term.clear()
  41.             term.setCursorPos(1, 1)
  42.             term.setCursorBlink(false)
  43.         end
  44.         if tconfig.modem.useNames then
  45.             modem_names = tconfig.modem.names
  46.         end
  47.         if tconfig.modem.differentNetworks then
  48.             for i, modem in pairs(modems) do
  49.                 for _i, port in pairs(ports) do
  50.                     modem.open(port)
  51.                 end
  52.             end
  53.         else
  54.             for i=0, math.floor(#ports / 128) do
  55.                 for _i=1, 128 do
  56.                     modems[i + 1].open( i*128 + _i )
  57.                 end
  58.             end
  59.         end -- ...
  60.        
  61.         os.pullEventRaw = function(str)
  62.             local tEvent = { native_pullEvent(raw) }
  63.            
  64.             if tEvent[1] == "modem_message" then
  65.                 local message = {}
  66.                
  67.                 if modem_names then
  68.                     message["Network"] = modem_names[tEvent[2]]
  69.                 end
  70.                 message["Port"] = tEvent[3]
  71.                 message["ReplyChannel"] = tEvent[4]
  72.                 message["Message"] = tEvent[5]
  73.                 message["Distance"] = tEvent[6]
  74.                
  75.                 local sMessage = siit( message, 1 )
  76.                
  77.                 if not tconfig["disable_doubleMessageFilter"] then
  78.                     if sMessage == old_message then
  79.                         return table.unpack(tEvent)
  80.                     else
  81.                         old_message = sMessage
  82.                     end
  83.                 end
  84.                
  85.                 do
  86.                     local bbreak = false
  87.                     local bcont = false
  88.                    
  89.                     if tconfig["whitelist"] then
  90.                         for i, v in pairs(tconfig.whitelist) do
  91.                             if sMessage:find(v) then
  92.                                 bcont = true
  93.                             end
  94.                         end
  95.                     else
  96.                         bcont = true
  97.                     end
  98.                
  99.                     if tconfig["blacklist"] then
  100.                         for i, v in pairs(tconfig.blacklist) do
  101.                             if sMessage:find(v) then
  102.                                 bbreak = true
  103.                             end
  104.                         end
  105.                     end
  106.                    
  107.                     if bbreak or not bcont then
  108.                         return table.unpack(tEvent)
  109.                     end
  110.                 end
  111.                
  112.                 if tconfig.file.save then
  113.                     file = fs.open(tconfig.file.name, tconfig.file.saveType)
  114.                     file.write(sMessage)
  115.                     file.close()
  116.                 else
  117.                     print(sMessage)
  118.                 end
  119.             end
  120.            
  121.             return table.unpack(tEvent)
  122.         end
  123.     else
  124.         error("Config file \"client.cfg\" expected", 0)
  125.     end
  126. end
  127.  
  128. local server = function()
  129.     if pcall( loadfile("server.cfg") ) then
  130.         local tconfig = loadfile("server.cfg")()
  131.     else
  132.         error("Config file \"server.cfg\" expected", 0)
  133.     end
  134. end
  135.  
  136. for i, v in pairs( peripheral.getNames() ) do
  137.     if peripheral.getType(v) == "modem" then
  138.         table.insert(modems, peripheral.wrap(v))
  139.         modems[#modems].closeAll()
  140.     end
  141. end
  142.  
  143. if pcall( loadfile("ports.n") ) then
  144.     local raw_ports = loadfile("ports.n")()
  145.     for i, v in pairs( raw_ports ) do
  146.         table.insert(ports, tonumber(v))
  147.     end
  148. else
  149.     error("Port file \"ports.n\" expected", 0)
  150. end
  151.  
  152. do
  153.     local args = { ... }
  154.     if #args == 1 and (args[1] == "server" or args[1] == "client") then
  155.         if #modems == 0 then
  156.             error("Modem expected", 0)
  157.         elseif #ports == 0 then
  158.             error("Portss expected", 0)
  159.         elseif #modems * 128 < #ports then
  160.             error("There cant be opened more PORTS than "..tostring(#modems * 128), 0)
  161.         end
  162.         if args[1] == "server" then
  163.             server()
  164.         elseif args[1] == "client" then
  165.             client()
  166.         end
  167.     else
  168.         printError("Use:")
  169.         printError("spy server or")
  170.         error("spy client", 0)
  171.     end
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement