Skip to content

Commit

Permalink
feat: update protocol version,支持接收戳一戳
Browse files Browse the repository at this point in the history
  • Loading branch information
Redmomn committed Jul 16, 2024
1 parent 953254d commit 49bf9cf
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 90 deletions.
11 changes: 6 additions & 5 deletions cmd/gocq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ func LoginInteract() {
time.Sleep(time.Second * 5)
}
log.Info("开始尝试登录并同步消息...")
log.Infof("使用协议: %s", "linux")
cli = newClient()
app := auth.AppList["linux"]["3.2.10-25765"]
log.Infof("使用协议: %s %s", app.OS, app.CurrentVersion)
cli = newClient(app)
cli.UseDevice(device)
isQRCodeLogin := (base.Account.Uin == 0 || len(base.Account.Password) == 0) && !base.Account.Encrypt
isTokenLogin := false
Expand Down Expand Up @@ -240,7 +241,7 @@ func LoginInteract() {
time.Sleep(time.Second)
cli.Disconnect()
cli.Release()
cli = newClient()
cli = newClient(app)
cli.UseDevice(device)
} else {
isTokenLogin = true
Expand Down Expand Up @@ -402,14 +403,14 @@ func PasswordHashDecrypt(encryptedPasswordHash string, key []byte) ([]byte, erro
return result, nil
}

func newClient() *client.QQClient {
func newClient(appInfo *auth.AppInfo) *client.QQClient {
var signUrl string
if len(base.SignServers) != 0 {
signUrl = base.SignServers[0].URL
} else {
signUrl = ""
}
c := client.NewClient(0, signUrl, auth.AppList["linux"])
c := client.NewClient(0, signUrl, appInfo)
// TODO 服务器更新通知
//c.OnServerUpdated(func(bot *client.QQClient, e *client.ServerUpdatedEvent) bool {
// if !base.UseSSOAddress {
Expand Down
16 changes: 8 additions & 8 deletions coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type CQBot struct {
lock sync.RWMutex
events []func(*Event)

friendReqCache syncx.Map[string, *event2.FriendRequest]
friendReqCache syncx.Map[string, *event2.NewFriendRequest]
//tempSessionCache syncx.Map[int64, *event2.]
}

Expand Down Expand Up @@ -86,8 +86,8 @@ func NewQQBot(cli *client.QQClient) *CQBot {
//bot.Client.TempMessageEvent.Subscribe(bot.tempMessageEvent)
bot.Client.GroupMuteEvent.Subscribe(bot.groupMutedEvent)
bot.Client.GroupRecallEvent.Subscribe(bot.groupRecallEvent)
// TODO 群聊通知消息 戳一戳,运气王
//bot.Client.GroupNotifyEvent.Subscribe(bot.groupNotifyEvent)
// TODO 群聊通知消息 运气王
bot.Client.GroupNotifyEvent.Subscribe(bot.groupNotifyEvent)
// TODO 好友戳一戳事件
//bot.Client.FriendNotifyEvent.Subscribe(bot.friendNotifyEvent)
// TODO 成员获得特殊群头衔
Expand Down Expand Up @@ -317,7 +317,7 @@ func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) (in
// return bot.InsertGroupMessage(ret, source), nil
case *message.AtElement:
self := bot.Client.GetCachedMemberInfo(bot.Client.Uin, uint32(groupID))
if i.Target == 0 && self.Permission == entity.Member {
if i.TargetUin == 0 && self.Permission == entity.Member {
e = message.NewText("@全体成员")
}
}
Expand Down Expand Up @@ -505,8 +505,8 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage, source message.Sou
reply := replyElem.(*message.ReplyElement)
msg.SubType = "quote"
msg.QuotedInfo = &db.QuotedInfo{
PrevID: encodeMessageID(int64(m.GroupCode), reply.ReplySeq),
PrevGlobalID: db.ToGlobalID(int64(m.GroupCode), reply.ReplySeq),
PrevID: encodeMessageID(int64(m.GroupCode), int32(reply.ReplySeq)),
PrevGlobalID: db.ToGlobalID(int64(m.GroupCode), int32(reply.ReplySeq)),
QuotedContent: ToMessageContent(reply.Elements, source),
}
}
Expand Down Expand Up @@ -548,8 +548,8 @@ func (bot *CQBot) InsertPrivateMessage(m *message.PrivateMessage, source message
reply := replyElem.(*message.ReplyElement)
msg.SubType = "quote"
msg.QuotedInfo = &db.QuotedInfo{
PrevID: encodeMessageID(int64(reply.Sender), reply.ReplySeq),
PrevGlobalID: db.ToGlobalID(int64(reply.Sender), reply.ReplySeq),
PrevID: encodeMessageID(int64(reply.SenderUin), int32(reply.ReplySeq)),
PrevGlobalID: db.ToGlobalID(int64(reply.SenderUin), int32(reply.ReplySeq)),
QuotedContent: ToMessageContent(reply.Elements, source),
}
}
Expand Down
59 changes: 30 additions & 29 deletions coolq/cqcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ const (
func replyID(r *message.ReplyElement, source message.Source) int32 {
id := source.PrimaryID
seq := r.ReplySeq
if r.GroupID != 0 {
id = int64(r.GroupID)
if r.GroupUin != 0 {
id = int64(r.GroupUin)
}
// 私聊时,部分(不确定)的账号会在 ReplyElement 中带有 GroupID 字段。
// 这里需要判断是由于 “直接回复” 功能,GroupID 为触发直接回复的来源那个群。
if source.SourceType == message.SourcePrivate && (r.Sender == uint64(source.PrimaryID) || r.GroupID == uint64(source.PrimaryID) || r.GroupID == 0) {
// 私聊时,部分(不确定)的账号会在 ReplyElement 中带有 GroupUin 字段。
// 这里需要判断是由于 “直接回复” 功能,GroupUin 为触发直接回复的来源那个群。
if source.SourceType == message.SourcePrivate && (r.SenderUin == uint32(source.PrimaryID) || r.GroupUin == uint32(source.PrimaryID) || r.GroupUin == 0) {
// 私聊似乎腾讯服务器有bug?
seq = int32(uint16(seq))
id = int64(r.Sender)
// ?
seq = seq
id = int64(r.SenderUin)
}
return db.ToGlobalID(id, seq)
return db.ToGlobalID(id, int32(seq))
}

// toElements 将消息元素数组转为MSG数组以用于消息上报
Expand Down Expand Up @@ -84,7 +85,7 @@ func toElements(e []message.IMessageElement, source message.Source) (r []msg.Ele
if base.ExtraReplyData {
elem.Data = append(elem.Data,
pair{K: "seq", V: strconv.FormatInt(int64(replyElem.ReplySeq), 10)},
pair{K: "qq", V: strconv.FormatInt(int64(replyElem.Sender), 10)},
pair{K: "qq", V: strconv.FormatInt(int64(replyElem.SenderUin), 10)},
pair{K: "time", V: strconv.FormatInt(int64(replyElem.Time), 10)},
pair{K: "text", V: toStringMessage(replyElem.Elements, source)},
)
Expand All @@ -97,7 +98,7 @@ func toElements(e []message.IMessageElement, source message.Source) (r []msg.Ele
case *message.ReplyElement:
if base.RemoveReplyAt && i+1 < len(e) {
elem, ok := e[i+1].(*message.AtElement)
if ok && elem.Target == uint32(o.Sender) {
if ok && elem.TargetUin == uint32(o.SenderUin) {
e[i+1] = nil
}
}
Expand All @@ -119,8 +120,8 @@ func toElements(e []message.IMessageElement, source message.Source) (r []msg.Ele
// }
case *message.AtElement:
target := "all"
if o.Target != 0 {
target = strconv.FormatUint(uint64(o.Target), 10)
if o.TargetUin != 0 {
target = strconv.FormatUint(uint64(o.TargetUin), 10)
}
m = msg.Element{
Type: "at",
Expand Down Expand Up @@ -299,7 +300,7 @@ func ToMessageContent(e []message.IMessageElement, source message.Source) (r []g
// "data": global.MSG{"data": o.Content},
// }
case *message.AtElement:
if o.Target == 0 {
if o.TargetUin == 0 {
m = global.MSG{
"type": "at",
"data": global.MSG{
Expand All @@ -311,7 +312,7 @@ func ToMessageContent(e []message.IMessageElement, source message.Source) (r []g
"type": "at",
"data": global.MSG{
"subType": "user",
"target": o.Target,
"target": o.TargetUin,
"display": o.Display,
},
}
Expand Down Expand Up @@ -486,27 +487,27 @@ func (bot *CQBot) reply(spec *onebot.Spec, elem msg.Element, sourceType message.
}
if org != nil {
re = &message.ReplyElement{
ReplySeq: org.GetAttribute().MessageSeq,
Sender: uint64(org.GetAttribute().SenderUin),
Time: int32(org.GetAttribute().Timestamp),
Elements: bot.ConvertStringMessage(spec, customText, sourceType),
ReplySeq: uint32(org.GetAttribute().MessageSeq),
SenderUin: uint32(org.GetAttribute().SenderUin),
Time: uint32(org.GetAttribute().Timestamp),
Elements: bot.ConvertStringMessage(spec, customText, sourceType),
}
if senderErr != nil {
re.Sender = uint64(sender)
re.SenderUin = uint32(sender)
}
if timeErr != nil {
re.Time = int32(msgTime)
re.Time = uint32(msgTime)
}
if seqErr != nil {
re.ReplySeq = int32(messageSeq)
re.ReplySeq = uint32(messageSeq)
}
break
}
re = &message.ReplyElement{
ReplySeq: int32(messageSeq),
Sender: uint64(sender),
Time: int32(msgTime),
Elements: bot.ConvertStringMessage(spec, customText, sourceType),
ReplySeq: uint32(messageSeq),
SenderUin: uint32(sender),
Time: uint32(msgTime),
Elements: bot.ConvertStringMessage(spec, customText, sourceType),
}

case err == nil:
Expand All @@ -515,10 +516,10 @@ func (bot *CQBot) reply(spec *onebot.Spec, elem msg.Element, sourceType message.
return nil, err
}
re = &message.ReplyElement{
ReplySeq: org.GetAttribute().MessageSeq,
Sender: uint64(org.GetAttribute().SenderUin),
Time: int32(org.GetAttribute().Timestamp),
Elements: bot.ConvertContentMessage(org.GetContent(), sourceType, true),
ReplySeq: uint32(org.GetAttribute().MessageSeq),
SenderUin: uint32(org.GetAttribute().SenderUin),
Time: uint32(org.GetAttribute().Timestamp),
Elements: bot.ConvertContentMessage(org.GetContent(), sourceType, true),
}

default:
Expand Down
80 changes: 40 additions & 40 deletions coolq/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,54 +237,54 @@ func (bot *CQBot) groupRecallEvent(c *client.QQClient, e *event2.GroupRecall) {
}

//TODO 群通知
/*
func (bot *CQBot) groupNotifyEvent(c *client.QQClient, e client.INotifyEvent) {
group := c.FindGroup(e.From())

func (bot *CQBot) groupNotifyEvent(c *client.QQClient, e event2.INotifyEvent) {
group := c.GetCachedGroupInfo(e.From())
switch notify := e.(type) {
case *client.GroupPokeNotifyEvent:
sender := group.FindMember(notify.Sender)
receiver := group.FindMember(notify.Receiver)
case *event2.GroupPokeEvent:
sender := c.GetCachedMemberInfo(notify.Sender, e.From())
receiver := c.GetCachedMemberInfo(notify.Receiver, e.From())
log.Infof("群 %v 内 %v 戳了戳 %v", formatGroupName(group), formatMemberName(sender), formatMemberName(receiver))
bot.dispatchEvent("notice/notify/poke", global.MSG{
"group_id": group.Code,
"group_id": group.GroupUin,
"user_id": notify.Sender,
"sender_id": notify.Sender,
"target_id": notify.Receiver,
})
case *client.GroupRedBagLuckyKingNotifyEvent:
sender := group.FindMember(notify.Sender)
luckyKing := group.FindMember(notify.LuckyKing)
log.Infof("群 %v 内 %v 的红包被抢完, %v 是运气王", formatGroupName(group), formatMemberName(sender), formatMemberName(luckyKing))
bot.dispatchEvent("notice/notify/lucky_king", global.MSG{
"group_id": group.Code,
"user_id": notify.Sender,
"sender_id": notify.Sender,
"target_id": notify.LuckyKing,
})
case *client.MemberHonorChangedNotifyEvent:
log.Info(notify.Content())
bot.dispatchEvent("notice/notify/honor", global.MSG{
"group_id": group.Code,
"user_id": notify.Uin,
"honor_type": func() string {
switch notify.Honor {
case client.Talkative:
return "talkative"
case client.Performer:
return "performer"
case client.Emotion:
return "emotion"
case client.Legend:
return "legend"
case client.StrongNewbie:
return "strong_newbie"
default:
return "ERROR"
}
}(),
})
//case *client.GroupRedBagLuckyKingNotifyEvent:
// sender := group.FindMember(notify.Sender)
// luckyKing := group.FindMember(notify.LuckyKing)
// log.Infof("群 %v 内 %v 的红包被抢完, %v 是运气王", formatGroupName(group), formatMemberName(sender), formatMemberName(luckyKing))
// bot.dispatchEvent("notice/notify/lucky_king", global.MSG{
// "group_id": group.Code,
// "user_id": notify.Sender,
// "sender_id": notify.Sender,
// "target_id": notify.LuckyKing,
// })
//case *client.MemberHonorChangedNotifyEvent:
// log.Info(notify.Content())
// bot.dispatchEvent("notice/notify/honor", global.MSG{
// "group_id": group.Code,
// "user_id": notify.Uin,
// "honor_type": func() string {
// switch notify.Honor {
// case client.Talkative:
// return "talkative"
// case client.Performer:
// return "performer"
// case client.Emotion:
// return "emotion"
// case client.Legend:
// return "legend"
// case client.StrongNewbie:
// return "strong_newbie"
// default:
// return "ERROR"
// }
// }(),
// })
}
}*/
}

// TODO 好友通知
/*
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/FloatTech/sqlite v1.6.3
github.com/LagrangeDev/LagrangeGo v0.0.0-20240628105048-21b1ba6e0035
github.com/LagrangeDev/LagrangeGo v0.0.0-20240715122710-e3a1cc4eeeb3
github.com/Microsoft/go-winio v0.6.2-0.20230724192519-b29bbd58a65a
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
Expand All @@ -17,11 +17,11 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.1
github.com/syndtr/goleveldb v1.0.0
github.com/tidwall/gjson v1.15.0
github.com/tidwall/gjson v1.17.1
github.com/wdvxdr1123/go-silk v0.0.0-20210316130616-d47b553def60
go.mongodb.org/mongo-driver v1.12.0
golang.org/x/crypto v0.23.0
golang.org/x/image v0.16.0
golang.org/x/image v0.18.0
golang.org/x/sys v0.20.0
golang.org/x/term v0.20.0
golang.org/x/time v0.3.0
Expand Down Expand Up @@ -54,11 +54,11 @@ require (
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.11.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/protobuf v1.33.0 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.40.0 // indirect
Expand Down
Loading

0 comments on commit 49bf9cf

Please sign in to comment.