Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local AUTHENTICATED = false
- local RUNNING = false
- local MODEM = "right"
- local FILE_CONFIG = "config"
- local USERNAME = nil
- local PASSWORD = nil
- local LINES = {}
- local ID = os.computerID()
- local ONLINE = {}
- --
- function install()
- term.clear()
- term.setCursorPos(1, 1)
- while USERNAME == nil or USERNAME == "" do
- USERNAME = inputUsername()
- end
- term.setCursorPos(1, 2)
- while PASSWORD == nil or PASSWORD == "" do
- PASSWORD = inputPassword()
- end
- local file = io.open(FILE_CONFIG, "w")
- file:write(textutils.serialize({username = USERNAME, password = PASSWORD}))
- file:close()
- term.setCursorPos(1, 3)
- print("Installation successfull. Restarting application ...")
- sleep(1)
- end
- function inputUsername()
- term.write("Choose a username: ")
- return read()
- end
- function inputPassword()
- term.write("Choose a password: ")
- return read()
- end
- function inputAuthenticate()
- term.clearLine()
- term.write("Password: ")
- return read("*")
- end
- function updateOnlineList()
- local i, v
- term.setCursorPos(1, 1)
- term.clearLine()
- term.write("Online: ")
- for i, v in ipairs(ONLINE) do
- if i > 1 then
- term.write(", ")
- end
- term.write(ONLINE[i])
- end
- term.setCursorPos(8, 3)
- end
- function writeLineBuffer()
- local i
- for i = 0, 13 do
- term.setCursorPos(1, i + 5)
- term.clearLine()
- term.write(LINES[i])
- end
- term.setCursorPos(8, 3)
- end
- function updateBuffer()
- --local senderId, message, distance
- while RUNNING do
- --senderId, message, distance = rednet.receive(1)
- --if message ~= "" and message ~= nil then
- -- addMessage(message)
- --end
- recieve()
- end
- return false
- end
- function handleInput()
- local message
- while RUNNING do
- term.setCursorPos(1, 3)
- term.clearLine()
- term.write("Send > ")
- message = read()
- if message ~= "" and message ~= nil then
- if message:sub(0, 1) == "/" then
- executeCommand(message:sub(2))
- else
- message = USERNAME .. ": " .. message
- addMessage(message)
- --rednet.broadcast(message)
- sendMessage(message)
- end
- end
- end
- return false
- end
- function executeCommand(command)
- command = command:lower()
- if command == "quit" then
- addMessage("[CONSOLE] Quiting application")
- sleep(2)
- endSession()
- term.clear()
- term.setCursorPos(1, 1)
- RUNNING = false
- end
- end
- function addMessage(message)
- local i, length
- for i = 0, 12 do
- LINES[i] = LINES[i + 1]
- end
- length = message:len()
- if length > 50 then
- LINES[13] = message:sub(1, 50)
- addMessage(message:sub(51))
- else
- LINES[13] = message
- end
- writeLineBuffer()
- end
- function reset()
- AUTHENTICATED = false
- end
- -- [ --------------------------------------------------------------------------
- function send(type, body)
- local request = {protocol = "rnc", username = USERNAME, type = type, body = body}
- rednet.broadcast(textutils.serialize(request))
- end
- function sendMessage(message)
- send("message", message)
- end
- function sendCommand(command)
- send("command", command)
- end
- function recieve()
- local sender, request, distance = rednet.receive(1)
- if request == "" or request == nil then
- return
- end
- request = textutils.unserialize(request)
- if request.protocol ~= "rnc" then
- return
- end
- if request.type == "command" then
- if request.body == "online" then
- addMessage("[CONSOLE] " .. request.username .. " is now online")
- table.insert(ONLINE, request.username)
- updateOnlineList()
- sendCommand("metoo")
- elseif request.body == "offline" then
- addMessage("[CONSOLE] " .. request.username .. " is now offline")
- for i, v in ipairs(ONLINE) do
- if ONLINE[i] == request.username then
- table.remove(ONLINE, i)
- end
- end
- updateOnlineList()
- elseif request.body == "metoo" then
- table.insert(ONLINE, request.username)
- updateOnlineList()
- end
- elseif request.type == "message" then
- addMessage(request.body)
- end
- end
- function startSession()
- table.insert(ONLINE, USERNAME)
- updateOnlineList()
- sendCommand("online")
- end
- function endSession()
- sendCommand("offline")
- end
- -- ] --------------------------------------------------------------------------
- if fs.exists(FILE_CONFIG) then
- local file = fs.open(FILE_CONFIG, "r")
- local config = textutils.unserialize(file:readLine())
- file:close()
- USERNAME = config.username
- PASSWORD = config.password
- if USERNAME == nil or username == "" or PASSWORD == nil or PASSWORD == "" then
- install()
- end
- else
- install()
- end
- term.clear()
- term.setCursorPos(1, 1)
- print("Welcome " .. USERNAME)
- while AUTHENTICATED == false do
- term.setCursorPos(1, 3)
- local password = inputAuthenticate()
- if password == PASSWORD then
- AUTHENTICATED = true
- RUNNING = true
- end
- end
- term.clear()
- term.setCursorPos(1, 2)
- term.write("-------------------------")
- term.setCursorPos(1, 4)
- term.write("-------------------------")
- rednet.open("right")
- startSession()
- parallel.waitForAny(handleInput, updateBuffer)
- rednet.close("right")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement