incinirate

server

Jun 23rd, 2013 (edited)
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.04 KB | None | 0 0
  1. --[[Dex chat |server| script writted by Emma B aka anemonemma. feel free to edit as much as you want, just give me credit please. Thanks for using DexChat]]
  2.  
  3. modem="back"
  4. rednet.open(modem)
  5. computers={}
  6. users={}
  7. notonline={}
  8. deny=false
  9. currtext=""
  10. stop=false
  11. sentcommand=false
  12. banned={}
  13. ubanned={}
  14.  
  15. shell.run("clear")
  16. print("DexChat Server V4 Starting!")
  17.  
  18. function refreshBanned()
  19.   for i=1,#banned do
  20.     banned[i]=nil
  21.   end
  22.   bannedips=fs.open(shell.resolve("").."/banlist","r")
  23.   if bannedips ~= nil then
  24.     tmpbnd = nil
  25.     tmpbnd = bannedips.readLine()
  26.     if tmpbnd ~= nil then
  27.       while tmpbnd ~= nil do
  28.         table.insert(banned,tmpbnd)
  29.         tmpbnd = bannedips.readLine()
  30.       end
  31.     end
  32.     bannedips.close()
  33.   end
  34.   bannedusr=fs.open(shell.resolve("").."/ubanlist","r")
  35.   if bannedusr ~= nil then
  36.     tmpbnd = bannedusr.readLine()
  37.     if tmpbnd ~= nil then
  38.       while tmpbnd ~= nil do
  39.         table.insert(ubanned,tmpbnd)
  40.         tmpbnd = bannedusr.readLine()
  41.       end
  42.     end
  43.     bannedusr.close()
  44.   end
  45. end
  46.  
  47. refreshBanned()
  48.  
  49. function isBanned(usr,id)
  50.   for i=1,#banned do
  51.     if tostring(id)==tostring(banned[i]) then
  52.       return true
  53.     end
  54.   end
  55.   for i=1,#ubanned do
  56.     if tostring(usr)==tostring(ubanned[i]) then
  57.       return true
  58.     end
  59.   end
  60.   return false
  61. end
  62.  
  63. function isConnected(im)
  64.   for i=1,#computers do
  65.     if computers[i]==im then
  66.       return true
  67.     end
  68.   end
  69.   return false
  70. end
  71.  
  72. function backspace()
  73.   cx,cy=term.getCursorPos()
  74.   local tempchars={}
  75.   local finalchars={}
  76.   for i=1,#currtext do
  77.     a=currtext:sub(i, i)
  78.     table.insert(tempchars, a)
  79.   end
  80.   for i=1,#tempchars-1 do
  81.     table.insert(finalchars,tempchars[i])
  82.   end
  83.   currtext=""
  84.   for i=1,#finalchars do
  85.     currtext=currtext..finalchars[i]
  86.   end
  87.   term.setCursorPos(1,cy)
  88.   term.clearLine()
  89.   write(currtext)
  90. end
  91.  
  92. main=function()
  93.   sentcommand=false
  94.   id,message=rednet.receive()
  95.   term.clearLine()
  96.   cx,cy=term.getCursorPos()
  97.   term.setCursorPos(1,cy)
  98.   print("Message from "..id..". Message: "..message)
  99.   iter = string.gmatch(message, "%P+")
  100.   u = iter()
  101.   k = iter()
  102.  
  103.   citer = string.gmatch(message, "%S+")
  104.   cu = citer()
  105.   ck = citer()
  106.   ch = citer()
  107.  
  108.   if cu ~= nil then
  109.     v=1
  110.     cchrs={}
  111.     for i=1,#cu do tmpchr=string.sub(cu, i, i) table.insert(cchrs,tmpchr) end
  112.     repeat
  113.     v=v+1
  114.     if cchrs[v]=="/" then
  115.       sentcommand=true
  116.     end
  117.     until cchrs[v]=="/" or v==#cchrs
  118.     if cu == "/list" then
  119.       userlist=""
  120.       if users[1] ~= nil then
  121.         userlist=userlist..users[1]
  122.         for i=2,#users do
  123.           userlist=userlist..", "..users[2]
  124.         end
  125.         rednet.send(id,"Connected users: "..userlist)
  126.       else
  127.         rednet.send(id,"No users are connected.")
  128.       end
  129.       sentcommand=true
  130.     end
  131.  
  132.   end
  133.  
  134.   if k~=nil then print("Found as, "..u.." and "..k) end
  135.   if k=="Connecting" then
  136.     if isBanned(u,id) then
  137.       print("Sending: You are banned from this server TO "..id)rednet.send(id,"You are banned from this server")
  138.       deny=true
  139.     end
  140.     for i=1,#computers do
  141.       if id==computers[i] then
  142.         deny=true
  143.       end
  144.       if u==users[i] then
  145.         deny=true
  146.       end
  147.     end
  148.     if deny==true then sleep(0.25) print("Sending: Denied TO "..id)rednet.send(id,"Denied")
  149.     else
  150.       print(u.." is connecting")
  151.       table.insert(computers,id)
  152.       table.insert(users,u)
  153.       for i=1,#notonline do
  154.         if notonline[i]==id then
  155.           table.remove(notonline,i)
  156.         end
  157.       end
  158.       for i=1,#computers do
  159.         sleep(0.25) print("Sending: "..u.."has connected! To all computers") rednet.send(computers[i],u.." has connected!")
  160.       end
  161.     end
  162.    else if k=="disconnect" then
  163.      
  164.      
  165.     print(u)
  166.         for i=1,#computers do
  167.                 print(u)
  168.                 if u~=computers[i] then print(u.."~="..computers[i]) end
  169.         print("computer "..i..":"..computers[i])
  170.                 print("user "..i..":"..users[i])
  171.                 disser=nil
  172.         if tonumber(u)==tonumber(computers[i]) then disser=i end
  173.   if tonumber(u)==tonumber(notonline[i]) then disser=nil end
  174.     end
  175.     if disser ~= nil then
  176.                 print("Disser is not NIL")
  177.                 print(disser)
  178.         print("Disconnecting "..users[disser])
  179.         for j=1,#computers do
  180.             rednet.send(computers[j],users[disser].." has disconnected.")
  181.         end
  182.   table.insert(notonline,disser)
  183.         table.remove(computers,disser)
  184.         table.remove(users,disser)
  185.     end
  186.  
  187.    
  188.   else
  189.     if isConnected(id) then
  190.       if sentcommand == false then
  191.         for i=1,#computers do
  192.           print("Sending: "..message.." to all computers.")
  193.           rednet.send(computers[i],message)
  194.           sentcommand = false
  195.         end
  196.       end
  197.     else
  198.       if sentcommand == false then
  199.         rednet.send(id,"You are not connected.")
  200.         sentcommand = false
  201.       end
  202.       end
  203.     end
  204.   end
  205. end
  206.  
  207.  
  208. readtxt = function()
  209.   term.clearLine()
  210.   x,y = term.getCursorPos()
  211.   term.setCursorPos(1,y)
  212.   write(currtext)
  213.   while true do
  214.     e,par1 = os.pullEvent()
  215.     if e == "char" then
  216.       currtext=currtext..par1
  217.       write(par1)
  218.     end
  219.     if e == "key" then
  220.       if par1==14 then
  221.         backspace()
  222.       end
  223.       if par1==28 then
  224.         got = currtext
  225.         currtext=""
  226.         break
  227.       end
  228.     end
  229.   end
  230. end
  231.  
  232. function activateCommand(com)
  233.   args = {}
  234.   for arg in string.gmatch(com,"%S+") do
  235.   table.insert(args,arg)
  236.   end
  237.  
  238.   print("")  
  239.   if args[1] == "/kick" then
  240.     if args[2] ~= nil then
  241.       foundu=false
  242.       for i=1,#users do
  243.         if users[i] == args[2] then
  244.           foundu=true
  245.           if args[3] == nil then
  246.           rednet.send(computers[i],"You were kicked.")
  247.           for j=1,#computers do
  248.             if j~=i then
  249.               rednet.send(computers[j],users[i].." was kicked.")
  250.             end
  251.           end
  252.           print("Kicked "..args[2]..".")
  253.           else
  254.           rednet.send(computers[i],"You were kicked for "..args[3])
  255.           for j=1,#computers do
  256.             if j~=i then
  257.               rednet.send(computers[j],users[i].." was kicked for "..args[3])
  258.             end
  259.           end
  260.           print("Kicked "..args[2].." for "..args[3]..".")
  261.           end
  262.           table.insert(notonline,i)
  263.           table.remove(computers,i)
  264.           table.remove(users,i)
  265.         end
  266.       end
  267.       if foundu == false then
  268.         print("Did not find a user by the name of "..args[2])
  269.         print("Usage: /kick <username> [reason]")
  270.       end
  271.     else
  272.       print("Usage: /kick <username> [reason]")
  273.     end
  274.   else if args[1] == "/help" then
  275.     print(" ")
  276.     print("/help -View Commands")
  277.     print("/list -Lists connected users")
  278.     print("/kick <username> [reason] -Kicks a user")
  279.     print("/ban <username> [reason] -Bans a user")
  280.     print("/stop -Stops the server")
  281.     print(" ")
  282.   else if args[1]=="/list" then
  283.     userlist=""
  284.     if users[1] ~= nil then
  285.       userlist=userlist..users[1]
  286.       for i=2,#users do
  287.         userlist=userlist..", "..users[2]
  288.       end
  289.       print("Connected users: "..userlist)
  290.     else
  291.       print("No users are connected.")
  292.     end
  293.   else if args[1]=="/stop" then
  294.     stop=true
  295.     for i=1,#computers do
  296.       rednet.send(computers[i],"Server Stopped. Please press DELETE to exit.")
  297.     end
  298.   else if args[1]=="/ban" then
  299.     if args[2] ~= nil then
  300.       foundu=false
  301.       for i=1,#users do
  302.         if users[i] == args[2] then
  303.           foundu=true
  304.           banlist = fs.open(shell.resolve("").."/banlist","a")
  305.           print(banlist)
  306.           banlist.writeLine(tostring(computers[i]))
  307.           ubanlist = fs.open(shell.resolve("").."/ubanlist","a")
  308.           ubanlist.writeLine(users[i])
  309.           banlist.close()
  310.           ubanlist.close()
  311.           if args[3] == nil then
  312.           rednet.send(computers[i],"You were banned.")
  313.           for j=1,#computers do
  314.             if j~=i then
  315.               rednet.send(computers[j],users[i].." was banned.")
  316.             end
  317.           end
  318.           print("Banned "..args[2]..".")
  319.           else
  320.           rednet.send(computers[i],"You were banned for "..args[3])
  321.           for j=1,#computers do
  322.             if j~=i then
  323.               rednet.send(computers[j],users[i].." was banned for "..args[3])
  324.             end
  325.           end
  326.           print("Banned "..args[2].." for "..args[3]..".")
  327.           end
  328.           table.insert(notonline,i)
  329.           table.remove(computers,i)
  330.           table.remove(users,i)
  331.           refreshBanned()
  332.         end
  333.       end
  334.       if foundu == false then
  335.         print("Did not find a user by the name of "..args[2])
  336.         print("Usage: /ban <username> [reason]")
  337.       end
  338.     else
  339.       print("Usage: /ban <username> [reason]")
  340.     end
  341.   else
  342.     print("Command not found. Try using /help")
  343.   end
  344. end
  345. end
  346. end
  347. end
  348. end
  349.  
  350. while true do
  351.   event = parallel.waitForAny(main,readtxt)
  352.   if got ~= nil then
  353.   if tonumber(event) == 2 then
  354.   activateCommand(got)
  355.   end
  356.   end
  357.   sleep(0.1)
  358.   if stop==true then
  359.     break
  360.   end
  361. end
  362. print("DexChat Server Stopping...")
  363. print("DexChat Server Stopped.")
Add Comment
Please, Sign In to add comment