bot: Invert RegisterWeb

This commit is contained in:
Chris Sexton 2019-02-07 11:30:42 -05:00
parent 104ff85a0d
commit a20839cdd7
36 changed files with 21 additions and 169 deletions

View File

@ -116,9 +116,6 @@ func (b *bot) AddPlugin(h Plugin) {
name := reflect.TypeOf(h).String() name := reflect.TypeOf(h).String()
b.plugins[name] = h b.plugins[name] = h
b.pluginOrdering = append(b.pluginOrdering, name) b.pluginOrdering = append(b.pluginOrdering, name)
if entry := h.RegisterWeb(); entry != nil {
b.httpEndPoints[name] = *entry
}
} }
func (b *bot) Who(channel string) []user.User { func (b *bot) Who(channel string) []user.User {
@ -260,3 +257,7 @@ func (b *bot) Register(p Plugin, kind Kind, cb Callback) {
} }
b.callbacks[t][kind] = append(b.callbacks[t][kind], cb) b.callbacks[t][kind] = append(b.callbacks[t][kind], cb)
} }
func (b *bot) RegisterWeb(root, name string) {
b.httpEndPoints[name] = root
}

View File

@ -59,6 +59,7 @@ type Bot interface {
CheckAdmin(string) bool CheckAdmin(string) bool
GetEmojiList() map[string]string GetEmojiList() map[string]string
RegisterFilter(string, func(string) string) RegisterFilter(string, func(string) string)
RegisterWeb(string, string)
} }
// Connector represents a server connection to a chat service // Connector represents a server connection to a chat service
@ -74,7 +75,6 @@ type Connector interface {
} }
// Plugin interface used for compatibility with the Plugin interface // Plugin interface used for compatibility with the Plugin interface
// Probably can disappear once RegisterWeb gets inverted // Uhh it turned empty, but we're still using it to ID plugins
type Plugin interface { type Plugin interface {
RegisterWeb() *string
} }

View File

@ -5,6 +5,7 @@ package bot
import ( import (
"fmt" "fmt"
"log" "log"
"net/http"
"strconv" "strconv"
"strings" "strings"
@ -48,6 +49,7 @@ func (mb *MockBot) Send(kind Kind, args ...interface{}) (string, error) {
} }
func (mb *MockBot) AddPlugin(f Plugin) {} func (mb *MockBot) AddPlugin(f Plugin) {}
func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {} func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {}
func (mb *MockBot) RegisterWeb(_, _ string) {}
func (mb *MockBot) Receive(kind Kind, msg msg.Message, args ...interface{}) bool { return false } func (mb *MockBot) Receive(kind Kind, msg msg.Message, args ...interface{}) bool { return false }
func (mb *MockBot) Filter(msg msg.Message, s string) string { return s } func (mb *MockBot) Filter(msg msg.Message, s string) string { return s }
func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil } func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil }
@ -99,5 +101,7 @@ func NewMockBot() *MockBot {
Messages: make([]string, 0), Messages: make([]string, 0),
Actions: make([]string, 0), Actions: make([]string, 0),
} }
// If any plugin registered a route, we need to reset those before any new test
http.DefaultServeMux = new(http.ServeMux)
return &b return &b
} }

View File

@ -149,8 +149,3 @@ func (p *AdminPlugin) help(kind bot.Kind, m msg.Message, args ...interface{}) bo
p.Bot.Send(bot.Message, m.Channel, "This does super secret things that you're not allowed to know about.") p.Bot.Send(bot.Message, m.Channel, "This does super secret things that you're not allowed to know about.")
return true return true
} }
// Register any web URLs desired
func (p *AdminPlugin) RegisterWeb() *string {
return nil
}

View File

@ -162,10 +162,6 @@ func (p *BabblerPlugin) help(kind bot.Kind, msg msg.Message, args ...interface{}
return true return true
} }
func (p *BabblerPlugin) RegisterWeb() *string {
return nil
}
func (p *BabblerPlugin) makeBabbler(name string) (*Babbler, error) { func (p *BabblerPlugin) makeBabbler(name string) (*Babbler, error) {
res, err := p.db.Exec(`insert into babblers (babbler) values (?);`, name) res, err := p.db.Exec(`insert into babblers (babbler) values (?);`, name)
if err == nil { if err == nil {

View File

@ -312,10 +312,3 @@ func TestHelp(t *testing.T) {
bp.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) bp.help(bot.Help, msg.Message{Channel: "channel"}, []string{})
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
} }
func TestRegisterWeb(t *testing.T) {
mb := bot.NewMockBot()
bp := newBabblerPlugin(mb)
assert.NotNil(t, bp)
assert.Nil(t, bp.RegisterWeb())
}

View File

@ -434,8 +434,3 @@ func (p *BeersPlugin) untappdLoop(channel string) {
p.checkUntappd(channel) p.checkUntappd(channel)
} }
} }
// Register any web URLs desired
func (p BeersPlugin) RegisterWeb() *string {
return nil
}

