Process captcha for cancelled chat events as well.

This commit is contained in:
asofold 2014-02-22 00:00:10 +01:00
parent 4957f2875a
commit d24a13ed12
3 changed files with 37 additions and 40 deletions

View File

@ -39,14 +39,14 @@ public class Captcha extends AsyncCheck implements ICaptcha{
// Have they failed too man times? // Have they failed too man times?
if (data.captchTries > cc.captchaTries) { if (data.captchTries > cc.captchaTries) {
// Find out if we need to kick the player or not. // Find out if we need to kick the player or not.
executeActions(player, data.captchaVL, 1, cc.captchaActions, executeActions(player, data.captchaVL, 1, cc.captchaActions, isMainThread);
isMainThread);
// (Resetting captcha tries is done on quit/kick). // (Resetting captcha tries is done on quit/kick).
} }
// Display the question again (if not kicked). // Display the question again (if not kicked).
if (player.isOnline()) if (player.isOnline()) {
sendCaptcha(player, cc, data); sendCaptcha(player, cc, data);
}
} }
} }

View File

@ -121,28 +121,27 @@ public class ChatListener extends CheckListener implements INotifyReload, JoinLe
* @param event * @param event
* the event * the event
*/ */
@EventHandler( @EventHandler(ignoreCancelled = false, priority = EventPriority.LOWEST)
ignoreCancelled = true, priority = EventPriority.LOWEST)
public void onPlayerChat(final AsyncPlayerChatEvent event) { public void onPlayerChat(final AsyncPlayerChatEvent event) {
/*
* ____ _ ____ _ _
* | _ \| | __ _ _ _ ___ _ __ / ___| |__ __ _| |_
* | |_) | |/ _` | | | |/ _ \ '__| | | | '_ \ / _` | __|
* | __/| | (_| | |_| | __/ | | |___| | | | (_| | |_
* |_| |_|\__,_|\__, |\___|_| \____|_| |_|\__,_|\__|
* |___/
*/
final Player player = event.getPlayer(); final Player player = event.getPlayer();
final boolean alreadyCancelled = event.isCancelled();
// Tell TickTask to update cached permissions. // Tell TickTask to update cached permissions.
// (Might omit this if already cancelled.)
TickTask.requestPermissionUpdate(player.getName(), CheckType.CHAT); TickTask.requestPermissionUpdate(player.getName(), CheckType.CHAT);
// First the color check. // First the color check.
if (color.isEnabled(player)) event.setMessage(color.check(player, event.getMessage(), false)); if (!alreadyCancelled && color.isEnabled(player)) {
event.setMessage(color.check(player, event.getMessage(), false));
}
// Then the no chat check. // Then the no chat check.
if (text.isEnabled(player) && text.check(player, event.getMessage(), captcha, false)) // TODO: isMainThread: Could consider event.isAsync ?
if (textChecks(player, event.getMessage(), false, alreadyCancelled)) {
event.setCancelled(true); event.setCancelled(true);
}
} }
/** /**
@ -151,23 +150,8 @@ public class ChatListener extends CheckListener implements INotifyReload, JoinLe
* @param event * @param event
* the event * the event
*/ */
@EventHandler( @EventHandler(priority = EventPriority.LOWEST)
priority = EventPriority.LOWEST)
public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) { public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) {
/*
* ____ _ ____ _
* | _ \| | __ _ _ _ ___ _ __ / ___|___ _ __ ___ _ __ ___ __ _ _ __ __| |
* | |_) | |/ _` | | | |/ _ \ '__| | | / _ \| '_ ` _ \| '_ ` _ \ / _` | '_ \ / _` |
* | __/| | (_| | |_| | __/ | | |__| (_) | | | | | | | | | | | (_| | | | | (_| |
* |_| |_|\__,_|\__, |\___|_| \____\___/|_| |_| |_|_| |_| |_|\__,_|_| |_|\__,_|
* |___/
* ____
* | _ \ _ __ ___ _ __ _ __ ___ ___ ___ ___ ___
* | |_) | '__/ _ \ '_ \| '__/ _ \ / __/ _ \/ __/ __|
* | __/| | | __/ |_) | | | (_) | (_| __/\__ \__ \
* |_| |_| \___| .__/|_| \___/ \___\___||___/___/
* |_|
*/
final Player player = event.getPlayer(); final Player player = event.getPlayer();
// Tell TickTask to update cached permissions. // Tell TickTask to update cached permissions.
@ -207,16 +191,23 @@ public class ChatListener extends CheckListener implements INotifyReload, JoinLe
final boolean handleAsChat = chatCommands.hasAnyPrefixWords(lcMessage, lcAltMessage); final boolean handleAsChat = chatCommands.hasAnyPrefixWords(lcMessage, lcAltMessage);
if (handleAsChat){ if (handleAsChat){
// Treat as chat. // Treat as chat.
// TODO: At least cut off the command (!). // TODO: Consider requesting permission updates on these, for consistency.
if (text.isEnabled(player) && text.check(player, message, captcha, true)) // TODO: Cut off the command (?).
event.setCancelled(true); if (textChecks(player, message, true, false)) {
event.setCancelled(true);
}
} }
else if (!commandExclusions.hasAnyPrefixWords(lcMessage, lcAltMessage)){ else if (!commandExclusions.hasAnyPrefixWords(lcMessage, lcAltMessage)){
// Treat as command. // Treat as command.
if (commands.isEnabled(player) && commands.check(player, message, captcha)) if (commands.isEnabled(player) && commands.check(player, message, captcha)) {
event.setCancelled(true); event.setCancelled(true);
}
} }
}
private boolean textChecks(final Player player, final String message, final boolean isMainThread, final boolean alreadyCancelled) {
return text.isEnabled(player) && text.check(player, message, captcha, isMainThread, alreadyCancelled);
} }
/** /**

View File

@ -53,15 +53,16 @@ public class Text extends AsyncCheck implements INotifyReload{
* The message to check. * The message to check.
* @param captcha * @param captcha
* Used for starting captcha on failure, if configured so. * Used for starting captcha on failure, if configured so.
* @param alreadyCancelled
* @return * @return
*/ */
public boolean check(final Player player, final String message, final ICaptcha captcha, boolean isMainThread) { public boolean check(final Player player, final String message, final ICaptcha captcha, boolean isMainThread, final boolean alreadyCancelled) {
final ChatConfig cc = ChatConfig.getConfig(player); final ChatConfig cc = ChatConfig.getConfig(player);
final ChatData data = ChatData.getData(player); final ChatData data = ChatData.getData(player);
synchronized (data) { synchronized (data) {
return unsafeCheck(player, message, captcha, cc, data, isMainThread); return unsafeCheck(player, message, captcha, cc, data, isMainThread, alreadyCancelled);
} }
} }
@ -93,15 +94,20 @@ public class Text extends AsyncCheck implements INotifyReload{
* @param cc * @param cc
* @param data * @param data
* @param isMainThread * @param isMainThread
* @param alreadyCancelled
* @return * @return
*/ */
private boolean unsafeCheck(final Player player, final String message, final ICaptcha captcha, private boolean unsafeCheck(final Player player, final String message, final ICaptcha captcha,
final ChatConfig cc, final ChatData data, boolean isMainThread) { final ChatConfig cc, final ChatData data, boolean isMainThread, final boolean alreadyCancelled) {
// Test captcha. // Test captcha.
if (captcha.shouldCheckCaptcha(cc, data)){ // TODO: Skip captcha for "handleaschat" commands? [controversial potential]
if (captcha.shouldCheckCaptcha(cc, data)) {
captcha.checkCaptcha(player, message, cc, data, isMainThread); captcha.checkCaptcha(player, message, cc, data, isMainThread);
return true; return true;
} else if (alreadyCancelled) {
// Skip checking.
return true;
} }
// Take time once: // Take time once: