Last active
August 29, 2015 13:58
-
-
Save CoderPuppy/10176089 to your computer and use it in GitHub Desktop.
Client for my Chat Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Chat Client by CoderPuppy | |
| -- Requires textEscapes and chatbot | |
| pcall(function() | |
| os.loadAPI('chatbot.lua') | |
| _G.chatbot = _G['chatbot.lua'] | |
| end) | |
| pcall(function() | |
| os.loadAPI('chatbot') | |
| end) | |
| if chatbot == nil then | |
| error('Chat Client requires chatbot', 0) | |
| end | |
| pcall(function() | |
| os.loadAPI('textEscapes.lua') | |
| _G.textEscapes = _G['textEscapes.lua'] | |
| end) | |
| pcall(function() | |
| os.loadAPI('textEscapes') | |
| end) | |
| if textEscapes == nil then | |
| error('Chat Client requires textEscapes', 0) | |
| end | |
| local tArgs = { ... } | |
| local function printUsage() | |
| print("Usages:") | |
| print("chatclient.lua <hostname> <username>") | |
| end | |
| local function openModem() | |
| local modemFound = false | |
| for i, side in ipairs(peripheral.getNames()) do | |
| if peripheral.getType(side) == "modem" then | |
| if not rednet.isOpen(side) then | |
| rednet.open(side) | |
| end | |
| modemFound = true | |
| end | |
| end | |
| if not modemFound then | |
| print("No modems found.") | |
| end | |
| return modemFound | |
| end | |
| -- Colours | |
| local highlightColor, textColor, pingColor | |
| if term.isColour() then | |
| textColor = colors.white | |
| pingColor = colors.red | |
| highlightColor = colors.yellow | |
| else | |
| textColor = colors.white | |
| pingColor = colors.white | |
| highlightColor = colors.white | |
| end | |
| local hostname = tArgs[1] | |
| local username = tArgs[2] | |
| if hostname == nil or username == nil then | |
| printUsage() | |
| return | |
| end | |
| -- Connect | |
| if not openModem() then | |
| return | |
| end | |
| local bot = chatbot.createBot(hostname, username) | |
| -- Handle messages | |
| local w, h = term.getSize() | |
| local parentTerm = term.current() | |
| local titleWindow = window.create(parentTerm, 1, 1, w, 1, true) | |
| local historyWindow = window.create(parentTerm, 1, 2, w, h-2, true) | |
| local promptWindow = window.create(parentTerm, 1, h, w, 1, true) | |
| historyWindow.setCursorPos(1, h-2) | |
| term.clear() | |
| term.setTextColour(textColor) | |
| term.redirect(promptWindow) | |
| promptWindow.restoreCursor() | |
| function drawTitle() | |
| local x, y = titleWindow.getCursorPos() | |
| local w, h = titleWindow.getSize() | |
| local sTitle = username .. " on " .. hostname | |
| titleWindow.setTextColour(highlightColor) | |
| titleWindow.setCursorPos(math.floor(w / 2 - string.len(sTitle) / 2), 1) | |
| titleWindow.clearLine() | |
| titleWindow.write(sTitle) | |
| promptWindow.restoreCursor() | |
| end | |
| function printMessage(msg) | |
| term.redirect(historyWindow) | |
| print() | |
| textEscapes.write(msg, { | |
| moveCursor = false; | |
| scroll = false; | |
| clear = false; | |
| clearLine = false; | |
| blink = false; | |
| }) | |
| term.redirect(promptWindow) | |
| promptWindow.restoreCursor() | |
| end | |
| drawTitle() | |
| local ok, err = pcall(function() | |
| parallel.waitForAny( | |
| function() | |
| while true do | |
| local ev = {os.pullEvent()} | |
| bot.handle(ev) | |
| if ev[1] == 'term_resize' then | |
| local w,h = parentTerm.getSize() | |
| titleWindow.reposition(1, 1, w, 1) | |
| historyWindow.reposition(1, 2, w, h-2) | |
| promptWindow.reposition(1, h, w, 1) | |
| elseif ev[1]:sub(1, 3) == 'bot' and ev[2] == bot.id then | |
| -- username, message, userid | |
| if ev[1] == 'bot_user_message' then | |
| if ev[4]:find(bot.username) then | |
| ev[4] = textEscapes.textColor(pingColor) .. ev[4] .. textEscapes.textColor(textColor) | |
| end | |
| printMessage( | |
| textEscapes.textColor(highlightColor) .. '[' .. ev[3] .. ']: ' .. textEscapes.textColor(textColor) .. ev[4] | |
| ) | |
| -- username, action, userid | |
| elseif ev[1] == 'bot_user_action' then | |
| printMessage(textEscapes.textColor(highlightColor) ..'* ' .. textEscapes.textColor(textColor) .. ev[3] .. ' ' .. ev[4]) | |
| -- oldusername, newusername, userid | |
| elseif ev[1] == 'bot_user_nickchange' then | |
| printMessage(textEscapes.textColor(highlightColor) .. '* ' .. textEscapes.textColor(textColor) .. ev[3] .. ' is now known as ' .. ev[4]) | |
| -- username, userid | |
| elseif ev[1] == 'bot_user_join' then | |
| -- if ev[4] ~= bot.userID then | |
| printMessage(textEscapes.textColor(highlightColor) .. '*' .. textEscapes.textColor(textColor) .. ' ' .. ev[3] .. ' has joined') | |
| -- end | |
| -- username, reason, userid | |
| elseif ev[1] == 'bot_user_leave' then | |
| if ev[5] == bot.userID then | |
| printMessage(textEscapes.textColor(highlightColor) .. '*' .. textEscapes.textColor(textColor) .. ' Quit: ' .. ev[4]) | |
| break | |
| else | |
| printMessage(textEscapes.textColor(highlightColor) .. '* ' .. textEscapes.textColor(textColor) .. ev[3] .. ' has left the chat (' .. ev[4] .. ')') | |
| end | |
| -- reason | |
| elseif ev[1] == 'bot_quit' then | |
| if ev[3] == 'timeout' then | |
| printMessage(textEscapes.textColor(highlightColor) .. '*' .. textEscapes.textColor(textColor) .. ' Server Timeout.') | |
| else | |
| printMessage(textEscapes.textColor(highlightColor) .. '*' .. textEscapes.textColor(textColor) .. ' Quit: ' .. ev[3]) | |
| end | |
| break | |
| else | |
| printMessage("Unknown bot related event: " .. ev[1]) | |
| end | |
| end | |
| end | |
| end, | |
| function() | |
| local tSendHistory = {} | |
| while true do | |
| promptWindow.setCursorPos(1,1) | |
| promptWindow.clearLine() | |
| promptWindow.setTextColor(highlightColor) | |
| promptWindow.write(": ") | |
| promptWindow.setTextColor(textColor) | |
| local sChat = read(nil, tSendHistory) | |
| if string.match(sChat, "^/quit") then | |
| if #sChat > 7 then | |
| bot.logout(sChat:sub(7)) | |
| else | |
| bot.logout() | |
| end | |
| else | |
| bot.send(sChat) | |
| table.insert(tSendHistory, sChat) | |
| end | |
| end | |
| end | |
| ) | |
| end) | |
| -- Close the windows | |
| term.redirect(parentTerm) | |
| -- Print error notice | |
| local w,h = term.getSize() | |
| term.setCursorPos(1, h) | |
| term.clearLine() | |
| term.setCursorBlink(false) | |
| if not ok then | |
| printError(err) | |
| if err == 'Terminated' then | |
| bot.logout('Terminated') | |
| else | |
| bot.logout('Error') | |
| end | |
| end | |
| -- Print disconnection notice | |
| print("Disconnected.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment