Advertisement
CryptekCoding

IRC|Server

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