2012-12-09 03:42:29 +00:00
package me.StevenLawson.TotalFreedomMod ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.Iterator ;
2013-08-24 19:35:09 +00:00
import java.util.List ;
2012-12-09 03:42:29 +00:00
import java.util.Set ;
import java.util.regex.Pattern ;
2013-07-09 00:37:02 +00:00
import net.minecraft.server.v1_6_R2.BanEntry ;
import net.minecraft.server.v1_6_R2.BanList ;
import net.minecraft.server.v1_6_R2.MinecraftServer ;
import net.minecraft.server.v1_6_R2.PlayerList ;
import net.minecraft.server.v1_6_R2.PropertyManager ;
2012-12-09 03:42:29 +00:00
import org.bukkit.ChatColor ;
import org.bukkit.Server ;
import org.bukkit.entity.Player ;
import org.bukkit.event.player.PlayerLoginEvent ;
public class TFM_ServerInterface
{
private static final SimpleDateFormat date_format = new SimpleDateFormat ( " yyyy-MM-dd \ 'at \ ' HH:mm:ss z " ) ;
public static void setOnlineMode ( boolean mode )
{
PropertyManager propertyManager = MinecraftServer . getServer ( ) . getPropertyManager ( ) ;
propertyManager . a ( " online-mode " , mode ) ;
propertyManager . savePropertiesFile ( ) ;
}
public static void wipeNameBans ( )
{
2012-12-21 02:33:22 +00:00
BanList nameBans = MinecraftServer . getServer ( ) . getPlayerList ( ) . getNameBans ( ) ;
2012-12-09 03:42:29 +00:00
nameBans . getEntries ( ) . clear ( ) ;
nameBans . save ( ) ;
}
public static void wipeIpBans ( )
{
2012-12-21 02:33:22 +00:00
BanList IPBans = MinecraftServer . getServer ( ) . getPlayerList ( ) . getIPBans ( ) ;
2012-12-09 03:42:29 +00:00
IPBans . getEntries ( ) . clear ( ) ;
IPBans . save ( ) ;
}
public static void unbanUsername ( String name )
{
name = name . toLowerCase ( ) . trim ( ) ;
2012-12-21 02:33:22 +00:00
BanList nameBans = MinecraftServer . getServer ( ) . getPlayerList ( ) . getNameBans ( ) ;
2012-12-09 03:42:29 +00:00
nameBans . remove ( name ) ;
}
public static void banUsername ( String name , String reason , String source , Date expire_date )
{
name = name . toLowerCase ( ) . trim ( ) ;
2013-08-24 19:35:09 +00:00
if ( TFM_SuperadminList . getSuperadminNames ( ) . contains ( name ) )
{
TFM_Log . info ( " Not banning username " + name + " : is superadmin " ) ;
return ;
}
2013-08-25 15:32:24 +00:00
for ( String username : ( List < String > ) TFM_ConfigEntry . UNBANNABLE_USERNAMES . getList ( ) )
2013-08-24 19:35:09 +00:00
{
if ( username . toLowerCase ( ) . trim ( ) . equals ( name ) )
{
TFM_Log . info ( " Not banning username " + name + " : is unbannable as defined in config. " ) ;
return ;
}
}
2012-12-09 03:42:29 +00:00
BanEntry ban_entry = new BanEntry ( name ) ;
if ( expire_date ! = null )
{
ban_entry . setExpires ( expire_date ) ;
}
if ( reason ! = null )
{
ban_entry . setReason ( reason ) ;
}
if ( source ! = null )
{
ban_entry . setSource ( source ) ;
}
2012-12-21 02:33:22 +00:00
BanList nameBans = MinecraftServer . getServer ( ) . getPlayerList ( ) . getNameBans ( ) ;
2012-12-09 03:42:29 +00:00
nameBans . add ( ban_entry ) ;
}
public static boolean isNameBanned ( String name )
{
name = name . toLowerCase ( ) . trim ( ) ;
2012-12-21 02:33:22 +00:00
BanList nameBans = MinecraftServer . getServer ( ) . getPlayerList ( ) . getNameBans ( ) ;
2012-12-09 03:42:29 +00:00
nameBans . removeExpired ( ) ;
return nameBans . getEntries ( ) . containsKey ( name ) ;
}
public static void banIP ( String ip , String reason , String source , Date expire_date )
{
ip = ip . toLowerCase ( ) . trim ( ) ;
BanEntry ban_entry = new BanEntry ( ip ) ;
if ( expire_date ! = null )
{
ban_entry . setExpires ( expire_date ) ;
}
if ( reason ! = null )
{
ban_entry . setReason ( reason ) ;
}
if ( source ! = null )
{
ban_entry . setSource ( source ) ;
}
2012-12-21 02:33:22 +00:00
BanList ipBans = MinecraftServer . getServer ( ) . getPlayerList ( ) . getIPBans ( ) ;
2012-12-09 03:42:29 +00:00
ipBans . add ( ban_entry ) ;
}
public static void unbanIP ( String ip )
{
ip = ip . toLowerCase ( ) . trim ( ) ;
2012-12-21 02:33:22 +00:00
BanList ipBans = MinecraftServer . getServer ( ) . getPlayerList ( ) . getIPBans ( ) ;
2012-12-09 03:42:29 +00:00
ipBans . remove ( ip ) ;
}
public static boolean isIPBanned ( String ip )
{
ip = ip . toLowerCase ( ) . trim ( ) ;
2012-12-21 02:33:22 +00:00
BanList ipBans = MinecraftServer . getServer ( ) . getPlayerList ( ) . getIPBans ( ) ;
2012-12-09 03:42:29 +00:00
ipBans . removeExpired ( ) ;
return ipBans . getEntries ( ) . containsKey ( ip ) ;
}
public static int purgeWhitelist ( )
{
2012-12-21 02:33:22 +00:00
Set whitelisted = MinecraftServer . getServer ( ) . getPlayerList ( ) . getWhitelisted ( ) ;
2012-12-09 03:42:29 +00:00
int size = whitelisted . size ( ) ;
whitelisted . clear ( ) ;
return size ;
}
public static void handlePlayerLogin ( PlayerLoginEvent event )
{
2013-07-11 22:21:23 +00:00
// this should supersede all other onPlayerLogin authentication on the TFM server.
// when using the TFM CraftBukkit, CraftBukkit itself should not do any of its own authentication.
2012-12-09 03:42:29 +00:00
final Server server = TotalFreedomMod . plugin . getServer ( ) ;
2012-12-21 02:33:22 +00:00
final PlayerList player_list = MinecraftServer . getServer ( ) . getPlayerList ( ) ;
final BanList banByIP = player_list . getIPBans ( ) ;
final BanList banByName = player_list . getNameBans ( ) ;
2012-12-09 03:42:29 +00:00
2013-08-14 14:01:42 +00:00
final Player player = event . getPlayer ( ) ;
2012-12-09 03:42:29 +00:00
2013-08-14 14:01:42 +00:00
final String player_name = player . getName ( ) ;
2012-12-09 03:42:29 +00:00
final String player_ip = event . getAddress ( ) . getHostAddress ( ) . trim ( ) . toLowerCase ( ) ;
if ( player_name . trim ( ) . length ( ) < = 2 )
{
2013-07-11 22:21:23 +00:00
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " Your username is too short (must be at least 3 characters long). " ) ;
2012-12-09 03:42:29 +00:00
return ;
}
else if ( Pattern . compile ( " [^a-zA-Z0-9 \\ - \\ . \\ _] " ) . matcher ( player_name ) . find ( ) )
{
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " Your username contains invalid characters. " ) ;
return ;
}
2013-08-14 14:01:42 +00:00
// not safe to use TFM_Util.isUserSuperadmin for player logging in because player.getAddress() will return a null until after player login.
2012-12-09 03:42:29 +00:00
boolean is_superadmin ;
if ( server . getOnlineMode ( ) )
{
is_superadmin = TFM_SuperadminList . getSuperadminNames ( ) . contains ( player_name . toLowerCase ( ) ) ;
}
else
{
2012-12-20 01:25:29 +00:00
is_superadmin = TFM_SuperadminList . checkPartialSuperadminIP ( player_ip , player_name . toLowerCase ( ) ) ;
2012-12-09 03:42:29 +00:00
}
if ( ! is_superadmin )
{
BanEntry ban_entry = null ;
if ( banByName . isBanned ( player_name . toLowerCase ( ) ) )
{
ban_entry = ( BanEntry ) banByName . getEntries ( ) . get ( player_name . toLowerCase ( ) ) ;
2013-05-09 13:39:33 +00:00
String kick_message = ChatColor . RED + " You are banned from this server. " ;
2012-12-09 03:42:29 +00:00
if ( ban_entry ! = null )
{
kick_message = kick_message + " \ nReason: " + ban_entry . getReason ( ) ;
if ( ban_entry . getExpires ( ) ! = null )
{
2013-07-11 01:46:29 +00:00
kick_message = kick_message + " \ nYour ban will be removed on " + date_format . format ( ban_entry . getExpires ( ) ) ;
2012-12-09 03:42:29 +00:00
}
}
event . disallow ( PlayerLoginEvent . Result . KICK_BANNED , kick_message ) ;
return ;
}
boolean is_ip_banned = false ;
Iterator ip_bans = banByIP . getEntries ( ) . keySet ( ) . iterator ( ) ;
while ( ip_bans . hasNext ( ) )
{
String test_ip = ( String ) ip_bans . next ( ) ;
if ( ! test_ip . matches ( " ^ \\ d{1,3} \\ . \\ d{1,3} \\ .( \\ d{1,3}| \\ *) \\ .( \\ d{1,3}| \\ *)$ " ) )
{
continue ;
}
if ( player_ip . equals ( test_ip ) )
{
ban_entry = ( BanEntry ) banByIP . getEntries ( ) . get ( test_ip ) ;
is_ip_banned = true ;
break ;
}
if ( TFM_Util . fuzzyIpMatch ( test_ip , player_ip , 4 ) )
{
ban_entry = ( BanEntry ) banByIP . getEntries ( ) . get ( test_ip ) ;
is_ip_banned = true ;
break ;
}
}
if ( is_ip_banned )
{
2013-05-09 13:39:33 +00:00
String kick_message = ChatColor . RED + " Your IP address is banned from this server. " ;
2012-12-09 03:42:29 +00:00
if ( ban_entry ! = null )
{
kick_message = kick_message + " \ nReason: " + ban_entry . getReason ( ) ;
if ( ban_entry . getExpires ( ) ! = null )
{
2013-07-11 01:46:29 +00:00
kick_message = kick_message + " \ nYour ban will be removed on " + date_format . format ( ban_entry . getExpires ( ) ) ;
2012-12-09 03:42:29 +00:00
}
}
event . disallow ( PlayerLoginEvent . Result . KICK_BANNED , kick_message ) ;
return ;
}
for ( String test_player : TotalFreedomMod . permbanned_players )
{
if ( test_player . equalsIgnoreCase ( player_name ) )
{
2013-08-09 15:57:45 +00:00
event . disallow ( PlayerLoginEvent . Result . KICK_BANNED , ChatColor . RED + " Your username is permanently banned from this server. \ nRelease procedures are available at http://bit.ly/PermBan " ) ;
2012-12-09 03:42:29 +00:00
return ;
}
}
for ( String test_ip : TotalFreedomMod . permbanned_ips )
{
if ( TFM_Util . fuzzyIpMatch ( test_ip , player_ip , 4 ) )
{
2013-08-09 15:57:45 +00:00
event . disallow ( PlayerLoginEvent . Result . KICK_BANNED , ChatColor . RED + " Your IP address is permanently banned from this server. \ nRelease procedures are available at http://bit.ly/PermBan " ) ;
2012-12-09 03:42:29 +00:00
return ;
}
}
if ( server . getOnlinePlayers ( ) . length > = server . getMaxPlayers ( ) )
{
event . disallow ( PlayerLoginEvent . Result . KICK_FULL , " Sorry, but this server is full. " ) ;
return ;
}
2013-08-18 18:52:46 +00:00
if ( TFM_ConfigEntry . ADMIN_ONLY_MODE . getBoolean ( ) )
2012-12-09 03:42:29 +00:00
{
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " Server is temporarily open to admins only. " ) ;
return ;
}
2013-08-12 18:47:36 +00:00
if ( TotalFreedomMod . lockdownEnabled )
{
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " Server is currently in lockdown mode. " ) ;
return ;
}
2012-12-21 02:33:22 +00:00
if ( player_list . hasWhitelist )
2012-12-09 03:42:29 +00:00
{
2012-12-21 02:33:22 +00:00
if ( ! player_list . getWhitelisted ( ) . contains ( player_name . toLowerCase ( ) ) )
2012-12-09 03:42:29 +00:00
{
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " You are not whitelisted on this server. " ) ;
return ;
}
}
for ( Player test_player : server . getOnlinePlayers ( ) )
{
if ( test_player . getName ( ) . equalsIgnoreCase ( player_name ) )
{
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " Your username is already logged into this server. " ) ;
return ;
}
}
}
2013-07-11 22:21:23 +00:00
else
2013-07-02 18:31:22 +00:00
{
2012-12-09 03:42:29 +00:00
for ( Player test_player : server . getOnlinePlayers ( ) )
{
if ( test_player . getName ( ) . equalsIgnoreCase ( player_name ) )
{
test_player . kickPlayer ( " An admin just logged in with the username you are using. " ) ;
}
}
2013-07-02 18:31:22 +00:00
2013-07-11 22:21:23 +00:00
boolean can_kick = true ; // if the server is full of superadmins, however unlikely that might be, this will prevent an infinite loop.
while ( server . getOnlinePlayers ( ) . length > = server . getMaxPlayers ( ) & & can_kick )
2012-12-09 03:42:29 +00:00
{
2013-07-11 22:21:23 +00:00
can_kick = false ;
for ( Player test_player : server . getOnlinePlayers ( ) )
2012-12-09 03:42:29 +00:00
{
2013-07-11 22:21:23 +00:00
if ( ! TFM_SuperadminList . isUserSuperadmin ( test_player ) )
2012-12-09 03:42:29 +00:00
{
2013-07-11 22:21:23 +00:00
can_kick = true ;
test_player . kickPlayer ( " You have been kicked to free up room for an admin. " ) ;
break ;
2012-12-09 03:42:29 +00:00
}
}
}
2013-08-12 18:47:36 +00:00
if ( TotalFreedomMod . lockdownEnabled )
{
2013-08-14 14:01:42 +00:00
TFM_Util . playerMsg ( player , " Warning: Server is currenty in lockdown-mode, new players will not be able to join! " , ChatColor . RED ) ;
2013-08-12 18:47:36 +00:00
}
2012-12-09 03:42:29 +00:00
}
}
2013-07-04 16:22:02 +00:00
public static String getVersion ( )
{
return MinecraftServer . getServer ( ) . getVersion ( ) ;
}
2012-12-09 03:42:29 +00:00
}