diff --git a/bot/bot.go b/bot/bot.go index 8674a34..4d0c1a0 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -3,10 +3,12 @@ package bot import ( - "html/template" + "fmt" + "math/rand" "net/http" "reflect" "strings" + "time" "github.com/jmoiron/sqlx" "github.com/rs/zerolog/log" @@ -38,12 +40,19 @@ type bot struct { version string // The entries to the bot's HTTP interface - httpEndPoints map[string]string + httpEndPoints []EndPoint // filters registered by plugins filters map[string]func(string) string callbacks CallbackMap + + password string + passwordCreated time.Time +} + +type EndPoint struct { + Name, URL string } // Variable represents a $var replacement @@ -73,7 +82,7 @@ func New(config *config.Config, connector Connector) Bot { me: users[0], logIn: logIn, logOut: logOut, - httpEndPoints: make(map[string]string), + httpEndPoints: make([]EndPoint, 0), filters: make(map[string]func(string) string), callbacks: make(CallbackMap), } @@ -133,46 +142,6 @@ func (b *bot) Who(channel string) []user.User { return users } -var rootIndex = ` - - - - Factoids - - - - {{if .EndPoints}} -
- - - - - - - - - {{range $key, $value := .EndPoints}} - - - - {{end}} - -
Plugin
{{$key}}
-
- {{end}} - -` - -func (b *bot) serveRoot(w http.ResponseWriter, r *http.Request) { - context := make(map[string]interface{}) - context["EndPoints"] = b.httpEndPoints - t, err := template.New("rootIndex").Parse(rootIndex) - if err != nil { - log.Error().Err(err) - } - t.Execute(w, context) -} - // IsCmd checks if message is a command and returns its curtailed version func IsCmd(c *config.Config, message string) (bool, string) { cmdcs := c.GetArray("CommandChar", []string{"!"}) @@ -266,5 +235,17 @@ func (b *bot) Register(p Plugin, kind Kind, cb Callback) { } func (b *bot) RegisterWeb(root, name string) { - b.httpEndPoints[name] = root + b.httpEndPoints = append(b.httpEndPoints, EndPoint{name, root}) +} + +func (b *bot) GetPassword() string { + if b.passwordCreated.Before(time.Now().Add(-24 * time.Hour)) { + adjs := b.config.GetArray("bot.passwordAdjectives", []string{"very"}) + nouns := b.config.GetArray("bot.passwordNouns", []string{"noun"}) + verbs := b.config.GetArray("bot.passwordVerbs", []string{"do"}) + a, n, v := adjs[rand.Intn(len(adjs))], nouns[rand.Intn(len(nouns))], verbs[rand.Intn(len(verbs))] + b.passwordCreated = time.Now() + b.password = fmt.Sprintf("%s-%s-%s", a, n, v) + } + return b.password } diff --git a/bot/interfaces.go b/bot/interfaces.go index fba9396..d9f5b62 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -66,6 +66,8 @@ type Bot interface { RegisterFilter(string, func(string) string) RegisterWeb(string, string) DefaultConnector() Connector + GetWebNavigation() []EndPoint + GetPassword() string } // Connector represents a server connection to a chat service diff --git a/bot/mock.go b/bot/mock.go index 6f67c62..6d7fb70 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -32,6 +32,7 @@ func (mb *MockBot) DB() *sqlx.DB { return mb.Cfg.DB } 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) Send(c Connector, kind Kind, args ...interface{}) (string, error) { switch kind { case Message: @@ -52,6 +53,7 @@ func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (string, er func (mb *MockBot) AddPlugin(f Plugin) {} func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {} func (mb *MockBot) RegisterWeb(_, _ string) {} +func (mb *MockBot) GetWebNavigation() []EndPoint { return nil } func (mb *MockBot) Receive(c Connector, kind Kind, msg msg.Message, args ...interface{}) bool { return false } diff --git a/bot/web.go b/bot/web.go new file mode 100644 index 0000000..b8e52ed --- /dev/null +++ b/bot/web.go @@ -0,0 +1,73 @@ +package bot + +import ( + "html/template" + "net/http" + "strings" +) + +func (b *bot) serveRoot(w http.ResponseWriter, r *http.Request) { + context := make(map[string]interface{}) + context["Nav"] = b.GetWebNavigation() + t := template.Must(template.New("rootIndex").Parse(rootIndex)) + t.Execute(w, context) +} + +// GetWebNavigation returns a list of bootstrap-vue links +// The parent