Advertisement
Guest User

chatserv

a guest
Mar 28th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.56 KB | None | 0 0
  1. rednet.open("top")
  2.  
  3. local connect1=false
  4. local connect2=false
  5. local user1id
  6. local user2id
  7. function handshakes()
  8. local senderID, toConnect=rednet.receive("cProt")
  9. user1id=senderID
  10. user2id=tonumber(toConnect)
  11. rednet.send(senderID,"handshake","handshake")
  12. print("sent user # "..senderID.." a handshake")
  13. rednet.send(tonumber(toConnect),"handshake","handshake")
  14. print("sent user # "..toConnect.." a handshake")
  15. local l, userOneHandshake,u6 = rednet.receive("handshakeConfirm")
  16. if(userOneHandshake == "confirm") then
  17.   connect1=true
  18.   print("user "..l.." handshake confirmed")
  19. end
  20. local p, userTwoHandshake,u1 = rednet.receive("handshakeConfirm")
  21. if(userTwoHandshake=="confirm") then
  22.   print("user "..p.." handshake confirmed")
  23. end
  24. rednet.send(82,user1id,"dbGet")
  25. print("requesting nickname data from database")
  26. local u2,nick1=rednet.receive("dbNickSend")
  27. print("recieved data")
  28. rednet.send(user2id,nick1,"nickProt")
  29. print("sent user: "..user2id.." default username: "..nick1)
  30. rednet.send(82,tostring(user2id),"dbGet")
  31. print("requesting nickname data from database")
  32. local u3, nick2=rednet.receive("dbNickSend")
  33. print("recieved data")
  34. rednet.send(user1id,nick2,"nickProt")
  35. print("sent user: "..user1id.." default username: "..nick2)
  36. connect2=true
  37. end
  38.  
  39. function messageTransfer()
  40. local id,msg,u4=rednet.receive("messageProtocol")
  41. print("message recieved. relaying message")
  42. if id==tonumber(user1id) then
  43.   rednet.send (user2id,msg,"MPincoming")
  44.   print("message sent to user 2")
  45.   elseif id==tonumber(user2id) then
  46.   rednet.send(user1id, msg,"MPincoming")
  47.   print("message sent to user 1")
  48. end
  49. end
  50. function Nicknames()
  51. local IDc, cmd, u5=rednet.receive("commandProtocol")
  52. local find, u6 =string.find(cmd,"/nick",1,true)
  53. local cmdlength=string.len(cmd)
  54. if find==1 then
  55.   local nickname=string.sub(cmd,7)
  56.   print("user "..IDc.." changed nickname to "..nickname)
  57.   if IDc==user1id then
  58.    rednet.send(user2id,nickname,"nickProt")
  59.   else if IDc==user2id then
  60.    rednet.send(user1id,nickname,"nickProt")
  61.   end
  62. end
  63. end
  64. end
  65.  
  66. function relayConnections()
  67. local senderID, toConnect=rednet.receive("cProt")
  68. if connect1==true and connect2==true and senderID ~=0 and toConnect~=0 then
  69.   rednet.send(82,"fillerTextLol","GetNextServer")
  70.   local db,nextServer=rednet.receive("NextServer")
  71.   rednet.send(senderID,nextServer,"relay")
  72.   rednet.send(tonumber(toConnect),nextServer,"relay")
  73. end
  74. end
  75. while connect1==false and connect2==false do
  76. handshakes()
  77. end
  78. while connect1==true and connect2==true do
  79. parallel.waitForAny(messageTransfer,Nicknames,relayConnections)
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement