diff --git a/bot/bot.go b/bot/bot.go index 8282290..4cae7b4 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -50,6 +50,8 @@ type bot struct { password string passwordCreated time.Time + + quiet bool } type EndPoint struct { @@ -259,3 +261,7 @@ func (b *bot) GetPassword() string { } return b.password } + +func (b *bot) SetQuiet(status bool) { + b.quiet = status +} diff --git a/bot/handlers.go b/bot/handlers.go index 7361b8f..c5baf21 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -54,6 +54,9 @@ func (b *bot) runCallback(conn Connector, plugin Plugin, evt Kind, message msg.M // Send a message to the connection func (b *bot) Send(conn Connector, kind Kind, args ...interface{}) (string, error) { + if b.quiet { + return "", nil + } return conn.Send(kind, args...) } diff --git a/bot/interfaces.go b/bot/interfaces.go index d9f5b62..1050a6f 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -68,6 +68,7 @@ type Bot interface { DefaultConnector() Connector GetWebNavigation() []EndPoint GetPassword() string + SetQuiet(bool) } // Connector represents a server connection to a chat service diff --git a/bot/mock.go b/bot/mock.go index 6d7fb70..a72fe18 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -33,6 +33,7 @@ func (mb *MockBot) Who(string) []user.User { return []user.User{} } func (mb *MockBot) WhoAmI() string { return "tester" } func (mb *MockBot) DefaultConnector() Connector { return nil } func (mb *MockBot) GetPassword() string { return "12345" } +func (mb *MockBot) SetQuiet(bool) {} func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (string, error) { switch kind { case Message: diff --git a/plugins/admin/admin.go b/plugins/admin/admin.go index 80adab7..ed7b1c1 100644 --- a/plugins/admin/admin.go +++ b/plugins/admin/admin.go @@ -55,7 +55,7 @@ var forbiddenKeys = map[string]bool{ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Message, args ...interface{}) bool { body := message.Body - if p.quiet { + if p.quiet && message.Body != "come back" { return true } @@ -67,6 +67,14 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag return false } + if p.quiet && message.Body == "come back" { + p.quiet = false + p.bot.SetQuiet(false) + backMsg := p.bot.Config().Get("admin.comeback", "Okay, I'm back.") + p.bot.Send(conn, bot.Message, message.Channel, backMsg) + return true + } + if strings.ToLower(body) == "reboot" { p.bot.Send(conn, bot.Message, message.Channel, "brb") log.Info().Msgf("Got reboot command") @@ -76,13 +84,18 @@ func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Messag if strings.ToLower(body) == "shut up" { dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute log.Info().Msgf("Going to sleep for %v, %v", dur, time.Now().Add(dur)) - p.bot.Send(conn, bot.Message, message.Channel, "Okay. I'll be back later.") + leaveMsg := p.bot.Config().Get("admin.shutup", "Okay. I'll be back later.") + p.bot.Send(conn, bot.Message, message.Channel, leaveMsg) p.quiet = true + p.bot.SetQuiet(true) go func() { select { case <-time.After(dur): p.quiet = false + p.bot.SetQuiet(false) log.Info().Msg("Waking up from nap.") + backMsg := p.bot.Config().Get("admin.backmsg", "I'm back, bitches.") + p.bot.Send(conn, bot.Message, message.Channel, backMsg) } }() return true