From 063ca24bf51bb13c2bcbf31492e1d28ced3c2ee4 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 1 Oct 2022 17:24:59 +0800 Subject: [PATCH 01/14] =?UTF-8?q?=E6=95=B4=E6=94=B9=E4=BA=86=20utils=20?= =?UTF-8?q?=E5=8C=85=E5=BD=93=E4=B8=AD=E7=9A=84=20common=20=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E6=A0=BC=E5=BC=8F=20(WARN:breking-changes)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cc/sukazyo/cono/morny/MornyCoeur.java | 2 +- .../cc/sukazyo/cono/morny/ServerMain.java | 4 +- .../cono/morny/bot/command/MornyCommands.java | 4 +- .../cono/morny/bot/event/OnCallMe.java | 6 +- .../sukazyo/cono/morny/bot/query/RawText.java | 6 +- .../cono/morny/daemon/MedicationTimer.java | 4 +- .../cc/sukazyo/cono/morny/data/MornyJrrp.java | 5 +- .../cono/morny/util/CommonConvert.java | 36 +++++++++++ .../cono/morny/util/CommonEncrypt.java | 38 ++++++++++++ ...mmonFormatUtils.java => CommonFormat.java} | 6 +- .../sukazyo/cono/morny/util/EncryptUtils.java | 62 ------------------- 13 files changed, 98 insertions(+), 81 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java rename src/main/java/cc/sukazyo/cono/morny/util/{CommonFormatUtils.java => CommonFormat.java} (79%) delete mode 100644 src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java diff --git a/gradle.properties b/gradle.properties index eb58aeb..3fa9e53 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.2.1 +VERSION = 0.8.0.0 CODENAME = fuzhou diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 9dcb287..1d1528b 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.7.2.1"; + public static final String VERSION = "0.8.0.0"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1663602816303L; + public static final long COMPILE_TIMESTAMP = 1664615965858L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index d2cb07e..4e716b8 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -59,7 +59,7 @@ public class MornyCoeur { * {@link cc.sukazyo.cono.morny.bot.event.OnUpdateTimestampOffsetLock} * 会根据这里定义的时间戳取消掉比此时间更早的事件链 */ - public long latestEventTimestamp; + public final long latestEventTimestamp; /** * morny 主程序启动时间
* 用于统计数据 diff --git a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java index 9f58547..17a831f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java +++ b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java @@ -1,6 +1,6 @@ package cc.sukazyo.cono.morny; -import cc.sukazyo.cono.morny.util.CommonFormatUtils; +import cc.sukazyo.cono.morny.util.CommonFormat; import javax.annotation.Nonnull; @@ -205,7 +205,7 @@ public class ServerMain { MornySystem.VERSION, MornySystem.CODENAME.toUpperCase(), MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP, - CommonFormatUtils.formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0) + CommonFormat.formatDate(GradleProjectConfigures.COMPILE_TIMESTAMP, 0) )); return; 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 2918a80..968cd2d 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 @@ -27,8 +27,8 @@ import java.util.List; import java.util.Map; 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.CommonFormat.formatDate; +import static cc.sukazyo.cono.morny.util.CommonFormat.formatDuration; import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; public class MornyCommands { 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 1684d69..777fe40 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 @@ -4,7 +4,7 @@ import cc.sukazyo.cono.morny.MornyCoeur; 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.CommonFormatUtils; +import cc.sukazyo.cono.morny.util.CommonFormat; import cc.sukazyo.untitled.telegram.api.formatting.TGToString; import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape; import com.pengrad.telegrambot.model.Chat; @@ -122,9 +122,9 @@ public class OnCallMe extends EventListener { event.message().from().id(), String.format("on %s [UTC+8]\n- %s before", MsgEscape.escapeHtml( - CommonFormatUtils.formatDate((long)lastDinnerData.forwardDate()*1000, 8) + CommonFormat.formatDate((long)lastDinnerData.forwardDate()*1000, 8) ), MsgEscape.escapeHtml( - CommonFormatUtils.formatDuration(System.currentTimeMillis()-(long)lastDinnerData.forwardDate()*1000) + CommonFormat.formatDuration(System.currentTimeMillis()-(long)lastDinnerData.forwardDate()*1000) ) ) ).replyToMessageId(sendResp.message().messageId()).parseMode(ParseMode.HTML)); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java index 91333ed..f9f9d4f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java @@ -1,7 +1,6 @@ package cc.sukazyo.cono.morny.bot.query; import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit; -import cc.sukazyo.cono.morny.util.EncryptUtils; import javax.annotation.Nullable; @@ -9,6 +8,9 @@ import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; import com.pengrad.telegrambot.model.request.InputTextMessageContent; +import static cc.sukazyo.cono.morny.util.CommonConvert.byteArrayToHex; +import static cc.sukazyo.cono.morny.util.CommonEncrypt.encryptByMD5; + public class RawText implements ITelegramQuery { public static final String ID_PREFIX = "[morny/r/text]"; @@ -19,7 +21,7 @@ public class RawText implements ITelegramQuery { public InlineQueryUnit query (Update event) { if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null; return new InlineQueryUnit<>(new InlineQueryResultArticle( - ID_PREFIX + EncryptUtils.encryptByMD5(event.inlineQuery().query()), + ID_PREFIX + byteArrayToHex(encryptByMD5(event.inlineQuery().query())), TITLE, new InputTextMessageContent(event.inlineQuery().query()) )); diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java index 5932bb8..813640b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MedicationTimer.java @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.daemon; import cc.sukazyo.cono.morny.MornyCoeur; -import cc.sukazyo.cono.morny.util.CommonFormatUtils; +import cc.sukazyo.cono.morny.util.CommonFormat; import com.pengrad.telegrambot.model.Message; import com.pengrad.telegrambot.model.MessageEntity; import com.pengrad.telegrambot.model.request.ParseMode; @@ -54,7 +54,7 @@ public class MedicationTimer extends Thread { public void refreshNotificationWrite (Message edited) { if (edited.messageId() != lastNotify) return; - final String editTime = CommonFormatUtils.formatDate(edited.editDate()*1000, 8); + final String editTime = CommonFormat.formatDate(edited.editDate()*1000, 8); ArrayList entities = new ArrayList<>(); if (edited.entities() != null) entities.addAll(List.of(edited.entities())); entities.add(new MessageEntity(MessageEntity.Type.italic, edited.text().length() + "\n-- ".length(), editTime.length())); diff --git a/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java b/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java index 2fd858e..f3b2db1 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java @@ -1,6 +1,7 @@ package cc.sukazyo.cono.morny.data; -import cc.sukazyo.cono.morny.util.EncryptUtils; +import cc.sukazyo.cono.morny.util.CommonConvert; +import cc.sukazyo.cono.morny.util.CommonEncrypt; import com.pengrad.telegrambot.model.User; /** @@ -39,7 +40,7 @@ public class MornyJrrp { * @return 算法得到的 jrrp 值,取值为 {@code [0.00. 100.00]} */ public static double calcJrrpXmomi (long userId, long dayStamp) { - return (double)Long.parseLong(EncryptUtils.encryptByMD5(userId + "@" + dayStamp).substring(0, 4), 16) / (double)0xffff; + return (double)Long.parseLong(CommonConvert.byteArrayToHex(CommonEncrypt.encryptByMD5(userId + "@" + dayStamp)).substring(0, 4), 16) / (double)0xffff; } } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java new file mode 100644 index 0000000..eddf5fa --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java @@ -0,0 +1,36 @@ +package cc.sukazyo.cono.morny.util; + +import javax.annotation.Nonnull; + +public class CommonConvert { + + private final static String[] hexArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; + + /** + * 将字节数组转换成十六进制,并以字符串的形式返回 + * 128位是指二进制位。二进制太长,所以一般都改写成16进制, + * 每一位16进制数可以代替4位二进制数,所以128位二进制数写成16进制就变成了128/4=32位。 + */ + @Nonnull + public static String byteArrayToHex(@Nonnull byte[] b){ + StringBuilder sb = new StringBuilder(); + for (byte value : b) { + sb.append(byteToHex(value)); + } + return sb.toString(); + } + + /** + * 将一个字节转换成十六进制,并以字符串的形式返回 + */ + @Nonnull + public static String byteToHex(byte b) { + int n = b; + if (n < 0) + n = n + 256; + int d1 = n / 16; + int d2 = n % 16; + return hexArray[d1]+hexArray[d2]; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java new file mode 100644 index 0000000..7bcf21d --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java @@ -0,0 +1,38 @@ +package cc.sukazyo.cono.morny.util; + +import javax.annotation.Nonnull; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * 用于数据加密或编码的工具类
+ * 显然大部分代码是抄来的
+ * + */ +public class CommonEncrypt { + + public static final Charset ENCRYPT_STANDARD_CHARSET = StandardCharsets.UTF_8; + + /*** + * 对指定的字符串进行MD5加密 + */ + + @Nonnull + public static byte[] encryptByMD5(@Nonnull byte[] data) { + try { + return MessageDigest.getInstance("md5").digest(data); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(); + } + + } + @Nonnull + public static byte[] encryptByMD5(String originString) { + return encryptByMD5(originString.getBytes(ENCRYPT_STANDARD_CHARSET)); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonFormatUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonFormat.java similarity index 79% rename from src/main/java/cc/sukazyo/cono/morny/util/CommonFormatUtils.java rename to src/main/java/cc/sukazyo/cono/morny/util/CommonFormat.java index c629548..0598ba6 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/CommonFormatUtils.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonFormat.java @@ -6,10 +6,12 @@ import java.time.ZoneId; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; -public class CommonFormatUtils { +public class CommonFormat { + + public static final String DATE_TIME_PATTERN_FULL_MILLIS = "yyyy-MM-dd HH:mm:ss:SSS"; public static String formatDate (long timestamp, int utcOffset) { - return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS").format(LocalDateTime.ofInstant( + return DateTimeFormatter.ofPattern(DATE_TIME_PATTERN_FULL_MILLIS).format(LocalDateTime.ofInstant( Instant.ofEpochMilli(timestamp), ZoneId.ofOffset("UTC", ZoneOffset.ofHours(utcOffset)) )); diff --git a/src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java deleted file mode 100644 index 243389a..0000000 --- a/src/main/java/cc/sukazyo/cono/morny/util/EncryptUtils.java +++ /dev/null @@ -1,62 +0,0 @@ -package cc.sukazyo.cono.morny.util; - -import javax.annotation.Nonnull; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * 用于数据加密或编码的工具类
- * 显然大部分代码是抄来的
- * - */ -public class EncryptUtils { - - private final static String[] hexArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; - - /*** - * 对指定的字符串进行MD5加密 - */ - @Nonnull - public static String encryptByMD5(String originString) { - try { - //创建具有MD5算法的信息摘要 - MessageDigest md = MessageDigest.getInstance("MD5"); - //使用指定的字节数组对摘要进行最后更新,然后完成摘要计算 - byte[] bytes = md.digest(originString.getBytes()); - //将得到的字节数组变成字符串返回 - String s = byteArrayToHex(bytes); - return s.toUpperCase(); - } catch (NoSuchAlgorithmException e) { - throw new IllegalStateException(); - } - } - - /** - * 将字节数组转换成十六进制,并以字符串的形式返回 - * 128位是指二进制位。二进制太长,所以一般都改写成16进制, - * 每一位16进制数可以代替4位二进制数,所以128位二进制数写成16进制就变成了128/4=32位。 - */ - private static String byteArrayToHex(byte[] b){ - StringBuilder sb = new StringBuilder(); - for (byte value : b) { - sb.append(byteToHex(value)); - } - return sb.toString(); - } - - /** - * 将一个字节转换成十六进制,并以字符串的形式返回 - */ - public static String byteToHex(byte b) { - int n = b; - if (n < 0) - n = n + 256; - int d1 = n / 16; - int d2 = n % 16; - return hexArray[d1]+hexArray[d2]; - } - -} From 459470be02c78424b9a6a054f23f8f39179f2991 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 2 Oct 2022 02:18:26 +0800 Subject: [PATCH 02/14] =?UTF-8?q?common=20=E5=B7=A5=E5=85=B7=E5=8C=85?= =?UTF-8?q?=E6=94=B9=E5=8A=A8=E4=BA=86=E4=B8=80=E4=BA=9B=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=92=8C=E8=A1=A5=E5=85=85=20javadoc=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=BA=E5=85=B6=E6=B7=BB=E5=8A=A0=E4=BA=86=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Commons 工具组的一些实现改动 - _Convert.byteToHex 的实现从手动处理字母改为使用 java Integer.toHexString 方法(同时删除了私有的 hexArray 常量数组) - (WARN:breaking-changes) _Encrypt 中的命名从 encryptByX 改为 hashX - 添加了 junit 的 params 组件 (org.junit.jupiter:junit-jupiter-params) - 为 Commons 工具组的每个方法添加了其单元测试 - 单元测试添加了一个名为 MornyCLI 的执行 程序 main 方法 的程序入口包装... --- .gitignore | 3 +- build.gradle | 1 + gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../sukazyo/cono/morny/bot/query/RawText.java | 4 +- .../cc/sukazyo/cono/morny/data/MornyJrrp.java | 2 +- .../cono/morny/util/CommonConvert.java | 24 ++++----- .../cono/morny/util/CommonEncrypt.java | 34 +++++++++---- .../java/cc/sukazyo/cono/morny/MornyCLI.java | 18 +++++++ .../cono/morny/util/TestCommonConvert.java | 50 +++++++++++++++++++ .../cono/morny/util/TestCommonEncrypt.java | 23 +++++++++ .../cono/morny/util/TestCommonFormat.java | 36 +++++++++++++ 12 files changed, 171 insertions(+), 30 deletions(-) create mode 100644 src/test/java/cc/sukazyo/cono/morny/MornyCLI.java create mode 100644 src/test/java/cc/sukazyo/cono/morny/util/TestCommonConvert.java create mode 100644 src/test/java/cc/sukazyo/cono/morny/util/TestCommonEncrypt.java create mode 100644 src/test/java/cc/sukazyo/cono/morny/util/TestCommonFormat.java diff --git a/.gitignore b/.gitignore index 8520bdf..881a611 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,8 @@ .vscode/ .gradle/ .settings/ -/src/test/* +/src/test/java/test/* +/src/test/resources/test/* #build /build/ diff --git a/build.gradle b/build.gradle index 9e60e84..694232a 100644 --- a/build.gradle +++ b/build.gradle @@ -35,6 +35,7 @@ dependencies { implementation "com.github.pengrad:java-telegram-bot-api:${libJavaTelegramBotApiVersion}" testImplementation "org.junit.jupiter:junit-jupiter-api:${libJunitVersion}" + testImplementation "org.junit.jupiter:junit-jupiter-params:${libJunitVersion}" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${libJunitVersion}" } diff --git a/gradle.properties b/gradle.properties index 3fa9e53..eca4732 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.8.0.0 +VERSION = 0.8.0.1 CODENAME = fuzhou diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 1d1528b..27b871d 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.0"; + public static final String VERSION = "0.8.0.1"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1664615965858L; + public static final long COMPILE_TIMESTAMP = 1664648280419L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java index f9f9d4f..655d03a 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java @@ -9,7 +9,7 @@ import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; import com.pengrad.telegrambot.model.request.InputTextMessageContent; import static cc.sukazyo.cono.morny.util.CommonConvert.byteArrayToHex; -import static cc.sukazyo.cono.morny.util.CommonEncrypt.encryptByMD5; +import static cc.sukazyo.cono.morny.util.CommonEncrypt.hashMd5; public class RawText implements ITelegramQuery { @@ -21,7 +21,7 @@ public class RawText implements ITelegramQuery { public InlineQueryUnit query (Update event) { if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null; return new InlineQueryUnit<>(new InlineQueryResultArticle( - ID_PREFIX + byteArrayToHex(encryptByMD5(event.inlineQuery().query())), + ID_PREFIX + byteArrayToHex(hashMd5(event.inlineQuery().query())), TITLE, new InputTextMessageContent(event.inlineQuery().query()) )); diff --git a/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java b/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java index f3b2db1..029e476 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/MornyJrrp.java @@ -40,7 +40,7 @@ public class MornyJrrp { * @return 算法得到的 jrrp 值,取值为 {@code [0.00. 100.00]} */ public static double calcJrrpXmomi (long userId, long dayStamp) { - return (double)Long.parseLong(CommonConvert.byteArrayToHex(CommonEncrypt.encryptByMD5(userId + "@" + dayStamp)).substring(0, 4), 16) / (double)0xffff; + return (double)Long.parseLong(CommonConvert.byteArrayToHex(CommonEncrypt.hashMd5(userId + "@" + dayStamp)).substring(0, 4), 16) / (double)0xffff; } } 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 eddf5fa..9fc02cf 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java @@ -2,14 +2,16 @@ package cc.sukazyo.cono.morny.util; import javax.annotation.Nonnull; +/** + * 进行简单类型转换等工作的类. + */ public class CommonConvert { - private final static String[] hexArray = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; - /** - * 将字节数组转换成十六进制,并以字符串的形式返回 - * 128位是指二进制位。二进制太长,所以一般都改写成16进制, - * 每一位16进制数可以代替4位二进制数,所以128位二进制数写成16进制就变成了128/4=32位。 + * 将字节数组转换成 hex 字符串. + * @param b 字节数组 + * @return String 格式的字节数组的 hex 值(每个字节当中没有分隔符) + * @see #byteToHex(byte) */ @Nonnull public static String byteArrayToHex(@Nonnull byte[] b){ @@ -21,16 +23,14 @@ public class CommonConvert { } /** - * 将一个字节转换成十六进制,并以字符串的形式返回 + * 将一个字节转换成十六进制 hex 字符串. + * @param b 字节值 + * @return String 格式的字节的 hex 值(小写) */ @Nonnull public static String byteToHex(byte b) { - int n = b; - if (n < 0) - n = n + 256; - int d1 = n / 16; - int d2 = n % 16; - return hexArray[d1]+hexArray[d2]; + final String hex = Integer.toHexString(b & 0xff); + return hex.length()<2?"0"+hex:hex; } } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java index 7bcf21d..f0dcedd 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java @@ -7,22 +7,25 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** - * 用于数据加密或编码的工具类
- * 显然大部分代码是抄来的
- * + * 用于数据加密或编解码的工具类. */ public class CommonEncrypt { + /** + * 在使用加密算法处理字符串时默认会使用的字符串编码. + *

+ * Morny 使用 UTF-8 编码因为这是一般而言加解密工具的默认行为 + */ public static final Charset ENCRYPT_STANDARD_CHARSET = StandardCharsets.UTF_8; - /*** - * 对指定的字符串进行MD5加密 + /** + * 取得数据的 md5 散列值. + * + * @param data byte 数组形式的数据体 + * @return 二进制(byte数组)格式的数据的 md5 散列值 */ - @Nonnull - public static byte[] encryptByMD5(@Nonnull byte[] data) { + public static byte[] hashMd5 (@Nonnull byte[] data) { try { return MessageDigest.getInstance("md5").digest(data); } catch (NoSuchAlgorithmException e) { @@ -30,9 +33,18 @@ public class CommonEncrypt { } } + + /** + * 取得一个字符串的 md5 散列值. + *

+ * 输入的字符串将会以 {@link #ENCRYPT_STANDARD_CHARSET 默认的 UTF-8} 编码进行解析 + * + * @param originString 要进行散列的字符串 + * @return 二进制(byte数组)格式的 md5 散列值 + */ @Nonnull - public static byte[] encryptByMD5(String originString) { - return encryptByMD5(originString.getBytes(ENCRYPT_STANDARD_CHARSET)); + public static byte[] hashMd5 (String originString) { + return hashMd5(originString.getBytes(ENCRYPT_STANDARD_CHARSET)); } } diff --git a/src/test/java/cc/sukazyo/cono/morny/MornyCLI.java b/src/test/java/cc/sukazyo/cono/morny/MornyCLI.java new file mode 100644 index 0000000..f844d71 --- /dev/null +++ b/src/test/java/cc/sukazyo/cono/morny/MornyCLI.java @@ -0,0 +1,18 @@ +package cc.sukazyo.cono.morny; + +import cc.sukazyo.untitled.util.command.CommonCommand; + +import java.util.*; + +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" ); + String x = line.nextLine(); + ServerMain.main(CommonCommand.format(x)); + + } + +} diff --git a/src/test/java/cc/sukazyo/cono/morny/util/TestCommonConvert.java b/src/test/java/cc/sukazyo/cono/morny/util/TestCommonConvert.java new file mode 100644 index 0000000..8fd9ed7 --- /dev/null +++ b/src/test/java/cc/sukazyo/cono/morny/util/TestCommonConvert.java @@ -0,0 +1,50 @@ +package cc.sukazyo.cono.morny.util; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static cc.sukazyo.cono.morny.util.CommonConvert.byteArrayToHex; +import static cc.sukazyo.cono.morny.util.CommonConvert.byteToHex; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +public class TestCommonConvert { + + @ParameterizedTest + @CsvSource(textBlock = """ + 0x00, 00 + 0x01, 01 + 0x20, 20 + 0x77, 77 + -0x60, a0 + 0x0a, 0a + -0x01, ff + -0x05, fb + """ + ) + void testByteToHex(byte source, String expected) { + assertEquals(expected, byteToHex(source)); + } + + public static Stream testByteArrayToHexProvider () { + return Stream.of( + arguments(new byte[]{0x00}, "00"), + arguments(new byte[]{(byte)0xff}, "ff"), + arguments(new byte[]{(byte)0xc3}, "c3"), + arguments(new byte[]{}, ""), + arguments(new byte[]{0x30,0x0a,0x00,0x04,(byte)0xb0,0x00}, "300a0004b000"), + arguments(new byte[]{0x00,0x00,0x0a,(byte)0xff,(byte)0xfc,(byte)0xab,(byte)0x00,0x04}, "00000afffcab0004"), + arguments(new byte[]{0x00,0x7c,0x11,0x28,(byte)0x88,(byte)0xa6,(byte)0xfc,0x30}, "007c112888a6fc30") + ); + } + @ParameterizedTest + @MethodSource("testByteArrayToHexProvider") + void testByteArrayToHex (byte[] raw, String expected) { + assertEquals(expected, byteArrayToHex(raw)); + } + +} diff --git a/src/test/java/cc/sukazyo/cono/morny/util/TestCommonEncrypt.java b/src/test/java/cc/sukazyo/cono/morny/util/TestCommonEncrypt.java new file mode 100644 index 0000000..429b163 --- /dev/null +++ b/src/test/java/cc/sukazyo/cono/morny/util/TestCommonEncrypt.java @@ -0,0 +1,23 @@ +package cc.sukazyo.cono.morny.util; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static cc.sukazyo.cono.morny.util.CommonConvert.byteArrayToHex; +import static cc.sukazyo.cono.morny.util.CommonEncrypt.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestCommonEncrypt { + + @ParameterizedTest + @SuppressWarnings("UnnecessaryStringEscape") + @CsvSource(textBlock = """ + 28be57d368b75051da76c068a6733284, '莲子' + 9644c5cbae223013228cd528817ba4f5, '莲子\n' + d41d8cd98f00b204e9800998ecf8427e, '' + """) + void testHashMd5_String (String md5, String text) { + assertEquals(md5, byteArrayToHex(hashMd5(text))); + } + +} diff --git a/src/test/java/cc/sukazyo/cono/morny/util/TestCommonFormat.java b/src/test/java/cc/sukazyo/cono/morny/util/TestCommonFormat.java new file mode 100644 index 0000000..d7b67fe --- /dev/null +++ b/src/test/java/cc/sukazyo/cono/morny/util/TestCommonFormat.java @@ -0,0 +1,36 @@ +package cc.sukazyo.cono.morny.util; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static cc.sukazyo.cono.morny.util.CommonFormat.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestCommonFormat { + + @ParameterizedTest + @CsvSource(textBlock = """ + 1664646870402, 8, 2022-10-02 01:54:30:402 + 1, 8, 1970-01-01 08:00:00:001 + 0, -1, 1969-12-31 23:00:00:000 + """ + ) + void testFormatDate (long timestamp, int utfOffset, String expectedHumanReadableTime) { + assertEquals(expectedHumanReadableTime, formatDate(timestamp, utfOffset)); + } + + @ParameterizedTest + @CsvSource(textBlock = """ + 100, '100ms' + 3000, '3s 0ms' + 326117522, '3d 18h 35min 17s 522ms' + 53373805, 14h 49min 33s 805ms + """) +// -1, '-1ms' // WARN: maybe sometime an unexpected usage +// -194271974291, '-291ms' // +// """) // + void testFormatDuration (long durationMillis, String humanReadableDuration) { + assertEquals(humanReadableDuration, formatDuration(durationMillis)); + } + +} From afef4f0345f725b69721b61b609b632cff0b4e3c Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 4 Oct 2022 15:18:42 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E8=84=B1=E7=A6=BB=20unititled=20?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E4=BE=9D=E8=B5=96=EF=BC=8C=E5=B0=86=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E5=85=A8=E9=83=A8=E7=A7=BB=E5=8A=A8=E8=87=B3?= =?UTF-8?q?=20util=20=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 9 -- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cc/sukazyo/cono/morny/MornyCoeur.java | 2 +- .../morny/bot/api/EventListenerManager.java | 3 +- .../morny/bot/command/DirectMsgClear.java | 2 +- .../cono/morny/bot/command/EventHack.java | 2 +- .../morny/bot/command/GetUsernameAndId.java | 4 +- .../morny/bot/command/ISimpleCommand.java | 2 +- .../cono/morny/bot/command/Ip186Query.java | 5 +- .../cono/morny/bot/command/MornyCommands.java | 6 +- .../cono/morny/bot/command/Nbnhhsh.java | 8 +- .../sukazyo/cono/morny/bot/command/喵呜.java | 2 +- .../cono/morny/bot/command/私わね.java | 2 +- .../cono/morny/bot/event/OnCallMe.java | 4 +- .../cono/morny/bot/event/OnCallMsgSend.java | 3 +- .../morny/bot/event/OnEventHackHandle.java | 2 +- .../morny/bot/event/OnTelegramCommand.java | 2 +- .../cono/morny/bot/event/OnUserRandoms.java | 4 +- .../morny/bot/event/OnUserSlashAction.java | 18 ++-- .../cono/morny/bot/query/MyInformation.java | 2 +- .../cono/morny/util/CommonConvert.java | 25 ++++++ .../cono/morny/util/UniversalCommand.java | 47 ++++++++++ .../cono/morny/util/tgapi/ExtraAction.java | 88 +++++++++++++++++++ .../cono/morny/util/tgapi/InputCommand.java | 66 ++++++++++++++ .../tgapi/event/EventRuntimeException.java | 35 ++++++++ .../util/tgapi/formatting/MsgEscape.java | 15 ++++ .../util/tgapi/formatting/TGToString.java | 21 +++++ .../tgapi/formatting/TGToStringFromChat.java | 22 +++++ .../TGToStringFromMessage.java | 5 +- .../tgapi/formatting/TGToStringFromUser.java | 53 +++++++++++ .../formatting}/TelegramUserInformation.java | 4 +- .../java/cc/sukazyo/cono/morny/MornyCLI.java | 6 +- 32 files changed, 417 insertions(+), 56 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/UniversalCommand.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/tgapi/ExtraAction.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/tgapi/InputCommand.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/tgapi/event/EventRuntimeException.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/MsgEscape.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToString.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromChat.java rename src/main/java/cc/sukazyo/cono/morny/util/tgapi/{ => formatting}/TGToStringFromMessage.java (71%) create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/TGToStringFromUser.java rename src/main/java/cc/sukazyo/cono/morny/util/{ => tgapi/formatting}/TelegramUserInformation.java (94%) 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)); } From 7bb179658e19d06ce6b41a63225145ef1a44fb97 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 5 Oct 2022 21:19:45 +0800 Subject: [PATCH 04/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8F=AF=E4=BB=A5=E6=8A=BD=E5=8F=96=20"debian?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E9=95=9C=E5=83=8F"=20=E7=9A=84=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=20/install?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 4 +--- .../cono/morny/GradleProjectConfigures.java | 4 ++-- .../cono/morny/bot/command/MornyCommands.java | 4 +++- .../cc/sukazyo/cono/morny/bot/command/喵呜.java | 15 +++++++++++++++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/gradle.properties b/gradle.properties index eca4732..40dc41e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.8.0.1 +VERSION = 0.8.0.3 CODENAME = fuzhou @@ -8,8 +8,6 @@ CODENAME = fuzhou libSpotbugsVersion = 4.7.2 -libUntitledVersionMajor = 1 - libMessivaVersion = 0.1.0.1 libJavaTelegramBotApiVersion = 5.6.0 diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index fae76ac..3f7956d 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.2"; + public static final String VERSION = "0.8.0.3"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1664867846222L; + public static final long COMPILE_TIMESTAMP = 1664975839021L; } 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 8233cf1..6c7191c 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 @@ -57,6 +57,7 @@ public class MornyCommands { } } + @SuppressWarnings("NonAsciiCharacters") public MornyCommands () { register( @@ -85,7 +86,8 @@ public class MornyCommands { new 喵呜.揉揉(), new 喵呜.蹭蹭(), new 喵呜.贴贴(), - new 私わね() + new 私わね(), + new 喵呜.Progynova() ); } 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 b9931f9..2bdbb7d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java @@ -1,10 +1,12 @@ package cc.sukazyo.cono.morny.bot.command; import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.data.TelegramStickers; 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 com.pengrad.telegrambot.request.SendSticker; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -56,4 +58,17 @@ public class 喵呜 { } } + public static class Progynova implements ITelegramCommand { + @Nonnull @Override public String getName () { return "install"; } + @Nullable @Override public String[] getAliases () { return new String[0]; } + @Nonnull @Override public String getParamRule () { return ""; } + @Nonnull @Override public String getDescription () { return "抽取一个神秘盒子"; } + @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { + MornyCoeur.extra().exec(new SendSticker( + event.message().chat().id(), + TelegramStickers.ID_PROGYNOVA + ).replyToMessageId(event.message().messageId())); + } + } + } From dd8f7c0cd52e3a62ba73ba837180c13b1ad03367 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sun, 9 Oct 2022 16:50:42 +0800 Subject: [PATCH 05/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=20/encrypt=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=91=BD=E4=BB=A4=20(currently=E5=8F=AA?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=9B=9E=E5=A4=8D=E6=96=87=E6=9C=AC=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E4=BD=9C=E4=B8=BA=E6=95=B0=E6=8D=AE=E4=BD=93)=20?= =?UTF-8?q?=E9=99=84=E5=B8=A6=20uppercase=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/command/EncUtils.java | 116 ++++++++++++++++++ .../cono/morny/bot/command/MornyCommands.java | 1 + .../cono/morny/util/CommonEncrypt.java | 91 +++++++++++++- 5 files changed, 205 insertions(+), 9 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java diff --git a/gradle.properties b/gradle.properties index 40dc41e..4e325fa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.8.0.3 +VERSION = 0.8.0.4 CODENAME = fuzhou diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 3f7956d..80572d4 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.3"; + public static final String VERSION = "0.8.0.4"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1664975839021L; + public static final long COMPILE_TIMESTAMP = 1665305196411L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java new file mode 100644 index 0000000..dabdbb8 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java @@ -0,0 +1,116 @@ +package cc.sukazyo.cono.morny.bot.command; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.data.TelegramStickers; +import cc.sukazyo.cono.morny.util.CommonConvert; +import cc.sukazyo.cono.morny.util.CommonEncrypt; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; +import cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.SendMessage; +import com.pengrad.telegrambot.request.SendSticker; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Base64; + +public class EncUtils implements ITelegramCommand { + + @Nonnull @Override public String getName () { return "encrypt"; } + @Nullable @Override public String[] getAliases () { return new String[0]; } + @Nonnull @Override public String getParamRule () { return "[algorithm|(l)] [(uppercase)]"; } + @Nonnull @Override public String getDescription () { return "通过指定算法加密回复的内容 (目前只支持文本)"; } + + @Override + public void execute (@Nonnull InputCommand command, @Nonnull Update event) { + + // show a simple help page + // the first paragraph lists available encrypt algorithms, and its aliases. + // with the separator "---", + // the second paragraphs shows the mods available and its aliases. + if (!command.hasArgs() || (command.getArgs()[0].equals("l") && command.getArgs().length==1)) { + MornyCoeur.extra().exec(new SendMessage( + event.message().chat().id(), """ + base64, b64 + base64decode, base64d, b64d + sha1 + sha256 + sha512 + md5 + --- + uppercase, upper, u (sha1/sha256/sha512/md5 only) + """ + ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + return; + } + + // param1 is the encrypting algorithm, it MUST EXIST. + // so the mod will be set in param2. + // and for now only support UPPERCASE mod, so it exists in param2, or there should no any params. + boolean modUpperCase = false; + if (command.getArgs().length > 1) { + if (command.getArgs().length < 3 && ( + command.getArgs()[1].equalsIgnoreCase("uppercase") || + command.getArgs()[1].equalsIgnoreCase("u") || + command.getArgs()[1].equalsIgnoreCase("upper") + )) { + modUpperCase = true; + } else { + MornyCoeur.extra().exec(new SendSticker( + event.message().chat().id(), TelegramStickers.ID_404 + ).replyToMessageId(event.message().messageId())); + return; + } + } + + // for now, only support reply to A TEXT MESSAGE and encrypt/hash the text value. + // if there's no text message in reply, it will report null as result. + if (event.message().replyToMessage() == null || event.message().replyToMessage().text() == null) { + MornyCoeur.extra().exec(new SendMessage( + event.message().chat().id(), + "null" + ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + return; + } + final String data = event.message().replyToMessage().text(); + + String result; + switch (command.getArgs()[0]) { + case "base64", "b64" -> result = Base64.getEncoder().encodeToString(data.getBytes(CommonEncrypt.ENCRYPT_STANDARD_CHARSET)); + case "base64decode", "base64d", "b64d" -> result = new String( + Base64.getDecoder().decode(data.getBytes(CommonEncrypt.ENCRYPT_STANDARD_CHARSET)), CommonEncrypt.ENCRYPT_STANDARD_CHARSET); + case "md5" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashMd5(data)); + case "sha1" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha1(data)); + case "sha256" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha256(data)); + case "sha512" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha512(data)); + default -> { + MornyCoeur.extra().exec(new SendSticker( + event.message().chat().id(), TelegramStickers.ID_404 + ).replyToMessageId(event.message().messageId())); + return; + } + } + if (modUpperCase) { + // modUpperCase support only algorithm that showed as HEX value. + // it means md5, sha1, sha256, sha512 here. + // other will report wrong param. + switch (command.getArgs()[0]) { + case "md5", "sha1", "sha256", "sha512" -> + result = result.toUpperCase(); + default -> { + MornyCoeur.extra().exec(new SendSticker( + event.message().chat().id(), TelegramStickers.ID_404 + ).replyToMessageId(event.message().messageId())); + return; + } + } + } + MornyCoeur.extra().exec(new SendMessage( + event.message().chat().id(), + "

" + MsgEscape.escapeHtml(result) + "
" + ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + + } + +} 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 6c7191c..85c968a 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 @@ -68,6 +68,7 @@ public class MornyCommands { new Nbnhhsh(), new Ip186Query.Ip(), new Ip186Query.Whois(), + new EncUtils(), new SaveData(), new Version(), new MornyRuntime(), diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java index f0dcedd..256a217 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java @@ -5,9 +5,12 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.Base64; /** * 用于数据加密或编解码的工具类. + *

+ * 出于 java std 中 Base64 的 {@link Base64.Encoder encode}/{@link Base64.Decoder decode} 十分好用,在此不再进行包装。 */ public class CommonEncrypt { @@ -18,6 +21,15 @@ public class CommonEncrypt { */ public static final Charset ENCRYPT_STANDARD_CHARSET = StandardCharsets.UTF_8; + @Nonnull + private static byte[] hashAsJavaMessageDigest(String algorithm, @Nonnull byte[] data) { + try { + return MessageDigest.getInstance(algorithm).digest(data); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException(e); + } + } + /** * 取得数据的 md5 散列值. * @@ -26,12 +38,7 @@ public class CommonEncrypt { */ @Nonnull public static byte[] hashMd5 (@Nonnull byte[] data) { - try { - return MessageDigest.getInstance("md5").digest(data); - } catch (NoSuchAlgorithmException e) { - throw new IllegalStateException(); - } - + return hashAsJavaMessageDigest("md5", data); } /** @@ -47,4 +54,76 @@ public class CommonEncrypt { return hashMd5(originString.getBytes(ENCRYPT_STANDARD_CHARSET)); } + /** + * 取得数据的 sha1 散列值. + * + * @param data byte 数组形式的数据体 + * @return 二进制(byte数组)格式的数据的 sha1 散列值 + */ + @Nonnull + public static byte[] hashSha1 (@Nonnull byte[] data) { + return hashAsJavaMessageDigest("sha1", data); + } + + /** + * 取得一个字符串的 sha1 散列值. + *

+ * 输入的字符串将会以 {@link #ENCRYPT_STANDARD_CHARSET 默认的 UTF-8} 编码进行解析 + * + * @param originString 要进行散列的字符串 + * @return 二进制(byte数组)格式的 sha1 散列值 + */ + @Nonnull + public static byte[] hashSha1 (String originString) { + return hashMd5(originString.getBytes(ENCRYPT_STANDARD_CHARSET)); + } + + /** + * 取得数据的 sha256 散列值. + * + * @param data byte 数组形式的数据体 + * @return 二进制(byte数组)格式的数据的 sha256 散列值 + */ + @Nonnull + public static byte[] hashSha256 (@Nonnull byte[] data) { + return hashAsJavaMessageDigest("sha256", data); + } + + /** + * 取得一个字符串的 sha256 散列值. + *

+ * 输入的字符串将会以 {@link #ENCRYPT_STANDARD_CHARSET 默认的 UTF-8} 编码进行解析 + * + * @param originString 要进行散列的字符串 + * @return 二进制(byte数组)格式的 sha256 散列值 + */ + @Nonnull + public static byte[] hashSha256 (String originString) { + return hashMd5(originString.getBytes(ENCRYPT_STANDARD_CHARSET)); + } + + /** + * 取得数据的 sha512 散列值. + * + * @param data byte 数组形式的数据体 + * @return 二进制(byte数组)格式的数据的 sha512 散列值 + */ + @Nonnull + public static byte[] hashSha512 (@Nonnull byte[] data) { + return hashAsJavaMessageDigest("md5", data); + } + + /** + * 取得一个字符串的 sha512 散列值. + *

+ * 输入的字符串将会以 {@link #ENCRYPT_STANDARD_CHARSET 默认的 UTF-8} 编码进行解析 + * + * @param originString 要进行散列的字符串 + * @return 二进制(byte数组)格式的 sha512 散列值 + */ + @Nonnull + public static byte[] hashSha512 (String originString) { + return hashMd5(originString.getBytes(ENCRYPT_STANDARD_CHARSET)); + } + } From e947c3f23928030428cbb080411fd14c634cb4c7 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 15 Oct 2022 00:01:35 +0800 Subject: [PATCH 06/14] =?UTF-8?q?=E4=B8=BA=20/encrypt=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86=E7=AE=80=E5=8D=95=E7=9A=84=E6=96=87=E4=BB=B6=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=94=AF=E6=8C=81=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=20NETWORK=5FERR=20=E5=8F=8D=E9=A6=88=E8=B4=B4=E7=BA=B8?= =?UTF-8?q?=EF=BC=88=EF=BC=8C=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=B2=A1=E7=94=A8=E7=9A=84=20/test=20=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/command/EncUtils.java | 96 +++++++++++++++---- .../cono/morny/bot/command/MornyCommands.java | 1 + .../cono/morny/bot/command/Testing.java | 36 +++++++ .../cono/morny/data/TelegramStickers.java | 1 + .../cono/morny/util/CommonEncrypt.java | 15 +++ 7 files changed, 135 insertions(+), 20 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/command/Testing.java diff --git a/gradle.properties b/gradle.properties index 4e325fa..3c7131f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.8.0.4 +VERSION = 0.8.0.5 CODENAME = fuzhou diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 80572d4..b1ea0ac 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.4"; + public static final String VERSION = "0.8.0.5"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1665305196411L; + public static final long COMPILE_TIMESTAMP = 1665763018652L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java index dabdbb8..1c439e0 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java @@ -8,13 +8,18 @@ import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.GetFile; +import com.pengrad.telegrambot.request.SendDocument; import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.request.SendSticker; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.io.IOException; import java.util.Base64; +import static cc.sukazyo.cono.morny.Log.logger; + public class EncUtils implements ITelegramCommand { @Nonnull @Override public String getName () { return "encrypt"; } @@ -64,26 +69,74 @@ public class EncUtils implements ITelegramCommand { } } - // for now, only support reply to A TEXT MESSAGE and encrypt/hash the text value. + // for now, only support reply to A TEXT MESSAGE or ONE UNIVERSAL FILE + // if the replied message contains a UNIVERSAL FILE, it will use the file and will not use the text with it + // do not support TELEGRAM INLINE IMAGE/VIDEO/AUDIO yet + // do not support MULTI_FILE yet // if there's no text message in reply, it will report null as result. - if (event.message().replyToMessage() == null || event.message().replyToMessage().text() == null) { + boolean inputText; + byte[] data; + String dataName; + if (event.message().replyToMessage() != null && event.message().replyToMessage().document() != null) { + inputText = false; + try { + data = MornyCoeur.getAccount().getFileContent(MornyCoeur.extra().exec(new GetFile( + event.message().replyToMessage().document().fileId() + )).file()); + } catch (IOException e) { + logger.warn("NetworkRequest error: TelegramFileAPI:\n\t" + e.getMessage()); + MornyCoeur.extra().exec(new SendSticker( + event.message().chat().id(), + TelegramStickers.ID_NETWORK_ERR + ).replyToMessageId(event.message().messageId())); + return; + } + dataName = event.message().replyToMessage().document().fileName(); + } else if (event.message().replyToMessage() != null && event.message().replyToMessage().text() != null) { + inputText = true; + data = event.message().replyToMessage().text().getBytes(CommonEncrypt.ENCRYPT_STANDARD_CHARSET); + dataName = null; + } else { MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), "null" ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); return; } - final String data = event.message().replyToMessage().text(); - String result; + boolean echoString = true; + String resultString = null; + byte[] result = null; + String resultName = null; switch (command.getArgs()[0]) { - case "base64", "b64" -> result = Base64.getEncoder().encodeToString(data.getBytes(CommonEncrypt.ENCRYPT_STANDARD_CHARSET)); - case "base64decode", "base64d", "b64d" -> result = new String( - Base64.getDecoder().decode(data.getBytes(CommonEncrypt.ENCRYPT_STANDARD_CHARSET)), CommonEncrypt.ENCRYPT_STANDARD_CHARSET); - case "md5" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashMd5(data)); - case "sha1" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha1(data)); - case "sha256" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha256(data)); - case "sha512" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha512(data)); + case "base64", "b64" -> { + result = Base64.getEncoder().encode(data); + if (!inputText) { + echoString = false; + resultName = dataName+".b64.txt"; + } else { + resultString = new String(result, CommonEncrypt.ENCRYPT_STANDARD_CHARSET); + } + } + case "base64decode", "base64d", "b64d" -> { + try { result = Base64.getDecoder().decode(data); } + catch (IllegalArgumentException e) { + MornyCoeur.extra().exec(new SendSticker( + event.message().chat().id(), TelegramStickers.ID_404 + ).replyToMessageId(event.message().messageId())); + return; + } + if (!inputText) { + echoString = false; + resultName = CommonEncrypt.base64FilenameLint(dataName); + } else { + resultString = new String(result, CommonEncrypt.ENCRYPT_STANDARD_CHARSET); + } + } + case "md5" -> resultString = CommonConvert.byteArrayToHex(CommonEncrypt.hashMd5(data)); + case "sha1" -> resultString = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha1(data)); + case "sha256" -> resultString = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha256(data)); + case "sha512" -> resultString = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha512(data)); default -> { MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_404 @@ -96,8 +149,10 @@ public class EncUtils implements ITelegramCommand { // it means md5, sha1, sha256, sha512 here. // other will report wrong param. switch (command.getArgs()[0]) { - case "md5", "sha1", "sha256", "sha512" -> - result = result.toUpperCase(); + case "md5", "sha1", "sha256", "sha512" -> { + assert resultString != null; + resultString = resultString.toUpperCase(); + } default -> { MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_404 @@ -106,10 +161,17 @@ public class EncUtils implements ITelegramCommand { } } } - MornyCoeur.extra().exec(new SendMessage( - event.message().chat().id(), - "

" + MsgEscape.escapeHtml(result) + "
" - ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + if (echoString) { + MornyCoeur.extra().exec(new SendMessage( + event.message().chat().id(), + "
" + MsgEscape.escapeHtml(resultString) + "
" + ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + } else { + MornyCoeur.extra().exec(new SendDocument( + event.message().chat().id(), + result + ).fileName(resultName).replyToMessageId(event.message().messageId())); + } } 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 85c968a..381dc36 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 @@ -78,6 +78,7 @@ public class MornyCommands { // 特殊的命令 register( + new Testing(), new DirectMsgClear() ); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/Testing.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/Testing.java new file mode 100644 index 0000000..d1bfb80 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/Testing.java @@ -0,0 +1,36 @@ +package cc.sukazyo.cono.morny.bot.command; + +import cc.sukazyo.cono.morny.MornyCoeur; +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 javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class Testing implements ISimpleCommand { + + @Nonnull + @Override + public String getName () { + return "test"; + } + + @Nullable + @Override + public String[] getAliases () { + return null; + } + + @Override + public void execute (@Nonnull InputCommand command, @Nonnull Update event) { + + MornyCoeur.extra().exec(new SendMessage( + event.message().chat().id(), + "Just a TEST command." + ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); + + } + +} 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 ecf4921..0acd42b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java @@ -15,5 +15,6 @@ public class TelegramStickers { public static final String ID_SENT = "CAACAgEAAx0CSQh32gABA--zYbiyU_wOijEitp-0tSl_k7W6l3gAAgMmAAJ4_MYF4GrompjXPx4jBA"; public static final String ID_SAVED = "CAACAgEAAx0CSQh32gABBExuYdB_G0srfhQldRWkBYxWzCOv4-IAApooAAJ4_MYFcjuNZszfQcQjBA"; public static final String ID_PROGYNOVA = "CAACAgUAAxkBAAICm2KEuL7UQqNP7vSPCg2DHJIND6UsAAKLAwACH4WSBszIo722aQ3jJAQ"; + public static final String ID_NETWORK_ERR = "CAACAgEAAxkBAAID0WNJgNEkD726KW4vZeFlw0FlVVyNAAIXJgACePzGBb50o7O1RbxoKgQ"; } diff --git a/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java index 256a217..0e9c35d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java @@ -126,4 +126,19 @@ public class CommonEncrypt { return hashMd5(originString.getBytes(ENCRYPT_STANDARD_CHARSET)); } + @Nonnull + public static String base64FilenameLint (String inputName) { + if (inputName.endsWith(".b64")) { + return inputName.substring(0, inputName.length()-".b64".length()); + } else if (inputName.endsWith(".b64.txt")) { + return inputName.substring(0, inputName.length()-".b64.txt".length()); + } else if (inputName.endsWith(".base64")) { + return inputName.substring(0, inputName.length()-".base64".length()); + } else if (inputName.endsWith(".base64.txt")) { + return inputName.substring(0, inputName.length()-".base64.txt".length()); + } else { + return inputName; + } + } + } From f195d5c1bbec1a5da013834ab7195d97336d2495 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 15 Oct 2022 17:16:03 +0800 Subject: [PATCH 07/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=20/info=20sti?= =?UTF-8?q?ckers=20=E5=91=BD=E4=BB=A4=E5=8F=AF=E4=BB=A5(=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E5=8F=8D=E5=B0=84)=E5=88=97=E5=87=BA=20morny=20=E6=89=80?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E7=9A=84=E6=89=80=E6=9C=89=E8=B4=B4=E7=BA=B8?= =?UTF-8?q?=E6=88=96=E6=98=AF=E6=9F=90=E8=B4=B4=E7=BA=B8=E5=90=8D=E7=A7=B0?= =?UTF-8?q?=E6=89=80=E5=AE=9A=E4=B9=89=E7=9A=84=E8=B4=B4=E7=BA=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../command/{EncUtils.java => Encryptor.java} | 2 +- .../cono/morny/bot/command/MornyCommands.java | 3 +- .../morny/bot/command/MornyInformations.java | 46 ++++++++++++++++++ .../cono/morny/data/TelegramStickers.java | 48 +++++++++++++++++++ 6 files changed, 100 insertions(+), 5 deletions(-) rename src/main/java/cc/sukazyo/cono/morny/bot/command/{EncUtils.java => Encryptor.java} (99%) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java diff --git a/gradle.properties b/gradle.properties index 3c7131f..dfdc9dc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.8.0.5 +VERSION = 0.8.0.6 CODENAME = fuzhou diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index b1ea0ac..203a4a6 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.5"; + public static final String VERSION = "0.8.0.6"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1665763018652L; + public static final long COMPILE_TIMESTAMP = 1665825000740L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java similarity index 99% rename from src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java rename to src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java index 1c439e0..93aacca 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/EncUtils.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java @@ -20,7 +20,7 @@ import java.util.Base64; import static cc.sukazyo.cono.morny.Log.logger; -public class EncUtils implements ITelegramCommand { +public class Encryptor implements ITelegramCommand { @Nonnull @Override public String getName () { return "encrypt"; } @Nullable @Override public String[] getAliases () { return new String[0]; } 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 381dc36..7b37258 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 @@ -68,8 +68,9 @@ public class MornyCommands { new Nbnhhsh(), new Ip186Query.Ip(), new Ip186Query.Whois(), - new EncUtils(), + new Encryptor(), new SaveData(), + new MornyInformations(), new Version(), new MornyRuntime(), new Jrrp(), diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java new file mode 100644 index 0000000..9d815bb --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java @@ -0,0 +1,46 @@ +package cc.sukazyo.cono.morny.bot.command; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.data.TelegramStickers; +import cc.sukazyo.cono.morny.util.tgapi.InputCommand; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.request.SendSticker; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class MornyInformations implements ITelegramCommand { + + private static final String ACT_STICKER = "stickers"; + + @Nonnull @Override public String getName () { return "info"; } + @Nullable @Override public String[] getAliases () { return new String[0]; } + @Nonnull @Override public String getParamRule () { return "[(stickers)|(stickers.)sticker_id]"; } + @Nonnull @Override public String getDescription () { return "输出 Morny 当前版本的一些预定义信息"; } + + @Override + public void execute (@Nonnull InputCommand command, @Nonnull Update event) { + + if (!command.hasArgs() || command.getArgs().length > 1) { + MornyCoeur.extra().exec(new SendSticker(event.message().chat().id(), TelegramStickers.ID_404).replyToMessageId(event.message().messageId())); + } + + final String action = command.getArgs()[0]; + + if (action.startsWith("stickers")) { + if (action.equals("stickers")) + TelegramStickers.echoAllStickers(MornyCoeur.extra(), event.message().chat().id(), event.message().messageId()); + else { + TelegramStickers.echoStickerByID( + action.substring((ACT_STICKER+".").length()), + MornyCoeur.extra(), event.message().chat().id(), event.message().messageId() + ); + } + return; + } + + MornyCoeur.extra().exec(new SendSticker(event.message().chat().id(), TelegramStickers.ID_404).replyToMessageId(event.message().messageId())); + + } + +} 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 0acd42b..fb3f01c 100644 --- a/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java +++ b/src/main/java/cc/sukazyo/cono/morny/data/TelegramStickers.java @@ -1,5 +1,12 @@ package cc.sukazyo.cono.morny.data; +import cc.sukazyo.cono.morny.util.tgapi.ExtraAction; +import com.pengrad.telegrambot.request.SendMessage; +import com.pengrad.telegrambot.request.SendSticker; +import com.pengrad.telegrambot.response.SendResponse; + +import java.lang.reflect.Field; + /** * 存放 bot 使用到的贴纸 * @since 0.4.2.0 @@ -17,4 +24,45 @@ public class TelegramStickers { public static final String ID_PROGYNOVA = "CAACAgUAAxkBAAICm2KEuL7UQqNP7vSPCg2DHJIND6UsAAKLAwACH4WSBszIo722aQ3jJAQ"; public static final String ID_NETWORK_ERR = "CAACAgEAAxkBAAID0WNJgNEkD726KW4vZeFlw0FlVVyNAAIXJgACePzGBb50o7O1RbxoKgQ"; + public static void echoAllStickers (ExtraAction actionObject, long sentChat, int replyToMessageId) { + + for (Field object : TelegramStickers.class.getFields()) { + if (object.getType()==String.class && object.getName().startsWith("ID_")) { + try { + + final String stickerId = (String)object.get(""); + SendSticker echo = new SendSticker(sentChat, stickerId); + SendMessage echoName = new SendMessage(sentChat, object.getName()); + if (replyToMessageId!=-1) echo.replyToMessageId(replyToMessageId); + SendResponse echoedName = actionObject.exec(echoName); + actionObject.exec(echo.replyToMessageId(echoedName.message().messageId())); + + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } + + } + + public static void echoStickerByID (String stickerFieldID, ExtraAction actionObject, long sentChat, int replyToMessageId) { + try { + // normally get the sticker and echo + Field sticker = TelegramStickers.class.getField(stickerFieldID); + SendMessage echoName = new SendMessage(sentChat, sticker.getName()); + SendSticker echo = new SendSticker(sentChat, (String)sticker.get("")); + if (replyToMessageId!=-1) echo.replyToMessageId(replyToMessageId); + SendResponse echoedName = actionObject.exec(echoName); + actionObject.exec(echo.replyToMessageId(echoedName.message().messageId())); + } catch (NoSuchFieldException e) { + // no such sticker found + SendSticker echo404 = new SendSticker(sentChat, TelegramStickers.ID_404); + if (replyToMessageId!=-1) echo404.replyToMessageId(replyToMessageId); + actionObject.exec(echo404); + } catch (IllegalAccessException e) { + // java-reflect get sticker FILE_ID failed + throw new RuntimeException(e); + } + } + } From e31dd6a0fb88e1beb46d27292c214ce20bc2eed3 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 15 Oct 2022 17:51:56 +0800 Subject: [PATCH 08/14] =?UTF-8?q?/encrypt=20=E7=8E=B0=E5=9C=A8=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BA=86=E5=9B=BE=E7=89=87=20//=20=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=89=88=E6=9C=AC=20CODENAME=20=E4=B8=BA=20putian?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 4 ++-- .../cono/morny/GradleProjectConfigures.java | 6 ++--- .../cono/morny/bot/command/Encryptor.java | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/gradle.properties b/gradle.properties index dfdc9dc..3c89bfe 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,8 +1,8 @@ ## Core -VERSION = 0.8.0.6 +VERSION = 0.8.0.7 -CODENAME = fuzhou +CODENAME = putian # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 203a4a6..d6ac208 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.6"; - public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1665825000740L; + public static final String VERSION = "0.8.0.7"; + public static final String CODENAME = "putian"; + public static final long COMPILE_TIMESTAMP = 1665827278036L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java index 93aacca..485a562 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java @@ -6,6 +6,7 @@ import cc.sukazyo.cono.morny.util.CommonConvert; import cc.sukazyo.cono.morny.util.CommonEncrypt; import cc.sukazyo.cono.morny.util.tgapi.InputCommand; import cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape; +import com.pengrad.telegrambot.model.PhotoSize; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.request.GetFile; @@ -92,6 +93,28 @@ public class Encryptor implements ITelegramCommand { return; } dataName = event.message().replyToMessage().document().fileName(); + } else if (event.message().replyToMessage() != null && event.message().replyToMessage().photo() != null) { + inputText = false; + try { + PhotoSize originPhoto = null; + long photoSize = 0; + for (PhotoSize size : event.message().replyToMessage().photo()) if (photoSize < (long)size.width() *size.height()) { + originPhoto = size; + photoSize = (long)size.width() *size.height(); + } // found max size (original) image in available sizes + if (originPhoto==null) throw new IOException("no photo object from api."); + data = MornyCoeur.getAccount().getFileContent(MornyCoeur.extra().exec(new GetFile( + originPhoto.fileId() + )).file()); + } catch (IOException e) { + logger.warn("NetworkRequest error: TelegramFileAPI:\n\t" + e.getMessage()); + MornyCoeur.extra().exec(new SendSticker( + event.message().chat().id(), + TelegramStickers.ID_NETWORK_ERR + ).replyToMessageId(event.message().messageId())); + return; + } + dataName = "photo"+CommonConvert.byteArrayToHex(CommonEncrypt.hashMd5(String.valueOf(System.currentTimeMillis()))).substring(32-12).toUpperCase()+".png"; } else if (event.message().replyToMessage() != null && event.message().replyToMessage().text() != null) { inputText = true; data = event.message().replyToMessage().text().getBytes(CommonEncrypt.ENCRYPT_STANDARD_CHARSET); From 5bb19095c885b06a661435a1012aef5532e0e7f2 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 17 Oct 2022 17:44:12 +0800 Subject: [PATCH 09/14] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E5=86=85=E8=81=94=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=B0=86=20twitter=20=E5=88=86=E4=BA=AB?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E9=87=8D=E6=96=B0=E8=BE=93=E5=87=BA=E4=B8=BA?= =?UTF-8?q?=20vxtwitter=20=E5=88=86=E4=BA=AB=E9=93=BE=E6=8E=A5=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E5=8E=BB=E8=B7=9F=E8=B8=AA=E5=8F=82=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E5=B0=86=20ITelegramQuery=20=E6=8A=BD=E8=B1=A1=E5=B1=82?= =?UTF-8?q?=E7=9A=84=E8=A6=81=E6=B1=82=E8=BF=94=E5=9B=9E=E4=BB=8E=E5=8D=95?= =?UTF-8?q?=E4=B8=AA=E9=99=90=E5=AE=9A=E7=B1=BB=E5=88=AB=20queryUnit=20?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=20list>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 通过 tgapi.NamedUtils#inlineIds 规范化了 InlineQuery 的 id 定义 - 将 raw text 的 id 重新定义为规范化行为 - 将 my information 的 id 重新定义为规范化行为 --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/query/ITelegramQuery.java | 7 +-- .../cono/morny/bot/query/MornyQueries.java | 9 ++-- .../cono/morny/bot/query/MyInformation.java | 15 ++++-- .../sukazyo/cono/morny/bot/query/RawText.java | 17 ++++--- .../morny/bot/query/ShareToolTwitter.java | 50 +++++++++++++++++++ .../util/tgapi/formatting/NamedUtils.java | 18 +++++++ 8 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/NamedUtils.java diff --git a/gradle.properties b/gradle.properties index 3c89bfe..5576ec9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.8.0.7 +VERSION = 0.8.0.8 CODENAME = putian diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index d6ac208..2c188e6 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.7"; + public static final String VERSION = "0.8.0.8"; public static final String CODENAME = "putian"; - public static final long COMPILE_TIMESTAMP = 1665827278036L; + public static final long COMPILE_TIMESTAMP = 1665999835121L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/ITelegramQuery.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/ITelegramQuery.java index b0fa978..c79ab37 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/ITelegramQuery.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/ITelegramQuery.java @@ -4,11 +4,12 @@ import javax.annotation.Nullable; import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit; import com.pengrad.telegrambot.model.Update; -import com.pengrad.telegrambot.model.request.InlineQueryResult; -public interface ITelegramQuery > { +import java.util.List; + +public interface ITelegramQuery { @Nullable - InlineQueryUnit query (Update event); + List> query (Update event); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java index a10f00d..a561303 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java @@ -9,19 +9,20 @@ import java.util.List; public class MornyQueries { - private final List> queryInstances = new ArrayList<>(); + private final List queryInstances = new ArrayList<>(); public MornyQueries () { queryInstances.add(new RawText()); queryInstances.add(new MyInformation()); + queryInstances.add(new ShareToolTwitter()); } @Nonnull public List> query (@Nonnull Update event) { final List> results = new ArrayList<>(); - for (ITelegramQuery instance : queryInstances) { - final InlineQueryUnit r = instance.query(event); - if (r!=null) results.add(r); + for (ITelegramQuery instance : queryInstances) { + final List> r = instance.query(event); + if (r!=null) results.addAll(r); } return results; } 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 53f666a..7921174 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 @@ -10,21 +10,26 @@ import com.pengrad.telegrambot.model.request.ParseMode; import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramUserInformation; -public class MyInformation implements ITelegramQuery { +import java.util.Collections; +import java.util.List; + +import static cc.sukazyo.cono.morny.util.tgapi.formatting.NamedUtils.inlineIds; + +public class MyInformation implements ITelegramQuery { public static final String ID_PREFIX = "[morny/info/me]"; public static final String TITLE = "My Account Information"; @Override @Nullable - public InlineQueryUnit query(Update event) { + public List> query(Update event) { if (!(event.inlineQuery().query() == null || "".equals(event.inlineQuery().query()))) return null; - return new InlineQueryUnit<>(new InlineQueryResultArticle( - ID_PREFIX, TITLE, + return Collections.singletonList(new InlineQueryUnit<>(new InlineQueryResultArticle( + inlineIds(ID_PREFIX), TITLE, new InputTextMessageContent( TelegramUserInformation.informationOutputHTML(event.inlineQuery().from()) ).parseMode(ParseMode.HTML) - )).isPersonal(true).cacheTime(10); + )).isPersonal(true).cacheTime(10)); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java index 655d03a..a773f6b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java @@ -8,23 +8,24 @@ import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; import com.pengrad.telegrambot.model.request.InputTextMessageContent; -import static cc.sukazyo.cono.morny.util.CommonConvert.byteArrayToHex; -import static cc.sukazyo.cono.morny.util.CommonEncrypt.hashMd5; +import java.util.Collections; +import java.util.List; -public class RawText implements ITelegramQuery { +import static cc.sukazyo.cono.morny.util.tgapi.formatting.NamedUtils.inlineIds; + +public class RawText implements ITelegramQuery { public static final String ID_PREFIX = "[morny/r/text]"; public static final String TITLE = "Raw Text"; @Override @Nullable - public InlineQueryUnit query (Update event) { + public List> query (Update event) { if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null; - return new InlineQueryUnit<>(new InlineQueryResultArticle( - ID_PREFIX + byteArrayToHex(hashMd5(event.inlineQuery().query())), - TITLE, + return Collections.singletonList(new InlineQueryUnit<>(new InlineQueryResultArticle( + inlineIds(ID_PREFIX, event.inlineQuery().query()), TITLE, new InputTextMessageContent(event.inlineQuery().query()) - )); + ))); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.java new file mode 100644 index 0000000..9c50d6f --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.java @@ -0,0 +1,50 @@ +package cc.sukazyo.cono.morny.bot.query; + +import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static cc.sukazyo.cono.morny.util.tgapi.formatting.NamedUtils.inlineIds; + +public class ShareToolTwitter implements ITelegramQuery { + + public static final String TITLE_VX = "[tweet] Share as VxTwitter"; + public static final String TITLE_VX_COMBINED = "[tweet] Share as VxTwitter(combination)"; + public static final String ID_PREFIX_VX = "[morny/share/twitter/vxtwi]"; + public static final String ID_PREFIX_VX_COMBINED = "[morny/share/twitter/vxtwi_combine]"; + + public static final Pattern REGEX_TWEET_LINK = Pattern.compile( + "^(?:https?://)?((?:(?:c\\.)?vx|fx|www\\.)?twitter\\.com)/((\\w+)/status/(\\d+)(?:/photo/(\\d+))?)/?(\\?[\\w&=-]+)?$"); + + @Nullable + @Override + public List> query (@Nonnull Update event) { + if (event.inlineQuery().query() == null) return null; + final Matcher regex = REGEX_TWEET_LINK.matcher(event.inlineQuery().query()); + if (regex.matches()) { + + List> result = new ArrayList<>(); + + result.add(new InlineQueryUnit<>(new InlineQueryResultArticle( + inlineIds(ID_PREFIX_VX+event.inlineQuery().query()), TITLE_VX, + String.format("https://vxtwitter.com/%s", regex.group(2)) + ))); + result.add(new InlineQueryUnit<>(new InlineQueryResultArticle( + inlineIds(ID_PREFIX_VX_COMBINED+event.inlineQuery().query()), TITLE_VX_COMBINED, + String.format("https://c.vxtwitter.com/%s", regex.group(2)) + ))); + + return result; + + } + return null; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/NamedUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/NamedUtils.java new file mode 100644 index 0000000..b046f30 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/NamedUtils.java @@ -0,0 +1,18 @@ +package cc.sukazyo.cono.morny.util.tgapi.formatting; + +import cc.sukazyo.cono.morny.util.CommonConvert; +import cc.sukazyo.cono.morny.util.CommonEncrypt; + +import javax.annotation.Nonnull; + +public class NamedUtils { + + public static String inlineIds (@Nonnull String tag) { + return inlineIds(tag, ""); + } + + public static String inlineIds (@Nonnull String tag, @Nonnull String taggedData) { + return CommonConvert.byteArrayToHex(CommonEncrypt.hashMd5(tag+taggedData)); + } + +} From 58b6f863bd13d00fc6d4332445020e7cac1653fe Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 18 Oct 2022 16:26:36 +0800 Subject: [PATCH 10/14] =?UTF-8?q?/encrypt=20=E6=B7=BB=E5=8A=A0=20base64=20?= =?UTF-8?q?=E5=AF=B9=20base64url=20=E6=A0=BC=E5=BC=8F=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../sukazyo/cono/morny/GradleProjectConfigures.java | 4 ++-- .../cc/sukazyo/cono/morny/bot/command/Encryptor.java | 12 ++++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/gradle.properties b/gradle.properties index 5576ec9..9f8614b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.8.0.8 +VERSION = 0.8.0.9 CODENAME = putian diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 2c188e6..4d26476 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.8"; + public static final String VERSION = "0.8.0.9"; public static final String CODENAME = "putian"; - public static final long COMPILE_TIMESTAMP = 1665999835121L; + public static final long COMPILE_TIMESTAMP = 1666081545158L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java index 485a562..e770b9f 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java @@ -39,7 +39,9 @@ public class Encryptor implements ITelegramCommand { MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), """ base64, b64 + base64url, base64u, b64u base64decode, base64d, b64d + base64url-decode, base64ud, b64ud sha1 sha256 sha512 @@ -132,8 +134,9 @@ public class Encryptor implements ITelegramCommand { byte[] result = null; String resultName = null; switch (command.getArgs()[0]) { - case "base64", "b64" -> { - result = Base64.getEncoder().encode(data); + case "base64", "b64", "base64url", "base64u", "b64u" -> { + final Base64.Encoder b64tool = command.getArgs()[0].contains("u") ? Base64.getUrlEncoder() : Base64.getEncoder(); + result = b64tool.encode(data); if (!inputText) { echoString = false; resultName = dataName+".b64.txt"; @@ -141,8 +144,9 @@ public class Encryptor implements ITelegramCommand { resultString = new String(result, CommonEncrypt.ENCRYPT_STANDARD_CHARSET); } } - case "base64decode", "base64d", "b64d" -> { - try { result = Base64.getDecoder().decode(data); } + case "base64decode", "base64d", "b64d", "base64url-decode", "base64ud", "b64ud" -> { + final Base64.Decoder b64tool = command.getArgs()[0].contains("u") ? Base64.getUrlDecoder() : Base64.getDecoder(); + try { result = b64tool.decode(data); } catch (IllegalArgumentException e) { MornyCoeur.extra().exec(new SendSticker( event.message().chat().id(), TelegramStickers.ID_404 From 2fa6950e4e4576452009370cdb7cadf7e52c4925 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 18 Oct 2022 20:28:06 +0800 Subject: [PATCH 11/14] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86=20bilibili=20?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E5=88=86=E4=BA=AB=E7=9A=84=E5=86=85=E8=81=94?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 bilibili 视频分享的内联查询可以输入 av/bv 号或是视频链接 - 可以输出为带有 av 视频链接的 av 号,或是 bv 视频链接的 bv 号 - 添加 BiliTool 工具可以互转 bilibili av/bv 号 --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/query/MornyQueries.java | 1 + .../morny/bot/query/ShareToolBilibili.java | 80 +++++++++++++++++++ .../cc/sukazyo/cono/morny/util/BiliTool.java | 73 +++++++++++++++++ .../sukazyo/cono/morny/util/TestBiliTool.java | 30 +++++++ 6 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolBilibili.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/BiliTool.java create mode 100644 src/test/java/cc/sukazyo/cono/morny/util/TestBiliTool.java diff --git a/gradle.properties b/gradle.properties index 9f8614b..1f88aa7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.8.0.9 +VERSION = 0.8.0.10 CODENAME = putian diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 4d26476..d4b2afc 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.9"; + public static final String VERSION = "0.8.0.10"; public static final String CODENAME = "putian"; - public static final long COMPILE_TIMESTAMP = 1666081545158L; + public static final long COMPILE_TIMESTAMP = 1666096021418L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java index a561303..b0dc135 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java @@ -15,6 +15,7 @@ public class MornyQueries { queryInstances.add(new RawText()); queryInstances.add(new MyInformation()); queryInstances.add(new ShareToolTwitter()); + queryInstances.add(new ShareToolBilibili()); } @Nonnull diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolBilibili.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolBilibili.java new file mode 100644 index 0000000..a5e1ced --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolBilibili.java @@ -0,0 +1,80 @@ +package cc.sukazyo.cono.morny.bot.query; + +import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit; +import cc.sukazyo.cono.morny.util.BiliTool; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; +import com.pengrad.telegrambot.model.request.InputTextMessageContent; +import com.pengrad.telegrambot.model.request.ParseMode; + +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +//import static cc.sukazyo.cono.morny.Log.logger; +import static cc.sukazyo.cono.morny.util.tgapi.formatting.NamedUtils.inlineIds; + +public class ShareToolBilibili implements ITelegramQuery { + + public static final String TITLE_BILI_AV = "[bilibili] Share video / av"; + public static final String TITLE_BILI_BV = "[bilibili] Share video / BV"; + public static final String ID_PREFIX_BILI_AV = "[morny/share/bili/av]"; + public static final String ID_PREFIX_BILI_BV = "[morny/share/bili/bv]"; + public static final Pattern REGEX_BILI_VIDEO = Pattern.compile("^(?:(?:https?://)?(?:www\\.)?bilibili\\.com(?:/s)?/video/((?:av|AV)(\\d+)|(?:bv|BV)([A-HJ-NP-Za-km-z1-9]+))/?(\\?(?:p=(\\d+))?.*)?|(?:av|AV)(\\d+)|(?:bv|BV)([A-HJ-NP-Za-km-z1-9]+))$"); + + private static final String SHARE_FORMAT_HTML = "%s"; + + @Nullable + @Override + public List> query (Update event) { + if (event.inlineQuery().query() == null) return null; + final Matcher regex = REGEX_BILI_VIDEO.matcher(event.inlineQuery().query()); + if (regex.matches()) { + +// logger.debug(String.format( +// "====== ok\n1: %s\n2: %s\n3: %s\n4: %s\n5: %s\n6: %s\n7: %s", +// regex.group(1), regex.group(2), regex.group(3), regex.group(4), +// regex.group(5), regex.group(6), regex.group(7) +// )); + + // get video id from input, also get video part id + String av = regex.group(2)==null ? regex.group(6)==null ? null : regex.group(6) : regex.group(2); + String bv = regex.group(3)==null ? regex.group(7)==null ? null : regex.group(7) : regex.group(3); +// logger.trace(String.format("catch id av[%s] bv[%s]", av, bv)); + final int part = regex.group(5)==null ? -1 : Integer.parseInt(regex.group(5)); +// logger.trace(String.format("catch part [%s]", part)); + if (av == null) { + assert bv != null; + av = String.valueOf(BiliTool.toAv(bv)); +// logger.trace(String.format("converted bv[%s] to av[%s]", bv, av)); + } else { + bv = BiliTool.toBv(Long.parseLong(av)); +// logger.trace(String.format("converted av[%s] to bv[%s]", av, bv)); + } + // build standard share links + final String linkPartParam = part==-1 ? "" : "?p="+part; + final String linkAv = "https://www.bilibili.com/video/av"+av + linkPartParam; + final String linkBv = "https://www.bilibili.com/video/BV"+bv + linkPartParam; + final String idAv = "av"+av; + final String idBv = "BV"+bv; +// logger.trace("built all data."); + + // build share message element + List> result = new ArrayList<>(); + result.add(new InlineQueryUnit<>(new InlineQueryResultArticle( + inlineIds(ID_PREFIX_BILI_AV+av), TITLE_BILI_AV+av, + new InputTextMessageContent(String.format(SHARE_FORMAT_HTML, linkAv, idAv)).parseMode(ParseMode.HTML) + ))); + result.add(new InlineQueryUnit<>(new InlineQueryResultArticle( + inlineIds(ID_PREFIX_BILI_BV+bv), TITLE_BILI_BV+bv, + new InputTextMessageContent(String.format(SHARE_FORMAT_HTML, linkBv, idBv)).parseMode(ParseMode.HTML) + ))); + return result; + + } + return null; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/BiliTool.java b/src/main/java/cc/sukazyo/cono/morny/util/BiliTool.java new file mode 100644 index 0000000..ef36993 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/BiliTool.java @@ -0,0 +1,73 @@ +package cc.sukazyo.cono.morny.util; + +import javax.annotation.Nonnegative; +import javax.annotation.Nonnull; +import java.util.HashMap; +import java.util.Map; + +public class BiliTool { + + private static final long V_CONV_XOR = 177451812L; + private static final long V_CONV_ADD = 8728348608L; + private static final char[] BV_TABLE = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF".toCharArray(); + private static final int TABLE_INT = BV_TABLE.length; + private static final Map BV_TABLE_REVERSED = new HashMap<>(); + static { for (int i = 0; i < BV_TABLE.length; i++) BV_TABLE_REVERSED.put(BV_TABLE[i], i); } + private static final char[] BV_TEMPLATE = "1 4 1 7 ".toCharArray(); + private static final int[] BV_TEMPLATE_FILTER = new int[]{9, 8, 1, 6, 2, 4}; + + /** + * Convert a Bilibili AV video id format to BV id format. + *

+ * the AV id is a number; the BV id is a special base58 number, it shows as String in programming.
+ * eg:
+ * while the link {@code https://www.bilibili.com/video/BV17x411w7KC/} + * shows the same with {@code https://www.bilibili.com/video/av170001/}, + * the AV id is {@code 170001}, the BV id is {@code 17x411w7KC} + *

+ * for now , the BV id has 10 digits. + * the method available while the av-id < 2^27, while it theoretically available when the av-id < 2^30. + * + * @see mcfx的回复: 如何看待 2020 年 3 月 23 日哔哩哔哩将稿件的「av 号」变更为「BV 号」? + * + * @param bv the BV id, a string in (a special) base58 number format, without "BV" prefix. + * @return the AV id corresponding to this bv id in Bilibili, formatted as a number. + */ + @Nonnegative + public static long toAv (@Nonnull String bv) { + long av = 0; + for (int i = 0; i < BV_TEMPLATE_FILTER.length; i++) { + av += BV_TABLE_REVERSED.get(bv.charAt(BV_TEMPLATE_FILTER[i])) * Math.pow(TABLE_INT,i); + } + return (av-V_CONV_ADD)^V_CONV_XOR; + } + + /** + * Convert a Bilibili BV video id format to AV id format. + *

+ * the AV id is a number; the BV id is a special base58 number, it shows as String in programming.
+ * eg:
+ * while the link {@code https://www.bilibili.com/video/BV17x411w7KC/} + * shows the same with {@code https://www.bilibili.com/video/av170001/}, + * the AV id is {@code 170001}, the BV id is {@code 17x411w7KC} + *

+ * for now , the BV id has 10 digits. + * the method available while the av-id < 2^27, while it theoretically available when the av-id < 2^30. + * + * @see mcfx的回复: 如何看待 2020 年 3 月 23 日哔哩哔哩将稿件的「av 号」变更为「BV 号」? + * + * @param av the (base10) AV id. + * @return the AV id corresponding to this bv id in Bilibili, + * as a (special) base 58 number format without "BV" prefix. + */ + @Nonnull + public static String toBv (@Nonnegative long av) { + av = (av^V_CONV_XOR)+V_CONV_ADD; + final char[] bv = BV_TEMPLATE.clone(); + for (int i = 0; i < BV_TEMPLATE_FILTER.length; i++) { + bv[BV_TEMPLATE_FILTER[i]] = BV_TABLE[(int)(Math.floor(av/(Math.pow(TABLE_INT, i)))%TABLE_INT)]; + } + return String.copyValueOf(bv); + } + +} diff --git a/src/test/java/cc/sukazyo/cono/morny/util/TestBiliTool.java b/src/test/java/cc/sukazyo/cono/morny/util/TestBiliTool.java new file mode 100644 index 0000000..125d9c6 --- /dev/null +++ b/src/test/java/cc/sukazyo/cono/morny/util/TestBiliTool.java @@ -0,0 +1,30 @@ +package cc.sukazyo.cono.morny.util; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static cc.sukazyo.cono.morny.util.BiliTool.*; + +public class TestBiliTool { + + private static final String AV_BV_DATA_CSV = """ + 17x411w7KC, 170001 + 1Q541167Qg, 455017605 + 1mK4y1C7Bz, 882584971 + 1T24y197V2, 688730800 + """; + + @ParameterizedTest + @CsvSource(textBlock = AV_BV_DATA_CSV) + void testAvToBv (String bv, int av) { + Assertions.assertEquals(bv, toBv(av)); + } + + @ParameterizedTest + @CsvSource(textBlock = AV_BV_DATA_CSV) + void testBvToAv (String bv, int av) { + Assertions.assertEquals(av, toAv(bv)); + } + +} From 732156c8229a56378545d9a304798c48e7d1ddeb Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Tue, 25 Oct 2022 14:13:23 +0800 Subject: [PATCH 12/14] add morny book repository --- .gitmodules | 3 +++ _book | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 _book diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..aa5e2ed --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "_book"] + path = _book + url = https://storage.sukazyo.cc/Eyre_S/morny-book.git diff --git a/_book b/_book new file mode 160000 index 0000000..33e051f --- /dev/null +++ b/_book @@ -0,0 +1 @@ +Subproject commit 33e051fee5acb5118d6c023039ff9c1db6aa0c60 From b144c4b8038fe946bdc9bdab848ba3a51f2f9de8 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 28 Oct 2022 19:05:25 +0800 Subject: [PATCH 13/14] =?UTF-8?q?readme:=20=E4=BF=AE=E5=A4=8D=E7=94=B1?= =?UTF-8?q?=E4=BA=8Emvn=E5=8D=87=E7=BA=A7=E5=AF=BC=E8=87=B4=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E5=A4=B1=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d1f1ca8..99bf217 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [tg-account]: https://t.me/morny_cono_annie_bot [issues]: https://github.com/Eyre-S/Coeur-Morny-Cono/issues [todo]: https://github.com/users/Eyre-S/projects/1 -[artifact]: https://mvn.sukazyo.cc/main/cc/sukazyo/morny-coeur +[artifact]: https://mvn.sukazyo.cc/#/releases/cc/sukazyo/morny-coeur [tg4j]: https://github.com/pengrad/java-telegram-bot-api [spotbugs]: https://spotbugs.github.io/ From 5f8cbcffd68c86e07017dfa3a45542d16eb7c2d7 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 2 Nov 2022 16:03:35 +0800 Subject: [PATCH 14/14] =?UTF-8?q?=E5=85=B3=E9=97=AD=20tracker=20=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../java/cc/sukazyo/cono/morny/GradleProjectConfigures.java | 4 ++-- .../cc/sukazyo/cono/morny/bot/event/EventListeners.java | 4 ++-- .../java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gradle.properties b/gradle.properties index 1f88aa7..338d93f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.8.0.10 +VERSION = 0.8.0.11 CODENAME = putian diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index d4b2afc..8735b7d 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.10"; + public static final String VERSION = "0.8.0.11"; public static final String CODENAME = "putian"; - public static final long COMPILE_TIMESTAMP = 1666096021418L; + public static final long COMPILE_TIMESTAMP = 1667376095614L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java index 0b300e2..7b165f9 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java @@ -5,7 +5,7 @@ import cc.sukazyo.cono.morny.bot.api.EventListenerManager; public class EventListeners { public static final OnTelegramCommand COMMANDS_LISTENER = new OnTelegramCommand(); - public static final OnActivityRecord ACTIVITY_RECORDER = new OnActivityRecord(); + @SuppressWarnings("unused") public static final OnActivityRecord ACTIVITY_RECORDER = new OnActivityRecord(); public static final OnUserSlashAction USER_SLASH_ACTION = new OnUserSlashAction(); public static final OnUpdateTimestampOffsetLock UPDATE_TIMESTAMP_OFFSET_LOCK = new OnUpdateTimestampOffsetLock(); public static final OnInlineQueries INLINE_QUERY = new OnInlineQueries(); @@ -19,7 +19,7 @@ public class EventListeners { public static void registerAllListeners () { EventListenerManager.addListener( - ACTIVITY_RECORDER, +// ACTIVITY_RECORDER, UPDATE_TIMESTAMP_OFFSET_LOCK, /* write functional event behind here */ // KUOHUANHUAN_NEED_SLEEP, diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java index b7585aa..d40ba47 100644 --- a/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java +++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyDaemons.java @@ -8,7 +8,7 @@ public class MornyDaemons { public static void start () { logger.info("ALL Morny Daemons starting..."); - TrackerDataManager.init(); +// TrackerDataManager.init(); medicationTimerInstance.start(); logger.info("Morny Daemons started."); } @@ -17,10 +17,10 @@ public class MornyDaemons { logger.info("ALL Morny Daemons stopping..."); - TrackerDataManager.DAEMON.interrupt(); +// TrackerDataManager.DAEMON.interrupt(); medicationTimerInstance.interrupt(); - TrackerDataManager.trackingLock.lock(); +// TrackerDataManager.trackingLock.lock(); try { medicationTimerInstance.join(); } catch (InterruptedException e) { e.printStackTrace(System.out); } logger.info("ALL Morny Daemons STOPPED.");