Skip to content

Commit

Permalink
refact:基本重构了适配器和事件总线的设计,准备合并
Browse files Browse the repository at this point in the history
  • Loading branch information
kyokukong committed Sep 28, 2024
1 parent 8d98866 commit 3eedc1d
Show file tree
Hide file tree
Showing 16 changed files with 1,201 additions and 76 deletions.
137 changes: 131 additions & 6 deletions adapters/lagrange/event_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,144 @@ import (
"github.com/Iceinu-Project/Iceinu/models/satori"
"github.com/LagrangeDev/LagrangeGo/client"
"github.com/LagrangeDev/LagrangeGo/message"
"strconv"
"time"
)

func EventsBinder() {
// 绑定私聊消息事件
Client.PrivateMessageEvent.Subscribe(func(client *client.QQClient, event *message.PrivateMessage) {
func BindEvents() {
// 注册私聊事件
Manager.RegisterPrivateMessageHandler(func(client *client.QQClient, event *message.PrivateMessage) {
// 尝试从适配器的缓存中获取自身信息
selfInfo := GetSelfInfoInCache(client)
// 尝试从适配器的缓存中获取好友信息
friendInfo := GetFriendsDataInCache(client)
ice.Publish(&ice.IceinuEvent{
Type: 5,
Type: 10,
From: ice.GetSelfNodeId(),
Target: ice.GetMasterNodeId(),
Timestamp: time.Now().Unix(),
Summary: "",
Event: &satori.EventSatori{},
Summary: "PrivateMessageEvent",
Event: &satori.EventSatori{
Id: uint64(event.Id),
Type: "PrivateMessageEvent",
Platform: "QQNT",
SelfId: strconv.Itoa(int(client.Uin)),
Timestamp: int64(event.Time),
Argv: nil,
Button: nil,
Channel: &satori.Channel{
Id: strconv.Itoa(int(event.Sender.Uin)),
Type: 1,
Name: event.Sender.Uid,
ParentId: "",
},
Group: &satori.Group{
Id: "",
Name: "",
Avatar: "",
Maxcount: 0,
MemberCount: 0,
},
Login: nil,
Member: nil,
Message: &satori.Message{
Id: strconv.Itoa(int(event.InternalId)),
Content: event.ToString(),
Channel: nil,
Group: nil,
Member: nil,
User: nil,
CreatedAt: int64(event.Time),
UpdatedAt: int64(event.Time),
MessageElements: ToSatoriElements(event.Elements),
},
Operator: &satori.User{
Id: strconv.Itoa(int(event.Sender.Uin)),
Name: event.Sender.CardName,
Nickname: event.Sender.Nickname,
Avatar: friendInfo[event.Sender.Uin].Avatar,
IsBot: false,
},
Role: nil,
User: &satori.User{
Id: strconv.Itoa(int(event.Self)),
Name: client.NickName(),
Nickname: client.NickName(),
Avatar: selfInfo.Avatar,
IsBot: false,
},
},
})
// 将LagrangeGo的消息存入消息缓存
err := Cache.Set(strconv.Itoa(int(event.InternalId)), event)
if err != nil {
return
}
})
// 注册群聊事件
Manager.RegisterGroupMessageHandler(func(client *client.QQClient, event *message.GroupMessage) {
// 尝试从LagrangeGo的内置缓存中获取群信息
groupInfo := client.GetCachedGroupInfo(event.GroupUin)
// 尝试从适配器的缓存中获取自身信息
selfInfo := GetSelfInfoInCache(client)
// 尝试从适配器的缓存中获取群成员映射
groupMemberData := GetGroupMembersDataInCache(client, event.GroupUin)
ice.Publish(&ice.IceinuEvent{
Type: 10,
From: ice.GetSelfNodeId(),
Target: ice.GetMasterNodeId(),
Timestamp: time.Now().Unix(),
Summary: "GroupMessageEvent",
Event: &satori.EventSatori{
Id: uint64(event.Id),
Type: "GroupMessageEvent",
Platform: "QQNT",
SelfId: strconv.Itoa(int(client.Uin)),
Timestamp: int64(event.Time),
Argv: nil,
Button: nil,
Channel: &satori.Channel{
Id: strconv.Itoa(int(event.GroupUin)),
Type: 0,
Name: event.GroupName,
ParentId: "",
},
Group: &satori.Group{
Id: strconv.Itoa(int(event.GroupUin)),
Name: event.GroupName,
Avatar: groupInfo.Avatar,
Maxcount: groupInfo.MaxMember,
MemberCount: groupInfo.MemberCount,
},
Login: nil,
Member: nil,
Message: &satori.Message{
Id: strconv.Itoa(int(event.InternalId)),
Content: event.ToString(),
Channel: nil,
Group: nil,
Member: nil,
User: nil,
CreatedAt: int64(event.Time),
UpdatedAt: int64(event.Time),
MessageElements: ToSatoriElements(event.Elements),
},
Operator: &satori.User{
Id: strconv.Itoa(int(event.Sender.Uin)),
Name: event.Sender.CardName,
Nickname: event.Sender.Nickname,
Avatar: groupMemberData[event.Sender.Uin].Avatar,
IsBot: false,
},
Role: nil,
User: &satori.User{
Id: strconv.Itoa(int(client.Uin)),
Name: client.NickName(),
Nickname: client.NickName(),
Avatar: selfInfo.Avatar,
IsBot: false,
},
},
})
})
}
64 changes: 39 additions & 25 deletions adapters/lagrange/lagrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package lagrange

import (
"github.com/Iceinu-Project/Iceinu/adapters"
"github.com/Iceinu-Project/Iceinu/cache"
"github.com/Iceinu-Project/Iceinu/ice"
"github.com/Iceinu-Project/Iceinu/log"
"github.com/LagrangeDev/LagrangeGo/client"
"github.com/LagrangeDev/LagrangeGo/client/auth"
"github.com/sirupsen/logrus"
"os"
"os/signal"
"strconv"
"syscall"
)

// InfosLagrangeAdapter LagrangeGo适配器元信息
Expand All @@ -24,12 +28,18 @@ var InfosLagrangeAdapter = adapters.AdapterInfo{
// Client LagrangeGo客户端实例
var Client *client.QQClient

// Cache 消息缓存
var Cache *cache.IceCacheManager

// AdapterLagrangeGo LagrangeGo适配器
type AdapterLagrangeGo struct{}

func (AdapterLagrangeGo) Init() error {
func (a *AdapterLagrangeGo) Init() error {
// 读取配置文件
AdapterConfigInit()
// 初始化消息缓存
log.Debug("正在初始化LagrangeGo的消息缓存...")
Cache = cache.NewIceCacheManager(AdapterLagrangeConf.CacheSize, AdapterLagrangeConf.CacheExpire)
// 日志输出
appInfo := auth.AppList["linux"]["3.2.10-25765"]
deviceInfo := auth.NewDeviceInfo(AdapterLagrangeConf.Lagrange.Account)
Expand All @@ -40,11 +50,11 @@ func (AdapterLagrangeGo) Init() error {
// 尝试读取签名文件
data, err := os.ReadFile("signature.bin")
if err != nil {
logrus.Warnln("读取签名文件时发生错误:", err)
log.Warn("读取签名文件时发生错误:", err)
} else {
sig, err := auth.UnmarshalSigInfo(data, true)
if err != nil {
logrus.Warnln("加载签名文件时发生错误:", err)
log.Warn("加载签名文件时发生错误:", err)
} else {
qqClientInstance.UseSig(sig)
}
Expand All @@ -54,45 +64,45 @@ func (AdapterLagrangeGo) Init() error {
return nil
}

func (AdapterLagrangeGo) SubscribeEvents() error {
EventsBinder()
func (a *AdapterLagrangeGo) SubscribeEvents() error {
BindEvents()
return nil
}

func (AdapterLagrangeGo) Start() error {
func (a *AdapterLagrangeGo) Start() error {
// 在函数结束时释放Client并尝试保存签名
defer Client.Release()
defer SaveSignature()
// 登录
err := Login()
if err != nil {
return err
}
// 尝试刷新client的所有缓存
err = Client.RefreshFriendCache()
if err != nil {
return err
}
err = Client.RefreshAllGroupsInfo()
// 事件订阅
err := a.SubscribeEvents()
if err != nil {
return err
}
err = Client.RefreshAllGroupMembersCache()
SetAllSubscribes()
// 登录
err = Login()
if err != nil {
return err
}
err = Client.RefreshAllRkeyInfoCache()
if err != nil {
return err
// 推送适配器连接事件
ice.MakeAdapterConnectEvent(InfosLagrangeAdapter.Name, InfosLagrangeAdapter.Model, strconv.Itoa(int(Client.Uin)), Client.NickName())

// 主协程关闭通道
mc := make(chan os.Signal, 2)
signal.Notify(mc, os.Interrupt, syscall.SIGTERM)
for {
switch <-mc {
case os.Interrupt, syscall.SIGTERM:
return nil
}
}
return nil
}

func (AdapterLagrangeGo) GetAdapterInfo() *adapters.AdapterInfo {
func (a *AdapterLagrangeGo) GetAdapterInfo() *adapters.AdapterInfo {
return &InfosLagrangeAdapter
}

func (AdapterLagrangeGo) GetUserTree() *adapters.UserTree {
func (a *AdapterLagrangeGo) GetUserTree() *adapters.UserTree {
//TODO implement me
panic("implement me")
}
Expand Down Expand Up @@ -122,3 +132,7 @@ func SaveSignature() {
}
log.Info("签名已被写入签名文件")
}

func GetAdapter() adapters.IceinuAdapter {
return &AdapterLagrangeGo{}
}
4 changes: 2 additions & 2 deletions adapters/lagrange/lagrange_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var AdapterLagrangeConf *AdapterConfig

// AdapterConfig 适配器配置
type AdapterConfig struct {
CacheSize int `toml:"message_cache_size"` // 消息缓存大小
CacheExpire int `toml:"message_cache_expire"` // 消息缓存过期时间
CacheSize uint32 `toml:"message_cache_size"` // 消息缓存大小
CacheExpire uint32 `toml:"message_cache_expire"` // 消息缓存过期时间
Lagrange LgrConfig `toml:"lagrange"`
}

Expand Down
3 changes: 2 additions & 1 deletion adapters/lagrange/protocol_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lagrange

import (
"fmt"
gradient "github.com/Iceinu-Project/IceGradient"
"github.com/Iceinu-Project/Iceinu/log"
"os"
"path"
Expand All @@ -13,7 +14,7 @@ type ProtocolLogger struct{}

var dumpspath = "dump"

const fromProtocol = "LGR | "
const fromProtocol = "LGR | " + gradient.DarkGray

func (p ProtocolLogger) Info(format string, arg ...any) {
log.Infof(fromProtocol+format, arg...)
Expand Down
Loading

0 comments on commit 3eedc1d

Please sign in to comment.