diff --git a/core/adapter.go b/core/adapter.go index 3b25eed..51de15f 100644 --- a/core/adapter.go +++ b/core/adapter.go @@ -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 阻塞延迟异常 diff --git a/core/carry.go b/core/carry.go index f073c01..2b33e2e 100644 --- a/core/carry.go +++ b/core/carry.go @@ -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 == "" { diff --git a/core/cdle.go b/core/cdle.go index 2819952..93d9a0f 100644 --- a/core/cdle.go +++ b/core/cdle.go @@ -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) diff --git a/core/common/function.go b/core/common/function.go index a64c5bc..b1b9a85 100644 --- a/core/common/function.go +++ b/core/common/function.go @@ -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 +} diff --git a/core/function.go b/core/function.go index d989b4b..a751241 100644 --- a/core/function.go +++ b/core/function.go @@ -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) } diff --git a/core/plugin_core.go b/core/plugin_core.go index 9b69995..79c4f4d 100644 --- a/core/plugin_core.go +++ b/core/plugin_core.go @@ -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 diff --git a/core/web.go b/core/web.go index e9cb874..7214bcb 100644 --- a/core/web.go +++ b/core/web.go @@ -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 {