From 3a09771432996714c8f9782f7ef0054128aa3bbe Mon Sep 17 00:00:00 2001 From: 1-6 Date: Tue, 25 Jul 2023 16:43:15 +0800 Subject: [PATCH] x --- core/adapter.go | 44 +++++++------- core/grpc_plugins.go | 129 ++++++++++++++++++++++++++++++++++++++++-- core/grpc_sender.go | 6 +- core/plugin_core.go | 3 +- core/web.go | 21 ++++++- main.go | 18 ++++-- proto3/sillygirl.d.ts | 87 ++++++++++++++++++++++++++++ proto3/sillygirl.js | 2 +- proto3/sillygirl.ts | 2 +- 9 files changed, 272 insertions(+), 40 deletions(-) create mode 100644 proto3/sillygirl.d.ts diff --git a/core/adapter.go b/core/adapter.go index 30afef6..c3eabab 100644 --- a/core/adapter.go +++ b/core/adapter.go @@ -31,7 +31,7 @@ type Details struct { type CustomSender struct { BaseSender details Details - f *Factory + F *Factory } type MsgChan struct { @@ -203,7 +203,7 @@ func (f *Factory) Init(botplt, botid string, params map[string]interface{}) { f.botid = botid f.msgChan = make(chan MsgChan, 100000) f.demo = &CustomSender{ - f: f, + F: f, } if v, ok := Bots[[2]string{botplt, botid}]; ok { if v.uuid != f.uuid { @@ -643,7 +643,7 @@ func (sender *CustomSender) GetChatID() string { return sender.details.ChatID } func (sender *CustomSender) GetImType() string { - return sender.f.botplt + return sender.F.botplt } func (sender *CustomSender) GetUserName() string { return sender.details.Username @@ -665,16 +665,16 @@ func (sender *CustomSender) GetReplySenderUserID() int { } func (sender *CustomSender) GetBotID() string { - return sender.f.botid + return sender.F.botid } type PUSH string func (sender *CustomSender) Action(options map[string]interface{}) (interface{}, error) { - if sender.f.action != nil { - return sender.f.action(options), nil + if sender.F.action != nil { + return sender.F.action(options), nil } - var platform = sender.f.botplt + var platform = sender.F.botplt var any *common.Function var one *common.Function var result interface{} @@ -683,7 +683,7 @@ func (sender *CustomSender) Action(options map[string]interface{}) (interface{}, if function.Reply != nil && function.Reply.Platform == platform { if len(function.Reply.BotsID) == 0 { any = function - } else if Contains(function.Reply.BotsID, sender.f.botid) { + } else if Contains(function.Reply.BotsID, sender.F.botid) { one = function } } @@ -711,7 +711,7 @@ func (sender *CustomSender) Action(options map[string]interface{}) (interface{}, }, }) vm.Set("action", proxy) - vm.Set("adapter", sender.f) + vm.Set("adapter", sender.F) }) } @@ -720,8 +720,8 @@ func (sender *CustomSender) Action(options map[string]interface{}) (interface{}, func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) { var push = false - var platform = sender.f.botplt - var bot_id = sender.f.botid + var platform = sender.F.botplt + var bot_id = sender.F.botid var args = []interface{}{} for _, item := range msgs { switch item := item.(type) { @@ -761,8 +761,8 @@ func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) { for k, v := range sender.GetExpandMessageInfo() { msg[k] = v } - if sender.f.umod { //订阅号模式 - v, loaded := sender.f.gmsgChan.LoadOrStore(user_id, &GMsgChan{}) + if sender.F.umod { //订阅号模式 + v, loaded := sender.F.gmsgChan.LoadOrStore(user_id, &GMsgChan{}) ch := v.(*GMsgChan) if !loaded { // console.Debug("发送创建:", ch.Chan) @@ -781,14 +781,14 @@ func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) { } return "", nil } - if sender.f.reply == nil { //未设置回复函数 + if sender.F.reply == nil { //未设置回复函数 var any *common.Function var one *common.Function for _, function := range Functions { if function.Reply != nil && function.Reply.Platform == platform { if len(function.Reply.BotsID) == 0 { any = function - } else if Contains(function.Reply.BotsID, sender.f.botid) { + } else if Contains(function.Reply.BotsID, sender.F.botid) { one = function } } @@ -813,7 +813,7 @@ func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) { }) vm.Set("msg", proxy) vm.Set("message", proxy) - vm.Set("adapter", sender.f) + vm.Set("adapter", sender.F) }) return message_id, nil } else { //存储消息 @@ -821,10 +821,10 @@ func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) { Msg: msg, Chan: make(chan string), } - if sender.f.destroid { + if sender.F.destroid { return "", errors.New("adapter destroid") } - sender.f.msgChan <- c + sender.F.msgChan <- c select { case id := <-c.Chan: return id, nil @@ -835,7 +835,7 @@ func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) { } } else { //todo 阻塞延迟异常 - v := sender.f.reply(msg) + v := sender.F.reply(msg) return v, nil } } @@ -930,10 +930,10 @@ func (sender *CustomSender) GroupUnban(uid string) error { } func (sender *CustomSender) IsAdmin() bool { - if sender.f.isAdmin == nil { - return Contains(strings.Split(MakeBucket(sender.f.botplt).GetString("masters"), "&"), sender.GetUserID()) + if sender.F.isAdmin == nil { + return Contains(strings.Split(MakeBucket(sender.F.botplt).GetString("masters"), "&"), sender.GetUserID()) } - return sender.f.isAdmin(sender.GetUserID()) + return sender.F.isAdmin(sender.GetUserID()) } func (sender *CustomSender) GetID() string { diff --git a/core/grpc_plugins.go b/core/grpc_plugins.go index 7977f69..182e5ea 100644 --- a/core/grpc_plugins.go +++ b/core/grpc_plugins.go @@ -111,6 +111,19 @@ func initNodePlugins() { } else { // fmt.Println("非插件目录", event.Name) } + tf := event.Name + "/node_modules/sillygirl.d.ts" + ti := event.Name + "/main.js" + if _, err := os.Stat(tf); err != nil { + os.Mkdir(event.Name+"/node_modules", 0700) + os.WriteFile(tf, []byte(typeat), 0700) + } + go func() { + time.Sleep(time.Second) + if _, err := os.Stat(ti); err != nil { + os.Mkdir(event.Name+"/node_modules", 0700) + os.WriteFile(ti, []byte(defaultScript(plugin_name)), 0700) + } + }() } else if plugin_index { // fmt.Println("增加插件", event.Name) // RemNodePlugin(plugin_name) @@ -207,8 +220,6 @@ func AddNodePlugin(path, name string) error { cmd := exec.Command("./node", path) cmd.Dir = utils.ExecPath + "/language/node" // cmd := exec.Command(utils.ExecPath+"/language/node/node", path) - id := s.SetID() - cmd.Env = append(cmd.Env, "SENDER_ID="+id) cmd.Env = append(cmd.Env, "PLUGIN_ID="+uuid) // 获取标准输出和标准错误输出的管道 stdout, err := cmd.StdoutPipe() @@ -221,10 +232,6 @@ func AddNodePlugin(path, name string) error { // fmt.Printf("获取标准错误输出管道失败:%v\n", err) // os.Exit(1) } - err = cmd.Start() - if err != nil { - - } // file, err := os.Create("output.log") // if err != nil { @@ -260,6 +267,12 @@ func AddNodePlugin(path, name string) error { }() processes.Store(cmd, s) if (plt) != "*" { + id := s.SetID() + cmd.Env = append(cmd.Env, "SENDER_ID="+id) + err = cmd.Start() + if err != nil { + + } senders.Store(id, s) defer senders.Delete(id) defer processes.Delete(cmd) @@ -269,6 +282,10 @@ func AddNodePlugin(path, name string) error { return nil } } else { + err = cmd.Start() + if err != nil { + + } processes.Range(func(key, value any) bool { p := key.(*exec.Cmd) if p == cmd { @@ -296,3 +313,103 @@ func AddNodePlugin(path, name string) error { AddCommand([]*common.Function{f}) return nil } + +var typeat = `declare class Sender { + uuid: string; + private destoried; + constructor(uuid: string); + destructor(): void; + getUserId(): Promise; + getUserName(): Promise; + getChatId(): Promise; + getChatName(): Promise; + getMessageId(): Promise; + getPlatform(): Promise; + getBotId(): Promise; + getContent(): Promise; + param(key: number | string): Promise; + setContent(content: string): Promise; + continue(): Promise; + getAdapter(): Promise; + listen(options?: { + rules?: string[]; + timeout?: number; + handle?: (s: Sender) => Promise | string | void; + listen_private?: boolean; + listen_group?: boolean; + allow_platforms?: string[]; + prohibit_platforms?: string[]; + allow_groups?: string[]; + prohibit_groups?: string[]; + allow_users?: string[]; + prohibit_users?: string[]; + persistent?: boolean; + }): Promise; + holdOn(str: string): string; + reply(content: string): Promise; + action(options: any): Promise; + event(): Promise; +} +declare class Bucket { + name: string; + constructor(name: string); + transform(v: string | undefined): string | number | boolean | undefined; + reverseTransform(value: any): string; + get(key: string, defaultValue?: any): Promise; + set(key: string, value: any): Promise<{ + message?: string; + changed?: boolean; + }>; + getAll(): Promise; + delete(): Promise; + keys(): Promise; + len(): Promise; + buckets(): Promise; + watch(key: string, handle: (old: any, now: any, key: string) => StorageFinal | void | any): void; + _name(): Promise; +} +interface StorageFinal { + echo?: string; + now?: any; + message?: string; + error?: string; +} +interface Message { + message_id?: string; + user_id: string; + chat_id?: string; + content: string; + user_name?: string; + chat_name?: string; +} +declare class Adapter { + platform: string | undefined; + bot_id: string | undefined; + call: any; + constructor(options: { + platform?: string; + bot_id?: string; + replyHandler?: (message: Message) => string | undefined; + actionHandler?: (message: Message) => string | undefined; + }); + setActionHandler(func: (action: {}) => any): void; + receive(message: Message): Promise; + push(message: Message): Promise; + destroy(): Promise; + sender(options: any): Promise; +} +declare let sender: Sender; +declare function sleep(ms: number | undefined): Promise; +export { Adapter, Bucket, sender, sleep }; +` + +func defaultScript(title string) string { + create_at := time.Now().Format("2006-01-02 15:04:05") + return `/** + * @title ` + title + ` + * @create_at ` + create_at + ` + * @description 🐒这个人很懒什么都没有留下 + * @author ` + sillyGirl.GetString("author", "佚名") + ` + * @version v1.0.0 + */` +} diff --git a/core/grpc_sender.go b/core/grpc_sender.go index a9b5c61..67dbda7 100644 --- a/core/grpc_sender.go +++ b/core/grpc_sender.go @@ -34,14 +34,16 @@ var senders sync.Map // } func GetSender(uuid string) (common.Sender, error) { + if uuid == "" { return &CustomSender{ - f: &Factory{ + F: &Factory{ botid: "*", }, }, nil } v, ok := senders.Load(uuid) + fmt.Println("uuid", uuid, v, ok) if !ok { return nil, errors.New("not found sender") } @@ -138,6 +140,7 @@ func (sg *SillyGirlService) SenderListen(stream srpc.SillyGirlService_SenderList // defer fmt.Println("已关闭,", "===") for { req, err := stream.Recv() + // fmt.Println("carry", carry, err) if err == io.EOF { break // 如果流已经关闭,则退出循环 @@ -146,6 +149,7 @@ func (sg *SillyGirlService) SenderListen(stream srpc.SillyGirlService_SenderList return err } if carry != nil { + fmt.Println("req.Uuid", req.Uuid) echo := req.GetUuid() value := req.GetValue() // fmt.Println("echo", echo, "value", value) diff --git a/core/plugin_core.go b/core/plugin_core.go index 2ca98bd..80396c6 100644 --- a/core/plugin_core.go +++ b/core/plugin_core.go @@ -115,7 +115,7 @@ func initPlugins() { if p.UUID != key { continue } - if p.Type != "goja" { //下载目录插件 + if p.Type != "goja" && p.Type != "" { //下载目录插件 // Content-Type var prefix = "?uuid=" + p.UUID address := p.Address @@ -149,7 +149,6 @@ func initPlugins() { Error: err, } } - fmt.Println(zipfile) defer os.Remove(zipfile) if err := unzip(zipfile, 0755); err != nil { return &storage.Final{ diff --git a/core/web.go b/core/web.go index 181e5d7..ea607d0 100644 --- a/core/web.go +++ b/core/web.go @@ -7,6 +7,7 @@ import ( "embed" "fmt" "io" + "io/ioutil" "net" "net/http" "os" @@ -100,8 +101,10 @@ func initWeb() { return nil } // 将路径转换为相对路径 + relPath, err := filepath.Rel(dir, path) relPath = name + "/" + relPath + is_index := relPath == "main.js" if err != nil { return err } @@ -124,8 +127,22 @@ func initWeb() { if err != nil { return err } - - _, err = io.Copy(wr, file) + if is_index { + var data []byte + data, err = ioutil.ReadAll(file) + if err != nil { + return err + } + su := &ScriptUtils{ + script: string(data), + } + if su.GetValue("public") == "true" { + su.SetValue("public", "false") + } + _, err = wr.Write([]byte(su.script)) + } else { + _, err = io.Copy(wr, file) + } return err }) if err != nil { diff --git a/main.go b/main.go index 26ca781..21cbc80 100644 --- a/main.go +++ b/main.go @@ -65,14 +65,22 @@ func main() { if t { core.Logs.Info("Terminal机器人已连接") scanner := bufio.NewScanner(os.Stdin) + a := &core.Factory{} + a.Init("terminal", "default", nil) + a.SetReplyHandler(func(m map[string]interface{}) string { + fmt.Printf("\x1b[%dm%s \x1b[0m\n", 31, m[core.CONETNT]) + return "" + }) for scanner.Scan() { data := scanner.Text() - f := &core.Faker{ - Type: "terminal", - Message: string(data), - Admin: true, + s := &core.CustomSender{ + // Type: "terminal", + // Message: string(data), + // Admin: true, + F: a, } - core.Messages <- f + s.SetContent(data) + core.Messages <- s } core.Logs.Info("Terminal机器人异常,请检查运行环境设置,如果是docker环境,请附加-it参数") } else { diff --git a/proto3/sillygirl.d.ts b/proto3/sillygirl.d.ts new file mode 100644 index 0000000..bb14ed6 --- /dev/null +++ b/proto3/sillygirl.d.ts @@ -0,0 +1,87 @@ +declare class Sender { + uuid: string; + private destoried; + constructor(uuid: string); + destructor(): void; + getUserId(): Promise; + getUserName(): Promise; + getChatId(): Promise; + getChatName(): Promise; + getMessageId(): Promise; + getPlatform(): Promise; + getBotId(): Promise; + getContent(): Promise; + param(key: number | string): Promise; + setContent(content: string): Promise; + continue(): Promise; + getAdapter(): Promise; + listen(options?: { + rules?: string[]; + timeout?: number; + handle?: (s: Sender) => Promise | string | void; + listen_private?: boolean; + listen_group?: boolean; + allow_platforms?: string[]; + prohibit_platforms?: string[]; + allow_groups?: string[]; + prohibit_groups?: string[]; + allow_users?: string[]; + prohibit_users?: string[]; + persistent?: boolean; + }): Promise; + holdOn(str: string): string; + reply(content: string): Promise; + action(options: any): Promise; + event(): Promise; +} +declare class Bucket { + name: string; + constructor(name: string); + transform(v: string | undefined): string | number | boolean | undefined; + reverseTransform(value: any): string; + get(key: string, defaultValue?: any): Promise; + set(key: string, value: any): Promise<{ + message?: string; + changed?: boolean; + }>; + getAll(): Promise; + delete(): Promise; + keys(): Promise; + len(): Promise; + buckets(): Promise; + watch(key: string, handle: (old: any, now: any, key: string) => StorageFinal | void | any): void; + _name(): Promise; +} +interface StorageFinal { + echo?: string; + now?: any; + message?: string; + error?: string; +} +interface Message { + message_id?: string; + user_id: string; + chat_id?: string; + content: string; + user_name?: string; + chat_name?: string; +} +declare class Adapter { + platform: string | undefined; + bot_id: string | undefined; + call: any; + constructor(options: { + platform?: string; + bot_id?: string; + replyHandler?: (message: Message) => string | undefined; + actionHandler?: (message: Message) => string | undefined; + }); + setActionHandler(func: (action: {}) => any): void; + receive(message: Message): Promise; + push(message: Message): Promise; + destroy(): Promise; + sender(options: any): Promise; +} +declare let sender: Sender; +declare function sleep(ms: number | undefined): Promise; +export { Adapter, Bucket, sender, sleep }; diff --git a/proto3/sillygirl.js b/proto3/sillygirl.js index dca8225..8bfc65e 100644 --- a/proto3/sillygirl.js +++ b/proto3/sillygirl.js @@ -226,7 +226,7 @@ class Sender { persistent: options?.persistent, plugin_id, }; - if (options?.handle && "*" == (await this.getPlatform())) { + if (!this.uuid) { params.persistent = true; } const call = client.SenderListen(); diff --git a/proto3/sillygirl.ts b/proto3/sillygirl.ts index 1718781..0062e87 100644 --- a/proto3/sillygirl.ts +++ b/proto3/sillygirl.ts @@ -248,7 +248,7 @@ class Sender { persistent: options?.persistent, plugin_id, }; - if (options?.handle && "*" == (await this.getPlatform())) { + if (!this.uuid) { params.persistent = true; } const call = client.SenderListen();