Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local computer = require("computer")
- local m = component.modem
- local gpu = component.gpu
- local thread = require("thread")
- local term = require("term")
- Netbuffer = {}
- UserList = {}
- ChatLog = {}
- SeverAddress = ""
- InputStr = ""
- exit = false
- function main()
- io.write("Address: ")
- Maddr = io.read()
- io.write("Port: ")
- Mport = tonumber(io.read())
- io.write("UserName: ")
- Muser = io.read()
- m.open(Mport)
- print("Connecting...")
- local x,y = term.getCursor()
- if m.isOpen(Mport) then
- m.broadcast(Mport, "/%ADDR"..Maddr)
- local time,ti = os.clock(), os.clock()
- while true do
- if os.clock() == ti+0.1 then
- local x,y = term.getCursor()
- io.write(".")
- ti = os.clock()
- if x-10 == 3 then term.setCursor(10,y) io.write(" ") term.setCursor(10,y) end
- end
- if os.clock() == time+5 then break end
- data = grabNB()
- if data ~= nil then
- if data[2] == "/%CONFIRMED" then
- connected = true
- SeverAddress = data[1]
- end
- end
- end
- term.setCursor(10,y)
- if connected then
- data = ""
- while data[2] ~= "/%CONFIRMED" and data[1] == SeverAddress do
- data = grabNB()
- print(" Successful, Logging into server as \""..Muser.."\".")
- send("/%USER"..Muser, Mport)
- end
- local time = os.clock()
- while true do
- if os.clock() == time+5 then
- data = grabNB()
- while true do
- data = grabNB()
- if data ~= nil then
- if data[2] == "/%USRLSTEND" and data[1] == SeverAddress then
- break
- end
- table.insert(UserList,data)
- end
- end
- print("UserList:")
- for i=1, #UserList do
- io.write("\\"..UserList[i].."\t")
- end
- break
- end
- end
- else
- print("Connection Failed...")
- end
- else
- print("Failed to connect to port...")
- os.exit()
- end
- width,height = getResolution()
- while true do
- data = grabNB()
- if data ~= nil then
- if data[2]:sub(0, 8) == "/%SYSMSG" and data[1] == SeverAddress then
- ChatLog:insert(data:sub(9,-1))
- if string.match(data:sub(9,-1),Muser) == Muser then computer.beep() end
- elseif data[2]:sub(0, 6) == "/%PING" and data[1] == SeverAddress then
- m.send(data[1],Mport,"/%PING")
- elseif data[2]:sub(0,12) == "/%DISCONNECT" and data[1] == SeverAddress then
- local reason
- if data[2]:sub(13,-1) == "" then
- reason = "*unknown*"
- else
- reason = data[2]:sub(13,-1)
- end
- print("You have been Disconnected by the server, Reason: "..reason)
- os.exit()
- end
- end
- 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)
- if exit == true then os.exit() end
- 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 FromSig == 8 and PortChar == 14 then -- [<-BackSpace]
- local tmp = ""; for i=1,#InputStr-1 do tmp=tmp..InputStr end
- elseif FromSig == 13 and PortChar == 28 then -- [Enter<-/]
- m.send(SeverAddress,Mport, "/%SENDMSG"..InputStr)
- elseif FromSig == 13 and PortChar == 28 then -- [CTRL] --IGNORE
- elseif FromSig == 13 and PortChar == 28 then -- [SHIFT] --IGNORE
- elseif FromSig == 3 and PortChar == 46 then -- [CTRL]+[C]
- m.send(SeverAddress,Mport,"/%LEAVE")
- print("Exiting...")
- table.insert(Netbuffer,{m.address, "/%"})
- break
- else
- InputStr = InputStr..PortChar
- end
- end
- end
- end)
- main()
Add Comment
Please, Sign In to add comment