Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local m = component.modem
- local gpu = component.gpu
- local thread = require("thread")
- local term = require("term")
- Enable_Profanity = true --default true
- ProfanityFilter = {}
- Netbuffer = {}
- UserList = {}
- InputStr = ""
- function main()
- io.write("Address: ")
- Maddr = io.read()
- io.write("Port: ")
- Mport = tonumber(io.read())
- m.open(Mport)
- print("Binding...")
- if m.isOpen(Mport) then
- print("Connected to Bind...")
- while true do
- for user,data in pairs(UserList) do
- if data[2] == 100000 then
- m.send(user,Mport,"/%PING")
- elseif data[2] == 150000 then
- table.remove(UserList, user, 1)
- m.send(user,Mport,"/%DISCONNECTTimed Out")
- m.broadcast(Mport,"/%USRLEAVE<user["..user.."] was disconnected from the server, Reason: Timed Out>")
- else
- data[2] = data[2] + 1
- end
- end
- data = grabNB()
- if data ~= nil then
- print("data: \""..data[2].."\"")
- if data[2]:sub(0, 9) == "/%SENDMSG" then
- if Enable_Profanity == false then
- local tmp,str,Profanity = "","",false
- for i=1, #data[2]:sub( 9,-1) do
- if #data[2]:sub( 9,-1)[i] == " " or i == #data[2]:sub( 9,-1) then
- for j=1, #ProfanityFilter do
- if tmp:lowwer() == ProfanityFilter[j] then
- Profanity = true
- for i=1, #tmp do
- str = str.."*"
- end
- str = str.." "
- break
- end
- end
- if Profanity == false then
- str = str..tmp
- end
- else
- tmp = tmp..data[2][i]
- end
- end
- m.broadcast(Mport,"/%SYSMSG*["..UserList[data[1]].."]: "..str)
- print("*MSG["..UserList[data[1]].."]: "..str)
- else
- m.broadcast(Mport,"/%SYSMSG["..UserList[data[1]].."]: "..data[2]:sub( 9,-1))
- print("MSG["..UserList[data[1]].."]: "..data[2]:sub( 9,-1))
- end
- elseif data[2]:sub(0, 9) == "/%LEAVE" then
- elseif data[2]:sub(0, 6) == "/%ADDR" then
- io.write("Found new connection: ")
- if data[2]:sub(7, -1) == Maddr then
- print(data[1])
- m.send(data[1],Mport,"/%CONFIRMED")
- else
- print("\nTheir requested address didn't match ours...")
- end
- elseif data[2]:sub(0, 6) == "/%USER" then
- local UserExists = false
- for i in UserList do
- if i == data[2]:sub(0, 9) then
- UserExists = true
- end
- end
- if UserExists == false then
- m.send(data[1],Mport,"/%CONFIRMED")
- UserList[data[1]] = {data[2]:sub(0, 9), 0}
- m.broadcast(Mport,"/%USRJOIN<user["..data[2]:sub(0, 9).."] has joined the server>")
- else
- m.send(data[1],Mport,"/%DENIED")
- end
- elseif data[2]:sub(0, 6) == "/%PING" then
- UserData[data[1]][2] = 0
- end
- end
- end
- else
- print("Bind Failed...")
- os.exit()
- end
- width,height = getResolution()
- while true do
- term.setCursor(0,height)
- for i=1, #ChatLog do
- print(ChatLog[i])
- end
- io.write(".")
- for i=3,width do print("-") end
- io.write(".\n|"..InputStr)
- term.setCursor(width-1,height)
- io.write("|")
- end
- end
- function grabNB()
- os.sleep(0)
- local out
- if #Netbuffer > 0 then
- out = Netbuffer[#Netbuffer]
- table.remove(Netbuffer,1,#Netbuffer)
- else
- out = nil
- end
- return out
- end
- thread.create(function()
- while true do
- local type, _, FromSig, PortChar, _, msg = event.pull()
- if type == "modem_message" then
- print(type, FromSig, PortChar, msg)
- table.insert(Netbuffer,{FromSig, msg})
- -- elseif type == "key_down" then
- -- if sig == 8 and char == 14 then -- [<-BackSpace]
- -- local tmp = ""; for i=1,#InputStr-1 do tmp=tmp..InputStr end
- -- elseif FromSig == 13 and PortChar == 28 then -- [Enter<-/]
- -- send("/%MSG"..Muser, Mport)
- -- elseif FromSig == 13 and PortChar == 28 then -- [CTRL]
- -- --IGNORE
- -- elseif FromSig == 13 and PortChar == 28 then -- [SHIFT]
- -- --IGNORE
- -- elseif sigFromSig == 3 and PortChar == 46 then -- [CTRL]+[C]
- -- send("/%LEAVE"..Muser, Mport)
- -- else
- -- InputStr = InputStr..PortChar
- -- end
- end
- end
- end)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement