Cleaned up captcha
This commit is contained in:
217
src/main/java/com/fpghoti/biscuit/user/CaptchaUser.java
Normal file
217
src/main/java/com/fpghoti/biscuit/user/CaptchaUser.java
Normal file
@@ -0,0 +1,217 @@
|
||||
package com.fpghoti.biscuit.user;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
import com.fpghoti.biscuit.Main;
|
||||
import com.fpghoti.biscuit.biscuit.Biscuit;
|
||||
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.User;
|
||||
import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent;
|
||||
|
||||
public class CaptchaUser implements Iterable<PreUser>{
|
||||
|
||||
private static ArrayList<CaptchaUser> captchaUsers = new ArrayList<CaptchaUser>();
|
||||
|
||||
public static void wipeCaptchaUsers() {
|
||||
captchaUsers = new ArrayList<CaptchaUser>();
|
||||
}
|
||||
|
||||
public static boolean contains(User u) {
|
||||
for(CaptchaUser c : captchaUsers) {
|
||||
if(c.equals(u)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean contains(PreUser u) {
|
||||
for(CaptchaUser c : captchaUsers) {
|
||||
if(c.equals(u)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public static boolean contains(CaptchaUser u) {
|
||||
for(CaptchaUser c : captchaUsers) {
|
||||
if(c.equals(u)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static CaptchaUser getCaptchaUser(User u) {
|
||||
if(contains(u)) {
|
||||
for(CaptchaUser c : captchaUsers) {
|
||||
if(c.equals(u)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}else {
|
||||
CaptchaUser c = new CaptchaUser(u);
|
||||
captchaUsers.add(c);
|
||||
return c;
|
||||
}
|
||||
Main.getMainBiscuit().error("Could not get Captcha User.");
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void remove(CaptchaUser u) {
|
||||
ArrayList<CaptchaUser> cu = new ArrayList<CaptchaUser>(captchaUsers);
|
||||
for(CaptchaUser c : cu) {
|
||||
if(c.equals(u)) {
|
||||
captchaUsers.remove(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private User user;
|
||||
private boolean testMode;
|
||||
private Captcha captcha;
|
||||
private ArrayList<PreUser> preUsers;
|
||||
|
||||
private CaptchaUser(User user) {
|
||||
this.user = user;
|
||||
this.testMode = false;
|
||||
this.captcha = null;
|
||||
this.preUsers = new ArrayList<PreUser>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<PreUser> iterator() {
|
||||
return new ArrayList<PreUser>(preUsers).iterator();
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public Captcha getCaptcha(PrivateMessageReceivedEvent event) {
|
||||
captcha = Captcha.getUpdatedCaptcha(event);
|
||||
return captcha;
|
||||
}
|
||||
|
||||
public void setCaptcha(Captcha c) {
|
||||
captcha = c;
|
||||
}
|
||||
|
||||
public boolean equals(User u) {
|
||||
return user.getId().equals(u.getId());
|
||||
}
|
||||
|
||||
public boolean equals(PreUser u) {
|
||||
return user.getId().equals(u.getUser().getId());
|
||||
}
|
||||
|
||||
public boolean equals(CaptchaUser u) {
|
||||
return user.getId().equals(u.getUser().getId());
|
||||
}
|
||||
|
||||
public boolean contains(Guild g) {
|
||||
if(g == null) {
|
||||
return false;
|
||||
}
|
||||
for(PreUser u : preUsers) {
|
||||
if(u.getBiscuit().getGuild().getId().equals(g.getId())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean contains(Biscuit b) {
|
||||
return contains(b.getGuild());
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return preUsers.isEmpty();
|
||||
}
|
||||
|
||||
public boolean inTestMode() {
|
||||
return testMode;
|
||||
}
|
||||
|
||||
public void enableTestMode() {
|
||||
testMode = true;
|
||||
}
|
||||
|
||||
public void disableTestMode() {
|
||||
testMode = false;
|
||||
}
|
||||
|
||||
public boolean shareGuild() {
|
||||
JDA jda = Main.getJDA();
|
||||
for(Guild g : jda.getGuilds()) {
|
||||
if(g.isMember(user)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public ArrayList<Guild> getSharedGuilds(){
|
||||
ArrayList<Guild> guilds = new ArrayList<Guild>();
|
||||
JDA jda = Main.getJDA();
|
||||
for(Guild g : jda.getGuilds()) {
|
||||
if(g.isMember(user)){
|
||||
guilds.add(g);
|
||||
}
|
||||
}
|
||||
return guilds;
|
||||
}
|
||||
|
||||
public void add(PreUser u) {
|
||||
if(u.getBiscuit() == null || u.getBiscuit().getGuild() == null) {
|
||||
return;
|
||||
}
|
||||
if(contains(u.getBiscuit())) {
|
||||
return;
|
||||
}
|
||||
preUsers.add(u);
|
||||
}
|
||||
|
||||
public void remove(PreUser u) {
|
||||
ArrayList<PreUser> ps = new ArrayList<PreUser>(preUsers);
|
||||
for(PreUser p : ps) {
|
||||
if(p.equals(u)) {
|
||||
preUsers.remove(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public PreUser get(Guild g) {
|
||||
if(g == null) {
|
||||
return null;
|
||||
}
|
||||
for(PreUser u : preUsers) {
|
||||
if(u.getBiscuit().getGuild().getId().equals(g.getId())) {
|
||||
return u;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public PreUser get(Biscuit b) {
|
||||
return get(b.getGuild());
|
||||
}
|
||||
|
||||
// public PreUser getTestUser() {
|
||||
// if(testUser == null) {
|
||||
// PreUser preu = PreUser.makeTestUser(this);
|
||||
// testUser = preu;
|
||||
// }
|
||||
// return testUser;
|
||||
// }
|
||||
|
||||
|
||||
public Captcha getCaptcha() {
|
||||
return captcha;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,52 +1,42 @@
|
||||
package com.fpghoti.biscuit.user;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import com.fpghoti.biscuit.Main;
|
||||
import com.fpghoti.biscuit.PluginCore;
|
||||
import com.fpghoti.biscuit.biscuit.Biscuit;
|
||||
import com.fpghoti.biscuit.util.PermUtil;
|
||||
import com.github.cage.Cage;
|
||||
|
||||
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.User;
|
||||
|
||||
public class PreUser {
|
||||
|
||||
public static CopyOnWriteArrayList<PreUser> testusers = new CopyOnWriteArrayList<PreUser>();
|
||||
|
||||
public static PreUser getTestUser(User u) {
|
||||
for(PreUser pu : testusers) {
|
||||
if(pu.getUser().getId().equals(u.getId())) {
|
||||
return pu;
|
||||
}
|
||||
|
||||
public static PreUser getPreUser(CaptchaUser capUser, Biscuit biscuit) {
|
||||
if(capUser == null) {
|
||||
Main.getMainBiscuit().error("Cannot get PreUser (Invalid Captcha User).");
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean hasTestUser(User u) {
|
||||
return getTestUser(u) != null;
|
||||
if(biscuit == null) {
|
||||
Main.getMainBiscuit().error("Cannot get PreUser (Invalid Biscuit Instance).");
|
||||
return null;
|
||||
}
|
||||
if(capUser.contains(biscuit)) {
|
||||
return capUser.get(biscuit);
|
||||
}
|
||||
PreUser preu = new PreUser(capUser, biscuit, false);
|
||||
capUser.add(preu);
|
||||
return preu;
|
||||
}
|
||||
|
||||
private CaptchaUser capUser;
|
||||
private User user;
|
||||
private String token;
|
||||
private int timeLeft;
|
||||
private boolean done;
|
||||
private boolean test;
|
||||
private Biscuit biscuit;
|
||||
|
||||
public PreUser(User user, Biscuit biscuit) {
|
||||
this(user, biscuit, false);
|
||||
}
|
||||
|
||||
public PreUser(User user, Biscuit biscuit, boolean test) {
|
||||
private PreUser(CaptchaUser capUser, Biscuit biscuit, boolean test) {
|
||||
this.test = test;
|
||||
this.user = user;
|
||||
this.token = null;
|
||||
this.capUser = capUser;
|
||||
this.user = capUser.getUser();
|
||||
this.biscuit = biscuit;
|
||||
this.done = false;
|
||||
this.timeLeft = biscuit.getProperties().noCaptchaKickTime() + 1;
|
||||
@@ -62,14 +52,13 @@ public class PreUser {
|
||||
public User getUser() {
|
||||
return this.user;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return this.token;
|
||||
|
||||
public boolean isTestUser() {
|
||||
return test;
|
||||
}
|
||||
|
||||
public void genToken() {
|
||||
Cage cage = biscuit.getCage();
|
||||
token = cage.getTokenGenerator().next();
|
||||
|
||||
public CaptchaUser getCaptchaUser() {
|
||||
return this.capUser;
|
||||
}
|
||||
|
||||
public void setDone() {
|
||||
@@ -81,7 +70,7 @@ public class PreUser {
|
||||
}
|
||||
|
||||
public void decrementTime() {
|
||||
if(!shareGuild()) {
|
||||
if(!capUser.shareGuild()) {
|
||||
remove();
|
||||
return;
|
||||
}
|
||||
@@ -108,52 +97,50 @@ public class PreUser {
|
||||
msg = msg + " " + invite;
|
||||
}
|
||||
final String fmsg = msg;
|
||||
user.openPrivateChannel().flatMap(channel -> channel.sendMessage(fmsg)).complete();
|
||||
user.openPrivateChannel().flatMap(channel -> channel.sendMessage(fmsg)).submit().whenComplete((message, error) -> {
|
||||
if (error != null) {
|
||||
biscuit.log("Unable to private message user " + user.getName() +".");
|
||||
}
|
||||
kick();
|
||||
});
|
||||
}else {
|
||||
kick();
|
||||
}
|
||||
biscuit.getGuild().kick(user.getId()).submit();
|
||||
}
|
||||
|
||||
remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public boolean shareGuild() {
|
||||
JDA jda = biscuit.getJDA();
|
||||
for(Guild g : jda.getGuilds()) {
|
||||
if(g.isMember(user)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
public void kick() {
|
||||
remove();
|
||||
biscuit.getGuild().kick(user.getId()).submit();
|
||||
}
|
||||
|
||||
public boolean equals(User u) {
|
||||
return user.getId().equals(u.getId());
|
||||
}
|
||||
|
||||
public ArrayList<Guild> getGuilds(){
|
||||
ArrayList<Guild> guilds = new ArrayList<Guild>();
|
||||
JDA jda = biscuit.getJDA();
|
||||
for(Guild g : jda.getGuilds()) {
|
||||
if(g.isMember(user)){
|
||||
guilds.add(g);
|
||||
}
|
||||
}
|
||||
return guilds;
|
||||
public boolean equals(PreUser u) {
|
||||
return user.getId().equals(u.getUser().getId());
|
||||
}
|
||||
|
||||
public boolean equals(CaptchaUser u) {
|
||||
return user.getId().equals(u.getUser().getId());
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
setDone();
|
||||
biscuit.log("Removing captcha data for user " + user.getName() + " " + user.getAsMention());
|
||||
File captcha;
|
||||
if(!Main.isPlugin) {
|
||||
captcha = new File("captcha/" + user.getId() + ".jpg");
|
||||
}else {
|
||||
captcha = new File(PluginCore.plugin.getDataFolder(), "captcha/" + user.getId() + ".jpg");
|
||||
if(capUser.getCaptcha() != null) {
|
||||
capUser.getCaptcha().removeFiles();
|
||||
}
|
||||
token = null;
|
||||
captcha.delete();
|
||||
biscuit.removePreUser(this);
|
||||
testusers.remove(this);
|
||||
if(biscuit.preUserExists(user)) {
|
||||
biscuit.error("CAPTCHA ERROR: PreUser exists after removal");
|
||||
}
|
||||
capUser.remove(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user