Flood protection.

This commit is contained in:
cnaude 2015-05-02 18:08:30 -07:00
parent 3e00ae9087
commit 3101663625
6 changed files with 218 additions and 17 deletions

View File

@ -0,0 +1,111 @@
/*
* Copyright (C) 2015 cnaude
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cnaude.purpleirc;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.entity.Player;
import org.pircbotx.User;
/**
*
* @author cnaude
*/
public class FloodChecker {
private final PurpleIRC plugin;
private final PurpleBot ircBot;
private final String TIME_FORMAT = "%2.2f";
private final Map<String, MyUser> myUsers;
private class MyUser {
Long lastCommand = System.currentTimeMillis();
Long coolDownTimer = 0L;
int commandCount = 0;
public MyUser() {
}
}
public FloodChecker(final PurpleBot ircBot, final PurpleIRC plugin) {
this.ircBot = ircBot;
this.plugin = plugin;
this.myUsers = new HashMap<>();
}
public boolean isSpam(Object object) {
if (ircBot.floodControlEnabled) {
String key;
String name;
if (object instanceof User) {
key = ((User) object).getHostmask();
name = ((User) object).getNick();
} else if (object instanceof Player) {
key = ((Player) object).getUniqueId().toString();
name = ((Player) object).getName();
} else {
return false;
}
if (myUsers.containsKey(key)) {
MyUser myUser = myUsers.get(key);
Long timeNow = System.currentTimeMillis();
Long timeDiff = timeNow - myUser.lastCommand;
Long coolDiff = timeNow - myUser.coolDownTimer;
myUser.commandCount = Math.max(myUser.commandCount
- (Math.round(timeDiff / ircBot.floodControlTimeInterval * ircBot.floodControlMaxMessages)), 0) + 1;
myUser.lastCommand = timeNow;
if (coolDiff < ircBot.floodControlCooldown) {
plugin.logDebug("Cooldown: " + name + "(" + coolDiff + " < " + ircBot.floodControlCooldown + ")");
return true;
}
if (myUser.commandCount > ircBot.floodControlMaxMessages) {
myUser.coolDownTimer = timeNow;
plugin.logDebug("Spam ignored from: " + name + "(" + key + ")");
return true;
}
} else {
myUsers.put(key, new MyUser());
}
}
return false;
}
public String getCoolDown(User user) {
return getCoolDown(user.getHostmask());
}
public String getCoolDown(Player player) {
return getCoolDown(player.getUniqueId().toString());
}
private String getCoolDown(String key) {
Long timeNow = System.currentTimeMillis();
if (myUsers.containsKey(key)) {
return String.format(TIME_FORMAT,
((ircBot.floodControlCooldown - (timeNow - myUsers.get(key).coolDownTimer)) / 1000f));
}
return "0";
}
}

View File

