将部分 util 移动至 untitled 项目,修复上个更新的 tracker 活动锁被误删的问题

This commit is contained in:
A.C.Sukazyo Eyre 2022-01-03 17:24:35 +08:00
parent 413f734034
commit 2a34b576b6
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
13 changed files with 57 additions and 178 deletions

View File

@ -21,6 +21,10 @@ dependencies {
compileOnlyApi "com.github.spotbugs:spotbugs-annotations:${libSpotbugsVersion}" 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}" api "cc.sukazyo:messiva:${libMessivaVersion}"
implementation "com.github.pengrad:java-telegram-bot-api:${libJavaTelegramBotApiVersion}" implementation "com.github.pengrad:java-telegram-bot-api:${libJavaTelegramBotApiVersion}"

View File

@ -1,11 +1,13 @@
## Core ## Core
VERSION = 0.4.3.0 VERSION = 0.4.3.1
# dependencies # dependencies
libSpotbugsVersion = 4.5.2 libSpotbugsVersion = 4.5.2
libUntitledVersion = 1.+
libMessivaVersion = 0.1.0.1 libMessivaVersion = 0.1.0.1
libJavaTelegramBotApiVersion = 5.5.0 libJavaTelegramBotApiVersion = 5.5.0

View File

@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny;
* the final field that will be updated by gradle automatically. * the final field that will be updated by gradle automatically.
*/ */
public class GradleProjectConfigures { public class GradleProjectConfigures {
public static final String VERSION = "0.4.3.0"; public static final String VERSION = "0.4.3.1";
public static final long COMPILE_TIMESTAMP = 1641056437585L; public static final long COMPILE_TIMESTAMP = 1641201595120L;
} }

View File

@ -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));
}
}

View File

