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 指定时间} 之前的事件处理. + *

+ * 只支持以下事件 + *

+ */ 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; }