From a20839cdd7d71fb0d7fea52f877c8325d831bd39 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Thu, 7 Feb 2019 11:30:42 -0500 Subject: [PATCH] bot: Invert RegisterWeb --- bot/bot.go | 7 ++++--- bot/interfaces.go | 4 ++-- bot/mock.go | 4 ++++ plugins/admin/admin.go | 5 ----- plugins/babbler/babbler.go | 4 ---- plugins/babbler/babbler_test.go | 7 ------- plugins/beers/beers.go | 5 ----- plugins/beers/beers_test.go | 5 ----- plugins/couldashouldawoulda/csw.go | 4 ---- plugins/counter/counter.go | 5 ----- plugins/counter/counter_test.go | 6 ------ plugins/db/db.go | 8 ++++---- plugins/dice/dice.go | 5 ----- plugins/dice/dice_test.go | 7 ------- plugins/emojifyme/emojifyme.go | 4 ---- plugins/fact/factoid.go | 7 ++++--- plugins/fact/remember.go | 5 ----- plugins/first/first.go | 5 ----- plugins/inventory/inventory.go | 5 ----- plugins/leftpad/leftpad.go | 5 ----- plugins/leftpad/leftpad_test.go | 5 ----- plugins/nerdepedia/nerdepedia.go | 5 ----- plugins/picker/picker.go | 7 ------- plugins/plugins.go | 12 ------------ plugins/reaction/reaction.go | 4 ---- plugins/reminder/reminder.go | 4 ---- plugins/reminder/reminder_test.go | 6 ------ plugins/rpgORdie/rpgORdie.go | 4 ---- plugins/rss/rss.go | 5 ----- plugins/sisyphus/sisyphus.go | 4 ---- plugins/talker/talker.go | 5 ----- plugins/talker/talker_test.go | 7 ------- plugins/tell/tell.go | 2 -- plugins/twitch/twitch.go | 6 +++--- plugins/your/your.go | 5 ----- plugins/zork/zork.go | 2 -- 36 files changed, 21 insertions(+), 169 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index c5b237d..c12d1bb 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -116,9 +116,6 @@ func (b *bot) AddPlugin(h Plugin) { name := reflect.TypeOf(h).String() b.plugins[name] = h b.pluginOrdering = append(b.pluginOrdering, name) - if entry := h.RegisterWeb(); entry != nil { - b.httpEndPoints[name] = *entry - } } 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) } + +func (b *bot) RegisterWeb(root, name string) { + b.httpEndPoints[name] = root +} diff --git a/bot/interfaces.go b/bot/interfaces.go index eb318ca..53d5aa3 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -59,6 +59,7 @@ type Bot interface { CheckAdmin(string) bool GetEmojiList() map[string]string RegisterFilter(string, func(string) string) + RegisterWeb(string, string) } // 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 -// Probably can disappear once RegisterWeb gets inverted +// Uhh it turned empty, but we're still using it to ID plugins type Plugin interface { - RegisterWeb() *string } diff --git a/bot/mock.go b/bot/mock.go index ec48201..f9f2917 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -5,6 +5,7 @@ package bot import ( "fmt" "log" + "net/http" "strconv" "strings" @@ -48,6 +49,7 @@ func (mb *MockBot) Send(kind Kind, args ...interface{}) (string, error) { } func (mb *MockBot) AddPlugin(f Plugin) {} 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) Filter(msg msg.Message, s string) string { return s } func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil } @@ -99,5 +101,7 @@ func NewMockBot() *MockBot { Messages: 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 } diff --git a/plugins/admin/admin.go b/plugins/admin/admin.go index 5de34f0..98b439f 100644 --- a/plugins/admin/admin.go +++ b/plugins/admin/admin.go @@ -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.") return true } - -// Register any web URLs desired -func (p *AdminPlugin) RegisterWeb() *string { - return nil -} diff --git a/plugins/babbler/babbler.go b/plugins/babbler/babbler.go index efc0c63..f95782f 100644 --- a/plugins/babbler/babbler.go +++ b/plugins/babbler/babbler.go @@ -162,10 +162,6 @@ func (p *BabblerPlugin) help(kind bot.Kind, msg msg.Message, args ...interface{} return true } -func (p *BabblerPlugin) RegisterWeb() *string { - return nil -} - func (p *BabblerPlugin) makeBabbler(name string) (*Babbler, error) { res, err := p.db.Exec(`insert into babblers (babbler) values (?);`, name) if err == nil { diff --git a/plugins/babbler/babbler_test.go b/plugins/babbler/babbler_test.go index 1631257..a569e0d 100644 --- a/plugins/babbler/babbler_test.go +++ b/plugins/babbler/babbler_test.go @@ -312,10 +312,3 @@ func TestHelp(t *testing.T) { bp.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) 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()) -} diff --git a/plugins/beers/beers.go b/plugins/beers/beers.go index 3ed679a..3ad3dee 100644 --- a/plugins/beers/beers.go +++ b/plugins/beers/beers.go @@ -434,8 +434,3 @@ func (p *BeersPlugin) untappdLoop(channel string) { p.checkUntappd(channel) } } - -// Register any web URLs desired -func (p BeersPlugin) RegisterWeb() *string { - return nil -} diff --git a/plugins/beers/beers_test.go b/plugins/beers/beers_test.go index bb34008..4fa07b1 100644 --- a/plugins/beers/beers_test.go +++ b/plugins/beers/beers_test.go @@ -124,8 +124,3 @@ func TestHelp(t *testing.T) { b.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) assert.Len(t, mb.Messages, 1) } - -func TestRegisterWeb(t *testing.T) { - b, _ := makeBeersPlugin(t) - assert.Nil(t, b.RegisterWeb()) -} diff --git a/plugins/couldashouldawoulda/csw.go b/plugins/couldashouldawoulda/csw.go index 9d2d7a3..0052a5e 100644 --- a/plugins/couldashouldawoulda/csw.go +++ b/plugins/couldashouldawoulda/csw.go @@ -71,7 +71,3 @@ func (p *CSWPlugin) message(kind bot.Kind, message msg.Message, args ...interfac return false } - -func (p *CSWPlugin) RegisterWeb() *string { - return nil -} diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index 6d0e0ed..b23c4ae 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -455,11 +455,6 @@ func (p *CounterPlugin) help(kind bot.Kind, message msg.Message, args ...interfa return true } -// Register any web URLs desired -func (p *CounterPlugin) RegisterWeb() *string { - return nil -} - func (p *CounterPlugin) checkMatch(message msg.Message) bool { nick := message.User.Name channel := message.Channel diff --git a/plugins/counter/counter_test.go b/plugins/counter/counter_test.go index 1cbec0c..417c44c 100644 --- a/plugins/counter/counter_test.go +++ b/plugins/counter/counter_test.go @@ -253,9 +253,3 @@ func TestHelp(t *testing.T) { c.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) assert.Len(t, mb.Messages, 1) } - -func TestRegisterWeb(t *testing.T) { - _, c := setup(t) - assert.NotNil(t, c) - assert.Nil(t, c.RegisterWeb()) -} diff --git a/plugins/db/db.go b/plugins/db/db.go index 4bf8387..026bf76 100644 --- a/plugins/db/db.go +++ b/plugins/db/db.go @@ -18,7 +18,9 @@ type DBPlugin struct { } 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 } @@ -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) Help(channel string, parts []string) {} -func (p *DBPlugin) RegisterWeb() *string { +func (p *DBPlugin) registerWeb() { http.HandleFunc("/db/catbase.db", p.serveQuery) - tmp := "/db/catbase.db" - return &tmp } func (p *DBPlugin) serveQuery(w http.ResponseWriter, r *http.Request) { diff --git a/plugins/dice/dice.go b/plugins/dice/dice.go index 721a652..dafc715 100644 --- a/plugins/dice/dice.go +++ b/plugins/dice/dice.go @@ -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\".") return true } - -// Register any web URLs desired -func (p *DicePlugin) RegisterWeb() *string { - return nil -} diff --git a/plugins/dice/dice_test.go b/plugins/dice/dice_test.go index bcb7a26..b7f2961 100644 --- a/plugins/dice/dice_test.go +++ b/plugins/dice/dice_test.go @@ -89,10 +89,3 @@ func TestHelp(t *testing.T) { c.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) 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()) -} diff --git a/plugins/emojifyme/emojifyme.go b/plugins/emojifyme/emojifyme.go index 03b7fbb..3419e4c 100644 --- a/plugins/emojifyme/emojifyme.go +++ b/plugins/emojifyme/emojifyme.go @@ -99,10 +99,6 @@ func (p *EmojifyMePlugin) message(kind bot.Kind, message msg.Message, args ...in return false } -func (p *EmojifyMePlugin) RegisterWeb() *string { - return nil -} - func stringsContain(haystack []string, needle string) bool { for _, s := range haystack { if s == needle { diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index b8b40c2..d7479a9 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -317,6 +317,8 @@ func New(botInst bot.Bot) *Factoid { botInst.Register(p, bot.Message, p.message) botInst.Register(p, bot.Help, p.help) + p.registerWeb() + return p } @@ -737,11 +739,10 @@ func (p *Factoid) factTimer(channel string) { } // Register any web URLs desired -func (p *Factoid) RegisterWeb() *string { +func (p *Factoid) registerWeb() { http.HandleFunc("/factoid/req", p.serveQuery) http.HandleFunc("/factoid", p.serveQuery) - tmp := "/factoid" - return &tmp + p.Bot.RegisterWeb("/factoid", "Factoid") } func linkify(text string) template.HTML { diff --git a/plugins/fact/remember.go b/plugins/fact/remember.go index 87485f1..8aa32da 100644 --- a/plugins/fact/remember.go +++ b/plugins/fact/remember.go @@ -153,11 +153,6 @@ func (p *RememberPlugin) randQuote() string { return f.Tidbit } -// Register any web URLs desired -func (p RememberPlugin) RegisterWeb() *string { - return nil -} - func (p *RememberPlugin) recordMsg(message msg.Message) { log.Printf("Logging message: %s: %s", message.User.Name, message.Body) p.Log[message.Channel] = append(p.Log[message.Channel], message) diff --git a/plugins/first/first.go b/plugins/first/first.go index 9771dbf..88fcb47 100644 --- a/plugins/first/first.go +++ b/plugins/first/first.go @@ -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.") return true } - -// Register any web URLs desired -func (p *FirstPlugin) RegisterWeb() *string { - return nil -} diff --git a/plugins/inventory/inventory.go b/plugins/inventory/inventory.go index 50fb6f3..c13ac9e 100644 --- a/plugins/inventory/inventory.go +++ b/plugins/inventory/inventory.go @@ -224,8 +224,3 @@ func checkerr(e error) { log.Println(e) } } - -func (p *InventoryPlugin) RegisterWeb() *string { - // nothing to register - return nil -} diff --git a/plugins/leftpad/leftpad.go b/plugins/leftpad/leftpad.go index e005b46..23847f2 100644 --- a/plugins/leftpad/leftpad.go +++ b/plugins/leftpad/leftpad.go @@ -62,8 +62,3 @@ func (p *LeftpadPlugin) message(kind bot.Kind, message msg.Message, args ...inte return false } - -func (p *LeftpadPlugin) RegisterWeb() *string { - // nothing to register - return nil -} diff --git a/plugins/leftpad/leftpad_test.go b/plugins/leftpad/leftpad_test.go index 1d97488..32b9cdc 100644 --- a/plugins/leftpad/leftpad_test.go +++ b/plugins/leftpad/leftpad_test.go @@ -85,8 +85,3 @@ func TestNotPadding(t *testing.T) { p.message(makeMessage("!lololol")) assert.Len(t, mb.Messages, 0) } - -func TestRegisterWeb(t *testing.T) { - p, _ := makePlugin(t) - assert.Nil(t, p.RegisterWeb()) -} diff --git a/plugins/nerdepedia/nerdepedia.go b/plugins/nerdepedia/nerdepedia.go index eb040a7..6c0ba3c 100644 --- a/plugins/nerdepedia/nerdepedia.go +++ b/plugins/nerdepedia/nerdepedia.go @@ -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") return true } - -// Register any web URLs desired -func (p *NerdepediaPlugin) RegisterWeb() *string { - return nil -} diff --git a/plugins/picker/picker.go b/plugins/picker/picker.go index 3a1c320..b7290d4 100644 --- a/plugins/picker/picker.go +++ b/plugins/picker/picker.go @@ -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}\".") 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 } diff --git a/plugins/plugins.go b/plugins/plugins.go index 364629f..b6a4fd5 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -1,15 +1,3 @@ // © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors. 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() -} diff --git a/plugins/reaction/reaction.go b/plugins/reaction/reaction.go index c964022..3dcd078 100644 --- a/plugins/reaction/reaction.go +++ b/plugins/reaction/reaction.go @@ -63,7 +63,3 @@ func (p *ReactionPlugin) message(kind bot.Kind, message msg.Message, args ...int return false } - -func (p *ReactionPlugin) RegisterWeb() *string { - return nil -} diff --git a/plugins/reminder/reminder.go b/plugins/reminder/reminder.go index 93d4dec..26078a8 100644 --- a/plugins/reminder/reminder.go +++ b/plugins/reminder/reminder.go @@ -200,10 +200,6 @@ func (p *ReminderPlugin) help(kind bot.Kind, message msg.Message, args ...interf return true } -func (p *ReminderPlugin) RegisterWeb() *string { - return nil -} - func (p *ReminderPlugin) getNextReminder() *Reminder { p.mutex.Lock() defer p.mutex.Unlock() diff --git a/plugins/reminder/reminder_test.go b/plugins/reminder/reminder_test.go index c4a4e9e..3618f16 100644 --- a/plugins/reminder/reminder_test.go +++ b/plugins/reminder/reminder_test.go @@ -226,9 +226,3 @@ func TestHelp(t *testing.T) { c.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) assert.Len(t, mb.Messages, 1) } - -func TestRegisterWeb(t *testing.T) { - c, _ := setup(t) - assert.NotNil(t, c) - assert.Nil(t, c.RegisterWeb()) -} diff --git a/plugins/rpgORdie/rpgORdie.go b/plugins/rpgORdie/rpgORdie.go index 8b5a40e..1035a06 100644 --- a/plugins/rpgORdie/rpgORdie.go +++ b/plugins/rpgORdie/rpgORdie.go @@ -124,10 +124,6 @@ func (p *RPGPlugin) help(kind bot.Kind, message msg.Message, args ...interface{} return true } -func (p *RPGPlugin) RegisterWeb() *string { - return nil -} - func (p *RPGPlugin) replyMessage(kind bot.Kind, message msg.Message, args ...interface{}) bool { identifier := args[0].(string) if strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Get("Nick", "bot")) { diff --git a/plugins/rss/rss.go b/plugins/rss/rss.go index b25d749..811f37f 100644 --- a/plugins/rss/rss.go +++ b/plugins/rss/rss.go @@ -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'") return true } - -// Register any web URLs desired -func (p *RSSPlugin) RegisterWeb() *string { - return nil -} diff --git a/plugins/sisyphus/sisyphus.go b/plugins/sisyphus/sisyphus.go index 5a0a8b8..31c3f02 100644 --- a/plugins/sisyphus/sisyphus.go +++ b/plugins/sisyphus/sisyphus.go @@ -187,10 +187,6 @@ func (p *SisyphusPlugin) help(kind bot.Kind, message msg.Message, args ...interf return true } -func (p *SisyphusPlugin) RegisterWeb() *string { - return nil -} - func (p *SisyphusPlugin) replyMessage(kind bot.Kind, message msg.Message, args ...interface{}) bool { identifier := args[0].(string) if strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Get("Nick", "bot")) { diff --git a/plugins/talker/talker.go b/plugins/talker/talker.go index 46ba61a..788161c 100644 --- a/plugins/talker/talker.go +++ b/plugins/talker/talker.go @@ -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!") return true } - -// Register any web URLs desired -func (p *TalkerPlugin) RegisterWeb() *string { - return nil -} diff --git a/plugins/talker/talker_test.go b/plugins/talker/talker_test.go index 2408f45..fe65779 100644 --- a/plugins/talker/talker_test.go +++ b/plugins/talker/talker_test.go @@ -81,10 +81,3 @@ func TestHelp(t *testing.T) { c.help(bot.Help, msg.Message{Channel: "channel"}, []string{}) 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()) -} diff --git a/plugins/tell/tell.go b/plugins/tell/tell.go index c852141..aba8410 100644 --- a/plugins/tell/tell.go +++ b/plugins/tell/tell.go @@ -41,5 +41,3 @@ func (t *TellPlugin) message(kind bot.Kind, message msg.Message, args ...interfa } return false } - -func (t *TellPlugin) RegisterWeb() *string { return nil } diff --git a/plugins/twitch/twitch.go b/plugins/twitch/twitch.go index c748cad..a088d40 100644 --- a/plugins/twitch/twitch.go +++ b/plugins/twitch/twitch.go @@ -71,13 +71,13 @@ func New(b bot.Bot) *TwitchPlugin { } b.Register(p, bot.Message, p.message) + p.registerWeb() + return p } -func (p *TwitchPlugin) RegisterWeb() *string { +func (p *TwitchPlugin) registerWeb() { http.HandleFunc("/isstreaming/", p.serveStreaming) - tmp := "/isstreaming" - return &tmp } func (p *TwitchPlugin) serveStreaming(w http.ResponseWriter, r *http.Request) { diff --git a/plugins/your/your.go b/plugins/your/your.go index a319039..1d26e9d 100644 --- a/plugins/your/your.go +++ b/plugins/your/your.go @@ -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.") return true } - -// Register any web URLs desired -func (p *YourPlugin) RegisterWeb() *string { - return nil -} diff --git a/plugins/zork/zork.go b/plugins/zork/zork.go index b5ac682..d2fda1b 100644 --- a/plugins/zork/zork.go +++ b/plugins/zork/zork.go @@ -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 '.") return true } - -func (p *ZorkPlugin) RegisterWeb() *string { return nil }