Example #1
0
 protected boolean skipLine() {
   try {
     scan.skip(".*\r{0,1}\n");
     return true;
   } catch (NoSuchElementException ex) {
     // EOF
     return false;
   }
 }
Example #2
0
  public boolean load() {
    if (!file.exists()) return true;

    Scanner in;
    try {
      in = new Scanner(file);
    } catch (FileNotFoundException e) {
      pl.getLogger().severe("ipbans.dat appears to exist, but Scanner threw exception.");
      pl.getLogger().throwing(this.getClass().getName(), "load", e);
      return false;
    }

    Scanner tempScan;
    byte version = -1;
    List<String> buffer = new ArrayList<String>(10);
    while (in.hasNextLine()) {
      buffer.add(in.nextLine());
    }
    in.close();

    for (int i = 0; i < buffer.size(); i++) {
      String line = buffer.get(i);
      tempScan = new Scanner(line);
      {
        if (tempScan.hasNext()) {
          String identifier = tempScan.next();
          if (identifier.equals("VERSION:")) {
            if (tempScan.hasNextByte()) {
              version = tempScan.nextByte();
            } else {
              pl.getLogger().warning("Missing version number in " + file.getName());
              pl.getLogger().warning("Assuming " + IP_BANS_VERSION);
              version = IP_BANS_VERSION;
            }
          } else // IP listing
          {
            try {

              String ips = tempScan.next();

              tempScan.useDelimiter("\u00C4");
              String reason = tempScan.next();
              String info = tempScan.next();
              tempScan.reset();
              tempScan.skip("\u00C4");
              long endTime = tempScan.nextLong();
              String uuidStr = tempScan.next();
              String bannerUuidStr = tempScan.next();
              boolean isReq = tempScan.nextBoolean();
              long expireTime = -1;
              boolean aoe = false;
              if (isReq) {
                expireTime = tempScan.nextLong();
                aoe = tempScan.nextBoolean();
              }

              Scanner ipsScan = new Scanner(ips);
              ipsScan.useDelimiter(",");

              List<String> ipsList = new ArrayList<String>();

              while (ipsScan.hasNext()) ipsList.add(ipsScan.next());

              ipsScan.close();

              UUID banned = uuidStr.equals("null") ? null : UUID.fromString(uuidStr);
              UUID banner = bannerUuidStr.equals("null") ? null : UUID.fromString(bannerUuidStr);

              IpBan ipban =
                  new IpBan(ipsList, reason, info, endTime, banned, banner, isReq, expireTime, aoe);

              if (ipban.isOver()) {
                // it's over- dont add it back
                this.removeBan(ipban);
              } else if (ipban.isExpired()) {
                // its expired, fetch new ban if needed
                IpBan newBan = ipban.expire();
                if (newBan != null) {
                  this.addBan(newBan);
                }
                this.removeBan(ipban);
              } else {
                this.ipBans.add(ipban);
              }

            } catch (InputMismatchException ime) {
              pl.getLogger().severe("Input mismatch in " + file.getName() + ". Ban will be lost.");
              pl.getLogger().throwing(this.getClass().getName(), "load", ime);
              continue;
            } catch (NoSuchElementException nsee) {
              pl.getLogger().severe("Missing element in " + file.getName() + ". Ban will be lost.");
              pl.getLogger().throwing(this.getClass().getName(), "load", nsee);
              continue;
            }
          }
        } else {
          pl.getLogger().warning("Found unexpected empty line in " + file.getName());
        }
      }
      tempScan.close();
    }

    return true;
  }