Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- MAKE SURE TO DOWNLOAD AND RUN THIS: https://pastebin.com/tHAMJgrz OTHERWIZE IT WONT WORK
- local function logo()
- print([[
- ____ ____ ____ ____
- U /"___|U /"___| ___ U | _"\ u U /"___|
- \| | u \| | u U u |_"_| \| |_) |/ \| | u
- | |/__ | |/__ /___\ | | | _ < | |/__
- \____| \____|__"__|U/| |\u |_| \_\ \____|
- _// \\ _// \\ .-,_|___|_,-.// \\_ _// \\
- (__)(__)(__)(__) \_)-' '-(_/(__) (__)(__)(__)
- ]])
- end
- local function getUserInput(prompt)
- write(prompt .. ": ")
- return read()
- end
- local function configureServer()
- local url = "http://localhost:8000/config"
- local server = getUserInput("Enter IRC server (e.g., irc.libera.chat)")
- local port = getUserInput("Enter IRC port (e.g., 6667)")
- local nick = getUserInput("Enter your nickname")
- local channel = getUserInput("Enter channel name (e.g., #testchannel)")
- local payload = textutils.serializeJSON({
- server = server,
- port = tonumber(port),
- nick = nick,
- channel = channel,
- })
- local headers = {["Content-Type"] = "application/json"}
- local response = http.post(url, payload, headers)
- if response then
- local responseBody = response.readAll()
- response.close()
- local data = textutils.unserializeJSON(responseBody)
- if data and data.status then
- print(data.status)
- return true
- else
- print(data.error or "Failed to configure server")
- return false
- end
- else
- print("Failed to connect to server")
- return false
- end
- end
- local function sendMessage(message)
- local url = "http://localhost:8000/send"
- local payload = textutils.serializeJSON({message = message})
- local headers = {["Content-Type"] = "application/json"}
- local response = http.post(url, payload, headers)
- if response then
- local responseBody = response.readAll()
- response.close()
- local data = textutils.unserializeJSON(responseBody)
- if data and data.status then
- print(data.status)
- else
- print(data.error or "Failed to send message")
- end
- else
- print("Failed to connect to server")
- end
- end
- local function fetchMessages()
- local url = "http://localhost:8000/messages"
- local response = http.get(url)
- if response then
- local responseBody = response.readAll()
- response.close()
- local data = textutils.unserializeJSON(responseBody)
- if data and data.messages then
- for _, message in ipairs(data.messages) do
- print(message)
- end
- end
- else
- print("Failed to connect to server")
- end
- end
- local function checkForNewMessages()
- while true do
- fetchMessages()
- sleep(0.5)
- end
- end
- local function chatClient()
- while true do
- write("> ")
- local input = read()
- if input == "/quit" then break end
- sendMessage(input)
- sleep(0.1)
- end
- end
- logo()
- if configureServer() then
- parallel.waitForAny(chatClient, checkForNewMessages)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement