From 0b7b35cdcc34b5cbcf3108febc50605190b5a280 Mon Sep 17 00:00:00 2001 From: fumiama Date: Sat, 16 Oct 2021 14:02:44 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20control=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=85=A8=E5=B1=80=E7=A6=81=E7=94=A8&=E4=B8=AA=E4=BA=BA?= =?UTF-8?q?=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +++-- control/rule.go | 59 +++++++++++++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index a7c7dfa6..afc29042 100644 --- a/README.md +++ b/README.md @@ -38,8 +38,10 @@ zerobot -h -t token -u url [-d|w] [-g] qq1 qq2 qq3 ... - **动态加载插件** `import _ github.com/FloatTech/ZeroBot-Plugin-Dynamic/dyloader` - 本功能需要`cgo`,故已分离出主线。详见[ZeroBot-Plugin-Dynamic](https://github.com/FloatTech/ZeroBot-Plugin-Dynamic) - **插件控制** - - [x] /启用 xxx - - [x] /禁用 xxx + - [x] /启用 xxx (在发送的群/用户启用xxx) + - [x] /禁用 xxx (在发送的群/用户禁用xxx) + - [x] /全局启用 xxx + - [x] /全局禁用 xxx - [x] /用法 xxx - [x] /服务列表 - **聊天** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_chat"` diff --git a/control/rule.go b/control/rule.go index c4038a63..cb036720 100644 --- a/control/rule.go +++ b/control/rule.go @@ -4,6 +4,7 @@ package control import ( "os" "strconv" + "strings" "sync" "github.com/sirupsen/logrus" @@ -14,6 +15,8 @@ import ( "github.com/FloatTech/ZeroBot-Plugin/utils/sql" ) +const ALL int64 = 0 + var ( db = &sql.Sqlite{DBPath: "data/control/plugins.db"} // managers 每个插件对应的管理 @@ -50,6 +53,7 @@ func newctrl(service string, o *Options) *Control { } // Enable enables a group to pass the Manager. +// groupID == 0 (ALL) will operate on all grps. func (m *Control) Enable(groupID int64) { m.Lock() err := db.Insert(m.service, &grpcfg{groupID, 0}) @@ -60,6 +64,7 @@ func (m *Control) Enable(groupID int64) { } // Disable disables a group to pass the Manager. +// groupID == 0 (ALL) will operate on all grps. func (m *Control) Disable(groupID int64) { m.Lock() err := db.Insert(m.service, &grpcfg{groupID, 1}) @@ -73,7 +78,15 @@ func (m *Control) Disable(groupID int64) { func (m *Control) IsEnabledIn(gid int64) bool { m.RLock() var c grpcfg - err := db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(gid, 10)) + err := db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(ALL, 10)) + if err == nil { + logrus.Debugf("[control] plugin %s of all : %d", m.service, c.GroupID, c.Disable) + if c.Disable != 0 { + m.RUnlock() + return false + } + } + err = db.Find(m.service, &c, "WHERE gid = "+strconv.FormatInt(gid, 10)) if err == nil { m.RUnlock() logrus.Debugf("[control] plugin %s of grp %d : %d", m.service, c.GroupID, c.Disable) @@ -93,7 +106,12 @@ func (m *Control) IsEnabledIn(gid int64) bool { func (m *Control) Handler() zero.Rule { return func(ctx *zero.Ctx) bool { ctx.State["manager"] = m - return m.IsEnabledIn(ctx.Event.GroupID) + grp := ctx.Event.GroupID + if grp == 0 { + // 个人用户 + grp = -ctx.Event.UserID + } + return m.IsEnabledIn(grp) } } @@ -135,7 +153,15 @@ func init() { panic(err) } else { hasinit = true - zero.OnCommandGroup([]string{"启用", "enable"}, zero.AdminPermission, zero.OnlyGroup). + zero.OnCommandGroup([]string{ + "启用", "enable", "禁用", "disable", + "全局启用", "enableall", "全局禁用", "disableall", + }, func(ctx *zero.Ctx) bool { + if zero.OnlyGroup(ctx) { + return zero.AdminPermission(ctx) + } + return zero.OnlyToMe(ctx) + }). Handle(func(ctx *zero.Ctx) { model := extension.CommandModel{} _ = ctx.Parse(&model) @@ -143,20 +169,21 @@ func init() { if !ok { ctx.SendChain(message.Text("没有找到指定服务!")) } - service.Enable(ctx.Event.GroupID) - ctx.SendChain(message.Text("已启用服务: " + model.Args)) - }) - - zero.OnCommandGroup([]string{"禁用", "disable"}, zero.AdminPermission, zero.OnlyGroup). - Handle(func(ctx *zero.Ctx) { - model := extension.CommandModel{} - _ = ctx.Parse(&model) - service, ok := Lookup(model.Args) - if !ok { - ctx.SendChain(message.Text("没有找到指定服务!")) + grp := ctx.Event.GroupID + if grp == 0 { + // 个人用户 + grp = -ctx.Event.UserID + } + if strings.Contains(model.Command, "全局") || strings.Contains(model.Command, "all") { + grp = 0 + } + if strings.Contains(model.Command, "启用") || strings.Contains(model.Command, "enable") { + service.Enable(grp) + ctx.SendChain(message.Text("已启用服务: " + model.Args)) + } else { + service.Disable(grp) + ctx.SendChain(message.Text("已禁用服务: " + model.Args)) } - service.Disable(ctx.Event.GroupID) - ctx.SendChain(message.Text("已关闭服务: " + model.Args)) }) zero.OnCommandGroup([]string{"用法", "usage"}, zero.AdminPermission, zero.OnlyGroup).