This commit is contained in:
cdle
2023-06-03 17:30:48 +08:00
parent 62238bad8e
commit bdc884fbe4
7 changed files with 163 additions and 50 deletions
+41 -15
View File
@@ -468,6 +468,8 @@ type PUSH string
func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) {
var push = false
var content = ""
var platform = sender.f.botplt
var bot_id = sender.f.botid
for _, item := range msgs {
switch item := item.(type) {
case PUSH:
@@ -491,25 +493,49 @@ func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) {
"content": content,
"user_id": sender.GetUserID(),
"chat_id": sender.GetChatID(),
// "bot_id": sender.GetBotID(),
"bot_id": bot_id,
// "uuid": utils.GenUUID(),
}
if sender.f.reply == nil {
c := MsgChan{
Msg: msg,
Chan: make(chan string),
matched := false
for _, function := range Functions {
if function.Reply != nil && function.Reply.Platform == platform && (len(function.Reply.BotsID) == 0 || Contains(function.Reply.BotsID, platform)) {
message_id := ""
function.Handle(&Faker{
Type: "*",
}, func(vm *goja.Runtime) {
obj := vm.NewObject()
for k, v := range msg {
obj.Set(k, v)
}
proxy := vm.NewProxy(obj, &goja.ProxyTrapConfig{
Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) {
message_id = fmt.Sprint(value.Export())
return true
},
})
vm.Set("msg", proxy)
vm.Set("message", proxy)
})
return message_id, nil
}
}
if sender.f.destroid {
return "", errors.New("adapter destroid")
}
sender.f.msgChan <- c
select {
case id := <-c.Chan:
return id, nil
case <-time.After(time.Second * 5):
close(c.Chan)
return "", errors.New("get message_id timeout")
if !matched {
c := MsgChan{
Msg: msg,
Chan: make(chan string),
}
if sender.f.destroid {
return "", errors.New("adapter destroid")
}
sender.f.msgChan <- c
select {
case id := <-c.Chan:
return id, nil
case <-time.After(time.Second * 5):
close(c.Chan)
return "", errors.New("get message_id timeout")
}
}
} else {
//todo 阻塞延迟异常
+4 -3
View File
@@ -11,6 +11,7 @@ import (
"github.com/cdle/sillyplus/core/common"
"github.com/cdle/sillyplus/core/storage"
"github.com/cdle/sillyplus/utils"
"github.com/dop251/goja"
"github.com/gin-gonic/gin"
"github.com/goccy/go-json"
)
@@ -35,7 +36,7 @@ func init() {
{
Rules: []string{`raw [\s\S]+`},
Hidden: true,
Handle: func(s common.Sender) interface{} {
Handle: func(s common.Sender, _ func(vm *goja.Runtime)) interface{} {
var bot_id = s.GetBotID()
var platform = s.GetImType()
var chat_id = s.GetChatID()
@@ -146,7 +147,7 @@ func init() {
for i := range fs {
for j := range from.Scripts {
if fs[i].UUID == from.Scripts[j] && !Contains(scripts, fs[i].UUID) {
fs[i].Handle(s)
fs[i].Handle(s, nil)
content = s.GetContent()
if content == "" {
return nil
@@ -162,7 +163,7 @@ func init() {
for j := range outs[i].Scripts {
for k := range fs {
if fs[k].UUID == outs[i].Scripts[j] && !Contains(scripts, fs[k].UUID) { //
fs[k].Handle(s)
fs[k].Handle(s, nil)
content = s.GetContent()
// fmt.Println(content)
if content == "" {
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"github.com/cdle/sillyplus/core/common"
"github.com/cdle/sillyplus/utils"
"github.com/dop251/goja"
)
var IsCdle = false
@@ -20,7 +21,7 @@ func init() {
{
Admin: true,
Rules: []string{"identify sublink [地址] [组织]"},
Handle: func(s common.Sender) interface{} {
Handle: func(s common.Sender, _ func(vm *goja.Runtime)) interface{} {
address := s.Get(0)
organization := s.Get(1)
// machine_id := s.Get(1)
+40 -26
View File
@@ -1,36 +1,40 @@
package common
import "github.com/dop251/goja"
type Function struct {
Rules []string `json:"-"`
Params [][]string `json:"-"`
ImType *Filter `json:"-"`
UserId *Filter `json:"-"`
GroupId *Filter `json:"-"`
FindAll bool `json:"-"`
Admin bool `json:"-"`
Handle func(s Sender) interface{} `json:"-"`
Cron string `json:"cron"`
Priority int `json:"-"`
Disable bool `json:"-"`
Hidden bool `json:"-"`
CronId int `json:"-"`
Origin string `json:"-"`
UUID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Public bool `json:"public"`
Icon string `json:"icon"`
Version string `json:"version"`
Author string `json:"author"`
Status int `json:"status"` //0未安装 1可更新 2已安装
Address string `json:"-"`
CreateAt string `json:"create_at"`
Module bool `json:"module"`
Rules []string `json:"-"`
Params [][]string `json:"-"`
ImType *Filter `json:"-"`
UserId *Filter `json:"-"`
GroupId *Filter `json:"-"`
FindAll bool `json:"-"`
Admin bool `json:"-"`
Handle func(Sender, func(vm *goja.Runtime)) interface{} `json:"-"`
Cron string `json:"cron"`
Priority int `json:"-"`
Disable bool `json:"-"`
Hidden bool `json:"-"`
CronId int `json:"-"`
Origin string `json:"-"`
UUID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Public bool `json:"public"`
Icon string `json:"icon"`
Version string `json:"version"`
Author string `json:"author"`
Status int `json:"status"` //0未安装 1可更新 2已安装
Address string `json:"-"`
CreateAt string `json:"create_at"`
Module bool `json:"module"`
// Web bool `json:"web"`
Encrypt bool `json:"encrypt"`
OnStart bool `json:"on_start"`
PluginPublisher
Running bool `json:"-"`
Running bool `json:"-"`
Http *Http `json:"-"`
Reply *Reply `json:"-"`
}
type Filter struct {
BlackMode bool
@@ -42,3 +46,13 @@ type PluginPublisher struct {
Organization string `json:"organization"`
Identified bool `json:"identified"`
}
type Http struct { //GET /abc
Path string
Method string
}
type Reply struct { //wx 123 1234
Platform string
BotsID []string
}
+3 -3
View File
@@ -371,7 +371,7 @@ func AddCommand(cmds []*common.Function) {
console.Log("初始化%v服务", f.Title)
f.Handle(&Faker{
Type: "*",
})
}, nil)
}(cmds[j])
}
fmtRule(cmds[j])
@@ -385,7 +385,7 @@ func AddCommand(cmds []*common.Function) {
cmds[j].Handle(&Faker{
Admin: true,
Type: "cron",
})
}, nil)
})
if err == nil {
cmds[j].CronId = int(cronId)
@@ -624,7 +624,7 @@ func HandleMessage(sender common.Sender) {
if function.Admin && !a {
return
}
rt := function.Handle(sender)
rt := function.Handle(sender, nil)
if rt != nil {
sender.Reply(rt)
}
+42 -2
View File
@@ -256,6 +256,8 @@ func initPlugin(data string, uuid string) (*common.Function, error) {
var encrypt bool
var onStart bool
var origin = "自定义"
var http *common.Http
var message *common.Reply
ress := regexp.MustCompile(
`\*\s?@([\d\w+-]+)\s+([^\n]+?)\n`,
).FindAllStringSubmatch(data, -1)
@@ -377,6 +379,34 @@ func initPlugin(data string, uuid string) (*common.Function, error) {
version = strings.TrimSpace(res[2])
case "author":
author = strings.TrimSpace(res[2])
case "http":
ss := regexp.MustCompile(`[\S]+`).FindAllString(strings.TrimSpace(res[2]), -1)
if len(ss) == 2 {
http = &common.Http{
Path: ss[1],
Method: strings.ToUpper(ss[0]),
}
} else {
console.Warn("http param is not 2")
}
case "message":
ss := regexp.MustCompile(`[\S]+`).FindAllString(strings.TrimSpace(res[2]), -1)
if len(ss) > 1 {
if len(ss) == 2 && ss[1] == "*" {
message = &common.Reply{
Platform: ss[0],
BotsID: []string{},
}
} else {
message = &common.Reply{
Platform: ss[0],
BotsID: ss[1:],
}
}
} else {
console.Warn("message param is 0")
}
case "create_at":
create_at = strings.TrimSpace(res[2])
case "origin":
@@ -415,7 +445,7 @@ func initPlugin(data string, uuid string) (*common.Function, error) {
}
var running func() bool
f := &common.Function{
Handle: func(s common.Sender) interface{} {
Handle: func(s common.Sender, set func(vm *goja.Runtime)) interface{} {
defer func() {
err := recover()
if err != nil {
@@ -438,6 +468,12 @@ func initPlugin(data string, uuid string) (*common.Function, error) {
Persistent: "persistent",
UUID: uuid,
}
vm.Set("msg", goja.Undefined())
vm.Set("message", goja.Undefined())
vm.Set("res", goja.Undefined())
vm.Set("req", goja.Undefined())
vm.Set("response", goja.Undefined())
vm.Set("request", goja.Undefined())
vm.Set("sender", ss)
vm.Set("s", ss)
vm.Set("InitAdapter", func(plt, botid string) *Factory {
@@ -473,7 +509,9 @@ func initPlugin(data string, uuid string) (*common.Function, error) {
vm.Set("uuid", func() string {
return uuid
})
if set != nil {
set(vm)
}
_, err := vm.RunProgram(prg)
if err != nil {
console.Error(strings.ReplaceAll(strings.ReplaceAll(err.Error(), "node_modules/", ""), "github.com/dop251/goja_nodejs/require", ""))
@@ -502,6 +540,8 @@ func initPlugin(data string, uuid string) (*common.Function, error) {
OnStart: onStart,
Origin: origin,
Running: onStart,
Reply: message,
Http: http,
}
running = func() bool {
return f.Running
+31
View File
@@ -13,6 +13,7 @@ import (
"github.com/cdle/sillyplus/core/logs"
"github.com/cdle/sillyplus/core/storage"
"github.com/cdle/sillyplus/utils"
"github.com/dop251/goja"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
)
@@ -88,6 +89,36 @@ func init() {
c: c,
}
for _, function := range Functions {
if function.Http != nil {
path := function.Http.Path
method := function.Http.Method
if c.Request.URL.Path == path && c.Request.Method == method {
req.handled = true
function.Handle(&Faker{
Type: "*",
}, func(vm *goja.Runtime) {
vm.Set("res", res)
vm.Set("req", req)
vm.Set("response", res)
vm.Set("request", req)
})
}
}
}
if req.handled {
if res.isRedirect {
return
}
if res.isJson {
c.Header("Content-Type", "application/json")
}
c.String(res.status, res.content)
return
}
httpListensAny.Range(func(_, value any) bool {
web := value.(*HttpListen)
if web.Closed {