TFM-4.3-Reloaded/src/main/java/me/totalfreedom/totalfreedommod/admin/AdminList.java
2017-08-01 15:59:24 -04:00

361 lines
8.8 KiB
Java

package me.totalfreedom.totalfreedommod.admin;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import lombok.Getter;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import net.pravian.aero.config.YamlConfig;
import net.pravian.aero.util.Ips;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.plugin.ServicePriority;
public class AdminList extends FreedomService
{
public static final String CONFIG_FILENAME = "admins.yml";
@Getter
private final Map<String, Admin> allAdmins = Maps.newHashMap(); // Includes disabled admins
// Only active admins below
@Getter
private final Set<Admin> activeAdmins = Sets.newHashSet();
private final Map<String, Admin> nameTable = Maps.newHashMap();
private final Map<String, Admin> ipTable = Maps.newHashMap();
//
private final YamlConfig config;
public AdminList(TotalFreedomMod plugin)
{
super(plugin);
this.config = new YamlConfig(plugin, CONFIG_FILENAME, true);
}
@Override
protected void onStart()
{
load();
server.getServicesManager().register(Function.class, new Function<Player, Boolean>()
{
@Override
public Boolean apply(Player player)
{
return isAdmin(player);
}
}, plugin, ServicePriority.Normal);
deactivateOldEntries(false);
}
@Override
protected void onStop()
{
save();
}
public void load()
{
config.load();
allAdmins.clear();
for (String key : config.getKeys(false))
{
ConfigurationSection section = config.getConfigurationSection(key);
if (section == null)
{
logger.warning("Invalid admin list format: " + key);
continue;
}
Admin admin = new Admin(key);
admin.loadFrom(section);
if (!admin.isValid())
{
FLog.warning("Could not load admin: " + key + ". Missing details!");
continue;
}
allAdmins.put(key, admin);
}
updateTables();
FLog.info("Loaded " + allAdmins.size() + " admins (" + nameTable.size() + " active, " + ipTable.size() + " IPs)");
}
public void save()
{
// Clear the config
for (String key : config.getKeys(false))
{
config.set(key, null);
}
for (Admin admin : allAdmins.values())
{
admin.saveTo(config.createSection(admin.getConfigKey()));
}
config.save();
}
public synchronized boolean isAdminSync(CommandSender sender)
{
return isAdmin(sender);
}
public boolean isAdmin(CommandSender sender)
{
if (!(sender instanceof Player))
{
return true;
}
Admin admin = getAdmin((Player) sender);
return admin != null && admin.isActive();
}
public boolean isSeniorAdmin(CommandSender sender)
{
Admin admin = getAdmin(sender);
if (admin == null)
{
return false;
}
return admin.getRank().ordinal() >= Rank.SENIOR_ADMIN.ordinal();
}
public Admin getAdmin(CommandSender sender)
{
if (sender instanceof Player)
{
return getAdmin((Player) sender);
}
return getEntryByName(sender.getName());
}
public Admin getAdmin(Player player)
{
// Find admin
String ip = Ips.getIp(player);
Admin admin = getEntryByName(player.getName());
// Admin by name
if (admin != null)
{
// Check if we're in online mode,
// Or the players IP is in the admin entry
if (Bukkit.getOnlineMode() || admin.getIps().contains(ip))
{
if (!admin.getIps().contains(ip))
{
// Add the new IP if we have to
admin.addIp(ip);
save();
updateTables();
}
return admin;
}
// Impostor
}
// Admin by ip
admin = getEntryByIp(ip);
if (admin != null)
{
// Set the new username
admin.setName(player.getName());
save();
updateTables();
}
return null;
}
public Admin getEntryByName(String name)
{
return nameTable.get(name.toLowerCase());
}
public Admin getEntryByIp(String ip)
{
return ipTable.get(ip);
}
public Admin getEntryByIpFuzzy(String needleIp)
{
final Admin directAdmin = getEntryByIp(needleIp);
if (directAdmin != null)
{
return directAdmin;
}
for (String ip : ipTable.keySet())
{
if (FUtil.fuzzyIpMatch(needleIp, ip, 3))
{
return ipTable.get(ip);
}
}
return null;
}
public void updateLastLogin(Player player)
{
final Admin admin = getAdmin(player);
if (admin == null)
{
return;
}
admin.setLastLogin(new Date());
admin.setName(player.getName());
save();
}
public boolean isAdminImpostor(Player player)
{
return getEntryByName(player.getName()) != null && !isAdmin(player);
}
public boolean isIdentityMatched(Player player)
{
if (Bukkit.getOnlineMode())
{
return true;
}
Admin admin = getAdmin(player);
return admin == null ? false : admin.getName().equalsIgnoreCase(player.getName());
}
public boolean addAdmin(Admin admin)
{
if (!admin.isValid())
{
logger.warning("Could not add admin: " + admin.getConfigKey() + " Admin is missing details!");
return false;
}
final String key = admin.getConfigKey();
// Store admin, update views
allAdmins.put(key, admin);
updateTables();
// Save admin
admin.saveTo(config.createSection(key));
config.save();
return true;
}
public boolean removeAdmin(Admin admin)
{
if (admin.getRank().isAtLeast(Rank.TELNET_ADMIN))
{
if (plugin.btb != null)
{
plugin.btb.killTelnetSessions(admin.getName());
}
}
// Remove admin, update views
if (allAdmins.remove(admin.getConfigKey()) == null)
{
return false;
}
updateTables();
// 'Unsave' admin
config.set(admin.getConfigKey(), null);
config.save();
return true;
}
public void updateTables()
{
activeAdmins.clear();
nameTable.clear();
ipTable.clear();
for (Admin admin : allAdmins.values())
{
if (!admin.isActive())
{
continue;
}
activeAdmins.add(admin);
nameTable.put(admin.getName().toLowerCase(), admin);
for (String ip : admin.getIps())
{
ipTable.put(ip, admin);
}
}
plugin.wm.adminworld.wipeAccessCache();
}
public Set<String> getAdminNames()
{
return nameTable.keySet();
}
public Set<String> getAdminIps()
{
return ipTable.keySet();
}
public void deactivateOldEntries(boolean verbose)
{
for (Admin admin : allAdmins.values())
{
if (!admin.isActive() || admin.getRank().isAtLeast(Rank.SENIOR_ADMIN))
{
continue;
}
final Date lastLogin = admin.getLastLogin();
final long lastLoginHours = TimeUnit.HOURS.convert(new Date().getTime() - lastLogin.getTime(), TimeUnit.MILLISECONDS);
if (lastLoginHours < ConfigEntry.ADMINLIST_CLEAN_THESHOLD_HOURS.getInteger())
{
continue;
}
if (verbose)
{
FUtil.adminAction("TotalFreedomMod", "Deactivating superadmin " + admin.getName() + ", inactive for " + lastLoginHours + " hours", true);
}
admin.setActive(false);
}
save();
updateTables();
}
}