View File

@ -124,8 +124,3 @@ func TestHelp(t *testing.T) {
b.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) b.help(bot.Help, msg.Message{Channel: "channel"}, []string{})
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
} }
func TestRegisterWeb(t *testing.T) {
b, _ := makeBeersPlugin(t)
assert.Nil(t, b.RegisterWeb())
}

View File

@ -71,7 +71,3 @@ func (p *CSWPlugin) message(kind bot.Kind, message msg.Message, args ...interfac
return false return false
} }
func (p *CSWPlugin) RegisterWeb() *string {
return nil
}

View File

@ -455,11 +455,6 @@ func (p *CounterPlugin) help(kind bot.Kind, message msg.Message, args ...interfa
return true return true
} }
// Register any web URLs desired
func (p *CounterPlugin) RegisterWeb() *string {
return nil
}
func (p *CounterPlugin) checkMatch(message msg.Message) bool { func (p *CounterPlugin) checkMatch(message msg.Message) bool {
nick := message.User.Name nick := message.User.Name
channel := message.Channel channel := message.Channel

View File

@ -253,9 +253,3 @@ func TestHelp(t *testing.T) {
c.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) c.help(bot.Help, msg.Message{Channel: "channel"}, []string{})
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
} }
func TestRegisterWeb(t *testing.T) {
_, c := setup(t)
assert.NotNil(t, c)
assert.Nil(t, c.RegisterWeb())
}

View File

@ -18,7 +18,9 @@ type DBPlugin struct {
} }
func New(b bot.Bot) *DBPlugin { func New(b bot.Bot) *DBPlugin {
return &DBPlugin{b, b.Config()} p := &DBPlugin{b, b.Config()}
p.registerWeb()
return p
} }
func (p *DBPlugin) Message(message msg.Message) bool { return false } func (p *DBPlugin) Message(message msg.Message) bool { return false }
@ -27,10 +29,8 @@ func (p *DBPlugin) ReplyMessage(msg.Message, string) bool { return false }
func (p *DBPlugin) BotMessage(message msg.Message) bool { return false } func (p *DBPlugin) BotMessage(message msg.Message) bool { return false }
func (p *DBPlugin) Help(channel string, parts []string) {} func (p *DBPlugin) Help(channel string, parts []string) {}
func (p *DBPlugin) RegisterWeb() *string { func (p *DBPlugin) registerWeb() {
http.HandleFunc("/db/catbase.db", p.serveQuery) http.HandleFunc("/db/catbase.db", p.serveQuery)
tmp := "/db/catbase.db"
return &tmp
} }
func (p *DBPlugin) serveQuery(w http.ResponseWriter, r *http.Request) { func (p *DBPlugin) serveQuery(w http.ResponseWriter, r *http.Request) {

View File

@ -74,8 +74,3 @@ func (p *DicePlugin) help(kind bot.Kind, message msg.Message, args ...interface{
p.Bot.Send(bot.Message, message.Channel, "Roll dice using notation XdY. Try \"3d20\".") p.Bot.Send(bot.Message, message.Channel, "Roll dice using notation XdY. Try \"3d20\".")
return true return true
} }
// Register any web URLs desired
func (p *DicePlugin) RegisterWeb() *string {
return nil
}

View File

@ -89,10 +89,3 @@ func TestHelp(t *testing.T) {
c.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) c.help(bot.Help, msg.Message{Channel: "channel"}, []string{})
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
} }
func TestRegisterWeb(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
assert.Nil(t, c.RegisterWeb())
}

View File

