Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Coded by LoneWolf with some help ]]--
- term.redirect(peripheral.wrap("back"))
- OtherServers = {}
- function Clear()
- term.setCursorPos(1,1)
- term.clear()
- end
- function tag(tag)
- return "["..tag.."]"
- end
- function tagView(msg)
- local ReturnMsg = msg
- local ReturnTag = {}
- if string.find(ReturnMsg, "%[") and string.find(ReturnMsg, "%]") then
- while true do
- first = string.find(ReturnMsg, "%[", 1)
- last = string.find(ReturnMsg, "%]", 2)
- if first and last then ReturnTag[#ReturnTag+1] = string.sub(ReturnMsg, first+1, last-1)
- ReturnMsg = string.sub(ReturnMsg, last+1, string.len(msg))
- else
- break
- end
- end
- ReturnTag[#ReturnTag+1] = ReturnMsg
- return unpack(ReturnTag)
- else
- return false
- end
- end
- function openRednet()
- for _,side in ipairs(rs.getSides()) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- rednet.open(side)
- return side
- end
- end
- error("No modem attached")
- end
- openRednet()
- if fs.exists("ChatSettings/Settings") then
- dofile("ChatSettings/Settings")
- else
- write("Server network name. Caps specific!: ")
- ServerName = io.read()
- fs.makeDir("ChatSettings")
- local WriteSettings = io.open("/ChatSettings/Settings","w")
- WriteSettings:write("\nServerName = "..[["]]..ServerName..[["]])
- WriteSettings:close()
- shell.run("clear")
- end
- Clear()
- print("Identifying myself")
- Messages = {}
- Clients = {}
- rednet.broadcast(tag(ServerName).."Im a server!")
- TimeOut = os.startTimer(0.5)
- while true do
- event,param1,param2 = os.pullEvent()
- if event == "rednet_message" then
- TagOne, TagTwo, TagThree = tagView(param2)
- if TagTwo == "We have the same name! Lets join!" then
- OtherServers[#OtherServers+1] = param1
- rednet.send(param1, "Ok. Im libraryaddict!")
- print("Found a network with the same name! Joining them")
- end
- elseif event == "timer" and param1 == TimeOut then
- break
- end
- end
- function checkServers(para)
- for n=1,#OtherServers do
- if OtherServers[n] == para then
- return true
- end
- end
- return false
- end
- function sendServers(para)
- for n=1,#OtherServers do
- rednet.send(OtherServers[n], para)
- end
- end
- Stored = {}
- function checkMessage(para)
- for n=1,10 do
- if Stored[n] == para then
- return true
- end
- end
- end
- function sendClients(para)
- for n=1,#Clients do
- rednet.send(Clients[n], para)
- end
- end
- function checkClients(para)
- for n=1,#Clients do
- if Clients[n] == para then
- return true
- end
- end
- return false
- end
- function setMessage(para)
- for n=1,10 do
- if Stored[n] == nil then
- Stored[n] = para
- if n ~= 10 then
- Stored[n+1] = nil
- else
- Stored[1] = nil
- end
- return true
- end
- end
- end
- ClientsNA = {}
- ServersNA = {}
- ClientsName = {}
- print("Now acting as a LibChat IRC Server")
- function Ping()
- for n=1,#Clients do
- for a=1,#ClientsNA do
- if Clients[n] == ClientsNA[a] then
- sendServers(tag(os.clock()..os.getComputerID())..tag("LibChatServer").."** "..ClientsName[n].." has left the server")
- sendClients(tag("LibChat").."** "..ClientsName[n].." has left the server")
- setMessage(tag(os.clock()..os.getComputerID())..tag("LibChatServer").."** "..ClientsName[n].." has left the server")
- table.remove(Clients, n)
- table.remove(ClientsName, n)
- end
- end
- end
- for n=1,#OtherServers do
- for a=1,#ServersNA do
- if OtherServers[n] == ServersNA[a] then
- table.remove(OtherServers, n)
- end
- end
- end
- ClientsNA = {}
- ServersNA = {}
- for n=1,#Clients do
- ClientsNA[n] = Clients[n]
- end
- for n=1,#OtherServers do
- ServersNA[n] = OtherServers[n]
- end
- for n=1,#Clients do
- rednet.send(Clients[n], "Ping")
- end
- for n=1,#OtherServers do
- rednet.send(OtherServers[n], "Ping")
- end
- Pi = os.startTimer(1)
- end
- function removeName(para)
- for n=1,#ClientsNA do
- if ClientsNA[n] == para then
- table.remove(ClientsNA, n)
- end
- end
- for n=1,#ServersNA do
- if ServersNA[n] == para then
- table.remove(ServersNA, n)
- end
- end
- end
- Ping()
- function Pig()
- for n=1,#OtherServers do
- rednet.send(OtherServers[n], "Ping")
- end
- Ba = os.startTimer(0.8)
- end
- function Players()
- Namez = ""
- for n=1,#ClientsName do
- Namez = Namez..ClientsName[n]
- if ClientsName[n+1] then
- Namez = Namez..", "
- end
- end
- if #ClientsName ~= 0 then
- return "People in channel: "..Namez
- else
- return "** Server is empty"
- end
- end
- Pig()
- while true do
- event,param1,param2 = os.pullEvent()
- if param2 == nil then param2 = "" end
- if event == "rednet_message" then
- TagOne, TagTwo, TagThree, TagFour = tagView(param2)
- if TagTwo == "Im a server!" then
- if TagOne == ServerName then
- rednet.send(param1, tag(ServerName).."We have the same name! Lets join!")
- end
- elseif param2 == "Ok. Im libraryaddict!" then
- OtherServers[#OtherServers+1] = param1
- print(param1.." joined the network!")
- for n=1,#ClientsName do
- rednet.send(param1, tag(os.time()..os.getComputerID())..tag("AddPlayer")..tag(n)..ClientsName[n])
- end
- elseif param2 == "Pinger!" then
- rednet.send(param1, tag("LibChat").."Pong!")
- elseif param2 == "Players?" then
- rednet.send(param1, tag("LibChat")..Players())
- elseif TagTwo == "NickChange" then
- for n=1,#ClientsName do
- if Clients[n] == param1 and ClientsName[n] == TagThree and not checkMessage(param2) then
- ClientsName[n] = TagFour
- setMessage(param2)
- sendServers(param2)
- sendClients(tag("LibChat").."** "..TagThree.." is now known as "..TagFour)
- end
- end
- elseif param2 == "Zero Zero Two Nine'r looking for network. Over" then
- rednet.send(param1, tag(ServerName).."We receive and we confirm. Over")
- elseif TagOne == "You are the chosen one" then
- if not checkClients(param1) then
- rednet.send(param1, tag("LibChat")..Players(param1))
- Clients[#Clients+1] = param1
- ClientsName[#ClientsName+1] = TagTwo
- setMessage(tag(os.time()..os.getComputerID())..tag("AddPlayer")..tag(#ClientsName)..TagTwo)
- sendServers(tag(os.time()..os.getComputerID())..tag("AddPlayer")..tag(#ClientsName)..TagTwo)
- print("ComputerID:"..param1.." is now one of my clients")
- setMessage(tag(os.clock()..os.getComputerID())..tag("LibChatServer").."** "..TagTwo.." has joined the server")
- sendServers(tag(os.clock()..os.getComputerID())..tag("LibChatServer").."** "..TagTwo.." has joined the server")
- sendClients(tag("LibChat").."** "..TagTwo.." has joined the server")
- print(TagTwo.." has joined the server")
- end
- elseif param2 == "Pong" then
- removeName(param1)
- elseif param2 == "Ping" then
- rednet.send(param1, "Pong")
- elseif checkServers(param1) and TagTwo == "AddPlayer" then
- if not checkMessage(param2) then
- ClientsName[tonumber(TagThree)] = TagFour
- setMessage(tag(os.time()..os.getComputerID())..tag("AddPlayer")..tag(#ClientsName)..TagThree)
- sendServers(tag(os.time()..os.getComputerID())..tag("AddPlayer")..tag(#ClientsName)..TagThree)
- end
- elseif checkClients(param1) and not checkMessage(param2) then
- local thing = string.gsub(TagThree, "!LeftBracket!", "%]")
- thing = string.gsub(thing, "!RightBracket!", "%[")
- print(thing)
- setMessage(param2)
- sendServers(param2)
- sendClients(tag("LibChat")..TagThree)
- elseif checkServers(param1) and not checkMessage(param2) then
- print(TagThree)
- setMessage(param2)
- sendServers(param2)
- sendClients(tag("LibChat")..TagThree)
- end
- elseif event == "timer" and param1 == Pi then
- Ping()
- elseif event == "timer" and param1 == Ba then
- Pig()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement