-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. 添加并完成新功能 “MD5工具” 2. 添加并完成新功能 “URL/URI转码/解码” 3. 添加并完成新功能 “Unicode转码/解码” 4. 添加Mirai转义字符反斜杠([ ] : , \),修改了 去转义 方法以适应反斜杠的正则匹配Matcher.quoteReplacement("\\")
- Loading branch information
Showing
11 changed files
with
460 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
qqrobot/src/main/java/cn/ultronxr/qqrobot/eventHandler/MsgCodeToolkitHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cn.ultronxr.qqrobot.eventHandler; | ||
|
||
import cn.ultronxr.qqrobot.bean.BotCmd; | ||
import net.mamoe.mirai.event.events.GroupMessageEvent; | ||
import org.apache.commons.cli.CommandLine; | ||
|
||
/** | ||
* @author Ultronxr | ||
* @date 2022/02/18 13:33 | ||
* | ||
* 消息事件 - 有关各种代码小工具的Handler,包括MD5、SHA-1、等等 | ||
*/ | ||
public interface MsgCodeToolkitHandler { | ||
|
||
/** | ||
* 套用BotCmd/BotMenu框架 | ||
* 群聊中的 MD5 命令处理器 | ||
* | ||
* @param botCmd 匹配成功的命令BotCmd对象 | ||
* @param cmdLine 解析完成的CommandLine对象 | ||
* @param groupMsgEvent 群消息事件 | ||
* @param msgPlain 纯消息主体文本内容(MiraiUtils自定义消息类型三) | ||
* {@link cn.ultronxr.qqrobot.util.MiraiUtils#getMsgPlain(net.mamoe.mirai.event.events.MessageEvent)} | ||
*/ | ||
void groupMD5Handler(BotCmd botCmd, CommandLine cmdLine, GroupMessageEvent groupMsgEvent, String msgPlain); | ||
|
||
/** | ||
* 套用BotCmd/BotMenu框架 | ||
* 群聊中的 URL转码/解码 命令处理器 | ||
* | ||
* @param botCmd 匹配成功的命令BotCmd对象 | ||
* @param cmdLine 解析完成的CommandLine对象 | ||
* @param groupMsgEvent 群消息事件 | ||
* @param msgPlain 纯消息主体文本内容(MiraiUtils自定义消息类型三) | ||
* {@link cn.ultronxr.qqrobot.util.MiraiUtils#getMsgPlain(net.mamoe.mirai.event.events.MessageEvent)} | ||
*/ | ||
void groupURLHandler(BotCmd botCmd, CommandLine cmdLine, GroupMessageEvent groupMsgEvent, String msgPlain); | ||
|
||
/** | ||
* 套用BotCmd/BotMenu框架 | ||
* 群聊中的 Unicode转码/解码 命令处理器 | ||
* | ||
* @param botCmd 匹配成功的命令BotCmd对象 | ||
* @param cmdLine 解析完成的CommandLine对象 | ||
* @param groupMsgEvent 群消息事件 | ||
* @param msgPlain 纯消息主体文本内容(MiraiUtils自定义消息类型三) | ||
* {@link cn.ultronxr.qqrobot.util.MiraiUtils#getMsgPlain(net.mamoe.mirai.event.events.MessageEvent)} | ||
*/ | ||
void groupUnicodeHandler(BotCmd botCmd, CommandLine cmdLine, GroupMessageEvent groupMsgEvent, String msgPlain); | ||
|
||
} |
130 changes: 130 additions & 0 deletions
130
...ain/java/cn/ultronxr/qqrobot/eventHandler/eventHandlerImpl/MsgCodeToolkitHandlerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package cn.ultronxr.qqrobot.eventHandler.eventHandlerImpl; | ||
|
||
import cn.ultronxr.qqrobot.bean.BotCmd; | ||
import cn.ultronxr.qqrobot.eventHandler.MsgCodeToolkitHandler; | ||
import cn.ultronxr.qqrobot.util.CommonCliUtils; | ||
import cn.ultronxr.qqrobot.util.MD5Utils; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.mamoe.mirai.event.events.GroupMessageEvent; | ||
import org.apache.commons.cli.CommandLine; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.UriUtils; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* @author Ultronxr | ||
* @date 2022/02/18 13:34 | ||
*/ | ||
@Component | ||
@Slf4j | ||
public class MsgCodeToolkitHandlerImpl implements MsgCodeToolkitHandler { | ||
|
||
|
||
@Override | ||
public void groupMD5Handler(BotCmd botCmd, CommandLine cmdLine, GroupMessageEvent groupMsgEvent, String msgPlain) { | ||
if(cmdLine.hasOption("help")) { | ||
// 优先判断 --help 选项 | ||
CommonCliUtils.defaultOptionHelper(groupMsgEvent, botCmd); | ||
return; | ||
} | ||
|
||
String plainText = null, salt = null, msgRes = null; | ||
if(cmdLine.getOptions().length == 0 && cmdLine.getArgList().size() == 2) { | ||
// 无任何选项,但有参数,直接取arg1作为-p选项参数 (如:“>md5 123abc” ) | ||
plainText = cmdLine.getArgList().get(1); | ||
msgRes = MD5Utils.md5(plainText); | ||
} else if(cmdLine.hasOption("p")) { | ||
// 有plainText选项 | ||
plainText = cmdLine.getOptionValue("p"); | ||
if(cmdLine.hasOption("s")) { | ||
// 且有salt选项 | ||
salt = cmdLine.getOptionValue("s"); | ||
msgRes = MD5Utils.md5WithSalt(plainText, salt); | ||
} else { | ||
msgRes = MD5Utils.md5(plainText); | ||
} | ||
} | ||
if(StringUtils.isEmpty(msgRes)) { | ||
// msgRes还是empty,说明不符合任何情况 | ||
CommonCliUtils.defaultOptionExceptionHandler(groupMsgEvent); | ||
return; | ||
} | ||
if(cmdLine.hasOption("U")) { | ||
// 转化为大写 | ||
msgRes = msgRes.toUpperCase(Locale.ENGLISH); | ||
} | ||
log.info("[Function] 文本:{}\n盐值:{}\nMD5结果:{}", plainText, salt, msgRes); | ||
|
||
groupMsgEvent.getSubject().sendMessage(msgRes); | ||
log.info("[Msg-Send] {}", msgRes); | ||
} | ||
|
||
@Override | ||
public void groupURLHandler(BotCmd botCmd, CommandLine cmdLine, GroupMessageEvent groupMsgEvent, String msgPlain) { | ||
if(cmdLine.hasOption("help")) { | ||
// 优先判断 --help 选项 | ||
CommonCliUtils.defaultOptionHelper(groupMsgEvent, botCmd); | ||
return; | ||
} | ||
|
||
String msgRes = null, url = null; | ||
if(cmdLine.hasOption("e")) { | ||
// 转码参数 -e | ||
if(cmdLine.hasOption("d")) { | ||
//且出现了解码参数 -d ,直接报错 | ||
CommonCliUtils.defaultOptionHelper(groupMsgEvent, botCmd); | ||
return; | ||
} | ||
url = cmdLine.getOptionValue("e"); | ||
if(cmdLine.hasOption("c")) { | ||
msgRes = UriUtils.encode(url, "UTF-8"); | ||
} else { | ||
msgRes = UriUtils.encodeQuery(url, "UTF-8"); | ||
} | ||
} else if(cmdLine.hasOption("d")) { | ||
// 解码参数 -d | ||
url = cmdLine.getOptionValue("d"); | ||
msgRes = UriUtils.decode(url, "UTF-8"); | ||
} else { | ||
CommonCliUtils.defaultOptionExceptionHandler(groupMsgEvent); | ||
return; | ||
} | ||
log.info("[Function] 文本:{}\nURL转码/解码结果:{}", url, msgRes); | ||
|
||
groupMsgEvent.getSubject().sendMessage(msgRes); | ||
log.info("[Msg-Send] {}", msgRes); | ||
} | ||
|
||
@Override | ||
public void groupUnicodeHandler(BotCmd botCmd, CommandLine cmdLine, GroupMessageEvent groupMsgEvent, String msgPlain) { | ||
if(cmdLine.hasOption("help")) { | ||
// 优先判断 --help 选项 | ||
CommonCliUtils.defaultOptionHelper(groupMsgEvent, botCmd); | ||
return; | ||
} | ||
|
||
String plainText = null, msgRes = null; | ||
if(cmdLine.hasOption("e")) { | ||
if(cmdLine.hasOption("d")) { | ||
//且出现了解码参数 -d ,直接报错 | ||
CommonCliUtils.defaultOptionHelper(groupMsgEvent, botCmd); | ||
return; | ||
} | ||
plainText = cmdLine.getOptionValue("e"); | ||
msgRes = cn.ultronxr.qqrobot.util.StringUtils.native2Unicode(plainText); | ||
} else if(cmdLine.hasOption("d")) { | ||
plainText = cmdLine.getOptionValue("d"); | ||
msgRes = cn.ultronxr.qqrobot.util.StringUtils.unicode2Native(plainText); | ||
} else { | ||
CommonCliUtils.defaultOptionExceptionHandler(groupMsgEvent); | ||
return; | ||
} | ||
log.info("[Function] 文本:{}\nUnicode转码/解码结果:{}", plainText, msgRes); | ||
|
||
groupMsgEvent.getSubject().sendMessage(msgRes); | ||
log.info("[Msg-Send] {}", msgRes); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
qqrobot/src/main/java/cn/ultronxr/qqrobot/util/MD5Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cn.ultronxr.qqrobot.util; | ||
|
||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.util.DigestUtils; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
/** | ||
* @author Ultronxr | ||
* @date 2022/02/18 12:34 | ||
*/ | ||
@Slf4j | ||
public class MD5Utils { | ||
|
||
/** | ||
* 返回32位的MD5结果 | ||
* @param plainText 待加密文本 | ||
* @return {@code String} 32位MD5结果 | ||
*/ | ||
public static String md5(@NotNull String plainText) { | ||
return DigestUtils.md5DigestAsHex(plainText.getBytes()); | ||
} | ||
|
||
/** | ||
* 返回32位的MD5结果(加盐salt) | ||
* @param plainText 待加密文本 | ||
* @param salt 盐值 | ||
* @return {@code String} 32位MD5结果 | ||
*/ | ||
public static String md5WithSalt(@NotNull String plainText, @NotNull String salt) { | ||
MessageDigest md = null; | ||
try { | ||
md = MessageDigest.getInstance("MD5"); | ||
} catch (NoSuchAlgorithmException ex) { | ||
ex.printStackTrace(); | ||
log.error("[Function] 无法获取MD5算法实例!"); | ||
return ""; | ||
} | ||
if(StringUtils.isNotEmpty(salt)) { | ||
md.update(salt.getBytes()); | ||
} | ||
md.update(plainText.getBytes()); | ||
return encodeHex(md.digest()); | ||
} | ||
|
||
/** | ||
* 把字节数组转化成16进制字符串 | ||
* @param byteData md5的结果字节数组 | ||
* @return 16进制字符串 | ||
* @see org.springframework.util.DigestUtils#encodeHex(byte[]) | ||
*/ | ||
private static String encodeHex(byte[] byteData) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (byte byteD : byteData) { | ||
sb.append(Integer.toString((byteD & 0xff) + 0x100, 16).substring(1)); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.