@ -99,10 +99,6 @@ func (p *EmojifyMePlugin) message(kind bot.Kind, message msg.Message, args ...in
return false return false
} }
func (p *EmojifyMePlugin) RegisterWeb() *string {
return nil
}
func stringsContain(haystack []string, needle string) bool { func stringsContain(haystack []string, needle string) bool {
for _, s := range haystack { for _, s := range haystack {
if s == needle { if s == needle {

View File

@ -317,6 +317,8 @@ func New(botInst bot.Bot) *Factoid {
botInst.Register(p, bot.Message, p.message) botInst.Register(p, bot.Message, p.message)
botInst.Register(p, bot.Help, p.help) botInst.Register(p, bot.Help, p.help)
p.registerWeb()
return p return p
} }
@ -737,11 +739,10 @@ func (p *Factoid) factTimer(channel string) {
} }
// Register any web URLs desired // Register any web URLs desired
func (p *Factoid) RegisterWeb() *string { func (p *Factoid) registerWeb() {
http.HandleFunc("/factoid/req", p.serveQuery) http.HandleFunc("/factoid/req", p.serveQuery)
http.HandleFunc("/factoid", p.serveQuery) http.HandleFunc("/factoid", p.serveQuery)
tmp := "/factoid" p.Bot.RegisterWeb("/factoid", "Factoid")
return &tmp
} }
func linkify(text string) template.HTML { func linkify(text string) template.HTML {

View File

@ -153,11 +153,6 @@ func (p *RememberPlugin) randQuote() string {
return f.Tidbit return f.Tidbit
} }
// Register any web URLs desired
func (p RememberPlugin) RegisterWeb() *string {
return nil
}
func (p *RememberPlugin) recordMsg(message msg.Message) { func (p *RememberPlugin) recordMsg(message msg.Message) {
log.Printf("Logging message: %s: %s", message.User.Name, message.Body) log.Printf("Logging message: %s: %s", message.User.Name, message.Body)
p.Log[message.Channel] = append(p.Log[message.Channel], message) p.Log[message.Channel] = append(p.Log[message.Channel], message)

View File

@ -215,8 +215,3 @@ func (p *FirstPlugin) help(kind bot.Kind, message msg.Message, args ...interface
p.Bot.Send(bot.Message, message.Channel, "Sorry, First does not do a goddamn thing.") p.Bot.Send(bot.Message, message.Channel, "Sorry, First does not do a goddamn thing.")
return true return true
} }
// Register any web URLs desired
func (p *FirstPlugin) RegisterWeb() *string {
return nil
}

View File

@ -224,8 +224,3 @@ func checkerr(e error) {
log.Println(e) log.Println(e)
} }
} }
func (p *InventoryPlugin) RegisterWeb() *string {
// nothing to register
return nil
}

View File

@ -62,8 +62,3 @@ func (p *LeftpadPlugin) message(kind bot.Kind, message msg.Message, args ...inte
return false return false
} }
func (p *LeftpadPlugin) RegisterWeb() *string {
// nothing to register
return nil
}

View File

@ -85,8 +85,3 @@ func TestNotPadding(t *testing.T) {
p.message(makeMessage("!lololol")) p.message(makeMessage("!lololol"))
assert.Len(t, mb.Messages, 0) assert.Len(t, mb.Messages, 0)
} }
func TestRegisterWeb(t *testing.T) {
p, _ := makePlugin(t)
assert.Nil(t, p.RegisterWeb())
}

View File

@ -94,8 +94,3 @@ func (p *NerdepediaPlugin) help(kind bot.Kind, message msg.Message, args ...inte
p.bot.Send(bot.Message, message.Channel, "nerd stuff") p.bot.Send(bot.Message, message.Channel, "nerd stuff")
return true return true
} }
// Register any web URLs desired
func (p *NerdepediaPlugin) RegisterWeb() *string {
return nil
}

View File

@ -115,10 +115,3 @@ func (p *PickerPlugin) help(kind bot.Kind, message msg.Message, args ...interfac
p.Bot.Send(bot.Message, message.Channel, "Choose from a list of options. Try \"pick {a,b,c}\".") p.Bot.Send(bot.Message, message.Channel, "Choose from a list of options. Try \"pick {a,b,c}\".")
return true return true
} }
// Register any web URLs desired
func (p *PickerPlugin) RegisterWeb() *string {
return nil
}
func (p *PickerPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }

View File

@ -1,15 +1,3 @@
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors. // © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package plugins package plugins
import "github.com/velour/catbase/bot/msg"
// Plugin interface defines the methods needed to accept a plugin
type Plugin interface {
Message(message msg.Message) bool
Event(kind string, message msg.Message) bool
BotMessage(message msg.Message) bool
LoadData()
Help()
RegisterWeb()
}

View File

@ -63,7 +63,3 @@ func (p *ReactionPlugin) message(kind bot.Kind, message msg.Message, args ...int
return false return false
} }
func (p *ReactionPlugin) RegisterWeb() *string {
return nil
}

View File

@ -200,10 +200,6 @@ func (p *ReminderPlugin) help(kind bot.Kind, message msg.Message, args ...interf
return true return true
} }
func (p *ReminderPlugin) RegisterWeb() *string {
return nil
}
func (p *ReminderPlugin) getNextReminder() *Reminder { func (p *ReminderPlugin) getNextReminder() *Reminder {
p.mutex.Lock() p.mutex.Lock()
defer p.mutex.Unlock() defer p.mutex.Unlock()

View File

@ -226,9 +226,3 @@ func TestHelp(t *testing.T) {
c.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) c.help(bot.Help, msg.Message{Channel: "channel"}, []string{})
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
} }
func TestRegisterWeb(t *testing.T) {
c, _ := setup(t)
assert.NotNil(t, c)
assert.Nil(t, c.RegisterWeb())
}

