TheCryptek

[LUA] IRC|Server

Aug 12th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.90 KB | None | 0 0
  1. --[[ term.redirect(peripheral.wrap("back")) ]]--
  2. OtherServers = {}
  3. function Clear()
  4.   term.setCursorPos(1,1)
  5.   term.clear()
  6. end
  7. function tag(tag)
  8.   return "["..tag.."]"
  9. end
  10. function tagView(msg)
  11.   local ReturnMsg = msg
  12.   local ReturnTag = {}
  13.   if string.find(ReturnMsg, "%[") and string.find(ReturnMsg, "%]") then
  14.     while true do
  15.       first = string.find(ReturnMsg, "%[", 1)
  16.       last = string.find(ReturnMsg, "%]", 2)
  17.       if first and last then ReturnTag[#ReturnTag+1] = string.sub(ReturnMsg, first+1, last-1)
  18.         ReturnMsg = string.sub(ReturnMsg, last+1, string.len(msg))
  19.       else
  20.         break
  21.       end
  22.     end
  23.     ReturnTag[#ReturnTag+1] = ReturnMsg
  24.     return unpack(ReturnTag)
  25.   else
  26.     return false
  27.   end
  28. end
  29.  
  30. function openRednet()
  31.   for _,side in ipairs(rs.getSides()) do
  32.     if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  33.       rednet.open(side)
  34.       return side
  35.     end
  36.   end
  37.   error("No modem attached")
  38. end
  39. openRednet()
  40. if fs.exists("ChatSettings/Settings") then
  41.   dofile("ChatSettings/Settings")
  42. else
  43.   write("Server network name. Caps specific!: ")
  44.   ServerName = io.read()
  45.   fs.makeDir("ChatSettings")
  46.   local WriteSettings = io.open("/ChatSettings/Settings","w")
  47.   WriteSettings:write("\nServerName = "..[["]]..ServerName..[["]])
  48.   WriteSettings:close()
  49.   shell.run("clear")
  50. end
  51. Clear()
  52. print("Identifying myself")
  53. Messages = {}
  54. Clients = {}
  55. rednet.broadcast(tag(ServerName).."Im a server!")
  56. TimeOut = os.startTimer(0.5)
  57. while true do
  58.   event,param1,param2 = os.pullEvent()
  59.   if event == "rednet_message" then
  60.     TagOne, TagTwo, TagThree = tagView(param2)
  61.     if TagTwo == "We have the same name! Lets join!" then
  62.       OtherServers[#OtherServers+1] = param1
  63.       rednet.send(param1, "Ok. Im libraryaddict!")
  64.       print("Found a network with the same name! Joining them")
  65.     end
  66.   elseif event == "timer" and param1 == TimeOut then
  67.     break
  68.   end
  69. end
  70.    
  71. function checkServers(para)
  72.   for n=1,#OtherServers do
  73.     if OtherServers[n] == para then
  74.       return true
  75.     end
  76.   end
  77.   return false
  78. end
  79.  
  80. function sendServers(para)
  81.   for n=1,#OtherServers do
  82.     rednet.send(OtherServers[n], para)
  83.   end
  84. end
  85. Stored = {}
  86. function checkMessage(para)
  87.   for n=1,10 do
  88.     if Stored[n] == para then
  89.       return true
  90.     end
  91.   end
  92. end
  93.  
  94. function sendClients(para)
  95.   for n=1,#Clients do
  96.     rednet.send(Clients[n], para)
  97.   end
  98. end
  99.  
  100. function checkClients(para)
  101.   for n=1,#Clients do
  102.     if Clients[n] == para then
  103.       return true
  104.     end
  105.   end
  106.   return false
  107. end
  108.  
  109. function setMessage(para)
  110.   for n=1,10 do
  111.     if Stored[n] == nil then
  112.       Stored[n] = para
  113.       if n ~= 10 then
  114.         Stored[n+1] = nil
  115.       else
  116.         Stored[1] = nil
  117.       end
  118.       return true
  119.     end
  120.   end
  121. end
  122. ClientsNA = {}
  123. ServersNA = {}
  124. ClientsName = {}
  125. print("Now acting as a LibChat IRC Server")
  126. function Ping()
  127.   for n=1,#Clients do
  128.     for a=1,#ClientsNA do
  129.       if Clients[n] == ClientsNA[a] then
  130.         sendServers(tag(os.clock()..os.getComputerID())..tag("LibChatServer").."** "..ClientsName[n].." has left the server")
  131.         sendClients(tag("LibChat").."** "..ClientsName[n].." has left the server")
  132.         setMessage(tag(os.clock()..os.getComputerID())..tag("LibChatServer").."** "..ClientsName[n].." has left the server")
  133.         table.remove(Clients, n)
  134.         table.remove(ClientsName, n)
  135.       end
  136.     end
  137.   end
  138.   for n=1,#OtherServers do
  139.     for a=1,#ServersNA do
  140.       if OtherServers[n] == ServersNA[a] then
  141.         table.remove(OtherServers, n)
  142.       end
  143.     end
  144.   end
  145.   ClientsNA = {}
  146.   ServersNA = {}
  147.   for n=1,#Clients do
  148.     ClientsNA[n] = Clients[n]
  149.   end
  150.   for n=1,#OtherServers do
  151.     ServersNA[n] = OtherServers[n]
  152.   end
  153.   for n=1,#Clients do
  154.     rednet.send(Clients[n], "Ping")
  155.   end
  156.   for n=1,#OtherServers do
  157.     rednet.send(OtherServers[n], "Ping")
  158.   end
  159.   Pi = os.startTimer(1)
  160. end
  161.  
  162. function removeName(para)
  163.   for n=1,#ClientsNA do
  164.     if ClientsNA[n] == para then
  165.       table.remove(ClientsNA, n)
  166.     end
  167.   end
  168.   for n=1,#ServersNA do
  169.     if ServersNA[n] == para then
  170.       table.remove(ServersNA, n)
  171.     end
  172.   end
  173. end
  174. Ping()
  175.  
  176. function Pig()
  177.   for n=1,#OtherServers do
  178.     rednet.send(OtherServers[n], "Ping")
  179.   end
  180.   Ba = os.startTimer(0.8)
  181. end
  182.  
  183. function Players()
  184.   Namez = ""
  185.   for n=1,#ClientsName do
  186.     Namez = Namez..ClientsName[n]
  187.     if ClientsName[n+1] then
  188.       Namez = Namez..", "
  189.     end
  190.   end
  191.   if #ClientsName ~= 0 then
  192.     return "People in channel: "..Namez
  193.   else
  194.     return "** Server is empty"
  195.   end
  196. end
  197. Pig()
  198. while true do
  199.   event,param1,param2 = os.pullEvent()
  200.   if param2 == nil then param2 = "" end
  201.   if event == "rednet_message" then
  202.     TagOne, TagTwo, TagThree, TagFour = tagView(param2)
  203.     if TagTwo == "Im a server!" then
  204.       if TagOne == ServerName then
  205.         rednet.send(param1, tag(ServerName).."We have the same name! Lets join!")
  206.       end
  207.     elseif param2 == "Ok. Im libraryaddict!" then
  208.       OtherServers[#OtherServers+1] = param1
  209.       print(param1.." joined the network!")
  210.       for n=1,#ClientsName do
  211.         rednet.send(param1, tag(os.time()..os.getComputerID())..tag("AddPlayer")..tag(n)..ClientsName[n])
  212.       end
  213.     elseif param2 == "Pinger!" then
  214.       rednet.send(param1, tag("LibChat").."Pong!")
  215.     elseif param2 == "Players?" then
  216.       rednet.send(param1, tag("LibChat")..Players())
  217.     elseif TagTwo == "NickChange" then
  218.       for n=1,#ClientsName do
  219.         if Clients[n] == param1 and ClientsName[n] == TagThree and not checkMessage(param2) then
  220.           ClientsName[n] = TagFour
  221.           setMessage(param2)
  222.           sendServers(param2)
  223.           sendClients(tag("LibChat").."** "..TagThree.." is now known as "..TagFour)
  224.         end
  225.       end
  226.     elseif param2 == "Zero Zero Two Nine'r looking for network. Over" then
  227.       rednet.send(param1, tag(ServerName).."We receive and we confirm. Over")
  228.     elseif TagOne == "You are the chosen one" then
  229.       if not checkClients(param1) then
  230.         rednet.send(param1, tag("LibChat")..Players(param1))
  231.         Clients[#Clients+1] = param1
  232.         ClientsName[#ClientsName+1] = TagTwo
  233.         setMessage(tag(os.time()..os.getComputerID())..tag("AddPlayer")..tag(#ClientsName)..TagTwo)
  234.         sendServers(tag(os.time()..os.getComputerID())..tag("AddPlayer")..tag(#ClientsName)..TagTwo)
  235.         print("ComputerID:"..param1.." is now one of my clients")
  236.         setMessage(tag(os.clock()..os.getComputerID())..tag("LibChatServer").."** "..TagTwo.." has joined the server")
  237.         sendServers(tag(os.clock()..os.getComputerID())..tag("LibChatServer").."** "..TagTwo.." has joined the server")
  238.         sendClients(tag("LibChat").."** "..TagTwo.." has joined the server")
  239.         print(TagTwo.." has joined the server")
  240.       end
  241.     elseif param2 == "Pong" then
  242.       removeName(param1)
  243.     elseif param2 == "Ping" then
  244.       rednet.send(param1, "Pong")
  245.     elseif checkServers(param1) and TagTwo == "AddPlayer" then
  246.       if not checkMessage(param2) then
  247.       ClientsName[tonumber(TagThree)] = TagFour
  248.       setMessage(tag(os.time()..os.getComputerID())..tag("AddPlayer")..tag(#ClientsName)..TagThree)
  249.       sendServers(tag(os.time()..os.getComputerID())..tag("AddPlayer")..tag(#ClientsName)..TagThree)
  250.       end
  251.     elseif checkClients(param1) and not checkMessage(param2) then
  252.       local thing = string.gsub(TagThree, "!LeftBracket!", "%]")
  253.       thing = string.gsub(thing, "!RightBracket!", "%[")
  254.       print(thing)
  255.       setMessage(param2)
  256.       sendServers(param2)
  257.       sendClients(tag("LibChat")..TagThree)
  258.     elseif checkServers(param1) and not checkMessage(param2) then
  259.       print(TagThree)
  260.       setMessage(param2)
  261.       sendServers(param2)
  262.       sendClients(tag("LibChat")..TagThree)
  263.     end
  264.   elseif event == "timer" and param1 == Pi then
  265.     Ping()
  266.   elseif event == "timer" and param1 == Ba then
  267.     Pig()
  268.   end
  269. end
Advertisement
Add Comment
Please, Sign In to add comment