@ -41,6 +41,15 @@ public class IRCMessageHandler {
public IRCMessageHandler(PurpleIRC plugin) { public IRCMessageHandler(PurpleIRC plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
private void sendFloodWarning(User user, PurpleBot ircBot) {
String message = plugin.colorConverter.gameColorsToIrc(plugin.getMsgTemplate(
ircBot.botNick, TemplateName.IRC_FLOOD_WARNING)
.replace("%COOLDOWN%", ircBot.floodChecker.getCoolDown(user)));
if (!message.isEmpty()) {
user.send().notice(message);
}
}
/** /**
* *
@ -51,6 +60,10 @@ public class IRCMessageHandler {
* @param privateMessage * @param privateMessage
*/ */
public void processMessage(PurpleBot ircBot, User user, Channel channel, String message, boolean privateMessage) { public void processMessage(PurpleBot ircBot, User user, Channel channel, String message, boolean privateMessage) {
if (ircBot.floodChecker.isSpam(user)) {
sendFloodWarning(user, ircBot);
return;
}
plugin.logDebug("processMessage: " + message); plugin.logDebug("processMessage: " + message);
String myChannel = channel.getName(); String myChannel = channel.getName();
if (ircBot.muteList.get(myChannel).contains(user.getNick())) { if (ircBot.muteList.get(myChannel).contains(user.getNick())) {

View File

@ -157,6 +157,11 @@ public final class PurpleBot {
public List<String> channelCmdNotifyIgnore; public List<String> channelCmdNotifyIgnore;
private final ArrayList<ListenerAdapter> ircListeners; private final ArrayList<ListenerAdapter> ircListeners;
public IRCMessageQueueWatcher messageQueue; public IRCMessageQueueWatcher messageQueue;
public FloodChecker floodChecker;
protected boolean floodControlEnabled;
protected int floodControlMaxMessages;
protected long floodControlTimeInterval;
protected long floodControlCooldown;
private final String fileName; private final String fileName;
int joinNoticeCoolDown; int joinNoticeCoolDown;
boolean joinNoticeEnabled; boolean joinNoticeEnabled;
@ -241,6 +246,7 @@ public final class PurpleBot {
} }
messageQueue = new IRCMessageQueueWatcher(this, plugin); messageQueue = new IRCMessageQueueWatcher(this, plugin);
floodChecker = new FloodChecker(this, plugin);
} }
@ -384,7 +390,7 @@ public final class PurpleBot {
sender.sendMessage("[PurpleIRC] [" + botNick + "] " + ChatColor.RED + "Error loading bot configuration!"); sender.sendMessage("[PurpleIRC] [" + botNick + "] " + ChatColor.RED + "Error loading bot configuration!");
} }
} }
/** /**
* *
* @param channelName * @param channelName
@ -397,7 +403,7 @@ public final class PurpleBot {
} else { } else {
sender.sendMessage("User '" + user + "' is now muted."); sender.sendMessage("User '" + user + "' is now muted.");
muteList.get(channelName).add(user); muteList.get(channelName).add(user);
saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".muted", muteList.get(channelName)); saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".muted", muteList.get(channelName));
} }
} }
@ -425,7 +431,7 @@ public final class PurpleBot {
if (muteList.get(channelName).contains(user)) { if (muteList.get(channelName).contains(user)) {
sender.sendMessage("User '" + user + "' is no longer muted."); sender.sendMessage("User '" + user + "' is no longer muted.");
muteList.get(channelName).remove(user); muteList.get(channelName).remove(user);
saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".muted", muteList.get(channelName)); saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".muted", muteList.get(channelName));
} else { } else {
sender.sendMessage("User '" + user + "' is not muted."); sender.sendMessage("User '" + user + "' is not muted.");
} }
@ -547,7 +553,7 @@ public final class PurpleBot {
plugin.logError(ex.getMessage()); plugin.logError(ex.getMessage());
} }
} }
/** /**
* *
* @param section * @param section
@ -577,7 +583,7 @@ public final class PurpleBot {
}); });
sender.sendMessage("Setting nickname to " + newNick); sender.sendMessage("Setting nickname to " + newNick);
saveConfig("nick", newNick); saveConfig("nick", newNick);
} }
public void asyncJoinChannel(final String channelName, final String password) { public void asyncJoinChannel(final String channelName, final String password) {
@ -635,13 +641,13 @@ public final class PurpleBot {
+ "Login set to " + ChatColor.WHITE + "Login set to " + ChatColor.WHITE
+ newLogin + ChatColor.DARK_PURPLE + newLogin + ChatColor.DARK_PURPLE
+ ". Reload the bot for the change to take effect."); + ". Reload the bot for the change to take effect.");
saveConfig("login", newLogin); saveConfig("login", newLogin);
} }
private void sanitizeServerName() { private void sanitizeServerName() {
botServer = botServer.replace("^.*\\/\\/", ""); botServer = botServer.replace("^.*\\/\\/", "");
botServer = botServer.replace(":\\d+$", ""); botServer = botServer.replace(":\\d+$", "");
saveConfig("server", botServer); saveConfig("server", botServer);
} }
private boolean loadConfig() { private boolean loadConfig() {
@ -732,7 +738,7 @@ public final class PurpleBot {
} }
} }
// build command notify recipient list // build command notify recipient list
for (String recipient : config.getStringList("command-notify.recipients")) { for (String recipient : config.getStringList("command-notify.recipients")) {
if (!channelCmdNotifyRecipients.contains(recipient)) { if (!channelCmdNotifyRecipients.contains(recipient)) {
channelCmdNotifyRecipients.add(recipient); channelCmdNotifyRecipients.add(recipient);
@ -743,7 +749,7 @@ public final class PurpleBot {
plugin.logInfo(" No command recipients defined."); plugin.logInfo(" No command recipients defined.");
} }
// build command notify ignore list // build command notify ignore list
for (String command : config.getStringList("command-notify.ignore")) { for (String command : config.getStringList("command-notify.ignore")) {
if (!channelCmdNotifyIgnore.contains(command)) { if (!channelCmdNotifyIgnore.contains(command)) {
channelCmdNotifyIgnore.add(command); channelCmdNotifyIgnore.add(command);
@ -822,7 +828,7 @@ public final class PurpleBot {
enableMessageFiltering.put(channelName, config.getBoolean("channels." + enChannelName + ".enable-filtering", false)); enableMessageFiltering.put(channelName, config.getBoolean("channels." + enChannelName + ".enable-filtering", false));
plugin.logDebug(" EnableMessageFiltering => " + enableMessageFiltering.get(channelName)); plugin.logDebug(" EnableMessageFiltering => " + enableMessageFiltering.get(channelName));
channelPrefix.put(channelName, config.getString("channels." + enChannelName + ".prefix", "")); channelPrefix.put(channelName, config.getString("channels." + enChannelName + ".prefix", ""));
plugin.logDebug(" ChannelPrefix => " + channelPrefix.get(channelName)); plugin.logDebug(" ChannelPrefix => " + channelPrefix.get(channelName));
@ -929,6 +935,12 @@ public final class PurpleBot {
plugin.logDebug("join-notice.ctcp: " + joinNoticeCtcp); plugin.logDebug("join-notice.ctcp: " + joinNoticeCtcp);
plugin.logDebug("join-notice.message: " + joinNoticeMessage); plugin.logDebug("join-notice.message: " + joinNoticeMessage);
// flood control setup
floodControlEnabled = config.getBoolean("flood-control.enabled", false);
floodControlMaxMessages = config.getInt("flood-control.max-messages", 2);
floodControlTimeInterval = config.getLong("flood-control.time-interval", 1000L);
floodControlCooldown = config.getLong("flood-control.cooldown", 60000L);
// build command map // build command map
CaseInsensitiveMap<CaseInsensitiveMap<String>> map = new CaseInsensitiveMap<>(); CaseInsensitiveMap<CaseInsensitiveMap<String>> map = new CaseInsensitiveMap<>();
CaseInsensitiveMap<List<String>> extraMap = new CaseInsensitiveMap<>(); CaseInsensitiveMap<List<String>> extraMap = new CaseInsensitiveMap<>();
@ -1010,6 +1022,10 @@ public final class PurpleBot {
if (!this.isConnected()) { if (!this.isConnected()) {
return; return;
} }
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) { for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) { if (!isPlayerInValidWorld(player, channelName)) {
continue; continue;
@ -1058,6 +1074,15 @@ public final class PurpleBot {
} }
} }
private void sendFloodWarning(Player player) {
String message = plugin.getMsgTemplate(
botNick, TemplateName.GAME_FLOOD_WARNING)
.replace("%COOLDOWN%", floodChecker.getCoolDown(player));
if (!message.isEmpty()) {
player.sendMessage(message);
}
}
// Called from HeroChat listener // Called from HeroChat listener
/** /**
* *
@ -1070,6 +1095,10 @@ public final class PurpleBot {
return; return;
} }
Player player = chatter.getPlayer(); Player player = chatter.getPlayer();
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) { for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) { if (!isPlayerInValidWorld(player, channelName)) {
continue; continue;
@ -1094,6 +1123,10 @@ public final class PurpleBot {
if (!this.isConnected()) { if (!this.isConnected()) {
return; return;
} }
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) { for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) { if (!isPlayerInValidWorld(player, channelName)) {
continue; continue;
@ -1114,6 +1147,10 @@ public final class PurpleBot {
if (!this.isConnected()) { if (!this.isConnected()) {
return; return;
} }
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) { for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) { if (!isPlayerInValidWorld(player, channelName)) {
continue; continue;
@ -1134,6 +1171,10 @@ public final class PurpleBot {
if (!this.isConnected()) { if (!this.isConnected()) {
return; return;
} }
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) { for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) { if (!isPlayerInValidWorld(player, channelName)) {
continue; continue;
@ -1154,6 +1195,10 @@ public final class PurpleBot {
if (!this.isConnected()) { if (!this.isConnected()) {
return; return;
} }
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
if (plugin.tcHook != null) { if (plugin.tcHook != null) {
for (String channelName : botChannels) { for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) { if (!isPlayerInValidWorld(player, channelName)) {
@ -1180,6 +1225,10 @@ public final class PurpleBot {
return; return;
} }
Player player = chatter.getPlayer(); Player player = chatter.getPlayer();
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) { for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) { if (!isPlayerInValidWorld(player, channelName)) {
continue; continue;
@ -1213,6 +1262,10 @@ public final class PurpleBot {
return; return;
} }
Player player = plugin.getServer().getPlayer(participant.getName()); Player player = plugin.getServer().getPlayer(participant.getName());
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) { for (String channelName : botChannels) {
if (!isPlayerInValidWorld(player, channelName)) { if (!isPlayerInValidWorld(player, channelName)) {
continue; continue;
@ -1362,6 +1415,10 @@ public final class PurpleBot {
if (!this.isConnected()) { if (!this.isConnected()) {
return; return;
} }
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) { for (String channelName : botChannels) {
if (isMessageEnabled(channelName, TemplateName.BROADCAST_MESSAGE)) { if (isMessageEnabled(channelName, TemplateName.BROADCAST_MESSAGE)) {
asyncIRCMessage(channelName, plugin.tokenizer asyncIRCMessage(channelName, plugin.tokenizer
@ -1644,6 +1701,10 @@ public final class PurpleBot {
if (!this.isConnected()) { if (!this.isConnected()) {
return; return;
} }
if (floodChecker.isSpam(player)) {
sendFloodWarning(player);
return;
}
for (String channelName : botChannels) { for (String channelName : botChannels) {
if (isMessageEnabled(channelName, TemplateName.GAME_ACTION)) { if (isMessageEnabled(channelName, TemplateName.GAME_ACTION)) {
if (!isPlayerInValidWorld(player, channelName)) { if (!isPlayerInValidWorld(player, channelName)) {
@ -1690,7 +1751,7 @@ public final class PurpleBot {
if (channel != null) { if (channel != null) {
setTheTopic(channel, tTopic); setTheTopic(channel, tTopic);
saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".topic", topic); saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".topic", topic);
channelTopic.put(channelName, topic); channelTopic.put(channelName, topic);
sender.sendMessage("IRC topic for " + channelName + " changed to \"" + topic + "\""); sender.sendMessage("IRC topic for " + channelName + " changed to \"" + topic + "\"");
} else { } else {
sender.sendMessage("Invalid channel: " + channelName); sender.sendMessage("Invalid channel: " + channelName);
@ -1765,7 +1826,7 @@ public final class PurpleBot {
+ ChatColor.RESET + " has been added to the ops list."); + ChatColor.RESET + " has been added to the ops list.");
opsList.get(channelName).add(userMask); opsList.get(channelName).add(userMask);
} }
saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".ops", opsList.get(channelName)); saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".ops", opsList.get(channelName));
} }
/** /**
@ -1783,7 +1844,7 @@ public final class PurpleBot {
+ ChatColor.RESET + " has been added to the voices list."); + ChatColor.RESET + " has been added to the voices list.");
voicesList.get(channelName).add(userMask); voicesList.get(channelName).add(userMask);
} }
saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".voices", voicesList.get(channelName)); saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".voices", voicesList.get(channelName));
} }
/** /**
@ -1801,7 +1862,7 @@ public final class PurpleBot {
sender.sendMessage("User mask " + ChatColor.WHITE + userMask sender.sendMessage("User mask " + ChatColor.WHITE + userMask
+ ChatColor.RESET + " is not in the ops list."); + ChatColor.RESET + " is not in the ops list.");
} }
saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".ops", opsList.get(channelName)); saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".ops", opsList.get(channelName));
} }
/** /**
@ -1820,7 +1881,7 @@ public final class PurpleBot {
+ ChatColor.RESET + " is not in the voices list."); + ChatColor.RESET + " is not in the voices list.");
} }
saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".voices", voicesList.get(channelName)); saveConfig("channels." + encodeChannel(getConfigChannelName(channelName)) + ".voices", voicesList.get(channelName));
} }
/** /**
@ -2125,7 +2186,7 @@ public final class PurpleBot {
} }
return ""; return "";
} }
public String getChannelPrefix(Channel channel) { public String getChannelPrefix(Channel channel) {
if (channelPrefix.containsKey(channel.getName())) { if (channelPrefix.containsKey(channel.getName())) {
return channelPrefix.get(channel.getName()); return channelPrefix.get(channel.getName());
@ -3170,5 +3231,5 @@ public final class PurpleBot {
plugin.logInfo("Trying alternate nick " + botNick); plugin.logInfo("Trying alternate nick " + botNick);
bot.sendIRC().changeNick(botNick); bot.sendIRC().changeNick(botNick);
} }
} }

View File

@ -132,5 +132,8 @@ public class TemplateName {
public final static String FAKE_JOIN = "fake-join"; public final static String FAKE_JOIN = "fake-join";
public final static String FAKE_QUIT = "fake-quit"; public final static String FAKE_QUIT = "fake-quit";
public final static String GAME_FLOOD_WARNING = "game-flood-warning";
public final static String IRC_FLOOD_WARNING = "irc-flood-warning";
} }

View File

@ -62,6 +62,16 @@ command-notify:
- example - example
ignore: ignore:
- /example - /example
# Messaging flood control (game and IRC)
flood-control:
# Enable or disable flood control
enabled: false
# The maximum number of messages per interval
max-messages: 2
# Time interval in milliseconds
time-interval: 1000
# Cooldown in milliseconds. If user is spamming then this cooldown takes effect.
cooldown: 60000
# Automatically part invalid channels # Automatically part invalid channels
part-invalid-channels: false part-invalid-channels: false
# Message when leaving invalid channel # Message when leaving invalid channel

View File

@ -213,6 +213,9 @@ message-format:
default-group-prefix: '' default-group-prefix: ''
default-player-world: 'world' default-player-world: 'world'
default-player-group: '' default-player-group: ''
# Flood control
game-flood-warning: '&3Message not sent to IRC due to spamming. &rCooldown: %COOLDOWN%s'
irc-flood-warning: '&3Message not sent to game due to spamming. &rCooldown: %COOLDOWN%s'
# Format for the @list command in IRC # Format for the @list command in IRC
list-format: '[&9Minecraft&r] &2Online &r(%COUNT%/%MAX%): %PLAYERS%' list-format: '[&9Minecraft&r] &2Online &r(%COUNT%/%MAX%): %PLAYERS%'
list-separator: ', ' list-separator: ', '