Advertisement
DOGGYWOOF

chatroom service

Sep 29th, 2024 (edited)
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1. -- Table to hold active private chatrooms
  2. local privateChatrooms = {}
  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 for encryption
  15. local function downloadOrCreateKey(protocol)
  16. local keyFilePath = protocol .. "_XOR.key"
  17.  
  18. -- Check if the key file exists
  19. if fileExists(keyFilePath) then
  20. print("Encryption key for protocol " .. protocol .. " found.")
  21. else
  22. -- Simulate downloading or generating the key
  23. print("Downloading encryption key for protocol " .. protocol .. "...")
  24. local key = "defaultEncryptionKey123" -- Can customize this key
  25.  
  26. -- Save the key to a file
  27. local file = fs.open(keyFilePath, "w")
  28. file.write(key)
  29. file.close()
  30. print("Encryption key saved as " .. keyFilePath)
  31. end
  32.  
  33. -- Read 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 for message security
  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 display a message within a chatroom
  53. local function displayMessage(protocol, senderId, message)
  54. term.setTextColor(colors.yellow)
  55. print("[" .. protocol .. "] [" .. senderId .. "]: " .. message)
  56. term.setTextColor(colors.white) -- Reset text color
  57. end
  58.  
  59. -- Function to handle messages for a private chatroom
  60. local function handlePrivateMessages(protocol, encryptionKey)
  61. while true do
  62. local senderId, encryptedMessage, receivedProtocol = rednet.receive(protocol)
  63. if receivedProtocol == protocol then
  64. local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
  65.  
  66. -- Split username and message
  67. local username, chatMessage = decryptedMessage:match("^(.-): (.+)$")
  68. if username and chatMessage then
  69. displayMessage(protocol, username, chatMessage)
  70. else
  71. displayMessage(protocol, senderId, decryptedMessage)
  72. end
  73. end
  74. end
  75. end
  76.  
  77. -- Function to send messages within a private chatroom
  78. local function sendPrivateMessage(protocol, username, encryptionKey)
  79. while true do
  80. print("[" .. protocol .. "] Type your message (or 'exit' to leave): ")
  81. local message = read()
  82. if message == "exit" then
  83. print("Exiting chatroom " .. protocol)
  84. break
  85. end
  86.  
  87. local formattedMessage = username .. ": " .. message
  88. local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
  89. rednet.broadcast(encryptedMessage, protocol)
  90. displayMessage(protocol, username, message)
  91. end
  92. end
  93.  
  94. -- Function to create or join a private chatroom with password protection
  95. local function createOrJoinPrivateChatroom(protocol, username)
  96. -- Check if the chatroom already exists
  97. if privateChatrooms[protocol] then
  98. -- If the chatroom exists, ask for the password to join
  99. print("Chatroom for protocol " .. protocol .. " exists.")
  100. print("Enter the password to join:")
  101. local passwordAttempt = read("*") -- Read password securely (hidden input)
  102.  
  103. -- Verify password
  104. if privateChatrooms[protocol].password == passwordAttempt then
  105. print("Password accepted! Joining chatroom...")
  106. else
  107. print("Incorrect password. Access denied.")
  108. return
  109. end
  110. else
  111. -- If the chatroom doesn't exist, create it with a password
  112. print("Creating new chatroom for protocol: " .. protocol)
  113. print("Enter a password for this chatroom:")
  114. local password = read("*") -- Read password securely (hidden input)
  115.  
  116. -- Save the password for this chatroom
  117. local encryptionKey = downloadOrCreateKey(protocol)
  118. privateChatrooms[protocol] = { encryptionKey = encryptionKey, password = password }
  119.  
  120. print("Chatroom created with password protection.")
  121. end
  122.  
  123. -- Start handling messages and sending messages for this protocol
  124. parallel.waitForAny(
  125. function() handlePrivateMessages(protocol, privateChatrooms[protocol].encryptionKey) end,
  126. function() sendPrivateMessage(protocol, username, privateChatrooms[protocol].encryptionKey) end
  127. )
  128. end
  129.  
  130. -- Function to list available modems
  131. local function listModems()
  132. local modems = {}
  133. for _, side in ipairs(peripheral.getNames()) do
  134. if peripheral.getType(side) == "modem" then
  135. table.insert(modems, side)
  136. end
  137. end
  138. return modems
  139. end
  140.  
  141. -- Function to set up the modem for communication
  142. local function setupModem()
  143. local modems = listModems()
  144. if #modems == 0 then
  145. print("No modems found! Please attach a modem and restart.")
  146. return nil
  147. end
  148.  
  149. print("Available modems:")
  150. for i, side in ipairs(modems) do
  151. print(i .. ": " .. side)
  152. end
  153.  
  154. rednet.open(modems[1]) -- Open the first modem found
  155. print("Modem on " .. modems[1] .. " initialized.")
  156. return modems[1]
  157. end
  158.  
  159. -- Main server function to manage connections and private chatrooms
  160. term.clear()
  161. term.setCursorPos(1, 1)
  162.  
  163. print("Welcome to the Protocol Private Chatroom Server with Password Protection")
  164.  
  165. -- Setup modem for server communication
  166. local modemSide = setupModem()
  167. if modemSide == nil then return end -- Exit if no modem found
  168.  
  169. while true do
  170. print("Enter a protocol for a private chatroom (or 'exit' to quit): ")
  171. local protocol = read()
  172.  
  173. if protocol == "exit" then
  174. print("Server shutting down...")
  175. break
  176. end
  177.  
  178. if protocol ~= "" then
  179. print("Enter your username: ")
  180. local username = read()
  181.  
  182. createOrJoinPrivateChatroom(protocol, username)
  183. end
  184. end
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement