From e548dd1537972443fdcd6f918b7616830a349605 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 25 Dec 2021 20:07:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20/ip=20/whois=20=E5=92=8C?= =?UTF-8?q?=20186ip=20=E6=9F=A5=E8=AF=A2=20(#17=20@186526)=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20apache-commons-text?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + gradle.properties | 5 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../morny/bot/event/OnCommandExecute.java | 5 ++ .../bot/event/on_commands/Ip186Query.java | 47 +++++++++++++++++++ .../morny/data/ip186/IP186QueryHandler.java | 40 ++++++++++++++++ .../morny/data/ip186/IP186QueryResponse.java | 4 ++ 7 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Ip186Query.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryHandler.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryResponse.java 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) { +}