Advertisement
BILLPC2684

chat-server.lua

Dec 9th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.11 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local m = component.modem
  4. local gpu = component.gpu
  5. local thread = require("thread")
  6. local term = require("term")
  7.  
  8. Enable_Profanity = true --default true
  9. ProfanityFilter = {}
  10.  
  11. Netbuffer = {}
  12. UserList = {}
  13. InputStr = ""
  14.  
  15. function main()
  16.  io.write("Address: ")
  17.  Maddr = io.read()
  18.  io.write("Port: ")
  19.  Mport = tonumber(io.read())
  20.  
  21.  m.open(Mport)
  22.  print("Binding...")
  23.  if m.isOpen(Mport) then
  24.   print("Connected to Bind...")
  25.   while true do
  26.    for user,data in pairs(UserList) do
  27.     if     data[2] == 100000 then
  28.      m.send(user,Mport,"/%PING")
  29.     elseif data[2] == 150000 then
  30.      table.remove(UserList, user, 1)
  31.      m.send(user,Mport,"/%DISCONNECTTimed Out")
  32.      m.broadcast(Mport,"/%USRLEAVE<user["..user.."] was disconnected from the server, Reason: Timed Out>")
  33.     else
  34.      data[2] = data[2] + 1
  35.     end
  36.    end
  37.    data = grabNB()
  38.    if data ~= nil then
  39.     print("data: \""..data[2].."\"")
  40.     if     data[2]:sub(0, 9) == "/%SENDMSG" then
  41.      if Enable_Profanity == false then
  42.       local tmp,str,Profanity = "","",false
  43.       for i=1, #data[2]:sub( 9,-1) do
  44.        if #data[2]:sub( 9,-1)[i] == " " or i == #data[2]:sub( 9,-1) then
  45.         for j=1, #ProfanityFilter do
  46.          if tmp:lowwer() == ProfanityFilter[j] then
  47.           Profanity = true
  48.           for i=1, #tmp do
  49.            str = str.."*"
  50.           end
  51.           str = str.." "
  52.           break
  53.          end
  54.         end
  55.         if Profanity == false then
  56.          str = str..tmp
  57.         end
  58.        else
  59.         tmp = tmp..data[2][i]
  60.        end
  61.       end
  62.       m.broadcast(Mport,"/%SYSMSG*["..UserList[data[1]].."]: "..str)
  63.       print("*MSG["..UserList[data[1]].."]: "..str)
  64.      else
  65.       m.broadcast(Mport,"/%SYSMSG["..UserList[data[1]].."]: "..data[2]:sub( 9,-1))
  66.       print("MSG["..UserList[data[1]].."]: "..data[2]:sub( 9,-1))
  67.      end
  68.     elseif data[2]:sub(0, 9) == "/%LEAVE" then
  69.      
  70.     elseif data[2]:sub(0, 6) == "/%ADDR" then
  71.      io.write("Found new connection: ")
  72.      if data[2]:sub(7, -1) == Maddr then
  73.       print(data[1])
  74.       m.send(data[1],Mport,"/%CONFIRMED")
  75.      else
  76.       print("\nTheir requested address didn't match ours...")
  77.      end
  78.     elseif data[2]:sub(0, 6) == "/%USER" then
  79.      local UserExists = false
  80.      for i in UserList do
  81.       if i == data[2]:sub(0, 9) then
  82.        UserExists = true
  83.       end
  84.      end
  85.      if UserExists == false then
  86.       m.send(data[1],Mport,"/%CONFIRMED")
  87.       UserList[data[1]] = {data[2]:sub(0, 9), 0}
  88.       m.broadcast(Mport,"/%USRJOIN<user["..data[2]:sub(0, 9).."] has joined the server>")
  89.      else
  90.       m.send(data[1],Mport,"/%DENIED")
  91.      end
  92.     elseif data[2]:sub(0, 6) == "/%PING" then
  93.      UserData[data[1]][2] = 0
  94.     end
  95.    end
  96.   end
  97.  else
  98.   print("Bind Failed...")
  99.   os.exit()
  100.  end
  101.  width,height = getResolution()
  102.  while true do
  103.   term.setCursor(0,height)
  104.   for i=1, #ChatLog do
  105.    print(ChatLog[i])
  106.   end
  107.   io.write(".")
  108.   for i=3,width do print("-") end
  109.   io.write(".\n|"..InputStr)
  110.   term.setCursor(width-1,height)
  111.   io.write("|")
  112.  end
  113. end
  114.  
  115. function grabNB()
  116.  os.sleep(0)
  117.  local out
  118.  if #Netbuffer > 0 then
  119.   out = Netbuffer[#Netbuffer]
  120.   table.remove(Netbuffer,1,#Netbuffer)
  121.  else
  122.   out = nil
  123.  end
  124.  return out
  125. end
  126.  
  127. thread.create(function()
  128.  while true do
  129.   local type, _, FromSig, PortChar, _, msg = event.pull()
  130.   if     type == "modem_message" then
  131.    print(type, FromSig, PortChar, msg)
  132.    table.insert(Netbuffer,{FromSig, msg})
  133. --  elseif type == "key_down" then
  134. --   if     sig ==  8 and char == 14 then -- [<-BackSpace]
  135. --    local tmp = ""; for i=1,#InputStr-1 do tmp=tmp..InputStr end
  136. --   elseif FromSig == 13 and PortChar == 28 then -- [Enter<-/]
  137. --    send("/%MSG"..Muser, Mport)
  138. --   elseif FromSig == 13 and PortChar == 28 then -- [CTRL]
  139. --    --IGNORE
  140. --   elseif FromSig == 13 and PortChar == 28 then -- [SHIFT]
  141. --    --IGNORE
  142. --   elseif sigFromSig ==  3 and PortChar == 46 then -- [CTRL]+[C]
  143. --    send("/%LEAVE"..Muser, Mport)
  144. --   else
  145. --    InputStr = InputStr..PortChar
  146. --   end
  147.   end
  148.  end
  149. end)
  150.  
  151. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement