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) { func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) {
var push = false var push = false
var content = "" var content = ""
var platform = sender.f.botplt
var bot_id = sender.f.botid
for _, item := range msgs { for _, item := range msgs {
switch item := item.(type) { switch item := item.(type) {
case PUSH: case PUSH:
@@ -491,25 +493,49 @@ func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) {
"content": content, "content": content,
"user_id": sender.GetUserID(), "user_id": sender.GetUserID(),
"chat_id": sender.GetChatID(), "chat_id": sender.GetChatID(),
// "bot_id": sender.GetBotID(), "bot_id": bot_id,
// "uuid": utils.GenUUID(), // "uuid": utils.GenUUID(),
} }
if sender.f.reply == nil { if sender.f.reply == nil {
c := MsgChan{ matched := false
Msg: msg, for _, function := range Functions {
Chan: make(chan string), 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 { if !matched {
return "", errors.New("adapter destroid") c := MsgChan{
} Msg: msg,
sender.f.msgChan <- c Chan: make(chan string),
select { }
case id := <-c.Chan: if sender.f.destroid {
return id, nil return "", errors.New("adapter destroid")
case <-time.After(time.Second * 5): }
close(c.Chan) sender.f.msgChan <- c
return "", errors.New("get message_id timeout") 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 { } else {
//todo 阻塞延迟异常 //todo 阻塞延迟异常
+4 -3
View File
@@ -11,6 +11,7 @@ import (
"github.com/cdle/sillyplus/core/common" "github.com/cdle/sillyplus/core/common"
"github.com/cdle/sillyplus/core/storage" "github.com/cdle/sillyplus/core/storage"
"github.com/cdle/sillyplus/utils" "github.com/cdle/sillyplus/utils"
"github.com/dop251/goja"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/goccy/go-json" "github.com/goccy/go-json"
) )
@@ -35,7 +36,7 @@ func init() {
{ {
Rules: []string{`raw [\s\S]+`}, Rules: []string{`raw [\s\S]+`},
Hidden: true, Hidden: true,
Handle: func(s common.Sender) interface{} { Handle: func(s common.Sender, _ func(vm *goja.Runtime)) interface{} {
var bot_id = s.GetBotID() var bot_id = s.GetBotID()
var platform = s.GetImType() var platform = s.GetImType()
var chat_id = s.GetChatID() var chat_id = s.GetChatID()
@@ -146,7 +147,7 @@ func init() {
for i := range fs { for i := range fs {
for j := range from.Scripts { for j := range from.Scripts {
if fs[i].UUID == from.Scripts[j] && !Contains(scripts, fs[i].UUID) { if fs[i].UUID == from.Scripts[j] && !Contains(scripts, fs[i].UUID) {
fs[i].Handle(s) fs[i].Handle(s, nil)
content = s.GetContent() content = s.GetContent()
if content == "" { if content == "" {
return nil return nil
@@ -162,7 +163,7 @@ func init() {
for j := range outs[i].Scripts { for j := range outs[i].Scripts {
for k := range fs { for k := range fs {
if fs[k].UUID == outs[i].Scripts[j] && !Contains(scripts, fs[k].UUID) { // 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() content = s.GetContent()
// fmt.Println(content) // fmt.Println(content)
if content == "" { if content == "" {
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"github.com/cdle/sillyplus/core/common" "github.com/cdle/sillyplus/core/common"
"github.com/cdle/sillyplus/utils" "github.com/cdle/sillyplus/utils"
"github.com/dop251/goja"
) )
var IsCdle = false var IsCdle = false
@@ -20,7 +21,7 @@ func init() {
{ {
Admin: true, Admin: true,
Rules: []string{"identify sublink [地址] [组织]"}, Rules: []string{"identify sublink [地址] [组织]"},
Handle: func(s common.Sender) interface{} { Handle: func(s common.Sender, _ func(vm *goja.Runtime)) interface{} {
address := s.Get(0) address := s.Get(0)
organization := s.Get(1) organization := s.Get(1)
// machine_id := s.Get(1) // machine_id := s.Get(1)
+40 -26
View File
@@ -1,36 +1,40 @@
package common package common
import "github.com/dop251/goja"
type Function struct { type Function struct {
Rules []string `json:"-"` Rules []string `json:"-"`
Params [][]string `json:"-"` Params [][]string `json:"-"`
ImType *Filter `json:"-"` ImType *Filter `json:"-"`
UserId *Filter `json:"-"` UserId *Filter `json:"-"`
GroupId *Filter `json:"-"` GroupId *Filter `json:"-"`
FindAll bool `json:"-"` FindAll bool `json:"-"`
Admin bool `json:"-"` Admin bool `json:"-"`
Handle func(s Sender) interface{} `json:"-"` Handle func(Sender, func(vm *goja.Runtime)) interface{} `json:"-"`
Cron string `json:"cron"` Cron string `json:"cron"`
Priority int `json:"-"` Priority int `json:"-"`
Disable bool `json:"-"` Disable bool `json:"-"`
Hidden bool `json:"-"` Hidden bool `json:"-"`
CronId int `json:"-"` CronId int `json:"-"`
Origin string `json:"-"` Origin string `json:"-"`
UUID string `json:"id"` UUID string `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Description string `json:"description"` Description string `json:"description"`
Public bool `json:"public"` Public bool `json:"public"`
Icon string `json:"icon"` Icon string `json:"icon"`
Version string `json:"version"` Version string `json:"version"`
Author string `json:"author"` Author string `json:"author"`
Status int `json:"status"` //0未安装 1可更新 2已安装 Status int `json:"status"` //0未安装 1可更新 2已安装
Address string `json:"-"` Address string `json:"-"`
CreateAt string `json:"create_at"` CreateAt string `json:"create_at"`
Module bool `json:"module"` Module bool `json:"module"`
// Web bool `json:"web"` // Web bool `json:"web"`
Encrypt bool `json:"encrypt"` Encrypt bool `json:"encrypt"`
OnStart bool `json:"on_start"` OnStart bool `json:"on_start"`
PluginPublisher PluginPublisher
Running bool `json:"-"` Running bool `json:"-"`
Http *Http `json:"-"`
Reply *Reply `json:"-"`
} }
type Filter struct { type Filter struct {
BlackMode bool BlackMode bool
@@ -42,3 +46,13 @@ type PluginPublisher struct {
Organization string `json:"organization"` Organization string `json:"organization"`
Identified bool `json:"identified"` 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) console.Log("初始化%v服务", f.Title)
f.Handle(&Faker{ f.Handle(&Faker{
Type: "*", Type: "*",
}) }, nil)
}(cmds[j]) }(cmds[j])
} }
fmtRule(cmds[j]) fmtRule(cmds[j])
@@ -385,7 +385,7 @@ func AddCommand(cmds []*common.Function) {
cmds[j].Handle(&Faker{ cmds[j].Handle(&Faker{
Admin: true, Admin: true,
Type: "cron", Type: "cron",
}) }, nil)
}) })
if err == nil { if err == nil {
cmds[j].CronId = int(cronId) cmds[j].CronId = int(cronId)
@@ -624,7 +624,7 @@ func HandleMessage(sender common.Sender) {
if function.Admin && !a { if function.Admin && !a {
return return
} }
rt := function.Handle(sender) rt := function.Handle(sender, nil)
if rt != nil { if rt != nil {
sender.Reply(rt) sender.Reply(rt)
} }
+42 -2
View File
@@ -256,6 +256,8 @@ func initPlugin(data string, uuid string) (*common.Function, error) {
var encrypt bool var encrypt bool
var onStart bool var onStart bool
var origin = "自定义" var origin = "自定义"
var http *common.Http
var message *common.Reply
ress := regexp.MustCompile( ress := regexp.MustCompile(
`\*\s?@([\d\w+-]+)\s+([^\n]+?)\n`, `\*\s?@([\d\w+-]+)\s+([^\n]+?)\n`,
).FindAllStringSubmatch(data, -1) ).FindAllStringSubmatch(data, -1)
@@ -377,6 +379,34 @@ func initPlugin(data string, uuid string) (*common.Function, error) {
version = strings.TrimSpace(res[2]) version = strings.TrimSpace(res[2])
case "author": case "author":
author = strings.TrimSpace(res[2]) 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": case "create_at":
create_at = strings.TrimSpace(res[2]) create_at = strings.TrimSpace(res[2])
case "origin": case "origin":
@@ -415,7 +445,7 @@ func initPlugin(data string, uuid string) (*common.Function, error) {
} }
var running func() bool var running func() bool
f := &common.Function{ f := &common.Function{
Handle: func(s common.Sender) interface{} { Handle: func(s common.Sender, set func(vm *goja.Runtime)) interface{} {
defer func() { defer func() {
err := recover() err := recover()
if err != nil { if err != nil {
@@ -438,6 +468,12 @@ func initPlugin(data string, uuid string) (*common.Function, error) {
Persistent: "persistent", Persistent: "persistent",
UUID: uuid, 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("sender", ss)
vm.Set("s", ss) vm.Set("s", ss)
vm.Set("InitAdapter", func(plt, botid string) *Factory { 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 { vm.Set("uuid", func() string {
return uuid return uuid
}) })
if set != nil {
set(vm)
}
_, err := vm.RunProgram(prg) _, err := vm.RunProgram(prg)
if err != nil { if err != nil {
console.Error(strings.ReplaceAll(strings.ReplaceAll(err.Error(), "node_modules/", ""), "github.com/dop251/goja_nodejs/require", "")) 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, OnStart: onStart,
Origin: origin, Origin: origin,
Running: onStart, Running: onStart,
Reply: message,
Http: http,
} }
running = func() bool { running = func() bool {
return f.Running return f.Running
+31
View File
@@ -13,6 +13,7 @@ import (
"github.com/cdle/sillyplus/core/logs" "github.com/cdle/sillyplus/core/logs"
"github.com/cdle/sillyplus/core/storage" "github.com/cdle/sillyplus/core/storage"
"github.com/cdle/sillyplus/utils" "github.com/cdle/sillyplus/utils"
"github.com/dop251/goja"
"github.com/gin-contrib/gzip" "github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@@ -88,6 +89,36 @@ func init() {
c: c, 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 { httpListensAny.Range(func(_, value any) bool {
web := value.(*HttpListen) web := value.(*HttpListen)
if web.Closed { if web.Closed {