Skip to content
This repository has been archived by the owner on Jul 27, 2019. It is now read-only.

Commit

Permalink
Add sslciphers option for /irc test
Browse files Browse the repository at this point in the history
  • Loading branch information
cnaude committed May 17, 2015
1 parent 967c17a commit abb11c7
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/cnaude/purpleirc/BotWatcher.java
Expand Up @@ -46,7 +46,7 @@ public void run() {
} else {
ircBot.setConnected(false);
if (ircBot.autoConnect) {
plugin.logInfo("[" + ircBot.botNick + "] NOT CONNECTED");
plugin.logDebug("[" + ircBot.botNick + "] Attempting reconnect...");
ircBot.reload();
}
}
Expand Down
35 changes: 28 additions & 7 deletions src/main/java/com/cnaude/purpleirc/Commands/Test.java
Expand Up @@ -17,6 +17,11 @@
package com.cnaude.purpleirc.Commands;

import com.cnaude.purpleirc.PurpleIRC;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

Expand All @@ -27,7 +32,7 @@
public class Test implements IRCCommandInterface {

private final PurpleIRC plugin;
private final String usage = "[player name]";
private final String usage = "[name|sslciphers]";
private final String desc = "Testing various Vault methods.";
private final String name = "test";
private final String fullUsage = ChatColor.WHITE + "Usage: " + ChatColor.GOLD + "/irc " + name + " " + usage;
Expand Down Expand Up @@ -55,12 +60,28 @@ public void dispatch(CommandSender sender, String[] args) {
if (plugin.debugMode()) {
if (args.length >= 2) {
String playername = args[1];
sender.sendMessage(ChatColor.LIGHT_PURPLE + "Testing " + playername);
sender.sendMessage("getGroupPrefix : " + plugin.getGroupPrefix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getGroupSuffix : " + plugin.getGroupSuffix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerPrefix : " + plugin.getPlayerPrefix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerSuffix : " + plugin.getPlayerSuffix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerGroup : " + plugin.getPlayerGroup(plugin.defaultPlayerWorld, playername));
if (playername.equalsIgnoreCase("sslciphers")) {
sender.sendMessage(ChatColor.LIGHT_PURPLE + "Available SSL ciphers");
SSLContext context;
try {
context = SSLContext.getDefault();
SSLSocketFactory sf = context.getSocketFactory();
String[] cipherSuites = sf.getSupportedCipherSuites();
for (String s : cipherSuites) {
sender.sendMessage(ChatColor.WHITE + "-- " + ChatColor.GOLD + s);
}
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}

} else {
sender.sendMessage(ChatColor.LIGHT_PURPLE + "Testing " + playername);
sender.sendMessage("getGroupPrefix : " + plugin.getGroupPrefix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getGroupSuffix : " + plugin.getGroupSuffix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerPrefix : " + plugin.getPlayerPrefix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerSuffix : " + plugin.getPlayerSuffix(plugin.defaultPlayerWorld, playername));
sender.sendMessage("getPlayerGroup : " + plugin.getPlayerGroup(plugin.defaultPlayerWorld, playername));
}
} else {
sender.sendMessage(fullUsage);
}
Expand Down
111 changes: 111 additions & 0 deletions src/main/java/com/cnaude/purpleirc/FloodChecker.java
@@ -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";
}

}
13 changes: 13 additions & 0 deletions src/main/java/com/cnaude/purpleirc/IRCMessageHandler.java
Expand Up @@ -41,6 +41,15 @@ public class IRCMessageHandler {
public IRCMessageHandler(PurpleIRC 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);
}
}

/**
*
Expand All @@ -51,6 +60,10 @@ public IRCMessageHandler(PurpleIRC plugin) {
* @param 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);
String myChannel = channel.getName();
if (ircBot.muteList.get(myChannel).contains(user.getNick())) {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/cnaude/purpleirc/PurpleBot.java
Expand Up @@ -158,6 +158,11 @@ public final class PurpleBot {
public List<String> channelCmdNotifyIgnore;
private final ArrayList<ListenerAdapter> ircListeners;
public IRCMessageQueueWatcher messageQueue;
public FloodChecker floodChecker;
protected boolean floodControlEnabled;
protected int floodControlMaxMessages;
protected long floodControlTimeInterval;
protected long floodControlCooldown;
private final String fileName;
int joinNoticeCoolDown;
boolean joinNoticeEnabled;
Expand Down Expand Up @@ -885,6 +890,12 @@ private boolean loadConfig() {
plugin.logDebug("join-notice.private: " + joinNoticePrivate);
plugin.logDebug("join-notice.ctcp: " + joinNoticeCtcp);
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
CaseInsensitiveMap<CaseInsensitiveMap<String>> map = new CaseInsensitiveMap<>();
Expand Down Expand Up @@ -1009,6 +1020,15 @@ public void gameChat(Player player, String message) {
}
}
}

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
/**
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/cnaude/purpleirc/PurpleTabCompleter.java
Expand Up @@ -46,10 +46,6 @@ public List<String> onTabComplete(CommandSender cs, Command cmnd, String string,

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

plugin.logDebug("S: " + string);
for (String s : strings) {
plugin.logDebug("S1: " + s);
}
if (strings.length == 1) {
for (String c : plugin.commandHandlers.sortedCommands) {
if (cs.hasPermission("irc." + c)) {
Expand Down
56 changes: 30 additions & 26 deletions src/main/java/com/cnaude/purpleirc/TemplateName.java
Expand Up @@ -21,6 +21,7 @@
* @author cnaude
*/
public class TemplateName {

public final static String GAME_ACHIEVEMENT = "game-achievement";
public final static String GAME_ACTION = "game-action";
public final static String GAME_AFK = "game-afk";
Expand All @@ -34,48 +35,48 @@ public class TemplateName {
public final static String GAME_KICK = "game-kick";
public final static String GAME_COMMAND = "game-command";
public final static String GAME_MODE = "game-mode";

public final static String DYNMAP_WEB_CHAT = "dynmap-web-chat";
public final static String IRC_DYNMAP_WEB_CHAT = "irc-dynmap-web-chat";
public final static String IRC_ACTION_DYNMAP_WEB_CHAT = "irc-action-dynmap-web-chat";
public final static String IRC_DYNMAP_NICK = "irc-dynmap-nick";
public final static String IRC_DYNMAP_ACTION_NICK = "irc-dynmap-action-nick";

public final static String CLEVER_SEND = "clever-send";

public final static String MCMMO_ADMIN_CHAT = "mcmmo-admin-chat";
public final static String MCMMO_PARTY_CHAT = "mcmmo-party-chat";
public final static String MCMMO_CHAT = "mcmmo-chat";

public final static String HERO_ACTION = "hero-action";
public final static String HERO_CHAT = "hero-chat";

public final static String IRC_HERO_ACTION = "irc-hero-action";
public final static String IRC_HERO_CHAT = "irc-hero-chat";
public final static String IRC_HERO_KICK = "irc-hero-kick";
public final static String IRC_HERO_JOIN = "irc-hero-join";
public final static String IRC_HERO_PART = "irc-hero-part";
public final static String IRC_HERO_QUIT = "irc-hero-quit";
public final static String IRC_HERO_TOPIC = "irc-hero-topic";
public final static String IRC_HERO_TOPIC = "irc-hero-topic";
public final static String IRC_ESS_HELPOP = "irc-ess-helpop";

public final static String IRC_HERO_CHANNELS = "irc-hero-channels";
public final static String HERO_CHANNELS = "hero-channels";
public final static String IRC_ACTION_CHANNELS = "irc-action-channels";

public final static String TITAN_CHAT = "titan-chat";
public final static String IRC_TITAN_CHAT = "irc-titan-chat";

public final static String TOWNY_CHAT = "towny-chat";
public final static String TOWNY_CHANNEL_CHAT = "towny-channel-chat";
public final static String IRC_TOWNY_CHAT = "irc-towny-chat";

public final static String FACTION_PUBLIC_CHAT = "faction-public-chat";
public final static String FACTION_ALLY_CHAT = "faction-ally-chat";
public final static String FACTION_ENEMY_CHAT = "faction-enemy-chat";
public final static String CONSOLE_CHAT = "console-chat";
public final static String FACTION_ENEMY_CHAT = "faction-enemy-chat";

public final static String CONSOLE_CHAT = "console-chat";

public final static String IRC_ACTION = "irc-action";
public final static String IRC_CHAT = "irc-chat";
public final static String IRC_HCHAT_RESPONSE = "irc-hchat-response";
Expand All @@ -91,18 +92,18 @@ public class TemplateName {
public final static String IRC_MODE = "irc-mode";
public final static String IRC_NOTICE = "irc-notice";
public final static String IRC_CONSOLE_CHAT = "irc-console-chat";

public final static String ESS_PLAYER_AFK = "ess-player-afk";
public final static String ESS_PLAYER_NOT_AFK = "ess-player-not-afk";
public final static String ESS_HELPOP = "ess-helpop";

public final static String VALID_IRC_COMMANDS = "valid-irc-commands";
public final static String INVALID_IRC_COMMAND = "invalid-irc-command";
public final static String NO_PERM_FOR_IRC_COMMAND = "no-perm-for-irc-command";

public final static String BROADCAST_MESSAGE = "broadcast-message";
public final static String BROADCAST_CONSOLE_MESSAGE = "broadcast-console-message";

public final static String RTS_SEND = "rts-notify";
public final static String RTS_COMPLETE = "rts-complete";
public final static String RTS_CLAIM = "rts-claim";
Expand All @@ -111,26 +112,29 @@ public class TemplateName {
public final static String RTS_ASSIGN = "rts-assign";
public final static String RTS_REOPEN = "rts-reopen";
public final static String RTS_MOD_BROADCAST = "rts-modbroadcast";

public final static String JOBS_SEPARATOR = "jobs-separator";

/* http://dev.bukkit.org/bukkit-plugins/death-messages */
public final static String DEATH_MESSAGES = "death-messages";

public final static String ORE_BROADCAST = "ore-broadcast";

public final static String PRISM_ROLLBACK = "prism-rollback";
public final static String PRISM_DRAIN = "prism-drain";
public final static String PRISM_EXTINGUISH = "prism-extinguish";
public final static String PRISM_CUSTOM = "prism-custom";

public final static String REDDIT_MESSAGES = "reddit-messages";

public final static String IRC_A_RESPONSE = "irc-a-response";
public final static String GAME_A_CHAT = "game-a-chat";
public final static String IRC_A_CHAT = "irc-a-chat";

public final static String FAKE_JOIN = "fake-join";
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";

}

0 comments on commit abb11c7

Please sign in to comment.