View File

@ -124,10 +124,6 @@ func (p *RPGPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}
return true return true
} }
func (p *RPGPlugin) RegisterWeb() *string {
return nil
}
func (p *RPGPlugin) replyMessage(kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *RPGPlugin) replyMessage(kind bot.Kind, message msg.Message, args ...interface{}) bool {
identifier := args[0].(string) identifier := args[0].(string)
if strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Get("Nick", "bot")) { if strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Get("Nick", "bot")) {

View File

@ -102,8 +102,3 @@ func (p *RSSPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}
p.Bot.Send(bot.Message, message.Channel, "try '!rss http://rss.cnn.com/rss/edition.rss'") p.Bot.Send(bot.Message, message.Channel, "try '!rss http://rss.cnn.com/rss/edition.rss'")
return true return true
} }
// Register any web URLs desired
func (p *RSSPlugin) RegisterWeb() *string {
return nil
}

View File

@ -187,10 +187,6 @@ func (p *SisyphusPlugin) help(kind bot.Kind, message msg.Message, args ...interf
return true return true
} }
func (p *SisyphusPlugin) RegisterWeb() *string {
return nil
}
func (p *SisyphusPlugin) replyMessage(kind bot.Kind, message msg.Message, args ...interface{}) bool { func (p *SisyphusPlugin) replyMessage(kind bot.Kind, message msg.Message, args ...interface{}) bool {
identifier := args[0].(string) identifier := args[0].(string)
if strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Get("Nick", "bot")) { if strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Get("Nick", "bot")) {

View File

@ -87,8 +87,3 @@ func (p *TalkerPlugin) help(kind bot.Kind, message msg.Message, args ...interfac
p.Bot.Send(bot.Message, message.Channel, "Hi, this is talker. I like to talk about FredFelps!") p.Bot.Send(bot.Message, message.Channel, "Hi, this is talker. I like to talk about FredFelps!")
return true return true
} }
// Register any web URLs desired
func (p *TalkerPlugin) RegisterWeb() *string {
return nil
}

View File

@ -81,10 +81,3 @@ func TestHelp(t *testing.T) {
c.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) c.help(bot.Help, msg.Message{Channel: "channel"}, []string{})
assert.Len(t, mb.Messages, 1) assert.Len(t, mb.Messages, 1)
} }
func TestRegisterWeb(t *testing.T) {
mb := bot.NewMockBot()
c := New(mb)
assert.NotNil(t, c)
assert.Nil(t, c.RegisterWeb())
}

View File

@ -41,5 +41,3 @@ func (t *TellPlugin) message(kind bot.Kind, message msg.Message, args ...interfa
} }
return false return false
} }
func (t *TellPlugin) RegisterWeb() *string { return nil }

View File

@ -71,13 +71,13 @@ func New(b bot.Bot) *TwitchPlugin {
} }
b.Register(p, bot.Message, p.message) b.Register(p, bot.Message, p.message)
p.registerWeb()
return p return p
} }
func (p *TwitchPlugin) RegisterWeb() *string { func (p *TwitchPlugin) registerWeb() {
http.HandleFunc("/isstreaming/", p.serveStreaming) http.HandleFunc("/isstreaming/", p.serveStreaming)
tmp := "/isstreaming"
return &tmp
} }
func (p *TwitchPlugin) serveStreaming(w http.ResponseWriter, r *http.Request) { func (p *TwitchPlugin) serveStreaming(w http.ResponseWriter, r *http.Request) {

View File

@ -57,8 +57,3 @@ func (p *YourPlugin) help(kind bot.Kind, message msg.Message, args ...interface{
p.bot.Send(bot.Message, message.Channel, "Your corrects people's grammar.") p.bot.Send(bot.Message, message.Channel, "Your corrects people's grammar.")
return true return true
} }
// Register any web URLs desired
func (p *YourPlugin) RegisterWeb() *string {
return nil
}

View File

@ -120,5 +120,3 @@ func (p *ZorkPlugin) help(kind bot.Kind, message msg.Message, args ...interface{
p.bot.Send(bot.Message, message.Channel, "Play zork using 'zork <zork command>'.") p.bot.Send(bot.Message, message.Channel, "Play zork using 'zork <zork command>'.")
return true return true
} }
func (p *ZorkPlugin) RegisterWeb() *string { return nil }