2012-12-09 03:42:29 +00:00
package me.StevenLawson.TotalFreedomMod ;
2014-04-11 15:24:44 +00:00
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry ;
2012-12-09 03:42:29 +00:00
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 ;
2014-04-04 11:45:35 +00:00
import net.minecraft.server.v1_7_R2.BanEntry ;
import net.minecraft.server.v1_7_R2.BanList ;
import net.minecraft.server.v1_7_R2.MinecraftServer ;
import net.minecraft.server.v1_7_R2.PlayerList ;
import net.minecraft.server.v1_7_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 ;
2013-12-01 12:33:39 +00:00
import org.bukkit.event.player.PlayerLoginEvent.Result ;
2012-12-09 03:42:29 +00:00
public class TFM_ServerInterface
{
2013-08-25 16:32:01 +00:00
private static final SimpleDateFormat dateFormat = new SimpleDateFormat ( " yyyy-MM-dd \ 'at \ ' HH:mm:ss z " ) ;
2012-12-09 03:42:29 +00:00
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 ) ;
}
2013-08-28 15:26:08 +00:00
@SuppressWarnings ( " unchecked " )
2013-08-25 16:32:01 +00:00
public static void banUsername ( String name , String reason , String source , Date expireDate )
2012-12-09 03:42:29 +00:00
{
name = name . toLowerCase ( ) . trim ( ) ;
2013-08-26 22:44:18 +00:00
2014-04-09 20:33:03 +00:00
if ( TFM_AdminList . getSuperadminUUIDs ( ) . contains ( name ) )
2013-08-24 19:35:09 +00:00
{
TFM_Log . info ( " Not banning username " + name + " : is superadmin " ) ;
return ;
}
2013-08-26 22:44:18 +00:00
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 ;
}
}
2013-08-26 22:44:18 +00:00
2013-08-25 16:32:01 +00:00
BanEntry entry = new BanEntry ( name ) ;
if ( expireDate ! = null )
2012-12-09 03:42:29 +00:00
{
2013-08-25 16:32:01 +00:00
entry . setExpires ( expireDate ) ;
2012-12-09 03:42:29 +00:00
}
if ( reason ! = null )
{
2013-08-25 16:32:01 +00:00
entry . setReason ( reason ) ;
2012-12-09 03:42:29 +00:00
}
if ( source ! = null )
{
2013-08-25 16:32:01 +00:00
entry . setSource ( source ) ;
2012-12-09 03:42:29 +00:00
}
2012-12-21 02:33:22 +00:00
BanList nameBans = MinecraftServer . getServer ( ) . getPlayerList ( ) . getNameBans ( ) ;
2013-08-25 16:32:01 +00:00
nameBans . add ( entry ) ;
2012-12-09 03:42:29 +00:00
}
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 ) ;
}
2013-08-25 16:32:01 +00:00
public static void banIP ( String ip , String reason , String source , Date expireDate )
2012-12-09 03:42:29 +00:00
{
ip = ip . toLowerCase ( ) . trim ( ) ;
2013-08-25 16:32:01 +00:00
BanEntry entry = new BanEntry ( ip ) ;
if ( expireDate ! = null )
2012-12-09 03:42:29 +00:00
{
2013-08-25 16:32:01 +00:00
entry . setExpires ( expireDate ) ;
2012-12-09 03:42:29 +00:00
}
if ( reason ! = null )
{
2013-08-25 16:32:01 +00:00
entry . setReason ( reason ) ;
2012-12-09 03:42:29 +00:00
}
if ( source ! = null )
{
2013-08-25 16:32:01 +00:00
entry . setSource ( source ) ;
2012-12-09 03:42:29 +00:00
}
2012-12-21 02:33:22 +00:00
BanList ipBans = MinecraftServer . getServer ( ) . getPlayerList ( ) . getIPBans ( ) ;
2013-08-25 16:32:01 +00:00
ipBans . add ( entry ) ;
2012-12-09 03:42:29 +00:00
}
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
2013-12-01 12:33:39 +00:00
final Server server = TotalFreedomMod . server ;
2012-12-09 03:42:29 +00:00
2013-08-25 16:32:01 +00:00
final PlayerList playerList = MinecraftServer . getServer ( ) . getPlayerList ( ) ;
2013-12-01 12:33:39 +00:00
final BanList ipBans = playerList . getIPBans ( ) ;
final BanList nameBans = playerList . 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-25 16:32:01 +00:00
final String username = player . getName ( ) ;
2013-12-01 12:33:39 +00:00
final String ip = event . getAddress ( ) . getHostAddress ( ) . trim ( ) ;
2012-12-09 03:42:29 +00:00
2013-08-25 16:32:01 +00:00
if ( username . trim ( ) . length ( ) < = 2 )
2012-12-09 03:42:29 +00:00
{
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 ;
}
2013-08-25 16:32:01 +00:00
else if ( Pattern . compile ( " [^a-zA-Z0-9 \\ - \\ . \\ _] " ) . matcher ( username ) . find ( ) )
2012-12-09 03:42:29 +00:00
{
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " Your username contains invalid characters. " ) ;
return ;
}
2014-04-08 18:31:04 +00:00
// not safe to use TFM_Util.isSuperAdmin for player logging in because player.getAddress() will return a null until after player login.
2013-08-25 16:32:01 +00:00
boolean isSuperadmin ;
2012-12-09 03:42:29 +00:00
if ( server . getOnlineMode ( ) )
{
2014-04-09 20:33:03 +00:00
isSuperadmin = TFM_AdminList . getSuperadminUUIDs ( ) . contains ( username . toLowerCase ( ) ) ;
2012-12-09 03:42:29 +00:00
}
else
{
2014-04-11 15:24:44 +00:00
isSuperadmin = TFM_AdminList . checkPartialSuperadminIp ( ip , username . toLowerCase ( ) ) ;
2012-12-09 03:42:29 +00:00
}
2013-12-01 12:33:39 +00:00
// Validation below this point
2012-12-09 03:42:29 +00:00
2013-12-01 12:33:39 +00:00
if ( ! isSuperadmin ) // non-admins
{
// banned-players.txt
if ( nameBans . isBanned ( username . toLowerCase ( ) ) )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
final BanEntry nameBan = ( BanEntry ) nameBans . getEntries ( ) . get ( username . toLowerCase ( ) ) ;
2012-12-09 03:42:29 +00:00
2013-12-03 15:41:43 +00:00
String kickMessage = ChatColor . RED + " You are temporarily banned from this server. \ nAppeal at http://totalfreedom.boards.net/. " ;
2013-12-01 12:33:39 +00:00
if ( nameBan ! = null )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
kickMessage = kickMessage + " \ nReason: " + nameBan . getReason ( ) ;
if ( nameBan . getExpires ( ) ! = null )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
kickMessage = kickMessage + " \ nYour ban will be removed on " + dateFormat . format ( nameBan . getExpires ( ) ) ;
2012-12-09 03:42:29 +00:00
}
}
2013-12-01 12:33:39 +00:00
event . disallow ( Result . KICK_OTHER , kickMessage ) ;
2012-12-09 03:42:29 +00:00
return ;
}
2013-12-01 12:33:39 +00:00
// banned-ips.txt
final Iterator ipBansIt = ipBans . getEntries ( ) . keySet ( ) . iterator ( ) ;
2013-08-25 16:32:01 +00:00
boolean isIpBanned = false ;
2013-12-01 12:33:39 +00:00
BanEntry ipBan = null ;
while ( ipBansIt . hasNext ( ) )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
String testIp = ( String ) ipBansIt . next ( ) ;
2012-12-09 03:42:29 +00:00
2013-08-25 16:32:01 +00:00
if ( ! testIp . matches ( " ^ \\ d{1,3} \\ . \\ d{1,3} \\ .( \\ d{1,3}| \\ *) \\ .( \\ d{1,3}| \\ *)$ " ) )
2012-12-09 03:42:29 +00:00
{
continue ;
}
2013-08-25 16:32:01 +00:00
if ( ip . equals ( testIp ) )
2012-12-09 03:42:29 +00:00
{
2013-08-25 16:32:01 +00:00
isIpBanned = true ;
2013-12-01 12:33:39 +00:00
ipBan = ( BanEntry ) ipBans . getEntries ( ) . get ( testIp ) ;
2012-12-09 03:42:29 +00:00
break ;
}
2013-08-25 16:32:01 +00:00
if ( TFM_Util . fuzzyIpMatch ( testIp , ip , 4 ) )
2012-12-09 03:42:29 +00:00
{
2013-08-25 16:32:01 +00:00
isIpBanned = true ;
2013-12-01 12:33:39 +00:00
ipBan = ( BanEntry ) ipBans . getEntries ( ) . get ( testIp ) ;
2012-12-09 03:42:29 +00:00
break ;
}
}
2013-08-25 16:32:01 +00:00
if ( isIpBanned )
2012-12-09 03:42:29 +00:00
{
2013-12-03 15:41:43 +00:00
String kickMessage = ChatColor . RED + " Your IP address is temporarily banned from this server. \ nAppeal at http://totalfreedom.boards.net/. " ;
2013-12-01 12:33:39 +00:00
if ( ipBan ! = null )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
kickMessage = kickMessage + " \ nReason: " + ipBan . getReason ( ) ;
if ( ipBan . getExpires ( ) ! = null )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
kickMessage = kickMessage + " \ nYour ban will be removed on " + dateFormat . format ( ipBan . getExpires ( ) ) ;
2012-12-09 03:42:29 +00:00
}
}
2013-12-01 12:33:39 +00:00
event . disallow ( Result . KICK_OTHER , kickMessage ) ;
2012-12-09 03:42:29 +00:00
return ;
}
2013-12-01 12:33:39 +00:00
// permban.yml - ips
for ( String testIp : TotalFreedomMod . permbannedIps )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
if ( TFM_Util . fuzzyIpMatch ( testIp , ip , 4 ) )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
event . disallow ( Result . KICK_OTHER , ChatColor . RED + " Your IP address is permanently banned from this server. \ nRelease procedures are available at http://bit.ly/TF_PermBan " ) ;
2012-12-09 03:42:29 +00:00
return ;
}
}
2013-12-01 12:33:39 +00:00
// permban.yml - names
for ( String testPlayer : TotalFreedomMod . permbannedPlayers )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
if ( testPlayer . equalsIgnoreCase ( username ) )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
event . disallow ( Result . KICK_OTHER , ChatColor . RED + " Your username is permanently banned from this server. \ nRelease procedures are available at http://bit.ly/TF_PermBan " ) ;
2012-12-09 03:42:29 +00:00
return ;
}
}
2013-12-01 12:33:39 +00:00
// Server full check
2012-12-09 03:42:29 +00:00
if ( server . getOnlinePlayers ( ) . length > = server . getMaxPlayers ( ) )
{
event . disallow ( PlayerLoginEvent . Result . KICK_FULL , " Sorry, but this server is full. " ) ;
return ;
}
2013-12-01 12:33:39 +00:00
// Admin-only mode
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-12-01 12:33:39 +00:00
// Lockdown mode
2013-08-12 18:47:36 +00:00
if ( TotalFreedomMod . lockdownEnabled )
{
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " Server is currently in lockdown mode. " ) ;
return ;
}
2013-12-01 12:33:39 +00:00
// Whitelist check
2013-08-25 16:32:01 +00:00
if ( playerList . hasWhitelist )
2012-12-09 03:42:29 +00:00
{
2013-08-25 16:32:01 +00:00
if ( ! playerList . getWhitelisted ( ) . contains ( username . toLowerCase ( ) ) )
2012-12-09 03:42:29 +00:00
{
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " You are not whitelisted on this server. " ) ;
return ;
}
}
2013-12-01 12:33:39 +00:00
// Username already logged in check
2012-12-09 03:42:29 +00:00
for ( Player test_player : server . getOnlinePlayers ( ) )
{
2013-08-25 16:32:01 +00:00
if ( test_player . getName ( ) . equalsIgnoreCase ( username ) )
2012-12-09 03:42:29 +00:00
{
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " Your username is already logged into this server. " ) ;
return ;
}
}
}
2013-12-01 12:33:39 +00:00
else // Player is superadmin
2013-07-02 18:31:22 +00:00
{
2013-12-01 12:33:39 +00:00
// force-allow superadmins to log in
event . allow ( ) ;
if ( isIPBanned ( ip ) )
{
unbanIP ( ip ) ;
}
if ( isNameBanned ( username ) )
{
unbanUsername ( username ) ;
}
2013-08-25 16:32:01 +00:00
for ( Player testPlayer : server . getOnlinePlayers ( ) )
2012-12-09 03:42:29 +00:00
{
2013-08-25 16:32:01 +00:00
if ( testPlayer . getName ( ) . equalsIgnoreCase ( username ) )
2012-12-09 03:42:29 +00:00
{
2013-08-25 16:32:01 +00:00
testPlayer . kickPlayer ( " An admin just logged in with the username you are using. " ) ;
2012-12-09 03:42:29 +00:00
}
}
2013-07-02 18:31:22 +00:00
2013-12-01 17:21:43 +00:00
int count = server . getOnlinePlayers ( ) . length ;
if ( count > = server . getMaxPlayers ( ) )
2012-12-09 03:42:29 +00:00
{
2013-12-01 12:33:39 +00:00
for ( Player p : server . getOnlinePlayers ( ) )
2012-12-09 03:42:29 +00:00
{
2014-04-09 20:33:03 +00:00
if ( ! TFM_AdminList . isSuperAdmin ( p ) )
2013-12-01 12:33:39 +00:00
{
p . kickPlayer ( " You have been kicked to free up room for an admin. " ) ;
2013-12-01 17:21:43 +00:00
count - - ;
2013-12-01 12:33:39 +00:00
}
2013-12-01 17:21:43 +00:00
if ( count < server . getMaxPlayers ( ) )
2012-12-09 03:42:29 +00:00
{
2013-07-11 22:21:23 +00:00
break ;
2012-12-09 03:42:29 +00:00
}
}
}
2013-08-12 18:47:36 +00:00
2013-12-01 17:21:43 +00:00
if ( count > = server . getMaxPlayers ( ) )
2013-08-12 18:47:36 +00:00
{
2013-12-01 12:33:39 +00:00
event . disallow ( PlayerLoginEvent . Result . KICK_OTHER , " The server is full and a player could not be kicked, sorry! " ) ;
return ;
2013-08-12 18:47:36 +00:00
}
2013-12-01 12:33:39 +00:00
}
if ( TotalFreedomMod . lockdownEnabled )
{
TFM_Util . playerMsg ( player , " Warning: Server is currenty in lockdown-mode, new players will not be able to join! " , ChatColor . RED ) ;
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
}