Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modem = peripheral.find("modem")
- local event, side, sChannel, id, data, dist
- local sessions = {}
- local channel = math.random(50000, 60000)
- local topBar = window.create(term.native(), 1, 1, 52, 1)
- local bg = window.create(term.native(), 1, 2, 52, 15)
- local chatBox = window.create(term.native(), 1, 17, 51, 3)
- chatBox.setBackgroundColor(colors.gray)
- chatBox.clear()
- bg.setBackgroundColor(colors.lightGray)
- bg.clear()
- bg.setCursorPos(1, 2)
- topBar.setBackgroundColor(colors.blue)
- topBar.clear()
- topBar.setCursorPos(1, 1)
- topBar.write("Cloud Chat Server")
- modem.open(os.getComputerID())
- local function writeMsg(input)
- input = input .. " "
- local recursive
- if #input > 50 and input:find("%s") then
- recursive = true
- local space = input:find("%s", 50)
- local line = input:sub(0, space - 1)
- input = input:sub(space + 1)
- bg.write(line)
- else
- recursive = false
- bg.write(input)
- end
- local x, y = bg.getCursorPos()
- if y >= 15 then
- bg.scroll(1)
- bg.setCursorPos(1, y)
- else
- bg.setCursorPos(1, y + 1)
- end
- if recursive then
- writeMsg(input)
- end
- end
- local function receive()
- event, side, sChannel, id, data, dist = os.pullEvent("modem_message")
- end
- local function send(id, msg)
- modem.transmit(id, os.getComputerID(), msg)
- end
- local function indexOf(table, val)
- for index, tmp in pairs(table) do
- if tmp == val then
- return index
- end
- end
- return nil
- end
- local function getSID()
- local rand = math.random(0, 10000)
- for index, tmp in pairs(sessions) do
- if tmp == rand then
- return getSID()
- end
- end
- return rand
- end
- local function input()
- chatBox.clear()
- chatBox.setCursorPos(1, 1)
- term.redirect(chatBox)
- data = read()
- end
- while true do
- event = parallel.waitForAny(receive, input)
- if event == 1 then
- local sid, user, msg
- if data:find(":") then
- local c = data:find(":")
- sid = tonumber(data:sub(0, c - 1))
- msg = data:sub(c + 1)
- user = indexOf(sessions, sid)
- if user == nil then
- send(id, "No such session")
- elseif msg:sub(0, 1) == "!" then
- if msg == "!logout" then
- sessions[user] = nil
- send(id, "You have been logged out")
- elseif msg:find("%s") then
- local space = msg:find("%s")
- local command = msg:sub(0, space - 1)
- if command == "!nick" then
- local newName = msg:sub(space + 1)
- sessions[newName] = sid
- sessions[user] = nil
- msg = (user .. " changed nick to " .. newName)
- send(channel, msg)
- writeMsg(msg)
- end
- elseif msg == "!list" then
- msg = "Users: "
- for index, data in pairs(sessions) do
- msg = msg .. " " .. index
- end
- send(id, msg)
- elseif msg == "!whoami" then
- send(id, user)
- else
- send(id, "No such command")
- end
- else
- msg = (user .. "> " .. msg)
- send(channel, msg)
- writeMsg(msg)
- end
- else
- user = data
- if sessions[user] == nil then
- sessions[user] = getSID()
- modem.transmit(id, channel, sessions[user])
- else
- send(id, "Username in use")
- end
- end
- else
- if data ~= "" then
- send(channel, "Server> " .. data)
- writeMsg("Server> " .. data)
- end
- end
- end
Add Comment
Please, Sign In to add comment