diff --git a/build.gradle b/build.gradle index 2b36477..4ec752e 100644 --- a/build.gradle +++ b/build.gradle @@ -21,6 +21,10 @@ dependencies { compileOnlyApi "com.github.spotbugs:spotbugs-annotations:${libSpotbugsVersion}" + implementation "cc.sukazyo.untitled:util-string-commons:${libUntitledVersion}" + implementation "cc.sukazyo.untitled:util-telegram-commons:${libUntitledVersion}" + implementation "cc.sukazyo.untitled:util-telegram-api:${libUntitledVersion}" + api "cc.sukazyo:messiva:${libMessivaVersion}" implementation "com.github.pengrad:java-telegram-bot-api:${libJavaTelegramBotApiVersion}" diff --git a/gradle.properties b/gradle.properties index 6f25b4a..bc97dde 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,11 +1,13 @@ ## Core -VERSION = 0.4.3.0 +VERSION = 0.4.3.1 # dependencies libSpotbugsVersion = 4.5.2 +libUntitledVersion = 1.+ + libMessivaVersion = 0.1.0.1 libJavaTelegramBotApiVersion = 5.5.0 diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index b19b3c0..7481938 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.3.0"; - public static final long COMPILE_TIMESTAMP = 1641056437585L; + public static final String VERSION = "0.4.3.1"; + public static final long COMPILE_TIMESTAMP = 1641201595120L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/InputCommand.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/InputCommand.java deleted file mode 100644 index 743d0da..0000000 --- a/src/main/java/cc/sukazyo/cono/morny/bot/api/InputCommand.java +++ /dev/null @@ -1,65 +0,0 @@ -package cc.sukazyo.cono.morny.bot.api; - -import cc.sukazyo.cono.morny.util.StringUtils; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import java.util.Arrays; - -public class InputCommand { - - private final String target; - private final String command; - private final String[] args; - - private InputCommand (@Nullable String target, @Nonnull String command, @Nonnull String[] args) { - this.target = target; - this.command = command; - this.args = args; - } - - public InputCommand (@Nonnull String[] inputArray) { - this(parseInputArray(inputArray)); - } - - public InputCommand (@Nonnull String input) { - this(StringUtils.formatCommand(input)); - } - - public InputCommand (@Nonnull InputCommand source) { - this(source.target, source.command, source.args); - } - - public static InputCommand parseInputArray (@Nonnull String[] inputArray) { - final String[] cx = inputArray[0].split("@", 2); - final String[] args = new String[inputArray.length-1]; - System.arraycopy(inputArray, 1, args, 0, inputArray.length - 1); - return new InputCommand(cx.length == 1 ? null : cx[1], cx[0], args); - } - - @Nullable - public String getTarget () { - return target; - } - - @Nonnull - public String getCommand () { - return command; - } - - @Nonnull - public String[] getArgs () { - return args; - } - - public boolean hasArgs () { - return args.length != 0; - } - - @Override - @Nonnull - public String toString() { - return String.format("{{%s}@{%s}#{%s}}", command, target, Arrays.toString(args)); - } - -} 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 f105b9e..10a814b 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,7 +13,7 @@ import com.pengrad.telegrambot.request.SendSticker; import javax.annotation.Nonnull; -import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; /** * 通过 bot 呼叫主人的事件监听管理类 @@ -76,7 +76,7 @@ public class OnCallMe extends EventListener { request STEAM LIBRARY from %s""", event.message().from().id(), - escapeHtmlTelegram( + escapeHtml( event.message().from().firstName() + " " + event.message().from().lastName() ) ) @@ -96,7 +96,7 @@ public class OnCallMe extends EventListener { request Hana Paresu from %s""", event.message().from().id(), - escapeHtmlTelegram( + escapeHtml( event.message().from().firstName() + " " + event.message().from().lastName() ) ) @@ -124,7 +124,7 @@ public class OnCallMe extends EventListener { request [???] from %s""", event.message().from().id(), - escapeHtmlTelegram( + escapeHtml( event.message().from().firstName() + " " + event.message().from().lastName() ) ) 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 5b49947..1b16641 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 @@ -4,12 +4,13 @@ import cc.sukazyo.cono.morny.GradleProjectConfigures; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornySystem; 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.untitled.util.telegram.object.InputCommand; + import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; @@ -20,7 +21,7 @@ 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; +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class OnCommandExecute extends EventListener { @@ -131,10 +132,10 @@ public class OnCommandExecute extends EventListener { compile timestamp: - %d - %s [UTC]""", - escapeHtmlTelegram(MornySystem.VERSION), - escapeHtmlTelegram(MornySystem.getJarMd5()), + escapeHtml(MornySystem.VERSION), + escapeHtml(MornySystem.getJarMd5()), GradleProjectConfigures.COMPILE_TIMESTAMP, - escapeHtmlTelegram(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)) + escapeHtml(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)) ) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } @@ -166,24 +167,24 @@ public class OnCommandExecute extends EventListener { - %s [UTC] - [%d]""", // system - escapeHtmlTelegram(System.getProperty("os.name")), - escapeHtmlTelegram(System.getProperty("os.version")), + escapeHtml(System.getProperty("os.name")), + escapeHtml(System.getProperty("os.version")), Runtime.getRuntime().availableProcessors(), // java - escapeHtmlTelegram(System.getProperty("java.vm.name")), - escapeHtmlTelegram(System.getProperty("java.version")), + escapeHtml(System.getProperty("java.vm.name")), + escapeHtml(System.getProperty("java.version")), // memory Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().maxMemory() / 1024 / 1024, // version - escapeHtmlTelegram(MornySystem.VERSION), - escapeHtmlTelegram(MornySystem.getJarMd5()), - escapeHtmlTelegram(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)), + escapeHtml(MornySystem.VERSION), + escapeHtml(MornySystem.getJarMd5()), + escapeHtml(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)), GradleProjectConfigures.COMPILE_TIMESTAMP, // continuous - escapeHtmlTelegram(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)), + escapeHtml(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)), System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp, - escapeHtmlTelegram(formatDate(MornyCoeur.coeurStartTimestamp, 0)), + escapeHtml(formatDate(MornyCoeur.coeurStartTimestamp, 0)), MornyCoeur.coeurStartTimestamp ) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); @@ -197,8 +198,8 @@ public class OnCommandExecute extends EventListener { String.format( "%s 在(utc的)今天的运气指数是———— %.2f%% %s", event.message().from().id(), - escapeHtmlTelegram(event.message().from().firstName()), - jrrp, escapeHtmlTelegram(endChar) + escapeHtml(event.message().from().firstName()), + jrrp, escapeHtml(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 3f814ff..d61391e 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,7 +2,8 @@ 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 cc.sukazyo.untitled.util.telegram.formatting.MsgEscape; + import com.google.gson.GsonBuilder; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; @@ -66,7 +67,7 @@ public class OnEventHackHandle extends EventListener { logger.debug("hacked event by " + x); MornyCoeur.getAccount().execute(new SendMessage(x.fromChatId, String.format( "%s", - StringUtils.escapeHtmlTelegram(new GsonBuilder().setPrettyPrinting().create().toJson(update)) + MsgEscape.escapeHtml(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 67dff8a..807d984 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 @@ -2,7 +2,9 @@ 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 cc.sukazyo.untitled.util.command.CommonCommand; +import cc.sukazyo.untitled.util.string.StringArrays; + import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.model.request.ParseMode; @@ -10,7 +12,7 @@ import com.pengrad.telegrambot.request.SendMessage; import javax.annotation.Nonnull; -import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class OnUserSlashAction extends EventListener { @@ -35,10 +37,10 @@ public class OnUserSlashAction extends EventListener { prefixLength = 2; } - final String[] action = StringUtils.formatCommand(text.substring(prefixLength)); + final String[] action = CommonCommand.format(text.substring(prefixLength)); final String verb = action[0]; final boolean hasObject = action.length != 1; - final String object = StringUtils.connectStringArray(action, " ", 1, action.length-1); + final String object = StringArrays.connectStringArray(action, " ", 1, action.length-1); final User origin = event.message().from(); final User target = (event.message().replyToMessage() == null ? ( origin @@ -50,11 +52,11 @@ public class OnUserSlashAction extends EventListener { event.message().chat().id(), String.format( "%s %s%s %s%s%s", - origin.id(), escapeHtmlTelegram(origin.firstName()), - verb, escapeHtmlTelegram((useVerbSuffix?"了":"")), - target.id(), escapeHtmlTelegram((origin==target ? "自己" : target.firstName())), - escapeHtmlTelegram((hasObject ? (useObjectPrefix ?" 的": " ") : "")), - escapeHtmlTelegram((hasObject ? object : "")) + origin.id(), escapeHtml(origin.firstName()), + verb, escapeHtml((useVerbSuffix?"了":"")), + target.id(), escapeHtml((origin==target ? "自己" : target.firstName())), + escapeHtml((hasObject ? (useObjectPrefix ?" 的": " ") : "")), + escapeHtml((hasObject ? object : "")) ) ).parseMode(ParseMode.HTML)); 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 2146535..2638ec6 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 @@ -2,9 +2,10 @@ 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 cc.sukazyo.untitled.util.telegram.object.InputCommand; + import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.SendSticker; 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 b5cd025..0bd7830 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,7 +10,7 @@ import com.pengrad.telegrambot.response.GetChatMemberResponse; import javax.annotation.Nonnull; -import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class GetUsernameAndId { @@ -67,7 +67,7 @@ public class GetUsernameAndId { username : - %s""", - escapeHtmlTelegram(user.username()) + escapeHtml(user.username()) )); } if (user.firstName() == null) { @@ -78,7 +78,7 @@ public class GetUsernameAndId { firstname : - %s""", - escapeHtmlTelegram(user.firstName()) + escapeHtml(user.firstName()) )); } if (user.lastName() == null) { @@ -89,7 +89,7 @@ public class GetUsernameAndId { lastname : - %s""", - escapeHtmlTelegram(user.lastName()) + escapeHtml(user.lastName()) )); } if (user.languageCode() != null) { @@ -98,7 +98,7 @@ public class GetUsernameAndId { language-code : - %s""", - escapeHtmlTelegram(user.languageCode()) + escapeHtml(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 9128ed1..12ccddb 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 @@ -1,8 +1,8 @@ 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.untitled.util.telegram.object.InputCommand; import cc.sukazyo.cono.morny.data.ip186.IP186QueryHandler; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; @@ -10,7 +10,7 @@ import com.pengrad.telegrambot.request.SendMessage; import javax.annotation.Nonnull; -import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; /** * {@value IP186QueryHandler#SITE_URL} 查询的 telegram 命令前端 @@ -50,12 +50,12 @@ public class Ip186Query { }; MornyCoeur.getAccount().execute(new SendMessage( event.message().chat().id(), - escapeHtmlTelegram(response.url()) + "\n" + escapeHtmlTelegram(response.body()) + "" + escapeHtml(response.url()) + "\n" + escapeHtml(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" + escapeHtmlTelegram(e.getMessage()) + "" + "[Exception] in query:\n" + escapeHtml(e.getMessage()) + "" ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); } 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 aed2c8d..06945ab 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,6 +26,7 @@ public class TrackerDataManager { @Override public void run () { + trackingLock.lock(); long lastWaitTimestamp = System.currentTimeMillis(); boolean postProcess = false; do { @@ -46,6 +47,7 @@ public class TrackerDataManager { } else logger.info("nothing to do yet"); } while (!postProcess); + trackingLock.unlock(); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java deleted file mode 100644 index ced5080..0000000 --- a/src/main/java/cc/sukazyo/cono/morny/util/StringUtils.java +++ /dev/null @@ -1,69 +0,0 @@ -package cc.sukazyo.cono.morny.util; - -import javax.annotation.Nonnegative; -import javax.annotation.Nonnull; -import java.util.ArrayList; - -public class StringUtils { - - @Nonnull - public static String[] formatCommand (@Nonnull String com) { - - final ArrayList arr = new ArrayList<>(); - - final StringBuilder tmp = new StringBuilder(); - final char[] coma = com.toCharArray(); - for (int i = 0; i < coma.length; i++) { - if (coma[i] == ' ') { - if (!tmp.toString().equals("")) { arr.add(tmp.toString()); } - tmp.setLength(0); - } else if (coma[i] == '"') { - while (true) { - i++; - if (coma[i] == '"') { - break; - } else if (coma[i] == '\\' && (coma[i+1] == '"' || coma[i+1] == '\\')) { - i++; - tmp.append(coma[i]); - } else { - tmp.append(coma[i]); - } - } - } else if (coma[i] == '\\' && (coma[i+1] == ' ' || coma[i+1] == '"' || coma[i+1] == '\\')) { - i++; - tmp.append(coma[i]); - } else { - tmp.append(coma[i]); - } - } - if (!tmp.toString().equals("")) { arr.add(tmp.toString()); } - tmp.setLength(0); - - final String[] out = new String[arr.size()]; - arr.toArray(out); - return out; - - } - - @Nonnull - public static String connectStringArray ( - @Nonnull String[] array, @Nonnull String connector, @Nonnegative int startIndex, @Nonnegative int stopIndex - ) { - final StringBuilder builder = new StringBuilder(); - for (int i = startIndex; i < stopIndex; i++) { - builder.append(array[i]); - builder.append(connector); - } - builder.append(array[stopIndex]); - return builder.toString(); - } - - @Nonnull - public static String escapeHtmlTelegram (String raw) { - raw = raw.replaceAll("&", "&"); - raw = raw.replaceAll("<", "<"); - raw = raw.replaceAll(">", ">"); - return raw; - } - -}