Advertisement
Guest User

Untitled

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