Advertisement
LDDestroier

ports - ComputerCraft Network Info

Apr 11th, 2015
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.75 KB | None | 0 0
  1. -- pastebin get XxkBiYJ6 ports
  2. -- std pb XxkBiYJ6 ports
  3. function getOpenChannels()
  4.     modemChannels = {}
  5.     sides = peripheral.getNames()
  6.     modemSides = {}
  7.     modemRednetSides = {}
  8.     for a = 1, #sides do
  9.         if peripheral.getType(sides[a]) == "modem" then
  10.             table.insert(modemSides, sides[a])
  11.             if rednet.isOpen(sides[a]) then
  12.                 table.insert(modemRednetSides, sides[a])
  13.             end
  14.         end
  15.     end
  16.     for a = 1, #modemSides do
  17.         if not oldPeripheralWrap then
  18.             modem = peripheral.wrap(modemSides[a])
  19.         else
  20.             modem = oldPeripheralWrap(modemSides[a])
  21.         end
  22.         channels = {}
  23.         for b = 1, 65535 do
  24.             if modem.isOpen(b) then
  25.                 table.insert(modemChannels, b)
  26.             end
  27.         end
  28.     end
  29. end
  30.  
  31. function printChannels()
  32.     print("ID: " .. os.getComputerID())
  33.     write("Open channels:")
  34.     if isLocked then
  35.         print(" LOCKED")
  36.     else
  37.         print("")
  38.     end
  39.     if modemChannels ~= nil then
  40.         for a = 1, #modemChannels do
  41.             if modemChannels[a] == rednet.CHANNEL_BROADCAST then
  42.                 print(" Rednet " .. modemChannels[a])
  43.             else
  44.                 print(" Modem " .. modemChannels[a])
  45.             end
  46.         end
  47.     else
  48.         print(" none!")
  49.     end
  50. end
  51.  
  52. tArg = {...}
  53.  
  54. command = tArg[1]
  55. argument1 = tArg[2]
  56. argument2 = tArg[3]
  57. argument3 = tArg[4]
  58.  
  59. getOpenChannels()
  60.  
  61. if not command then
  62.     getOpenChannels()
  63.     printChannels()
  64.     return
  65. elseif command == "open" then
  66.     if tonumber(argument1) == nil then
  67.         success = false
  68.         for a = 1, #modemSides do
  69.             if modemSides[a] == argument1 then
  70.                 success = true
  71.             end
  72.         end
  73.         if argument1 == nil then
  74.             error("no side given")
  75.         end
  76.         if success == true then
  77.             rednet.open(argument1)
  78.             print("Opened side " .. argument1 .. " on rednet")
  79.         else
  80.             error("modem " .. argument1 .. " not found")
  81.         end
  82.     else
  83.         argument1 = tonumber(argument1)
  84.         if argument1 <= 0 or argument1 > 65535 then
  85.             error("channel '" .. argument1 .. "' not good")
  86.         else
  87.             modem.open(argument1)
  88.             print("Opened channel " .. argument1)
  89.         end
  90.     end
  91. elseif command == "close" then
  92.     if tonumber(argument1) == nil then
  93.         success = false
  94.         if argument1 == "*" then
  95.             modem.closeAll()
  96.             print("All channels closed.")
  97.         else
  98.             for a = 1, #modemSides do
  99.                 if modemSides[a] == argument1 then
  100.                     success = true
  101.                 end
  102.             end
  103.             if argument1 == nil then
  104.                 error("no side given")
  105.             end
  106.             if success == true then
  107.                 rednet.close(argument1)
  108.                 print("Closed side '" .. argument1 .. "' on rednet")
  109.             else
  110.                 error("modem '" .. argument1 .. "' not found")
  111.             end
  112.         end
  113.     else
  114.         argument1 = tonumber(argument1)
  115.         if argument1 <= 0 or argument1 > 65535 then
  116.             error("channel " .. argument1 .. " not good")
  117.         else
  118.             modem.close(argument1)
  119.             print("Closed channel " .. argument1)
  120.         end
  121.     end
  122. elseif command == "lock" then
  123.     if not isLocked then
  124.         oldOpenChannelList = modemChannels
  125.         for a = 1, #modemSides do
  126.             modem = peripheral.wrap(modemSides[a])
  127.             modem.closeAll()
  128.         end
  129.         modem = nil
  130.         oldRednetSend = rednet.send
  131.         oldRednetBroadcast = rednet.broadcast
  132.         oldPeripheralWrap = peripheral.wrap
  133.         oldPeripheralFind = peripheral.find
  134.         oldPeripheralCall = peripheral.call
  135.         oldRednetLookup = rednet.lookup
  136.         oldRednetHost = rednet.host
  137.         oldRednetUnhost = rednet.unhost
  138.         peripheral.wrap = function(object)
  139.             if peripheral.getType(object) == "modem" then
  140.                 return nil
  141.             else
  142.                 return oldPeripheralWrap(object)
  143.             end
  144.         end
  145.         peripheral.find = function(object)
  146.             if object == "modem" then
  147.                 return nil
  148.             else
  149.                 return oldPeripheralFind(object)
  150.             end
  151.         end
  152.         peripheral.call = function(...)
  153.             arg = {...}
  154.             if peripheral.getType(arg[1]) == "modem" then
  155.                 return nil
  156.             else
  157.                 return oldPeripheralCall(...)
  158.             end
  159.         end
  160.         rednet.send = function(id, contents, protocol)
  161.             return nil
  162.         end
  163.         rednet.broadcast = function(contents, protocol)
  164.             return nil
  165.         end
  166.         rednet.lookup = function(protocol, hostname)
  167.             return nil
  168.         end
  169.         rednet.host = function(protocol, hostname)
  170.             return nil
  171.         end
  172.         rednet.unhost = function(protocol, hostname)
  173.             return nil
  174.         end
  175.         _G.peripheral.wrap = peripheral.wrap
  176.         _G.peripheral.find = peripheral.find
  177.         _G.peripheral.call = peripheral.call
  178.         _G.rednet.send = rednet.send
  179.         _G.rednet.broadcast = rednet.broadcast
  180.         _G.rednet.lookup = rednet.lookup
  181.         _G.rednet.host = rednet.host
  182.         _G.rednet.unhost = rednet.unhost
  183.         isLocked = true
  184.         print("Modem communication is LOCKED.")
  185.     else
  186.         rednet.send = oldRednetSend
  187.         rednet.broadcast = oldRednetBroadcast
  188.         peripheral.wrap = oldPeripheralWrap
  189.         peripheral.find = oldPeripheralFind
  190.         peripheral.call = oldPeripheralCall
  191.         rednet.lookup = oldRednetLookup
  192.         rednet.host = oldRednetHost
  193.         rednet.unhost = oldRednetUnhost
  194.         _G.peripheral.wrap = peripheral.wrap
  195.         _G.peripheral.find = peripheral.find
  196.         _G.peripheral.call = peripheral.call
  197.         _G.rednet.send = rednet.send
  198.         _G.rednet.broadcast = rednet.broadcast
  199.         _G.rednet.lookup = rednet.lookup
  200.         _G.rednet.host = rednet.host
  201.         _G.rednet.unhost = rednet.unhost
  202.         isLocked = false
  203.         modem = peripheral.find("modem")
  204.         for a = 1, #oldOpenChannelList do
  205.             modem.open(oldOpenChannelList[a])
  206.         end
  207.         modem = nil
  208.         print("Modem communication is UNLOCKED.")
  209.     end
  210. elseif command == "listen" or command == "receive" then
  211.     modem = peripheral.find("modem")
  212.     if not modem then
  213.         error("No modem to listen from!")
  214.     end
  215.     if not argument1 then
  216.         argument1 = rednet.CHANNEL_BROADCAST
  217.     end
  218.     write("Listening on port "..argument1)
  219.     if tonumber(argument2) then
  220.         print(" for "..argument2.." sec...")
  221.     else
  222.         print("...")
  223.     end
  224.     if argument1 == "rednet" then
  225.         modem.open(rednet.CHANNEL_BROADCAST)
  226.         event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  227.     else
  228.         modem.open(tonumber(argument1))
  229.         event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  230.     end
  231.     if event then
  232.         print("msg: '"..message.."'")
  233.         print("senderID: "..senderChannel)
  234.         print("replyChannel: "..replyChannel)
  235.         print("distance: "..senderDistance)
  236.     else
  237.         print("Nothing...")
  238.     end
  239. elseif command == "send" then
  240.     modem = peripheral.find("modem")
  241.     if not modem then
  242.         error("Get a modem!")
  243.     end
  244.     if not argument1 then
  245.         error("No message!")
  246.     end
  247.     if not argument2 then
  248.         argument2 = rednet.CHANNEL_BROADCAST
  249.     end
  250.     if not argument3 then
  251.         argument3 = rednet.CHANNEL_REPEAT
  252.     end
  253.     modem.open(tonumber(argument2))
  254.     modem.transmit(tonumber(argument2),tonumber(argument3),argument1)
  255.     print("Sent '"..argument1.."' on channel "..argument2.." (reply:"..argument3..")")
  256. else
  257.     print("//ports// - modem tool")
  258.     print("Syntax:")
  259.     print(" 'ports'")
  260.     print(" 'ports [open/close] <sideName>'")
  261.     print(" 'ports [open/close] <channelNum>'")
  262.     print(" 'ports lock'")
  263.     print(" 'ports send <message> [channel] [replyChannel]")
  264.     print(" 'ports receive'")
  265. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement