Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Custom protocol (default)
- 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"
- if fileExists(keyFilePath) then
- print("Encryption key found.")
- else
- print("Downloading encryption key...")
- local key = "x3A7rB8zD2L5qW4nH1" -- Customize this key
- local file = fs.open(keyFilePath, "w")
- file.write(key)
- file.close()
- print("Encryption key downloaded and saved as XOR.key.")
- end
- 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
- 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
- local function handleIncomingMessages(encryptionKey)
- while true do
- local senderId, encryptedMessage, protocol = rednet.receive(chatProtocol)
- local decryptedMessage = xorEncryptDecrypt(encryptedMessage, encryptionKey)
- local username, chatMessage = decryptedMessage:match("^(.-): (.+)$")
- if username and chatMessage then
- displayMessage(username, chatMessage, false)
- else
- displayMessage(senderId, decryptedMessage, true)
- end
- end
- end
- -- Function to send a message
- local function sendMessage(username, encryptionKey)
- while true do
- term.setCursorPos(1, 1)
- print("Type your message (or 'exit' to quit): ")
- local userInput = read()
- if userInput == "exit" then
- print("Exiting chat...")
- shell.run("/disk/os/gui")
- break
- end
- local formattedMessage = username .. ": " .. userInput
- local encryptedMessage = xorEncryptDecrypt(formattedMessage, encryptionKey)
- rednet.broadcast(encryptedMessage, chatProtocol)
- displayMessage(username, userInput, false)
- end
- end
- -- Function to create the chat GUI
- local function createChatGUI()
- term.clear()
- term.setCursorPos(1, 1)
- print("======================================")
- print(" Welcome to Doggy OS Chatroom ")
- print("======================================")
- -- Prompt for the username
- print("Enter your username:")
- local username = read()
- -- Ask the user to choose between public and private chatroom
- print("Join public (p) or private (c) chatroom?")
- local chatroomChoice = read():lower()
- if chatroomChoice == "p" then
- print("Joining public chatroom with protocol: " .. chatProtocol)
- else
- print("Enter a custom protocol name for the private chatroom:")
- chatProtocol = read()
- print("Using custom protocol: " .. chatProtocol)
- end
- return username
- end
- -- Main program
- term.clear()
- term.setCursorPos(1, 1)
- local username = createChatGUI()
- local encryptionKey = downloadOrCreateKey()
- local modemSide = setupModem()
- if modemSide == nil then return end
- print("Type 'exit' to leave the chat.")
- -- Start message handling
- parallel.waitForAny(
- function() handleIncomingMessages(encryptionKey) end,
- function() sendMessage(username, encryptionKey) end
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement