Advertisement
mc1030

chat_server

Nov 22nd, 2020 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | None | 0 0
  1. -- CHAT SERVER
  2. -- by mcprog
  3.  
  4. SERVER_CHAN = 5
  5. CLIENT_CHAN = 1
  6. version = "s1.0.2"
  7.  
  8. SERVER_MSG = "serverMsg"
  9. SUPER_CHAT = "/superchat"
  10. WHISPER = "/whisper"
  11. CMD = "cmd"
  12.  
  13. SRC_SERVER = "server"
  14.  
  15. users = {}
  16. userCount = 0
  17. op = "mc1030"
  18.  
  19. local modem = peripheral.wrap("left")
  20.  
  21. function resetLine()
  22.     term.clearLine()
  23.     x,y = term.getCursorPos()
  24.     term.setCursorPos(1, y)
  25. end
  26.  
  27. function printColor(msg,textColor)
  28.     term.setTextColor(textColor)
  29.     resetLine()
  30.     print(msg)
  31. end
  32.  
  33. function split(s, delimiter)
  34.     result = {};
  35.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  36.         table.insert(result, match);
  37.     end
  38.     return result;
  39. end
  40.  
  41. function join(array, delimiter, indexStart)
  42.     result = ""
  43.     for i,v in ipairs(array) do
  44.         if i >= indexStart then
  45.             result = result + " " + i
  46.         end
  47.     end
  48.     return result
  49. end
  50.  
  51. function post(message)
  52.     modem.transmit(CLIENT_CHAN, SERVER_CHAN, message)
  53. end
  54.  
  55. function addUser(id, loginTime)
  56.     users[id] = loginTime
  57.     userCount = userCount + 1
  58.    
  59.     printColor(id.." joined at "..textutils.formatTime(loginTime, false)..".", colors.lightBlue)
  60. end
  61.  
  62. function removeUser(id, logoutTime)
  63.     users[id] = nil
  64.     userCount = userCount - 1
  65.     if userCount < 0 then
  66.         userCount = 0
  67.     end
  68.     printColor(id.." left at "..textutils.formatTime(logoutTime, false)..".", colors.blue)
  69. end
  70.  
  71. function listen()
  72.     local event, side, senderChan, replyChan, message, dist = os.pullEvent("modem_message")
  73.     if message then
  74.         local currentTime = os.time()
  75.         if message.type == "join" then
  76.             addUser(message.id, currentTime)
  77.            
  78.             post(message)
  79.         elseif message.type == "left" then
  80.             removeUser(message.id, currentTime)
  81.             post(message)
  82.         elseif message.type == "chat" then
  83.             if users[message.id] == nil then
  84.                addUser(message.id, currentTime)
  85.             end
  86.             post(message)
  87.         elseif message.type == CMD then
  88.             if message.id == op then
  89.                 -- use src as command name
  90.                 command(message.src, message.id, message.value, message.extra) --cmd, from, to
  91.             else -- client asks to run cmd but is rejected
  92.                 whisper = {
  93.                     type = WHISPER,
  94.                     src = SRC_SERVER,
  95.                     id = message.id,
  96.                     value = "You are not opped, " ..(message.id).."."
  97.                 }
  98.                 post(whisper)
  99.                 printColor(message.id.. "tried to run "..message.src..", but is not opped.", colors.white)
  100.             end
  101.         else
  102.             printColor("Error! Bad message:", colors.red)
  103.             printColor(textutils.serialize(message), colors.red)
  104.         end
  105.     end
  106. end
  107.  
  108. function command(cmdName, from, to, meta)
  109.     if cmdName == "/kick" then
  110.         serverMsg = {
  111.             type = SERVER_MSG,
  112.             src = from,
  113.             id = to,
  114.             value = "/kicked"
  115.         }
  116.         printColor("Trying to kick: ".. to, colors.orange)
  117.         post(serverMsg)
  118.     elseif cmdName == SUPER_CHAT then
  119.         superChat = {
  120.             type = SUPER_CHAT,
  121.             src = from,
  122.             value = meta
  123.         }
  124.         post(superChat)
  125.     elseif cmdName == "/shutdown" then
  126.         serverMsg = {
  127.             type = SERVER_MSG,
  128.             src = from,
  129.             value = "/shutdown"
  130.         }
  131.         post(serverMsg)
  132.         print("Shuting down chat_server.")
  133.         error()
  134.     elseif cmdName == WHISPER then
  135.         whisper = {
  136.             type = WHISPER,
  137.             src = from,
  138.             id = to,
  139.             value = meta
  140.         }
  141.         post(whisper)
  142.     elseif cmdName == "users" then
  143.         onlineMsg = "Users online: " ..tostring(userCount).."."
  144.         if from == SRC_SERVER then
  145.             printColor(onlineMsg, colors.white)
  146.         else
  147.             serverWhisper(from, "Unknown command: " ..cmdName)
  148.         end
  149.     else
  150.         serverWhisper(from, "Unknown command: " ..cmdName)
  151.     end
  152. end
  153.  
  154. function serverWhisper(to, msg)
  155.     whisper = {
  156.         type = WHISPER,
  157.         src = SRC_SERVER,
  158.         id = to,
  159.         value = msg
  160.     }
  161.     post(whisper)
  162. end
  163.  
  164. function acceptLocalCommands()
  165.     resetLine()
  166.     term.setTextColor(colors.white)
  167.     write("$ ")
  168.     input = read()
  169.     parsed = split(input, " ")
  170.     if parsed then
  171.         if parsed[1] == "kick" and #parsed == 2 then
  172.             command("/kick", SRC_SERVER, parsed[2])
  173.         elseif parsed[1] == "shutdown" then
  174.             command("/shutdown", SRC_SERVER)
  175.         elseif parsed[1] == "users" then
  176.             command("users", SRC_SERVER) --server side only
  177.         elseif parsed[1] == SUPER_CHAT and #parsed == 2 then
  178.             command(SUPER_CHAT, SRC_SERVER, parsed[2])
  179.         elseif parsed[1] == "whisper" and #parsed > 3 then
  180.             command("/whisper", SRC_SERVER, parsed[2], join(parsed, " ", 3))
  181.         end
  182.     else
  183.         print("Invalid command: " ..input)
  184.     end
  185. end
  186.  
  187. function start()
  188.     modem.open(CLIENT_CHAN)
  189.     term.clear()
  190.     term.setCursorPos(1, 1)
  191.  
  192.     printColor("Server version: "..version, colors.white)
  193.  
  194.     while true do
  195.         parallel.waitForAny(acceptLocalCommands, listen)
  196.     end
  197. end
  198.  
  199. start()
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement