diff --git a/build.gradle b/build.gradle index 2b36477..5dc0fd9 100644 --- a/build.gradle +++ b/build.gradle @@ -20,6 +20,7 @@ repositories { dependencies { compileOnlyApi "com.github.spotbugs:spotbugs-annotations:${libSpotbugsVersion}" + implementation "org.apache.commons:commons-text:${libApacheCommonsTextVersion}" api "cc.sukazyo:messiva:${libMessivaVersion}" diff --git a/gradle.properties b/gradle.properties index f8bd912..9690518 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,10 +1,11 @@ ## Core -VERSION = 0.4.2.9 +VERSION = 0.4.2.10 # dependencies -libSpotbugsVersion = 4.5.0 +libSpotbugsVersion = 4.5.2 +libApacheCommonsTextVersion = 1.9 libMessivaVersion = 0.1.0.1 diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index a2cdfd5..b810d23 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.4.2.9"; - public static final long COMPILE_TIMESTAMP = 1640371604243L; + public static final String VERSION = "0.4.2.10"; + public static final long COMPILE_TIMESTAMP = 1640434011159L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java index 8b9d052..486430f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java @@ -7,6 +7,7 @@ import cc.sukazyo.cono.morny.bot.api.EventListener; import cc.sukazyo.cono.morny.bot.api.InputCommand; import cc.sukazyo.cono.morny.bot.event.on_commands.EventHack; import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId; +import cc.sukazyo.cono.morny.bot.event.on_commands.Ip186Query; import cc.sukazyo.cono.morny.data.MornyJrrp; import cc.sukazyo.cono.morny.data.TelegramStickers; import cc.sukazyo.cono.morny.util.CommonFormatUtils; @@ -56,6 +57,10 @@ public class OnCommandExecute extends EventListener { case "/jrrp": onCommandJrrpExec(event); break; + case "/ip": + case "/whois": + Ip186Query.exec(event, command); + break; default: return nonCommandExecutable(event, command); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java new file mode 100644 index 0000000..9186707 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java @@ -0,0 +1,47 @@ +package cc.sukazyo.cono.morny.bot.event.on_commands; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.bot.api.InputCommand; +import cc.sukazyo.cono.morny.data.ip186.IP186QueryResponse; +import cc.sukazyo.cono.morny.data.ip186.IP186QueryHandler; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.SendMessage; +import org.apache.commons.text.StringEscapeUtils; + +import javax.annotation.Nonnull; + +public class Ip186Query { + + public static void exec (@Nonnull Update event, @Nonnull InputCommand command) { + + if (!command.hasArgs()) { MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Unavailable] No ip defined." + ).replyToMessageId(event.message().messageId())); return; } + + if (command.getArgs().length > 1) { MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Unavailable] Too much arguments." + ).replyToMessageId(event.message().messageId())); return; } + + try { + IP186QueryResponse response = switch (command.getCommand()) { + case "/ip" -> IP186QueryHandler.queryIp(command.getArgs()[0]); + case "/whois" -> IP186QueryHandler.queryWhois(command.getArgs()[0]); + default -> throw new IllegalArgumentException("Unknown 186-IP query method " + command.getCommand()); + }; + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + response.url() + "\n" + StringEscapeUtils.escapeHtml4(response.body()) + "" + ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); + } catch (Exception e) { + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Exception] in query:\n" + e.getMessage() + "" + ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); + } + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryHandler.java b/src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryHandler.java new file mode 100644 index 0000000..fcfe0da --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryHandler.java @@ -0,0 +1,40 @@ +package cc.sukazyo.cono.morny.data.ip186; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; + +import javax.annotation.Nonnull; +import java.io.IOException; + +public class IP186QueryHandler { + + public static final String SITE_URL = "https://ip.186526.xyz/"; + + private static final String QUERY_IP_PARAM = "type=json&format=true"; + private static final String QUERY_WHOIS_PARAM = "type=plain"; + + private static final OkHttpClient httpClient = new OkHttpClient(); + + public static IP186QueryResponse queryIp (String ip) throws IOException { + final String requestUrl = SITE_URL + ip; + return commonQuery(requestUrl, QUERY_IP_PARAM); + } + + public static IP186QueryResponse queryWhois (String domain) throws IOException { + final String requestUrl = SITE_URL + "whois/" + domain; + return commonQuery(requestUrl, QUERY_WHOIS_PARAM); + } + + @Nonnull + private static IP186QueryResponse commonQuery (String requestUrl, String queryIpParam) throws IOException { + Request request = new Request.Builder().url(requestUrl + "?" + queryIpParam).build(); + try (Response response = httpClient.newCall(request).execute()) { + final ResponseBody body = response.body(); + if (body == null) throw new IOException("Null body."); + return new IP186QueryResponse(requestUrl, body.string()); + } + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryResponse.java b/src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryResponse.java new file mode 100644 index 0000000..24c075f --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryResponse.java @@ -0,0 +1,4 @@ +package cc.sukazyo.cono.morny.data.ip186; + +public record IP186QueryResponse(String url, String body) { +}