BILLPC2684

chat-client.lua

Dec 9th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.90 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local computer = require("computer")
  4. local m = component.modem
  5. local gpu = component.gpu
  6. local thread = require("thread")
  7. local term = require("term")
  8.  
  9. Netbuffer = {}
  10. UserList = {}
  11. ChatLog = {}
  12. SeverAddress = ""
  13. InputStr = ""
  14. exit = false
  15.  
  16. function main()
  17.  io.write("Address: ")
  18.  Maddr = io.read()
  19.  io.write("Port: ")
  20.  Mport = tonumber(io.read())
  21.  io.write("UserName: ")
  22.  Muser = io.read()
  23.  
  24.  m.open(Mport)
  25.  print("Connecting...")
  26.  local x,y = term.getCursor()
  27.  if m.isOpen(Mport) then
  28.   m.broadcast(Mport, "/%ADDR"..Maddr)
  29.   local time,ti = os.clock(), os.clock()
  30.   while true do
  31.    if os.clock() == ti+0.1 then
  32.     local x,y = term.getCursor()
  33.     io.write(".")
  34.     ti = os.clock()
  35.     if x-10 == 3 then term.setCursor(10,y) io.write("   ") term.setCursor(10,y) end
  36.    end
  37.    if os.clock() == time+5 then break end
  38.    data = grabNB()
  39.    if data ~= nil then
  40.     if data[2] == "/%CONFIRMED" then
  41.      connected = true
  42.      SeverAddress = data[1]
  43.     end
  44.    end
  45.   end
  46.   term.setCursor(10,y)
  47.   if connected then
  48.    data = ""
  49.    while data[2] ~= "/%CONFIRMED" and data[1] == SeverAddress do
  50.     data = grabNB()
  51.     print(" Successful, Logging into server as \""..Muser.."\".")
  52.     send("/%USER"..Muser, Mport)
  53.    end
  54.    local time = os.clock()
  55.    while true do
  56.     if os.clock() == time+5 then
  57.      data = grabNB()
  58.      while true do
  59.       data = grabNB()
  60.       if data ~= nil then
  61.        if data[2] == "/%USRLSTEND" and data[1] == SeverAddress then
  62.         break
  63.        end
  64.        table.insert(UserList,data)
  65.       end
  66.      end
  67.      print("UserList:")
  68.      for i=1, #UserList do
  69.       io.write("\\"..UserList[i].."\t")
  70.      end
  71.      break
  72.     end
  73.    end
  74.   else
  75.    print("Connection Failed...")
  76.   end
  77.  else
  78.   print("Failed to connect to port...")
  79.   os.exit()
  80.  end
  81.  width,height = getResolution()
  82.  while true do
  83.   data = grabNB()
  84.   if data ~= nil then
  85.    if     data[2]:sub(0, 8) == "/%SYSMSG"     and data[1] == SeverAddress then
  86.     ChatLog:insert(data:sub(9,-1))
  87.     if string.match(data:sub(9,-1),Muser) == Muser then computer.beep() end
  88.    elseif data[2]:sub(0, 6) == "/%PING"       and data[1] == SeverAddress then
  89.     m.send(data[1],Mport,"/%PING")
  90.    elseif data[2]:sub(0,12) == "/%DISCONNECT" and data[1] == SeverAddress then
  91.     local reason
  92.     if data[2]:sub(13,-1) == "" then
  93.      reason = "*unknown*"
  94.     else
  95.      reason = data[2]:sub(13,-1)
  96.     end
  97.     print("You have been Disconnected by the server, Reason: "..reason)
  98.     os.exit()
  99.    end
  100.   end
  101.   term.setCursor(0,height)
  102.   for i=1, #ChatLog do
  103.    print(ChatLog[i])
  104.   end
  105.   io.write(".")
  106.   for i=3,width do print("-") end
  107.   io.write(".\n|"..InputStr)
  108.   term.setCursor(width-1,height)
  109.   io.write("|")
  110.  end
  111. end
  112.  
  113. function grabNB()
  114.  os.sleep(0)
  115.  if exit == true then os.exit() end
  116.  local out
  117.  if #Netbuffer > 0 then
  118.   out = Netbuffer[#Netbuffer]
  119.   table.remove(Netbuffer,1,#Netbuffer)
  120.  else
  121.   out = nil
  122.  end
  123.  return out
  124. end
  125.  
  126. thread.create(function()
  127.  while true do
  128.   local type, _, FromSig, PortChar, _, msg = event.pull()
  129.   if     type == "modem_message" then
  130.    print(type, FromSig, PortChar, msg)
  131.    table.insert(Netbuffer,{FromSig, msg})
  132.   elseif type == "key_down" then
  133.    if     FromSig ==  8 and PortChar == 14 then -- [<-BackSpace]
  134.     local tmp = ""; for i=1,#InputStr-1 do tmp=tmp..InputStr end
  135.    elseif FromSig == 13 and PortChar == 28 then -- [Enter<-/]
  136.     m.send(SeverAddress,Mport, "/%SENDMSG"..InputStr)
  137.    elseif FromSig == 13 and PortChar == 28 then -- [CTRL]    --IGNORE
  138.    elseif FromSig == 13 and PortChar == 28 then -- [SHIFT]   --IGNORE
  139.    elseif FromSig ==  3 and PortChar == 46 then -- [CTRL]+[C]
  140.     m.send(SeverAddress,Mport,"/%LEAVE")
  141.     print("Exiting...")
  142.     table.insert(Netbuffer,{m.address, "/%"})
  143.     break
  144.    else
  145.     InputStr = InputStr..PortChar
  146.    end
  147.   end
  148.  end
  149. end)
  150.  
  151. main()
Add Comment
Please, Sign In to add comment