Skip to content

Commit

Permalink
Beta91 (#244)
Browse files Browse the repository at this point in the history
* beta88

* beta89

* bugfix

* beta90

* beta91
  • Loading branch information
Hoshinonyaruko authored Dec 16, 2023
1 parent 0cdb914 commit 95c007a
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 16 deletions.
17 changes: 15 additions & 2 deletions Processor/Processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
26 changes: 26 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 从文件中加载配置并初始化单例配置
Expand Down Expand Up @@ -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
}
16 changes: 12 additions & 4 deletions echo/echo.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package echo

import (
"math/rand"
"strconv"
"sync"
"time"

"github.com/hoshinonyaruko/gensokyo/config"
"github.com/tencent-connect/botgo/dto"
)

Expand Down Expand Up @@ -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 添加一个新的映射
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
38 changes: 34 additions & 4 deletions handlers/message_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand All @@ -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的库 适用于多种场景
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -238,20 +240,36 @@ 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为空
if isIP && !VisualIP {
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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion handlers/send_group_forward_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion handlers/send_guild_channel_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions handlers/send_private_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions template/config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion template/config_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 #如果你的机器人收到事件报错,请开启此选项增加兼容性
native_ob11 : false #如果你的机器人收到事件报错,请开启此选项增加兼容性
ramdom_seq : false #当多开gensokyo时,如果遇到群信息只能发出一条,请开启每个gsk的此项.(建议使用一个gsk连接多个应用)
url_to_qrimage : false #将信息中的url转换为二维码单独作为图片发出,需要同时设置 #SSL配置类 机器人发送URL设置 的 transfer_url 为 true

0 comments on commit 95c007a

Please sign in to comment.