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/.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/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/
diff --git a/_book b/_book
new file mode 160000
index 0000000..3072bce
--- /dev/null
+++ b/_book
@@ -0,0 +1 @@
+Subproject commit 3072bcee8e498e87ecdd36958185ad423e80bcf3
diff --git a/build.gradle b/build.gradle
index 9e60e84..0e2a6aa 100644
--- a/build.gradle
+++ b/build.gradle
@@ -17,24 +17,16 @@ 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}"
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 eb58aeb..338d93f 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -1,15 +1,13 @@
## Core
-VERSION = 0.7.2.1
+VERSION = 0.8.0.11
-CODENAME = fuzhou
+CODENAME = putian
# dependencies
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 8608a79..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.7.2.1";
- public static final String CODENAME = "fuzhou";
- public static final long COMPILE_TIMESTAMP = 1664590869773L;
+ public static final String VERSION = "0.8.0.11";
+ public static final String CODENAME = "putian";
+ public static final long COMPILE_TIMESTAMP = 1667376095614L;
}
diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java
index d2cb07e..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;
@@ -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/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/Encryptor.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java
new file mode 100644
index 0000000..e770b9f
--- /dev/null
+++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/Encryptor.java
@@ -0,0 +1,205 @@
+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.PhotoSize;
+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 Encryptor 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
+ base64url, base64u, b64u
+ base64decode, base64d, b64d
+ base64url-decode, base64ud, b64ud
+ 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 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.
+ 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().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);
+ dataName = null;
+ } else {
+ MornyCoeur.extra().exec(new SendMessage(
+ event.message().chat().id(),
+ "null"
+ ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
+ return;
+ }
+
+ boolean echoString = true;
+ String resultString = null;
+ byte[] result = null;
+ String resultName = null;
+ switch (command.getArgs()[0]) {
+ 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";
+ } else {
+ resultString = new String(result, CommonEncrypt.ENCRYPT_STANDARD_CHARSET);
+ }
+ }
+ 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
+ ).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
+ ).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" -> {
+ assert resultString != null;
+ resultString = resultString.toUpperCase();
+ }
+ default -> {
+ MornyCoeur.extra().exec(new SendSticker(
+ event.message().chat().id(), TelegramStickers.ID_404
+ ).replyToMessageId(event.message().messageId()));
+ return;
+ }
+ }
+ }
+ 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/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 2918a80..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
@@ -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;
@@ -27,9 +27,9 @@ 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.untitled.util.telegram.formatting.MsgEscape.escapeHtml;
+import static cc.sukazyo.cono.morny.util.CommonFormat.formatDate;
+import static cc.sukazyo.cono.morny.util.CommonFormat.formatDuration;
+import static cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape.escapeHtml;
public class MornyCommands {
@@ -57,6 +57,7 @@ public class MornyCommands {
}
}
+ @SuppressWarnings("NonAsciiCharacters")
public MornyCommands () {
register(
@@ -67,7 +68,9 @@ public class MornyCommands {
new Nbnhhsh(),
new Ip186Query.Ip(),
new Ip186Query.Whois(),
+ new Encryptor(),
new SaveData(),
+ new MornyInformations(),
new Version(),
new MornyRuntime(),
new Jrrp(),
@@ -76,6 +79,7 @@ public class MornyCommands {
// 特殊的命令
register(
+ new Testing(),
new DirectMsgClear()
);
@@ -85,7 +89,8 @@ public class MornyCommands {
new 喵呜.揉揉(),
new 喵呜.蹭蹭(),
new 喵呜.贴贴(),
- new 私わね()
+ new 私わね(),
+ new 喵呜.Progynova()
);
}
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/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/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/bot/command/喵呜.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/喵呜.java
index 67d644a..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.untitled.util.telegram.object.InputCommand;
+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()));
+ }
+ }
+
}
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/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/bot/event/OnCallMe.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCallMe.java
index 1684d69..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
@@ -4,9 +4,9 @@ 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.untitled.telegram.api.formatting.TGToString;
-import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape;
+import cc.sukazyo.cono.morny.util.CommonFormat;
+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;
@@ -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/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/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
+ * 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/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..907cdd5 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonConvert.java @@ -0,0 +1,61 @@ +package cc.sukazyo.cono.morny.util; + +import javax.annotation.Nonnegative; +import javax.annotation.Nonnull; + +/** + * 进行简单类型转换等工作的类. + */ +public class CommonConvert { + + /** + * 将字节数组转换成 hex 字符串. + * @param b 字节数组 + * @return String 格式的字节数组的 hex 值(每个字节当中没有分隔符) + * @see #byteToHex(byte) + */ + @Nonnull + public static String byteArrayToHex(@Nonnull byte[] b){ + StringBuilder sb = new StringBuilder(); + for (byte value : b) { + sb.append(byteToHex(value)); + } + return sb.toString(); + } + + /** + * 将一个字节转换成十六进制 hex 字符串. + * @param b 字节值 + * @return String 格式的字节的 hex 值(小写) + */ + @Nonnull + public static String byteToHex(byte b) { + final String hex = Integer.toHexString(b & 0xff); + 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/CommonEncrypt.java b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java new file mode 100644 index 0000000..0e9c35d --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/CommonEncrypt.java @@ -0,0 +1,144 @@ +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; +import java.util.Base64; + +/** + * 用于数据加密或编解码的工具类. + *
+ * 出于 java std 中 Base64 的 {@link Base64.Encoder encode}/{@link Base64.Decoder decode} 十分好用,在此不再进行包装。 + */ +public class CommonEncrypt { + + /** + * 在使用加密算法处理字符串时默认会使用的字符串编码. + *
+ * Morny 使用 UTF-8 编码因为这是一般而言加解密工具的默认行为 + */ + 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 散列值. + * + * @param data byte 数组形式的数据体 + * @return 二进制(byte数组)格式的数据的 md5 散列值 + */ + @Nonnull + public static byte[] hashMd5 (@Nonnull byte[] data) { + return hashAsJavaMessageDigest("md5", data); + } + + /** + * 取得一个字符串的 md5 散列值. + *
+ * 输入的字符串将会以 {@link #ENCRYPT_STANDARD_CHARSET 默认的 UTF-8} 编码进行解析 + * + * @param originString 要进行散列的字符串 + * @return 二进制(byte数组)格式的 md5 散列值 + */ + @Nonnull + public static byte[] hashMd5 (String originString) { + 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));
+ }
+
+ @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;
+ }
+ }
+
+}
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;
-
-/**
- * 用于数据加密或编码的工具类
- * 显然大部分代码是抄来的
- *