diff --git a/core/common/function.go b/core/common/function.go index e05c754..a59d8fc 100644 --- a/core/common/function.go +++ b/core/common/function.go @@ -32,12 +32,13 @@ type Function struct { Encrypt bool `json:"encrypt"` OnStart bool `json:"on_start"` PluginPublisher - Running bool `json:"running"` - Https []*Http `json:"-"` - Reply *Reply `json:"-"` - Downloads int `json:"downloads"` - HasForm bool `json:"has_form"` - Carry bool `json:"carry"` + Running bool `json:"running"` + Https []*Http `json:"-"` + Reply *Reply `json:"-"` + Downloads int `json:"downloads"` + HasForm bool `json:"has_form"` + Carry bool `json:"carry"` + Messages interface{} `json:"messages"` } type Filter struct { BlackMode bool diff --git a/core/plugin_impl.go b/core/plugin_impl.go index f1c0e55..37e82b4 100644 --- a/core/plugin_impl.go +++ b/core/plugin_impl.go @@ -41,6 +41,7 @@ type SenderJsIplm struct { } type Console struct { + UUID string } var console = &Console{} @@ -77,6 +78,7 @@ func (c *Console) Warn(v ...interface{}) { } log := utils.FormatLog(v[0], v[1:]...) logs.Warn(log) + WritePluginMessage(c.UUID, "warn", log) Broadcast2WebUser(log, "warn") } @@ -86,6 +88,7 @@ func (c *Console) Error(v ...interface{}) { } log := utils.FormatLog(v[0], v[1:]...) logs.Error(log) + WritePluginMessage(c.UUID, "error", log) Broadcast2WebUser(log, "error") } diff --git a/core/plugin_message.go b/core/plugin_message.go new file mode 100644 index 0000000..783ff04 --- /dev/null +++ b/core/plugin_message.go @@ -0,0 +1,56 @@ +package core + +import ( + "encoding/json" + "time" + + "github.com/cdle/sillyplus/utils" +) + +type PMsg struct { + Class string `json:"class"` + Unix int `json:"unix"` + Content string `json:"content"` +} + +var plugin_messages = MakeBucket("plugin_messages") + +func WritePluginMessage(uuid string, class string, content string) { + if uuid == "" { + return + } + var data = plugin_messages.GetBytes(uuid) + pmsgs := []PMsg{} + json.Unmarshal(data, &pmsgs) + s := &Strings{} + ok := false + new := PMsg{ + Class: class, + Unix: int(time.Now().Unix()), + Content: content, + } + for i := range pmsgs { + if pmsgs[i].Class != class { + continue + } + if content == "" { + continue + } + if s.Similarity(pmsgs[i].Content, content) > 0.9 { + pmsgs[i] = new + ok = true + break + } + } + if !ok { + pmsgs = append(pmsgs, new) + } + plugin_messages.Set(uuid, utils.JsonMarshal(pmsgs)) +} + +func GetPluginMessage(uuid string) []PMsg { + var data = plugin_messages.GetBytes(uuid) + pmsgs := []PMsg{} + json.Unmarshal(data, &pmsgs) + return pmsgs +} diff --git a/core/plugin_subscribe.go b/core/plugin_subscribe.go index 638de5f..a1bf75b 100644 --- a/core/plugin_subscribe.go +++ b/core/plugin_subscribe.go @@ -210,6 +210,7 @@ func initWebPluginList() { rr.Data[i].Running = false for j := range fc { if rr.Data[i].UUID == fc[j].UUID { + rr.Data[i].Messages = GetPluginMessage(rr.Data[i].UUID) if rr.Data[i].Version != fc[j].Version { rr.Data[i].Status = 1 } else { diff --git a/core/plugin_utils.go b/core/plugin_utils.go index 3dcc2a6..a374ac9 100644 --- a/core/plugin_utils.go +++ b/core/plugin_utils.go @@ -322,7 +322,7 @@ func SetPluginMethod(vm *goja.Runtime, uuid string, on_start bool, running func( vm.Set("md5", utils.Md5) vm.Set("image", utils.ToImageQrcode) vm.Set("video", utils.ToVideoQrcode) - vm.Set("console", console) + vm.Set("console", &Console{UUID: uuid}) vm.Set("sillyplus", sillyplusJsIplm) vm.Set("call", func(str string) interface{} { return RegistFuncs[str]