From 95c007a7a2c1b8d314e7fe012ef6b6cc9b069a82 Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Sat, 16 Dec 2023 15:10:07 +0800 Subject: [PATCH] Beta91 (#244) * beta88 * beta89 * bugfix * beta90 * beta91 --- Processor/Processor.go | 17 +++++++++++-- config/config.go | 26 ++++++++++++++++++++ echo/echo.go | 16 +++++++++---- go.mod | 1 + go.sum | 2 ++ handlers/message_parser.go | 38 ++++++++++++++++++++++++++---- handlers/send_group_forward_msg.go | 2 +- handlers/send_group_msg.go | 2 +- handlers/send_guild_channel_msg.go | 2 +- handlers/send_private_msg.go | 4 ++-- template/config_template.go | 2 ++ template/config_template.yml | 4 +++- 12 files changed, 100 insertions(+), 16 deletions(-) diff --git a/Processor/Processor.go b/Processor/Processor.go index 46e6a122..b0e34154 100644 --- a/Processor/Processor.go +++ b/Processor/Processor.go @@ -344,6 +344,7 @@ func (p *Processors) HandleFrameworkCommand(messageText string, data interface{} } var err error var now, new, newpro1, newpro2 string + var nowgroup, newgroup string var realid, realid2 string var guildid, guilduserid string switch v := data.(type) { @@ -378,10 +379,22 @@ func (p *Processors) HandleFrameworkCommand(messageText string, data interface{} // 获取MasterID数组 masterIDs := config.GetMasterID() - // 根据realid获取new + // 根据realid获取new(用户id) now, new, err = idmap.RetrieveVirtualValuev2(realid) + if err != nil { + mylog.Printf("根据realid获取new(用户id) 错误:%v", err) + } + // 根据realid获取new(群id) + nowgroup, newgroup, err = idmap.RetrieveVirtualValuev2(realid2) + if err != nil { + mylog.Printf("根据realid获取new(群id)错误:%v", err) + } + // idmaps-pro获取群和用户id if config.GetIdmapPro() { newpro1, newpro2, err = idmap.RetrieveVirtualValuev2Pro(realid2, realid) + if err != nil { + mylog.Printf("idmaps-pro获取群和用户id 错误:%v", err) + } } // 检查真实值或虚拟值是否在数组中 var realValueIncluded, virtualValueIncluded bool @@ -437,7 +450,7 @@ func (p *Processors) HandleFrameworkCommand(messageText string, data interface{} message := fmt.Sprintf("idmaps-pro状态:\n%s\n%s\n%s", userMapping, groupMapping, bindInstruction) SendMessage(message, data, Type, p.Api, p.Apiv2) } else { - SendMessage("目前状态:\n当前真实值 "+now+"\n当前虚拟值 "+new+"\nbind指令:"+config.GetBindPrefix()+" 当前虚拟值"+" 目标虚拟值", data, Type, p.Api, p.Apiv2) + SendMessage("目前状态:\n当前真实值(用户) "+now+"\n当前虚拟值(用户) "+new+"\n当前真实值(群/频道) "+nowgroup+"\n当前虚拟值(群/频道) "+newgroup+"\nbind指令:"+config.GetBindPrefix()+" 当前虚拟值"+" 目标虚拟值", data, Type, p.Api, p.Apiv2) } return nil } diff --git a/config/config.go b/config/config.go index 21269394..b465c779 100644 --- a/config/config.go +++ b/config/config.go @@ -102,6 +102,8 @@ type Settings struct { PostMaxRetries []int `yaml:"post_max_retries"` PostRetriesInterval []int `yaml:"post_retries_interval"` NativeOb11 bool `yaml:"native_ob11"` + RamDomSeq bool `yaml:"ramdom_seq"` + UrlToQrimage bool `yaml:"url_to_qrimage"` } // LoadConfig 从文件中加载配置并初始化单例配置 @@ -1234,3 +1236,27 @@ func GetNativeOb11() bool { } return instance.Settings.NativeOb11 } + +// 获取GetRamDomSeq的值 +func GetRamDomSeq() bool { + mu.Lock() + defer mu.Unlock() + + if instance == nil { + mylog.Println("Warning: instance is nil when trying to GetRamDomSeq value.") + return false + } + return instance.Settings.RamDomSeq +} + +// 获取GetUrlToQrimage的值 +func GetUrlToQrimage() bool { + mu.Lock() + defer mu.Unlock() + + if instance == nil { + mylog.Println("Warning: instance is nil when trying to GetUrlToQrimage value.") + return false + } + return instance.Settings.UrlToQrimage +} diff --git a/echo/echo.go b/echo/echo.go index 91f22e9a..3c099d35 100644 --- a/echo/echo.go +++ b/echo/echo.go @@ -1,9 +1,12 @@ package echo import ( + "math/rand" "strconv" "sync" + "time" + "github.com/hoshinonyaruko/gensokyo/config" "github.com/tencent-connect/botgo/dto" ) @@ -142,11 +145,16 @@ func AddMappingSeq(key string, value int) { globalStringToIntMappingSeq.mapping[key] = value } -// GetMapping 根据给定的 string 键获取映射值 +// GetMappingSeq 根据给定的 string 键获取映射值 func GetMappingSeq(key string) int { - globalStringToIntMappingSeq.mu.Lock() - defer globalStringToIntMappingSeq.mu.Unlock() - return globalStringToIntMappingSeq.mapping[key] + if config.GetRamDomSeq() { + rng := rand.New(rand.NewSource(time.Now().UnixNano())) + return rng.Intn(10000) + 1 // 生成 1 到 10000 的随机数 + } else { + globalStringToIntMappingSeq.mu.Lock() + defer globalStringToIntMappingSeq.mu.Unlock() + return globalStringToIntMappingSeq.mapping[key] + } } // AddMapping 添加一个新的映射 diff --git a/go.mod b/go.mod index 228c9d2f..0ca57807 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/go-ole/go-ole v1.2.6 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mvdan/xurls v1.1.0 // indirect + github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect diff --git a/go.sum b/go.sum index 52ae7f96..9d54ec2d 100644 --- a/go.sum +++ b/go.sum @@ -103,6 +103,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/handlers/message_parser.go b/handlers/message_parser.go index 8b0e03d4..e9bf2871 100644 --- a/handlers/message_parser.go +++ b/handlers/message_parser.go @@ -2,6 +2,7 @@ package handlers import ( "context" + "encoding/base64" "encoding/json" "errors" "fmt" @@ -20,6 +21,7 @@ import ( "github.com/hoshinonyaruko/gensokyo/idmap" "github.com/hoshinonyaruko/gensokyo/mylog" "github.com/hoshinonyaruko/gensokyo/url" + "github.com/skip2/go-qrcode" "github.com/tencent-connect/botgo/dto" "github.com/tencent-connect/botgo/openapi" "mvdan.cc/xurls" //xurls是一个从文本提取url的库 适用于多种场景 @@ -77,7 +79,7 @@ func SendResponse(client callapi.Client, err error, message *callapi.ActionMessa } // 信息处理函数 -func parseMessageContent(paramsMessage callapi.ParamsContent) (string, map[string][]string) { +func parseMessageContent(paramsMessage callapi.ParamsContent, message callapi.ActionMessage, client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI) (string, map[string][]string) { messageText := "" switch message := paramsMessage.Message.(type) { @@ -192,7 +194,7 @@ func parseMessageContent(paramsMessage callapi.ParamsContent) (string, map[strin messageText = pattern.pattern.ReplaceAllString(messageText, "") } //最后再处理Url - messageText = transformMessageTextUrl(messageText) + messageText = transformMessageTextUrl(messageText, message, client, api, apiv2) // for key, items := range foundItems { // fmt.Printf("Key: %s, Items: %v\n", key, items) @@ -238,13 +240,14 @@ func transformMessageTextAt(messageText string) string { } // 链接处理 -func transformMessageTextUrl(messageText string) string { - //是否处理url +func transformMessageTextUrl(messageText string, message callapi.ActionMessage, client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI) string { + // 是否处理url if config.GetTransferUrl() { // 判断服务器地址是否是IP地址 serverAddress := config.GetServer_dir() isIP := isIPAddress(serverAddress) VisualIP := config.GetVisibleIP() + // 使用xurls来查找和替换所有的URL messageText = xurls.Relaxed.ReplaceAllStringFunc(messageText, func(originalURL string) string { // 当服务器地址是IP地址且GetVisibleIP为false时,替换URL为空 @@ -252,6 +255,21 @@ func transformMessageTextUrl(messageText string) string { return "" } + // 如果启用了URL到QR码的转换 + if config.GetUrlToQrimage() { + // 将URL转换为QR码的字节形式 + qrCodeGenerator, _ := qrcode.New(originalURL, qrcode.High) + qrCodeGenerator.DisableBorder = true + pngBytes, _ := qrCodeGenerator.PNG(37) + //pngBytes 二维码图片的字节数据 + base64Image := base64.StdEncoding.EncodeToString(pngBytes) + picmsg := processActionMessageWithBase64PicReplace(base64Image, message) + ret := callapi.CallAPIFromDict(client, api, apiv2, picmsg) + mylog.Printf("发送url转图片结果:%v", ret) + // 从文本中去除原始URL + return "" // 返回空字符串以去除URL + } + // 根据配置处理URL if config.GetLotusValue() { // 连接到另一个gensokyo @@ -268,6 +286,18 @@ func transformMessageTextUrl(messageText string) string { return messageText } +// processActionMessageWithBase64PicReplace 将原有的callapi.ActionMessage内容替换为一个base64图片 +func processActionMessageWithBase64PicReplace(base64Image string, message callapi.ActionMessage) callapi.ActionMessage { + newMessage := createCQImageMessage(base64Image) + message.Params.Message = newMessage + return message +} + +// createCQImageMessage 从 base64 编码的图片创建 CQ 码格式的消息 +func createCQImageMessage(base64Image string) string { + return "[CQ:image,file=" + base64Image + "]" +} + // 处理at和其他定形文到onebotv11格式(cq码) func RevertTransformedText(data interface{}, msgtype string, api openapi.OpenAPI, apiv2 openapi.OpenAPI, vgid int64) string { var msg *dto.Message diff --git a/handlers/send_group_forward_msg.go b/handlers/send_group_forward_msg.go index 3a9960dd..aa475b8b 100644 --- a/handlers/send_group_forward_msg.go +++ b/handlers/send_group_forward_msg.go @@ -44,7 +44,7 @@ func HandleSendGroupForwardMsg(client callapi.Client, api openapi.OpenAPI, apiv2 content, ok := nodeData["content"].([]interface{}) if ok { // 处理 segment 类型的 content - messageText, _ = parseMessageContent(callapi.ParamsContent{Message: content}) + messageText, _ = parseMessageContent(callapi.ParamsContent{Message: content}, message, client, api, apiv2) } else { // 处理直接包含的文本内容 contentString, ok := nodeData["content"].(string) diff --git a/handlers/send_group_msg.go b/handlers/send_group_msg.go index 3f05e047..31203be2 100644 --- a/handlers/send_group_msg.go +++ b/handlers/send_group_msg.go @@ -77,7 +77,7 @@ func HandleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap switch msgType { case "group": // 解析消息内容 - messageText, foundItems := parseMessageContent(message.Params) + messageText, foundItems := parseMessageContent(message.Params, message, client, api, apiv2) var SSM bool // 使用 echo 获取消息ID var messageID string diff --git a/handlers/send_guild_channel_msg.go b/handlers/send_guild_channel_msg.go index 7b06c5ce..73364679 100644 --- a/handlers/send_guild_channel_msg.go +++ b/handlers/send_guild_channel_msg.go @@ -52,7 +52,7 @@ func HandleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2 //原生guild信息 case "guild": params := message.Params - messageText, foundItems := parseMessageContent(params) + messageText, foundItems := parseMessageContent(params, message, client, api, apiv2) channelID := params.ChannelID // 使用 echo 获取消息ID diff --git a/handlers/send_private_msg.go b/handlers/send_private_msg.go index 094c9770..7a8a709b 100644 --- a/handlers/send_private_msg.go +++ b/handlers/send_private_msg.go @@ -90,7 +90,7 @@ func HandleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open } // 解析消息内容 - messageText, foundItems := parseMessageContent(message.Params) + messageText, foundItems := parseMessageContent(message.Params, message, client, api, apiv2) // 使用 echo 获取消息ID var messageID string @@ -293,7 +293,7 @@ func HandleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open // 处理频道私信 最后2个指针参数可空 代表使用userid倒推 func HandleSendGuildChannelPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage, optionalGuildID *string, optionalChannelID *string) (string, error) { params := message.Params - messageText, foundItems := parseMessageContent(params) + messageText, foundItems := parseMessageContent(params, message, client, api, apiv2) var guildID, channelID string var err error diff --git a/template/config_template.go b/template/config_template.go index 0b95b310..46c61b55 100644 --- a/template/config_template.go +++ b/template/config_template.go @@ -53,6 +53,8 @@ settings: heart_beat_interval : 10 #反向ws心跳间隔 单位秒 推荐5-10 launch_reconnect_times : 1 #启动时尝试反向ws连接次数,建议先打开应用端再开启gensokyo,因为启动时连接会阻塞webui启动,默认只连接一次,可自行增大 native_ob11 : false #如果你的机器人收到事件报错,请开启此选项增加兼容性 + ramdom_seq : false #当多开gensokyo时,如果遇到群信息只能发出一条,请开启每个gsk的此项.(建议使用一个gsk连接多个应用) + url_to_qrimage : false #将信息中的url转换为二维码单独作为图片发出,需要同时设置 #SSL配置类 机器人发送URL设置 的 transfer_url 为 true #正向ws设置 ws_server_path : "ws" #默认监听0.0.0.0:port/ws_server_path 若有安全需求,可不放通port到公网,或设置ws_server_token 若想监听/ 可改为"",若想监听到不带/地址请写nil diff --git a/template/config_template.yml b/template/config_template.yml index cce6756c..c59894a6 100644 --- a/template/config_template.yml +++ b/template/config_template.yml @@ -84,4 +84,6 @@ settings: launch_reconnect_times : 1 #启动时尝试反向ws连接次数,建议先打开应用端再开启gensokyo,因为启动时连接会阻塞webui启动,默认只连接一次,可自行增大 white_bypass : [] #格式[1,2,3],白名单不生效的群,用于设置自己的灰度沙箱,避免测试时候反复开关白名单的不便. transfer_url : true #默认开启,关闭后自理url发送,配置server_dir为你的域名,配置crt和key后,将域名/url和/image在q.qq.com后台通过校验,自动使用302跳转处理机器人发出的所有域名. - native_ob11 : false #如果你的机器人收到事件报错,请开启此选项增加兼容性 \ No newline at end of file + native_ob11 : false #如果你的机器人收到事件报错,请开启此选项增加兼容性 + ramdom_seq : false #当多开gensokyo时,如果遇到群信息只能发出一条,请开启每个gsk的此项.(建议使用一个gsk连接多个应用) + url_to_qrimage : false #将信息中的url转换为二维码单独作为图片发出,需要同时设置 #SSL配置类 机器人发送URL设置 的 transfer_url 为 true