LBPHacker

RedStuff chat server v0.16 by LBPHacker

Sep 19th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.83 KB | None | 0 0
  1. -- One program from LBPHacker's RedStuff collection
  2. -- Chat server for RS Chat client (http://pastebin.com/CcAK5mcb)
  3. -- For ComputerCraft 1.3 or above
  4.  
  5. term.clear()
  6. term.setCursorPos(1, 1)
  7. termWidth, termHeight=term.getSize()
  8. local servername=""
  9.  
  10. function trim(stringT)
  11.     local i=1
  12.     while (string.sub(stringT, i, i)==" ") do
  13.         i=i+1
  14.     end
  15.     local startT=i
  16.     i=string.len(stringT)
  17.     while (string.sub(stringT, i, i)==" ") do
  18.         i=i-1
  19.     end
  20.     local endT=i
  21.     return string.sub(stringT, startT, endT)
  22. end
  23.  
  24. function getKickUser(message)
  25.     local i=7
  26.     while (string.sub(message, i, i)~=" ") do
  27.         i=i+1
  28.     end
  29.     return string.sub(message, 7, i-1)
  30. end
  31.  
  32. function getKickMessage(message)
  33.     if (string.len(getKickUser(message))+6==string.len(message)) then
  34.         return false
  35.     else
  36.         return string.sub(message, string.len(getKickUser(message))+8, string.len(message))
  37.     end
  38. end
  39.  
  40. print("RedStuff chat server v0.16 by LBPHacker")
  41. print("Report bugs at lbphacker@gmail.com")
  42. print("Detecting RedNet modems...")
  43. local modemOnSide=0
  44. local modems=rs.getSides()
  45. for i=1, #modems do
  46.     if (peripheral.isPresent(modems[i]) and peripheral.getType(modems[i])=="modem") then
  47.         modemOnSide=i
  48.     end
  49. end
  50. if (modemOnSide==0) then
  51.     print("No modems found. Place a RedNet modem one side of the computer, then restart this program.")
  52.     print("Press any key to exit")
  53.     os.pullEvent("key")
  54.     return 1
  55. end
  56. print("Found modem on '"..modems[modemOnSide].."', opening it")
  57. rednet.open(modems[modemOnSide])
  58. if (fs.exists("config")) then
  59.     local config=fs.open("config", "r")
  60.     servername=config.readLine()
  61.     config.close()
  62. else
  63.     print("First-time-running configuration")
  64.     term.write(string.rep("-", termWidth))
  65.     print()
  66.     local config=fs.open("config", "w")
  67.     term.write("Server name?: ")
  68.     servername=read()
  69.     print()
  70.     config.writeLine(servername)
  71.     config.close()
  72. end
  73. print()
  74. print("Server name: '"..servername.."'")
  75. print("Waiting for requests")
  76.  
  77. local userControl={{}, {}, {}, {}}
  78. local arrayUcEt=0
  79. local fromClient
  80. local shutdown=false
  81. local event, sender, message
  82. local twac=false
  83.  
  84. while (not shutdown) do
  85.     event, sender, message=os.pullEvent("rednet_message")
  86.     message=trim(message)
  87.     print()
  88.     print("Received data from "..sender..": "..message)
  89.     if (message=="/rcsping") then
  90.         rednet.send(sender, "/rcsresp "..servername)
  91.         print("Received ping, responding server name")
  92.     else
  93.         print("Searching for client in operation queue...")
  94.         fromClient=0
  95.         for i=1, arrayUcEt do
  96.             if (sender==userControl[2][i] and userControl[4][i]) then
  97.                 fromClient=i
  98.                 print("Found client: "..fromClient)
  99.             end
  100.         end
  101.         if (fromClient==0) then
  102.             if (fromClient==0) then
  103.                 print("Client not found, searching for empty slot")
  104.                 for i=1, arrayUcEt do
  105.                     if (not userControl[4][i] and fromClient==0) then
  106.                         fromClient=i
  107.                         print("Found empty slot: "..fromClient)
  108.                     end
  109.                 end
  110.             end
  111.             if (fromClient==0) then
  112.                 print("Empty slot not found, extending operation queue")
  113.                 arrayUcEt=arrayUcEt+1
  114.                 fromClient=arrayUcEt
  115.                 print("New empty slot: "..fromClient)
  116.             end
  117.             if (fromClient>0) then
  118.                 print("Recording user information")
  119.                 userControl[1][fromClient]=message
  120.                 userControl[2][fromClient]=sender
  121.                 userControl[3][fromClient]=0
  122.                 userControl[4][fromClient]=true
  123.                 for i=1, arrayUcEt do
  124.                     if (userControl[4][i]) then
  125.                         rednet.send(userControl[2][i], "-- "..message.." joined the server")
  126.                     end
  127.                 end
  128.             end
  129.         else
  130.             twac=false
  131.             if (message=="/exit") then
  132.                 for i=1, arrayUcEt do
  133.                     if (userControl[4][i]) then
  134.                         rednet.send(userControl[2][i], "-- "..userControl[1][fromClient].." left the server")
  135.                     end
  136.                 end
  137.                 userControl[4][fromClient]=false
  138.                 twac=true
  139.             end
  140.             if (message=="/stop" and userControl[1][fromClient]=="admin") then
  141.                 shutdown=true
  142.                 for i=1, arrayUcEt do
  143.                     if (userControl[4][i]) then
  144.                         rednet.send(userControl[2][i], "/kick Server stopped")
  145.                     end
  146.                 end
  147.                 twac=true
  148.             end
  149.             if (string.sub(message, 1, 5)=="/kick" and userControl[1][fromClient]=="admin") then
  150.                 shutdown=true
  151.                 if (not getKickMessage(message)) then
  152.                     for i=1, arrayUcEt do
  153.                         if (userControl[4][i] and userControl[1][i]==getKickUser(message)) then
  154.                             rednet.send(userControl[2][i], "/kick Kicked by the admin")
  155.                         end
  156.                     end
  157.                 else
  158.                     for i=1, arrayUcEt do
  159.                         if (userControl[4][i] and userControl[1][i]==string.sub(message, 7, string.len(message))) then
  160.                             rednet.send(userControl[2][i], "/kick "..getKickMessage(message))
  161.                         end
  162.                     end
  163.                 end
  164.                 twac=true
  165.             end
  166.             if (not twac) then
  167.                 for i=1, arrayUcEt do
  168.                     rednet.send(userControl[2][i], userControl[1][fromClient]..": "..message)
  169.                 end
  170.             end
  171.         end
  172.     end
  173. end
  174.  
  175. print()
  176. print("Shutting down...")
  177. sleep(2)
  178. rednet.close(modems[modemOnSide])
  179. term.clear()
  180. term.setCursorPos(1, 1)
Add Comment
Please, Sign In to add comment