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