    001package net.minecraft.server.management;
    002
    003import java.io.BufferedReader;
    004import java.io.File;
    005import java.io.FileNotFoundException;
    006import java.io.FileReader;
    007import java.io.FileWriter;
    008import java.io.IOException;
    009import java.io.PrintWriter;
    010import java.text.SimpleDateFormat;
    011import java.util.Date;
    012import java.util.Iterator;
    013import java.util.Map;
    014import java.util.logging.Level;
    015import java.util.logging.Logger;
    016
    017public class BanList
    018{
    019    private final LowerStringMap theBanList = new LowerStringMap();
    020    private final File fileName;
    021
    022    /** set to true if not singlePlayer */
    023    private boolean listActive = true;
    024
    025    public BanList(File par1File)
    026    {
    027        this.fileName = par1File;
    028    }
    029
    030    public boolean isListActive()
    031    {
    032        return this.listActive;
    033    }
    034
    035    public void setListActive(boolean par1)
    036    {
    037        this.listActive = par1;
    038    }
    039
    040    /**
    041     * removes expired Bans before returning
    042     */
    043    public Map getBannedList()
    044    {
    045        this.removeExpiredBans();
    046        return this.theBanList;
    047    }
    048
    049    public boolean isBanned(String par1Str)
    050    {
    051        if (!this.isListActive())
    052        {
    053            return false;
    054        }
    055        else
    056        {
    057            this.removeExpiredBans();
    058            return this.theBanList.containsKey(par1Str);
    059        }
    060    }
    061
    062    public void put(BanEntry par1BanEntry)
    063    {
    064        this.theBanList.putLower(par1BanEntry.getBannedUsername(), par1BanEntry);
    065        this.saveToFileWithHeader();
    066    }
    067
    068    public void remove(String par1Str)
    069    {
    070        this.theBanList.remove(par1Str);
    071        this.saveToFileWithHeader();
    072    }
    073
    074    public void removeExpiredBans()
    075    {
    076        Iterator var1 = this.theBanList.values().iterator();
    077
    078        while (var1.hasNext())
    079        {
    080            BanEntry var2 = (BanEntry)var1.next();
    081
    082            if (var2.hasBanExpired())
    083            {
    084                var1.remove();
    085            }
    086        }
    087    }
    088
    089    /**
    090     * Loads the ban list from the file (adds every entry, does not clear the current list).
    091     */
    092    public void loadBanList()
    093    {
    094        if (this.fileName.isFile())
    095        {
    096            BufferedReader var1;
    097
    098            try
    099            {
    100                var1 = new BufferedReader(new FileReader(this.fileName));
    101            }
    102            catch (FileNotFoundException var4)
    103            {
    104                throw new Error();
    105            }
    106
    107            String var2;
    108
    109            try
    110            {
    111                while ((var2 = var1.readLine()) != null)
    112                {
    113                    if (!var2.startsWith("#"))
    114                    {
    115                        BanEntry var3 = BanEntry.parse(var2);
    116
    117                        if (var3 != null)
    118                        {
    119                            this.theBanList.putLower(var3.getBannedUsername(), var3);
    120                        }
    121                    }
    122                }
    123            }
    124            catch (IOException var5)
    125            {
    126                Logger.getLogger("Minecraft").log(Level.SEVERE, "Could not load ban list", var5);
    127            }
    128        }
    129    }
    130
    131    public void saveToFileWithHeader()
    132    {
    133        this.saveToFile(true);
    134    }
    135
    136    /**
    137     * par1: include header
    138     */
    139    public void saveToFile(boolean par1)
    140    {
    141        this.removeExpiredBans();
    142
    143        try
    144        {
    145            PrintWriter var2 = new PrintWriter(new FileWriter(this.fileName, false));
    146
    147            if (par1)
    148            {
    149                var2.println("# Updated " + (new SimpleDateFormat()).format(new Date()) + " by Minecraft " + "1.4.7");
    150                var2.println("# victim name | ban date | banned by | banned until | reason");
    151                var2.println();
    152            }
    153
    154            Iterator var3 = this.theBanList.values().iterator();
    155
    156            while (var3.hasNext())
    157            {
    158                BanEntry var4 = (BanEntry)var3.next();
    159                var2.println(var4.buildBanString());
    160            }
    161
    162            var2.close();
    163        }
    164        catch (IOException var5)
    165        {
    166            Logger.getLogger("Minecraft").log(Level.SEVERE, "Could not save ban list", var5);
    167        }
    168    }
    169}