Advertisement
DOGGYWOOF

Untitled

Sep 29th, 2024 (edited)
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 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. if fileExists(keyFilePath) then
  19. print("Encryption key found.")
  20. else
  21. print("Downloading encryption key...")
  22. local key = "x3A7rB8zD2L5qW4nH1" -- Customize this key
  23.  
  24. local file = fs.open(keyFilePath, "w")
  25. file.write(key)
  26. file.close()
  27. print("Encryption key downloaded and saved as XOR.key.")
  28. end
  29.  
  30. local file = fs.open(keyFilePath, "r")
  31. local key = file.readAll()
  32. file.close()
  33.  
  34. return key
  35. end
  36.  
  37. -- XOR encryption function
  38. local function xorEncryptDecrypt(message, key)
  39. local encrypted = {}
  40. for i = 1, #message do
  41. local char = string.byte(message, i)
  42. local keyChar = string.byte(key, (i - 1) % #key + 1)
  43. table.insert(encrypted, string.char(bit.bxor(char, keyChar)))
  44. end
  45. return table.concat(encrypted)
  46. end
  47.  
  48. -- Function to list all connected modems
  49. local function listModems()
  50. local modems = {}
  51. for _, side in ipairs(peripheral.getNames()) do
  52. if peripheral.getType(side) == "modem" then
  53. table.insert(modems, side)
  54. end
  55. end
  56. return modems
  57. end
  58.  
  59. -- Function to setup the modem
  60. local function setupModem()
  61. local modems = listModems()
  62. if #modems == 0 then
  63. print("No modems found! Please connect a modem and restart.")
  64. return nil
  65. end
  66.  
  67. print("Connected modems:")
  68. for i, side in ipairs(modems) do
  69. print(i .. ": " .. side)
  70. end
  71.  
  72. rednet.open(modems[1])
  73. print("Using modem on " .. modems[1])
  74. return modems[1]
  75. end
  76.  
  77. -- Function to display a message in the chat
  78. local function displayMessage(senderId, message, isOlderClient)
  79. term.setTextColor(colors.yellow) -- Set text color
  80. if isOlderClient then
  81. print("[Unknown User (" .. senderId .. ")]: " .. message) -- Show ID for older clients
  82. else
  83. print("[" .. senderId .. "]: " .. message)
  84. end
  85. term.setTextColor(colors.white) -- Reset text color
  86. end
  87.  
  88. -- Function to handle incoming messages
  89. local function handleIncomingMessages(encryptionKey)
  90. while true do
  91. local senderId, encryptedMessage, protocol = rednet.receive(chatProtocol)
  92. local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
  93.  
  94. local username, chatMessage = decryptedMessage:match("^(.-): (.+)$")
  95. if username and chatMessage then
  96. displayMessage(username, chatMessage, false)
  97. else
  98. displayMessage(senderId, decryptedMessage, true)
  99. end
  100. end
  101. end
  102.  
  103. -- Function to send a message
  104. local function sendMessage(username, encryptionKey)
  105. while true do
  106. term.setCursorPos(1, 1)
  107. print("Type your message (or 'exit' to quit): ")
  108. local userInput = read()
  109. if userInput == "exit" then
  110. print("Exiting chat...")
  111. shell.run("/disk/os/gui")
  112. break
  113. end
  114.  
  115. local formattedMessage = username .. ": " .. userInput
  116. local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
  117. rednet.broadcast(encryptedMessage, chatProtocol)
  118. displayMessage(username, userInput, false)
  119. end
  120. end
  121.  
  122. -- Function to create the chat GUI
  123. local function createChatGUI()
  124. term.clear()
  125. term.setCursorPos(1, 1)
  126. print("======================================")
  127. print(" Welcome to Doggy OS Chatroom ")
  128. print("======================================")
  129.  
  130. -- Prompt for the username
  131. print("Enter your username:")
  132. local username = read()
  133.  
  134. -- Ask the user to choose between public and private chatroom
  135. print("Join public (p) or private (c) chatroom?")
  136. local chatroomChoice = read():lower()
  137.  
  138. if chatroomChoice == "p" then
  139. print("Joining public chatroom with protocol: " .. chatProtocol)
  140. else
  141. print("Enter a custom protocol name for the private chatroom:")
  142. chatProtocol = read()
  143. print("Using custom protocol: " .. chatProtocol)
  144. end
  145.  
  146. return username
  147. end
  148.  
  149. -- Main program
  150. term.clear()
  151. term.setCursorPos(1, 1)
  152.  
  153. local username = createChatGUI()
  154.  
  155. local encryptionKey = downloadOrCreateKey()
  156. local modemSide = setupModem()
  157. if modemSide == nil then return end
  158.  
  159. print("Type 'exit' to leave the chat.")
  160.  
  161. -- Start message handling
  162. parallel.waitForAny(
  163. function() handleIncomingMessages(encryptionKey) end,
  164. function() sendMessage(username, encryptionKey) end
  165. )
  166.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement