anfbckdo

network.lua

Dec 21st, 2023 (edited)
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.45 KB | None | 0 0
  1. local modem = peripheral.find("modem") or error("No modem found", 0) -- checks for a modem
  2. local compID = os.getComputerID()
  3. if modem then
  4.  
  5.   print("modem found!")
  6.  
  7.   write("Transmit or receive (T or R)? ") -- asks user if they want to transmit AND receive , or only receive.
  8.     local user_mode = read()
  9.  
  10.     if user_mode == "T" then
  11.  
  12.     write("Transmitting Channel (0-65535): ") -- asks user for input
  13.     transmitChannel = read()
  14.  transmitChannelInt = tonumber(transmitChannel) -- defines the transmitChannel as an integer, as modem.open can only handle integer input.
  15.      write("Recieving Channel #2(0-65535): ") -- asks user for input
  16.  
  17.     receiveChannel = read()
  18.  
  19.     receiveChannelInt = tonumber(receiveChannel) -- defines the receiveChannel as an integer, as modem.open can only handle integer input.
  20.  
  21.     print("Opening channel " .. receiveChannelInt .. "...")
  22.    
  23.     modem.open(receiveChannelInt)
  24.    
  25.         while true do
  26.     print("Message to transmit? ")
  27.     tmessage = read()
  28. payload = compID .. ": " .. tmessage
  29.     print("Channel " .. receiveChannelInt .. " open.")
  30.       modem.transmit(transmitChannelInt, receiveChannelInt, payload)
  31.     print(
  32.         "broadcasting message as " .. compID .. ', ' .. '"' .. tmessage .. '"' .. " on channel " .. transmitChannelInt .. " .")-- large broadcast message printed to computer screen.
  33.       end
  34.    
  35.   elseif user_mode == "R" then
  36.      write("Recieving Channel #2(0-65535): ") -- asks user for input
  37.  
  38.     receiveChannel = read()    
  39.  
  40.     receiveChannelInt = tonumber(receiveChannel) -- defines the receiveChannel as an integer, as modem.open can only handle integer input.
  41.  
  42.     print("Opening channel " .. receiveChannelInt .. "...")
  43.    
  44.     modem.open(receiveChannelInt)
  45.  
  46.     print("Channel " .. receiveChannelInt .. " open.")
  47.     while true do
  48.     local event, side, channel, replyChannel, message, distance -- defines vars, the channel variable here is from the computer contacting us from a channel (x).
  49.     repeat
  50.         event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  51.     until channel == receiveChannelInt -- puts variables into event until the channel variable is equal to the recieving channels variable. This helps prevent spoofing and allows messages from a certain channel.
  52.      print("received a reply from " .. tostring(message))  -- prints a reply
  53.     end
  54.   end
  55.   else
  56.     printError("Invalid input.") --error handling
  57.   end
Advertisement
Add Comment
Please, Sign In to add comment