Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Custom protocol for the chat
- local chatProtocol = "doggyChat"
- -- Function to check if a file exists
- local function fileExists(path)
- local file = fs.open(path, "r")
- if file then
- file.close()
- return true
- end
- return false
- end
- -- Function to download or create the XOR.key file
- local function downloadOrCreateKey()
- local keyFilePath = "XOR.key"
- -- Check if the key file exists
- if fileExists(keyFilePath) then
- print("Encryption key found.")
- else
- -- Simulate downloading the key by creating a new one
- print("Downloading encryption key...")
- local key = "defaultEncryptionKey123" -- You can customize this key
- -- Save the key to the XOR.key file
- local file = fs.open(keyFilePath, "w")
- file.write(key)
- file.close()
- print("Encryption key downloaded and saved as XOR.key.")
- end
- -- Read and return the key from the file
- local file = fs.open(keyFilePath, "r")
- local key = file.readAll()
- file.close()
- return key
- end
- -- XOR encryption function
- local function xorEncryptDecrypt(message, key)
- local encrypted = {}
- for i = 1, #message do
- local char = string.byte(message, i)
- local keyChar = string.byte(key, (i - 1) % #key + 1)
- table.insert(encrypted, string.char(bit.bxor(char, keyChar)))
- end
- return table.concat(encrypted)
- end
- -- Function to list all connected modems
- local function listModems()
- local modems = {}
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- table.insert(modems, side)
- end
- end
- return modems
- end
- -- Function to setup the modem
- local function setupModem()
- local modems = listModems()
- if #modems == 0 then
- print("No modems found! Please connect a modem and restart.")
- return nil
- end
- print("Connected modems:")
- for i, side in ipairs(modems) do
- print(i .. ": " .. side)
- end
- -- Open the first modem found
- rednet.open(modems[1])
- print("Using modem on " .. modems[1])
- return modems[1]
- end
- -- Function to display a message in the chat
- local function displayMessage(senderId, message, isOlderClient)
- term.setTextColor(colors.yellow) -- Set text color
- if isOlderClient then
- print("[Unknown User (" .. senderId .. ")]: " .. message) -- Show ID for older clients
- else
- print("[" .. senderId .. "]: " .. message)
- end
- term.setTextColor(colors.white) -- Reset text color
- end
- -- Function to handle incoming messages (with decryption and protocol)
- local function handleIncomingMessages(encryptionKey)
- while true do
- -- Receive message only with the custom protocol
- local senderId, encryptedMessage, protocol = rednet.receive(chatProtocol)
- -- Decrypt the incoming message
- local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
- -- Split the username and chat message
- local username, chatMessage = decryptedMessage:match("^(.-): (.+)$")
- if username and chatMessage then
- displayMessage(username, chatMessage, false)
- else
- displayMessage(senderId, decryptedMessage, true) -- Handle messages from older clients
- end
- end
- end
- -- Function to send a message (with encryption and protocol)
- local function sendMessage(username, encryptionKey)
- while true do
- print("Type your message (or 'exit' to quit): ")
- local userInput = read() -- Get the user's input
- if userInput == "exit" then
- print("Exiting chat...")
- shell.run("/disk/os/gui") -- Run the GUI program on exit
- break
- end
- -- Format the message to include the username
- local formattedMessage = username .. ": " .. userInput
- -- Encrypt the message before broadcasting
- local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
- -- Broadcast the encrypted message to all users with the custom protocol
- rednet.broadcast(encryptedMessage, chatProtocol)
- -- Display the unencrypted message locally
- displayMessage(username, userInput, false)
- end
- end
- -- Main program
- term.clear() -- Clear the terminal
- term.setCursorPos(1, 1) -- Set cursor position
- print("Welcome to the Doggy OS Network Chatroom!")
- -- Prompt for the username
- print("Enter your username:")
- local username = read()
- -- Download or load the encryption key from XOR.key
- local encryptionKey = downloadOrCreateKey()
- local modemSide = setupModem()
- if modemSide == nil then return end -- Exit if no modem was found
- print("Type 'exit' to leave the chat.")
- -- Start monitoring messages and detecting jamming in parallel
- parallel.waitForAny(function()
- handleIncomingMessages(encryptionKey)
- end, function()
- sendMessage(username, encryptionKey)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement