Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/gocq/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/LagrangeDev/LagrangeGo/client"
"github.com/LagrangeDev/LagrangeGo/client/auth"
"github.com/LagrangeDev/LagrangeGo/client/packets/wtlogin/qrcodestate"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/io"
"github.com/mattn/go-colorable"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -81,7 +81,7 @@ func printQRCode(imgData []byte) {
}

bound := img.Bounds().Max.X
buf := make([]byte, 0, (bound+1)*(bound/2+utils.Ternary(bound%2 == 0, 0, 1)))
buf := make([]byte, 0, (bound+1)*(bound/2+io.Ternary(bound%2 == 0, 0, 1)))

padding := 0
lastColor := img.At(padding, padding).(color.Gray).Y
Expand All @@ -94,7 +94,7 @@ func printQRCode(imgData []byte) {
for y := padding; y < bound-padding; y += 2 {
for x := padding; x < bound-padding; x++ {
isUpWhite := img.At(x, y).(color.Gray).Y == 255
isDownWhite := utils.Ternary(y < bound-padding, img.At(x, y+1).(color.Gray).Y == 255, false)
isDownWhite := io.Ternary(y < bound-padding, img.At(x, y+1).(color.Gray).Y == 255, false)

switch {
case !isUpWhite && !isDownWhite:
Expand Down Expand Up @@ -277,7 +277,7 @@ func getTicket(u string) (string, string) {
log.Warnf("2. 手动抓取提交")
log.Warn("请输入(1 - 2):")
text := readLine()
id := utils.NewUUID()
id := io.NewUUID()
auto := !strings.Contains(text, "2")
// TODO 自动获取验证码
if auto {
Expand Down
12 changes: 6 additions & 6 deletions cmd/gocq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"github.com/LagrangeDev/LagrangeGo/client"
"github.com/LagrangeDev/LagrangeGo/client/auth"
"github.com/LagrangeDev/LagrangeGo/client/packets/pb/action"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/crypto"
"github.com/LagrangeDev/LagrangeGo/utils/io"
para "github.com/fumiama/go-hide-param"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -377,7 +377,7 @@ func LoginInteract() {
if uint(base.Account.Status) >= 3000 {
base.Account.Status = 10
}
_ = cli.SetOnlineStatus(utils.Ternary(base.Account.Status >= 1000, action.SetStatus{
_ = cli.SetOnlineStatus(io.Ternary(base.Account.Status >= 1000, action.SetStatus{
Status: 10,
ExtStatus: uint32(base.Account.Status),
}, action.SetStatus{Status: uint32(base.Account.Status)}))
Expand Down Expand Up @@ -488,19 +488,19 @@ type protocolLogger struct{}

const fromProtocol = "Protocol -> "

func (p protocolLogger) Info(format string, arg ...any) {
func (p protocolLogger) Infof(format string, arg ...any) {
log.Infof(fromProtocol+format, arg...)
}

func (p protocolLogger) Warning(format string, arg ...any) {
func (p protocolLogger) Warningf(format string, arg ...any) {
log.Warnf(fromProtocol+format, arg...)
}

func (p protocolLogger) Debug(format string, arg ...any) {
func (p protocolLogger) Debugf(format string, arg ...any) {
log.Debugf(fromProtocol+format, arg...)
}

func (p protocolLogger) Error(format string, arg ...any) {
func (p protocolLogger) Errorf(format string, arg ...any) {
log.Errorf(fromProtocol+format, arg...)
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/gocq/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/LagrangeDev/LagrangeGo/client/auth"
"github.com/LagrangeDev/LagrangeGo/client/sign"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/io"
log "github.com/sirupsen/logrus"

"github.com/Mrs4s/go-cqhttp/internal/base"
Expand Down Expand Up @@ -113,7 +113,7 @@ func (c *signer) AddRequestHeader(header map[string]string) {
func (c *signer) AddSignServer(signServers ...string) {
c.lock.Lock()
defer c.lock.Unlock()
c.instances = append(c.instances, utils.Map[string, *remote](signServers, func(s string) *remote {
c.instances = append(c.instances, io.Map(signServers, func(s string) *remote {
return &remote{server: s}
})...)
}
Expand All @@ -122,7 +122,7 @@ func (c *signer) AddSignServer(signServers ...string) {
func (c *signer) GetSignServer() []string {
c.lock.RLock()
defer c.lock.RUnlock()
return utils.Map(c.instances, func(sign *remote) string {
return io.Map(c.instances, func(sign *remote) string {
return sign.server
})
}
Expand Down
19 changes: 10 additions & 9 deletions coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/LagrangeDev/LagrangeGo/client/entity"
"github.com/LagrangeDev/LagrangeGo/client/sign"
"github.com/LagrangeDev/LagrangeGo/message"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/binary"
"github.com/LagrangeDev/LagrangeGo/utils/io"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"

Expand Down Expand Up @@ -272,7 +272,7 @@ func (bot *CQBot) CQUploadGroupFile(groupID int64, file, name, folder string) gl
log.Warnf("上传群文件 %v 失败: 文件不存在", file)
return Failed(100, "FILE_NOT_FOUND", "文件不存在")
}
if err := bot.Client.SendGroupFile(uint32(groupID), file, name, utils.Ternary(folder == "", "/", folder)); err != nil {
if err := bot.Client.SendGroupFile(uint32(groupID), file, name, io.Ternary(folder == "", "/", folder)); err != nil {
log.Warnf("上传群 %v 文件 %v 失败: %v", groupID, file, err)
return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error())
}
Expand Down Expand Up @@ -837,9 +837,9 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理")
}
if approve {
_ = bot.Client.SetGroupRequest(req.IsFiltered, true, req.Sequence, uint32(req.EventType), req.GroupUin, "")
_ = bot.Client.SetGroupRequest(req.IsFiltered, entity.GroupRequestOperateAllow, req.Sequence, uint32(req.EventType), req.GroupUin, "")
} else {
_ = bot.Client.SetGroupRequest(req.IsFiltered, false, req.Sequence, uint32(req.EventType), req.GroupUin, reason)
_ = bot.Client.SetGroupRequest(req.IsFiltered, entity.GroupRequestOperateDeny, req.Sequence, uint32(req.EventType), req.GroupUin, reason)
}
return OK(nil)
}
Expand All @@ -852,9 +852,9 @@ func (bot *CQBot) CQProcessGroupRequest(flag, subType, reason string, approve bo
return Failed(100, "FLAG_HAS_BEEN_CHECKED", "消息已被处理")
}
if approve {
_ = bot.Client.SetGroupRequest(req.IsFiltered, true, req.Sequence, uint32(req.EventType), req.GroupUin, "")
_ = bot.Client.SetGroupRequest(req.IsFiltered, entity.GroupRequestOperateAllow, req.Sequence, uint32(req.EventType), req.GroupUin, "")
} else {
_ = bot.Client.SetGroupRequest(req.IsFiltered, false, req.Sequence, uint32(req.EventType), req.GroupUin, reason)
_ = bot.Client.SetGroupRequest(req.IsFiltered, entity.GroupRequestOperateDeny, req.Sequence, uint32(req.EventType), req.GroupUin, reason)
}
return OK(nil)
}
Expand Down Expand Up @@ -1057,7 +1057,7 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) global
},
})

err := json.Unmarshal(utils.S2B(reply.Raw), &replySegments)
err := json.Unmarshal(io.S2B(reply.Raw), &replySegments)
if err != nil {
log.WithError(err).Warnf("处理 at_sender 过程中发生错误")
return Failed(-1, "处理 at_sender 过程中发生错误", err.Error())
Expand All @@ -1071,7 +1071,7 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) global
return Failed(-1, "处理 at_sender 过程中发生错误", err.Error())
}

reply = gjson.Parse(utils.B2S(modified))
reply = gjson.Parse(io.B2S(modified))
} else if at && reply.Type == gjson.String {
reply = gjson.Parse(fmt.Sprintf(
"\"[CQ:at,qq=%d]%s\"",
Expand Down Expand Up @@ -1141,11 +1141,12 @@ func (bot *CQBot) CQGetImage(file string) global.MSG {

if err == nil {
r := binary.NewReader(b)
r.ReadBytes(16)
r.SkipBytes(16)
msg := global.MSG{
"size": r.ReadI32(),
"filename": r.ReadStringWithLength("u32", true),
"url": r.ReadStringWithLength("u32", true),
"fileuuid": r.ReadStringWithLength("u32", true),
}
local := path.Join(global.CachePath, file+path.Ext(msg["filename"].(string)))
if !global.FileExists(local) {
Expand Down
21 changes: 9 additions & 12 deletions coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
event2 "github.com/LagrangeDev/LagrangeGo/client/event"
"github.com/LagrangeDev/LagrangeGo/client/sign"
"github.com/LagrangeDev/LagrangeGo/message"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/binary"
"github.com/LagrangeDev/LagrangeGo/utils/io"
"github.com/RomiChan/syncx"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -67,7 +67,7 @@ func (e *Event) JSONBytes() []byte {
// by lazy marshalling.
func (e *Event) JSONString() string {
e.once.Do(e.marshal)
return utils.B2S(e.buffer.Bytes())
return io.B2S(e.buffer.Bytes())
}

// NewQQBot 初始化一个QQBot实例
Expand Down Expand Up @@ -426,12 +426,12 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage, source message.Sou
Timestamp: int64(m.Time),
},
GroupCode: int64(m.GroupUin),
AnonymousID: func() string {
if m.Sender.IsAnonymous() {
return m.Sender.AnonymousInfo.AnonymousID
}
return ""
}(),
// AnonymousID: func() string {
// if m.Sender.IsAnonymous() {
// return m.Sender.AnonymousInfo.AnonymousID
// }
// return ""
// }(),
Content: ToMessageContent(m.Elements, source),
}
if replyElem != nil {
Expand Down Expand Up @@ -557,8 +557,5 @@ func formatMemberName(mem *entity.GroupMember) string {

// encodeMessageID 临时先这样, 暂时用不上
func encodeMessageID(target int64, seq int32) string {
return hex.EncodeToString(binary.NewWriterF(func(w *binary.Builder) {
w.WriteU64(uint64(target))
w.WriteU32(uint32(seq))
}))
return hex.EncodeToString(binary.NewBuilder().WriteU64(uint64(target)).WriteU32(uint32(seq)).ToBytes())
}
68 changes: 34 additions & 34 deletions coolq/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,44 +73,44 @@ func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) *event {
},
"user_id": m.Sender.Uin,
}
if m.Sender.IsAnonymous() {
gm["anonymous"] = global.MSG{
"flag": m.Sender.AnonymousInfo.AnonymousID + "|" + m.Sender.AnonymousInfo.AnonymousNick,
"id": m.Sender.Uin,
"name": m.Sender.AnonymousInfo.AnonymousNick,
// if m.Sender.IsAnonymous() {
// gm["anonymous"] = global.MSG{
// "flag": m.Sender.AnonymousInfo.AnonymousID + "|" + m.Sender.AnonymousInfo.AnonymousNick,
// "id": m.Sender.Uin,
// "name": m.Sender.AnonymousInfo.AnonymousNick,
// }
// gm["sender"].(global.MSG)["nickname"] = "匿名消息"
// typ = "message/group/anonymous"
// } else {
mem := bot.Client.GetCachedMemberInfo(m.Sender.Uin, m.GroupUin)
if mem == nil {
log.Warnf("获取 %v 成员信息失败,尝试刷新成员列表", m.Sender.Uin)
err := bot.Client.RefreshGroupMembersCache(m.GroupUin)
if err != nil {
log.Warnf("刷新群 %v 成员列表失败: %v", m.GroupUin, err)
return nil
}
gm["sender"].(global.MSG)["nickname"] = "匿名消息"
typ = "message/group/anonymous"
} else {
mem := bot.Client.GetCachedMemberInfo(m.Sender.Uin, m.GroupUin)
mem = bot.Client.GetCachedMemberInfo(m.Sender.Uin, m.GroupUin)
if mem == nil {
log.Warnf("获取 %v 成员信息失败,尝试刷新成员列表", m.Sender.Uin)
err := bot.Client.RefreshGroupMembersCache(m.GroupUin)
if err != nil {
log.Warnf("刷新群 %v 成员列表失败: %v", m.GroupUin, err)
return nil
}
mem = bot.Client.GetCachedMemberInfo(m.Sender.Uin, m.GroupUin)
if mem == nil {
return nil
}
return nil
}
ms := gm["sender"].(global.MSG)
role := "member"
switch mem.Permission { // nolint:exhaustive
case entity.Owner:
role = "owner"
case entity.Admin:
role = "admin"
case entity.Member:
role = "member"
}
ms["role"] = role
ms["nickname"] = m.Sender.Nickname
ms["card"] = m.Sender.CardName
// TODO 获取专属头衔
ms["title"] = ""
}
ms := gm["sender"].(global.MSG)
role := "member"
switch mem.Permission { // nolint:exhaustive
case entity.Owner:
role = "owner"
case entity.Admin:
role = "admin"
case entity.Member:
role = "member"
}
ms["role"] = role
ms["nickname"] = m.Sender.Nickname
ms["card"] = m.Sender.CardName
// TODO 获取专属头衔
ms["title"] = ""
// }
ev := bot.event(typ, gm)
ev.Time = int64(m.Time)
return ev
Expand Down
4 changes: 2 additions & 2 deletions coolq/cqcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (

"github.com/LagrangeDev/LagrangeGo/client/packets/pb/service/oidb"
"github.com/LagrangeDev/LagrangeGo/message"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/LagrangeDev/LagrangeGo/utils/binary"
"github.com/LagrangeDev/LagrangeGo/utils/crypto"
lgrio "github.com/LagrangeDev/LagrangeGo/utils/io"
b14 "github.com/fumiama/go-base16384"
"github.com/segmentio/asm/base64"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -940,7 +940,7 @@ func (bot *CQBot) makeImageOrVideoElem(elem msg.Element, video bool, sourceType
return &msg.LocalImage{Stream: bytes.NewReader(b), URL: f}, nil
}
if !video && strings.HasPrefix(f, "base16384") {
b, err := b14.UTF82UTF16BE(utils.S2B(strings.TrimPrefix(f, "base16384://")))
b, err := b14.UTF82UTF16BE(lgrio.S2B(strings.TrimPrefix(f, "base16384://")))
if err != nil {
return nil, err
}
Expand Down
29 changes: 16 additions & 13 deletions coolq/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,14 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, source message.Source)
// i.URL = u
// }
//}
data := binary.NewWriterF(func(w *binary.Builder) {
_, _ = w.Write(i.Md5)
w.WritePacketString(i.FileUUID, "u32", true)
w.WritePacketString(i.ImageID, "u32", true)
})
// 根据 https://github.com/Mrs4s/go-cqhttp/blob/a5923f179b360331786a6509eb33481e775a7bd1/coolq/event.go#L663-L669 修复
data := binary.NewBuilder().
WriteBytes(i.Md5).
WriteU32(i.Size).
WritePacketString(i.ImageID, "u32", true).
WritePacketString(i.URL, "u32", true).
WritePacketString(i.FileUUID, "u32", true).
ToBytes()
cache.Image.Insert(i.Md5, data)

case *message.VoiceElement:
Expand All @@ -552,14 +555,14 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, source message.Source)
}
}
case *message.ShortVideoElement:
data := binary.NewWriterF(func(w *binary.Builder) {
w.WriteBool(source.SourceType == message.SourceGroup)
w.WriteBytes(i.Md5)
w.WriteBytes(i.Sha1)
w.WritePacketString(i.Name, "u32", true)
w.WritePacketString(i.UUID, "u32", true)
w.WriteU32(uint32(source.PrimaryID))
})
data := binary.NewBuilder().
WriteBool(source.SourceType == message.SourceGroup).
WriteBytes(i.Md5).
WriteBytes(i.Sha1).
WritePacketString(i.Name, "u32", true).
WritePacketString(i.UUID, "u32", true).
WriteU32(uint32(source.PrimaryID)).
ToBytes()
filename := hex.EncodeToString(i.Md5) + ".video"
cache.Video.Insert(i.Md5, data)
if source.SourceType == message.SourceGroup {
Expand Down
Loading