为 /encrypt 添加了简单的文件处理支持,添加了 NETWORK_ERR 反馈贴纸(,添加了一个没用的 /test 命令)

This commit is contained in:
A.C.Sukazyo Eyre 2022-10-15 00:01:35 +08:00
parent dd8f7c0cd5
commit e947c3f239
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
7 changed files with 135 additions and 20 deletions

View File

@ -1,6 +1,6 @@
## Core ## Core
VERSION = 0.8.0.4 VERSION = 0.8.0.5
CODENAME = fuzhou CODENAME = fuzhou

View File

@ -4,7 +4,7 @@ package cc.sukazyo.cono.morny;
* the final field that will be updated by gradle automatically. * the final field that will be updated by gradle automatically.
*/ */
public class GradleProjectConfigures { public class GradleProjectConfigures {
public static final String VERSION = "0.8.0.4"; public static final String VERSION = "0.8.0.5";
public static final String CODENAME = "fuzhou"; public static final String CODENAME = "fuzhou";
public static final long COMPILE_TIMESTAMP = 1665305196411L; public static final long COMPILE_TIMESTAMP = 1665763018652L;
} }

View File

@ -8,13 +8,18 @@ import cc.sukazyo.cono.morny.util.tgapi.InputCommand;
import cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape; import cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape;
import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.ParseMode; import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.GetFile;
import com.pengrad.telegrambot.request.SendDocument;
import com.pengrad.telegrambot.request.SendMessage; import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.SendSticker; import com.pengrad.telegrambot.request.SendSticker;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Base64; import java.util.Base64;
import static cc.sukazyo.cono.morny.Log.logger;
public class EncUtils implements ITelegramCommand { public class EncUtils implements ITelegramCommand {
@Nonnull @Override public String getName () { return "encrypt"; } @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 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( MornyCoeur.extra().exec(new SendMessage(
event.message().chat().id(), event.message().chat().id(),
"<i><u>null</u></i>" "<i><u>null</u></i>"
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
return; 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]) { switch (command.getArgs()[0]) {
case "base64", "b64" -> result = Base64.getEncoder().encodeToString(data.getBytes(CommonEncrypt.ENCRYPT_STANDARD_CHARSET)); case "base64", "b64" -> {
case "base64decode", "base64d", "b64d" -> result = new String( result = Base64.getEncoder().encode(data);
Base64.getDecoder().decode(data.getBytes(CommonEncrypt.ENCRYPT_STANDARD_CHARSET)), CommonEncrypt.ENCRYPT_STANDARD_CHARSET); if (!inputText) {
case "md5" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashMd5(data)); echoString = false;
case "sha1" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha1(data)); resultName = dataName+".b64.txt";
case "sha256" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha256(data)); } else {
case "sha512" -> result = CommonConvert.byteArrayToHex(CommonEncrypt.hashSha512(data)); 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 -> { default -> {
MornyCoeur.extra().exec(new SendSticker( MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(), TelegramStickers.ID_404 event.message().chat().id(), TelegramStickers.ID_404
@ -96,8 +149,10 @@ public class EncUtils implements ITelegramCommand {
// it means md5, sha1, sha256, sha512 here. // it means md5, sha1, sha256, sha512 here.
// other will report wrong param. // other will report wrong param.
switch (command.getArgs()[0]) { switch (command.getArgs()[0]) {
case "md5", "sha1", "sha256", "sha512" -> case "md5", "sha1", "sha256", "sha512" -> {
result = result.toUpperCase(); assert resultString != null;
resultString = resultString.toUpperCase();
}
default -> { default -> {
MornyCoeur.extra().exec(new SendSticker( MornyCoeur.extra().exec(new SendSticker(
event.message().chat().id(), TelegramStickers.ID_404 event.message().chat().id(), TelegramStickers.ID_404
@ -106,10 +161,17 @@ public class EncUtils implements ITelegramCommand {
} }
} }
} }
MornyCoeur.extra().exec(new SendMessage( if (echoString) {
event.message().chat().id(), MornyCoeur.extra().exec(new SendMessage(
"<pre><code>" + MsgEscape.escapeHtml(result) + "</code></pre>" event.message().chat().id(),
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); "<pre><code>" + MsgEscape.escapeHtml(resultString) + "</code></pre>"
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
} else {
MornyCoeur.extra().exec(new SendDocument(
event.message().chat().id(),
result
).fileName(resultName).replyToMessageId(event.message().messageId()));
}
} }

View File

@ -78,6 +78,7 @@ public class MornyCommands {
// 特殊的命令 // 特殊的命令
register( register(
new Testing(),
new DirectMsgClear() new DirectMsgClear()
); );

View File

@ -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(),
"<b>Just<b/> a TEST command."
).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
}
}

View File

@ -15,5 +15,6 @@ public class TelegramStickers {
public static final String ID_SENT = "CAACAgEAAx0CSQh32gABA--zYbiyU_wOijEitp-0tSl_k7W6l3gAAgMmAAJ4_MYF4GrompjXPx4jBA"; 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_SAVED = "CAACAgEAAx0CSQh32gABBExuYdB_G0srfhQldRWkBYxWzCOv4-IAApooAAJ4_MYFcjuNZszfQcQjBA";
public static final String ID_PROGYNOVA = "CAACAgUAAxkBAAICm2KEuL7UQqNP7vSPCg2DHJIND6UsAAKLAwACH4WSBszIo722aQ3jJAQ"; public static final String ID_PROGYNOVA = "CAACAgUAAxkBAAICm2KEuL7UQqNP7vSPCg2DHJIND6UsAAKLAwACH4WSBszIo722aQ3jJAQ";
public static final String ID_NETWORK_ERR = "CAACAgEAAxkBAAID0WNJgNEkD726KW4vZeFlw0FlVVyNAAIXJgACePzGBb50o7O1RbxoKgQ";
} }

View File

@ -126,4 +126,19 @@ public class CommonEncrypt {
return hashMd5(originString.getBytes(ENCRYPT_STANDARD_CHARSET)); 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;
}
}
} }