Advertisement
DOGGYWOOF

Doggy chat

Sep 29th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. -- Custom protocol (default)
  2. local chatProtocol = "doggyChat"
  3.  
  4. -- Function to check if a file exists
  5. local function fileExists(path)
  6. local file = fs.open(path, "r")
  7. if file then
  8. file.close()
  9. return true
  10. end
  11. return false
  12. end
  13.  
  14. -- Function to download or create the XOR.key file
  15. local function downloadOrCreateKey()
  16. local keyFilePath = "XOR.key"
  17.  
  18. -- Check if the key file exists
  19. if fileExists(keyFilePath) then
  20. print("Encryption key found.")
  21. else
  22. -- Simulate downloading the key by creating a new one
  23. print("Downloading encryption key...")
  24. local key = "defaultEncryptionKey123" -- You can customize this key
  25.  
  26. -- Save the key to the XOR.key file
  27. local file = fs.open(keyFilePath, "w")
  28. file.write(key)
  29. file.close()
  30. print("Encryption key downloaded and saved as XOR.key.")
  31. end
  32.  
  33. -- Read and return the key from the file
  34. local file = fs.open(keyFilePath, "r")
  35. local key = file.readAll()
  36. file.close()
  37.  
  38. return key
  39. end
  40.  
  41. -- XOR encryption function
  42. local function xorEncryptDecrypt(message, key)
  43. local encrypted = {}
  44. for i = 1, #message do
  45. local char = string.byte(message, i)
  46. local keyChar = string.byte(key, (i - 1) % #key + 1)
  47. table.insert(encrypted, string.char(bit.bxor(char, keyChar)))
  48. end
  49. return table.concat(encrypted)
  50. end
  51.  
  52. -- Function to list all connected modems
  53. local function listModems()
  54. local modems = {}
  55. for _, side in ipairs(peripheral.getNames()) do
  56. if peripheral.getType(side) == "modem" then
  57. table.insert(modems, side)
  58. end
  59. end
  60. return modems
  61. end
  62.  
  63. -- Function to setup the modem
  64. local function setupModem()
  65. local modems = listModems()
  66. if #modems == 0 then
  67. print("No modems found! Please connect a modem and restart.")
  68. return nil
  69. end
  70.  
  71. print("Connected modems:")
  72. for i, side in ipairs(modems) do
  73. print(i .. ": " .. side)
  74. end
  75.  
  76. -- Open the first modem found
  77. rednet.open(modems[1])
  78. print("Using modem on " .. modems[1])
  79. return modems[1]
  80. end
  81.  
  82. -- Function to display a message in the chat
  83. local function displayMessage(senderId, message, isOlderClient)
  84. term.setTextColor(colors.yellow) -- Set text color
  85. if isOlderClient then
  86. print("[Unknown User (" .. senderId .. ")]: " .. message) -- Show ID for older clients
  87. else
  88. print("[" .. senderId .. "]: " .. message)
  89. end
  90. term.setTextColor(colors.white) -- Reset text color
  91. end
  92.  
  93. -- Function to handle incoming messages (with decryption and protocol)
  94. local function handleIncomingMessages(encryptionKey)
  95. while true do
  96. -- Receive message only with the custom protocol
  97. local senderId, encryptedMessage, protocol = rednet.receive(chatProtocol)
  98.  
  99. -- Decrypt the incoming message
  100. local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
  101.  
  102. -- Split the username and chat message
  103. local username, chatMessage = decryptedMessage:match("^(.-): (.+)$")
  104. if username and chatMessage then
  105. displayMessage(username, chatMessage, false)
  106. else
  107. displayMessage(senderId, decryptedMessage, true) -- Handle messages from older clients
  108. end
  109. end
  110. end
  111.  
  112. -- Function to send a message (with encryption and protocol)
  113. local function sendMessage(username, encryptionKey)
  114. while true do
  115. print("Type your message (or 'exit' to quit): ")
  116. local userInput = read() -- Get the user's input
  117. if userInput == "exit" then
  118. print("Exiting chat...")
  119. shell.run("/disk/os/gui") -- Run the GUI program on exit
  120. break
  121. end
  122.  
  123. -- Format the message to include the username
  124. local formattedMessage = username .. ": " .. userInput
  125.  
  126. -- Encrypt the message before broadcasting
  127. local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
  128.  
  129. -- Broadcast the encrypted message to all users with the custom protocol
  130. rednet.broadcast(encryptedMessage, chatProtocol)
  131.  
  132. -- Display the unencrypted message locally
  133. displayMessage(username, userInput, false)
  134. end
  135. end
  136.  
  137. -- Main program
  138. term.clear() -- Clear the terminal
  139. term.setCursorPos(1, 1) -- Set cursor position
  140.  
  141. print("Welcome to the Doggy OS Network Chatroom!")
  142.  
  143. -- Prompt for the username
  144. print("Enter your username:")
  145. local username = read()
  146.  
  147. -- Ask for custom protocol or default
  148. print("Would you like to use a new protocol? (y/n)")
  149. local useNewProtocol = read()
  150.  
  151. if useNewProtocol == "y" or useNewProtocol == "Y" then
  152. print("Enter the new protocol name:")
  153. chatProtocol = read()
  154. print("Using custom protocol: " .. chatProtocol)
  155. else
  156. print("Using default protocol: " .. chatProtocol)
  157. end
  158.  
  159. -- Download or load the encryption key from XOR.key
  160. local encryptionKey = downloadOrCreateKey()
  161.  
  162. local modemSide = setupModem()
  163. if modemSide == nil then return end -- Exit if no modem was found
  164.  
  165. print("Type 'exit' to leave the chat.")
  166.  
  167. -- Start monitoring messages and detecting jamming in parallel
  168. parallel.waitForAny(function()
  169. handleIncomingMessages(encryptionKey)
  170. end, function()
  171. sendMessage(username, encryptionKey)
  172. end)
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement