|
2 | 2 | package crypter |
3 | 3 |
|
4 | 4 | import ( |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
5 | 10 | "github.com/FloatTech/AnimeAPI/airecord" |
6 | 11 | zero "github.com/wdvxdr1123/ZeroBot" |
7 | 12 | "github.com/wdvxdr1123/ZeroBot/message" |
8 | 13 | ) |
9 | 14 |
|
| 15 | +var faceTagRe = regexp.MustCompile(`\{\{face:(\d+)\}\}`) |
| 16 | + |
| 17 | +func parseID(v interface{}) int64 { |
| 18 | + n, _ := strconv.ParseInt(fmt.Sprint(v), 10, 64) |
| 19 | + return n |
| 20 | +} |
| 21 | + |
| 22 | +func serializeMsg(segs message.Message) string { |
| 23 | + var sb strings.Builder |
| 24 | + for _, seg := range segs { |
| 25 | + switch seg.Type { |
| 26 | + case "text": |
| 27 | + sb.WriteString(seg.Data["text"]) |
| 28 | + case "face": |
| 29 | + fmt.Fprintf(&sb, "{{face:%v}}", seg.Data["id"]) |
| 30 | + } |
| 31 | + } |
| 32 | + return sb.String() |
| 33 | +} |
| 34 | + |
| 35 | +func deserializeMsg(s string) message.Message { |
| 36 | + var msg message.Message |
| 37 | + parts := faceTagRe.Split(s, -1) |
| 38 | + matches := faceTagRe.FindAllStringSubmatch(s, -1) |
| 39 | + for i, part := range parts { |
| 40 | + if part != "" { |
| 41 | + msg = append(msg, message.Text(part)) |
| 42 | + } |
| 43 | + if i < len(matches) { |
| 44 | + id, _ := strconv.Atoi(matches[i][1]) |
| 45 | + msg = append(msg, message.Face(id)) |
| 46 | + } |
| 47 | + } |
| 48 | + return msg |
| 49 | +} |
| 50 | + |
| 51 | +func getInput(ctx *zero.Ctx, cmds ...string) string { |
| 52 | + full := serializeMsg(ctx.Event.Message) |
| 53 | + for _, cmd := range cmds { |
| 54 | + if idx := strings.Index(full, cmd); idx >= 0 { |
| 55 | + return strings.TrimSpace(full[idx+len(cmd):]) |
| 56 | + } |
| 57 | + } |
| 58 | + return "" |
| 59 | +} |
| 60 | + |
| 61 | +func getReplyContent(ctx *zero.Ctx) string { |
| 62 | + for _, seg := range ctx.Event.Message { |
| 63 | + if seg.Type == "reply" { |
| 64 | + if msgID := parseID(seg.Data["id"]); msgID > 0 { |
| 65 | + if msg := ctx.GetMessage(msgID); msg.Elements != nil { |
| 66 | + return serializeMsg(msg.Elements) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return "" |
| 72 | +} |
| 73 | + |
| 74 | +func getReplyFaceIDs(ctx *zero.Ctx) []int { |
| 75 | + for _, seg := range ctx.Event.Message { |
| 76 | + if seg.Type == "reply" { |
| 77 | + if msgID := parseID(seg.Data["id"]); msgID > 0 { |
| 78 | + return extractFaceIDs(ctx.GetMessage(msgID).Elements) |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func extractFaceIDs(segs message.Message) []int { |
| 86 | + var ids []int |
| 87 | + for _, seg := range segs { |
| 88 | + if seg.Type == "face" { |
| 89 | + if id := int(parseID(seg.Data["id"])); id > 0 { |
| 90 | + ids = append(ids, id) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return ids |
| 95 | +} |
| 96 | + |
10 | 97 | // hou |
11 | 98 | func houEncryptHandler(ctx *zero.Ctx) { |
12 | | - text := ctx.State["regex_matched"].([]string)[1] |
| 99 | + text := getInput(ctx, "h加密", "齁语加密") |
13 | 100 | result := encodeHou(text) |
14 | 101 | recCfg := airecord.GetConfig() |
15 | | - record := ctx.GetAIRecord(recCfg.ModelID, recCfg.Customgid, result) |
16 | | - if record != "" { |
| 102 | + if record := ctx.GetAIRecord(recCfg.ModelID, recCfg.Customgid, result); record != "" { |
17 | 103 | ctx.SendChain(message.Record(record)) |
18 | 104 | } else { |
19 | 105 | ctx.SendChain(message.Text(result)) |
20 | 106 | } |
21 | 107 | } |
22 | 108 |
|
23 | 109 | func houDecryptHandler(ctx *zero.Ctx) { |
24 | | - text := ctx.State["regex_matched"].([]string)[1] |
25 | | - result := decodeHou(text) |
26 | | - ctx.SendChain(message.Text(result)) |
| 110 | + text := getInput(ctx, "h解密", "齁语解密") |
| 111 | + if text == "" { |
| 112 | + text = getReplyContent(ctx) |
| 113 | + } |
| 114 | + if text == "" { |
| 115 | + ctx.SendChain(message.Text("请输入密文或回复加密消息")) |
| 116 | + return |
| 117 | + } |
| 118 | + ctx.SendChain(deserializeMsg(decodeHou(text))...) |
27 | 119 | } |
28 | 120 |
|
29 | 121 | // fumo |
30 | 122 | func fumoEncryptHandler(ctx *zero.Ctx) { |
31 | | - text := ctx.State["regex_matched"].([]string)[1] |
32 | | - result := encryptFumo(text) |
33 | | - ctx.SendChain(message.Text(result)) |
| 123 | + ctx.SendChain(message.Text(encryptFumo(getInput(ctx, "fumo加密")))) |
34 | 124 | } |
35 | 125 |
|
36 | 126 | func fumoDecryptHandler(ctx *zero.Ctx) { |
37 | | - text := ctx.State["regex_matched"].([]string)[1] |
38 | | - result := decryptFumo(text) |
39 | | - ctx.SendChain(message.Text(result)) |
| 127 | + text := getInput(ctx, "fumo解密") |
| 128 | + if text == "" { |
| 129 | + text = getReplyContent(ctx) |
| 130 | + } |
| 131 | + if text == "" { |
| 132 | + ctx.SendChain(message.Text("请输入密文或回复加密消息")) |
| 133 | + return |
| 134 | + } |
| 135 | + ctx.SendChain(deserializeMsg(decryptFumo(text))...) |
| 136 | +} |
| 137 | + |
| 138 | +// qq表情 |
| 139 | +func qqEmojiEncryptHandler(ctx *zero.Ctx) { |
| 140 | + text := getInput(ctx, "qq加密") |
| 141 | + if text == "" { |
| 142 | + ctx.SendChain(message.Text("请输入要加密的文本")) |
| 143 | + return |
| 144 | + } |
| 145 | + ctx.SendChain(encodeQQEmoji(text)...) |
| 146 | +} |
| 147 | + |
| 148 | +func qqEmojiDecryptHandler(ctx *zero.Ctx) { |
| 149 | + faceIDs := extractFaceIDs(ctx.Event.Message) |
| 150 | + if len(faceIDs) == 0 { |
| 151 | + faceIDs = getReplyFaceIDs(ctx) |
| 152 | + } |
| 153 | + if len(faceIDs) == 0 { |
| 154 | + ctx.SendChain(message.Text("请回复QQ表情加密消息进行解密")) |
| 155 | + return |
| 156 | + } |
| 157 | + ctx.SendChain(deserializeMsg(decodeQQEmoji(faceIDs))...) |
40 | 158 | } |
0 commit comments