@ -13,7 +13,7 @@ import com.pengrad.telegrambot.request.SendSticker;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import static cc.sukazyo.cono.morny.util.StringUtils.escapeHtmlTelegram; import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml;
/** /**
* 通过 bot 呼叫主人的事件监听管理类 * 通过 bot 呼叫主人的事件监听管理类
@ -76,7 +76,7 @@ public class OnCallMe extends EventListener {
request <b>STEAM LIBRARY</b> request <b>STEAM LIBRARY</b>
from <a href="tg://user?id=%d">%s</a>""", from <a href="tg://user?id=%d">%s</a>""",
event.message().from().id(), event.message().from().id(),
escapeHtmlTelegram( escapeHtml(
event.message().from().firstName() + " " + event.message().from().lastName() event.message().from().firstName() + " " + event.message().from().lastName()
) )
) )
@ -96,7 +96,7 @@ public class OnCallMe extends EventListener {
request <b>Hana Paresu</b> request <b>Hana Paresu</b>
from <a href="tg://user?id=%d">%s</a>""", from <a href="tg://user?id=%d">%s</a>""",
event.message().from().id(), event.message().from().id(),
escapeHtmlTelegram( escapeHtml(
event.message().from().firstName() + " " + event.message().from().lastName() event.message().from().firstName() + " " + event.message().from().lastName()
) )
) )
@ -124,7 +124,7 @@ public class OnCallMe extends EventListener {
request <u>[???]</u> request <u>[???]</u>
from <a href="tg://user?id=%d">%s</a>""", from <a href="tg://user?id=%d">%s</a>""",
event.message().from().id(), event.message().from().id(),
escapeHtmlTelegram( escapeHtml(
event.message().from().firstName() + " " + event.message().from().lastName() event.message().from().firstName() + " " + event.message().from().lastName()
) )
) )

View File

@ -4,12 +4,13 @@ import cc.sukazyo.cono.morny.GradleProjectConfigures;
import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.MornySystem; import cc.sukazyo.cono.morny.MornySystem;
import cc.sukazyo.cono.morny.bot.api.EventListener; 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.EventHack;
import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId; 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.bot.event.on_commands.Ip186Query;
import cc.sukazyo.cono.morny.data.MornyJrrp; import cc.sukazyo.cono.morny.data.MornyJrrp;
import cc.sukazyo.cono.morny.data.TelegramStickers; 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.Update;
import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.SendMessage; 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.Log.logger;
import static cc.sukazyo.cono.morny.util.CommonFormatUtils.formatDate; 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.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 { public class OnCommandExecute extends EventListener {
@ -131,10 +132,10 @@ public class OnCommandExecute extends EventListener {
compile timestamp: compile timestamp:
- <code>%d</code> - <code>%d</code>
- <code>%s [UTC]</code>""", - <code>%s [UTC]</code>""",
escapeHtmlTelegram(MornySystem.VERSION), escapeHtml(MornySystem.VERSION),
escapeHtmlTelegram(MornySystem.getJarMd5()), escapeHtml(MornySystem.getJarMd5()),
GradleProjectConfigures.COMPILE_TIMESTAMP, GradleProjectConfigures.COMPILE_TIMESTAMP,
escapeHtmlTelegram(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)) escapeHtml(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0))
) )
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
} }
@ -166,24 +167,24 @@ public class OnCommandExecute extends EventListener {
- <code>%s [UTC]</code> - <code>%s [UTC]</code>
- [<code>%d</code>]""", - [<code>%d</code>]""",
// system // system
escapeHtmlTelegram(System.getProperty("os.name")), escapeHtml(System.getProperty("os.name")),
escapeHtmlTelegram(System.getProperty("os.version")), escapeHtml(System.getProperty("os.version")),
Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().availableProcessors(),
// java // java
escapeHtmlTelegram(System.getProperty("java.vm.name")), escapeHtml(System.getProperty("java.vm.name")),
escapeHtmlTelegram(System.getProperty("java.version")), escapeHtml(System.getProperty("java.version")),
// memory // memory
Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().totalMemory() / 1024 / 1024,
Runtime.getRuntime().maxMemory() / 1024 / 1024, Runtime.getRuntime().maxMemory() / 1024 / 1024,
// version // version
escapeHtmlTelegram(MornySystem.VERSION), escapeHtml(MornySystem.VERSION),
escapeHtmlTelegram(MornySystem.getJarMd5()), escapeHtml(MornySystem.getJarMd5()),
escapeHtmlTelegram(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)), escapeHtml(formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0)),
GradleProjectConfigures.COMPILE_TIMESTAMP, GradleProjectConfigures.COMPILE_TIMESTAMP,
// continuous // continuous
escapeHtmlTelegram(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)), escapeHtml(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)),
System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp, System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp,
escapeHtmlTelegram(formatDate(MornyCoeur.coeurStartTimestamp, 0)), escapeHtml(formatDate(MornyCoeur.coeurStartTimestamp, 0)),
MornyCoeur.coeurStartTimestamp MornyCoeur.coeurStartTimestamp
) )
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
@ -197,8 +198,8 @@ public class OnCommandExecute extends EventListener {
String.format( String.format(
"<a href='tg://user?id=%d'>%s</a> 在(utc的)今天的运气指数是———— <code>%.2f%%</code> %s", "<a href='tg://user?id=%d'>%s</a> 在(utc的)今天的运气指数是———— <code>%.2f%%</code> %s",
event.message().from().id(), event.message().from().id(),
escapeHtmlTelegram(event.message().from().firstName()), escapeHtml(event.message().from().firstName()),
jrrp, escapeHtmlTelegram(endChar) jrrp, escapeHtml(endChar)
) )
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
} }

View File

@ -2,7 +2,8 @@ package cc.sukazyo.cono.morny.bot.event;
import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.bot.api.EventListener; 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.google.gson.GsonBuilder;
import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.model.request.ParseMode;
@ -66,7 +67,7 @@ public class OnEventHackHandle extends EventListener {
logger.debug("hacked event by " + x); logger.debug("hacked event by " + x);
MornyCoeur.getAccount().execute(new SendMessage(x.fromChatId, String.format( MornyCoeur.getAccount().execute(new SendMessage(x.fromChatId, String.format(
"<code>%s</code>", "<code>%s</code>",
StringUtils.escapeHtmlTelegram(new GsonBuilder().setPrettyPrinting().create().toJson(update)) MsgEscape.escapeHtml(new GsonBuilder().setPrettyPrinting().create().toJson(update))
)).parseMode(ParseMode.HTML).replyToMessageId((int)x.fromMessageId)); )).parseMode(ParseMode.HTML).replyToMessageId((int)x.fromMessageId));
return true; return true;
} }

View File

@ -2,7 +2,9 @@ package cc.sukazyo.cono.morny.bot.event;
import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.bot.api.EventListener; 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.Update;
import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.model.User;
import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.model.request.ParseMode;
@ -10,7 +12,7 @@ import com.pengrad.telegrambot.request.SendMessage;
import javax.annotation.Nonnull; 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 { public class OnUserSlashAction extends EventListener {
@ -35,10 +37,10 @@ public class OnUserSlashAction extends EventListener {
prefixLength = 2; prefixLength = 2;
} }
final String[] action = StringUtils.formatCommand(text.substring(prefixLength)); final String[] action = CommonCommand.format(text.substring(prefixLength));
final String verb = action[0]; final String verb = action[0];
final boolean hasObject = action.length != 1; 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 origin = event.message().from();
final User target = (event.message().replyToMessage() == null ? ( final User target = (event.message().replyToMessage() == null ? (
origin origin
@ -50,11 +52,11 @@ public class OnUserSlashAction extends EventListener {
event.message().chat().id(), event.message().chat().id(),
String.format( String.format(
"<a href='tg://user?id=%d'>%s</a> %s%s <a href='tg://user?id=%d'>%s</a>%s%s", "<a href='tg://user?id=%d'>%s</a> %s%s <a href='tg://user?id=%d'>%s</a>%s%s",
origin.id(), escapeHtmlTelegram(origin.firstName()), origin.id(), escapeHtml(origin.firstName()),
verb, escapeHtmlTelegram((useVerbSuffix?"":"")), verb, escapeHtml((useVerbSuffix?"":"")),
target.id(), escapeHtmlTelegram((origin==target ? "自己" : target.firstName())), target.id(), escapeHtml((origin==target ? "自己" : target.firstName())),
escapeHtmlTelegram((hasObject ? (useObjectPrefix ?"": " ") : "")), escapeHtml((hasObject ? (useObjectPrefix ?"": " ") : "")),
escapeHtmlTelegram((hasObject ? object : "")) escapeHtml((hasObject ? object : ""))
) )
).parseMode(ParseMode.HTML)); ).parseMode(ParseMode.HTML));

View File

@ -2,9 +2,10 @@ package cc.sukazyo.cono.morny.bot.event.on_commands;
import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.MornyTrusted; 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.bot.event.OnEventHackHandle;
import cc.sukazyo.cono.morny.data.TelegramStickers; 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.Update;
import com.pengrad.telegrambot.request.SendSticker; import com.pengrad.telegrambot.request.SendSticker;

View File

@ -10,7 +10,7 @@ import com.pengrad.telegrambot.response.GetChatMemberResponse;
import javax.annotation.Nonnull; 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 { public class GetUsernameAndId {
@ -67,7 +67,7 @@ public class GetUsernameAndId {
username : username :
- <code>%s</code>""", - <code>%s</code>""",
escapeHtmlTelegram(user.username()) escapeHtml(user.username())
)); ));
} }
if (user.firstName() == null) { if (user.firstName() == null) {
@ -78,7 +78,7 @@ public class GetUsernameAndId {
firstname : firstname :
- <code>%s</code>""", - <code>%s</code>""",
escapeHtmlTelegram(user.firstName()) escapeHtml(user.firstName())
)); ));
} }
if (user.lastName() == null) { if (user.lastName() == null) {
@ -89,7 +89,7 @@ public class GetUsernameAndId {
lastname : lastname :
- <code>%s</code>""", - <code>%s</code>""",
escapeHtmlTelegram(user.lastName()) escapeHtml(user.lastName())
)); ));
} }
if (user.languageCode() != null) { if (user.languageCode() != null) {
@ -98,7 +98,7 @@ public class GetUsernameAndId {
language-code : language-code :
- <code>%s</code>""", - <code>%s</code>""",
escapeHtmlTelegram(user.languageCode()) escapeHtml(user.languageCode())
)); ));
} }

View File

@ -1,8 +1,8 @@
package cc.sukazyo.cono.morny.bot.event.on_commands; package cc.sukazyo.cono.morny.bot.event.on_commands;
import cc.sukazyo.cono.morny.MornyCoeur; 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.IP186QueryResponse;
import cc.sukazyo.untitled.util.telegram.object.InputCommand;
import cc.sukazyo.cono.morny.data.ip186.IP186QueryHandler; import cc.sukazyo.cono.morny.data.ip186.IP186QueryHandler;
import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.model.request.ParseMode;
@ -10,7 +10,7 @@ import com.pengrad.telegrambot.request.SendMessage;
import javax.annotation.Nonnull; 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 命令前端 * {@value IP186QueryHandler#SITE_URL} 查询的 telegram 命令前端
@ -50,12 +50,12 @@ public class Ip186Query {
}; };
MornyCoeur.getAccount().execute(new SendMessage( MornyCoeur.getAccount().execute(new SendMessage(
event.message().chat().id(), event.message().chat().id(),
escapeHtmlTelegram(response.url()) + "\n<code>" + escapeHtmlTelegram(response.body()) + "</code>" escapeHtml(response.url()) + "\n<code>" + escapeHtml(response.body()) + "</code>"
).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId()));
} catch (Exception e) { } catch (Exception e) {
MornyCoeur.getAccount().execute(new SendMessage( MornyCoeur.getAccount().execute(new SendMessage(
event.message().chat().id(), event.message().chat().id(),
"[Exception] in query:\n<code>" + escapeHtmlTelegram(e.getMessage()) + "</code>" "[Exception] in query:\n<code>" + escapeHtml(e.getMessage()) + "</code>"
).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId()));
} }

View File

@ -26,6 +26,7 @@ public class TrackerDataManager {
@Override @Override
public void run () { public void run () {
trackingLock.lock();
long lastWaitTimestamp = System.currentTimeMillis(); long lastWaitTimestamp = System.currentTimeMillis();
boolean postProcess = false; boolean postProcess = false;
do { do {
@ -46,6 +47,7 @@ public class TrackerDataManager {
} }
else logger.info("nothing to do yet"); else logger.info("nothing to do yet");
} while (!postProcess); } while (!postProcess);
trackingLock.unlock();
} }
} }

View File

@ -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<String> 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("&", "&amp;");
raw = raw.replaceAll("<", "&lt;");
raw = raw.replaceAll(">", "&gt;");
return raw;
}
}