diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 8a07db6..36a618a 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.fpghoti Biscuit - 1.9 + 1.10 src/main/java @@ -103,7 +103,7 @@ - UTF-8 UTF-8 + UTF-8 diff --git a/pom.xml b/pom.xml index 3d6e6a6..0c7b189 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.fpghoti Biscuit - 1.9 + 1.10 src/main/java diff --git a/src/main/java/com/fpghoti/biscuit/captcha/Captcha.java b/src/main/java/com/fpghoti/biscuit/captcha/Captcha.java index 5bd4e08..559f60f 100644 --- a/src/main/java/com/fpghoti/biscuit/captcha/Captcha.java +++ b/src/main/java/com/fpghoti/biscuit/captcha/Captcha.java @@ -17,54 +17,60 @@ import com.github.cage.Cage; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.MessageChannel; import net.dv8tion.jda.api.entities.PrivateChannel; import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.TextChannel; import net.dv8tion.jda.api.entities.User; -import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent; public class Captcha { - public static Captcha getUpdatedCaptcha(PrivateMessageReceivedEvent event) { - CaptchaUser c = CaptchaUser.getCaptchaUser(event.getAuthor()); + public static Captcha getUpdatedCaptcha(User user, MessageChannel channel, String response) { + CaptchaUser c = CaptchaUser.getCaptchaUser(user); if(c.getCaptcha() == null) { - Captcha captcha = new Captcha(event); + Captcha captcha = new Captcha(user, channel, response); c.setCaptcha(captcha); return captcha; }else { Captcha captcha = c.getCaptcha(); - captcha.setEvent(event); + captcha.setChannel(channel); + captcha.setResponse(response); c.setCaptcha(captcha); return captcha; } } - private CaptchaUser user; - private PrivateMessageReceivedEvent event; - private PrivateChannel channel; - private User author; + private CaptchaUser captchaUser; + private MessageChannel channel; + private User user; + private String response; private String token; - private Captcha(PrivateMessageReceivedEvent event) { - this.user = CaptchaUser.getCaptchaUser(event.getAuthor()); - this.event = event; - this.channel = event.getChannel(); - this.author = event.getAuthor(); + private Captcha(User user, MessageChannel channel, String response) { + this.captchaUser = CaptchaUser.getCaptchaUser(user); + this.channel = channel; + this.user = user; + this.response = response; this.token = null; } - public void setEvent(PrivateMessageReceivedEvent event) { - this.event = event; - } - - public User getAuthor() { - return author; + private void setChannel(MessageChannel channel) { + this.channel = channel; } - public CaptchaUser getCaptchaUser() { + private void setResponse(String response) { + this.response = response; + } + + public User getUser() { return user; } - public PrivateChannel getChannel() { + public CaptchaUser getCaptchaUser() { + return captchaUser; + } + + public MessageChannel getChannel() { return channel; } @@ -78,20 +84,20 @@ public class Captcha { } public void handleResponse() { - if(user.isEmpty() && !user.inTestMode()) { + if(captchaUser.isEmpty() && !captchaUser.inTestMode()) { return; } - String response = leeway(event.getMessage().getContentDisplay()); - - if(token != null && !response.equalsIgnoreCase(token)) { + String captchaString = leeway(response); + + if(token != null && !captchaString.equalsIgnoreCase(token)) { respond("Sorry! That's not quite right! Please try again."); return; } if(token == null) { - - log("Generating captcha challenge for user " + author.getName() + " " + author.getAsMention() + "..."); + + log("Generating captcha challenge for user " + user.getName() + " " + user.getAsMention() + "..."); genToken(); generateImage(); @@ -102,14 +108,14 @@ public class Captcha { }else { boolean disable = false; - if(user.inTestMode()) { + if(captchaUser.inTestMode()) { disable = true; }else { doCaptchaReward(); } - respond("Well done, " + author.getAsMention() + "!"); + respond("Well done, " + user.getAsMention() + "!"); if(disable) { - user.disableTestMode(); + captchaUser.disableTestMode(); } } } @@ -121,10 +127,10 @@ public class Captcha { try { if(!Main.isPlugin) { //If Biscuit is running standalone output to this directory - os = new FileOutputStream("captcha/" + author.getId() + ".jpg", false); + os = new FileOutputStream("captcha/" + user.getId() + ".jpg", false); }else { //If Biscuit is running as a Spigot plugin output to this directory - File c = new File(PluginCore.plugin.getDataFolder(), "captcha/" + author.getId() + ".jpg"); + File c = new File(PluginCore.plugin.getDataFolder(), "captcha/" + user.getId() + ".jpg"); os = new FileOutputStream(c, false); } } catch (FileNotFoundException e) { @@ -151,14 +157,14 @@ public class Captcha { public File getImageFile() { if(!Main.isPlugin) { - return new File("captcha/" + author.getId() + ".jpg"); + return new File("captcha/" + user.getId() + ".jpg"); }else { - return new File(PluginCore.plugin.getDataFolder(), "captcha/" + author.getId() + ".jpg"); + return new File(PluginCore.plugin.getDataFolder(), "captcha/" + user.getId() + ".jpg"); } } public void doCaptchaReward() { - for(PreUser p : user) { + for(PreUser p : captchaUser) { //mark the PreUser as "done" p.setDone(); @@ -189,15 +195,15 @@ public class Captcha { return; } - Member member = g.getMemberById(author.getId()); + Member member = g.getMemberById(user.getId()); g.addRoleToMember(member, newrole).complete(); g.removeRoleFromMember(member, defaultrole).complete(); p.remove(); token = null; - log(BColor.YELLOW_BOLD + author.getName() + " successfully completed a captcha challenge. Granting role."); - biscuit.eventLog(" ``" + author.getName() +"`` " + author.getAsMention() + " successfully completed a captcha challenge. Granting role."); + log(BColor.YELLOW_BOLD + user.getName() + " successfully completed a captcha challenge. Granting role."); + biscuit.eventLog(" ``" + user.getName() +"`` " + user.getAsMention() + " successfully completed a captcha challenge. Granting role."); } } @@ -205,10 +211,10 @@ public class Captcha { File captcha; if(!Main.isPlugin) { //Biscuit is running standalone. Remove file from this directory - captcha = new File("captcha/" + user.getUser().getId() + ".jpg"); + captcha = new File("captcha/" + captchaUser.getUser().getId() + ".jpg"); }else { //Biscuit is running as Spigot plugin. Remove file from this directory - captcha = new File(PluginCore.plugin.getDataFolder(), "captcha/" + user.getUser().getId() + ".jpg"); + captcha = new File(PluginCore.plugin.getDataFolder(), "captcha/" + captchaUser.getUser().getId() + ".jpg"); } captcha.delete(); } @@ -222,7 +228,7 @@ public class Captcha { private void log(String msg) { String prefix = ""; - if(user.inTestMode()) { + if(captchaUser.inTestMode()) { prefix = "[TEST] "; } Main.getMainBiscuit().log(prefix + msg); @@ -230,10 +236,14 @@ public class Captcha { private void respond(String msg) { String prefix = ""; - if(user.inTestMode()) { + if(captchaUser.inTestMode()) { prefix = "[TEST] "; } - MessageText.send(channel, prefix + msg); + if(channel instanceof TextChannel) { + MessageText.send((TextChannel)channel, prefix + msg); + }else if(channel instanceof PrivateChannel) { + MessageText.send((PrivateChannel)channel, prefix + msg); + } } } diff --git a/src/main/java/com/fpghoti/biscuit/captcha/CaptchaChannel.java b/src/main/java/com/fpghoti/biscuit/captcha/CaptchaChannel.java new file mode 100644 index 0000000..8876092 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/captcha/CaptchaChannel.java @@ -0,0 +1,5 @@ +package com.fpghoti.biscuit.captcha; + +public class CaptchaChannel { + +} diff --git a/src/main/java/com/fpghoti/biscuit/captcha/HandleCaptcha.java b/src/main/java/com/fpghoti/biscuit/captcha/HandleCaptcha.java new file mode 100644 index 0000000..1bbba76 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/captcha/HandleCaptcha.java @@ -0,0 +1,48 @@ +package com.fpghoti.biscuit.captcha; + +import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.biscuit.Biscuit; +import com.fpghoti.biscuit.user.CaptchaUser; +import com.fpghoti.biscuit.util.PermUtil; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.MessageChannel; +import net.dv8tion.jda.api.entities.TextChannel; +import net.dv8tion.jda.api.entities.User; + +public class HandleCaptcha { + + public static void handleCaptcha(User user, MessageChannel channel, String content) { + //String content = event.getMessage().getContentDisplay(); + Captcha captcha = Captcha.getUpdatedCaptcha(user, channel, content); + CaptchaUser capUser = captcha.getCaptchaUser(); + + //User is requesting a captcha test + if(content.equalsIgnoreCase("captcha pls") || content.equalsIgnoreCase("cpls")) { + JDA jda = Main.getJDA(); + for(Guild g : jda.getGuilds()) { + if(g.isMember(capUser.getUser())) { + Member m = g.getMember(capUser.getUser()); + if(PermUtil.isAdmin(m)) { + capUser.enableTestMode(); + } + } + } + } + captcha.handleResponse(); + } + + public static Boolean isCaptchaChannel(TextChannel c) { + Biscuit biscuit = Biscuit.getBiscuit(c.getGuild()); + Boolean a = true; + for(String s: biscuit.getProperties().getCaptchaChannels()) { + if(c.getName().equalsIgnoreCase(s)) { + a = false; + } + } + return a; + } + +} diff --git a/src/main/java/com/fpghoti/biscuit/config/BiscuitConfig.java b/src/main/java/com/fpghoti/biscuit/config/BiscuitConfig.java index 7157c51..6be0e46 100644 --- a/src/main/java/com/fpghoti/biscuit/config/BiscuitConfig.java +++ b/src/main/java/com/fpghoti/biscuit/config/BiscuitConfig.java @@ -181,7 +181,8 @@ public class BiscuitConfig { added = addProperty("Allow-Music-Bot", "true", prop, added, silent); added = addProperty("Music-Channels", "", prop, added, silent); added = addProperty("Music-Controller-Role", "music-key", prop, added, silent); - + added = addProperty("Captcha-Channels", "captcha,verification", prop, added, silent); + if(!isMain) { if(code != null) { prop.setProperty("Guild-Code", code); diff --git a/src/main/java/com/fpghoti/biscuit/config/BiscuitProperties.java b/src/main/java/com/fpghoti/biscuit/config/BiscuitProperties.java index 21e48a5..ee242d9 100644 --- a/src/main/java/com/fpghoti/biscuit/config/BiscuitProperties.java +++ b/src/main/java/com/fpghoti/biscuit/config/BiscuitProperties.java @@ -380,4 +380,17 @@ public class BiscuitProperties { } return list; } + + public String[] getCaptchaChannels(){ + String key = "Captcha-Channels"; + if(biscuit.getConfig().getFromConfig(key).equalsIgnoreCase("[global]") && biscuit.getGuild() != null) { + return Main.getMainBiscuit().getProperties().getToggleChannels(); + } + String [] list = biscuit.getConfig().getFromConfig(key).replace(" ", "").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; + } } \ No newline at end of file diff --git a/src/main/java/com/fpghoti/biscuit/listener/DMListener.java b/src/main/java/com/fpghoti/biscuit/listener/DMListener.java index 474e881..de40a71 100644 --- a/src/main/java/com/fpghoti/biscuit/listener/DMListener.java +++ b/src/main/java/com/fpghoti/biscuit/listener/DMListener.java @@ -2,6 +2,7 @@ package com.fpghoti.biscuit.listener; import com.fpghoti.biscuit.Main; import com.fpghoti.biscuit.captcha.Captcha; +import com.fpghoti.biscuit.captcha.HandleCaptcha; import com.fpghoti.biscuit.logging.BColor; import com.fpghoti.biscuit.user.CaptchaUser; import com.fpghoti.biscuit.util.PermUtil; @@ -25,23 +26,7 @@ public class DMListener extends ListenerAdapter{ Main.getMainBiscuit().log(BColor.YELLOW + event.getAuthor().getName() + ": " + BColor.WHITE_BOLD + event.getMessage().getContentDisplay()); } - String content = event.getMessage().getContentDisplay(); - Captcha captcha = Captcha.getUpdatedCaptcha(event); - CaptchaUser capUser = captcha.getCaptchaUser(); - - //User is requesting a captcha test - if(content.equalsIgnoreCase("captcha pls") || content.equalsIgnoreCase("cpls")) { - JDA jda = Main.getJDA(); - for(Guild g : jda.getGuilds()) { - if(g.isMember(capUser.getUser())) { - Member m = g.getMember(capUser.getUser()); - if(PermUtil.isAdmin(m)) { - capUser.enableTestMode(); - } - } - } - } - captcha.handleResponse(); + HandleCaptcha.handleCaptcha(event.getAuthor(), event.getChannel(), event.getMessage().getContentDisplay()); } } diff --git a/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java b/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java index 62cfc42..d8b6737 100644 --- a/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java +++ b/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java @@ -2,6 +2,7 @@ package com.fpghoti.biscuit.listener; import com.fpghoti.biscuit.biscuit.Biscuit; import com.fpghoti.biscuit.biscuit.BiscuitMessageStore; +import com.fpghoti.biscuit.captcha.HandleCaptcha; import com.fpghoti.biscuit.logging.BColor; import com.fpghoti.biscuit.rest.MessageText; import com.fpghoti.biscuit.util.ChatFilter; @@ -26,19 +27,22 @@ public class MessageReceiveListener extends ListenerAdapter{ return; } PermUtil.clearUndeservedRoles(event.getMember()); - logUser(event, biscuit); - if(isNaughty(event)) { return; } - if(handleSoftmuted(event, biscuit)) { return; } if(!handleSpammer(event, biscuit) && biscuit.getProperties().spamPunishAllow()){ checkNewSpammer(event, biscuit); } + + //Channel is a captcha channel + if(HandleCaptcha.isCaptchaChannel(event.getChannel())) { + HandleCaptcha.handleCaptcha(event.getAuthor(), event.getChannel(), event.getMessage().getContentDisplay()); + } + } private void logBot(GuildMessageReceivedEvent event, Biscuit biscuit) { diff --git a/src/main/java/com/fpghoti/biscuit/user/CaptchaUser.java b/src/main/java/com/fpghoti/biscuit/user/CaptchaUser.java index 8b1039d..79b4bcf 100644 --- a/src/main/java/com/fpghoti/biscuit/user/CaptchaUser.java +++ b/src/main/java/com/fpghoti/biscuit/user/CaptchaUser.java @@ -9,6 +9,7 @@ import com.fpghoti.biscuit.captcha.Captcha; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.MessageChannel; import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent; @@ -92,8 +93,8 @@ public class CaptchaUser implements Iterable{ return user; } - public Captcha getCaptcha(PrivateMessageReceivedEvent event) { - captcha = Captcha.getUpdatedCaptcha(event); + public Captcha getCaptcha(MessageChannel channel, String content) { + captcha = Captcha.getUpdatedCaptcha(user, channel, content); return captcha; } diff --git a/src/main/java/com/fpghoti/biscuit/util/Util.java b/src/main/java/com/fpghoti/biscuit/util/Util.java index d66918f..7f20b46 100644 --- a/src/main/java/com/fpghoti/biscuit/util/Util.java +++ b/src/main/java/com/fpghoti/biscuit/util/Util.java @@ -25,7 +25,7 @@ public class Util { } return a; } - + public static boolean isDigit(String s){ return s.matches("[0-9]+"); } diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties index b37049b..fb9a326 100644 --- a/src/main/resources/config.properties +++ b/src/main/resources/config.properties @@ -59,6 +59,10 @@ Done-Emote = ActionComplete #Role will be replaced with reward role on clear Captcha = false +#All channels listed here will allow for users in these channels +#to initiate captcha with the bot. +Captcha-Channels = captcha,verification + #Reward this role when captcha cleared Captcha-Reward-Role = cleared