diff --git a/build.gradle b/build.gradle index 694232a..0e2a6aa 100644 --- a/build.gradle +++ b/build.gradle @@ -17,19 +17,10 @@ repositories { maven { name '-ws'; url 'https://mvn.sukazyo.cc/releases' } } -String untitled (String lib, String upd = null) { - int majorCode = Integer.parseInt(project.libUntitledVersionMajor) - return "cc.sukazyo.untitled:$lib:[$majorCode${upd==null?"":".$upd"}, ${majorCode+1}[" -} dependencies { compileOnlyApi "com.github.spotbugs:spotbugs-annotations:${libSpotbugsVersion}" - implementation untitled("util-command-parser","1.0") - implementation untitled("util-string-commons", "1.0") - implementation untitled("util-telegram-api", "2.1") - implementation untitled("util-telegram-api-formatter", "3.3") - implementation untitled("util-telegram-commons", "1.0") api "cc.sukazyo:messiva:${libMessivaVersion}" implementation "com.github.pengrad:java-telegram-bot-api:${libJavaTelegramBotApiVersion}" diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 27b871d..fae76ac 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,7 +4,7 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.8.0.1"; + public static final String VERSION = "0.8.0.2"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1664648280419L; + public static final long COMPILE_TIMESTAMP = 1664867846222L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 4e716b8..9c0cee9 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -6,7 +6,7 @@ import cc.sukazyo.cono.morny.bot.event.EventListeners; import cc.sukazyo.cono.morny.bot.query.MornyQueries; import cc.sukazyo.cono.morny.daemon.MornyDaemons; import cc.sukazyo.cono.morny.daemon.TrackerDataManager; -import cc.sukazyo.untitled.telegram.api.extra.ExtraAction; +import cc.sukazyo.cono.morny.util.tgapi.ExtraAction; import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.impl.FileApi; import com.pengrad.telegrambot.model.User; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java index f38d94a..18d0004 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java @@ -1,10 +1,9 @@ package cc.sukazyo.cono.morny.bot.api; +import cc.sukazyo.cono.morny.util.tgapi.event.EventRuntimeException; import com.google.gson.GsonBuilder; import com.pengrad.telegrambot.model.Update; -import cc.sukazyo.untitled.telegram.api.event.EventRuntimeException; - import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java index 7c66a8f..54b850a 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/DirectMsgClear.java @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.command; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.untitled.util.telegram.object.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.DeleteMessage; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/EventHack.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/EventHack.java index 18edc7b..ddbe9e2 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/EventHack.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/EventHack.java @@ -4,8 +4,8 @@ import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyTrusted; import cc.sukazyo.cono.morny.bot.event.OnEventHackHandle; import cc.sukazyo.cono.morny.data.TelegramStickers; -import cc.sukazyo.untitled.util.telegram.object.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.SendSticker; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java index bb06ed2..bcc2bcf 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java @@ -1,8 +1,8 @@ package cc.sukazyo.cono.morny.bot.command; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.cono.morny.util.TelegramUserInformation; -import cc.sukazyo.untitled.util.telegram.object.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramUserInformation; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.model.request.ParseMode; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/ISimpleCommand.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/ISimpleCommand.java index 0f3fea2..453a97e 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/ISimpleCommand.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/ISimpleCommand.java @@ -1,6 +1,6 @@ package cc.sukazyo.cono.morny.bot.command; -import cc.sukazyo.untitled.util.telegram.object.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import com.pengrad.telegrambot.model.Update; import javax.annotation.Nonnull; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java index 1390096..212305a 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/Ip186Query.java @@ -2,8 +2,8 @@ package cc.sukazyo.cono.morny.bot.command; import cc.sukazyo.cono.morny.MornyCoeur; 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.util.tgapi.InputCommand; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; @@ -12,7 +12,8 @@ import org.jetbrains.annotations.NotNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; +import static cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape.escapeHtml; + /** * {@value IP186QueryHandler#SITE_URL} 查询的 telegram 命令前端 diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java index 968cd2d..8233cf1 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyCommands.java @@ -5,8 +5,8 @@ import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornySystem; import cc.sukazyo.cono.morny.data.MornyJrrp; import cc.sukazyo.cono.morny.data.TelegramStickers; -import cc.sukazyo.untitled.telegram.api.formatting.TGToString; -import cc.sukazyo.untitled.util.telegram.object.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.formatting.TGToString; import com.pengrad.telegrambot.model.BotCommand; import com.pengrad.telegrambot.model.DeleteMyCommands; import com.pengrad.telegrambot.model.Update; @@ -29,7 +29,7 @@ import java.util.Map; import static cc.sukazyo.cono.morny.Log.logger; import static cc.sukazyo.cono.morny.util.CommonFormat.formatDate; import static cc.sukazyo.cono.morny.util.CommonFormat.formatDuration; -import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; +import static cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape.escapeHtml; public class MornyCommands { diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.java index fced75b..22e87cc 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/Nbnhhsh.java @@ -1,18 +1,18 @@ package cc.sukazyo.cono.morny.bot.command; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.data.NbnhhshQuery; -import cc.sukazyo.untitled.util.string.StringArrays; -import cc.sukazyo.untitled.util.telegram.object.InputCommand; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; +import static cc.sukazyo.cono.morny.util.CommonConvert.stringsConnecting; +import static cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape.escapeHtml; public class Nbnhhsh implements ITelegramCommand { @@ -30,7 +30,7 @@ public class Nbnhhsh implements ITelegramCommand { if (event.message().replyToMessage() != null && event.message().replyToMessage().text() != null) queryTarget = event.message().replyToMessage().text(); if (command.hasArgs()) - queryTarget = StringArrays.connectStringArray(command.getArgs(), " ", 0, command.getArgs().length-1); + queryTarget = stringsConnecting(command.getArgs(), " ", 0, command.getArgs().length-1); NbnhhshQuery.GuessResult response = NbnhhshQuery.sendGuess(queryTarget); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java index 67d644a..b9931f9 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.command; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.untitled.util.telegram.object.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.SendMessage; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java index a8ad800..0c92c84 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/私わね.java @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.command; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.untitled.util.telegram.object.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.SendMessage; 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 777fe40..c448881 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 @@ -5,8 +5,8 @@ import cc.sukazyo.cono.morny.MornyTrusted; import cc.sukazyo.cono.morny.bot.api.EventListener; import cc.sukazyo.cono.morny.data.TelegramStickers; import cc.sukazyo.cono.morny.util.CommonFormat; -import cc.sukazyo.untitled.telegram.api.formatting.TGToString; -import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape; +import cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape; +import cc.sukazyo.cono.morny.util.tgapi.formatting.TGToString; import com.pengrad.telegrambot.model.Chat; import com.pengrad.telegrambot.model.Message; import com.pengrad.telegrambot.model.Update; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java index b5d7a34..d02beae 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMsgSend.java @@ -22,7 +22,8 @@ import cc.sukazyo.cono.morny.data.TelegramStickers; import com.pengrad.telegrambot.response.GetChatResponse; import com.pengrad.telegrambot.response.SendResponse; -import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; +import static cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape.escapeHtml; + public class OnCallMsgSend extends EventListener { 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 7189857..ffba9b5 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,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape; +import cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape; import com.google.gson.GsonBuilder; import com.pengrad.telegrambot.model.Update; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnTelegramCommand.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnTelegramCommand.java index 0266cfa..a74e7a5 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnTelegramCommand.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnTelegramCommand.java @@ -2,7 +2,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.untitled.util.telegram.object.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import com.pengrad.telegrambot.model.Update; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java index 3655a97..898bdac 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnUserRandoms.java @@ -2,7 +2,7 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.untitled.util.command.CommonCommand; +import cc.sukazyo.cono.morny.util.UniversalCommand; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.SendMessage; import org.jetbrains.annotations.NotNull; @@ -22,7 +22,7 @@ public class OnUserRandoms extends EventListener { if (update.message().text() == null) return false; if (!update.message().text().startsWith("/")) return false; - final String[] preProcess = CommonCommand.format(update.message().text()); + final String[] preProcess = UniversalCommand.format(update.message().text()); if (preProcess.length > 1) return false; final String query = preProcess[0]; 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 900f387..cf1910b 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,9 +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.tgapi.TGToStringFromMessage; -import cc.sukazyo.untitled.util.command.CommonCommand; -import cc.sukazyo.untitled.util.string.StringArrays; +import cc.sukazyo.cono.morny.util.UniversalCommand; +import cc.sukazyo.cono.morny.util.tgapi.formatting.TGToString; import com.pengrad.telegrambot.model.Message; import com.pengrad.telegrambot.model.Update; @@ -13,7 +12,8 @@ import com.pengrad.telegrambot.request.SendMessage; import javax.annotation.Nonnull; -import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; +import static cc.sukazyo.cono.morny.util.CommonConvert.stringsConnecting; +import static cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape.escapeHtml; public class OnUserSlashAction extends EventListener { @@ -38,7 +38,7 @@ public class OnUserSlashAction extends EventListener { // return false; // } - final String[] action = CommonCommand.format(text); + final String[] action = UniversalCommand.format(text); action[0] = action[0].substring(1); if (action[0].matches("^\\w+(@\\w+)?$")) { @@ -53,7 +53,7 @@ public class OnUserSlashAction extends EventListener { final boolean hasObject = action.length != (isHardParse?2:1); final String object = hasObject ? - StringArrays.connectStringArray(action, " ", isHardParse?2:1, action.length-1) : + stringsConnecting(action, " ", isHardParse?2:1, action.length-1) : ""; final Message origin = event.message(); final Message target = (event.message().replyToMessage() == null ? ( @@ -66,11 +66,11 @@ public class OnUserSlashAction extends EventListener { event.message().chat().id(), String.format( "%s %s%s %s %s!", - TGToStringFromMessage.as(origin).getSenderFirstNameRefHtml(), + TGToString.as(origin).getSenderFirstNameRefHtml(), escapeHtml(verb), escapeHtml((hasObject?"":"了")), origin==target ? - "自己" : - TGToStringFromMessage.as(target).getSenderFirstNameRefHtml(), + "自己" : + TGToString.as(target).getSenderFirstNameRefHtml(), escapeHtml(hasObject ? object+" " : "") ) ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/MyInformation.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/MyInformation.java index 7d5dcb1..53f666a 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/MyInformation.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/MyInformation.java @@ -8,7 +8,7 @@ import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; import com.pengrad.telegrambot.model.request.InputTextMessageContent; import com.pengrad.telegrambot.model.request.ParseMode; -import cc.sukazyo.cono.morny.util.TelegramUserInformation; +import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramUserInformation; public class MyInformation implements ITelegramQuery { diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java index 9fc02cf..907cdd5 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java @@ -1,5 +1,6 @@ package cc.sukazyo.cono.morny.util; +import javax.annotation.Nonnegative; import javax.annotation.Nonnull; /** @@ -33,4 +34,28 @@ public class CommonConvert { return hex.length()<2?"0"+hex:hex; } + /** + * 将一个字符串数组按照一定规则连接. + *

+ * 连接的方式类似于"数据1+分隔符+数据2+分隔符+...+数据n-1+分隔符+数据n" + * + * @param array 需要进行连接的字符串数组,数组中每一个元素会是一个数据 + * @param connector 在每两个传入数据中插入的分隔符 + * @param startIndex 从传入的数据组中的哪一个位置开始(第一个元素的位置是 {@code 0}) + * @param stopIndex 从传入的数据组中的哪一个位置停止(元素位置计算方式同上) + * @return 连接好的字符串 + */ + @Nonnull + public static String stringsConnecting ( + @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(); + } + } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/UniversalCommand.java b/src/main/java/cc/sukazyo/cono/morny/util/UniversalCommand.java new file mode 100644 index 0000000..f9e11b8 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/UniversalCommand.java @@ -0,0 +1,47 @@ +package cc.sukazyo.cono.morny.util; + +import javax.annotation.Nonnull; +import java.util.ArrayList; + +public class UniversalCommand { + + @Nonnull + public static String[] format (@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; + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/tgapi/ExtraAction.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/ExtraAction.java new file mode 100644 index 0000000..784e6c6 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/ExtraAction.java @@ -0,0 +1,88 @@ +package cc.sukazyo.cono.morny.util.tgapi; + +import cc.sukazyo.cono.morny.util.tgapi.event.EventRuntimeException; +import com.pengrad.telegrambot.TelegramBot; +import com.pengrad.telegrambot.model.Chat; +import com.pengrad.telegrambot.model.ChatMember; +import com.pengrad.telegrambot.model.User; +import com.pengrad.telegrambot.request.BaseRequest; +import com.pengrad.telegrambot.request.GetChatMember; +import com.pengrad.telegrambot.response.BaseResponse; + +public class ExtraAction { + + private final TelegramBot bot; + + public ExtraAction (TelegramBot bot) { + this.bot = bot; + } + + public static ExtraAction as (TelegramBot bot) { + return new ExtraAction(bot); + } + + public boolean isUserInGroup (User user, Chat chat) { + return isUserInGroup(user.id(), chat.id()); + } + + public , R extends BaseResponse> R exec (T req) { + return exec(req, ""); + } + + public , R extends BaseResponse> R exec (T req, String errorMessage) { + final R resp = bot.execute(req); + if (!resp.isOk()) throw new EventRuntimeException.ActionFailed( + (errorMessage.equals("") ? String.valueOf(resp.errorCode()) : errorMessage), + resp + ); + return resp; + } + + public boolean isUserInGroup (User user, Chat chat, ChatMember.Status permissionLevel) { + return isUserInGroup(user.id(), chat.id(), permissionLevel); + } + + public boolean isUserInGroup (long userId, long chatId) { + return isUserInGroup(userId, chatId, ChatMember.Status.restricted); + } + + public boolean isUserInGroup (long userId, long chatId, ChatMember.Status permissionLevel) { + final ChatMember chatMember = exec(new GetChatMember(chatId, userId)).chatMember(); + return + chatMember != null && + UserPermissionLevel.as(chatMember.status()).hasPermission(UserPermissionLevel.as(permissionLevel)); + } + +} + +enum UserPermissionLevel { + + CREATOR(3), + ADMINISTRATOR(2), + MEMBER(1), + RESTRICTED(0), + LEFT(-1), + KICKED(-2); + + final int permissionLevel; + + UserPermissionLevel (int permissionLevel) { + this.permissionLevel = permissionLevel; + } + + static UserPermissionLevel as (ChatMember.Status status) { + return switch (status) { + case creator -> CREATOR; + case administrator -> ADMINISTRATOR; + case member -> MEMBER; + case restricted -> RESTRICTED; + case left -> LEFT; + case kicked -> KICKED; + }; + } + + boolean hasPermission (UserPermissionLevel required) { + return this.permissionLevel >= required.permissionLevel; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/tgapi/InputCommand.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/InputCommand.java new file mode 100644 index 0000000..a62a3c9 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/InputCommand.java @@ -0,0 +1,66 @@ +package cc.sukazyo.cono.morny.util.tgapi; + +import cc.sukazyo.cono.morny.util.UniversalCommand; + +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(UniversalCommand.format(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/util/tgapi/event/EventRuntimeException.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/event/EventRuntimeException.java new file mode 100644 index 0000000..368d3b9 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/event/EventRuntimeException.java @@ -0,0 +1,35 @@ +package cc.sukazyo.cono.morny.util.tgapi.event; + +import com.pengrad.telegrambot.response.BaseResponse; + +public class EventRuntimeException extends RuntimeException { + + public EventRuntimeException () { + super(); + } + + public EventRuntimeException (String message) { + super(message); + } + + public static class ActionFailed extends EventRuntimeException { + + private final BaseResponse response; + + public ActionFailed (BaseResponse response) { + super(); + this.response = response; + } + + public ActionFailed (String message, BaseResponse response) { + super(message); + this.response = response; + } + + public BaseResponse getResponse() { + return response; + } + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/MsgEscape.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/MsgEscape.java new file mode 100644 index 0000000..06703a4 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/MsgEscape.java @@ -0,0 +1,15 @@ +package cc.sukazyo.cono.morny.util.tgapi.formatting; + +import javax.annotation.Nonnull; + +public class MsgEscape { + + @Nonnull + public static String escapeHtml (@Nonnull String raw) { + raw = raw.replaceAll("&", "&"); + raw = raw.replaceAll("<", "<"); + raw = raw.replaceAll(">", ">"); + return raw; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToString.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToString.java new file mode 100644 index 0000000..027f515 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToString.java @@ -0,0 +1,21 @@ +package cc.sukazyo.cono.morny.util.tgapi.formatting; + +import com.pengrad.telegrambot.model.Chat; +import com.pengrad.telegrambot.model.Message; +import com.pengrad.telegrambot.model.User; + +public class TGToString { + + public static TGToStringFromChat as (Chat chat) { + return new TGToStringFromChat(chat); + } + + public static TGToStringFromUser as (User user) { + return new TGToStringFromUser(user); + } + + public static TGToStringFromMessage as (Message message) { + return new TGToStringFromMessage(message); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromChat.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromChat.java new file mode 100644 index 0000000..8671056 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromChat.java @@ -0,0 +1,22 @@ +package cc.sukazyo.cono.morny.util.tgapi.formatting; + +import com.pengrad.telegrambot.model.Chat; + +public class TGToStringFromChat { + + private final Chat data; + + public TGToStringFromChat(Chat chat) { + this.data = chat; + } + + public String toStringFullNameId() { + if (data.title() == null) { + throw new IllegalArgumentException("Cannot format private chat to group Name+Id format."); + } + return (data.username() == null) ? + (String.format("%s [%d]", data.title(), data.id())) : + (String.format("%s {%s}[%d]", data.title(), data.username(), data.id())); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/tgapi/TGToStringFromMessage.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromMessage.java similarity index 71% rename from src/main/java/cc/sukazyo/cono/morny/util/tgapi/TGToStringFromMessage.java rename to src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromMessage.java index e4fac35..08b2d94 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/tgapi/TGToStringFromMessage.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromMessage.java @@ -1,7 +1,5 @@ -package cc.sukazyo.cono.morny.util.tgapi; +package cc.sukazyo.cono.morny.util.tgapi.formatting; -import cc.sukazyo.untitled.telegram.api.formatting.TGToString; -import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape; import com.pengrad.telegrambot.model.Message; import javax.annotation.Nonnull; @@ -12,7 +10,6 @@ public class TGToStringFromMessage extends TGToString { private final Message message; public TGToStringFromMessage (@Nonnull Message message) { this.message = message; } - public static TGToStringFromMessage as (@Nonnull Message message) { return new TGToStringFromMessage(message); } @Nonnull public String getSenderFirstNameRefHtml () { diff --git a/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromUser.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromUser.java new file mode 100644 index 0000000..fa41d3c --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromUser.java @@ -0,0 +1,53 @@ +package cc.sukazyo.cono.morny.util.tgapi.formatting; + +import com.pengrad.telegrambot.model.User; + +public class TGToStringFromUser { + + private final User data; + + public TGToStringFromUser (User user) { + this.data = user; + } + + public String fullname () { + return data.firstName() + (data.lastName()==null ? "" : " "+data.lastName()); + } + + public String fullnameRefHtml () { + return String.format( + "%s", + data.id(), + MsgEscape.escapeHtml(fullname()) + ); + } + + public String fullnameRefMarkdown () { + return String.format( + "[%s](tg://user?id=%d)", + fullname(), + data.id() + ); + } + + public String firstnameRefHtml () { + return String.format( + "%s", + data.id(), + MsgEscape.escapeHtml(data.firstName()) + ); + } + + public String firstnameRefMarkdown () { + return String.format( + "[%s](tg://user?id=%d)", + data.firstName(), + data.id() + ); + } + + public String toStringLogTag () { + return (data.username()==null ? fullname()+" " : "@"+data.username()) + "[" + data.id() + "]"; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TelegramUserInformation.java similarity index 94% rename from src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java rename to src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TelegramUserInformation.java index efa3257..e1703e4 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TelegramUserInformation.java @@ -1,4 +1,4 @@ -package cc.sukazyo.cono.morny.util; +package cc.sukazyo.cono.morny.util.tgapi.formatting; import com.pengrad.telegrambot.model.User; import okhttp3.OkHttpClient; @@ -11,7 +11,7 @@ import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; +import static cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape.escapeHtml; public class TelegramUserInformation { diff --git a/src/test/java/cc/sukazyo/cono/morny/MornyCLI.java b/src/test/java/cc/sukazyo/cono/morny/MornyCLI.java index f844d71..1b6eea7 100644 --- a/src/test/java/cc/sukazyo/cono/morny/MornyCLI.java +++ b/src/test/java/cc/sukazyo/cono/morny/MornyCLI.java @@ -1,6 +1,6 @@ package cc.sukazyo.cono.morny; -import cc.sukazyo.untitled.util.command.CommonCommand; +import cc.sukazyo.cono.morny.util.UniversalCommand; import java.util.*; @@ -9,9 +9,9 @@ public class MornyCLI { public static void main (String[] args) { Scanner line = new Scanner(System.in); - System.out.print("$ java -jar morny-coeur-"+GradleProjectConfigures.VERSION+".jar" ); + System.out.print("$ java -jar morny-coeur-"+GradleProjectConfigures.VERSION+".jar " ); String x = line.nextLine(); - ServerMain.main(CommonCommand.format(x)); + ServerMain.main(UniversalCommand.format(x)); }