Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
16 kB
2
Indexable
Never
package ballistickemu.Lobby.handlers;

import ballistickemu.Lobby.LobbyServer;
import ballistickemu.Main;
import ballistickemu.Tools.DatabaseTools;
import ballistickemu.Tools.PasswordHasher;
import ballistickemu.Tools.StickPacketMaker;
import ballistickemu.Types.StickClient;
import ballistickemu.Types.StickClientRegistry;
import ballistickemu.Types.StickColour;
import ballistickemu.Types.StickItem;
import ballistickemu.Types.StickPacket;
import ballistickemu.Types.StickRoom;
import ballistickemu.Types.StickRoomRegistry;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.apache.mina.core.session.IoSession;


public class ModCommandHandler
{
  public static void ProcessModCommand(StickClient paramStickClient, String paramString)
  {
    if (paramStickClient.getName() == "michal") {
      return;
    }
    String[] arrayOfString1 = parseArgs(paramString);
    
    if (arrayOfString1[0].equalsIgnoreCase("::create"))
    {
      if (arrayOfString1.length == 3)
      {
        createUser(arrayOfString1[1], arrayOfString1[2], paramStickClient);
        paramStickClient.writeCallbackMessage("Successfully made " + arrayOfString1[1]);
      }
      else
      {
        paramStickClient.writeCallbackMessage("Usage: ::create [username] [password]");
      }
      return;
    }
    
    if (paramStickClient.getUserLevel() < 1) {
      return;
    }
    String[] arrayOfString2 = parseArgs(paramString);
    
    if (arrayOfString2[0].equalsIgnoreCase("::ban"))
    {
      if (arrayOfString2.length == 2)
      {
        if (banPlayer(arrayOfString2[1], paramStickClient) > 0)
        {
          Main.getLobbyServer().BroadcastAnnouncement2("User '" + arrayOfString2[1] + "' has just been banned by " + paramStickClient.getName() + " and will disconnect in 15 seconds.".replaceAll("\000", ""));
          try
          {
            TimeUnit.SECONDS.sleep(15L);
          }
          catch (InterruptedException localInterruptedException)
          {
            Thread.currentThread().interrupt();
          }
          disconnectPlayer(arrayOfString2[1]);
        }
        
      }
      else {
        paramStickClient.writeCallbackMessage("Usage: ::ban [username] [reason]");
      }
      return;
    }
    
    if (arrayOfString2[0].equalsIgnoreCase("::unban"))
    {
      if (arrayOfString2.length == 2)
      {
        if (unbanPlayer(arrayOfString2[1], paramStickClient) <= 0) {}


      }
      else
      {

        paramStickClient.writeCallbackMessage("Usage: ::unban [username]");
      }
      return;
    }
    Object localObject1;
    if (arrayOfString2[0].equalsIgnoreCase("::mute"))
    {
      if (arrayOfString2.length == 2)
      {
        localObject1 = Main.getLobbyServer().getClientRegistry().getClientfromName(arrayOfString2[1]);
        if (localObject1 != null)
        {
          ((StickClient)localObject1).setMuteStatus(Boolean.valueOf(true));
          paramStickClient.writeCallbackMessage("User " + arrayOfString2[1] + " successfully muted.");
        }
        else
        {
          paramStickClient.writeCallbackMessage("User " + arrayOfString2[1] + " was not found.");
        }
      }
      else
      {
        paramStickClient.writeCallbackMessage("Usage: ::mute [username]");
      }
      return;
    }
    
    if (arrayOfString2[0].equalsIgnoreCase("::unmute"))
    {
      if (arrayOfString2.length == 2)
      {
        localObject1 = Main.getLobbyServer().getClientRegistry().getClientfromName(arrayOfString2[1]);
        if (localObject1 != null)
        {
          ((StickClient)localObject1).setMuteStatus(Boolean.valueOf(false));
          paramStickClient.writeCallbackMessage("User " + arrayOfString2[1] + " successfully unmuted.");
        }
        else
        {
          paramStickClient.writeCallbackMessage("User " + arrayOfString2[1] + " was not found.");
        }
      }
      else
      {
        paramStickClient.writeCallbackMessage("Usage: ::unmute [username]");
      }
      return;
    }
    
    if (arrayOfString2[0].equalsIgnoreCase("::deleteroom"))
    {
      if (arrayOfString2.length >= 2)
      {
        localObject1 = Main.getLobbyServer().getRoomRegistry().GetRoomFromName(paramString.substring(13).replaceAll("\000", ""));
        if (localObject1 != null)
        {
          ((StickRoom)localObject1).killRoom();
        }
        else
        {
          paramStickClient.writeCallbackMessage("Room " + paramString.replaceAll("\000", "").substring(13) + " was not found.");
        }
      }
      return;
    }
    
    if (arrayOfString2[0].equalsIgnoreCase("::disconnect"))
    {
      if (arrayOfString2.length == 2)
      {





        if (!disconnectPlayer(arrayOfString2[1]).booleanValue())
        {
          paramStickClient.writeCallbackMessage("User " + arrayOfString2[1] + " was not found.");
        }
        
      }
      else {
        paramStickClient.writeCallbackMessage("Usage: ::disconnect [username]");
      }
      return;
    }
    Object localObject2;
    if (arrayOfString2[0].equalsIgnoreCase("::ip"))
    {
      if (arrayOfString2.length == 2)
      {
        localObject1 = Main.getLobbyServer().getClientRegistry().getClientfromName(arrayOfString2[1]);
        if (localObject1 == null)
        {
          paramStickClient.writeCallbackMessage("User is not online!");
          return;
        }
        localObject2 = localObject1.getIoSession().getRemoteAddress().toString().substring(1).split(":")[0];
        paramStickClient.writeCallbackMessage(arrayOfString2[1] + "'s IP is " + (String)localObject2);
      }
      else
      {
        paramStickClient.writeCallbackMessage("Usage: ::ip [username]");
      }
    }
    else {
      if (arrayOfString2[0].equalsIgnoreCase("::ipban"))
      {
        localObject1 = Main.getLobbyServer().getClientRegistry().getClientfromName(arrayOfString2[1]);
        if (localObject1 == null)
        {
          paramStickClient.writeCallbackMessage("Error IP Banning: User " + arrayOfString2[1] + " does not exist or is not online.");
          return;
        }
        localObject2 = localObject1.getIoSession().getRemoteAddress().toString().substring(1).split(":")[0];
        System.out.println("IP address for user " + arrayOfString2[1] + "is: " + (String)localObject2 + ".");
        synchronized (DatabaseTools.lock)
        {
          try
          {
            PreparedStatement localPreparedStatement = DatabaseTools.getDbConnection().prepareStatement("INSERT INTO `ipbans` (`ip`, `playername`, `mod_responsible`) VALUES (?, ?, ?)");
            localPreparedStatement.setString(1, (String)localObject2);
            localPreparedStatement.setString(2, arrayOfString2[1]);
            localPreparedStatement.setString(3, paramStickClient.getName());
            localPreparedStatement.executeUpdate();
            ((StickClient)localObject1).getIoSession().close(false);
          }
          catch (SQLException localSQLException) {
            System.out.println("Exception whilst inserting IP ban: " + localSQLException.toString());
          }
        }
        return;
      }
      
      if (arrayOfString2[0].equalsIgnoreCase("::announce"))
      {
        if (paramString.length() > 10)
          Main.getLobbyServer().BroadcastAnnouncement(paramString.substring(11).replaceAll("\000", ""));
        return;
      }
      
      if (arrayOfString2[0].equalsIgnoreCase("::announce2"))
      {
        if (paramString.length() > 10)
          Main.getLobbyServer().BroadcastAnnouncement2(paramString.substring(11).replaceAll("\000", ""));
        return;
      }
      
      if (arrayOfString2[0].equalsIgnoreCase("::getAllPlayers"))
      {
        localObject1 = new StringBuilder();
        ((StringBuilder)localObject1).append("User list:");
        for (localObject2 = Main.getLobbyServer().getClientRegistry().getAllClients().iterator(); ((Iterator)localObject2).hasNext();) { ??? = (StickClient)((Iterator)localObject2).next();
          
          if (((StickClient)???).getName() != null)
            ((StringBuilder)localObject1).append(" " + ((StickClient)???).getName() + ",");
        }
        paramStickClient.writeCallbackMessage(((StringBuilder)localObject1).toString());
        return;
      }
      if (arrayOfString2[0].equalsIgnoreCase("::help"))
      {
        paramStickClient.writeCallbackMessage("All mod commands are: ::disconnect, ::ban, ::unban, ::ip, ::announce, ::announce2, !modspinner, ::deleteroom, ::killserver, ::blend, ::mute, ::unmute, ::setgametime, ::resetgametime, ::ipban, ::getallplayers, /warn");

      }
      else if ((arrayOfString2[0].equalsIgnoreCase("::resetgametime")) && (paramStickClient.getRoom() != null))
      {
        localObject1 = paramStickClient.getRoom();
        ((StickRoom)localObject1).setRoundTime(300);
        localObject2 = StickPacketMaker.getSendRoundDetail(((StickRoom)localObject1).getMapID(), ((StickRoom)localObject1).getCycleMode(), ((StickRoom)localObject1).GetCR().getAllClients().size(), ((StickRoom)localObject1).getCurrentRoundTime());
        ((StickRoom)localObject1).BroadcastToRoom((StickPacket)localObject2);
        paramStickClient.writeCallbackMessage("Round time successfully reset.");
      }
      else {
        if ((arrayOfString2[0].equalsIgnoreCase("::setgametime")) && (paramStickClient.getRoom() != null))
        {
          int i = -1;
          if (arrayOfString2.length < 2)
          {
            paramStickClient.writeCallbackMessage("Syntax - ::setgametime [time in seconds]");
            return;
          }
          try
          {
            i = Integer.valueOf(arrayOfString2[1]).intValue();
          }
          catch (NumberFormatException localNumberFormatException) {
            paramStickClient.writeCallbackMessage("Please ensure you entered a correct numerical value.");
            return;
          }
          if (i > -1)
          {
            StickRoom localStickRoom = paramStickClient.getRoom();
            localStickRoom.setRoundTime(i);
            ??? = StickPacketMaker.getSendRoundDetail(localStickRoom.getMapID(), localStickRoom.getCycleMode(), localStickRoom.GetCR().getAllClients().size(), localStickRoom.getCurrentRoundTime());
            localStickRoom.BroadcastToRoom((StickPacket)???);
            paramStickClient.writeCallbackMessage("Round time successfully set as " + i + " seconds remaining.");
            return;
          }
          paramStickClient.writeCallbackMessage("There was an error in resetting the round time.");
          return;
        }
        

        if (arrayOfString2[0].equalsIgnoreCase("::killserver"))
        {
          System.out.printf("Server terminated at %s by moderator %s", new Object[] { Calendar.getInstance().getTime().toString(), paramStickClient.getName() });
          System.exit(0);

        }
        else if (arrayOfString2[0].equalsIgnoreCase("::blend"))
        {
          if (paramStickClient.getLobbyStatus().booleanValue())
          {
            paramStickClient.setBlendedStatus(Boolean.valueOf(true));
            Main.getLobbyServer().BroadcastPacket(StickPacketMaker.getClientInfo(paramStickClient.getUID(), paramStickClient.getName(), paramStickClient.getSelectedSpinner().getColour().getColour1AsString(), paramStickClient.getKills(), paramStickClient
              .getDeaths(), paramStickClient.getWins(), paramStickClient.getLosses(), paramStickClient.getRounds(), paramStickClient.getPass().booleanValue() ? 1 : 0, 0));
            paramStickClient.writeCallbackMessage("You will be now shown as normal player (relog to undo this)");
          }
        }
        else if (arrayOfString2[0].equalsIgnoreCase("::unblend"))
        {
          if (paramStickClient.getLobbyStatus().booleanValue())
          {
            paramStickClient.setBlendedStatus(Boolean.valueOf(false));
            Main.getLobbyServer().BroadcastPacket(StickPacketMaker.getClientInfo(paramStickClient.getUID(), paramStickClient.getName(), paramStickClient.getSelectedSpinner().getColour().getColour1AsString(), paramStickClient.getKills(), paramStickClient
              .getDeaths(), paramStickClient.getWins(), paramStickClient.getLosses(), paramStickClient.getRounds(), paramStickClient.getPass().booleanValue() ? 1 : 0, 1));
            paramStickClient.writeCallbackMessage("Unblended!");
          }
        }
      }
    }
  }
  

  private static String[] parseArgs(String paramString)
  {
    return paramString.split(" ");
  }
  
  public static int banPlayer(String paramString, StickClient paramStickClient)
  {
    PreparedStatement localPreparedStatement = null;
    int i = -1;
    synchronized (DatabaseTools.lock)
    {
      try
      {
        localPreparedStatement = DatabaseTools.getDbConnection().prepareStatement("UPDATE `users` set `ban` = '1' where `username` = ?");
        localPreparedStatement.setString(1, paramString);
        i = localPreparedStatement.executeUpdate();
      }
      catch (SQLException localSQLException) {
        System.out.println("Exception during ban command: " + localSQLException.toString());
      }
      
      if (i == -1)
      {
        paramStickClient.writeCallbackMessage("There was an error banning " + paramString + ".");
      }
      else if (i == 0)
      {
        paramStickClient.writeCallbackMessage("User " + paramString + " does not exist.");
      }
      else
      {
        paramStickClient.writeCallbackMessage("User " + paramString + " was banned successfully.");
      }
    }
    return i;
  }
  
  public static int unbanPlayer(String paramString, StickClient paramStickClient)
  {
    PreparedStatement localPreparedStatement = null;
    int i = -1;
    synchronized (DatabaseTools.lock)
    {
      try
      {
        localPreparedStatement = DatabaseTools.getDbConnection().prepareStatement("UPDATE `users` set `ban` = '0' where `username` = ?");
        localPreparedStatement.setString(1, paramString);
        i = localPreparedStatement.executeUpdate();
      }
      catch (SQLException localSQLException) {
        System.out.println("Exception during unban command: " + localSQLException.toString());
      }
      
      if (i == -1)
      {
        paramStickClient.writeCallbackMessage("There was an error unbanning " + paramString + ".");
      }
      else if (i == 0)
      {
        paramStickClient.writeCallbackMessage("User " + paramString + " does not exist.");
      }
      else
      {
        paramStickClient.writeCallbackMessage("User " + paramString + " was unbanned successfully.");
      }
    }
    return i;
  }
  
  public static int createUser(String paramString1, String paramString2, StickClient paramStickClient) {
    PreparedStatement localPreparedStatement = null;
    int i = -1;
    try
    {
      String str = "";
      str = PasswordHasher.generateHashedPassword(paramString2);
      localPreparedStatement = DatabaseTools.getDbConnection().prepareStatement("INSERT INTO `users` (USERname,USERpass) VALUES (?,?)");
      localPreparedStatement.setString(1, paramString1);
      localPreparedStatement.setString(2, str);
      i = localPreparedStatement.executeUpdate();
    }
    catch (Exception localException) {
      System.out.println("Exception during create command: " + localException.toString());
    }
    return i;
  }
  
  private static Boolean disconnectPlayer(String paramString)
  {
    StickClient localStickClient = null;
    int i = 0;
    do
    {
      localStickClient = Main.getLobbyServer().getClientRegistry().getClientfromName(paramString);
      if (localStickClient != null)
      {
        localStickClient.getIoSession().close(false);
      }
      i++;
    } while (localStickClient != null);
    return Boolean.valueOf(i > 0);
  }
}