From 413f73403465460d8e6e4c38e1a74070874108f4 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 2 Jan 2022 01:04:13 +0800 Subject: [PATCH] =?UTF-8?q?ip186=20=E7=B3=BB=E5=88=97=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9B=9E=E5=A4=8D=E4=BD=9C=E4=B8=BA=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E6=B7=BB=E5=8A=A0=20/save=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E7=AB=8B=E5=8D=B3=E4=BF=9D=E5=AD=98=EF=BC=8Cgit=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20.vscode=20=E5=BF=BD=E7=95=A5=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - gitignore 添加了 .vscode 字段 - /ip 与 /whois 命令支持了回复某条消息,以其消息内容作为参数的查询方式 - 添加 /save 与其对应的 Morny save 指令链,用于手动/统一触发数据持久化储存事件 - 更新了 hack event 的 debug log 文案 - 稍微修改了 Tracker 的保存函数链,用以支持外部触发 --- .gitignore | 1 + gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +-- .../cc/sukazyo/cono/morny/MornyCoeur.java | 17 ++++++++++ .../morny/bot/event/OnCommandExecute.java | 25 ++++++++++++++ .../morny/bot/event/OnEventHackHandle.java | 2 +- .../bot/event/on_commands/Ip186Query.java | 34 +++++++++++++------ .../cono/morny/data/TelegramStickers.java | 1 + .../data/tracker/TrackerDataManager.java | 10 +++--- 9 files changed, 77 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 037aacb..8520bdf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # IDE .idea/ +.vscode/ .gradle/ .settings/ /src/test/* diff --git a/gradle.properties b/gradle.properties index f697b89..6f25b4a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.2.11 +VERSION = 0.4.3.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 4760921..b19b3c0 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.11"; - public static final long COMPILE_TIMESTAMP = 1640595623685L; + public static final String VERSION = "0.4.3.0"; + public static final long COMPILE_TIMESTAMP = 1641056437585L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 06d7944..7f8a239 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -122,6 +122,14 @@ public class MornyCoeur { logger.error("System already started coeur!!!"); } + /** + * 向所有的数据管理器发起保存数据的指令 + * @since 0.4.3.0 + */ + public void saveDataAll () { + TrackerDataManager.save(); + } + /** * 用于退出时进行缓存的任务处理等进行安全退出 */ @@ -167,6 +175,15 @@ public class MornyCoeur { throw new RuntimeException("Login failed.."); } + /** + * @see #saveDataAll() + * @since 0.4.3.0 + */ + public static void callSaveData () { + INSTANCE.saveDataAll(); + logger.info("done all save action."); + } + /** * 获取登录成功后的 telegram 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 a49d884..5b49947 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 @@ -56,6 +56,9 @@ public class OnCommandExecute extends EventListener { case "/runtime": onCommandRuntimeExec(event); break; + case "/save": + onSaveDataExec(event); + break; case "/jrrp": onCommandJrrpExec(event); break; @@ -200,4 +203,26 @@ public class OnCommandExecute extends EventListener { ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } + /** + * @since 0.4.3.0 + */ + private void onSaveDataExec (Update event) { + if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) { + logger.info(String.format("called save from command by @%s.", event.message().from().username())); + MornyCoeur.callSaveData(); + MornyCoeur.getAccount().execute(new SendSticker( + event.message().chat().id(), + TelegramStickers.ID_SAVED + ).replyToMessageId(event.message().messageId()) + ); + } else { + MornyCoeur.getAccount().execute(new SendSticker( + event.message().chat().id(), + TelegramStickers.ID_403 + ).replyToMessageId(event.message().messageId()) + ); + logger.info("403 call save tag from user @" + event.message().from().username()); + } + } + } 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 a4275a4..3f814ff 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 @@ -57,7 +57,7 @@ public class OnEventHackHandle extends EventListener { } private boolean onEventHacked (Update update, long chatId, long fromUser) { - logger.debug(String.format("try hack {{%d}}((%d))", chatId, fromUser)); + logger.debug(String.format("got event signed {{%d}}((%d))", chatId, fromUser)); Hacker x; x = hackers.remove(String.format("((%d))", fromUser)); if (x == null) x = hackers.remove(String.format("{{%d}}", chatId)); 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 3c73c8a..9128ed1 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 @@ -20,20 +20,32 @@ 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; } + String arg = null; + if (!command.hasArgs()) { + if (event.message().replyToMessage() != null) { + arg = event.message().replyToMessage().text(); + } + } else if (command.getArgs().length > 1) { + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Unavailable] Too much arguments." + ).replyToMessageId(event.message().messageId())); + return; + } else { + arg = command.getArgs()[0]; + } + if (arg == null) { + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Unavailable] No ip defined." + ).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]); + case "/ip" -> IP186QueryHandler.queryIp(arg); + case "/whois" -> IP186QueryHandler.queryWhois(arg); default -> throw new IllegalArgumentException("Unknown 186-IP query method " + command.getCommand()); }; MornyCoeur.getAccount().execute(new SendMessage( 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 d478e05..c53024c 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java @@ -13,5 +13,6 @@ public class TelegramStickers { 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"; + public static final String ID_SAVED = "CAACAgEAAx0CSQh32gABBExuYdB_G0srfhQldRWkBYxWzCOv4-IAApooAAJ4_MYFcjuNZszfQcQjBA"; } diff --git a/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java b/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java index 753944d..aed2c8d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/tracker/TrackerDataManager.java @@ -26,7 +26,6 @@ public class TrackerDataManager { @Override public void run () { - trackingLock.lock(); long lastWaitTimestamp = System.currentTimeMillis(); boolean postProcess = false; do { @@ -43,13 +42,10 @@ public class TrackerDataManager { logger.info("CALLED TO EXIT! writing cache."); } if (record.size() != 0) { - logger.info("start writing tracker data."); save(reset()); - logger.info("done writing tracker data."); } else logger.info("nothing to do yet"); } while (!postProcess); - trackingLock.unlock(); } } @@ -68,6 +64,12 @@ public class TrackerDataManager { DAEMON.start(); } + public static void save () { + logger.info("start writing tracker data."); + save(reset()); + logger.info("done writing tracker data."); + } + private static HashMap>> reset () { recordLock.lock(); HashMap>> recordOld = record;