Skip to content

Commit

Permalink
feat(red-protocol): red 协议的适配 (#341)
Browse files Browse the repository at this point in the history
* feat(red-protocol): red 协议的简单适配

* chore: make lint happy
  • Loading branch information
JustAnotherID authored Oct 19, 2023
1 parent ec4fa3a commit f912fe5
Show file tree
Hide file tree
Showing 10 changed files with 1,019 additions and 28 deletions.
1 change: 1 addition & 0 deletions api/api_bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ func Bind(e *echo.Echo, _myDice *dice.DiceManager) {
e.POST(prefix+"/im_connections/addDodo", ImConnectionsAddDodo)
e.POST(prefix+"/im_connections/addWalleQ", ImConnectionsAddWalleQ)
e.POST(prefix+"/im_connections/addGocqSeparate", ImConnectionsAddGocqSeparate)
e.POST(prefix+"/im_connections/addRed", ImConnectionsAddRed)
e.POST(prefix+"/im_connections/del", ImConnectionsDel)
e.POST(prefix+"/im_connections/set_enable", ImConnectionsSetEnable)
e.POST(prefix+"/im_connections/set_data", ImConnectionsSetData)
Expand Down
28 changes: 28 additions & 0 deletions api/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,31 @@ func ImConnectionsAddGocqSeparate(c echo.Context) error {
}
return c.String(430, "")
}

func ImConnectionsAddRed(c echo.Context) error {
if !doAuth(c) {
return c.JSON(http.StatusForbidden, nil)
}
if dm.JustForTest {
return Success(&c, Response{"testMode": true})
}

v := struct {
Host string `yaml:"host" json:"host"`
Port int `yaml:"port" json:"port"`
Token string `yaml:"token" json:"token"`
}{}
err := c.Bind(&v)
if err == nil {
conn := dice.NewRedConnItem(v.Host, v.Port, v.Token)
conn.Session = myDice.ImSession
pa := conn.Adapter.(*dice.PlatformAdapterRed)
pa.Session = myDice.ImSession
myDice.ImSession.EndPoints = append(myDice.ImSession.EndPoints, conn)
myDice.LastUpdatedTime = time.Now().Unix()
myDice.Save(false)
go dice.ServeRed(myDice, conn)
return Success(&c, Response{})
}
return c.String(430, "")
}
18 changes: 16 additions & 2 deletions dice/im_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ type EndPointInfoBase struct {
Platform string `yaml:"platform" json:"platform" jsbind:"platform"` // 平台,如QQ等
RelWorkDir string `yaml:"relWorkDir" json:"relWorkDir"` // 工作目录
Enable bool `yaml:"enable" json:"enable" jsbind:"enable"` // 是否启用
ProtocolType string `yaml:"protocolType"` // 协议类型,如onebot、koishi等
ProtocolType string `yaml:"protocolType" json:"protocolType"` // 协议类型,如onebot、koishi等

IsPublic bool `yaml:"isPublic"`
Session *IMSession `yaml:"-" json:"-"`
Expand Down Expand Up @@ -274,6 +274,15 @@ func (ep *EndPointInfo) UnmarshalYAML(value *yaml.Node) error {
Adapter *PlatformAdapterWalleQ `yaml:"adapter"`
}

err = value.Decode(&val)
if err != nil {
return err
}
ep.Adapter = val.Adapter
case "red":
var val struct {
Adapter *PlatformAdapterRed `yaml:"adapter"`
}
err = value.Decode(&val)
if err != nil {
return err
Expand Down Expand Up @@ -407,7 +416,7 @@ type MsgContext struct {
}

// func (s *IMSession) GroupEnableCheck(ep *EndPointInfo, msg *Message, runInSync bool) {
//}
// }

// fillPrivilege 填写MsgContext中的权限字段, 并返回填写的权限等级
// - msg 使用其中的msg.Sender.GroupRole
Expand Down Expand Up @@ -1162,6 +1171,11 @@ func (ep *EndPointInfo) AdapterSetup() {
pa.Session = ep.Session
pa.EndPoint = ep
}
if ep.ProtocolType == "red" {
pa := ep.Adapter.(*PlatformAdapterRed)
pa.Session = ep.Session
pa.EndPoint = ep
}
case "DISCORD":
pa := ep.Adapter.(*PlatformAdapterDiscord)
pa.Session = ep.Session
Expand Down
2 changes: 1 addition & 1 deletion dice/platform_adapter_qq.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ func (pa *PlatformAdapterGocq) Serve() int {

socket.OnDisconnected = func(err error, socket gowebsocket.Socket) {
log.Info("onebot 服务的连接被对方关闭 ")
_ = pa.Session.Parent.SendMail("", MailTypeOnebotClose)
_ = pa.Session.Parent.SendMail("", MailTypeConnectClose)
pa.InPackGoCqhttpDisconnectedCH <- 1
}

Expand Down
Loading

0 comments on commit f912fe5

Please sign in to comment.