Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- One program from LBPHacker's RedStuff collection
- -- Chat server for RS Chat client (http://pastebin.com/CcAK5mcb)
- -- For ComputerCraft 1.3 or above
- term.clear()
- term.setCursorPos(1, 1)
- termWidth, termHeight=term.getSize()
- local servername=""
- function trim(stringT)
- local i=1
- while (string.sub(stringT, i, i)==" ") do
- i=i+1
- end
- local startT=i
- i=string.len(stringT)
- while (string.sub(stringT, i, i)==" ") do
- i=i-1
- end
- local endT=i
- return string.sub(stringT, startT, endT)
- end
- function getKickUser(message)
- local i=7
- while (string.sub(message, i, i)~=" ") do
- i=i+1
- end
- return string.sub(message, 7, i-1)
- end
- function getKickMessage(message)
- if (string.len(getKickUser(message))+6==string.len(message)) then
- return false
- else
- return string.sub(message, string.len(getKickUser(message))+8, string.len(message))
- end
- end
- print("RedStuff chat server v0.16 by LBPHacker")
- print("Report bugs at lbphacker@gmail.com")
- print("Detecting RedNet modems...")
- local modemOnSide=0
- local modems=rs.getSides()
- for i=1, #modems do
- if (peripheral.isPresent(modems[i]) and peripheral.getType(modems[i])=="modem") then
- modemOnSide=i
- end
- end
- if (modemOnSide==0) then
- print("No modems found. Place a RedNet modem one side of the computer, then restart this program.")
- print("Press any key to exit")
- os.pullEvent("key")
- return 1
- end
- print("Found modem on '"..modems[modemOnSide].."', opening it")
- rednet.open(modems[modemOnSide])
- if (fs.exists("config")) then
- local config=fs.open("config", "r")
- servername=config.readLine()
- config.close()
- else
- print("First-time-running configuration")
- term.write(string.rep("-", termWidth))
- print()
- local config=fs.open("config", "w")
- term.write("Server name?: ")
- servername=read()
- print()
- config.writeLine(servername)
- config.close()
- end
- print()
- print("Server name: '"..servername.."'")
- print("Waiting for requests")
- local userControl={{}, {}, {}, {}}
- local arrayUcEt=0
- local fromClient
- local shutdown=false
- local event, sender, message
- local twac=false
- while (not shutdown) do
- event, sender, message=os.pullEvent("rednet_message")
- message=trim(message)
- print()
- print("Received data from "..sender..": "..message)
- if (message=="/rcsping") then
- rednet.send(sender, "/rcsresp "..servername)
- print("Received ping, responding server name")
- else
- print("Searching for client in operation queue...")
- fromClient=0
- for i=1, arrayUcEt do
- if (sender==userControl[2][i] and userControl[4][i]) then
- fromClient=i
- print("Found client: "..fromClient)
- end
- end
- if (fromClient==0) then
- if (fromClient==0) then
- print("Client not found, searching for empty slot")
- for i=1, arrayUcEt do
- if (not userControl[4][i] and fromClient==0) then
- fromClient=i
- print("Found empty slot: "..fromClient)
- end
- end
- end
- if (fromClient==0) then
- print("Empty slot not found, extending operation queue")
- arrayUcEt=arrayUcEt+1
- fromClient=arrayUcEt
- print("New empty slot: "..fromClient)
- end
- if (fromClient>0) then
- print("Recording user information")
- userControl[1][fromClient]=message
- userControl[2][fromClient]=sender
- userControl[3][fromClient]=0
- userControl[4][fromClient]=true
- for i=1, arrayUcEt do
- if (userControl[4][i]) then
- rednet.send(userControl[2][i], "-- "..message.." joined the server")
- end
- end
- end
- else
- twac=false
- if (message=="/exit") then
- for i=1, arrayUcEt do
- if (userControl[4][i]) then
- rednet.send(userControl[2][i], "-- "..userControl[1][fromClient].." left the server")
- end
- end
- userControl[4][fromClient]=false
- twac=true
- end
- if (message=="/stop" and userControl[1][fromClient]=="admin") then
- shutdown=true
- for i=1, arrayUcEt do
- if (userControl[4][i]) then
- rednet.send(userControl[2][i], "/kick Server stopped")
- end
- end
- twac=true
- end
- if (string.sub(message, 1, 5)=="/kick" and userControl[1][fromClient]=="admin") then
- shutdown=true
- if (not getKickMessage(message)) then
- for i=1, arrayUcEt do
- if (userControl[4][i] and userControl[1][i]==getKickUser(message)) then
- rednet.send(userControl[2][i], "/kick Kicked by the admin")
- end
- end
- else
- for i=1, arrayUcEt do
- if (userControl[4][i] and userControl[1][i]==string.sub(message, 7, string.len(message))) then
- rednet.send(userControl[2][i], "/kick "..getKickMessage(message))
- end
- end
- end
- twac=true
- end
- if (not twac) then
- for i=1, arrayUcEt do
- rednet.send(userControl[2][i], userControl[1][fromClient]..": "..message)
- end
- end
- end
- end
- end
- print()
- print("Shutting down...")
- sleep(2)
- rednet.close(modems[modemOnSide])
- term.clear()
- term.setCursorPos(1, 1)
Add Comment
Please, Sign In to add comment