From ff428f76b2644e040e5f073462689d140e00fdfd Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 14 Dec 2021 22:38:53 +0800 Subject: [PATCH 01/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=8A=AB=E6=8C=81=E5=8A=9F=E8=83=BD=E7=9A=84=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=EF=BC=8C=E6=96=87=E6=9C=AC=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E8=B4=B4=E7=BA=B8=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加事件劫持与返回命令的事件请求命令 - 添加贴纸管理类集中存放贴纸 id - 修改 runtime 方法中的 memory 字段名为 "vm memory" - 为 runtime 方法, OnInlineQuery 添加 @since 标签 --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../morny/bot/event/OnCommandExecute.java | 28 +++---- .../morny/bot/event/OnEventHackHandle.java | 21 +++++ .../cono/morny/bot/event/OnInlineQuery.java | 3 + .../bot/event/on_commands/EventHack.java | 76 +++++++++++++++++++ .../cono/morny/data/TelegramStickers.java | 16 ++++ 7 files changed, 134 insertions(+), 16 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java diff --git a/gradle.properties b/gradle.properties index ad6e88f..67c4c6b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.1.3 +VERSION = 0.4.2.0 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 9000285..04559c8 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.1.3"; - public static final long COMPILE_TIMESTAMP = 1639476313268L; + public static final String VERSION = "0.4.2.0"; + public static final long COMPILE_TIMESTAMP = 1639492714364L; } 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 d3e6def..5f5da75 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 @@ -6,7 +6,9 @@ import cc.sukazyo.cono.morny.MornySystem; import cc.sukazyo.cono.morny.MornyTrusted; 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.data.TelegramStickers; import cc.sukazyo.cono.morny.util.CommonFormatUtils; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; @@ -19,12 +21,6 @@ import static cc.sukazyo.cono.morny.Log.logger; public class OnCommandExecute extends EventListener { - private static final String ONLINE_STATUS_RETURN_STICKER_ID = "CAACAgEAAx0CW-CvvgAC5eBhhhODGRuu0pxKLwoQ3yMsowjviAACcycAAnj8xgVVU666si1utiIE"; - private static final String HELLO_STICKER_ID = "CAACAgEAAxkBAAMnYYYWKNXO4ibo9dlsmDctHhhV6fIAAqooAAJ4_MYFJJhrHS74xUAiBA"; - private static final String EXIT_STICKER_ID = "CAACAgEAAxkBAAMoYYYWt8UjvP0N405SAyvg2SQZmokAAkMiAAJ4_MYFw6yZLu06b-MiBA"; - private static final String EXIT_403_STICKER_ID = "CAACAgEAAxkBAAMqYYYa_7hpXH6hMOYMX4Nh8AVYd74AAnQnAAJ4_MYFRdmmsQKLDZgiBA"; - private static final String NON_COMMAND_QUESTION_STICKER_ID = "CAACAgEAAx0CSQh32gABA966YbRJpbmi2lCHINBDuo1DknSTsbsAAqUoAAJ4_MYFUa8SIaZriAojBA"; - @Override public boolean onMessage (@Nonnull Update event) { if (event.message().text() == null) { @@ -38,6 +34,9 @@ public class OnCommandExecute extends EventListener { case "/user": GetUsernameAndId.exec(command.getArgs(), event); break; + case "/event_hack": + EventHack.exec(event, command); + break; case "/o": onCommandOnExec(event); break; @@ -65,7 +64,7 @@ public class OnCommandExecute extends EventListener { else { // 无法解析的显式命令格式,报错找不到命令 MornyCoeur.getAccount().execute(new SendSticker( event.message().chat().id(), - NON_COMMAND_QUESTION_STICKER_ID + TelegramStickers.ID_404 ).replyToMessageId(event.message().messageId()) ); return true; @@ -75,15 +74,15 @@ public class OnCommandExecute extends EventListener { private void onCommandOnExec (@Nonnull Update event) { MornyCoeur.getAccount().execute(new SendSticker( event.message().chat().id(), - ONLINE_STATUS_RETURN_STICKER_ID + TelegramStickers.ID_ONLINE_STATUS_RETURN ).replyToMessageId(event.message().messageId()) ); } private void onCommandHelloExec (@Nonnull Update event) { MornyCoeur.getAccount().execute(new SendSticker( - event.message().chat().id(), - HELLO_STICKER_ID + event.message().chat().id(), + TelegramStickers.ID_HELLO ).replyToMessageId(event.message().messageId()) ); } @@ -92,7 +91,7 @@ public class OnCommandExecute extends EventListener { if (MornyTrusted.isTrusted(event.message().from().id())) { MornyCoeur.getAccount().execute(new SendSticker( event.message().chat().id(), - EXIT_STICKER_ID + TelegramStickers.ID_EXIT ).replyToMessageId(event.message().messageId()) ); logger.info("Morny exited by user @" + event.message().from().username()); @@ -100,7 +99,7 @@ public class OnCommandExecute extends EventListener { } else { MornyCoeur.getAccount().execute(new SendSticker( event.message().chat().id(), - EXIT_403_STICKER_ID + TelegramStickers.ID_403 ).replyToMessageId(event.message().messageId()) ); logger.info("403 exited tag from user @" + event.message().from().username()); @@ -126,6 +125,9 @@ public class OnCommandExecute extends EventListener { ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } + /** + * @since 0.4.1.2 + */ private void onCommandRuntimeExec (@Nonnull Update event) { MornyCoeur.getAccount().execute(new SendMessage( event.message().chat().id(), @@ -137,7 +139,7 @@ public class OnCommandExecute extends EventListener { java runtime: - %s - %s - memory: + vm memory: - %d / %d MB morny version: - %s diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java new file mode 100644 index 0000000..3b5f1bc --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java @@ -0,0 +1,21 @@ +package cc.sukazyo.cono.morny.bot.event; + +/** + * @since 0.4.2.0 + */ +public class OnEventHackHandle { + + /** + * @since 0.4.2.0 + */ + public enum HackType { + USER, GROUP, ANY + } + + /** + * @since 0.4.2.0 + */ + public static void registerHack(long fromMessageId, long fromChatId, long fromUserId,HackType type) { + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java index 6bf189e..5dfd54e 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java @@ -11,6 +11,9 @@ import com.pengrad.telegrambot.request.AnswerInlineQuery; import org.jetbrains.annotations.NotNull; /** + * telegram inlineQuery 功能的处理类, + * 也是一个 InlineQueryManager(还没做) + * * @since 0.4.1.3 */ public class OnInlineQuery extends EventListener { diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java new file mode 100644 index 0000000..cd3c6f2 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java @@ -0,0 +1,76 @@ +package cc.sukazyo.cono.morny.bot.event.on_commands; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.MornyTrusted; +import cc.sukazyo.cono.morny.bot.api.InputCommand; +import cc.sukazyo.cono.morny.bot.event.OnEventHackHandle; +import cc.sukazyo.cono.morny.data.TelegramStickers; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.request.SendSticker; + +/** + * @since 0.4.2.0 + */ +public class EventHack { + + /** + * @since 0.4.2.0 + */ + public static void exec (Update event, InputCommand command) { + + boolean isOk = false; + + String x_mode = ""; + if (command.hasArgs()) { + x_mode = command.getArgs()[0]; + } + + switch (x_mode) { + case "any": + if (MornyTrusted.isTrusted(event.message().from().id())) { + OnEventHackHandle.registerHack( + event.message().messageId(), + event.message().from().id(), + event.message().chat().id(), + OnEventHackHandle.HackType.ANY + ); + isOk = true; + } + break; + case "group": + OnEventHackHandle.registerHack( + event.message().messageId(), + event.message().from().id(), + event.message().chat().id(), + OnEventHackHandle.HackType.GROUP + ); + isOk = true; + break; + default: + OnEventHackHandle.registerHack( + event.message().messageId(), + event.message().from().id(), + event.message().chat().id(), + OnEventHackHandle.HackType.USER + ); + isOk = true; + break; + } + + if (isOk) { + MornyCoeur.getAccount().execute(new SendSticker( + event.message().chat().id(), + TelegramStickers.ID_WAITING + ).replyToMessageId(event.message().messageId()) + ); + } else { + MornyCoeur.getAccount().execute(new SendSticker( + event.message().chat().id(), + TelegramStickers.ID_403 + ).replyToMessageId(event.message().messageId()) + ); + } + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java new file mode 100644 index 0000000..d4715e1 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java @@ -0,0 +1,16 @@ +package cc.sukazyo.cono.morny.data; + +/** + * 存放 bot 使用到的贴纸 + * @since 0.4.2.0 + */ +public class TelegramStickers { + + public static final String ID_ONLINE_STATUS_RETURN = "CAACAgEAAx0CW-CvvgAC5eBhhhODGRuu0pxKLwoQ3yMsowjviAACcycAAnj8xgVVU666si1utiIE"; + public static final String ID_HELLO = "CAACAgEAAxkBAAMnYYYWKNXO4ibo9dlsmDctHhhV6fIAAqooAAJ4_MYFJJhrHS74xUAiBA"; + public static final String ID_EXIT = "CAACAgEAAxkBAAMoYYYWt8UjvP0N405SAyvg2SQZmokAAkMiAAJ4_MYFw6yZLu06b-MiBA"; + public static final String ID_403 = "CAACAgEAAxkBAAMqYYYa_7hpXH6hMOYMX4Nh8AVYd74AAnQnAAJ4_MYFRdmmsQKLDZgiBA"; + public static final String ID_404 = "CAACAgEAAx0CSQh32gABA966YbRJpbmi2lCHINBDuo1DknSTsbsAAqUoAAJ4_MYFUa8SIaZriAojBA"; + public static final String ID_WAITING = "CAACAgEAAx0CSQh32gABA-8DYbh7W2VhJ490ucfZMUMrgMR2FW4AAm4nAAJ4_MYFjx6zpxJPWsQjBA"; + +} From 6219773ebcc877f960147ef5707810d02d1a6fb0 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 14 Dec 2021 23:15:39 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=91=BC=E5=8F=AB?= =?UTF-8?q?=E4=B8=BB=E4=BA=BA=E4=BB=A5=E8=BF=9B=E8=A1=8C=E6=9F=90=E4=BA=9B?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E8=AF=B7=E6=B1=82=E7=9A=84=E6=96=B0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8CMornyTrusted=20=E6=B7=BB=E5=8A=A0=E4=B8=BB?= =?UTF-8?q?=E4=BA=BAid=E8=AE=B0=E5=BD=95=E4=B8=8E=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cc/sukazyo/cono/morny/MornyTrusted.java | 7 ++ .../cono/morny/bot/event/EventListeners.java | 4 +- .../cono/morny/bot/event/OnCallMe.java | 74 +++++++++++++++++++ .../cono/morny/data/TelegramStickers.java | 1 + 6 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java diff --git a/gradle.properties b/gradle.properties index 67c4c6b..2e5218c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.0 +VERSION = 0.4.2.1 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 04559c8..cff1c8f 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.0"; - public static final long COMPILE_TIMESTAMP = 1639492714364L; + public static final String VERSION = "0.4.2.1"; + public static final long COMPILE_TIMESTAMP = 1639494688933L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java b/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java index 6fa7883..c2a07de 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java @@ -14,6 +14,12 @@ public class MornyTrusted { */ public static final long TRUSTED_CHAT_ID = -1001541451710L; + /** + * morny 的主人
+ * 这项值的对象总是会被认为是可信任的 + */ + public static final long MASTER = 793274677L; + /** * 用于检查一个 telegram-user 是否受信任
*
@@ -25,6 +31,7 @@ public class MornyTrusted { * @return 所传递的用户id对应的用户是否受信任 */ public static boolean isTrusted (long userId) { + if (userId == MASTER) return true; final ChatMember chatMember = MornyCoeur.getAccount().execute(new GetChatMember(TRUSTED_CHAT_ID, userId)).chatMember(); return ( chatMember != null && ( diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java index a08e3fe..47e4699 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java @@ -9,6 +9,7 @@ public class EventListeners { public static final OnUserSlashAction USER_SLASH_ACTION = new OnUserSlashAction(); public static final OnUpdateTimestampOffsetLock UPDATE_TIMESTAMP_OFFSET_LOCK = new OnUpdateTimestampOffsetLock(); public static final OnInlineQuery INLINE_QUERY = new OnInlineQuery(); + public static final OnCallMe CALL_ME = new OnCallMe(); public static void registerAllListeners () { EventListenerManager.addListener( @@ -16,7 +17,8 @@ public class EventListeners { UPDATE_TIMESTAMP_OFFSET_LOCK, COMMANDS_LISTENER, USER_SLASH_ACTION, - INLINE_QUERY + INLINE_QUERY, + CALL_ME ); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java new file mode 100644 index 0000000..db1f20f --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -0,0 +1,74 @@ +package cc.sukazyo.cono.morny.bot.event; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.MornyTrusted; +import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.data.TelegramStickers; +import com.pengrad.telegrambot.model.Chat; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.SendMessage; +import com.pengrad.telegrambot.request.SendSticker; +import org.jetbrains.annotations.NotNull; + +/** + * @since 0.4.2.1 + */ +public class OnCallMe extends EventListener { + + /** + * @since 0.4.2.1 + */ + private static final long ME = MornyTrusted.MASTER; + + @Override + public boolean onMessage (@NotNull Update update) { + if (update.message().text() == null) + return false; + if (update.message().chat().type() != Chat.Type.Private) + return false; + switch (update.message().text().toLowerCase()) { + case "steam": + case "sbeam": + case "sdeam": + requestSteamJoin(update); + break; + case "hana paresu": + case "花宫": + case "内群": + requestHanaParesuJoin(update); + break; + default: + return false; + } + MornyCoeur.getAccount().execute(new SendSticker( + update.message().chat().id(), + TelegramStickers.ID_SENT + ).replyToMessageId(update.message().messageId()) + ); + return true; + } + + private static void requestSteamJoin (Update event) { + MornyCoeur.getAccount().execute(new SendMessage( + ME, String.format(""" + request STEAM LIBRARY + from %s""", + event.message().from().id(), + event.message().from().firstName() + " " + event.message().from().lastName() + ) + ).parseMode(ParseMode.HTML)); + } + + private static void requestHanaParesuJoin (Update event) { + MornyCoeur.getAccount().execute(new SendMessage( + ME, String.format(""" + request Hana Paresu + from %s""", + event.message().from().id(), + event.message().from().firstName() + " " + event.message().from().lastName() + ) + ).parseMode(ParseMode.HTML)); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java index d4715e1..d478e05 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java @@ -12,5 +12,6 @@ public class TelegramStickers { public static final String ID_403 = "CAACAgEAAxkBAAMqYYYa_7hpXH6hMOYMX4Nh8AVYd74AAnQnAAJ4_MYFRdmmsQKLDZgiBA"; public static final String ID_404 = "CAACAgEAAx0CSQh32gABA966YbRJpbmi2lCHINBDuo1DknSTsbsAAqUoAAJ4_MYFUa8SIaZriAojBA"; public static final String ID_WAITING = "CAACAgEAAx0CSQh32gABA-8DYbh7W2VhJ490ucfZMUMrgMR2FW4AAm4nAAJ4_MYFjx6zpxJPWsQjBA"; + public static final String ID_SENT = "CAACAgEAAx0CSQh32gABA--zYbiyU_wOijEitp-0tSl_k7W6l3gAAgMmAAJ4_MYF4GrompjXPx4jBA"; } From 12b4c918291da2b43a8796874d3896304f877553 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 15 Dec 2021 12:06:55 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=91=BC=E5=8F=AB=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20event=5Fhack=20=E5=91=BD=E4=BB=A4=E4=B8=8E=E5=91=BC?= =?UTF-8?q?=E5=8F=AB=E5=8A=9F=E8=83=BD=E7=9A=84=20javadoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/event/OnCallMe.java | 72 ++++++++++++++++--- .../bot/event/on_commands/EventHack.java | 9 +++ .../morny/bot/event/on_commands/Roll.java | 4 ++ 5 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Roll.java diff --git a/gradle.properties b/gradle.properties index 2e5218c..5a63cde 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.1 +VERSION = 0.4.2.2 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index cff1c8f..53e4adc 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.1"; - public static final long COMPILE_TIMESTAMP = 1639494688933L; + public static final String VERSION = "0.4.2.2"; + public static final long COMPILE_TIMESTAMP = 1639541150226L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java index db1f20f..608c73b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -7,20 +7,32 @@ import cc.sukazyo.cono.morny.data.TelegramStickers; import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.ForwardMessage; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.request.SendSticker; import org.jetbrains.annotations.NotNull; /** + * 通过 bot 呼叫主人的事件监听管理类 * @since 0.4.2.1 */ public class OnCallMe extends EventListener { /** + * 主人的 telegram user id,同时被用于 chat id
+ * 跟随 {@link MornyTrusted#MASTER} 的值 * @since 0.4.2.1 */ private static final long ME = MornyTrusted.MASTER; + /** + * 监听私聊 bot 的消息进行呼叫关键字匹配。 + * 如果成功,将会执行呼叫函数,并向呼叫者回显{@link TelegramStickers#ID_WAITING "已呼叫"贴纸} + * + * @param update 事件基础参数,消息事件所属的 tgapi:update 对象 + * @return 事件基础返回值,是否已完成处理事件:
+ * 如果匹配到呼叫,则返回{@code true},反之返回{@code false} + */ @Override public boolean onMessage (@NotNull Update update) { if (update.message().text() == null) @@ -28,18 +40,17 @@ public class OnCallMe extends EventListener { if (update.message().chat().type() != Chat.Type.Private) return false; switch (update.message().text().toLowerCase()) { - case "steam": - case "sbeam": - case "sdeam": - requestSteamJoin(update); - break; - case "hana paresu": - case "花宫": - case "内群": - requestHanaParesuJoin(update); - break; - default: + case "steam", "sbeam", "sdeam" -> + requestSteamJoin(update); + case "hana paresu", "花宫", "内群" -> + requestHanaParesuJoin(update); + default -> { + if (update.message().text().startsWith("cc::")) { + requestCustomCall(update); + break; + } return false; + } } MornyCoeur.getAccount().execute(new SendSticker( update.message().chat().id(), @@ -49,6 +60,12 @@ public class OnCallMe extends EventListener { return true; } + /** + * 执行 steam library 呼叫
+ * 将会向 {@link #ME} 发送 + * + * @param event 执行呼叫的tg事件 + */ private static void requestSteamJoin (Update event) { MornyCoeur.getAccount().execute(new SendMessage( ME, String.format(""" @@ -60,6 +77,12 @@ public class OnCallMe extends EventListener { ).parseMode(ParseMode.HTML)); } + /** + * 执行花宫呼叫
+ * 将会向 {@link #ME} 发送 + * + * @param event 执行呼叫的tg事件 + */ private static void requestHanaParesuJoin (Update event) { MornyCoeur.getAccount().execute(new SendMessage( ME, String.format(""" @@ -71,4 +94,31 @@ public class OnCallMe extends EventListener { ).parseMode(ParseMode.HTML)); } + /** + * 执行自定义呼叫
+ * 将会向 {@link #ME} 发送一个 request 数据消息和转发的原始请求消息
+ *
+ * known issue
    + *
  • 无法处理与转发带有媒体的消息
  • + *
+ * + * @param event 执行呼叫的tg事件 + * @since 0.4.2.2 + */ + private static void requestCustomCall (Update event) { + MornyCoeur.getAccount().execute(new SendMessage( + ME, String.format(""" + request [???] + from %s""", + event.message().from().id(), + event.message().from().firstName() + " " + event.message().from().lastName() + ) + ).parseMode(ParseMode.HTML)); + MornyCoeur.getAccount().execute(new ForwardMessage( + ME, + event.message().chat().id(), + event.message().messageId() + )); + } + } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java index cd3c6f2..0df037a 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java @@ -9,11 +9,20 @@ import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.SendSticker; /** + * {@link OnEventHackHandle} 的命令行前端 * @since 0.4.2.0 */ public class EventHack { /** + * {@link OnEventHackHandle} 的命令行前端
+ *
+ * 实现了通过命令行进行 EventHack 功能。
+ * 支持三种模式,默认为 {@link OnEventHackHandle.HackType#USER USER}, + * {@link OnEventHackHandle.HackType#ANY ANY} 时,将会通过 {@link MornyTrusted#isTrusted(long)} 检查触发用户的权限 + * + * @param event 命令基础参数,触发的事件对象本身 + * @param command 命令基础参数,解析出的命令对象 * @since 0.4.2.0 */ public static void exec (Update event, InputCommand command) { diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Roll.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Roll.java new file mode 100644 index 0000000..4cb209a --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Roll.java @@ -0,0 +1,4 @@ +package cc.sukazyo.cono.morny.bot.event.on_commands; + +public class Roll { +} From ee30b0a0fffa1809e90a1a14cc2d06043586d99e Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 15 Dec 2021 12:43:00 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=99=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BB=A5=E9=80=89=E9=A1=B9=E6=96=B9=E5=BC=8F=E4=B8=BA?= =?UTF-8?q?=20token=20=E5=92=8C=20username=20=E8=B5=8B=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cc/sukazyo/cono/morny/ServerMain.java | 37 +++++++++++++++---- .../cono/morny/bot/event/OnCallMe.java | 3 ++ 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5a63cde..50cbf59 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.2 +VERSION = 0.4.2.3 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 53e4adc..8a50a22 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.2"; - public static final long COMPILE_TIMESTAMP = 1639541150226L; + public static final String VERSION = "0.4.2.3"; + public static final long COMPILE_TIMESTAMP = 1639543312227L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java index 89147c3..78f1702 100644 --- a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java +++ b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java @@ -35,6 +35,13 @@ public class ServerMain { * 不要同时使用 {@code --no-hello},原因见下。 * *
  • + * {@code --token} 主程序模式的必选项
    + * 用于 bot 启动的 telegram bot api token + *
  • + *
  • + * {@code --username} {@link MornyCoeur#getUsername() bot 的 username} 预定义 + *
  • + *
  • * {@code --no-hello} 不在主程序启动时输出用于欢迎消息的字符画。 * 与 {@code --only-hello} 参数不兼容 —— 会导致程序完全没有任何输出 *
  • @@ -44,7 +51,13 @@ public class ServerMain { * * * 除去选项之外,第一个参数会被赋值为 bot 的 telegram bot api token, - * 第二个参数会被赋值为 bot 的 username 限定名。其余的参数会被认定为无法理解。 + * 第二个参数会被赋值为 bot 的 username 限定名。其余的参数会被认定为无法理解。
    + *
    + * 自 {@code 0.4.2.3},token 和 username 的赋值已被选项组支持
    + * 使用参数所进行取值的 token 和 username 已被转移至 {@code --token} 和 {@code --username} 参数, + * 或许,直接参数赋值的支持将计划在 {@code 0.4.3} 标记废弃并在 {@code 0.5} 删除。 + * 但实际上这并不影响现在的使用,选项赋值目前仍属于测试功能
    + * 但请勿混用,这将使两个赋值出现混淆并产生不可知的结果 * * @see MornyCoeur#main(String, String, long) * @since 0.4.0.0 @@ -56,11 +69,11 @@ public class ServerMain { String username = null; boolean outdatedBlock = false; - for (String arg : args) { + for (int i = 0; i < args.length; i++) { - if (arg.startsWith("-")) { + if (args[i].startsWith("-")) { - switch (arg) { + switch (args[i]) { case "--outdated-block" -> { outdatedBlock = true; continue; @@ -77,22 +90,32 @@ public class ServerMain { versionEchoMode = true; continue; } + case "--token" -> { + i++; + key = args[i]; + continue; + } + case "--username" -> { + i++; + username = args[i]; + continue; + } } } else { if (key == null) { - key = arg; + key = args[i]; continue; } if (username == null) { - username = arg; + username = args[i]; continue; } } - logger.warn("Can't understand arg to some meaning :\n " + arg); + logger.warn("Can't understand arg to some meaning :\n " + args[i]); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java index 608c73b..c1c14e0 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -101,6 +101,9 @@ public class OnCallMe extends EventListener { * known issue
      *
    • 无法处理与转发带有媒体的消息
    • *
    + *
    + * 现在你可以通过这个 bot 来呼叫主人(sukazyo)任何事情了 —— + * 但是直接私聊sukazyo不好吗 * * @param event 执行呼叫的tg事件 * @since 0.4.2.2 From 8dad0930bcb9035b639c1e602e52ee1dfb074030 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 17 Dec 2021 18:22:25 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E4=BD=BF=20user=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E5=9C=A8=E6=97=A0=E5=8F=82=E6=97=B6=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E7=9B=AE=E6=A0=87=E4=B8=BA=E8=B0=83=E7=94=A8=E8=80=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cc/sukazyo/cono/morny/GradleProjectConfigures.java | 4 ++-- .../morny/bot/event/on_commands/GetUsernameAndId.java | 10 +--------- 3 files changed, 4 insertions(+), 12 deletions(-) diff --git a/gradle.properties b/gradle.properties index 50cbf59..2d2e5dc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.3 +VERSION = 0.4.2.4 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 8a50a22..3a8381d 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.3"; - public static final long COMPILE_TIMESTAMP = 1639543312227L; + public static final String VERSION = "0.4.2.4"; + public static final long COMPILE_TIMESTAMP = 1639736122490L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java index b54bb9d..18e91a0 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java @@ -19,7 +19,7 @@ public class GetUsernameAndId { "[Unavailable] Too much arguments." ).replyToMessageId(event.message().messageId())); return; } - long userId = 0; + long userId = event.message().from().id(); if ( event.message().replyToMessage()!= null) { userId = event.message().replyToMessage().from().id(); @@ -36,14 +36,6 @@ public class GetUsernameAndId { } } - if (userId == 0) { - MornyCoeur.getAccount().execute(new SendMessage( - event.message().chat().id(), - "[Unavailable] no userid given." - ).replyToMessageId(event.message().messageId())); - return; - } - final GetChatMemberResponse response = MornyCoeur.getAccount().execute( new GetChatMember(event.message().chat().id(), userId) ); From 7b88b40c1c21f297090cfadf3d9626d6d6c9ce77 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 19 Dec 2021 18:31:36 +0800 Subject: [PATCH 06/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20editorconfig?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8D=20null=20username=20=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E8=A7=84=E8=8C=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 532 ++++++++++++++++++ gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../event/on_commands/GetUsernameAndId.java | 17 +- 4 files changed, 548 insertions(+), 7 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ee8e124 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,532 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = tab +insert_final_newline = true +max_line_length = 120 +tab_width = 4 +ij_continuation_indent_size = 8 +ij_formatter_off_tag = @formatter:off +ij_formatter_on_tag = @formatter:on +ij_formatter_tags_enabled = false +ij_smart_tabs = false +ij_visual_guides = none +ij_wrap_on_typing = false + +[*.java] +ij_java_align_consecutive_assignments = false +ij_java_align_consecutive_variable_declarations = false +ij_java_align_group_field_declarations = false +ij_java_align_multiline_annotation_parameters = false +ij_java_align_multiline_array_initializer_expression = false +ij_java_align_multiline_assignment = true +ij_java_align_multiline_binary_operation = false +ij_java_align_multiline_chained_methods = false +ij_java_align_multiline_extends_list = false +ij_java_align_multiline_for = false +ij_java_align_multiline_method_parentheses = false +ij_java_align_multiline_parameters = false +ij_java_align_multiline_parameters_in_calls = false +ij_java_align_multiline_parenthesized_expression = true +ij_java_align_multiline_records = true +ij_java_align_multiline_resources = false +ij_java_align_multiline_ternary_operation = true +ij_java_align_multiline_text_blocks = false +ij_java_align_multiline_throws_list = false +ij_java_align_subsequent_simple_methods = false +ij_java_align_throws_keyword = true +ij_java_annotation_parameter_wrap = off +ij_java_array_initializer_new_line_after_left_brace = true +ij_java_array_initializer_right_brace_on_new_line = true +ij_java_array_initializer_wrap = on_every_item +ij_java_assert_statement_colon_on_next_line = true +ij_java_assert_statement_wrap = normal +ij_java_assignment_wrap = normal +ij_java_binary_operation_sign_on_next_line = false +ij_java_binary_operation_wrap = off +ij_java_blank_lines_after_anonymous_class_header = 0 +ij_java_blank_lines_after_class_header = 1 +ij_java_blank_lines_after_imports = 1 +ij_java_blank_lines_after_package = 1 +ij_java_blank_lines_around_class = 1 +ij_java_blank_lines_around_field = 0 +ij_java_blank_lines_around_field_in_interface = 0 +ij_java_blank_lines_around_initializer = 0 +ij_java_blank_lines_around_method = 1 +ij_java_blank_lines_around_method_in_interface = 1 +ij_java_blank_lines_before_class_end = 1 +ij_java_blank_lines_before_imports = 1 +ij_java_blank_lines_before_method_body = 0 +ij_java_blank_lines_before_package = 0 +ij_java_block_brace_style = end_of_line +ij_java_block_comment_add_space = false +ij_java_block_comment_at_first_column = true +ij_java_builder_methods = none +ij_java_call_parameters_new_line_after_left_paren = true +ij_java_call_parameters_right_paren_on_new_line = true +ij_java_call_parameters_wrap = normal +ij_java_case_statement_on_separate_line = true +ij_java_catch_on_new_line = false +ij_java_class_annotation_wrap = split_into_lines +ij_java_class_brace_style = end_of_line +ij_java_class_count_to_use_import_on_demand = 5 +ij_java_class_names_in_javadoc = 1 +ij_java_do_not_indent_top_level_class_members = false +ij_java_do_not_wrap_after_single_annotation = false +ij_java_do_while_brace_force = never +ij_java_doc_add_blank_line_after_description = true +ij_java_doc_add_blank_line_after_param_comments = false +ij_java_doc_add_blank_line_after_return = false +ij_java_doc_add_p_tag_on_empty_lines = true +ij_java_doc_align_exception_comments = false +ij_java_doc_align_param_comments = false +ij_java_doc_do_not_wrap_if_one_line = true +ij_java_doc_enable_formatting = true +ij_java_doc_enable_leading_asterisks = true +ij_java_doc_indent_on_continuation = false +ij_java_doc_keep_empty_lines = true +ij_java_doc_keep_empty_parameter_tag = true +ij_java_doc_keep_empty_return_tag = true +ij_java_doc_keep_empty_throws_tag = true +ij_java_doc_keep_invalid_tags = true +ij_java_doc_param_description_on_new_line = false +ij_java_doc_preserve_line_breaks = true +ij_java_doc_use_throws_not_exception_tag = true +ij_java_else_on_new_line = false +ij_java_entity_dd_suffix = EJB +ij_java_entity_eb_suffix = Bean +ij_java_entity_hi_suffix = Home +ij_java_entity_lhi_prefix = Local +ij_java_entity_lhi_suffix = Home +ij_java_entity_li_prefix = Local +ij_java_entity_pk_class = java.lang.String +ij_java_entity_vo_suffix = VO +ij_java_enum_constants_wrap = on_every_item +ij_java_extends_keyword_wrap = normal +ij_java_extends_list_wrap = off +ij_java_field_annotation_wrap = normal +ij_java_finally_on_new_line = false +ij_java_for_brace_force = if_multiline +ij_java_for_statement_new_line_after_left_paren = true +ij_java_for_statement_right_paren_on_new_line = true +ij_java_for_statement_wrap = on_every_item +ij_java_generate_final_locals = false +ij_java_generate_final_parameters = false +ij_java_if_brace_force = never +ij_java_imports_layout = *,|,javax.**,java.**,|,$* +ij_java_indent_case_from_switch = true +ij_java_insert_inner_class_imports = false +ij_java_insert_override_annotation = true +ij_java_keep_blank_lines_before_right_brace = 2 +ij_java_keep_blank_lines_between_package_declaration_and_header = 2 +ij_java_keep_blank_lines_in_code = 2 +ij_java_keep_blank_lines_in_declarations = 2 +ij_java_keep_builder_methods_indents = false +ij_java_keep_control_statement_in_one_line = false +ij_java_keep_first_column_comment = false +ij_java_keep_indents_on_empty_lines = true +ij_java_keep_line_breaks = false +ij_java_keep_multiple_expressions_in_one_line = false +ij_java_keep_simple_blocks_in_one_line = false +ij_java_keep_simple_classes_in_one_line = false +ij_java_keep_simple_lambdas_in_one_line = false +ij_java_keep_simple_methods_in_one_line = false +ij_java_label_indent_absolute = false +ij_java_label_indent_size = 0 +ij_java_lambda_brace_style = end_of_line +ij_java_layout_static_imports_separately = true +ij_java_line_comment_add_space = false +ij_java_line_comment_at_first_column = true +ij_java_message_dd_suffix = EJB +ij_java_message_eb_suffix = Bean +ij_java_method_annotation_wrap = split_into_lines +ij_java_method_brace_style = end_of_line +ij_java_method_call_chain_wrap = normal +ij_java_method_parameters_new_line_after_left_paren = true +ij_java_method_parameters_right_paren_on_new_line = true +ij_java_method_parameters_wrap = normal +ij_java_modifier_list_wrap = false +ij_java_names_count_to_use_import_on_demand = 3 +ij_java_new_line_after_lparen_in_record_header = false +ij_java_packages_to_use_import_on_demand = java.awt.*,javax.swing.* +ij_java_parameter_annotation_wrap = normal +ij_java_parentheses_expression_new_line_after_left_paren = false +ij_java_parentheses_expression_right_paren_on_new_line = false +ij_java_place_assignment_sign_on_next_line = false +ij_java_prefer_longer_names = true +ij_java_prefer_parameters_wrap = false +ij_java_record_components_wrap = normal +ij_java_repeat_synchronized = true +ij_java_replace_instanceof_and_cast = false +ij_java_replace_null_check = true +ij_java_replace_sum_lambda_with_method_ref = true +ij_java_resource_list_new_line_after_left_paren = true +ij_java_resource_list_right_paren_on_new_line = true +ij_java_resource_list_wrap = normal +ij_java_rparen_on_new_line_in_record_header = false +ij_java_session_dd_suffix = EJB +ij_java_session_eb_suffix = Bean +ij_java_session_hi_suffix = Home +ij_java_session_lhi_prefix = Local +ij_java_session_lhi_suffix = Home +ij_java_session_li_prefix = Local +ij_java_session_si_suffix = Service +ij_java_space_after_closing_angle_bracket_in_type_argument = false +ij_java_space_after_colon = true +ij_java_space_after_comma = true +ij_java_space_after_comma_in_type_arguments = true +ij_java_space_after_for_semicolon = true +ij_java_space_after_quest = true +ij_java_space_after_type_cast = false +ij_java_space_before_annotation_array_initializer_left_brace = false +ij_java_space_before_annotation_parameter_list = false +ij_java_space_before_array_initializer_left_brace = false +ij_java_space_before_catch_keyword = true +ij_java_space_before_catch_left_brace = true +ij_java_space_before_catch_parentheses = true +ij_java_space_before_class_left_brace = true +ij_java_space_before_colon = true +ij_java_space_before_colon_in_foreach = true +ij_java_space_before_comma = false +ij_java_space_before_do_left_brace = true +ij_java_space_before_else_keyword = true +ij_java_space_before_else_left_brace = true +ij_java_space_before_finally_keyword = true +ij_java_space_before_finally_left_brace = true +ij_java_space_before_for_left_brace = true +ij_java_space_before_for_parentheses = true +ij_java_space_before_for_semicolon = false +ij_java_space_before_if_left_brace = true +ij_java_space_before_if_parentheses = true +ij_java_space_before_method_call_parentheses = false +ij_java_space_before_method_left_brace = true +ij_java_space_before_method_parentheses = true +ij_java_space_before_opening_angle_bracket_in_type_parameter = true +ij_java_space_before_quest = true +ij_java_space_before_switch_left_brace = true +ij_java_space_before_switch_parentheses = true +ij_java_space_before_synchronized_left_brace = true +ij_java_space_before_synchronized_parentheses = true +ij_java_space_before_try_left_brace = true +ij_java_space_before_try_parentheses = true +ij_java_space_before_type_parameter_list = false +ij_java_space_before_while_keyword = true +ij_java_space_before_while_left_brace = true +ij_java_space_before_while_parentheses = true +ij_java_space_inside_one_line_enum_braces = false +ij_java_space_within_empty_array_initializer_braces = false +ij_java_space_within_empty_method_call_parentheses = false +ij_java_space_within_empty_method_parentheses = false +ij_java_spaces_around_additive_operators = true +ij_java_spaces_around_assignment_operators = true +ij_java_spaces_around_bitwise_operators = true +ij_java_spaces_around_equality_operators = true +ij_java_spaces_around_lambda_arrow = true +ij_java_spaces_around_logical_operators = true +ij_java_spaces_around_method_ref_dbl_colon = false +ij_java_spaces_around_multiplicative_operators = true +ij_java_spaces_around_relational_operators = true +ij_java_spaces_around_shift_operators = true +ij_java_spaces_around_type_bounds_in_type_parameters = true +ij_java_spaces_around_unary_operator = false +ij_java_spaces_within_angle_brackets = false +ij_java_spaces_within_annotation_parentheses = false +ij_java_spaces_within_array_initializer_braces = false +ij_java_spaces_within_braces = false +ij_java_spaces_within_brackets = false +ij_java_spaces_within_cast_parentheses = false +ij_java_spaces_within_catch_parentheses = false +ij_java_spaces_within_for_parentheses = false +ij_java_spaces_within_if_parentheses = false +ij_java_spaces_within_method_call_parentheses = false +ij_java_spaces_within_method_parentheses = false +ij_java_spaces_within_parentheses = false +ij_java_spaces_within_record_header = false +ij_java_spaces_within_switch_parentheses = false +ij_java_spaces_within_synchronized_parentheses = false +ij_java_spaces_within_try_parentheses = false +ij_java_spaces_within_while_parentheses = false +ij_java_special_else_if_treatment = true +ij_java_subclass_name_suffix = Impl +ij_java_ternary_operation_signs_on_next_line = false +ij_java_ternary_operation_wrap = on_every_item +ij_java_test_name_suffix = Test +ij_java_throws_keyword_wrap = normal +ij_java_throws_list_wrap = off +ij_java_use_external_annotations = false +ij_java_use_fq_class_names = false +ij_java_use_relative_indents = true +ij_java_use_single_class_imports = true +ij_java_variable_annotation_wrap = on_every_item +ij_java_visibility = public +ij_java_while_brace_force = never +ij_java_while_on_new_line = true +ij_java_wrap_comments = false +ij_java_wrap_first_method_in_call_chain = true +ij_java_wrap_long_lines = false + +[.editorconfig] +ij_editorconfig_align_group_field_declarations = false +ij_editorconfig_space_after_colon = false +ij_editorconfig_space_after_comma = true +ij_editorconfig_space_before_colon = false +ij_editorconfig_space_before_comma = false +ij_editorconfig_spaces_around_assignment_operators = true + +[{*.ant,*.fxml,*.isc,*.jhm,*.jnlp,*.jrxml,*.pom,*.rng,*.tld,*.wadl,*.wsdd,*.wsdl,*.xjb,*.xml,*.xsd,*.xsl,*.xslt,*.xul}] +ij_smart_tabs = true +ij_xml_align_attributes = false +ij_xml_align_text = false +ij_xml_attribute_wrap = off +ij_xml_block_comment_add_space = false +ij_xml_block_comment_at_first_column = true +ij_xml_keep_blank_lines = 2 +ij_xml_keep_indents_on_empty_lines = true +ij_xml_keep_line_breaks = true +ij_xml_keep_line_breaks_in_text = true +ij_xml_keep_whitespaces = false +ij_xml_keep_whitespaces_around_cdata = preserve +ij_xml_keep_whitespaces_inside_cdata = false +ij_xml_line_comment_at_first_column = true +ij_xml_space_after_tag_name = false +ij_xml_space_around_equals_in_attribute = false +ij_xml_space_inside_empty_tag = true +ij_xml_text_wrap = normal +ij_xml_use_custom_settings = false + +[{*.gant,*.groovy,*.gson,*.gy}] +ij_smart_tabs = true +ij_groovy_align_group_field_declarations = false +ij_groovy_align_multiline_array_initializer_expression = false +ij_groovy_align_multiline_assignment = false +ij_groovy_align_multiline_binary_operation = false +ij_groovy_align_multiline_chained_methods = false +ij_groovy_align_multiline_extends_list = false +ij_groovy_align_multiline_for = true +ij_groovy_align_multiline_list_or_map = true +ij_groovy_align_multiline_method_parentheses = false +ij_groovy_align_multiline_parameters = true +ij_groovy_align_multiline_parameters_in_calls = false +ij_groovy_align_multiline_resources = true +ij_groovy_align_multiline_ternary_operation = false +ij_groovy_align_multiline_throws_list = false +ij_groovy_align_named_args_in_map = true +ij_groovy_align_throws_keyword = true +ij_groovy_array_initializer_new_line_after_left_brace = true +ij_groovy_array_initializer_right_brace_on_new_line = true +ij_groovy_array_initializer_wrap = on_every_item +ij_groovy_assert_statement_wrap = off +ij_groovy_assignment_wrap = off +ij_groovy_binary_operation_wrap = off +ij_groovy_blank_lines_after_class_header = 1 +ij_groovy_blank_lines_after_imports = 1 +ij_groovy_blank_lines_after_package = 1 +ij_groovy_blank_lines_around_class = 1 +ij_groovy_blank_lines_around_field = 0 +ij_groovy_blank_lines_around_field_in_interface = 0 +ij_groovy_blank_lines_around_method = 1 +ij_groovy_blank_lines_around_method_in_interface = 1 +ij_groovy_blank_lines_before_imports = 1 +ij_groovy_blank_lines_before_method_body = 0 +ij_groovy_blank_lines_before_package = 0 +ij_groovy_block_brace_style = end_of_line +ij_groovy_block_comment_add_space = false +ij_groovy_block_comment_at_first_column = true +ij_groovy_call_parameters_new_line_after_left_paren = true +ij_groovy_call_parameters_right_paren_on_new_line = true +ij_groovy_call_parameters_wrap = on_every_item +ij_groovy_catch_on_new_line = false +ij_groovy_class_annotation_wrap = split_into_lines +ij_groovy_class_brace_style = end_of_line +ij_groovy_class_count_to_use_import_on_demand = 5 +ij_groovy_do_while_brace_force = never +ij_groovy_else_on_new_line = true +ij_groovy_enum_constants_wrap = on_every_item +ij_groovy_extends_keyword_wrap = normal +ij_groovy_extends_list_wrap = off +ij_groovy_field_annotation_wrap = split_into_lines +ij_groovy_finally_on_new_line = false +ij_groovy_for_brace_force = never +ij_groovy_for_statement_new_line_after_left_paren = true +ij_groovy_for_statement_right_paren_on_new_line = true +ij_groovy_for_statement_wrap = on_every_item +ij_groovy_if_brace_force = never +ij_groovy_import_annotation_wrap = 2 +ij_groovy_imports_layout = *,|,javax.**,java.**,|,$* +ij_groovy_indent_case_from_switch = true +ij_groovy_indent_label_blocks = true +ij_groovy_insert_inner_class_imports = false +ij_groovy_keep_blank_lines_before_right_brace = 2 +ij_groovy_keep_blank_lines_in_code = 2 +ij_groovy_keep_blank_lines_in_declarations = 2 +ij_groovy_keep_control_statement_in_one_line = true +ij_groovy_keep_first_column_comment = true +ij_groovy_keep_indents_on_empty_lines = true +ij_groovy_keep_line_breaks = true +ij_groovy_keep_multiple_expressions_in_one_line = false +ij_groovy_keep_simple_blocks_in_one_line = false +ij_groovy_keep_simple_classes_in_one_line = true +ij_groovy_keep_simple_lambdas_in_one_line = true +ij_groovy_keep_simple_methods_in_one_line = true +ij_groovy_label_indent_absolute = false +ij_groovy_label_indent_size = 0 +ij_groovy_lambda_brace_style = end_of_line +ij_groovy_layout_static_imports_separately = true +ij_groovy_line_comment_add_space = false +ij_groovy_line_comment_at_first_column = true +ij_groovy_method_annotation_wrap = split_into_lines +ij_groovy_method_brace_style = end_of_line +ij_groovy_method_call_chain_wrap = off +ij_groovy_method_parameters_new_line_after_left_paren = true +ij_groovy_method_parameters_right_paren_on_new_line = true +ij_groovy_method_parameters_wrap = on_every_item +ij_groovy_modifier_list_wrap = false +ij_groovy_names_count_to_use_import_on_demand = 3 +ij_groovy_packages_to_use_import_on_demand = java.awt.*,javax.swing.* +ij_groovy_parameter_annotation_wrap = normal +ij_groovy_parentheses_expression_new_line_after_left_paren = false +ij_groovy_parentheses_expression_right_paren_on_new_line = false +ij_groovy_prefer_parameters_wrap = false +ij_groovy_resource_list_new_line_after_left_paren = true +ij_groovy_resource_list_right_paren_on_new_line = true +ij_groovy_resource_list_wrap = on_every_item +ij_groovy_space_after_assert_separator = true +ij_groovy_space_after_colon = true +ij_groovy_space_after_comma = true +ij_groovy_space_after_comma_in_type_arguments = true +ij_groovy_space_after_for_semicolon = true +ij_groovy_space_after_quest = true +ij_groovy_space_after_type_cast = false +ij_groovy_space_before_annotation_parameter_list = false +ij_groovy_space_before_array_initializer_left_brace = false +ij_groovy_space_before_assert_separator = false +ij_groovy_space_before_catch_keyword = true +ij_groovy_space_before_catch_left_brace = true +ij_groovy_space_before_catch_parentheses = true +ij_groovy_space_before_class_left_brace = true +ij_groovy_space_before_closure_left_brace = true +ij_groovy_space_before_colon = true +ij_groovy_space_before_comma = false +ij_groovy_space_before_do_left_brace = true +ij_groovy_space_before_else_keyword = true +ij_groovy_space_before_else_left_brace = true +ij_groovy_space_before_finally_keyword = true +ij_groovy_space_before_finally_left_brace = true +ij_groovy_space_before_for_left_brace = true +ij_groovy_space_before_for_parentheses = true +ij_groovy_space_before_for_semicolon = false +ij_groovy_space_before_if_left_brace = true +ij_groovy_space_before_if_parentheses = true +ij_groovy_space_before_method_call_parentheses = false +ij_groovy_space_before_method_left_brace = true +ij_groovy_space_before_method_parentheses = true +ij_groovy_space_before_quest = true +ij_groovy_space_before_record_parentheses = false +ij_groovy_space_before_switch_left_brace = true +ij_groovy_space_before_switch_parentheses = true +ij_groovy_space_before_synchronized_left_brace = true +ij_groovy_space_before_synchronized_parentheses = true +ij_groovy_space_before_try_left_brace = true +ij_groovy_space_before_try_parentheses = true +ij_groovy_space_before_while_keyword = true +ij_groovy_space_before_while_left_brace = true +ij_groovy_space_before_while_parentheses = true +ij_groovy_space_in_named_argument = true +ij_groovy_space_in_named_argument_before_colon = false +ij_groovy_space_within_empty_array_initializer_braces = false +ij_groovy_space_within_empty_method_call_parentheses = false +ij_groovy_spaces_around_additive_operators = true +ij_groovy_spaces_around_assignment_operators = true +ij_groovy_spaces_around_bitwise_operators = true +ij_groovy_spaces_around_equality_operators = true +ij_groovy_spaces_around_lambda_arrow = true +ij_groovy_spaces_around_logical_operators = true +ij_groovy_spaces_around_multiplicative_operators = true +ij_groovy_spaces_around_regex_operators = true +ij_groovy_spaces_around_relational_operators = true +ij_groovy_spaces_around_shift_operators = true +ij_groovy_spaces_within_annotation_parentheses = false +ij_groovy_spaces_within_array_initializer_braces = false +ij_groovy_spaces_within_braces = true +ij_groovy_spaces_within_brackets = false +ij_groovy_spaces_within_cast_parentheses = false +ij_groovy_spaces_within_catch_parentheses = false +ij_groovy_spaces_within_for_parentheses = false +ij_groovy_spaces_within_gstring_injection_braces = false +ij_groovy_spaces_within_if_parentheses = false +ij_groovy_spaces_within_list_or_map = false +ij_groovy_spaces_within_method_call_parentheses = false +ij_groovy_spaces_within_method_parentheses = false +ij_groovy_spaces_within_parentheses = false +ij_groovy_spaces_within_switch_parentheses = false +ij_groovy_spaces_within_synchronized_parentheses = false +ij_groovy_spaces_within_try_parentheses = false +ij_groovy_spaces_within_tuple_expression = false +ij_groovy_spaces_within_while_parentheses = false +ij_groovy_special_else_if_treatment = true +ij_groovy_ternary_operation_wrap = normal +ij_groovy_throws_keyword_wrap = normal +ij_groovy_throws_list_wrap = off +ij_groovy_use_flying_geese_braces = false +ij_groovy_use_fq_class_names = false +ij_groovy_use_fq_class_names_in_javadoc = true +ij_groovy_use_relative_indents = false +ij_groovy_use_single_class_imports = true +ij_groovy_variable_annotation_wrap = normal +ij_groovy_while_brace_force = never +ij_groovy_while_on_new_line = false +ij_groovy_wrap_chain_calls_after_dot = false +ij_groovy_wrap_long_lines = false + +[{*.htm,*.html,*.ng,*.sht,*.shtm,*.shtml}] +ij_smart_tabs = true +ij_html_add_new_line_before_tags = body,div,p,form,h1,h2,h3 +ij_html_align_attributes = false +ij_html_align_text = false +ij_html_attribute_wrap = off +ij_html_block_comment_add_space = false +ij_html_block_comment_at_first_column = true +ij_html_do_not_align_children_of_min_lines = 0 +ij_html_do_not_break_if_inline_tags = title,h1,h2,h3,h4,h5,h6,p +ij_html_do_not_indent_children_of_tags = none +ij_html_enforce_quotes = false +ij_html_inline_tags = a,abbr,acronym,b,basefont,bdo,big,br,cite,cite,code,dfn,em,font,i,img,input,kbd,label,q,s,samp,select,small,span,strike,strong,sub,sup,textarea,tt,u,var +ij_html_keep_blank_lines = 0 +ij_html_keep_indents_on_empty_lines = true +ij_html_keep_line_breaks = false +ij_html_keep_line_breaks_in_text = true +ij_html_keep_whitespaces = false +ij_html_keep_whitespaces_inside = span,pre,textarea +ij_html_line_comment_at_first_column = true +ij_html_new_line_after_last_attribute = never +ij_html_new_line_before_first_attribute = never +ij_html_quote_style = double +ij_html_remove_new_line_before_tags = br +ij_html_space_after_tag_name = false +ij_html_space_around_equality_in_attribute = false +ij_html_space_inside_empty_tag = true +ij_html_text_wrap = off + +[{*.markdown,*.md}] +ij_smart_tabs = true +ij_markdown_force_one_space_after_blockquote_symbol = true +ij_markdown_force_one_space_after_header_symbol = true +ij_markdown_force_one_space_after_list_bullet = true +ij_markdown_force_one_space_between_words = true +ij_markdown_keep_indents_on_empty_lines = true +ij_markdown_max_lines_around_block_elements = 1 +ij_markdown_max_lines_around_header = 1 +ij_markdown_max_lines_between_paragraphs = 1 +ij_markdown_min_lines_around_block_elements = 1 +ij_markdown_min_lines_around_header = 1 +ij_markdown_min_lines_between_paragraphs = 1 + +[{*.properties,spring.handlers,spring.schemas}] +ij_properties_align_group_field_declarations = false +ij_properties_keep_blank_lines = true +ij_properties_key_value_delimiter = equals +ij_properties_spaces_around_key_value_delimiter = true + diff --git a/gradle.properties b/gradle.properties index 2d2e5dc..7709178 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.4 +VERSION = 0.4.2.5 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 3a8381d..f4479f2 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.4"; - public static final long COMPILE_TIMESTAMP = 1639736122490L; + public static final String VERSION = "0.4.2.5"; + public static final long COMPILE_TIMESTAMP = 1639909473042L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java index 18e91a0..6ed80af 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java @@ -54,11 +54,20 @@ public class GetUsernameAndId { userInformation.append(String.format( """ userid : - - %d - username : - - %s""", - userId, user.username() + - %d""", + userId )); + if (user.username() == null) { + userInformation.append("\nusername : null"); + } else { + userInformation.append(String.format( + """ + + username : + - %s""", + user.username() + )); + } if (user.firstName() == null) { userInformation.append("\nfirstname : null"); } else { From 6a5a23b6beb9fca97ee2f91701abbff7df35957c Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 21 Dec 2021 13:53:51 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=8A=AB=E6=8C=81=E5=A4=84=E7=90=86=E4=B8=8E=E8=BE=93=E5=87=BA?= =?UTF-8?q?=EF=BC=8C=E6=89=A9=E5=85=85=E8=B6=85=E6=97=B6=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E5=BF=BD=E7=95=A5=E7=9A=84=E4=BD=9C=E7=94=A8=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/event/EventListeners.java | 4 +- .../morny/bot/event/OnEventHackHandle.java | 128 +++++++++++++++++- .../event/OnUpdateTimestampOffsetLock.java | 31 +++++ .../bot/event/on_commands/EventHack.java | 9 +- 6 files changed, 165 insertions(+), 13 deletions(-) diff --git a/gradle.properties b/gradle.properties index 7709178..38fb156 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.5 +VERSION = 0.4.2.6 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index f4479f2..5b30b66 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.5"; - public static final long COMPILE_TIMESTAMP = 1639909473042L; + public static final String VERSION = "0.4.2.6"; + public static final long COMPILE_TIMESTAMP = 1640065256004L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java index 47e4699..716b99d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java @@ -10,6 +10,7 @@ public class EventListeners { public static final OnUpdateTimestampOffsetLock UPDATE_TIMESTAMP_OFFSET_LOCK = new OnUpdateTimestampOffsetLock(); public static final OnInlineQuery INLINE_QUERY = new OnInlineQuery(); public static final OnCallMe CALL_ME = new OnCallMe(); + public static final OnEventHackHandle EVENT_HACK_HANDLE = new OnEventHackHandle(); public static void registerAllListeners () { EventListenerManager.addListener( @@ -18,7 +19,8 @@ public class EventListeners { COMMANDS_LISTENER, USER_SLASH_ACTION, INLINE_QUERY, - CALL_ME + CALL_ME, + EVENT_HACK_HANDLE ); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java index 3b5f1bc..ffabd4e 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java @@ -1,21 +1,143 @@ package cc.sukazyo.cono.morny.bot.event; +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.bot.api.EventListener; +import com.google.gson.GsonBuilder; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.SendMessage; + +import javax.annotation.Nonnull; +import java.util.HashMap; +import java.util.Map; + +import static cc.sukazyo.cono.morny.Log.logger; + /** + * 事件劫持与序列化工具. * @since 0.4.2.0 */ -public class OnEventHackHandle { +public class OnEventHackHandle extends EventListener { + + /** 事件劫持请求列表 */ + private static final Map hackers = new HashMap<>(); /** + * 触发事件劫持的限定条件. * @since 0.4.2.0 */ public enum HackType { - USER, GROUP, ANY + /** 只有相同用户发起的事件才会被触发 */ + USER, + /** 只有相同群组内发生的事件才会触发 */ + GROUP, + /** 任何事件都可以触发 */ + ANY + } + + public record Hacker(long fromChatId, long fromMessageId) { + @Override public String toString() { + return fromChatId + "/" + fromMessageId; + } } /** * @since 0.4.2.0 */ - public static void registerHack(long fromMessageId, long fromChatId, long fromUserId,HackType type) { + public static void registerHack(long fromMessageId, long fromUserId, long fromChatId, @Nonnull HackType type) { + String rec = null; + switch (type) { + case USER -> rec = String.format("((%d))", fromUserId); + case GROUP -> rec = String.format("{{%d}}", fromChatId); + case ANY -> rec = "[[]]"; + } + hackers.put(rec, new Hacker(fromChatId, fromMessageId)); + logger.debug("add hacker track " + rec); + } + + private boolean onEventHacked (Update update, long chatId, long fromUser) { + logger.debug(String.format("try hack {{%d}}((%d))", chatId, fromUser)); + Hacker x; + x = hackers.remove(String.format("((%d))", fromUser)); + if (x == null) x = hackers.remove(String.format("{{%d}}", chatId)); + if (x == null) x = hackers.remove("[[]]"); + if (x == null) return false; + logger.debug("hacked event by " + x); + MornyCoeur.getAccount().execute(new SendMessage(x.fromChatId, String.format( + "%s", + new GsonBuilder().setPrettyPrinting().create().toJson(update) + )).parseMode(ParseMode.HTML).replyToMessageId((int)x.fromMessageId)); + return true; + } + + @Override + public boolean onMessage (@Nonnull Update update) { + return onEventHacked(update, update.message().chat().id(), update.message().from().id()); + } + + @Override + public boolean onEditedMessage (@Nonnull Update update) { + return onEventHacked(update, update.editedMessage().chat().id(), update.editedMessage().from().id()); + } + + @Override + public boolean onChannelPost (@Nonnull Update update) { + return onEventHacked(update, update.channelPost().chat().id(), update.channelPost().from().id()); + } + + @Override + public boolean onEditedChannelPost (@Nonnull Update update) { + return onEventHacked(update, update.editedChannelPost().chat().id(), update.editedChannelPost().from().id()); + } + + @Override + public boolean onInlineQuery (@Nonnull Update update) { + return onEventHacked(update, 0, update.inlineQuery().from().id()); + } + + @Override + public boolean onChosenInlineResult (@Nonnull Update update) { + return onEventHacked(update, 0, update.chosenInlineResult().from().id()); + } + + @Override + public boolean onCallbackQuery (@Nonnull Update update) { + return onEventHacked(update, 0, update.callbackQuery().from().id()); + } + + @Override + public boolean onShippingQuery (@Nonnull Update update) { + return onEventHacked(update, 0, update.shippingQuery().from().id()); + } + + @Override + public boolean onPreCheckoutQuery (@Nonnull Update update) { + return onEventHacked(update, 0, update.preCheckoutQuery().from().id()); + } + + @Override + public boolean onPoll (@Nonnull Update update) { + return onEventHacked(update, 0, 0); + } + + @Override + public boolean onPollAnswer (@Nonnull Update update) { + return onEventHacked(update, 0, update.pollAnswer().user().id()); + } + + @Override + public boolean onMyChatMemberUpdated (@Nonnull Update update) { + return onEventHacked(update, update.myChatMember().chat().id(), update.myChatMember().from().id()); + } + + @Override + public boolean onChatMemberUpdated (@Nonnull Update update) { + return onEventHacked(update, update.chatMember().chat().id(), update.chatMember().from().id()); + } + + @Override + public boolean onChatJoinRequest (@Nonnull Update update) { + return onEventHacked(update, update.chatJoinRequest().chat().id(), update.chatJoinRequest().from().id()); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java index 7130277..3f7068b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java @@ -5,6 +5,19 @@ import cc.sukazyo.cono.morny.bot.api.EventListener; import com.pengrad.telegrambot.model.Update; import org.jetbrains.annotations.NotNull; +import javax.annotation.Nonnull; + +/** + * 阻止 {@link MornyCoeur#latestEventTimestamp 指定时间} 之前的事件处理. + *

    + * 只支持以下事件 + *

      + *
    • {@link EventListener#onMessage(Update) 收到消息}
    • + *
    • {@link EventListener#onEditedMessage(Update) 消息被更新}
    • + *
    • {@link EventListener#onChannelPost(Update) 收到频道消息}
    • + *
    • {@link EventListener#onEditedChannelPost(Update) 频道消息被更新}
    • + *
    + */ public class OnUpdateTimestampOffsetLock extends EventListener { @Override @@ -12,4 +25,22 @@ public class OnUpdateTimestampOffsetLock extends EventListener { return update.message().date() < MornyCoeur.latestEventTimestamp/1000; } + /** @since 0.4.2.6 */ + @Override + public boolean onEditedMessage (@Nonnull Update update) { + return update.editedMessage().editDate() < MornyCoeur.latestEventTimestamp/1000; + } + + /** @since 0.4.2.6 */ + @Override + public boolean onChannelPost (@Nonnull Update update) { + return update.channelPost().date() < MornyCoeur.latestEventTimestamp/1000; + } + + /** @since 0.4.2.6 */ + @Override + public boolean onEditedChannelPost (@Nonnull Update update) { + return update.editedChannelPost().editDate() < MornyCoeur.latestEventTimestamp/1000; + } + } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java index 0df037a..94ad6cd 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java @@ -42,8 +42,7 @@ public class EventHack { event.message().from().id(), event.message().chat().id(), OnEventHackHandle.HackType.ANY - ); - isOk = true; + );isOk = true; } break; case "group": @@ -52,8 +51,7 @@ public class EventHack { event.message().from().id(), event.message().chat().id(), OnEventHackHandle.HackType.GROUP - ); - isOk = true; + );isOk = true; break; default: OnEventHackHandle.registerHack( @@ -61,8 +59,7 @@ public class EventHack { event.message().from().id(), event.message().chat().id(), OnEventHackHandle.HackType.USER - ); - isOk = true; + );isOk = true; break; } From def7ee79c25b4d76581fe6e54003f22c40bc63c7 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 22 Dec 2021 00:43:56 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E4=BD=BF=20MornyCoeur=20=E5=92=8C=20Morn?= =?UTF-8?q?yTrusted=20=E5=AF=B9=E8=B1=A1=E5=B0=81=E8=A3=85=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20master=20=E5=92=8C=20trustedChat=20?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cc/sukazyo/cono/morny/MornyCoeur.java | 119 ++++++++++++++---- .../cc/sukazyo/cono/morny/MornyTrusted.java | 11 +- .../cc/sukazyo/cono/morny/ServerMain.java | 16 ++- .../cono/morny/bot/event/OnCallMe.java | 2 +- .../morny/bot/event/OnCommandExecute.java | 3 +- .../event/OnUpdateTimestampOffsetLock.java | 19 ++- .../bot/event/on_commands/EventHack.java | 2 +- 9 files changed, 135 insertions(+), 43 deletions(-) diff --git a/gradle.properties b/gradle.properties index 38fb156..a3de3a3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.6 +VERSION = 0.4.2.7 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 5b30b66..f7af85d 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.6"; - public static final long COMPILE_TIMESTAMP = 1640065256004L; + public static final String VERSION = "0.4.2.7"; + public static final long COMPILE_TIMESTAMP = 1640104964102L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index c6fdf0d..06d7944 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -17,8 +17,14 @@ import static cc.sukazyo.cono.morny.Log.logger; */ public class MornyCoeur { + /** 当前程序的 Morny Coeur 实例 */ + private static MornyCoeur INSTANCE; + + /** 当前 Morny 的{@link MornyTrusted 信任验证机}实例 */ + private final MornyTrusted trusted; + /** morny 的 bot 账户 */ - private static TelegramBot account; + private final TelegramBot account; /** * morny 的 bot 账户的用户名
    *
    @@ -28,36 +34,40 @@ public class MornyCoeur { * 如果在登陆之前就定义了此字段,则登陆代码会验证登陆的 bot 的 username * 是否与定义的 username 符合。如果不符合则会报错。 */ - private static String username; + private final String username; /** * morny 的事件忽略前缀时间
    *
    * {@link cc.sukazyo.cono.morny.bot.event.OnUpdateTimestampOffsetLock} * 会根据这里定义的时间戳取消掉比此时间更早的事件链 */ - public static long latestEventTimestamp; + public long latestEventTimestamp; /** * morny 主程序启动时间
    * 用于统计数据 */ public static final long coeurStartTimestamp = System.currentTimeMillis(); + private record LogInResult(TelegramBot account, String username) { } + /** - * bot 启动入口,执行 bot 初始化 + * 执行 bot 初始化 * * @param botKey bot 的 telegram bot api token * @param botUsername bot 的 username 限定。如果为 null 则表示不限定, * 如果指定,则登录时会检查所登陆的 bot 的用户名是否与此相等 + * @param master morny 实例所信任的主人的 id。用于初始化 {@link #trusted} + * @param trustedChat morny 实例所信任的群组的 id。用于初始化 {@link #trusted} * @param latestEventTimestamp 事件处理器会处理事件的最早时间戳 —— * 只有限定的 message 事件会受此影响。 * 单位为毫秒 */ - public static void main (@Nonnull String botKey, @Nullable String botUsername, long latestEventTimestamp) { - - MornyCoeur.latestEventTimestamp = latestEventTimestamp; - - logger.info("System Starting"); + private MornyCoeur ( + @Nonnull String botKey, @Nullable String botUsername, + long master, long trustedChat, long latestEventTimestamp + ) { + this.latestEventTimestamp = latestEventTimestamp; configureSafeExit(); logger.info("args key:\n " + botKey); @@ -65,23 +75,57 @@ public class MornyCoeur { logger.info("login as:\n " + botUsername); } - try { account = login(botKey); } - catch (Exception e) { logger.error("Cannot login to bot/api. :\n " + e.getMessage()); System.exit(-1); } + try { + final LogInResult loginResult = login(botKey); + this.account = loginResult.account; + this.username = loginResult.username; + this.trusted = new MornyTrusted(master, trustedChat); + logger.info(String.format(""" + trusted param set: + - master (id) + %d + - trusted chat (id) + %d""", + master, trustedChat + )); + } + catch (Exception e) { + RuntimeException ex = new RuntimeException("Cannot login to bot/api. :\n " + e.getMessage()); + logger.error(ex.getMessage()); + throw ex; + } logger.info("Bot login succeed."); - TrackerDataManager.init(); - EventListeners.registerAllListeners(); - account.setUpdatesListener(OnUpdate::onNormalUpdate); - - logger.info("System start complete"); - + } + + /** + * 向外界暴露的 morny 初始化入口. + *

    + * 如果 morny 已经初始化,则不会进行初始化,抛出错误消息并直接退出方法。 + * + * @see #MornyCoeur 程序初始化方法 + */ + public static void main ( + @Nonnull String botKey, @Nullable String botUsername, + long master, long trustedChat, long latestEventTimestamp + ) { + if (INSTANCE == null) { + logger.info("System Starting"); + INSTANCE = new MornyCoeur(botKey, botUsername, master, trustedChat, latestEventTimestamp); + TrackerDataManager.init(); + EventListeners.registerAllListeners(); + INSTANCE.account.setUpdatesListener(OnUpdate::onNormalUpdate); + logger.info("System start complete"); + return; + } + logger.error("System already started coeur!!!"); } /** * 用于退出时进行缓存的任务处理等进行安全退出 */ - private static void exitCleanup () { + private void exitCleanup () { TrackerDataManager.DAEMON.interrupt(); TrackerDataManager.trackingLock.lock(); } @@ -89,8 +133,8 @@ public class MornyCoeur { /** * 为程序在虚拟机上添加退出钩子 */ - private static void configureSafeExit () { - Runtime.getRuntime().addShutdownHook(new Thread(MornyCoeur::exitCleanup)); + private void configureSafeExit () { + Runtime.getRuntime().addShutdownHook(new Thread(this::exitCleanup)); } /** @@ -104,18 +148,17 @@ public class MornyCoeur { * @return 成功登录后的 {@link TelegramBot} 对象 */ @Nonnull - public static TelegramBot login (@Nonnull String key) { + private LogInResult login (@Nonnull String key) { final TelegramBot account = new TelegramBot(key); logger.info("Trying to login..."); for (int i = 1; i < 4; i++) { if (i != 1) logger.info("retrying..."); try { final String username = account.execute(new GetMe()).user().username(); - if (MornyCoeur.username != null && !MornyCoeur.username.equals(username)) - throw new RuntimeException("Required the bot @" + MornyCoeur.username + " but @" + username + " logged in!"); - else MornyCoeur.username = username; + if (this.username != null && !this.username.equals(username)) + throw new RuntimeException("Required the bot @" + this.username + " but @" + username + " logged in!"); logger.info("Succeed login to @" + username); - return account; + return new LogInResult(account, username); } catch (Exception e) { e.printStackTrace(System.out); logger.error("login failed."); @@ -129,8 +172,9 @@ public class MornyCoeur { * * @return {@link #account MornyCoeur.account} */ + @Nonnull public static TelegramBot getAccount () { - return account; + return INSTANCE.account; } /** @@ -138,8 +182,29 @@ public class MornyCoeur { * * @return {@link #username MornyCoeur.username} */ + @Nonnull public static String getUsername () { - return username; + return INSTANCE.username; + } + + /** + * + * 获取忽略时间点 + * + * @return {@link #latestEventTimestamp MornyCoeur.latestEventTimestamp} + */ + public static long getLatestEventTimestamp () { + return INSTANCE.latestEventTimestamp; + } + + /** + * 获取 Morny 的{@link MornyTrusted 信任验证机} + * + * @return {@link #trusted MornyCoeur.trusted} + */ + @Nonnull + public static MornyTrusted trustedInstance () { + return INSTANCE.trusted; } } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java b/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java index c2a07de..f892b9d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java @@ -12,13 +12,18 @@ public class MornyTrusted { * 群聊id,其指向的群聊指示了哪个群的成员是受信任的 * @see #isTrusted(long) 受信检查 */ - public static final long TRUSTED_CHAT_ID = -1001541451710L; + public final Long TRUSTED_CHAT_ID; /** * morny 的主人
    * 这项值的对象总是会被认为是可信任的 */ - public static final long MASTER = 793274677L; + public final long MASTER; + + public MornyTrusted (long master, long trustedChatId) { + this.TRUSTED_CHAT_ID = trustedChatId; + this.MASTER = master; + } /** * 用于检查一个 telegram-user 是否受信任
    @@ -30,7 +35,7 @@ public class MornyTrusted { * @param userId 需要检查的用户的id * @return 所传递的用户id对应的用户是否受信任 */ - public static boolean isTrusted (long userId) { + public boolean isTrusted (long userId) { if (userId == MASTER) return true; final ChatMember chatMember = MornyCoeur.getAccount().execute(new GetChatMember(TRUSTED_CHAT_ID, userId)).chatMember(); return ( diff --git a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java index 78f1702..fcbe489 100644 --- a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java +++ b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java @@ -59,7 +59,7 @@ public class ServerMain { * 但实际上这并不影响现在的使用,选项赋值目前仍属于测试功能
    * 但请勿混用,这将使两个赋值出现混淆并产生不可知的结果 * - * @see MornyCoeur#main(String, String, long) + * @see MornyCoeur#main(String, String, long, long, long) * @since 0.4.0.0 * @param args 参数组 */ @@ -68,6 +68,8 @@ public class ServerMain { String key = null; String username = null; boolean outdatedBlock = false; + long master = 793274677L; + long trustedChat = -1001541451710L; for (int i = 0; i < args.length; i++) { @@ -100,6 +102,16 @@ public class ServerMain { username = args[i]; continue; } + case "--master" -> { + i++; + master = Long.parseLong(args[i]); + continue; + } + case "--trusted-chat" -> { + i++; + trustedChat = Long.parseLong(args[i]); + continue; + } } } else { @@ -143,7 +155,7 @@ public class ServerMain { if (welcomeEchoMode) return; assert key != null; - MornyCoeur.main(key, username, outdatedBlock?System.currentTimeMillis():0); + MornyCoeur.main(key, username, master, trustedChat, outdatedBlock?System.currentTimeMillis():0); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java index c1c14e0..6d10041 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -23,7 +23,7 @@ public class OnCallMe extends EventListener { * 跟随 {@link MornyTrusted#MASTER} 的值 * @since 0.4.2.1 */ - private static final long ME = MornyTrusted.MASTER; + private static final long ME = MornyCoeur.trustedInstance().MASTER; /** * 监听私聊 bot 的消息进行呼叫关键字匹配。 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 5f5da75..3090192 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 @@ -3,7 +3,6 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.GradleProjectConfigures; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornySystem; -import cc.sukazyo.cono.morny.MornyTrusted; 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; @@ -88,7 +87,7 @@ public class OnCommandExecute extends EventListener { } private void onCommandExitExec (@Nonnull Update event) { - if (MornyTrusted.isTrusted(event.message().from().id())) { + if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { MornyCoeur.getAccount().execute(new SendSticker( event.message().chat().id(), TelegramStickers.ID_EXIT diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java index 3f7068b..4b96284 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java @@ -17,30 +17,41 @@ import javax.annotation.Nonnull; *

  • {@link EventListener#onChannelPost(Update) 收到频道消息}
  • *
  • {@link EventListener#onEditedChannelPost(Update) 频道消息被更新}
  • * + * @see #isOutdated 时间判断 */ public class OnUpdateTimestampOffsetLock extends EventListener { + /** + * 检查传入时间是否在要求时间之前(即"过期"). + * @param timestamp 传入时间,秒级 + * @return 如果传入时间在要求时间之前,返回true,反之false + * @since 0.4.2.7 + */ + public boolean isOutdated(long timestamp) { + return timestamp < MornyCoeur.getLatestEventTimestamp()/1000; + } + @Override public boolean onMessage (@NotNull Update update) { - return update.message().date() < MornyCoeur.latestEventTimestamp/1000; + return isOutdated(update.message().date()); } /** @since 0.4.2.6 */ @Override public boolean onEditedMessage (@Nonnull Update update) { - return update.editedMessage().editDate() < MornyCoeur.latestEventTimestamp/1000; + return isOutdated(update.editedMessage().editDate()); } /** @since 0.4.2.6 */ @Override public boolean onChannelPost (@Nonnull Update update) { - return update.channelPost().date() < MornyCoeur.latestEventTimestamp/1000; + return isOutdated(update.channelPost().date()); } /** @since 0.4.2.6 */ @Override public boolean onEditedChannelPost (@Nonnull Update update) { - return update.editedChannelPost().editDate() < MornyCoeur.latestEventTimestamp/1000; + return isOutdated(update.editedChannelPost().editDate()); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java index 94ad6cd..2146535 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/EventHack.java @@ -36,7 +36,7 @@ public class EventHack { switch (x_mode) { case "any": - if (MornyTrusted.isTrusted(event.message().from().id())) { + if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { OnEventHackHandle.registerHack( event.message().messageId(), event.message().from().id(), From a6040a681835eb66541d857a9b5e4ea4b913e09e Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 22 Dec 2021 14:19:07 +0800 Subject: [PATCH 09/12] =?UTF-8?q?/runtime=20=E7=9A=84=20continuous=20?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E6=B7=BB=E5=8A=A0=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E7=9A=84=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cc/sukazyo/cono/morny/GradleProjectConfigures.java | 4 ++-- .../cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java | 8 ++++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/gradle.properties b/gradle.properties index a3de3a3..6ae3faa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.7 +VERSION = 0.4.2.8 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index f7af85d..a708d13 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.7"; - public static final long COMPILE_TIMESTAMP = 1640104964102L; + public static final String VERSION = "0.4.2.8"; + public static final long COMPILE_TIMESTAMP = 1640153916855L; } 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 3090192..5c4bee4 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 @@ -145,8 +145,10 @@ public class OnCommandExecute extends EventListener { - %s - %s [UTC] - [%d] - continuous + continuous: - %s + - [%d] + - %s [UTC] - [%d]""", // system System.getProperty("os.name"), @@ -165,7 +167,9 @@ public class OnCommandExecute extends EventListener { GradleProjectConfigures.COMPILE_TIMESTAMP, // continuous CommonFormatUtils.formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp), - System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp + System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp, + CommonFormatUtils.formatDate(MornyCoeur.coeurStartTimestamp, 0), + MornyCoeur.coeurStartTimestamp ) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } From bd29b4355e5aabd7ad810606e1b8ddf173634365 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 25 Dec 2021 02:50:30 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AE=80=E5=8D=95?= =?UTF-8?q?=E7=9A=84=20jrrp=20(#13)=EF=BC=8C=E5=B0=86=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=9A=84=20NotNull=20=E6=94=B9=E4=B8=BA=20Nonnull?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 ++-- .../cono/morny/bot/event/OnCallMe.java | 5 ++-- .../morny/bot/event/OnCommandExecute.java | 17 ++++++++++++++ .../cono/morny/bot/event/OnInlineQuery.java | 5 ++-- .../event/OnUpdateTimestampOffsetLock.java | 3 +-- .../cc/sukazyo/cono/morny/data/MornyJrrp.java | 23 +++++++++++++++++++ .../sukazyo/cono/morny/util/EncryptUtils.java | 5 ++-- 8 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java diff --git a/gradle.properties b/gradle.properties index 6ae3faa..f8bd912 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.8 +VERSION = 0.4.2.9 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index a708d13..a2cdfd5 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.8"; - public static final long COMPILE_TIMESTAMP = 1640153916855L; + public static final String VERSION = "0.4.2.9"; + public static final long COMPILE_TIMESTAMP = 1640371604243L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java index 6d10041..911cb4f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -10,7 +10,8 @@ import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.ForwardMessage; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.request.SendSticker; -import org.jetbrains.annotations.NotNull; + +import javax.annotation.Nonnull; /** * 通过 bot 呼叫主人的事件监听管理类 @@ -34,7 +35,7 @@ public class OnCallMe extends EventListener { * 如果匹配到呼叫,则返回{@code true},反之返回{@code false} */ @Override - public boolean onMessage (@NotNull Update update) { + public boolean onMessage (@Nonnull Update update) { if (update.message().text() == null) return false; if (update.message().chat().type() != Chat.Type.Private) 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 5c4bee4..8b9d052 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.data.MornyJrrp; import cc.sukazyo.cono.morny.data.TelegramStickers; import cc.sukazyo.cono.morny.util.CommonFormatUtils; import com.pengrad.telegrambot.model.Update; @@ -52,6 +53,9 @@ public class OnCommandExecute extends EventListener { case "/runtime": onCommandRuntimeExec(event); break; + case "/jrrp": + onCommandJrrpExec(event); + break; default: return nonCommandExecutable(event, command); } @@ -174,4 +178,17 @@ public class OnCommandExecute extends EventListener { ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } + private void onCommandJrrpExec (Update event) { + final double jrrp = MornyJrrp.getJrrpFromTelegramUser(event.message().from(), System.currentTimeMillis()); + final String endChar = jrrp>70 ? "!" : jrrp>30 ? ";" : "..."; + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + String.format( + "%s 在(utc的)今天的运气指数是———— %.2f%% %s", + event.message().from().id(), event.message().from().firstName(), + jrrp, endChar + ) + ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + } + } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java index 5dfd54e..1867887 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java @@ -8,7 +8,8 @@ import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; import com.pengrad.telegrambot.model.request.InputTextMessageContent; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.AnswerInlineQuery; -import org.jetbrains.annotations.NotNull; + +import javax.annotation.Nonnull; /** * telegram inlineQuery 功能的处理类, @@ -22,7 +23,7 @@ public class OnInlineQuery extends EventListener { * @since 0.4.1.3 */ @Override - public boolean onInlineQuery (@NotNull Update update) { + public boolean onInlineQuery (@Nonnull Update update) { MornyCoeur.getAccount().execute(new AnswerInlineQuery(update.inlineQuery().id(), new InlineQueryResultArticle[]{ new InlineQueryResultArticle( EncryptUtils.encryptByMD5(update.inlineQuery().query()), diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java index 4b96284..80eff7f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUpdateTimestampOffsetLock.java @@ -3,7 +3,6 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; import com.pengrad.telegrambot.model.Update; -import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; @@ -32,7 +31,7 @@ public class OnUpdateTimestampOffsetLock extends EventListener { } @Override - public boolean onMessage (@NotNull Update update) { + public boolean onMessage (@Nonnull Update update) { return isOutdated(update.message().date()); } diff --git a/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java b/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java new file mode 100644 index 0000000..eb80f66 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java @@ -0,0 +1,23 @@ +package cc.sukazyo.cono.morny.data; + +import cc.sukazyo.cono.morny.util.EncryptUtils; +import com.pengrad.telegrambot.model.User; + +/** + * Morny 的 jrrp 运算类. + * + * @see #getJrrpFromTelegramUser(User,long) + * @see #calcJrrpXmomi(long,long) + * @since 0.4.2.9 + */ +public class MornyJrrp { + + public static double getJrrpFromTelegramUser (User user, long timestamp) { + return calcJrrpXmomi(user.id(), timestamp / (1000 * 60 * 60 * 24)) * 100.0; + } + + public static double calcJrrpXmomi (long userId, long dayStamp) { + return (double)Long.parseLong(EncryptUtils.encryptByMD5(userId + "@" + dayStamp).substring(0, 4), 16) / (double)0xffff; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java index 6d502f9..243389a 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java @@ -1,5 +1,6 @@ package cc.sukazyo.cono.morny.util; +import javax.annotation.Nonnull; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -18,6 +19,7 @@ public class EncryptUtils { /*** * 对指定的字符串进行MD5加密 */ + @Nonnull public static String encryptByMD5(String originString) { try { //创建具有MD5算法的信息摘要 @@ -28,9 +30,8 @@ public class EncryptUtils { String s = byteArrayToHex(bytes); return s.toUpperCase(); } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); + throw new IllegalStateException(); } - return null; } /** From e548dd1537972443fdcd6f918b7616830a349605 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 25 Dec 2021 20:07:46 +0800 Subject: [PATCH 11/12] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20/ip=20/whois=20?= =?UTF-8?q?=E5=92=8C=20186ip=20=E6=9F=A5=E8=AF=A2=20(#17=20@186526)?= =?UTF-8?q?=EF=BC=8C=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) { +} From e808a37fb2537cc4030e736de3ddd8160a2010e7 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 27 Dec 2021 17:01:17 +0800 Subject: [PATCH 12/12] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20tgapi.HTML=20?= =?UTF-8?q?=E7=BC=96=E7=A0=81=E5=99=A8=E5=B9=B6=E8=A1=A5=E5=85=A8=E8=B0=83?= =?UTF-8?q?=E7=94=A8=20(#19)=EF=BC=8C=E8=A1=A5=E5=85=A8=20IP186/javadoc=20?= =?UTF-8?q?(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修订 Telegram HTML 实体转义 (#19 - 为所有 HTML 内嵌输入补全转义代码 - 移除了 apache-commons-text 类库 - 添加了自己的 HTML 实体转义类 - 补充 IP186(#17) 的 javadoc (#18 --- build.gradle | 1 - gradle.properties | 3 +- .../cono/morny/GradleProjectConfigures.java | 4 +-- .../cono/morny/bot/event/OnCallMe.java | 23 ++++++++---- .../morny/bot/event/OnCommandExecute.java | 36 ++++++++++--------- .../morny/bot/event/OnEventHackHandle.java | 3 +- .../morny/bot/event/OnUserSlashAction.java | 12 ++++--- .../event/on_commands/GetUsernameAndId.java | 10 +++--- .../bot/event/on_commands/Ip186Query.java | 11 ++++-- .../cc/sukazyo/cono/morny/data/MornyJrrp.java | 22 ++++++++++++ .../morny/data/ip186/IP186QueryHandler.java | 35 ++++++++++++++++++ .../morny/data/ip186/IP186QueryResponse.java | 7 ++++ .../sukazyo/cono/morny/util/StringUtils.java | 16 ++++----- 13 files changed, 135 insertions(+), 48 deletions(-) diff --git a/build.gradle b/build.gradle index 5dc0fd9..2b36477 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,6 @@ 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 9690518..f697b89 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,11 +1,10 @@ ## Core -VERSION = 0.4.2.10 +VERSION = 0.4.2.11 # dependencies 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 b810d23..4760921 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.10"; - public static final long COMPILE_TIMESTAMP = 1640434011159L; + public static final String VERSION = "0.4.2.11"; + public static final long COMPILE_TIMESTAMP = 1640595623685L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java index 911cb4f..f105b9e 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java @@ -13,6 +13,8 @@ import com.pengrad.telegrambot.request.SendSticker; import javax.annotation.Nonnull; +import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; + /** * 通过 bot 呼叫主人的事件监听管理类 * @since 0.4.2.1 @@ -69,11 +71,14 @@ public class OnCallMe extends EventListener { */ private static void requestSteamJoin (Update event) { MornyCoeur.getAccount().execute(new SendMessage( - ME, String.format(""" + ME, String.format( + """ request STEAM LIBRARY from %s""", event.message().from().id(), - event.message().from().firstName() + " " + event.message().from().lastName() + escapeHtmlTelegram( + event.message().from().firstName() + " " + event.message().from().lastName() + ) ) ).parseMode(ParseMode.HTML)); } @@ -86,11 +91,14 @@ public class OnCallMe extends EventListener { */ private static void requestHanaParesuJoin (Update event) { MornyCoeur.getAccount().execute(new SendMessage( - ME, String.format(""" + ME, String.format( + """ request Hana Paresu from %s""", event.message().from().id(), - event.message().from().firstName() + " " + event.message().from().lastName() + escapeHtmlTelegram( + event.message().from().firstName() + " " + event.message().from().lastName() + ) ) ).parseMode(ParseMode.HTML)); } @@ -111,11 +119,14 @@ public class OnCallMe extends EventListener { */ private static void requestCustomCall (Update event) { MornyCoeur.getAccount().execute(new SendMessage( - ME, String.format(""" + ME, String.format( + """ request [???] from %s""", event.message().from().id(), - event.message().from().firstName() + " " + event.message().from().lastName() + escapeHtmlTelegram( + event.message().from().firstName() + " " + event.message().from().lastName() + ) ) ).parseMode(ParseMode.HTML)); MornyCoeur.getAccount().execute(new ForwardMessage( 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 486430f..a49d884 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 @@ -10,7 +10,6 @@ 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; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; @@ -19,6 +18,9 @@ import com.pengrad.telegrambot.request.SendSticker; import javax.annotation.Nonnull; import static cc.sukazyo.cono.morny.Log.logger; +import static cc.sukazyo.cono.morny.util.CommonFormatUtils.formatDate; +import static cc.sukazyo.cono.morny.util.CommonFormatUtils.formatDuration; +import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; public class OnCommandExecute extends EventListener { @@ -117,7 +119,8 @@ public class OnCommandExecute extends EventListener { private void onCommandVersionExec (@Nonnull Update event) { MornyCoeur.getAccount().execute(new SendMessage( event.message().chat().id(), - String.format(""" + String.format( + """ version: - %s core md5_hash: @@ -125,10 +128,10 @@ public class OnCommandExecute extends EventListener { compile timestamp: - %d - %s [UTC]""", - MornySystem.VERSION, - MornySystem.getJarMd5(), + escapeHtmlTelegram(MornySystem.VERSION), + escapeHtmlTelegram(MornySystem.getJarMd5()), GradleProjectConfigures.COMPILE_TIMESTAMP, - CommonFormatUtils.formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0) + escapeHtmlTelegram(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)) ) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } @@ -160,24 +163,24 @@ public class OnCommandExecute extends EventListener { - %s [UTC] - [%d]""", // system - System.getProperty("os.name"), - System.getProperty("os.version"), + escapeHtmlTelegram(System.getProperty("os.name")), + escapeHtmlTelegram(System.getProperty("os.version")), Runtime.getRuntime().availableProcessors(), // java - System.getProperty("java.vm.name"), - System.getProperty("java.version"), + escapeHtmlTelegram(System.getProperty("java.vm.name")), + escapeHtmlTelegram(System.getProperty("java.version")), // memory Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().maxMemory() / 1024 / 1024, // version - MornySystem.VERSION, - MornySystem.getJarMd5(), - CommonFormatUtils.formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0), + escapeHtmlTelegram(MornySystem.VERSION), + escapeHtmlTelegram(MornySystem.getJarMd5()), + escapeHtmlTelegram(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)), GradleProjectConfigures.COMPILE_TIMESTAMP, // continuous - CommonFormatUtils.formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp), + escapeHtmlTelegram(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)), System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp, - CommonFormatUtils.formatDate(MornyCoeur.coeurStartTimestamp, 0), + escapeHtmlTelegram(formatDate(MornyCoeur.coeurStartTimestamp, 0)), MornyCoeur.coeurStartTimestamp ) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); @@ -190,8 +193,9 @@ public class OnCommandExecute extends EventListener { event.message().chat().id(), String.format( "%s 在(utc的)今天的运气指数是———— %.2f%% %s", - event.message().from().id(), event.message().from().firstName(), - jrrp, endChar + event.message().from().id(), + escapeHtmlTelegram(event.message().from().firstName()), + jrrp, escapeHtmlTelegram(endChar) ) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java index ffabd4e..a4275a4 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnEventHackHandle.java @@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; +import cc.sukazyo.cono.morny.util.StringUtils; import com.google.gson.GsonBuilder; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; @@ -65,7 +66,7 @@ public class OnEventHackHandle extends EventListener { logger.debug("hacked event by " + x); MornyCoeur.getAccount().execute(new SendMessage(x.fromChatId, String.format( "%s", - new GsonBuilder().setPrettyPrinting().create().toJson(update) + StringUtils.escapeHtmlTelegram(new GsonBuilder().setPrettyPrinting().create().toJson(update)) )).parseMode(ParseMode.HTML).replyToMessageId((int)x.fromMessageId)); return true; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java index 19db7e5..67dff8a 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserSlashAction.java @@ -10,6 +10,8 @@ import com.pengrad.telegrambot.request.SendMessage; import javax.annotation.Nonnull; +import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; + public class OnUserSlashAction extends EventListener { @Override @@ -48,11 +50,11 @@ public class OnUserSlashAction extends EventListener { event.message().chat().id(), String.format( "%s %s%s %s%s%s", - origin.id(), origin.firstName(), - verb, (useVerbSuffix?"了":""), - target.id(), (origin==target ? "自己" : target.firstName()), - (hasObject ? (useObjectPrefix ?" 的": " ") : ""), - (hasObject ? object : "") + origin.id(), escapeHtmlTelegram(origin.firstName()), + verb, escapeHtmlTelegram((useVerbSuffix?"了":"")), + target.id(), escapeHtmlTelegram((origin==target ? "自己" : target.firstName())), + escapeHtmlTelegram((hasObject ? (useObjectPrefix ?" 的": " ") : "")), + escapeHtmlTelegram((hasObject ? object : "")) ) ).parseMode(ParseMode.HTML)); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java index 6ed80af..b5cd025 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/GetUsernameAndId.java @@ -10,6 +10,8 @@ import com.pengrad.telegrambot.response.GetChatMemberResponse; import javax.annotation.Nonnull; +import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; + public class GetUsernameAndId { public static void exec (@Nonnull String[] args, @Nonnull Update event) { @@ -65,7 +67,7 @@ public class GetUsernameAndId { username : - %s""", - user.username() + escapeHtmlTelegram(user.username()) )); } if (user.firstName() == null) { @@ -76,7 +78,7 @@ public class GetUsernameAndId { firstname : - %s""", - user.firstName() + escapeHtmlTelegram(user.firstName()) )); } if (user.lastName() == null) { @@ -87,7 +89,7 @@ public class GetUsernameAndId { lastname : - %s""", - user.lastName() + escapeHtmlTelegram(user.lastName()) )); } if (user.languageCode() != null) { @@ -96,7 +98,7 @@ public class GetUsernameAndId { language-code : - %s""", - user.languageCode() + escapeHtmlTelegram(user.languageCode()) )); } 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 index 9186707..3c73c8a 100644 --- 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 @@ -7,10 +7,15 @@ 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; +import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; + +/** + * {@value IP186QueryHandler#SITE_URL} 查询的 telegram 命令前端 + * @since 0.4.2.10 + */ public class Ip186Query { public static void exec (@Nonnull Update event, @Nonnull InputCommand command) { @@ -33,12 +38,12 @@ public class Ip186Query { }; MornyCoeur.getAccount().execute(new SendMessage( event.message().chat().id(), - response.url() + "\n" + StringEscapeUtils.escapeHtml4(response.body()) + "" + escapeHtmlTelegram(response.url()) + "\n" + escapeHtmlTelegram(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() + "" + "[Exception] in query:\n" + escapeHtmlTelegram(e.getMessage()) + "" ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); } diff --git a/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java b/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java index eb80f66..2fd858e 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java @@ -12,10 +12,32 @@ import com.pengrad.telegrambot.model.User; */ public class MornyJrrp { + /** + * 通过 telegram 用户和时间戳作为参数获取 jrrp. + * + * @see #calcJrrpXmomi 当前版本的实现算法 {@code Xmomi} + * @since 0.4.2.9 + * @param user telegram 用户 + * @param timestamp 时间戳 + * @return 通过当前版本的算法计算出的用户 jrrp 值,取值为 {@code [0.00, 100.00]} + */ public static double getJrrpFromTelegramUser (User user, long timestamp) { return calcJrrpXmomi(user.id(), timestamp / (1000 * 60 * 60 * 24)) * 100.0; } + /** + * {@code Xmomi} 版本的 jrrp 算法. + *

    + * 算法规则为,将用户id与日期戳链接为 uid@daystamp 这样的字符串, + * 然后通过 MD5 计算出字符串的哈希值,取哈希值前4个字节,将其作为16进制数值表示法转换为取值为 {@code [0x0000, 0xffff]} 的数值, + * 得到的数值除以区间最大值 {@code 0xffff} 即可得到一个分布在 {@code [0.0, 1.0]} 之间的分布值, + * 这个分布值乘以 {@code 100.0},即为计算得到的 jrrp 数值。 + * + * @since 0.4.2.9 + * @param userId telegram 用户 uid + * @param dayStamp unix 时间戳转换为日期单位后的数值. 数值应该在转换前转换时区 + * @return 算法得到的 jrrp 值,取值为 {@code [0.00. 100.00]} + */ public static double calcJrrpXmomi (long userId, long dayStamp) { return (double)Long.parseLong(EncryptUtils.encryptByMD5(userId + "@" + dayStamp).substring(0, 4), 16) / (double)0xffff; } 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 index fcfe0da..c29d0b9 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryHandler.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryHandler.java @@ -8,20 +8,55 @@ import okhttp3.ResponseBody; import javax.annotation.Nonnull; import java.io.IOException; +/** + * 通过 {@value #SITE_URL} 进行 {@link #queryIp ip}/{@link #queryWhois whois} 数据查询的工具类 + * + * @since 0.4.2.10 + */ public class IP186QueryHandler { + /** + * 请求所使用的 HTTP API 站点链接 + * @since 0.4.2.10 + */ public static final String SITE_URL = "https://ip.186526.xyz/"; + /** + * 进行 {@link #queryIp ip 查询}时所使用的 API 参数.
    + * 目的使 API 直接返回原始数据 + */ private static final String QUERY_IP_PARAM = "type=json&format=true"; + + /** + * 进行 {@link #queryWhois whois 查询}时所使用的 API 参数.
    + * 目的使 API 直接返回原始数据 + */ private static final String QUERY_WHOIS_PARAM = "type=plain"; + /** 请求时使用的 OkHttp 请求工具实例 */ private static final OkHttpClient httpClient = new OkHttpClient(); + /** + * 通过 {@value #SITE_URL} 获取 ip 信息. + * @see #QUERY_IP_PARAM 发送请求时所使用的 API 参数 + * @param ip 需要进行查询的 ip + * @return 查询结果。data 根据 {@value #SITE_URL} 的规则以 json 序列化 + * @throws IOException 任何请求或解析错误 + */ + @Nonnull public static IP186QueryResponse queryIp (String ip) throws IOException { final String requestUrl = SITE_URL + ip; return commonQuery(requestUrl, QUERY_IP_PARAM); } + /** + * 通过 {@value #SITE_URL} 获取域名信息. + * @see #QUERY_WHOIS_PARAM 发送请求时所使用的 API 参数 + * @param domain 需要进行查询的域名 + * @return 查询结果。data 根据 {@value #SITE_URL} 的规则以 plain 序列化 + * @throws IOException 任何请求或解析错误 + */ + @Nonnull public static IP186QueryResponse queryWhois (String domain) throws IOException { final String requestUrl = SITE_URL + "whois/" + domain; return commonQuery(requestUrl, QUERY_WHOIS_PARAM); 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 index 24c075f..f30fb3d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryResponse.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/ip186/IP186QueryResponse.java @@ -1,4 +1,11 @@ package cc.sukazyo.cono.morny.data.ip186; +/** + * {@link IP186QueryHandler} 的请求结果数据的通用封装类. + * + * @since 0.4.2.10 + * @param url 请求数据的人类可读的来源链接,并非api链接 + * @param body API 传回的数据内容 + */ public record IP186QueryResponse(String url, String body) { } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java index 7ff40c2..ced5080 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java @@ -3,17 +3,9 @@ package cc.sukazyo.cono.morny.util; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import java.util.ArrayList; -import java.util.Arrays; public class StringUtils { - @Nonnull - public static String repeatChar (char c, int i) { - final char[] chars = new char[i]; - Arrays.fill(chars, c); - return new String(chars); - } - @Nonnull public static String[] formatCommand (@Nonnull String com) { @@ -66,4 +58,12 @@ public class StringUtils { return builder.toString(); } + @Nonnull + public static String escapeHtmlTelegram (String raw) { + raw = raw.replaceAll("&", "&"); + raw = raw.replaceAll("<", "<"); + raw = raw.replaceAll(">", ">"); + return raw; + } + }