Skip to content

Commit

Permalink
Merge pull request #15 from nickperkins/#3
Browse files Browse the repository at this point in the history
Implement IP Bans
  • Loading branch information
TerrorBite committed Jun 9, 2014
2 parents a8d4e3a + 4125616 commit cd28411
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/main/java/net/lethargiclion/informaban/InformaBan.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private void createDBClassList() {
ebeans.add(net.lethargiclion.informaban.events.Event.class);
ebeans.add(net.lethargiclion.informaban.events.TimedEvent.class);
ebeans.add(net.lethargiclion.informaban.events.Ban.class);
ebeans.add(net.lethargiclion.informaban.events.IPBan.class);
ebeans.add(net.lethargiclion.informaban.events.Unban.class);
ebeans.add(net.lethargiclion.informaban.events.Kick.class);
ebeans.add(net.lethargiclion.informaban.events.ActiveEvent.class);
Expand Down Expand Up @@ -143,6 +144,14 @@ public void onEnable() {
} else
log.warning(msgFailed.format(new Object[] { "/ban" }));

PluginCommand ipban = getCommand("ipban");
if (ipban != null) {
ipban.setExecutor(commandExecutor);
ipban.setDescription(messages.getString("command.ipban.description"));
ipban.setUsage(messages.getString("command.ipban.usage"));
} else
log.warning(msgFailed.format(new Object[] { "/ban" }));

PluginCommand unban = getCommand("unban");
if (unban != null) {
unban.setExecutor(commandExecutor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.lethargiclion.informaban.events.ActiveBan;
import net.lethargiclion.informaban.events.Ban;
import net.lethargiclion.informaban.events.Event;
import net.lethargiclion.informaban.events.IPBan;
import net.lethargiclion.informaban.events.Kick;
import net.lethargiclion.informaban.events.Unban;

Expand All @@ -34,6 +35,8 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.google.common.net.InetAddresses;

/**
* InformaBan command executor class. Processes commands.
*
Expand Down Expand Up @@ -66,6 +69,8 @@ public boolean onCommand(CommandSender sender, Command command,
return commandRap(sender, args);
if (command.getName().equalsIgnoreCase("ban"))
return commandBan(sender, args);
if (command.getName().equalsIgnoreCase("ipban"))
return commandIPBan(sender, args);
if (command.getName().equalsIgnoreCase("unban"))
return commandUnban(sender, args);
} catch (java.util.MissingResourceException e) {
Expand Down Expand Up @@ -103,8 +108,8 @@ private boolean commandKick(CommandSender sender, String[] args) {
MessageFormat.format(
plugin.messages
.getString("command.kick.consoleLog"),
new Object[] { sender.getName(),
victim.getName() }));
new Object[] {sender.getName(),
victim.getName()}));

// Do the kick and record it
Kick k = new Kick();
Expand Down Expand Up @@ -143,8 +148,8 @@ private boolean commandBan(CommandSender sender, String[] args) {
MessageFormat.format(
plugin.messages
.getString("command.ban.consoleLog"),
new Object[] { sender.getName(),
victim.getName() }));
new Object[] {sender.getName(),
victim.getName()}));

// Do the ban and record it
Ban b = new Ban();
Expand All @@ -160,6 +165,68 @@ private boolean commandBan(CommandSender sender, String[] args) {
return false;
}

/**
* Handles the /ipban command.
*
* @param sender
* The CommandSender executing this command.
* @param args
* The command arguments.
* @return False if a usage message should be displayed.
*/
private boolean commandIPBan(CommandSender sender, String[] args) {

if (args.length == 1)
sender.sendMessage(plugin.messages
.getString("command.ban.reasonRequired"));

String subject = null;

if (args.length > 1) {
if (InetAddresses.isInetAddress(args[0])) {
subject = args[0];
} else {
subject = sender.getServer().getPlayer(args[0]).getAddress().getAddress().getHostAddress();
}

// Check for existing IP ban
ActiveBan ab = plugin.getDatabase().find(ActiveBan.class).where()
.eq("subject", subject)
.findUnique();

if (ab != null) {
sender.sendMessage(plugin.messages
.getString("error.IPAlreadyBanned"));
return true;
}

if (subject != null) {
// Set up ban message
String banReason = StringUtils.join(
Arrays.copyOfRange(args, 1, args.length), ' ');

// Log ban to console
plugin.getLogger().info(
MessageFormat.format(
plugin.messages
.getString("command.ban.consoleLog"),
new Object[] {sender.getName(),
subject}));

// Do the ban and record it
IPBan b = new IPBan();
b.apply(plugin.messages, subject, sender, banReason,
Ban.PERMANENT);
plugin.getDatabase().insert(b); // Record the banning event
plugin.getDatabase().insert(b.makeActiveEvent()); // Set the actual ban
} else
sender.sendMessage(plugin.messages
.getString("error.playerNotFound"));
return true;
}
return false;
}

/**
* Handles the /rap command.
*
Expand All @@ -179,15 +246,44 @@ private boolean commandRap(CommandSender sender, String[] args) {
sender.sendMessage(MessageFormat.format(
plugin.messages.getString("command.rap.clean"), name));
}
else {
sender.sendMessage(MessageFormat.format(
plugin.messages.getString("command.rap.ban"), name));
}
Iterator<Event> i = events.iterator();
while (i.hasNext()) {
sender.sendMessage(i.next().toString());
}

// Check for online player, and get bans matching IP address
Player p = plugin.getServer().getPlayer(name);

if (p != null) {
String ipaddress = p.getAddress().getAddress().getHostAddress();

if (ipaddress != null) {
List<Event> ipevents = plugin.getDatabase().find(Event.class).where()
.disjunction()
.eq("subject", ipaddress)
.eq("subjectIP", ipaddress)
.findList();

if (!ipevents.isEmpty()) {
sender.sendMessage(MessageFormat.format(
plugin.messages.getString("command.rap.ip"), ipaddress));
}
Iterator<Event> j = ipevents.iterator();

while (j.hasNext()) {
sender.sendMessage(j.next().toString());
}
}
}
return true;
}
return false;
}

/**
* Handles the /unban command.
*
Expand All @@ -214,8 +310,8 @@ private boolean commandUnban(CommandSender sender, String[] args) {
MessageFormat.format(
plugin.messages
.getString("command.unban.consoleLog"),
new Object[] { sender.getName(),
ab.getSubject() }));
new Object[] {sender.getName(),
ab.getSubject()}));

// Do the unban and record it
Unban b = new Unban();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private void checkBans(PlayerLoginEvent event) {
.eq("subject", event.getAddress().getHostAddress())
.findUnique();


if (b != null) {
if (b.isActive()) {
event.setKickMessage(b.getParent().getMessage());
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/net/lethargiclion/informaban/events/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.google.common.net.InetAddresses;

/**
* This base class represents a record of a ban, kick, jailing or other event
* tracked by InformaBan.
Expand Down Expand Up @@ -167,13 +169,17 @@ protected boolean apply(Player subject, CommandSender enforcer,
}

/**
* Applies this event to an offline subject. No IP is recorded.
* Applies this event to an offline subject. Accepts IP or player as subject.
*/
protected boolean apply(String subject, CommandSender enforcer,
String reason) {
if (getDateIssued() != null)
return false;
setDateIssued(new Date());
if (InetAddresses.isInetAddress(subject)) {
this.subjectIP = subject;

}
this.subject = subject;
this.enforcer = enforcer.getName();
this.reason = reason;
Expand Down
Loading

0 comments on commit cd28411

Please sign in to comment.