Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- CHAT SERVER
- -- by mcprog
- SERVER_CHAN = 5
- CLIENT_CHAN = 1
- version = "s1.0.2"
- SERVER_MSG = "serverMsg"
- SUPER_CHAT = "/superchat"
- WHISPER = "/whisper"
- CMD = "cmd"
- SRC_SERVER = "server"
- users = {}
- userCount = 0
- op = "mc1030"
- local modem = peripheral.wrap("left")
- function resetLine()
- term.clearLine()
- x,y = term.getCursorPos()
- term.setCursorPos(1, y)
- end
- function printColor(msg,textColor)
- term.setTextColor(textColor)
- resetLine()
- print(msg)
- end
- function split(s, delimiter)
- result = {};
- for match in (s..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- function join(array, delimiter, indexStart)
- result = ""
- for i,v in ipairs(array) do
- if i >= indexStart then
- result = result + " " + i
- end
- end
- return result
- end
- function post(message)
- modem.transmit(CLIENT_CHAN, SERVER_CHAN, message)
- end
- function addUser(id, loginTime)
- users[id] = loginTime
- userCount = userCount + 1
- printColor(id.." joined at "..textutils.formatTime(loginTime, false)..".", colors.lightBlue)
- end
- function removeUser(id, logoutTime)
- users[id] = nil
- userCount = userCount - 1
- if userCount < 0 then
- userCount = 0
- end
- printColor(id.." left at "..textutils.formatTime(logoutTime, false)..".", colors.blue)
- end
- function listen()
- local event, side, senderChan, replyChan, message, dist = os.pullEvent("modem_message")
- if message then
- local currentTime = os.time()
- if message.type == "join" then
- addUser(message.id, currentTime)
- post(message)
- elseif message.type == "left" then
- removeUser(message.id, currentTime)
- post(message)
- elseif message.type == "chat" then
- if users[message.id] == nil then
- addUser(message.id, currentTime)
- end
- post(message)
- elseif message.type == CMD then
- if message.id == op then
- -- use src as command name
- command(message.src, message.id, message.value, message.extra) --cmd, from, to
- else -- client asks to run cmd but is rejected
- whisper = {
- type = WHISPER,
- src = SRC_SERVER,
- id = message.id,
- value = "You are not opped, " ..(message.id).."."
- }
- post(whisper)
- printColor(message.id.. "tried to run "..message.src..", but is not opped.", colors.white)
- end
- else
- printColor("Error! Bad message:", colors.red)
- printColor(textutils.serialize(message), colors.red)
- end
- end
- end
- function command(cmdName, from, to, meta)
- if cmdName == "/kick" then
- serverMsg = {
- type = SERVER_MSG,
- src = from,
- id = to,
- value = "/kicked"
- }
- printColor("Trying to kick: ".. to, colors.orange)
- post(serverMsg)
- elseif cmdName == SUPER_CHAT then
- superChat = {
- type = SUPER_CHAT,
- src = from,
- value = meta
- }
- post(superChat)
- elseif cmdName == "/shutdown" then
- serverMsg = {
- type = SERVER_MSG,
- src = from,
- value = "/shutdown"
- }
- post(serverMsg)
- print("Shuting down chat_server.")
- error()
- elseif cmdName == WHISPER then
- whisper = {
- type = WHISPER,
- src = from,
- id = to,
- value = meta
- }
- post(whisper)
- elseif cmdName == "users" then
- onlineMsg = "Users online: " ..tostring(userCount).."."
- if from == SRC_SERVER then
- printColor(onlineMsg, colors.white)
- else
- serverWhisper(from, "Unknown command: " ..cmdName)
- end
- else
- serverWhisper(from, "Unknown command: " ..cmdName)
- end
- end
- function serverWhisper(to, msg)
- whisper = {
- type = WHISPER,
- src = SRC_SERVER,
- id = to,
- value = msg
- }
- post(whisper)
- end
- function acceptLocalCommands()
- resetLine()
- term.setTextColor(colors.white)
- write("$ ")
- input = read()
- parsed = split(input, " ")
- if parsed then
- if parsed[1] == "kick" and #parsed == 2 then
- command("/kick", SRC_SERVER, parsed[2])
- elseif parsed[1] == "shutdown" then
- command("/shutdown", SRC_SERVER)
- elseif parsed[1] == "users" then
- command("users", SRC_SERVER) --server side only
- elseif parsed[1] == SUPER_CHAT and #parsed == 2 then
- command(SUPER_CHAT, SRC_SERVER, parsed[2])
- elseif parsed[1] == "whisper" and #parsed > 3 then
- command("/whisper", SRC_SERVER, parsed[2], join(parsed, " ", 3))
- end
- else
- print("Invalid command: " ..input)
- end
- end
- function start()
- modem.open(CLIENT_CHAN)
- term.clear()
- term.setCursorPos(1, 1)
- printColor("Server version: "..version, colors.white)
- while true do
- parallel.waitForAny(acceptLocalCommands, listen)
- end
- end
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement