From e947c3f23928030428cbb080411fd14c634cb4c7 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 15 Oct 2022 00:01:35 +0800 Subject: [PATCH] =?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; + } + } + }