Search in sources :

Example 1 with Pair

use of stream.flarebot.flarebot.util.Pair in project FlareBot by FlareBot.

the class GeneralUtils method jsonContains.

/**
 * Checks if paths exist in the given json
 * <p>
 * Key of the {@link Pair} is a list of the paths that exist in the JSON
 * Value of the {@link Pair} is a list of the paths that don't exist in the JSON
 *
 * @param json  The JSON to check <b>Mustn't be null</b>
 * @param paths The paths to check <b>Mustn't be null or empty</b>
 * @return
 */
public static Pair<List<String>, List<String>> jsonContains(String json, String... paths) {
    Objects.requireNonNull(json);
    Objects.requireNonNull(paths);
    if (paths.length == 0)
        throw new IllegalArgumentException("Paths cannot be empty!");
    JsonElement jelem = FlareBot.GSON.fromJson(json, JsonElement.class);
    JSONConfig config = new JSONConfig(jelem.getAsJsonObject());
    List<String> contains = new ArrayList<>();
    List<String> notContains = new ArrayList<>();
    for (String path : paths) {
        if (path == null)
            continue;
        if (config.getElement(path).isPresent())
            contains.add(path);
        else
            notContains.add(path);
    }
    return new Pair<>(Collections.unmodifiableList(contains), Collections.unmodifiableList(notContains));
}
Also used : JsonElement(com.google.gson.JsonElement) JSONConfig(io.github.binaryoverload.JSONConfig) Pair(stream.flarebot.flarebot.util.Pair)

Example 2 with Pair

use of stream.flarebot.flarebot.util.Pair in project FlareBot by FlareBot.

the class URLChecker method checkURL.

private Pair<URLCheckFlag, String> checkURL(String url, Set<URLCheckFlag> flags) {
    Matcher matcher;
    logger.debug("Checking {} with flags: {}", url, Arrays.toString(flags.toArray()));
    // Check whitelisted domains
    if ((matcher = URLConstants.WHITELISTED_DOMAINS_PATTERN.matcher(url)).find()) {
        return new Pair<>(null, matcher.group());
    }
    // IP Grabber
    if (flags.contains(URLCheckFlag.IP_GRABBER)) {
        logger.debug(URLConstants.IP_GRABBER_PATTERN.toString());
        if ((matcher = URLConstants.IP_GRABBER_PATTERN.matcher(url)).find()) {
            return new Pair<>(URLCheckFlag.IP_GRABBER, matcher.group());
        }
    }
    // Discord Invite
    if (flags.contains(URLCheckFlag.DISCORD_INVITE)) {
        if ((matcher = URLConstants.DISCORD_INVITE_PATTERN.matcher(url)).find()) {
            return new Pair<>(URLCheckFlag.DISCORD_INVITE, matcher.group());
        }
    }
    // Phishing
    if (flags.contains(URLCheckFlag.PHISHING)) {
        if ((matcher = URLConstants.PHISHING_PATTERN.matcher(url)).find()) {
            return new Pair<>(URLCheckFlag.PHISHING, matcher.group());
        }
    }
    // Suspicious TLDs
    if (flags.contains(URLCheckFlag.SUSPICIOUS)) {
        if ((matcher = URLConstants.SUSPICIOUS_TLDS_PATTERN.matcher(url)).find()) {
            return new Pair<>(URLCheckFlag.SUSPICIOUS, matcher.group());
        }
    }
    // URL
    if (flags.contains(URLCheckFlag.URL)) {
        return new Pair<>(URLCheckFlag.URL, url);
    }
    return null;
}
Also used : Matcher(java.util.regex.Matcher) Pair(stream.flarebot.flarebot.util.Pair)

Aggregations

Pair (stream.flarebot.flarebot.util.Pair)2 JsonElement (com.google.gson.JsonElement)1 JSONConfig (io.github.binaryoverload.JSONConfig)1 Matcher (java.util.regex.Matcher)1