This commit is contained in:
1-6
2023-07-25 09:41:56 +08:00
parent 172c244656
commit e5163b233a
23 changed files with 4729 additions and 312 deletions
+133 -53
View File
@@ -4,6 +4,9 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"sync"
"time"
@@ -14,23 +17,30 @@ import (
var senders sync.Map
func init() {
//垃圾回收
go func() {
for {
time.Sleep(time.Minute)
senders.Range(func(key, value any) bool {
s := value.(common.Sender)
if s.GetTime().Add(time.Minute * 20).Before(time.Now()) {
senders.Delete(s.GetID())
}
return true
})
}
}()
}
// func init() {
// //垃圾回收
// go func() {
// for {
// time.Sleep(time.Minute)
// senders.Range(func(key, value any) bool {
// s := value.(common.Sender)
// if s.GetTime().Add(time.Minute * 20).Before(time.Now()) {
// senders.Delete(s.GetID())
// }
// return true
// })
// }
// }()
// }
func GetSender(uuid string) (common.Sender, error) {
if uuid == "" {
return &CustomSender{
f: &Factory{
botid: "*",
},
}, nil
}
v, ok := senders.Load(uuid)
if !ok {
return nil, errors.New("not found sender")
@@ -121,47 +131,96 @@ func (sg *SillyGirlService) SenderContinue(ctx context.Context, req *srpc.Sender
}
// todo
func (sg *SillyGirlService) SenderListen(ctx context.Context, req *srpc.SenderListenRequest) (*srpc.Default, error) {
s, err := GetSender(req.Uuid)
if err != nil {
return nil, err
}
options := []interface{}{}
var carry = &Carry{
ListenPrivate: req.ListenPrivate,
ListenGroup: req.ListenGroup,
RequireAdmin: req.RequireAdmin,
AllowPlatforms: req.AllowPlatforms,
ProhibitPlatforms: req.ProhibitPlatforms,
AllowGroups: req.AllowGroups,
ProhibitGroups: req.ProhibitGroups,
AllowUsers: req.AllowUsers,
ProhibitUsers: req.ProhibitUsers,
UserID: s.GetUserID(),
ChatID: s.GetChatID(),
}
if req.Timeout != 0 {
options = append(options, time.Duration(req.Timeout)*time.Millisecond)
}
if len(req.Rules) != 0 {
for _, rule := range req.Rules {
_rs := formatRule(rule)
if len(_rs) != 0 {
carry.Function.Rules = append(carry.Function.Rules, _rs...)
} else {
carry.Function.Rules = append(carry.Function.Rules, rule)
func (sg *SillyGirlService) SenderListen(stream srpc.SillyGirlService_SenderListenServer) error {
var carry *Carry
var echos sync.Map
var persistent bool
// defer fmt.Println("已关闭,", "===")
for {
req, err := stream.Recv()
// fmt.Println("carry", carry, err)
if err == io.EOF {
break // 如果流已经关闭,则退出循环
}
if err != nil {
return err
}
if carry != nil {
echo := req.GetUuid()
value := req.GetValue()
// fmt.Println("echo", echo, "value", value)
v, ok := echos.Load(echo)
if ok {
select {
case v.(chan string) <- value:
case <-time.After(time.Millisecond):
}
}
// if !persistent {
// return nil
// }
continue
}
s, err := GetSender(req.Uuid)
if err != nil {
return err
}
options := []interface{}{}
carry = &Carry{
ListenPrivate: req.ListenPrivate,
ListenGroup: req.ListenGroup,
RequireAdmin: req.RequireAdmin,
AllowPlatforms: req.AllowPlatforms,
ProhibitPlatforms: req.ProhibitPlatforms,
AllowGroups: req.AllowGroups,
ProhibitGroups: req.ProhibitGroups,
AllowUsers: req.AllowUsers,
ProhibitUsers: req.ProhibitUsers,
UserID: s.GetUserID(),
ChatID: s.GetChatID(),
UUID: req.PluginId,
}
if req.Timeout != 0 {
options = append(options, time.Duration(req.Timeout)*time.Millisecond)
}
if len(req.Rules) != 0 {
for _, rule := range req.Rules {
_rs := formatRule(rule)
if len(_rs) != 0 {
carry.Function.Rules = append(carry.Function.Rules, _rs...)
} else {
carry.Function.Rules = append(carry.Function.Rules, rule)
}
}
}
options = append(options, carry)
if req.Persistent {
persistent = req.Persistent
options = append(options, "persistent")
}
go s.Await(s, func(s common.Sender) interface{} {
id := s.SetID()
defer senders.Delete(id)
echo := utils.GenUUID()
ch := make(chan string)
echos.Store(echo, ch)
defer echos.Delete(echo)
stream.Send(&srpc.SenderListenResponse{Echo: echo, Uuid: id})
value := <-ch
if !persistent {
if strings.HasPrefix(value, "go_again_") {
value = strings.Replace(value, "go_again_", "", 1)
return GoAgain(value)
} else {
stream.Send(&srpc.SenderListenResponse{Echo: "END"})
}
} else {
value = strings.Replace(value, "go_again_", "", 1)
}
return value
}, options...)
}
options = append(options, carry)
id := ""
s.Await(s, func(s common.Sender) interface{} {
id = s.GetID()
return nil
}, options...)
return &srpc.Default{Value: id}, nil
return nil
}
func (sg *SillyGirlService) SenderEvent(ctx context.Context, req *srpc.SenderRequest) (*srpc.Default, error) {
@@ -181,6 +240,21 @@ func (sg *SillyGirlService) SenderReply(ctx context.Context, req *srpc.ReplyRequ
return &srpc.Default{Value: message_id}, err
}
func (sg *SillyGirlService) SenderParam(ctx context.Context, req *srpc.ReplyRequest) (*srpc.Default, error) {
s, err := GetSender(req.Uuid)
if err != nil {
return nil, err
}
value := ""
i := utils.Int(req.Content)
if fmt.Sprint(i) == req.Content {
value = s.Get(i - 1)
} else {
value = s.Get(req.Content)
}
return &srpc.Default{Value: value}, err
}
func (sg *SillyGirlService) SenderAction(ctx context.Context, req *srpc.ReplyRequest) (*srpc.Default, error) {
s, err := GetSender(req.Uuid)
if err != nil {
@@ -194,3 +268,9 @@ func (sg *SillyGirlService) SenderAction(ctx context.Context, req *srpc.ReplyReq
result, err := s.Action(params)
return &srpc.Default{Value: string(utils.JsonMarshal(result))}, err
}
func (sg *SillyGirlService) SenderDestroy(ctx context.Context, req *srpc.ReplyRequest) (*srpc.Empty, error) {
// fmt.Println("删除", req.Uuid)
senders.Delete(req.Uuid)
return &srpc.Empty{}, nil
}