mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono.git
synced 2024-11-22 11:14:55 +08:00
完成事件劫持处理与输出,扩充超时事件忽略的作用范围
This commit is contained in:
parent
7b88b40c1c
commit
6a5a23b6be
@ -1,6 +1,6 @@
|
||||
## Core
|
||||
|
||||
VERSION = 0.4.2.5
|
||||
VERSION = 0.4.2.6
|
||||
|
||||
# dependencies
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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<String, Hacker> 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(
|
||||
"<code>%s</code>",
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 指定时间} 之前的事件处理.
|
||||
* <p>
|
||||
* 只支持以下事件
|
||||
* <ul>
|
||||
* <li>{@link EventListener#onMessage(Update) 收到消息}</li>
|
||||
* <li>{@link EventListener#onEditedMessage(Update) 消息被更新}</li>
|
||||
* <li>{@link EventListener#onChannelPost(Update) 收到频道消息}</li>
|
||||
* <li>{@link EventListener#onEditedChannelPost(Update) 频道消息被更新}</li>
|
||||
* </ul>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user