Advertisement
DOGGYWOOF

Untitled

Sep 24th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. -- Function to list all connected modems
  2. local function listModems()
  3. local modems = {}
  4. for _, side in ipairs(peripheral.getNames()) do
  5. if peripheral.getType(side) == "modem" then
  6. table.insert(modems, side)
  7. end
  8. end
  9. return modems
  10. end
  11.  
  12. -- Function to setup the modem
  13. local function setupModem()
  14. local modems = listModems()
  15. if #modems == 0 then
  16. print("No modems found! Please connect a modem and restart.")
  17. return nil
  18. end
  19.  
  20. print("Connected modems:")
  21. for i, side in ipairs(modems) do
  22. print(i .. ": " .. side)
  23. end
  24.  
  25. -- Open the first modem found
  26. rednet.open(modems[1])
  27. print("Using modem on " .. modems[1])
  28. return modems[1]
  29. end
  30.  
  31. -- Function to display a message in the chat
  32. local function displayMessage(senderId, message, isOlderClient)
  33. term.setTextColor(colors.yellow) -- Set text color
  34. if isOlderClient then
  35. print("[Unknown User (" .. senderId .. ")]: " .. message) -- Show ID for older clients
  36. else
  37. print("[" .. senderId .. "]: " .. message)
  38. end
  39. term.setTextColor(colors.white) -- Reset text color
  40. end
  41.  
  42. -- Function to handle incoming messages
  43. local function handleIncomingMessages()
  44. while true do
  45. local senderId, message, protocol = rednet.receive()
  46. local username, chatMessage = message:match("^(.-): (.+)$")
  47. if username and chatMessage then
  48. displayMessage(username, chatMessage, false)
  49. else
  50. displayMessage(senderId, message, true) -- Handle messages from older clients
  51. end
  52. end
  53. end
  54.  
  55. -- Function to send a message to all connected Rednet users
  56. local function sendMessage(username)
  57. while true do
  58. print("Type your message (or 'exit' to quit): ")
  59. local userInput = read() -- Get the user's input
  60. if userInput == "exit" then
  61. print("Exiting chat...")
  62. shell.run("/disk/os/gui") -- Run the GUI program on exit
  63. break
  64. end
  65. -- Format the message to include the username
  66. local formattedMessage = username .. ": " .. userInput
  67. -- Broadcast the message to all users
  68. rednet.broadcast(formattedMessage)
  69. displayMessage(username, userInput, false)
  70. end
  71. end
  72.  
  73. -- Function to jam the chat and networks
  74. local isJamming = false -- Variable to track the jamming state
  75.  
  76. local function jamNetwork()
  77. while true do
  78. if isJamming then
  79. rednet.broadcast("Chat Jammed! No messages will be received.") -- Broadcast jamming message
  80. end
  81. os.sleep(1) -- Wait for a second before checking again
  82. end
  83. end
  84.  
  85. -- Function to toggle the jamming
  86. local function toggleJammer()
  87. while true do
  88. local _, message = rednet.receive() -- Wait for incoming messages to check for toggle command
  89. if message == "jam" then
  90. isJamming = not isJamming -- Toggle the jamming state
  91. local status = isJamming and "enabled" or "disabled"
  92. rednet.broadcast("Jammer has been " .. status .. ".")
  93. end
  94. end
  95. end
  96.  
  97. -- Main program
  98. term.clear() -- Clear the terminal
  99. term.setCursorPos(1, 1) -- Set cursor position
  100.  
  101. print("Welcome to the Doggy OS Network Chatroom!")
  102.  
  103. -- Prompt for the username
  104. print("Enter your username:")
  105. local username = read()
  106.  
  107. local modemSide = setupModem()
  108. if modemSide == nil then return end -- Exit if no modem was found
  109.  
  110. print("Type 'exit' to leave the chat.")
  111. print("Type 'jam' to toggle the jamming.")
  112.  
  113. -- Start monitoring messages in a separate thread
  114. parallel.waitForAny(handleIncomingMessages, function()
  115. sendMessage(username)
  116. end)
  117.  
  118. -- Start the user jammer in a separate thread
  119. parallel.waitForAny(jamNetwork, toggleJammer)
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement